Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / include / linux / blk-mq.h
blobab0e9b2025b36d401443f213a646fb68fe392605
1 #ifndef BLK_MQ_H
2 #define BLK_MQ_H
4 #include <linux/blkdev.h>
6 struct blk_mq_tags;
8 struct blk_mq_cpu_notifier {
9 struct list_head list;
10 void *data;
11 void (*notify)(void *data, unsigned long action, unsigned int cpu);
14 struct blk_mq_hw_ctx {
15 struct {
16 spinlock_t lock;
17 struct list_head dispatch;
18 } ____cacheline_aligned_in_smp;
20 unsigned long state; /* BLK_MQ_S_* flags */
21 struct delayed_work delayed_work;
23 unsigned long flags; /* BLK_MQ_F_* flags */
25 struct request_queue *queue;
26 unsigned int queue_num;
28 void *driver_data;
30 unsigned int nr_ctx;
31 struct blk_mq_ctx **ctxs;
32 unsigned int nr_ctx_map;
33 unsigned long *ctx_map;
35 struct request **rqs;
36 struct list_head page_list;
37 struct blk_mq_tags *tags;
39 unsigned long queued;
40 unsigned long run;
41 #define BLK_MQ_MAX_DISPATCH_ORDER 10
42 unsigned long dispatched[BLK_MQ_MAX_DISPATCH_ORDER];
44 unsigned int queue_depth;
45 unsigned int numa_node;
46 unsigned int cmd_size; /* per-request extra data */
48 struct blk_mq_cpu_notifier cpu_notifier;
49 struct kobject kobj;
52 struct blk_mq_reg {
53 struct blk_mq_ops *ops;
54 unsigned int nr_hw_queues;
55 unsigned int queue_depth;
56 unsigned int reserved_tags;
57 unsigned int cmd_size; /* per-request extra data */
58 int numa_node;
59 unsigned int timeout;
60 unsigned int flags; /* BLK_MQ_F_* */
63 typedef int (queue_rq_fn)(struct blk_mq_hw_ctx *, struct request *);
64 typedef struct blk_mq_hw_ctx *(map_queue_fn)(struct request_queue *, const int);
65 typedef struct blk_mq_hw_ctx *(alloc_hctx_fn)(struct blk_mq_reg *,unsigned int);
66 typedef void (free_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
67 typedef int (init_hctx_fn)(struct blk_mq_hw_ctx *, void *, unsigned int);
68 typedef void (exit_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
70 struct blk_mq_ops {
72 * Queue request
74 queue_rq_fn *queue_rq;
77 * Map to specific hardware queue
79 map_queue_fn *map_queue;
82 * Called on request timeout
84 rq_timed_out_fn *timeout;
87 * Override for hctx allocations (should probably go)
89 alloc_hctx_fn *alloc_hctx;
90 free_hctx_fn *free_hctx;
93 * Called when the block layer side of a hardware queue has been
94 * set up, allowing the driver to allocate/init matching structures.
95 * Ditto for exit/teardown.
97 init_hctx_fn *init_hctx;
98 exit_hctx_fn *exit_hctx;
101 enum {
102 BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
103 BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
104 BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
106 BLK_MQ_F_SHOULD_MERGE = 1 << 0,
107 BLK_MQ_F_SHOULD_SORT = 1 << 1,
108 BLK_MQ_F_SHOULD_IPI = 1 << 2,
110 BLK_MQ_S_STOPPED = 1 << 0,
112 BLK_MQ_MAX_DEPTH = 2048,
115 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *, void *);
116 void blk_mq_free_queue(struct request_queue *);
117 int blk_mq_register_disk(struct gendisk *);
118 void blk_mq_unregister_disk(struct gendisk *);
119 void blk_mq_init_commands(struct request_queue *, void (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
121 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
123 void blk_mq_insert_request(struct request_queue *, struct request *, bool);
124 void blk_mq_run_queues(struct request_queue *q, bool async);
125 void blk_mq_free_request(struct request *rq);
126 bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
127 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp, bool reserved);
128 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, gfp_t gfp);
129 struct request *blk_mq_rq_from_tag(struct request_queue *q, unsigned int tag);
131 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
132 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *, unsigned int);
133 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *, unsigned int);
135 void blk_mq_end_io(struct request *rq, int error);
137 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
138 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
139 void blk_mq_stop_hw_queues(struct request_queue *q);
140 void blk_mq_start_stopped_hw_queues(struct request_queue *q);
143 * Driver command data is immediately after the request. So subtract request
144 * size to get back to the original request.
146 static inline struct request *blk_mq_rq_from_pdu(void *pdu)
148 return pdu - sizeof(struct request);
150 static inline void *blk_mq_rq_to_pdu(struct request *rq)
152 return (void *) rq + sizeof(*rq);
155 static inline struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx,
156 unsigned int tag)
158 return hctx->rqs[tag];
161 #define queue_for_each_hw_ctx(q, hctx, i) \
162 for ((i) = 0, hctx = (q)->queue_hw_ctx[0]; \
163 (i) < (q)->nr_hw_queues; (i)++, hctx = (q)->queue_hw_ctx[i])
165 #define queue_for_each_ctx(q, ctx, i) \
166 for ((i) = 0, ctx = per_cpu_ptr((q)->queue_ctx, 0); \
167 (i) < (q)->nr_queues; (i)++, ctx = per_cpu_ptr(q->queue_ctx, (i)))
169 #define hctx_for_each_ctx(hctx, ctx, i) \
170 for ((i) = 0, ctx = (hctx)->ctxs[0]; \
171 (i) < (hctx)->nr_ctx; (i)++, ctx = (hctx)->ctxs[(i)])
173 #define blk_ctx_sum(q, sum) \
174 ({ \
175 struct blk_mq_ctx *__x; \
176 unsigned int __ret = 0, __i; \
178 queue_for_each_ctx((q), __x, __i) \
179 __ret += sum; \
180 __ret; \
183 #endif