1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/percpu.h>
6 #include <linux/hashtable.h>
11 struct blk_mq_debugfs_attr;
14 * Return values from elevator merger
17 ELEVATOR_NO_MERGE = 0,
18 ELEVATOR_FRONT_MERGE = 1,
19 ELEVATOR_BACK_MERGE = 2,
20 ELEVATOR_DISCARD_MERGE = 3,
23 struct blk_mq_alloc_data;
26 struct elevator_mq_ops {
27 int (*init_sched)(struct request_queue *, struct elevator_type *);
28 void (*exit_sched)(struct elevator_queue *);
29 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
30 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
31 void (*depth_updated)(struct blk_mq_hw_ctx *);
33 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
34 bool (*bio_merge)(struct request_queue *, struct bio *, unsigned int);
35 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
36 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
37 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
38 void (*limit_depth)(blk_opf_t, struct blk_mq_alloc_data *);
39 void (*prepare_request)(struct request *);
40 void (*finish_request)(struct request *);
41 void (*insert_requests)(struct blk_mq_hw_ctx *hctx, struct list_head *list,
43 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
44 bool (*has_work)(struct blk_mq_hw_ctx *);
45 void (*completed_request)(struct request *, u64);
46 void (*requeue_request)(struct request *);
47 struct request *(*former_request)(struct request_queue *, struct request *);
48 struct request *(*next_request)(struct request_queue *, struct request *);
49 void (*init_icq)(struct io_cq *);
50 void (*exit_icq)(struct io_cq *);
53 #define ELV_NAME_MAX (16)
56 struct attribute attr;
57 ssize_t (*show)(struct elevator_queue *, char *);
58 ssize_t (*store)(struct elevator_queue *, const char *, size_t);
62 * identifies an elevator type, such as AS or deadline
66 /* managed by elevator core */
67 struct kmem_cache *icq_cache;
69 /* fields provided by elevator implementation */
70 struct elevator_mq_ops ops;
72 size_t icq_size; /* see iocontext.h */
73 size_t icq_align; /* ditto */
74 struct elv_fs_entry *elevator_attrs;
75 const char *elevator_name;
76 const char *elevator_alias;
77 const unsigned int elevator_features;
78 struct module *elevator_owner;
79 #ifdef CONFIG_BLK_DEBUG_FS
80 const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
81 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
84 /* managed by elevator core */
85 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */
86 struct list_head list;
89 static inline bool elevator_tryget(struct elevator_type *e)
91 return try_module_get(e->elevator_owner);
94 static inline void __elevator_get(struct elevator_type *e)
96 __module_get(e->elevator_owner);
99 static inline void elevator_put(struct elevator_type *e)
101 module_put(e->elevator_owner);
104 #define ELV_HASH_BITS 6
106 void elv_rqhash_del(struct request_queue *q, struct request *rq);
107 void elv_rqhash_add(struct request_queue *q, struct request *rq);
108 void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
109 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
112 * each queue has an elevator_queue associated with it
114 struct elevator_queue
116 struct elevator_type *type;
119 struct mutex sysfs_lock;
121 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
124 #define ELEVATOR_FLAG_REGISTERED 0
125 #define ELEVATOR_FLAG_DISABLE_WBT 1
128 * block elevator interface
130 extern enum elv_merge elv_merge(struct request_queue *, struct request **,
132 extern void elv_merge_requests(struct request_queue *, struct request *,
134 extern void elv_merged_request(struct request_queue *, struct request *,
136 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *,
138 extern struct request *elv_former_request(struct request_queue *, struct request *);
139 extern struct request *elv_latter_request(struct request_queue *, struct request *);
140 void elevator_init_mq(struct request_queue *q);
143 * io scheduler registration
145 extern int elv_register(struct elevator_type *);
146 extern void elv_unregister(struct elevator_type *);
149 * io scheduler sysfs switching
151 extern ssize_t elv_iosched_show(struct request_queue *, char *);
152 extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
154 extern bool elv_bio_merge_ok(struct request *, struct bio *);
155 extern struct elevator_queue *elevator_alloc(struct request_queue *,
156 struct elevator_type *);
161 extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
162 extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
165 * rb support functions.
167 extern void elv_rb_add(struct rb_root *, struct request *);
168 extern void elv_rb_del(struct rb_root *, struct request *);
169 extern struct request *elv_rb_find(struct rb_root *, sector_t);
172 * Insertion selection
174 #define ELEVATOR_INSERT_FRONT 1
175 #define ELEVATOR_INSERT_BACK 2
176 #define ELEVATOR_INSERT_SORT 3
177 #define ELEVATOR_INSERT_REQUEUE 4
178 #define ELEVATOR_INSERT_FLUSH 5
179 #define ELEVATOR_INSERT_SORT_MERGE 6
181 #define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
183 #define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
184 #define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
186 #endif /* _ELEVATOR_H */