blk-mq.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #ifndef BLK_MQ_H
  2. #define BLK_MQ_H
  3. #include <linux/blkdev.h>
  4. struct blk_mq_tags;
  5. struct blk_mq_cpu_notifier {
  6. struct list_head list;
  7. void *data;
  8. void (*notify)(void *data, unsigned long action, unsigned int cpu);
  9. };
  10. struct blk_mq_hw_ctx {
  11. struct {
  12. spinlock_t lock;
  13. struct list_head dispatch;
  14. } ____cacheline_aligned_in_smp;
  15. unsigned long state; /* BLK_MQ_S_* flags */
  16. struct delayed_work delayed_work;
  17. unsigned long flags; /* BLK_MQ_F_* flags */
  18. struct request_queue *queue;
  19. unsigned int queue_num;
  20. void *driver_data;
  21. unsigned int nr_ctx;
  22. struct blk_mq_ctx **ctxs;
  23. unsigned int nr_ctx_map;
  24. unsigned long *ctx_map;
  25. struct request **rqs;
  26. struct list_head page_list;
  27. struct blk_mq_tags *tags;
  28. unsigned long queued;
  29. unsigned long run;
  30. #define BLK_MQ_MAX_DISPATCH_ORDER 10
  31. unsigned long dispatched[BLK_MQ_MAX_DISPATCH_ORDER];
  32. unsigned int queue_depth;
  33. unsigned int numa_node;
  34. unsigned int cmd_size; /* per-request extra data */
  35. struct blk_mq_cpu_notifier cpu_notifier;
  36. struct kobject kobj;
  37. };
  38. struct blk_mq_reg {
  39. struct blk_mq_ops *ops;
  40. unsigned int nr_hw_queues;
  41. unsigned int queue_depth;
  42. unsigned int reserved_tags;
  43. unsigned int cmd_size; /* per-request extra data */
  44. int numa_node;
  45. unsigned int timeout;
  46. unsigned int flags; /* BLK_MQ_F_* */
  47. };
  48. typedef int (queue_rq_fn)(struct blk_mq_hw_ctx *, struct request *);
  49. typedef struct blk_mq_hw_ctx *(map_queue_fn)(struct request_queue *, const int);
  50. typedef struct blk_mq_hw_ctx *(alloc_hctx_fn)(struct blk_mq_reg *,unsigned int);
  51. typedef void (free_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
  52. typedef int (init_hctx_fn)(struct blk_mq_hw_ctx *, void *, unsigned int);
  53. typedef void (exit_hctx_fn)(struct blk_mq_hw_ctx *, unsigned int);
  54. struct blk_mq_ops {
  55. /*
  56. * Queue request
  57. */
  58. queue_rq_fn *queue_rq;
  59. /*
  60. * Map to specific hardware queue
  61. */
  62. map_queue_fn *map_queue;
  63. /*
  64. * Called on request timeout
  65. */
  66. rq_timed_out_fn *timeout;
  67. /*
  68. * Override for hctx allocations (should probably go)
  69. */
  70. alloc_hctx_fn *alloc_hctx;
  71. free_hctx_fn *free_hctx;
  72. /*
  73. * Called when the block layer side of a hardware queue has been
  74. * set up, allowing the driver to allocate/init matching structures.
  75. * Ditto for exit/teardown.
  76. */
  77. init_hctx_fn *init_hctx;
  78. exit_hctx_fn *exit_hctx;
  79. };
  80. enum {
  81. BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
  82. BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
  83. BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
  84. BLK_MQ_F_SHOULD_MERGE = 1 << 0,
  85. BLK_MQ_F_SHOULD_SORT = 1 << 1,
  86. BLK_MQ_F_SHOULD_IPI = 1 << 2,
  87. BLK_MQ_S_STOPPED = 1 << 0,
  88. BLK_MQ_MAX_DEPTH = 2048,
  89. };
  90. struct request_queue *blk_mq_init_queue(struct blk_mq_reg *, void *);
  91. void blk_mq_free_queue(struct request_queue *);
  92. int blk_mq_register_disk(struct gendisk *);
  93. void blk_mq_unregister_disk(struct gendisk *);
  94. void blk_mq_init_commands(struct request_queue *, void (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
  95. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
  96. void blk_mq_insert_request(struct request_queue *, struct request *, bool);
  97. void blk_mq_run_queues(struct request_queue *q, bool async);
  98. void blk_mq_free_request(struct request *rq);
  99. bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
  100. struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp, bool reserved);
  101. struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, gfp_t gfp);
  102. struct request *blk_mq_rq_from_tag(struct request_queue *q, unsigned int tag);
  103. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  104. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *, unsigned int);
  105. void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *, unsigned int);
  106. void blk_mq_end_io(struct request *rq, int error);
  107. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
  108. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
  109. void blk_mq_stop_hw_queues(struct request_queue *q);
  110. void blk_mq_start_stopped_hw_queues(struct request_queue *q);
  111. /*
  112. * Driver command data is immediately after the request. So subtract request
  113. * size to get back to the original request.
  114. */
  115. static inline struct request *blk_mq_rq_from_pdu(void *pdu)
  116. {
  117. return pdu - sizeof(struct request);
  118. }
  119. static inline void *blk_mq_rq_to_pdu(struct request *rq)
  120. {
  121. return (void *) rq + sizeof(*rq);
  122. }
  123. static inline struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx,
  124. unsigned int tag)
  125. {
  126. return hctx->rqs[tag];
  127. }
  128. #define queue_for_each_hw_ctx(q, hctx, i) \
  129. for ((i) = 0, hctx = (q)->queue_hw_ctx[0]; \
  130. (i) < (q)->nr_hw_queues; (i)++, hctx = (q)->queue_hw_ctx[i])
  131. #define queue_for_each_ctx(q, ctx, i) \
  132. for ((i) = 0, ctx = per_cpu_ptr((q)->queue_ctx, 0); \
  133. (i) < (q)->nr_queues; (i)++, ctx = per_cpu_ptr(q->queue_ctx, (i)))
  134. #define hctx_for_each_ctx(hctx, ctx, i) \
  135. for ((i) = 0, ctx = (hctx)->ctxs[0]; \
  136. (i) < (hctx)->nr_ctx; (i)++, ctx = (hctx)->ctxs[(i)])
  137. #define blk_ctx_sum(q, sum) \
  138. ({ \
  139. struct blk_mq_ctx *__x; \
  140. unsigned int __ret = 0, __i; \
  141. \
  142. queue_for_each_ctx((q), __x, __i) \
  143. __ret += sum; \
  144. __ret; \
  145. })
  146. #endif