t10-pi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * t10_pi.c - Functions for generating and verifying T10 Protection
  3. * Information.
  4. *
  5. * Copyright (C) 2007, 2008, 2014 Oracle Corporation
  6. * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; see the file COPYING. If not, write to
  19. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  20. * USA.
  21. *
  22. */
  23. #include <linux/t10-pi.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/crc-t10dif.h>
  26. #include <net/checksum.h>
  27. typedef __be16 (csum_fn) (void *, unsigned int);
  28. static const __be16 APP_ESCAPE = (__force __be16) 0xffff;
  29. static const __be32 REF_ESCAPE = (__force __be32) 0xffffffff;
  30. static __be16 t10_pi_crc_fn(void *data, unsigned int len)
  31. {
  32. return cpu_to_be16(crc_t10dif(data, len));
  33. }
  34. static __be16 t10_pi_ip_fn(void *data, unsigned int len)
  35. {
  36. return (__force __be16)ip_compute_csum(data, len);
  37. }
  38. /*
  39. * Type 1 and Type 2 protection use the same format: 16 bit guard tag,
  40. * 16 bit app tag, 32 bit reference tag. Type 3 does not define the ref
  41. * tag.
  42. */
  43. static int t10_pi_generate(struct blk_integrity_iter *iter, csum_fn *fn,
  44. unsigned int type)
  45. {
  46. unsigned int i;
  47. for (i = 0 ; i < iter->data_size ; i += iter->interval) {
  48. struct t10_pi_tuple *pi = iter->prot_buf;
  49. pi->guard_tag = fn(iter->data_buf, iter->interval);
  50. pi->app_tag = 0;
  51. if (type == 1)
  52. pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed));
  53. else
  54. pi->ref_tag = 0;
  55. iter->data_buf += iter->interval;
  56. iter->prot_buf += sizeof(struct t10_pi_tuple);
  57. iter->seed++;
  58. }
  59. return 0;
  60. }
  61. static int t10_pi_verify(struct blk_integrity_iter *iter, csum_fn *fn,
  62. unsigned int type)
  63. {
  64. unsigned int i;
  65. for (i = 0 ; i < iter->data_size ; i += iter->interval) {
  66. struct t10_pi_tuple *pi = iter->prot_buf;
  67. __be16 csum;
  68. switch (type) {
  69. case 1:
  70. case 2:
  71. if (pi->app_tag == APP_ESCAPE)
  72. goto next;
  73. if (be32_to_cpu(pi->ref_tag) !=
  74. lower_32_bits(iter->seed)) {
  75. pr_err("%s: ref tag error at location %llu " \
  76. "(rcvd %u)\n", iter->disk_name,
  77. (unsigned long long)
  78. iter->seed, be32_to_cpu(pi->ref_tag));
  79. return -EILSEQ;
  80. }
  81. break;
  82. case 3:
  83. if (pi->app_tag == APP_ESCAPE &&
  84. pi->ref_tag == REF_ESCAPE)
  85. goto next;
  86. break;
  87. }
  88. csum = fn(iter->data_buf, iter->interval);
  89. if (pi->guard_tag != csum) {
  90. pr_err("%s: guard tag error at sector %llu " \
  91. "(rcvd %04x, want %04x)\n", iter->disk_name,
  92. (unsigned long long)iter->seed,
  93. be16_to_cpu(pi->guard_tag), be16_to_cpu(csum));
  94. return -EILSEQ;
  95. }
  96. next:
  97. iter->data_buf += iter->interval;
  98. iter->prot_buf += sizeof(struct t10_pi_tuple);
  99. iter->seed++;
  100. }
  101. return 0;
  102. }
  103. static int t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
  104. {
  105. return t10_pi_generate(iter, t10_pi_crc_fn, 1);
  106. }
  107. static int t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
  108. {
  109. return t10_pi_generate(iter, t10_pi_ip_fn, 1);
  110. }
  111. static int t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
  112. {
  113. return t10_pi_verify(iter, t10_pi_crc_fn, 1);
  114. }
  115. static int t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
  116. {
  117. return t10_pi_verify(iter, t10_pi_ip_fn, 1);
  118. }
  119. static int t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
  120. {
  121. return t10_pi_generate(iter, t10_pi_crc_fn, 3);
  122. }
  123. static int t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
  124. {
  125. return t10_pi_generate(iter, t10_pi_ip_fn, 3);
  126. }
  127. static int t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
  128. {
  129. return t10_pi_verify(iter, t10_pi_crc_fn, 3);
  130. }
  131. static int t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
  132. {
  133. return t10_pi_verify(iter, t10_pi_ip_fn, 3);
  134. }
  135. struct blk_integrity t10_pi_type1_crc = {
  136. .name = "T10-DIF-TYPE1-CRC",
  137. .generate_fn = t10_pi_type1_generate_crc,
  138. .verify_fn = t10_pi_type1_verify_crc,
  139. .tuple_size = sizeof(struct t10_pi_tuple),
  140. .tag_size = 0,
  141. };
  142. EXPORT_SYMBOL(t10_pi_type1_crc);
  143. struct blk_integrity t10_pi_type1_ip = {
  144. .name = "T10-DIF-TYPE1-IP",
  145. .generate_fn = t10_pi_type1_generate_ip,
  146. .verify_fn = t10_pi_type1_verify_ip,
  147. .tuple_size = sizeof(struct t10_pi_tuple),
  148. .tag_size = 0,
  149. };
  150. EXPORT_SYMBOL(t10_pi_type1_ip);
  151. struct blk_integrity t10_pi_type3_crc = {
  152. .name = "T10-DIF-TYPE3-CRC",
  153. .generate_fn = t10_pi_type3_generate_crc,
  154. .verify_fn = t10_pi_type3_verify_crc,
  155. .tuple_size = sizeof(struct t10_pi_tuple),
  156. .tag_size = 0,
  157. };
  158. EXPORT_SYMBOL(t10_pi_type3_crc);
  159. struct blk_integrity t10_pi_type3_ip = {
  160. .name = "T10-DIF-TYPE3-IP",
  161. .generate_fn = t10_pi_type3_generate_ip,
  162. .verify_fn = t10_pi_type3_verify_ip,
  163. .tuple_size = sizeof(struct t10_pi_tuple),
  164. .tag_size = 0,
  165. };
  166. EXPORT_SYMBOL(t10_pi_type3_ip);