virtio_pci_modern.c 21 KB

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