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 *
vp_modern_map_capability(struct virtio_pci_modern_device * mdev,int off,size_t minlen,u32 align,u32 start,u32 size,size_t * len,resource_size_t * pa)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  */
virtio_pci_find_capability(struct pci_dev * dev,u8 cfg_type,u32 ioresource_types,int * bars)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. */
check_offsets(void)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 }
207 
208 /*
209  * vp_modern_probe: probe the modern virtio pci device, note that the
210  * caller is required to enable PCI device before calling this function.
211  * @mdev: the modern virtio-pci device
212  *
213  * Return 0 on succeed otherwise fail
214  */
vp_modern_probe(struct virtio_pci_modern_device * mdev)215 int vp_modern_probe(struct virtio_pci_modern_device *mdev)
216 {
217 	struct pci_dev *pci_dev = mdev->pci_dev;
218 	int err, common, isr, notify, device;
219 	u32 notify_length;
220 	u32 notify_offset;
221 	int devid;
222 
223 	check_offsets();
224 
225 	if (mdev->device_id_check) {
226 		devid = mdev->device_id_check(pci_dev);
227 		if (devid < 0)
228 			return devid;
229 		mdev->id.device = devid;
230 	} else {
231 		/* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
232 		if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
233 			return -ENODEV;
234 
235 		if (pci_dev->device < 0x1040) {
236 			/* Transitional devices: use the PCI subsystem device id as
237 			 * virtio device id, same as legacy driver always did.
238 			 */
239 			mdev->id.device = pci_dev->subsystem_device;
240 		} else {
241 			/* Modern devices: simply use PCI device id, but start from 0x1040. */
242 			mdev->id.device = pci_dev->device - 0x1040;
243 		}
244 	}
245 	mdev->id.vendor = pci_dev->subsystem_vendor;
246 
247 	/* check for a common config: if not, use legacy mode (bar 0). */
248 	common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
249 					    IORESOURCE_IO | IORESOURCE_MEM,
250 					    &mdev->modern_bars);
251 	if (!common) {
252 		dev_info(&pci_dev->dev,
253 			 "virtio_pci: leaving for legacy driver\n");
254 		return -ENODEV;
255 	}
256 
257 	/* If common is there, these should be too... */
258 	isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
259 					 IORESOURCE_IO | IORESOURCE_MEM,
260 					 &mdev->modern_bars);
261 	notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
262 					    IORESOURCE_IO | IORESOURCE_MEM,
263 					    &mdev->modern_bars);
264 	if (!isr || !notify) {
265 		dev_err(&pci_dev->dev,
266 			"virtio_pci: missing capabilities %i/%i/%i\n",
267 			common, isr, notify);
268 		return -EINVAL;
269 	}
270 
271 	err = dma_set_mask_and_coherent(&pci_dev->dev,
272 					mdev->dma_mask ? : DMA_BIT_MASK(64));
273 	if (err)
274 		err = dma_set_mask_and_coherent(&pci_dev->dev,
275 						DMA_BIT_MASK(32));
276 	if (err)
277 		dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
278 
279 	/* Device capability is only mandatory for devices that have
280 	 * device-specific configuration.
281 	 */
282 	device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
283 					    IORESOURCE_IO | IORESOURCE_MEM,
284 					    &mdev->modern_bars);
285 
286 	err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
287 					   "virtio-pci-modern");
288 	if (err)
289 		return err;
290 
291 	err = -EINVAL;
292 	mdev->common = vp_modern_map_capability(mdev, common,
293 				      sizeof(struct virtio_pci_common_cfg), 4,
294 				      0, sizeof(struct virtio_pci_modern_common_cfg),
295 				      NULL, NULL);
296 	if (!mdev->common)
297 		goto err_map_common;
298 	mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
299 					     0, 1,
300 					     NULL, NULL);
301 	if (!mdev->isr)
302 		goto err_map_isr;
303 
304 	/* Read notify_off_multiplier from config space. */
305 	pci_read_config_dword(pci_dev,
306 			      notify + offsetof(struct virtio_pci_notify_cap,
307 						notify_off_multiplier),
308 			      &mdev->notify_offset_multiplier);
309 	/* Read notify length and offset from config space. */
310 	pci_read_config_dword(pci_dev,
311 			      notify + offsetof(struct virtio_pci_notify_cap,
312 						cap.length),
313 			      &notify_length);
314 
315 	pci_read_config_dword(pci_dev,
316 			      notify + offsetof(struct virtio_pci_notify_cap,
317 						cap.offset),
318 			      &notify_offset);
319 
320 	/* We don't know how many VQs we'll map, ahead of the time.
321 	 * If notify length is small, map it all now.
322 	 * Otherwise, map each VQ individually later.
323 	 */
324 	if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
325 		mdev->notify_base = vp_modern_map_capability(mdev, notify,
326 							     2, 2,
327 							     0, notify_length,
328 							     &mdev->notify_len,
329 							     &mdev->notify_pa);
330 		if (!mdev->notify_base)
331 			goto err_map_notify;
332 	} else {
333 		mdev->notify_map_cap = notify;
334 	}
335 
336 	/* Again, we don't know how much we should map, but PAGE_SIZE
337 	 * is more than enough for all existing devices.
338 	 */
339 	if (device) {
340 		mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
341 							0, PAGE_SIZE,
342 							&mdev->device_len,
343 							NULL);
344 		if (!mdev->device)
345 			goto err_map_device;
346 	}
347 
348 	return 0;
349 
350 err_map_device:
351 	if (mdev->notify_base)
352 		pci_iounmap(pci_dev, mdev->notify_base);
353 err_map_notify:
354 	pci_iounmap(pci_dev, mdev->isr);
355 err_map_isr:
356 	pci_iounmap(pci_dev, mdev->common);
357 err_map_common:
358 	pci_release_selected_regions(pci_dev, mdev->modern_bars);
359 	return err;
360 }
361 EXPORT_SYMBOL_GPL(vp_modern_probe);
362 
363 /*
364  * vp_modern_remove: remove and cleanup the modern virtio pci device
365  * @mdev: the modern virtio-pci device
366  */
vp_modern_remove(struct virtio_pci_modern_device * mdev)367 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
368 {
369 	struct pci_dev *pci_dev = mdev->pci_dev;
370 
371 	if (mdev->device)
372 		pci_iounmap(pci_dev, mdev->device);
373 	if (mdev->notify_base)
374 		pci_iounmap(pci_dev, mdev->notify_base);
375 	pci_iounmap(pci_dev, mdev->isr);
376 	pci_iounmap(pci_dev, mdev->common);
377 	pci_release_selected_regions(pci_dev, mdev->modern_bars);
378 }
379 EXPORT_SYMBOL_GPL(vp_modern_remove);
380 
381 /*
382  * vp_modern_get_features - get features from device
383  * @mdev: the modern virtio-pci device
384  *
385  * Returns the features read from the device
386  */
vp_modern_get_features(struct virtio_pci_modern_device * mdev)387 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
388 {
389 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
390 
391 	u64 features;
392 
393 	vp_iowrite32(0, &cfg->device_feature_select);
394 	features = vp_ioread32(&cfg->device_feature);
395 	vp_iowrite32(1, &cfg->device_feature_select);
396 	features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
397 
398 	return features;
399 }
400 EXPORT_SYMBOL_GPL(vp_modern_get_features);
401 
402 /*
403  * vp_modern_get_driver_features - get driver features from device
404  * @mdev: the modern virtio-pci device
405  *
406  * Returns the driver features read from the device
407  */
vp_modern_get_driver_features(struct virtio_pci_modern_device * mdev)408 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
409 {
410 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
411 
412 	u64 features;
413 
414 	vp_iowrite32(0, &cfg->guest_feature_select);
415 	features = vp_ioread32(&cfg->guest_feature);
416 	vp_iowrite32(1, &cfg->guest_feature_select);
417 	features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
418 
419 	return features;
420 }
421 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
422 
423 /*
424  * vp_modern_set_features - set features to device
425  * @mdev: the modern virtio-pci device
426  * @features: the features set to device
427  */
vp_modern_set_features(struct virtio_pci_modern_device * mdev,u64 features)428 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
429 			    u64 features)
430 {
431 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
432 
433 	vp_iowrite32(0, &cfg->guest_feature_select);
434 	vp_iowrite32((u32)features, &cfg->guest_feature);
435 	vp_iowrite32(1, &cfg->guest_feature_select);
436 	vp_iowrite32(features >> 32, &cfg->guest_feature);
437 }
438 EXPORT_SYMBOL_GPL(vp_modern_set_features);
439 
440 /*
441  * vp_modern_generation - get the device genreation
442  * @mdev: the modern virtio-pci device
443  *
444  * Returns the genreation read from device
445  */
vp_modern_generation(struct virtio_pci_modern_device * mdev)446 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
447 {
448 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
449 
450 	return vp_ioread8(&cfg->config_generation);
451 }
452 EXPORT_SYMBOL_GPL(vp_modern_generation);
453 
454 /*
455  * vp_modern_get_status - get the device status
456  * @mdev: the modern virtio-pci device
457  *
458  * Returns the status read from device
459  */
vp_modern_get_status(struct virtio_pci_modern_device * mdev)460 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
461 {
462 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
463 
464 	return vp_ioread8(&cfg->device_status);
465 }
466 EXPORT_SYMBOL_GPL(vp_modern_get_status);
467 
468 /*
469  * vp_modern_set_status - set status to device
470  * @mdev: the modern virtio-pci device
471  * @status: the status set to device
472  */
vp_modern_set_status(struct virtio_pci_modern_device * mdev,u8 status)473 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
474 				 u8 status)
475 {
476 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
477 
478 	/*
479 	 * Per memory-barriers.txt, wmb() is not needed to guarantee
480 	 * that the cache coherent memory writes have completed
481 	 * before writing to the MMIO region.
482 	 */
483 	vp_iowrite8(status, &cfg->device_status);
484 }
485 EXPORT_SYMBOL_GPL(vp_modern_set_status);
486 
487 /*
488  * vp_modern_get_queue_reset - get the queue reset status
489  * @mdev: the modern virtio-pci device
490  * @index: queue index
491  */
vp_modern_get_queue_reset(struct virtio_pci_modern_device * mdev,u16 index)492 int vp_modern_get_queue_reset(struct virtio_pci_modern_device *mdev, u16 index)
493 {
494 	struct virtio_pci_modern_common_cfg __iomem *cfg;
495 
496 	cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
497 
498 	vp_iowrite16(index, &cfg->cfg.queue_select);
499 	return vp_ioread16(&cfg->queue_reset);
500 }
501 EXPORT_SYMBOL_GPL(vp_modern_get_queue_reset);
502 
503 /*
504  * vp_modern_set_queue_reset - reset the queue
505  * @mdev: the modern virtio-pci device
506  * @index: queue index
507  */
vp_modern_set_queue_reset(struct virtio_pci_modern_device * mdev,u16 index)508 void vp_modern_set_queue_reset(struct virtio_pci_modern_device *mdev, u16 index)
509 {
510 	struct virtio_pci_modern_common_cfg __iomem *cfg;
511 
512 	cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
513 
514 	vp_iowrite16(index, &cfg->cfg.queue_select);
515 	vp_iowrite16(1, &cfg->queue_reset);
516 
517 	while (vp_ioread16(&cfg->queue_reset))
518 		msleep(1);
519 
520 	while (vp_ioread16(&cfg->cfg.queue_enable))
521 		msleep(1);
522 }
523 EXPORT_SYMBOL_GPL(vp_modern_set_queue_reset);
524 
525 /*
526  * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
527  * @mdev: the modern virtio-pci device
528  * @index: queue index
529  * @vector: the config vector
530  *
531  * Returns the config vector read from the device
532  */
vp_modern_queue_vector(struct virtio_pci_modern_device * mdev,u16 index,u16 vector)533 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
534 			   u16 index, u16 vector)
535 {
536 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
537 
538 	vp_iowrite16(index, &cfg->queue_select);
539 	vp_iowrite16(vector, &cfg->queue_msix_vector);
540 	/* Flush the write out to device */
541 	return vp_ioread16(&cfg->queue_msix_vector);
542 }
543 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
544 
545 /*
546  * vp_modern_config_vector - set the vector for config interrupt
547  * @mdev: the modern virtio-pci device
548  * @vector: the config vector
549  *
550  * Returns the config vector read from the device
551  */
vp_modern_config_vector(struct virtio_pci_modern_device * mdev,u16 vector)552 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
553 			    u16 vector)
554 {
555 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
556 
557 	/* Setup the vector used for configuration events */
558 	vp_iowrite16(vector, &cfg->msix_config);
559 	/* Verify we had enough resources to assign the vector */
560 	/* Will also flush the write out to device */
561 	return vp_ioread16(&cfg->msix_config);
562 }
563 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
564 
565 /*
566  * vp_modern_queue_address - set the virtqueue address
567  * @mdev: the modern virtio-pci device
568  * @index: the queue index
569  * @desc_addr: address of the descriptor area
570  * @driver_addr: address of the driver area
571  * @device_addr: address of the device area
572  */
vp_modern_queue_address(struct virtio_pci_modern_device * mdev,u16 index,u64 desc_addr,u64 driver_addr,u64 device_addr)573 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
574 			     u16 index, u64 desc_addr, u64 driver_addr,
575 			     u64 device_addr)
576 {
577 	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
578 
579 	vp_iowrite16(index, &cfg->queue_select);
580 
581 	vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
582 			     &cfg->queue_desc_hi);
583 	vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
584 			     &cfg->queue_avail_hi);
585 	vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
586 			     &cfg->queue_used_hi);
587 }
588 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
589 
590 /*
591  * vp_modern_set_queue_enable - enable a virtqueue
592  * @mdev: the modern virtio-pci device
593  * @index: the queue index
594  * @enable: whether the virtqueue is enable or not
595  */
vp_modern_set_queue_enable(struct virtio_pci_modern_device * mdev,u16 index,bool enable)596 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
597 				u16 index, bool enable)
598 {
599 	vp_iowrite16(index, &mdev->common->queue_select);
600 	vp_iowrite16(enable, &mdev->common->queue_enable);
601 }
602 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
603 
604 /*
605  * vp_modern_get_queue_enable - enable a virtqueue
606  * @mdev: the modern virtio-pci device
607  * @index: the queue index
608  *
609  * Returns whether a virtqueue is enabled or not
610  */
vp_modern_get_queue_enable(struct virtio_pci_modern_device * mdev,u16 index)611 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
612 				u16 index)
613 {
614 	vp_iowrite16(index, &mdev->common->queue_select);
615 
616 	return vp_ioread16(&mdev->common->queue_enable);
617 }
618 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
619 
620 /*
621  * vp_modern_set_queue_size - set size for a virtqueue
622  * @mdev: the modern virtio-pci device
623  * @index: the queue index
624  * @size: the size of the virtqueue
625  */
vp_modern_set_queue_size(struct virtio_pci_modern_device * mdev,u16 index,u16 size)626 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
627 			      u16 index, u16 size)
628 {
629 	vp_iowrite16(index, &mdev->common->queue_select);
630 	vp_iowrite16(size, &mdev->common->queue_size);
631 
632 }
633 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
634 
635 /*
636  * vp_modern_get_queue_size - get size for a virtqueue
637  * @mdev: the modern virtio-pci device
638  * @index: the queue index
639  *
640  * Returns the size of the virtqueue
641  */
vp_modern_get_queue_size(struct virtio_pci_modern_device * mdev,u16 index)642 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
643 			     u16 index)
644 {
645 	vp_iowrite16(index, &mdev->common->queue_select);
646 
647 	return vp_ioread16(&mdev->common->queue_size);
648 
649 }
650 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
651 
652 /*
653  * vp_modern_get_num_queues - get the number of virtqueues
654  * @mdev: the modern virtio-pci device
655  *
656  * Returns the number of virtqueues
657  */
vp_modern_get_num_queues(struct virtio_pci_modern_device * mdev)658 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
659 {
660 	return vp_ioread16(&mdev->common->num_queues);
661 }
662 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
663 
664 /*
665  * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
666  * @mdev: the modern virtio-pci device
667  * @index: the queue index
668  *
669  * Returns the notification offset for a virtqueue
670  */
vp_modern_get_queue_notify_off(struct virtio_pci_modern_device * mdev,u16 index)671 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
672 					  u16 index)
673 {
674 	vp_iowrite16(index, &mdev->common->queue_select);
675 
676 	return vp_ioread16(&mdev->common->queue_notify_off);
677 }
678 
679 /*
680  * vp_modern_map_vq_notify - map notification area for a
681  * specific virtqueue
682  * @mdev: the modern virtio-pci device
683  * @index: the queue index
684  * @pa: the pointer to the physical address of the nofity area
685  *
686  * Returns the address of the notification area
687  */
vp_modern_map_vq_notify(struct virtio_pci_modern_device * mdev,u16 index,resource_size_t * pa)688 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
689 				      u16 index, resource_size_t *pa)
690 {
691 	u16 off = vp_modern_get_queue_notify_off(mdev, index);
692 
693 	if (mdev->notify_base) {
694 		/* offset should not wrap */
695 		if ((u64)off * mdev->notify_offset_multiplier + 2
696 			> mdev->notify_len) {
697 			dev_warn(&mdev->pci_dev->dev,
698 				 "bad notification offset %u (x %u) "
699 				 "for queue %u > %zd",
700 				 off, mdev->notify_offset_multiplier,
701 				 index, mdev->notify_len);
702 			return NULL;
703 		}
704 		if (pa)
705 			*pa = mdev->notify_pa +
706 			      off * mdev->notify_offset_multiplier;
707 		return mdev->notify_base + off * mdev->notify_offset_multiplier;
708 	} else {
709 		return vp_modern_map_capability(mdev,
710 				       mdev->notify_map_cap, 2, 2,
711 				       off * mdev->notify_offset_multiplier, 2,
712 				       NULL, pa);
713 	}
714 }
715 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
716 
717 MODULE_VERSION("0.1");
718 MODULE_DESCRIPTION("Modern Virtio PCI Device");
719 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
720 MODULE_LICENSE("GPL");
721