virtio_pci_modern.c 20 KB

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