virtio_pci_modern.c 20 KB

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