sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / md / dm-rq.c
blob9d7275fb541ad422b171cbe0ee43335e3e1efc27
1 /*
2 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
4 * This file is released under the GPL.
5 */
7 #include "dm-core.h"
8 #include "dm-rq.h"
10 #include <linux/elevator.h> /* for rq_end_sector() */
11 #include <linux/blk-mq.h>
13 #define DM_MSG_PREFIX "core-rq"
15 #define DM_MQ_NR_HW_QUEUES 1
16 #define DM_MQ_QUEUE_DEPTH 2048
17 static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
18 static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
21 * Request-based DM's mempools' reserved IOs set by the user.
23 #define RESERVED_REQUEST_BASED_IOS 256
24 static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
26 static bool use_blk_mq = IS_ENABLED(CONFIG_DM_MQ_DEFAULT);
28 bool dm_use_blk_mq_default(void)
30 return use_blk_mq;
33 bool dm_use_blk_mq(struct mapped_device *md)
35 return md->use_blk_mq;
37 EXPORT_SYMBOL_GPL(dm_use_blk_mq);
39 unsigned dm_get_reserved_rq_based_ios(void)
41 return __dm_get_module_param(&reserved_rq_based_ios,
42 RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
44 EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
46 static unsigned dm_get_blk_mq_nr_hw_queues(void)
48 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
51 static unsigned dm_get_blk_mq_queue_depth(void)
53 return __dm_get_module_param(&dm_mq_queue_depth,
54 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
57 int dm_request_based(struct mapped_device *md)
59 return blk_queue_stackable(md->queue);
62 static void dm_old_start_queue(struct request_queue *q)
64 unsigned long flags;
66 spin_lock_irqsave(q->queue_lock, flags);
67 if (blk_queue_stopped(q))
68 blk_start_queue(q);
69 spin_unlock_irqrestore(q->queue_lock, flags);
72 static void dm_mq_start_queue(struct request_queue *q)
74 blk_mq_start_stopped_hw_queues(q, true);
75 blk_mq_kick_requeue_list(q);
78 void dm_start_queue(struct request_queue *q)
80 if (!q->mq_ops)
81 dm_old_start_queue(q);
82 else
83 dm_mq_start_queue(q);
86 static void dm_old_stop_queue(struct request_queue *q)
88 unsigned long flags;
90 spin_lock_irqsave(q->queue_lock, flags);
91 if (!blk_queue_stopped(q))
92 blk_stop_queue(q);
93 spin_unlock_irqrestore(q->queue_lock, flags);
96 static void dm_mq_stop_queue(struct request_queue *q)
98 if (blk_mq_queue_stopped(q))
99 return;
101 blk_mq_quiesce_queue(q);
104 void dm_stop_queue(struct request_queue *q)
106 if (!q->mq_ops)
107 dm_old_stop_queue(q);
108 else
109 dm_mq_stop_queue(q);
112 static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
113 gfp_t gfp_mask)
115 return mempool_alloc(md->io_pool, gfp_mask);
118 static void free_old_rq_tio(struct dm_rq_target_io *tio)
120 mempool_free(tio, tio->md->io_pool);
123 static struct request *alloc_old_clone_request(struct mapped_device *md,
124 gfp_t gfp_mask)
126 return mempool_alloc(md->rq_pool, gfp_mask);
129 static void free_old_clone_request(struct mapped_device *md, struct request *rq)
131 mempool_free(rq, md->rq_pool);
135 * Partial completion handling for request-based dm
137 static void end_clone_bio(struct bio *clone)
139 struct dm_rq_clone_bio_info *info =
140 container_of(clone, struct dm_rq_clone_bio_info, clone);
141 struct dm_rq_target_io *tio = info->tio;
142 struct bio *bio = info->orig;
143 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
144 int error = clone->bi_error;
146 bio_put(clone);
148 if (tio->error)
150 * An error has already been detected on the request.
151 * Once error occurred, just let clone->end_io() handle
152 * the remainder.
154 return;
155 else if (error) {
157 * Don't notice the error to the upper layer yet.
158 * The error handling decision is made by the target driver,
159 * when the request is completed.
161 tio->error = error;
162 return;
166 * I/O for the bio successfully completed.
167 * Notice the data completion to the upper layer.
171 * bios are processed from the head of the list.
172 * So the completing bio should always be rq->bio.
173 * If it's not, something wrong is happening.
175 if (tio->orig->bio != bio)
176 DMERR("bio completion is going in the middle of the request");
179 * Update the original request.
180 * Do not use blk_end_request() here, because it may complete
181 * the original request before the clone, and break the ordering.
183 blk_update_request(tio->orig, 0, nr_bytes);
186 static struct dm_rq_target_io *tio_from_request(struct request *rq)
188 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
191 static void rq_end_stats(struct mapped_device *md, struct request *orig)
193 if (unlikely(dm_stats_used(&md->stats))) {
194 struct dm_rq_target_io *tio = tio_from_request(orig);
195 tio->duration_jiffies = jiffies - tio->duration_jiffies;
196 dm_stats_account_io(&md->stats, rq_data_dir(orig),
197 blk_rq_pos(orig), tio->n_sectors, true,
198 tio->duration_jiffies, &tio->stats_aux);
203 * Don't touch any member of the md after calling this function because
204 * the md may be freed in dm_put() at the end of this function.
205 * Or do dm_get() before calling this function and dm_put() later.
207 static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
209 struct request_queue *q = md->queue;
210 unsigned long flags;
212 atomic_dec(&md->pending[rw]);
214 /* nudge anyone waiting on suspend queue */
215 if (!md_in_flight(md))
216 wake_up(&md->wait);
219 * Run this off this callpath, as drivers could invoke end_io while
220 * inside their request_fn (and holding the queue lock). Calling
221 * back into ->request_fn() could deadlock attempting to grab the
222 * queue lock again.
224 if (!q->mq_ops && run_queue) {
225 spin_lock_irqsave(q->queue_lock, flags);
226 blk_run_queue_async(q);
227 spin_unlock_irqrestore(q->queue_lock, flags);
231 * dm_put() must be at the end of this function. See the comment above
233 dm_put(md);
236 static void free_rq_clone(struct request *clone)
238 struct dm_rq_target_io *tio = clone->end_io_data;
239 struct mapped_device *md = tio->md;
241 blk_rq_unprep_clone(clone);
244 * It is possible for a clone_old_rq() allocated clone to
245 * get passed in -- it may not yet have a request_queue.
246 * This is known to occur if the error target replaces
247 * a multipath target that has a request_fn queue stacked
248 * on blk-mq queue(s).
250 if (clone->q && clone->q->mq_ops)
251 /* stacked on blk-mq queue(s) */
252 tio->ti->type->release_clone_rq(clone);
253 else if (!md->queue->mq_ops)
254 /* request_fn queue stacked on request_fn queue(s) */
255 free_old_clone_request(md, clone);
257 if (!md->queue->mq_ops)
258 free_old_rq_tio(tio);
262 * Complete the clone and the original request.
263 * Must be called without clone's queue lock held,
264 * see end_clone_request() for more details.
266 static void dm_end_request(struct request *clone, int error)
268 int rw = rq_data_dir(clone);
269 struct dm_rq_target_io *tio = clone->end_io_data;
270 struct mapped_device *md = tio->md;
271 struct request *rq = tio->orig;
273 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
274 rq->errors = clone->errors;
275 rq->resid_len = clone->resid_len;
277 if (rq->sense)
279 * We are using the sense buffer of the original
280 * request.
281 * So setting the length of the sense data is enough.
283 rq->sense_len = clone->sense_len;
286 free_rq_clone(clone);
287 rq_end_stats(md, rq);
288 if (!rq->q->mq_ops)
289 blk_end_request_all(rq, error);
290 else
291 blk_mq_end_request(rq, error);
292 rq_completed(md, rw, true);
295 static void dm_unprep_request(struct request *rq)
297 struct dm_rq_target_io *tio = tio_from_request(rq);
298 struct request *clone = tio->clone;
300 if (!rq->q->mq_ops) {
301 rq->special = NULL;
302 rq->rq_flags &= ~RQF_DONTPREP;
305 if (clone)
306 free_rq_clone(clone);
307 else if (!tio->md->queue->mq_ops)
308 free_old_rq_tio(tio);
312 * Requeue the original request of a clone.
314 static void dm_old_requeue_request(struct request *rq)
316 struct request_queue *q = rq->q;
317 unsigned long flags;
319 spin_lock_irqsave(q->queue_lock, flags);
320 blk_requeue_request(q, rq);
321 blk_run_queue_async(q);
322 spin_unlock_irqrestore(q->queue_lock, flags);
325 static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
327 blk_mq_delay_kick_requeue_list(q, msecs);
330 void dm_mq_kick_requeue_list(struct mapped_device *md)
332 __dm_mq_kick_requeue_list(dm_get_md_queue(md), 0);
334 EXPORT_SYMBOL(dm_mq_kick_requeue_list);
336 static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
338 blk_mq_requeue_request(rq, false);
339 __dm_mq_kick_requeue_list(rq->q, msecs);
342 static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
344 struct mapped_device *md = tio->md;
345 struct request *rq = tio->orig;
346 int rw = rq_data_dir(rq);
348 rq_end_stats(md, rq);
349 dm_unprep_request(rq);
351 if (!rq->q->mq_ops)
352 dm_old_requeue_request(rq);
353 else
354 dm_mq_delay_requeue_request(rq, delay_requeue ? 5000 : 0);
356 rq_completed(md, rw, false);
359 static void dm_done(struct request *clone, int error, bool mapped)
361 int r = error;
362 struct dm_rq_target_io *tio = clone->end_io_data;
363 dm_request_endio_fn rq_end_io = NULL;
365 if (tio->ti) {
366 rq_end_io = tio->ti->type->rq_end_io;
368 if (mapped && rq_end_io)
369 r = rq_end_io(tio->ti, clone, error, &tio->info);
372 if (unlikely(r == -EREMOTEIO && (req_op(clone) == REQ_OP_WRITE_SAME) &&
373 !clone->q->limits.max_write_same_sectors))
374 disable_write_same(tio->md);
376 if (r <= 0)
377 /* The target wants to complete the I/O */
378 dm_end_request(clone, r);
379 else if (r == DM_ENDIO_INCOMPLETE)
380 /* The target will handle the I/O */
381 return;
382 else if (r == DM_ENDIO_REQUEUE)
383 /* The target wants to requeue the I/O */
384 dm_requeue_original_request(tio, false);
385 else {
386 DMWARN("unimplemented target endio return value: %d", r);
387 BUG();
392 * Request completion handler for request-based dm
394 static void dm_softirq_done(struct request *rq)
396 bool mapped = true;
397 struct dm_rq_target_io *tio = tio_from_request(rq);
398 struct request *clone = tio->clone;
399 int rw;
401 if (!clone) {
402 rq_end_stats(tio->md, rq);
403 rw = rq_data_dir(rq);
404 if (!rq->q->mq_ops) {
405 blk_end_request_all(rq, tio->error);
406 rq_completed(tio->md, rw, false);
407 free_old_rq_tio(tio);
408 } else {
409 blk_mq_end_request(rq, tio->error);
410 rq_completed(tio->md, rw, false);
412 return;
415 if (rq->rq_flags & RQF_FAILED)
416 mapped = false;
418 dm_done(clone, tio->error, mapped);
422 * Complete the clone and the original request with the error status
423 * through softirq context.
425 static void dm_complete_request(struct request *rq, int error)
427 struct dm_rq_target_io *tio = tio_from_request(rq);
429 tio->error = error;
430 if (!rq->q->mq_ops)
431 blk_complete_request(rq);
432 else
433 blk_mq_complete_request(rq, error);
437 * Complete the not-mapped clone and the original request with the error status
438 * through softirq context.
439 * Target's rq_end_io() function isn't called.
440 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
442 static void dm_kill_unmapped_request(struct request *rq, int error)
444 rq->rq_flags |= RQF_FAILED;
445 dm_complete_request(rq, error);
449 * Called with the clone's queue lock held (in the case of .request_fn)
451 static void end_clone_request(struct request *clone, int error)
453 struct dm_rq_target_io *tio = clone->end_io_data;
455 if (!clone->q->mq_ops) {
457 * For just cleaning up the information of the queue in which
458 * the clone was dispatched.
459 * The clone is *NOT* freed actually here because it is alloced
460 * from dm own mempool (RQF_ALLOCED isn't set).
462 __blk_put_request(clone->q, clone);
466 * Actual request completion is done in a softirq context which doesn't
467 * hold the clone's queue lock. Otherwise, deadlock could occur because:
468 * - another request may be submitted by the upper level driver
469 * of the stacking during the completion
470 * - the submission which requires queue lock may be done
471 * against this clone's queue
473 dm_complete_request(tio->orig, error);
476 static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
478 int r;
480 if (blk_queue_io_stat(clone->q))
481 clone->rq_flags |= RQF_IO_STAT;
483 clone->start_time = jiffies;
484 r = blk_insert_cloned_request(clone->q, clone);
485 if (r)
486 /* must complete clone in terms of original request */
487 dm_complete_request(rq, r);
490 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
491 void *data)
493 struct dm_rq_target_io *tio = data;
494 struct dm_rq_clone_bio_info *info =
495 container_of(bio, struct dm_rq_clone_bio_info, clone);
497 info->orig = bio_orig;
498 info->tio = tio;
499 bio->bi_end_io = end_clone_bio;
501 return 0;
504 static int setup_clone(struct request *clone, struct request *rq,
505 struct dm_rq_target_io *tio, gfp_t gfp_mask)
507 int r;
509 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
510 dm_rq_bio_constructor, tio);
511 if (r)
512 return r;
514 clone->cmd = rq->cmd;
515 clone->cmd_len = rq->cmd_len;
516 clone->sense = rq->sense;
517 clone->end_io = end_clone_request;
518 clone->end_io_data = tio;
520 tio->clone = clone;
522 return 0;
525 static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
526 struct dm_rq_target_io *tio, gfp_t gfp_mask)
529 * Create clone for use with .request_fn request_queue
531 struct request *clone;
533 clone = alloc_old_clone_request(md, gfp_mask);
534 if (!clone)
535 return NULL;
537 blk_rq_init(NULL, clone);
538 if (setup_clone(clone, rq, tio, gfp_mask)) {
539 /* -ENOMEM */
540 free_old_clone_request(md, clone);
541 return NULL;
544 return clone;
547 static void map_tio_request(struct kthread_work *work);
549 static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
550 struct mapped_device *md)
552 tio->md = md;
553 tio->ti = NULL;
554 tio->clone = NULL;
555 tio->orig = rq;
556 tio->error = 0;
558 * Avoid initializing info for blk-mq; it passes
559 * target-specific data through info.ptr
560 * (see: dm_mq_init_request)
562 if (!md->init_tio_pdu)
563 memset(&tio->info, 0, sizeof(tio->info));
564 if (md->kworker_task)
565 kthread_init_work(&tio->work, map_tio_request);
568 static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
569 struct mapped_device *md,
570 gfp_t gfp_mask)
572 struct dm_rq_target_io *tio;
573 int srcu_idx;
574 struct dm_table *table;
576 tio = alloc_old_rq_tio(md, gfp_mask);
577 if (!tio)
578 return NULL;
580 init_tio(tio, rq, md);
582 table = dm_get_live_table(md, &srcu_idx);
584 * Must clone a request if this .request_fn DM device
585 * is stacked on .request_fn device(s).
587 if (!dm_table_all_blk_mq_devices(table)) {
588 if (!clone_old_rq(rq, md, tio, gfp_mask)) {
589 dm_put_live_table(md, srcu_idx);
590 free_old_rq_tio(tio);
591 return NULL;
594 dm_put_live_table(md, srcu_idx);
596 return tio;
600 * Called with the queue lock held.
602 static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
604 struct mapped_device *md = q->queuedata;
605 struct dm_rq_target_io *tio;
607 if (unlikely(rq->special)) {
608 DMWARN("Already has something in rq->special.");
609 return BLKPREP_KILL;
612 tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
613 if (!tio)
614 return BLKPREP_DEFER;
616 rq->special = tio;
617 rq->rq_flags |= RQF_DONTPREP;
619 return BLKPREP_OK;
623 * Returns:
624 * DM_MAPIO_* : the request has been processed as indicated
625 * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
626 * < 0 : the request was completed due to failure
628 static int map_request(struct dm_rq_target_io *tio)
630 int r;
631 struct dm_target *ti = tio->ti;
632 struct mapped_device *md = tio->md;
633 struct request *rq = tio->orig;
634 struct request *clone = NULL;
636 if (tio->clone) {
637 clone = tio->clone;
638 r = ti->type->map_rq(ti, clone, &tio->info);
639 if (r == DM_MAPIO_DELAY_REQUEUE)
640 return DM_MAPIO_REQUEUE; /* .request_fn requeue is always immediate */
641 } else {
642 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
643 if (r < 0) {
644 /* The target wants to complete the I/O */
645 dm_kill_unmapped_request(rq, r);
646 return r;
648 if (r == DM_MAPIO_REMAPPED &&
649 setup_clone(clone, rq, tio, GFP_ATOMIC)) {
650 /* -ENOMEM */
651 ti->type->release_clone_rq(clone);
652 return DM_MAPIO_REQUEUE;
656 switch (r) {
657 case DM_MAPIO_SUBMITTED:
658 /* The target has taken the I/O to submit by itself later */
659 break;
660 case DM_MAPIO_REMAPPED:
661 /* The target has remapped the I/O so dispatch it */
662 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
663 blk_rq_pos(rq));
664 dm_dispatch_clone_request(clone, rq);
665 break;
666 case DM_MAPIO_REQUEUE:
667 /* The target wants to requeue the I/O */
668 break;
669 case DM_MAPIO_DELAY_REQUEUE:
670 /* The target wants to requeue the I/O after a delay */
671 dm_requeue_original_request(tio, true);
672 break;
673 default:
674 if (r > 0) {
675 DMWARN("unimplemented target map return value: %d", r);
676 BUG();
679 /* The target wants to complete the I/O */
680 dm_kill_unmapped_request(rq, r);
683 return r;
686 static void dm_start_request(struct mapped_device *md, struct request *orig)
688 if (!orig->q->mq_ops)
689 blk_start_request(orig);
690 else
691 blk_mq_start_request(orig);
692 atomic_inc(&md->pending[rq_data_dir(orig)]);
694 if (md->seq_rq_merge_deadline_usecs) {
695 md->last_rq_pos = rq_end_sector(orig);
696 md->last_rq_rw = rq_data_dir(orig);
697 md->last_rq_start_time = ktime_get();
700 if (unlikely(dm_stats_used(&md->stats))) {
701 struct dm_rq_target_io *tio = tio_from_request(orig);
702 tio->duration_jiffies = jiffies;
703 tio->n_sectors = blk_rq_sectors(orig);
704 dm_stats_account_io(&md->stats, rq_data_dir(orig),
705 blk_rq_pos(orig), tio->n_sectors, false, 0,
706 &tio->stats_aux);
710 * Hold the md reference here for the in-flight I/O.
711 * We can't rely on the reference count by device opener,
712 * because the device may be closed during the request completion
713 * when all bios are completed.
714 * See the comment in rq_completed() too.
716 dm_get(md);
719 static void map_tio_request(struct kthread_work *work)
721 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
723 if (map_request(tio) == DM_MAPIO_REQUEUE)
724 dm_requeue_original_request(tio, false);
727 ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
729 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
732 #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
734 ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
735 const char *buf, size_t count)
737 unsigned deadline;
739 if (dm_get_md_type(md) != DM_TYPE_REQUEST_BASED)
740 return count;
742 if (kstrtouint(buf, 10, &deadline))
743 return -EINVAL;
745 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
746 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
748 md->seq_rq_merge_deadline_usecs = deadline;
750 return count;
753 static bool dm_old_request_peeked_before_merge_deadline(struct mapped_device *md)
755 ktime_t kt_deadline;
757 if (!md->seq_rq_merge_deadline_usecs)
758 return false;
760 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
761 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
763 return !ktime_after(ktime_get(), kt_deadline);
767 * q->request_fn for old request-based dm.
768 * Called with the queue lock held.
770 static void dm_old_request_fn(struct request_queue *q)
772 struct mapped_device *md = q->queuedata;
773 struct dm_target *ti = md->immutable_target;
774 struct request *rq;
775 struct dm_rq_target_io *tio;
776 sector_t pos = 0;
778 if (unlikely(!ti)) {
779 int srcu_idx;
780 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
782 ti = dm_table_find_target(map, pos);
783 dm_put_live_table(md, srcu_idx);
787 * For suspend, check blk_queue_stopped() and increment
788 * ->pending within a single queue_lock not to increment the
789 * number of in-flight I/Os after the queue is stopped in
790 * dm_suspend().
792 while (!blk_queue_stopped(q)) {
793 rq = blk_peek_request(q);
794 if (!rq)
795 return;
797 /* always use block 0 to find the target for flushes for now */
798 pos = 0;
799 if (req_op(rq) != REQ_OP_FLUSH)
800 pos = blk_rq_pos(rq);
802 if ((dm_old_request_peeked_before_merge_deadline(md) &&
803 md_in_flight(md) && rq->bio && !bio_multiple_segments(rq->bio) &&
804 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
805 (ti->type->busy && ti->type->busy(ti))) {
806 blk_delay_queue(q, 10);
807 return;
810 dm_start_request(md, rq);
812 tio = tio_from_request(rq);
813 /* Establish tio->ti before queuing work (map_tio_request) */
814 tio->ti = ti;
815 kthread_queue_work(&md->kworker, &tio->work);
816 BUG_ON(!irqs_disabled());
821 * Fully initialize a .request_fn request-based queue.
823 int dm_old_init_request_queue(struct mapped_device *md)
825 /* Fully initialize the queue */
826 if (!blk_init_allocated_queue(md->queue, dm_old_request_fn, NULL))
827 return -EINVAL;
829 /* disable dm_old_request_fn's merge heuristic by default */
830 md->seq_rq_merge_deadline_usecs = 0;
832 dm_init_normal_md_queue(md);
833 blk_queue_softirq_done(md->queue, dm_softirq_done);
834 blk_queue_prep_rq(md->queue, dm_old_prep_fn);
836 /* Initialize the request-based DM worker thread */
837 kthread_init_worker(&md->kworker);
838 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
839 "kdmwork-%s", dm_device_name(md));
840 if (IS_ERR(md->kworker_task)) {
841 int error = PTR_ERR(md->kworker_task);
842 md->kworker_task = NULL;
843 return error;
846 elv_register_queue(md->queue);
848 return 0;
851 static int dm_mq_init_request(void *data, struct request *rq,
852 unsigned int hctx_idx, unsigned int request_idx,
853 unsigned int numa_node)
855 struct mapped_device *md = data;
856 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
859 * Must initialize md member of tio, otherwise it won't
860 * be available in dm_mq_queue_rq.
862 tio->md = md;
864 if (md->init_tio_pdu) {
865 /* target-specific per-io data is immediately after the tio */
866 tio->info.ptr = tio + 1;
869 return 0;
872 static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
873 const struct blk_mq_queue_data *bd)
875 struct request *rq = bd->rq;
876 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
877 struct mapped_device *md = tio->md;
878 struct dm_target *ti = md->immutable_target;
880 if (unlikely(!ti)) {
881 int srcu_idx;
882 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
884 ti = dm_table_find_target(map, 0);
885 dm_put_live_table(md, srcu_idx);
888 if (ti->type->busy && ti->type->busy(ti))
889 return BLK_MQ_RQ_QUEUE_BUSY;
891 dm_start_request(md, rq);
893 /* Init tio using md established in .init_request */
894 init_tio(tio, rq, md);
897 * Establish tio->ti before calling map_request().
899 tio->ti = ti;
901 /* Direct call is fine since .queue_rq allows allocations */
902 if (map_request(tio) == DM_MAPIO_REQUEUE) {
903 /* Undo dm_start_request() before requeuing */
904 rq_end_stats(md, rq);
905 rq_completed(md, rq_data_dir(rq), false);
906 return BLK_MQ_RQ_QUEUE_BUSY;
909 return BLK_MQ_RQ_QUEUE_OK;
912 static struct blk_mq_ops dm_mq_ops = {
913 .queue_rq = dm_mq_queue_rq,
914 .complete = dm_softirq_done,
915 .init_request = dm_mq_init_request,
918 int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
920 struct request_queue *q;
921 struct dm_target *immutable_tgt;
922 int err;
924 if (!dm_table_all_blk_mq_devices(t)) {
925 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
926 return -EINVAL;
929 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
930 if (!md->tag_set)
931 return -ENOMEM;
933 md->tag_set->ops = &dm_mq_ops;
934 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
935 md->tag_set->numa_node = md->numa_node_id;
936 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
937 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
938 md->tag_set->driver_data = md;
940 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
941 immutable_tgt = dm_table_get_immutable_target(t);
942 if (immutable_tgt && immutable_tgt->per_io_data_size) {
943 /* any target-specific per-io data is immediately after the tio */
944 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
945 md->init_tio_pdu = true;
948 err = blk_mq_alloc_tag_set(md->tag_set);
949 if (err)
950 goto out_kfree_tag_set;
952 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
953 if (IS_ERR(q)) {
954 err = PTR_ERR(q);
955 goto out_tag_set;
957 dm_init_md_queue(md);
959 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
960 blk_mq_register_dev(disk_to_dev(md->disk), q);
962 return 0;
964 out_tag_set:
965 blk_mq_free_tag_set(md->tag_set);
966 out_kfree_tag_set:
967 kfree(md->tag_set);
969 return err;
972 void dm_mq_cleanup_mapped_device(struct mapped_device *md)
974 if (md->tag_set) {
975 blk_mq_free_tag_set(md->tag_set);
976 kfree(md->tag_set);
980 module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
981 MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
983 module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
984 MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
986 module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
987 MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
989 module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
990 MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");