sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / target / target_core_cdb.c
blob7f19c8b7b84c704c0ab74ffdef7a19dca63737e2
1 /*
2 * CDB emulation for non-READ/WRITE commands.
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
9 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_transport.h>
31 #include <target/target_core_fabric_ops.h>
32 #include "target_core_ua.h"
34 static void
35 target_fill_alua_data(struct se_port *port, unsigned char *buf)
37 struct t10_alua_tg_pt_gp *tg_pt_gp;
38 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
41 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
43 buf[5] = 0x80;
46 * Set TPGS field for explict and/or implict ALUA access type
47 * and opteration.
49 * See spc4r17 section 6.4.2 Table 135
51 if (!port)
52 return;
53 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
54 if (!tg_pt_gp_mem)
55 return;
57 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
58 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
59 if (tg_pt_gp)
60 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
61 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
64 static int
65 target_emulate_inquiry_std(struct se_cmd *cmd)
67 struct se_lun *lun = SE_LUN(cmd);
68 struct se_device *dev = SE_DEV(cmd);
69 unsigned char *buf = cmd->t_task->t_task_buf;
72 * Make sure we at least have 6 bytes of INQUIRY response
73 * payload going back for EVPD=0
75 if (cmd->data_length < 6) {
76 printk(KERN_ERR "SCSI Inquiry payload length: %u"
77 " too small for EVPD=0\n", cmd->data_length);
78 return -1;
81 buf[0] = dev->transport->get_device_type(dev);
82 if (buf[0] == TYPE_TAPE)
83 buf[1] = 0x80;
84 buf[2] = dev->transport->get_device_rev(dev);
87 * Enable SCCS and TPGS fields for Emulated ALUA
89 if (T10_ALUA(dev->se_sub_dev)->alua_type == SPC3_ALUA_EMULATED)
90 target_fill_alua_data(lun->lun_sep, buf);
92 if (cmd->data_length < 8) {
93 buf[4] = 1; /* Set additional length to 1 */
94 return 0;
97 buf[7] = 0x32; /* Sync=1 and CmdQue=1 */
100 * Do not include vendor, product, reversion info in INQUIRY
101 * response payload for cdbs with a small allocation length.
103 if (cmd->data_length < 36) {
104 buf[4] = 3; /* Set additional length to 3 */
105 return 0;
108 snprintf((unsigned char *)&buf[8], 8, "LIO-ORG");
109 snprintf((unsigned char *)&buf[16], 16, "%s",
110 &DEV_T10_WWN(dev)->model[0]);
111 snprintf((unsigned char *)&buf[32], 4, "%s",
112 &DEV_T10_WWN(dev)->revision[0]);
113 buf[4] = 31; /* Set additional length to 31 */
114 return 0;
117 /* supported vital product data pages */
118 static int
119 target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
121 buf[1] = 0x00;
122 if (cmd->data_length < 8)
123 return 0;
125 buf[4] = 0x0;
127 * Only report the INQUIRY EVPD=1 pages after a valid NAA
128 * Registered Extended LUN WWN has been set via ConfigFS
129 * during device creation/restart.
131 if (SE_DEV(cmd)->se_sub_dev->su_dev_flags &
132 SDF_EMULATED_VPD_UNIT_SERIAL) {
133 buf[3] = 3;
134 buf[5] = 0x80;
135 buf[6] = 0x83;
136 buf[7] = 0x86;
139 return 0;
142 /* unit serial number */
143 static int
144 target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
146 struct se_device *dev = SE_DEV(cmd);
147 u16 len = 0;
149 buf[1] = 0x80;
150 if (dev->se_sub_dev->su_dev_flags &
151 SDF_EMULATED_VPD_UNIT_SERIAL) {
152 u32 unit_serial_len;
154 unit_serial_len =
155 strlen(&DEV_T10_WWN(dev)->unit_serial[0]);
156 unit_serial_len++; /* For NULL Terminator */
158 if (((len + 4) + unit_serial_len) > cmd->data_length) {
159 len += unit_serial_len;
160 buf[2] = ((len >> 8) & 0xff);
161 buf[3] = (len & 0xff);
162 return 0;
164 len += sprintf((unsigned char *)&buf[4], "%s",
165 &DEV_T10_WWN(dev)->unit_serial[0]);
166 len++; /* Extra Byte for NULL Terminator */
167 buf[3] = len;
169 return 0;
173 * Device identification VPD, for a complete list of
174 * DESIGNATOR TYPEs see spc4r17 Table 459.
176 static int
177 target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
179 struct se_device *dev = SE_DEV(cmd);
180 struct se_lun *lun = SE_LUN(cmd);
181 struct se_port *port = NULL;
182 struct se_portal_group *tpg = NULL;
183 struct t10_alua_lu_gp_member *lu_gp_mem;
184 struct t10_alua_tg_pt_gp *tg_pt_gp;
185 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
186 unsigned char binary, binary_new;
187 unsigned char *prod = &DEV_T10_WWN(dev)->model[0];
188 u32 prod_len;
189 u32 unit_serial_len, off = 0;
190 int i;
191 u16 len = 0, id_len;
193 buf[1] = 0x83;
194 off = 4;
197 * NAA IEEE Registered Extended Assigned designator format, see
198 * spc4r17 section 7.7.3.6.5
200 * We depend upon a target_core_mod/ConfigFS provided
201 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
202 * value in order to return the NAA id.
204 if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
205 goto check_t10_vend_desc;
207 if (off + 20 > cmd->data_length)
208 goto check_t10_vend_desc;
210 /* CODE SET == Binary */
211 buf[off++] = 0x1;
213 /* Set ASSOICATION == addressed logical unit: 0)b */
214 buf[off] = 0x00;
216 /* Identifier/Designator type == NAA identifier */
217 buf[off++] = 0x3;
218 off++;
220 /* Identifier/Designator length */
221 buf[off++] = 0x10;
224 * Start NAA IEEE Registered Extended Identifier/Designator
226 buf[off++] = (0x6 << 4);
229 * Use OpenFabrics IEEE Company ID: 00 14 05
231 buf[off++] = 0x01;
232 buf[off++] = 0x40;
233 buf[off] = (0x5 << 4);
236 * Return ConfigFS Unit Serial Number information for
237 * VENDOR_SPECIFIC_IDENTIFIER and
238 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
240 binary = transport_asciihex_to_binaryhex(
241 &DEV_T10_WWN(dev)->unit_serial[0]);
242 buf[off++] |= (binary & 0xf0) >> 4;
243 for (i = 0; i < 24; i += 2) {
244 binary_new = transport_asciihex_to_binaryhex(
245 &DEV_T10_WWN(dev)->unit_serial[i+2]);
246 buf[off] = (binary & 0x0f) << 4;
247 buf[off++] |= (binary_new & 0xf0) >> 4;
248 binary = binary_new;
250 len = 20;
251 off = (len + 4);
253 check_t10_vend_desc:
255 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
257 id_len = 8; /* For Vendor field */
258 prod_len = 4; /* For VPD Header */
259 prod_len += 8; /* For Vendor field */
260 prod_len += strlen(prod);
261 prod_len++; /* For : */
263 if (dev->se_sub_dev->su_dev_flags &
264 SDF_EMULATED_VPD_UNIT_SERIAL) {
265 unit_serial_len =
266 strlen(&DEV_T10_WWN(dev)->unit_serial[0]);
267 unit_serial_len++; /* For NULL Terminator */
269 if ((len + (id_len + 4) +
270 (prod_len + unit_serial_len)) >
271 cmd->data_length) {
272 len += (prod_len + unit_serial_len);
273 goto check_port;
275 id_len += sprintf((unsigned char *)&buf[off+12],
276 "%s:%s", prod,
277 &DEV_T10_WWN(dev)->unit_serial[0]);
279 buf[off] = 0x2; /* ASCII */
280 buf[off+1] = 0x1; /* T10 Vendor ID */
281 buf[off+2] = 0x0;
282 memcpy((unsigned char *)&buf[off+4], "LIO-ORG", 8);
283 /* Extra Byte for NULL Terminator */
284 id_len++;
285 /* Identifier Length */
286 buf[off+3] = id_len;
287 /* Header size for Designation descriptor */
288 len += (id_len + 4);
289 off += (id_len + 4);
291 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
293 check_port:
294 port = lun->lun_sep;
295 if (port) {
296 struct t10_alua_lu_gp *lu_gp;
297 u32 padding, scsi_name_len;
298 u16 lu_gp_id = 0;
299 u16 tg_pt_gp_id = 0;
300 u16 tpgt;
302 tpg = port->sep_tpg;
304 * Relative target port identifer, see spc4r17
305 * section 7.7.3.7
307 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
308 * section 7.5.1 Table 362
310 if (((len + 4) + 8) > cmd->data_length) {
311 len += 8;
312 goto check_tpgi;
314 buf[off] =
315 (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
316 buf[off++] |= 0x1; /* CODE SET == Binary */
317 buf[off] = 0x80; /* Set PIV=1 */
318 /* Set ASSOICATION == target port: 01b */
319 buf[off] |= 0x10;
320 /* DESIGNATOR TYPE == Relative target port identifer */
321 buf[off++] |= 0x4;
322 off++; /* Skip over Reserved */
323 buf[off++] = 4; /* DESIGNATOR LENGTH */
324 /* Skip over Obsolete field in RTPI payload
325 * in Table 472 */
326 off += 2;
327 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
328 buf[off++] = (port->sep_rtpi & 0xff);
329 len += 8; /* Header size + Designation descriptor */
331 * Target port group identifier, see spc4r17
332 * section 7.7.3.8
334 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
335 * section 7.5.1 Table 362
337 check_tpgi:
338 if (T10_ALUA(dev->se_sub_dev)->alua_type !=
339 SPC3_ALUA_EMULATED)
340 goto check_scsi_name;
342 if (((len + 4) + 8) > cmd->data_length) {
343 len += 8;
344 goto check_lu_gp;
346 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
347 if (!tg_pt_gp_mem)
348 goto check_lu_gp;
350 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
351 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
352 if (!(tg_pt_gp)) {
353 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
354 goto check_lu_gp;
356 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
357 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
359 buf[off] =
360 (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
361 buf[off++] |= 0x1; /* CODE SET == Binary */
362 buf[off] = 0x80; /* Set PIV=1 */
363 /* Set ASSOICATION == target port: 01b */
364 buf[off] |= 0x10;
365 /* DESIGNATOR TYPE == Target port group identifier */
366 buf[off++] |= 0x5;
367 off++; /* Skip over Reserved */
368 buf[off++] = 4; /* DESIGNATOR LENGTH */
369 off += 2; /* Skip over Reserved Field */
370 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
371 buf[off++] = (tg_pt_gp_id & 0xff);
372 len += 8; /* Header size + Designation descriptor */
374 * Logical Unit Group identifier, see spc4r17
375 * section 7.7.3.8
377 check_lu_gp:
378 if (((len + 4) + 8) > cmd->data_length) {
379 len += 8;
380 goto check_scsi_name;
382 lu_gp_mem = dev->dev_alua_lu_gp_mem;
383 if (!(lu_gp_mem))
384 goto check_scsi_name;
386 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
387 lu_gp = lu_gp_mem->lu_gp;
388 if (!(lu_gp)) {
389 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
390 goto check_scsi_name;
392 lu_gp_id = lu_gp->lu_gp_id;
393 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
395 buf[off++] |= 0x1; /* CODE SET == Binary */
396 /* DESIGNATOR TYPE == Logical Unit Group identifier */
397 buf[off++] |= 0x6;
398 off++; /* Skip over Reserved */
399 buf[off++] = 4; /* DESIGNATOR LENGTH */
400 off += 2; /* Skip over Reserved Field */
401 buf[off++] = ((lu_gp_id >> 8) & 0xff);
402 buf[off++] = (lu_gp_id & 0xff);
403 len += 8; /* Header size + Designation descriptor */
405 * SCSI name string designator, see spc4r17
406 * section 7.7.3.11
408 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
409 * section 7.5.1 Table 362
411 check_scsi_name:
412 scsi_name_len = strlen(TPG_TFO(tpg)->tpg_get_wwn(tpg));
413 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
414 scsi_name_len += 10;
415 /* Check for 4-byte padding */
416 padding = ((-scsi_name_len) & 3);
417 if (padding != 0)
418 scsi_name_len += padding;
419 /* Header size + Designation descriptor */
420 scsi_name_len += 4;
422 if (((len + 4) + scsi_name_len) > cmd->data_length) {
423 len += scsi_name_len;
424 goto set_len;
426 buf[off] =
427 (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
428 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
429 buf[off] = 0x80; /* Set PIV=1 */
430 /* Set ASSOICATION == target port: 01b */
431 buf[off] |= 0x10;
432 /* DESIGNATOR TYPE == SCSI name string */
433 buf[off++] |= 0x8;
434 off += 2; /* Skip over Reserved and length */
436 * SCSI name string identifer containing, $FABRIC_MOD
437 * dependent information. For LIO-Target and iSCSI
438 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
439 * UTF-8 encoding.
441 tpgt = TPG_TFO(tpg)->tpg_get_tag(tpg);
442 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
443 TPG_TFO(tpg)->tpg_get_wwn(tpg), tpgt);
444 scsi_name_len += 1 /* Include NULL terminator */;
446 * The null-terminated, null-padded (see 4.4.2) SCSI
447 * NAME STRING field contains a UTF-8 format string.
448 * The number of bytes in the SCSI NAME STRING field
449 * (i.e., the value in the DESIGNATOR LENGTH field)
450 * shall be no larger than 256 and shall be a multiple
451 * of four.
453 if (padding)
454 scsi_name_len += padding;
456 buf[off-1] = scsi_name_len;
457 off += scsi_name_len;
458 /* Header size + Designation descriptor */
459 len += (scsi_name_len + 4);
461 set_len:
462 buf[2] = ((len >> 8) & 0xff);
463 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
464 return 0;
467 /* Extended INQUIRY Data VPD Page */
468 static int
469 target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
471 if (cmd->data_length < 60)
472 return 0;
474 buf[1] = 0x86;
475 buf[2] = 0x3c;
476 /* Set HEADSUP, ORDSUP, SIMPSUP */
477 buf[5] = 0x07;
479 /* If WriteCache emulation is enabled, set V_SUP */
480 if (DEV_ATTRIB(SE_DEV(cmd))->emulate_write_cache > 0)
481 buf[6] = 0x01;
482 return 0;
485 /* Block Limits VPD page */
486 static int
487 target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
489 struct se_device *dev = SE_DEV(cmd);
490 int have_tp = 0;
493 * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
494 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
495 * different page length for Thin Provisioning.
497 if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
498 have_tp = 1;
500 if (cmd->data_length < (0x10 + 4)) {
501 printk(KERN_INFO "Received data_length: %u"
502 " too small for EVPD 0xb0\n",
503 cmd->data_length);
504 return -1;
507 if (have_tp && cmd->data_length < (0x3c + 4)) {
508 printk(KERN_INFO "Received data_length: %u"
509 " too small for TPE=1 EVPD 0xb0\n",
510 cmd->data_length);
511 have_tp = 0;
514 buf[0] = dev->transport->get_device_type(dev);
515 buf[1] = 0xb0;
516 buf[3] = have_tp ? 0x3c : 0x10;
519 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
521 put_unaligned_be16(1, &buf[6]);
524 * Set MAXIMUM TRANSFER LENGTH
526 put_unaligned_be32(DEV_ATTRIB(dev)->max_sectors, &buf[8]);
529 * Set OPTIMAL TRANSFER LENGTH
531 put_unaligned_be32(DEV_ATTRIB(dev)->optimal_sectors, &buf[12]);
534 * Exit now if we don't support TP or the initiator sent a too
535 * short buffer.
537 if (!have_tp || cmd->data_length < (0x3c + 4))
538 return 0;
541 * Set MAXIMUM UNMAP LBA COUNT
543 put_unaligned_be32(DEV_ATTRIB(dev)->max_unmap_lba_count, &buf[20]);
546 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
548 put_unaligned_be32(DEV_ATTRIB(dev)->max_unmap_block_desc_count,
549 &buf[24]);
552 * Set OPTIMAL UNMAP GRANULARITY
554 put_unaligned_be32(DEV_ATTRIB(dev)->unmap_granularity, &buf[28]);
557 * UNMAP GRANULARITY ALIGNMENT
559 put_unaligned_be32(DEV_ATTRIB(dev)->unmap_granularity_alignment,
560 &buf[32]);
561 if (DEV_ATTRIB(dev)->unmap_granularity_alignment != 0)
562 buf[32] |= 0x80; /* Set the UGAVALID bit */
564 return 0;
567 /* Thin Provisioning VPD */
568 static int
569 target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
571 struct se_device *dev = SE_DEV(cmd);
574 * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
576 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
577 * zero, then the page length shall be set to 0004h. If the DP bit
578 * is set to one, then the page length shall be set to the value
579 * defined in table 162.
581 buf[0] = dev->transport->get_device_type(dev);
582 buf[1] = 0xb2;
585 * Set Hardcoded length mentioned above for DP=0
587 put_unaligned_be16(0x0004, &buf[2]);
590 * The THRESHOLD EXPONENT field indicates the threshold set size in
591 * LBAs as a power of 2 (i.e., the threshold set size is equal to
592 * 2(threshold exponent)).
594 * Note that this is currently set to 0x00 as mkp says it will be
595 * changing again. We can enable this once it has settled in T10
596 * and is actually used by Linux/SCSI ML code.
598 buf[4] = 0x00;
601 * A TPU bit set to one indicates that the device server supports
602 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
603 * that the device server does not support the UNMAP command.
605 if (DEV_ATTRIB(dev)->emulate_tpu != 0)
606 buf[5] = 0x80;
609 * A TPWS bit set to one indicates that the device server supports
610 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
611 * A TPWS bit set to zero indicates that the device server does not
612 * support the use of the WRITE SAME (16) command to unmap LBAs.
614 if (DEV_ATTRIB(dev)->emulate_tpws != 0)
615 buf[5] |= 0x40;
617 return 0;
620 static int
621 target_emulate_inquiry(struct se_cmd *cmd)
623 struct se_device *dev = SE_DEV(cmd);
624 unsigned char *buf = cmd->t_task->t_task_buf;
625 unsigned char *cdb = cmd->t_task->t_task_cdb;
627 if (!(cdb[1] & 0x1))
628 return target_emulate_inquiry_std(cmd);
631 * Make sure we at least have 4 bytes of INQUIRY response
632 * payload for 0x00 going back for EVPD=1. Note that 0x80
633 * and 0x83 will check for enough payload data length and
634 * jump to set_len: label when there is not enough inquiry EVPD
635 * payload length left for the next outgoing EVPD metadata
637 if (cmd->data_length < 4) {
638 printk(KERN_ERR "SCSI Inquiry payload length: %u"
639 " too small for EVPD=1\n", cmd->data_length);
640 return -1;
642 buf[0] = dev->transport->get_device_type(dev);
644 switch (cdb[2]) {
645 case 0x00:
646 return target_emulate_evpd_00(cmd, buf);
647 case 0x80:
648 return target_emulate_evpd_80(cmd, buf);
649 case 0x83:
650 return target_emulate_evpd_83(cmd, buf);
651 case 0x86:
652 return target_emulate_evpd_86(cmd, buf);
653 case 0xb0:
654 return target_emulate_evpd_b0(cmd, buf);
655 case 0xb2:
656 return target_emulate_evpd_b2(cmd, buf);
657 default:
658 printk(KERN_ERR "Unknown VPD Code: 0x%02x\n", cdb[2]);
659 return -1;
662 return 0;
665 static int
666 target_emulate_readcapacity(struct se_cmd *cmd)
668 struct se_device *dev = SE_DEV(cmd);
669 unsigned char *buf = cmd->t_task->t_task_buf;
670 unsigned long long blocks_long = dev->transport->get_blocks(dev);
671 u32 blocks;
673 if (blocks_long >= 0x00000000ffffffff)
674 blocks = 0xffffffff;
675 else
676 blocks = (u32)blocks_long;
678 buf[0] = (blocks >> 24) & 0xff;
679 buf[1] = (blocks >> 16) & 0xff;
680 buf[2] = (blocks >> 8) & 0xff;
681 buf[3] = blocks & 0xff;
682 buf[4] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
683 buf[5] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
684 buf[6] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
685 buf[7] = DEV_ATTRIB(dev)->block_size & 0xff;
687 * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
689 if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
690 put_unaligned_be32(0xFFFFFFFF, &buf[0]);
692 return 0;
695 static int
696 target_emulate_readcapacity_16(struct se_cmd *cmd)
698 struct se_device *dev = SE_DEV(cmd);
699 unsigned char *buf = cmd->t_task->t_task_buf;
700 unsigned long long blocks = dev->transport->get_blocks(dev);
702 buf[0] = (blocks >> 56) & 0xff;
703 buf[1] = (blocks >> 48) & 0xff;
704 buf[2] = (blocks >> 40) & 0xff;
705 buf[3] = (blocks >> 32) & 0xff;
706 buf[4] = (blocks >> 24) & 0xff;
707 buf[5] = (blocks >> 16) & 0xff;
708 buf[6] = (blocks >> 8) & 0xff;
709 buf[7] = blocks & 0xff;
710 buf[8] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
711 buf[9] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
712 buf[10] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
713 buf[11] = DEV_ATTRIB(dev)->block_size & 0xff;
715 * Set Thin Provisioning Enable bit following sbc3r22 in section
716 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
718 if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
719 buf[14] = 0x80;
721 return 0;
724 static int
725 target_modesense_rwrecovery(unsigned char *p)
727 p[0] = 0x01;
728 p[1] = 0x0a;
730 return 12;
733 static int
734 target_modesense_control(struct se_device *dev, unsigned char *p)
736 p[0] = 0x0a;
737 p[1] = 0x0a;
738 p[2] = 2;
740 * From spc4r17, section 7.4.6 Control mode Page
742 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
744 * 00b: The logical unit shall clear any unit attention condition
745 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
746 * status and shall not establish a unit attention condition when a com-
747 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
748 * status.
750 * 10b: The logical unit shall not clear any unit attention condition
751 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
752 * status and shall not establish a unit attention condition when
753 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
754 * CONFLICT status.
756 * 11b a The logical unit shall not clear any unit attention condition
757 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
758 * status and shall establish a unit attention condition for the
759 * initiator port associated with the I_T nexus on which the BUSY,
760 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
761 * Depending on the status, the additional sense code shall be set to
762 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
763 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
764 * command, a unit attention condition shall be established only once
765 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
766 * to the number of commands completed with one of those status codes.
768 p[4] = (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 2) ? 0x30 :
769 (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
771 * From spc4r17, section 7.4.6 Control mode Page
773 * Task Aborted Status (TAS) bit set to zero.
775 * A task aborted status (TAS) bit set to zero specifies that aborted
776 * tasks shall be terminated by the device server without any response
777 * to the application client. A TAS bit set to one specifies that tasks
778 * aborted by the actions of an I_T nexus other than the I_T nexus on
779 * which the command was received shall be completed with TASK ABORTED
780 * status (see SAM-4).
782 p[5] = (DEV_ATTRIB(dev)->emulate_tas) ? 0x40 : 0x00;
783 p[8] = 0xff;
784 p[9] = 0xff;
785 p[11] = 30;
787 return 12;
790 static int
791 target_modesense_caching(struct se_device *dev, unsigned char *p)
793 p[0] = 0x08;
794 p[1] = 0x12;
795 if (DEV_ATTRIB(dev)->emulate_write_cache > 0)
796 p[2] = 0x04; /* Write Cache Enable */
797 p[12] = 0x20; /* Disabled Read Ahead */
799 return 20;
802 static void
803 target_modesense_write_protect(unsigned char *buf, int type)
806 * I believe that the WP bit (bit 7) in the mode header is the same for
807 * all device types..
809 switch (type) {
810 case TYPE_DISK:
811 case TYPE_TAPE:
812 default:
813 buf[0] |= 0x80; /* WP bit */
814 break;
818 static void
819 target_modesense_dpofua(unsigned char *buf, int type)
821 switch (type) {
822 case TYPE_DISK:
823 buf[0] |= 0x10; /* DPOFUA bit */
824 break;
825 default:
826 break;
830 static int
831 target_emulate_modesense(struct se_cmd *cmd, int ten)
833 struct se_device *dev = SE_DEV(cmd);
834 char *cdb = cmd->t_task->t_task_cdb;
835 unsigned char *rbuf = cmd->t_task->t_task_buf;
836 int type = dev->transport->get_device_type(dev);
837 int offset = (ten) ? 8 : 4;
838 int length = 0;
839 unsigned char buf[SE_MODE_PAGE_BUF];
841 memset(buf, 0, SE_MODE_PAGE_BUF);
843 switch (cdb[2] & 0x3f) {
844 case 0x01:
845 length = target_modesense_rwrecovery(&buf[offset]);
846 break;
847 case 0x08:
848 length = target_modesense_caching(dev, &buf[offset]);
849 break;
850 case 0x0a:
851 length = target_modesense_control(dev, &buf[offset]);
852 break;
853 case 0x3f:
854 length = target_modesense_rwrecovery(&buf[offset]);
855 length += target_modesense_caching(dev, &buf[offset+length]);
856 length += target_modesense_control(dev, &buf[offset+length]);
857 break;
858 default:
859 printk(KERN_ERR "Got Unknown Mode Page: 0x%02x\n",
860 cdb[2] & 0x3f);
861 return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
863 offset += length;
865 if (ten) {
866 offset -= 2;
867 buf[0] = (offset >> 8) & 0xff;
868 buf[1] = offset & 0xff;
870 if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
871 (cmd->se_deve &&
872 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
873 target_modesense_write_protect(&buf[3], type);
875 if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
876 (DEV_ATTRIB(dev)->emulate_fua_write > 0))
877 target_modesense_dpofua(&buf[3], type);
879 if ((offset + 2) > cmd->data_length)
880 offset = cmd->data_length;
882 } else {
883 offset -= 1;
884 buf[0] = offset & 0xff;
886 if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
887 (cmd->se_deve &&
888 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
889 target_modesense_write_protect(&buf[2], type);
891 if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
892 (DEV_ATTRIB(dev)->emulate_fua_write > 0))
893 target_modesense_dpofua(&buf[2], type);
895 if ((offset + 1) > cmd->data_length)
896 offset = cmd->data_length;
898 memcpy(rbuf, buf, offset);
900 return 0;
903 static int
904 target_emulate_request_sense(struct se_cmd *cmd)
906 unsigned char *cdb = cmd->t_task->t_task_cdb;
907 unsigned char *buf = cmd->t_task->t_task_buf;
908 u8 ua_asc = 0, ua_ascq = 0;
910 if (cdb[1] & 0x01) {
911 printk(KERN_ERR "REQUEST_SENSE description emulation not"
912 " supported\n");
913 return PYX_TRANSPORT_INVALID_CDB_FIELD;
915 if (!(core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))) {
917 * CURRENT ERROR, UNIT ATTENTION
919 buf[0] = 0x70;
920 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
922 * Make sure request data length is enough for additional
923 * sense data.
925 if (cmd->data_length <= 18) {
926 buf[7] = 0x00;
927 return 0;
930 * The Additional Sense Code (ASC) from the UNIT ATTENTION
932 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
933 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
934 buf[7] = 0x0A;
935 } else {
937 * CURRENT ERROR, NO SENSE
939 buf[0] = 0x70;
940 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
942 * Make sure request data length is enough for additional
943 * sense data.
945 if (cmd->data_length <= 18) {
946 buf[7] = 0x00;
947 return 0;
950 * NO ADDITIONAL SENSE INFORMATION
952 buf[SPC_ASC_KEY_OFFSET] = 0x00;
953 buf[7] = 0x0A;
956 return 0;
960 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
961 * Note this is not used for TCM/pSCSI passthrough
963 static int
964 target_emulate_unmap(struct se_task *task)
966 struct se_cmd *cmd = TASK_CMD(task);
967 struct se_device *dev = SE_DEV(cmd);
968 unsigned char *buf = cmd->t_task->t_task_buf, *ptr = NULL;
969 unsigned char *cdb = &cmd->t_task->t_task_cdb[0];
970 sector_t lba;
971 unsigned int size = cmd->data_length, range;
972 int ret, offset;
973 unsigned short dl, bd_dl;
975 /* First UNMAP block descriptor starts at 8 byte offset */
976 offset = 8;
977 size -= 8;
978 dl = get_unaligned_be16(&cdb[0]);
979 bd_dl = get_unaligned_be16(&cdb[2]);
980 ptr = &buf[offset];
981 printk(KERN_INFO "UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
982 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
984 while (size) {
985 lba = get_unaligned_be64(&ptr[0]);
986 range = get_unaligned_be32(&ptr[8]);
987 printk(KERN_INFO "UNMAP: Using lba: %llu and range: %u\n",
988 (unsigned long long)lba, range);
990 ret = dev->transport->do_discard(dev, lba, range);
991 if (ret < 0) {
992 printk(KERN_ERR "blkdev_issue_discard() failed: %d\n",
993 ret);
994 return -1;
997 ptr += 16;
998 size -= 16;
1001 task->task_scsi_status = GOOD;
1002 transport_complete_task(task, 1);
1003 return 0;
1007 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1008 * Note this is not used for TCM/pSCSI passthrough
1010 static int
1011 target_emulate_write_same(struct se_task *task)
1013 struct se_cmd *cmd = TASK_CMD(task);
1014 struct se_device *dev = SE_DEV(cmd);
1015 sector_t lba = cmd->t_task->t_task_lba;
1016 unsigned int range;
1017 int ret;
1019 range = (cmd->data_length / DEV_ATTRIB(dev)->block_size);
1021 printk(KERN_INFO "WRITE_SAME UNMAP: LBA: %llu Range: %u\n",
1022 (unsigned long long)lba, range);
1024 ret = dev->transport->do_discard(dev, lba, range);
1025 if (ret < 0) {
1026 printk(KERN_INFO "blkdev_issue_discard() failed for WRITE_SAME\n");
1027 return -1;
1030 task->task_scsi_status = GOOD;
1031 transport_complete_task(task, 1);
1032 return 0;
1036 transport_emulate_control_cdb(struct se_task *task)
1038 struct se_cmd *cmd = TASK_CMD(task);
1039 struct se_device *dev = SE_DEV(cmd);
1040 unsigned short service_action;
1041 int ret = 0;
1043 switch (cmd->t_task->t_task_cdb[0]) {
1044 case INQUIRY:
1045 ret = target_emulate_inquiry(cmd);
1046 break;
1047 case READ_CAPACITY:
1048 ret = target_emulate_readcapacity(cmd);
1049 break;
1050 case MODE_SENSE:
1051 ret = target_emulate_modesense(cmd, 0);
1052 break;
1053 case MODE_SENSE_10:
1054 ret = target_emulate_modesense(cmd, 1);
1055 break;
1056 case SERVICE_ACTION_IN:
1057 switch (cmd->t_task->t_task_cdb[1] & 0x1f) {
1058 case SAI_READ_CAPACITY_16:
1059 ret = target_emulate_readcapacity_16(cmd);
1060 break;
1061 default:
1062 printk(KERN_ERR "Unsupported SA: 0x%02x\n",
1063 cmd->t_task->t_task_cdb[1] & 0x1f);
1064 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1066 break;
1067 case REQUEST_SENSE:
1068 ret = target_emulate_request_sense(cmd);
1069 break;
1070 case UNMAP:
1071 if (!dev->transport->do_discard) {
1072 printk(KERN_ERR "UNMAP emulation not supported for: %s\n",
1073 dev->transport->name);
1074 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1076 ret = target_emulate_unmap(task);
1077 break;
1078 case WRITE_SAME_16:
1079 if (!dev->transport->do_discard) {
1080 printk(KERN_ERR "WRITE_SAME_16 emulation not supported"
1081 " for: %s\n", dev->transport->name);
1082 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1084 ret = target_emulate_write_same(task);
1085 break;
1086 case VARIABLE_LENGTH_CMD:
1087 service_action =
1088 get_unaligned_be16(&cmd->t_task->t_task_cdb[8]);
1089 switch (service_action) {
1090 case WRITE_SAME_32:
1091 if (!dev->transport->do_discard) {
1092 printk(KERN_ERR "WRITE_SAME_32 SA emulation not"
1093 " supported for: %s\n",
1094 dev->transport->name);
1095 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1097 ret = target_emulate_write_same(task);
1098 break;
1099 default:
1100 printk(KERN_ERR "Unsupported VARIABLE_LENGTH_CMD SA:"
1101 " 0x%02x\n", service_action);
1102 break;
1104 break;
1105 case SYNCHRONIZE_CACHE:
1106 case 0x91: /* SYNCHRONIZE_CACHE_16: */
1107 if (!dev->transport->do_sync_cache) {
1108 printk(KERN_ERR
1109 "SYNCHRONIZE_CACHE emulation not supported"
1110 " for: %s\n", dev->transport->name);
1111 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1113 dev->transport->do_sync_cache(task);
1114 break;
1115 case ALLOW_MEDIUM_REMOVAL:
1116 case ERASE:
1117 case REZERO_UNIT:
1118 case SEEK_10:
1119 case SPACE:
1120 case START_STOP:
1121 case TEST_UNIT_READY:
1122 case VERIFY:
1123 case WRITE_FILEMARKS:
1124 break;
1125 default:
1126 printk(KERN_ERR "Unsupported SCSI Opcode: 0x%02x for %s\n",
1127 cmd->t_task->t_task_cdb[0], dev->transport->name);
1128 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1131 if (ret < 0)
1132 return ret;
1133 task->task_scsi_status = GOOD;
1134 transport_complete_task(task, 1);
1136 return PYX_TRANSPORT_SENT_TO_TRANSPORT;