blob: 16cd8bdedb7eac633636e4325c91ddb552a5632a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Christoph Hellwig2e9bc342021-09-20 14:33:23 +02002#ifndef _ELEVATOR_H
3#define _ELEVATOR_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
Jens Axboe4a893e82006-07-22 15:37:43 +02005#include <linux/percpu.h>
Sasha Levin242d98f2012-12-17 10:01:27 -05006#include <linux/hashtable.h>
Jens Axboe4a893e82006-07-22 15:37:43 +02007
Tejun Heo7e5a8792011-12-14 00:33:42 +01008struct io_cq;
Jianpeng Mad50235b2013-07-03 13:25:24 +02009struct elevator_type;
Omar Sandovald332ce02017-05-04 08:24:40 -060010struct blk_mq_debugfs_attr;
Tejun Heo7e5a8792011-12-14 00:33:42 +010011
Christoph Hellwig34fe7c02017-02-08 14:46:48 +010012/*
13 * Return values from elevator merger
14 */
15enum elv_merge {
16 ELEVATOR_NO_MERGE = 0,
17 ELEVATOR_FRONT_MERGE = 1,
18 ELEVATOR_BACK_MERGE = 2,
Christoph Hellwig1e739732017-02-08 14:46:49 +010019 ELEVATOR_DISCARD_MERGE = 3,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +010020};
21
Jens Axboebd166ef2017-01-17 06:03:22 -070022struct blk_mq_alloc_data;
23struct blk_mq_hw_ctx;
24
25struct elevator_mq_ops {
26 int (*init_sched)(struct request_queue *, struct elevator_type *);
27 void (*exit_sched)(struct elevator_queue *);
Omar Sandovalee056f92017-04-05 12:01:34 -070028 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
29 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
Jens Axboe77f1e0a2019-01-18 10:34:16 -070030 void (*depth_updated)(struct blk_mq_hw_ctx *);
Jens Axboebd166ef2017-01-17 06:03:22 -070031
32 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
Omar Sandovalefed9a32021-05-10 17:05:35 -070033 bool (*bio_merge)(struct request_queue *, struct bio *, unsigned int);
Jens Axboebd166ef2017-01-17 06:03:22 -070034 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
Christoph Hellwig34fe7c02017-02-08 14:46:48 +010035 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
Jens Axboebd166ef2017-01-17 06:03:22 -070036 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +020037 void (*limit_depth)(unsigned int, struct blk_mq_alloc_data *);
Christoph Hellwig5d9c3052020-05-29 15:53:08 +020038 void (*prepare_request)(struct request *);
Christoph Hellwig7b9e9362017-06-16 18:15:21 +020039 void (*finish_request)(struct request *);
Jens Axboebd166ef2017-01-17 06:03:22 -070040 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
Jens Axboec13660a2017-01-26 12:40:07 -070041 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
Jens Axboebd166ef2017-01-17 06:03:22 -070042 bool (*has_work)(struct blk_mq_hw_ctx *);
Omar Sandovaled886602018-09-27 15:55:51 -070043 void (*completed_request)(struct request *, u64);
Jens Axboebd166ef2017-01-17 06:03:22 -070044 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 *);
Jens Axboebd166ef2017-01-17 06:03:22 -070047 void (*init_icq)(struct io_cq *);
48 void (*exit_icq)(struct io_cq *);
49};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ELV_NAME_MAX (16)
52
Al Viroe572ec72006-03-18 22:27:18 -050053struct elv_fs_entry {
54 struct attribute attr;
Jens Axboeb374d182008-10-31 10:05:07 +010055 ssize_t (*show)(struct elevator_queue *, char *);
56 ssize_t (*store)(struct elevator_queue *, const char *, size_t);
Al Viroe572ec72006-03-18 22:27:18 -050057};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * identifies an elevator type, such as AS or deadline
61 */
62struct elevator_type
63{
Tejun Heo3d3c2372011-12-14 00:33:42 +010064 /* managed by elevator core */
65 struct kmem_cache *icq_cache;
66
67 /* fields provided by elevator implementation */
Jens Axboef9cd4bf2018-11-01 16:41:41 -060068 struct elevator_mq_ops ops;
Jens Axboea1ce35f2018-10-29 10:23:51 -060069
Tejun Heof1f8cc92011-12-14 00:33:42 +010070 size_t icq_size; /* see iocontext.h */
71 size_t icq_align; /* ditto */
Al Viroe572ec72006-03-18 22:27:18 -050072 struct elv_fs_entry *elevator_attrs;
Damien Le Moal9305d5d2019-07-11 00:57:41 +090073 const char *elevator_name;
Jens Axboe8ac0d9a2017-10-25 12:35:02 -060074 const char *elevator_alias;
Damien Le Moal68c43f12019-09-05 18:51:31 +090075 const unsigned int elevator_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct module *elevator_owner;
Omar Sandovald332ce02017-05-04 08:24:40 -060077#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
Tejun Heo3d3c2372011-12-14 00:33:42 +010081
82 /* managed by elevator core */
Eric Biggers9bd2bbc2017-06-02 20:35:51 -070083 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */
Tejun Heo22f746e2011-12-14 00:33:41 +010084 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Sasha Levin242d98f2012-12-17 10:01:27 -050087#define ELV_HASH_BITS 6
88
Jens Axboe70b3ea02016-12-07 08:43:31 -070089void elv_rqhash_del(struct request_queue *q, struct request *rq);
90void elv_rqhash_add(struct request_queue *q, struct request *rq);
91void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
92struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Adrian Bunk54069582006-01-10 00:09:36 +010095 * each queue has an elevator_queue associated with it
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97struct elevator_queue
98{
Tejun Heo22f746e2011-12-14 00:33:41 +010099 struct elevator_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 void *elevator_data;
101 struct kobject kobj;
Al Viro3d1ab402006-03-18 18:35:43 -0500102 struct mutex sysfs_lock;
Jens Axboe430c62f2010-10-07 09:35:16 +0200103 unsigned int registered:1;
Sasha Levin242d98f2012-12-17 10:01:27 -0500104 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
107/*
108 * block elevator interface
109 */
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100110extern enum elv_merge elv_merge(struct request_queue *, struct request **,
111 struct bio *);
Jens Axboe165125e2007-07-24 09:28:11 +0200112extern void elv_merge_requests(struct request_queue *, struct request *,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct request *);
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100114extern void elv_merged_request(struct request_queue *, struct request *,
115 enum elv_merge);
Jan Karafd2ef392021-06-23 11:36:34 +0200116extern bool elv_attempt_insert_merge(struct request_queue *, struct request *,
117 struct list_head *);
Jens Axboe165125e2007-07-24 09:28:11 +0200118extern struct request *elv_former_request(struct request_queue *, struct request *);
119extern struct request *elv_latter_request(struct request_queue *, struct request *);
Christoph Hellwig26a97502021-06-02 09:53:17 +0300120void elevator_init_mq(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * io scheduler registration
124 */
Jens Axboee567bf72014-06-22 16:32:48 -0600125extern int elv_register(struct elevator_type *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126extern void elv_unregister(struct elevator_type *);
127
128/*
129 * io scheduler sysfs switching
130 */
Jens Axboe165125e2007-07-24 09:28:11 +0200131extern ssize_t elv_iosched_show(struct request_queue *, char *);
132extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700134extern bool elv_bio_merge_ok(struct request *, struct bio *);
Jianpeng Mad50235b2013-07-03 13:25:24 +0200135extern struct elevator_queue *elevator_alloc(struct request_queue *,
136 struct elevator_type *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200139 * Helper functions.
140 */
Jens Axboe165125e2007-07-24 09:28:11 +0200141extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
142extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
Jens Axboe2e662b62006-07-13 11:55:04 +0200143
144/*
145 * rb support functions.
146 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200147extern void elv_rb_add(struct rb_root *, struct request *);
Jens Axboe2e662b62006-07-13 11:55:04 +0200148extern void elv_rb_del(struct rb_root *, struct request *);
149extern struct request *elv_rb_find(struct rb_root *, sector_t);
150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Insertion selection
153 */
154#define ELEVATOR_INSERT_FRONT 1
155#define ELEVATOR_INSERT_BACK 2
156#define ELEVATOR_INSERT_SORT 3
Tejun Heo797e7db2006-01-06 09:51:03 +0100157#define ELEVATOR_INSERT_REQUEUE 4
Tejun Heoae1b1532011-01-25 12:43:54 +0100158#define ELEVATOR_INSERT_FLUSH 5
Jens Axboe5e84ea32011-03-21 10:14:27 +0100159#define ELEVATOR_INSERT_SORT_MERGE 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jens Axboe2e662b62006-07-13 11:55:04 +0200161#define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
Jens Axboe1b47f532005-10-20 16:37:00 +0200162
Jens Axboe1fbfdfc2006-07-11 21:49:15 +0200163#define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Frederic Weisbeckerd9a74df2014-02-24 16:39:53 +0100164#define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
Jens Axboe1fbfdfc2006-07-11 21:49:15 +0200165
Christoph Hellwig2e9bc342021-09-20 14:33:23 +0200166#endif /* _ELEVATOR_H */