blob: cf1b8bb310c433a70c5415b51fbf07d9c7c7c3ea [file] [log] [blame]
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001/*
2 * SCSI Primary Commands (SPC) parsing and emulation.
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
5 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
6 * Copyright (c) 2007-2010 Rising Tide Systems
7 * Copyright (c) 2008-2010 Linux-iSCSI.org
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <asm/unaligned.h>
29
30#include <scsi/scsi.h>
31#include <scsi/scsi_tcq.h>
32
33#include <target/target_core_base.h>
34#include <target/target_core_backend.h>
35#include <target/target_core_fabric.h>
36
37#include "target_core_internal.h"
Nicholas Bellingereba2ca42012-05-30 14:09:10 -070038#include "target_core_alua.h"
Christoph Hellwig88455ec2012-05-20 11:59:13 -040039#include "target_core_pr.h"
40#include "target_core_ua.h"
41
42
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040043static void spc_fill_alua_data(struct se_port *port, unsigned char *buf)
44{
45 struct t10_alua_tg_pt_gp *tg_pt_gp;
46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
47
48 /*
49 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
50 */
51 buf[5] = 0x80;
52
53 /*
54 * Set TPGS field for explict and/or implict ALUA access type
55 * and opteration.
56 *
57 * See spc4r17 section 6.4.2 Table 135
58 */
59 if (!port)
60 return;
61 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
62 if (!tg_pt_gp_mem)
63 return;
64
65 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
66 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
67 if (tg_pt_gp)
68 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
69 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
70}
71
Christoph Hellwigde103c92012-11-06 12:24:09 -080072static sense_reason_t
73spc_emulate_inquiry_std(struct se_cmd *cmd, char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040074{
75 struct se_lun *lun = cmd->se_lun;
76 struct se_device *dev = cmd->se_dev;
77
78 /* Set RMB (removable media) for tape devices */
79 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
80 buf[1] = 0x80;
81
Christoph Hellwig48c25672012-10-10 17:37:17 -040082 buf[2] = 0x05; /* SPC-3 */
Christoph Hellwig1fd032e2012-05-20 11:59:15 -040083
84 /*
85 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
86 *
87 * SPC4 says:
88 * A RESPONSE DATA FORMAT field set to 2h indicates that the
89 * standard INQUIRY data is in the format defined in this
90 * standard. Response data format values less than 2h are
91 * obsolete. Response data format values greater than 2h are
92 * reserved.
93 */
94 buf[3] = 2;
95
96 /*
97 * Enable SCCS and TPGS fields for Emulated ALUA
98 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -040099 spc_fill_alua_data(lun->lun_sep, buf);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400100
101 buf[7] = 0x2; /* CmdQue=1 */
102
103 snprintf(&buf[8], 8, "LIO-ORG");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400104 snprintf(&buf[16], 16, "%s", dev->t10_wwn.model);
105 snprintf(&buf[32], 4, "%s", dev->t10_wwn.revision);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400106 buf[4] = 31; /* Set additional length to 31 */
107
108 return 0;
109}
110
111/* unit serial number */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800112static sense_reason_t
113spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400114{
115 struct se_device *dev = cmd->se_dev;
116 u16 len = 0;
117
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400118 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400119 u32 unit_serial_len;
120
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400121 unit_serial_len = strlen(dev->t10_wwn.unit_serial);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400122 unit_serial_len++; /* For NULL Terminator */
123
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400124 len += sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400125 len++; /* Extra Byte for NULL Terminator */
126 buf[3] = len;
127 }
128 return 0;
129}
130
131static void spc_parse_naa_6h_vendor_specific(struct se_device *dev,
132 unsigned char *buf)
133{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400134 unsigned char *p = &dev->t10_wwn.unit_serial[0];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400135 int cnt;
136 bool next = true;
137
138 /*
139 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
140 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
141 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
142 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
143 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
144 * per device uniqeness.
145 */
146 for (cnt = 0; *p && cnt < 13; p++) {
147 int val = hex_to_bin(*p);
148
149 if (val < 0)
150 continue;
151
152 if (next) {
153 next = false;
154 buf[cnt++] |= val;
155 } else {
156 next = true;
157 buf[cnt] = val << 4;
158 }
159 }
160}
161
162/*
163 * Device identification VPD, for a complete list of
164 * DESIGNATOR TYPEs see spc4r17 Table 459.
165 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800166static sense_reason_t
167spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400168{
169 struct se_device *dev = cmd->se_dev;
170 struct se_lun *lun = cmd->se_lun;
171 struct se_port *port = NULL;
172 struct se_portal_group *tpg = NULL;
173 struct t10_alua_lu_gp_member *lu_gp_mem;
174 struct t10_alua_tg_pt_gp *tg_pt_gp;
175 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400176 unsigned char *prod = &dev->t10_wwn.model[0];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400177 u32 prod_len;
178 u32 unit_serial_len, off = 0;
179 u16 len = 0, id_len;
180
181 off = 4;
182
183 /*
184 * NAA IEEE Registered Extended Assigned designator format, see
185 * spc4r17 section 7.7.3.6.5
186 *
187 * We depend upon a target_core_mod/ConfigFS provided
188 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
189 * value in order to return the NAA id.
190 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400191 if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL))
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400192 goto check_t10_vend_desc;
193
194 /* CODE SET == Binary */
195 buf[off++] = 0x1;
196
197 /* Set ASSOCIATION == addressed logical unit: 0)b */
198 buf[off] = 0x00;
199
200 /* Identifier/Designator type == NAA identifier */
201 buf[off++] |= 0x3;
202 off++;
203
204 /* Identifier/Designator length */
205 buf[off++] = 0x10;
206
207 /*
208 * Start NAA IEEE Registered Extended Identifier/Designator
209 */
210 buf[off++] = (0x6 << 4);
211
212 /*
213 * Use OpenFabrics IEEE Company ID: 00 14 05
214 */
215 buf[off++] = 0x01;
216 buf[off++] = 0x40;
217 buf[off] = (0x5 << 4);
218
219 /*
220 * Return ConfigFS Unit Serial Number information for
221 * VENDOR_SPECIFIC_IDENTIFIER and
222 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
223 */
224 spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
225
226 len = 20;
227 off = (len + 4);
228
229check_t10_vend_desc:
230 /*
231 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
232 */
233 id_len = 8; /* For Vendor field */
234 prod_len = 4; /* For VPD Header */
235 prod_len += 8; /* For Vendor field */
236 prod_len += strlen(prod);
237 prod_len++; /* For : */
238
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400239 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
240 unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400241 unit_serial_len++; /* For NULL Terminator */
242
243 id_len += sprintf(&buf[off+12], "%s:%s", prod,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400244 &dev->t10_wwn.unit_serial[0]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400245 }
246 buf[off] = 0x2; /* ASCII */
247 buf[off+1] = 0x1; /* T10 Vendor ID */
248 buf[off+2] = 0x0;
249 memcpy(&buf[off+4], "LIO-ORG", 8);
250 /* Extra Byte for NULL Terminator */
251 id_len++;
252 /* Identifier Length */
253 buf[off+3] = id_len;
254 /* Header size for Designation descriptor */
255 len += (id_len + 4);
256 off += (id_len + 4);
257 /*
258 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
259 */
260 port = lun->lun_sep;
261 if (port) {
262 struct t10_alua_lu_gp *lu_gp;
263 u32 padding, scsi_name_len;
264 u16 lu_gp_id = 0;
265 u16 tg_pt_gp_id = 0;
266 u16 tpgt;
267
268 tpg = port->sep_tpg;
269 /*
270 * Relative target port identifer, see spc4r17
271 * section 7.7.3.7
272 *
273 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
274 * section 7.5.1 Table 362
275 */
276 buf[off] =
277 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
278 buf[off++] |= 0x1; /* CODE SET == Binary */
279 buf[off] = 0x80; /* Set PIV=1 */
280 /* Set ASSOCIATION == target port: 01b */
281 buf[off] |= 0x10;
282 /* DESIGNATOR TYPE == Relative target port identifer */
283 buf[off++] |= 0x4;
284 off++; /* Skip over Reserved */
285 buf[off++] = 4; /* DESIGNATOR LENGTH */
286 /* Skip over Obsolete field in RTPI payload
287 * in Table 472 */
288 off += 2;
289 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
290 buf[off++] = (port->sep_rtpi & 0xff);
291 len += 8; /* Header size + Designation descriptor */
292 /*
293 * Target port group identifier, see spc4r17
294 * section 7.7.3.8
295 *
296 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
297 * section 7.5.1 Table 362
298 */
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400299 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
300 if (!tg_pt_gp_mem)
301 goto check_lu_gp;
302
303 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
304 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
305 if (!tg_pt_gp) {
306 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
307 goto check_lu_gp;
308 }
309 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
310 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
311
312 buf[off] =
313 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
314 buf[off++] |= 0x1; /* CODE SET == Binary */
315 buf[off] = 0x80; /* Set PIV=1 */
316 /* Set ASSOCIATION == target port: 01b */
317 buf[off] |= 0x10;
318 /* DESIGNATOR TYPE == Target port group identifier */
319 buf[off++] |= 0x5;
320 off++; /* Skip over Reserved */
321 buf[off++] = 4; /* DESIGNATOR LENGTH */
322 off += 2; /* Skip over Reserved Field */
323 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
324 buf[off++] = (tg_pt_gp_id & 0xff);
325 len += 8; /* Header size + Designation descriptor */
326 /*
327 * Logical Unit Group identifier, see spc4r17
328 * section 7.7.3.8
329 */
330check_lu_gp:
331 lu_gp_mem = dev->dev_alua_lu_gp_mem;
332 if (!lu_gp_mem)
333 goto check_scsi_name;
334
335 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
336 lu_gp = lu_gp_mem->lu_gp;
337 if (!lu_gp) {
338 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
339 goto check_scsi_name;
340 }
341 lu_gp_id = lu_gp->lu_gp_id;
342 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
343
344 buf[off++] |= 0x1; /* CODE SET == Binary */
345 /* DESIGNATOR TYPE == Logical Unit Group identifier */
346 buf[off++] |= 0x6;
347 off++; /* Skip over Reserved */
348 buf[off++] = 4; /* DESIGNATOR LENGTH */
349 off += 2; /* Skip over Reserved Field */
350 buf[off++] = ((lu_gp_id >> 8) & 0xff);
351 buf[off++] = (lu_gp_id & 0xff);
352 len += 8; /* Header size + Designation descriptor */
353 /*
354 * SCSI name string designator, see spc4r17
355 * section 7.7.3.11
356 *
357 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
358 * section 7.5.1 Table 362
359 */
360check_scsi_name:
361 scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
362 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
363 scsi_name_len += 10;
364 /* Check for 4-byte padding */
365 padding = ((-scsi_name_len) & 3);
366 if (padding != 0)
367 scsi_name_len += padding;
368 /* Header size + Designation descriptor */
369 scsi_name_len += 4;
370
371 buf[off] =
372 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
373 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
374 buf[off] = 0x80; /* Set PIV=1 */
375 /* Set ASSOCIATION == target port: 01b */
376 buf[off] |= 0x10;
377 /* DESIGNATOR TYPE == SCSI name string */
378 buf[off++] |= 0x8;
379 off += 2; /* Skip over Reserved and length */
380 /*
381 * SCSI name string identifer containing, $FABRIC_MOD
382 * dependent information. For LIO-Target and iSCSI
383 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
384 * UTF-8 encoding.
385 */
386 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
387 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
388 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
389 scsi_name_len += 1 /* Include NULL terminator */;
390 /*
391 * The null-terminated, null-padded (see 4.4.2) SCSI
392 * NAME STRING field contains a UTF-8 format string.
393 * The number of bytes in the SCSI NAME STRING field
394 * (i.e., the value in the DESIGNATOR LENGTH field)
395 * shall be no larger than 256 and shall be a multiple
396 * of four.
397 */
398 if (padding)
399 scsi_name_len += padding;
400
401 buf[off-1] = scsi_name_len;
402 off += scsi_name_len;
403 /* Header size + Designation descriptor */
404 len += (scsi_name_len + 4);
405 }
406 buf[2] = ((len >> 8) & 0xff);
407 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
408 return 0;
409}
410
411/* Extended INQUIRY Data VPD Page */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800412static sense_reason_t
413spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400414{
415 buf[3] = 0x3c;
416 /* Set HEADSUP, ORDSUP, SIMPSUP */
417 buf[5] = 0x07;
418
419 /* If WriteCache emulation is enabled, set V_SUP */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400420 if (cmd->se_dev->dev_attrib.emulate_write_cache > 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400421 buf[6] = 0x01;
422 return 0;
423}
424
425/* Block Limits VPD page */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800426static sense_reason_t
427spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400428{
429 struct se_device *dev = cmd->se_dev;
430 u32 max_sectors;
431 int have_tp = 0;
432
433 /*
434 * Following spc3r22 section 6.5.3 Block Limits VPD page, when
435 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
436 * different page length for Thin Provisioning.
437 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400438 if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400439 have_tp = 1;
440
441 buf[0] = dev->transport->get_device_type(dev);
442 buf[3] = have_tp ? 0x3c : 0x10;
443
444 /* Set WSNZ to 1 */
445 buf[4] = 0x01;
446
447 /*
448 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
449 */
450 put_unaligned_be16(1, &buf[6]);
451
452 /*
453 * Set MAXIMUM TRANSFER LENGTH
454 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400455 max_sectors = min(dev->dev_attrib.fabric_max_sectors,
456 dev->dev_attrib.hw_max_sectors);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400457 put_unaligned_be32(max_sectors, &buf[8]);
458
459 /*
460 * Set OPTIMAL TRANSFER LENGTH
461 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400462 put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400463
464 /*
465 * Exit now if we don't support TP.
466 */
467 if (!have_tp)
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800468 goto max_write_same;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400469
470 /*
471 * Set MAXIMUM UNMAP LBA COUNT
472 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400473 put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400474
475 /*
476 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
477 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400478 put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400479 &buf[24]);
480
481 /*
482 * Set OPTIMAL UNMAP GRANULARITY
483 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400484 put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400485
486 /*
487 * UNMAP GRANULARITY ALIGNMENT
488 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400489 put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400490 &buf[32]);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400491 if (dev->dev_attrib.unmap_granularity_alignment != 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400492 buf[32] |= 0x80; /* Set the UGAVALID bit */
493
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800494 /*
495 * MAXIMUM WRITE SAME LENGTH
496 */
497max_write_same:
498 put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
499
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400500 return 0;
501}
502
503/* Block Device Characteristics VPD page */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800504static sense_reason_t
505spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400506{
507 struct se_device *dev = cmd->se_dev;
508
509 buf[0] = dev->transport->get_device_type(dev);
510 buf[3] = 0x3c;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400511 buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400512
513 return 0;
514}
515
516/* Thin Provisioning VPD */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800517static sense_reason_t
518spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400519{
520 struct se_device *dev = cmd->se_dev;
521
522 /*
523 * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
524 *
525 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
526 * zero, then the page length shall be set to 0004h. If the DP bit
527 * is set to one, then the page length shall be set to the value
528 * defined in table 162.
529 */
530 buf[0] = dev->transport->get_device_type(dev);
531
532 /*
533 * Set Hardcoded length mentioned above for DP=0
534 */
535 put_unaligned_be16(0x0004, &buf[2]);
536
537 /*
538 * The THRESHOLD EXPONENT field indicates the threshold set size in
539 * LBAs as a power of 2 (i.e., the threshold set size is equal to
540 * 2(threshold exponent)).
541 *
542 * Note that this is currently set to 0x00 as mkp says it will be
543 * changing again. We can enable this once it has settled in T10
544 * and is actually used by Linux/SCSI ML code.
545 */
546 buf[4] = 0x00;
547
548 /*
549 * A TPU bit set to one indicates that the device server supports
550 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
551 * that the device server does not support the UNMAP command.
552 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400553 if (dev->dev_attrib.emulate_tpu != 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400554 buf[5] = 0x80;
555
556 /*
557 * A TPWS bit set to one indicates that the device server supports
558 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
559 * A TPWS bit set to zero indicates that the device server does not
560 * support the use of the WRITE SAME (16) command to unmap LBAs.
561 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400562 if (dev->dev_attrib.emulate_tpws != 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400563 buf[5] |= 0x40;
564
565 return 0;
566}
567
Christoph Hellwigde103c92012-11-06 12:24:09 -0800568static sense_reason_t
569spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400570
571static struct {
572 uint8_t page;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800573 sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400574} evpd_handlers[] = {
575 { .page = 0x00, .emulate = spc_emulate_evpd_00 },
576 { .page = 0x80, .emulate = spc_emulate_evpd_80 },
577 { .page = 0x83, .emulate = spc_emulate_evpd_83 },
578 { .page = 0x86, .emulate = spc_emulate_evpd_86 },
579 { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
580 { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
581 { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
582};
583
584/* supported vital product data pages */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800585static sense_reason_t
586spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400587{
588 int p;
589
590 /*
591 * Only report the INQUIRY EVPD=1 pages after a valid NAA
592 * Registered Extended LUN WWN has been set via ConfigFS
593 * during device creation/restart.
594 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400595 if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400596 buf[3] = ARRAY_SIZE(evpd_handlers);
597 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
598 buf[p + 4] = evpd_handlers[p].page;
599 }
600
601 return 0;
602}
603
Christoph Hellwigde103c92012-11-06 12:24:09 -0800604static sense_reason_t
605spc_emulate_inquiry(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400606{
607 struct se_device *dev = cmd->se_dev;
608 struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
Paolo Bonziniffe7b0e2012-09-07 17:30:38 +0200609 unsigned char *rbuf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400610 unsigned char *cdb = cmd->t_task_cdb;
Paolo Bonziniffe7b0e2012-09-07 17:30:38 +0200611 unsigned char buf[SE_INQUIRY_BUF];
Christoph Hellwigde103c92012-11-06 12:24:09 -0800612 sense_reason_t ret;
613 int p;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400614
Nicholas Bellingerdea5f092012-10-31 22:04:26 -0700615 memset(buf, 0, SE_INQUIRY_BUF);
616
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400617 if (dev == tpg->tpg_virt_lun0.lun_se_dev)
618 buf[0] = 0x3f; /* Not connected */
619 else
620 buf[0] = dev->transport->get_device_type(dev);
621
622 if (!(cdb[1] & 0x1)) {
623 if (cdb[2]) {
624 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
625 cdb[2]);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800626 ret = TCM_INVALID_CDB_FIELD;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400627 goto out;
628 }
629
630 ret = spc_emulate_inquiry_std(cmd, buf);
631 goto out;
632 }
633
634 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
635 if (cdb[2] == evpd_handlers[p].page) {
636 buf[1] = cdb[2];
637 ret = evpd_handlers[p].emulate(cmd, buf);
638 goto out;
639 }
640 }
641
642 pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800643 ret = TCM_INVALID_CDB_FIELD;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400644
645out:
Paolo Bonziniffe7b0e2012-09-07 17:30:38 +0200646 rbuf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800647 if (!rbuf)
648 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
649
650 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
651 transport_kunmap_data_sg(cmd);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400652
653 if (!ret)
654 target_complete_cmd(cmd, GOOD);
655 return ret;
656}
657
Roland Dreierd4b2b862012-10-31 09:16:48 -0700658static int spc_modesense_rwrecovery(struct se_device *dev, u8 pc, u8 *p)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400659{
660 p[0] = 0x01;
661 p[1] = 0x0a;
662
Roland Dreierd4b2b862012-10-31 09:16:48 -0700663 /* No changeable values for now */
664 if (pc == 1)
665 goto out;
666
667out:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400668 return 12;
669}
670
Roland Dreierd4b2b862012-10-31 09:16:48 -0700671static int spc_modesense_control(struct se_device *dev, u8 pc, u8 *p)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400672{
673 p[0] = 0x0a;
674 p[1] = 0x0a;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700675
676 /* No changeable values for now */
677 if (pc == 1)
678 goto out;
679
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400680 p[2] = 2;
681 /*
682 * From spc4r23, 7.4.7 Control mode page
683 *
684 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
685 * restrictions on the algorithm used for reordering commands
686 * having the SIMPLE task attribute (see SAM-4).
687 *
688 * Table 368 -- QUEUE ALGORITHM MODIFIER field
689 * Code Description
690 * 0h Restricted reordering
691 * 1h Unrestricted reordering allowed
692 * 2h to 7h Reserved
693 * 8h to Fh Vendor specific
694 *
695 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
696 * the device server shall order the processing sequence of commands
697 * having the SIMPLE task attribute such that data integrity is maintained
698 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
699 * requests is halted at any time, the final value of all data observable
700 * on the medium shall be the same as if all the commands had been processed
701 * with the ORDERED task attribute).
702 *
703 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
704 * device server may reorder the processing sequence of commands having the
705 * SIMPLE task attribute in any manner. Any data integrity exposures related to
706 * command sequence order shall be explicitly handled by the application client
707 * through the selection of appropriate ommands and task attributes.
708 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400709 p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400710 /*
711 * From spc4r17, section 7.4.6 Control mode Page
712 *
713 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
714 *
715 * 00b: The logical unit shall clear any unit attention condition
716 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
717 * status and shall not establish a unit attention condition when a com-
718 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
719 * status.
720 *
721 * 10b: The logical unit shall not clear any unit attention condition
722 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
723 * status and shall not establish a unit attention condition when
724 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
725 * CONFLICT status.
726 *
727 * 11b a The logical unit shall not clear any unit attention condition
728 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
729 * status and shall establish a unit attention condition for the
730 * initiator port associated with the I_T nexus on which the BUSY,
731 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
732 * Depending on the status, the additional sense code shall be set to
733 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
734 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
735 * command, a unit attention condition shall be established only once
736 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
737 * to the number of commands completed with one of those status codes.
738 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400739 p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
740 (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400741 /*
742 * From spc4r17, section 7.4.6 Control mode Page
743 *
744 * Task Aborted Status (TAS) bit set to zero.
745 *
746 * A task aborted status (TAS) bit set to zero specifies that aborted
747 * tasks shall be terminated by the device server without any response
748 * to the application client. A TAS bit set to one specifies that tasks
749 * aborted by the actions of an I_T nexus other than the I_T nexus on
750 * which the command was received shall be completed with TASK ABORTED
751 * status (see SAM-4).
752 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400753 p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400754 p[8] = 0xff;
755 p[9] = 0xff;
756 p[11] = 30;
757
Roland Dreierd4b2b862012-10-31 09:16:48 -0700758out:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400759 return 12;
760}
761
Roland Dreierd4b2b862012-10-31 09:16:48 -0700762static int spc_modesense_caching(struct se_device *dev, u8 pc, u8 *p)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400763{
764 p[0] = 0x08;
765 p[1] = 0x12;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700766
767 /* No changeable values for now */
768 if (pc == 1)
769 goto out;
770
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400771 if (dev->dev_attrib.emulate_write_cache > 0)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400772 p[2] = 0x04; /* Write Cache Enable */
773 p[12] = 0x20; /* Disabled Read Ahead */
774
Roland Dreierd4b2b862012-10-31 09:16:48 -0700775out:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400776 return 20;
777}
778
Roland Dreier0f6d64c2012-10-31 09:16:49 -0700779static int spc_modesense_informational_exceptions(struct se_device *dev, u8 pc, unsigned char *p)
780{
781 p[0] = 0x1c;
782 p[1] = 0x0a;
783
784 /* No changeable values for now */
785 if (pc == 1)
786 goto out;
787
788out:
789 return 12;
790}
791
Roland Dreierd4b2b862012-10-31 09:16:48 -0700792static struct {
793 uint8_t page;
794 uint8_t subpage;
795 int (*emulate)(struct se_device *, u8, unsigned char *);
796} modesense_handlers[] = {
797 { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
798 { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
799 { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
Roland Dreier0f6d64c2012-10-31 09:16:49 -0700800 { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
Roland Dreierd4b2b862012-10-31 09:16:48 -0700801};
802
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400803static void spc_modesense_write_protect(unsigned char *buf, int type)
804{
805 /*
806 * I believe that the WP bit (bit 7) in the mode header is the same for
807 * all device types..
808 */
809 switch (type) {
810 case TYPE_DISK:
811 case TYPE_TAPE:
812 default:
813 buf[0] |= 0x80; /* WP bit */
814 break;
815 }
816}
817
818static void spc_modesense_dpofua(unsigned char *buf, int type)
819{
820 switch (type) {
821 case TYPE_DISK:
822 buf[0] |= 0x10; /* DPOFUA bit */
823 break;
824 default:
825 break;
826 }
827}
828
Roland Dreierd4b2b862012-10-31 09:16:48 -0700829static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
830{
831 *buf++ = 8;
832 put_unaligned_be32(min(blocks, 0xffffffffull), buf);
833 buf += 4;
834 put_unaligned_be32(block_size, buf);
835 return 9;
836}
837
838static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
839{
840 if (blocks <= 0xffffffff)
841 return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
842
843 *buf++ = 1; /* LONGLBA */
844 buf += 2;
845 *buf++ = 16;
846 put_unaligned_be64(blocks, buf);
847 buf += 12;
848 put_unaligned_be32(block_size, buf);
849
850 return 17;
851}
852
Christoph Hellwigde103c92012-11-06 12:24:09 -0800853static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400854{
855 struct se_device *dev = cmd->se_dev;
856 char *cdb = cmd->t_task_cdb;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700857 unsigned char *buf, *map_buf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400858 int type = dev->transport->get_device_type(dev);
859 int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
Roland Dreierd4b2b862012-10-31 09:16:48 -0700860 bool dbd = !!(cdb[1] & 0x08);
861 bool llba = ten ? !!(cdb[1] & 0x10) : false;
862 u8 pc = cdb[2] >> 6;
863 u8 page = cdb[2] & 0x3f;
864 u8 subpage = cdb[3];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400865 int length = 0;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700866 int ret;
867 int i;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400868
Roland Dreierd4b2b862012-10-31 09:16:48 -0700869 map_buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800870 if (!map_buf)
871 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700872 /*
873 * If SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is not set, then we
874 * know we actually allocated a full page. Otherwise, if the
875 * data buffer is too small, allocate a temporary buffer so we
876 * don't have to worry about overruns in all our INQUIRY
877 * emulation handling.
878 */
879 if (cmd->data_length < SE_MODE_PAGE_BUF &&
880 (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
881 buf = kzalloc(SE_MODE_PAGE_BUF, GFP_KERNEL);
882 if (!buf) {
883 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800884 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700885 }
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400886 } else {
Roland Dreierd4b2b862012-10-31 09:16:48 -0700887 buf = map_buf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400888 }
Nicholas Bellingerfecae402012-11-01 18:43:03 -0700889 /*
890 * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
891 * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
892 */
893 length = ten ? 3 : 2;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700894
895 /* DEVICE-SPECIFIC PARAMETER */
896 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
897 (cmd->se_deve &&
898 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
899 spc_modesense_write_protect(&buf[length], type);
900
901 if ((dev->dev_attrib.emulate_write_cache > 0) &&
902 (dev->dev_attrib.emulate_fua_write > 0))
903 spc_modesense_dpofua(&buf[length], type);
904
905 ++length;
906
907 /* BLOCK DESCRIPTOR */
908
909 /*
910 * For now we only include a block descriptor for disk (SBC)
911 * devices; other command sets use a slightly different format.
912 */
913 if (!dbd && type == TYPE_DISK) {
914 u64 blocks = dev->transport->get_blocks(dev);
915 u32 block_size = dev->dev_attrib.block_size;
916
917 if (ten) {
918 if (llba) {
919 length += spc_modesense_long_blockdesc(&buf[length],
920 blocks, block_size);
921 } else {
922 length += 3;
923 length += spc_modesense_blockdesc(&buf[length],
924 blocks, block_size);
925 }
926 } else {
927 length += spc_modesense_blockdesc(&buf[length], blocks,
928 block_size);
929 }
930 } else {
931 if (ten)
932 length += 4;
933 else
934 length += 1;
Paolo Bonzini7a3f3692012-09-07 17:30:39 +0200935 }
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400936
Roland Dreierd4b2b862012-10-31 09:16:48 -0700937 if (page == 0x3f) {
938 if (subpage != 0x00 && subpage != 0xff) {
Christoph Hellwigde103c92012-11-06 12:24:09 -0800939 pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
940 kfree(buf);
941 transport_kunmap_data_sg(cmd);
942 return TCM_INVALID_CDB_FIELD;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700943 }
944
945 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
946 /*
947 * Tricky way to say all subpage 00h for
948 * subpage==0, all subpages for subpage==0xff
949 * (and we just checked above that those are
950 * the only two possibilities).
951 */
952 if ((modesense_handlers[i].subpage & ~subpage) == 0) {
953 ret = modesense_handlers[i].emulate(dev, pc, &buf[length]);
954 if (!ten && length + ret >= 255)
955 break;
956 length += ret;
957 }
958 }
959
960 goto set_length;
961 }
962
963 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
964 if (modesense_handlers[i].page == page &&
965 modesense_handlers[i].subpage == subpage) {
966 length += modesense_handlers[i].emulate(dev, pc, &buf[length]);
967 goto set_length;
968 }
969
970 /*
971 * We don't intend to implement:
972 * - obsolete page 03h "format parameters" (checked by Solaris)
973 */
974 if (page != 0x03)
975 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
976 page, subpage);
977
Christoph Hellwigde103c92012-11-06 12:24:09 -0800978 transport_kunmap_data_sg(cmd);
979 return TCM_UNKNOWN_MODE_PAGE;
Roland Dreierd4b2b862012-10-31 09:16:48 -0700980
981set_length:
982 if (ten)
983 put_unaligned_be16(length - 2, buf);
984 else
985 buf[0] = length - 1;
986
Roland Dreierd4b2b862012-10-31 09:16:48 -0700987 if (buf != map_buf) {
988 memcpy(map_buf, buf, cmd->data_length);
989 kfree(buf);
990 }
991
992 transport_kunmap_data_sg(cmd);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400993 target_complete_cmd(cmd, GOOD);
994 return 0;
995}
996
Christoph Hellwigde103c92012-11-06 12:24:09 -0800997static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
Roland Dreier3a3c5e42012-10-31 09:16:50 -0700998{
999 struct se_device *dev = cmd->se_dev;
1000 char *cdb = cmd->t_task_cdb;
1001 bool ten = cdb[0] == MODE_SELECT_10;
1002 int off = ten ? 8 : 4;
1003 bool pf = !!(cdb[1] & 0x10);
1004 u8 page, subpage;
1005 unsigned char *buf;
1006 unsigned char tbuf[SE_MODE_PAGE_BUF];
1007 int length;
1008 int ret = 0;
1009 int i;
1010
1011 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001012 if (!buf)
1013 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001014
1015 if (!pf) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08001016 ret = TCM_INVALID_CDB_FIELD;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001017 goto out;
1018 }
1019
1020 page = buf[off] & 0x3f;
1021 subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
1022
1023 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
1024 if (modesense_handlers[i].page == page &&
1025 modesense_handlers[i].subpage == subpage) {
1026 memset(tbuf, 0, SE_MODE_PAGE_BUF);
1027 length = modesense_handlers[i].emulate(dev, 0, tbuf);
1028 goto check_contents;
1029 }
1030
Christoph Hellwigde103c92012-11-06 12:24:09 -08001031 ret = TCM_UNKNOWN_MODE_PAGE;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001032 goto out;
1033
1034check_contents:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001035 if (memcmp(buf + off, tbuf, length))
1036 ret = TCM_INVALID_PARAMETER_LIST;
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001037
1038out:
1039 transport_kunmap_data_sg(cmd);
1040
1041 if (!ret)
1042 target_complete_cmd(cmd, GOOD);
1043 return ret;
1044}
1045
Christoph Hellwigde103c92012-11-06 12:24:09 -08001046static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001047{
1048 unsigned char *cdb = cmd->t_task_cdb;
Paolo Bonzini32a88112012-09-07 17:30:36 +02001049 unsigned char *rbuf;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001050 u8 ua_asc = 0, ua_ascq = 0;
Paolo Bonzini32a88112012-09-07 17:30:36 +02001051 unsigned char buf[SE_SENSE_BUF];
1052
1053 memset(buf, 0, SE_SENSE_BUF);
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001054
1055 if (cdb[1] & 0x01) {
1056 pr_err("REQUEST_SENSE description emulation not"
1057 " supported\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001058 return TCM_INVALID_CDB_FIELD;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001059 }
1060
Paolo Bonzini32a88112012-09-07 17:30:36 +02001061 rbuf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001062 if (!rbuf)
1063 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1064
1065 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001066 /*
1067 * CURRENT ERROR, UNIT ATTENTION
1068 */
1069 buf[0] = 0x70;
1070 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
1071
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001072 /*
1073 * The Additional Sense Code (ASC) from the UNIT ATTENTION
1074 */
1075 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
1076 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
1077 buf[7] = 0x0A;
1078 } else {
1079 /*
1080 * CURRENT ERROR, NO SENSE
1081 */
1082 buf[0] = 0x70;
1083 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
1084
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001085 /*
1086 * NO ADDITIONAL SENSE INFORMATION
1087 */
1088 buf[SPC_ASC_KEY_OFFSET] = 0x00;
1089 buf[7] = 0x0A;
1090 }
1091
Christoph Hellwigde103c92012-11-06 12:24:09 -08001092 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
1093 transport_kunmap_data_sg(cmd);
Paolo Bonzini32a88112012-09-07 17:30:36 +02001094
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001095 target_complete_cmd(cmd, GOOD);
1096 return 0;
1097}
1098
Christoph Hellwigde103c92012-11-06 12:24:09 -08001099sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001100{
1101 struct se_dev_entry *deve;
1102 struct se_session *sess = cmd->se_sess;
1103 unsigned char *buf;
1104 u32 lun_count = 0, offset = 8, i;
1105
1106 if (cmd->data_length < 16) {
1107 pr_warn("REPORT LUNS allocation length %u too small\n",
1108 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001109 return TCM_INVALID_CDB_FIELD;
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001110 }
1111
1112 buf = transport_kmap_data_sg(cmd);
1113 if (!buf)
Christoph Hellwigde103c92012-11-06 12:24:09 -08001114 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001115
1116 /*
1117 * If no struct se_session pointer is present, this struct se_cmd is
1118 * coming via a target_core_mod PASSTHROUGH op, and not through
1119 * a $FABRIC_MOD. In that case, report LUN=0 only.
1120 */
1121 if (!sess) {
1122 int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
1123 lun_count = 1;
1124 goto done;
1125 }
1126
1127 spin_lock_irq(&sess->se_node_acl->device_list_lock);
1128 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
1129 deve = sess->se_node_acl->device_list[i];
1130 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
1131 continue;
1132 /*
1133 * We determine the correct LUN LIST LENGTH even once we
1134 * have reached the initial allocation length.
1135 * See SPC2-R20 7.19.
1136 */
1137 lun_count++;
1138 if ((offset + 8) > cmd->data_length)
1139 continue;
1140
1141 int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
1142 offset += 8;
1143 }
1144 spin_unlock_irq(&sess->se_node_acl->device_list_lock);
1145
1146 /*
1147 * See SPC3 r07, page 159.
1148 */
1149done:
1150 lun_count *= 8;
1151 buf[0] = ((lun_count >> 24) & 0xff);
1152 buf[1] = ((lun_count >> 16) & 0xff);
1153 buf[2] = ((lun_count >> 8) & 0xff);
1154 buf[3] = (lun_count & 0xff);
1155 transport_kunmap_data_sg(cmd);
1156
1157 target_complete_cmd(cmd, GOOD);
1158 return 0;
1159}
Christoph Hellwig8de530a2012-10-07 10:55:52 -04001160EXPORT_SYMBOL(spc_emulate_report_luns);
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001161
Christoph Hellwigde103c92012-11-06 12:24:09 -08001162static sense_reason_t
1163spc_emulate_testunitready(struct se_cmd *cmd)
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001164{
1165 target_complete_cmd(cmd, GOOD);
1166 return 0;
1167}
1168
Christoph Hellwigde103c92012-11-06 12:24:09 -08001169sense_reason_t
1170spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001171{
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001172 struct se_device *dev = cmd->se_dev;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001173 unsigned char *cdb = cmd->t_task_cdb;
1174
1175 switch (cdb[0]) {
1176 case MODE_SELECT:
1177 *size = cdb[4];
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001178 cmd->execute_cmd = spc_emulate_modeselect;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001179 break;
1180 case MODE_SELECT_10:
1181 *size = (cdb[7] << 8) + cdb[8];
Roland Dreier3a3c5e42012-10-31 09:16:50 -07001182 cmd->execute_cmd = spc_emulate_modeselect;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001183 break;
1184 case MODE_SENSE:
1185 *size = cdb[4];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001186 cmd->execute_cmd = spc_emulate_modesense;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001187 break;
1188 case MODE_SENSE_10:
1189 *size = (cdb[7] << 8) + cdb[8];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001190 cmd->execute_cmd = spc_emulate_modesense;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001191 break;
1192 case LOG_SELECT:
1193 case LOG_SENSE:
1194 *size = (cdb[7] << 8) + cdb[8];
1195 break;
1196 case PERSISTENT_RESERVE_IN:
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001197 *size = (cdb[7] << 8) + cdb[8];
Christoph Hellwigd977f432012-10-10 17:37:15 -04001198 cmd->execute_cmd = target_scsi3_emulate_pr_in;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001199 break;
1200 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001201 *size = (cdb[7] << 8) + cdb[8];
Christoph Hellwigd977f432012-10-10 17:37:15 -04001202 cmd->execute_cmd = target_scsi3_emulate_pr_out;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001203 break;
1204 case RELEASE:
1205 case RELEASE_10:
1206 if (cdb[0] == RELEASE_10)
1207 *size = (cdb[7] << 8) | cdb[8];
1208 else
1209 *size = cmd->data_length;
1210
Christoph Hellwigd977f432012-10-10 17:37:15 -04001211 cmd->execute_cmd = target_scsi2_reservation_release;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001212 break;
1213 case RESERVE:
1214 case RESERVE_10:
1215 /*
1216 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
1217 * Assume the passthrough or $FABRIC_MOD will tell us about it.
1218 */
1219 if (cdb[0] == RESERVE_10)
1220 *size = (cdb[7] << 8) | cdb[8];
1221 else
1222 *size = cmd->data_length;
1223
Christoph Hellwigd977f432012-10-10 17:37:15 -04001224 cmd->execute_cmd = target_scsi2_reservation_reserve;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001225 break;
1226 case REQUEST_SENSE:
1227 *size = cdb[4];
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001228 cmd->execute_cmd = spc_emulate_request_sense;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001229 break;
1230 case INQUIRY:
1231 *size = (cdb[3] << 8) + cdb[4];
1232
1233 /*
1234 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
1235 * See spc4r17 section 5.3
1236 */
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001237 cmd->sam_task_attr = MSG_HEAD_TAG;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001238 cmd->execute_cmd = spc_emulate_inquiry;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001239 break;
1240 case SECURITY_PROTOCOL_IN:
1241 case SECURITY_PROTOCOL_OUT:
1242 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1243 break;
1244 case EXTENDED_COPY:
1245 case READ_ATTRIBUTE:
1246 case RECEIVE_COPY_RESULTS:
1247 case WRITE_ATTRIBUTE:
1248 *size = (cdb[10] << 24) | (cdb[11] << 16) |
1249 (cdb[12] << 8) | cdb[13];
1250 break;
1251 case RECEIVE_DIAGNOSTIC:
1252 case SEND_DIAGNOSTIC:
1253 *size = (cdb[3] << 8) | cdb[4];
1254 break;
1255 case WRITE_BUFFER:
1256 *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
1257 break;
1258 case REPORT_LUNS:
Christoph Hellwigd1b1f802012-10-07 10:55:51 -04001259 cmd->execute_cmd = spc_emulate_report_luns;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001260 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1261 /*
1262 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
1263 * See spc4r17 section 5.3
1264 */
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001265 cmd->sam_task_attr = MSG_HEAD_TAG;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001266 break;
1267 case TEST_UNIT_READY:
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001268 cmd->execute_cmd = spc_emulate_testunitready;
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001269 *size = 0;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001270 break;
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001271 case MAINTENANCE_IN:
1272 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1273 /*
1274 * MAINTENANCE_IN from SCC-2
1275 * Check for emulated MI_REPORT_TARGET_PGS
1276 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001277 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001278 cmd->execute_cmd =
1279 target_emulate_report_target_port_groups;
1280 }
1281 *size = get_unaligned_be32(&cdb[6]);
1282 } else {
1283 /*
1284 * GPCMD_SEND_KEY from multi media commands
1285 */
1286 *size = get_unaligned_be16(&cdb[8]);
1287 }
1288 break;
1289 case MAINTENANCE_OUT:
1290 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
1291 /*
1292 * MAINTENANCE_OUT from SCC-2
1293 * Check for emulated MO_SET_TARGET_PGS.
1294 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001295 if (cdb[1] == MO_SET_TARGET_PGS) {
Nicholas Bellingereba2ca42012-05-30 14:09:10 -07001296 cmd->execute_cmd =
1297 target_emulate_set_target_port_groups;
1298 }
1299 *size = get_unaligned_be32(&cdb[6]);
1300 } else {
1301 /*
1302 * GPCMD_SEND_KEY from multi media commands
1303 */
1304 *size = get_unaligned_be16(&cdb[8]);
1305 }
1306 break;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001307 default:
1308 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
1309 " 0x%02x, sending CHECK_CONDITION.\n",
1310 cmd->se_tfo->get_fabric_name(), cdb[0]);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001311 return TCM_UNSUPPORTED_SCSI_OPCODE;
Christoph Hellwig88455ec2012-05-20 11:59:13 -04001312 }
1313
1314 return 0;
1315}
1316EXPORT_SYMBOL(spc_parse_cdb);