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