virtio_pci_modern.c 20 KB

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