blob: 0c0094609dd6307c608ff4b562747b661c82f332 [file] [log] [blame]
Christoph Hellwig8c165672019-04-30 14:42:39 -04001// SPDX-License-Identifier: GPL-2.0
Martin K. Petersen2341c2f82014-09-26 19:20:07 -04002/*
3 * t10_pi.c - Functions for generating and verifying T10 Protection
4 * Information.
Martin K. Petersen2341c2f82014-09-26 19:20:07 -04005 */
6
7#include <linux/t10-pi.h>
8#include <linux/blkdev.h>
9#include <linux/crc-t10dif.h>
10#include <net/checksum.h>
11
12typedef __be16 (csum_fn) (void *, unsigned int);
13
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040014static __be16 t10_pi_crc_fn(void *data, unsigned int len)
15{
16 return cpu_to_be16(crc_t10dif(data, len));
17}
18
19static __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 Hellwig4e4cbee2017-06-03 09:38:06 +020029static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
30 csum_fn *fn, unsigned int type)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040031{
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
40 if (type == 1)
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 Hellwig4e4cbee2017-06-03 09:38:06 +020050 return BLK_STS_OK;
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040051}
52
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020053static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
54 csum_fn *fn, unsigned int type)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040055{
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) {
63 case 1:
64 case 2:
Dmitry Monakhov128b6f92017-06-29 11:31:12 -070065 if (pi->app_tag == T10_PI_APP_ESCAPE)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040066 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));
Bart Van Asschea462b952017-06-13 08:07:33 -070074 return BLK_STS_PROTECTION;
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040075 }
76 break;
77 case 3:
Dmitry Monakhov128b6f92017-06-29 11:31:12 -070078 if (pi->app_tag == T10_PI_APP_ESCAPE &&
79 pi->ref_tag == T10_PI_REF_ESCAPE)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040080 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));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020091 return BLK_STS_PROTECTION;
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040092 }
93
94next:
95 iter->data_buf += iter->interval;
96 iter->prot_buf += sizeof(struct t10_pi_tuple);
97 iter->seed++;
98 }
99
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200100 return BLK_STS_OK;
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400101}
102
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200103static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400104{
105 return t10_pi_generate(iter, t10_pi_crc_fn, 1);
106}
107
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200108static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400109{
110 return t10_pi_generate(iter, t10_pi_ip_fn, 1);
111}
112
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200113static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400114{
115 return t10_pi_verify(iter, t10_pi_crc_fn, 1);
116}
117
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200118static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400119{
120 return t10_pi_verify(iter, t10_pi_ip_fn, 1);
121}
122
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200123static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400124{
125 return t10_pi_generate(iter, t10_pi_crc_fn, 3);
126}
127
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200128static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400129{
130 return t10_pi_generate(iter, t10_pi_ip_fn, 3);
131}
132
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200133static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400134{
135 return t10_pi_verify(iter, t10_pi_crc_fn, 3);
136}
137
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200138static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400139{
140 return t10_pi_verify(iter, t10_pi_ip_fn, 3);
141}
142
Eric Biggers869ab902017-03-24 18:03:48 -0700143const struct blk_integrity_profile t10_pi_type1_crc = {
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400144 .name = "T10-DIF-TYPE1-CRC",
145 .generate_fn = t10_pi_type1_generate_crc,
146 .verify_fn = t10_pi_type1_verify_crc,
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400147};
148EXPORT_SYMBOL(t10_pi_type1_crc);
149
Eric Biggers869ab902017-03-24 18:03:48 -0700150const struct blk_integrity_profile t10_pi_type1_ip = {
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400151 .name = "T10-DIF-TYPE1-IP",
152 .generate_fn = t10_pi_type1_generate_ip,
153 .verify_fn = t10_pi_type1_verify_ip,
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400154};
155EXPORT_SYMBOL(t10_pi_type1_ip);
156
Eric Biggers869ab902017-03-24 18:03:48 -0700157const struct blk_integrity_profile t10_pi_type3_crc = {
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400158 .name = "T10-DIF-TYPE3-CRC",
159 .generate_fn = t10_pi_type3_generate_crc,
160 .verify_fn = t10_pi_type3_verify_crc,
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400161};
162EXPORT_SYMBOL(t10_pi_type3_crc);
163
Eric Biggers869ab902017-03-24 18:03:48 -0700164const struct blk_integrity_profile t10_pi_type3_ip = {
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400165 .name = "T10-DIF-TYPE3-IP",
166 .generate_fn = t10_pi_type3_generate_ip,
167 .verify_fn = t10_pi_type3_verify_ip,
Martin K. Petersen2341c2f82014-09-26 19:20:07 -0400168};
169EXPORT_SYMBOL(t10_pi_type3_ip);
Max Gurtovoy10c41dd2018-07-30 00:15:32 +0300170
171/**
172 * t10_pi_prepare - prepare PI prior submitting request to device
173 * @rq: request with PI that should be prepared
174 * @protection_type: PI type (Type 1/Type 2/Type 3)
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. Remap protection information to match the
180 * physical LBA.
181 *
182 * Type 3 does not have a reference tag so no remapping is required.
183 */
184void t10_pi_prepare(struct request *rq, u8 protection_type)
185{
186 const int tuple_sz = rq->q->integrity.tuple_size;
187 u32 ref_tag = t10_pi_ref_tag(rq);
188 struct bio *bio;
189
190 if (protection_type == T10_PI_TYPE3_PROTECTION)
191 return;
192
193 __rq_for_each_bio(bio, rq) {
194 struct bio_integrity_payload *bip = bio_integrity(bio);
195 u32 virt = bip_get_seed(bip) & 0xffffffff;
196 struct bio_vec iv;
197 struct bvec_iter iter;
198
199 /* Already remapped? */
200 if (bip->bip_flags & BIP_MAPPED_INTEGRITY)
201 break;
202
203 bip_for_each_vec(iv, bip, iter) {
204 void *p, *pmap;
205 unsigned int j;
206
207 pmap = kmap_atomic(iv.bv_page);
208 p = pmap + iv.bv_offset;
209 for (j = 0; j < iv.bv_len; j += tuple_sz) {
210 struct t10_pi_tuple *pi = p;
211
212 if (be32_to_cpu(pi->ref_tag) == virt)
213 pi->ref_tag = cpu_to_be32(ref_tag);
214 virt++;
215 ref_tag++;
216 p += tuple_sz;
217 }
218
219 kunmap_atomic(pmap);
220 }
221
222 bip->bip_flags |= BIP_MAPPED_INTEGRITY;
223 }
224}
225EXPORT_SYMBOL(t10_pi_prepare);
226
227/**
228 * t10_pi_complete - prepare PI prior returning request to the block layer
229 * @rq: request with PI that should be prepared
230 * @protection_type: PI type (Type 1/Type 2/Type 3)
231 * @intervals: total elements to prepare
232 *
233 * For Type 1/Type 2, the virtual start sector is the one that was
234 * originally submitted by the block layer for the ref_tag usage. Due to
235 * partitioning, MD/DM cloning, etc. the actual physical start sector is
236 * likely to be different. Since the physical start sector was submitted
237 * to the device, we should remap it back to virtual values expected by the
238 * block layer.
239 *
240 * Type 3 does not have a reference tag so no remapping is required.
241 */
242void t10_pi_complete(struct request *rq, u8 protection_type,
243 unsigned int intervals)
244{
245 const int tuple_sz = rq->q->integrity.tuple_size;
246 u32 ref_tag = t10_pi_ref_tag(rq);
247 struct bio *bio;
248
249 if (protection_type == T10_PI_TYPE3_PROTECTION)
250 return;
251
252 __rq_for_each_bio(bio, rq) {
253 struct bio_integrity_payload *bip = bio_integrity(bio);
254 u32 virt = bip_get_seed(bip) & 0xffffffff;
255 struct bio_vec iv;
256 struct bvec_iter iter;
257
258 bip_for_each_vec(iv, bip, iter) {
259 void *p, *pmap;
260 unsigned int j;
261
262 pmap = kmap_atomic(iv.bv_page);
263 p = pmap + iv.bv_offset;
264 for (j = 0; j < iv.bv_len && intervals; j += tuple_sz) {
265 struct t10_pi_tuple *pi = p;
266
267 if (be32_to_cpu(pi->ref_tag) == ref_tag)
268 pi->ref_tag = cpu_to_be32(virt);
269 virt++;
270 ref_tag++;
271 intervals--;
272 p += tuple_sz;
273 }
274
275 kunmap_atomic(pmap);
276 }
277 }
278}
279EXPORT_SYMBOL(t10_pi_complete);