virtio_pci_modern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. static void vp_transport_features(struct virtio_device *vdev, u64 features)
  134. {
  135. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  136. struct pci_dev *pci_dev = vp_dev->pci_dev;
  137. if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
  138. pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
  139. __virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
  140. }
  141. /* virtio config->finalize_features() implementation */
  142. static int vp_finalize_features(struct virtio_device *vdev)
  143. {
  144. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  145. u64 features = vdev->features;
  146. /* Give virtio_ring a chance to accept features. */
  147. vring_transport_features(vdev);
  148. /* Give virtio_pci a chance to accept features. */
  149. vp_transport_features(vdev, features);
  150. if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  151. dev_err(&vdev->dev, "virtio: device uses modern interface "
  152. "but does not have VIRTIO_F_VERSION_1\n");
  153. return -EINVAL;
  154. }
  155. vp_iowrite32(0, &vp_dev->common->guest_feature_select);
  156. vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
  157. vp_iowrite32(1, &vp_dev->common->guest_feature_select);
  158. vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
  159. return 0;
  160. }
  161. /* virtio config->get() implementation */
  162. static void vp_get(struct virtio_device *vdev, unsigned offset,
  163. void *buf, unsigned len)
  164. {
  165. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  166. u8 b;
  167. __le16 w;
  168. __le32 l;
  169. BUG_ON(offset + len > vp_dev->device_len);
  170. switch (len) {
  171. case 1:
  172. b = ioread8(vp_dev->device + offset);
  173. memcpy(buf, &b, sizeof b);
  174. break;
  175. case 2:
  176. w = cpu_to_le16(ioread16(vp_dev->device + offset));
  177. memcpy(buf, &w, sizeof w);
  178. break;
  179. case 4:
  180. l = cpu_to_le32(ioread32(vp_dev->device + offset));
  181. memcpy(buf, &l, sizeof l);
  182. break;
  183. case 8:
  184. l = cpu_to_le32(ioread32(vp_dev->device + offset));
  185. memcpy(buf, &l, sizeof l);
  186. l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
  187. memcpy(buf + sizeof l, &l, sizeof l);
  188. break;
  189. default:
  190. BUG();
  191. }
  192. }
  193. /* the config->set() implementation. it's symmetric to the config->get()
  194. * implementation */
  195. static void vp_set(struct virtio_device *vdev, unsigned offset,
  196. const void *buf, unsigned len)
  197. {
  198. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  199. u8 b;
  200. __le16 w;
  201. __le32 l;
  202. BUG_ON(offset + len > vp_dev->device_len);
  203. switch (len) {
  204. case 1:
  205. memcpy(&b, buf, sizeof b);
  206. iowrite8(b, vp_dev->device + offset);
  207. break;
  208. case 2:
  209. memcpy(&w, buf, sizeof w);
  210. iowrite16(le16_to_cpu(w), vp_dev->device + offset);
  211. break;
  212. case 4:
  213. memcpy(&l, buf, sizeof l);
  214. iowrite32(le32_to_cpu(l), vp_dev->device + offset);
  215. break;
  216. case 8:
  217. memcpy(&l, buf, sizeof l);
  218. iowrite32(le32_to_cpu(l), vp_dev->device + offset);
  219. memcpy(&l, buf + sizeof l, sizeof l);
  220. iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
  221. break;
  222. default:
  223. BUG();
  224. }
  225. }
  226. static u32 vp_generation(struct virtio_device *vdev)
  227. {
  228. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  229. return vp_ioread8(&vp_dev->common->config_generation);
  230. }
  231. /* config->{get,set}_status() implementations */
  232. static u8 vp_get_status(struct virtio_device *vdev)
  233. {
  234. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  235. return vp_ioread8(&vp_dev->common->device_status);
  236. }
  237. static void vp_set_status(struct virtio_device *vdev, u8 status)
  238. {
  239. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  240. /* We should never be setting status to 0. */
  241. BUG_ON(status == 0);
  242. vp_iowrite8(status, &vp_dev->common->device_status);
  243. }
  244. static void vp_reset(struct virtio_device *vdev)
  245. {
  246. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  247. /* 0 status means a reset. */
  248. vp_iowrite8(0, &vp_dev->common->device_status);
  249. /* After writing 0 to device_status, the driver MUST wait for a read of
  250. * device_status to return 0 before reinitializing the device.
  251. * This will flush out the status write, and flush in device writes,
  252. * including MSI-X interrupts, if any.
  253. */
  254. while (vp_ioread8(&vp_dev->common->device_status))
  255. msleep(1);
  256. /* Flush pending VQ/configuration callbacks. */
  257. vp_synchronize_vectors(vdev);
  258. }
  259. static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
  260. {
  261. /* Setup the vector used for configuration events */
  262. vp_iowrite16(vector, &vp_dev->common->msix_config);
  263. /* Verify we had enough resources to assign the vector */
  264. /* Will also flush the write out to device */
  265. return vp_ioread16(&vp_dev->common->msix_config);
  266. }
  267. static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
  268. struct virtio_pci_vq_info *info,
  269. unsigned index,
  270. void (*callback)(struct virtqueue *vq),
  271. const char *name,
  272. bool ctx,
  273. u16 msix_vec)
  274. {
  275. struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
  276. struct virtqueue *vq;
  277. u16 num, off;
  278. int err;
  279. if (index >= vp_ioread16(&cfg->num_queues))
  280. return ERR_PTR(-ENOENT);
  281. /* Select the queue we're interested in */
  282. vp_iowrite16(index, &cfg->queue_select);
  283. /* Check if queue is either not available or already active. */
  284. num = vp_ioread16(&cfg->queue_size);
  285. if (!num || vp_ioread16(&cfg->queue_enable))
  286. return ERR_PTR(-ENOENT);
  287. if (num & (num - 1)) {
  288. dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
  289. return ERR_PTR(-EINVAL);
  290. }
  291. /* get offset of notification word for this vq */
  292. off = vp_ioread16(&cfg->queue_notify_off);
  293. info->msix_vector = msix_vec;
  294. /* create the vring */
  295. vq = vring_create_virtqueue(index, num,
  296. SMP_CACHE_BYTES, &vp_dev->vdev,
  297. true, true, ctx,
  298. vp_notify, callback, name);
  299. if (!vq)
  300. return ERR_PTR(-ENOMEM);
  301. /* activate the queue */
  302. vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
  303. vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
  304. &cfg->queue_desc_lo, &cfg->queue_desc_hi);
  305. vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
  306. &cfg->queue_avail_lo, &cfg->queue_avail_hi);
  307. vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
  308. &cfg->queue_used_lo, &cfg->queue_used_hi);
  309. if (vp_dev->notify_base) {
  310. /* offset should not wrap */
  311. if ((u64)off * vp_dev->notify_offset_multiplier + 2
  312. > vp_dev->notify_len) {
  313. dev_warn(&vp_dev->pci_dev->dev,
  314. "bad notification offset %u (x %u) "
  315. "for queue %u > %zd",
  316. off, vp_dev->notify_offset_multiplier,
  317. index, vp_dev->notify_len);
  318. err = -EINVAL;
  319. goto err_map_notify;
  320. }
  321. vq->priv = (void __force *)vp_dev->notify_base +
  322. off * vp_dev->notify_offset_multiplier;
  323. } else {
  324. vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
  325. vp_dev->notify_map_cap, 2, 2,
  326. off * vp_dev->notify_offset_multiplier, 2,
  327. NULL);
  328. }
  329. if (!vq->priv) {
  330. err = -ENOMEM;
  331. goto err_map_notify;
  332. }
  333. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  334. vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
  335. msix_vec = vp_ioread16(&cfg->queue_msix_vector);
  336. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  337. err = -EBUSY;
  338. goto err_assign_vector;
  339. }
  340. }
  341. return vq;
  342. err_assign_vector:
  343. if (!vp_dev->notify_base)
  344. pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
  345. err_map_notify:
  346. vring_del_virtqueue(vq);
  347. return ERR_PTR(err);
  348. }
  349. static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  350. struct virtqueue *vqs[],
  351. vq_callback_t *callbacks[],
  352. const char * const names[], const bool *ctx,
  353. struct irq_affinity *desc)
  354. {
  355. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  356. struct virtqueue *vq;
  357. int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
  358. if (rc)
  359. return rc;
  360. /* Select and activate all queues. Has to be done last: once we do
  361. * this, there's no way to go back except reset.
  362. */
  363. list_for_each_entry(vq, &vdev->vqs, list) {
  364. vp_iowrite16(vq->index, &vp_dev->common->queue_select);
  365. vp_iowrite16(1, &vp_dev->common->queue_enable);
  366. }
  367. return 0;
  368. }
  369. static void del_vq(struct virtio_pci_vq_info *info)
  370. {
  371. struct virtqueue *vq = info->vq;
  372. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  373. vp_iowrite16(vq->index, &vp_dev->common->queue_select);
  374. if (vp_dev->msix_enabled) {
  375. vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
  376. &vp_dev->common->queue_msix_vector);
  377. /* Flush the write out to device */
  378. vp_ioread16(&vp_dev->common->queue_msix_vector);
  379. }
  380. if (!vp_dev->notify_base)
  381. pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
  382. vring_del_virtqueue(vq);
  383. }
  384. static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
  385. .get = NULL,
  386. .set = NULL,
  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. .get_vq_affinity = vp_get_vq_affinity,
  398. };
  399. static const struct virtio_config_ops virtio_pci_config_ops = {
  400. .get = vp_get,
  401. .set = vp_set,
  402. .generation = vp_generation,
  403. .get_status = vp_get_status,
  404. .set_status = vp_set_status,
  405. .reset = vp_reset,
  406. .find_vqs = vp_modern_find_vqs,
  407. .del_vqs = vp_del_vqs,
  408. .get_features = vp_get_features,
  409. .finalize_features = vp_finalize_features,
  410. .bus_name = vp_bus_name,
  411. .set_vq_affinity = vp_set_vq_affinity,
  412. .get_vq_affinity = vp_get_vq_affinity,
  413. };
  414. /**
  415. * virtio_pci_find_capability - walk capabilities to find device info.
  416. * @dev: the pci device
  417. * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
  418. * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
  419. *
  420. * Returns offset of the capability, or 0.
  421. */
  422. static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
  423. u32 ioresource_types, int *bars)
  424. {
  425. int pos;
  426. for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
  427. pos > 0;
  428. pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
  429. u8 type, bar;
  430. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  431. cfg_type),
  432. &type);
  433. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  434. bar),
  435. &bar);
  436. /* Ignore structures with reserved BAR values */
  437. if (bar > 0x5)
  438. continue;
  439. if (type == cfg_type) {
  440. if (pci_resource_len(dev, bar) &&
  441. pci_resource_flags(dev, bar) & ioresource_types) {
  442. *bars |= (1 << bar);
  443. return pos;
  444. }
  445. }
  446. }
  447. return 0;
  448. }
  449. /* This is part of the ABI. Don't screw with it. */
  450. static inline void check_offsets(void)
  451. {
  452. /* Note: disk space was harmed in compilation of this function. */
  453. BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
  454. offsetof(struct virtio_pci_cap, cap_vndr));
  455. BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
  456. offsetof(struct virtio_pci_cap, cap_next));
  457. BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
  458. offsetof(struct virtio_pci_cap, cap_len));
  459. BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
  460. offsetof(struct virtio_pci_cap, cfg_type));
  461. BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
  462. offsetof(struct virtio_pci_cap, bar));
  463. BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
  464. offsetof(struct virtio_pci_cap, offset));
  465. BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
  466. offsetof(struct virtio_pci_cap, length));
  467. BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
  468. offsetof(struct virtio_pci_notify_cap,
  469. notify_off_multiplier));
  470. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
  471. offsetof(struct virtio_pci_common_cfg,
  472. device_feature_select));
  473. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
  474. offsetof(struct virtio_pci_common_cfg, device_feature));
  475. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
  476. offsetof(struct virtio_pci_common_cfg,
  477. guest_feature_select));
  478. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
  479. offsetof(struct virtio_pci_common_cfg, guest_feature));
  480. BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
  481. offsetof(struct virtio_pci_common_cfg, msix_config));
  482. BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
  483. offsetof(struct virtio_pci_common_cfg, num_queues));
  484. BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
  485. offsetof(struct virtio_pci_common_cfg, device_status));
  486. BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
  487. offsetof(struct virtio_pci_common_cfg, config_generation));
  488. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
  489. offsetof(struct virtio_pci_common_cfg, queue_select));
  490. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
  491. offsetof(struct virtio_pci_common_cfg, queue_size));
  492. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
  493. offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
  494. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
  495. offsetof(struct virtio_pci_common_cfg, queue_enable));
  496. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
  497. offsetof(struct virtio_pci_common_cfg, queue_notify_off));
  498. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
  499. offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
  500. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
  501. offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
  502. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
  503. offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
  504. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
  505. offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
  506. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
  507. offsetof(struct virtio_pci_common_cfg, queue_used_lo));
  508. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
  509. offsetof(struct virtio_pci_common_cfg, queue_used_hi));
  510. }
  511. /* the PCI probing function */
  512. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  513. {
  514. struct pci_dev *pci_dev = vp_dev->pci_dev;
  515. int err, common, isr, notify, device;
  516. u32 notify_length;
  517. u32 notify_offset;
  518. check_offsets();
  519. /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
  520. if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
  521. return -ENODEV;
  522. if (pci_dev->device < 0x1040) {
  523. /* Transitional devices: use the PCI subsystem device id as
  524. * virtio device id, same as legacy driver always did.
  525. */
  526. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  527. } else {
  528. /* Modern devices: simply use PCI device id, but start from 0x1040. */
  529. vp_dev->vdev.id.device = pci_dev->device - 0x1040;
  530. }
  531. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  532. /* check for a common config: if not, use legacy mode (bar 0). */
  533. common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
  534. IORESOURCE_IO | IORESOURCE_MEM,
  535. &vp_dev->modern_bars);
  536. if (!common) {
  537. dev_info(&pci_dev->dev,
  538. "virtio_pci: leaving for legacy driver\n");
  539. return -ENODEV;
  540. }
  541. /* If common is there, these should be too... */
  542. isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
  543. IORESOURCE_IO | IORESOURCE_MEM,
  544. &vp_dev->modern_bars);
  545. notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
  546. IORESOURCE_IO | IORESOURCE_MEM,
  547. &vp_dev->modern_bars);
  548. if (!isr || !notify) {
  549. dev_err(&pci_dev->dev,
  550. "virtio_pci: missing capabilities %i/%i/%i\n",
  551. common, isr, notify);
  552. return -EINVAL;
  553. }
  554. err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
  555. if (err)
  556. err = dma_set_mask_and_coherent(&pci_dev->dev,
  557. DMA_BIT_MASK(32));
  558. if (err)
  559. dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  560. /* Device capability is only mandatory for devices that have
  561. * device-specific configuration.
  562. */
  563. device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
  564. IORESOURCE_IO | IORESOURCE_MEM,
  565. &vp_dev->modern_bars);
  566. err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
  567. "virtio-pci-modern");
  568. if (err)
  569. return err;
  570. err = -EINVAL;
  571. vp_dev->common = map_capability(pci_dev, common,
  572. sizeof(struct virtio_pci_common_cfg), 4,
  573. 0, sizeof(struct virtio_pci_common_cfg),
  574. NULL);
  575. if (!vp_dev->common)
  576. goto err_map_common;
  577. vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
  578. 0, 1,
  579. NULL);
  580. if (!vp_dev->isr)
  581. goto err_map_isr;
  582. /* Read notify_off_multiplier from config space. */
  583. pci_read_config_dword(pci_dev,
  584. notify + offsetof(struct virtio_pci_notify_cap,
  585. notify_off_multiplier),
  586. &vp_dev->notify_offset_multiplier);
  587. /* Read notify length and offset from config space. */
  588. pci_read_config_dword(pci_dev,
  589. notify + offsetof(struct virtio_pci_notify_cap,
  590. cap.length),
  591. &notify_length);
  592. pci_read_config_dword(pci_dev,
  593. notify + offsetof(struct virtio_pci_notify_cap,
  594. cap.offset),
  595. &notify_offset);
  596. /* We don't know how many VQs we'll map, ahead of the time.
  597. * If notify length is small, map it all now.
  598. * Otherwise, map each VQ individually later.
  599. */
  600. if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
  601. vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
  602. 0, notify_length,
  603. &vp_dev->notify_len);
  604. if (!vp_dev->notify_base)
  605. goto err_map_notify;
  606. } else {
  607. vp_dev->notify_map_cap = notify;
  608. }
  609. /* Again, we don't know how much we should map, but PAGE_SIZE
  610. * is more than enough for all existing devices.
  611. */
  612. if (device) {
  613. vp_dev->device = map_capability(pci_dev, device, 0, 4,
  614. 0, PAGE_SIZE,
  615. &vp_dev->device_len);
  616. if (!vp_dev->device)
  617. goto err_map_device;
  618. vp_dev->vdev.config = &virtio_pci_config_ops;
  619. } else {
  620. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  621. }
  622. vp_dev->config_vector = vp_config_vector;
  623. vp_dev->setup_vq = setup_vq;
  624. vp_dev->del_vq = del_vq;
  625. return 0;
  626. err_map_device:
  627. if (vp_dev->notify_base)
  628. pci_iounmap(pci_dev, vp_dev->notify_base);
  629. err_map_notify:
  630. pci_iounmap(pci_dev, vp_dev->isr);
  631. err_map_isr:
  632. pci_iounmap(pci_dev, vp_dev->common);
  633. err_map_common:
  634. return err;
  635. }
  636. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  637. {
  638. struct pci_dev *pci_dev = vp_dev->pci_dev;
  639. if (vp_dev->device)
  640. pci_iounmap(pci_dev, vp_dev->device);
  641. if (vp_dev->notify_base)
  642. pci_iounmap(pci_dev, vp_dev->notify_base);
  643. pci_iounmap(pci_dev, vp_dev->isr);
  644. pci_iounmap(pci_dev, vp_dev->common);
  645. pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
  646. }