]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/target/target_core_fabric_lib.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[mirror_ubuntu-jammy-kernel.git] / drivers / target / target_core_fabric_lib.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
c66ac9db
NB
2/*******************************************************************************
3 * Filename: target_core_fabric_lib.c
4 *
5 * This file contains generic high level protocol identifier and PR
6 * handlers for TCM fabric modules
7 *
4c76251e 8 * (c) Copyright 2010-2013 Datera, Inc.
c66ac9db
NB
9 *
10 * Nicholas A. Bellinger <nab@linux-iscsi.org>
11 *
c66ac9db
NB
12 ******************************************************************************/
13
2650d71e
CH
14/*
15 * See SPC4, section 7.5 "Protocol specific parameters" for details
16 * on the formats implemented in this file.
17 */
18
11650b85 19#include <linux/kernel.h>
c66ac9db
NB
20#include <linux/string.h>
21#include <linux/ctype.h>
22#include <linux/spinlock.h>
c53181af 23#include <linux/export.h>
a85d667e 24#include <asm/unaligned.h>
c66ac9db 25
6546a02a
SR
26#include <scsi/scsi_proto.h>
27
c66ac9db 28#include <target/target_core_base.h>
c4795fb2 29#include <target/target_core_fabric.h>
c66ac9db 30
e26d99ae 31#include "target_core_internal.h"
c66ac9db
NB
32#include "target_core_pr.h"
33
c66ac9db 34
2650d71e
CH
35static int sas_get_pr_transport_id(
36 struct se_node_acl *nacl,
c66ac9db
NB
37 int *format_code,
38 unsigned char *buf)
39{
8c35ad20 40 int ret;
11650b85 41
2650d71e
CH
42 /* Skip over 'naa. prefix */
43 ret = hex2bin(&buf[4], &nacl->initiatorname[4], 8);
44 if (ret) {
45 pr_debug("%s: invalid hex string\n", __func__);
46 return ret;
47 }
c66ac9db 48
c66ac9db
NB
49 return 24;
50}
c66ac9db 51
2650d71e 52static int fc_get_pr_transport_id(
c66ac9db 53 struct se_node_acl *se_nacl,
c66ac9db
NB
54 int *format_code,
55 unsigned char *buf)
56{
11650b85 57 unsigned char *ptr;
8c35ad20 58 int i, ret;
c66ac9db 59 u32 off = 8;
8c35ad20 60
c66ac9db 61 /*
c66ac9db
NB
62 * We convert the ASCII formatted N Port name into a binary
63 * encoded TransportID.
64 */
65 ptr = &se_nacl->initiatorname[0];
8fed04eb 66 for (i = 0; i < 23; ) {
6708bb27 67 if (!strncmp(&ptr[i], ":", 1)) {
c66ac9db
NB
68 i++;
69 continue;
70 }
8c35ad20 71 ret = hex2bin(&buf[off++], &ptr[i], 1);
2650d71e
CH
72 if (ret < 0) {
73 pr_debug("%s: invalid hex string\n", __func__);
74 return ret;
75 }
c66ac9db
NB
76 i += 2;
77 }
78 /*
79 * The FC Transport ID is a hardcoded 24-byte length
80 */
81 return 24;
82}
c66ac9db 83
2650d71e
CH
84static int sbp_get_pr_transport_id(
85 struct se_node_acl *nacl,
86 int *format_code,
87 unsigned char *buf)
c66ac9db 88{
2650d71e 89 int ret;
c66ac9db 90
2650d71e
CH
91 ret = hex2bin(&buf[8], nacl->initiatorname, 8);
92 if (ret) {
93 pr_debug("%s: invalid hex string\n", __func__);
94 return ret;
95 }
c66ac9db 96
2650d71e 97 return 24;
c66ac9db 98}
c66ac9db 99
2650d71e
CH
100static int srp_get_pr_transport_id(
101 struct se_node_acl *nacl,
102 int *format_code,
103 unsigned char *buf)
c66ac9db 104{
2650d71e
CH
105 const char *p;
106 unsigned len, count, leading_zero_bytes;
107 int rc;
108
109 p = nacl->initiatorname;
110 if (strncasecmp(p, "0x", 2) == 0)
111 p += 2;
112 len = strlen(p);
113 if (len % 2)
114 return -EINVAL;
115
116 count = min(len / 2, 16U);
117 leading_zero_bytes = 16 - count;
118 memset(buf + 8, 0, leading_zero_bytes);
119 rc = hex2bin(buf + 8 + leading_zero_bytes, p, count);
120 if (rc < 0) {
c941e0d1 121 pr_debug("hex2bin failed for %s: %d\n", p, rc);
2650d71e
CH
122 return rc;
123 }
124
125 return 24;
c66ac9db 126}
c66ac9db 127
2650d71e 128static int iscsi_get_pr_transport_id(
c66ac9db
NB
129 struct se_node_acl *se_nacl,
130 struct t10_pr_registration *pr_reg,
131 int *format_code,
132 unsigned char *buf)
133{
134 u32 off = 4, padding = 0;
135 u16 len = 0;
136
137 spin_lock_irq(&se_nacl->nacl_sess_lock);
c66ac9db
NB
138 /*
139 * From spc4r17 Section 7.5.4.6: TransportID for initiator
140 * ports using SCSI over iSCSI.
141 *
142 * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
143 * shall contain the iSCSI name of an iSCSI initiator node (see
144 * RFC 3720). The first ISCSI NAME field byte containing an ASCII
145 * null character terminates the ISCSI NAME field without regard for
146 * the specified length of the iSCSI TransportID or the contents of
147 * the ADDITIONAL LENGTH field.
148 */
149 len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
150 /*
151 * Add Extra byte for NULL terminator
152 */
153 len++;
154 /*
155 * If there is ISID present with the registration and *format code == 1
156 * 1, use iSCSI Initiator port TransportID format.
157 *
158 * Otherwise use iSCSI Initiator device TransportID format that
159 * does not contain the ASCII encoded iSCSI Initiator iSID value
160 * provied by the iSCSi Initiator during the iSCSI login process.
161 */
162 if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
163 /*
164 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
165 * format.
166 */
167 buf[0] |= 0x40;
168 /*
169 * From spc4r17 Section 7.5.4.6: TransportID for initiator
170 * ports using SCSI over iSCSI. Table 390
171 *
172 * The SEPARATOR field shall contain the five ASCII
173 * characters ",i,0x".
174 *
175 * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
176 * field shall contain the iSCSI initiator session identifier
177 * (see RFC 3720) in the form of ASCII characters that are the
178 * hexadecimal digits converted from the binary iSCSI initiator
179 * session identifier value. The first ISCSI INITIATOR SESSION
180 * ID field byte containing an ASCII null character
181 */
182 buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
183 buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
184 buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
185 buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
186 buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
187 len += 5;
188 buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
189 buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
190 buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
191 buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
192 buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
193 buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
194 buf[off+len] = '\0'; off++;
195 len += 7;
196 }
197 spin_unlock_irq(&se_nacl->nacl_sess_lock);
198 /*
199 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
200 * in the TransportID. The additional length shall be at least 20 and
201 * shall be a multiple of four.
202 */
203 padding = ((-len) & 3);
204 if (padding != 0)
205 len += padding;
206
a85d667e 207 put_unaligned_be16(len, &buf[2]);
c66ac9db
NB
208 /*
209 * Increment value for total payload + header length for
210 * full status descriptor
211 */
212 len += 4;
213
214 return len;
215}
c66ac9db 216
2650d71e 217static int iscsi_get_pr_transport_id_len(
c66ac9db
NB
218 struct se_node_acl *se_nacl,
219 struct t10_pr_registration *pr_reg,
220 int *format_code)
221{
222 u32 len = 0, padding = 0;
223
224 spin_lock_irq(&se_nacl->nacl_sess_lock);
225 len = strlen(se_nacl->initiatorname);
226 /*
227 * Add extra byte for NULL terminator
228 */
229 len++;
230 /*
231 * If there is ISID present with the registration, use format code:
232 * 01b: iSCSI Initiator port TransportID format
233 *
234 * If there is not an active iSCSI session, use format code:
235 * 00b: iSCSI Initiator device TransportID format
236 */
237 if (pr_reg->isid_present_at_reg) {
35d1efe8 238 len += 5; /* For ",i,0x" ASCII separator */
c66ac9db
NB
239 len += 7; /* For iSCSI Initiator Session ID + Null terminator */
240 *format_code = 1;
241 } else
242 *format_code = 0;
243 spin_unlock_irq(&se_nacl->nacl_sess_lock);
244 /*
245 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
246 * in the TransportID. The additional length shall be at least 20 and
247 * shall be a multiple of four.
248 */
249 padding = ((-len) & 3);
250 if (padding != 0)
251 len += padding;
252 /*
253 * Increment value for total payload + header length for
254 * full status descriptor
255 */
256 len += 4;
257
258 return len;
259}
c66ac9db 260
2650d71e 261static char *iscsi_parse_pr_out_transport_id(
c66ac9db 262 struct se_portal_group *se_tpg,
094bb5d7 263 char *buf,
c66ac9db
NB
264 u32 *out_tid_len,
265 char **port_nexus_ptr)
266{
267 char *p;
268 u32 tid_len, padding;
269 int i;
270 u16 add_len;
271 u8 format_code = (buf[0] & 0xc0);
272 /*
273 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
274 *
275 * TransportID for initiator ports using SCSI over iSCSI,
276 * from Table 388 -- iSCSI TransportID formats.
277 *
278 * 00b Initiator port is identified using the world wide unique
279 * SCSI device name of the iSCSI initiator
280 * device containing the initiator port (see table 389).
281 * 01b Initiator port is identified using the world wide unique
282 * initiator port identifier (see table 390).10b to 11b
283 * Reserved
284 */
285 if ((format_code != 0x00) && (format_code != 0x40)) {
6708bb27 286 pr_err("Illegal format code: 0x%02x for iSCSI"
c66ac9db
NB
287 " Initiator Transport ID\n", format_code);
288 return NULL;
289 }
290 /*
291 * If the caller wants the TransportID Length, we set that value for the
292 * entire iSCSI Tarnsport ID now.
293 */
68edbce4
JE
294 if (out_tid_len) {
295 /* The shift works thanks to integer promotion rules */
a85d667e 296 add_len = get_unaligned_be16(&buf[2]);
c66ac9db 297
8359cf43 298 tid_len = strlen(&buf[4]);
c66ac9db
NB
299 tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
300 tid_len += 1; /* Add one byte for NULL terminator */
301 padding = ((-tid_len) & 3);
302 if (padding != 0)
303 tid_len += padding;
304
305 if ((add_len + 4) != tid_len) {
6708bb27 306 pr_debug("LIO-Target Extracted add_len: %hu "
c66ac9db
NB
307 "does not match calculated tid_len: %u,"
308 " using tid_len instead\n", add_len+4, tid_len);
309 *out_tid_len = tid_len;
310 } else
311 *out_tid_len = (add_len + 4);
312 }
313 /*
35d1efe8 314 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
c66ac9db
NB
315 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
316 * format.
317 */
318 if (format_code == 0x40) {
8359cf43 319 p = strstr(&buf[4], ",i,0x");
6708bb27 320 if (!p) {
35d1efe8 321 pr_err("Unable to locate \",i,0x\" separator"
c66ac9db 322 " for Initiator port identifier: %s\n",
8359cf43 323 &buf[4]);
c66ac9db
NB
324 return NULL;
325 }
326 *p = '\0'; /* Terminate iSCSI Name */
35d1efe8 327 p += 5; /* Skip over ",i,0x" separator */
c66ac9db
NB
328
329 *port_nexus_ptr = p;
330 /*
331 * Go ahead and do the lower case conversion of the received
332 * 12 ASCII characters representing the ISID in the TransportID
25985edc 333 * for comparison against the running iSCSI session's ISID from
c66ac9db
NB
334 * iscsi_target.c:lio_sess_get_initiator_sid()
335 */
336 for (i = 0; i < 12; i++) {
337 if (isdigit(*p)) {
338 p++;
339 continue;
340 }
341 *p = tolower(*p);
342 p++;
343 }
13ef143d
BS
344 } else
345 *port_nexus_ptr = NULL;
c66ac9db 346
094bb5d7 347 return &buf[4];
c66ac9db 348}
2650d71e
CH
349
350int target_get_pr_transport_id_len(struct se_node_acl *nacl,
351 struct t10_pr_registration *pr_reg, int *format_code)
352{
353 switch (nacl->se_tpg->proto_id) {
354 case SCSI_PROTOCOL_FCP:
355 case SCSI_PROTOCOL_SBP:
356 case SCSI_PROTOCOL_SRP:
357 case SCSI_PROTOCOL_SAS:
358 break;
359 case SCSI_PROTOCOL_ISCSI:
360 return iscsi_get_pr_transport_id_len(nacl, pr_reg, format_code);
361 default:
362 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
363 return -EINVAL;
364 }
365
366 /*
367 * Most transports use a fixed length 24 byte identifier.
368 */
369 *format_code = 0;
370 return 24;
371}
372
373int target_get_pr_transport_id(struct se_node_acl *nacl,
374 struct t10_pr_registration *pr_reg, int *format_code,
375 unsigned char *buf)
376{
377 switch (nacl->se_tpg->proto_id) {
378 case SCSI_PROTOCOL_SAS:
379 return sas_get_pr_transport_id(nacl, format_code, buf);
380 case SCSI_PROTOCOL_SBP:
381 return sbp_get_pr_transport_id(nacl, format_code, buf);
382 case SCSI_PROTOCOL_SRP:
383 return srp_get_pr_transport_id(nacl, format_code, buf);
384 case SCSI_PROTOCOL_FCP:
385 return fc_get_pr_transport_id(nacl, format_code, buf);
386 case SCSI_PROTOCOL_ISCSI:
387 return iscsi_get_pr_transport_id(nacl, pr_reg, format_code,
388 buf);
389 default:
390 pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
391 return -EINVAL;
392 }
393}
394
395const char *target_parse_pr_out_transport_id(struct se_portal_group *tpg,
094bb5d7 396 char *buf, u32 *out_tid_len, char **port_nexus_ptr)
2650d71e
CH
397{
398 u32 offset;
399
400 switch (tpg->proto_id) {
401 case SCSI_PROTOCOL_SAS:
402 /*
403 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
404 * for initiator ports using SCSI over SAS Serial SCSI Protocol.
405 */
406 offset = 4;
407 break;
408 case SCSI_PROTOCOL_SBP:
409 case SCSI_PROTOCOL_SRP:
410 case SCSI_PROTOCOL_FCP:
411 offset = 8;
412 break;
413 case SCSI_PROTOCOL_ISCSI:
414 return iscsi_parse_pr_out_transport_id(tpg, buf, out_tid_len,
415 port_nexus_ptr);
416 default:
417 pr_err("Unknown proto_id: 0x%02x\n", tpg->proto_id);
418 return NULL;
419 }
420
421 *port_nexus_ptr = NULL;
422 *out_tid_len = 24;
423 return buf + offset;
424}