t10-pi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * t10_pi.c - Functions for generating and verifying T10 Protection
  4. * Information.
  5. */
  6. #include <linux/t10-pi.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/crc-t10dif.h>
  9. #include <linux/module.h>
  10. #include <net/checksum.h>
  11. typedef __be16 (csum_fn) (void *, unsigned int);
  12. static __be16 t10_pi_crc_fn(void *data, unsigned int len)
  13. {
  14. return cpu_to_be16(crc_t10dif(data, len));
  15. }
  16. static __be16 t10_pi_ip_fn(void *data, unsigned int len)
  17. {
  18. return (__force __be16)ip_compute_csum(data, len);
  19. }
  20. /*
  21. * Type 1 and Type 2 protection use the same format: 16 bit guard tag,
  22. * 16 bit app tag, 32 bit reference tag. Type 3 does not define the ref
  23. * tag.
  24. */
  25. static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
  26. csum_fn *fn, enum t10_dif_type type)
  27. {
  28. unsigned int i;
  29. for (i = 0 ; i < iter->data_size ; i += iter->interval) {
  30. struct t10_pi_tuple *pi = iter->prot_buf;
  31. pi->guard_tag = fn(iter->data_buf, iter->interval);
  32. pi->app_tag = 0;
  33. if (type == T10_PI_TYPE1_PROTECTION)
  34. pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed));
  35. else
  36. pi->ref_tag = 0;
  37. iter->data_buf += iter->interval;
  38. iter->prot_buf += sizeof(struct t10_pi_tuple);
  39. iter->seed++;
  40. }
  41. return BLK_STS_OK;
  42. }
  43. static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
  44. csum_fn *fn, enum t10_dif_type type)
  45. {
  46. unsigned int i;
  47. BUG_ON(type == T10_PI_TYPE0_PROTECTION);
  48. for (i = 0 ; i < iter->data_size ; i += iter->interval) {
  49. struct t10_pi_tuple *pi = iter->prot_buf;
  50. __be16 csum;
  51. if (type == T10_PI_TYPE1_PROTECTION ||
  52. type == T10_PI_TYPE2_PROTECTION) {
  53. if (pi->app_tag == T10_PI_APP_ESCAPE)
  54. goto next;
  55. if (be32_to_cpu(pi->ref_tag) !=
  56. lower_32_bits(iter->seed)) {
  57. pr_err("%s: ref tag error at location %llu " \
  58. "(rcvd %u)\n", iter->disk_name,
  59. (unsigned long long)
  60. iter->seed, be32_to_cpu(pi->ref_tag));
  61. return BLK_STS_PROTECTION;
  62. }
  63. } else if (type == T10_PI_TYPE3_PROTECTION) {
  64. if (pi->app_tag == T10_PI_APP_ESCAPE &&
  65. pi->ref_tag == T10_PI_REF_ESCAPE)
  66. goto next;
  67. }
  68. csum = fn(iter->data_buf, iter->interval);
  69. if (pi->guard_tag != csum) {
  70. pr_err("%s: guard tag error at sector %llu " \
  71. "(rcvd %04x, want %04x)\n", iter->disk_name,
  72. (unsigned long long)iter->seed,
  73. be16_to_cpu(pi->guard_tag), be16_to_cpu(csum));
  74. return BLK_STS_PROTECTION;
  75. }
  76. next:
  77. iter->data_buf += iter->interval;
  78. iter->prot_buf += sizeof(struct t10_pi_tuple);
  79. iter->seed++;
  80. }
  81. return BLK_STS_OK;
  82. }
  83. static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
  84. {
  85. return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE1_PROTECTION);
  86. }
  87. static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
  88. {
  89. return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION);
  90. }
  91. static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
  92. {
  93. return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE1_PROTECTION);
  94. }
  95. static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
  96. {
  97. return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION);
  98. }
  99. /**
  100. * t10_pi_type1_prepare - prepare PI prior submitting request to device
  101. * @rq: request with PI that should be prepared
  102. *
  103. * For Type 1/Type 2, the virtual start sector is the one that was
  104. * originally submitted by the block layer for the ref_tag usage. Due to
  105. * partitioning, MD/DM cloning, etc. the actual physical start sector is
  106. * likely to be different. Remap protection information to match the
  107. * physical LBA.
  108. */
  109. static void t10_pi_type1_prepare(struct request *rq)
  110. {
  111. const int tuple_sz = rq->q->integrity.tuple_size;
  112. u32 ref_tag = t10_pi_ref_tag(rq);
  113. struct bio *bio;
  114. __rq_for_each_bio(bio, rq) {
  115. struct bio_integrity_payload *bip = bio_integrity(bio);
  116. u32 virt = bip_get_seed(bip) & 0xffffffff;
  117. struct bio_vec iv;
  118. struct bvec_iter iter;
  119. /* Already remapped? */
  120. if (bip->bip_flags & BIP_MAPPED_INTEGRITY)
  121. break;
  122. bip_for_each_vec(iv, bip, iter) {
  123. void *p, *pmap;
  124. unsigned int j;
  125. pmap = kmap_atomic(iv.bv_page);
  126. p = pmap + iv.bv_offset;
  127. for (j = 0; j < iv.bv_len; j += tuple_sz) {
  128. struct t10_pi_tuple *pi = p;
  129. if (be32_to_cpu(pi->ref_tag) == virt)
  130. pi->ref_tag = cpu_to_be32(ref_tag);
  131. virt++;
  132. ref_tag++;
  133. p += tuple_sz;
  134. }
  135. kunmap_atomic(pmap);
  136. }
  137. bip->bip_flags |= BIP_MAPPED_INTEGRITY;
  138. }
  139. }
  140. /**
  141. * t10_pi_type1_complete - prepare PI prior returning request to the blk layer
  142. * @rq: request with PI that should be prepared
  143. * @nr_bytes: total bytes to prepare
  144. *
  145. * For Type 1/Type 2, the virtual start sector is the one that was
  146. * originally submitted by the block layer for the ref_tag usage. Due to
  147. * partitioning, MD/DM cloning, etc. the actual physical start sector is
  148. * likely to be different. Since the physical start sector was submitted
  149. * to the device, we should remap it back to virtual values expected by the
  150. * block layer.
  151. */
  152. static void t10_pi_type1_complete(struct request *rq, unsigned int nr_bytes)
  153. {
  154. unsigned intervals = nr_bytes >> rq->q->integrity.interval_exp;
  155. const int tuple_sz = rq->q->integrity.tuple_size;
  156. u32 ref_tag = t10_pi_ref_tag(rq);
  157. struct bio *bio;
  158. __rq_for_each_bio(bio, rq) {
  159. struct bio_integrity_payload *bip = bio_integrity(bio);
  160. u32 virt = bip_get_seed(bip) & 0xffffffff;
  161. struct bio_vec iv;
  162. struct bvec_iter iter;
  163. bip_for_each_vec(iv, bip, iter) {
  164. void *p, *pmap;
  165. unsigned int j;
  166. pmap = kmap_atomic(iv.bv_page);
  167. p = pmap + iv.bv_offset;
  168. for (j = 0; j < iv.bv_len && intervals; j += tuple_sz) {
  169. struct t10_pi_tuple *pi = p;
  170. if (be32_to_cpu(pi->ref_tag) == ref_tag)
  171. pi->ref_tag = cpu_to_be32(virt);
  172. virt++;
  173. ref_tag++;
  174. intervals--;
  175. p += tuple_sz;
  176. }
  177. kunmap_atomic(pmap);
  178. }
  179. }
  180. }
  181. static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
  182. {
  183. return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
  184. }
  185. static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
  186. {
  187. return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
  188. }
  189. static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
  190. {
  191. return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
  192. }
  193. static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
  194. {
  195. return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
  196. }
  197. /* Type 3 does not have a reference tag so no remapping is required. */
  198. static void t10_pi_type3_prepare(struct request *rq)
  199. {
  200. }
  201. /* Type 3 does not have a reference tag so no remapping is required. */
  202. static void t10_pi_type3_complete(struct request *rq, unsigned int nr_bytes)
  203. {
  204. }
  205. const struct blk_integrity_profile t10_pi_type1_crc = {
  206. .name = "T10-DIF-TYPE1-CRC",
  207. .generate_fn = t10_pi_type1_generate_crc,
  208. .verify_fn = t10_pi_type1_verify_crc,
  209. .prepare_fn = t10_pi_type1_prepare,
  210. .complete_fn = t10_pi_type1_complete,
  211. };
  212. EXPORT_SYMBOL(t10_pi_type1_crc);
  213. const struct blk_integrity_profile t10_pi_type1_ip = {
  214. .name = "T10-DIF-TYPE1-IP",
  215. .generate_fn = t10_pi_type1_generate_ip,
  216. .verify_fn = t10_pi_type1_verify_ip,
  217. .prepare_fn = t10_pi_type1_prepare,
  218. .complete_fn = t10_pi_type1_complete,
  219. };
  220. EXPORT_SYMBOL(t10_pi_type1_ip);
  221. const struct blk_integrity_profile t10_pi_type3_crc = {
  222. .name = "T10-DIF-TYPE3-CRC",
  223. .generate_fn = t10_pi_type3_generate_crc,
  224. .verify_fn = t10_pi_type3_verify_crc,
  225. .prepare_fn = t10_pi_type3_prepare,
  226. .complete_fn = t10_pi_type3_complete,
  227. };
  228. EXPORT_SYMBOL(t10_pi_type3_crc);
  229. const struct blk_integrity_profile t10_pi_type3_ip = {
  230. .name = "T10-DIF-TYPE3-IP",
  231. .generate_fn = t10_pi_type3_generate_ip,
  232. .verify_fn = t10_pi_type3_verify_ip,
  233. .prepare_fn = t10_pi_type3_prepare,
  234. .complete_fn = t10_pi_type3_complete,
  235. };
  236. EXPORT_SYMBOL(t10_pi_type3_ip);
  237. MODULE_LICENSE("GPL");