blk-mq.h 8.2 KB

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