blk-mq.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef INT_BLK_MQ_H
  3. #define INT_BLK_MQ_H
  4. #include "blk-stat.h"
  5. struct blk_mq_tag_set;
  6. struct blk_mq_ctx {
  7. struct {
  8. spinlock_t lock;
  9. struct list_head rq_list;
  10. } ____cacheline_aligned_in_smp;
  11. unsigned int cpu;
  12. unsigned int index_hw;
  13. /* incremented at dispatch time */
  14. unsigned long rq_dispatched[2];
  15. unsigned long rq_merged;
  16. /* incremented at completion time */
  17. unsigned long ____cacheline_aligned_in_smp rq_completed[2];
  18. struct request_queue *queue;
  19. struct kobject kobj;
  20. } ____cacheline_aligned_in_smp;
  21. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
  22. void blk_mq_freeze_queue(struct request_queue *q);
  23. void blk_mq_free_queue(struct request_queue *q);
  24. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr);
  25. void blk_mq_wake_waiters(struct request_queue *q);
  26. bool blk_mq_dispatch_rq_list(struct request_queue *, struct list_head *);
  27. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);
  28. bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx);
  29. bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx,
  30. bool wait);
  31. /*
  32. * Internal helpers for allocating/freeing the request map
  33. */
  34. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  35. unsigned int hctx_idx);
  36. void blk_mq_free_rq_map(struct blk_mq_tags *tags);
  37. struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  38. unsigned int hctx_idx,
  39. unsigned int nr_tags,
  40. unsigned int reserved_tags);
  41. int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  42. unsigned int hctx_idx, unsigned int depth);
  43. /*
  44. * Internal helpers for request insertion into sw queues
  45. */
  46. void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  47. bool at_head);
  48. void blk_mq_request_bypass_insert(struct request *rq);
  49. void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  50. struct list_head *list);
  51. /*
  52. * CPU -> queue mappings
  53. */
  54. extern int blk_mq_hw_queue_to_node(unsigned int *map, unsigned int);
  55. static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,
  56. int cpu)
  57. {
  58. return q->queue_hw_ctx[q->mq_map[cpu]];
  59. }
  60. /*
  61. * sysfs helpers
  62. */
  63. extern void blk_mq_sysfs_init(struct request_queue *q);
  64. extern void blk_mq_sysfs_deinit(struct request_queue *q);
  65. extern int __blk_mq_register_dev(struct device *dev, struct request_queue *q);
  66. extern int blk_mq_sysfs_register(struct request_queue *q);
  67. extern void blk_mq_sysfs_unregister(struct request_queue *q);
  68. extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx);
  69. extern void blk_mq_rq_timed_out(struct request *req, bool reserved);
  70. void blk_mq_release(struct request_queue *q);
  71. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  72. unsigned int cpu)
  73. {
  74. return per_cpu_ptr(q->queue_ctx, cpu);
  75. }
  76. /*
  77. * This assumes per-cpu software queueing queues. They could be per-node
  78. * as well, for instance. For now this is hardcoded as-is. Note that we don't
  79. * care about preemption, since we know the ctx's are persistent. This does
  80. * mean that we can't rely on ctx always matching the currently running CPU.
  81. */
  82. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  83. {
  84. return __blk_mq_get_ctx(q, get_cpu());
  85. }
  86. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  87. {
  88. put_cpu();
  89. }
  90. struct blk_mq_alloc_data {
  91. /* input parameter */
  92. struct request_queue *q;
  93. unsigned int flags;
  94. unsigned int shallow_depth;
  95. /* input & output parameter */
  96. struct blk_mq_ctx *ctx;
  97. struct blk_mq_hw_ctx *hctx;
  98. };
  99. static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data)
  100. {
  101. if (data->flags & BLK_MQ_REQ_INTERNAL)
  102. return data->hctx->sched_tags;
  103. return data->hctx->tags;
  104. }
  105. static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx)
  106. {
  107. return test_bit(BLK_MQ_S_STOPPED, &hctx->state);
  108. }
  109. static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx)
  110. {
  111. return hctx->nr_ctx && hctx->tags;
  112. }
  113. void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
  114. unsigned int inflight[2]);
  115. void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
  116. unsigned int inflight[2]);
  117. #endif