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