xref: /openbmc/linux/include/linux/blk-integrity.h (revision 827beb77)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BLK_INTEGRITY_H
3 #define _LINUX_BLK_INTEGRITY_H
4 
5 #include <linux/blk-mq.h>
6 
7 struct request;
8 
9 enum blk_integrity_flags {
10 	BLK_INTEGRITY_VERIFY		= 1 << 0,
11 	BLK_INTEGRITY_GENERATE		= 1 << 1,
12 	BLK_INTEGRITY_DEVICE_CAPABLE	= 1 << 2,
13 	BLK_INTEGRITY_IP_CHECKSUM	= 1 << 3,
14 };
15 
16 struct blk_integrity_iter {
17 	void			*prot_buf;
18 	void			*data_buf;
19 	sector_t		seed;
20 	unsigned int		data_size;
21 	unsigned short		interval;
22 	const char		*disk_name;
23 };
24 
25 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
26 typedef void (integrity_prepare_fn) (struct request *);
27 typedef void (integrity_complete_fn) (struct request *, unsigned int);
28 
29 struct blk_integrity_profile {
30 	integrity_processing_fn		*generate_fn;
31 	integrity_processing_fn		*verify_fn;
32 	integrity_prepare_fn		*prepare_fn;
33 	integrity_complete_fn		*complete_fn;
34 	const char			*name;
35 };
36 
37 #ifdef CONFIG_BLK_DEV_INTEGRITY
38 void blk_integrity_register(struct gendisk *, struct blk_integrity *);
39 void blk_integrity_unregister(struct gendisk *);
40 int blk_integrity_compare(struct gendisk *, struct gendisk *);
41 int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
42 				   struct scatterlist *);
43 int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
44 
45 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
46 {
47 	struct blk_integrity *bi = &disk->queue->integrity;
48 
49 	if (!bi->profile)
50 		return NULL;
51 
52 	return bi;
53 }
54 
55 static inline struct blk_integrity *
56 bdev_get_integrity(struct block_device *bdev)
57 {
58 	return blk_get_integrity(bdev->bd_disk);
59 }
60 
61 static inline bool
62 blk_integrity_queue_supports_integrity(struct request_queue *q)
63 {
64 	return q->integrity.profile;
65 }
66 
67 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
68 						    unsigned int segs)
69 {
70 	q->limits.max_integrity_segments = segs;
71 }
72 
73 static inline unsigned short
74 queue_max_integrity_segments(const struct request_queue *q)
75 {
76 	return q->limits.max_integrity_segments;
77 }
78 
79 /**
80  * bio_integrity_intervals - Return number of integrity intervals for a bio
81  * @bi:		blk_integrity profile for device
82  * @sectors:	Size of the bio in 512-byte sectors
83  *
84  * Description: The block layer calculates everything in 512 byte
85  * sectors but integrity metadata is done in terms of the data integrity
86  * interval size of the storage device.  Convert the block layer sectors
87  * to the appropriate number of integrity intervals.
88  */
89 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
90 						   unsigned int sectors)
91 {
92 	return sectors >> (bi->interval_exp - 9);
93 }
94 
95 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
96 					       unsigned int sectors)
97 {
98 	return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
99 }
100 
101 static inline bool blk_integrity_rq(struct request *rq)
102 {
103 	return rq->cmd_flags & REQ_INTEGRITY;
104 }
105 
106 /*
107  * Return the first bvec that contains integrity data.  Only drivers that are
108  * limited to a single integrity segment should use this helper.
109  */
110 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
111 {
112 	if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
113 		return NULL;
114 	return rq->bio->bi_integrity->bip_vec;
115 }
116 #else /* CONFIG_BLK_DEV_INTEGRITY */
117 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
118 					    struct bio *b)
119 {
120 	return 0;
121 }
122 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
123 					  struct bio *b,
124 					  struct scatterlist *s)
125 {
126 	return 0;
127 }
128 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
129 {
130 	return NULL;
131 }
132 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
133 {
134 	return NULL;
135 }
136 static inline bool
137 blk_integrity_queue_supports_integrity(struct request_queue *q)
138 {
139 	return false;
140 }
141 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
142 {
143 	return 0;
144 }
145 static inline void blk_integrity_register(struct gendisk *d,
146 					 struct blk_integrity *b)
147 {
148 }
149 static inline void blk_integrity_unregister(struct gendisk *d)
150 {
151 }
152 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
153 						    unsigned int segs)
154 {
155 }
156 static inline unsigned short
157 queue_max_integrity_segments(const struct request_queue *q)
158 {
159 	return 0;
160 }
161 
162 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
163 						   unsigned int sectors)
164 {
165 	return 0;
166 }
167 
168 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
169 					       unsigned int sectors)
170 {
171 	return 0;
172 }
173 static inline int blk_integrity_rq(struct request *rq)
174 {
175 	return 0;
176 }
177 
178 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
179 {
180 	return NULL;
181 }
182 #endif /* CONFIG_BLK_DEV_INTEGRITY */
183 #endif /* _LINUX_BLK_INTEGRITY_H */
184