]>
Commit | Line | Data |
---|---|---|
d6e0175c CH |
1 | /* |
2 | * SCSI Block Commands (SBC) parsing and emulation. | |
3 | * | |
4c76251e | 4 | * (c) Copyright 2002-2013 Datera, Inc. |
d6e0175c CH |
5 | * |
6 | * Nicholas A. Bellinger <nab@kernel.org> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/kernel.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/ratelimit.h> | |
41861fa8 | 26 | #include <linux/crc-t10dif.h> |
d6e0175c | 27 | #include <asm/unaligned.h> |
ba929992 | 28 | #include <scsi/scsi_proto.h> |
68ff9b9b | 29 | #include <scsi/scsi_tcq.h> |
d6e0175c CH |
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" | |
c66094bf | 37 | #include "target_core_alua.h" |
d6e0175c | 38 | |
afd73f1b NB |
39 | static sense_reason_t |
40 | sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char *, u32, bool); | |
62e46942 | 41 | static sense_reason_t sbc_execute_unmap(struct se_cmd *cmd); |
afd73f1b | 42 | |
de103c93 CH |
43 | static sense_reason_t |
44 | sbc_emulate_readcapacity(struct se_cmd *cmd) | |
1fd032ee CH |
45 | { |
46 | struct se_device *dev = cmd->se_dev; | |
8dc8632a | 47 | unsigned char *cdb = cmd->t_task_cdb; |
1fd032ee | 48 | unsigned long long blocks_long = dev->transport->get_blocks(dev); |
a50da144 PB |
49 | unsigned char *rbuf; |
50 | unsigned char buf[8]; | |
1fd032ee CH |
51 | u32 blocks; |
52 | ||
8dc8632a RD |
53 | /* |
54 | * SBC-2 says: | |
55 | * If the PMI bit is set to zero and the LOGICAL BLOCK | |
56 | * ADDRESS field is not set to zero, the device server shall | |
57 | * terminate the command with CHECK CONDITION status with | |
58 | * the sense key set to ILLEGAL REQUEST and the additional | |
59 | * sense code set to INVALID FIELD IN CDB. | |
60 | * | |
61 | * In SBC-3, these fields are obsolete, but some SCSI | |
62 | * compliance tests actually check this, so we might as well | |
63 | * follow SBC-2. | |
64 | */ | |
65 | if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5])) | |
66 | return TCM_INVALID_CDB_FIELD; | |
67 | ||
1fd032ee CH |
68 | if (blocks_long >= 0x00000000ffffffff) |
69 | blocks = 0xffffffff; | |
70 | else | |
71 | blocks = (u32)blocks_long; | |
72 | ||
1fd032ee CH |
73 | buf[0] = (blocks >> 24) & 0xff; |
74 | buf[1] = (blocks >> 16) & 0xff; | |
75 | buf[2] = (blocks >> 8) & 0xff; | |
76 | buf[3] = blocks & 0xff; | |
0fd97ccf CH |
77 | buf[4] = (dev->dev_attrib.block_size >> 24) & 0xff; |
78 | buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff; | |
79 | buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff; | |
80 | buf[7] = dev->dev_attrib.block_size & 0xff; | |
1fd032ee | 81 | |
a50da144 | 82 | rbuf = transport_kmap_data_sg(cmd); |
8b4b0dcb NB |
83 | if (rbuf) { |
84 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); | |
85 | transport_kunmap_data_sg(cmd); | |
86 | } | |
1fd032ee | 87 | |
2426bd45 | 88 | target_complete_cmd_with_length(cmd, GOOD, 8); |
1fd032ee CH |
89 | return 0; |
90 | } | |
91 | ||
de103c93 CH |
92 | static sense_reason_t |
93 | sbc_emulate_readcapacity_16(struct se_cmd *cmd) | |
1fd032ee CH |
94 | { |
95 | struct se_device *dev = cmd->se_dev; | |
2d335983 | 96 | struct se_session *sess = cmd->se_sess; |
9ef5466e NB |
97 | int pi_prot_type = dev->dev_attrib.pi_prot_type; |
98 | ||
a50da144 PB |
99 | unsigned char *rbuf; |
100 | unsigned char buf[32]; | |
1fd032ee CH |
101 | unsigned long long blocks = dev->transport->get_blocks(dev); |
102 | ||
a50da144 | 103 | memset(buf, 0, sizeof(buf)); |
1fd032ee CH |
104 | buf[0] = (blocks >> 56) & 0xff; |
105 | buf[1] = (blocks >> 48) & 0xff; | |
106 | buf[2] = (blocks >> 40) & 0xff; | |
107 | buf[3] = (blocks >> 32) & 0xff; | |
108 | buf[4] = (blocks >> 24) & 0xff; | |
109 | buf[5] = (blocks >> 16) & 0xff; | |
110 | buf[6] = (blocks >> 8) & 0xff; | |
111 | buf[7] = blocks & 0xff; | |
0fd97ccf CH |
112 | buf[8] = (dev->dev_attrib.block_size >> 24) & 0xff; |
113 | buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff; | |
114 | buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff; | |
115 | buf[11] = dev->dev_attrib.block_size & 0xff; | |
56dac14c NB |
116 | /* |
117 | * Set P_TYPE and PROT_EN bits for DIF support | |
118 | */ | |
2d335983 | 119 | if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { |
9ef5466e NB |
120 | /* |
121 | * Only override a device's pi_prot_type if no T10-PI is | |
122 | * available, and sess_prot_type has been explicitly enabled. | |
123 | */ | |
124 | if (!pi_prot_type) | |
125 | pi_prot_type = sess->sess_prot_type; | |
126 | ||
127 | if (pi_prot_type) | |
128 | buf[12] = (pi_prot_type - 1) << 1 | 0x1; | |
2d335983 | 129 | } |
7f7caf6a AG |
130 | |
131 | if (dev->transport->get_lbppbe) | |
132 | buf[13] = dev->transport->get_lbppbe(dev) & 0x0f; | |
133 | ||
134 | if (dev->transport->get_alignment_offset_lbas) { | |
135 | u16 lalba = dev->transport->get_alignment_offset_lbas(dev); | |
136 | buf[14] = (lalba >> 8) & 0x3f; | |
137 | buf[15] = lalba & 0xff; | |
138 | } | |
139 | ||
1fd032ee CH |
140 | /* |
141 | * Set Thin Provisioning Enable bit following sbc3r22 in section | |
142 | * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled. | |
143 | */ | |
0fd97ccf | 144 | if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) |
7f7caf6a | 145 | buf[14] |= 0x80; |
1fd032ee | 146 | |
a50da144 | 147 | rbuf = transport_kmap_data_sg(cmd); |
8b4b0dcb NB |
148 | if (rbuf) { |
149 | memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); | |
150 | transport_kunmap_data_sg(cmd); | |
151 | } | |
1fd032ee | 152 | |
2426bd45 | 153 | target_complete_cmd_with_length(cmd, GOOD, 32); |
1fd032ee CH |
154 | return 0; |
155 | } | |
156 | ||
45182ed5 BB |
157 | static sense_reason_t |
158 | sbc_emulate_startstop(struct se_cmd *cmd) | |
159 | { | |
160 | unsigned char *cdb = cmd->t_task_cdb; | |
161 | ||
162 | /* | |
163 | * See sbc3r36 section 5.25 | |
164 | * Immediate bit should be set since there is nothing to complete | |
165 | * POWER CONDITION MODIFIER 0h | |
166 | */ | |
167 | if (!(cdb[1] & 1) || cdb[2] || cdb[3]) | |
168 | return TCM_INVALID_CDB_FIELD; | |
169 | ||
170 | /* | |
171 | * See sbc3r36 section 5.25 | |
172 | * POWER CONDITION 0h START_VALID - process START and LOEJ | |
173 | */ | |
174 | if (cdb[4] >> 4 & 0xf) | |
175 | return TCM_INVALID_CDB_FIELD; | |
176 | ||
177 | /* | |
178 | * See sbc3r36 section 5.25 | |
179 | * LOEJ 0h - nothing to load or unload | |
180 | * START 1h - we are ready | |
181 | */ | |
182 | if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4)) | |
183 | return TCM_INVALID_CDB_FIELD; | |
184 | ||
185 | target_complete_cmd(cmd, SAM_STAT_GOOD); | |
186 | return 0; | |
187 | } | |
188 | ||
972b29c8 | 189 | sector_t sbc_get_write_same_sectors(struct se_cmd *cmd) |
1fd032ee | 190 | { |
1fd032ee | 191 | u32 num_blocks; |
1fd032ee CH |
192 | |
193 | if (cmd->t_task_cdb[0] == WRITE_SAME) | |
194 | num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]); | |
195 | else if (cmd->t_task_cdb[0] == WRITE_SAME_16) | |
196 | num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]); | |
197 | else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */ | |
198 | num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]); | |
199 | ||
200 | /* | |
201 | * Use the explicit range when non zero is supplied, otherwise calculate | |
202 | * the remaining range based on ->get_blocks() - starting LBA. | |
203 | */ | |
6f974e8c CH |
204 | if (num_blocks) |
205 | return num_blocks; | |
1fd032ee | 206 | |
6f974e8c CH |
207 | return cmd->se_dev->transport->get_blocks(cmd->se_dev) - |
208 | cmd->t_task_lba + 1; | |
1fd032ee | 209 | } |
972b29c8 | 210 | EXPORT_SYMBOL(sbc_get_write_same_sectors); |
1fd032ee | 211 | |
b753d643 CH |
212 | static sense_reason_t |
213 | sbc_execute_write_same_unmap(struct se_cmd *cmd) | |
214 | { | |
215 | struct sbc_ops *ops = cmd->protocol_data; | |
216 | sector_t nolb = sbc_get_write_same_sectors(cmd); | |
217 | sense_reason_t ret; | |
218 | ||
219 | if (nolb) { | |
220 | ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb); | |
221 | if (ret) | |
222 | return ret; | |
223 | } | |
224 | ||
225 | target_complete_cmd(cmd, GOOD); | |
226 | return 0; | |
227 | } | |
228 | ||
de103c93 | 229 | static sense_reason_t |
1920ed61 | 230 | sbc_emulate_noop(struct se_cmd *cmd) |
1a1ff38c BK |
231 | { |
232 | target_complete_cmd(cmd, GOOD); | |
233 | return 0; | |
234 | } | |
235 | ||
d6e0175c CH |
236 | static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors) |
237 | { | |
0fd97ccf | 238 | return cmd->se_dev->dev_attrib.block_size * sectors; |
d6e0175c CH |
239 | } |
240 | ||
d6e0175c CH |
241 | static inline u32 transport_get_sectors_6(unsigned char *cdb) |
242 | { | |
243 | /* | |
244 | * Use 8-bit sector value. SBC-3 says: | |
245 | * | |
246 | * A TRANSFER LENGTH field set to zero specifies that 256 | |
247 | * logical blocks shall be written. Any other value | |
248 | * specifies the number of logical blocks that shall be | |
249 | * written. | |
250 | */ | |
251 | return cdb[4] ? : 256; | |
252 | } | |
253 | ||
254 | static inline u32 transport_get_sectors_10(unsigned char *cdb) | |
255 | { | |
256 | return (u32)(cdb[7] << 8) + cdb[8]; | |
257 | } | |
258 | ||
259 | static inline u32 transport_get_sectors_12(unsigned char *cdb) | |
260 | { | |
261 | return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; | |
262 | } | |
263 | ||
264 | static inline u32 transport_get_sectors_16(unsigned char *cdb) | |
265 | { | |
266 | return (u32)(cdb[10] << 24) + (cdb[11] << 16) + | |
267 | (cdb[12] << 8) + cdb[13]; | |
268 | } | |
269 | ||
270 | /* | |
271 | * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants | |
272 | */ | |
273 | static inline u32 transport_get_sectors_32(unsigned char *cdb) | |
274 | { | |
275 | return (u32)(cdb[28] << 24) + (cdb[29] << 16) + | |
276 | (cdb[30] << 8) + cdb[31]; | |
277 | ||
278 | } | |
279 | ||
280 | static inline u32 transport_lba_21(unsigned char *cdb) | |
281 | { | |
282 | return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; | |
283 | } | |
284 | ||
285 | static inline u32 transport_lba_32(unsigned char *cdb) | |
286 | { | |
287 | return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; | |
288 | } | |
289 | ||
290 | static inline unsigned long long transport_lba_64(unsigned char *cdb) | |
291 | { | |
292 | unsigned int __v1, __v2; | |
293 | ||
294 | __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; | |
295 | __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; | |
296 | ||
297 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; | |
298 | } | |
299 | ||
300 | /* | |
301 | * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs | |
302 | */ | |
303 | static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) | |
304 | { | |
305 | unsigned int __v1, __v2; | |
306 | ||
307 | __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15]; | |
308 | __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19]; | |
309 | ||
310 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; | |
311 | } | |
312 | ||
cd063bef NB |
313 | static sense_reason_t |
314 | sbc_setup_write_same(struct se_cmd *cmd, unsigned char *flags, struct sbc_ops *ops) | |
d6e0175c | 315 | { |
8e575c50 NB |
316 | struct se_device *dev = cmd->se_dev; |
317 | sector_t end_lba = dev->transport->get_blocks(dev) + 1; | |
972b29c8 | 318 | unsigned int sectors = sbc_get_write_same_sectors(cmd); |
afd73f1b | 319 | sense_reason_t ret; |
773cbaf7 | 320 | |
d6e0175c CH |
321 | if ((flags[0] & 0x04) || (flags[0] & 0x02)) { |
322 | pr_err("WRITE_SAME PBDATA and LBDATA" | |
323 | " bits not supported for Block Discard" | |
324 | " Emulation\n"); | |
cd063bef | 325 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
d6e0175c | 326 | } |
773cbaf7 NB |
327 | if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) { |
328 | pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n", | |
329 | sectors, cmd->se_dev->dev_attrib.max_write_same_len); | |
330 | return TCM_INVALID_CDB_FIELD; | |
331 | } | |
8e575c50 NB |
332 | /* |
333 | * Sanity check for LBA wrap and request past end of device. | |
334 | */ | |
335 | if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || | |
336 | ((cmd->t_task_lba + sectors) > end_lba)) { | |
337 | pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n", | |
338 | (unsigned long long)end_lba, cmd->t_task_lba, sectors); | |
339 | return TCM_ADDRESS_OUT_OF_RANGE; | |
340 | } | |
341 | ||
5cb770bf RD |
342 | /* We always have ANC_SUP == 0 so setting ANCHOR is always an error */ |
343 | if (flags[0] & 0x10) { | |
344 | pr_warn("WRITE SAME with ANCHOR not supported\n"); | |
345 | return TCM_INVALID_CDB_FIELD; | |
346 | } | |
d6e0175c | 347 | /* |
cd063bef NB |
348 | * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting |
349 | * translated into block discard requests within backend code. | |
d6e0175c | 350 | */ |
cd063bef | 351 | if (flags[0] & 0x08) { |
b753d643 | 352 | if (!ops->execute_unmap) |
cd063bef NB |
353 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
354 | ||
d0a91295 NB |
355 | if (!dev->dev_attrib.emulate_tpws) { |
356 | pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device" | |
357 | " has emulate_tpws disabled\n"); | |
358 | return TCM_UNSUPPORTED_SCSI_OPCODE; | |
359 | } | |
b753d643 | 360 | cmd->execute_cmd = sbc_execute_write_same_unmap; |
cd063bef | 361 | return 0; |
d6e0175c | 362 | } |
cd063bef NB |
363 | if (!ops->execute_write_same) |
364 | return TCM_UNSUPPORTED_SCSI_OPCODE; | |
d6e0175c | 365 | |
afd73f1b NB |
366 | ret = sbc_check_prot(dev, cmd, &cmd->t_task_cdb[0], sectors, true); |
367 | if (ret) | |
368 | return ret; | |
369 | ||
cd063bef | 370 | cmd->execute_cmd = ops->execute_write_same; |
d6e0175c CH |
371 | return 0; |
372 | } | |
373 | ||
c8e63985 | 374 | static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success) |
d6e0175c CH |
375 | { |
376 | unsigned char *buf, *addr; | |
377 | struct scatterlist *sg; | |
378 | unsigned int offset; | |
a6b0133c NB |
379 | sense_reason_t ret = TCM_NO_SENSE; |
380 | int i, count; | |
d6e0175c CH |
381 | /* |
382 | * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command | |
383 | * | |
384 | * 1) read the specified logical block(s); | |
385 | * 2) transfer logical blocks from the data-out buffer; | |
386 | * 3) XOR the logical blocks transferred from the data-out buffer with | |
387 | * the logical blocks read, storing the resulting XOR data in a buffer; | |
388 | * 4) if the DISABLE WRITE bit is set to zero, then write the logical | |
389 | * blocks transferred from the data-out buffer; and | |
390 | * 5) transfer the resulting XOR data to the data-in buffer. | |
391 | */ | |
392 | buf = kmalloc(cmd->data_length, GFP_KERNEL); | |
393 | if (!buf) { | |
394 | pr_err("Unable to allocate xor_callback buf\n"); | |
a6b0133c | 395 | return TCM_OUT_OF_RESOURCES; |
d6e0175c CH |
396 | } |
397 | /* | |
398 | * Copy the scatterlist WRITE buffer located at cmd->t_data_sg | |
399 | * into the locally allocated *buf | |
400 | */ | |
401 | sg_copy_to_buffer(cmd->t_data_sg, | |
402 | cmd->t_data_nents, | |
403 | buf, | |
404 | cmd->data_length); | |
405 | ||
406 | /* | |
407 | * Now perform the XOR against the BIDI read memory located at | |
408 | * cmd->t_mem_bidi_list | |
409 | */ | |
410 | ||
411 | offset = 0; | |
412 | for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { | |
413 | addr = kmap_atomic(sg_page(sg)); | |
a6b0133c NB |
414 | if (!addr) { |
415 | ret = TCM_OUT_OF_RESOURCES; | |
d6e0175c | 416 | goto out; |
a6b0133c | 417 | } |
d6e0175c CH |
418 | |
419 | for (i = 0; i < sg->length; i++) | |
420 | *(addr + sg->offset + i) ^= *(buf + offset + i); | |
421 | ||
422 | offset += sg->length; | |
423 | kunmap_atomic(addr); | |
424 | } | |
425 | ||
426 | out: | |
427 | kfree(buf); | |
a6b0133c | 428 | return ret; |
d6e0175c CH |
429 | } |
430 | ||
a82a9538 NB |
431 | static sense_reason_t |
432 | sbc_execute_rw(struct se_cmd *cmd) | |
433 | { | |
7a971b1b CH |
434 | struct sbc_ops *ops = cmd->protocol_data; |
435 | ||
436 | return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents, | |
a82a9538 NB |
437 | cmd->data_direction); |
438 | } | |
439 | ||
c8e63985 | 440 | static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success) |
68ff9b9b NB |
441 | { |
442 | struct se_device *dev = cmd->se_dev; | |
443 | ||
d8855c15 NB |
444 | /* |
445 | * Only set SCF_COMPARE_AND_WRITE_POST to force a response fall-through | |
446 | * within target_complete_ok_work() if the command was successfully | |
447 | * sent to the backend driver. | |
448 | */ | |
449 | spin_lock_irq(&cmd->t_state_lock); | |
450 | if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) | |
451 | cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST; | |
452 | spin_unlock_irq(&cmd->t_state_lock); | |
453 | ||
68ff9b9b NB |
454 | /* |
455 | * Unlock ->caw_sem originally obtained during sbc_compare_and_write() | |
456 | * before the original READ I/O submission. | |
457 | */ | |
458 | up(&dev->caw_sem); | |
459 | ||
460 | return TCM_NO_SENSE; | |
461 | } | |
462 | ||
c8e63985 | 463 | static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success) |
68ff9b9b NB |
464 | { |
465 | struct se_device *dev = cmd->se_dev; | |
466 | struct scatterlist *write_sg = NULL, *sg; | |
db60df88 | 467 | unsigned char *buf = NULL, *addr; |
68ff9b9b NB |
468 | struct sg_mapping_iter m; |
469 | unsigned int offset = 0, len; | |
470 | unsigned int nlbas = cmd->t_task_nolb; | |
471 | unsigned int block_size = dev->dev_attrib.block_size; | |
472 | unsigned int compare_len = (nlbas * block_size); | |
473 | sense_reason_t ret = TCM_NO_SENSE; | |
474 | int rc, i; | |
475 | ||
cf6d1f09 NB |
476 | /* |
477 | * Handle early failure in transport_generic_request_failure(), | |
c8e63985 | 478 | * which will not have taken ->caw_sem yet.. |
cf6d1f09 | 479 | */ |
c8e63985 | 480 | if (!success && (!cmd->t_data_sg || !cmd->t_bidi_data_sg)) |
cf6d1f09 | 481 | return TCM_NO_SENSE; |
c8e63985 NB |
482 | /* |
483 | * Handle special case for zero-length COMPARE_AND_WRITE | |
484 | */ | |
485 | if (!cmd->data_length) | |
486 | goto out; | |
db60df88 NB |
487 | /* |
488 | * Immediately exit + release dev->caw_sem if command has already | |
489 | * been failed with a non-zero SCSI status. | |
490 | */ | |
491 | if (cmd->scsi_status) { | |
492 | pr_err("compare_and_write_callback: non zero scsi_status:" | |
493 | " 0x%02x\n", cmd->scsi_status); | |
494 | goto out; | |
495 | } | |
cf6d1f09 | 496 | |
68ff9b9b NB |
497 | buf = kzalloc(cmd->data_length, GFP_KERNEL); |
498 | if (!buf) { | |
499 | pr_err("Unable to allocate compare_and_write buf\n"); | |
a2890087 NB |
500 | ret = TCM_OUT_OF_RESOURCES; |
501 | goto out; | |
68ff9b9b NB |
502 | } |
503 | ||
a1e1774c | 504 | write_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents, |
68ff9b9b NB |
505 | GFP_KERNEL); |
506 | if (!write_sg) { | |
507 | pr_err("Unable to allocate compare_and_write sg\n"); | |
508 | ret = TCM_OUT_OF_RESOURCES; | |
509 | goto out; | |
510 | } | |
a1e1774c | 511 | sg_init_table(write_sg, cmd->t_data_nents); |
68ff9b9b NB |
512 | /* |
513 | * Setup verify and write data payloads from total NumberLBAs. | |
514 | */ | |
515 | rc = sg_copy_to_buffer(cmd->t_data_sg, cmd->t_data_nents, buf, | |
516 | cmd->data_length); | |
517 | if (!rc) { | |
518 | pr_err("sg_copy_to_buffer() failed for compare_and_write\n"); | |
519 | ret = TCM_OUT_OF_RESOURCES; | |
520 | goto out; | |
521 | } | |
522 | /* | |
523 | * Compare against SCSI READ payload against verify payload | |
524 | */ | |
525 | for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, i) { | |
526 | addr = (unsigned char *)kmap_atomic(sg_page(sg)); | |
527 | if (!addr) { | |
528 | ret = TCM_OUT_OF_RESOURCES; | |
529 | goto out; | |
530 | } | |
531 | ||
532 | len = min(sg->length, compare_len); | |
533 | ||
534 | if (memcmp(addr, buf + offset, len)) { | |
535 | pr_warn("Detected MISCOMPARE for addr: %p buf: %p\n", | |
536 | addr, buf + offset); | |
537 | kunmap_atomic(addr); | |
538 | goto miscompare; | |
539 | } | |
540 | kunmap_atomic(addr); | |
541 | ||
542 | offset += len; | |
543 | compare_len -= len; | |
544 | if (!compare_len) | |
545 | break; | |
546 | } | |
547 | ||
548 | i = 0; | |
549 | len = cmd->t_task_nolb * block_size; | |
550 | sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG); | |
551 | /* | |
552 | * Currently assumes NoLB=1 and SGLs are PAGE_SIZE.. | |
553 | */ | |
554 | while (len) { | |
555 | sg_miter_next(&m); | |
556 | ||
557 | if (block_size < PAGE_SIZE) { | |
558 | sg_set_page(&write_sg[i], m.page, block_size, | |
559 | block_size); | |
560 | } else { | |
561 | sg_miter_next(&m); | |
562 | sg_set_page(&write_sg[i], m.page, block_size, | |
563 | 0); | |
564 | } | |
565 | len -= block_size; | |
566 | i++; | |
567 | } | |
568 | sg_miter_stop(&m); | |
569 | /* | |
570 | * Save the original SGL + nents values before updating to new | |
571 | * assignments, to be released in transport_free_pages() -> | |
572 | * transport_reset_sgl_orig() | |
573 | */ | |
574 | cmd->t_data_sg_orig = cmd->t_data_sg; | |
575 | cmd->t_data_sg = write_sg; | |
576 | cmd->t_data_nents_orig = cmd->t_data_nents; | |
577 | cmd->t_data_nents = 1; | |
578 | ||
68d81f40 | 579 | cmd->sam_task_attr = TCM_HEAD_TAG; |
68ff9b9b NB |
580 | cmd->transport_complete_callback = compare_and_write_post; |
581 | /* | |
582 | * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler | |
583 | * for submitting the adjusted SGL to write instance user-data. | |
584 | */ | |
585 | cmd->execute_cmd = sbc_execute_rw; | |
586 | ||
587 | spin_lock_irq(&cmd->t_state_lock); | |
588 | cmd->t_state = TRANSPORT_PROCESSING; | |
589 | cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT; | |
590 | spin_unlock_irq(&cmd->t_state_lock); | |
591 | ||
592 | __target_execute_cmd(cmd); | |
593 | ||
594 | kfree(buf); | |
595 | return ret; | |
596 | ||
597 | miscompare: | |
598 | pr_warn("Target/%s: Send MISCOMPARE check condition and sense\n", | |
599 | dev->transport->name); | |
600 | ret = TCM_MISCOMPARE_VERIFY; | |
601 | out: | |
602 | /* | |
603 | * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in | |
604 | * sbc_compare_and_write() before the original READ I/O submission. | |
605 | */ | |
606 | up(&dev->caw_sem); | |
607 | kfree(write_sg); | |
608 | kfree(buf); | |
609 | return ret; | |
610 | } | |
611 | ||
612 | static sense_reason_t | |
613 | sbc_compare_and_write(struct se_cmd *cmd) | |
614 | { | |
7a971b1b | 615 | struct sbc_ops *ops = cmd->protocol_data; |
68ff9b9b NB |
616 | struct se_device *dev = cmd->se_dev; |
617 | sense_reason_t ret; | |
618 | int rc; | |
619 | /* | |
620 | * Submit the READ first for COMPARE_AND_WRITE to perform the | |
621 | * comparision using SGLs at cmd->t_bidi_data_sg.. | |
622 | */ | |
623 | rc = down_interruptible(&dev->caw_sem); | |
ee7619f2 | 624 | if (rc != 0) { |
68ff9b9b NB |
625 | cmd->transport_complete_callback = NULL; |
626 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
627 | } | |
b7191253 NB |
628 | /* |
629 | * Reset cmd->data_length to individual block_size in order to not | |
630 | * confuse backend drivers that depend on this value matching the | |
631 | * size of the I/O being submitted. | |
632 | */ | |
633 | cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size; | |
68ff9b9b | 634 | |
7a971b1b | 635 | ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents, |
68ff9b9b NB |
636 | DMA_FROM_DEVICE); |
637 | if (ret) { | |
638 | cmd->transport_complete_callback = NULL; | |
639 | up(&dev->caw_sem); | |
640 | return ret; | |
641 | } | |
642 | /* | |
643 | * Unlock of dev->caw_sem to occur in compare_and_write_callback() | |
644 | * upon MISCOMPARE, or in compare_and_write_done() upon completion | |
645 | * of WRITE instance user-data. | |
646 | */ | |
647 | return TCM_NO_SENSE; | |
648 | } | |
649 | ||
19f9361a | 650 | static int |
38b57f82 | 651 | sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type, |
19f9361a SG |
652 | bool is_write, struct se_cmd *cmd) |
653 | { | |
654 | if (is_write) { | |
38b57f82 NB |
655 | cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP : |
656 | protect ? TARGET_PROT_DOUT_PASS : | |
657 | TARGET_PROT_DOUT_INSERT; | |
19f9361a SG |
658 | switch (protect) { |
659 | case 0x0: | |
660 | case 0x3: | |
661 | cmd->prot_checks = 0; | |
662 | break; | |
663 | case 0x1: | |
664 | case 0x5: | |
665 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; | |
666 | if (prot_type == TARGET_DIF_TYPE1_PROT) | |
667 | cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; | |
668 | break; | |
669 | case 0x2: | |
670 | if (prot_type == TARGET_DIF_TYPE1_PROT) | |
671 | cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; | |
672 | break; | |
673 | case 0x4: | |
674 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; | |
675 | break; | |
676 | default: | |
677 | pr_err("Unsupported protect field %d\n", protect); | |
678 | return -EINVAL; | |
679 | } | |
680 | } else { | |
38b57f82 NB |
681 | cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT : |
682 | protect ? TARGET_PROT_DIN_PASS : | |
683 | TARGET_PROT_DIN_STRIP; | |
19f9361a SG |
684 | switch (protect) { |
685 | case 0x0: | |
686 | case 0x1: | |
687 | case 0x5: | |
688 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; | |
689 | if (prot_type == TARGET_DIF_TYPE1_PROT) | |
690 | cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; | |
691 | break; | |
692 | case 0x2: | |
693 | if (prot_type == TARGET_DIF_TYPE1_PROT) | |
694 | cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; | |
695 | break; | |
696 | case 0x3: | |
697 | cmd->prot_checks = 0; | |
698 | break; | |
699 | case 0x4: | |
700 | cmd->prot_checks = TARGET_DIF_CHECK_GUARD; | |
701 | break; | |
702 | default: | |
703 | pr_err("Unsupported protect field %d\n", protect); | |
704 | return -EINVAL; | |
705 | } | |
706 | } | |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
f7b7c06f | 711 | static sense_reason_t |
499bf77b | 712 | sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb, |
19f9361a | 713 | u32 sectors, bool is_write) |
499bf77b | 714 | { |
19f9361a | 715 | u8 protect = cdb[1] >> 5; |
38b57f82 NB |
716 | int sp_ops = cmd->se_sess->sup_prot_ops; |
717 | int pi_prot_type = dev->dev_attrib.pi_prot_type; | |
718 | bool fabric_prot = false; | |
19f9361a | 719 | |
f7b7c06f | 720 | if (!cmd->t_prot_sg || !cmd->t_prot_nents) { |
38b57f82 NB |
721 | if (unlikely(protect && |
722 | !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) { | |
723 | pr_err("CDB contains protect bit, but device + fabric does" | |
724 | " not advertise PROTECT=1 feature bit\n"); | |
f7b7c06f NB |
725 | return TCM_INVALID_CDB_FIELD; |
726 | } | |
727 | if (cmd->prot_pto) | |
728 | return TCM_NO_SENSE; | |
729 | } | |
499bf77b NB |
730 | |
731 | switch (dev->dev_attrib.pi_prot_type) { | |
732 | case TARGET_DIF_TYPE3_PROT: | |
499bf77b NB |
733 | cmd->reftag_seed = 0xffffffff; |
734 | break; | |
735 | case TARGET_DIF_TYPE2_PROT: | |
19f9361a | 736 | if (protect) |
f7b7c06f | 737 | return TCM_INVALID_CDB_FIELD; |
499bf77b NB |
738 | |
739 | cmd->reftag_seed = cmd->t_task_lba; | |
740 | break; | |
741 | case TARGET_DIF_TYPE1_PROT: | |
499bf77b NB |
742 | cmd->reftag_seed = cmd->t_task_lba; |
743 | break; | |
744 | case TARGET_DIF_TYPE0_PROT: | |
38b57f82 NB |
745 | /* |
746 | * See if the fabric supports T10-PI, and the session has been | |
747 | * configured to allow export PROTECT=1 feature bit with backend | |
748 | * devices that don't support T10-PI. | |
749 | */ | |
750 | fabric_prot = is_write ? | |
751 | !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) : | |
752 | !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT)); | |
753 | ||
754 | if (fabric_prot && cmd->se_sess->sess_prot_type) { | |
755 | pi_prot_type = cmd->se_sess->sess_prot_type; | |
756 | break; | |
757 | } | |
cceca4a6 NB |
758 | if (!protect) |
759 | return TCM_NO_SENSE; | |
38b57f82 | 760 | /* Fallthrough */ |
499bf77b | 761 | default: |
cceca4a6 NB |
762 | pr_err("Unable to determine pi_prot_type for CDB: 0x%02x " |
763 | "PROTECT: 0x%02x\n", cdb[0], protect); | |
764 | return TCM_INVALID_CDB_FIELD; | |
499bf77b NB |
765 | } |
766 | ||
38b57f82 | 767 | if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd)) |
f7b7c06f | 768 | return TCM_INVALID_CDB_FIELD; |
19f9361a | 769 | |
38b57f82 | 770 | cmd->prot_type = pi_prot_type; |
499bf77b | 771 | cmd->prot_length = dev->prot_length * sectors; |
e2a4f55c SG |
772 | |
773 | /** | |
774 | * In case protection information exists over the wire | |
775 | * we modify command data length to describe pure data. | |
776 | * The actual transfer length is data length + protection | |
777 | * length | |
778 | **/ | |
779 | if (protect) | |
780 | cmd->data_length = sectors * dev->dev_attrib.block_size; | |
781 | ||
782 | pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d " | |
783 | "prot_op=%d prot_checks=%d\n", | |
784 | __func__, cmd->prot_type, cmd->data_length, cmd->prot_length, | |
03abad9e | 785 | cmd->prot_op, cmd->prot_checks); |
499bf77b | 786 | |
f7b7c06f | 787 | return TCM_NO_SENSE; |
499bf77b NB |
788 | } |
789 | ||
fde9f50f NB |
790 | static int |
791 | sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb) | |
792 | { | |
793 | if (cdb[1] & 0x10) { | |
814e5b45 CH |
794 | /* see explanation in spc_emulate_modesense */ |
795 | if (!target_check_fua(dev)) { | |
fde9f50f NB |
796 | pr_err("Got CDB: 0x%02x with DPO bit set, but device" |
797 | " does not advertise support for DPO\n", cdb[0]); | |
798 | return -EINVAL; | |
799 | } | |
800 | } | |
801 | if (cdb[1] & 0x8) { | |
814e5b45 | 802 | if (!target_check_fua(dev)) { |
fde9f50f NB |
803 | pr_err("Got CDB: 0x%02x with FUA bit set, but device" |
804 | " does not advertise support for FUA write\n", | |
805 | cdb[0]); | |
806 | return -EINVAL; | |
807 | } | |
808 | cmd->se_cmd_flags |= SCF_FUA; | |
809 | } | |
810 | return 0; | |
499bf77b NB |
811 | } |
812 | ||
de103c93 CH |
813 | sense_reason_t |
814 | sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops) | |
d6e0175c | 815 | { |
d6e0175c CH |
816 | struct se_device *dev = cmd->se_dev; |
817 | unsigned char *cdb = cmd->t_task_cdb; | |
1fd032ee | 818 | unsigned int size; |
d6e0175c | 819 | u32 sectors = 0; |
de103c93 | 820 | sense_reason_t ret; |
d6e0175c | 821 | |
7a971b1b CH |
822 | cmd->protocol_data = ops; |
823 | ||
d6e0175c CH |
824 | switch (cdb[0]) { |
825 | case READ_6: | |
826 | sectors = transport_get_sectors_6(cdb); | |
827 | cmd->t_task_lba = transport_lba_21(cdb); | |
828 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | |
a82a9538 | 829 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
830 | break; |
831 | case READ_10: | |
832 | sectors = transport_get_sectors_10(cdb); | |
833 | cmd->t_task_lba = transport_lba_32(cdb); | |
499bf77b | 834 | |
fde9f50f NB |
835 | if (sbc_check_dpofua(dev, cmd, cdb)) |
836 | return TCM_INVALID_CDB_FIELD; | |
837 | ||
f7b7c06f NB |
838 | ret = sbc_check_prot(dev, cmd, cdb, sectors, false); |
839 | if (ret) | |
840 | return ret; | |
499bf77b | 841 | |
d6e0175c | 842 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
a82a9538 | 843 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
844 | break; |
845 | case READ_12: | |
846 | sectors = transport_get_sectors_12(cdb); | |
847 | cmd->t_task_lba = transport_lba_32(cdb); | |
499bf77b | 848 | |
fde9f50f NB |
849 | if (sbc_check_dpofua(dev, cmd, cdb)) |
850 | return TCM_INVALID_CDB_FIELD; | |
851 | ||
f7b7c06f NB |
852 | ret = sbc_check_prot(dev, cmd, cdb, sectors, false); |
853 | if (ret) | |
854 | return ret; | |
499bf77b | 855 | |
d6e0175c | 856 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
a82a9538 | 857 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
858 | break; |
859 | case READ_16: | |
860 | sectors = transport_get_sectors_16(cdb); | |
861 | cmd->t_task_lba = transport_lba_64(cdb); | |
499bf77b | 862 | |
fde9f50f NB |
863 | if (sbc_check_dpofua(dev, cmd, cdb)) |
864 | return TCM_INVALID_CDB_FIELD; | |
865 | ||
f7b7c06f NB |
866 | ret = sbc_check_prot(dev, cmd, cdb, sectors, false); |
867 | if (ret) | |
868 | return ret; | |
499bf77b | 869 | |
d6e0175c | 870 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
a82a9538 | 871 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
872 | break; |
873 | case WRITE_6: | |
874 | sectors = transport_get_sectors_6(cdb); | |
875 | cmd->t_task_lba = transport_lba_21(cdb); | |
876 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | |
a82a9538 | 877 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
878 | break; |
879 | case WRITE_10: | |
880 | case WRITE_VERIFY: | |
881 | sectors = transport_get_sectors_10(cdb); | |
882 | cmd->t_task_lba = transport_lba_32(cdb); | |
499bf77b | 883 | |
fde9f50f NB |
884 | if (sbc_check_dpofua(dev, cmd, cdb)) |
885 | return TCM_INVALID_CDB_FIELD; | |
886 | ||
f7b7c06f NB |
887 | ret = sbc_check_prot(dev, cmd, cdb, sectors, true); |
888 | if (ret) | |
889 | return ret; | |
499bf77b | 890 | |
d6e0175c | 891 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
a82a9538 | 892 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
893 | break; |
894 | case WRITE_12: | |
895 | sectors = transport_get_sectors_12(cdb); | |
896 | cmd->t_task_lba = transport_lba_32(cdb); | |
499bf77b | 897 | |
fde9f50f NB |
898 | if (sbc_check_dpofua(dev, cmd, cdb)) |
899 | return TCM_INVALID_CDB_FIELD; | |
900 | ||
f7b7c06f NB |
901 | ret = sbc_check_prot(dev, cmd, cdb, sectors, true); |
902 | if (ret) | |
903 | return ret; | |
499bf77b | 904 | |
d6e0175c | 905 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
a82a9538 | 906 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
907 | break; |
908 | case WRITE_16: | |
909 | sectors = transport_get_sectors_16(cdb); | |
910 | cmd->t_task_lba = transport_lba_64(cdb); | |
499bf77b | 911 | |
fde9f50f NB |
912 | if (sbc_check_dpofua(dev, cmd, cdb)) |
913 | return TCM_INVALID_CDB_FIELD; | |
914 | ||
f7b7c06f NB |
915 | ret = sbc_check_prot(dev, cmd, cdb, sectors, true); |
916 | if (ret) | |
917 | return ret; | |
499bf77b | 918 | |
d6e0175c | 919 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
a82a9538 | 920 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c CH |
921 | break; |
922 | case XDWRITEREAD_10: | |
de103c93 | 923 | if (cmd->data_direction != DMA_TO_DEVICE || |
d6e0175c | 924 | !(cmd->se_cmd_flags & SCF_BIDI)) |
de103c93 | 925 | return TCM_INVALID_CDB_FIELD; |
d6e0175c CH |
926 | sectors = transport_get_sectors_10(cdb); |
927 | ||
fde9f50f NB |
928 | if (sbc_check_dpofua(dev, cmd, cdb)) |
929 | return TCM_INVALID_CDB_FIELD; | |
930 | ||
d6e0175c CH |
931 | cmd->t_task_lba = transport_lba_32(cdb); |
932 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | |
933 | ||
934 | /* | |
935 | * Setup BIDI XOR callback to be run after I/O completion. | |
936 | */ | |
a82a9538 | 937 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c | 938 | cmd->transport_complete_callback = &xdreadwrite_callback; |
d6e0175c CH |
939 | break; |
940 | case VARIABLE_LENGTH_CMD: | |
941 | { | |
942 | u16 service_action = get_unaligned_be16(&cdb[8]); | |
943 | switch (service_action) { | |
944 | case XDWRITEREAD_32: | |
945 | sectors = transport_get_sectors_32(cdb); | |
946 | ||
fde9f50f NB |
947 | if (sbc_check_dpofua(dev, cmd, cdb)) |
948 | return TCM_INVALID_CDB_FIELD; | |
d6e0175c CH |
949 | /* |
950 | * Use WRITE_32 and READ_32 opcodes for the emulated | |
951 | * XDWRITE_READ_32 logic. | |
952 | */ | |
953 | cmd->t_task_lba = transport_lba_64_ext(cdb); | |
954 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | |
955 | ||
956 | /* | |
957 | * Setup BIDI XOR callback to be run during after I/O | |
958 | * completion. | |
959 | */ | |
a82a9538 | 960 | cmd->execute_cmd = sbc_execute_rw; |
d6e0175c | 961 | cmd->transport_complete_callback = &xdreadwrite_callback; |
d6e0175c CH |
962 | break; |
963 | case WRITE_SAME_32: | |
964 | sectors = transport_get_sectors_32(cdb); | |
965 | if (!sectors) { | |
966 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" | |
967 | " supported\n"); | |
de103c93 | 968 | return TCM_INVALID_CDB_FIELD; |
d6e0175c CH |
969 | } |
970 | ||
1fd032ee | 971 | size = sbc_get_size(cmd, 1); |
d6e0175c CH |
972 | cmd->t_task_lba = get_unaligned_be64(&cdb[12]); |
973 | ||
cd063bef | 974 | ret = sbc_setup_write_same(cmd, &cdb[10], ops); |
6b64e1fe | 975 | if (ret) |
cd063bef | 976 | return ret; |
d6e0175c CH |
977 | break; |
978 | default: | |
979 | pr_err("VARIABLE_LENGTH_CMD service action" | |
980 | " 0x%04x not supported\n", service_action); | |
de103c93 | 981 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
d6e0175c CH |
982 | } |
983 | break; | |
984 | } | |
68ff9b9b NB |
985 | case COMPARE_AND_WRITE: |
986 | sectors = cdb[13]; | |
987 | /* | |
988 | * Currently enforce COMPARE_AND_WRITE for a single sector | |
989 | */ | |
990 | if (sectors > 1) { | |
991 | pr_err("COMPARE_AND_WRITE contains NoLB: %u greater" | |
992 | " than 1\n", sectors); | |
993 | return TCM_INVALID_CDB_FIELD; | |
994 | } | |
ab81a5e0 DD |
995 | if (sbc_check_dpofua(dev, cmd, cdb)) |
996 | return TCM_INVALID_CDB_FIELD; | |
997 | ||
68ff9b9b NB |
998 | /* |
999 | * Double size because we have two buffers, note that | |
1000 | * zero is not an error.. | |
1001 | */ | |
1002 | size = 2 * sbc_get_size(cmd, sectors); | |
1003 | cmd->t_task_lba = get_unaligned_be64(&cdb[2]); | |
1004 | cmd->t_task_nolb = sectors; | |
1005 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE; | |
68ff9b9b NB |
1006 | cmd->execute_cmd = sbc_compare_and_write; |
1007 | cmd->transport_complete_callback = compare_and_write_callback; | |
1008 | break; | |
d6e0175c | 1009 | case READ_CAPACITY: |
1fd032ee CH |
1010 | size = READ_CAP_LEN; |
1011 | cmd->execute_cmd = sbc_emulate_readcapacity; | |
d6e0175c | 1012 | break; |
eb846d9f | 1013 | case SERVICE_ACTION_IN_16: |
d6e0175c CH |
1014 | switch (cmd->t_task_cdb[1] & 0x1f) { |
1015 | case SAI_READ_CAPACITY_16: | |
1fd032ee | 1016 | cmd->execute_cmd = sbc_emulate_readcapacity_16; |
d6e0175c | 1017 | break; |
c66094bf HR |
1018 | case SAI_REPORT_REFERRALS: |
1019 | cmd->execute_cmd = target_emulate_report_referrals; | |
1020 | break; | |
d6e0175c CH |
1021 | default: |
1022 | pr_err("Unsupported SA: 0x%02x\n", | |
1023 | cmd->t_task_cdb[1] & 0x1f); | |
de103c93 | 1024 | return TCM_INVALID_CDB_FIELD; |
d6e0175c | 1025 | } |
1fd032ee | 1026 | size = (cdb[10] << 24) | (cdb[11] << 16) | |
d6e0175c CH |
1027 | (cdb[12] << 8) | cdb[13]; |
1028 | break; | |
1029 | case SYNCHRONIZE_CACHE: | |
1030 | case SYNCHRONIZE_CACHE_16: | |
d6e0175c CH |
1031 | if (cdb[0] == SYNCHRONIZE_CACHE) { |
1032 | sectors = transport_get_sectors_10(cdb); | |
1033 | cmd->t_task_lba = transport_lba_32(cdb); | |
1034 | } else { | |
1035 | sectors = transport_get_sectors_16(cdb); | |
1036 | cmd->t_task_lba = transport_lba_64(cdb); | |
1037 | } | |
6ef31dc7 CVB |
1038 | if (ops->execute_sync_cache) { |
1039 | cmd->execute_cmd = ops->execute_sync_cache; | |
1040 | goto check_lba; | |
d6e0175c | 1041 | } |
6ef31dc7 CVB |
1042 | size = 0; |
1043 | cmd->execute_cmd = sbc_emulate_noop; | |
d6e0175c CH |
1044 | break; |
1045 | case UNMAP: | |
14150a6b | 1046 | if (!ops->execute_unmap) |
de103c93 | 1047 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
14150a6b | 1048 | |
61fdb4ac NB |
1049 | if (!dev->dev_attrib.emulate_tpu) { |
1050 | pr_err("Got UNMAP, but backend device has" | |
1051 | " emulate_tpu disabled\n"); | |
1052 | return TCM_UNSUPPORTED_SCSI_OPCODE; | |
1053 | } | |
1fd032ee | 1054 | size = get_unaligned_be16(&cdb[7]); |
62e46942 | 1055 | cmd->execute_cmd = sbc_execute_unmap; |
d6e0175c CH |
1056 | break; |
1057 | case WRITE_SAME_16: | |
1058 | sectors = transport_get_sectors_16(cdb); | |
1059 | if (!sectors) { | |
1060 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); | |
de103c93 | 1061 | return TCM_INVALID_CDB_FIELD; |
d6e0175c CH |
1062 | } |
1063 | ||
1fd032ee | 1064 | size = sbc_get_size(cmd, 1); |
d6e0175c CH |
1065 | cmd->t_task_lba = get_unaligned_be64(&cdb[2]); |
1066 | ||
cd063bef | 1067 | ret = sbc_setup_write_same(cmd, &cdb[1], ops); |
6b64e1fe | 1068 | if (ret) |
cd063bef | 1069 | return ret; |
d6e0175c CH |
1070 | break; |
1071 | case WRITE_SAME: | |
1072 | sectors = transport_get_sectors_10(cdb); | |
1073 | if (!sectors) { | |
1074 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); | |
de103c93 | 1075 | return TCM_INVALID_CDB_FIELD; |
d6e0175c CH |
1076 | } |
1077 | ||
1fd032ee | 1078 | size = sbc_get_size(cmd, 1); |
d6e0175c CH |
1079 | cmd->t_task_lba = get_unaligned_be32(&cdb[2]); |
1080 | ||
1081 | /* | |
1082 | * Follow sbcr26 with WRITE_SAME (10) and check for the existence | |
1083 | * of byte 1 bit 3 UNMAP instead of original reserved field | |
1084 | */ | |
cd063bef | 1085 | ret = sbc_setup_write_same(cmd, &cdb[1], ops); |
6b64e1fe | 1086 | if (ret) |
cd063bef | 1087 | return ret; |
d6e0175c CH |
1088 | break; |
1089 | case VERIFY: | |
1fd032ee | 1090 | size = 0; |
c52716de CVB |
1091 | sectors = transport_get_sectors_10(cdb); |
1092 | cmd->t_task_lba = transport_lba_32(cdb); | |
1920ed61 | 1093 | cmd->execute_cmd = sbc_emulate_noop; |
c52716de | 1094 | goto check_lba; |
1a1ff38c BK |
1095 | case REZERO_UNIT: |
1096 | case SEEK_6: | |
1097 | case SEEK_10: | |
1098 | /* | |
1099 | * There are still clients out there which use these old SCSI-2 | |
1100 | * commands. This mainly happens when running VMs with legacy | |
1101 | * guest systems, connected via SCSI command pass-through to | |
1102 | * iSCSI targets. Make them happy and return status GOOD. | |
1103 | */ | |
1104 | size = 0; | |
1105 | cmd->execute_cmd = sbc_emulate_noop; | |
1106 | break; | |
45182ed5 BB |
1107 | case START_STOP: |
1108 | size = 0; | |
1109 | cmd->execute_cmd = sbc_emulate_startstop; | |
1110 | break; | |
d6e0175c | 1111 | default: |
1fd032ee | 1112 | ret = spc_parse_cdb(cmd, &size); |
d6e0175c CH |
1113 | if (ret) |
1114 | return ret; | |
1115 | } | |
1116 | ||
1117 | /* reject any command that we don't have a handler for */ | |
20959c4b | 1118 | if (!cmd->execute_cmd) |
de103c93 | 1119 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
d6e0175c CH |
1120 | |
1121 | if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { | |
1fd032ee | 1122 | unsigned long long end_lba; |
6ef31dc7 | 1123 | check_lba: |
1fd032ee | 1124 | end_lba = dev->transport->get_blocks(dev) + 1; |
aa179935 NB |
1125 | if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || |
1126 | ((cmd->t_task_lba + sectors) > end_lba)) { | |
1fd032ee CH |
1127 | pr_err("cmd exceeds last lba %llu " |
1128 | "(lba %llu, sectors %u)\n", | |
1129 | end_lba, cmd->t_task_lba, sectors); | |
09ceadc7 | 1130 | return TCM_ADDRESS_OUT_OF_RANGE; |
1fd032ee CH |
1131 | } |
1132 | ||
68ff9b9b NB |
1133 | if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) |
1134 | size = sbc_get_size(cmd, sectors); | |
d6e0175c CH |
1135 | } |
1136 | ||
de103c93 | 1137 | return target_cmd_size_check(cmd, size); |
d6e0175c CH |
1138 | } |
1139 | EXPORT_SYMBOL(sbc_parse_cdb); | |
6f23ac8a | 1140 | |
6f23ac8a CH |
1141 | u32 sbc_get_device_type(struct se_device *dev) |
1142 | { | |
1143 | return TYPE_DISK; | |
1144 | } | |
1145 | EXPORT_SYMBOL(sbc_get_device_type); | |
86d71829 | 1146 | |
62e46942 CH |
1147 | static sense_reason_t |
1148 | sbc_execute_unmap(struct se_cmd *cmd) | |
86d71829 | 1149 | { |
62e46942 | 1150 | struct sbc_ops *ops = cmd->protocol_data; |
86d71829 AH |
1151 | struct se_device *dev = cmd->se_dev; |
1152 | unsigned char *buf, *ptr = NULL; | |
1153 | sector_t lba; | |
1154 | int size; | |
1155 | u32 range; | |
1156 | sense_reason_t ret = 0; | |
1157 | int dl, bd_dl; | |
1158 | ||
1159 | /* We never set ANC_SUP */ | |
1160 | if (cmd->t_task_cdb[1]) | |
1161 | return TCM_INVALID_CDB_FIELD; | |
1162 | ||
1163 | if (cmd->data_length == 0) { | |
1164 | target_complete_cmd(cmd, SAM_STAT_GOOD); | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | if (cmd->data_length < 8) { | |
1169 | pr_warn("UNMAP parameter list length %u too small\n", | |
1170 | cmd->data_length); | |
1171 | return TCM_PARAMETER_LIST_LENGTH_ERROR; | |
1172 | } | |
1173 | ||
1174 | buf = transport_kmap_data_sg(cmd); | |
1175 | if (!buf) | |
1176 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
1177 | ||
1178 | dl = get_unaligned_be16(&buf[0]); | |
1179 | bd_dl = get_unaligned_be16(&buf[2]); | |
1180 | ||
1181 | size = cmd->data_length - 8; | |
1182 | if (bd_dl > size) | |
1183 | pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n", | |
1184 | cmd->data_length, bd_dl); | |
1185 | else | |
1186 | size = bd_dl; | |
1187 | ||
1188 | if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) { | |
1189 | ret = TCM_INVALID_PARAMETER_LIST; | |
1190 | goto err; | |
1191 | } | |
1192 | ||
1193 | /* First UNMAP block descriptor starts at 8 byte offset */ | |
1194 | ptr = &buf[8]; | |
1195 | pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u" | |
1196 | " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); | |
1197 | ||
1198 | while (size >= 16) { | |
1199 | lba = get_unaligned_be64(&ptr[0]); | |
1200 | range = get_unaligned_be32(&ptr[8]); | |
1201 | pr_debug("UNMAP: Using lba: %llu and range: %u\n", | |
1202 | (unsigned long long)lba, range); | |
1203 | ||
1204 | if (range > dev->dev_attrib.max_unmap_lba_count) { | |
1205 | ret = TCM_INVALID_PARAMETER_LIST; | |
1206 | goto err; | |
1207 | } | |
1208 | ||
1209 | if (lba + range > dev->transport->get_blocks(dev) + 1) { | |
1210 | ret = TCM_ADDRESS_OUT_OF_RANGE; | |
1211 | goto err; | |
1212 | } | |
1213 | ||
62e46942 | 1214 | ret = ops->execute_unmap(cmd, lba, range); |
86d71829 AH |
1215 | if (ret) |
1216 | goto err; | |
1217 | ||
1218 | ptr += 16; | |
1219 | size -= 16; | |
1220 | } | |
1221 | ||
1222 | err: | |
1223 | transport_kunmap_data_sg(cmd); | |
1224 | if (!ret) | |
1225 | target_complete_cmd(cmd, GOOD); | |
1226 | return ret; | |
1227 | } | |
41861fa8 | 1228 | |
66a3d5bc NB |
1229 | void |
1230 | sbc_dif_generate(struct se_cmd *cmd) | |
1231 | { | |
1232 | struct se_device *dev = cmd->se_dev; | |
fe052a18 | 1233 | struct t10_pi_tuple *sdt; |
18213afb | 1234 | struct scatterlist *dsg = cmd->t_data_sg, *psg; |
66a3d5bc NB |
1235 | sector_t sector = cmd->t_task_lba; |
1236 | void *daddr, *paddr; | |
1237 | int i, j, offset = 0; | |
18213afb | 1238 | unsigned int block_size = dev->dev_attrib.block_size; |
66a3d5bc | 1239 | |
18213afb | 1240 | for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { |
66a3d5bc | 1241 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
18213afb | 1242 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; |
66a3d5bc | 1243 | |
18213afb | 1244 | for (j = 0; j < psg->length; |
fe052a18 | 1245 | j += sizeof(*sdt)) { |
18213afb AM |
1246 | __u16 crc; |
1247 | unsigned int avail; | |
66a3d5bc | 1248 | |
18213afb AM |
1249 | if (offset >= dsg->length) { |
1250 | offset -= dsg->length; | |
1251 | kunmap_atomic(daddr - dsg->offset); | |
1252 | dsg = sg_next(dsg); | |
1253 | if (!dsg) { | |
1254 | kunmap_atomic(paddr - psg->offset); | |
1255 | return; | |
1256 | } | |
1257 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; | |
66a3d5bc | 1258 | } |
66a3d5bc | 1259 | |
18213afb AM |
1260 | sdt = paddr + j; |
1261 | avail = min(block_size, dsg->length - offset); | |
1262 | crc = crc_t10dif(daddr + offset, avail); | |
1263 | if (avail < block_size) { | |
1264 | kunmap_atomic(daddr - dsg->offset); | |
1265 | dsg = sg_next(dsg); | |
1266 | if (!dsg) { | |
1267 | kunmap_atomic(paddr - psg->offset); | |
1268 | return; | |
1269 | } | |
1270 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; | |
1271 | offset = block_size - avail; | |
1272 | crc = crc_t10dif_update(crc, daddr, offset); | |
1273 | } else { | |
1274 | offset += block_size; | |
66a3d5bc NB |
1275 | } |
1276 | ||
18213afb | 1277 | sdt->guard_tag = cpu_to_be16(crc); |
823ddd87 | 1278 | if (cmd->prot_type == TARGET_DIF_TYPE1_PROT) |
66a3d5bc NB |
1279 | sdt->ref_tag = cpu_to_be32(sector & 0xffffffff); |
1280 | sdt->app_tag = 0; | |
1281 | ||
6ae50408 | 1282 | pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x" |
66a3d5bc | 1283 | " app_tag: 0x%04x ref_tag: %u\n", |
6ae50408 NB |
1284 | (cmd->data_direction == DMA_TO_DEVICE) ? |
1285 | "WRITE" : "READ", (unsigned long long)sector, | |
1286 | sdt->guard_tag, sdt->app_tag, | |
1287 | be32_to_cpu(sdt->ref_tag)); | |
66a3d5bc NB |
1288 | |
1289 | sector++; | |
66a3d5bc NB |
1290 | } |
1291 | ||
18213afb AM |
1292 | kunmap_atomic(daddr - dsg->offset); |
1293 | kunmap_atomic(paddr - psg->offset); | |
66a3d5bc NB |
1294 | } |
1295 | } | |
1296 | ||
41861fa8 | 1297 | static sense_reason_t |
fe052a18 | 1298 | sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt, |
18213afb | 1299 | __u16 crc, sector_t sector, unsigned int ei_lba) |
41861fa8 | 1300 | { |
41861fa8 NB |
1301 | __be16 csum; |
1302 | ||
d7a463b0 NB |
1303 | if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD)) |
1304 | goto check_ref; | |
1305 | ||
18213afb | 1306 | csum = cpu_to_be16(crc); |
41861fa8 NB |
1307 | |
1308 | if (sdt->guard_tag != csum) { | |
1309 | pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x" | |
1310 | " csum 0x%04x\n", (unsigned long long)sector, | |
1311 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); | |
1312 | return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; | |
1313 | } | |
1314 | ||
d7a463b0 NB |
1315 | check_ref: |
1316 | if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)) | |
1317 | return 0; | |
1318 | ||
823ddd87 | 1319 | if (cmd->prot_type == TARGET_DIF_TYPE1_PROT && |
41861fa8 NB |
1320 | be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { |
1321 | pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x" | |
1322 | " sector MSB: 0x%08x\n", (unsigned long long)sector, | |
1323 | be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff)); | |
1324 | return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; | |
1325 | } | |
1326 | ||
823ddd87 | 1327 | if (cmd->prot_type == TARGET_DIF_TYPE2_PROT && |
41861fa8 NB |
1328 | be32_to_cpu(sdt->ref_tag) != ei_lba) { |
1329 | pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x" | |
1330 | " ei_lba: 0x%08x\n", (unsigned long long)sector, | |
1331 | be32_to_cpu(sdt->ref_tag), ei_lba); | |
1332 | return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; | |
1333 | } | |
1334 | ||
1335 | return 0; | |
1336 | } | |
1337 | ||
f75b6fae SG |
1338 | void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read, |
1339 | struct scatterlist *sg, int sg_off) | |
41861fa8 NB |
1340 | { |
1341 | struct se_device *dev = cmd->se_dev; | |
1342 | struct scatterlist *psg; | |
1343 | void *paddr, *addr; | |
1344 | unsigned int i, len, left; | |
10762e80 | 1345 | unsigned int offset = sg_off; |
41861fa8 | 1346 | |
38b57f82 NB |
1347 | if (!sg) |
1348 | return; | |
1349 | ||
41861fa8 NB |
1350 | left = sectors * dev->prot_length; |
1351 | ||
1352 | for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { | |
16c0ae02 | 1353 | unsigned int psg_len, copied = 0; |
d6a65fdc | 1354 | |
41861fa8 | 1355 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
16c0ae02 SG |
1356 | psg_len = min(left, psg->length); |
1357 | while (psg_len) { | |
1358 | len = min(psg_len, sg->length - offset); | |
1359 | addr = kmap_atomic(sg_page(sg)) + sg->offset + offset; | |
1360 | ||
1361 | if (read) | |
1362 | memcpy(paddr + copied, addr, len); | |
1363 | else | |
1364 | memcpy(addr, paddr + copied, len); | |
1365 | ||
1366 | left -= len; | |
1367 | offset += len; | |
1368 | copied += len; | |
1369 | psg_len -= len; | |
1370 | ||
57636388 AM |
1371 | kunmap_atomic(addr - sg->offset - offset); |
1372 | ||
16c0ae02 SG |
1373 | if (offset >= sg->length) { |
1374 | sg = sg_next(sg); | |
1375 | offset = 0; | |
1376 | } | |
16c0ae02 | 1377 | } |
57636388 | 1378 | kunmap_atomic(paddr - psg->offset); |
41861fa8 NB |
1379 | } |
1380 | } | |
f75b6fae | 1381 | EXPORT_SYMBOL(sbc_dif_copy_prot); |
41861fa8 NB |
1382 | |
1383 | sense_reason_t | |
f75b6fae | 1384 | sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors, |
414e4627 | 1385 | unsigned int ei_lba, struct scatterlist *psg, int psg_off) |
41861fa8 NB |
1386 | { |
1387 | struct se_device *dev = cmd->se_dev; | |
fe052a18 | 1388 | struct t10_pi_tuple *sdt; |
18213afb | 1389 | struct scatterlist *dsg = cmd->t_data_sg; |
41861fa8 NB |
1390 | sector_t sector = start; |
1391 | void *daddr, *paddr; | |
18213afb | 1392 | int i; |
41861fa8 | 1393 | sense_reason_t rc; |
18213afb AM |
1394 | int dsg_off = 0; |
1395 | unsigned int block_size = dev->dev_attrib.block_size; | |
41861fa8 | 1396 | |
18213afb | 1397 | for (; psg && sector < start + sectors; psg = sg_next(psg)) { |
41861fa8 | 1398 | paddr = kmap_atomic(sg_page(psg)) + psg->offset; |
41861fa8 | 1399 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; |
41861fa8 | 1400 | |
18213afb AM |
1401 | for (i = psg_off; i < psg->length && |
1402 | sector < start + sectors; | |
fe052a18 | 1403 | i += sizeof(*sdt)) { |
18213afb AM |
1404 | __u16 crc; |
1405 | unsigned int avail; | |
41861fa8 | 1406 | |
18213afb AM |
1407 | if (dsg_off >= dsg->length) { |
1408 | dsg_off -= dsg->length; | |
1409 | kunmap_atomic(daddr - dsg->offset); | |
1410 | dsg = sg_next(dsg); | |
1411 | if (!dsg) { | |
1412 | kunmap_atomic(paddr - psg->offset); | |
1413 | return 0; | |
1414 | } | |
1415 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; | |
41861fa8 NB |
1416 | } |
1417 | ||
18213afb | 1418 | sdt = paddr + i; |
41861fa8 NB |
1419 | |
1420 | pr_debug("DIF READ sector: %llu guard_tag: 0x%04x" | |
1421 | " app_tag: 0x%04x ref_tag: %u\n", | |
1422 | (unsigned long long)sector, sdt->guard_tag, | |
1423 | sdt->app_tag, be32_to_cpu(sdt->ref_tag)); | |
1424 | ||
1425 | if (sdt->app_tag == cpu_to_be16(0xffff)) { | |
18213afb AM |
1426 | dsg_off += block_size; |
1427 | goto next; | |
1428 | } | |
1429 | ||
1430 | avail = min(block_size, dsg->length - dsg_off); | |
1431 | crc = crc_t10dif(daddr + dsg_off, avail); | |
1432 | if (avail < block_size) { | |
1433 | kunmap_atomic(daddr - dsg->offset); | |
1434 | dsg = sg_next(dsg); | |
1435 | if (!dsg) { | |
1436 | kunmap_atomic(paddr - psg->offset); | |
1437 | return 0; | |
1438 | } | |
1439 | daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; | |
1440 | dsg_off = block_size - avail; | |
1441 | crc = crc_t10dif_update(crc, daddr, dsg_off); | |
1442 | } else { | |
1443 | dsg_off += block_size; | |
41861fa8 NB |
1444 | } |
1445 | ||
18213afb | 1446 | rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba); |
41861fa8 | 1447 | if (rc) { |
414e4627 | 1448 | kunmap_atomic(daddr - dsg->offset); |
18213afb | 1449 | kunmap_atomic(paddr - psg->offset); |
76736db3 | 1450 | cmd->bad_sector = sector; |
41861fa8 NB |
1451 | return rc; |
1452 | } | |
18213afb | 1453 | next: |
41861fa8 NB |
1454 | sector++; |
1455 | ei_lba++; | |
41861fa8 NB |
1456 | } |
1457 | ||
18213afb | 1458 | psg_off = 0; |
414e4627 | 1459 | kunmap_atomic(daddr - dsg->offset); |
18213afb | 1460 | kunmap_atomic(paddr - psg->offset); |
41861fa8 | 1461 | } |
41861fa8 NB |
1462 | |
1463 | return 0; | |
1464 | } | |
f75b6fae | 1465 | EXPORT_SYMBOL(sbc_dif_verify); |