virtio_pci_modern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio PCI driver - modern (virtio 1.0) device support
  4. *
  5. * This module allows virtio devices to be used over a virtual PCI device.
  6. * This can be used with QEMU based VMMs like KVM or Xen.
  7. *
  8. * Copyright IBM Corp. 2007
  9. * Copyright Red Hat, Inc. 2014
  10. *
  11. * Authors:
  12. * Anthony Liguori <aliguori@us.ibm.com>
  13. * Rusty Russell <rusty@rustcorp.com.au>
  14. * Michael S. Tsirkin <mst@redhat.com>
  15. */
  16. #include <linux/delay.h>
  17. #define VIRTIO_PCI_NO_LEGACY
  18. #define VIRTIO_RING_NO_LEGACY
  19. #include "virtio_pci_common.h"
  20. #define VIRTIO_AVQ_SGS_MAX 4
  21. static u64 vp_get_features(struct virtio_device *vdev)
  22. {
  23. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  24. return vp_modern_get_features(&vp_dev->mdev);
  25. }
  26. static int vp_avq_index(struct virtio_device *vdev, u16 *index, u16 *num)
  27. {
  28. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  29. *num = 0;
  30. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  31. return 0;
  32. *num = vp_modern_avq_num(&vp_dev->mdev);
  33. if (!(*num))
  34. return -EINVAL;
  35. *index = vp_modern_avq_index(&vp_dev->mdev);
  36. return 0;
  37. }
  38. void vp_modern_avq_done(struct virtqueue *vq)
  39. {
  40. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  41. struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq;
  42. struct virtio_admin_cmd *cmd;
  43. unsigned long flags;
  44. unsigned int len;
  45. spin_lock_irqsave(&admin_vq->lock, flags);
  46. do {
  47. virtqueue_disable_cb(vq);
  48. while ((cmd = virtqueue_get_buf(vq, &len)))
  49. complete(&cmd->completion);
  50. } while (!virtqueue_enable_cb(vq));
  51. spin_unlock_irqrestore(&admin_vq->lock, flags);
  52. }
  53. static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
  54. u16 opcode,
  55. struct scatterlist **sgs,
  56. unsigned int out_num,
  57. unsigned int in_num,
  58. struct virtio_admin_cmd *cmd)
  59. {
  60. struct virtqueue *vq;
  61. unsigned long flags;
  62. int ret;
  63. vq = admin_vq->info->vq;
  64. if (!vq)
  65. return -EIO;
  66. if (opcode != VIRTIO_ADMIN_CMD_LIST_QUERY &&
  67. opcode != VIRTIO_ADMIN_CMD_LIST_USE &&
  68. !((1ULL << opcode) & admin_vq->supported_cmds))
  69. return -EOPNOTSUPP;
  70. init_completion(&cmd->completion);
  71. again:
  72. if (virtqueue_is_broken(vq))
  73. return -EIO;
  74. spin_lock_irqsave(&admin_vq->lock, flags);
  75. ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
  76. if (ret < 0) {
  77. if (ret == -ENOSPC) {
  78. spin_unlock_irqrestore(&admin_vq->lock, flags);
  79. cpu_relax();
  80. goto again;
  81. }
  82. goto unlock_err;
  83. }
  84. if (!virtqueue_kick(vq))
  85. goto unlock_err;
  86. spin_unlock_irqrestore(&admin_vq->lock, flags);
  87. wait_for_completion(&cmd->completion);
  88. return cmd->ret;
  89. unlock_err:
  90. spin_unlock_irqrestore(&admin_vq->lock, flags);
  91. return -EIO;
  92. }
  93. int vp_modern_admin_cmd_exec(struct virtio_device *vdev,
  94. struct virtio_admin_cmd *cmd)
  95. {
  96. struct scatterlist *sgs[VIRTIO_AVQ_SGS_MAX], hdr, stat;
  97. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  98. struct virtio_admin_cmd_status *va_status;
  99. unsigned int out_num = 0, in_num = 0;
  100. struct virtio_admin_cmd_hdr *va_hdr;
  101. u16 status;
  102. int ret;
  103. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  104. return -EOPNOTSUPP;
  105. va_status = kzalloc(sizeof(*va_status), GFP_KERNEL);
  106. if (!va_status)
  107. return -ENOMEM;
  108. va_hdr = kzalloc(sizeof(*va_hdr), GFP_KERNEL);
  109. if (!va_hdr) {
  110. ret = -ENOMEM;
  111. goto err_alloc;
  112. }
  113. va_hdr->opcode = cmd->opcode;
  114. va_hdr->group_type = cmd->group_type;
  115. va_hdr->group_member_id = cmd->group_member_id;
  116. /* Add header */
  117. sg_init_one(&hdr, va_hdr, sizeof(*va_hdr));
  118. sgs[out_num] = &hdr;
  119. out_num++;
  120. if (cmd->data_sg) {
  121. sgs[out_num] = cmd->data_sg;
  122. out_num++;
  123. }
  124. /* Add return status */
  125. sg_init_one(&stat, va_status, sizeof(*va_status));
  126. sgs[out_num + in_num] = &stat;
  127. in_num++;
  128. if (cmd->result_sg) {
  129. sgs[out_num + in_num] = cmd->result_sg;
  130. in_num++;
  131. }
  132. ret = virtqueue_exec_admin_cmd(&vp_dev->admin_vq,
  133. le16_to_cpu(cmd->opcode),
  134. sgs, out_num, in_num, cmd);
  135. if (ret) {
  136. dev_err(&vdev->dev,
  137. "Failed to execute command on admin vq: %d\n.", ret);
  138. goto err_cmd_exec;
  139. }
  140. status = le16_to_cpu(va_status->status);
  141. if (status != VIRTIO_ADMIN_STATUS_OK) {
  142. dev_err(&vdev->dev,
  143. "admin command error: status(%#x) qualifier(%#x)\n",
  144. status, le16_to_cpu(va_status->status_qualifier));
  145. ret = -status;
  146. }
  147. err_cmd_exec:
  148. kfree(va_hdr);
  149. err_alloc:
  150. kfree(va_status);
  151. return ret;
  152. }
  153. static void virtio_pci_admin_cmd_list_init(struct virtio_device *virtio_dev)
  154. {
  155. struct virtio_pci_device *vp_dev = to_vp_device(virtio_dev);
  156. struct virtio_admin_cmd cmd = {};
  157. struct scatterlist result_sg;
  158. struct scatterlist data_sg;
  159. __le64 *data;
  160. int ret;
  161. data = kzalloc(sizeof(*data), GFP_KERNEL);
  162. if (!data)
  163. return;
  164. sg_init_one(&result_sg, data, sizeof(*data));
  165. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_QUERY);
  166. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  167. cmd.result_sg = &result_sg;
  168. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  169. if (ret)
  170. goto end;
  171. *data &= cpu_to_le64(VIRTIO_ADMIN_CMD_BITMAP);
  172. sg_init_one(&data_sg, data, sizeof(*data));
  173. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_USE);
  174. cmd.data_sg = &data_sg;
  175. cmd.result_sg = NULL;
  176. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  177. if (ret)
  178. goto end;
  179. vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
  180. end:
  181. kfree(data);
  182. }
  183. static void vp_modern_avq_activate(struct virtio_device *vdev)
  184. {
  185. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  186. return;
  187. virtio_pci_admin_cmd_list_init(vdev);
  188. }
  189. static void vp_modern_avq_cleanup(struct virtio_device *vdev)
  190. {
  191. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  192. struct virtio_admin_cmd *cmd;
  193. struct virtqueue *vq;
  194. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  195. return;
  196. vq = vp_dev->admin_vq.info->vq;
  197. if (!vq)
  198. return;
  199. while ((cmd = virtqueue_detach_unused_buf(vq))) {
  200. cmd->ret = -EIO;
  201. complete(&cmd->completion);
  202. }
  203. }
  204. static void vp_transport_features(struct virtio_device *vdev, u64 features)
  205. {
  206. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  207. struct pci_dev *pci_dev = vp_dev->pci_dev;
  208. if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
  209. pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
  210. __virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
  211. if (features & BIT_ULL(VIRTIO_F_RING_RESET))
  212. __virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
  213. if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
  214. __virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
  215. }
  216. static int __vp_check_common_size_one_feature(struct virtio_device *vdev, u32 fbit,
  217. u32 offset, const char *fname)
  218. {
  219. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  220. if (!__virtio_test_bit(vdev, fbit))
  221. return 0;
  222. if (likely(vp_dev->mdev.common_len >= offset))
  223. return 0;
  224. dev_err(&vdev->dev,
  225. "virtio: common cfg size(%zu) does not match the feature %s\n",
  226. vp_dev->mdev.common_len, fname);
  227. return -EINVAL;
  228. }
  229. #define vp_check_common_size_one_feature(vdev, fbit, field) \
  230. __vp_check_common_size_one_feature(vdev, fbit, \
  231. offsetofend(struct virtio_pci_modern_common_cfg, field), #fbit)
  232. static int vp_check_common_size(struct virtio_device *vdev)
  233. {
  234. if (vp_check_common_size_one_feature(vdev, VIRTIO_F_NOTIF_CONFIG_DATA, queue_notify_data))
  235. return -EINVAL;
  236. if (vp_check_common_size_one_feature(vdev, VIRTIO_F_RING_RESET, queue_reset))
  237. return -EINVAL;
  238. if (vp_check_common_size_one_feature(vdev, VIRTIO_F_ADMIN_VQ, admin_queue_num))
  239. return -EINVAL;
  240. return 0;
  241. }
  242. /* virtio config->finalize_features() implementation */
  243. static int vp_finalize_features(struct virtio_device *vdev)
  244. {
  245. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  246. u64 features = vdev->features;
  247. /* Give virtio_ring a chance to accept features. */
  248. vring_transport_features(vdev);
  249. /* Give virtio_pci a chance to accept features. */
  250. vp_transport_features(vdev, features);
  251. if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  252. dev_err(&vdev->dev, "virtio: device uses modern interface "
  253. "but does not have VIRTIO_F_VERSION_1\n");
  254. return -EINVAL;
  255. }
  256. if (vp_check_common_size(vdev))
  257. return -EINVAL;
  258. vp_modern_set_features(&vp_dev->mdev, vdev->features);
  259. return 0;
  260. }
  261. /* virtio config->get() implementation */
  262. static void vp_get(struct virtio_device *vdev, unsigned int offset,
  263. void *buf, unsigned int len)
  264. {
  265. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  266. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  267. void __iomem *device = mdev->device;
  268. u8 b;
  269. __le16 w;
  270. __le32 l;
  271. BUG_ON(offset + len > mdev->device_len);
  272. switch (len) {
  273. case 1:
  274. b = ioread8(device + offset);
  275. memcpy(buf, &b, sizeof b);
  276. break;
  277. case 2:
  278. w = cpu_to_le16(ioread16(device + offset));
  279. memcpy(buf, &w, sizeof w);
  280. break;
  281. case 4:
  282. l = cpu_to_le32(ioread32(device + offset));
  283. memcpy(buf, &l, sizeof l);
  284. break;
  285. case 8:
  286. l = cpu_to_le32(ioread32(device + offset));
  287. memcpy(buf, &l, sizeof l);
  288. l = cpu_to_le32(ioread32(device + offset + sizeof l));
  289. memcpy(buf + sizeof l, &l, sizeof l);
  290. break;
  291. default:
  292. BUG();
  293. }
  294. }
  295. /* the config->set() implementation. it's symmetric to the config->get()
  296. * implementation */
  297. static void vp_set(struct virtio_device *vdev, unsigned int offset,
  298. const void *buf, unsigned int len)
  299. {
  300. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  301. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  302. void __iomem *device = mdev->device;
  303. u8 b;
  304. __le16 w;
  305. __le32 l;
  306. BUG_ON(offset + len > mdev->device_len);
  307. switch (len) {
  308. case 1:
  309. memcpy(&b, buf, sizeof b);
  310. iowrite8(b, device + offset);
  311. break;
  312. case 2:
  313. memcpy(&w, buf, sizeof w);
  314. iowrite16(le16_to_cpu(w), device + offset);
  315. break;
  316. case 4:
  317. memcpy(&l, buf, sizeof l);
  318. iowrite32(le32_to_cpu(l), device + offset);
  319. break;
  320. case 8:
  321. memcpy(&l, buf, sizeof l);
  322. iowrite32(le32_to_cpu(l), device + offset);
  323. memcpy(&l, buf + sizeof l, sizeof l);
  324. iowrite32(le32_to_cpu(l), device + offset + sizeof l);
  325. break;
  326. default:
  327. BUG();
  328. }
  329. }
  330. static u32 vp_generation(struct virtio_device *vdev)
  331. {
  332. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  333. return vp_modern_generation(&vp_dev->mdev);
  334. }
  335. /* config->{get,set}_status() implementations */
  336. static u8 vp_get_status(struct virtio_device *vdev)
  337. {
  338. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  339. return vp_modern_get_status(&vp_dev->mdev);
  340. }
  341. static void vp_set_status(struct virtio_device *vdev, u8 status)
  342. {
  343. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  344. /* We should never be setting status to 0. */
  345. BUG_ON(status == 0);
  346. vp_modern_set_status(&vp_dev->mdev, status);
  347. if (status & VIRTIO_CONFIG_S_DRIVER_OK)
  348. vp_modern_avq_activate(vdev);
  349. }
  350. static void vp_reset(struct virtio_device *vdev)
  351. {
  352. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  353. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  354. /* 0 status means a reset. */
  355. vp_modern_set_status(mdev, 0);
  356. /* After writing 0 to device_status, the driver MUST wait for a read of
  357. * device_status to return 0 before reinitializing the device.
  358. * This will flush out the status write, and flush in device writes,
  359. * including MSI-X interrupts, if any.
  360. */
  361. while (vp_modern_get_status(mdev))
  362. msleep(1);
  363. vp_modern_avq_cleanup(vdev);
  364. /* Flush pending VQ/configuration callbacks. */
  365. vp_synchronize_vectors(vdev);
  366. }
  367. static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
  368. {
  369. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  370. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  371. unsigned long index;
  372. index = vq->index;
  373. /* activate the queue */
  374. vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
  375. vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
  376. virtqueue_get_avail_addr(vq),
  377. virtqueue_get_used_addr(vq));
  378. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  379. msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
  380. if (msix_vec == VIRTIO_MSI_NO_VECTOR)
  381. return -EBUSY;
  382. }
  383. return 0;
  384. }
  385. static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
  386. {
  387. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  388. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  389. struct virtio_pci_vq_info *info;
  390. unsigned long flags;
  391. if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
  392. return -ENOENT;
  393. vp_modern_set_queue_reset(mdev, vq->index);
  394. info = vp_dev->vqs[vq->index];
  395. /* delete vq from irq handler */
  396. spin_lock_irqsave(&vp_dev->lock, flags);
  397. list_del(&info->node);
  398. spin_unlock_irqrestore(&vp_dev->lock, flags);
  399. INIT_LIST_HEAD(&info->node);
  400. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  401. __virtqueue_break(vq);
  402. #endif
  403. /* For the case where vq has an exclusive irq, call synchronize_irq() to
  404. * wait for completion.
  405. *
  406. * note: We can't use disable_irq() since it conflicts with the affinity
  407. * managed IRQ that is used by some drivers.
  408. */
  409. if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  410. synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
  411. vq->reset = true;
  412. return 0;
  413. }
  414. static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
  415. {
  416. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  417. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  418. struct virtio_pci_vq_info *info;
  419. unsigned long flags, index;
  420. int err;
  421. if (!vq->reset)
  422. return -EBUSY;
  423. index = vq->index;
  424. info = vp_dev->vqs[index];
  425. if (vp_modern_get_queue_reset(mdev, index))
  426. return -EBUSY;
  427. if (vp_modern_get_queue_enable(mdev, index))
  428. return -EBUSY;
  429. err = vp_active_vq(vq, info->msix_vector);
  430. if (err)
  431. return err;
  432. if (vq->callback) {
  433. spin_lock_irqsave(&vp_dev->lock, flags);
  434. list_add(&info->node, &vp_dev->virtqueues);
  435. spin_unlock_irqrestore(&vp_dev->lock, flags);
  436. } else {
  437. INIT_LIST_HEAD(&info->node);
  438. }
  439. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  440. __virtqueue_unbreak(vq);
  441. #endif
  442. vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
  443. vq->reset = false;
  444. return 0;
  445. }
  446. static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
  447. {
  448. return vp_modern_config_vector(&vp_dev->mdev, vector);
  449. }
  450. static bool vp_notify_with_data(struct virtqueue *vq)
  451. {
  452. u32 data = vring_notification_data(vq);
  453. iowrite32(data, (void __iomem *)vq->priv);
  454. return true;
  455. }
  456. static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
  457. struct virtio_pci_vq_info *info,
  458. unsigned int index,
  459. void (*callback)(struct virtqueue *vq),
  460. const char *name,
  461. bool ctx,
  462. u16 msix_vec)
  463. {
  464. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  465. bool (*notify)(struct virtqueue *vq);
  466. struct virtqueue *vq;
  467. bool is_avq;
  468. u16 num;
  469. int err;
  470. if (__virtio_test_bit(&vp_dev->vdev, VIRTIO_F_NOTIFICATION_DATA))
  471. notify = vp_notify_with_data;
  472. else
  473. notify = vp_notify;
  474. is_avq = vp_is_avq(&vp_dev->vdev, index);
  475. if (index >= vp_modern_get_num_queues(mdev) && !is_avq)
  476. return ERR_PTR(-EINVAL);
  477. num = vp_modern_get_queue_size(mdev, index);
  478. /* Check if queue is either not available or already active. */
  479. if (!num || vp_modern_get_queue_enable(mdev, index))
  480. return ERR_PTR(-ENOENT);
  481. info->msix_vector = msix_vec;
  482. /* create the vring */
  483. vq = vring_create_virtqueue(index, num,
  484. SMP_CACHE_BYTES, &vp_dev->vdev,
  485. true, true, ctx,
  486. notify, callback, name);
  487. if (!vq)
  488. return ERR_PTR(-ENOMEM);
  489. vq->num_max = num;
  490. err = vp_active_vq(vq, msix_vec);
  491. if (err)
  492. goto err;
  493. vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
  494. if (!vq->priv) {
  495. err = -ENOMEM;
  496. goto err;
  497. }
  498. return vq;
  499. err:
  500. vring_del_virtqueue(vq);
  501. return ERR_PTR(err);
  502. }
  503. static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  504. struct virtqueue *vqs[],
  505. struct virtqueue_info vqs_info[],
  506. struct irq_affinity *desc)
  507. {
  508. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  509. struct virtqueue *vq;
  510. int rc = vp_find_vqs(vdev, nvqs, vqs, vqs_info, desc);
  511. if (rc)
  512. return rc;
  513. /* Select and activate all queues. Has to be done last: once we do
  514. * this, there's no way to go back except reset.
  515. */
  516. list_for_each_entry(vq, &vdev->vqs, list)
  517. vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
  518. return 0;
  519. }
  520. static void del_vq(struct virtio_pci_vq_info *info)
  521. {
  522. struct virtqueue *vq = info->vq;
  523. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  524. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  525. if (vp_dev->msix_enabled)
  526. vp_modern_queue_vector(mdev, vq->index,
  527. VIRTIO_MSI_NO_VECTOR);
  528. if (!mdev->notify_base)
  529. pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
  530. vring_del_virtqueue(vq);
  531. }
  532. static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
  533. u8 *bar, u64 *offset, u64 *len)
  534. {
  535. int pos;
  536. for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
  537. pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
  538. u8 type, cap_len, id, res_bar;
  539. u32 tmp32;
  540. u64 res_offset, res_length;
  541. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  542. cfg_type), &type);
  543. if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
  544. continue;
  545. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  546. cap_len), &cap_len);
  547. if (cap_len != sizeof(struct virtio_pci_cap64)) {
  548. dev_err(&dev->dev, "%s: shm cap with bad size offset:"
  549. " %d size: %d\n", __func__, pos, cap_len);
  550. continue;
  551. }
  552. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  553. id), &id);
  554. if (id != required_id)
  555. continue;
  556. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  557. bar), &res_bar);
  558. if (res_bar >= PCI_STD_NUM_BARS)
  559. continue;
  560. /* Type and ID match, and the BAR value isn't reserved.
  561. * Looks good.
  562. */
  563. /* Read the lower 32bit of length and offset */
  564. pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
  565. offset), &tmp32);
  566. res_offset = tmp32;
  567. pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
  568. length), &tmp32);
  569. res_length = tmp32;
  570. /* and now the top half */
  571. pci_read_config_dword(dev,
  572. pos + offsetof(struct virtio_pci_cap64,
  573. offset_hi), &tmp32);
  574. res_offset |= ((u64)tmp32) << 32;
  575. pci_read_config_dword(dev,
  576. pos + offsetof(struct virtio_pci_cap64,
  577. length_hi), &tmp32);
  578. res_length |= ((u64)tmp32) << 32;
  579. *bar = res_bar;
  580. *offset = res_offset;
  581. *len = res_length;
  582. return pos;
  583. }
  584. return 0;
  585. }
  586. static bool vp_get_shm_region(struct virtio_device *vdev,
  587. struct virtio_shm_region *region, u8 id)
  588. {
  589. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  590. struct pci_dev *pci_dev = vp_dev->pci_dev;
  591. u8 bar;
  592. u64 offset, len;
  593. phys_addr_t phys_addr;
  594. size_t bar_len;
  595. if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
  596. return false;
  597. phys_addr = pci_resource_start(pci_dev, bar);
  598. bar_len = pci_resource_len(pci_dev, bar);
  599. if ((offset + len) < offset) {
  600. dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
  601. __func__);
  602. return false;
  603. }
  604. if (offset + len > bar_len) {
  605. dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
  606. __func__);
  607. return false;
  608. }
  609. region->len = len;
  610. region->addr = (u64) phys_addr + offset;
  611. return true;
  612. }
  613. static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
  614. .get = NULL,
  615. .set = NULL,
  616. .generation = vp_generation,
  617. .get_status = vp_get_status,
  618. .set_status = vp_set_status,
  619. .reset = vp_reset,
  620. .find_vqs = vp_modern_find_vqs,
  621. .del_vqs = vp_del_vqs,
  622. .synchronize_cbs = vp_synchronize_vectors,
  623. .get_features = vp_get_features,
  624. .finalize_features = vp_finalize_features,
  625. .bus_name = vp_bus_name,
  626. .set_vq_affinity = vp_set_vq_affinity,
  627. .get_vq_affinity = vp_get_vq_affinity,
  628. .get_shm_region = vp_get_shm_region,
  629. .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
  630. .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
  631. };
  632. static const struct virtio_config_ops virtio_pci_config_ops = {
  633. .get = vp_get,
  634. .set = vp_set,
  635. .generation = vp_generation,
  636. .get_status = vp_get_status,
  637. .set_status = vp_set_status,
  638. .reset = vp_reset,
  639. .find_vqs = vp_modern_find_vqs,
  640. .del_vqs = vp_del_vqs,
  641. .synchronize_cbs = vp_synchronize_vectors,
  642. .get_features = vp_get_features,
  643. .finalize_features = vp_finalize_features,
  644. .bus_name = vp_bus_name,
  645. .set_vq_affinity = vp_set_vq_affinity,
  646. .get_vq_affinity = vp_get_vq_affinity,
  647. .get_shm_region = vp_get_shm_region,
  648. .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
  649. .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
  650. };
  651. /* the PCI probing function */
  652. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  653. {
  654. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  655. struct pci_dev *pci_dev = vp_dev->pci_dev;
  656. int err;
  657. mdev->pci_dev = pci_dev;
  658. err = vp_modern_probe(mdev);
  659. if (err)
  660. return err;
  661. if (mdev->device)
  662. vp_dev->vdev.config = &virtio_pci_config_ops;
  663. else
  664. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  665. vp_dev->config_vector = vp_config_vector;
  666. vp_dev->setup_vq = setup_vq;
  667. vp_dev->del_vq = del_vq;
  668. vp_dev->avq_index = vp_avq_index;
  669. vp_dev->isr = mdev->isr;
  670. vp_dev->vdev.id = mdev->id;
  671. spin_lock_init(&vp_dev->admin_vq.lock);
  672. return 0;
  673. }
  674. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  675. {
  676. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  677. vp_modern_remove(mdev);
  678. }