virtio_pci_modern_dev.c 22 KB

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