1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/percpu.h>
6 #include <linux/hashtable.h>
10 struct blk_mq_debugfs_attr;
13 * Return values from elevator merger
16 ELEVATOR_NO_MERGE = 0,
17 ELEVATOR_FRONT_MERGE = 1,
18 ELEVATOR_BACK_MERGE = 2,
19 ELEVATOR_DISCARD_MERGE = 3,
22 struct blk_mq_alloc_data;
25 struct elevator_mq_ops {
26 int (*init_sched)(struct request_queue *, struct elevator_type *);
27 void (*exit_sched)(struct elevator_queue *);
28 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
29 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
30 void (*depth_updated)(struct blk_mq_hw_ctx *);
32 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
33 bool (*bio_merge)(struct request_queue *, struct bio *, unsigned int);
34 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
35 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
36 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
37 void (*limit_depth)(blk_opf_t, struct blk_mq_alloc_data *);
38 void (*prepare_request)(struct request *);
39 void (*finish_request)(struct request *);
40 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
41 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
42 bool (*has_work)(struct blk_mq_hw_ctx *);
43 void (*completed_request)(struct request *, u64);
44 void (*requeue_request)(struct request *);
45 struct request *(*former_request)(struct request_queue *, struct request *);
46 struct request *(*next_request)(struct request_queue *, struct request *);
47 void (*init_icq)(struct io_cq *);
48 void (*exit_icq)(struct io_cq *);
51 #define ELV_NAME_MAX (16)
54 struct attribute attr;
55 ssize_t (*show)(struct elevator_queue *, char *);
56 ssize_t (*store)(struct elevator_queue *, const char *, size_t);
60 * identifies an elevator type, such as AS or deadline
64 /* managed by elevator core */
65 struct kmem_cache *icq_cache;
67 /* fields provided by elevator implementation */
68 struct elevator_mq_ops ops;
70 size_t icq_size; /* see iocontext.h */
71 size_t icq_align; /* ditto */
72 struct elv_fs_entry *elevator_attrs;
73 const char *elevator_name;
74 const char *elevator_alias;
75 const unsigned int elevator_features;
76 struct module *elevator_owner;
77 #ifdef CONFIG_BLK_DEBUG_FS
78 const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
79 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
82 /* managed by elevator core */
83 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */
84 struct list_head list;
87 static inline bool elevator_tryget(struct elevator_type *e)
89 return try_module_get(e->elevator_owner);
92 static inline void __elevator_get(struct elevator_type *e)
94 __module_get(e->elevator_owner);
97 static inline void elevator_put(struct elevator_type *e)
99 module_put(e->elevator_owner);
102 #define ELV_HASH_BITS 6
104 void elv_rqhash_del(struct request_queue *q, struct request *rq);
105 void elv_rqhash_add(struct request_queue *q, struct request *rq);
106 void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
107 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
110 * each queue has an elevator_queue associated with it
112 struct elevator_queue
114 struct elevator_type *type;
117 struct mutex sysfs_lock;
119 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
122 #define ELEVATOR_FLAG_REGISTERED 0
123 #define ELEVATOR_FLAG_DISABLE_WBT 1
126 * block elevator interface
128 extern enum elv_merge elv_merge(struct request_queue *, struct request **,
130 extern void elv_merge_requests(struct request_queue *, struct request *,
132 extern void elv_merged_request(struct request_queue *, struct request *,
134 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *,
136 extern struct request *elv_former_request(struct request_queue *, struct request *);
137 extern struct request *elv_latter_request(struct request_queue *, struct request *);
138 void elevator_init_mq(struct request_queue *q);
141 * io scheduler registration
143 extern int elv_register(struct elevator_type *);
144 extern void elv_unregister(struct elevator_type *);
147 * io scheduler sysfs switching
149 extern ssize_t elv_iosched_show(struct request_queue *, char *);
150 extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
152 extern bool elv_bio_merge_ok(struct request *, struct bio *);
153 extern struct elevator_queue *elevator_alloc(struct request_queue *,
154 struct elevator_type *);
159 extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
160 extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
163 * rb support functions.
165 extern void elv_rb_add(struct rb_root *, struct request *);
166 extern void elv_rb_del(struct rb_root *, struct request *);
167 extern struct request *elv_rb_find(struct rb_root *, sector_t);
170 * Insertion selection
172 #define ELEVATOR_INSERT_FRONT 1
173 #define ELEVATOR_INSERT_BACK 2
174 #define ELEVATOR_INSERT_SORT 3
175 #define ELEVATOR_INSERT_REQUEUE 4
176 #define ELEVATOR_INSERT_FLUSH 5
177 #define ELEVATOR_INSERT_SORT_MERGE 6
179 #define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
181 #define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
182 #define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
184 #endif /* _ELEVATOR_H */