Linux 6.7-rc7
[linux-modified.git] / drivers / virtio / virtio_pci_modern_dev.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #include <linux/virtio_pci_modern.h>
4 #include <linux/module.h>
5 #include <linux/pci.h>
6 #include <linux/delay.h>
7
8 /*
9  * vp_modern_map_capability - map a part of virtio pci capability
10  * @mdev: the modern virtio-pci device
11  * @off: offset of the capability
12  * @minlen: minimal length of the capability
13  * @align: align requirement
14  * @start: start from the capability
15  * @size: map size
16  * @len: the length that is actually mapped
17  * @pa: physical address of the capability
18  *
19  * Returns the io address of for the part of the capability
20  */
21 static void __iomem *
22 vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
23                          size_t minlen, u32 align, u32 start, u32 size,
24                          size_t *len, resource_size_t *pa)
25 {
26         struct pci_dev *dev = mdev->pci_dev;
27         u8 bar;
28         u32 offset, length;
29         void __iomem *p;
30
31         pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
32                                                  bar),
33                              &bar);
34         pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
35                              &offset);
36         pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
37                               &length);
38
39         /* Check if the BAR may have changed since we requested the region. */
40         if (bar >= PCI_STD_NUM_BARS || !(mdev->modern_bars & (1 << bar))) {
41                 dev_err(&dev->dev,
42                         "virtio_pci: bar unexpectedly changed to %u\n", bar);
43                 return NULL;
44         }
45
46         if (length <= start) {
47                 dev_err(&dev->dev,
48                         "virtio_pci: bad capability len %u (>%u expected)\n",
49                         length, start);
50                 return NULL;
51         }
52
53         if (length - start < minlen) {
54                 dev_err(&dev->dev,
55                         "virtio_pci: bad capability len %u (>=%zu expected)\n",
56                         length, minlen);
57                 return NULL;
58         }
59
60         length -= start;
61
62         if (start + offset < offset) {
63                 dev_err(&dev->dev,
64                         "virtio_pci: map wrap-around %u+%u\n",
65                         start, offset);
66                 return NULL;
67         }
68
69         offset += start;
70
71         if (offset & (align - 1)) {
72                 dev_err(&dev->dev,
73                         "virtio_pci: offset %u not aligned to %u\n",
74                         offset, align);
75                 return NULL;
76         }
77
78         if (length > size)
79                 length = size;
80
81         if (len)
82                 *len = length;
83
84         if (minlen + offset < minlen ||
85             minlen + offset > pci_resource_len(dev, bar)) {
86                 dev_err(&dev->dev,
87                         "virtio_pci: map virtio %zu@%u "
88                         "out of range on bar %i length %lu\n",
89                         minlen, offset,
90                         bar, (unsigned long)pci_resource_len(dev, bar));
91                 return NULL;
92         }
93
94         p = pci_iomap_range(dev, bar, offset, length);
95         if (!p)
96                 dev_err(&dev->dev,
97                         "virtio_pci: unable to map virtio %u@%u on bar %i\n",
98                         length, offset, bar);
99         else if (pa)
100                 *pa = pci_resource_start(dev, bar) + offset;
101
102         return p;
103 }
104
105 /**
106  * virtio_pci_find_capability - walk capabilities to find device info.
107  * @dev: the pci device
108  * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
109  * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
110  * @bars: the bitmask of BARs
111  *
112  * Returns offset of the capability, or 0.
113  */
114 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
115                                              u32 ioresource_types, int *bars)
116 {
117         int pos;
118
119         for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
120              pos > 0;
121              pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
122                 u8 type, bar;
123                 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
124                                                          cfg_type),
125                                      &type);
126                 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
127                                                          bar),
128                                      &bar);
129
130                 /* Ignore structures with reserved BAR values */
131                 if (bar >= PCI_STD_NUM_BARS)
132                         continue;
133
134                 if (type == cfg_type) {
135                         if (pci_resource_len(dev, bar) &&
136                             pci_resource_flags(dev, bar) & ioresource_types) {
137                                 *bars |= (1 << bar);
138                                 return pos;
139                         }
140                 }
141         }
142         return 0;
143 }
144
145 /* This is part of the ABI.  Don't screw with it. */
146 static inline void check_offsets(void)
147 {
148         /* Note: disk space was harmed in compilation of this function. */
149         BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
150                      offsetof(struct virtio_pci_cap, cap_vndr));
151         BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
152                      offsetof(struct virtio_pci_cap, cap_next));
153         BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
154                      offsetof(struct virtio_pci_cap, cap_len));
155         BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
156                      offsetof(struct virtio_pci_cap, cfg_type));
157         BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
158                      offsetof(struct virtio_pci_cap, bar));
159         BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
160                      offsetof(struct virtio_pci_cap, offset));
161         BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
162                      offsetof(struct virtio_pci_cap, length));
163         BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
164                      offsetof(struct virtio_pci_notify_cap,
165                               notify_off_multiplier));
166         BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
167                      offsetof(struct virtio_pci_common_cfg,
168                               device_feature_select));
169         BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
170                      offsetof(struct virtio_pci_common_cfg, device_feature));
171         BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
172                      offsetof(struct virtio_pci_common_cfg,
173                               guest_feature_select));
174         BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
175                      offsetof(struct virtio_pci_common_cfg, guest_feature));
176         BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
177                      offsetof(struct virtio_pci_common_cfg, msix_config));
178         BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
179                      offsetof(struct virtio_pci_common_cfg, num_queues));
180         BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
181                      offsetof(struct virtio_pci_common_cfg, device_status));
182         BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
183                      offsetof(struct virtio_pci_common_cfg, config_generation));
184         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
185                      offsetof(struct virtio_pci_common_cfg, queue_select));
186         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
187                      offsetof(struct virtio_pci_common_cfg, queue_size));
188         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
189                      offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
190         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
191                      offsetof(struct virtio_pci_common_cfg, queue_enable));
192         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
193                      offsetof(struct virtio_pci_common_cfg, queue_notify_off));
194         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
195                      offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
196         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
197                      offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
198         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
199                      offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
200         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
201                      offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
202         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
203                      offsetof(struct virtio_pci_common_cfg, queue_used_lo));
204         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
205                      offsetof(struct virtio_pci_common_cfg, queue_used_hi));
206         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NDATA !=
207                      offsetof(struct virtio_pci_modern_common_cfg, queue_notify_data));
208         BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_RESET !=
209                      offsetof(struct virtio_pci_modern_common_cfg, queue_reset));
210 }
211
212 /*
213  * vp_modern_probe: probe the modern virtio pci device, note that the
214  * caller is required to enable PCI device before calling this function.
215  * @mdev: the modern virtio-pci device
216  *
217  * Return 0 on succeed otherwise fail
218  */
219 int vp_modern_probe(struct virtio_pci_modern_device *mdev)
220 {
221         struct pci_dev *pci_dev = mdev->pci_dev;
222         int err, common, isr, notify, device;
223         u32 notify_length;
224         u32 notify_offset;
225         int devid;
226
227         check_offsets();
228
229         if (mdev->device_id_check) {
230                 devid = mdev->device_id_check(pci_dev);
231                 if (devid < 0)
232                         return devid;
233                 mdev->id.device = devid;
234         } else {
235                 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
236                 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
237                         return -ENODEV;
238
239                 if (pci_dev->device < 0x1040) {
240                         /* Transitional devices: use the PCI subsystem device id as
241                          * virtio device id, same as legacy driver always did.
242                          */
243                         mdev->id.device = pci_dev->subsystem_device;
244                 } else {
245                         /* Modern devices: simply use PCI device id, but start from 0x1040. */
246                         mdev->id.device = pci_dev->device - 0x1040;
247                 }
248         }
249         mdev->id.vendor = pci_dev->subsystem_vendor;
250
251         /* check for a common config: if not, use legacy mode (bar 0). */
252         common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
253                                             IORESOURCE_IO | IORESOURCE_MEM,
254                                             &mdev->modern_bars);
255         if (!common) {
256                 dev_info(&pci_dev->dev,
257                          "virtio_pci: leaving for legacy driver\n");
258                 return -ENODEV;
259         }
260
261         /* If common is there, these should be too... */
262         isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
263                                          IORESOURCE_IO | IORESOURCE_MEM,
264                                          &mdev->modern_bars);
265         notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
266                                             IORESOURCE_IO | IORESOURCE_MEM,
267                                             &mdev->modern_bars);
268         if (!isr || !notify) {
269                 dev_err(&pci_dev->dev,
270                         "virtio_pci: missing capabilities %i/%i/%i\n",
271                         common, isr, notify);
272                 return -EINVAL;
273         }
274
275         err = dma_set_mask_and_coherent(&pci_dev->dev,
276                                         mdev->dma_mask ? : DMA_BIT_MASK(64));
277         if (err)
278                 err = dma_set_mask_and_coherent(&pci_dev->dev,
279                                                 DMA_BIT_MASK(32));
280         if (err)
281                 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
282
283         /* Device capability is only mandatory for devices that have
284          * device-specific configuration.
285          */
286         device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
287                                             IORESOURCE_IO | IORESOURCE_MEM,
288                                             &mdev->modern_bars);
289
290         err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
291                                            "virtio-pci-modern");
292         if (err)
293                 return err;
294
295         err = -EINVAL;
296         mdev->common = vp_modern_map_capability(mdev, common,
297                               sizeof(struct virtio_pci_common_cfg), 4, 0,
298                               offsetofend(struct virtio_pci_modern_common_cfg,
299                                           queue_reset),
300                               &mdev->common_len, NULL);
301         if (!mdev->common)
302                 goto err_map_common;
303         mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
304                                              0, 1,
305                                              NULL, NULL);
306         if (!mdev->isr)
307                 goto err_map_isr;
308
309         /* Read notify_off_multiplier from config space. */
310         pci_read_config_dword(pci_dev,
311                               notify + offsetof(struct virtio_pci_notify_cap,
312                                                 notify_off_multiplier),
313                               &mdev->notify_offset_multiplier);
314         /* Read notify length and offset from config space. */
315         pci_read_config_dword(pci_dev,
316                               notify + offsetof(struct virtio_pci_notify_cap,
317                                                 cap.length),
318                               &notify_length);
319
320         pci_read_config_dword(pci_dev,
321                               notify + offsetof(struct virtio_pci_notify_cap,
322                                                 cap.offset),
323                               &notify_offset);
324
325         /* We don't know how many VQs we'll map, ahead of the time.
326          * If notify length is small, map it all now.
327          * Otherwise, map each VQ individually later.
328          */
329         if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
330                 mdev->notify_base = vp_modern_map_capability(mdev, notify,
331                                                              2, 2,
332                                                              0, notify_length,
333                                                              &mdev->notify_len,
334                                                              &mdev->notify_pa);
335                 if (!mdev->notify_base)
336                         goto err_map_notify;
337         } else {
338                 mdev->notify_map_cap = notify;
339         }
340
341         /* Again, we don't know how much we should map, but PAGE_SIZE
342          * is more than enough for all existing devices.
343          */
344         if (device) {
345                 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
346                                                         0, PAGE_SIZE,
347                                                         &mdev->device_len,
348                                                         NULL);
349                 if (!mdev->device)
350                         goto err_map_device;
351         }
352
353         return 0;
354
355 err_map_device:
356         if (mdev->notify_base)
357                 pci_iounmap(pci_dev, mdev->notify_base);
358 err_map_notify:
359         pci_iounmap(pci_dev, mdev->isr);
360 err_map_isr:
361         pci_iounmap(pci_dev, mdev->common);
362 err_map_common:
363         pci_release_selected_regions(pci_dev, mdev->modern_bars);
364         return err;
365 }
366 EXPORT_SYMBOL_GPL(vp_modern_probe);
367
368 /*
369  * vp_modern_remove: remove and cleanup the modern virtio pci device
370  * @mdev: the modern virtio-pci device
371  */
372 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
373 {
374         struct pci_dev *pci_dev = mdev->pci_dev;
375
376         if (mdev->device)
377                 pci_iounmap(pci_dev, mdev->device);
378         if (mdev->notify_base)
379                 pci_iounmap(pci_dev, mdev->notify_base);
380         pci_iounmap(pci_dev, mdev->isr);
381         pci_iounmap(pci_dev, mdev->common);
382         pci_release_selected_regions(pci_dev, mdev->modern_bars);
383 }
384 EXPORT_SYMBOL_GPL(vp_modern_remove);
385
386 /*
387  * vp_modern_get_features - get features from device
388  * @mdev: the modern virtio-pci device
389  *
390  * Returns the features read from the device
391  */
392 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
393 {
394         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
395
396         u64 features;
397
398         vp_iowrite32(0, &cfg->device_feature_select);
399         features = vp_ioread32(&cfg->device_feature);
400         vp_iowrite32(1, &cfg->device_feature_select);
401         features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
402
403         return features;
404 }
405 EXPORT_SYMBOL_GPL(vp_modern_get_features);
406
407 /*
408  * vp_modern_get_driver_features - get driver features from device
409  * @mdev: the modern virtio-pci device
410  *
411  * Returns the driver features read from the device
412  */
413 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
414 {
415         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
416
417         u64 features;
418
419         vp_iowrite32(0, &cfg->guest_feature_select);
420         features = vp_ioread32(&cfg->guest_feature);
421         vp_iowrite32(1, &cfg->guest_feature_select);
422         features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
423
424         return features;
425 }
426 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
427
428 /*
429  * vp_modern_set_features - set features to device
430  * @mdev: the modern virtio-pci device
431  * @features: the features set to device
432  */
433 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
434                             u64 features)
435 {
436         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
437
438         vp_iowrite32(0, &cfg->guest_feature_select);
439         vp_iowrite32((u32)features, &cfg->guest_feature);
440         vp_iowrite32(1, &cfg->guest_feature_select);
441         vp_iowrite32(features >> 32, &cfg->guest_feature);
442 }
443 EXPORT_SYMBOL_GPL(vp_modern_set_features);
444
445 /*
446  * vp_modern_generation - get the device genreation
447  * @mdev: the modern virtio-pci device
448  *
449  * Returns the genreation read from device
450  */
451 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
452 {
453         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
454
455         return vp_ioread8(&cfg->config_generation);
456 }
457 EXPORT_SYMBOL_GPL(vp_modern_generation);
458
459 /*
460  * vp_modern_get_status - get the device status
461  * @mdev: the modern virtio-pci device
462  *
463  * Returns the status read from device
464  */
465 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
466 {
467         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
468
469         return vp_ioread8(&cfg->device_status);
470 }
471 EXPORT_SYMBOL_GPL(vp_modern_get_status);
472
473 /*
474  * vp_modern_set_status - set status to device
475  * @mdev: the modern virtio-pci device
476  * @status: the status set to device
477  */
478 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
479                                  u8 status)
480 {
481         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
482
483         /*
484          * Per memory-barriers.txt, wmb() is not needed to guarantee
485          * that the cache coherent memory writes have completed
486          * before writing to the MMIO region.
487          */
488         vp_iowrite8(status, &cfg->device_status);
489 }
490 EXPORT_SYMBOL_GPL(vp_modern_set_status);
491
492 /*
493  * vp_modern_get_queue_reset - get the queue reset status
494  * @mdev: the modern virtio-pci device
495  * @index: queue index
496  */
497 int vp_modern_get_queue_reset(struct virtio_pci_modern_device *mdev, u16 index)
498 {
499         struct virtio_pci_modern_common_cfg __iomem *cfg;
500
501         cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
502
503         vp_iowrite16(index, &cfg->cfg.queue_select);
504         return vp_ioread16(&cfg->queue_reset);
505 }
506 EXPORT_SYMBOL_GPL(vp_modern_get_queue_reset);
507
508 /*
509  * vp_modern_set_queue_reset - reset the queue
510  * @mdev: the modern virtio-pci device
511  * @index: queue index
512  */
513 void vp_modern_set_queue_reset(struct virtio_pci_modern_device *mdev, u16 index)
514 {
515         struct virtio_pci_modern_common_cfg __iomem *cfg;
516
517         cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
518
519         vp_iowrite16(index, &cfg->cfg.queue_select);
520         vp_iowrite16(1, &cfg->queue_reset);
521
522         while (vp_ioread16(&cfg->queue_reset))
523                 msleep(1);
524
525         while (vp_ioread16(&cfg->cfg.queue_enable))
526                 msleep(1);
527 }
528 EXPORT_SYMBOL_GPL(vp_modern_set_queue_reset);
529
530 /*
531  * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
532  * @mdev: the modern virtio-pci device
533  * @index: queue index
534  * @vector: the config vector
535  *
536  * Returns the config vector read from the device
537  */
538 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
539                            u16 index, u16 vector)
540 {
541         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
542
543         vp_iowrite16(index, &cfg->queue_select);
544         vp_iowrite16(vector, &cfg->queue_msix_vector);
545         /* Flush the write out to device */
546         return vp_ioread16(&cfg->queue_msix_vector);
547 }
548 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
549
550 /*
551  * vp_modern_config_vector - set the vector for config interrupt
552  * @mdev: the modern virtio-pci device
553  * @vector: the config vector
554  *
555  * Returns the config vector read from the device
556  */
557 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
558                             u16 vector)
559 {
560         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
561
562         /* Setup the vector used for configuration events */
563         vp_iowrite16(vector, &cfg->msix_config);
564         /* Verify we had enough resources to assign the vector */
565         /* Will also flush the write out to device */
566         return vp_ioread16(&cfg->msix_config);
567 }
568 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
569
570 /*
571  * vp_modern_queue_address - set the virtqueue address
572  * @mdev: the modern virtio-pci device
573  * @index: the queue index
574  * @desc_addr: address of the descriptor area
575  * @driver_addr: address of the driver area
576  * @device_addr: address of the device area
577  */
578 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
579                              u16 index, u64 desc_addr, u64 driver_addr,
580                              u64 device_addr)
581 {
582         struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
583
584         vp_iowrite16(index, &cfg->queue_select);
585
586         vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
587                              &cfg->queue_desc_hi);
588         vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
589                              &cfg->queue_avail_hi);
590         vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
591                              &cfg->queue_used_hi);
592 }
593 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
594
595 /*
596  * vp_modern_set_queue_enable - enable a virtqueue
597  * @mdev: the modern virtio-pci device
598  * @index: the queue index
599  * @enable: whether the virtqueue is enable or not
600  */
601 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
602                                 u16 index, bool enable)
603 {
604         vp_iowrite16(index, &mdev->common->queue_select);
605         vp_iowrite16(enable, &mdev->common->queue_enable);
606 }
607 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
608
609 /*
610  * vp_modern_get_queue_enable - enable a virtqueue
611  * @mdev: the modern virtio-pci device
612  * @index: the queue index
613  *
614  * Returns whether a virtqueue is enabled or not
615  */
616 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
617                                 u16 index)
618 {
619         vp_iowrite16(index, &mdev->common->queue_select);
620
621         return vp_ioread16(&mdev->common->queue_enable);
622 }
623 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
624
625 /*
626  * vp_modern_set_queue_size - set size for a virtqueue
627  * @mdev: the modern virtio-pci device
628  * @index: the queue index
629  * @size: the size of the virtqueue
630  */
631 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
632                               u16 index, u16 size)
633 {
634         vp_iowrite16(index, &mdev->common->queue_select);
635         vp_iowrite16(size, &mdev->common->queue_size);
636
637 }
638 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
639
640 /*
641  * vp_modern_get_queue_size - get size for a virtqueue
642  * @mdev: the modern virtio-pci device
643  * @index: the queue index
644  *
645  * Returns the size of the virtqueue
646  */
647 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
648                              u16 index)
649 {
650         vp_iowrite16(index, &mdev->common->queue_select);
651
652         return vp_ioread16(&mdev->common->queue_size);
653
654 }
655 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
656
657 /*
658  * vp_modern_get_num_queues - get the number of virtqueues
659  * @mdev: the modern virtio-pci device
660  *
661  * Returns the number of virtqueues
662  */
663 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
664 {
665         return vp_ioread16(&mdev->common->num_queues);
666 }
667 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
668
669 /*
670  * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
671  * @mdev: the modern virtio-pci device
672  * @index: the queue index
673  *
674  * Returns the notification offset for a virtqueue
675  */
676 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
677                                           u16 index)
678 {
679         vp_iowrite16(index, &mdev->common->queue_select);
680
681         return vp_ioread16(&mdev->common->queue_notify_off);
682 }
683
684 /*
685  * vp_modern_map_vq_notify - map notification area for a
686  * specific virtqueue
687  * @mdev: the modern virtio-pci device
688  * @index: the queue index
689  * @pa: the pointer to the physical address of the nofity area
690  *
691  * Returns the address of the notification area
692  */
693 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
694                                       u16 index, resource_size_t *pa)
695 {
696         u16 off = vp_modern_get_queue_notify_off(mdev, index);
697
698         if (mdev->notify_base) {
699                 /* offset should not wrap */
700                 if ((u64)off * mdev->notify_offset_multiplier + 2
701                         > mdev->notify_len) {
702                         dev_warn(&mdev->pci_dev->dev,
703                                  "bad notification offset %u (x %u) "
704                                  "for queue %u > %zd",
705                                  off, mdev->notify_offset_multiplier,
706                                  index, mdev->notify_len);
707                         return NULL;
708                 }
709                 if (pa)
710                         *pa = mdev->notify_pa +
711                               off * mdev->notify_offset_multiplier;
712                 return mdev->notify_base + off * mdev->notify_offset_multiplier;
713         } else {
714                 return vp_modern_map_capability(mdev,
715                                        mdev->notify_map_cap, 2, 2,
716                                        off * mdev->notify_offset_multiplier, 2,
717                                        NULL, pa);
718         }
719 }
720 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
721
722 MODULE_VERSION("0.1");
723 MODULE_DESCRIPTION("Modern Virtio PCI Device");
724 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
725 MODULE_LICENSE("GPL");