]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/blk-mq.h
blk-mq: make ->flush_rq fully transparent to drivers
[mirror_ubuntu-artful-kernel.git] / include / linux / blk-mq.h
CommitLineData
320ae51f
JA
1#ifndef BLK_MQ_H
2#define BLK_MQ_H
3
4#include <linux/blkdev.h>
5
6struct blk_mq_tags;
7
8struct blk_mq_cpu_notifier {
9 struct list_head list;
10 void *data;
11 void (*notify)(void *data, unsigned long action, unsigned int cpu);
12};
13
14struct blk_mq_hw_ctx {
15 struct {
16 spinlock_t lock;
17 struct list_head dispatch;
18 } ____cacheline_aligned_in_smp;
19
20 unsigned long state; /* BLK_MQ_S_* flags */
21 struct delayed_work delayed_work;
e4043dcf 22 cpumask_var_t cpumask;
320ae51f
JA
23
24 unsigned long flags; /* BLK_MQ_F_* flags */
25
26 struct request_queue *queue;
27 unsigned int queue_num;
28
29 void *driver_data;
30
31 unsigned int nr_ctx;
32 struct blk_mq_ctx **ctxs;
33 unsigned int nr_ctx_map;
34 unsigned long *ctx_map;
35
36 struct request **rqs;
37 struct list_head page_list;
38 struct blk_mq_tags *tags;
39
40 unsigned long queued;
41 unsigned long run;
42#define BLK_MQ_MAX_DISPATCH_ORDER 10
43 unsigned long dispatched[BLK_MQ_MAX_DISPATCH_ORDER];
44
45 unsigned int queue_depth;
46 unsigned int numa_node;
47 unsigned int cmd_size; /* per-request extra data */
48
49 struct blk_mq_cpu_notifier cpu_notifier;
50 struct kobject kobj;
51};
52
53struct blk_mq_reg {
54 struct blk_mq_ops *ops;
55 unsigned int nr_hw_queues;
56 unsigned int queue_depth;
57 unsigned int reserved_tags;
58 unsigned int cmd_size; /* per-request extra data */
59 int numa_node;
60 unsigned int timeout;
61 unsigned int flags; /* BLK_MQ_F_* */
62};
63
64typedef int (queue_rq_fn)(struct blk_mq_hw_ctx *, struct request *);
65typedef struct blk_mq_hw_ctx *(map_queue_fn)(struct request_queue *, const int);
66typedef struct blk_mq_hw_ctx *(alloc_hctx_fn)(struct blk_mq_reg *,unsigned int);
67typedef void (free_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
68typedef int (init_hctx_fn)(struct blk_mq_hw_ctx *, void *, unsigned int);
69typedef void (exit_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
70
71struct blk_mq_ops {
72 /*
73 * Queue request
74 */
75 queue_rq_fn *queue_rq;
76
77 /*
78 * Map to specific hardware queue
79 */
80 map_queue_fn *map_queue;
81
82 /*
83 * Called on request timeout
84 */
85 rq_timed_out_fn *timeout;
86
30a91cb4
CH
87 softirq_done_fn *complete;
88
320ae51f
JA
89 /*
90 * Override for hctx allocations (should probably go)
91 */
92 alloc_hctx_fn *alloc_hctx;
93 free_hctx_fn *free_hctx;
94
95 /*
96 * Called when the block layer side of a hardware queue has been
97 * set up, allowing the driver to allocate/init matching structures.
98 * Ditto for exit/teardown.
99 */
100 init_hctx_fn *init_hctx;
101 exit_hctx_fn *exit_hctx;
102};
103
104enum {
105 BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
106 BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
107 BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
108
109 BLK_MQ_F_SHOULD_MERGE = 1 << 0,
110 BLK_MQ_F_SHOULD_SORT = 1 << 1,
111 BLK_MQ_F_SHOULD_IPI = 1 << 2,
112
5d12f905 113 BLK_MQ_S_STOPPED = 0,
320ae51f
JA
114
115 BLK_MQ_MAX_DEPTH = 2048,
116};
117
118struct request_queue *blk_mq_init_queue(struct blk_mq_reg *, void *);
320ae51f
JA
119int blk_mq_register_disk(struct gendisk *);
120void blk_mq_unregister_disk(struct gendisk *);
95363efd
JA
121int blk_mq_init_commands(struct request_queue *, int (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
122void blk_mq_free_commands(struct request_queue *, void (*free)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
320ae51f
JA
123
124void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
125
feb71dae 126void blk_mq_insert_request(struct request *, bool, bool, bool);
320ae51f
JA
127void blk_mq_run_queues(struct request_queue *q, bool async);
128void blk_mq_free_request(struct request *rq);
129bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
18741986 130struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp);
320ae51f
JA
131struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, gfp_t gfp);
132struct request *blk_mq_rq_from_tag(struct request_queue *q, unsigned int tag);
133
134struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
135struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *, unsigned int);
136void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *, unsigned int);
137
d6a25b31
CH
138bool blk_mq_end_io_partial(struct request *rq, int error,
139 unsigned int nr_bytes);
140static inline void blk_mq_end_io(struct request *rq, int error)
141{
142 bool done = !blk_mq_end_io_partial(rq, error, blk_rq_bytes(rq));
143 BUG_ON(!done);
144}
320ae51f 145
30a91cb4
CH
146void blk_mq_complete_request(struct request *rq);
147
320ae51f
JA
148void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
149void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
280d45f6 150void blk_mq_stop_hw_queues(struct request_queue *q);
320ae51f
JA
151void blk_mq_start_stopped_hw_queues(struct request_queue *q);
152
153/*
154 * Driver command data is immediately after the request. So subtract request
155 * size to get back to the original request.
156 */
157static inline struct request *blk_mq_rq_from_pdu(void *pdu)
158{
159 return pdu - sizeof(struct request);
160}
161static inline void *blk_mq_rq_to_pdu(struct request *rq)
162{
163 return (void *) rq + sizeof(*rq);
164}
165
166static inline struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx,
167 unsigned int tag)
168{
169 return hctx->rqs[tag];
170}
171
172#define queue_for_each_hw_ctx(q, hctx, i) \
0d0b7d42
JA
173 for ((i) = 0; (i) < (q)->nr_hw_queues && \
174 ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
320ae51f
JA
175
176#define queue_for_each_ctx(q, ctx, i) \
0d0b7d42
JA
177 for ((i) = 0; (i) < (q)->nr_queues && \
178 ({ ctx = per_cpu_ptr((q)->queue_ctx, (i)); 1; }); (i)++)
320ae51f
JA
179
180#define hctx_for_each_ctx(hctx, ctx, i) \
0d0b7d42
JA
181 for ((i) = 0; (i) < (hctx)->nr_ctx && \
182 ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
320ae51f
JA
183
184#define blk_ctx_sum(q, sum) \
185({ \
186 struct blk_mq_ctx *__x; \
187 unsigned int __ret = 0, __i; \
188 \
189 queue_for_each_ctx((q), __x, __i) \
190 __ret += sum; \
191 __ret; \
192})
193
194#endif