mtd: rawnand: r852: Use dev_get_drvdata
[linux/fpc-iii.git] / block / t10-pi.c
blob0c0094609dd6307c608ff4b562747b661c82f332
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * t10_pi.c - Functions for generating and verifying T10 Protection
4 * Information.
5 */
7 #include <linux/t10-pi.h>
8 #include <linux/blkdev.h>
9 #include <linux/crc-t10dif.h>
10 #include <net/checksum.h>
12 typedef __be16 (csum_fn) (void *, unsigned int);
14 static __be16 t10_pi_crc_fn(void *data, unsigned int len)
16 return cpu_to_be16(crc_t10dif(data, len));
19 static __be16 t10_pi_ip_fn(void *data, unsigned int len)
21 return (__force __be16)ip_compute_csum(data, len);
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.
29 static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
30 csum_fn *fn, unsigned int type)
32 unsigned int i;
34 for (i = 0 ; i < iter->data_size ; i += iter->interval) {
35 struct t10_pi_tuple *pi = iter->prot_buf;
37 pi->guard_tag = fn(iter->data_buf, iter->interval);
38 pi->app_tag = 0;
40 if (type == 1)
41 pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed));
42 else
43 pi->ref_tag = 0;
45 iter->data_buf += iter->interval;
46 iter->prot_buf += sizeof(struct t10_pi_tuple);
47 iter->seed++;
50 return BLK_STS_OK;
53 static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
54 csum_fn *fn, unsigned int type)
56 unsigned int i;
58 for (i = 0 ; i < iter->data_size ; i += iter->interval) {
59 struct t10_pi_tuple *pi = iter->prot_buf;
60 __be16 csum;
62 switch (type) {
63 case 1:
64 case 2:
65 if (pi->app_tag == T10_PI_APP_ESCAPE)
66 goto next;
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;
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;
84 csum = fn(iter->data_buf, iter->interval);
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;
94 next:
95 iter->data_buf += iter->interval;
96 iter->prot_buf += sizeof(struct t10_pi_tuple);
97 iter->seed++;
100 return BLK_STS_OK;
103 static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
105 return t10_pi_generate(iter, t10_pi_crc_fn, 1);
108 static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
110 return t10_pi_generate(iter, t10_pi_ip_fn, 1);
113 static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
115 return t10_pi_verify(iter, t10_pi_crc_fn, 1);
118 static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
120 return t10_pi_verify(iter, t10_pi_ip_fn, 1);
123 static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
125 return t10_pi_generate(iter, t10_pi_crc_fn, 3);
128 static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
130 return t10_pi_generate(iter, t10_pi_ip_fn, 3);
133 static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
135 return t10_pi_verify(iter, t10_pi_crc_fn, 3);
138 static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
140 return t10_pi_verify(iter, t10_pi_ip_fn, 3);
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,
148 EXPORT_SYMBOL(t10_pi_type1_crc);
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,
155 EXPORT_SYMBOL(t10_pi_type1_ip);
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,
162 EXPORT_SYMBOL(t10_pi_type3_crc);
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,
169 EXPORT_SYMBOL(t10_pi_type3_ip);
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)
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.
182 * Type 3 does not have a reference tag so no remapping is required.
184 void t10_pi_prepare(struct request *rq, u8 protection_type)
186 const int tuple_sz = rq->q->integrity.tuple_size;
187 u32 ref_tag = t10_pi_ref_tag(rq);
188 struct bio *bio;
190 if (protection_type == T10_PI_TYPE3_PROTECTION)
191 return;
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;
199 /* Already remapped? */
200 if (bip->bip_flags & BIP_MAPPED_INTEGRITY)
201 break;
203 bip_for_each_vec(iv, bip, iter) {
204 void *p, *pmap;
205 unsigned int j;
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;
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;
219 kunmap_atomic(pmap);
222 bip->bip_flags |= BIP_MAPPED_INTEGRITY;
225 EXPORT_SYMBOL(t10_pi_prepare);
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
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.
240 * Type 3 does not have a reference tag so no remapping is required.
242 void t10_pi_complete(struct request *rq, u8 protection_type,
243 unsigned int intervals)
245 const int tuple_sz = rq->q->integrity.tuple_size;
246 u32 ref_tag = t10_pi_ref_tag(rq);
247 struct bio *bio;
249 if (protection_type == T10_PI_TYPE3_PROTECTION)
250 return;
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;
258 bip_for_each_vec(iv, bip, iter) {
259 void *p, *pmap;
260 unsigned int j;
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;
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;
275 kunmap_atomic(pmap);
279 EXPORT_SYMBOL(t10_pi_complete);