blk-mq.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifdef CONFIG_BLK_DEBUG_FS
  44. struct dentry *debugfs_dir;
  45. #endif
  46. };
  47. struct blk_mq_tag_set {
  48. unsigned int *mq_map;
  49. const 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. bool last;
  65. };
  66. typedef int (queue_rq_fn)(struct blk_mq_hw_ctx *, const struct blk_mq_queue_data *);
  67. typedef enum blk_eh_timer_return (timeout_fn)(struct request *, bool);
  68. typedef int (init_hctx_fn)(struct blk_mq_hw_ctx *, void *, unsigned int);
  69. typedef void (exit_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
  70. typedef int (init_request_fn)(struct blk_mq_tag_set *set, struct request *,
  71. unsigned int, unsigned int);
  72. typedef void (exit_request_fn)(struct blk_mq_tag_set *set, struct request *,
  73. unsigned int);
  74. typedef int (reinit_request_fn)(void *, struct request *);
  75. typedef void (busy_iter_fn)(struct blk_mq_hw_ctx *, struct request *, void *,
  76. bool);
  77. typedef void (busy_tag_iter_fn)(struct request *, void *, bool);
  78. typedef int (poll_fn)(struct blk_mq_hw_ctx *, unsigned int);
  79. typedef int (map_queues_fn)(struct blk_mq_tag_set *set);
  80. struct blk_mq_ops {
  81. /*
  82. * Queue request
  83. */
  84. queue_rq_fn *queue_rq;
  85. /*
  86. * Called on request timeout
  87. */
  88. timeout_fn *timeout;
  89. /*
  90. * Called to poll for completion of a specific tag.
  91. */
  92. poll_fn *poll;
  93. softirq_done_fn *complete;
  94. /*
  95. * Called when the block layer side of a hardware queue has been
  96. * set up, allowing the driver to allocate/init matching structures.
  97. * Ditto for exit/teardown.
  98. */
  99. init_hctx_fn *init_hctx;
  100. exit_hctx_fn *exit_hctx;
  101. /*
  102. * Called for every command allocated by the block layer to allow
  103. * the driver to set up driver specific data.
  104. *
  105. * Tag greater than or equal to queue_depth is for setting up
  106. * flush request.
  107. *
  108. * Ditto for exit/teardown.
  109. */
  110. init_request_fn *init_request;
  111. exit_request_fn *exit_request;
  112. reinit_request_fn *reinit_request;
  113. map_queues_fn *map_queues;
  114. #ifdef CONFIG_BLK_DEBUG_FS
  115. /*
  116. * Used by the debugfs implementation to show driver-specific
  117. * information about a request.
  118. */
  119. void (*show_rq)(struct seq_file *m, struct request *rq);
  120. #endif
  121. };
  122. enum {
  123. BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
  124. BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
  125. BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
  126. BLK_MQ_F_SHOULD_MERGE = 1 << 0,
  127. BLK_MQ_F_TAG_SHARED = 1 << 1,
  128. BLK_MQ_F_SG_MERGE = 1 << 2,
  129. BLK_MQ_F_BLOCKING = 1 << 5,
  130. BLK_MQ_F_NO_SCHED = 1 << 6,
  131. BLK_MQ_F_ALLOC_POLICY_START_BIT = 8,
  132. BLK_MQ_F_ALLOC_POLICY_BITS = 1,
  133. BLK_MQ_S_STOPPED = 0,
  134. BLK_MQ_S_TAG_ACTIVE = 1,
  135. BLK_MQ_S_SCHED_RESTART = 2,
  136. BLK_MQ_S_TAG_WAITING = 3,
  137. BLK_MQ_S_START_ON_RUN = 4,
  138. BLK_MQ_MAX_DEPTH = 10240,
  139. BLK_MQ_CPU_WORK_BATCH = 8,
  140. };
  141. #define BLK_MQ_FLAG_TO_ALLOC_POLICY(flags) \
  142. ((flags >> BLK_MQ_F_ALLOC_POLICY_START_BIT) & \
  143. ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1))
  144. #define BLK_ALLOC_POLICY_TO_MQ_FLAG(policy) \
  145. ((policy & ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) \
  146. << BLK_MQ_F_ALLOC_POLICY_START_BIT)
  147. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *);
  148. struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  149. struct request_queue *q);
  150. int blk_mq_register_dev(struct device *, struct request_queue *);
  151. void blk_mq_unregister_dev(struct device *, struct request_queue *);
  152. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set);
  153. void blk_mq_free_tag_set(struct blk_mq_tag_set *set);
  154. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
  155. void blk_mq_free_request(struct request *rq);
  156. bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
  157. enum {
  158. BLK_MQ_REQ_NOWAIT = (1 << 0), /* return when out of requests */
  159. BLK_MQ_REQ_RESERVED = (1 << 1), /* allocate from reserved pool */
  160. BLK_MQ_REQ_INTERNAL = (1 << 2), /* allocate internal/sched tag */
  161. };
  162. struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
  163. unsigned int flags);
  164. struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int op,
  165. unsigned int flags, unsigned int hctx_idx);
  166. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag);
  167. enum {
  168. BLK_MQ_UNIQUE_TAG_BITS = 16,
  169. BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
  170. };
  171. u32 blk_mq_unique_tag(struct request *rq);
  172. static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
  173. {
  174. return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
  175. }
  176. static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
  177. {
  178. return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
  179. }
  180. int blk_mq_request_started(struct request *rq);
  181. void blk_mq_start_request(struct request *rq);
  182. void blk_mq_end_request(struct request *rq, int error);
  183. void __blk_mq_end_request(struct request *rq, int error);
  184. void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
  185. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
  186. bool kick_requeue_list);
  187. void blk_mq_kick_requeue_list(struct request_queue *q);
  188. void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
  189. void blk_mq_abort_requeue_list(struct request_queue *q);
  190. void blk_mq_complete_request(struct request *rq);
  191. bool blk_mq_queue_stopped(struct request_queue *q);
  192. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
  193. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
  194. void blk_mq_stop_hw_queues(struct request_queue *q);
  195. void blk_mq_start_hw_queues(struct request_queue *q);
  196. void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
  197. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
  198. void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
  199. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
  200. void blk_mq_run_hw_queues(struct request_queue *q, bool async);
  201. void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
  202. void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
  203. busy_tag_iter_fn *fn, void *priv);
  204. void blk_mq_freeze_queue(struct request_queue *q);
  205. void blk_mq_unfreeze_queue(struct request_queue *q);
  206. void blk_freeze_queue_start(struct request_queue *q);
  207. void blk_mq_freeze_queue_wait(struct request_queue *q);
  208. int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
  209. unsigned long timeout);
  210. int blk_mq_reinit_tagset(struct blk_mq_tag_set *set);
  211. int blk_mq_map_queues(struct blk_mq_tag_set *set);
  212. void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);
  213. /*
  214. * Driver command data is immediately after the request. So subtract request
  215. * size to get back to the original request, add request size to get the PDU.
  216. */
  217. static inline struct request *blk_mq_rq_from_pdu(void *pdu)
  218. {
  219. return pdu - sizeof(struct request);
  220. }
  221. static inline void *blk_mq_rq_to_pdu(struct request *rq)
  222. {
  223. return rq + 1;
  224. }
  225. #define queue_for_each_hw_ctx(q, hctx, i) \
  226. for ((i) = 0; (i) < (q)->nr_hw_queues && \
  227. ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
  228. #define hctx_for_each_ctx(hctx, ctx, i) \
  229. for ((i) = 0; (i) < (hctx)->nr_ctx && \
  230. ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
  231. #endif