xref: /openbmc/linux/block/t10-pi.c (revision 59bd9ded)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * t10_pi.c - Functions for generating and verifying T10 Protection
4  *	      Information.
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 
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  */
29 static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
30 		csum_fn *fn, unsigned int type)
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 
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 
50 	return BLK_STS_OK;
51 }
52 
53 static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
54 		csum_fn *fn, unsigned int type)
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) {
63 		case 1:
64 		case 2:
65 			if (pi->app_tag == T10_PI_APP_ESCAPE)
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));
74 				return BLK_STS_PROTECTION;
75 			}
76 			break;
77 		case 3:
78 			if (pi->app_tag == T10_PI_APP_ESCAPE &&
79 			    pi->ref_tag == T10_PI_REF_ESCAPE)
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));
91 			return BLK_STS_PROTECTION;
92 		}
93 
94 next:
95 		iter->data_buf += iter->interval;
96 		iter->prot_buf += sizeof(struct t10_pi_tuple);
97 		iter->seed++;
98 	}
99 
100 	return BLK_STS_OK;
101 }
102 
103 static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
104 {
105 	return t10_pi_generate(iter, t10_pi_crc_fn, 1);
106 }
107 
108 static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
109 {
110 	return t10_pi_generate(iter, t10_pi_ip_fn, 1);
111 }
112 
113 static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
114 {
115 	return t10_pi_verify(iter, t10_pi_crc_fn, 1);
116 }
117 
118 static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
119 {
120 	return t10_pi_verify(iter, t10_pi_ip_fn, 1);
121 }
122 
123 static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
124 {
125 	return t10_pi_generate(iter, t10_pi_crc_fn, 3);
126 }
127 
128 static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
129 {
130 	return t10_pi_generate(iter, t10_pi_ip_fn, 3);
131 }
132 
133 static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
134 {
135 	return t10_pi_verify(iter, t10_pi_crc_fn, 3);
136 }
137 
138 static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
139 {
140 	return t10_pi_verify(iter, t10_pi_ip_fn, 3);
141 }
142 
143 const struct blk_integrity_profile t10_pi_type1_crc = {
144 	.name			= "T10-DIF-TYPE1-CRC",
145 	.generate_fn		= t10_pi_type1_generate_crc,
146 	.verify_fn		= t10_pi_type1_verify_crc,
147 };
148 EXPORT_SYMBOL(t10_pi_type1_crc);
149 
150 const struct blk_integrity_profile t10_pi_type1_ip = {
151 	.name			= "T10-DIF-TYPE1-IP",
152 	.generate_fn		= t10_pi_type1_generate_ip,
153 	.verify_fn		= t10_pi_type1_verify_ip,
154 };
155 EXPORT_SYMBOL(t10_pi_type1_ip);
156 
157 const struct blk_integrity_profile t10_pi_type3_crc = {
158 	.name			= "T10-DIF-TYPE3-CRC",
159 	.generate_fn		= t10_pi_type3_generate_crc,
160 	.verify_fn		= t10_pi_type3_verify_crc,
161 };
162 EXPORT_SYMBOL(t10_pi_type3_crc);
163 
164 const struct blk_integrity_profile t10_pi_type3_ip = {
165 	.name			= "T10-DIF-TYPE3-IP",
166 	.generate_fn		= t10_pi_type3_generate_ip,
167 	.verify_fn		= t10_pi_type3_verify_ip,
168 };
169 EXPORT_SYMBOL(t10_pi_type3_ip);
170 
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  */
184 void 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 }
225 EXPORT_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  */
242 void 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 }
279 EXPORT_SYMBOL(t10_pi_complete);
280