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