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