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 unsigned char tuple_size; 23 const char *disk_name; 24 }; 25 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); 29 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; 35 const char *name; 36 }; 37 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 *); 45 46 static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk) 47 { 48 struct blk_integrity *bi = &disk->queue->integrity; 49 50 if (!bi->profile) 51 return NULL; 52 53 return bi; 54 } 55 56 static inline struct blk_integrity * 57 bdev_get_integrity(struct block_device *bdev) 58 { 59 return blk_get_integrity(bdev->bd_disk); 60 } 61 62 static inline bool 63 blk_integrity_queue_supports_integrity(struct request_queue *q) 64 { 65 return q->integrity.profile; 66 } 67 68 static inline void blk_queue_max_integrity_segments(struct request_queue *q, 69 unsigned int segs) 70 { 71 q->limits.max_integrity_segments = segs; 72 } 73 74 static inline unsigned short 75 queue_max_integrity_segments(const struct request_queue *q) 76 { 77 return q->limits.max_integrity_segments; 78 } 79 80 /** 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 84 * 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. 89 */ 90 static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi, 91 unsigned int sectors) 92 { 93 return sectors >> (bi->interval_exp - 9); 94 } 95 96 static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi, 97 unsigned int sectors) 98 { 99 return bio_integrity_intervals(bi, sectors) * bi->tuple_size; 100 } 101 102 static inline bool blk_integrity_rq(struct request *rq) 103 { 104 return rq->cmd_flags & REQ_INTEGRITY; 105 } 106 107 /* 108 * Return the current bvec that contains the integrity data. bip_iter may be 109 * advanced to iterate over the integrity data. 110 */ 111 static inline struct bio_vec rq_integrity_vec(struct request *rq) 112 { 113 return mp_bvec_iter_bvec(rq->bio->bi_integrity->bip_vec, 114 rq->bio->bi_integrity->bip_iter); 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 /* the optimizer will remove all calls to this function */ 181 return (struct bio_vec){ }; 182 } 183 #endif /* CONFIG_BLK_DEV_INTEGRITY */ 184 #endif /* _LINUX_BLK_INTEGRITY_H */ 185