sd_dif.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * sd_dif.c - SCSI Data Integrity Field
  3. *
  4. * Copyright (C) 2007, 2008 Oracle Corporation
  5. * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; see the file COPYING. If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  19. * USA.
  20. *
  21. */
  22. #include <linux/blkdev.h>
  23. #include <linux/t10-pi.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_dbg.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_driver.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_ioctl.h>
  32. #include <scsi/scsicam.h>
  33. #include "sd.h"
  34. /*
  35. * Configure exchange of protection information between OS and HBA.
  36. */
  37. void sd_dif_config_host(struct scsi_disk *sdkp)
  38. {
  39. struct scsi_device *sdp = sdkp->device;
  40. struct gendisk *disk = sdkp->disk;
  41. u8 type = sdkp->protection_type;
  42. int dif, dix;
  43. dif = scsi_host_dif_capable(sdp->host, type);
  44. dix = scsi_host_dix_capable(sdp->host, type);
  45. if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
  46. dif = 0; dix = 1;
  47. }
  48. if (!dix)
  49. return;
  50. /* Enable DMA of protection information */
  51. if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
  52. if (type == SD_DIF_TYPE3_PROTECTION)
  53. blk_integrity_register(disk, &t10_pi_type3_ip);
  54. else
  55. blk_integrity_register(disk, &t10_pi_type1_ip);
  56. disk->integrity->flags |= BLK_INTEGRITY_IP_CHECKSUM;
  57. } else
  58. if (type == SD_DIF_TYPE3_PROTECTION)
  59. blk_integrity_register(disk, &t10_pi_type3_crc);
  60. else
  61. blk_integrity_register(disk, &t10_pi_type1_crc);
  62. sd_printk(KERN_NOTICE, sdkp,
  63. "Enabling DIX %s protection\n", disk->integrity->name);
  64. /* Signal to block layer that we support sector tagging */
  65. if (dif && type) {
  66. disk->integrity->flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  67. if (!sdkp->ATO)
  68. return;
  69. if (type == SD_DIF_TYPE3_PROTECTION)
  70. disk->integrity->tag_size = sizeof(u16) + sizeof(u32);
  71. else
  72. disk->integrity->tag_size = sizeof(u16);
  73. sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
  74. disk->integrity->tag_size);
  75. }
  76. }
  77. /*
  78. * The virtual start sector is the one that was originally submitted
  79. * by the block layer. Due to partitioning, MD/DM cloning, etc. the
  80. * actual physical start sector is likely to be different. Remap
  81. * protection information to match the physical LBA.
  82. *
  83. * From a protocol perspective there's a slight difference between
  84. * Type 1 and 2. The latter uses 32-byte CDBs exclusively, and the
  85. * reference tag is seeded in the CDB. This gives us the potential to
  86. * avoid virt->phys remapping during write. However, at read time we
  87. * don't know whether the virt sector is the same as when we wrote it
  88. * (we could be reading from real disk as opposed to MD/DM device. So
  89. * we always remap Type 2 making it identical to Type 1.
  90. *
  91. * Type 3 does not have a reference tag so no remapping is required.
  92. */
  93. void sd_dif_prepare(struct scsi_cmnd *scmd)
  94. {
  95. const int tuple_sz = sizeof(struct t10_pi_tuple);
  96. struct bio *bio;
  97. struct scsi_disk *sdkp;
  98. struct t10_pi_tuple *pi;
  99. u32 phys, virt;
  100. sdkp = scsi_disk(scmd->request->rq_disk);
  101. if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION)
  102. return;
  103. phys = scsi_prot_ref_tag(scmd);
  104. __rq_for_each_bio(bio, scmd->request) {
  105. struct bio_integrity_payload *bip = bio_integrity(bio);
  106. struct bio_vec iv;
  107. struct bvec_iter iter;
  108. unsigned int j;
  109. /* Already remapped? */
  110. if (bip->bip_flags & BIP_MAPPED_INTEGRITY)
  111. break;
  112. virt = bip_get_seed(bip) & 0xffffffff;
  113. bip_for_each_vec(iv, bip, iter) {
  114. pi = kmap_atomic(iv.bv_page) + iv.bv_offset;
  115. for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
  116. if (be32_to_cpu(pi->ref_tag) == virt)
  117. pi->ref_tag = cpu_to_be32(phys);
  118. virt++;
  119. phys++;
  120. }
  121. kunmap_atomic(pi);
  122. }
  123. bip->bip_flags |= BIP_MAPPED_INTEGRITY;
  124. }
  125. }
  126. /*
  127. * Remap physical sector values in the reference tag to the virtual
  128. * values expected by the block layer.
  129. */
  130. void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
  131. {
  132. const int tuple_sz = sizeof(struct t10_pi_tuple);
  133. struct scsi_disk *sdkp;
  134. struct bio *bio;
  135. struct t10_pi_tuple *pi;
  136. unsigned int j, intervals;
  137. u32 phys, virt;
  138. sdkp = scsi_disk(scmd->request->rq_disk);
  139. if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0)
  140. return;
  141. intervals = good_bytes / scsi_prot_interval(scmd);
  142. phys = scsi_prot_ref_tag(scmd);
  143. __rq_for_each_bio(bio, scmd->request) {
  144. struct bio_integrity_payload *bip = bio_integrity(bio);
  145. struct bio_vec iv;
  146. struct bvec_iter iter;
  147. virt = bip_get_seed(bip) & 0xffffffff;
  148. bip_for_each_vec(iv, bip, iter) {
  149. pi = kmap_atomic(iv.bv_page) + iv.bv_offset;
  150. for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
  151. if (intervals == 0) {
  152. kunmap_atomic(pi);
  153. return;
  154. }
  155. if (be32_to_cpu(pi->ref_tag) == phys)
  156. pi->ref_tag = cpu_to_be32(virt);
  157. virt++;
  158. phys++;
  159. intervals--;
  160. }
  161. kunmap_atomic(pi);
  162. }
  163. }
  164. }