iscsi-target: make some temporary buffers larger
[deliverable/linux.git] / drivers / target / target_core_spc.c
CommitLineData
88455ec4
CH
1/*
2 * SCSI Primary Commands (SPC) parsing and emulation.
3 *
fd9a11d7 4 * (c) Copyright 2002-2012 RisingTide Systems LLC.
88455ec4
CH
5 *
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"
eba2ca45 35#include "target_core_alua.h"
88455ec4
CH
36#include "target_core_pr.h"
37#include "target_core_ua.h"
38
39
1fd032ee
CH
40static 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 /*
51 * Set TPGS field for explict and/or implict ALUA access type
52 * 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
0dfa1c5d
HR
69sense_reason_t
70spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
71{
72 struct se_lun *lun = cmd->se_lun;
73 struct se_device *dev = cmd->se_dev;
74
75 /* Set RMB (removable media) for tape devices */
76 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
77 buf[1] = 0x80;
78
48c2567d 79 buf[2] = 0x05; /* SPC-3 */
1fd032ee
CH
80
81 /*
82 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
83 *
84 * SPC4 says:
85 * A RESPONSE DATA FORMAT field set to 2h indicates that the
86 * standard INQUIRY data is in the format defined in this
87 * standard. Response data format values less than 2h are
88 * obsolete. Response data format values greater than 2h are
89 * reserved.
90 */
91 buf[3] = 2;
92
93 /*
94 * Enable SCCS and TPGS fields for Emulated ALUA
95 */
c87fbd56 96 spc_fill_alua_data(lun->lun_sep, buf);
1fd032ee
CH
97
98 buf[7] = 0x2; /* CmdQue=1 */
99
100 snprintf(&buf[8], 8, "LIO-ORG");
0fd97ccf
CH
101 snprintf(&buf[16], 16, "%s", dev->t10_wwn.model);
102 snprintf(&buf[32], 4, "%s", dev->t10_wwn.revision);
1fd032ee
CH
103 buf[4] = 31; /* Set additional length to 31 */
104
105 return 0;
106}
0dfa1c5d 107EXPORT_SYMBOL(spc_emulate_inquiry_std);
1fd032ee
CH
108
109/* unit serial number */
de103c93
CH
110static sense_reason_t
111spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
112{
113 struct se_device *dev = cmd->se_dev;
114 u16 len = 0;
115
0fd97ccf 116 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
1fd032ee
CH
117 u32 unit_serial_len;
118
0fd97ccf 119 unit_serial_len = strlen(dev->t10_wwn.unit_serial);
1fd032ee
CH
120 unit_serial_len++; /* For NULL Terminator */
121
0fd97ccf 122 len += sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial);
1fd032ee
CH
123 len++; /* Extra Byte for NULL Terminator */
124 buf[3] = len;
125 }
126 return 0;
127}
128
129static void spc_parse_naa_6h_vendor_specific(struct se_device *dev,
130 unsigned char *buf)
131{
0fd97ccf 132 unsigned char *p = &dev->t10_wwn.unit_serial[0];
1fd032ee
CH
133 int cnt;
134 bool next = true;
135
136 /*
137 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
138 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
139 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
140 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
141 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
142 * per device uniqeness.
143 */
144 for (cnt = 0; *p && cnt < 13; p++) {
145 int val = hex_to_bin(*p);
146
147 if (val < 0)
148 continue;
149
150 if (next) {
151 next = false;
152 buf[cnt++] |= val;
153 } else {
154 next = true;
155 buf[cnt] = val << 4;
156 }
157 }
158}
159
160/*
161 * Device identification VPD, for a complete list of
162 * DESIGNATOR TYPEs see spc4r17 Table 459.
163 */
0dfa1c5d 164sense_reason_t
de103c93 165spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
166{
167 struct se_device *dev = cmd->se_dev;
168 struct se_lun *lun = cmd->se_lun;
169 struct se_port *port = NULL;
170 struct se_portal_group *tpg = NULL;
171 struct t10_alua_lu_gp_member *lu_gp_mem;
172 struct t10_alua_tg_pt_gp *tg_pt_gp;
173 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
0fd97ccf 174 unsigned char *prod = &dev->t10_wwn.model[0];
1fd032ee
CH
175 u32 prod_len;
176 u32 unit_serial_len, off = 0;
177 u16 len = 0, id_len;
178
179 off = 4;
180
181 /*
182 * NAA IEEE Registered Extended Assigned designator format, see
183 * spc4r17 section 7.7.3.6.5
184 *
185 * We depend upon a target_core_mod/ConfigFS provided
186 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
187 * value in order to return the NAA id.
188 */
0fd97ccf 189 if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL))
1fd032ee
CH
190 goto check_t10_vend_desc;
191
192 /* CODE SET == Binary */
193 buf[off++] = 0x1;
194
195 /* Set ASSOCIATION == addressed logical unit: 0)b */
196 buf[off] = 0x00;
197
198 /* Identifier/Designator type == NAA identifier */
199 buf[off++] |= 0x3;
200 off++;
201
202 /* Identifier/Designator length */
203 buf[off++] = 0x10;
204
205 /*
206 * Start NAA IEEE Registered Extended Identifier/Designator
207 */
208 buf[off++] = (0x6 << 4);
209
210 /*
211 * Use OpenFabrics IEEE Company ID: 00 14 05
212 */
213 buf[off++] = 0x01;
214 buf[off++] = 0x40;
215 buf[off] = (0x5 << 4);
216
217 /*
218 * Return ConfigFS Unit Serial Number information for
219 * VENDOR_SPECIFIC_IDENTIFIER and
220 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
221 */
222 spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
223
224 len = 20;
225 off = (len + 4);
226
227check_t10_vend_desc:
228 /*
229 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
230 */
231 id_len = 8; /* For Vendor field */
232 prod_len = 4; /* For VPD Header */
233 prod_len += 8; /* For Vendor field */
234 prod_len += strlen(prod);
235 prod_len++; /* For : */
236
0fd97ccf
CH
237 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
238 unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]);
1fd032ee
CH
239 unit_serial_len++; /* For NULL Terminator */
240
241 id_len += sprintf(&buf[off+12], "%s:%s", prod,
0fd97ccf 242 &dev->t10_wwn.unit_serial[0]);
1fd032ee
CH
243 }
244 buf[off] = 0x2; /* ASCII */
245 buf[off+1] = 0x1; /* T10 Vendor ID */
246 buf[off+2] = 0x0;
247 memcpy(&buf[off+4], "LIO-ORG", 8);
248 /* Extra Byte for NULL Terminator */
249 id_len++;
250 /* Identifier Length */
251 buf[off+3] = id_len;
252 /* Header size for Designation descriptor */
253 len += (id_len + 4);
254 off += (id_len + 4);
255 /*
256 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
257 */
258 port = lun->lun_sep;
259 if (port) {
260 struct t10_alua_lu_gp *lu_gp;
261 u32 padding, scsi_name_len;
262 u16 lu_gp_id = 0;
263 u16 tg_pt_gp_id = 0;
264 u16 tpgt;
265
266 tpg = port->sep_tpg;
267 /*
268 * Relative target port identifer, see spc4r17
269 * section 7.7.3.7
270 *
271 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
272 * section 7.5.1 Table 362
273 */
274 buf[off] =
275 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
276 buf[off++] |= 0x1; /* CODE SET == Binary */
277 buf[off] = 0x80; /* Set PIV=1 */
278 /* Set ASSOCIATION == target port: 01b */
279 buf[off] |= 0x10;
280 /* DESIGNATOR TYPE == Relative target port identifer */
281 buf[off++] |= 0x4;
282 off++; /* Skip over Reserved */
283 buf[off++] = 4; /* DESIGNATOR LENGTH */
284 /* Skip over Obsolete field in RTPI payload
285 * in Table 472 */
286 off += 2;
287 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
288 buf[off++] = (port->sep_rtpi & 0xff);
289 len += 8; /* Header size + Designation descriptor */
290 /*
291 * Target port group identifier, see spc4r17
292 * section 7.7.3.8
293 *
294 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
295 * section 7.5.1 Table 362
296 */
1fd032ee
CH
297 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
298 if (!tg_pt_gp_mem)
299 goto check_lu_gp;
300
301 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
302 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
303 if (!tg_pt_gp) {
304 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
305 goto check_lu_gp;
306 }
307 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
308 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
309
310 buf[off] =
311 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
312 buf[off++] |= 0x1; /* CODE SET == Binary */
313 buf[off] = 0x80; /* Set PIV=1 */
314 /* Set ASSOCIATION == target port: 01b */
315 buf[off] |= 0x10;
316 /* DESIGNATOR TYPE == Target port group identifier */
317 buf[off++] |= 0x5;
318 off++; /* Skip over Reserved */
319 buf[off++] = 4; /* DESIGNATOR LENGTH */
320 off += 2; /* Skip over Reserved Field */
321 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
322 buf[off++] = (tg_pt_gp_id & 0xff);
323 len += 8; /* Header size + Designation descriptor */
324 /*
325 * Logical Unit Group identifier, see spc4r17
326 * section 7.7.3.8
327 */
328check_lu_gp:
329 lu_gp_mem = dev->dev_alua_lu_gp_mem;
330 if (!lu_gp_mem)
331 goto check_scsi_name;
332
333 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
334 lu_gp = lu_gp_mem->lu_gp;
335 if (!lu_gp) {
336 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
337 goto check_scsi_name;
338 }
339 lu_gp_id = lu_gp->lu_gp_id;
340 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
341
342 buf[off++] |= 0x1; /* CODE SET == Binary */
343 /* DESIGNATOR TYPE == Logical Unit Group identifier */
344 buf[off++] |= 0x6;
345 off++; /* Skip over Reserved */
346 buf[off++] = 4; /* DESIGNATOR LENGTH */
347 off += 2; /* Skip over Reserved Field */
348 buf[off++] = ((lu_gp_id >> 8) & 0xff);
349 buf[off++] = (lu_gp_id & 0xff);
350 len += 8; /* Header size + Designation descriptor */
351 /*
352 * SCSI name string designator, see spc4r17
353 * section 7.7.3.11
354 *
355 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
356 * section 7.5.1 Table 362
357 */
358check_scsi_name:
359 scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
360 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
361 scsi_name_len += 10;
362 /* Check for 4-byte padding */
363 padding = ((-scsi_name_len) & 3);
364 if (padding != 0)
365 scsi_name_len += padding;
366 /* Header size + Designation descriptor */
367 scsi_name_len += 4;
368
369 buf[off] =
370 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
371 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
372 buf[off] = 0x80; /* Set PIV=1 */
373 /* Set ASSOCIATION == target port: 01b */
374 buf[off] |= 0x10;
375 /* DESIGNATOR TYPE == SCSI name string */
376 buf[off++] |= 0x8;
377 off += 2; /* Skip over Reserved and length */
378 /*
379 * SCSI name string identifer containing, $FABRIC_MOD
380 * dependent information. For LIO-Target and iSCSI
381 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
382 * UTF-8 encoding.
383 */
384 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
385 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
386 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
387 scsi_name_len += 1 /* Include NULL terminator */;
388 /*
389 * The null-terminated, null-padded (see 4.4.2) SCSI
390 * NAME STRING field contains a UTF-8 format string.
391 * The number of bytes in the SCSI NAME STRING field
392 * (i.e., the value in the DESIGNATOR LENGTH field)
393 * shall be no larger than 256 and shall be a multiple
394 * of four.
395 */
396 if (padding)
397 scsi_name_len += padding;
398
399 buf[off-1] = scsi_name_len;
400 off += scsi_name_len;
401 /* Header size + Designation descriptor */
402 len += (scsi_name_len + 4);
403 }
404 buf[2] = ((len >> 8) & 0xff);
405 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
406 return 0;
407}
0dfa1c5d 408EXPORT_SYMBOL(spc_emulate_evpd_83);
1fd032ee
CH
409
410/* Extended INQUIRY Data VPD Page */
de103c93
CH
411static sense_reason_t
412spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
413{
414 buf[3] = 0x3c;
415 /* Set HEADSUP, ORDSUP, SIMPSUP */
416 buf[5] = 0x07;
417
418 /* If WriteCache emulation is enabled, set V_SUP */
0fd97ccf 419 if (cmd->se_dev->dev_attrib.emulate_write_cache > 0)
1fd032ee
CH
420 buf[6] = 0x01;
421 return 0;
422}
423
424/* Block Limits VPD page */
de103c93
CH
425static sense_reason_t
426spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
427{
428 struct se_device *dev = cmd->se_dev;
429 u32 max_sectors;
430 int have_tp = 0;
431
432 /*
433 * Following spc3r22 section 6.5.3 Block Limits VPD page, when
434 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
435 * different page length for Thin Provisioning.
436 */
0fd97ccf 437 if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
1fd032ee
CH
438 have_tp = 1;
439
440 buf[0] = dev->transport->get_device_type(dev);
441 buf[3] = have_tp ? 0x3c : 0x10;
442
443 /* Set WSNZ to 1 */
444 buf[4] = 0x01;
445
446 /*
447 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
448 */
449 put_unaligned_be16(1, &buf[6]);
450
451 /*
452 * Set MAXIMUM TRANSFER LENGTH
453 */
0fd97ccf
CH
454 max_sectors = min(dev->dev_attrib.fabric_max_sectors,
455 dev->dev_attrib.hw_max_sectors);
1fd032ee
CH
456 put_unaligned_be32(max_sectors, &buf[8]);
457
458 /*
459 * Set OPTIMAL TRANSFER LENGTH
460 */
0fd97ccf 461 put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
1fd032ee
CH
462
463 /*
464 * Exit now if we don't support TP.
465 */
466 if (!have_tp)
773cbaf7 467 goto max_write_same;
1fd032ee
CH
468
469 /*
470 * Set MAXIMUM UNMAP LBA COUNT
471 */
0fd97ccf 472 put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
1fd032ee
CH
473
474 /*
475 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
476 */
0fd97ccf 477 put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
1fd032ee
CH
478 &buf[24]);
479
480 /*
481 * Set OPTIMAL UNMAP GRANULARITY
482 */
0fd97ccf 483 put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
1fd032ee
CH
484
485 /*
486 * UNMAP GRANULARITY ALIGNMENT
487 */
0fd97ccf 488 put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
1fd032ee 489 &buf[32]);
0fd97ccf 490 if (dev->dev_attrib.unmap_granularity_alignment != 0)
1fd032ee
CH
491 buf[32] |= 0x80; /* Set the UGAVALID bit */
492
773cbaf7
NB
493 /*
494 * MAXIMUM WRITE SAME LENGTH
495 */
496max_write_same:
497 put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
498
1fd032ee
CH
499 return 0;
500}
501
502/* Block Device Characteristics VPD page */
de103c93
CH
503static sense_reason_t
504spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
505{
506 struct se_device *dev = cmd->se_dev;
507
508 buf[0] = dev->transport->get_device_type(dev);
509 buf[3] = 0x3c;
0fd97ccf 510 buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
1fd032ee
CH
511
512 return 0;
513}
514
515/* Thin Provisioning VPD */
de103c93
CH
516static sense_reason_t
517spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
518{
519 struct se_device *dev = cmd->se_dev;
520
521 /*
522 * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
523 *
524 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
525 * zero, then the page length shall be set to 0004h. If the DP bit
526 * is set to one, then the page length shall be set to the value
527 * defined in table 162.
528 */
529 buf[0] = dev->transport->get_device_type(dev);
530
531 /*
532 * Set Hardcoded length mentioned above for DP=0
533 */
534 put_unaligned_be16(0x0004, &buf[2]);
535
536 /*
537 * The THRESHOLD EXPONENT field indicates the threshold set size in
538 * LBAs as a power of 2 (i.e., the threshold set size is equal to
539 * 2(threshold exponent)).
540 *
541 * Note that this is currently set to 0x00 as mkp says it will be
542 * changing again. We can enable this once it has settled in T10
543 * and is actually used by Linux/SCSI ML code.
544 */
545 buf[4] = 0x00;
546
547 /*
548 * A TPU bit set to one indicates that the device server supports
549 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
550 * that the device server does not support the UNMAP command.
551 */
0fd97ccf 552 if (dev->dev_attrib.emulate_tpu != 0)
1fd032ee
CH
553 buf[5] = 0x80;
554
555 /*
556 * A TPWS bit set to one indicates that the device server supports
557 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
558 * A TPWS bit set to zero indicates that the device server does not
559 * support the use of the WRITE SAME (16) command to unmap LBAs.
560 */
0fd97ccf 561 if (dev->dev_attrib.emulate_tpws != 0)
1fd032ee
CH
562 buf[5] |= 0x40;
563
564 return 0;
565}
566
de103c93
CH
567static sense_reason_t
568spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
1fd032ee
CH
569
570static struct {
571 uint8_t page;
de103c93 572 sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
1fd032ee
CH
573} evpd_handlers[] = {
574 { .page = 0x00, .emulate = spc_emulate_evpd_00 },
575 { .page = 0x80, .emulate = spc_emulate_evpd_80 },
576 { .page = 0x83, .emulate = spc_emulate_evpd_83 },
577 { .page = 0x86, .emulate = spc_emulate_evpd_86 },
578 { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
579 { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
580 { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
581};
582
583/* supported vital product data pages */
de103c93
CH
584static sense_reason_t
585spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
1fd032ee
CH
586{
587 int p;
588
589 /*
590 * Only report the INQUIRY EVPD=1 pages after a valid NAA
591 * Registered Extended LUN WWN has been set via ConfigFS
592 * during device creation/restart.
593 */
0fd97ccf 594 if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
1fd032ee
CH
595 buf[3] = ARRAY_SIZE(evpd_handlers);
596 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
597 buf[p + 4] = evpd_handlers[p].page;
598 }
599
600 return 0;
601}
602
de103c93
CH
603static sense_reason_t
604spc_emulate_inquiry(struct se_cmd *cmd)
1fd032ee
CH
605{
606 struct se_device *dev = cmd->se_dev;
607 struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
ffe7b0e9 608 unsigned char *rbuf;
1fd032ee 609 unsigned char *cdb = cmd->t_task_cdb;
ffe7b0e9 610 unsigned char buf[SE_INQUIRY_BUF];
de103c93
CH
611 sense_reason_t ret;
612 int p;
1fd032ee 613
dea5f099
NB
614 memset(buf, 0, SE_INQUIRY_BUF);
615
1fd032ee
CH
616 if (dev == tpg->tpg_virt_lun0.lun_se_dev)
617 buf[0] = 0x3f; /* Not connected */
618 else
619 buf[0] = dev->transport->get_device_type(dev);
620
621 if (!(cdb[1] & 0x1)) {
622 if (cdb[2]) {
623 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
624 cdb[2]);
de103c93 625 ret = TCM_INVALID_CDB_FIELD;
1fd032ee
CH
626 goto out;
627 }
628
629 ret = spc_emulate_inquiry_std(cmd, buf);
630 goto out;
631 }
632
633 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
634 if (cdb[2] == evpd_handlers[p].page) {
635 buf[1] = cdb[2];
636 ret = evpd_handlers[p].emulate(cmd, buf);
637 goto out;
638 }
639 }
640
641 pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
de103c93 642 ret = TCM_INVALID_CDB_FIELD;
1fd032ee
CH
643
644out:
ffe7b0e9 645 rbuf = transport_kmap_data_sg(cmd);
49df9fc9
NB
646 if (rbuf) {
647 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
648 transport_kunmap_data_sg(cmd);
649 }
1fd032ee
CH
650
651 if (!ret)
652 target_complete_cmd(cmd, GOOD);
653 return ret;
654}
655
d4b2b867 656static int spc_modesense_rwrecovery(struct se_device *dev, u8 pc, u8 *p)
1fd032ee
CH
657{
658 p[0] = 0x01;
659 p[1] = 0x0a;
660
d4b2b867
RD
661 /* No changeable values for now */
662 if (pc == 1)
663 goto out;
664
665out:
1fd032ee
CH
666 return 12;
667}
668
d4b2b867 669static int spc_modesense_control(struct se_device *dev, u8 pc, u8 *p)
1fd032ee
CH
670{
671 p[0] = 0x0a;
672 p[1] = 0x0a;
d4b2b867
RD
673
674 /* No changeable values for now */
675 if (pc == 1)
676 goto out;
677
1fd032ee
CH
678 p[2] = 2;
679 /*
680 * From spc4r23, 7.4.7 Control mode page
681 *
682 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
683 * restrictions on the algorithm used for reordering commands
684 * having the SIMPLE task attribute (see SAM-4).
685 *
686 * Table 368 -- QUEUE ALGORITHM MODIFIER field
687 * Code Description
688 * 0h Restricted reordering
689 * 1h Unrestricted reordering allowed
690 * 2h to 7h Reserved
691 * 8h to Fh Vendor specific
692 *
693 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
694 * the device server shall order the processing sequence of commands
695 * having the SIMPLE task attribute such that data integrity is maintained
696 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
697 * requests is halted at any time, the final value of all data observable
698 * on the medium shall be the same as if all the commands had been processed
699 * with the ORDERED task attribute).
700 *
701 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
702 * device server may reorder the processing sequence of commands having the
703 * SIMPLE task attribute in any manner. Any data integrity exposures related to
704 * command sequence order shall be explicitly handled by the application client
705 * through the selection of appropriate ommands and task attributes.
706 */
0fd97ccf 707 p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
1fd032ee
CH
708 /*
709 * From spc4r17, section 7.4.6 Control mode Page
710 *
711 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
712 *
713 * 00b: The logical unit shall clear any unit attention condition
714 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
715 * status and shall not establish a unit attention condition when a com-
716 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
717 * status.
718 *
719 * 10b: The logical unit shall not clear any unit attention condition
720 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
721 * status and shall not establish a unit attention condition when
722 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
723 * CONFLICT status.
724 *
725 * 11b a The logical unit shall not clear any unit attention condition
726 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
727 * status and shall establish a unit attention condition for the
728 * initiator port associated with the I_T nexus on which the BUSY,
729 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
730 * Depending on the status, the additional sense code shall be set to
731 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
732 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
733 * command, a unit attention condition shall be established only once
734 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
735 * to the number of commands completed with one of those status codes.
736 */
0fd97ccf
CH
737 p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
738 (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
1fd032ee
CH
739 /*
740 * From spc4r17, section 7.4.6 Control mode Page
741 *
742 * Task Aborted Status (TAS) bit set to zero.
743 *
744 * A task aborted status (TAS) bit set to zero specifies that aborted
745 * tasks shall be terminated by the device server without any response
746 * to the application client. A TAS bit set to one specifies that tasks
747 * aborted by the actions of an I_T nexus other than the I_T nexus on
748 * which the command was received shall be completed with TASK ABORTED
749 * status (see SAM-4).
750 */
0fd97ccf 751 p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
1fd032ee
CH
752 p[8] = 0xff;
753 p[9] = 0xff;
754 p[11] = 30;
755
d4b2b867 756out:
1fd032ee
CH
757 return 12;
758}
759
d4b2b867 760static int spc_modesense_caching(struct se_device *dev, u8 pc, u8 *p)
1fd032ee
CH
761{
762 p[0] = 0x08;
763 p[1] = 0x12;
d4b2b867
RD
764
765 /* No changeable values for now */
766 if (pc == 1)
767 goto out;
768
0fd97ccf 769 if (dev->dev_attrib.emulate_write_cache > 0)
1fd032ee
CH
770 p[2] = 0x04; /* Write Cache Enable */
771 p[12] = 0x20; /* Disabled Read Ahead */
772
d4b2b867 773out:
1fd032ee
CH
774 return 20;
775}
776
0f6d64ce
RD
777static int spc_modesense_informational_exceptions(struct se_device *dev, u8 pc, unsigned char *p)
778{
779 p[0] = 0x1c;
780 p[1] = 0x0a;
781
782 /* No changeable values for now */
783 if (pc == 1)
784 goto out;
785
786out:
787 return 12;
788}
789
d4b2b867
RD
790static struct {
791 uint8_t page;
792 uint8_t subpage;
793 int (*emulate)(struct se_device *, u8, unsigned char *);
794} modesense_handlers[] = {
795 { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
796 { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
797 { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
0f6d64ce 798 { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
d4b2b867
RD
799};
800
1fd032ee
CH
801static void spc_modesense_write_protect(unsigned char *buf, int type)
802{
803 /*
804 * I believe that the WP bit (bit 7) in the mode header is the same for
805 * all device types..
806 */
807 switch (type) {
808 case TYPE_DISK:
809 case TYPE_TAPE:
810 default:
811 buf[0] |= 0x80; /* WP bit */
812 break;
813 }
814}
815
816static void spc_modesense_dpofua(unsigned char *buf, int type)
817{
818 switch (type) {
819 case TYPE_DISK:
820 buf[0] |= 0x10; /* DPOFUA bit */
821 break;
822 default:
823 break;
824 }
825}
826
d4b2b867
RD
827static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
828{
829 *buf++ = 8;
830 put_unaligned_be32(min(blocks, 0xffffffffull), buf);
831 buf += 4;
832 put_unaligned_be32(block_size, buf);
833 return 9;
834}
835
836static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
837{
838 if (blocks <= 0xffffffff)
839 return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
840
841 *buf++ = 1; /* LONGLBA */
842 buf += 2;
843 *buf++ = 16;
844 put_unaligned_be64(blocks, buf);
845 buf += 12;
846 put_unaligned_be32(block_size, buf);
847
848 return 17;
849}
850
de103c93 851static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
1fd032ee
CH
852{
853 struct se_device *dev = cmd->se_dev;
854 char *cdb = cmd->t_task_cdb;
cab9609b 855 unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
1fd032ee
CH
856 int type = dev->transport->get_device_type(dev);
857 int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
d4b2b867
RD
858 bool dbd = !!(cdb[1] & 0x08);
859 bool llba = ten ? !!(cdb[1] & 0x10) : false;
860 u8 pc = cdb[2] >> 6;
861 u8 page = cdb[2] & 0x3f;
862 u8 subpage = cdb[3];
1fd032ee 863 int length = 0;
d4b2b867
RD
864 int ret;
865 int i;
1fd032ee 866
cab9609b
NB
867 memset(buf, 0, SE_MODE_PAGE_BUF);
868
fecae40a
NB
869 /*
870 * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
871 * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
872 */
873 length = ten ? 3 : 2;
d4b2b867
RD
874
875 /* DEVICE-SPECIFIC PARAMETER */
876 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
877 (cmd->se_deve &&
878 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
879 spc_modesense_write_protect(&buf[length], type);
880
881 if ((dev->dev_attrib.emulate_write_cache > 0) &&
882 (dev->dev_attrib.emulate_fua_write > 0))
883 spc_modesense_dpofua(&buf[length], type);
884
885 ++length;
886
887 /* BLOCK DESCRIPTOR */
888
889 /*
890 * For now we only include a block descriptor for disk (SBC)
891 * devices; other command sets use a slightly different format.
892 */
893 if (!dbd && type == TYPE_DISK) {
894 u64 blocks = dev->transport->get_blocks(dev);
895 u32 block_size = dev->dev_attrib.block_size;
896
897 if (ten) {
898 if (llba) {
899 length += spc_modesense_long_blockdesc(&buf[length],
900 blocks, block_size);
901 } else {
902 length += 3;
903 length += spc_modesense_blockdesc(&buf[length],
904 blocks, block_size);
905 }
906 } else {
907 length += spc_modesense_blockdesc(&buf[length], blocks,
908 block_size);
909 }
1fd032ee 910 } else {
d4b2b867
RD
911 if (ten)
912 length += 4;
913 else
914 length += 1;
1fd032ee
CH
915 }
916
d4b2b867
RD
917 if (page == 0x3f) {
918 if (subpage != 0x00 && subpage != 0xff) {
de103c93 919 pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
de103c93 920 return TCM_INVALID_CDB_FIELD;
d4b2b867
RD
921 }
922
923 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
924 /*
925 * Tricky way to say all subpage 00h for
926 * subpage==0, all subpages for subpage==0xff
927 * (and we just checked above that those are
928 * the only two possibilities).
929 */
930 if ((modesense_handlers[i].subpage & ~subpage) == 0) {
931 ret = modesense_handlers[i].emulate(dev, pc, &buf[length]);
932 if (!ten && length + ret >= 255)
933 break;
934 length += ret;
935 }
936 }
937
938 goto set_length;
939 }
940
941 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
942 if (modesense_handlers[i].page == page &&
943 modesense_handlers[i].subpage == subpage) {
944 length += modesense_handlers[i].emulate(dev, pc, &buf[length]);
945 goto set_length;
946 }
947
948 /*
949 * We don't intend to implement:
950 * - obsolete page 03h "format parameters" (checked by Solaris)
951 */
952 if (page != 0x03)
953 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
954 page, subpage);
955
de103c93 956 return TCM_UNKNOWN_MODE_PAGE;
d4b2b867
RD
957
958set_length:
959 if (ten)
960 put_unaligned_be16(length - 2, buf);
961 else
962 buf[0] = length - 1;
963
cab9609b
NB
964 rbuf = transport_kmap_data_sg(cmd);
965 if (rbuf) {
966 memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
967 transport_kunmap_data_sg(cmd);
7a3f369c 968 }
1fd032ee
CH
969
970 target_complete_cmd(cmd, GOOD);
971 return 0;
972}
973
de103c93 974static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
3a3c5e4a
RD
975{
976 struct se_device *dev = cmd->se_dev;
977 char *cdb = cmd->t_task_cdb;
978 bool ten = cdb[0] == MODE_SELECT_10;
979 int off = ten ? 8 : 4;
980 bool pf = !!(cdb[1] & 0x10);
981 u8 page, subpage;
982 unsigned char *buf;
983 unsigned char tbuf[SE_MODE_PAGE_BUF];
984 int length;
985 int ret = 0;
986 int i;
987
988 buf = transport_kmap_data_sg(cmd);
de103c93
CH
989 if (!buf)
990 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3a3c5e4a
RD
991
992 if (!pf) {
de103c93 993 ret = TCM_INVALID_CDB_FIELD;
3a3c5e4a
RD
994 goto out;
995 }
996
997 page = buf[off] & 0x3f;
998 subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
999
1000 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
1001 if (modesense_handlers[i].page == page &&
1002 modesense_handlers[i].subpage == subpage) {
1003 memset(tbuf, 0, SE_MODE_PAGE_BUF);
1004 length = modesense_handlers[i].emulate(dev, 0, tbuf);
1005 goto check_contents;
1006 }
1007
de103c93 1008 ret = TCM_UNKNOWN_MODE_PAGE;
3a3c5e4a
RD
1009 goto out;
1010
1011check_contents:
de103c93
CH
1012 if (memcmp(buf + off, tbuf, length))
1013 ret = TCM_INVALID_PARAMETER_LIST;
3a3c5e4a
RD
1014
1015out:
1016 transport_kunmap_data_sg(cmd);
1017
1018 if (!ret)
1019 target_complete_cmd(cmd, GOOD);
1020 return ret;
1021}
1022
de103c93 1023static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
1fd032ee
CH
1024{
1025 unsigned char *cdb = cmd->t_task_cdb;
32a8811f 1026 unsigned char *rbuf;
1fd032ee 1027 u8 ua_asc = 0, ua_ascq = 0;
32a8811f
PB
1028 unsigned char buf[SE_SENSE_BUF];
1029
1030 memset(buf, 0, SE_SENSE_BUF);
1fd032ee
CH
1031
1032 if (cdb[1] & 0x01) {
1033 pr_err("REQUEST_SENSE description emulation not"
1034 " supported\n");
de103c93 1035 return TCM_INVALID_CDB_FIELD;
1fd032ee
CH
1036 }
1037
32a8811f 1038 rbuf = transport_kmap_data_sg(cmd);
de103c93
CH
1039 if (!rbuf)
1040 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1041
1042 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
1fd032ee
CH
1043 /*
1044 * CURRENT ERROR, UNIT ATTENTION
1045 */
1046 buf[0] = 0x70;
1047 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
1048
1fd032ee
CH
1049 /*
1050 * The Additional Sense Code (ASC) from the UNIT ATTENTION
1051 */
1052 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
1053 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
1054 buf[7] = 0x0A;
1055 } else {
1056 /*
1057 * CURRENT ERROR, NO SENSE
1058 */
1059 buf[0] = 0x70;
1060 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
1061
1fd032ee
CH
1062 /*
1063 * NO ADDITIONAL SENSE INFORMATION
1064 */
1065 buf[SPC_ASC_KEY_OFFSET] = 0x00;
1066 buf[7] = 0x0A;
1067 }
1068
de103c93
CH
1069 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
1070 transport_kunmap_data_sg(cmd);
32a8811f 1071
1fd032ee
CH
1072 target_complete_cmd(cmd, GOOD);
1073 return 0;
1074}
1075
de103c93 1076sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
d1b1f805
CH
1077{
1078 struct se_dev_entry *deve;
1079 struct se_session *sess = cmd->se_sess;
1080 unsigned char *buf;
1081 u32 lun_count = 0, offset = 8, i;
1082
1083 if (cmd->data_length < 16) {
1084 pr_warn("REPORT LUNS allocation length %u too small\n",
1085 cmd->data_length);
de103c93 1086 return TCM_INVALID_CDB_FIELD;
d1b1f805
CH
1087 }
1088
1089 buf = transport_kmap_data_sg(cmd);
1090 if (!buf)
de103c93 1091 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
d1b1f805
CH
1092
1093 /*
1094 * If no struct se_session pointer is present, this struct se_cmd is
1095 * coming via a target_core_mod PASSTHROUGH op, and not through
1096 * a $FABRIC_MOD. In that case, report LUN=0 only.
1097 */
1098 if (!sess) {
1099 int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
1100 lun_count = 1;
1101 goto done;
1102 }
1103
1104 spin_lock_irq(&sess->se_node_acl->device_list_lock);
1105 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
1106 deve = sess->se_node_acl->device_list[i];
1107 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
1108 continue;
1109 /*
1110 * We determine the correct LUN LIST LENGTH even once we
1111 * have reached the initial allocation length.
1112 * See SPC2-R20 7.19.
1113 */
1114 lun_count++;
1115 if ((offset + 8) > cmd->data_length)
1116 continue;
1117
1118 int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
1119 offset += 8;
1120 }
1121 spin_unlock_irq(&sess->se_node_acl->device_list_lock);
1122
1123 /*
1124 * See SPC3 r07, page 159.
1125 */
1126done:
1127 lun_count *= 8;
1128 buf[0] = ((lun_count >> 24) & 0xff);
1129 buf[1] = ((lun_count >> 16) & 0xff);
1130 buf[2] = ((lun_count >> 8) & 0xff);
1131 buf[3] = (lun_count & 0xff);
1132 transport_kunmap_data_sg(cmd);
1133
1134 target_complete_cmd(cmd, GOOD);
1135 return 0;
1136}
8de530a5 1137EXPORT_SYMBOL(spc_emulate_report_luns);
d1b1f805 1138
de103c93
CH
1139static sense_reason_t
1140spc_emulate_testunitready(struct se_cmd *cmd)
1fd032ee
CH
1141{
1142 target_complete_cmd(cmd, GOOD);
1143 return 0;
1144}
1145
de103c93
CH
1146sense_reason_t
1147spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
88455ec4 1148{
eba2ca45 1149 struct se_device *dev = cmd->se_dev;
88455ec4
CH
1150 unsigned char *cdb = cmd->t_task_cdb;
1151
1152 switch (cdb[0]) {
1153 case MODE_SELECT:
1154 *size = cdb[4];
3a3c5e4a 1155 cmd->execute_cmd = spc_emulate_modeselect;
88455ec4
CH
1156 break;
1157 case MODE_SELECT_10:
1158 *size = (cdb[7] << 8) + cdb[8];
3a3c5e4a 1159 cmd->execute_cmd = spc_emulate_modeselect;
88455ec4
CH
1160 break;
1161 case MODE_SENSE:
1162 *size = cdb[4];
1fd032ee 1163 cmd->execute_cmd = spc_emulate_modesense;
88455ec4
CH
1164 break;
1165 case MODE_SENSE_10:
1166 *size = (cdb[7] << 8) + cdb[8];
1fd032ee 1167 cmd->execute_cmd = spc_emulate_modesense;
88455ec4
CH
1168 break;
1169 case LOG_SELECT:
1170 case LOG_SENSE:
1171 *size = (cdb[7] << 8) + cdb[8];
1172 break;
1173 case PERSISTENT_RESERVE_IN:
88455ec4 1174 *size = (cdb[7] << 8) + cdb[8];
d977f437 1175 cmd->execute_cmd = target_scsi3_emulate_pr_in;
88455ec4
CH
1176 break;
1177 case PERSISTENT_RESERVE_OUT:
88455ec4 1178 *size = (cdb[7] << 8) + cdb[8];
d977f437 1179 cmd->execute_cmd = target_scsi3_emulate_pr_out;
88455ec4
CH
1180 break;
1181 case RELEASE:
1182 case RELEASE_10:
1183 if (cdb[0] == RELEASE_10)
1184 *size = (cdb[7] << 8) | cdb[8];
1185 else
1186 *size = cmd->data_length;
1187
d977f437 1188 cmd->execute_cmd = target_scsi2_reservation_release;
88455ec4
CH
1189 break;
1190 case RESERVE:
1191 case RESERVE_10:
1192 /*
1193 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
1194 * Assume the passthrough or $FABRIC_MOD will tell us about it.
1195 */
1196 if (cdb[0] == RESERVE_10)
1197 *size = (cdb[7] << 8) | cdb[8];
1198 else
1199 *size = cmd->data_length;
1200
d977f437 1201 cmd->execute_cmd = target_scsi2_reservation_reserve;
88455ec4
CH
1202 break;
1203 case REQUEST_SENSE:
1204 *size = cdb[4];
1fd032ee 1205 cmd->execute_cmd = spc_emulate_request_sense;
88455ec4
CH
1206 break;
1207 case INQUIRY:
1208 *size = (cdb[3] << 8) + cdb[4];
1209
1210 /*
1211 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
1212 * See spc4r17 section 5.3
1213 */
019c4ca6 1214 cmd->sam_task_attr = MSG_HEAD_TAG;
1fd032ee 1215 cmd->execute_cmd = spc_emulate_inquiry;
88455ec4
CH
1216 break;
1217 case SECURITY_PROTOCOL_IN:
1218 case SECURITY_PROTOCOL_OUT:
1219 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1220 break;
1221 case EXTENDED_COPY:
1222 case READ_ATTRIBUTE:
1223 case RECEIVE_COPY_RESULTS:
1224 case WRITE_ATTRIBUTE:
1225 *size = (cdb[10] << 24) | (cdb[11] << 16) |
1226 (cdb[12] << 8) | cdb[13];
1227 break;
1228 case RECEIVE_DIAGNOSTIC:
1229 case SEND_DIAGNOSTIC:
1230 *size = (cdb[3] << 8) | cdb[4];
1231 break;
1232 case WRITE_BUFFER:
1233 *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
1234 break;
1235 case REPORT_LUNS:
d1b1f805 1236 cmd->execute_cmd = spc_emulate_report_luns;
88455ec4
CH
1237 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1238 /*
1239 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
1240 * See spc4r17 section 5.3
1241 */
019c4ca6 1242 cmd->sam_task_attr = MSG_HEAD_TAG;
88455ec4
CH
1243 break;
1244 case TEST_UNIT_READY:
1fd032ee 1245 cmd->execute_cmd = spc_emulate_testunitready;
d6e0175c 1246 *size = 0;
88455ec4 1247 break;
eba2ca45
NB
1248 case MAINTENANCE_IN:
1249 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1250 /*
1251 * MAINTENANCE_IN from SCC-2
1252 * Check for emulated MI_REPORT_TARGET_PGS
1253 */
c87fbd56 1254 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
eba2ca45
NB
1255 cmd->execute_cmd =
1256 target_emulate_report_target_port_groups;
1257 }
1258 *size = get_unaligned_be32(&cdb[6]);
1259 } else {
1260 /*
1261 * GPCMD_SEND_KEY from multi media commands
1262 */
1263 *size = get_unaligned_be16(&cdb[8]);
1264 }
1265 break;
1266 case MAINTENANCE_OUT:
1267 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1268 /*
1269 * MAINTENANCE_OUT from SCC-2
1270 * Check for emulated MO_SET_TARGET_PGS.
1271 */
c87fbd56 1272 if (cdb[1] == MO_SET_TARGET_PGS) {
eba2ca45
NB
1273 cmd->execute_cmd =
1274 target_emulate_set_target_port_groups;
1275 }
1276 *size = get_unaligned_be32(&cdb[6]);
1277 } else {
1278 /*
1279 * GPCMD_SEND_KEY from multi media commands
1280 */
1281 *size = get_unaligned_be16(&cdb[8]);
1282 }
1283 break;
88455ec4
CH
1284 default:
1285 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
1286 " 0x%02x, sending CHECK_CONDITION.\n",
1287 cmd->se_tfo->get_fabric_name(), cdb[0]);
de103c93 1288 return TCM_UNSUPPORTED_SCSI_OPCODE;
88455ec4
CH
1289 }
1290
1291 return 0;
1292}
1293EXPORT_SYMBOL(spc_parse_cdb);
This page took 0.105006 seconds and 5 git commands to generate.