Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Virtio PCI driver - modern (virtio 1.0) device support
4 *
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.
7 *
8 * Copyright IBM Corp. 2007
9 * Copyright Red Hat, Inc. 2014
10 *
11 * Authors:
12 * Anthony Liguori <aliguori@us.ibm.com>
13 * Rusty Russell <rusty@rustcorp.com.au>
14 * Michael S. Tsirkin <mst@redhat.com>
15 */
16
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"
22
23#define VIRTIO_AVQ_SGS_MAX 4
24
25static u64 vp_get_features(struct virtio_device *vdev)
26{
27 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
28
29 return vp_modern_get_features(&vp_dev->mdev);
30}
31
32static int vp_avq_index(struct virtio_device *vdev, u16 *index, u16 *num)
33{
34 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
35
36 *num = 0;
37 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
38 return 0;
39
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;
45}
46
47void vp_modern_avq_done(struct virtqueue *vq)
48{
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;
54
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);
61 }
62 } while (!virtqueue_enable_cb(vq));
63 spin_unlock_irqrestore(&admin_vq->lock, flags);
64}
65
66static 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)
72{
73 struct virtqueue *vq;
74 unsigned long flags;
75 int ret;
76
77 vq = admin_vq->info->vq;
78 if (!vq)
79 return -EIO;
80
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;
85
86 init_completion(&cmd->completion);
87
88again:
89 if (virtqueue_is_broken(vq))
90 return -EIO;
91
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;
99 }
100 goto unlock_err;
101 }
102 if (!virtqueue_kick(vq))
103 goto unlock_err;
104 spin_unlock_irqrestore(&admin_vq->lock, flags);
105
106 wait_for_completion(&cmd->completion);
107
108 return cmd->ret;
109
110unlock_err:
111 spin_unlock_irqrestore(&admin_vq->lock, flags);
112 return -EIO;
113}
114
115int vp_modern_admin_cmd_exec(struct virtio_device *vdev,
116 struct virtio_admin_cmd *cmd)
117{
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;
125
126 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
127 return -EOPNOTSUPP;
128
129 va_status = kzalloc(sizeof(*va_status), GFP_KERNEL);
130 if (!va_status)
131 return -ENOMEM;
132
133 va_hdr = kzalloc(sizeof(*va_hdr), GFP_KERNEL);
134 if (!va_hdr) {
135 ret = -ENOMEM;
136 goto err_alloc;
137 }
138
139 va_hdr->opcode = cmd->opcode;
140 va_hdr->group_type = cmd->group_type;
141 va_hdr->group_member_id = cmd->group_member_id;
142
143 /* Add header */
144 sg_init_one(&hdr, va_hdr, sizeof(*va_hdr));
145 sgs[out_num] = &hdr;
146 out_num++;
147
148 if (cmd->data_sg) {
149 sgs[out_num] = cmd->data_sg;
150 out_num++;
151 }
152
153 /* Add return status */
154 sg_init_one(&stat, va_status, sizeof(*va_status));
155 sgs[out_num + in_num] = &stat;
156 in_num++;
157
158 if (cmd->result_sg) {
159 sgs[out_num + in_num] = cmd->result_sg;
160 in_num++;
161 }
162
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;
170 }
171
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;
178 }
179
180err_cmd_exec:
181 kfree(va_hdr);
182err_alloc:
183 kfree(va_status);
184 return ret;
185}
186
187static void virtio_pci_admin_cmd_list_init(struct virtio_device *virtio_dev)
188{
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;
195
196 data = kzalloc(sizeof(*data), GFP_KERNEL);
197 if (!data)
198 return;
199
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;
204
205 ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
206 if (ret)
207 goto end;
208
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;
214
215 ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
216 if (ret)
217 goto end;
218
219 vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
220end:
221 kfree(data);
222}
223
224static void
225virtio_pci_admin_cmd_dev_parts_objects_enable(struct virtio_device *virtio_dev)
226{
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;
237
238 get_data = kzalloc(sizeof(*get_data), GFP_KERNEL);
239 if (!get_data)
240 return;
241
242 result = kzalloc(sizeof(*result), GFP_KERNEL);
243 if (!result)
244 goto end;
245
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;
256
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;
261
262 set_data->id = cpu_to_le16(VIRTIO_DEV_PARTS_CAP);
263
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.
268 */
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;
281
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;
285
286err_set:
287 kfree(set_data);
288err_get:
289 kfree(result);
290end:
291 kfree(get_data);
292}
293
294static void virtio_pci_admin_cmd_cap_init(struct virtio_device *virtio_dev)
295{
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;
301
302 data = kzalloc(sizeof(*data), GFP_KERNEL);
303 if (!data)
304 return;
305
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;
310
311 ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
312 if (ret)
313 goto end;
314
315 /* Max number of caps fits into a single u64 */
316 BUILD_BUG_ON(sizeof(data->supported_caps) > sizeof(u64));
317
318 vp_dev->admin_vq.supported_caps = le64_to_cpu(data->supported_caps[0]);
319
320 if (!(vp_dev->admin_vq.supported_caps & (1 << VIRTIO_DEV_PARTS_CAP)))
321 goto end;
322
323 virtio_pci_admin_cmd_dev_parts_objects_enable(virtio_dev);
324end:
325 kfree(data);
326}
327
328static void vp_modern_avq_activate(struct virtio_device *vdev)
329{
330 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
331 return;
332
333 virtio_pci_admin_cmd_list_init(vdev);
334 virtio_pci_admin_cmd_cap_init(vdev);
335}
336
337static void vp_modern_avq_cleanup(struct virtio_device *vdev)
338{
339 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
340 struct virtio_admin_cmd *cmd;
341 struct virtqueue *vq;
342
343 if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
344 return;
345
346 vq = vp_dev->admin_vq.info->vq;
347 if (!vq)
348 return;
349
350 while ((cmd = virtqueue_detach_unused_buf(vq))) {
351 cmd->ret = -EIO;
352 complete(&cmd->completion);
353 }
354}
355
356static void vp_transport_features(struct virtio_device *vdev, u64 features)
357{
358 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
359 struct pci_dev *pci_dev = vp_dev->pci_dev;
360
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);
364
365 if (features & BIT_ULL(VIRTIO_F_RING_RESET))
366 __virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
367
368 if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
369 __virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
370}
371
372static int __vp_check_common_size_one_feature(struct virtio_device *vdev, u32 fbit,
373 u32 offset, const char *fname)
374{
375 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
376
377 if (!__virtio_test_bit(vdev, fbit))
378 return 0;
379
380 if (likely(vp_dev->mdev.common_len >= offset))
381 return 0;
382
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);
386
387 return -EINVAL;
388}
389
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)
393
394static int vp_check_common_size(struct virtio_device *vdev)
395{
396 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_NOTIF_CONFIG_DATA, queue_notify_data))
397 return -EINVAL;
398
399 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_RING_RESET, queue_reset))
400 return -EINVAL;
401
402 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_ADMIN_VQ, admin_queue_num))
403 return -EINVAL;
404
405 return 0;
406}
407
408/* virtio config->finalize_features() implementation */
409static int vp_finalize_features(struct virtio_device *vdev)
410{
411 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
412 u64 features = vdev->features;
413
414 /* Give virtio_ring a chance to accept features. */
415 vring_transport_features(vdev);
416
417 /* Give virtio_pci a chance to accept features. */
418 vp_transport_features(vdev, features);
419
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;
424 }
425
426 if (vp_check_common_size(vdev))
427 return -EINVAL;
428
429 vp_modern_set_features(&vp_dev->mdev, vdev->features);
430
431 return 0;
432}
433
434/* virtio config->get() implementation */
435static void vp_get(struct virtio_device *vdev, unsigned int offset,
436 void *buf, unsigned int len)
437{
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;
444
445 BUG_ON(offset + len > mdev->device_len);
446
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();
468 }
469}
470
471/* the config->set() implementation. it's symmetric to the config->get()
472 * implementation */
473static void vp_set(struct virtio_device *vdev, unsigned int offset,
474 const void *buf, unsigned int len)
475{
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;
482
483 BUG_ON(offset + len > mdev->device_len);
484
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();
506 }
507}
508
509static u32 vp_generation(struct virtio_device *vdev)
510{
511 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
512
513 return vp_modern_generation(&vp_dev->mdev);
514}
515
516/* config->{get,set}_status() implementations */
517static u8 vp_get_status(struct virtio_device *vdev)
518{
519 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
520
521 return vp_modern_get_status(&vp_dev->mdev);
522}
523
524static void vp_set_status(struct virtio_device *vdev, u8 status)
525{
526 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
527
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);
533}
534
535static void vp_reset(struct virtio_device *vdev)
536{
537 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
538 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
539
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.
546 */
547 while (vp_modern_get_status(mdev))
548 msleep(1);
549
550 vp_modern_avq_cleanup(vdev);
551
552 /* Flush pending VQ/configuration callbacks. */
553 vp_synchronize_vectors(vdev);
554}
555
556static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
557{
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;
561
562 index = vq->index;
563
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));
569
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;
574 }
575
576 return 0;
577}
578
579static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
580{
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;
585
586 if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
587 return -ENOENT;
588
589 vp_modern_set_queue_reset(mdev, vq->index);
590
591 info = vp_dev->vqs[vq->index];
592
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);
597
598 INIT_LIST_HEAD(&info->node);
599
600#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
601 __virtqueue_break(vq);
602#endif
603
604 /* For the case where vq has an exclusive irq, call synchronize_irq() to
605 * wait for completion.
606 *
607 * note: We can't use disable_irq() since it conflicts with the affinity
608 * managed IRQ that is used by some drivers.
609 */
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));
612
613 vq->reset = true;
614
615 return 0;
616}
617
618static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
619{
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;
625
626 if (!vq->reset)
627 return -EBUSY;
628
629 index = vq->index;
630 info = vp_dev->vqs[index];
631
632 if (vp_modern_get_queue_reset(mdev, index))
633 return -EBUSY;
634
635 if (vp_modern_get_queue_enable(mdev, index))
636 return -EBUSY;
637
638 err = vp_active_vq(vq, info->msix_vector);
639 if (err)
640 return err;
641
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);
648 }
649
650#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
651 __virtqueue_unbreak(vq);
652#endif
653
654 vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
655 vq->reset = false;
656
657 return 0;
658}
659
660static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
661{
662 return vp_modern_config_vector(&vp_dev->mdev, vector);
663}
664
665static bool vp_notify_with_data(struct virtqueue *vq)
666{
667 u32 data = vring_notification_data(vq);
668
669 iowrite32(data, (void __iomem *)vq->priv);
670
671 return true;
672}
673
674static 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)
681{
682
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;
689
690 if (__virtio_test_bit(&vp_dev->vdev, VIRTIO_F_NOTIFICATION_DATA))
691 notify = vp_notify_with_data;
692 else
693 notify = vp_notify;
694
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);
698
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);
703
704 info->msix_vector = msix_vec;
705
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);
713
714 vq->num_max = num;
715
716 err = vp_active_vq(vq, msix_vec);
717 if (err)
718 goto err;
719
720 vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
721 if (!vq->priv) {
722 err = -ENOMEM;
723 goto err;
724 }
725
726 return vq;
727
728err:
729 vring_del_virtqueue(vq);
730 return ERR_PTR(err);
731}
732
733static 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)
737{
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);
741
742 if (rc)
743 return rc;
744
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.
747 */
748 list_for_each_entry(vq, &vdev->vqs, list)
749 vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
750
751 return 0;
752}
753
754static void del_vq(struct virtio_pci_vq_info *info)
755{
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;
759
760 if (vp_dev->msix_enabled)
761 vp_modern_queue_vector(mdev, vq->index,
762 VIRTIO_MSI_NO_VECTOR);
763
764 if (!mdev->notify_base)
765 pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
766
767 vring_del_virtqueue(vq);
768}
769
770static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
771 u8 *bar, u64 *offset, u64 *len)
772{
773 int pos;
774
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;
780
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;
785
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;
792 }
793
794 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
795 id), &id);
796 if (id != required_id)
797 continue;
798
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;
803
804 /* Type and ID match, and the BAR value isn't reserved.
805 * Looks good.
806 */
807
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;
815
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;
825
826 *bar = res_bar;
827 *offset = res_offset;
828 *len = res_length;
829
830 return pos;
831 }
832 return 0;
833}
834
835static bool vp_get_shm_region(struct virtio_device *vdev,
836 struct virtio_shm_region *region, u8 id)
837{
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;
844
845 if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
846 return false;
847
848 phys_addr = pci_resource_start(pci_dev, bar);
849 bar_len = pci_resource_len(pci_dev, bar);
850
851 if ((offset + len) < offset) {
852 dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
853 __func__);
854 return false;
855 }
856
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;
861 }
862
863 region->len = len;
864 region->addr = (u64) phys_addr + offset;
865
866 return true;
867}
868
869/*
870 * virtio_pci_admin_has_dev_parts - Checks whether the device parts
871 * functionality is supported
872 * @pdev: VF pci_dev
873 *
874 * Returns true on success.
875 */
876bool virtio_pci_admin_has_dev_parts(struct pci_dev *pdev)
877{
878 struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
879 struct virtio_pci_device *vp_dev;
880
881 if (!virtio_dev)
882 return false;
883
884 if (!virtio_has_feature(virtio_dev, VIRTIO_F_ADMIN_VQ))
885 return false;
886
887 vp_dev = to_vp_device(virtio_dev);
888
889 if (!((vp_dev->admin_vq.supported_cmds & VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP) ==
890 VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP))
891 return false;
892
893 return vp_dev->admin_vq.max_dev_parts_objects;
894}
895EXPORT_SYMBOL_GPL(virtio_pci_admin_has_dev_parts);
896
897/*
898 * virtio_pci_admin_mode_set - Sets the mode of a member device
899 * @pdev: VF pci_dev
900 * @flags: device mode's flags
901 *
902 * Note: caller must serialize access for the given device.
903 * Returns 0 on success, or negative on failure.
904 */
905int virtio_pci_admin_mode_set(struct pci_dev *pdev, u8 flags)
906{
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;
913
914 if (!virtio_dev)
915 return -ENODEV;
916
917 vf_id = pci_iov_vf_id(pdev);
918 if (vf_id < 0)
919 return vf_id;
920
921 data = kzalloc(sizeof(*data), GFP_KERNEL);
922 if (!data)
923 return -ENOMEM;
924
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);
932
933 kfree(data);
934 return ret;
935}
936EXPORT_SYMBOL_GPL(virtio_pci_admin_mode_set);
937
938/*
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
945 *
946 * Note: caller must serialize access for the given device.
947 * Returns 0 on success, or negative on failure.
948 */
949int virtio_pci_admin_obj_create(struct pci_dev *pdev, u16 obj_type, u8 operation_type,
950 u32 *obj_id)
951{
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;
963
964 if (!virtio_dev)
965 return -ENODEV;
966
967 vf_id = pci_iov_vf_id(pdev);
968 if (vf_id < 0)
969 return vf_id;
970
971 if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
972 return -EOPNOTSUPP;
973
974 if (operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET &&
975 operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET)
976 return -EINVAL;
977
978 avq = &to_vp_device(virtio_dev)->admin_vq;
979 if (!avq->max_dev_parts_objects)
980 return -EOPNOTSUPP;
981
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;
986
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;
993 }
994
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);
1007
1008 kfree(data);
1009end:
1010 if (ret)
1011 ida_free(&avq->dev_parts_ida, id);
1012
1013 return ret;
1014}
1015EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_create);
1016
1017/*
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
1022 *
1023 * Note: caller must serialize access for the given device.
1024 * Returns 0 on success, or negative on failure.
1025 */
1026int virtio_pci_admin_obj_destroy(struct pci_dev *pdev, u16 obj_type, u32 id)
1027{
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;
1035
1036 if (!virtio_dev)
1037 return -ENODEV;
1038
1039 vf_id = pci_iov_vf_id(pdev);
1040 if (vf_id < 0)
1041 return vf_id;
1042
1043 if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
1044 return -EINVAL;
1045
1046 data = kzalloc(sizeof(*data), GFP_KERNEL);
1047 if (!data)
1048 return -ENOMEM;
1049
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);
1061 }
1062
1063 kfree(data);
1064 return ret;
1065}
1066EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_destroy);
1067
1068/*
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'
1076 *
1077 * Note: caller must serialize access for the given device.
1078 * Returns 0 on success, or negative on failure.
1079 */
1080int virtio_pci_admin_dev_parts_metadata_get(struct pci_dev *pdev, u16 obj_type,
1081 u32 id, u8 metadata_type, u32 *out)
1082{
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;
1090
1091 if (!virtio_dev)
1092 return -ENODEV;
1093
1094 if (metadata_type != VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_SIZE)
1095 return -EOPNOTSUPP;
1096
1097 vf_id = pci_iov_vf_id(pdev);
1098 if (vf_id < 0)
1099 return vf_id;
1100
1101 data = kzalloc(sizeof(*data), GFP_KERNEL);
1102 if (!data)
1103 return -ENOMEM;
1104
1105 result = kzalloc(sizeof(*result), GFP_KERNEL);
1106 if (!result) {
1107 ret = -ENOMEM;
1108 goto end;
1109 }
1110
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);
1124
1125 kfree(result);
1126end:
1127 kfree(data);
1128 return ret;
1129}
1130EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_metadata_get);
1131
1132/*
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
1140 *
1141 * Note: caller must serialize access for the given device.
1142 * Returns 0 on success, or negative on failure.
1143 */
1144int 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)
1147{
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;
1154
1155 if (!virtio_dev)
1156 return -ENODEV;
1157
1158 if (get_type != VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_ALL)
1159 return -EOPNOTSUPP;
1160
1161 vf_id = pci_iov_vf_id(pdev);
1162 if (vf_id < 0)
1163 return vf_id;
1164
1165 data = kzalloc(sizeof(*data), GFP_KERNEL);
1166 if (!data)
1167 return -ENOMEM;
1168
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;
1181
1182 kfree(data);
1183 return ret;
1184}
1185EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_get);
1186
1187/*
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
1191 *
1192 * Note: caller must serialize access for the given device.
1193 * Returns 0 on success, or negative on failure.
1194 */
1195int virtio_pci_admin_dev_parts_set(struct pci_dev *pdev, struct scatterlist *data_sg)
1196{
1197 struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
1198 struct virtio_admin_cmd cmd = {};
1199 int vf_id;
1200
1201 if (!virtio_dev)
1202 return -ENODEV;
1203
1204 vf_id = pci_iov_vf_id(pdev);
1205 if (vf_id < 0)
1206 return vf_id;
1207
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);
1213}
1214EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_set);
1215
1216static 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,
1234};
1235
1236static 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,
1254};
1255
1256/* the PCI probing function */
1257int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
1258{
1259 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
1260 struct pci_dev *pci_dev = vp_dev->pci_dev;
1261 int err;
1262
1263 mdev->pci_dev = pci_dev;
1264
1265 err = vp_modern_probe(mdev);
1266 if (err)
1267 return err;
1268
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;
1273
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;
1280
1281 spin_lock_init(&vp_dev->admin_vq.lock);
1282 return 0;
1283}
1284
1285void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
1286{
1287 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
1288
1289 vp_modern_remove(mdev);
1290}
1/*
2 * Virtio PCI driver - modern (virtio 1.0) device support
3 *
4 * This module allows virtio devices to be used over a virtual PCI device.
5 * This can be used with QEMU based VMMs like KVM or Xen.
6 *
7 * Copyright IBM Corp. 2007
8 * Copyright Red Hat, Inc. 2014
9 *
10 * Authors:
11 * Anthony Liguori <aliguori@us.ibm.com>
12 * Rusty Russell <rusty@rustcorp.com.au>
13 * Michael S. Tsirkin <mst@redhat.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2 or later.
16 * See the COPYING file in the top-level directory.
17 *
18 */
19
20#include <linux/delay.h>
21#define VIRTIO_PCI_NO_LEGACY
22#include "virtio_pci_common.h"
23
24/*
25 * Type-safe wrappers for io accesses.
26 * Use these to enforce at compile time the following spec requirement:
27 *
28 * The driver MUST access each field using the “natural” access
29 * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
30 * for 16-bit fields and 8-bit accesses for 8-bit fields.
31 */
32static inline u8 vp_ioread8(u8 __iomem *addr)
33{
34 return ioread8(addr);
35}
36static inline u16 vp_ioread16 (__le16 __iomem *addr)
37{
38 return ioread16(addr);
39}
40
41static inline u32 vp_ioread32(__le32 __iomem *addr)
42{
43 return ioread32(addr);
44}
45
46static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
47{
48 iowrite8(value, addr);
49}
50
51static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
52{
53 iowrite16(value, addr);
54}
55
56static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
57{
58 iowrite32(value, addr);
59}
60
61static void vp_iowrite64_twopart(u64 val,
62 __le32 __iomem *lo, __le32 __iomem *hi)
63{
64 vp_iowrite32((u32)val, lo);
65 vp_iowrite32(val >> 32, hi);
66}
67
68static void __iomem *map_capability(struct pci_dev *dev, int off,
69 size_t minlen,
70 u32 align,
71 u32 start, u32 size,
72 size_t *len)
73{
74 u8 bar;
75 u32 offset, length;
76 void __iomem *p;
77
78 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
79 bar),
80 &bar);
81 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
82 &offset);
83 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
84 &length);
85
86 if (length <= start) {
87 dev_err(&dev->dev,
88 "virtio_pci: bad capability len %u (>%u expected)\n",
89 length, start);
90 return NULL;
91 }
92
93 if (length - start < minlen) {
94 dev_err(&dev->dev,
95 "virtio_pci: bad capability len %u (>=%zu expected)\n",
96 length, minlen);
97 return NULL;
98 }
99
100 length -= start;
101
102 if (start + offset < offset) {
103 dev_err(&dev->dev,
104 "virtio_pci: map wrap-around %u+%u\n",
105 start, offset);
106 return NULL;
107 }
108
109 offset += start;
110
111 if (offset & (align - 1)) {
112 dev_err(&dev->dev,
113 "virtio_pci: offset %u not aligned to %u\n",
114 offset, align);
115 return NULL;
116 }
117
118 if (length > size)
119 length = size;
120
121 if (len)
122 *len = length;
123
124 if (minlen + offset < minlen ||
125 minlen + offset > pci_resource_len(dev, bar)) {
126 dev_err(&dev->dev,
127 "virtio_pci: map virtio %zu@%u "
128 "out of range on bar %i length %lu\n",
129 minlen, offset,
130 bar, (unsigned long)pci_resource_len(dev, bar));
131 return NULL;
132 }
133
134 p = pci_iomap_range(dev, bar, offset, length);
135 if (!p)
136 dev_err(&dev->dev,
137 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
138 length, offset, bar);
139 return p;
140}
141
142/* virtio config->get_features() implementation */
143static u64 vp_get_features(struct virtio_device *vdev)
144{
145 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
146 u64 features;
147
148 vp_iowrite32(0, &vp_dev->common->device_feature_select);
149 features = vp_ioread32(&vp_dev->common->device_feature);
150 vp_iowrite32(1, &vp_dev->common->device_feature_select);
151 features |= ((u64)vp_ioread32(&vp_dev->common->device_feature) << 32);
152
153 return features;
154}
155
156/* virtio config->finalize_features() implementation */
157static int vp_finalize_features(struct virtio_device *vdev)
158{
159 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
160
161 /* Give virtio_ring a chance to accept features. */
162 vring_transport_features(vdev);
163
164 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
165 dev_err(&vdev->dev, "virtio: device uses modern interface "
166 "but does not have VIRTIO_F_VERSION_1\n");
167 return -EINVAL;
168 }
169
170 vp_iowrite32(0, &vp_dev->common->guest_feature_select);
171 vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
172 vp_iowrite32(1, &vp_dev->common->guest_feature_select);
173 vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
174
175 return 0;
176}
177
178/* virtio config->get() implementation */
179static void vp_get(struct virtio_device *vdev, unsigned offset,
180 void *buf, unsigned len)
181{
182 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
183 u8 b;
184 __le16 w;
185 __le32 l;
186
187 BUG_ON(offset + len > vp_dev->device_len);
188
189 switch (len) {
190 case 1:
191 b = ioread8(vp_dev->device + offset);
192 memcpy(buf, &b, sizeof b);
193 break;
194 case 2:
195 w = cpu_to_le16(ioread16(vp_dev->device + offset));
196 memcpy(buf, &w, sizeof w);
197 break;
198 case 4:
199 l = cpu_to_le32(ioread32(vp_dev->device + offset));
200 memcpy(buf, &l, sizeof l);
201 break;
202 case 8:
203 l = cpu_to_le32(ioread32(vp_dev->device + offset));
204 memcpy(buf, &l, sizeof l);
205 l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
206 memcpy(buf + sizeof l, &l, sizeof l);
207 break;
208 default:
209 BUG();
210 }
211}
212
213/* the config->set() implementation. it's symmetric to the config->get()
214 * implementation */
215static void vp_set(struct virtio_device *vdev, unsigned offset,
216 const void *buf, unsigned len)
217{
218 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
219 u8 b;
220 __le16 w;
221 __le32 l;
222
223 BUG_ON(offset + len > vp_dev->device_len);
224
225 switch (len) {
226 case 1:
227 memcpy(&b, buf, sizeof b);
228 iowrite8(b, vp_dev->device + offset);
229 break;
230 case 2:
231 memcpy(&w, buf, sizeof w);
232 iowrite16(le16_to_cpu(w), vp_dev->device + offset);
233 break;
234 case 4:
235 memcpy(&l, buf, sizeof l);
236 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
237 break;
238 case 8:
239 memcpy(&l, buf, sizeof l);
240 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
241 memcpy(&l, buf + sizeof l, sizeof l);
242 iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
243 break;
244 default:
245 BUG();
246 }
247}
248
249static u32 vp_generation(struct virtio_device *vdev)
250{
251 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
252 return vp_ioread8(&vp_dev->common->config_generation);
253}
254
255/* config->{get,set}_status() implementations */
256static u8 vp_get_status(struct virtio_device *vdev)
257{
258 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
259 return vp_ioread8(&vp_dev->common->device_status);
260}
261
262static void vp_set_status(struct virtio_device *vdev, u8 status)
263{
264 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
265 /* We should never be setting status to 0. */
266 BUG_ON(status == 0);
267 vp_iowrite8(status, &vp_dev->common->device_status);
268}
269
270static void vp_reset(struct virtio_device *vdev)
271{
272 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
273 /* 0 status means a reset. */
274 vp_iowrite8(0, &vp_dev->common->device_status);
275 /* After writing 0 to device_status, the driver MUST wait for a read of
276 * device_status to return 0 before reinitializing the device.
277 * This will flush out the status write, and flush in device writes,
278 * including MSI-X interrupts, if any.
279 */
280 while (vp_ioread8(&vp_dev->common->device_status))
281 msleep(1);
282 /* Flush pending VQ/configuration callbacks. */
283 vp_synchronize_vectors(vdev);
284}
285
286static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
287{
288 /* Setup the vector used for configuration events */
289 vp_iowrite16(vector, &vp_dev->common->msix_config);
290 /* Verify we had enough resources to assign the vector */
291 /* Will also flush the write out to device */
292 return vp_ioread16(&vp_dev->common->msix_config);
293}
294
295static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
296 struct virtio_pci_vq_info *info,
297 unsigned index,
298 void (*callback)(struct virtqueue *vq),
299 const char *name,
300 bool ctx,
301 u16 msix_vec)
302{
303 struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
304 struct virtqueue *vq;
305 u16 num, off;
306 int err;
307
308 if (index >= vp_ioread16(&cfg->num_queues))
309 return ERR_PTR(-ENOENT);
310
311 /* Select the queue we're interested in */
312 vp_iowrite16(index, &cfg->queue_select);
313
314 /* Check if queue is either not available or already active. */
315 num = vp_ioread16(&cfg->queue_size);
316 if (!num || vp_ioread16(&cfg->queue_enable))
317 return ERR_PTR(-ENOENT);
318
319 if (num & (num - 1)) {
320 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
321 return ERR_PTR(-EINVAL);
322 }
323
324 /* get offset of notification word for this vq */
325 off = vp_ioread16(&cfg->queue_notify_off);
326
327 info->msix_vector = msix_vec;
328
329 /* create the vring */
330 vq = vring_create_virtqueue(index, num,
331 SMP_CACHE_BYTES, &vp_dev->vdev,
332 true, true, ctx,
333 vp_notify, callback, name);
334 if (!vq)
335 return ERR_PTR(-ENOMEM);
336
337 /* activate the queue */
338 vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
339 vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
340 &cfg->queue_desc_lo, &cfg->queue_desc_hi);
341 vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
342 &cfg->queue_avail_lo, &cfg->queue_avail_hi);
343 vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
344 &cfg->queue_used_lo, &cfg->queue_used_hi);
345
346 if (vp_dev->notify_base) {
347 /* offset should not wrap */
348 if ((u64)off * vp_dev->notify_offset_multiplier + 2
349 > vp_dev->notify_len) {
350 dev_warn(&vp_dev->pci_dev->dev,
351 "bad notification offset %u (x %u) "
352 "for queue %u > %zd",
353 off, vp_dev->notify_offset_multiplier,
354 index, vp_dev->notify_len);
355 err = -EINVAL;
356 goto err_map_notify;
357 }
358 vq->priv = (void __force *)vp_dev->notify_base +
359 off * vp_dev->notify_offset_multiplier;
360 } else {
361 vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
362 vp_dev->notify_map_cap, 2, 2,
363 off * vp_dev->notify_offset_multiplier, 2,
364 NULL);
365 }
366
367 if (!vq->priv) {
368 err = -ENOMEM;
369 goto err_map_notify;
370 }
371
372 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
373 vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
374 msix_vec = vp_ioread16(&cfg->queue_msix_vector);
375 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
376 err = -EBUSY;
377 goto err_assign_vector;
378 }
379 }
380
381 return vq;
382
383err_assign_vector:
384 if (!vp_dev->notify_base)
385 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
386err_map_notify:
387 vring_del_virtqueue(vq);
388 return ERR_PTR(err);
389}
390
391static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
392 struct virtqueue *vqs[],
393 vq_callback_t *callbacks[],
394 const char * const names[], const bool *ctx,
395 struct irq_affinity *desc)
396{
397 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
398 struct virtqueue *vq;
399 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
400
401 if (rc)
402 return rc;
403
404 /* Select and activate all queues. Has to be done last: once we do
405 * this, there's no way to go back except reset.
406 */
407 list_for_each_entry(vq, &vdev->vqs, list) {
408 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
409 vp_iowrite16(1, &vp_dev->common->queue_enable);
410 }
411
412 return 0;
413}
414
415static void del_vq(struct virtio_pci_vq_info *info)
416{
417 struct virtqueue *vq = info->vq;
418 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
419
420 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
421
422 if (vp_dev->msix_enabled) {
423 vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
424 &vp_dev->common->queue_msix_vector);
425 /* Flush the write out to device */
426 vp_ioread16(&vp_dev->common->queue_msix_vector);
427 }
428
429 if (!vp_dev->notify_base)
430 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
431
432 vring_del_virtqueue(vq);
433}
434
435static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
436 .get = NULL,
437 .set = NULL,
438 .generation = vp_generation,
439 .get_status = vp_get_status,
440 .set_status = vp_set_status,
441 .reset = vp_reset,
442 .find_vqs = vp_modern_find_vqs,
443 .del_vqs = vp_del_vqs,
444 .get_features = vp_get_features,
445 .finalize_features = vp_finalize_features,
446 .bus_name = vp_bus_name,
447 .set_vq_affinity = vp_set_vq_affinity,
448 .get_vq_affinity = vp_get_vq_affinity,
449};
450
451static const struct virtio_config_ops virtio_pci_config_ops = {
452 .get = vp_get,
453 .set = vp_set,
454 .generation = vp_generation,
455 .get_status = vp_get_status,
456 .set_status = vp_set_status,
457 .reset = vp_reset,
458 .find_vqs = vp_modern_find_vqs,
459 .del_vqs = vp_del_vqs,
460 .get_features = vp_get_features,
461 .finalize_features = vp_finalize_features,
462 .bus_name = vp_bus_name,
463 .set_vq_affinity = vp_set_vq_affinity,
464 .get_vq_affinity = vp_get_vq_affinity,
465};
466
467/**
468 * virtio_pci_find_capability - walk capabilities to find device info.
469 * @dev: the pci device
470 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
471 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
472 *
473 * Returns offset of the capability, or 0.
474 */
475static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
476 u32 ioresource_types, int *bars)
477{
478 int pos;
479
480 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
481 pos > 0;
482 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
483 u8 type, bar;
484 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
485 cfg_type),
486 &type);
487 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
488 bar),
489 &bar);
490
491 /* Ignore structures with reserved BAR values */
492 if (bar > 0x5)
493 continue;
494
495 if (type == cfg_type) {
496 if (pci_resource_len(dev, bar) &&
497 pci_resource_flags(dev, bar) & ioresource_types) {
498 *bars |= (1 << bar);
499 return pos;
500 }
501 }
502 }
503 return 0;
504}
505
506/* This is part of the ABI. Don't screw with it. */
507static inline void check_offsets(void)
508{
509 /* Note: disk space was harmed in compilation of this function. */
510 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
511 offsetof(struct virtio_pci_cap, cap_vndr));
512 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
513 offsetof(struct virtio_pci_cap, cap_next));
514 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
515 offsetof(struct virtio_pci_cap, cap_len));
516 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
517 offsetof(struct virtio_pci_cap, cfg_type));
518 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
519 offsetof(struct virtio_pci_cap, bar));
520 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
521 offsetof(struct virtio_pci_cap, offset));
522 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
523 offsetof(struct virtio_pci_cap, length));
524 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
525 offsetof(struct virtio_pci_notify_cap,
526 notify_off_multiplier));
527 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
528 offsetof(struct virtio_pci_common_cfg,
529 device_feature_select));
530 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
531 offsetof(struct virtio_pci_common_cfg, device_feature));
532 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
533 offsetof(struct virtio_pci_common_cfg,
534 guest_feature_select));
535 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
536 offsetof(struct virtio_pci_common_cfg, guest_feature));
537 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
538 offsetof(struct virtio_pci_common_cfg, msix_config));
539 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
540 offsetof(struct virtio_pci_common_cfg, num_queues));
541 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
542 offsetof(struct virtio_pci_common_cfg, device_status));
543 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
544 offsetof(struct virtio_pci_common_cfg, config_generation));
545 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
546 offsetof(struct virtio_pci_common_cfg, queue_select));
547 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
548 offsetof(struct virtio_pci_common_cfg, queue_size));
549 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
550 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
551 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
552 offsetof(struct virtio_pci_common_cfg, queue_enable));
553 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
554 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
555 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
556 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
557 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
558 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
559 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
560 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
561 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
562 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
563 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
564 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
565 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
566 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
567}
568
569/* the PCI probing function */
570int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
571{
572 struct pci_dev *pci_dev = vp_dev->pci_dev;
573 int err, common, isr, notify, device;
574 u32 notify_length;
575 u32 notify_offset;
576
577 check_offsets();
578
579 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
580 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
581 return -ENODEV;
582
583 if (pci_dev->device < 0x1040) {
584 /* Transitional devices: use the PCI subsystem device id as
585 * virtio device id, same as legacy driver always did.
586 */
587 vp_dev->vdev.id.device = pci_dev->subsystem_device;
588 } else {
589 /* Modern devices: simply use PCI device id, but start from 0x1040. */
590 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
591 }
592 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
593
594 /* check for a common config: if not, use legacy mode (bar 0). */
595 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
596 IORESOURCE_IO | IORESOURCE_MEM,
597 &vp_dev->modern_bars);
598 if (!common) {
599 dev_info(&pci_dev->dev,
600 "virtio_pci: leaving for legacy driver\n");
601 return -ENODEV;
602 }
603
604 /* If common is there, these should be too... */
605 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
606 IORESOURCE_IO | IORESOURCE_MEM,
607 &vp_dev->modern_bars);
608 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
609 IORESOURCE_IO | IORESOURCE_MEM,
610 &vp_dev->modern_bars);
611 if (!isr || !notify) {
612 dev_err(&pci_dev->dev,
613 "virtio_pci: missing capabilities %i/%i/%i\n",
614 common, isr, notify);
615 return -EINVAL;
616 }
617
618 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
619 if (err)
620 err = dma_set_mask_and_coherent(&pci_dev->dev,
621 DMA_BIT_MASK(32));
622 if (err)
623 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
624
625 /* Device capability is only mandatory for devices that have
626 * device-specific configuration.
627 */
628 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
629 IORESOURCE_IO | IORESOURCE_MEM,
630 &vp_dev->modern_bars);
631
632 err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
633 "virtio-pci-modern");
634 if (err)
635 return err;
636
637 err = -EINVAL;
638 vp_dev->common = map_capability(pci_dev, common,
639 sizeof(struct virtio_pci_common_cfg), 4,
640 0, sizeof(struct virtio_pci_common_cfg),
641 NULL);
642 if (!vp_dev->common)
643 goto err_map_common;
644 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
645 0, 1,
646 NULL);
647 if (!vp_dev->isr)
648 goto err_map_isr;
649
650 /* Read notify_off_multiplier from config space. */
651 pci_read_config_dword(pci_dev,
652 notify + offsetof(struct virtio_pci_notify_cap,
653 notify_off_multiplier),
654 &vp_dev->notify_offset_multiplier);
655 /* Read notify length and offset from config space. */
656 pci_read_config_dword(pci_dev,
657 notify + offsetof(struct virtio_pci_notify_cap,
658 cap.length),
659 ¬ify_length);
660
661 pci_read_config_dword(pci_dev,
662 notify + offsetof(struct virtio_pci_notify_cap,
663 cap.offset),
664 ¬ify_offset);
665
666 /* We don't know how many VQs we'll map, ahead of the time.
667 * If notify length is small, map it all now.
668 * Otherwise, map each VQ individually later.
669 */
670 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
671 vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
672 0, notify_length,
673 &vp_dev->notify_len);
674 if (!vp_dev->notify_base)
675 goto err_map_notify;
676 } else {
677 vp_dev->notify_map_cap = notify;
678 }
679
680 /* Again, we don't know how much we should map, but PAGE_SIZE
681 * is more than enough for all existing devices.
682 */
683 if (device) {
684 vp_dev->device = map_capability(pci_dev, device, 0, 4,
685 0, PAGE_SIZE,
686 &vp_dev->device_len);
687 if (!vp_dev->device)
688 goto err_map_device;
689
690 vp_dev->vdev.config = &virtio_pci_config_ops;
691 } else {
692 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
693 }
694
695 vp_dev->config_vector = vp_config_vector;
696 vp_dev->setup_vq = setup_vq;
697 vp_dev->del_vq = del_vq;
698
699 return 0;
700
701err_map_device:
702 if (vp_dev->notify_base)
703 pci_iounmap(pci_dev, vp_dev->notify_base);
704err_map_notify:
705 pci_iounmap(pci_dev, vp_dev->isr);
706err_map_isr:
707 pci_iounmap(pci_dev, vp_dev->common);
708err_map_common:
709 return err;
710}
711
712void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
713{
714 struct pci_dev *pci_dev = vp_dev->pci_dev;
715
716 if (vp_dev->device)
717 pci_iounmap(pci_dev, vp_dev->device);
718 if (vp_dev->notify_base)
719 pci_iounmap(pci_dev, vp_dev->notify_base);
720 pci_iounmap(pci_dev, vp_dev->isr);
721 pci_iounmap(pci_dev, vp_dev->common);
722 pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
723}