xref: /openbmc/linux/include/linux/t10-pi.h (revision 868e6139)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
22341c2f8SMartin K. Petersen #ifndef _LINUX_T10_PI_H
32341c2f8SMartin K. Petersen #define _LINUX_T10_PI_H
42341c2f8SMartin K. Petersen 
52341c2f8SMartin K. Petersen #include <linux/types.h>
624b83debSChristoph Hellwig #include <linux/blk-mq.h>
72341c2f8SMartin K. Petersen 
82341c2f8SMartin K. Petersen /*
98475c811SChristoph Hellwig  * A T10 PI-capable target device can be formatted with different
108475c811SChristoph Hellwig  * protection schemes.	Currently 0 through 3 are defined:
118475c811SChristoph Hellwig  *
128475c811SChristoph Hellwig  * Type 0 is regular (unprotected) I/O
138475c811SChristoph Hellwig  *
148475c811SChristoph Hellwig  * Type 1 defines the contents of the guard and reference tags
158475c811SChristoph Hellwig  *
168475c811SChristoph Hellwig  * Type 2 defines the contents of the guard and reference tags and
178475c811SChristoph Hellwig  * uses 32-byte commands to seed the latter
188475c811SChristoph Hellwig  *
198475c811SChristoph Hellwig  * Type 3 defines the contents of the guard tag only
208475c811SChristoph Hellwig  */
218475c811SChristoph Hellwig enum t10_dif_type {
228475c811SChristoph Hellwig 	T10_PI_TYPE0_PROTECTION = 0x0,
238475c811SChristoph Hellwig 	T10_PI_TYPE1_PROTECTION = 0x1,
248475c811SChristoph Hellwig 	T10_PI_TYPE2_PROTECTION = 0x2,
258475c811SChristoph Hellwig 	T10_PI_TYPE3_PROTECTION = 0x3,
268475c811SChristoph Hellwig };
278475c811SChristoph Hellwig 
288475c811SChristoph Hellwig /*
292341c2f8SMartin K. Petersen  * T10 Protection Information tuple.
302341c2f8SMartin K. Petersen  */
312341c2f8SMartin K. Petersen struct t10_pi_tuple {
322341c2f8SMartin K. Petersen 	__be16 guard_tag;	/* Checksum */
332341c2f8SMartin K. Petersen 	__be16 app_tag;		/* Opaque storage */
342341c2f8SMartin K. Petersen 	__be32 ref_tag;		/* Target LBA or indirect LBA */
352341c2f8SMartin K. Petersen };
362341c2f8SMartin K. Petersen 
37128b6f9fSDmitry Monakhov #define T10_PI_APP_ESCAPE cpu_to_be16(0xffff)
38128b6f9fSDmitry Monakhov #define T10_PI_REF_ESCAPE cpu_to_be32(0xffffffff)
392341c2f8SMartin K. Petersen 
t10_pi_ref_tag(struct request * rq)40ddd0bc75SMax Gurtovoy static inline u32 t10_pi_ref_tag(struct request *rq)
41ddd0bc75SMax Gurtovoy {
4260a89a3cSMartin K. Petersen 	unsigned int shift = ilog2(queue_logical_block_size(rq->q));
4360a89a3cSMartin K. Petersen 
44ddd0bc75SMax Gurtovoy #ifdef CONFIG_BLK_DEV_INTEGRITY
4560a89a3cSMartin K. Petersen 	if (rq->q->integrity.interval_exp)
4660a89a3cSMartin K. Petersen 		shift = rq->q->integrity.interval_exp;
47ddd0bc75SMax Gurtovoy #endif
4860a89a3cSMartin K. Petersen 	return blk_rq_pos(rq) >> (shift - SECTOR_SHIFT) & 0xffffffff;
49ddd0bc75SMax Gurtovoy }
50ddd0bc75SMax Gurtovoy 
51869ab90fSEric Biggers extern const struct blk_integrity_profile t10_pi_type1_crc;
52869ab90fSEric Biggers extern const struct blk_integrity_profile t10_pi_type1_ip;
53869ab90fSEric Biggers extern const struct blk_integrity_profile t10_pi_type3_crc;
54869ab90fSEric Biggers extern const struct blk_integrity_profile t10_pi_type3_ip;
5508fcf813SJens Axboe 
56a7d4383fSKeith Busch struct crc64_pi_tuple {
57a7d4383fSKeith Busch 	__be64 guard_tag;
58a7d4383fSKeith Busch 	__be16 app_tag;
59a7d4383fSKeith Busch 	__u8   ref_tag[6];
60a7d4383fSKeith Busch };
61a7d4383fSKeith Busch 
62*868e6139SKeith Busch /**
63*868e6139SKeith Busch  * lower_48_bits() - return bits 0-47 of a number
64*868e6139SKeith Busch  * @n: the number we're accessing
65*868e6139SKeith Busch  */
lower_48_bits(u64 n)66*868e6139SKeith Busch static inline u64 lower_48_bits(u64 n)
67*868e6139SKeith Busch {
68*868e6139SKeith Busch 	return n & ((1ull << 48) - 1);
69*868e6139SKeith Busch }
70*868e6139SKeith Busch 
ext_pi_ref_tag(struct request * rq)71a7d4383fSKeith Busch static inline u64 ext_pi_ref_tag(struct request *rq)
72a7d4383fSKeith Busch {
73a7d4383fSKeith Busch 	unsigned int shift = ilog2(queue_logical_block_size(rq->q));
74a7d4383fSKeith Busch 
75a7d4383fSKeith Busch #ifdef CONFIG_BLK_DEV_INTEGRITY
76a7d4383fSKeith Busch 	if (rq->q->integrity.interval_exp)
77a7d4383fSKeith Busch 		shift = rq->q->integrity.interval_exp;
78a7d4383fSKeith Busch #endif
79a7d4383fSKeith Busch 	return lower_48_bits(blk_rq_pos(rq) >> (shift - SECTOR_SHIFT));
80a7d4383fSKeith Busch }
81a7d4383fSKeith Busch 
82a7d4383fSKeith Busch extern const struct blk_integrity_profile ext_pi_type1_crc64;
83a7d4383fSKeith Busch extern const struct blk_integrity_profile ext_pi_type3_crc64;
84a7d4383fSKeith Busch 
852341c2f8SMartin K. Petersen #endif
86