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
10struct io_cq;
11struct elevator_type;
12#ifdef CONFIG_BLK_DEBUG_FS
13struct blk_mq_debugfs_attr;
14#endif
15
16/*
17 * Return values from elevator merger
18 */
19enum elv_merge {
20 ELEVATOR_NO_MERGE = 0,
21 ELEVATOR_FRONT_MERGE = 1,
22 ELEVATOR_BACK_MERGE = 2,
23 ELEVATOR_DISCARD_MERGE = 3,
24};
25
26struct blk_mq_alloc_data;
27struct blk_mq_hw_ctx;
28
29struct 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
35 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
36 bool (*bio_merge)(struct blk_mq_hw_ctx *, struct bio *);
37 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
38 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
39 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
40 void (*limit_depth)(unsigned int, struct blk_mq_alloc_data *);
41 void (*prepare_request)(struct request *, struct bio *bio);
42 void (*finish_request)(struct request *);
43 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
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 (*started_request)(struct request *);
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
57struct 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 */
66struct 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 char elevator_name[ELV_NAME_MAX];
78 const char *elevator_alias;
79 struct module *elevator_owner;
80#ifdef CONFIG_BLK_DEBUG_FS
81 const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
82 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
83#endif
84
85 /* managed by elevator core */
86 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */
87 struct list_head list;
88};
89
90#define ELV_HASH_BITS 6
91
92void elv_rqhash_del(struct request_queue *q, struct request *rq);
93void elv_rqhash_add(struct request_queue *q, struct request *rq);
94void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
95struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
96
97/*
98 * each queue has an elevator_queue associated with it
99 */
100struct elevator_queue
101{
102 struct elevator_type *type;
103 void *elevator_data;
104 struct kobject kobj;
105 struct mutex sysfs_lock;
106 unsigned int registered:1;
107 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
108};
109
110/*
111 * block elevator interface
112 */
113extern enum elv_merge elv_merge(struct request_queue *, struct request **,
114 struct bio *);
115extern void elv_merge_requests(struct request_queue *, struct request *,
116 struct request *);
117extern void elv_merged_request(struct request_queue *, struct request *,
118 enum elv_merge);
119extern bool elv_attempt_insert_merge(struct request_queue *, struct request *);
120extern struct request *elv_former_request(struct request_queue *, struct request *);
121extern struct request *elv_latter_request(struct request_queue *, struct request *);
122
123/*
124 * io scheduler registration
125 */
126extern int elv_register(struct elevator_type *);
127extern void elv_unregister(struct elevator_type *);
128
129/*
130 * io scheduler sysfs switching
131 */
132extern ssize_t elv_iosched_show(struct request_queue *, char *);
133extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
134
135extern bool elv_bio_merge_ok(struct request *, struct bio *);
136extern struct elevator_queue *elevator_alloc(struct request_queue *,
137 struct elevator_type *);
138
139/*
140 * Helper functions.
141 */
142extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
143extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
144
145/*
146 * rb support functions.
147 */
148extern void elv_rb_add(struct rb_root *, struct request *);
149extern void elv_rb_del(struct rb_root *, struct request *);
150extern struct request *elv_rb_find(struct rb_root *, sector_t);
151
152/*
153 * Insertion selection
154 */
155#define ELEVATOR_INSERT_FRONT 1
156#define ELEVATOR_INSERT_BACK 2
157#define ELEVATOR_INSERT_SORT 3
158#define ELEVATOR_INSERT_REQUEUE 4
159#define ELEVATOR_INSERT_FLUSH 5
160#define ELEVATOR_INSERT_SORT_MERGE 6
161
162/*
163 * return values from elevator_may_queue_fn
164 */
165enum {
166 ELV_MQUEUE_MAY,
167 ELV_MQUEUE_NO,
168 ELV_MQUEUE_MUST,
169};
170
171#define rq_end_sector(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
172#define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
173
174#define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
175#define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
176
177#endif /* CONFIG_BLOCK */
178#endif
179