virtio_pci_modern.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. unsigned index,
  258. void (*callback)(struct virtqueue *vq),
  259. const char *name,
  260. u16 msix_vec)
  261. {
  262. struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
  263. struct virtqueue *vq;
  264. u16 num, off;
  265. int err;
  266. if (index >= vp_ioread16(&cfg->num_queues))
  267. return ERR_PTR(-ENOENT);
  268. /* Select the queue we're interested in */
  269. vp_iowrite16(index, &cfg->queue_select);
  270. /* Check if queue is either not available or already active. */
  271. num = vp_ioread16(&cfg->queue_size);
  272. if (!num || vp_ioread16(&cfg->queue_enable))
  273. return ERR_PTR(-ENOENT);
  274. if (num & (num - 1)) {
  275. dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
  276. return ERR_PTR(-EINVAL);
  277. }
  278. /* get offset of notification word for this vq */
  279. off = vp_ioread16(&cfg->queue_notify_off);
  280. /* create the vring */
  281. vq = vring_create_virtqueue(index, num,
  282. SMP_CACHE_BYTES, &vp_dev->vdev,
  283. true, true, vp_notify, callback, name);
  284. if (!vq)
  285. return ERR_PTR(-ENOMEM);
  286. /* activate the queue */
  287. vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
  288. vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
  289. &cfg->queue_desc_lo, &cfg->queue_desc_hi);
  290. vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
  291. &cfg->queue_avail_lo, &cfg->queue_avail_hi);
  292. vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
  293. &cfg->queue_used_lo, &cfg->queue_used_hi);
  294. if (vp_dev->notify_base) {
  295. /* offset should not wrap */
  296. if ((u64)off * vp_dev->notify_offset_multiplier + 2
  297. > vp_dev->notify_len) {
  298. dev_warn(&vp_dev->pci_dev->dev,
  299. "bad notification offset %u (x %u) "
  300. "for queue %u > %zd",
  301. off, vp_dev->notify_offset_multiplier,
  302. index, vp_dev->notify_len);
  303. err = -EINVAL;
  304. goto err_map_notify;
  305. }
  306. vq->priv = (void __force *)vp_dev->notify_base +
  307. off * vp_dev->notify_offset_multiplier;
  308. } else {
  309. vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
  310. vp_dev->notify_map_cap, 2, 2,
  311. off * vp_dev->notify_offset_multiplier, 2,
  312. NULL);
  313. }
  314. if (!vq->priv) {
  315. err = -ENOMEM;
  316. goto err_map_notify;
  317. }
  318. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  319. vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
  320. msix_vec = vp_ioread16(&cfg->queue_msix_vector);
  321. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  322. err = -EBUSY;
  323. goto err_assign_vector;
  324. }
  325. }
  326. return vq;
  327. err_assign_vector:
  328. if (!vp_dev->notify_base)
  329. pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
  330. err_map_notify:
  331. vring_del_virtqueue(vq);
  332. return ERR_PTR(err);
  333. }
  334. static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  335. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  336. const char * const names[], struct irq_affinity *desc)
  337. {
  338. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  339. struct virtqueue *vq;
  340. int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, desc);
  341. if (rc)
  342. return rc;
  343. /* Select and activate all queues. Has to be done last: once we do
  344. * this, there's no way to go back except reset.
  345. */
  346. list_for_each_entry(vq, &vdev->vqs, list) {
  347. vp_iowrite16(vq->index, &vp_dev->common->queue_select);
  348. vp_iowrite16(1, &vp_dev->common->queue_enable);
  349. }
  350. return 0;
  351. }
  352. static void del_vq(struct virtqueue *vq)
  353. {
  354. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  355. vp_iowrite16(vq->index, &vp_dev->common->queue_select);
  356. if (vp_dev->pci_dev->msix_enabled) {
  357. vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
  358. &vp_dev->common->queue_msix_vector);
  359. /* Flush the write out to device */
  360. vp_ioread16(&vp_dev->common->queue_msix_vector);
  361. }
  362. if (!vp_dev->notify_base)
  363. pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
  364. vring_del_virtqueue(vq);
  365. }
  366. static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
  367. .get = NULL,
  368. .set = NULL,
  369. .generation = vp_generation,
  370. .get_status = vp_get_status,
  371. .set_status = vp_set_status,
  372. .reset = vp_reset,
  373. .find_vqs = vp_modern_find_vqs,
  374. .del_vqs = vp_del_vqs,
  375. .get_features = vp_get_features,
  376. .finalize_features = vp_finalize_features,
  377. .bus_name = vp_bus_name,
  378. .set_vq_affinity = vp_set_vq_affinity,
  379. };
  380. static const struct virtio_config_ops virtio_pci_config_ops = {
  381. .get = vp_get,
  382. .set = vp_set,
  383. .generation = vp_generation,
  384. .get_status = vp_get_status,
  385. .set_status = vp_set_status,
  386. .reset = vp_reset,
  387. .find_vqs = vp_modern_find_vqs,
  388. .del_vqs = vp_del_vqs,
  389. .get_features = vp_get_features,
  390. .finalize_features = vp_finalize_features,
  391. .bus_name = vp_bus_name,
  392. .set_vq_affinity = vp_set_vq_affinity,
  393. };
  394. /**
  395. * virtio_pci_find_capability - walk capabilities to find device info.
  396. * @dev: the pci device
  397. * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
  398. * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
  399. *
  400. * Returns offset of the capability, or 0.
  401. */
  402. static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
  403. u32 ioresource_types, int *bars)
  404. {
  405. int pos;
  406. for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
  407. pos > 0;
  408. pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
  409. u8 type, bar;
  410. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  411. cfg_type),
  412. &type);
  413. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  414. bar),
  415. &bar);
  416. /* Ignore structures with reserved BAR values */
  417. if (bar > 0x5)
  418. continue;
  419. if (type == cfg_type) {
  420. if (pci_resource_len(dev, bar) &&
  421. pci_resource_flags(dev, bar) & ioresource_types) {
  422. *bars |= (1 << bar);
  423. return pos;
  424. }
  425. }
  426. }
  427. return 0;
  428. }
  429. /* This is part of the ABI. Don't screw with it. */
  430. static inline void check_offsets(void)
  431. {
  432. /* Note: disk space was harmed in compilation of this function. */
  433. BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
  434. offsetof(struct virtio_pci_cap, cap_vndr));
  435. BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
  436. offsetof(struct virtio_pci_cap, cap_next));
  437. BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
  438. offsetof(struct virtio_pci_cap, cap_len));
  439. BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
  440. offsetof(struct virtio_pci_cap, cfg_type));
  441. BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
  442. offsetof(struct virtio_pci_cap, bar));
  443. BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
  444. offsetof(struct virtio_pci_cap, offset));
  445. BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
  446. offsetof(struct virtio_pci_cap, length));
  447. BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
  448. offsetof(struct virtio_pci_notify_cap,
  449. notify_off_multiplier));
  450. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
  451. offsetof(struct virtio_pci_common_cfg,
  452. device_feature_select));
  453. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
  454. offsetof(struct virtio_pci_common_cfg, device_feature));
  455. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
  456. offsetof(struct virtio_pci_common_cfg,
  457. guest_feature_select));
  458. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
  459. offsetof(struct virtio_pci_common_cfg, guest_feature));
  460. BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
  461. offsetof(struct virtio_pci_common_cfg, msix_config));
  462. BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
  463. offsetof(struct virtio_pci_common_cfg, num_queues));
  464. BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
  465. offsetof(struct virtio_pci_common_cfg, device_status));
  466. BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
  467. offsetof(struct virtio_pci_common_cfg, config_generation));
  468. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
  469. offsetof(struct virtio_pci_common_cfg, queue_select));
  470. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
  471. offsetof(struct virtio_pci_common_cfg, queue_size));
  472. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
  473. offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
  474. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
  475. offsetof(struct virtio_pci_common_cfg, queue_enable));
  476. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
  477. offsetof(struct virtio_pci_common_cfg, queue_notify_off));
  478. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
  479. offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
  480. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
  481. offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
  482. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
  483. offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
  484. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
  485. offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
  486. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
  487. offsetof(struct virtio_pci_common_cfg, queue_used_lo));
  488. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
  489. offsetof(struct virtio_pci_common_cfg, queue_used_hi));
  490. }
  491. /* the PCI probing function */
  492. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  493. {
  494. struct pci_dev *pci_dev = vp_dev->pci_dev;
  495. int err, common, isr, notify, device;
  496. u32 notify_length;
  497. u32 notify_offset;
  498. check_offsets();
  499. /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
  500. if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
  501. return -ENODEV;
  502. if (pci_dev->device < 0x1040) {
  503. /* Transitional devices: use the PCI subsystem device id as
  504. * virtio device id, same as legacy driver always did.
  505. */
  506. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  507. } else {
  508. /* Modern devices: simply use PCI device id, but start from 0x1040. */
  509. vp_dev->vdev.id.device = pci_dev->device - 0x1040;
  510. }
  511. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  512. /* check for a common config: if not, use legacy mode (bar 0). */
  513. common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
  514. IORESOURCE_IO | IORESOURCE_MEM,
  515. &vp_dev->modern_bars);
  516. if (!common) {
  517. dev_info(&pci_dev->dev,
  518. "virtio_pci: leaving for legacy driver\n");
  519. return -ENODEV;
  520. }
  521. /* If common is there, these should be too... */
  522. isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
  523. IORESOURCE_IO | IORESOURCE_MEM,
  524. &vp_dev->modern_bars);
  525. notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
  526. IORESOURCE_IO | IORESOURCE_MEM,
  527. &vp_dev->modern_bars);
  528. if (!isr || !notify) {
  529. dev_err(&pci_dev->dev,
  530. "virtio_pci: missing capabilities %i/%i/%i\n",
  531. common, isr, notify);
  532. return -EINVAL;
  533. }
  534. err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
  535. if (err)
  536. err = dma_set_mask_and_coherent(&pci_dev->dev,
  537. DMA_BIT_MASK(32));
  538. if (err)
  539. dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  540. /* Device capability is only mandatory for devices that have
  541. * device-specific configuration.
  542. */
  543. device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
  544. IORESOURCE_IO | IORESOURCE_MEM,
  545. &vp_dev->modern_bars);
  546. err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
  547. "virtio-pci-modern");
  548. if (err)
  549. return err;
  550. err = -EINVAL;
  551. vp_dev->common = map_capability(pci_dev, common,
  552. sizeof(struct virtio_pci_common_cfg), 4,
  553. 0, sizeof(struct virtio_pci_common_cfg),
  554. NULL);
  555. if (!vp_dev->common)
  556. goto err_map_common;
  557. vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
  558. 0, 1,
  559. NULL);
  560. if (!vp_dev->isr)
  561. goto err_map_isr;
  562. /* Read notify_off_multiplier from config space. */
  563. pci_read_config_dword(pci_dev,
  564. notify + offsetof(struct virtio_pci_notify_cap,
  565. notify_off_multiplier),
  566. &vp_dev->notify_offset_multiplier);
  567. /* Read notify length and offset from config space. */
  568. pci_read_config_dword(pci_dev,
  569. notify + offsetof(struct virtio_pci_notify_cap,
  570. cap.length),
  571. &notify_length);
  572. pci_read_config_dword(pci_dev,
  573. notify + offsetof(struct virtio_pci_notify_cap,
  574. cap.offset),
  575. &notify_offset);
  576. /* We don't know how many VQs we'll map, ahead of the time.
  577. * If notify length is small, map it all now.
  578. * Otherwise, map each VQ individually later.
  579. */
  580. if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
  581. vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
  582. 0, notify_length,
  583. &vp_dev->notify_len);
  584. if (!vp_dev->notify_base)
  585. goto err_map_notify;
  586. } else {
  587. vp_dev->notify_map_cap = notify;
  588. }
  589. /* Again, we don't know how much we should map, but PAGE_SIZE
  590. * is more than enough for all existing devices.
  591. */
  592. if (device) {
  593. vp_dev->device = map_capability(pci_dev, device, 0, 4,
  594. 0, PAGE_SIZE,
  595. &vp_dev->device_len);
  596. if (!vp_dev->device)
  597. goto err_map_device;
  598. vp_dev->vdev.config = &virtio_pci_config_ops;
  599. } else {
  600. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  601. }
  602. vp_dev->config_vector = vp_config_vector;
  603. vp_dev->setup_vq = setup_vq;
  604. vp_dev->del_vq = del_vq;
  605. return 0;
  606. err_map_device:
  607. if (vp_dev->notify_base)
  608. pci_iounmap(pci_dev, vp_dev->notify_base);
  609. err_map_notify:
  610. pci_iounmap(pci_dev, vp_dev->isr);
  611. err_map_isr:
  612. pci_iounmap(pci_dev, vp_dev->common);
  613. err_map_common:
  614. return err;
  615. }
  616. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  617. {
  618. struct pci_dev *pci_dev = vp_dev->pci_dev;
  619. if (vp_dev->device)
  620. pci_iounmap(pci_dev, vp_dev->device);
  621. if (vp_dev->notify_base)
  622. pci_iounmap(pci_dev, vp_dev->notify_base);
  623. pci_iounmap(pci_dev, vp_dev->isr);
  624. pci_iounmap(pci_dev, vp_dev->common);
  625. pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
  626. }