1 // SPDX-License-Identifier: GPL-2.0
3 * t10_pi.c - Functions for generating and verifying T10 Protection
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
29 static blk_status_t
t10_pi_generate(struct blk_integrity_iter
*iter
,
30 csum_fn
*fn
, enum t10_dif_type type
)
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
);
40 if (type
== T10_PI_TYPE1_PROTECTION
)
41 pi
->ref_tag
= cpu_to_be32(lower_32_bits(iter
->seed
));
45 iter
->data_buf
+= iter
->interval
;
46 iter
->prot_buf
+= sizeof(struct t10_pi_tuple
);
53 static blk_status_t
t10_pi_verify(struct blk_integrity_iter
*iter
,
54 csum_fn
*fn
, enum t10_dif_type type
)
58 BUG_ON(type
== T10_PI_TYPE0_PROTECTION
);
60 for (i
= 0 ; i
< iter
->data_size
; i
+= iter
->interval
) {
61 struct t10_pi_tuple
*pi
= iter
->prot_buf
;
64 if (type
== T10_PI_TYPE1_PROTECTION
||
65 type
== T10_PI_TYPE2_PROTECTION
) {
66 if (pi
->app_tag
== T10_PI_APP_ESCAPE
)
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
,
74 iter
->seed
, be32_to_cpu(pi
->ref_tag
));
75 return BLK_STS_PROTECTION
;
77 } else if (type
== T10_PI_TYPE3_PROTECTION
) {
78 if (pi
->app_tag
== T10_PI_APP_ESCAPE
&&
79 pi
->ref_tag
== T10_PI_REF_ESCAPE
)
83 csum
= fn(iter
->data_buf
, iter
->interval
);
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
));
90 return BLK_STS_PROTECTION
;
94 iter
->data_buf
+= iter
->interval
;
95 iter
->prot_buf
+= sizeof(struct t10_pi_tuple
);
102 static blk_status_t
t10_pi_type1_generate_crc(struct blk_integrity_iter
*iter
)
104 return t10_pi_generate(iter
, t10_pi_crc_fn
, T10_PI_TYPE1_PROTECTION
);
107 static blk_status_t
t10_pi_type1_generate_ip(struct blk_integrity_iter
*iter
)
109 return t10_pi_generate(iter
, t10_pi_ip_fn
, T10_PI_TYPE1_PROTECTION
);
112 static blk_status_t
t10_pi_type1_verify_crc(struct blk_integrity_iter
*iter
)
114 return t10_pi_verify(iter
, t10_pi_crc_fn
, T10_PI_TYPE1_PROTECTION
);
117 static blk_status_t
t10_pi_type1_verify_ip(struct blk_integrity_iter
*iter
)
119 return t10_pi_verify(iter
, t10_pi_ip_fn
, T10_PI_TYPE1_PROTECTION
);
123 * t10_pi_type1_prepare - prepare PI prior submitting request to device
124 * @rq: request with PI that should be prepared
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
132 static void t10_pi_type1_prepare(struct request
*rq
)
134 const int tuple_sz
= rq
->q
->integrity
.tuple_size
;
135 u32 ref_tag
= t10_pi_ref_tag(rq
);
138 __rq_for_each_bio(bio
, rq
) {
139 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
140 u32 virt
= bip_get_seed(bip
) & 0xffffffff;
142 struct bvec_iter iter
;
144 /* Already remapped? */
145 if (bip
->bip_flags
& BIP_MAPPED_INTEGRITY
)
148 bip_for_each_vec(iv
, bip
, iter
) {
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
;
157 if (be32_to_cpu(pi
->ref_tag
) == virt
)
158 pi
->ref_tag
= cpu_to_be32(ref_tag
);
167 bip
->bip_flags
|= BIP_MAPPED_INTEGRITY
;
172 * t10_pi_type1_complete - prepare PI prior returning request to the blk layer
173 * @rq: request with PI that should be prepared
174 * @nr_bytes: total bytes to prepare
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
183 static void t10_pi_type1_complete(struct request
*rq
, unsigned int nr_bytes
)
185 unsigned intervals
= nr_bytes
>> rq
->q
->integrity
.interval_exp
;
186 const int tuple_sz
= rq
->q
->integrity
.tuple_size
;
187 u32 ref_tag
= t10_pi_ref_tag(rq
);
190 __rq_for_each_bio(bio
, rq
) {
191 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
192 u32 virt
= bip_get_seed(bip
) & 0xffffffff;
194 struct bvec_iter iter
;
196 bip_for_each_vec(iv
, bip
, iter
) {
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
;
205 if (be32_to_cpu(pi
->ref_tag
) == ref_tag
)
206 pi
->ref_tag
= cpu_to_be32(virt
);
218 static blk_status_t
t10_pi_type3_generate_crc(struct blk_integrity_iter
*iter
)
220 return t10_pi_generate(iter
, t10_pi_crc_fn
, T10_PI_TYPE3_PROTECTION
);
223 static blk_status_t
t10_pi_type3_generate_ip(struct blk_integrity_iter
*iter
)
225 return t10_pi_generate(iter
, t10_pi_ip_fn
, T10_PI_TYPE3_PROTECTION
);
228 static blk_status_t
t10_pi_type3_verify_crc(struct blk_integrity_iter
*iter
)
230 return t10_pi_verify(iter
, t10_pi_crc_fn
, T10_PI_TYPE3_PROTECTION
);
233 static blk_status_t
t10_pi_type3_verify_ip(struct blk_integrity_iter
*iter
)
235 return t10_pi_verify(iter
, t10_pi_ip_fn
, T10_PI_TYPE3_PROTECTION
);
238 /* Type 3 does not have a reference tag so no remapping is required. */
239 static void t10_pi_type3_prepare(struct request
*rq
)
243 /* Type 3 does not have a reference tag so no remapping is required. */
244 static void t10_pi_type3_complete(struct request
*rq
, unsigned int nr_bytes
)
248 const struct blk_integrity_profile t10_pi_type1_crc
= {
249 .name
= "T10-DIF-TYPE1-CRC",
250 .generate_fn
= t10_pi_type1_generate_crc
,
251 .verify_fn
= t10_pi_type1_verify_crc
,
252 .prepare_fn
= t10_pi_type1_prepare
,
253 .complete_fn
= t10_pi_type1_complete
,
255 EXPORT_SYMBOL(t10_pi_type1_crc
);
257 const struct blk_integrity_profile t10_pi_type1_ip
= {
258 .name
= "T10-DIF-TYPE1-IP",
259 .generate_fn
= t10_pi_type1_generate_ip
,
260 .verify_fn
= t10_pi_type1_verify_ip
,
261 .prepare_fn
= t10_pi_type1_prepare
,
262 .complete_fn
= t10_pi_type1_complete
,
264 EXPORT_SYMBOL(t10_pi_type1_ip
);
266 const struct blk_integrity_profile t10_pi_type3_crc
= {
267 .name
= "T10-DIF-TYPE3-CRC",
268 .generate_fn
= t10_pi_type3_generate_crc
,
269 .verify_fn
= t10_pi_type3_verify_crc
,
270 .prepare_fn
= t10_pi_type3_prepare
,
271 .complete_fn
= t10_pi_type3_complete
,
273 EXPORT_SYMBOL(t10_pi_type3_crc
);
275 const struct blk_integrity_profile t10_pi_type3_ip
= {
276 .name
= "T10-DIF-TYPE3-IP",
277 .generate_fn
= t10_pi_type3_generate_ip
,
278 .verify_fn
= t10_pi_type3_verify_ip
,
279 .prepare_fn
= t10_pi_type3_prepare
,
280 .complete_fn
= t10_pi_type3_complete
,
282 EXPORT_SYMBOL(t10_pi_type3_ip
);