blk-mq.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. softirq_done_fn *complete;
  68. /*
  69. * Override for hctx allocations (should probably go)
  70. */
  71. alloc_hctx_fn *alloc_hctx;
  72. free_hctx_fn *free_hctx;
  73. /*
  74. * Called when the block layer side of a hardware queue has been
  75. * set up, allowing the driver to allocate/init matching structures.
  76. * Ditto for exit/teardown.
  77. */
  78. init_hctx_fn *init_hctx;
  79. exit_hctx_fn *exit_hctx;
  80. };
  81. enum {
  82. BLK_MQ_RQ_QUEUE_OK = 0, /* queued fine */
  83. BLK_MQ_RQ_QUEUE_BUSY = 1, /* requeue IO for later */
  84. BLK_MQ_RQ_QUEUE_ERROR = 2, /* end IO with error */
  85. BLK_MQ_F_SHOULD_MERGE = 1 << 0,
  86. BLK_MQ_F_SHOULD_SORT = 1 << 1,
  87. BLK_MQ_F_SHOULD_IPI = 1 << 2,
  88. BLK_MQ_S_STOPPED = 0,
  89. BLK_MQ_MAX_DEPTH = 2048,
  90. };
  91. struct request_queue *blk_mq_init_queue(struct blk_mq_reg *, void *);
  92. int blk_mq_register_disk(struct gendisk *);
  93. void blk_mq_unregister_disk(struct gendisk *);
  94. int blk_mq_init_commands(struct request_queue *, int (*init)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
  95. void blk_mq_free_commands(struct request_queue *, void (*free)(void *data, struct blk_mq_hw_ctx *, struct request *, unsigned int), void *data);
  96. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
  97. void blk_mq_insert_request(struct request *, bool, bool, bool);
  98. void blk_mq_run_queues(struct request_queue *q, bool async);
  99. void blk_mq_free_request(struct request *rq);
  100. bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
  101. struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp);
  102. struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, gfp_t gfp);
  103. struct request *blk_mq_rq_from_tag(struct request_queue *q, unsigned int tag);
  104. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  105. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *, unsigned int);
  106. void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *, unsigned int);
  107. void blk_mq_end_io(struct request *rq, int error);
  108. void blk_mq_complete_request(struct request *rq);
  109. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
  110. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
  111. void blk_mq_stop_hw_queues(struct request_queue *q);
  112. void blk_mq_start_stopped_hw_queues(struct request_queue *q);
  113. /*
  114. * Driver command data is immediately after the request. So subtract request
  115. * size to get back to the original request.
  116. */
  117. static inline struct request *blk_mq_rq_from_pdu(void *pdu)
  118. {
  119. return pdu - sizeof(struct request);
  120. }
  121. static inline void *blk_mq_rq_to_pdu(struct request *rq)
  122. {
  123. return (void *) rq + sizeof(*rq);
  124. }
  125. static inline struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx,
  126. unsigned int tag)
  127. {
  128. return hctx->rqs[tag];
  129. }
  130. #define queue_for_each_hw_ctx(q, hctx, i) \
  131. for ((i) = 0; (i) < (q)->nr_hw_queues && \
  132. ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
  133. #define queue_for_each_ctx(q, ctx, i) \
  134. for ((i) = 0; (i) < (q)->nr_queues && \
  135. ({ ctx = per_cpu_ptr((q)->queue_ctx, (i)); 1; }); (i)++)
  136. #define hctx_for_each_ctx(hctx, ctx, i) \
  137. for ((i) = 0; (i) < (hctx)->nr_ctx && \
  138. ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
  139. #define blk_ctx_sum(q, sum) \
  140. ({ \
  141. struct blk_mq_ctx *__x; \
  142. unsigned int __ret = 0, __i; \
  143. \
  144. queue_for_each_ctx((q), __x, __i) \
  145. __ret += sum; \
  146. __ret; \
  147. })
  148. #endif