1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtio PCI driver - modern (virtio 1.0) device support
5 * This module allows virtio devices to be used over a virtual PCI device.
6 * This can be used with QEMU based VMMs like KVM or Xen.
8 * Copyright IBM Corp. 2007
9 * Copyright Red Hat, Inc. 2014
12 * Anthony Liguori <aliguori@us.ibm.com>
13 * Rusty Russell <rusty@rustcorp.com.au>
14 * Michael S. Tsirkin <mst@redhat.com>
17 #include <linux/delay.h>
18 #include <linux/virtio_pci_admin.h>
19 #define VIRTIO_PCI_NO_LEGACY
20 #define VIRTIO_RING_NO_LEGACY
21 #include "virtio_pci_common.h"
23 #define VIRTIO_AVQ_SGS_MAX 4
25 static u64
vp_get_features(struct virtio_device
*vdev
)
27 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
29 return vp_modern_get_features(&vp_dev
->mdev
);
32 static int vp_avq_index(struct virtio_device
*vdev
, u16
*index
, u16
*num
)
34 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
37 if (!virtio_has_feature(vdev
, VIRTIO_F_ADMIN_VQ
))
40 *num
= vp_modern_avq_num(&vp_dev
->mdev
);
43 *index
= vp_modern_avq_index(&vp_dev
->mdev
);
47 void vp_modern_avq_done(struct virtqueue
*vq
)
49 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
50 struct virtio_pci_admin_vq
*admin_vq
= &vp_dev
->admin_vq
;
51 struct virtio_admin_cmd
*cmd
;
55 spin_lock_irqsave(&admin_vq
->lock
, flags
);
57 virtqueue_disable_cb(vq
);
58 while ((cmd
= virtqueue_get_buf(vq
, &len
))) {
59 cmd
->result_sg_size
= len
;
60 complete(&cmd
->completion
);
62 } while (!virtqueue_enable_cb(vq
));
63 spin_unlock_irqrestore(&admin_vq
->lock
, flags
);
66 static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq
*admin_vq
,
68 struct scatterlist
**sgs
,
71 struct virtio_admin_cmd
*cmd
)
77 vq
= admin_vq
->info
->vq
;
81 if (opcode
!= VIRTIO_ADMIN_CMD_LIST_QUERY
&&
82 opcode
!= VIRTIO_ADMIN_CMD_LIST_USE
&&
83 !((1ULL << opcode
) & admin_vq
->supported_cmds
))
86 init_completion(&cmd
->completion
);
89 if (virtqueue_is_broken(vq
))
92 spin_lock_irqsave(&admin_vq
->lock
, flags
);
93 ret
= virtqueue_add_sgs(vq
, sgs
, out_num
, in_num
, cmd
, GFP_KERNEL
);
96 spin_unlock_irqrestore(&admin_vq
->lock
, flags
);
102 if (!virtqueue_kick(vq
))
104 spin_unlock_irqrestore(&admin_vq
->lock
, flags
);
106 wait_for_completion(&cmd
->completion
);
111 spin_unlock_irqrestore(&admin_vq
->lock
, flags
);
115 int vp_modern_admin_cmd_exec(struct virtio_device
*vdev
,
116 struct virtio_admin_cmd
*cmd
)
118 struct scatterlist
*sgs
[VIRTIO_AVQ_SGS_MAX
], hdr
, stat
;
119 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
120 struct virtio_admin_cmd_status
*va_status
;
121 unsigned int out_num
= 0, in_num
= 0;
122 struct virtio_admin_cmd_hdr
*va_hdr
;
126 if (!virtio_has_feature(vdev
, VIRTIO_F_ADMIN_VQ
))
129 va_status
= kzalloc(sizeof(*va_status
), GFP_KERNEL
);
133 va_hdr
= kzalloc(sizeof(*va_hdr
), GFP_KERNEL
);
139 va_hdr
->opcode
= cmd
->opcode
;
140 va_hdr
->group_type
= cmd
->group_type
;
141 va_hdr
->group_member_id
= cmd
->group_member_id
;
144 sg_init_one(&hdr
, va_hdr
, sizeof(*va_hdr
));
149 sgs
[out_num
] = cmd
->data_sg
;
153 /* Add return status */
154 sg_init_one(&stat
, va_status
, sizeof(*va_status
));
155 sgs
[out_num
+ in_num
] = &stat
;
158 if (cmd
->result_sg
) {
159 sgs
[out_num
+ in_num
] = cmd
->result_sg
;
163 ret
= virtqueue_exec_admin_cmd(&vp_dev
->admin_vq
,
164 le16_to_cpu(cmd
->opcode
),
165 sgs
, out_num
, in_num
, cmd
);
168 "Failed to execute command on admin vq: %d\n.", ret
);
172 status
= le16_to_cpu(va_status
->status
);
173 if (status
!= VIRTIO_ADMIN_STATUS_OK
) {
175 "admin command error: status(%#x) qualifier(%#x)\n",
176 status
, le16_to_cpu(va_status
->status_qualifier
));
187 static void virtio_pci_admin_cmd_list_init(struct virtio_device
*virtio_dev
)
189 struct virtio_pci_device
*vp_dev
= to_vp_device(virtio_dev
);
190 struct virtio_admin_cmd cmd
= {};
191 struct scatterlist result_sg
;
192 struct scatterlist data_sg
;
196 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
200 sg_init_one(&result_sg
, data
, sizeof(*data
));
201 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_QUERY
);
202 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
203 cmd
.result_sg
= &result_sg
;
205 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
209 *data
&= cpu_to_le64(VIRTIO_ADMIN_CMD_BITMAP
);
210 sg_init_one(&data_sg
, data
, sizeof(*data
));
211 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_USE
);
212 cmd
.data_sg
= &data_sg
;
213 cmd
.result_sg
= NULL
;
215 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
219 vp_dev
->admin_vq
.supported_cmds
= le64_to_cpu(*data
);
225 virtio_pci_admin_cmd_dev_parts_objects_enable(struct virtio_device
*virtio_dev
)
227 struct virtio_pci_device
*vp_dev
= to_vp_device(virtio_dev
);
228 struct virtio_admin_cmd_cap_get_data
*get_data
;
229 struct virtio_admin_cmd_cap_set_data
*set_data
;
230 struct virtio_dev_parts_cap
*result
;
231 struct virtio_admin_cmd cmd
= {};
232 struct scatterlist result_sg
;
233 struct scatterlist data_sg
;
234 u8 resource_objects_limit
;
238 get_data
= kzalloc(sizeof(*get_data
), GFP_KERNEL
);
242 result
= kzalloc(sizeof(*result
), GFP_KERNEL
);
246 get_data
->id
= cpu_to_le16(VIRTIO_DEV_PARTS_CAP
);
247 sg_init_one(&data_sg
, get_data
, sizeof(*get_data
));
248 sg_init_one(&result_sg
, result
, sizeof(*result
));
249 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_DEVICE_CAP_GET
);
250 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
251 cmd
.data_sg
= &data_sg
;
252 cmd
.result_sg
= &result_sg
;
253 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
257 set_data_size
= sizeof(*set_data
) + sizeof(*result
);
258 set_data
= kzalloc(set_data_size
, GFP_KERNEL
);
262 set_data
->id
= cpu_to_le16(VIRTIO_DEV_PARTS_CAP
);
264 /* Set the limit to the minimum value between the GET and SET values
265 * supported by the device. Since the obj_id for VIRTIO_DEV_PARTS_CAP
266 * is a globally unique value per PF, there is no possibility of
267 * overlap between GET and SET operations.
269 resource_objects_limit
= min(result
->get_parts_resource_objects_limit
,
270 result
->set_parts_resource_objects_limit
);
271 result
->get_parts_resource_objects_limit
= resource_objects_limit
;
272 result
->set_parts_resource_objects_limit
= resource_objects_limit
;
273 memcpy(set_data
->cap_specific_data
, result
, sizeof(*result
));
274 sg_init_one(&data_sg
, set_data
, set_data_size
);
275 cmd
.data_sg
= &data_sg
;
276 cmd
.result_sg
= NULL
;
277 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_DRIVER_CAP_SET
);
278 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
282 /* Allocate IDR to manage the dev caps objects */
283 ida_init(&vp_dev
->admin_vq
.dev_parts_ida
);
284 vp_dev
->admin_vq
.max_dev_parts_objects
= resource_objects_limit
;
294 static void virtio_pci_admin_cmd_cap_init(struct virtio_device
*virtio_dev
)
296 struct virtio_pci_device
*vp_dev
= to_vp_device(virtio_dev
);
297 struct virtio_admin_cmd_query_cap_id_result
*data
;
298 struct virtio_admin_cmd cmd
= {};
299 struct scatterlist result_sg
;
302 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
306 sg_init_one(&result_sg
, data
, sizeof(*data
));
307 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY
);
308 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
309 cmd
.result_sg
= &result_sg
;
311 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
315 /* Max number of caps fits into a single u64 */
316 BUILD_BUG_ON(sizeof(data
->supported_caps
) > sizeof(u64
));
318 vp_dev
->admin_vq
.supported_caps
= le64_to_cpu(data
->supported_caps
[0]);
320 if (!(vp_dev
->admin_vq
.supported_caps
& (1 << VIRTIO_DEV_PARTS_CAP
)))
323 virtio_pci_admin_cmd_dev_parts_objects_enable(virtio_dev
);
328 static void vp_modern_avq_activate(struct virtio_device
*vdev
)
330 if (!virtio_has_feature(vdev
, VIRTIO_F_ADMIN_VQ
))
333 virtio_pci_admin_cmd_list_init(vdev
);
334 virtio_pci_admin_cmd_cap_init(vdev
);
337 static void vp_modern_avq_cleanup(struct virtio_device
*vdev
)
339 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
340 struct virtio_admin_cmd
*cmd
;
341 struct virtqueue
*vq
;
343 if (!virtio_has_feature(vdev
, VIRTIO_F_ADMIN_VQ
))
346 vq
= vp_dev
->admin_vq
.info
->vq
;
350 while ((cmd
= virtqueue_detach_unused_buf(vq
))) {
352 complete(&cmd
->completion
);
356 static void vp_transport_features(struct virtio_device
*vdev
, u64 features
)
358 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
359 struct pci_dev
*pci_dev
= vp_dev
->pci_dev
;
361 if ((features
& BIT_ULL(VIRTIO_F_SR_IOV
)) &&
362 pci_find_ext_capability(pci_dev
, PCI_EXT_CAP_ID_SRIOV
))
363 __virtio_set_bit(vdev
, VIRTIO_F_SR_IOV
);
365 if (features
& BIT_ULL(VIRTIO_F_RING_RESET
))
366 __virtio_set_bit(vdev
, VIRTIO_F_RING_RESET
);
368 if (features
& BIT_ULL(VIRTIO_F_ADMIN_VQ
))
369 __virtio_set_bit(vdev
, VIRTIO_F_ADMIN_VQ
);
372 static int __vp_check_common_size_one_feature(struct virtio_device
*vdev
, u32 fbit
,
373 u32 offset
, const char *fname
)
375 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
377 if (!__virtio_test_bit(vdev
, fbit
))
380 if (likely(vp_dev
->mdev
.common_len
>= offset
))
384 "virtio: common cfg size(%zu) does not match the feature %s\n",
385 vp_dev
->mdev
.common_len
, fname
);
390 #define vp_check_common_size_one_feature(vdev, fbit, field) \
391 __vp_check_common_size_one_feature(vdev, fbit, \
392 offsetofend(struct virtio_pci_modern_common_cfg, field), #fbit)
394 static int vp_check_common_size(struct virtio_device
*vdev
)
396 if (vp_check_common_size_one_feature(vdev
, VIRTIO_F_NOTIF_CONFIG_DATA
, queue_notify_data
))
399 if (vp_check_common_size_one_feature(vdev
, VIRTIO_F_RING_RESET
, queue_reset
))
402 if (vp_check_common_size_one_feature(vdev
, VIRTIO_F_ADMIN_VQ
, admin_queue_num
))
408 /* virtio config->finalize_features() implementation */
409 static int vp_finalize_features(struct virtio_device
*vdev
)
411 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
412 u64 features
= vdev
->features
;
414 /* Give virtio_ring a chance to accept features. */
415 vring_transport_features(vdev
);
417 /* Give virtio_pci a chance to accept features. */
418 vp_transport_features(vdev
, features
);
420 if (!__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
421 dev_err(&vdev
->dev
, "virtio: device uses modern interface "
422 "but does not have VIRTIO_F_VERSION_1\n");
426 if (vp_check_common_size(vdev
))
429 vp_modern_set_features(&vp_dev
->mdev
, vdev
->features
);
434 /* virtio config->get() implementation */
435 static void vp_get(struct virtio_device
*vdev
, unsigned int offset
,
436 void *buf
, unsigned int len
)
438 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
439 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
440 void __iomem
*device
= mdev
->device
;
445 BUG_ON(offset
+ len
> mdev
->device_len
);
449 b
= ioread8(device
+ offset
);
450 memcpy(buf
, &b
, sizeof b
);
453 w
= cpu_to_le16(ioread16(device
+ offset
));
454 memcpy(buf
, &w
, sizeof w
);
457 l
= cpu_to_le32(ioread32(device
+ offset
));
458 memcpy(buf
, &l
, sizeof l
);
461 l
= cpu_to_le32(ioread32(device
+ offset
));
462 memcpy(buf
, &l
, sizeof l
);
463 l
= cpu_to_le32(ioread32(device
+ offset
+ sizeof l
));
464 memcpy(buf
+ sizeof l
, &l
, sizeof l
);
471 /* the config->set() implementation. it's symmetric to the config->get()
473 static void vp_set(struct virtio_device
*vdev
, unsigned int offset
,
474 const void *buf
, unsigned int len
)
476 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
477 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
478 void __iomem
*device
= mdev
->device
;
483 BUG_ON(offset
+ len
> mdev
->device_len
);
487 memcpy(&b
, buf
, sizeof b
);
488 iowrite8(b
, device
+ offset
);
491 memcpy(&w
, buf
, sizeof w
);
492 iowrite16(le16_to_cpu(w
), device
+ offset
);
495 memcpy(&l
, buf
, sizeof l
);
496 iowrite32(le32_to_cpu(l
), device
+ offset
);
499 memcpy(&l
, buf
, sizeof l
);
500 iowrite32(le32_to_cpu(l
), device
+ offset
);
501 memcpy(&l
, buf
+ sizeof l
, sizeof l
);
502 iowrite32(le32_to_cpu(l
), device
+ offset
+ sizeof l
);
509 static u32
vp_generation(struct virtio_device
*vdev
)
511 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
513 return vp_modern_generation(&vp_dev
->mdev
);
516 /* config->{get,set}_status() implementations */
517 static u8
vp_get_status(struct virtio_device
*vdev
)
519 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
521 return vp_modern_get_status(&vp_dev
->mdev
);
524 static void vp_set_status(struct virtio_device
*vdev
, u8 status
)
526 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
528 /* We should never be setting status to 0. */
530 vp_modern_set_status(&vp_dev
->mdev
, status
);
531 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
)
532 vp_modern_avq_activate(vdev
);
535 static void vp_reset(struct virtio_device
*vdev
)
537 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
538 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
540 /* 0 status means a reset. */
541 vp_modern_set_status(mdev
, 0);
542 /* After writing 0 to device_status, the driver MUST wait for a read of
543 * device_status to return 0 before reinitializing the device.
544 * This will flush out the status write, and flush in device writes,
545 * including MSI-X interrupts, if any.
547 while (vp_modern_get_status(mdev
))
550 vp_modern_avq_cleanup(vdev
);
552 /* Flush pending VQ/configuration callbacks. */
553 vp_synchronize_vectors(vdev
);
556 static int vp_active_vq(struct virtqueue
*vq
, u16 msix_vec
)
558 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
559 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
564 /* activate the queue */
565 vp_modern_set_queue_size(mdev
, index
, virtqueue_get_vring_size(vq
));
566 vp_modern_queue_address(mdev
, index
, virtqueue_get_desc_addr(vq
),
567 virtqueue_get_avail_addr(vq
),
568 virtqueue_get_used_addr(vq
));
570 if (msix_vec
!= VIRTIO_MSI_NO_VECTOR
) {
571 msix_vec
= vp_modern_queue_vector(mdev
, index
, msix_vec
);
572 if (msix_vec
== VIRTIO_MSI_NO_VECTOR
)
579 static int vp_modern_disable_vq_and_reset(struct virtqueue
*vq
)
581 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
582 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
583 struct virtio_pci_vq_info
*info
;
586 if (!virtio_has_feature(vq
->vdev
, VIRTIO_F_RING_RESET
))
589 vp_modern_set_queue_reset(mdev
, vq
->index
);
591 info
= vp_dev
->vqs
[vq
->index
];
593 /* delete vq from irq handler */
594 spin_lock_irqsave(&vp_dev
->lock
, flags
);
595 list_del(&info
->node
);
596 spin_unlock_irqrestore(&vp_dev
->lock
, flags
);
598 INIT_LIST_HEAD(&info
->node
);
600 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
601 __virtqueue_break(vq
);
604 /* For the case where vq has an exclusive irq, call synchronize_irq() to
605 * wait for completion.
607 * note: We can't use disable_irq() since it conflicts with the affinity
608 * managed IRQ that is used by some drivers.
610 if (vp_dev
->per_vq_vectors
&& info
->msix_vector
!= VIRTIO_MSI_NO_VECTOR
)
611 synchronize_irq(pci_irq_vector(vp_dev
->pci_dev
, info
->msix_vector
));
618 static int vp_modern_enable_vq_after_reset(struct virtqueue
*vq
)
620 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
621 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
622 struct virtio_pci_vq_info
*info
;
623 unsigned long flags
, index
;
630 info
= vp_dev
->vqs
[index
];
632 if (vp_modern_get_queue_reset(mdev
, index
))
635 if (vp_modern_get_queue_enable(mdev
, index
))
638 err
= vp_active_vq(vq
, info
->msix_vector
);
643 spin_lock_irqsave(&vp_dev
->lock
, flags
);
644 list_add(&info
->node
, &vp_dev
->virtqueues
);
645 spin_unlock_irqrestore(&vp_dev
->lock
, flags
);
647 INIT_LIST_HEAD(&info
->node
);
650 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
651 __virtqueue_unbreak(vq
);
654 vp_modern_set_queue_enable(&vp_dev
->mdev
, index
, true);
660 static u16
vp_config_vector(struct virtio_pci_device
*vp_dev
, u16 vector
)
662 return vp_modern_config_vector(&vp_dev
->mdev
, vector
);
665 static bool vp_notify_with_data(struct virtqueue
*vq
)
667 u32 data
= vring_notification_data(vq
);
669 iowrite32(data
, (void __iomem
*)vq
->priv
);
674 static struct virtqueue
*setup_vq(struct virtio_pci_device
*vp_dev
,
675 struct virtio_pci_vq_info
*info
,
677 void (*callback
)(struct virtqueue
*vq
),
683 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
684 bool (*notify
)(struct virtqueue
*vq
);
685 struct virtqueue
*vq
;
690 if (__virtio_test_bit(&vp_dev
->vdev
, VIRTIO_F_NOTIFICATION_DATA
))
691 notify
= vp_notify_with_data
;
695 is_avq
= vp_is_avq(&vp_dev
->vdev
, index
);
696 if (index
>= vp_modern_get_num_queues(mdev
) && !is_avq
)
697 return ERR_PTR(-EINVAL
);
699 num
= vp_modern_get_queue_size(mdev
, index
);
700 /* Check if queue is either not available or already active. */
701 if (!num
|| vp_modern_get_queue_enable(mdev
, index
))
702 return ERR_PTR(-ENOENT
);
704 info
->msix_vector
= msix_vec
;
706 /* create the vring */
707 vq
= vring_create_virtqueue(index
, num
,
708 SMP_CACHE_BYTES
, &vp_dev
->vdev
,
710 notify
, callback
, name
);
712 return ERR_PTR(-ENOMEM
);
716 err
= vp_active_vq(vq
, msix_vec
);
720 vq
->priv
= (void __force
*)vp_modern_map_vq_notify(mdev
, index
, NULL
);
729 vring_del_virtqueue(vq
);
733 static int vp_modern_find_vqs(struct virtio_device
*vdev
, unsigned int nvqs
,
734 struct virtqueue
*vqs
[],
735 struct virtqueue_info vqs_info
[],
736 struct irq_affinity
*desc
)
738 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
739 struct virtqueue
*vq
;
740 int rc
= vp_find_vqs(vdev
, nvqs
, vqs
, vqs_info
, desc
);
745 /* Select and activate all queues. Has to be done last: once we do
746 * this, there's no way to go back except reset.
748 list_for_each_entry(vq
, &vdev
->vqs
, list
)
749 vp_modern_set_queue_enable(&vp_dev
->mdev
, vq
->index
, true);
754 static void del_vq(struct virtio_pci_vq_info
*info
)
756 struct virtqueue
*vq
= info
->vq
;
757 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
758 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
760 if (vp_dev
->msix_enabled
)
761 vp_modern_queue_vector(mdev
, vq
->index
,
762 VIRTIO_MSI_NO_VECTOR
);
764 if (!mdev
->notify_base
)
765 pci_iounmap(mdev
->pci_dev
, (void __force __iomem
*)vq
->priv
);
767 vring_del_virtqueue(vq
);
770 static int virtio_pci_find_shm_cap(struct pci_dev
*dev
, u8 required_id
,
771 u8
*bar
, u64
*offset
, u64
*len
)
775 for (pos
= pci_find_capability(dev
, PCI_CAP_ID_VNDR
); pos
> 0;
776 pos
= pci_find_next_capability(dev
, pos
, PCI_CAP_ID_VNDR
)) {
777 u8 type
, cap_len
, id
, res_bar
;
779 u64 res_offset
, res_length
;
781 pci_read_config_byte(dev
, pos
+ offsetof(struct virtio_pci_cap
,
783 if (type
!= VIRTIO_PCI_CAP_SHARED_MEMORY_CFG
)
786 pci_read_config_byte(dev
, pos
+ offsetof(struct virtio_pci_cap
,
788 if (cap_len
!= sizeof(struct virtio_pci_cap64
)) {
789 dev_err(&dev
->dev
, "%s: shm cap with bad size offset:"
790 " %d size: %d\n", __func__
, pos
, cap_len
);
794 pci_read_config_byte(dev
, pos
+ offsetof(struct virtio_pci_cap
,
796 if (id
!= required_id
)
799 pci_read_config_byte(dev
, pos
+ offsetof(struct virtio_pci_cap
,
801 if (res_bar
>= PCI_STD_NUM_BARS
)
804 /* Type and ID match, and the BAR value isn't reserved.
808 /* Read the lower 32bit of length and offset */
809 pci_read_config_dword(dev
, pos
+ offsetof(struct virtio_pci_cap
,
812 pci_read_config_dword(dev
, pos
+ offsetof(struct virtio_pci_cap
,
816 /* and now the top half */
817 pci_read_config_dword(dev
,
818 pos
+ offsetof(struct virtio_pci_cap64
,
820 res_offset
|= ((u64
)tmp32
) << 32;
821 pci_read_config_dword(dev
,
822 pos
+ offsetof(struct virtio_pci_cap64
,
824 res_length
|= ((u64
)tmp32
) << 32;
827 *offset
= res_offset
;
835 static bool vp_get_shm_region(struct virtio_device
*vdev
,
836 struct virtio_shm_region
*region
, u8 id
)
838 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
839 struct pci_dev
*pci_dev
= vp_dev
->pci_dev
;
842 phys_addr_t phys_addr
;
845 if (!virtio_pci_find_shm_cap(pci_dev
, id
, &bar
, &offset
, &len
))
848 phys_addr
= pci_resource_start(pci_dev
, bar
);
849 bar_len
= pci_resource_len(pci_dev
, bar
);
851 if ((offset
+ len
) < offset
) {
852 dev_err(&pci_dev
->dev
, "%s: cap offset+len overflow detected\n",
857 if (offset
+ len
> bar_len
) {
858 dev_err(&pci_dev
->dev
, "%s: bar shorter than cap offset+len\n",
864 region
->addr
= (u64
) phys_addr
+ offset
;
870 * virtio_pci_admin_has_dev_parts - Checks whether the device parts
871 * functionality is supported
874 * Returns true on success.
876 bool virtio_pci_admin_has_dev_parts(struct pci_dev
*pdev
)
878 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
879 struct virtio_pci_device
*vp_dev
;
884 if (!virtio_has_feature(virtio_dev
, VIRTIO_F_ADMIN_VQ
))
887 vp_dev
= to_vp_device(virtio_dev
);
889 if (!((vp_dev
->admin_vq
.supported_cmds
& VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP
) ==
890 VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP
))
893 return vp_dev
->admin_vq
.max_dev_parts_objects
;
895 EXPORT_SYMBOL_GPL(virtio_pci_admin_has_dev_parts
);
898 * virtio_pci_admin_mode_set - Sets the mode of a member device
900 * @flags: device mode's flags
902 * Note: caller must serialize access for the given device.
903 * Returns 0 on success, or negative on failure.
905 int virtio_pci_admin_mode_set(struct pci_dev
*pdev
, u8 flags
)
907 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
908 struct virtio_admin_cmd_dev_mode_set_data
*data
;
909 struct virtio_admin_cmd cmd
= {};
910 struct scatterlist data_sg
;
917 vf_id
= pci_iov_vf_id(pdev
);
921 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
926 sg_init_one(&data_sg
, data
, sizeof(*data
));
927 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_MODE_SET
);
928 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
929 cmd
.group_member_id
= cpu_to_le64(vf_id
+ 1);
930 cmd
.data_sg
= &data_sg
;
931 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
936 EXPORT_SYMBOL_GPL(virtio_pci_admin_mode_set
);
939 * virtio_pci_admin_obj_create - Creates an object for a given type and operation,
940 * following the max objects that can be created for that request.
942 * @obj_type: Object type
943 * @operation_type: Operation type
944 * @obj_id: Output unique object id
946 * Note: caller must serialize access for the given device.
947 * Returns 0 on success, or negative on failure.
949 int virtio_pci_admin_obj_create(struct pci_dev
*pdev
, u16 obj_type
, u8 operation_type
,
952 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
953 u16 data_size
= sizeof(struct virtio_admin_cmd_resource_obj_create_data
);
954 struct virtio_admin_cmd_resource_obj_create_data
*obj_create_data
;
955 struct virtio_resource_obj_dev_parts obj_dev_parts
= {};
956 struct virtio_pci_admin_vq
*avq
;
957 struct virtio_admin_cmd cmd
= {};
958 struct scatterlist data_sg
;
967 vf_id
= pci_iov_vf_id(pdev
);
971 if (obj_type
!= VIRTIO_RESOURCE_OBJ_DEV_PARTS
)
974 if (operation_type
!= VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET
&&
975 operation_type
!= VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET
)
978 avq
= &to_vp_device(virtio_dev
)->admin_vq
;
979 if (!avq
->max_dev_parts_objects
)
982 id
= ida_alloc_range(&avq
->dev_parts_ida
, 0,
983 avq
->max_dev_parts_objects
- 1, GFP_KERNEL
);
988 data_size
+= sizeof(obj_dev_parts
);
989 data
= kzalloc(data_size
, GFP_KERNEL
);
995 obj_create_data
= data
;
996 obj_create_data
->hdr
.type
= cpu_to_le16(obj_type
);
997 obj_create_data
->hdr
.id
= cpu_to_le32(*obj_id
);
998 obj_dev_parts
.type
= operation_type
;
999 memcpy(obj_create_data
->resource_obj_specific_data
, &obj_dev_parts
,
1000 sizeof(obj_dev_parts
));
1001 sg_init_one(&data_sg
, data
, data_size
);
1002 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE
);
1003 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
1004 cmd
.group_member_id
= cpu_to_le64(vf_id
+ 1);
1005 cmd
.data_sg
= &data_sg
;
1006 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
1011 ida_free(&avq
->dev_parts_ida
, id
);
1015 EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_create
);
1018 * virtio_pci_admin_obj_destroy - Destroys an object of a given type and id
1020 * @obj_type: Object type
1023 * Note: caller must serialize access for the given device.
1024 * Returns 0 on success, or negative on failure.
1026 int virtio_pci_admin_obj_destroy(struct pci_dev
*pdev
, u16 obj_type
, u32 id
)
1028 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
1029 struct virtio_admin_cmd_resource_obj_cmd_hdr
*data
;
1030 struct virtio_pci_device
*vp_dev
;
1031 struct virtio_admin_cmd cmd
= {};
1032 struct scatterlist data_sg
;
1039 vf_id
= pci_iov_vf_id(pdev
);
1043 if (obj_type
!= VIRTIO_RESOURCE_OBJ_DEV_PARTS
)
1046 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1050 data
->type
= cpu_to_le16(obj_type
);
1051 data
->id
= cpu_to_le32(id
);
1052 sg_init_one(&data_sg
, data
, sizeof(*data
));
1053 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY
);
1054 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
1055 cmd
.group_member_id
= cpu_to_le64(vf_id
+ 1);
1056 cmd
.data_sg
= &data_sg
;
1057 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
1059 vp_dev
= to_vp_device(virtio_dev
);
1060 ida_free(&vp_dev
->admin_vq
.dev_parts_ida
, id
);
1066 EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_destroy
);
1069 * virtio_pci_admin_dev_parts_metadata_get - Gets the metadata of the device parts
1070 * identified by the below attributes.
1072 * @obj_type: Object type
1074 * @metadata_type: Metadata type
1075 * @out: Upon success holds the output for 'metadata type size'
1077 * Note: caller must serialize access for the given device.
1078 * Returns 0 on success, or negative on failure.
1080 int virtio_pci_admin_dev_parts_metadata_get(struct pci_dev
*pdev
, u16 obj_type
,
1081 u32 id
, u8 metadata_type
, u32
*out
)
1083 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
1084 struct virtio_admin_cmd_dev_parts_metadata_result
*result
;
1085 struct virtio_admin_cmd_dev_parts_metadata_data
*data
;
1086 struct scatterlist data_sg
, result_sg
;
1087 struct virtio_admin_cmd cmd
= {};
1094 if (metadata_type
!= VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_SIZE
)
1097 vf_id
= pci_iov_vf_id(pdev
);
1101 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1105 result
= kzalloc(sizeof(*result
), GFP_KERNEL
);
1111 data
->hdr
.type
= cpu_to_le16(obj_type
);
1112 data
->hdr
.id
= cpu_to_le32(id
);
1113 data
->type
= metadata_type
;
1114 sg_init_one(&data_sg
, data
, sizeof(*data
));
1115 sg_init_one(&result_sg
, result
, sizeof(*result
));
1116 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_GET
);
1117 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
1118 cmd
.group_member_id
= cpu_to_le64(vf_id
+ 1);
1119 cmd
.data_sg
= &data_sg
;
1120 cmd
.result_sg
= &result_sg
;
1121 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
1123 *out
= le32_to_cpu(result
->parts_size
.size
);
1130 EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_metadata_get
);
1133 * virtio_pci_admin_dev_parts_get - Gets the device parts identified by the below attributes.
1135 * @obj_type: Object type
1137 * @get_type: Get type
1138 * @res_sg: Upon success holds the output result data
1139 * @res_size: Upon success holds the output result size
1141 * Note: caller must serialize access for the given device.
1142 * Returns 0 on success, or negative on failure.
1144 int virtio_pci_admin_dev_parts_get(struct pci_dev
*pdev
, u16 obj_type
, u32 id
,
1145 u8 get_type
, struct scatterlist
*res_sg
,
1148 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
1149 struct virtio_admin_cmd_dev_parts_get_data
*data
;
1150 struct scatterlist data_sg
;
1151 struct virtio_admin_cmd cmd
= {};
1158 if (get_type
!= VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_ALL
)
1161 vf_id
= pci_iov_vf_id(pdev
);
1165 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1169 data
->hdr
.type
= cpu_to_le16(obj_type
);
1170 data
->hdr
.id
= cpu_to_le32(id
);
1171 data
->type
= get_type
;
1172 sg_init_one(&data_sg
, data
, sizeof(*data
));
1173 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_PARTS_GET
);
1174 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
1175 cmd
.group_member_id
= cpu_to_le64(vf_id
+ 1);
1176 cmd
.data_sg
= &data_sg
;
1177 cmd
.result_sg
= res_sg
;
1178 ret
= vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
1180 *res_size
= cmd
.result_sg_size
;
1185 EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_get
);
1188 * virtio_pci_admin_dev_parts_set - Sets the device parts identified by the below attributes.
1190 * @data_sg: The device parts data, its layout follows struct virtio_admin_cmd_dev_parts_set_data
1192 * Note: caller must serialize access for the given device.
1193 * Returns 0 on success, or negative on failure.
1195 int virtio_pci_admin_dev_parts_set(struct pci_dev
*pdev
, struct scatterlist
*data_sg
)
1197 struct virtio_device
*virtio_dev
= virtio_pci_vf_get_pf_dev(pdev
);
1198 struct virtio_admin_cmd cmd
= {};
1204 vf_id
= pci_iov_vf_id(pdev
);
1208 cmd
.opcode
= cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_PARTS_SET
);
1209 cmd
.group_type
= cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV
);
1210 cmd
.group_member_id
= cpu_to_le64(vf_id
+ 1);
1211 cmd
.data_sg
= data_sg
;
1212 return vp_modern_admin_cmd_exec(virtio_dev
, &cmd
);
1214 EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_set
);
1216 static const struct virtio_config_ops virtio_pci_config_nodev_ops
= {
1219 .generation
= vp_generation
,
1220 .get_status
= vp_get_status
,
1221 .set_status
= vp_set_status
,
1223 .find_vqs
= vp_modern_find_vqs
,
1224 .del_vqs
= vp_del_vqs
,
1225 .synchronize_cbs
= vp_synchronize_vectors
,
1226 .get_features
= vp_get_features
,
1227 .finalize_features
= vp_finalize_features
,
1228 .bus_name
= vp_bus_name
,
1229 .set_vq_affinity
= vp_set_vq_affinity
,
1230 .get_vq_affinity
= vp_get_vq_affinity
,
1231 .get_shm_region
= vp_get_shm_region
,
1232 .disable_vq_and_reset
= vp_modern_disable_vq_and_reset
,
1233 .enable_vq_after_reset
= vp_modern_enable_vq_after_reset
,
1236 static const struct virtio_config_ops virtio_pci_config_ops
= {
1239 .generation
= vp_generation
,
1240 .get_status
= vp_get_status
,
1241 .set_status
= vp_set_status
,
1243 .find_vqs
= vp_modern_find_vqs
,
1244 .del_vqs
= vp_del_vqs
,
1245 .synchronize_cbs
= vp_synchronize_vectors
,
1246 .get_features
= vp_get_features
,
1247 .finalize_features
= vp_finalize_features
,
1248 .bus_name
= vp_bus_name
,
1249 .set_vq_affinity
= vp_set_vq_affinity
,
1250 .get_vq_affinity
= vp_get_vq_affinity
,
1251 .get_shm_region
= vp_get_shm_region
,
1252 .disable_vq_and_reset
= vp_modern_disable_vq_and_reset
,
1253 .enable_vq_after_reset
= vp_modern_enable_vq_after_reset
,
1256 /* the PCI probing function */
1257 int virtio_pci_modern_probe(struct virtio_pci_device
*vp_dev
)
1259 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
1260 struct pci_dev
*pci_dev
= vp_dev
->pci_dev
;
1263 mdev
->pci_dev
= pci_dev
;
1265 err
= vp_modern_probe(mdev
);
1270 vp_dev
->vdev
.config
= &virtio_pci_config_ops
;
1272 vp_dev
->vdev
.config
= &virtio_pci_config_nodev_ops
;
1274 vp_dev
->config_vector
= vp_config_vector
;
1275 vp_dev
->setup_vq
= setup_vq
;
1276 vp_dev
->del_vq
= del_vq
;
1277 vp_dev
->avq_index
= vp_avq_index
;
1278 vp_dev
->isr
= mdev
->isr
;
1279 vp_dev
->vdev
.id
= mdev
->id
;
1281 spin_lock_init(&vp_dev
->admin_vq
.lock
);
1285 void virtio_pci_modern_remove(struct virtio_pci_device
*vp_dev
)
1287 struct virtio_pci_modern_device
*mdev
= &vp_dev
->mdev
;
1289 vp_modern_remove(mdev
);