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