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 	return err;
351 }
352 EXPORT_SYMBOL_GPL(vp_modern_probe);
353 
354 /*
355  * vp_modern_remove: remove and cleanup the modern virtio pci device
356  * @mdev: the modern virtio-pci device
357  */
358 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
359 {
360 	struct pci_dev *pci_dev = mdev->pci_dev;
361 
362 	if (mdev->device)
363 		pci_iounmap(pci_dev, mdev->device);
364 	if (mdev->notify_base)
365 		pci_iounmap(pci_dev, mdev->notify_base);
366 	pci_iounmap(pci_dev, mdev->isr);
367 	pci_iounmap(pci_dev, mdev->common);
368 	pci_release_selected_regions(pci_dev, mdev->modern_bars);
369 }
370 EXPORT_SYMBOL_GPL(vp_modern_remove);
371 
372 /*
373  * vp_modern_get_features - get features from device
374  * @mdev: the modern virtio-pci device
375  *
376  * Returns the features read from the device
377  */
378 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
379 {
380 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
381 
382 	u64 features;
383 
384 	vp_iowrite32(0, &cfg->device_feature_select);
385 	features = vp_ioread32(&cfg->device_feature);
386 	vp_iowrite32(1, &cfg->device_feature_select);
387 	features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
388 
389 	return features;
390 }
391 EXPORT_SYMBOL_GPL(vp_modern_get_features);
392 
393 /*
394  * vp_modern_get_driver_features - get driver features from device
395  * @mdev: the modern virtio-pci device
396  *
397  * Returns the driver features read from the device
398  */
399 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
400 {
401 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
402 
403 	u64 features;
404 
405 	vp_iowrite32(0, &cfg->guest_feature_select);
406 	features = vp_ioread32(&cfg->guest_feature);
407 	vp_iowrite32(1, &cfg->guest_feature_select);
408 	features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
409 
410 	return features;
411 }
412 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
413 
414 /*
415  * vp_modern_set_features - set features to device
416  * @mdev: the modern virtio-pci device
417  * @features: the features set to device
418  */
419 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
420 			    u64 features)
421 {
422 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
423 
424 	vp_iowrite32(0, &cfg->guest_feature_select);
425 	vp_iowrite32((u32)features, &cfg->guest_feature);
426 	vp_iowrite32(1, &cfg->guest_feature_select);
427 	vp_iowrite32(features >> 32, &cfg->guest_feature);
428 }
429 EXPORT_SYMBOL_GPL(vp_modern_set_features);
430 
431 /*
432  * vp_modern_generation - get the device genreation
433  * @mdev: the modern virtio-pci device
434  *
435  * Returns the genreation read from device
436  */
437 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
438 {
439 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
440 
441 	return vp_ioread8(&cfg->config_generation);
442 }
443 EXPORT_SYMBOL_GPL(vp_modern_generation);
444 
445 /*
446  * vp_modern_get_status - get the device status
447  * @mdev: the modern virtio-pci device
448  *
449  * Returns the status read from device
450  */
451 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
452 {
453 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
454 
455 	return vp_ioread8(&cfg->device_status);
456 }
457 EXPORT_SYMBOL_GPL(vp_modern_get_status);
458 
459 /*
460  * vp_modern_set_status - set status to device
461  * @mdev: the modern virtio-pci device
462  * @status: the status set to device
463  */
464 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
465 				 u8 status)
466 {
467 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
468 
469 	vp_iowrite8(status, &cfg->device_status);
470 }
471 EXPORT_SYMBOL_GPL(vp_modern_set_status);
472 
473 /*
474  * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
475  * @mdev: the modern virtio-pci device
476  * @index: queue index
477  * @vector: the config vector
478  *
479  * Returns the config vector read from the device
480  */
481 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
482 			   u16 index, u16 vector)
483 {
484 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
485 
486 	vp_iowrite16(index, &cfg->queue_select);
487 	vp_iowrite16(vector, &cfg->queue_msix_vector);
488 	/* Flush the write out to device */
489 	return vp_ioread16(&cfg->queue_msix_vector);
490 }
491 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
492 
493 /*
494  * vp_modern_config_vector - set the vector for config interrupt
495  * @mdev: the modern virtio-pci device
496  * @vector: the config vector
497  *
498  * Returns the config vector read from the device
499  */
500 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
501 			    u16 vector)
502 {
503 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
504 
505 	/* Setup the vector used for configuration events */
506 	vp_iowrite16(vector, &cfg->msix_config);
507 	/* Verify we had enough resources to assign the vector */
508 	/* Will also flush the write out to device */
509 	return vp_ioread16(&cfg->msix_config);
510 }
511 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
512 
513 /*
514  * vp_modern_queue_address - set the virtqueue address
515  * @mdev: the modern virtio-pci device
516  * @index: the queue index
517  * @desc_addr: address of the descriptor area
518  * @driver_addr: address of the driver area
519  * @device_addr: address of the device area
520  */
521 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
522 			     u16 index, u64 desc_addr, u64 driver_addr,
523 			     u64 device_addr)
524 {
525 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
526 
527 	vp_iowrite16(index, &cfg->queue_select);
528 
529 	vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
530 			     &cfg->queue_desc_hi);
531 	vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
532 			     &cfg->queue_avail_hi);
533 	vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
534 			     &cfg->queue_used_hi);
535 }
536 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
537 
538 /*
539  * vp_modern_set_queue_enable - enable a virtqueue
540  * @mdev: the modern virtio-pci device
541  * @index: the queue index
542  * @enable: whether the virtqueue is enable or not
543  */
544 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
545 				u16 index, bool enable)
546 {
547 	vp_iowrite16(index, &mdev->common->queue_select);
548 	vp_iowrite16(enable, &mdev->common->queue_enable);
549 }
550 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
551 
552 /*
553  * vp_modern_get_queue_enable - enable a virtqueue
554  * @mdev: the modern virtio-pci device
555  * @index: the queue index
556  *
557  * Returns whether a virtqueue is enabled or not
558  */
559 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
560 				u16 index)
561 {
562 	vp_iowrite16(index, &mdev->common->queue_select);
563 
564 	return vp_ioread16(&mdev->common->queue_enable);
565 }
566 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
567 
568 /*
569  * vp_modern_set_queue_size - set size for a virtqueue
570  * @mdev: the modern virtio-pci device
571  * @index: the queue index
572  * @size: the size of the virtqueue
573  */
574 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
575 			      u16 index, u16 size)
576 {
577 	vp_iowrite16(index, &mdev->common->queue_select);
578 	vp_iowrite16(size, &mdev->common->queue_size);
579 
580 }
581 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
582 
583 /*
584  * vp_modern_get_queue_size - get size for a virtqueue
585  * @mdev: the modern virtio-pci device
586  * @index: the queue index
587  *
588  * Returns the size of the virtqueue
589  */
590 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
591 			     u16 index)
592 {
593 	vp_iowrite16(index, &mdev->common->queue_select);
594 
595 	return vp_ioread16(&mdev->common->queue_size);
596 
597 }
598 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
599 
600 /*
601  * vp_modern_get_num_queues - get the number of virtqueues
602  * @mdev: the modern virtio-pci device
603  *
604  * Returns the number of virtqueues
605  */
606 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
607 {
608 	return vp_ioread16(&mdev->common->num_queues);
609 }
610 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
611 
612 /*
613  * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
614  * @mdev: the modern virtio-pci device
615  * @index: the queue index
616  *
617  * Returns the notification offset for a virtqueue
618  */
619 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
620 					  u16 index)
621 {
622 	vp_iowrite16(index, &mdev->common->queue_select);
623 
624 	return vp_ioread16(&mdev->common->queue_notify_off);
625 }
626 
627 /*
628  * vp_modern_map_vq_notify - map notification area for a
629  * specific virtqueue
630  * @mdev: the modern virtio-pci device
631  * @index: the queue index
632  * @pa: the pointer to the physical address of the nofity area
633  *
634  * Returns the address of the notification area
635  */
636 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
637 				      u16 index, resource_size_t *pa)
638 {
639 	u16 off = vp_modern_get_queue_notify_off(mdev, index);
640 
641 	if (mdev->notify_base) {
642 		/* offset should not wrap */
643 		if ((u64)off * mdev->notify_offset_multiplier + 2
644 			> mdev->notify_len) {
645 			dev_warn(&mdev->pci_dev->dev,
646 				 "bad notification offset %u (x %u) "
647 				 "for queue %u > %zd",
648 				 off, mdev->notify_offset_multiplier,
649 				 index, mdev->notify_len);
650 			return NULL;
651 		}
652 		if (pa)
653 			*pa = mdev->notify_pa +
654 			      off * mdev->notify_offset_multiplier;
655 		return mdev->notify_base + off * mdev->notify_offset_multiplier;
656 	} else {
657 		return vp_modern_map_capability(mdev,
658 				       mdev->notify_map_cap, 2, 2,
659 				       off * mdev->notify_offset_multiplier, 2,
660 				       NULL, pa);
661 	}
662 }
663 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
664 
665 MODULE_VERSION("0.1");
666 MODULE_DESCRIPTION("Modern Virtio PCI Device");
667 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
668 MODULE_LICENSE("GPL");
669