blk-mq.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 map_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. unsigned int queue_num;
  30. struct blk_flush_queue *fq;
  31. void *driver_data;
  32. struct blk_mq_ctxmap ctx_map;
  33. unsigned int nr_ctx;
  34. struct blk_mq_ctx **ctxs;
  35. atomic_t wait_index;
  36. struct blk_mq_tags *tags;
  37. unsigned long queued;
  38. unsigned long run;
  39. #define BLK_MQ_MAX_DISPATCH_ORDER 10
  40. unsigned long dispatched[BLK_MQ_MAX_DISPATCH_ORDER];
  41. unsigned int numa_node;
  42. unsigned int cmd_size; /* per-request extra data */
  43. atomic_t nr_active;
  44. struct blk_mq_cpu_notifier cpu_notifier;
  45. struct kobject kobj;
  46. };
  47. struct blk_mq_tag_set {
  48. struct blk_mq_ops *ops;
  49. unsigned int nr_hw_queues;
  50. unsigned int queue_depth; /* max hw supported */
  51. unsigned int reserved_tags;
  52. unsigned int cmd_size; /* per-request extra data */
  53. int numa_node;
  54. unsigned int timeout;
  55. unsigned int flags; /* BLK_MQ_F_* */
  56. void *driver_data;
  57. struct blk_mq_tags **tags;
  58. struct mutex tag_list_lock;
  59. struct list_head tag_list;
  60. };
  61. typedef int (queue_rq_fn)(struct blk_mq_hw_ctx *, struct request *, bool);
  62. typedef struct blk_mq_hw_ctx *(map_queue_fn)(struct request_queue *, const int);
  63. typedef enum blk_eh_timer_return (timeout_fn)(struct request *, bool);
  64. typedef int (init_hctx_fn)(struct blk_mq_hw_ctx *, void *, unsigned int);
  65. typedef void (exit_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
  66. typedef int (init_request_fn)(void *, struct request *, unsigned int,
  67. unsigned int, unsigned int);
  68. typedef void (exit_request_fn)(void *, struct request *, unsigned int,
  69. unsigned int);
  70. typedef void (busy_iter_fn)(struct blk_mq_hw_ctx *, struct request *, void *,
  71. bool);
  72. struct blk_mq_ops {
  73. /*
  74. * Queue request
  75. */
  76. queue_rq_fn *queue_rq;
  77. /*
  78. * Map to specific hardware queue
  79. */
  80. map_queue_fn *map_queue;
  81. /*
  82. * Called on request timeout
  83. */
  84. timeout_fn *timeout;
  85. softirq_done_fn *complete;
  86. /*
  87. * Called when the block layer side of a hardware queue has been
  88. * set up, allowing the driver to allocate/init matching structures.
  89. * Ditto for exit/teardown.
  90. */
  91. init_hctx_fn *init_hctx;
  92. exit_hctx_fn *exit_hctx;
  93. /*
  94. * Called for every command allocated by the block layer to allow
  95. * the driver to set up driver specific data.
  96. *
  97. * Tag greater than or equal to queue_depth is for setting up
  98. * flush request.
  99. *
  100. * Ditto for exit/teardown.
  101. */
  102. init_request_fn *init_request;
  103. exit_request_fn *exit_request;
  104. };
  105. enum {
  106. BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
  107. BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
  108. BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
  109. BLK_MQ_F_SHOULD_MERGE = 1 << 0,
  110. BLK_MQ_F_TAG_SHARED = 1 << 1,
  111. BLK_MQ_F_SG_MERGE = 1 << 2,
  112. BLK_MQ_F_SYSFS_UP = 1 << 3,
  113. BLK_MQ_S_STOPPED = 0,
  114. BLK_MQ_S_TAG_ACTIVE = 1,
  115. BLK_MQ_MAX_DEPTH = 10240,
  116. BLK_MQ_CPU_WORK_BATCH = 8,
  117. };
  118. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *);
  119. void blk_mq_finish_init(struct request_queue *q);
  120. int blk_mq_register_disk(struct gendisk *);
  121. void blk_mq_unregister_disk(struct gendisk *);
  122. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set);
  123. void blk_mq_free_tag_set(struct blk_mq_tag_set *set);
  124. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
  125. void blk_mq_insert_request(struct request *, bool, bool, bool);
  126. void blk_mq_run_queues(struct request_queue *q, bool async);
  127. void blk_mq_free_request(struct request *rq);
  128. bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
  129. struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
  130. gfp_t gfp, bool reserved);
  131. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag);
  132. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  133. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  134. void blk_mq_start_request(struct request *rq);
  135. void blk_mq_end_request(struct request *rq, int error);
  136. void __blk_mq_end_request(struct request *rq, int error);
  137. void blk_mq_requeue_request(struct request *rq);
  138. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head);
  139. void blk_mq_kick_requeue_list(struct request_queue *q);
  140. void blk_mq_complete_request(struct request *rq);
  141. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
  142. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
  143. void blk_mq_stop_hw_queues(struct request_queue *q);
  144. void blk_mq_start_hw_queues(struct request_queue *q);
  145. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
  146. void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
  147. void blk_mq_tag_busy_iter(struct blk_mq_hw_ctx *hctx, busy_iter_fn *fn,
  148. void *priv);
  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. #define queue_for_each_hw_ctx(q, hctx, i) \
  162. for ((i) = 0; (i) < (q)->nr_hw_queues && \
  163. ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
  164. #define queue_for_each_ctx(q, ctx, i) \
  165. for ((i) = 0; (i) < (q)->nr_queues && \
  166. ({ ctx = per_cpu_ptr((q)->queue_ctx, (i)); 1; }); (i)++)
  167. #define hctx_for_each_ctx(hctx, ctx, i) \
  168. for ((i) = 0; (i) < (hctx)->nr_ctx && \
  169. ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
  170. #define blk_ctx_sum(q, sum) \
  171. ({ \
  172. struct blk_mq_ctx *__x; \
  173. unsigned int __ret = 0, __i; \
  174. \
  175. queue_for_each_ctx((q), __x, __i) \
  176. __ret += sum; \
  177. __ret; \
  178. })
  179. #endif