virtio_pci_modern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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, int *bars)
  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. *bars |= (1 << bar);
  451. return pos;
  452. }
  453. }
  454. }
  455. return 0;
  456. }
  457. /* This is part of the ABI. Don't screw with it. */
  458. static inline void check_offsets(void)
  459. {
  460. /* Note: disk space was harmed in compilation of this function. */
  461. BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
  462. offsetof(struct virtio_pci_cap, cap_vndr));
  463. BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
  464. offsetof(struct virtio_pci_cap, cap_next));
  465. BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
  466. offsetof(struct virtio_pci_cap, cap_len));
  467. BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
  468. offsetof(struct virtio_pci_cap, cfg_type));
  469. BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
  470. offsetof(struct virtio_pci_cap, bar));
  471. BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
  472. offsetof(struct virtio_pci_cap, offset));
  473. BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
  474. offsetof(struct virtio_pci_cap, length));
  475. BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
  476. offsetof(struct virtio_pci_notify_cap,
  477. notify_off_multiplier));
  478. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
  479. offsetof(struct virtio_pci_common_cfg,
  480. device_feature_select));
  481. BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
  482. offsetof(struct virtio_pci_common_cfg, device_feature));
  483. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
  484. offsetof(struct virtio_pci_common_cfg,
  485. guest_feature_select));
  486. BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
  487. offsetof(struct virtio_pci_common_cfg, guest_feature));
  488. BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
  489. offsetof(struct virtio_pci_common_cfg, msix_config));
  490. BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
  491. offsetof(struct virtio_pci_common_cfg, num_queues));
  492. BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
  493. offsetof(struct virtio_pci_common_cfg, device_status));
  494. BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
  495. offsetof(struct virtio_pci_common_cfg, config_generation));
  496. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
  497. offsetof(struct virtio_pci_common_cfg, queue_select));
  498. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
  499. offsetof(struct virtio_pci_common_cfg, queue_size));
  500. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
  501. offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
  502. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
  503. offsetof(struct virtio_pci_common_cfg, queue_enable));
  504. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
  505. offsetof(struct virtio_pci_common_cfg, queue_notify_off));
  506. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
  507. offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
  508. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
  509. offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
  510. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
  511. offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
  512. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
  513. offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
  514. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
  515. offsetof(struct virtio_pci_common_cfg, queue_used_lo));
  516. BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
  517. offsetof(struct virtio_pci_common_cfg, queue_used_hi));
  518. }
  519. /* the PCI probing function */
  520. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  521. {
  522. struct pci_dev *pci_dev = vp_dev->pci_dev;
  523. int err, common, isr, notify, device;
  524. u32 notify_length;
  525. u32 notify_offset;
  526. check_offsets();
  527. /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
  528. if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
  529. return -ENODEV;
  530. if (pci_dev->device < 0x1040) {
  531. /* Transitional devices: use the PCI subsystem device id as
  532. * virtio device id, same as legacy driver always did.
  533. */
  534. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  535. } else {
  536. /* Modern devices: simply use PCI device id, but start from 0x1040. */
  537. vp_dev->vdev.id.device = pci_dev->device - 0x1040;
  538. }
  539. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  540. /* check for a common config: if not, use legacy mode (bar 0). */
  541. common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
  542. IORESOURCE_IO | IORESOURCE_MEM,
  543. &vp_dev->modern_bars);
  544. if (!common) {
  545. dev_info(&pci_dev->dev,
  546. "virtio_pci: leaving for legacy driver\n");
  547. return -ENODEV;
  548. }
  549. /* If common is there, these should be too... */
  550. isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
  551. IORESOURCE_IO | IORESOURCE_MEM,
  552. &vp_dev->modern_bars);
  553. notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
  554. IORESOURCE_IO | IORESOURCE_MEM,
  555. &vp_dev->modern_bars);
  556. if (!isr || !notify) {
  557. dev_err(&pci_dev->dev,
  558. "virtio_pci: missing capabilities %i/%i/%i\n",
  559. common, isr, notify);
  560. return -EINVAL;
  561. }
  562. /* Device capability is only mandatory for devices that have
  563. * device-specific configuration.
  564. */
  565. device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
  566. IORESOURCE_IO | IORESOURCE_MEM,
  567. &vp_dev->modern_bars);
  568. err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
  569. "virtio-pci-modern");
  570. if (err)
  571. return err;
  572. err = -EINVAL;
  573. vp_dev->common = map_capability(pci_dev, common,
  574. sizeof(struct virtio_pci_common_cfg), 4,
  575. 0, sizeof(struct virtio_pci_common_cfg),
  576. NULL);
  577. if (!vp_dev->common)
  578. goto err_map_common;
  579. vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
  580. 0, 1,
  581. NULL);
  582. if (!vp_dev->isr)
  583. goto err_map_isr;
  584. /* Read notify_off_multiplier from config space. */
  585. pci_read_config_dword(pci_dev,
  586. notify + offsetof(struct virtio_pci_notify_cap,
  587. notify_off_multiplier),
  588. &vp_dev->notify_offset_multiplier);
  589. /* Read notify length and offset from config space. */
  590. pci_read_config_dword(pci_dev,
  591. notify + offsetof(struct virtio_pci_notify_cap,
  592. cap.length),
  593. &notify_length);
  594. pci_read_config_dword(pci_dev,
  595. notify + offsetof(struct virtio_pci_notify_cap,
  596. cap.length),
  597. &notify_offset);
  598. /* We don't know how many VQs we'll map, ahead of the time.
  599. * If notify length is small, map it all now.
  600. * Otherwise, map each VQ individually later.
  601. */
  602. if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
  603. vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
  604. 0, notify_length,
  605. &vp_dev->notify_len);
  606. if (!vp_dev->notify_base)
  607. goto err_map_notify;
  608. } else {
  609. vp_dev->notify_map_cap = notify;
  610. }
  611. /* Again, we don't know how much we should map, but PAGE_SIZE
  612. * is more than enough for all existing devices.
  613. */
  614. if (device) {
  615. vp_dev->device = map_capability(pci_dev, device, 0, 4,
  616. 0, PAGE_SIZE,
  617. &vp_dev->device_len);
  618. if (!vp_dev->device)
  619. goto err_map_device;
  620. vp_dev->vdev.config = &virtio_pci_config_ops;
  621. } else {
  622. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  623. }
  624. vp_dev->config_vector = vp_config_vector;
  625. vp_dev->setup_vq = setup_vq;
  626. vp_dev->del_vq = del_vq;
  627. return 0;
  628. err_map_device:
  629. if (vp_dev->notify_base)
  630. pci_iounmap(pci_dev, vp_dev->notify_base);
  631. err_map_notify:
  632. pci_iounmap(pci_dev, vp_dev->isr);
  633. err_map_isr:
  634. pci_iounmap(pci_dev, vp_dev->common);
  635. err_map_common:
  636. return err;
  637. }
  638. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  639. {
  640. struct pci_dev *pci_dev = vp_dev->pci_dev;
  641. if (vp_dev->device)
  642. pci_iounmap(pci_dev, vp_dev->device);
  643. if (vp_dev->notify_base)
  644. pci_iounmap(pci_dev, vp_dev->notify_base);
  645. pci_iounmap(pci_dev, vp_dev->isr);
  646. pci_iounmap(pci_dev, vp_dev->common);
  647. pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
  648. }