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