blob: 9ec64596588ca2728a4b135301d9e3517c6d0ac2 [file] [log] [blame]
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001/*
2 * SCSI Primary Commands (SPC) parsing and emulation.
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2002-2013 Datera, Inc.
Christoph Hellwig88455ec2012-05-20 11:59:13 -04005 *
6 * Nicholas A. Bellinger <nab@kernel.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <asm/unaligned.h>
26
27#include <scsi/scsi.h>
28#include <scsi/scsi_tcq.h>
29
30#include <target/target_core_base.h>
31#include <target/target_core_backend.h>
32#include <target/target_core_fabric.h>
33
34#include "target_core_internal.h"
Nicholas Bellingereba2ca42012-05-30 14:09:10 -070035#include "target_core_alua.h"
Christoph Hellwig88455ec2012-05-20 11:59:13 -040036#include "target_core_pr.h"
37#include "target_core_ua.h"
Nicholas Bellinger04b1b792013-08-22 12:29:59 -070038#include "target_core_xcopy.h"
Christoph Hellwig88455ec2012-05-20 11:59:13 -040039
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040040static void spc_fill_alua_data(struct se_port *port, unsigned char *buf)
41{
42 struct t10_alua_tg_pt_gp *tg_pt_gp;
43 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
44
45 /*
46 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
47 */
48 buf[5] = 0x80;
49
50 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +010051 * Set TPGS field for explicit and/or implicit ALUA access type
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040052 * and opteration.
53 *
54 * See spc4r17 section 6.4.2 Table 135
55 */
56 if (!port)
57 return;
58 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
59 if (!tg_pt_gp_mem)
60 return;
61
62 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
63 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
64 if (tg_pt_gp)
65 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
66 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
67}
68
Hannes Reinecke0dfa1c52012-12-17 09:53:35 +010069sense_reason_t
70spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040071{
72 struct se_lun *lun = cmd->se_lun;
73 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerd2c53042014-04-02 13:27:43 -070074 struct se_session *sess = cmd->se_sess;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040075
76 /* Set RMB (removable media) for tape devices */
77 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
78 buf[1] = 0x80;
79
Christoph Hellwig48c25672012-10-10 17:37:17 -040080 buf[2] = 0x05; /* SPC-3 */
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040081
82 /*
83 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
84 *
85 * SPC4 says:
86 * A RESPONSE DATA FORMAT field set to 2h indicates that the
87 * standard INQUIRY data is in the format defined in this
88 * standard. Response data format values less than 2h are
89 * obsolete. Response data format values greater than 2h are
90 * reserved.
91 */
92 buf[3] = 2;
93
94 /*
95 * Enable SCCS and TPGS fields for Emulated ALUA
96 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -040097 spc_fill_alua_data(lun->lun_sep, buf);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040098
Nicholas Bellingerd397a442013-08-22 14:17:20 -070099 /*
100 * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY
101 */
102 if (dev->dev_attrib.emulate_3pc)
103 buf[5] |= 0x8;
Nicholas Bellingerbdbad2b2013-12-23 20:32:46 +0000104 /*
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700105 * Set Protection (PROTECT) bit when DIF has been enabled on the
Nicholas Bellinger9ef54662015-02-27 22:05:33 -0800106 * device, and the fabric supports VERIFY + PASS. Also report
107 * PROTECT=1 if sess_prot_type has been configured to allow T10-PI
108 * to unprotected devices.
Nicholas Bellingerbdbad2b2013-12-23 20:32:46 +0000109 */
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700110 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
Nicholas Bellinger9ef54662015-02-27 22:05:33 -0800111 if (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type)
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700112 buf[5] |= 0x1;
113 }
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700114
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400115 buf[7] = 0x2; /* CmdQue=1 */
116
Nicholas Bellingeree60bdd2013-07-24 16:15:08 -0700117 memcpy(&buf[8], "LIO-ORG ", 8);
118 memset(&buf[16], 0x20, 16);
119 memcpy(&buf[16], dev->t10_wwn.model,
120 min_t(size_t, strlen(dev->t10_wwn.model), 16));
121 memcpy(&buf[32], dev->t10_wwn.revision,
122 min_t(size_t, strlen(dev->t10_wwn.revision), 4));
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400123 buf[4] = 31; /* Set additional length to 31 */
124
125 return 0;
126}
Hannes Reinecke0dfa1c52012-12-17 09:53:35 +0100127EXPORT_SYMBOL(spc_emulate_inquiry_std);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400128
129/* unit serial number */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800130static sense_reason_t
131spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400132{
133 struct se_device *dev = cmd->se_dev;
Christophe Vu-Brugier9aff64e2014-06-06 17:15:15 +0200134 u16 len;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400135
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400136 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
Christophe Vu-Brugier9aff64e2014-06-06 17:15:15 +0200137 len = sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400138 len++; /* Extra Byte for NULL Terminator */
139 buf[3] = len;
140 }
141 return 0;
142}
143
Nicholas Bellinger68366022013-08-20 17:02:21 -0700144void spc_parse_naa_6h_vendor_specific(struct se_device *dev,
145 unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400146{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400147 unsigned char *p = &dev->t10_wwn.unit_serial[0];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400148 int cnt;
149 bool next = true;
150
151 /*
152 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
153 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
154 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
155 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
156 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
157 * per device uniqeness.
158 */
159 for (cnt = 0; *p && cnt < 13; p++) {
160 int val = hex_to_bin(*p);
161
162 if (val < 0)
163 continue;
164
165 if (next) {
166 next = false;
167 buf[cnt++] |= val;
168 } else {
169 next = true;
170 buf[cnt] = val << 4;
171 }
172 }
173}
174
175/*
176 * Device identification VPD, for a complete list of
177 * DESIGNATOR TYPEs see spc4r17 Table 459.
178 */
Hannes Reinecke0dfa1c52012-12-17 09:53:35 +0100179sense_reason_t
Christoph Hellwigde103c92012-11-06 12:24:09 -0800180spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400181{
182 struct se_device *dev = cmd->se_dev;
183 struct se_lun *lun = cmd->se_lun;
184 struct se_port *port = NULL;
185 struct se_portal_group *tpg = NULL;
186 struct t10_alua_lu_gp_member *lu_gp_mem;
187 struct t10_alua_tg_pt_gp *tg_pt_gp;
188 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400189 unsigned char *prod = &dev->t10_wwn.model[0];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400190 u32 prod_len;
191 u32 unit_serial_len, off = 0;
192 u16 len = 0, id_len;
193
194 off = 4;
195
196 /*
197 * NAA IEEE Registered Extended Assigned designator format, see
198 * spc4r17 section 7.7.3.6.5
199 *
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.
203 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400204 if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL))
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400205 goto check_t10_vend_desc;
206
207 /* CODE SET == Binary */
208 buf[off++] = 0x1;
209
210 /* Set ASSOCIATION == addressed logical unit: 0)b */
211 buf[off] = 0x00;
212
213 /* Identifier/Designator type == NAA identifier */
214 buf[off++] |= 0x3;
215 off++;
216
217 /* Identifier/Designator length */
218 buf[off++] = 0x10;
219
220 /*
221 * Start NAA IEEE Registered Extended Identifier/Designator
222 */
223 buf[off++] = (0x6 << 4);
224
225 /*
226 * Use OpenFabrics IEEE Company ID: 00 14 05
227 */
228 buf[off++] = 0x01;
229 buf[off++] = 0x40;
230 buf[off] = (0x5 << 4);
231
232 /*
233 * Return ConfigFS Unit Serial Number information for
234 * VENDOR_SPECIFIC_IDENTIFIER and
235 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
236 */
237 spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
238
239 len = 20;
240 off = (len + 4);
241
242check_t10_vend_desc:
243 /*
244 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
245 */
246 id_len = 8; /* For Vendor field */
247 prod_len = 4; /* For VPD Header */
248 prod_len += 8; /* For Vendor field */
249 prod_len += strlen(prod);
250 prod_len++; /* For : */
251
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400252 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
253 unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400254 unit_serial_len++; /* For NULL Terminator */
255
256 id_len += sprintf(&buf[off+12], "%s:%s", prod,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400257 &dev->t10_wwn.unit_serial[0]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400258 }
259 buf[off] = 0x2; /* ASCII */
260 buf[off+1] = 0x1; /* T10 Vendor ID */
261 buf[off+2] = 0x0;
262 memcpy(&buf[off+4], "LIO-ORG", 8);
263 /* Extra Byte for NULL Terminator */
264 id_len++;
265 /* Identifier Length */
266 buf[off+3] = id_len;
267 /* Header size for Designation descriptor */
268 len += (id_len + 4);
269 off += (id_len + 4);
270 /*
271 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
272 */
273 port = lun->lun_sep;
274 if (port) {
275 struct t10_alua_lu_gp *lu_gp;
Hannes Reineckefbfe8582013-12-17 09:18:48 +0100276 u32 padding, scsi_name_len, scsi_target_len;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400277 u16 lu_gp_id = 0;
278 u16 tg_pt_gp_id = 0;
279 u16 tpgt;
280
281 tpg = port->sep_tpg;
282 /*
283 * Relative target port identifer, see spc4r17
284 * section 7.7.3.7
285 *
286 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
287 * section 7.5.1 Table 362
288 */
289 buf[off] =
290 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
291 buf[off++] |= 0x1; /* CODE SET == Binary */
292 buf[off] = 0x80; /* Set PIV=1 */
293 /* Set ASSOCIATION == target port: 01b */
294 buf[off] |= 0x10;
295 /* DESIGNATOR TYPE == Relative target port identifer */
296 buf[off++] |= 0x4;
297 off++; /* Skip over Reserved */
298 buf[off++] = 4; /* DESIGNATOR LENGTH */
299 /* Skip over Obsolete field in RTPI payload
300 * in Table 472 */
301 off += 2;
302 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
303 buf[off++] = (port->sep_rtpi & 0xff);
304 len += 8; /* Header size + Designation descriptor */
305 /*
306 * Target port group identifier, see spc4r17
307 * section 7.7.3.8
308 *
309 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
310 * section 7.5.1 Table 362
311 */
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400312 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
313 if (!tg_pt_gp_mem)
314 goto check_lu_gp;
315
316 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
317 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
318 if (!tg_pt_gp) {
319 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
320 goto check_lu_gp;
321 }
322 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
323 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
324
325 buf[off] =
326 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
327 buf[off++] |= 0x1; /* CODE SET == Binary */
328 buf[off] = 0x80; /* Set PIV=1 */
329 /* Set ASSOCIATION == target port: 01b */
330 buf[off] |= 0x10;
331 /* DESIGNATOR TYPE == Target port group identifier */
332 buf[off++] |= 0x5;
333 off++; /* Skip over Reserved */
334 buf[off++] = 4; /* DESIGNATOR LENGTH */
335 off += 2; /* Skip over Reserved Field */
336 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
337 buf[off++] = (tg_pt_gp_id & 0xff);
338 len += 8; /* Header size + Designation descriptor */
339 /*
340 * Logical Unit Group identifier, see spc4r17
341 * section 7.7.3.8
342 */
343check_lu_gp:
344 lu_gp_mem = dev->dev_alua_lu_gp_mem;
345 if (!lu_gp_mem)
346 goto check_scsi_name;
347
348 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
349 lu_gp = lu_gp_mem->lu_gp;
350 if (!lu_gp) {
351 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
352 goto check_scsi_name;
353 }
354 lu_gp_id = lu_gp->lu_gp_id;
355 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
356
357 buf[off++] |= 0x1; /* CODE SET == Binary */
358 /* DESIGNATOR TYPE == Logical Unit Group identifier */
359 buf[off++] |= 0x6;
360 off++; /* Skip over Reserved */
361 buf[off++] = 4; /* DESIGNATOR LENGTH */
362 off += 2; /* Skip over Reserved Field */
363 buf[off++] = ((lu_gp_id >> 8) & 0xff);
364 buf[off++] = (lu_gp_id & 0xff);
365 len += 8; /* Header size + Designation descriptor */
366 /*
367 * SCSI name string designator, see spc4r17
368 * section 7.7.3.11
369 *
370 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
371 * section 7.5.1 Table 362
372 */
373check_scsi_name:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400374 buf[off] =
375 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
376 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
377 buf[off] = 0x80; /* Set PIV=1 */
378 /* Set ASSOCIATION == target port: 01b */
379 buf[off] |= 0x10;
380 /* DESIGNATOR TYPE == SCSI name string */
381 buf[off++] |= 0x8;
382 off += 2; /* Skip over Reserved and length */
383 /*
384 * SCSI name string identifer containing, $FABRIC_MOD
385 * dependent information. For LIO-Target and iSCSI
386 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
387 * UTF-8 encoding.
388 */
389 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
390 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
391 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
392 scsi_name_len += 1 /* Include NULL terminator */;
393 /*
394 * The null-terminated, null-padded (see 4.4.2) SCSI
395 * NAME STRING field contains a UTF-8 format string.
396 * The number of bytes in the SCSI NAME STRING field
397 * (i.e., the value in the DESIGNATOR LENGTH field)
398 * shall be no larger than 256 and shall be a multiple
399 * of four.
400 */
Hannes Reinecke03ba84c2013-12-17 09:18:47 +0100401 padding = ((-scsi_name_len) & 3);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400402 if (padding)
403 scsi_name_len += padding;
Hannes Reinecke03ba84c2013-12-17 09:18:47 +0100404 if (scsi_name_len > 256)
405 scsi_name_len = 256;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400406
407 buf[off-1] = scsi_name_len;
408 off += scsi_name_len;
409 /* Header size + Designation descriptor */
410 len += (scsi_name_len + 4);
Hannes Reineckefbfe8582013-12-17 09:18:48 +0100411
412 /*
413 * Target device designator
414 */
415 buf[off] =
416 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
417 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
418 buf[off] = 0x80; /* Set PIV=1 */
419 /* Set ASSOCIATION == target device: 10b */
420 buf[off] |= 0x20;
421 /* DESIGNATOR TYPE == SCSI name string */
422 buf[off++] |= 0x8;
423 off += 2; /* Skip over Reserved and length */
424 /*
425 * SCSI name string identifer containing, $FABRIC_MOD
426 * dependent information. For LIO-Target and iSCSI
427 * Target Port, this means "<iSCSI name>" in
428 * UTF-8 encoding.
429 */
430 scsi_target_len = sprintf(&buf[off], "%s",
431 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
432 scsi_target_len += 1 /* Include NULL terminator */;
433 /*
434 * The null-terminated, null-padded (see 4.4.2) SCSI
435 * NAME STRING field contains a UTF-8 format string.
436 * The number of bytes in the SCSI NAME STRING field
437 * (i.e., the value in the DESIGNATOR LENGTH field)
438 * shall be no larger than 256 and shall be a multiple
439 * of four.
440 */
441 padding = ((-scsi_target_len) & 3);
442 if (padding)
443 scsi_target_len += padding;
Roland Dreier6a16d7b2014-02-03 00:35:03 -0800444 if (scsi_target_len > 256)
445 scsi_target_len = 256;
Hannes Reineckefbfe8582013-12-17 09:18:48 +0100446
447 buf[off-1] = scsi_target_len;
448 off += scsi_target_len;
449
450 /* Header size + Designation descriptor */
451 len += (scsi_target_len + 4);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400452 }
453 buf[2] = ((len >> 8) & 0xff);
454 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
455 return 0;
456}
Hannes Reinecke0dfa1c52012-12-17 09:53:35 +0100457EXPORT_SYMBOL(spc_emulate_evpd_83);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400458
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800459static bool
460spc_check_dev_wce(struct se_device *dev)
461{
462 bool wce = false;
463
464 if (dev->transport->get_write_cache)
465 wce = dev->transport->get_write_cache(dev);
466 else if (dev->dev_attrib.emulate_write_cache > 0)
467 wce = true;
468
469 return wce;
470}
471
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400472/* Extended INQUIRY Data VPD Page */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800473static sense_reason_t
474spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400475{
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800476 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700477 struct se_session *sess = cmd->se_sess;
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800478
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400479 buf[3] = 0x3c;
Nicholas Bellinger43bb95c2013-12-23 20:33:21 +0000480 /*
481 * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK
482 * only for TYPE3 protection.
483 */
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700484 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
Nicholas Bellinger9ef54662015-02-27 22:05:33 -0800485 if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT ||
486 cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE1_PROT)
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700487 buf[4] = 0x5;
Nicholas Bellinger9ef54662015-02-27 22:05:33 -0800488 else if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE3_PROT ||
489 cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE3_PROT)
Nicholas Bellingerd2c53042014-04-02 13:27:43 -0700490 buf[4] = 0x4;
491 }
Nicholas Bellinger43bb95c2013-12-23 20:33:21 +0000492
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400493 /* Set HEADSUP, ORDSUP, SIMPSUP */
494 buf[5] = 0x07;
495
496 /* If WriteCache emulation is enabled, set V_SUP */
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800497 if (spc_check_dev_wce(dev))
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400498 buf[6] = 0x01;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100499 /* If an LBA map is present set R_SUP */
500 spin_lock(&cmd->se_dev->t10_alua.lba_map_lock);
501 if (!list_empty(&dev->t10_alua.lba_map_list))
502 buf[8] = 0x10;
503 spin_unlock(&cmd->se_dev->t10_alua.lba_map_lock);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400504 return 0;
505}
506
507/* Block Limits VPD page */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800508static sense_reason_t
509spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400510{
511 struct se_device *dev = cmd->se_dev;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400512 int have_tp = 0;
Andy Grover7f7caf62013-11-11 08:59:17 -0800513 int opt, min;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400514
515 /*
516 * Following spc3r22 section 6.5.3 Block Limits VPD page, when
517 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
518 * different page length for Thin Provisioning.
519 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400520 if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400521 have_tp = 1;
522
523 buf[0] = dev->transport->get_device_type(dev);
524 buf[3] = have_tp ? 0x3c : 0x10;
525
526 /* Set WSNZ to 1 */
527 buf[4] = 0x01;
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700528 /*
529 * Set MAXIMUM COMPARE AND WRITE LENGTH
530 */
531 if (dev->dev_attrib.emulate_caw)
532 buf[5] = 0x01;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400533
534 /*
535 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
536 */
Andy Grover7f7caf62013-11-11 08:59:17 -0800537 if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev)))
538 put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]);
539 else
540 put_unaligned_be16(1, &buf[6]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400541
542 /*
543 * Set MAXIMUM TRANSFER LENGTH
544 */
Nicholas Bellinger046ba642015-01-06 16:10:37 -0800545 put_unaligned_be32(dev->dev_attrib.hw_max_sectors, &buf[8]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400546
547 /*
548 * Set OPTIMAL TRANSFER LENGTH
549 */
Andy Grover7f7caf62013-11-11 08:59:17 -0800550 if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev)))
551 put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]);
552 else
553 put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400554
555 /*
556 * Exit now if we don't support TP.
557 */
558 if (!have_tp)
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800559 goto max_write_same;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400560
561 /*
562 * Set MAXIMUM UNMAP LBA COUNT
563 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400564 put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400565
566 /*
567 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
568 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400569 put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400570 &buf[24]);
571
572 /*
573 * Set OPTIMAL UNMAP GRANULARITY
574 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400575 put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400576
577 /*
578 * UNMAP GRANULARITY ALIGNMENT
579 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400580 put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400581 &buf[32]);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400582 if (dev->dev_attrib.unmap_granularity_alignment != 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400583 buf[32] |= 0x80; /* Set the UGAVALID bit */
584
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800585 /*
586 * MAXIMUM WRITE SAME LENGTH
587 */
588max_write_same:
589 put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
590
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400591 return 0;
592}
593
594/* Block Device Characteristics VPD page */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800595static sense_reason_t
596spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400597{
598 struct se_device *dev = cmd->se_dev;
599
600 buf[0] = dev->transport->get_device_type(dev);
601 buf[3] = 0x3c;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400602 buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400603
604 return 0;
605}
606
607/* Thin Provisioning VPD */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800608static sense_reason_t
609spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400610{
611 struct se_device *dev = cmd->se_dev;
612
613 /*
614 * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
615 *
616 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
617 * zero, then the page length shall be set to 0004h. If the DP bit
618 * is set to one, then the page length shall be set to the value
619 * defined in table 162.
620 */
621 buf[0] = dev->transport->get_device_type(dev);
622
623 /*
624 * Set Hardcoded length mentioned above for DP=0
625 */
626 put_unaligned_be16(0x0004, &buf[2]);
627
628 /*
629 * The THRESHOLD EXPONENT field indicates the threshold set size in
630 * LBAs as a power of 2 (i.e., the threshold set size is equal to
631 * 2(threshold exponent)).
632 *
633 * Note that this is currently set to 0x00 as mkp says it will be
634 * changing again. We can enable this once it has settled in T10
635 * and is actually used by Linux/SCSI ML code.
636 */
637 buf[4] = 0x00;
638
639 /*
640 * A TPU bit set to one indicates that the device server supports
641 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
642 * that the device server does not support the UNMAP command.
643 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400644 if (dev->dev_attrib.emulate_tpu != 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400645 buf[5] = 0x80;
646
647 /*
648 * A TPWS bit set to one indicates that the device server supports
649 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
650 * A TPWS bit set to zero indicates that the device server does not
651 * support the use of the WRITE SAME (16) command to unmap LBAs.
652 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400653 if (dev->dev_attrib.emulate_tpws != 0)
Nicholas Bellingeraa04dae2015-02-14 02:05:31 +0000654 buf[5] |= 0x40 | 0x20;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400655
656 return 0;
657}
658
Hannes Reineckec66094b2013-12-17 09:18:49 +0100659/* Referrals VPD page */
660static sense_reason_t
661spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf)
662{
663 struct se_device *dev = cmd->se_dev;
664
665 buf[0] = dev->transport->get_device_type(dev);
666 buf[3] = 0x0c;
667 put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]);
Sebastian Herbszt096b499512014-08-30 00:31:11 +0200668 put_unaligned_be32(dev->t10_alua.lba_map_segment_multiplier, &buf[12]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100669
670 return 0;
671}
672
Christoph Hellwigde103c92012-11-06 12:24:09 -0800673static sense_reason_t
674spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400675
676static struct {
677 uint8_t page;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800678 sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400679} evpd_handlers[] = {
680 { .page = 0x00, .emulate = spc_emulate_evpd_00 },
681 { .page = 0x80, .emulate = spc_emulate_evpd_80 },
682 { .page = 0x83, .emulate = spc_emulate_evpd_83 },
683 { .page = 0x86, .emulate = spc_emulate_evpd_86 },
684 { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
685 { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
686 { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
Hannes Reineckec66094b2013-12-17 09:18:49 +0100687 { .page = 0xb3, .emulate = spc_emulate_evpd_b3 },
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400688};
689
690/* supported vital product data pages */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800691static sense_reason_t
692spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400693{
694 int p;
695
696 /*
697 * Only report the INQUIRY EVPD=1 pages after a valid NAA
698 * Registered Extended LUN WWN has been set via ConfigFS
699 * during device creation/restart.
700 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400701 if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400702 buf[3] = ARRAY_SIZE(evpd_handlers);
703 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
704 buf[p + 4] = evpd_handlers[p].page;
705 }
706
707 return 0;
708}
709
Christoph Hellwigde103c92012-11-06 12:24:09 -0800710static sense_reason_t
711spc_emulate_inquiry(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400712{
713 struct se_device *dev = cmd->se_dev;
714 struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
Paolo Bonziniffe7b0e2012-09-07 17:30:38 +0200715 unsigned char *rbuf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400716 unsigned char *cdb = cmd->t_task_cdb;
Nicholas Bellingerf82f3202013-12-19 14:13:28 -0800717 unsigned char *buf;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800718 sense_reason_t ret;
719 int p;
Roland Dreier2426bd42014-06-10 11:07:47 -0700720 int len = 0;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400721
Nicholas Bellingerf82f3202013-12-19 14:13:28 -0800722 buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
723 if (!buf) {
724 pr_err("Unable to allocate response buffer for INQUIRY\n");
725 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
726 }
Nicholas Bellingerdea5f092012-10-31 22:04:26 -0700727
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400728 if (dev == tpg->tpg_virt_lun0.lun_se_dev)
729 buf[0] = 0x3f; /* Not connected */
730 else
731 buf[0] = dev->transport->get_device_type(dev);
732
733 if (!(cdb[1] & 0x1)) {
734 if (cdb[2]) {
735 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
736 cdb[2]);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800737 ret = TCM_INVALID_CDB_FIELD;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400738 goto out;
739 }
740
741 ret = spc_emulate_inquiry_std(cmd, buf);
Roland Dreier2426bd42014-06-10 11:07:47 -0700742 len = buf[4] + 5;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400743 goto out;
744 }
745
746 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
747 if (cdb[2] == evpd_handlers[p].page) {
748 buf[1] = cdb[2];
749 ret = evpd_handlers[p].emulate(cmd, buf);
Roland Dreier2426bd42014-06-10 11:07:47 -0700750 len = get_unaligned_be16(&buf[2]) + 4;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400751 goto out;
752 }
753 }
754
755 pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800756 ret = TCM_INVALID_CDB_FIELD;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400757
758out:
Paolo Bonziniffe7b0e2012-09-07 17:30:38 +0200759 rbuf = transport_kmap_data_sg(cmd);
Nicholas Bellinger49df9fc2013-01-29 13:33:05 -0800760 if (rbuf) {
Nicholas Bellingerf82f3202013-12-19 14:13:28 -0800761 memcpy(rbuf, buf, min_t(u32, SE_INQUIRY_BUF, cmd->data_length));
Nicholas Bellinger49df9fc2013-01-29 13:33:05 -0800762 transport_kunmap_data_sg(cmd);
763 }
Nicholas Bellingerf82f3202013-12-19 14:13:28 -0800764 kfree(buf);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400765
766 if (!ret)
Roland Dreier2426bd42014-06-10 11:07:47 -0700767 target_complete_cmd_with_length(cmd, GOOD, len);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400768 return ret;
769}
770
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700771static int spc_modesense_rwrecovery(struct se_cmd *cmd, u8 pc, u8 *p)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400772{
773 p[0] = 0x01;
774 p[1] = 0x0a;
775
Roland Dreierd4b2b862012-10-31 09:16:48 -0700776 /* No changeable values for now */
777 if (pc == 1)
778 goto out;
779
780out:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400781 return 12;
782}
783
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700784static int spc_modesense_control(struct se_cmd *cmd, u8 pc, u8 *p)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400785{
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700786 struct se_device *dev = cmd->se_dev;
787 struct se_session *sess = cmd->se_sess;
788
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400789 p[0] = 0x0a;
790 p[1] = 0x0a;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700791
792 /* No changeable values for now */
793 if (pc == 1)
794 goto out;
795
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400796 p[2] = 2;
797 /*
798 * From spc4r23, 7.4.7 Control mode page
799 *
800 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
801 * restrictions on the algorithm used for reordering commands
802 * having the SIMPLE task attribute (see SAM-4).
803 *
804 * Table 368 -- QUEUE ALGORITHM MODIFIER field
805 * Code Description
806 * 0h Restricted reordering
807 * 1h Unrestricted reordering allowed
808 * 2h to 7h Reserved
809 * 8h to Fh Vendor specific
810 *
811 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
812 * the device server shall order the processing sequence of commands
813 * having the SIMPLE task attribute such that data integrity is maintained
814 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
815 * requests is halted at any time, the final value of all data observable
816 * on the medium shall be the same as if all the commands had been processed
817 * with the ORDERED task attribute).
818 *
819 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
820 * device server may reorder the processing sequence of commands having the
821 * SIMPLE task attribute in any manner. Any data integrity exposures related to
822 * command sequence order shall be explicitly handled by the application client
823 * through the selection of appropriate ommands and task attributes.
824 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400825 p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400826 /*
827 * From spc4r17, section 7.4.6 Control mode Page
828 *
829 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
830 *
831 * 00b: The logical unit shall clear any unit attention condition
832 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
833 * status and shall not establish a unit attention condition when a com-
834 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
835 * status.
836 *
837 * 10b: The logical unit shall not clear any unit attention condition
838 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
839 * status and shall not establish a unit attention condition when
840 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
841 * CONFLICT status.
842 *
843 * 11b a The logical unit shall not clear any unit attention condition
844 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
845 * status and shall establish a unit attention condition for the
846 * initiator port associated with the I_T nexus on which the BUSY,
847 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
848 * Depending on the status, the additional sense code shall be set to
849 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
850 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
851 * command, a unit attention condition shall be established only once
852 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
853 * to the number of commands completed with one of those status codes.
854 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400855 p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
856 (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400857 /*
858 * From spc4r17, section 7.4.6 Control mode Page
859 *
860 * Task Aborted Status (TAS) bit set to zero.
861 *
862 * A task aborted status (TAS) bit set to zero specifies that aborted
863 * tasks shall be terminated by the device server without any response
864 * to the application client. A TAS bit set to one specifies that tasks
865 * aborted by the actions of an I_T nexus other than the I_T nexus on
866 * which the command was received shall be completed with TASK ABORTED
867 * status (see SAM-4).
868 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400869 p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
Nicholas Bellinger0c30f422013-12-23 21:23:34 +0000870 /*
871 * From spc4r30, section 7.5.7 Control mode page
872 *
873 * Application Tag Owner (ATO) bit set to one.
874 *
875 * If the ATO bit is set to one the device server shall not modify the
876 * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection
877 * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE
878 * TAG field.
879 */
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700880 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
Nicholas Bellinger9ef54662015-02-27 22:05:33 -0800881 if (dev->dev_attrib.pi_prot_type || sess->sess_prot_type)
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700882 p[5] |= 0x80;
883 }
Nicholas Bellinger0c30f422013-12-23 21:23:34 +0000884
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400885 p[8] = 0xff;
886 p[9] = 0xff;
887 p[11] = 30;
888
Roland Dreierd4b2b862012-10-31 09:16:48 -0700889out:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400890 return 12;
891}
892
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700893static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400894{
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700895 struct se_device *dev = cmd->se_dev;
896
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400897 p[0] = 0x08;
898 p[1] = 0x12;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700899
900 /* No changeable values for now */
901 if (pc == 1)
902 goto out;
903
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800904 if (spc_check_dev_wce(dev))
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400905 p[2] = 0x04; /* Write Cache Enable */
906 p[12] = 0x20; /* Disabled Read Ahead */
907
Roland Dreierd4b2b862012-10-31 09:16:48 -0700908out:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400909 return 20;
910}
911
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700912static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p)
Roland Dreier0f6d64c2012-10-31 09:16:49 -0700913{
914 p[0] = 0x1c;
915 p[1] = 0x0a;
916
917 /* No changeable values for now */
918 if (pc == 1)
919 goto out;
920
921out:
922 return 12;
923}
924
Roland Dreierd4b2b862012-10-31 09:16:48 -0700925static struct {
926 uint8_t page;
927 uint8_t subpage;
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -0700928 int (*emulate)(struct se_cmd *, u8, unsigned char *);
Roland Dreierd4b2b862012-10-31 09:16:48 -0700929} modesense_handlers[] = {
930 { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
931 { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
932 { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
Roland Dreier0f6d64c2012-10-31 09:16:49 -0700933 { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
Roland Dreierd4b2b862012-10-31 09:16:48 -0700934};
935
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400936static void spc_modesense_write_protect(unsigned char *buf, int type)
937{
938 /*
939 * I believe that the WP bit (bit 7) in the mode header is the same for
940 * all device types..
941 */
942 switch (type) {
943 case TYPE_DISK:
944 case TYPE_TAPE:
945 default:
946 buf[0] |= 0x80; /* WP bit */
947 break;
948 }
949}
950
951static void spc_modesense_dpofua(unsigned char *buf, int type)
952{
953 switch (type) {
954 case TYPE_DISK:
955 buf[0] |= 0x10; /* DPOFUA bit */
956 break;
957 default:
958 break;
959 }
960}
961
Roland Dreierd4b2b862012-10-31 09:16:48 -0700962static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
963{
964 *buf++ = 8;
965 put_unaligned_be32(min(blocks, 0xffffffffull), buf);
966 buf += 4;
967 put_unaligned_be32(block_size, buf);
968 return 9;
969}
970
971static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
972{
973 if (blocks <= 0xffffffff)
974 return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
975
976 *buf++ = 1; /* LONGLBA */
977 buf += 2;
978 *buf++ = 16;
979 put_unaligned_be64(blocks, buf);
980 buf += 12;
981 put_unaligned_be32(block_size, buf);
982
983 return 17;
984}
985
Christoph Hellwigde103c92012-11-06 12:24:09 -0800986static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400987{
988 struct se_device *dev = cmd->se_dev;
989 char *cdb = cmd->t_task_cdb;
Nicholas Bellingercab96092013-01-29 14:10:01 -0800990 unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400991 int type = dev->transport->get_device_type(dev);
992 int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
Roland Dreierd4b2b862012-10-31 09:16:48 -0700993 bool dbd = !!(cdb[1] & 0x08);
994 bool llba = ten ? !!(cdb[1] & 0x10) : false;
995 u8 pc = cdb[2] >> 6;
996 u8 page = cdb[2] & 0x3f;
997 u8 subpage = cdb[3];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400998 int length = 0;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700999 int ret;
1000 int i;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001001
Nicholas Bellingercab96092013-01-29 14:10:01 -08001002 memset(buf, 0, SE_MODE_PAGE_BUF);
1003
Nicholas Bellingerfecae402012-11-01 18:43:03 -07001004 /*
1005 * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
1006 * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
1007 */
1008 length = ten ? 3 : 2;
Roland Dreierd4b2b862012-10-31 09:16:48 -07001009
1010 /* DEVICE-SPECIFIC PARAMETER */
1011 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
1012 (cmd->se_deve &&
1013 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
1014 spc_modesense_write_protect(&buf[length], type);
1015
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -08001016 if ((spc_check_dev_wce(dev)) &&
Roland Dreierd4b2b862012-10-31 09:16:48 -07001017 (dev->dev_attrib.emulate_fua_write > 0))
1018 spc_modesense_dpofua(&buf[length], type);
1019
1020 ++length;
1021
1022 /* BLOCK DESCRIPTOR */
1023
1024 /*
1025 * For now we only include a block descriptor for disk (SBC)
1026 * devices; other command sets use a slightly different format.
1027 */
1028 if (!dbd && type == TYPE_DISK) {
1029 u64 blocks = dev->transport->get_blocks(dev);
1030 u32 block_size = dev->dev_attrib.block_size;
1031
1032 if (ten) {
1033 if (llba) {
1034 length += spc_modesense_long_blockdesc(&buf[length],
1035 blocks, block_size);
1036 } else {
1037 length += 3;
1038 length += spc_modesense_blockdesc(&buf[length],
1039 blocks, block_size);
1040 }
1041 } else {
1042 length += spc_modesense_blockdesc(&buf[length], blocks,
1043 block_size);
1044 }
1045 } else {
1046 if (ten)
1047 length += 4;
1048 else
1049 length += 1;
Paolo Bonzini7a3f3692012-09-07 17:30:39 +02001050 }
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001051
Roland Dreierd4b2b862012-10-31 09:16:48 -07001052 if (page == 0x3f) {
1053 if (subpage != 0x00 && subpage != 0xff) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08001054 pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001055 return TCM_INVALID_CDB_FIELD;
Roland Dreierd4b2b862012-10-31 09:16:48 -07001056 }
1057
1058 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
1059 /*
1060 * Tricky way to say all subpage 00h for
1061 * subpage==0, all subpages for subpage==0xff
1062 * (and we just checked above that those are
1063 * the only two possibilities).
1064 */
1065 if ((modesense_handlers[i].subpage & ~subpage) == 0) {
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -07001066 ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]);
Roland Dreierd4b2b862012-10-31 09:16:48 -07001067 if (!ten && length + ret >= 255)
1068 break;
1069 length += ret;
1070 }
1071 }
1072
1073 goto set_length;
1074 }
1075
1076 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
1077 if (modesense_handlers[i].page == page &&
1078 modesense_handlers[i].subpage == subpage) {
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -07001079 length += modesense_handlers[i].emulate(cmd, pc, &buf[length]);
Roland Dreierd4b2b862012-10-31 09:16:48 -07001080 goto set_length;
1081 }
1082
1083 /*
1084 * We don't intend to implement:
1085 * - obsolete page 03h "format parameters" (checked by Solaris)
1086 */
1087 if (page != 0x03)
1088 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
1089 page, subpage);
1090
Christoph Hellwigde103c92012-11-06 12:24:09 -08001091 return TCM_UNKNOWN_MODE_PAGE;
Roland Dreierd4b2b862012-10-31 09:16:48 -07001092
1093set_length:
1094 if (ten)
1095 put_unaligned_be16(length - 2, buf);
1096 else
1097 buf[0] = length - 1;
1098
Nicholas Bellingercab96092013-01-29 14:10:01 -08001099 rbuf = transport_kmap_data_sg(cmd);
1100 if (rbuf) {
1101 memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
1102 transport_kunmap_data_sg(cmd);
Roland Dreierd4b2b862012-10-31 09:16:48 -07001103 }
1104
Roland Dreier2426bd42014-06-10 11:07:47 -07001105 target_complete_cmd_with_length(cmd, GOOD, length);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001106 return 0;
1107}
1108
Christoph Hellwigde103c92012-11-06 12:24:09 -08001109static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001110{
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001111 char *cdb = cmd->t_task_cdb;
1112 bool ten = cdb[0] == MODE_SELECT_10;
1113 int off = ten ? 8 : 4;
1114 bool pf = !!(cdb[1] & 0x10);
1115 u8 page, subpage;
1116 unsigned char *buf;
1117 unsigned char tbuf[SE_MODE_PAGE_BUF];
1118 int length;
Christoph Hellwig3abff1e2015-03-26 12:27:35 +01001119 sense_reason_t ret = 0;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001120 int i;
1121
Roland Dreier71f41fe2013-02-08 15:18:40 -08001122 if (!cmd->data_length) {
1123 target_complete_cmd(cmd, GOOD);
1124 return 0;
1125 }
1126
1127 if (cmd->data_length < off + 2)
1128 return TCM_PARAMETER_LIST_LENGTH_ERROR;
1129
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001130 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001131 if (!buf)
1132 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001133
1134 if (!pf) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08001135 ret = TCM_INVALID_CDB_FIELD;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001136 goto out;
1137 }
1138
1139 page = buf[off] & 0x3f;
1140 subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
1141
1142 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
1143 if (modesense_handlers[i].page == page &&
1144 modesense_handlers[i].subpage == subpage) {
1145 memset(tbuf, 0, SE_MODE_PAGE_BUF);
Nicholas Bellingerd45aca42a2014-04-02 13:28:49 -07001146 length = modesense_handlers[i].emulate(cmd, 0, tbuf);
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001147 goto check_contents;
1148 }
1149
Christoph Hellwigde103c92012-11-06 12:24:09 -08001150 ret = TCM_UNKNOWN_MODE_PAGE;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001151 goto out;
1152
1153check_contents:
Roland Dreier71f41fe2013-02-08 15:18:40 -08001154 if (cmd->data_length < off + length) {
1155 ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
1156 goto out;
1157 }
1158
Christoph Hellwigde103c92012-11-06 12:24:09 -08001159 if (memcmp(buf + off, tbuf, length))
1160 ret = TCM_INVALID_PARAMETER_LIST;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001161
1162out:
1163 transport_kunmap_data_sg(cmd);
1164
1165 if (!ret)
1166 target_complete_cmd(cmd, GOOD);
1167 return ret;
1168}
1169
Christoph Hellwigde103c92012-11-06 12:24:09 -08001170static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001171{
1172 unsigned char *cdb = cmd->t_task_cdb;
Paolo Bonzini32a88112012-09-07 17:30:36 +02001173 unsigned char *rbuf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001174 u8 ua_asc = 0, ua_ascq = 0;
Paolo Bonzini32a88112012-09-07 17:30:36 +02001175 unsigned char buf[SE_SENSE_BUF];
1176
1177 memset(buf, 0, SE_SENSE_BUF);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001178
1179 if (cdb[1] & 0x01) {
1180 pr_err("REQUEST_SENSE description emulation not"
1181 " supported\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001182 return TCM_INVALID_CDB_FIELD;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001183 }
1184
Paolo Bonzini32a88112012-09-07 17:30:36 +02001185 rbuf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001186 if (!rbuf)
1187 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1188
1189 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001190 /*
1191 * CURRENT ERROR, UNIT ATTENTION
1192 */
1193 buf[0] = 0x70;
1194 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
1195
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001196 /*
1197 * The Additional Sense Code (ASC) from the UNIT ATTENTION
1198 */
1199 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
1200 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
1201 buf[7] = 0x0A;
1202 } else {
1203 /*
1204 * CURRENT ERROR, NO SENSE
1205 */
1206 buf[0] = 0x70;
1207 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
1208
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001209 /*
1210 * NO ADDITIONAL SENSE INFORMATION
1211 */
1212 buf[SPC_ASC_KEY_OFFSET] = 0x00;
1213 buf[7] = 0x0A;
1214 }
1215
Christoph Hellwigde103c92012-11-06 12:24:09 -08001216 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
1217 transport_kunmap_data_sg(cmd);
Paolo Bonzini32a88112012-09-07 17:30:36 +02001218
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001219 target_complete_cmd(cmd, GOOD);
1220 return 0;
1221}
1222
Christoph Hellwigde103c92012-11-06 12:24:09 -08001223sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001224{
1225 struct se_dev_entry *deve;
1226 struct se_session *sess = cmd->se_sess;
1227 unsigned char *buf;
1228 u32 lun_count = 0, offset = 8, i;
1229
1230 if (cmd->data_length < 16) {
1231 pr_warn("REPORT LUNS allocation length %u too small\n",
1232 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001233 return TCM_INVALID_CDB_FIELD;
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001234 }
1235
1236 buf = transport_kmap_data_sg(cmd);
1237 if (!buf)
Christoph Hellwigde103c92012-11-06 12:24:09 -08001238 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001239
1240 /*
1241 * If no struct se_session pointer is present, this struct se_cmd is
1242 * coming via a target_core_mod PASSTHROUGH op, and not through
1243 * a $FABRIC_MOD. In that case, report LUN=0 only.
1244 */
1245 if (!sess) {
1246 int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
1247 lun_count = 1;
1248 goto done;
1249 }
1250
1251 spin_lock_irq(&sess->se_node_acl->device_list_lock);
1252 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
1253 deve = sess->se_node_acl->device_list[i];
1254 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
1255 continue;
1256 /*
1257 * We determine the correct LUN LIST LENGTH even once we
1258 * have reached the initial allocation length.
1259 * See SPC2-R20 7.19.
1260 */
1261 lun_count++;
1262 if ((offset + 8) > cmd->data_length)
1263 continue;
1264
1265 int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
1266 offset += 8;
1267 }
1268 spin_unlock_irq(&sess->se_node_acl->device_list_lock);
1269
1270 /*
1271 * See SPC3 r07, page 159.
1272 */
1273done:
1274 lun_count *= 8;
1275 buf[0] = ((lun_count >> 24) & 0xff);
1276 buf[1] = ((lun_count >> 16) & 0xff);
1277 buf[2] = ((lun_count >> 8) & 0xff);
1278 buf[3] = (lun_count & 0xff);
1279 transport_kunmap_data_sg(cmd);
1280
Roland Dreier2426bd42014-06-10 11:07:47 -07001281 target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8);
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001282 return 0;
1283}
Christoph Hellwig8de530a2012-10-07 10:55:52 -04001284EXPORT_SYMBOL(spc_emulate_report_luns);
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001285
Christoph Hellwigde103c92012-11-06 12:24:09 -08001286static sense_reason_t
1287spc_emulate_testunitready(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001288{
1289 target_complete_cmd(cmd, GOOD);
1290 return 0;
1291}
1292
Christoph Hellwigde103c92012-11-06 12:24:09 -08001293sense_reason_t
1294spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001295{
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001296 struct se_device *dev = cmd->se_dev;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001297 unsigned char *cdb = cmd->t_task_cdb;
1298
1299 switch (cdb[0]) {
1300 case MODE_SELECT:
1301 *size = cdb[4];
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001302 cmd->execute_cmd = spc_emulate_modeselect;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001303 break;
1304 case MODE_SELECT_10:
1305 *size = (cdb[7] << 8) + cdb[8];
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001306 cmd->execute_cmd = spc_emulate_modeselect;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001307 break;
1308 case MODE_SENSE:
1309 *size = cdb[4];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001310 cmd->execute_cmd = spc_emulate_modesense;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001311 break;
1312 case MODE_SENSE_10:
1313 *size = (cdb[7] << 8) + cdb[8];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001314 cmd->execute_cmd = spc_emulate_modesense;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001315 break;
1316 case LOG_SELECT:
1317 case LOG_SENSE:
1318 *size = (cdb[7] << 8) + cdb[8];
1319 break;
1320 case PERSISTENT_RESERVE_IN:
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001321 *size = (cdb[7] << 8) + cdb[8];
Christoph Hellwigd977f432012-10-10 17:37:15 -04001322 cmd->execute_cmd = target_scsi3_emulate_pr_in;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001323 break;
1324 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001325 *size = (cdb[7] << 8) + cdb[8];
Christoph Hellwigd977f432012-10-10 17:37:15 -04001326 cmd->execute_cmd = target_scsi3_emulate_pr_out;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001327 break;
1328 case RELEASE:
1329 case RELEASE_10:
1330 if (cdb[0] == RELEASE_10)
1331 *size = (cdb[7] << 8) | cdb[8];
1332 else
1333 *size = cmd->data_length;
1334
Christoph Hellwigd977f432012-10-10 17:37:15 -04001335 cmd->execute_cmd = target_scsi2_reservation_release;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001336 break;
1337 case RESERVE:
1338 case RESERVE_10:
1339 /*
1340 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
1341 * Assume the passthrough or $FABRIC_MOD will tell us about it.
1342 */
1343 if (cdb[0] == RESERVE_10)
1344 *size = (cdb[7] << 8) | cdb[8];
1345 else
1346 *size = cmd->data_length;
1347
Christoph Hellwigd977f432012-10-10 17:37:15 -04001348 cmd->execute_cmd = target_scsi2_reservation_reserve;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001349 break;
1350 case REQUEST_SENSE:
1351 *size = cdb[4];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001352 cmd->execute_cmd = spc_emulate_request_sense;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001353 break;
1354 case INQUIRY:
1355 *size = (cdb[3] << 8) + cdb[4];
1356
1357 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001358 * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001359 * See spc4r17 section 5.3
1360 */
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001361 cmd->sam_task_attr = TCM_HEAD_TAG;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001362 cmd->execute_cmd = spc_emulate_inquiry;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001363 break;
1364 case SECURITY_PROTOCOL_IN:
1365 case SECURITY_PROTOCOL_OUT:
1366 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1367 break;
1368 case EXTENDED_COPY:
Nicholas Bellinger04b1b792013-08-22 12:29:59 -07001369 *size = get_unaligned_be32(&cdb[10]);
1370 cmd->execute_cmd = target_do_xcopy;
1371 break;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001372 case RECEIVE_COPY_RESULTS:
Nicholas Bellinger04b1b792013-08-22 12:29:59 -07001373 *size = get_unaligned_be32(&cdb[10]);
1374 cmd->execute_cmd = target_do_receive_copy_results;
1375 break;
1376 case READ_ATTRIBUTE:
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001377 case WRITE_ATTRIBUTE:
1378 *size = (cdb[10] << 24) | (cdb[11] << 16) |
1379 (cdb[12] << 8) | cdb[13];
1380 break;
1381 case RECEIVE_DIAGNOSTIC:
1382 case SEND_DIAGNOSTIC:
1383 *size = (cdb[3] << 8) | cdb[4];
1384 break;
1385 case WRITE_BUFFER:
1386 *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
1387 break;
1388 case REPORT_LUNS:
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001389 cmd->execute_cmd = spc_emulate_report_luns;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001390 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1391 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001392 * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001393 * See spc4r17 section 5.3
1394 */
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001395 cmd->sam_task_attr = TCM_HEAD_TAG;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001396 break;
1397 case TEST_UNIT_READY:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001398 cmd->execute_cmd = spc_emulate_testunitready;
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001399 *size = 0;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001400 break;
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001401 case MAINTENANCE_IN:
1402 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1403 /*
1404 * MAINTENANCE_IN from SCC-2
1405 * Check for emulated MI_REPORT_TARGET_PGS
1406 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001407 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001408 cmd->execute_cmd =
1409 target_emulate_report_target_port_groups;
1410 }
1411 *size = get_unaligned_be32(&cdb[6]);
1412 } else {
1413 /*
1414 * GPCMD_SEND_KEY from multi media commands
1415 */
1416 *size = get_unaligned_be16(&cdb[8]);
1417 }
1418 break;
1419 case MAINTENANCE_OUT:
1420 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1421 /*
1422 * MAINTENANCE_OUT from SCC-2
1423 * Check for emulated MO_SET_TARGET_PGS.
1424 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001425 if (cdb[1] == MO_SET_TARGET_PGS) {
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001426 cmd->execute_cmd =
1427 target_emulate_set_target_port_groups;
1428 }
1429 *size = get_unaligned_be32(&cdb[6]);
1430 } else {
1431 /*
1432 * GPCMD_SEND_KEY from multi media commands
1433 */
1434 *size = get_unaligned_be16(&cdb[8]);
1435 }
1436 break;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001437 default:
1438 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
1439 " 0x%02x, sending CHECK_CONDITION.\n",
1440 cmd->se_tfo->get_fabric_name(), cdb[0]);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001441 return TCM_UNSUPPORTED_SCSI_OPCODE;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001442 }
1443
1444 return 0;
1445}
1446EXPORT_SYMBOL(spc_parse_cdb);