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