1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2320ae51fSJens Axboe #ifndef INT_BLK_MQ_H 3320ae51fSJens Axboe #define INT_BLK_MQ_H 4320ae51fSJens Axboe 5cf43e6beSJens Axboe #include "blk-stat.h" 6244c65a3SMing Lei #include "blk-mq-tag.h" 7cf43e6beSJens Axboe 824d2f903SChristoph Hellwig struct blk_mq_tag_set; 924d2f903SChristoph Hellwig 101db4909eSMing Lei struct blk_mq_ctxs { 111db4909eSMing Lei struct kobject kobj; 121db4909eSMing Lei struct blk_mq_ctx __percpu *queue_ctx; 131db4909eSMing Lei }; 141db4909eSMing Lei 15fe644072SLinus Walleij /** 16fe644072SLinus Walleij * struct blk_mq_ctx - State for a software queue facing the submitting CPUs 17fe644072SLinus Walleij */ 18320ae51fSJens Axboe struct blk_mq_ctx { 19320ae51fSJens Axboe struct { 20320ae51fSJens Axboe spinlock_t lock; 21c16d6b5aSMing Lei struct list_head rq_lists[HCTX_MAX_TYPES]; 22320ae51fSJens Axboe } ____cacheline_aligned_in_smp; 23320ae51fSJens Axboe 24320ae51fSJens Axboe unsigned int cpu; 25f31967f0SJens Axboe unsigned short index_hw[HCTX_MAX_TYPES]; 268ccdf4a3SJianchao Wang struct blk_mq_hw_ctx *hctxs[HCTX_MAX_TYPES]; 27320ae51fSJens Axboe 28320ae51fSJens Axboe /* incremented at dispatch time */ 29320ae51fSJens Axboe unsigned long rq_dispatched[2]; 30320ae51fSJens Axboe unsigned long rq_merged; 31320ae51fSJens Axboe 32320ae51fSJens Axboe /* incremented at completion time */ 33320ae51fSJens Axboe unsigned long ____cacheline_aligned_in_smp rq_completed[2]; 34320ae51fSJens Axboe 35320ae51fSJens Axboe struct request_queue *queue; 361db4909eSMing Lei struct blk_mq_ctxs *ctxs; 37320ae51fSJens Axboe struct kobject kobj; 384bb659b1SJens Axboe } ____cacheline_aligned_in_smp; 39320ae51fSJens Axboe 403edcc0ceSMing Lei void blk_mq_free_queue(struct request_queue *q); 41e3a2b3f9SJens Axboe int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr); 42aed3ea94SJens Axboe void blk_mq_wake_waiters(struct request_queue *q); 43de148297SMing Lei bool blk_mq_dispatch_rq_list(struct request_queue *, struct list_head *, bool); 44*e6c98712SBart Van Assche void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, 45*e6c98712SBart Van Assche bool kick_requeue_list); 462c3ad667SJens Axboe void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list); 478ab6bb9eSMing Lei bool blk_mq_get_driver_tag(struct request *rq); 48b347689fSMing Lei struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, 49b347689fSMing Lei struct blk_mq_ctx *start); 502c3ad667SJens Axboe 512c3ad667SJens Axboe /* 522c3ad667SJens Axboe * Internal helpers for allocating/freeing the request map 532c3ad667SJens Axboe */ 54cc71a6f4SJens Axboe void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, 552c3ad667SJens Axboe unsigned int hctx_idx); 56cc71a6f4SJens Axboe void blk_mq_free_rq_map(struct blk_mq_tags *tags); 57cc71a6f4SJens Axboe struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, 58cc71a6f4SJens Axboe unsigned int hctx_idx, 59cc71a6f4SJens Axboe unsigned int nr_tags, 60cc71a6f4SJens Axboe unsigned int reserved_tags); 61cc71a6f4SJens Axboe int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, 62cc71a6f4SJens Axboe unsigned int hctx_idx, unsigned int depth); 632c3ad667SJens Axboe 642c3ad667SJens Axboe /* 652c3ad667SJens Axboe * Internal helpers for request insertion into sw queues 662c3ad667SJens Axboe */ 672c3ad667SJens Axboe void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, 682c3ad667SJens Axboe bool at_head); 69b0850297SMing Lei void blk_mq_request_bypass_insert(struct request *rq, bool run_queue); 70bd166ef1SJens Axboe void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, 71bd166ef1SJens Axboe struct list_head *list); 72320ae51fSJens Axboe 73d6a51a97SJianchao Wang blk_status_t blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, 74d6a51a97SJianchao Wang struct request *rq, 75d6a51a97SJianchao Wang blk_qc_t *cookie, 76d6a51a97SJianchao Wang bool bypass, bool last); 776ce3dd6eSMing Lei void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, 786ce3dd6eSMing Lei struct list_head *list); 79396eaf21SMing Lei 80320ae51fSJens Axboe /* 81320ae51fSJens Axboe * CPU -> queue mappings 82320ae51fSJens Axboe */ 83ed76e329SJens Axboe extern int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int); 84320ae51fSJens Axboe 85b3c661b1SJens Axboe /* 86b3c661b1SJens Axboe * blk_mq_map_queue_type() - map (hctx_type,cpu) to hardware queue 87b3c661b1SJens Axboe * @q: request queue 88e20ba6e1SChristoph Hellwig * @type: the hctx type index 89b3c661b1SJens Axboe * @cpu: CPU 90b3c661b1SJens Axboe */ 91ff2c5660SJens Axboe static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *q, 92e20ba6e1SChristoph Hellwig enum hctx_type type, 93ff2c5660SJens Axboe unsigned int cpu) 94ff2c5660SJens Axboe { 95e20ba6e1SChristoph Hellwig return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]]; 96b3c661b1SJens Axboe } 97b3c661b1SJens Axboe 98b3c661b1SJens Axboe /* 99b3c661b1SJens Axboe * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue 100b3c661b1SJens Axboe * @q: request queue 101b3c661b1SJens Axboe * @flags: request command flags 1028ccdf4a3SJianchao Wang * @cpu: cpu ctx 103b3c661b1SJens Axboe */ 104b3c661b1SJens Axboe static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, 105b3c661b1SJens Axboe unsigned int flags, 1068ccdf4a3SJianchao Wang struct blk_mq_ctx *ctx) 107b3c661b1SJens Axboe { 108e20ba6e1SChristoph Hellwig enum hctx_type type = HCTX_TYPE_DEFAULT; 109b3c661b1SJens Axboe 110bb94aea1SJianchao Wang /* 111bb94aea1SJianchao Wang * The caller ensure that if REQ_HIPRI, poll must be enabled. 112bb94aea1SJianchao Wang */ 113bb94aea1SJianchao Wang if (flags & REQ_HIPRI) 114e20ba6e1SChristoph Hellwig type = HCTX_TYPE_POLL; 115bb94aea1SJianchao Wang else if ((flags & REQ_OP_MASK) == REQ_OP_READ) 116e20ba6e1SChristoph Hellwig type = HCTX_TYPE_READ; 117e20ba6e1SChristoph Hellwig 1188ccdf4a3SJianchao Wang return ctx->hctxs[type]; 119ff2c5660SJens Axboe } 120ff2c5660SJens Axboe 121e93ecf60SJens Axboe /* 12267aec14cSJens Axboe * sysfs helpers 12367aec14cSJens Axboe */ 124737f98cfSMing Lei extern void blk_mq_sysfs_init(struct request_queue *q); 1257ea5fe31SMing Lei extern void blk_mq_sysfs_deinit(struct request_queue *q); 1262d0364c8SBart Van Assche extern int __blk_mq_register_dev(struct device *dev, struct request_queue *q); 12767aec14cSJens Axboe extern int blk_mq_sysfs_register(struct request_queue *q); 12867aec14cSJens Axboe extern void blk_mq_sysfs_unregister(struct request_queue *q); 129868f2f0bSKeith Busch extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx); 13067aec14cSJens Axboe 131e09aae7eSMing Lei void blk_mq_release(struct request_queue *q); 132e09aae7eSMing Lei 1331d9bd516STejun Heo /** 1341d9bd516STejun Heo * blk_mq_rq_state() - read the current MQ_RQ_* state of a request 1351d9bd516STejun Heo * @rq: target request. 1361d9bd516STejun Heo */ 13712f5b931SKeith Busch static inline enum mq_rq_state blk_mq_rq_state(struct request *rq) 1381d9bd516STejun Heo { 13912f5b931SKeith Busch return READ_ONCE(rq->state); 1401d9bd516STejun Heo } 1411d9bd516STejun Heo 1421aecfe48SMing Lei static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q, 1431aecfe48SMing Lei unsigned int cpu) 1441aecfe48SMing Lei { 1451aecfe48SMing Lei return per_cpu_ptr(q->queue_ctx, cpu); 1461aecfe48SMing Lei } 1471aecfe48SMing Lei 1481aecfe48SMing Lei /* 1491aecfe48SMing Lei * This assumes per-cpu software queueing queues. They could be per-node 1501aecfe48SMing Lei * as well, for instance. For now this is hardcoded as-is. Note that we don't 1511aecfe48SMing Lei * care about preemption, since we know the ctx's are persistent. This does 1521aecfe48SMing Lei * mean that we can't rely on ctx always matching the currently running CPU. 1531aecfe48SMing Lei */ 1541aecfe48SMing Lei static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q) 1551aecfe48SMing Lei { 1561aecfe48SMing Lei return __blk_mq_get_ctx(q, get_cpu()); 1571aecfe48SMing Lei } 1581aecfe48SMing Lei 1591aecfe48SMing Lei static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx) 1601aecfe48SMing Lei { 1611aecfe48SMing Lei put_cpu(); 1621aecfe48SMing Lei } 1631aecfe48SMing Lei 164cb96a42cSMing Lei struct blk_mq_alloc_data { 165cb96a42cSMing Lei /* input parameter */ 166cb96a42cSMing Lei struct request_queue *q; 1679a95e4efSBart Van Assche blk_mq_req_flags_t flags; 168229a9287SOmar Sandoval unsigned int shallow_depth; 169f9afca4dSJens Axboe unsigned int cmd_flags; 170cb96a42cSMing Lei 171cb96a42cSMing Lei /* input & output parameter */ 172cb96a42cSMing Lei struct blk_mq_ctx *ctx; 173cb96a42cSMing Lei struct blk_mq_hw_ctx *hctx; 174cb96a42cSMing Lei }; 175cb96a42cSMing Lei 1764941115bSJens Axboe static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data) 1774941115bSJens Axboe { 178bd166ef1SJens Axboe if (data->flags & BLK_MQ_REQ_INTERNAL) 179bd166ef1SJens Axboe return data->hctx->sched_tags; 180bd166ef1SJens Axboe 1814941115bSJens Axboe return data->hctx->tags; 1824941115bSJens Axboe } 1834941115bSJens Axboe 1845d1b25c1SBart Van Assche static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx) 1855d1b25c1SBart Van Assche { 1865d1b25c1SBart Van Assche return test_bit(BLK_MQ_S_STOPPED, &hctx->state); 1875d1b25c1SBart Van Assche } 1885d1b25c1SBart Van Assche 18919c66e59SMing Lei static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx) 19019c66e59SMing Lei { 19119c66e59SMing Lei return hctx->nr_ctx && hctx->tags; 19219c66e59SMing Lei } 19319c66e59SMing Lei 194e016b782SMikulas Patocka unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part); 195bf0ddabaSOmar Sandoval void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part, 196bf0ddabaSOmar Sandoval unsigned int inflight[2]); 197f299b7c7SJens Axboe 198de148297SMing Lei static inline void blk_mq_put_dispatch_budget(struct blk_mq_hw_ctx *hctx) 199de148297SMing Lei { 200de148297SMing Lei struct request_queue *q = hctx->queue; 201de148297SMing Lei 202de148297SMing Lei if (q->mq_ops->put_budget) 203de148297SMing Lei q->mq_ops->put_budget(hctx); 204de148297SMing Lei } 205de148297SMing Lei 20688022d72SMing Lei static inline bool blk_mq_get_dispatch_budget(struct blk_mq_hw_ctx *hctx) 207de148297SMing Lei { 208de148297SMing Lei struct request_queue *q = hctx->queue; 209de148297SMing Lei 210de148297SMing Lei if (q->mq_ops->get_budget) 211de148297SMing Lei return q->mq_ops->get_budget(hctx); 21288022d72SMing Lei return true; 213de148297SMing Lei } 214de148297SMing Lei 215244c65a3SMing Lei static inline void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx, 216244c65a3SMing Lei struct request *rq) 217244c65a3SMing Lei { 218244c65a3SMing Lei blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag); 219244c65a3SMing Lei rq->tag = -1; 220244c65a3SMing Lei 221244c65a3SMing Lei if (rq->rq_flags & RQF_MQ_INFLIGHT) { 222244c65a3SMing Lei rq->rq_flags &= ~RQF_MQ_INFLIGHT; 223244c65a3SMing Lei atomic_dec(&hctx->nr_active); 224244c65a3SMing Lei } 225244c65a3SMing Lei } 226244c65a3SMing Lei 227244c65a3SMing Lei static inline void blk_mq_put_driver_tag_hctx(struct blk_mq_hw_ctx *hctx, 228244c65a3SMing Lei struct request *rq) 229244c65a3SMing Lei { 230244c65a3SMing Lei if (rq->tag == -1 || rq->internal_tag == -1) 231244c65a3SMing Lei return; 232244c65a3SMing Lei 233244c65a3SMing Lei __blk_mq_put_driver_tag(hctx, rq); 234244c65a3SMing Lei } 235244c65a3SMing Lei 236244c65a3SMing Lei static inline void blk_mq_put_driver_tag(struct request *rq) 237244c65a3SMing Lei { 238244c65a3SMing Lei if (rq->tag == -1 || rq->internal_tag == -1) 239244c65a3SMing Lei return; 240244c65a3SMing Lei 241ea4f995eSJens Axboe __blk_mq_put_driver_tag(rq->mq_hctx, rq); 242244c65a3SMing Lei } 243244c65a3SMing Lei 244ed76e329SJens Axboe static inline void blk_mq_clear_mq_map(struct blk_mq_queue_map *qmap) 2450da73d00SMinwoo Im { 2460da73d00SMinwoo Im int cpu; 2470da73d00SMinwoo Im 2480da73d00SMinwoo Im for_each_possible_cpu(cpu) 249ed76e329SJens Axboe qmap->mq_map[cpu] = 0; 2500da73d00SMinwoo Im } 2510da73d00SMinwoo Im 252320ae51fSJens Axboe #endif 253