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