diff options
author | Christoph Hellwig <hch@infradead.org> | 2012-05-20 11:59:14 -0400 |
---|---|---|
committer | Nicholas Bellinger <nab@linux-iscsi.org> | 2012-07-16 20:25:56 -0400 |
commit | d6e0175cf3f9737a760482d185bb73566bcc9331 (patch) | |
tree | 99ca8361ebc8c4e83f8335193963faa3f30007b5 /drivers/target/target_core_sbc.c | |
parent | 88455ec4be02c395820b1ff57656b0844ec03ac3 (diff) |
target: add a parse_cdb method to the backend drivers
Instead of trying to handle all SCSI command sets in one function
(transport_generic_cmd_sequencer) call out to the backend driver to perform
this functionality. For pSCSI a copy of the existing code is used, but for
all virtual backends we can use a new parse_sbc_cdb helper is used to
provide a simple SBC emulation.
For now this setups means a fair amount of duplication between pSCSI and the
SBC library, but patches later in this series will sort out that problem.
(nab: Fix up build failure in target_core_pscsi.c)
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
Diffstat (limited to 'drivers/target/target_core_sbc.c')
-rw-r--r-- | drivers/target/target_core_sbc.c | 450 |
1 files changed, 450 insertions, 0 deletions
diff --git a/drivers/target/target_core_sbc.c b/drivers/target/target_core_sbc.c new file mode 100644 index 000000000000..9d1ca3814876 --- /dev/null +++ b/drivers/target/target_core_sbc.c | |||
@@ -0,0 +1,450 @@ | |||
1 | /* | ||
2 | * SCSI Block Commands (SBC) parsing and emulation. | ||
3 | * | ||
4 | * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc. | ||
5 | * Copyright (c) 2005, 2006, 2007 SBE, Inc. | ||
6 | * Copyright (c) 2007-2010 Rising Tide Systems | ||
7 | * Copyright (c) 2008-2010 Linux-iSCSI.org | ||
8 | * | ||
9 | * Nicholas A. Bellinger <nab@kernel.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | * You should have received a copy of the GNU General Public License | ||
22 | * along with this program; if not, write to the Free Software | ||
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
24 | */ | ||
25 | |||
26 | #include <linux/kernel.h> | ||
27 | #include <linux/module.h> | ||
28 | #include <linux/ratelimit.h> | ||
29 | #include <asm/unaligned.h> | ||
30 | #include <scsi/scsi.h> | ||
31 | |||
32 | #include <target/target_core_base.h> | ||
33 | #include <target/target_core_backend.h> | ||
34 | #include <target/target_core_fabric.h> | ||
35 | |||
36 | #include "target_core_internal.h" | ||
37 | #include "target_core_ua.h" | ||
38 | |||
39 | |||
40 | static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors) | ||
41 | { | ||
42 | return cmd->se_dev->se_sub_dev->se_dev_attrib.block_size * sectors; | ||
43 | } | ||
44 | |||
45 | static int sbc_check_valid_sectors(struct se_cmd *cmd) | ||
46 | { | ||
47 | struct se_device *dev = cmd->se_dev; | ||
48 | unsigned long long end_lba; | ||
49 | u32 sectors; | ||
50 | |||
51 | sectors = cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size; | ||
52 | end_lba = dev->transport->get_blocks(dev) + 1; | ||
53 | |||
54 | if (cmd->t_task_lba + sectors > end_lba) { | ||
55 | pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n", | ||
56 | cmd->t_task_lba, sectors, end_lba); | ||
57 | return -EINVAL; | ||
58 | } | ||
59 | |||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | static inline u32 transport_get_sectors_6(unsigned char *cdb) | ||
64 | { | ||
65 | /* | ||
66 | * Use 8-bit sector value. SBC-3 says: | ||
67 | * | ||
68 | * A TRANSFER LENGTH field set to zero specifies that 256 | ||
69 | * logical blocks shall be written. Any other value | ||
70 | * specifies the number of logical blocks that shall be | ||
71 | * written. | ||
72 | */ | ||
73 | return cdb[4] ? : 256; | ||
74 | } | ||
75 | |||
76 | static inline u32 transport_get_sectors_10(unsigned char *cdb) | ||
77 | { | ||
78 | return (u32)(cdb[7] << 8) + cdb[8]; | ||
79 | } | ||
80 | |||
81 | static inline u32 transport_get_sectors_12(unsigned char *cdb) | ||
82 | { | ||
83 | return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; | ||
84 | } | ||
85 | |||
86 | static inline u32 transport_get_sectors_16(unsigned char *cdb) | ||
87 | { | ||
88 | return (u32)(cdb[10] << 24) + (cdb[11] << 16) + | ||
89 | (cdb[12] << 8) + cdb[13]; | ||
90 | } | ||
91 | |||
92 | /* | ||
93 | * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants | ||
94 | */ | ||
95 | static inline u32 transport_get_sectors_32(unsigned char *cdb) | ||
96 | { | ||
97 | return (u32)(cdb[28] << 24) + (cdb[29] << 16) + | ||
98 | (cdb[30] << 8) + cdb[31]; | ||
99 | |||
100 | } | ||
101 | |||
102 | static inline u32 transport_lba_21(unsigned char *cdb) | ||
103 | { | ||
104 | return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; | ||
105 | } | ||
106 | |||
107 | static inline u32 transport_lba_32(unsigned char *cdb) | ||
108 | { | ||
109 | return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; | ||
110 | } | ||
111 | |||
112 | static inline unsigned long long transport_lba_64(unsigned char *cdb) | ||
113 | { | ||
114 | unsigned int __v1, __v2; | ||
115 | |||
116 | __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; | ||
117 | __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; | ||
118 | |||
119 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; | ||
120 | } | ||
121 | |||
122 | /* | ||
123 | * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs | ||
124 | */ | ||
125 | static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) | ||
126 | { | ||
127 | unsigned int __v1, __v2; | ||
128 | |||
129 | __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15]; | ||
130 | __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19]; | ||
131 | |||
132 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; | ||
133 | } | ||
134 | |||
135 | static int sbc_write_same_supported(struct se_device *dev, | ||
136 | unsigned char *flags) | ||
137 | { | ||
138 | if ((flags[0] & 0x04) || (flags[0] & 0x02)) { | ||
139 | pr_err("WRITE_SAME PBDATA and LBDATA" | ||
140 | " bits not supported for Block Discard" | ||
141 | " Emulation\n"); | ||
142 | return -ENOSYS; | ||
143 | } | ||
144 | |||
145 | /* | ||
146 | * Currently for the emulated case we only accept | ||
147 | * tpws with the UNMAP=1 bit set. | ||
148 | */ | ||
149 | if (!(flags[0] & 0x08)) { | ||
150 | pr_err("WRITE_SAME w/o UNMAP bit not" | ||
151 | " supported for Block Discard Emulation\n"); | ||
152 | return -ENOSYS; | ||
153 | } | ||
154 | |||
155 | return 0; | ||
156 | } | ||
157 | |||
158 | static void xdreadwrite_callback(struct se_cmd *cmd) | ||
159 | { | ||
160 | unsigned char *buf, *addr; | ||
161 | struct scatterlist *sg; | ||
162 | unsigned int offset; | ||
163 | int i; | ||
164 | int count; | ||
165 | /* | ||
166 | * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command | ||
167 | * | ||
168 | * 1) read the specified logical block(s); | ||
169 | * 2) transfer logical blocks from the data-out buffer; | ||
170 | * 3) XOR the logical blocks transferred from the data-out buffer with | ||
171 | * the logical blocks read, storing the resulting XOR data in a buffer; | ||
172 | * 4) if the DISABLE WRITE bit is set to zero, then write the logical | ||
173 | * blocks transferred from the data-out buffer; and | ||
174 | * 5) transfer the resulting XOR data to the data-in buffer. | ||
175 | */ | ||
176 | buf = kmalloc(cmd->data_length, GFP_KERNEL); | ||
177 | if (!buf) { | ||
178 | pr_err("Unable to allocate xor_callback buf\n"); | ||
179 | return; | ||
180 | } | ||
181 | /* | ||
182 | * Copy the scatterlist WRITE buffer located at cmd->t_data_sg | ||
183 | * into the locally allocated *buf | ||
184 | */ | ||
185 | sg_copy_to_buffer(cmd->t_data_sg, | ||
186 | cmd->t_data_nents, | ||
187 | buf, | ||
188 | cmd->data_length); | ||
189 | |||
190 | /* | ||
191 | * Now perform the XOR against the BIDI read memory located at | ||
192 | * cmd->t_mem_bidi_list | ||
193 | */ | ||
194 | |||
195 | offset = 0; | ||
196 | for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { | ||
197 | addr = kmap_atomic(sg_page(sg)); | ||
198 | if (!addr) | ||
199 | goto out; | ||
200 | |||
201 | for (i = 0; i < sg->length; i++) | ||
202 | *(addr + sg->offset + i) ^= *(buf + offset + i); | ||
203 | |||
204 | offset += sg->length; | ||
205 | kunmap_atomic(addr); | ||
206 | } | ||
207 | |||
208 | out: | ||
209 | kfree(buf); | ||
210 | } | ||
211 | |||
212 | int sbc_parse_cdb(struct se_cmd *cmd, unsigned int *size) | ||
213 | { | ||
214 | struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev; | ||
215 | struct se_device *dev = cmd->se_dev; | ||
216 | unsigned char *cdb = cmd->t_task_cdb; | ||
217 | u32 sectors = 0; | ||
218 | int ret; | ||
219 | |||
220 | switch (cdb[0]) { | ||
221 | case READ_6: | ||
222 | sectors = transport_get_sectors_6(cdb); | ||
223 | cmd->t_task_lba = transport_lba_21(cdb); | ||
224 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
225 | break; | ||
226 | case READ_10: | ||
227 | sectors = transport_get_sectors_10(cdb); | ||
228 | cmd->t_task_lba = transport_lba_32(cdb); | ||
229 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
230 | break; | ||
231 | case READ_12: | ||
232 | sectors = transport_get_sectors_12(cdb); | ||
233 | cmd->t_task_lba = transport_lba_32(cdb); | ||
234 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
235 | break; | ||
236 | case READ_16: | ||
237 | sectors = transport_get_sectors_16(cdb); | ||
238 | cmd->t_task_lba = transport_lba_64(cdb); | ||
239 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
240 | break; | ||
241 | case WRITE_6: | ||
242 | sectors = transport_get_sectors_6(cdb); | ||
243 | cmd->t_task_lba = transport_lba_21(cdb); | ||
244 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
245 | break; | ||
246 | case WRITE_10: | ||
247 | case WRITE_VERIFY: | ||
248 | sectors = transport_get_sectors_10(cdb); | ||
249 | cmd->t_task_lba = transport_lba_32(cdb); | ||
250 | if (cdb[1] & 0x8) | ||
251 | cmd->se_cmd_flags |= SCF_FUA; | ||
252 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
253 | break; | ||
254 | case WRITE_12: | ||
255 | sectors = transport_get_sectors_12(cdb); | ||
256 | cmd->t_task_lba = transport_lba_32(cdb); | ||
257 | if (cdb[1] & 0x8) | ||
258 | cmd->se_cmd_flags |= SCF_FUA; | ||
259 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
260 | break; | ||
261 | case WRITE_16: | ||
262 | sectors = transport_get_sectors_16(cdb); | ||
263 | cmd->t_task_lba = transport_lba_64(cdb); | ||
264 | if (cdb[1] & 0x8) | ||
265 | cmd->se_cmd_flags |= SCF_FUA; | ||
266 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
267 | break; | ||
268 | case XDWRITEREAD_10: | ||
269 | if ((cmd->data_direction != DMA_TO_DEVICE) || | ||
270 | !(cmd->se_cmd_flags & SCF_BIDI)) | ||
271 | goto out_invalid_cdb_field; | ||
272 | sectors = transport_get_sectors_10(cdb); | ||
273 | |||
274 | cmd->t_task_lba = transport_lba_32(cdb); | ||
275 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
276 | |||
277 | /* | ||
278 | * Setup BIDI XOR callback to be run after I/O completion. | ||
279 | */ | ||
280 | cmd->transport_complete_callback = &xdreadwrite_callback; | ||
281 | if (cdb[1] & 0x8) | ||
282 | cmd->se_cmd_flags |= SCF_FUA; | ||
283 | break; | ||
284 | case VARIABLE_LENGTH_CMD: | ||
285 | { | ||
286 | u16 service_action = get_unaligned_be16(&cdb[8]); | ||
287 | switch (service_action) { | ||
288 | case XDWRITEREAD_32: | ||
289 | sectors = transport_get_sectors_32(cdb); | ||
290 | |||
291 | /* | ||
292 | * Use WRITE_32 and READ_32 opcodes for the emulated | ||
293 | * XDWRITE_READ_32 logic. | ||
294 | */ | ||
295 | cmd->t_task_lba = transport_lba_64_ext(cdb); | ||
296 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | ||
297 | |||
298 | /* | ||
299 | * Setup BIDI XOR callback to be run during after I/O | ||
300 | * completion. | ||
301 | */ | ||
302 | cmd->transport_complete_callback = &xdreadwrite_callback; | ||
303 | if (cdb[1] & 0x8) | ||
304 | cmd->se_cmd_flags |= SCF_FUA; | ||
305 | break; | ||
306 | case WRITE_SAME_32: | ||
307 | sectors = transport_get_sectors_32(cdb); | ||
308 | if (!sectors) { | ||
309 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" | ||
310 | " supported\n"); | ||
311 | goto out_invalid_cdb_field; | ||
312 | } | ||
313 | |||
314 | *size = sbc_get_size(cmd, 1); | ||
315 | cmd->t_task_lba = get_unaligned_be64(&cdb[12]); | ||
316 | |||
317 | if (sbc_write_same_supported(dev, &cdb[10]) < 0) | ||
318 | goto out_unsupported_cdb; | ||
319 | cmd->execute_cmd = target_emulate_write_same; | ||
320 | break; | ||
321 | default: | ||
322 | pr_err("VARIABLE_LENGTH_CMD service action" | ||
323 | " 0x%04x not supported\n", service_action); | ||
324 | goto out_unsupported_cdb; | ||
325 | } | ||
326 | break; | ||
327 | } | ||
328 | case READ_CAPACITY: | ||
329 | *size = READ_CAP_LEN; | ||
330 | cmd->execute_cmd = target_emulate_readcapacity; | ||
331 | break; | ||
332 | case SERVICE_ACTION_IN: | ||
333 | switch (cmd->t_task_cdb[1] & 0x1f) { | ||
334 | case SAI_READ_CAPACITY_16: | ||
335 | cmd->execute_cmd = target_emulate_readcapacity_16; | ||
336 | break; | ||
337 | default: | ||
338 | pr_err("Unsupported SA: 0x%02x\n", | ||
339 | cmd->t_task_cdb[1] & 0x1f); | ||
340 | goto out_invalid_cdb_field; | ||
341 | } | ||
342 | *size = (cdb[10] << 24) | (cdb[11] << 16) | | ||
343 | (cdb[12] << 8) | cdb[13]; | ||
344 | break; | ||
345 | case SYNCHRONIZE_CACHE: | ||
346 | case SYNCHRONIZE_CACHE_16: | ||
347 | /* | ||
348 | * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE | ||
349 | */ | ||
350 | if (cdb[0] == SYNCHRONIZE_CACHE) { | ||
351 | sectors = transport_get_sectors_10(cdb); | ||
352 | cmd->t_task_lba = transport_lba_32(cdb); | ||
353 | } else { | ||
354 | sectors = transport_get_sectors_16(cdb); | ||
355 | cmd->t_task_lba = transport_lba_64(cdb); | ||
356 | } | ||
357 | |||
358 | *size = sbc_get_size(cmd, sectors); | ||
359 | |||
360 | /* | ||
361 | * Check to ensure that LBA + Range does not exceed past end of | ||
362 | * device for IBLOCK and FILEIO ->do_sync_cache() backend calls | ||
363 | */ | ||
364 | if (cmd->t_task_lba || sectors) { | ||
365 | if (sbc_check_valid_sectors(cmd) < 0) | ||
366 | goto out_invalid_cdb_field; | ||
367 | } | ||
368 | cmd->execute_cmd = target_emulate_synchronize_cache; | ||
369 | break; | ||
370 | case UNMAP: | ||
371 | *size = get_unaligned_be16(&cdb[7]); | ||
372 | cmd->execute_cmd = target_emulate_unmap; | ||
373 | break; | ||
374 | case WRITE_SAME_16: | ||
375 | sectors = transport_get_sectors_16(cdb); | ||
376 | if (!sectors) { | ||
377 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); | ||
378 | goto out_invalid_cdb_field; | ||
379 | } | ||
380 | |||
381 | *size = sbc_get_size(cmd, 1); | ||
382 | cmd->t_task_lba = get_unaligned_be64(&cdb[2]); | ||
383 | |||
384 | if (sbc_write_same_supported(dev, &cdb[1]) < 0) | ||
385 | goto out_unsupported_cdb; | ||
386 | cmd->execute_cmd = target_emulate_write_same; | ||
387 | break; | ||
388 | case WRITE_SAME: | ||
389 | sectors = transport_get_sectors_10(cdb); | ||
390 | if (!sectors) { | ||
391 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); | ||
392 | goto out_invalid_cdb_field; | ||
393 | } | ||
394 | |||
395 | *size = sbc_get_size(cmd, 1); | ||
396 | cmd->t_task_lba = get_unaligned_be32(&cdb[2]); | ||
397 | |||
398 | /* | ||
399 | * Follow sbcr26 with WRITE_SAME (10) and check for the existence | ||
400 | * of byte 1 bit 3 UNMAP instead of original reserved field | ||
401 | */ | ||
402 | if (sbc_write_same_supported(dev, &cdb[1]) < 0) | ||
403 | goto out_unsupported_cdb; | ||
404 | cmd->execute_cmd = target_emulate_write_same; | ||
405 | break; | ||
406 | case VERIFY: | ||
407 | *size = 0; | ||
408 | cmd->execute_cmd = target_emulate_noop; | ||
409 | break; | ||
410 | default: | ||
411 | ret = spc_parse_cdb(cmd, size, false); | ||
412 | if (ret) | ||
413 | return ret; | ||
414 | } | ||
415 | |||
416 | /* reject any command that we don't have a handler for */ | ||
417 | if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd) | ||
418 | goto out_unsupported_cdb; | ||
419 | |||
420 | if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { | ||
421 | if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) { | ||
422 | printk_ratelimited(KERN_ERR "SCSI OP %02xh with too" | ||
423 | " big sectors %u exceeds fabric_max_sectors:" | ||
424 | " %u\n", cdb[0], sectors, | ||
425 | su_dev->se_dev_attrib.fabric_max_sectors); | ||
426 | goto out_invalid_cdb_field; | ||
427 | } | ||
428 | if (sectors > su_dev->se_dev_attrib.hw_max_sectors) { | ||
429 | printk_ratelimited(KERN_ERR "SCSI OP %02xh with too" | ||
430 | " big sectors %u exceeds backend hw_max_sectors:" | ||
431 | " %u\n", cdb[0], sectors, | ||
432 | su_dev->se_dev_attrib.hw_max_sectors); | ||
433 | goto out_invalid_cdb_field; | ||
434 | } | ||
435 | |||
436 | *size = sbc_get_size(cmd, sectors); | ||
437 | } | ||
438 | |||
439 | return 0; | ||
440 | |||
441 | out_unsupported_cdb: | ||
442 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | ||
443 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; | ||
444 | return -EINVAL; | ||
445 | out_invalid_cdb_field: | ||
446 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | ||
447 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; | ||
448 | return -EINVAL; | ||
449 | } | ||
450 | EXPORT_SYMBOL(sbc_parse_cdb); | ||