virtio_pci_modern.c 20 KB

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