blk-mq.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifndef BLK_MQ_H
  2. #define BLK_MQ_H
  3. #include <linux/blkdev.h>
  4. struct blk_mq_tags;
  5. struct blk_flush_queue;
  6. struct blk_mq_cpu_notifier {
  7. struct list_head list;
  8. void *data;
  9. int (*notify)(void *data, unsigned long action, unsigned int cpu);
  10. };
  11. struct blk_mq_ctxmap {
  12. unsigned int size;
  13. unsigned int bits_per_word;
  14. struct blk_align_bitmap *map;
  15. };
  16. struct blk_mq_hw_ctx {
  17. struct {
  18. spinlock_t lock;
  19. struct list_head dispatch;
  20. } ____cacheline_aligned_in_smp;
  21. unsigned long state; /* BLK_MQ_S_* flags */
  22. struct delayed_work run_work;
  23. struct delayed_work delay_work;
  24. cpumask_var_t cpumask;
  25. int next_cpu;
  26. int next_cpu_batch;
  27. unsigned long flags; /* BLK_MQ_F_* flags */
  28. struct request_queue *queue;
  29. struct blk_flush_queue *fq;
  30. void *driver_data;
  31. struct blk_mq_ctxmap ctx_map;
  32. unsigned int nr_ctx;
  33. struct blk_mq_ctx **ctxs;
  34. atomic_t wait_index;
  35. struct blk_mq_tags *tags;
  36. unsigned long queued;
  37. unsigned long run;
  38. #define BLK_MQ_MAX_DISPATCH_ORDER 10
  39. unsigned long dispatched[BLK_MQ_MAX_DISPATCH_ORDER];
  40. unsigned int numa_node;
  41. unsigned int queue_num;
  42. atomic_t nr_active;
  43. struct blk_mq_cpu_notifier cpu_notifier;
  44. struct kobject kobj;
  45. unsigned long poll_invoked;
  46. unsigned long poll_success;
  47. };
  48. struct blk_mq_tag_set {
  49. struct blk_mq_ops *ops;
  50. unsigned int nr_hw_queues;
  51. unsigned int queue_depth; /* max hw supported */
  52. unsigned int reserved_tags;
  53. unsigned int cmd_size; /* per-request extra data */
  54. int numa_node;
  55. unsigned int timeout;
  56. unsigned int flags; /* BLK_MQ_F_* */
  57. void *driver_data;
  58. struct blk_mq_tags **tags;
  59. struct mutex tag_list_lock;
  60. struct list_head tag_list;
  61. };
  62. struct blk_mq_queue_data {
  63. struct request *rq;
  64. struct list_head *list;
  65. bool last;
  66. };
  67. typedef int (queue_rq_fn)(struct blk_mq_hw_ctx *, const struct blk_mq_queue_data *);
  68. typedef struct blk_mq_hw_ctx *(map_queue_fn)(struct request_queue *, const int);
  69. typedef enum blk_eh_timer_return (timeout_fn)(struct request *, bool);
  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. typedef int (init_request_fn)(void *, struct request *, unsigned int,
  73. unsigned int, unsigned int);
  74. typedef void (exit_request_fn)(void *, struct request *, unsigned int,
  75. unsigned int);
  76. typedef void (busy_iter_fn)(struct blk_mq_hw_ctx *, struct request *, void *,
  77. bool);
  78. typedef void (busy_tag_iter_fn)(struct request *, void *, bool);
  79. typedef int (poll_fn)(struct blk_mq_hw_ctx *, unsigned int);
  80. struct blk_mq_ops {
  81. /*
  82. * Queue request
  83. */
  84. queue_rq_fn *queue_rq;
  85. /*
  86. * Map to specific hardware queue
  87. */
  88. map_queue_fn *map_queue;
  89. /*
  90. * Called on request timeout
  91. */
  92. timeout_fn *timeout;
  93. /*
  94. * Called to poll for completion of a specific tag.
  95. */
  96. poll_fn *poll;
  97. softirq_done_fn *complete;
  98. /*
  99. * Called when the block layer side of a hardware queue has been
  100. * set up, allowing the driver to allocate/init matching structures.
  101. * Ditto for exit/teardown.
  102. */
  103. init_hctx_fn *init_hctx;
  104. exit_hctx_fn *exit_hctx;
  105. /*
  106. * Called for every command allocated by the block layer to allow
  107. * the driver to set up driver specific data.
  108. *
  109. * Tag greater than or equal to queue_depth is for setting up
  110. * flush request.
  111. *
  112. * Ditto for exit/teardown.
  113. */
  114. init_request_fn *init_request;
  115. exit_request_fn *exit_request;
  116. };
  117. enum {
  118. BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
  119. BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
  120. BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
  121. BLK_MQ_F_SHOULD_MERGE = 1 << 0,
  122. BLK_MQ_F_TAG_SHARED = 1 << 1,
  123. BLK_MQ_F_SG_MERGE = 1 << 2,
  124. BLK_MQ_F_DEFER_ISSUE = 1 << 4,
  125. BLK_MQ_F_ALLOC_POLICY_START_BIT = 8,
  126. BLK_MQ_F_ALLOC_POLICY_BITS = 1,
  127. BLK_MQ_S_STOPPED = 0,
  128. BLK_MQ_S_TAG_ACTIVE = 1,
  129. BLK_MQ_MAX_DEPTH = 10240,
  130. BLK_MQ_CPU_WORK_BATCH = 8,
  131. };
  132. #define BLK_MQ_FLAG_TO_ALLOC_POLICY(flags) \
  133. ((flags >> BLK_MQ_F_ALLOC_POLICY_START_BIT) & \
  134. ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1))
  135. #define BLK_ALLOC_POLICY_TO_MQ_FLAG(policy) \
  136. ((policy & ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) \
  137. << BLK_MQ_F_ALLOC_POLICY_START_BIT)
  138. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *);
  139. struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  140. struct request_queue *q);
  141. int blk_mq_register_disk(struct gendisk *);
  142. void blk_mq_unregister_disk(struct gendisk *);
  143. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set);
  144. void blk_mq_free_tag_set(struct blk_mq_tag_set *set);
  145. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
  146. void blk_mq_insert_request(struct request *, bool, bool, bool);
  147. void blk_mq_free_request(struct request *rq);
  148. void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *, struct request *rq);
  149. bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
  150. struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
  151. gfp_t gfp, bool reserved);
  152. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag);
  153. struct cpumask *blk_mq_tags_cpumask(struct blk_mq_tags *tags);
  154. enum {
  155. BLK_MQ_UNIQUE_TAG_BITS = 16,
  156. BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
  157. };
  158. u32 blk_mq_unique_tag(struct request *rq);
  159. static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
  160. {
  161. return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
  162. }
  163. static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
  164. {
  165. return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
  166. }
  167. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  168. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  169. int blk_mq_request_started(struct request *rq);
  170. void blk_mq_start_request(struct request *rq);
  171. void blk_mq_end_request(struct request *rq, int error);
  172. void __blk_mq_end_request(struct request *rq, int error);
  173. void blk_mq_requeue_request(struct request *rq);
  174. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head);
  175. void blk_mq_cancel_requeue_work(struct request_queue *q);
  176. void blk_mq_kick_requeue_list(struct request_queue *q);
  177. void blk_mq_abort_requeue_list(struct request_queue *q);
  178. void blk_mq_complete_request(struct request *rq, int error);
  179. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
  180. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
  181. void blk_mq_stop_hw_queues(struct request_queue *q);
  182. void blk_mq_start_hw_queues(struct request_queue *q);
  183. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
  184. void blk_mq_run_hw_queues(struct request_queue *q, bool async);
  185. void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
  186. void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
  187. void *priv);
  188. void blk_mq_freeze_queue(struct request_queue *q);
  189. void blk_mq_unfreeze_queue(struct request_queue *q);
  190. void blk_mq_freeze_queue_start(struct request_queue *q);
  191. /*
  192. * Driver command data is immediately after the request. So subtract request
  193. * size to get back to the original request, add request size to get the PDU.
  194. */
  195. static inline struct request *blk_mq_rq_from_pdu(void *pdu)
  196. {
  197. return pdu - sizeof(struct request);
  198. }
  199. static inline void *blk_mq_rq_to_pdu(struct request *rq)
  200. {
  201. return rq + 1;
  202. }
  203. #define queue_for_each_hw_ctx(q, hctx, i) \
  204. for ((i) = 0; (i) < (q)->nr_hw_queues && \
  205. ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
  206. #define queue_for_each_ctx(q, ctx, i) \
  207. for ((i) = 0; (i) < (q)->nr_queues && \
  208. ({ ctx = per_cpu_ptr((q)->queue_ctx, (i)); 1; }); (i)++)
  209. #define hctx_for_each_ctx(hctx, ctx, i) \
  210. for ((i) = 0; (i) < (hctx)->nr_ctx && \
  211. ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
  212. #define blk_ctx_sum(q, sum) \
  213. ({ \
  214. struct blk_mq_ctx *__x; \
  215. unsigned int __ret = 0, __i; \
  216. \
  217. queue_for_each_ctx((q), __x, __i) \
  218. __ret += sum; \
  219. __ret; \
  220. })
  221. #endif