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