1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BLK_INTEGRITY_H
3 #define _LINUX_BLK_INTEGRITY_H
5 #include <linux/blk-mq.h>
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,
16 struct blk_integrity_iter {
20 unsigned int data_size;
21 unsigned short interval;
22 unsigned char tuple_size;
23 const char *disk_name;
26 typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
27 typedef void (integrity_prepare_fn) (struct request *);
28 typedef void (integrity_complete_fn) (struct request *, unsigned int);
30 struct blk_integrity_profile {
31 integrity_processing_fn *generate_fn;
32 integrity_processing_fn *verify_fn;
33 integrity_prepare_fn *prepare_fn;
34 integrity_complete_fn *complete_fn;
38 #ifdef CONFIG_BLK_DEV_INTEGRITY
39 void blk_integrity_register(struct gendisk *, struct blk_integrity *);
40 void blk_integrity_unregister(struct gendisk *);
41 int blk_integrity_compare(struct gendisk *, struct gendisk *);
42 int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
43 struct scatterlist *);
44 int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
46 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
48 struct blk_integrity *bi = &disk->queue->integrity;
56 static inline struct blk_integrity *
57 bdev_get_integrity(struct block_device *bdev)
59 return blk_get_integrity(bdev->bd_disk);
63 blk_integrity_queue_supports_integrity(struct request_queue *q)
65 return q->integrity.profile;
68 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
71 q->limits.max_integrity_segments = segs;
74 static inline unsigned short
75 queue_max_integrity_segments(const struct request_queue *q)
77 return q->limits.max_integrity_segments;
81 * bio_integrity_intervals - Return number of integrity intervals for a bio
82 * @bi: blk_integrity profile for device
83 * @sectors: Size of the bio in 512-byte sectors
85 * Description: The block layer calculates everything in 512 byte
86 * sectors but integrity metadata is done in terms of the data integrity
87 * interval size of the storage device. Convert the block layer sectors
88 * to the appropriate number of integrity intervals.
90 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
93 return sectors >> (bi->interval_exp - 9);
96 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
99 return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
102 static inline bool blk_integrity_rq(struct request *rq)
104 return rq->cmd_flags & REQ_INTEGRITY;
108 * Return the first bvec that contains integrity data. Only drivers that are
109 * limited to a single integrity segment should use this helper.
111 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
113 if (WARN_ON_ONCE(queue_max_integrity_segments(rq->q) > 1))
115 return rq->bio->bi_integrity->bip_vec;
117 #else /* CONFIG_BLK_DEV_INTEGRITY */
118 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
123 static inline int blk_rq_map_integrity_sg(struct request_queue *q,
125 struct scatterlist *s)
129 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
133 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
138 blk_integrity_queue_supports_integrity(struct request_queue *q)
142 static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
146 static inline void blk_integrity_register(struct gendisk *d,
147 struct blk_integrity *b)
150 static inline void blk_integrity_unregister(struct gendisk *d)
153 static inline void blk_queue_max_integrity_segments(struct request_queue *q,
157 static inline unsigned short
158 queue_max_integrity_segments(const struct request_queue *q)
163 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
164 unsigned int sectors)
169 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
170 unsigned int sectors)
174 static inline int blk_integrity_rq(struct request *rq)
179 static inline struct bio_vec *rq_integrity_vec(struct request *rq)
183 #endif /* CONFIG_BLK_DEV_INTEGRITY */
184 #endif /* _LINUX_BLK_INTEGRITY_H */