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