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