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