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