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 #include <linux/bio-integrity.h>
7 
8 struct request;
9 
10 enum blk_integrity_flags {
11 	BLK_INTEGRITY_NOVERIFY		= 1 << 0,
12 	BLK_INTEGRITY_NOGENERATE	= 1 << 1,
13 	BLK_INTEGRITY_DEVICE_CAPABLE	= 1 << 2,
14 	BLK_INTEGRITY_REF_TAG		= 1 << 3,
15 	BLK_INTEGRITY_STACKED		= 1 << 4,
16 };
17 
18 const char *blk_integrity_profile_name(struct blk_integrity *bi);
19 bool queue_limits_stack_integrity(struct queue_limits *t,
20 		struct queue_limits *b);
queue_limits_stack_integrity_bdev(struct queue_limits * t,struct block_device * bdev)21 static inline bool queue_limits_stack_integrity_bdev(struct queue_limits *t,
22 		struct block_device *bdev)
23 {
24 	return queue_limits_stack_integrity(t, &bdev->bd_disk->queue->limits);
25 }
26 
27 #ifdef CONFIG_BLK_DEV_INTEGRITY
28 int blk_rq_map_integrity_sg(struct request *, struct scatterlist *);
29 int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
30 int blk_rq_integrity_map_user(struct request *rq, void __user *ubuf,
31 			      ssize_t bytes, u32 seed);
32 
33 static inline bool
blk_integrity_queue_supports_integrity(struct request_queue * q)34 blk_integrity_queue_supports_integrity(struct request_queue *q)
35 {
36 	return q->limits.integrity.tuple_size;
37 }
38 
blk_get_integrity(struct gendisk * disk)39 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
40 {
41 	if (!blk_integrity_queue_supports_integrity(disk->queue))
42 		return NULL;
43 	return &disk->queue->limits.integrity;
44 }
45 
46 static inline struct blk_integrity *
bdev_get_integrity(struct block_device * bdev)47 bdev_get_integrity(struct block_device *bdev)
48 {
49 	return blk_get_integrity(bdev->bd_disk);
50 }
51 
52 static inline unsigned short
queue_max_integrity_segments(const struct request_queue * q)53 queue_max_integrity_segments(const struct request_queue *q)
54 {
55 	return q->limits.max_integrity_segments;
56 }
57 
58 /**
59  * bio_integrity_intervals - Return number of integrity intervals for a bio
60  * @bi:		blk_integrity profile for device
61  * @sectors:	Size of the bio in 512-byte sectors
62  *
63  * Description: The block layer calculates everything in 512 byte
64  * sectors but integrity metadata is done in terms of the data integrity
65  * interval size of the storage device.  Convert the block layer sectors
66  * to the appropriate number of integrity intervals.
67  */
bio_integrity_intervals(struct blk_integrity * bi,unsigned int sectors)68 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
69 						   unsigned int sectors)
70 {
71 	return sectors >> (bi->interval_exp - 9);
72 }
73 
bio_integrity_bytes(struct blk_integrity * bi,unsigned int sectors)74 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
75 					       unsigned int sectors)
76 {
77 	return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
78 }
79 
blk_integrity_rq(struct request * rq)80 static inline bool blk_integrity_rq(struct request *rq)
81 {
82 	return rq->cmd_flags & REQ_INTEGRITY;
83 }
84 
85 /*
86  * Return the current bvec that contains the integrity data. bip_iter may be
87  * advanced to iterate over the integrity data.
88  */
rq_integrity_vec(struct request * rq)89 static inline struct bio_vec rq_integrity_vec(struct request *rq)
90 {
91 	return mp_bvec_iter_bvec(rq->bio->bi_integrity->bip_vec,
92 				 rq->bio->bi_integrity->bip_iter);
93 }
94 #else /* CONFIG_BLK_DEV_INTEGRITY */
blk_rq_count_integrity_sg(struct request_queue * q,struct bio * b)95 static inline int blk_rq_count_integrity_sg(struct request_queue *q,
96 					    struct bio *b)
97 {
98 	return 0;
99 }
blk_rq_map_integrity_sg(struct request * q,struct scatterlist * s)100 static inline int blk_rq_map_integrity_sg(struct request *q,
101 					  struct scatterlist *s)
102 {
103 	return 0;
104 }
blk_rq_integrity_map_user(struct request * rq,void __user * ubuf,ssize_t bytes,u32 seed)105 static inline int blk_rq_integrity_map_user(struct request *rq,
106 					    void __user *ubuf,
107 					    ssize_t bytes,
108 					    u32 seed)
109 {
110 	return -EINVAL;
111 }
bdev_get_integrity(struct block_device * b)112 static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
113 {
114 	return NULL;
115 }
blk_get_integrity(struct gendisk * disk)116 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
117 {
118 	return NULL;
119 }
120 static inline bool
blk_integrity_queue_supports_integrity(struct request_queue * q)121 blk_integrity_queue_supports_integrity(struct request_queue *q)
122 {
123 	return false;
124 }
125 static inline unsigned short
queue_max_integrity_segments(const struct request_queue * q)126 queue_max_integrity_segments(const struct request_queue *q)
127 {
128 	return 0;
129 }
130 
bio_integrity_intervals(struct blk_integrity * bi,unsigned int sectors)131 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
132 						   unsigned int sectors)
133 {
134 	return 0;
135 }
136 
bio_integrity_bytes(struct blk_integrity * bi,unsigned int sectors)137 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
138 					       unsigned int sectors)
139 {
140 	return 0;
141 }
blk_integrity_rq(struct request * rq)142 static inline int blk_integrity_rq(struct request *rq)
143 {
144 	return 0;
145 }
146 
rq_integrity_vec(struct request * rq)147 static inline struct bio_vec rq_integrity_vec(struct request *rq)
148 {
149 	/* the optimizer will remove all calls to this function */
150 	return (struct bio_vec){ };
151 }
152 #endif /* CONFIG_BLK_DEV_INTEGRITY */
153 
154 #endif /* _LINUX_BLK_INTEGRITY_H */
155