xref: /openbmc/linux/block/elevator.h (revision 3bf90eca)
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 
8 struct io_cq;
9 struct elevator_type;
10 struct blk_mq_debugfs_attr;
11 
12 /*
13  * Return values from elevator merger
14  */
15 enum elv_merge {
16 	ELEVATOR_NO_MERGE	= 0,
17 	ELEVATOR_FRONT_MERGE	= 1,
18 	ELEVATOR_BACK_MERGE	= 2,
19 	ELEVATOR_DISCARD_MERGE	= 3,
20 };
21 
22 struct blk_mq_alloc_data;
23 struct blk_mq_hw_ctx;
24 
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 *);
31 
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 *);
49 };
50 
51 #define ELV_NAME_MAX	(16)
52 
53 struct elv_fs_entry {
54 	struct attribute attr;
55 	ssize_t (*show)(struct elevator_queue *, char *);
56 	ssize_t (*store)(struct elevator_queue *, const char *, size_t);
57 };
58 
59 /*
60  * identifies an elevator type, such as AS or deadline
61  */
62 struct elevator_type
63 {
64 	/* managed by elevator core */
65 	struct kmem_cache *icq_cache;
66 
67 	/* fields provided by elevator implementation */
68 	struct elevator_mq_ops ops;
69 
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;
80 #endif
81 
82 	/* managed by elevator core */
83 	char icq_cache_name[ELV_NAME_MAX + 6];	/* elvname + "_io_cq" */
84 	struct list_head list;
85 };
86 
87 static inline bool elevator_tryget(struct elevator_type *e)
88 {
89 	return try_module_get(e->elevator_owner);
90 }
91 
92 static inline void __elevator_get(struct elevator_type *e)
93 {
94 	__module_get(e->elevator_owner);
95 }
96 
97 static inline void elevator_put(struct elevator_type *e)
98 {
99 	module_put(e->elevator_owner);
100 }
101 
102 #define ELV_HASH_BITS 6
103 
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);
108 
109 /*
110  * each queue has an elevator_queue associated with it
111  */
112 struct elevator_queue
113 {
114 	struct elevator_type *type;
115 	void *elevator_data;
116 	struct kobject kobj;
117 	struct mutex sysfs_lock;
118 	unsigned long flags;
119 	DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
120 };
121 
122 #define ELEVATOR_FLAG_REGISTERED	0
123 #define ELEVATOR_FLAG_DISABLE_WBT	1
124 
125 /*
126  * block elevator interface
127  */
128 extern enum elv_merge elv_merge(struct request_queue *, struct request **,
129 		struct bio *);
130 extern void elv_merge_requests(struct request_queue *, struct request *,
131 			       struct request *);
132 extern void elv_merged_request(struct request_queue *, struct request *,
133 		enum elv_merge);
134 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *,
135 				     struct list_head *);
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);
139 
140 /*
141  * io scheduler registration
142  */
143 extern int elv_register(struct elevator_type *);
144 extern void elv_unregister(struct elevator_type *);
145 
146 /*
147  * io scheduler sysfs switching
148  */
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);
151 
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 *);
155 
156 /*
157  * Helper functions.
158  */
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 *);
161 
162 /*
163  * rb support functions.
164  */
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);
168 
169 /*
170  * Insertion selection
171  */
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
178 
179 #define rb_entry_rq(node)	rb_entry((node), struct request, rb_node)
180 
181 #define rq_entry_fifo(ptr)	list_entry((ptr), struct request, queuelist)
182 #define rq_fifo_clear(rq)	list_del_init(&(rq)->queuelist)
183 
184 #endif /* _ELEVATOR_H */
185