spi: Add support pl022 spi driver
[platform/kernel/linux-starfive.git] / block / blk-mq-tag.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Tag allocation using scalable bitmaps. Uses active queue tracking to support
4  * fairer distribution of tags between multiple submitters when a shared tag map
5  * is used.
6  *
7  * Copyright (C) 2013-2014 Jens Axboe
8  */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11
12 #include <linux/blk-mq.h>
13 #include <linux/delay.h>
14 #include "blk.h"
15 #include "blk-mq.h"
16 #include "blk-mq-sched.h"
17 #include "blk-mq-tag.h"
18
19 /*
20  * Recalculate wakeup batch when tag is shared by hctx.
21  */
22 static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
23                 unsigned int users)
24 {
25         if (!users)
26                 return;
27
28         sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
29                         users);
30         sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
31                         users);
32 }
33
34 /*
35  * If a previously inactive queue goes active, bump the active user count.
36  * We need to do this before try to allocate driver tag, then even if fail
37  * to get tag when first time, the other shared-tag users could reserve
38  * budget for it.
39  */
40 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
41 {
42         unsigned int users;
43         struct blk_mq_tags *tags = hctx->tags;
44
45         /*
46          * calling test_bit() prior to test_and_set_bit() is intentional,
47          * it avoids dirtying the cacheline if the queue is already active.
48          */
49         if (blk_mq_is_shared_tags(hctx->flags)) {
50                 struct request_queue *q = hctx->queue;
51
52                 if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags) ||
53                     test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
54                         return;
55         } else {
56                 if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) ||
57                     test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
58                         return;
59         }
60
61         spin_lock_irq(&tags->lock);
62         users = tags->active_queues + 1;
63         WRITE_ONCE(tags->active_queues, users);
64         blk_mq_update_wake_batch(tags, users);
65         spin_unlock_irq(&tags->lock);
66 }
67
68 /*
69  * Wakeup all potentially sleeping on tags
70  */
71 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
72 {
73         sbitmap_queue_wake_all(&tags->bitmap_tags);
74         if (include_reserve)
75                 sbitmap_queue_wake_all(&tags->breserved_tags);
76 }
77
78 /*
79  * If a previously busy queue goes inactive, potential waiters could now
80  * be allowed to queue. Wake them up and check.
81  */
82 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
83 {
84         struct blk_mq_tags *tags = hctx->tags;
85         unsigned int users;
86
87         if (blk_mq_is_shared_tags(hctx->flags)) {
88                 struct request_queue *q = hctx->queue;
89
90                 if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
91                                         &q->queue_flags))
92                         return;
93         } else {
94                 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
95                         return;
96         }
97
98         spin_lock_irq(&tags->lock);
99         users = tags->active_queues - 1;
100         WRITE_ONCE(tags->active_queues, users);
101         blk_mq_update_wake_batch(tags, users);
102         spin_unlock_irq(&tags->lock);
103
104         blk_mq_tag_wakeup_all(tags, false);
105 }
106
107 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
108                             struct sbitmap_queue *bt)
109 {
110         if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
111                         !hctx_may_queue(data->hctx, bt))
112                 return BLK_MQ_NO_TAG;
113
114         if (data->shallow_depth)
115                 return sbitmap_queue_get_shallow(bt, data->shallow_depth);
116         else
117                 return __sbitmap_queue_get(bt);
118 }
119
120 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
121                               unsigned int *offset)
122 {
123         struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
124         struct sbitmap_queue *bt = &tags->bitmap_tags;
125         unsigned long ret;
126
127         if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
128             data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
129                 return 0;
130         ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
131         *offset += tags->nr_reserved_tags;
132         return ret;
133 }
134
135 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
136 {
137         struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
138         struct sbitmap_queue *bt;
139         struct sbq_wait_state *ws;
140         DEFINE_SBQ_WAIT(wait);
141         unsigned int tag_offset;
142         int tag;
143
144         if (data->flags & BLK_MQ_REQ_RESERVED) {
145                 if (unlikely(!tags->nr_reserved_tags)) {
146                         WARN_ON_ONCE(1);
147                         return BLK_MQ_NO_TAG;
148                 }
149                 bt = &tags->breserved_tags;
150                 tag_offset = 0;
151         } else {
152                 bt = &tags->bitmap_tags;
153                 tag_offset = tags->nr_reserved_tags;
154         }
155
156         tag = __blk_mq_get_tag(data, bt);
157         if (tag != BLK_MQ_NO_TAG)
158                 goto found_tag;
159
160         if (data->flags & BLK_MQ_REQ_NOWAIT)
161                 return BLK_MQ_NO_TAG;
162
163         ws = bt_wait_ptr(bt, data->hctx);
164         do {
165                 struct sbitmap_queue *bt_prev;
166
167                 /*
168                  * We're out of tags on this hardware queue, kick any
169                  * pending IO submits before going to sleep waiting for
170                  * some to complete.
171                  */
172                 blk_mq_run_hw_queue(data->hctx, false);
173
174                 /*
175                  * Retry tag allocation after running the hardware queue,
176                  * as running the queue may also have found completions.
177                  */
178                 tag = __blk_mq_get_tag(data, bt);
179                 if (tag != BLK_MQ_NO_TAG)
180                         break;
181
182                 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
183
184                 tag = __blk_mq_get_tag(data, bt);
185                 if (tag != BLK_MQ_NO_TAG)
186                         break;
187
188                 bt_prev = bt;
189                 io_schedule();
190
191                 sbitmap_finish_wait(bt, ws, &wait);
192
193                 data->ctx = blk_mq_get_ctx(data->q);
194                 data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
195                                                 data->ctx);
196                 tags = blk_mq_tags_from_data(data);
197                 if (data->flags & BLK_MQ_REQ_RESERVED)
198                         bt = &tags->breserved_tags;
199                 else
200                         bt = &tags->bitmap_tags;
201
202                 /*
203                  * If destination hw queue is changed, fake wake up on
204                  * previous queue for compensating the wake up miss, so
205                  * other allocations on previous queue won't be starved.
206                  */
207                 if (bt != bt_prev)
208                         sbitmap_queue_wake_up(bt_prev, 1);
209
210                 ws = bt_wait_ptr(bt, data->hctx);
211         } while (1);
212
213         sbitmap_finish_wait(bt, ws, &wait);
214
215 found_tag:
216         /*
217          * Give up this allocation if the hctx is inactive.  The caller will
218          * retry on an active hctx.
219          */
220         if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
221                 blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
222                 return BLK_MQ_NO_TAG;
223         }
224         return tag + tag_offset;
225 }
226
227 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
228                     unsigned int tag)
229 {
230         if (!blk_mq_tag_is_reserved(tags, tag)) {
231                 const int real_tag = tag - tags->nr_reserved_tags;
232
233                 BUG_ON(real_tag >= tags->nr_tags);
234                 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
235         } else {
236                 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
237         }
238 }
239
240 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
241 {
242         sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
243                                         tag_array, nr_tags);
244 }
245
246 struct bt_iter_data {
247         struct blk_mq_hw_ctx *hctx;
248         struct request_queue *q;
249         busy_tag_iter_fn *fn;
250         void *data;
251         bool reserved;
252 };
253
254 static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
255                 unsigned int bitnr)
256 {
257         struct request *rq;
258         unsigned long flags;
259
260         spin_lock_irqsave(&tags->lock, flags);
261         rq = tags->rqs[bitnr];
262         if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
263                 rq = NULL;
264         spin_unlock_irqrestore(&tags->lock, flags);
265         return rq;
266 }
267
268 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
269 {
270         struct bt_iter_data *iter_data = data;
271         struct blk_mq_hw_ctx *hctx = iter_data->hctx;
272         struct request_queue *q = iter_data->q;
273         struct blk_mq_tag_set *set = q->tag_set;
274         struct blk_mq_tags *tags;
275         struct request *rq;
276         bool ret = true;
277
278         if (blk_mq_is_shared_tags(set->flags))
279                 tags = set->shared_tags;
280         else
281                 tags = hctx->tags;
282
283         if (!iter_data->reserved)
284                 bitnr += tags->nr_reserved_tags;
285         /*
286          * We can hit rq == NULL here, because the tagging functions
287          * test and set the bit before assigning ->rqs[].
288          */
289         rq = blk_mq_find_and_get_req(tags, bitnr);
290         if (!rq)
291                 return true;
292
293         if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
294                 ret = iter_data->fn(rq, iter_data->data);
295         blk_mq_put_rq_ref(rq);
296         return ret;
297 }
298
299 /**
300  * bt_for_each - iterate over the requests associated with a hardware queue
301  * @hctx:       Hardware queue to examine.
302  * @q:          Request queue to examine.
303  * @bt:         sbitmap to examine. This is either the breserved_tags member
304  *              or the bitmap_tags member of struct blk_mq_tags.
305  * @fn:         Pointer to the function that will be called for each request
306  *              associated with @hctx that has been assigned a driver tag.
307  *              @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
308  *              where rq is a pointer to a request. Return true to continue
309  *              iterating tags, false to stop.
310  * @data:       Will be passed as third argument to @fn.
311  * @reserved:   Indicates whether @bt is the breserved_tags member or the
312  *              bitmap_tags member of struct blk_mq_tags.
313  */
314 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
315                         struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
316                         void *data, bool reserved)
317 {
318         struct bt_iter_data iter_data = {
319                 .hctx = hctx,
320                 .fn = fn,
321                 .data = data,
322                 .reserved = reserved,
323                 .q = q,
324         };
325
326         sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
327 }
328
329 struct bt_tags_iter_data {
330         struct blk_mq_tags *tags;
331         busy_tag_iter_fn *fn;
332         void *data;
333         unsigned int flags;
334 };
335
336 #define BT_TAG_ITER_RESERVED            (1 << 0)
337 #define BT_TAG_ITER_STARTED             (1 << 1)
338 #define BT_TAG_ITER_STATIC_RQS          (1 << 2)
339
340 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
341 {
342         struct bt_tags_iter_data *iter_data = data;
343         struct blk_mq_tags *tags = iter_data->tags;
344         struct request *rq;
345         bool ret = true;
346         bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
347
348         if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
349                 bitnr += tags->nr_reserved_tags;
350
351         /*
352          * We can hit rq == NULL here, because the tagging functions
353          * test and set the bit before assigning ->rqs[].
354          */
355         if (iter_static_rqs)
356                 rq = tags->static_rqs[bitnr];
357         else
358                 rq = blk_mq_find_and_get_req(tags, bitnr);
359         if (!rq)
360                 return true;
361
362         if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
363             blk_mq_request_started(rq))
364                 ret = iter_data->fn(rq, iter_data->data);
365         if (!iter_static_rqs)
366                 blk_mq_put_rq_ref(rq);
367         return ret;
368 }
369
370 /**
371  * bt_tags_for_each - iterate over the requests in a tag map
372  * @tags:       Tag map to iterate over.
373  * @bt:         sbitmap to examine. This is either the breserved_tags member
374  *              or the bitmap_tags member of struct blk_mq_tags.
375  * @fn:         Pointer to the function that will be called for each started
376  *              request. @fn will be called as follows: @fn(rq, @data,
377  *              @reserved) where rq is a pointer to a request. Return true
378  *              to continue iterating tags, false to stop.
379  * @data:       Will be passed as second argument to @fn.
380  * @flags:      BT_TAG_ITER_*
381  */
382 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
383                              busy_tag_iter_fn *fn, void *data, unsigned int flags)
384 {
385         struct bt_tags_iter_data iter_data = {
386                 .tags = tags,
387                 .fn = fn,
388                 .data = data,
389                 .flags = flags,
390         };
391
392         if (tags->rqs)
393                 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
394 }
395
396 static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
397                 busy_tag_iter_fn *fn, void *priv, unsigned int flags)
398 {
399         WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
400
401         if (tags->nr_reserved_tags)
402                 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
403                                  flags | BT_TAG_ITER_RESERVED);
404         bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
405 }
406
407 /**
408  * blk_mq_all_tag_iter - iterate over all requests in a tag map
409  * @tags:       Tag map to iterate over.
410  * @fn:         Pointer to the function that will be called for each
411  *              request. @fn will be called as follows: @fn(rq, @priv,
412  *              reserved) where rq is a pointer to a request. 'reserved'
413  *              indicates whether or not @rq is a reserved request. Return
414  *              true to continue iterating tags, false to stop.
415  * @priv:       Will be passed as second argument to @fn.
416  *
417  * Caller has to pass the tag map from which requests are allocated.
418  */
419 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
420                 void *priv)
421 {
422         __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
423 }
424
425 /**
426  * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
427  * @tagset:     Tag set to iterate over.
428  * @fn:         Pointer to the function that will be called for each started
429  *              request. @fn will be called as follows: @fn(rq, @priv,
430  *              reserved) where rq is a pointer to a request. 'reserved'
431  *              indicates whether or not @rq is a reserved request. Return
432  *              true to continue iterating tags, false to stop.
433  * @priv:       Will be passed as second argument to @fn.
434  *
435  * We grab one request reference before calling @fn and release it after
436  * @fn returns.
437  */
438 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
439                 busy_tag_iter_fn *fn, void *priv)
440 {
441         unsigned int flags = tagset->flags;
442         int i, nr_tags;
443
444         nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
445
446         for (i = 0; i < nr_tags; i++) {
447                 if (tagset->tags && tagset->tags[i])
448                         __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
449                                               BT_TAG_ITER_STARTED);
450         }
451 }
452 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
453
454 static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
455 {
456         unsigned *count = data;
457
458         if (blk_mq_request_completed(rq))
459                 (*count)++;
460         return true;
461 }
462
463 /**
464  * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
465  * completions have finished.
466  * @tagset:     Tag set to drain completed request
467  *
468  * Note: This function has to be run after all IO queues are shutdown
469  */
470 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
471 {
472         while (true) {
473                 unsigned count = 0;
474
475                 blk_mq_tagset_busy_iter(tagset,
476                                 blk_mq_tagset_count_completed_rqs, &count);
477                 if (!count)
478                         break;
479                 msleep(5);
480         }
481 }
482 EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
483
484 /**
485  * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
486  * @q:          Request queue to examine.
487  * @fn:         Pointer to the function that will be called for each request
488  *              on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
489  *              reserved) where rq is a pointer to a request and hctx points
490  *              to the hardware queue associated with the request. 'reserved'
491  *              indicates whether or not @rq is a reserved request.
492  * @priv:       Will be passed as third argument to @fn.
493  *
494  * Note: if @q->tag_set is shared with other request queues then @fn will be
495  * called for all requests on all queues that share that tag set and not only
496  * for requests associated with @q.
497  */
498 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
499                 void *priv)
500 {
501         /*
502          * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
503          * while the queue is frozen. So we can use q_usage_counter to avoid
504          * racing with it.
505          */
506         if (!percpu_ref_tryget(&q->q_usage_counter))
507                 return;
508
509         if (blk_mq_is_shared_tags(q->tag_set->flags)) {
510                 struct blk_mq_tags *tags = q->tag_set->shared_tags;
511                 struct sbitmap_queue *bresv = &tags->breserved_tags;
512                 struct sbitmap_queue *btags = &tags->bitmap_tags;
513
514                 if (tags->nr_reserved_tags)
515                         bt_for_each(NULL, q, bresv, fn, priv, true);
516                 bt_for_each(NULL, q, btags, fn, priv, false);
517         } else {
518                 struct blk_mq_hw_ctx *hctx;
519                 unsigned long i;
520
521                 queue_for_each_hw_ctx(q, hctx, i) {
522                         struct blk_mq_tags *tags = hctx->tags;
523                         struct sbitmap_queue *bresv = &tags->breserved_tags;
524                         struct sbitmap_queue *btags = &tags->bitmap_tags;
525
526                         /*
527                          * If no software queues are currently mapped to this
528                          * hardware queue, there's nothing to check
529                          */
530                         if (!blk_mq_hw_queue_mapped(hctx))
531                                 continue;
532
533                         if (tags->nr_reserved_tags)
534                                 bt_for_each(hctx, q, bresv, fn, priv, true);
535                         bt_for_each(hctx, q, btags, fn, priv, false);
536                 }
537         }
538         blk_queue_exit(q);
539 }
540
541 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
542                     bool round_robin, int node)
543 {
544         return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
545                                        node);
546 }
547
548 int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
549                         struct sbitmap_queue *breserved_tags,
550                         unsigned int queue_depth, unsigned int reserved,
551                         int node, int alloc_policy)
552 {
553         unsigned int depth = queue_depth - reserved;
554         bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
555
556         if (bt_alloc(bitmap_tags, depth, round_robin, node))
557                 return -ENOMEM;
558         if (bt_alloc(breserved_tags, reserved, round_robin, node))
559                 goto free_bitmap_tags;
560
561         return 0;
562
563 free_bitmap_tags:
564         sbitmap_queue_free(bitmap_tags);
565         return -ENOMEM;
566 }
567
568 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
569                                      unsigned int reserved_tags,
570                                      int node, int alloc_policy)
571 {
572         struct blk_mq_tags *tags;
573
574         if (total_tags > BLK_MQ_TAG_MAX) {
575                 pr_err("blk-mq: tag depth too large\n");
576                 return NULL;
577         }
578
579         tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
580         if (!tags)
581                 return NULL;
582
583         tags->nr_tags = total_tags;
584         tags->nr_reserved_tags = reserved_tags;
585         spin_lock_init(&tags->lock);
586
587         if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
588                                 total_tags, reserved_tags, node,
589                                 alloc_policy) < 0) {
590                 kfree(tags);
591                 return NULL;
592         }
593         return tags;
594 }
595
596 void blk_mq_free_tags(struct blk_mq_tags *tags)
597 {
598         sbitmap_queue_free(&tags->bitmap_tags);
599         sbitmap_queue_free(&tags->breserved_tags);
600         kfree(tags);
601 }
602
603 int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
604                             struct blk_mq_tags **tagsptr, unsigned int tdepth,
605                             bool can_grow)
606 {
607         struct blk_mq_tags *tags = *tagsptr;
608
609         if (tdepth <= tags->nr_reserved_tags)
610                 return -EINVAL;
611
612         /*
613          * If we are allowed to grow beyond the original size, allocate
614          * a new set of tags before freeing the old one.
615          */
616         if (tdepth > tags->nr_tags) {
617                 struct blk_mq_tag_set *set = hctx->queue->tag_set;
618                 struct blk_mq_tags *new;
619
620                 if (!can_grow)
621                         return -EINVAL;
622
623                 /*
624                  * We need some sort of upper limit, set it high enough that
625                  * no valid use cases should require more.
626                  */
627                 if (tdepth > MAX_SCHED_RQ)
628                         return -EINVAL;
629
630                 /*
631                  * Only the sbitmap needs resizing since we allocated the max
632                  * initially.
633                  */
634                 if (blk_mq_is_shared_tags(set->flags))
635                         return 0;
636
637                 new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
638                 if (!new)
639                         return -ENOMEM;
640
641                 blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
642                 *tagsptr = new;
643         } else {
644                 /*
645                  * Don't need (or can't) update reserved tags here, they
646                  * remain static and should never need resizing.
647                  */
648                 sbitmap_queue_resize(&tags->bitmap_tags,
649                                 tdepth - tags->nr_reserved_tags);
650         }
651
652         return 0;
653 }
654
655 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
656 {
657         struct blk_mq_tags *tags = set->shared_tags;
658
659         sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
660 }
661
662 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
663 {
664         sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
665                              q->nr_requests - q->tag_set->reserved_tags);
666 }
667
668 /**
669  * blk_mq_unique_tag() - return a tag that is unique queue-wide
670  * @rq: request for which to compute a unique tag
671  *
672  * The tag field in struct request is unique per hardware queue but not over
673  * all hardware queues. Hence this function that returns a tag with the
674  * hardware context index in the upper bits and the per hardware queue tag in
675  * the lower bits.
676  *
677  * Note: When called for a request that is queued on a non-multiqueue request
678  * queue, the hardware context index is set to zero.
679  */
680 u32 blk_mq_unique_tag(struct request *rq)
681 {
682         return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
683                 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
684 }
685 EXPORT_SYMBOL(blk_mq_unique_tag);