sd_dif.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. struct blk_integrity bi;
  43. int dif, dix;
  44. dif = scsi_host_dif_capable(sdp->host, type);
  45. dix = scsi_host_dix_capable(sdp->host, type);
  46. if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
  47. dif = 0; dix = 1;
  48. }
  49. if (!dix)
  50. return;
  51. memset(&bi, 0, sizeof(bi));
  52. /* Enable DMA of protection information */
  53. if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
  54. if (type == SD_DIF_TYPE3_PROTECTION)
  55. bi.profile = &t10_pi_type3_ip;
  56. else
  57. bi.profile = &t10_pi_type1_ip;
  58. bi.flags |= BLK_INTEGRITY_IP_CHECKSUM;
  59. } else
  60. if (type == SD_DIF_TYPE3_PROTECTION)
  61. bi.profile = &t10_pi_type3_crc;
  62. else
  63. bi.profile = &t10_pi_type1_crc;
  64. bi.tuple_size = sizeof(struct t10_pi_tuple);
  65. sd_printk(KERN_NOTICE, sdkp,
  66. "Enabling DIX %s protection\n", bi.profile->name);
  67. if (dif && type) {
  68. bi.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  69. if (!sdkp->ATO)
  70. goto out;
  71. if (type == SD_DIF_TYPE3_PROTECTION)
  72. bi.tag_size = sizeof(u16) + sizeof(u32);
  73. else
  74. bi.tag_size = sizeof(u16);
  75. sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
  76. bi.tag_size);
  77. }
  78. out:
  79. blk_integrity_register(disk, &bi);
  80. }
  81. /*
  82. * The virtual start sector is the one that was originally submitted
  83. * by the block layer. Due to partitioning, MD/DM cloning, etc. the
  84. * actual physical start sector is likely to be different. Remap
  85. * protection information to match the physical LBA.
  86. *
  87. * From a protocol perspective there's a slight difference between
  88. * Type 1 and 2. The latter uses 32-byte CDBs exclusively, and the
  89. * reference tag is seeded in the CDB. This gives us the potential to
  90. * avoid virt->phys remapping during write. However, at read time we
  91. * don't know whether the virt sector is the same as when we wrote it
  92. * (we could be reading from real disk as opposed to MD/DM device. So
  93. * we always remap Type 2 making it identical to Type 1.
  94. *
  95. * Type 3 does not have a reference tag so no remapping is required.
  96. */
  97. void sd_dif_prepare(struct scsi_cmnd *scmd)
  98. {
  99. const int tuple_sz = sizeof(struct t10_pi_tuple);
  100. struct bio *bio;
  101. struct scsi_disk *sdkp;
  102. struct t10_pi_tuple *pi;
  103. u32 phys, virt;
  104. sdkp = scsi_disk(scmd->request->rq_disk);
  105. if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION)
  106. return;
  107. phys = scsi_prot_ref_tag(scmd);
  108. __rq_for_each_bio(bio, scmd->request) {
  109. struct bio_integrity_payload *bip = bio_integrity(bio);
  110. struct bio_vec iv;
  111. struct bvec_iter iter;
  112. unsigned int j;
  113. /* Already remapped? */
  114. if (bip->bip_flags & BIP_MAPPED_INTEGRITY)
  115. break;
  116. virt = bip_get_seed(bip) & 0xffffffff;
  117. bip_for_each_vec(iv, bip, iter) {
  118. pi = kmap_atomic(iv.bv_page) + iv.bv_offset;
  119. for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
  120. if (be32_to_cpu(pi->ref_tag) == virt)
  121. pi->ref_tag = cpu_to_be32(phys);
  122. virt++;
  123. phys++;
  124. }
  125. kunmap_atomic(pi);
  126. }
  127. bip->bip_flags |= BIP_MAPPED_INTEGRITY;
  128. }
  129. }
  130. /*
  131. * Remap physical sector values in the reference tag to the virtual
  132. * values expected by the block layer.
  133. */
  134. void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
  135. {
  136. const int tuple_sz = sizeof(struct t10_pi_tuple);
  137. struct scsi_disk *sdkp;
  138. struct bio *bio;
  139. struct t10_pi_tuple *pi;
  140. unsigned int j, intervals;
  141. u32 phys, virt;
  142. sdkp = scsi_disk(scmd->request->rq_disk);
  143. if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0)
  144. return;
  145. intervals = good_bytes / scsi_prot_interval(scmd);
  146. phys = scsi_prot_ref_tag(scmd);
  147. __rq_for_each_bio(bio, scmd->request) {
  148. struct bio_integrity_payload *bip = bio_integrity(bio);
  149. struct bio_vec iv;
  150. struct bvec_iter iter;
  151. virt = bip_get_seed(bip) & 0xffffffff;
  152. bip_for_each_vec(iv, bip, iter) {
  153. pi = kmap_atomic(iv.bv_page) + iv.bv_offset;
  154. for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
  155. if (intervals == 0) {
  156. kunmap_atomic(pi);
  157. return;
  158. }
  159. if (be32_to_cpu(pi->ref_tag) == phys)
  160. pi->ref_tag = cpu_to_be32(virt);
  161. virt++;
  162. phys++;
  163. intervals--;
  164. }
  165. kunmap_atomic(pi);
  166. }
  167. }
  168. }