virtio_pci_modern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * Virtio PCI driver - modern (virtio 1.0) device support
  3. *
  4. * This module allows virtio devices to be used over a virtual PCI device.
  5. * This can be used with QEMU based VMMs like KVM or Xen.
  6. *
  7. * Copyright IBM Corp. 2007
  8. * Copyright Red Hat, Inc. 2014
  9. *
  10. * Authors:
  11. * Anthony Liguori <aliguori@us.ibm.com>
  12. * Rusty Russell <rusty@rustcorp.com.au>
  13. * Michael S. Tsirkin <mst@redhat.com>
  14. *
  15. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  16. * See the COPYING file in the top-level directory.
  17. *
  18. */
  19. #include <linux/delay.h>
  20. #define VIRTIO_PCI_NO_LEGACY
  21. #include "virtio_pci_common.h"
  22. /*
  23. * Type-safe wrappers for io accesses.
  24. * Use these to enforce at compile time the following spec requirement:
  25. *
  26. * The driver MUST access each field using the “natural” access
  27. * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
  28. * for 16-bit fields and 8-bit accesses for 8-bit fields.
  29. */
  30. static inline u8 vp_ioread8(u8 __iomem *addr)
  31. {
  32. return ioread8(addr);
  33. }
  34. static inline u16 vp_ioread16 (__le16 __iomem *addr)
  35. {
  36. return ioread16(addr);
  37. }
  38. static inline u32 vp_ioread32(__le32 __iomem *addr)
  39. {
  40. return ioread32(addr);
  41. }
  42. static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
  43. {
  44. iowrite8(value, addr);
  45. }
  46. static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
  47. {
  48. iowrite16(value, addr);
  49. }
  50. static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
  51. {
  52. iowrite32(value, addr);
  53. }
  54. static void vp_iowrite64_twopart(u64 val,
  55. __le32 __iomem *lo, __le32 __iomem *hi)
  56. {
  57. vp_iowrite32((u32)val, lo);
  58. vp_iowrite32(val >> 32, hi);
  59. }
  60. static void __iomem *map_capability(struct pci_dev *dev, int off,
  61. size_t minlen,
  62. u32 align,
  63. u32 start, u32 size,
  64. size_t *len)
  65. {
  66. u8 bar;
  67. u32 offset, length;
  68. void __iomem *p;
  69. pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
  70. bar),
  71. &bar);
  72. pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
  73. &offset);
  74. pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
  75. &length);
  76. if (length <= start) {
  77. dev_err(&dev->dev,
  78. "virtio_pci: bad capability len %u (>%u expected)\n",
  79. length, start);
  80. return NULL;
  81. }
  82. if (length - start < minlen) {
  83. dev_err(&dev->dev,
  84. "virtio_pci: bad capability len %u (>=%zu expected)\n",
  85. length, minlen);
  86. return NULL;
  87. }
  88. length -= start;
  89. if (start + offset < offset) {
  90. dev_err(&dev->dev,
  91. "virtio_pci: map wrap-around %u+%u\n",
  92. start, offset);
  93. return NULL;
  94. }
  95. offset += start;
  96. if (offset & (align - 1)) {
  97. dev_err(&dev->dev,
  98. "virtio_pci: offset %u not aligned to %u\n",
  99. offset, align);
  100. return NULL;
  101. }
  102. if (length > size)
  103. length = size;
  104. if (len)
  105. *len = length;
  106. if (minlen + offset < minlen ||
  107. minlen + offset > pci_resource_len(dev, bar)) {
  108. dev_err(&dev->dev,
  109. "virtio_pci: map virtio %zu@%u "
  110. "out of range on bar %i length %lu\n",
  111. minlen, offset,
  112. bar, (unsigned long)pci_resource_len(dev, bar));
  113. return NULL;
  114. }
  115. p = pci_iomap_range(dev, bar, offset, length);
  116. if (!p)
  117. dev_err(&dev->dev,
  118. "virtio_pci: unable to map virtio %u@%u on bar %i\n",
  119. length, offset, bar);
  120. return p;
  121. }
  122. /* virtio config->get_features() implementation */
  123. static u64 vp_get_features(struct virtio_device *vdev)
  124. {
  125. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  126. u64 features;
  127. vp_iowrite32(0, &vp_dev->common->device_feature_select);
  128. features = vp_ioread32(&vp_dev->common->device_feature);
  129. vp_iowrite32(1, &vp_dev->common->device_feature_select);
  130. features |= ((u64)vp_ioread32(&vp_dev->common->device_feature) << 32);
  131. return features;
  132. }
  133. /* virtio config->finalize_features() implementation */
  134. static int vp_finalize_features(struct virtio_device *vdev)
  135. {
  136. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  137. /* Give virtio_ring a chance to accept features. */
  138. vring_transport_features(vdev);
  139. if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  140. dev_err(&vdev->dev, "virtio: device uses modern interface "
  141. "but does not have VIRTIO_F_VERSION_1\n");
  142. return -EINVAL;
  143. }
  144. vp_iowrite32(0, &vp_dev->common->guest_feature_select);
  145. vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
  146. vp_iowrite32(1, &vp_dev->common->guest_feature_select);
  147. vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
  148. return 0;
  149. }
  150. /* virtio config->get() implementation */
  151. static void vp_get(struct virtio_device *vdev, unsigned offset,
  152. void *buf, unsigned len)
  153. {
  154. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  155. u8 b;
  156. __le16 w;
  157. __le32 l;
  158. BUG_ON(offset + len > vp_dev->device_len);
  159. switch (len) {
  160. case 1:
  161. b = ioread8(vp_dev->device + offset);
  162. memcpy(buf, &b, sizeof b);
  163. break;
  164. case 2:
  165. w = cpu_to_le16(ioread16(vp_dev->device + offset));
  166. memcpy(buf, &w, sizeof w);
  167. break;
  168. case 4:
  169. l = cpu_to_le32(ioread32(vp_dev->device + offset));
  170. memcpy(buf, &l, sizeof l);
  171. break;
  172. case 8:
  173. l = cpu_to_le32(ioread32(vp_dev->device + offset));
  174. memcpy(buf, &l, sizeof l);
  175. l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
  176. memcpy(buf + sizeof l, &l, sizeof l);
  177. break;
  178. default:
  179. BUG();
  180. }
  181. }
  182. /* the config->set() implementation. it's symmetric to the config->get()
  183. * implementation */
  184. static void vp_set(struct virtio_device *vdev, unsigned offset,
  185. const void *buf, unsigned len)
  186. {
  187. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  188. u8 b;
  189. __le16 w;
  190. __le32 l;
  191. BUG_ON(offset + len > vp_dev->device_len);
  192. switch (len) {
  193. case 1:
  194. memcpy(&b, buf, sizeof b);
  195. iowrite8(b, vp_dev->device + offset);
  196. break;
  197. case 2:
  198. memcpy(&w, buf, sizeof w);
  199. iowrite16(le16_to_cpu(w), vp_dev->device + offset);
  200. break;
  201. case 4:
  202. memcpy(&l, buf, sizeof l);
  203. iowrite32(le32_to_cpu(l), vp_dev->device + offset);
  204. break;
  205. case 8:
  206. memcpy(&l, buf, sizeof l);
  207. iowrite32(le32_to_cpu(l), vp_dev->device + offset);
  208. memcpy(&l, buf + sizeof l, sizeof l);
  209. iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
  210. break;
  211. default:
  212. BUG();
  213. }
  214. }
  215. static u32 vp_generation(struct virtio_device *vdev)
  216. {
  217. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  218. return vp_ioread8(&vp_dev->common->config_generation);
  219. }
  220. /* config->{get,set}_status() implementations */
  221. static u8 vp_get_status(struct virtio_device *vdev)
  222. {
  223. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  224. return vp_ioread8(&vp_dev->common->device_status);
  225. }
  226. static void vp_set_status(struct virtio_device *vdev, u8 status)
  227. {
  228. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  229. /* We should never be setting status to 0. */
  230. BUG_ON(status == 0);
  231. vp_iowrite8(status, &vp_dev->common->device_status);
  232. }
  233. static void vp_reset(struct virtio_device *vdev)
  234. {
  235. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  236. /* 0 status means a reset. */
  237. vp_iowrite8(0, &vp_dev->common->device_status);
  238. /* After writing 0 to device_status, the driver MUST wait for a read of
  239. * device_status to return 0 before reinitializing the device.
  240. * This will flush out the status write, and flush in device writes,
  241. * including MSI-X interrupts, if any.
  242. */
  243. while (vp_ioread8(&vp_dev->common->device_status))
  244. msleep(1);
  245. /* Flush pending VQ/configuration callbacks. */
  246. vp_synchronize_vectors(vdev);
  247. }
  248. static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
  249. {
  250. /* Setup the vector used for configuration events */
  251. vp_iowrite16(vector, &vp_dev->common->msix_config);
  252. /* Verify we had enough resources to assign the vector */
  253. /* Will also flush the write out to device */
  254. return vp_ioread16(&vp_dev->common->msix_config);
  255. }
  256. static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
  257. struct virtio_pci_vq_info *info,
  258. unsigned index,
  259. void (*callback)(struct virtqueue *vq),
  260. const char *name,
  261. bool ctx,
  262. u16 msix_vec)
  263. {
  264. struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
  265. struct virtqueue *vq;
  266. u16 num, off;
  267. int err;
  268. if (index >= vp_ioread16(&cfg->num_queues))
  269. return ERR_PTR(-ENOENT);
  270. /* Select the queue we're interested in */
  271. vp_iowrite16(index, &cfg->queue_select);
  272. /* Check if queue is either not available or already active. */
  273. num = vp_ioread16(&cfg->queue_size);
  274. if (!num || vp_ioread16(&cfg->queue_enable))
  275. return ERR_PTR(-ENOENT);
  276. if (num & (num - 1)) {
  277. dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
  278. return ERR_PTR(-EINVAL);
  279. }
  280. /* get offset of notification word for this vq */
  281. off = vp_ioread16(&cfg->queue_notify_off);
  282. info->msix_vector = msix_vec;
  283. /* create the vring */
  284. vq = vring_create_virtqueue(index, num,
  285. SMP_CACHE_BYTES, &vp_dev->vdev,
  286. true, true, ctx,
  287. vp_notify, callback, name);
  288. if (!vq)
  289. return ERR_PTR(-ENOMEM);
  290. /* activate the queue */
  291. vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
  292. vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
  293. &cfg->queue_desc_lo, &cfg->queue_desc_hi);
  294. vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
  295. &cfg->queue_avail_lo, &cfg->queue_avail_hi);
  296. vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
  297. &cfg->queue_used_lo, &cfg->queue_used_hi);
  298. if (vp_dev->notify_base) {
  299. /* offset should not wrap */
  300. if ((u64)off * vp_dev->notify_offset_multiplier + 2
  301. > vp_dev->notify_len) {
  302. dev_warn(&vp_dev->pci_dev->dev,
  303. "bad notification offset %u (x %u) "
  304. "for queue %u > %zd",
  305. off, vp_dev->notify_offset_multiplier,
  306. index, vp_dev->notify_len);
  307. err = -EINVAL;
  308. goto err_map_notify;
  309. }
  310. vq->priv = (void __force *)vp_dev->notify_base +
  311. off * vp_dev->notify_offset_multiplier;
  312. } else {
  313. vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
  314. vp_dev->notify_map_cap, 2, 2,
  315. off * vp_dev->notify_offset_multiplier, 2,
  316. NULL);
  317. }
  318. if (!vq->priv) {
  319. err = -ENOMEM;
  320. goto err_map_notify;
  321. }
  322. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  323. vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
  324. msix_vec = vp_ioread16(&cfg->queue_msix_vector);
  325. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  326. err = -EBUSY;
  327. goto err_assign_vector;
  328. }
  329. }
  330. return vq;
  331. err_assign_vector:
  332. if (!vp_dev->notify_base)
  333. pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
  334. err_map_notify:
  335. vring_del_virtqueue(vq);
  336. return ERR_PTR(err);
  337. }
  338. static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  339. struct virtqueue *vqs[],
  340. vq_callback_t *callbacks[],
  341. const char * const names[], const bool *ctx,
  342. struct irq_affinity *desc)
  343. {
  344. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  345. struct virtqueue *vq;
  346. int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
  347. if (rc)
  348. return rc;
  349. /* Select and activate all queues. Has to be done last: once we do
  350. * this, there's no way to go back except reset.
  351. */
  352. list_for_each_entry(vq, &vdev->vqs, list) {
  353. vp_iowrite16(vq->index, &vp_dev->common->queue_select);
  354. vp_iowrite16(1, &vp_dev->common->queue_enable);
  355. }
  356. return 0;
  357. }
  358. static void del_vq(struct virtio_pci_vq_info *info)
  359. {
  360. struct virtqueue *vq = info->vq;
  361. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  362. vp_iowrite16(vq->index, &vp_dev->common->queue_select);
  363. if (vp_dev->msix_enabled) {
  364. vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
  365. &vp_dev->common->queue_msix_vector);
  366. /* Flush the write out to device */
  367. vp_ioread16(&vp_dev->common->queue_msix_vector);
  368. }
  369. if (!vp_dev->notify_base)
  370. pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
  371. vring_del_virtqueue(vq);
  372. }
  373. static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
  374. .get = NULL,
  375. .set = NULL,
  376. .generation = vp_generation,
  377. .get_status = vp_get_status,
  378. .set_status = vp_set_status,
  379. .reset = vp_reset,
  380. .find_vqs = vp_modern_find_vqs,
  381. .del_vqs = vp_del_vqs,
  382. .get_features = vp_get_features,
  383. .finalize_features = vp_finalize_features,
  384. .bus_name = vp_bus_name,
  385. .set_vq_affinity = vp_set_vq_affinity,
  386. .get_vq_affinity = vp_get_vq_affinity,
  387. };
  388. static const struct virtio_config_ops virtio_pci_config_ops = {
  389. .get = vp_get,
  390. .set = vp_set,
  391. .generation = vp_generation,
  392. .get_status = vp_get_status,
  393. .set_status = vp_set_status,
  394. .reset = vp_reset,
  395. .find_vqs = vp_modern_find_vqs,
  396. .del_vqs = vp_del_vqs,
  397. .get_features = vp_get_features,
  398. .finalize_features = vp_finalize_features,
  399. .bus_name = vp_bus_name,
  400. .set_vq_affinity = vp_set_vq_affinity,
  401. .get_vq_affinity = vp_get_vq_affinity,
  402. };
  403. /**
  404. * virtio_pci_find_capability - walk capabilities to find device info.
  405. * @dev: the pci device
  406. * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
  407. * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
  408. *
  409. * Returns offset of the capability, or 0.
  410. */
  411. static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
  412. u32 ioresource_types, int *bars)
  413. {
  414. int pos;
  415. for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
  416. pos > 0;
  417. pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
  418. u8 type, bar;
  419. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  420. cfg_type),
  421. &type);
  422. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  423. bar),
  424. &bar);
  425. /* Ignore structures with reserved BAR values */
  426. if (bar > 0x5)
  427. continue;
  428. if (type == cfg_type) {
  429. if (pci_resource_len(dev, bar) &&
  430. pci_resource_flags(dev, bar) & ioresource_types) {
  431. *bars |= (1 << bar);
  432. return pos;
  433. }
  434. }
  435. }
  436. return 0;
  437. }
  438. /* This is part of the ABI. Don't screw with it. */
  439. static inline void check_offsets(void)
  440. {
  441. /* Note: disk space was harmed in compilation of this function. */
  442. BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
  443. offsetof(struct virtio_pci_cap, cap_vndr));
  444. BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
  445. offsetof(struct virtio_pci_cap, cap_next));
  446. BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
  447. offsetof(struct virtio_pci_cap, cap_len));
  448. BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
  449. offsetof(struct virtio_pci_cap, cfg_type));
  450. BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
  451. offsetof(struct virtio_pci_cap, bar));
  452. BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
  453. offsetof(struct virtio_pci_cap, offset));
  454. BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
  455. offsetof(struct virtio_pci_cap, length));
  456. BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
  457. offsetof(struct virtio_pci_notify_cap,
  458. notify_off_multiplier));
  459. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
  460. offsetof(struct virtio_pci_common_cfg,
  461. device_feature_select));
  462. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
  463. offsetof(struct virtio_pci_common_cfg, device_feature));
  464. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
  465. offsetof(struct virtio_pci_common_cfg,
  466. guest_feature_select));
  467. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
  468. offsetof(struct virtio_pci_common_cfg, guest_feature));
  469. BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
  470. offsetof(struct virtio_pci_common_cfg, msix_config));
  471. BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
  472. offsetof(struct virtio_pci_common_cfg, num_queues));
  473. BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
  474. offsetof(struct virtio_pci_common_cfg, device_status));
  475. BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
  476. offsetof(struct virtio_pci_common_cfg, config_generation));
  477. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
  478. offsetof(struct virtio_pci_common_cfg, queue_select));
  479. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
  480. offsetof(struct virtio_pci_common_cfg, queue_size));
  481. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
  482. offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
  483. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
  484. offsetof(struct virtio_pci_common_cfg, queue_enable));
  485. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
  486. offsetof(struct virtio_pci_common_cfg, queue_notify_off));
  487. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
  488. offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
  489. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
  490. offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
  491. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
  492. offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
  493. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
  494. offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
  495. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
  496. offsetof(struct virtio_pci_common_cfg, queue_used_lo));
  497. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
  498. offsetof(struct virtio_pci_common_cfg, queue_used_hi));
  499. }
  500. /* the PCI probing function */
  501. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  502. {
  503. struct pci_dev *pci_dev = vp_dev->pci_dev;
  504. int err, common, isr, notify, device;
  505. u32 notify_length;
  506. u32 notify_offset;
  507. check_offsets();
  508. /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
  509. if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
  510. return -ENODEV;
  511. if (pci_dev->device < 0x1040) {
  512. /* Transitional devices: use the PCI subsystem device id as
  513. * virtio device id, same as legacy driver always did.
  514. */
  515. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  516. } else {
  517. /* Modern devices: simply use PCI device id, but start from 0x1040. */
  518. vp_dev->vdev.id.device = pci_dev->device - 0x1040;
  519. }
  520. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  521. /* check for a common config: if not, use legacy mode (bar 0). */
  522. common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
  523. IORESOURCE_IO | IORESOURCE_MEM,
  524. &vp_dev->modern_bars);
  525. if (!common) {
  526. dev_info(&pci_dev->dev,
  527. "virtio_pci: leaving for legacy driver\n");
  528. return -ENODEV;
  529. }
  530. /* If common is there, these should be too... */
  531. isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
  532. IORESOURCE_IO | IORESOURCE_MEM,
  533. &vp_dev->modern_bars);
  534. notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
  535. IORESOURCE_IO | IORESOURCE_MEM,
  536. &vp_dev->modern_bars);
  537. if (!isr || !notify) {
  538. dev_err(&pci_dev->dev,
  539. "virtio_pci: missing capabilities %i/%i/%i\n",
  540. common, isr, notify);
  541. return -EINVAL;
  542. }
  543. err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
  544. if (err)
  545. err = dma_set_mask_and_coherent(&pci_dev->dev,
  546. DMA_BIT_MASK(32));
  547. if (err)
  548. dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  549. /* Device capability is only mandatory for devices that have
  550. * device-specific configuration.
  551. */
  552. device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
  553. IORESOURCE_IO | IORESOURCE_MEM,
  554. &vp_dev->modern_bars);
  555. err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
  556. "virtio-pci-modern");
  557. if (err)
  558. return err;
  559. err = -EINVAL;
  560. vp_dev->common = map_capability(pci_dev, common,
  561. sizeof(struct virtio_pci_common_cfg), 4,
  562. 0, sizeof(struct virtio_pci_common_cfg),
  563. NULL);
  564. if (!vp_dev->common)
  565. goto err_map_common;
  566. vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
  567. 0, 1,
  568. NULL);
  569. if (!vp_dev->isr)
  570. goto err_map_isr;
  571. /* Read notify_off_multiplier from config space. */
  572. pci_read_config_dword(pci_dev,
  573. notify + offsetof(struct virtio_pci_notify_cap,
  574. notify_off_multiplier),
  575. &vp_dev->notify_offset_multiplier);
  576. /* Read notify length and offset from config space. */
  577. pci_read_config_dword(pci_dev,
  578. notify + offsetof(struct virtio_pci_notify_cap,
  579. cap.length),
  580. &notify_length);
  581. pci_read_config_dword(pci_dev,
  582. notify + offsetof(struct virtio_pci_notify_cap,
  583. cap.offset),
  584. &notify_offset);
  585. /* We don't know how many VQs we'll map, ahead of the time.
  586. * If notify length is small, map it all now.
  587. * Otherwise, map each VQ individually later.
  588. */
  589. if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
  590. vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
  591. 0, notify_length,
  592. &vp_dev->notify_len);
  593. if (!vp_dev->notify_base)
  594. goto err_map_notify;
  595. } else {
  596. vp_dev->notify_map_cap = notify;
  597. }
  598. /* Again, we don't know how much we should map, but PAGE_SIZE
  599. * is more than enough for all existing devices.
  600. */
  601. if (device) {
  602. vp_dev->device = map_capability(pci_dev, device, 0, 4,
  603. 0, PAGE_SIZE,
  604. &vp_dev->device_len);
  605. if (!vp_dev->device)
  606. goto err_map_device;
  607. vp_dev->vdev.config = &virtio_pci_config_ops;
  608. } else {
  609. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  610. }
  611. vp_dev->config_vector = vp_config_vector;
  612. vp_dev->setup_vq = setup_vq;
  613. vp_dev->del_vq = del_vq;
  614. return 0;
  615. err_map_device:
  616. if (vp_dev->notify_base)
  617. pci_iounmap(pci_dev, vp_dev->notify_base);
  618. err_map_notify:
  619. pci_iounmap(pci_dev, vp_dev->isr);
  620. err_map_isr:
  621. pci_iounmap(pci_dev, vp_dev->common);
  622. err_map_common:
  623. return err;
  624. }
  625. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  626. {
  627. struct pci_dev *pci_dev = vp_dev->pci_dev;
  628. if (vp_dev->device)
  629. pci_iounmap(pci_dev, vp_dev->device);
  630. if (vp_dev->notify_base)
  631. pci_iounmap(pci_dev, vp_dev->notify_base);
  632. pci_iounmap(pci_dev, vp_dev->isr);
  633. pci_iounmap(pci_dev, vp_dev->common);
  634. pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
  635. }