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