Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / virtio / virtio_pci_modern.c
blob5eaade7578606e4b02af0d66447417ad6aa11064
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
11 * Authors:
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);
36 *num = 0;
37 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
38 return 0;
40 *num = vp_modern_avq_num(&vp_dev->mdev);
41 if (!(*num))
42 return -EINVAL;
43 *index = vp_modern_avq_index(&vp_dev->mdev);
44 return 0;
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;
52 unsigned long flags;
53 unsigned int len;
55 spin_lock_irqsave(&admin_vq->lock, flags);
56 do {
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,
67 u16 opcode,
68 struct scatterlist **sgs,
69 unsigned int out_num,
70 unsigned int in_num,
71 struct virtio_admin_cmd *cmd)
73 struct virtqueue *vq;
74 unsigned long flags;
75 int ret;
77 vq = admin_vq->info->vq;
78 if (!vq)
79 return -EIO;
81 if (opcode != VIRTIO_ADMIN_CMD_LIST_QUERY &&
82 opcode != VIRTIO_ADMIN_CMD_LIST_USE &&
83 !((1ULL << opcode) & admin_vq->supported_cmds))
84 return -EOPNOTSUPP;
86 init_completion(&cmd->completion);
88 again:
89 if (virtqueue_is_broken(vq))
90 return -EIO;
92 spin_lock_irqsave(&admin_vq->lock, flags);
93 ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
94 if (ret < 0) {
95 if (ret == -ENOSPC) {
96 spin_unlock_irqrestore(&admin_vq->lock, flags);
97 cpu_relax();
98 goto again;
100 goto unlock_err;
102 if (!virtqueue_kick(vq))
103 goto unlock_err;
104 spin_unlock_irqrestore(&admin_vq->lock, flags);
106 wait_for_completion(&cmd->completion);
108 return cmd->ret;
110 unlock_err:
111 spin_unlock_irqrestore(&admin_vq->lock, flags);
112 return -EIO;
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;
123 u16 status;
124 int ret;
126 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
127 return -EOPNOTSUPP;
129 va_status = kzalloc(sizeof(*va_status), GFP_KERNEL);
130 if (!va_status)
131 return -ENOMEM;
133 va_hdr = kzalloc(sizeof(*va_hdr), GFP_KERNEL);
134 if (!va_hdr) {
135 ret = -ENOMEM;
136 goto err_alloc;
139 va_hdr->opcode = cmd->opcode;
140 va_hdr->group_type = cmd->group_type;
141 va_hdr->group_member_id = cmd->group_member_id;
143 /* Add header */
144 sg_init_one(&hdr, va_hdr, sizeof(*va_hdr));
145 sgs[out_num] = &hdr;
146 out_num++;
148 if (cmd->data_sg) {
149 sgs[out_num] = cmd->data_sg;
150 out_num++;
153 /* Add return status */
154 sg_init_one(&stat, va_status, sizeof(*va_status));
155 sgs[out_num + in_num] = &stat;
156 in_num++;
158 if (cmd->result_sg) {
159 sgs[out_num + in_num] = cmd->result_sg;
160 in_num++;
163 ret = virtqueue_exec_admin_cmd(&vp_dev->admin_vq,
164 le16_to_cpu(cmd->opcode),
165 sgs, out_num, in_num, cmd);
166 if (ret) {
167 dev_err(&vdev->dev,
168 "Failed to execute command on admin vq: %d\n.", ret);
169 goto err_cmd_exec;
172 status = le16_to_cpu(va_status->status);
173 if (status != VIRTIO_ADMIN_STATUS_OK) {
174 dev_err(&vdev->dev,
175 "admin command error: status(%#x) qualifier(%#x)\n",
176 status, le16_to_cpu(va_status->status_qualifier));
177 ret = -status;
180 err_cmd_exec:
181 kfree(va_hdr);
182 err_alloc:
183 kfree(va_status);
184 return ret;
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;
193 __le64 *data;
194 int ret;
196 data = kzalloc(sizeof(*data), GFP_KERNEL);
197 if (!data)
198 return;
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);
206 if (ret)
207 goto end;
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);
216 if (ret)
217 goto end;
219 vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
220 end:
221 kfree(data);
224 static void
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;
235 u16 set_data_size;
236 int ret;
238 get_data = kzalloc(sizeof(*get_data), GFP_KERNEL);
239 if (!get_data)
240 return;
242 result = kzalloc(sizeof(*result), GFP_KERNEL);
243 if (!result)
244 goto end;
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);
254 if (ret)
255 goto err_get;
257 set_data_size = sizeof(*set_data) + sizeof(*result);
258 set_data = kzalloc(set_data_size, GFP_KERNEL);
259 if (!set_data)
260 goto err_get;
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);
279 if (ret)
280 goto err_set;
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;
286 err_set:
287 kfree(set_data);
288 err_get:
289 kfree(result);
290 end:
291 kfree(get_data);
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;
300 int ret;
302 data = kzalloc(sizeof(*data), GFP_KERNEL);
303 if (!data)
304 return;
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);
312 if (ret)
313 goto end;
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)))
321 goto end;
323 virtio_pci_admin_cmd_dev_parts_objects_enable(virtio_dev);
324 end:
325 kfree(data);
328 static void vp_modern_avq_activate(struct virtio_device *vdev)
330 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
331 return;
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))
344 return;
346 vq = vp_dev->admin_vq.info->vq;
347 if (!vq)
348 return;
350 while ((cmd = virtqueue_detach_unused_buf(vq))) {
351 cmd->ret = -EIO;
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))
378 return 0;
380 if (likely(vp_dev->mdev.common_len >= offset))
381 return 0;
383 dev_err(&vdev->dev,
384 "virtio: common cfg size(%zu) does not match the feature %s\n",
385 vp_dev->mdev.common_len, fname);
387 return -EINVAL;
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))
397 return -EINVAL;
399 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_RING_RESET, queue_reset))
400 return -EINVAL;
402 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_ADMIN_VQ, admin_queue_num))
403 return -EINVAL;
405 return 0;
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");
423 return -EINVAL;
426 if (vp_check_common_size(vdev))
427 return -EINVAL;
429 vp_modern_set_features(&vp_dev->mdev, vdev->features);
431 return 0;
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;
441 u8 b;
442 __le16 w;
443 __le32 l;
445 BUG_ON(offset + len > mdev->device_len);
447 switch (len) {
448 case 1:
449 b = ioread8(device + offset);
450 memcpy(buf, &b, sizeof b);
451 break;
452 case 2:
453 w = cpu_to_le16(ioread16(device + offset));
454 memcpy(buf, &w, sizeof w);
455 break;
456 case 4:
457 l = cpu_to_le32(ioread32(device + offset));
458 memcpy(buf, &l, sizeof l);
459 break;
460 case 8:
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);
465 break;
466 default:
467 BUG();
471 /* the config->set() implementation. it's symmetric to the config->get()
472 * implementation */
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;
479 u8 b;
480 __le16 w;
481 __le32 l;
483 BUG_ON(offset + len > mdev->device_len);
485 switch (len) {
486 case 1:
487 memcpy(&b, buf, sizeof b);
488 iowrite8(b, device + offset);
489 break;
490 case 2:
491 memcpy(&w, buf, sizeof w);
492 iowrite16(le16_to_cpu(w), device + offset);
493 break;
494 case 4:
495 memcpy(&l, buf, sizeof l);
496 iowrite32(le32_to_cpu(l), device + offset);
497 break;
498 case 8:
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);
503 break;
504 default:
505 BUG();
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. */
529 BUG_ON(status == 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))
548 msleep(1);
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;
560 unsigned long index;
562 index = vq->index;
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)
573 return -EBUSY;
576 return 0;
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;
584 unsigned long flags;
586 if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
587 return -ENOENT;
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);
602 #endif
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));
613 vq->reset = true;
615 return 0;
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;
624 int err;
626 if (!vq->reset)
627 return -EBUSY;
629 index = vq->index;
630 info = vp_dev->vqs[index];
632 if (vp_modern_get_queue_reset(mdev, index))
633 return -EBUSY;
635 if (vp_modern_get_queue_enable(mdev, index))
636 return -EBUSY;
638 err = vp_active_vq(vq, info->msix_vector);
639 if (err)
640 return err;
642 if (vq->callback) {
643 spin_lock_irqsave(&vp_dev->lock, flags);
644 list_add(&info->node, &vp_dev->virtqueues);
645 spin_unlock_irqrestore(&vp_dev->lock, flags);
646 } else {
647 INIT_LIST_HEAD(&info->node);
650 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
651 __virtqueue_unbreak(vq);
652 #endif
654 vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
655 vq->reset = false;
657 return 0;
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);
671 return true;
674 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
675 struct virtio_pci_vq_info *info,
676 unsigned int index,
677 void (*callback)(struct virtqueue *vq),
678 const char *name,
679 bool ctx,
680 u16 msix_vec)
683 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
684 bool (*notify)(struct virtqueue *vq);
685 struct virtqueue *vq;
686 bool is_avq;
687 u16 num;
688 int err;
690 if (__virtio_test_bit(&vp_dev->vdev, VIRTIO_F_NOTIFICATION_DATA))
691 notify = vp_notify_with_data;
692 else
693 notify = vp_notify;
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,
709 true, true, ctx,
710 notify, callback, name);
711 if (!vq)
712 return ERR_PTR(-ENOMEM);
714 vq->num_max = num;
716 err = vp_active_vq(vq, msix_vec);
717 if (err)
718 goto err;
720 vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
721 if (!vq->priv) {
722 err = -ENOMEM;
723 goto err;
726 return vq;
728 err:
729 vring_del_virtqueue(vq);
730 return ERR_PTR(err);
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);
742 if (rc)
743 return rc;
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);
751 return 0;
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)
773 int pos;
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;
778 u32 tmp32;
779 u64 res_offset, res_length;
781 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
782 cfg_type), &type);
783 if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
784 continue;
786 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
787 cap_len), &cap_len);
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);
791 continue;
794 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
795 id), &id);
796 if (id != required_id)
797 continue;
799 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
800 bar), &res_bar);
801 if (res_bar >= PCI_STD_NUM_BARS)
802 continue;
804 /* Type and ID match, and the BAR value isn't reserved.
805 * Looks good.
808 /* Read the lower 32bit of length and offset */
809 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
810 offset), &tmp32);
811 res_offset = tmp32;
812 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
813 length), &tmp32);
814 res_length = tmp32;
816 /* and now the top half */
817 pci_read_config_dword(dev,
818 pos + offsetof(struct virtio_pci_cap64,
819 offset_hi), &tmp32);
820 res_offset |= ((u64)tmp32) << 32;
821 pci_read_config_dword(dev,
822 pos + offsetof(struct virtio_pci_cap64,
823 length_hi), &tmp32);
824 res_length |= ((u64)tmp32) << 32;
826 *bar = res_bar;
827 *offset = res_offset;
828 *len = res_length;
830 return pos;
832 return 0;
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;
840 u8 bar;
841 u64 offset, len;
842 phys_addr_t phys_addr;
843 size_t bar_len;
845 if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
846 return false;
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",
853 __func__);
854 return false;
857 if (offset + len > bar_len) {
858 dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
859 __func__);
860 return false;
863 region->len = len;
864 region->addr = (u64) phys_addr + offset;
866 return true;
870 * virtio_pci_admin_has_dev_parts - Checks whether the device parts
871 * functionality is supported
872 * @pdev: VF pci_dev
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;
881 if (!virtio_dev)
882 return false;
884 if (!virtio_has_feature(virtio_dev, VIRTIO_F_ADMIN_VQ))
885 return false;
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))
891 return false;
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
899 * @pdev: VF pci_dev
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;
911 int vf_id;
912 int ret;
914 if (!virtio_dev)
915 return -ENODEV;
917 vf_id = pci_iov_vf_id(pdev);
918 if (vf_id < 0)
919 return vf_id;
921 data = kzalloc(sizeof(*data), GFP_KERNEL);
922 if (!data)
923 return -ENOMEM;
925 data->flags = flags;
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);
933 kfree(data);
934 return ret;
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.
941 * @pdev: VF pci_dev
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,
950 u32 *obj_id)
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;
959 void *data;
960 int id = -1;
961 int vf_id;
962 int ret;
964 if (!virtio_dev)
965 return -ENODEV;
967 vf_id = pci_iov_vf_id(pdev);
968 if (vf_id < 0)
969 return vf_id;
971 if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
972 return -EOPNOTSUPP;
974 if (operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET &&
975 operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET)
976 return -EINVAL;
978 avq = &to_vp_device(virtio_dev)->admin_vq;
979 if (!avq->max_dev_parts_objects)
980 return -EOPNOTSUPP;
982 id = ida_alloc_range(&avq->dev_parts_ida, 0,
983 avq->max_dev_parts_objects - 1, GFP_KERNEL);
984 if (id < 0)
985 return id;
987 *obj_id = id;
988 data_size += sizeof(obj_dev_parts);
989 data = kzalloc(data_size, GFP_KERNEL);
990 if (!data) {
991 ret = -ENOMEM;
992 goto end;
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);
1008 kfree(data);
1009 end:
1010 if (ret)
1011 ida_free(&avq->dev_parts_ida, id);
1013 return ret;
1015 EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_create);
1018 * virtio_pci_admin_obj_destroy - Destroys an object of a given type and id
1019 * @pdev: VF pci_dev
1020 * @obj_type: Object type
1021 * @id: Object id
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;
1033 int vf_id;
1034 int ret;
1036 if (!virtio_dev)
1037 return -ENODEV;
1039 vf_id = pci_iov_vf_id(pdev);
1040 if (vf_id < 0)
1041 return vf_id;
1043 if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
1044 return -EINVAL;
1046 data = kzalloc(sizeof(*data), GFP_KERNEL);
1047 if (!data)
1048 return -ENOMEM;
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);
1058 if (!ret) {
1059 vp_dev = to_vp_device(virtio_dev);
1060 ida_free(&vp_dev->admin_vq.dev_parts_ida, id);
1063 kfree(data);
1064 return ret;
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.
1071 * @pdev: VF pci_dev
1072 * @obj_type: Object type
1073 * @id: Object id
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 = {};
1088 int vf_id;
1089 int ret;
1091 if (!virtio_dev)
1092 return -ENODEV;
1094 if (metadata_type != VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_SIZE)
1095 return -EOPNOTSUPP;
1097 vf_id = pci_iov_vf_id(pdev);
1098 if (vf_id < 0)
1099 return vf_id;
1101 data = kzalloc(sizeof(*data), GFP_KERNEL);
1102 if (!data)
1103 return -ENOMEM;
1105 result = kzalloc(sizeof(*result), GFP_KERNEL);
1106 if (!result) {
1107 ret = -ENOMEM;
1108 goto end;
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);
1122 if (!ret)
1123 *out = le32_to_cpu(result->parts_size.size);
1125 kfree(result);
1126 end:
1127 kfree(data);
1128 return ret;
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.
1134 * @pdev: VF pci_dev
1135 * @obj_type: Object type
1136 * @id: Object id
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,
1146 u32 *res_size)
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 = {};
1152 int vf_id;
1153 int ret;
1155 if (!virtio_dev)
1156 return -ENODEV;
1158 if (get_type != VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_ALL)
1159 return -EOPNOTSUPP;
1161 vf_id = pci_iov_vf_id(pdev);
1162 if (vf_id < 0)
1163 return vf_id;
1165 data = kzalloc(sizeof(*data), GFP_KERNEL);
1166 if (!data)
1167 return -ENOMEM;
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);
1179 if (!ret)
1180 *res_size = cmd.result_sg_size;
1182 kfree(data);
1183 return ret;
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.
1189 * @pdev: VF pci_dev
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 = {};
1199 int vf_id;
1201 if (!virtio_dev)
1202 return -ENODEV;
1204 vf_id = pci_iov_vf_id(pdev);
1205 if (vf_id < 0)
1206 return vf_id;
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 = {
1217 .get = NULL,
1218 .set = NULL,
1219 .generation = vp_generation,
1220 .get_status = vp_get_status,
1221 .set_status = vp_set_status,
1222 .reset = vp_reset,
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 = {
1237 .get = vp_get,
1238 .set = vp_set,
1239 .generation = vp_generation,
1240 .get_status = vp_get_status,
1241 .set_status = vp_set_status,
1242 .reset = vp_reset,
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;
1261 int err;
1263 mdev->pci_dev = pci_dev;
1265 err = vp_modern_probe(mdev);
1266 if (err)
1267 return err;
1269 if (mdev->device)
1270 vp_dev->vdev.config = &virtio_pci_config_ops;
1271 else
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);
1282 return 0;
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);