xref: /openbmc/linux/block/blk-mq-virtio.c (revision fa0dadde)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Christoph Hellwig.
4  */
5 #include <linux/device.h>
6 #include <linux/blk-mq-virtio.h>
7 #include <linux/virtio_config.h>
8 #include <linux/module.h>
9 #include "blk-mq.h"
10 
11 /**
12  * blk_mq_virtio_map_queues - provide a default queue mapping for virtio device
13  * @qmap:	CPU to hardware queue map.
14  * @vdev:	virtio device to provide a mapping for.
15  * @first_vec:	first interrupt vectors to use for queues (usually 0)
16  *
17  * This function assumes the virtio device @vdev has at least as many available
18  * interrupt vectors as @set has queues.  It will then query the vector
19  * corresponding to each queue for it's affinity mask and built queue mapping
20  * that maps a queue to the CPUs that have irq affinity for the corresponding
21  * vector.
22  */
23 void blk_mq_virtio_map_queues(struct blk_mq_queue_map *qmap,
24 		struct virtio_device *vdev, int first_vec)
25 {
26 	const struct cpumask *mask;
27 	unsigned int queue, cpu;
28 
29 	if (!vdev->config->get_vq_affinity)
30 		goto fallback;
31 
32 	for (queue = 0; queue < qmap->nr_queues; queue++) {
33 		mask = vdev->config->get_vq_affinity(vdev, first_vec + queue);
34 		if (!mask)
35 			goto fallback;
36 
37 		for_each_cpu(cpu, mask)
38 			qmap->mq_map[cpu] = qmap->queue_offset + queue;
39 	}
40 
41 	return;
42 
43 fallback:
44 	blk_mq_map_queues(qmap);
45 }
46 EXPORT_SYMBOL_GPL(blk_mq_virtio_map_queues);
47