1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_ELEVATOR_H 3 #define _LINUX_ELEVATOR_H 4 5 #include <linux/percpu.h> 6 #include <linux/hashtable.h> 7 8 #ifdef CONFIG_BLOCK 9 10 struct io_cq; 11 struct elevator_type; 12 #ifdef CONFIG_BLK_DEBUG_FS 13 struct blk_mq_debugfs_attr; 14 #endif 15 16 /* 17 * Return values from elevator merger 18 */ 19 enum elv_merge { 20 ELEVATOR_NO_MERGE = 0, 21 ELEVATOR_FRONT_MERGE = 1, 22 ELEVATOR_BACK_MERGE = 2, 23 ELEVATOR_DISCARD_MERGE = 3, 24 }; 25 26 struct blk_mq_alloc_data; 27 struct blk_mq_hw_ctx; 28 29 struct elevator_mq_ops { 30 int (*init_sched)(struct request_queue *, struct elevator_type *); 31 void (*exit_sched)(struct elevator_queue *); 32 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int); 33 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int); 34 void (*depth_updated)(struct blk_mq_hw_ctx *); 35 36 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *); 37 bool (*bio_merge)(struct request_queue *, struct bio *, unsigned int); 38 int (*request_merge)(struct request_queue *q, struct request **, struct bio *); 39 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge); 40 void (*requests_merged)(struct request_queue *, struct request *, struct request *); 41 void (*limit_depth)(unsigned int, struct blk_mq_alloc_data *); 42 void (*prepare_request)(struct request *); 43 void (*finish_request)(struct request *); 44 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool); 45 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *); 46 bool (*has_work)(struct blk_mq_hw_ctx *); 47 void (*completed_request)(struct request *, u64); 48 void (*requeue_request)(struct request *); 49 struct request *(*former_request)(struct request_queue *, struct request *); 50 struct request *(*next_request)(struct request_queue *, struct request *); 51 void (*init_icq)(struct io_cq *); 52 void (*exit_icq)(struct io_cq *); 53 }; 54 55 #define ELV_NAME_MAX (16) 56 57 struct elv_fs_entry { 58 struct attribute attr; 59 ssize_t (*show)(struct elevator_queue *, char *); 60 ssize_t (*store)(struct elevator_queue *, const char *, size_t); 61 }; 62 63 /* 64 * identifies an elevator type, such as AS or deadline 65 */ 66 struct elevator_type 67 { 68 /* managed by elevator core */ 69 struct kmem_cache *icq_cache; 70 71 /* fields provided by elevator implementation */ 72 struct elevator_mq_ops ops; 73 74 size_t icq_size; /* see iocontext.h */ 75 size_t icq_align; /* ditto */ 76 struct elv_fs_entry *elevator_attrs; 77 const char *elevator_name; 78 const char *elevator_alias; 79 const unsigned int elevator_features; 80 struct module *elevator_owner; 81 #ifdef CONFIG_BLK_DEBUG_FS 82 const struct blk_mq_debugfs_attr *queue_debugfs_attrs; 83 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs; 84 #endif 85 86 /* managed by elevator core */ 87 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */ 88 struct list_head list; 89 }; 90 91 #define ELV_HASH_BITS 6 92 93 void elv_rqhash_del(struct request_queue *q, struct request *rq); 94 void elv_rqhash_add(struct request_queue *q, struct request *rq); 95 void elv_rqhash_reposition(struct request_queue *q, struct request *rq); 96 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset); 97 98 /* 99 * each queue has an elevator_queue associated with it 100 */ 101 struct elevator_queue 102 { 103 struct elevator_type *type; 104 void *elevator_data; 105 struct kobject kobj; 106 struct mutex sysfs_lock; 107 unsigned int registered:1; 108 DECLARE_HASHTABLE(hash, ELV_HASH_BITS); 109 }; 110 111 /* 112 * block elevator interface 113 */ 114 extern enum elv_merge elv_merge(struct request_queue *, struct request **, 115 struct bio *); 116 extern void elv_merge_requests(struct request_queue *, struct request *, 117 struct request *); 118 extern void elv_merged_request(struct request_queue *, struct request *, 119 enum elv_merge); 120 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *, 121 struct list_head *); 122 extern struct request *elv_former_request(struct request_queue *, struct request *); 123 extern struct request *elv_latter_request(struct request_queue *, struct request *); 124 void elevator_init_mq(struct request_queue *q); 125 126 /* 127 * io scheduler registration 128 */ 129 extern int elv_register(struct elevator_type *); 130 extern void elv_unregister(struct elevator_type *); 131 132 /* 133 * io scheduler sysfs switching 134 */ 135 extern ssize_t elv_iosched_show(struct request_queue *, char *); 136 extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t); 137 138 extern bool elv_bio_merge_ok(struct request *, struct bio *); 139 extern struct elevator_queue *elevator_alloc(struct request_queue *, 140 struct elevator_type *); 141 142 /* 143 * Helper functions. 144 */ 145 extern struct request *elv_rb_former_request(struct request_queue *, struct request *); 146 extern struct request *elv_rb_latter_request(struct request_queue *, struct request *); 147 148 /* 149 * rb support functions. 150 */ 151 extern void elv_rb_add(struct rb_root *, struct request *); 152 extern void elv_rb_del(struct rb_root *, struct request *); 153 extern struct request *elv_rb_find(struct rb_root *, sector_t); 154 155 /* 156 * Insertion selection 157 */ 158 #define ELEVATOR_INSERT_FRONT 1 159 #define ELEVATOR_INSERT_BACK 2 160 #define ELEVATOR_INSERT_SORT 3 161 #define ELEVATOR_INSERT_REQUEUE 4 162 #define ELEVATOR_INSERT_FLUSH 5 163 #define ELEVATOR_INSERT_SORT_MERGE 6 164 165 #define rq_end_sector(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq)) 166 #define rb_entry_rq(node) rb_entry((node), struct request, rb_node) 167 168 #define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist) 169 #define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist) 170 171 /* 172 * Elevator features. 173 */ 174 175 /* Supports zoned block devices sequential write constraint */ 176 #define ELEVATOR_F_ZBD_SEQ_WRITE (1U << 0) 177 /* Supports scheduling on multiple hardware queues */ 178 #define ELEVATOR_F_MQ_AWARE (1U << 1) 179 180 #endif /* CONFIG_BLOCK */ 181 #endif 182