Merge tag 'please-pull-ia64-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / target / target_core_spc.c
1 /*
2 * SCSI Primary Commands (SPC) parsing and emulation.
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
5 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
6 * Copyright (c) 2007-2010 Rising Tide Systems
7 * Copyright (c) 2008-2010 Linux-iSCSI.org
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/unaligned.h>
29
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_tcq.h>
32
33 #include <target/target_core_base.h>
34 #include <target/target_core_backend.h>
35 #include <target/target_core_fabric.h>
36
37 #include "target_core_internal.h"
38 #include "target_core_alua.h"
39 #include "target_core_pr.h"
40 #include "target_core_ua.h"
41
42
43 static void spc_fill_alua_data(struct se_port *port, unsigned char *buf)
44 {
45 struct t10_alua_tg_pt_gp *tg_pt_gp;
46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
47
48 /*
49 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
50 */
51 buf[5] = 0x80;
52
53 /*
54 * Set TPGS field for explict and/or implict ALUA access type
55 * and opteration.
56 *
57 * See spc4r17 section 6.4.2 Table 135
58 */
59 if (!port)
60 return;
61 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
62 if (!tg_pt_gp_mem)
63 return;
64
65 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
66 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
67 if (tg_pt_gp)
68 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
69 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
70 }
71
72 static int spc_emulate_inquiry_std(struct se_cmd *cmd, char *buf)
73 {
74 struct se_lun *lun = cmd->se_lun;
75 struct se_device *dev = cmd->se_dev;
76
77 /* Set RMB (removable media) for tape devices */
78 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
79 buf[1] = 0x80;
80
81 buf[2] = dev->transport->get_device_rev(dev);
82
83 /*
84 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
85 *
86 * SPC4 says:
87 * A RESPONSE DATA FORMAT field set to 2h indicates that the
88 * standard INQUIRY data is in the format defined in this
89 * standard. Response data format values less than 2h are
90 * obsolete. Response data format values greater than 2h are
91 * reserved.
92 */
93 buf[3] = 2;
94
95 /*
96 * Enable SCCS and TPGS fields for Emulated ALUA
97 */
98 if (dev->se_sub_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED)
99 spc_fill_alua_data(lun->lun_sep, buf);
100
101 buf[7] = 0x2; /* CmdQue=1 */
102
103 snprintf(&buf[8], 8, "LIO-ORG");
104 snprintf(&buf[16], 16, "%s", dev->se_sub_dev->t10_wwn.model);
105 snprintf(&buf[32], 4, "%s", dev->se_sub_dev->t10_wwn.revision);
106 buf[4] = 31; /* Set additional length to 31 */
107
108 return 0;
109 }
110
111 /* unit serial number */
112 static int spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
113 {
114 struct se_device *dev = cmd->se_dev;
115 u16 len = 0;
116
117 if (dev->se_sub_dev->su_dev_flags &
118 SDF_EMULATED_VPD_UNIT_SERIAL) {
119 u32 unit_serial_len;
120
121 unit_serial_len = strlen(dev->se_sub_dev->t10_wwn.unit_serial);
122 unit_serial_len++; /* For NULL Terminator */
123
124 len += sprintf(&buf[4], "%s",
125 dev->se_sub_dev->t10_wwn.unit_serial);
126 len++; /* Extra Byte for NULL Terminator */
127 buf[3] = len;
128 }
129 return 0;
130 }
131
132 static void spc_parse_naa_6h_vendor_specific(struct se_device *dev,
133 unsigned char *buf)
134 {
135 unsigned char *p = &dev->se_sub_dev->t10_wwn.unit_serial[0];
136 int cnt;
137 bool next = true;
138
139 /*
140 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
141 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
142 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
143 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
144 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
145 * per device uniqeness.
146 */
147 for (cnt = 0; *p && cnt < 13; p++) {
148 int val = hex_to_bin(*p);
149
150 if (val < 0)
151 continue;
152
153 if (next) {
154 next = false;
155 buf[cnt++] |= val;
156 } else {
157 next = true;
158 buf[cnt] = val << 4;
159 }
160 }
161 }
162
163 /*
164 * Device identification VPD, for a complete list of
165 * DESIGNATOR TYPEs see spc4r17 Table 459.
166 */
167 static int spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
168 {
169 struct se_device *dev = cmd->se_dev;
170 struct se_lun *lun = cmd->se_lun;
171 struct se_port *port = NULL;
172 struct se_portal_group *tpg = NULL;
173 struct t10_alua_lu_gp_member *lu_gp_mem;
174 struct t10_alua_tg_pt_gp *tg_pt_gp;
175 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
176 unsigned char *prod = &dev->se_sub_dev->t10_wwn.model[0];
177 u32 prod_len;
178 u32 unit_serial_len, off = 0;
179 u16 len = 0, id_len;
180
181 off = 4;
182
183 /*
184 * NAA IEEE Registered Extended Assigned designator format, see
185 * spc4r17 section 7.7.3.6.5
186 *
187 * We depend upon a target_core_mod/ConfigFS provided
188 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
189 * value in order to return the NAA id.
190 */
191 if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
192 goto check_t10_vend_desc;
193
194 /* CODE SET == Binary */
195 buf[off++] = 0x1;
196
197 /* Set ASSOCIATION == addressed logical unit: 0)b */
198 buf[off] = 0x00;
199
200 /* Identifier/Designator type == NAA identifier */
201 buf[off++] |= 0x3;
202 off++;
203
204 /* Identifier/Designator length */
205 buf[off++] = 0x10;
206
207 /*
208 * Start NAA IEEE Registered Extended Identifier/Designator
209 */
210 buf[off++] = (0x6 << 4);
211
212 /*
213 * Use OpenFabrics IEEE Company ID: 00 14 05
214 */
215 buf[off++] = 0x01;
216 buf[off++] = 0x40;
217 buf[off] = (0x5 << 4);
218
219 /*
220 * Return ConfigFS Unit Serial Number information for
221 * VENDOR_SPECIFIC_IDENTIFIER and
222 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
223 */
224 spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
225
226 len = 20;
227 off = (len + 4);
228
229 check_t10_vend_desc:
230 /*
231 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
232 */
233 id_len = 8; /* For Vendor field */
234 prod_len = 4; /* For VPD Header */
235 prod_len += 8; /* For Vendor field */
236 prod_len += strlen(prod);
237 prod_len++; /* For : */
238
239 if (dev->se_sub_dev->su_dev_flags &
240 SDF_EMULATED_VPD_UNIT_SERIAL) {
241 unit_serial_len =
242 strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
243 unit_serial_len++; /* For NULL Terminator */
244
245 id_len += sprintf(&buf[off+12], "%s:%s", prod,
246 &dev->se_sub_dev->t10_wwn.unit_serial[0]);
247 }
248 buf[off] = 0x2; /* ASCII */
249 buf[off+1] = 0x1; /* T10 Vendor ID */
250 buf[off+2] = 0x0;
251 memcpy(&buf[off+4], "LIO-ORG", 8);
252 /* Extra Byte for NULL Terminator */
253 id_len++;
254 /* Identifier Length */
255 buf[off+3] = id_len;
256 /* Header size for Designation descriptor */
257 len += (id_len + 4);
258 off += (id_len + 4);
259 /*
260 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
261 */
262 port = lun->lun_sep;
263 if (port) {
264 struct t10_alua_lu_gp *lu_gp;
265 u32 padding, scsi_name_len;
266 u16 lu_gp_id = 0;
267 u16 tg_pt_gp_id = 0;
268 u16 tpgt;
269
270 tpg = port->sep_tpg;
271 /*
272 * Relative target port identifer, see spc4r17
273 * section 7.7.3.7
274 *
275 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
276 * section 7.5.1 Table 362
277 */
278 buf[off] =
279 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
280 buf[off++] |= 0x1; /* CODE SET == Binary */
281 buf[off] = 0x80; /* Set PIV=1 */
282 /* Set ASSOCIATION == target port: 01b */
283 buf[off] |= 0x10;
284 /* DESIGNATOR TYPE == Relative target port identifer */
285 buf[off++] |= 0x4;
286 off++; /* Skip over Reserved */
287 buf[off++] = 4; /* DESIGNATOR LENGTH */
288 /* Skip over Obsolete field in RTPI payload
289 * in Table 472 */
290 off += 2;
291 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
292 buf[off++] = (port->sep_rtpi & 0xff);
293 len += 8; /* Header size + Designation descriptor */
294 /*
295 * Target port group identifier, see spc4r17
296 * section 7.7.3.8
297 *
298 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
299 * section 7.5.1 Table 362
300 */
301 if (dev->se_sub_dev->t10_alua.alua_type !=
302 SPC3_ALUA_EMULATED)
303 goto check_scsi_name;
304
305 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
306 if (!tg_pt_gp_mem)
307 goto check_lu_gp;
308
309 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
310 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
311 if (!tg_pt_gp) {
312 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
313 goto check_lu_gp;
314 }
315 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
316 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
317
318 buf[off] =
319 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
320 buf[off++] |= 0x1; /* CODE SET == Binary */
321 buf[off] = 0x80; /* Set PIV=1 */
322 /* Set ASSOCIATION == target port: 01b */
323 buf[off] |= 0x10;
324 /* DESIGNATOR TYPE == Target port group identifier */
325 buf[off++] |= 0x5;
326 off++; /* Skip over Reserved */
327 buf[off++] = 4; /* DESIGNATOR LENGTH */
328 off += 2; /* Skip over Reserved Field */
329 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
330 buf[off++] = (tg_pt_gp_id & 0xff);
331 len += 8; /* Header size + Designation descriptor */
332 /*
333 * Logical Unit Group identifier, see spc4r17
334 * section 7.7.3.8
335 */
336 check_lu_gp:
337 lu_gp_mem = dev->dev_alua_lu_gp_mem;
338 if (!lu_gp_mem)
339 goto check_scsi_name;
340
341 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
342 lu_gp = lu_gp_mem->lu_gp;
343 if (!lu_gp) {
344 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
345 goto check_scsi_name;
346 }
347 lu_gp_id = lu_gp->lu_gp_id;
348 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
349
350 buf[off++] |= 0x1; /* CODE SET == Binary */
351 /* DESIGNATOR TYPE == Logical Unit Group identifier */
352 buf[off++] |= 0x6;
353 off++; /* Skip over Reserved */
354 buf[off++] = 4; /* DESIGNATOR LENGTH */
355 off += 2; /* Skip over Reserved Field */
356 buf[off++] = ((lu_gp_id >> 8) & 0xff);
357 buf[off++] = (lu_gp_id & 0xff);
358 len += 8; /* Header size + Designation descriptor */
359 /*
360 * SCSI name string designator, see spc4r17
361 * section 7.7.3.11
362 *
363 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
364 * section 7.5.1 Table 362
365 */
366 check_scsi_name:
367 scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
368 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
369 scsi_name_len += 10;
370 /* Check for 4-byte padding */
371 padding = ((-scsi_name_len) & 3);
372 if (padding != 0)
373 scsi_name_len += padding;
374 /* Header size + Designation descriptor */
375 scsi_name_len += 4;
376
377 buf[off] =
378 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
379 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
380 buf[off] = 0x80; /* Set PIV=1 */
381 /* Set ASSOCIATION == target port: 01b */
382 buf[off] |= 0x10;
383 /* DESIGNATOR TYPE == SCSI name string */
384 buf[off++] |= 0x8;
385 off += 2; /* Skip over Reserved and length */
386 /*
387 * SCSI name string identifer containing, $FABRIC_MOD
388 * dependent information. For LIO-Target and iSCSI
389 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
390 * UTF-8 encoding.
391 */
392 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
393 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
394 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
395 scsi_name_len += 1 /* Include NULL terminator */;
396 /*
397 * The null-terminated, null-padded (see 4.4.2) SCSI
398 * NAME STRING field contains a UTF-8 format string.
399 * The number of bytes in the SCSI NAME STRING field
400 * (i.e., the value in the DESIGNATOR LENGTH field)
401 * shall be no larger than 256 and shall be a multiple
402 * of four.
403 */
404 if (padding)
405 scsi_name_len += padding;
406
407 buf[off-1] = scsi_name_len;
408 off += scsi_name_len;
409 /* Header size + Designation descriptor */
410 len += (scsi_name_len + 4);
411 }
412 buf[2] = ((len >> 8) & 0xff);
413 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
414 return 0;
415 }
416
417 /* Extended INQUIRY Data VPD Page */
418 static int spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
419 {
420 buf[3] = 0x3c;
421 /* Set HEADSUP, ORDSUP, SIMPSUP */
422 buf[5] = 0x07;
423
424 /* If WriteCache emulation is enabled, set V_SUP */
425 if (cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
426 buf[6] = 0x01;
427 return 0;
428 }
429
430 /* Block Limits VPD page */
431 static int spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
432 {
433 struct se_device *dev = cmd->se_dev;
434 u32 max_sectors;
435 int have_tp = 0;
436
437 /*
438 * Following spc3r22 section 6.5.3 Block Limits VPD page, when
439 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
440 * different page length for Thin Provisioning.
441 */
442 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
443 have_tp = 1;
444
445 buf[0] = dev->transport->get_device_type(dev);
446 buf[3] = have_tp ? 0x3c : 0x10;
447
448 /* Set WSNZ to 1 */
449 buf[4] = 0x01;
450
451 /*
452 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
453 */
454 put_unaligned_be16(1, &buf[6]);
455
456 /*
457 * Set MAXIMUM TRANSFER LENGTH
458 */
459 max_sectors = min(dev->se_sub_dev->se_dev_attrib.fabric_max_sectors,
460 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
461 put_unaligned_be32(max_sectors, &buf[8]);
462
463 /*
464 * Set OPTIMAL TRANSFER LENGTH
465 */
466 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.optimal_sectors, &buf[12]);
467
468 /*
469 * Exit now if we don't support TP.
470 */
471 if (!have_tp)
472 return 0;
473
474 /*
475 * Set MAXIMUM UNMAP LBA COUNT
476 */
477 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count, &buf[20]);
478
479 /*
480 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
481 */
482 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count,
483 &buf[24]);
484
485 /*
486 * Set OPTIMAL UNMAP GRANULARITY
487 */
488 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity, &buf[28]);
489
490 /*
491 * UNMAP GRANULARITY ALIGNMENT
492 */
493 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment,
494 &buf[32]);
495 if (dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment != 0)
496 buf[32] |= 0x80; /* Set the UGAVALID bit */
497
498 return 0;
499 }
500
501 /* Block Device Characteristics VPD page */
502 static int spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
503 {
504 struct se_device *dev = cmd->se_dev;
505
506 buf[0] = dev->transport->get_device_type(dev);
507 buf[3] = 0x3c;
508 buf[5] = dev->se_sub_dev->se_dev_attrib.is_nonrot ? 1 : 0;
509
510 return 0;
511 }
512
513 /* Thin Provisioning VPD */
514 static int spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
515 {
516 struct se_device *dev = cmd->se_dev;
517
518 /*
519 * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
520 *
521 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
522 * zero, then the page length shall be set to 0004h. If the DP bit
523 * is set to one, then the page length shall be set to the value
524 * defined in table 162.
525 */
526 buf[0] = dev->transport->get_device_type(dev);
527
528 /*
529 * Set Hardcoded length mentioned above for DP=0
530 */
531 put_unaligned_be16(0x0004, &buf[2]);
532
533 /*
534 * The THRESHOLD EXPONENT field indicates the threshold set size in
535 * LBAs as a power of 2 (i.e., the threshold set size is equal to
536 * 2(threshold exponent)).
537 *
538 * Note that this is currently set to 0x00 as mkp says it will be
539 * changing again. We can enable this once it has settled in T10
540 * and is actually used by Linux/SCSI ML code.
541 */
542 buf[4] = 0x00;
543
544 /*
545 * A TPU bit set to one indicates that the device server supports
546 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
547 * that the device server does not support the UNMAP command.
548 */
549 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu != 0)
550 buf[5] = 0x80;
551
552 /*
553 * A TPWS bit set to one indicates that the device server supports
554 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
555 * A TPWS bit set to zero indicates that the device server does not
556 * support the use of the WRITE SAME (16) command to unmap LBAs.
557 */
558 if (dev->se_sub_dev->se_dev_attrib.emulate_tpws != 0)
559 buf[5] |= 0x40;
560
561 return 0;
562 }
563
564 static int spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
565
566 static struct {
567 uint8_t page;
568 int (*emulate)(struct se_cmd *, unsigned char *);
569 } evpd_handlers[] = {
570 { .page = 0x00, .emulate = spc_emulate_evpd_00 },
571 { .page = 0x80, .emulate = spc_emulate_evpd_80 },
572 { .page = 0x83, .emulate = spc_emulate_evpd_83 },
573 { .page = 0x86, .emulate = spc_emulate_evpd_86 },
574 { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
575 { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
576 { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
577 };
578
579 /* supported vital product data pages */
580 static int spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
581 {
582 int p;
583
584 /*
585 * Only report the INQUIRY EVPD=1 pages after a valid NAA
586 * Registered Extended LUN WWN has been set via ConfigFS
587 * during device creation/restart.
588 */
589 if (cmd->se_dev->se_sub_dev->su_dev_flags &
590 SDF_EMULATED_VPD_UNIT_SERIAL) {
591 buf[3] = ARRAY_SIZE(evpd_handlers);
592 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
593 buf[p + 4] = evpd_handlers[p].page;
594 }
595
596 return 0;
597 }
598
599 static int spc_emulate_inquiry(struct se_cmd *cmd)
600 {
601 struct se_device *dev = cmd->se_dev;
602 struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
603 unsigned char *buf, *map_buf;
604 unsigned char *cdb = cmd->t_task_cdb;
605 int p, ret;
606
607 map_buf = transport_kmap_data_sg(cmd);
608 /*
609 * If SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is not set, then we
610 * know we actually allocated a full page. Otherwise, if the
611 * data buffer is too small, allocate a temporary buffer so we
612 * don't have to worry about overruns in all our INQUIRY
613 * emulation handling.
614 */
615 if (cmd->data_length < SE_INQUIRY_BUF &&
616 (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
617 buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
618 if (!buf) {
619 transport_kunmap_data_sg(cmd);
620 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
621 return -ENOMEM;
622 }
623 } else {
624 buf = map_buf;
625 }
626
627 if (dev == tpg->tpg_virt_lun0.lun_se_dev)
628 buf[0] = 0x3f; /* Not connected */
629 else
630 buf[0] = dev->transport->get_device_type(dev);
631
632 if (!(cdb[1] & 0x1)) {
633 if (cdb[2]) {
634 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
635 cdb[2]);
636 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
637 ret = -EINVAL;
638 goto out;
639 }
640
641 ret = spc_emulate_inquiry_std(cmd, buf);
642 goto out;
643 }
644
645 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
646 if (cdb[2] == evpd_handlers[p].page) {
647 buf[1] = cdb[2];
648 ret = evpd_handlers[p].emulate(cmd, buf);
649 goto out;
650 }
651 }
652
653 pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
654 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
655 ret = -EINVAL;
656
657 out:
658 if (buf != map_buf) {
659 memcpy(map_buf, buf, cmd->data_length);
660 kfree(buf);
661 }
662 transport_kunmap_data_sg(cmd);
663
664 if (!ret)
665 target_complete_cmd(cmd, GOOD);
666 return ret;
667 }
668
669 static int spc_modesense_rwrecovery(unsigned char *p)
670 {
671 p[0] = 0x01;
672 p[1] = 0x0a;
673
674 return 12;
675 }
676
677 static int spc_modesense_control(struct se_device *dev, unsigned char *p)
678 {
679 p[0] = 0x0a;
680 p[1] = 0x0a;
681 p[2] = 2;
682 /*
683 * From spc4r23, 7.4.7 Control mode page
684 *
685 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
686 * restrictions on the algorithm used for reordering commands
687 * having the SIMPLE task attribute (see SAM-4).
688 *
689 * Table 368 -- QUEUE ALGORITHM MODIFIER field
690 * Code Description
691 * 0h Restricted reordering
692 * 1h Unrestricted reordering allowed
693 * 2h to 7h Reserved
694 * 8h to Fh Vendor specific
695 *
696 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
697 * the device server shall order the processing sequence of commands
698 * having the SIMPLE task attribute such that data integrity is maintained
699 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
700 * requests is halted at any time, the final value of all data observable
701 * on the medium shall be the same as if all the commands had been processed
702 * with the ORDERED task attribute).
703 *
704 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
705 * device server may reorder the processing sequence of commands having the
706 * SIMPLE task attribute in any manner. Any data integrity exposures related to
707 * command sequence order shall be explicitly handled by the application client
708 * through the selection of appropriate ommands and task attributes.
709 */
710 p[3] = (dev->se_sub_dev->se_dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
711 /*
712 * From spc4r17, section 7.4.6 Control mode Page
713 *
714 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
715 *
716 * 00b: The logical unit shall clear any unit attention condition
717 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
718 * status and shall not establish a unit attention condition when a com-
719 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
720 * status.
721 *
722 * 10b: The logical unit shall not clear any unit attention condition
723 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
724 * status and shall not establish a unit attention condition when
725 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
726 * CONFLICT status.
727 *
728 * 11b a The logical unit shall not clear any unit attention condition
729 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
730 * status and shall establish a unit attention condition for the
731 * initiator port associated with the I_T nexus on which the BUSY,
732 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
733 * Depending on the status, the additional sense code shall be set to
734 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
735 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
736 * command, a unit attention condition shall be established only once
737 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
738 * to the number of commands completed with one of those status codes.
739 */
740 p[4] = (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
741 (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
742 /*
743 * From spc4r17, section 7.4.6 Control mode Page
744 *
745 * Task Aborted Status (TAS) bit set to zero.
746 *
747 * A task aborted status (TAS) bit set to zero specifies that aborted
748 * tasks shall be terminated by the device server without any response
749 * to the application client. A TAS bit set to one specifies that tasks
750 * aborted by the actions of an I_T nexus other than the I_T nexus on
751 * which the command was received shall be completed with TASK ABORTED
752 * status (see SAM-4).
753 */
754 p[5] = (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
755 p[8] = 0xff;
756 p[9] = 0xff;
757 p[11] = 30;
758
759 return 12;
760 }
761
762 static int spc_modesense_caching(struct se_device *dev, unsigned char *p)
763 {
764 p[0] = 0x08;
765 p[1] = 0x12;
766 if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
767 p[2] = 0x04; /* Write Cache Enable */
768 p[12] = 0x20; /* Disabled Read Ahead */
769
770 return 20;
771 }
772
773 static void spc_modesense_write_protect(unsigned char *buf, int type)
774 {
775 /*
776 * I believe that the WP bit (bit 7) in the mode header is the same for
777 * all device types..
778 */
779 switch (type) {
780 case TYPE_DISK:
781 case TYPE_TAPE:
782 default:
783 buf[0] |= 0x80; /* WP bit */
784 break;
785 }
786 }
787
788 static void spc_modesense_dpofua(unsigned char *buf, int type)
789 {
790 switch (type) {
791 case TYPE_DISK:
792 buf[0] |= 0x10; /* DPOFUA bit */
793 break;
794 default:
795 break;
796 }
797 }
798
799 static int spc_emulate_modesense(struct se_cmd *cmd)
800 {
801 struct se_device *dev = cmd->se_dev;
802 char *cdb = cmd->t_task_cdb;
803 unsigned char *rbuf;
804 int type = dev->transport->get_device_type(dev);
805 int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
806 int offset = ten ? 8 : 4;
807 int length = 0;
808 unsigned char buf[SE_MODE_PAGE_BUF];
809
810 memset(buf, 0, SE_MODE_PAGE_BUF);
811
812 switch (cdb[2] & 0x3f) {
813 case 0x01:
814 length = spc_modesense_rwrecovery(&buf[offset]);
815 break;
816 case 0x08:
817 length = spc_modesense_caching(dev, &buf[offset]);
818 break;
819 case 0x0a:
820 length = spc_modesense_control(dev, &buf[offset]);
821 break;
822 case 0x3f:
823 length = spc_modesense_rwrecovery(&buf[offset]);
824 length += spc_modesense_caching(dev, &buf[offset+length]);
825 length += spc_modesense_control(dev, &buf[offset+length]);
826 break;
827 default:
828 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
829 cdb[2] & 0x3f, cdb[3]);
830 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
831 return -EINVAL;
832 }
833 offset += length;
834
835 if (ten) {
836 offset -= 2;
837 buf[0] = (offset >> 8) & 0xff;
838 buf[1] = offset & 0xff;
839
840 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
841 (cmd->se_deve &&
842 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
843 spc_modesense_write_protect(&buf[3], type);
844
845 if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
846 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
847 spc_modesense_dpofua(&buf[3], type);
848
849 if ((offset + 2) > cmd->data_length)
850 offset = cmd->data_length;
851
852 } else {
853 offset -= 1;
854 buf[0] = offset & 0xff;
855
856 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
857 (cmd->se_deve &&
858 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
859 spc_modesense_write_protect(&buf[2], type);
860
861 if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
862 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
863 spc_modesense_dpofua(&buf[2], type);
864
865 if ((offset + 1) > cmd->data_length)
866 offset = cmd->data_length;
867 }
868
869 rbuf = transport_kmap_data_sg(cmd);
870 memcpy(rbuf, buf, offset);
871 transport_kunmap_data_sg(cmd);
872
873 target_complete_cmd(cmd, GOOD);
874 return 0;
875 }
876
877 static int spc_emulate_request_sense(struct se_cmd *cmd)
878 {
879 unsigned char *cdb = cmd->t_task_cdb;
880 unsigned char *buf;
881 u8 ua_asc = 0, ua_ascq = 0;
882 int err = 0;
883
884 if (cdb[1] & 0x01) {
885 pr_err("REQUEST_SENSE description emulation not"
886 " supported\n");
887 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
888 return -ENOSYS;
889 }
890
891 buf = transport_kmap_data_sg(cmd);
892
893 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
894 /*
895 * CURRENT ERROR, UNIT ATTENTION
896 */
897 buf[0] = 0x70;
898 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
899
900 if (cmd->data_length < 18) {
901 buf[7] = 0x00;
902 err = -EINVAL;
903 goto end;
904 }
905 /*
906 * The Additional Sense Code (ASC) from the UNIT ATTENTION
907 */
908 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
909 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
910 buf[7] = 0x0A;
911 } else {
912 /*
913 * CURRENT ERROR, NO SENSE
914 */
915 buf[0] = 0x70;
916 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
917
918 if (cmd->data_length < 18) {
919 buf[7] = 0x00;
920 err = -EINVAL;
921 goto end;
922 }
923 /*
924 * NO ADDITIONAL SENSE INFORMATION
925 */
926 buf[SPC_ASC_KEY_OFFSET] = 0x00;
927 buf[7] = 0x0A;
928 }
929
930 end:
931 transport_kunmap_data_sg(cmd);
932 target_complete_cmd(cmd, GOOD);
933 return 0;
934 }
935
936 static int spc_emulate_testunitready(struct se_cmd *cmd)
937 {
938 target_complete_cmd(cmd, GOOD);
939 return 0;
940 }
941
942 int spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
943 {
944 struct se_device *dev = cmd->se_dev;
945 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
946 unsigned char *cdb = cmd->t_task_cdb;
947
948 switch (cdb[0]) {
949 case MODE_SELECT:
950 *size = cdb[4];
951 break;
952 case MODE_SELECT_10:
953 *size = (cdb[7] << 8) + cdb[8];
954 break;
955 case MODE_SENSE:
956 *size = cdb[4];
957 cmd->execute_cmd = spc_emulate_modesense;
958 break;
959 case MODE_SENSE_10:
960 *size = (cdb[7] << 8) + cdb[8];
961 cmd->execute_cmd = spc_emulate_modesense;
962 break;
963 case LOG_SELECT:
964 case LOG_SENSE:
965 *size = (cdb[7] << 8) + cdb[8];
966 break;
967 case PERSISTENT_RESERVE_IN:
968 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
969 cmd->execute_cmd = target_scsi3_emulate_pr_in;
970 *size = (cdb[7] << 8) + cdb[8];
971 break;
972 case PERSISTENT_RESERVE_OUT:
973 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
974 cmd->execute_cmd = target_scsi3_emulate_pr_out;
975 *size = (cdb[7] << 8) + cdb[8];
976 break;
977 case RELEASE:
978 case RELEASE_10:
979 if (cdb[0] == RELEASE_10)
980 *size = (cdb[7] << 8) | cdb[8];
981 else
982 *size = cmd->data_length;
983
984 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
985 cmd->execute_cmd = target_scsi2_reservation_release;
986 break;
987 case RESERVE:
988 case RESERVE_10:
989 /*
990 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
991 * Assume the passthrough or $FABRIC_MOD will tell us about it.
992 */
993 if (cdb[0] == RESERVE_10)
994 *size = (cdb[7] << 8) | cdb[8];
995 else
996 *size = cmd->data_length;
997
998 /*
999 * Setup the legacy emulated handler for SPC-2 and
1000 * >= SPC-3 compatible reservation handling (CRH=1)
1001 * Otherwise, we assume the underlying SCSI logic is
1002 * is running in SPC_PASSTHROUGH, and wants reservations
1003 * emulation disabled.
1004 */
1005 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
1006 cmd->execute_cmd = target_scsi2_reservation_reserve;
1007 break;
1008 case REQUEST_SENSE:
1009 *size = cdb[4];
1010 cmd->execute_cmd = spc_emulate_request_sense;
1011 break;
1012 case INQUIRY:
1013 *size = (cdb[3] << 8) + cdb[4];
1014
1015 /*
1016 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
1017 * See spc4r17 section 5.3
1018 */
1019 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1020 cmd->sam_task_attr = MSG_HEAD_TAG;
1021 cmd->execute_cmd = spc_emulate_inquiry;
1022 break;
1023 case SECURITY_PROTOCOL_IN:
1024 case SECURITY_PROTOCOL_OUT:
1025 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1026 break;
1027 case EXTENDED_COPY:
1028 case READ_ATTRIBUTE:
1029 case RECEIVE_COPY_RESULTS:
1030 case WRITE_ATTRIBUTE:
1031 *size = (cdb[10] << 24) | (cdb[11] << 16) |
1032 (cdb[12] << 8) | cdb[13];
1033 break;
1034 case RECEIVE_DIAGNOSTIC:
1035 case SEND_DIAGNOSTIC:
1036 *size = (cdb[3] << 8) | cdb[4];
1037 break;
1038 case WRITE_BUFFER:
1039 *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
1040 break;
1041 case REPORT_LUNS:
1042 cmd->execute_cmd = target_report_luns;
1043 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1044 /*
1045 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
1046 * See spc4r17 section 5.3
1047 */
1048 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1049 cmd->sam_task_attr = MSG_HEAD_TAG;
1050 break;
1051 case TEST_UNIT_READY:
1052 cmd->execute_cmd = spc_emulate_testunitready;
1053 *size = 0;
1054 break;
1055 case MAINTENANCE_IN:
1056 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1057 /*
1058 * MAINTENANCE_IN from SCC-2
1059 * Check for emulated MI_REPORT_TARGET_PGS
1060 */
1061 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS &&
1062 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
1063 cmd->execute_cmd =
1064 target_emulate_report_target_port_groups;
1065 }
1066 *size = get_unaligned_be32(&cdb[6]);
1067 } else {
1068 /*
1069 * GPCMD_SEND_KEY from multi media commands
1070 */
1071 *size = get_unaligned_be16(&cdb[8]);
1072 }
1073 break;
1074 case MAINTENANCE_OUT:
1075 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1076 /*
1077 * MAINTENANCE_OUT from SCC-2
1078 * Check for emulated MO_SET_TARGET_PGS.
1079 */
1080 if (cdb[1] == MO_SET_TARGET_PGS &&
1081 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
1082 cmd->execute_cmd =
1083 target_emulate_set_target_port_groups;
1084 }
1085 *size = get_unaligned_be32(&cdb[6]);
1086 } else {
1087 /*
1088 * GPCMD_SEND_KEY from multi media commands
1089 */
1090 *size = get_unaligned_be16(&cdb[8]);
1091 }
1092 break;
1093 default:
1094 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
1095 " 0x%02x, sending CHECK_CONDITION.\n",
1096 cmd->se_tfo->get_fabric_name(), cdb[0]);
1097 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1098 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1099 return -EINVAL;
1100 }
1101
1102 return 0;
1103 }
1104 EXPORT_SYMBOL(spc_parse_cdb);
This page took 0.055056 seconds and 5 git commands to generate.