vp_vdpa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vDPA bridge driver for modern virtio-pci device
  4. *
  5. * Copyright (c) 2020, Red Hat Inc. All rights reserved.
  6. * Author: Jason Wang <jasowang@redhat.com>
  7. *
  8. * Based on virtio_pci_modern.c.
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/vdpa.h>
  14. #include <linux/virtio.h>
  15. #include <linux/virtio_config.h>
  16. #include <linux/virtio_ring.h>
  17. #include <linux/virtio_pci.h>
  18. #include <linux/virtio_pci_modern.h>
  19. #include <uapi/linux/vdpa.h>
  20. #define VP_VDPA_QUEUE_MAX 256
  21. #define VP_VDPA_DRIVER_NAME "vp_vdpa"
  22. #define VP_VDPA_NAME_SIZE 256
  23. struct vp_vring {
  24. void __iomem *notify;
  25. char msix_name[VP_VDPA_NAME_SIZE];
  26. struct vdpa_callback cb;
  27. resource_size_t notify_pa;
  28. int irq;
  29. };
  30. struct vp_vdpa {
  31. struct vdpa_device vdpa;
  32. struct virtio_pci_modern_device *mdev;
  33. struct vp_vring *vring;
  34. struct vdpa_callback config_cb;
  35. u64 device_features;
  36. char msix_name[VP_VDPA_NAME_SIZE];
  37. int config_irq;
  38. int queues;
  39. int vectors;
  40. };
  41. struct vp_vdpa_mgmtdev {
  42. struct vdpa_mgmt_dev mgtdev;
  43. struct virtio_pci_modern_device *mdev;
  44. struct vp_vdpa *vp_vdpa;
  45. };
  46. static struct vp_vdpa *vdpa_to_vp(struct vdpa_device *vdpa)
  47. {
  48. return container_of(vdpa, struct vp_vdpa, vdpa);
  49. }
  50. static struct virtio_pci_modern_device *vdpa_to_mdev(struct vdpa_device *vdpa)
  51. {
  52. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  53. return vp_vdpa->mdev;
  54. }
  55. static struct virtio_pci_modern_device *vp_vdpa_to_mdev(struct vp_vdpa *vp_vdpa)
  56. {
  57. return vp_vdpa->mdev;
  58. }
  59. static u64 vp_vdpa_get_device_features(struct vdpa_device *vdpa)
  60. {
  61. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  62. return vp_vdpa->device_features;
  63. }
  64. static int vp_vdpa_set_driver_features(struct vdpa_device *vdpa, u64 features)
  65. {
  66. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  67. vp_modern_set_features(mdev, features);
  68. return 0;
  69. }
  70. static u64 vp_vdpa_get_driver_features(struct vdpa_device *vdpa)
  71. {
  72. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  73. return vp_modern_get_driver_features(mdev);
  74. }
  75. static u8 vp_vdpa_get_status(struct vdpa_device *vdpa)
  76. {
  77. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  78. return vp_modern_get_status(mdev);
  79. }
  80. static int vp_vdpa_get_vq_irq(struct vdpa_device *vdpa, u16 idx)
  81. {
  82. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  83. int irq = vp_vdpa->vring[idx].irq;
  84. if (irq == VIRTIO_MSI_NO_VECTOR)
  85. return -EINVAL;
  86. return irq;
  87. }
  88. static void vp_vdpa_free_irq(struct vp_vdpa *vp_vdpa)
  89. {
  90. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  91. struct pci_dev *pdev = mdev->pci_dev;
  92. int i;
  93. for (i = 0; i < vp_vdpa->queues; i++) {
  94. if (vp_vdpa->vring[i].irq != VIRTIO_MSI_NO_VECTOR) {
  95. vp_modern_queue_vector(mdev, i, VIRTIO_MSI_NO_VECTOR);
  96. devm_free_irq(&pdev->dev, vp_vdpa->vring[i].irq,
  97. &vp_vdpa->vring[i]);
  98. vp_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
  99. }
  100. }
  101. if (vp_vdpa->config_irq != VIRTIO_MSI_NO_VECTOR) {
  102. vp_modern_config_vector(mdev, VIRTIO_MSI_NO_VECTOR);
  103. devm_free_irq(&pdev->dev, vp_vdpa->config_irq, vp_vdpa);
  104. vp_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
  105. }
  106. if (vp_vdpa->vectors) {
  107. pci_free_irq_vectors(pdev);
  108. vp_vdpa->vectors = 0;
  109. }
  110. }
  111. static irqreturn_t vp_vdpa_vq_handler(int irq, void *arg)
  112. {
  113. struct vp_vring *vring = arg;
  114. if (vring->cb.callback)
  115. return vring->cb.callback(vring->cb.private);
  116. return IRQ_HANDLED;
  117. }
  118. static irqreturn_t vp_vdpa_config_handler(int irq, void *arg)
  119. {
  120. struct vp_vdpa *vp_vdpa = arg;
  121. if (vp_vdpa->config_cb.callback)
  122. return vp_vdpa->config_cb.callback(vp_vdpa->config_cb.private);
  123. return IRQ_HANDLED;
  124. }
  125. static int vp_vdpa_request_irq(struct vp_vdpa *vp_vdpa)
  126. {
  127. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  128. struct pci_dev *pdev = mdev->pci_dev;
  129. int i, ret, irq;
  130. int queues = vp_vdpa->queues;
  131. int vectors = 1;
  132. int msix_vec = 0;
  133. for (i = 0; i < queues; i++) {
  134. if (vp_vdpa->vring[i].cb.callback)
  135. vectors++;
  136. }
  137. ret = pci_alloc_irq_vectors(pdev, vectors, vectors, PCI_IRQ_MSIX);
  138. if (ret != vectors) {
  139. dev_err(&pdev->dev,
  140. "vp_vdpa: fail to allocate irq vectors want %d but %d\n",
  141. vectors, ret);
  142. return ret;
  143. }
  144. vp_vdpa->vectors = vectors;
  145. for (i = 0; i < queues; i++) {
  146. if (!vp_vdpa->vring[i].cb.callback)
  147. continue;
  148. snprintf(vp_vdpa->vring[i].msix_name, VP_VDPA_NAME_SIZE,
  149. "vp-vdpa[%s]-%d\n", pci_name(pdev), i);
  150. irq = pci_irq_vector(pdev, msix_vec);
  151. ret = devm_request_irq(&pdev->dev, irq,
  152. vp_vdpa_vq_handler,
  153. 0, vp_vdpa->vring[i].msix_name,
  154. &vp_vdpa->vring[i]);
  155. if (ret) {
  156. dev_err(&pdev->dev,
  157. "vp_vdpa: fail to request irq for vq %d\n", i);
  158. goto err;
  159. }
  160. vp_modern_queue_vector(mdev, i, msix_vec);
  161. vp_vdpa->vring[i].irq = irq;
  162. msix_vec++;
  163. }
  164. snprintf(vp_vdpa->msix_name, VP_VDPA_NAME_SIZE, "vp-vdpa[%s]-config\n",
  165. pci_name(pdev));
  166. irq = pci_irq_vector(pdev, msix_vec);
  167. ret = devm_request_irq(&pdev->dev, irq, vp_vdpa_config_handler, 0,
  168. vp_vdpa->msix_name, vp_vdpa);
  169. if (ret) {
  170. dev_err(&pdev->dev,
  171. "vp_vdpa: fail to request irq for config: %d\n", ret);
  172. goto err;
  173. }
  174. vp_modern_config_vector(mdev, msix_vec);
  175. vp_vdpa->config_irq = irq;
  176. return 0;
  177. err:
  178. vp_vdpa_free_irq(vp_vdpa);
  179. return ret;
  180. }
  181. static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
  182. {
  183. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  184. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  185. u8 s = vp_vdpa_get_status(vdpa);
  186. if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
  187. !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
  188. if (vp_vdpa_request_irq(vp_vdpa)) {
  189. WARN_ON(1);
  190. return;
  191. }
  192. }
  193. vp_modern_set_status(mdev, status);
  194. }
  195. static int vp_vdpa_reset(struct vdpa_device *vdpa)
  196. {
  197. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  198. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  199. u8 s = vp_vdpa_get_status(vdpa);
  200. vp_modern_set_status(mdev, 0);
  201. if (s & VIRTIO_CONFIG_S_DRIVER_OK)
  202. vp_vdpa_free_irq(vp_vdpa);
  203. return 0;
  204. }
  205. static u16 vp_vdpa_get_vq_num_max(struct vdpa_device *vdpa)
  206. {
  207. return VP_VDPA_QUEUE_MAX;
  208. }
  209. static int vp_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 qid,
  210. struct vdpa_vq_state *state)
  211. {
  212. /* Note that this is not supported by virtio specification, so
  213. * we return -EOPNOTSUPP here. This means we can't support live
  214. * migration, vhost device start/stop.
  215. */
  216. return -EOPNOTSUPP;
  217. }
  218. static int vp_vdpa_set_vq_state_split(struct vdpa_device *vdpa,
  219. const struct vdpa_vq_state *state)
  220. {
  221. const struct vdpa_vq_state_split *split = &state->split;
  222. if (split->avail_index == 0)
  223. return 0;
  224. return -EOPNOTSUPP;
  225. }
  226. static int vp_vdpa_set_vq_state_packed(struct vdpa_device *vdpa,
  227. const struct vdpa_vq_state *state)
  228. {
  229. const struct vdpa_vq_state_packed *packed = &state->packed;
  230. if (packed->last_avail_counter == 1 &&
  231. packed->last_avail_idx == 0 &&
  232. packed->last_used_counter == 1 &&
  233. packed->last_used_idx == 0)
  234. return 0;
  235. return -EOPNOTSUPP;
  236. }
  237. static int vp_vdpa_set_vq_state(struct vdpa_device *vdpa, u16 qid,
  238. const struct vdpa_vq_state *state)
  239. {
  240. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  241. /* Note that this is not supported by virtio specification.
  242. * But if the state is by chance equal to the device initial
  243. * state, we can let it go.
  244. */
  245. if ((vp_modern_get_status(mdev) & VIRTIO_CONFIG_S_FEATURES_OK) &&
  246. !vp_modern_get_queue_enable(mdev, qid)) {
  247. if (vp_modern_get_driver_features(mdev) &
  248. BIT_ULL(VIRTIO_F_RING_PACKED))
  249. return vp_vdpa_set_vq_state_packed(vdpa, state);
  250. else
  251. return vp_vdpa_set_vq_state_split(vdpa, state);
  252. }
  253. return -EOPNOTSUPP;
  254. }
  255. static void vp_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 qid,
  256. struct vdpa_callback *cb)
  257. {
  258. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  259. vp_vdpa->vring[qid].cb = *cb;
  260. }
  261. static void vp_vdpa_set_vq_ready(struct vdpa_device *vdpa,
  262. u16 qid, bool ready)
  263. {
  264. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  265. vp_modern_set_queue_enable(mdev, qid, ready);
  266. }
  267. static bool vp_vdpa_get_vq_ready(struct vdpa_device *vdpa, u16 qid)
  268. {
  269. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  270. return vp_modern_get_queue_enable(mdev, qid);
  271. }
  272. static void vp_vdpa_set_vq_num(struct vdpa_device *vdpa, u16 qid,
  273. u32 num)
  274. {
  275. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  276. vp_modern_set_queue_size(mdev, qid, num);
  277. }
  278. static u16 vp_vdpa_get_vq_size(struct vdpa_device *vdpa, u16 qid)
  279. {
  280. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  281. return vp_modern_get_queue_size(mdev, qid);
  282. }
  283. static int vp_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 qid,
  284. u64 desc_area, u64 driver_area,
  285. u64 device_area)
  286. {
  287. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  288. vp_modern_queue_address(mdev, qid, desc_area,
  289. driver_area, device_area);
  290. return 0;
  291. }
  292. static void vp_vdpa_kick_vq(struct vdpa_device *vdpa, u16 qid)
  293. {
  294. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  295. vp_iowrite16(qid, vp_vdpa->vring[qid].notify);
  296. }
  297. static u32 vp_vdpa_get_generation(struct vdpa_device *vdpa)
  298. {
  299. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  300. return vp_modern_generation(mdev);
  301. }
  302. static u32 vp_vdpa_get_device_id(struct vdpa_device *vdpa)
  303. {
  304. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  305. return mdev->id.device;
  306. }
  307. static u32 vp_vdpa_get_vendor_id(struct vdpa_device *vdpa)
  308. {
  309. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  310. return mdev->id.vendor;
  311. }
  312. static u32 vp_vdpa_get_vq_align(struct vdpa_device *vdpa)
  313. {
  314. return PAGE_SIZE;
  315. }
  316. static size_t vp_vdpa_get_config_size(struct vdpa_device *vdpa)
  317. {
  318. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  319. return mdev->device_len;
  320. }
  321. static void vp_vdpa_get_config(struct vdpa_device *vdpa,
  322. unsigned int offset,
  323. void *buf, unsigned int len)
  324. {
  325. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  326. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  327. u8 old, new;
  328. u8 *p;
  329. int i;
  330. do {
  331. old = vp_ioread8(&mdev->common->config_generation);
  332. p = buf;
  333. for (i = 0; i < len; i++)
  334. *p++ = vp_ioread8(mdev->device + offset + i);
  335. new = vp_ioread8(&mdev->common->config_generation);
  336. } while (old != new);
  337. }
  338. static void vp_vdpa_set_config(struct vdpa_device *vdpa,
  339. unsigned int offset, const void *buf,
  340. unsigned int len)
  341. {
  342. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  343. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  344. const u8 *p = buf;
  345. int i;
  346. for (i = 0; i < len; i++)
  347. vp_iowrite8(*p++, mdev->device + offset + i);
  348. }
  349. static void vp_vdpa_set_config_cb(struct vdpa_device *vdpa,
  350. struct vdpa_callback *cb)
  351. {
  352. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  353. vp_vdpa->config_cb = *cb;
  354. }
  355. static struct vdpa_notification_area
  356. vp_vdpa_get_vq_notification(struct vdpa_device *vdpa, u16 qid)
  357. {
  358. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  359. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  360. struct vdpa_notification_area notify;
  361. notify.addr = vp_vdpa->vring[qid].notify_pa;
  362. notify.size = mdev->notify_offset_multiplier;
  363. return notify;
  364. }
  365. static const struct vdpa_config_ops vp_vdpa_ops = {
  366. .get_device_features = vp_vdpa_get_device_features,
  367. .set_driver_features = vp_vdpa_set_driver_features,
  368. .get_driver_features = vp_vdpa_get_driver_features,
  369. .get_status = vp_vdpa_get_status,
  370. .set_status = vp_vdpa_set_status,
  371. .reset = vp_vdpa_reset,
  372. .get_vq_num_max = vp_vdpa_get_vq_num_max,
  373. .get_vq_state = vp_vdpa_get_vq_state,
  374. .get_vq_notification = vp_vdpa_get_vq_notification,
  375. .set_vq_state = vp_vdpa_set_vq_state,
  376. .set_vq_cb = vp_vdpa_set_vq_cb,
  377. .set_vq_ready = vp_vdpa_set_vq_ready,
  378. .get_vq_ready = vp_vdpa_get_vq_ready,
  379. .set_vq_num = vp_vdpa_set_vq_num,
  380. .get_vq_size = vp_vdpa_get_vq_size,
  381. .set_vq_address = vp_vdpa_set_vq_address,
  382. .kick_vq = vp_vdpa_kick_vq,
  383. .get_generation = vp_vdpa_get_generation,
  384. .get_device_id = vp_vdpa_get_device_id,
  385. .get_vendor_id = vp_vdpa_get_vendor_id,
  386. .get_vq_align = vp_vdpa_get_vq_align,
  387. .get_config_size = vp_vdpa_get_config_size,
  388. .get_config = vp_vdpa_get_config,
  389. .set_config = vp_vdpa_set_config,
  390. .set_config_cb = vp_vdpa_set_config_cb,
  391. .get_vq_irq = vp_vdpa_get_vq_irq,
  392. };
  393. static void vp_vdpa_free_irq_vectors(void *data)
  394. {
  395. pci_free_irq_vectors(data);
  396. }
  397. static int vp_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
  398. const struct vdpa_dev_set_config *add_config)
  399. {
  400. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev =
  401. container_of(v_mdev, struct vp_vdpa_mgmtdev, mgtdev);
  402. struct virtio_pci_modern_device *mdev = vp_vdpa_mgtdev->mdev;
  403. struct pci_dev *pdev = mdev->pci_dev;
  404. struct device *dev = &pdev->dev;
  405. struct vp_vdpa *vp_vdpa = NULL;
  406. u64 device_features;
  407. int ret, i;
  408. vp_vdpa = vdpa_alloc_device(struct vp_vdpa, vdpa,
  409. dev, &vp_vdpa_ops, 1, 1, name, false);
  410. if (IS_ERR(vp_vdpa)) {
  411. dev_err(dev, "vp_vdpa: Failed to allocate vDPA structure\n");
  412. return PTR_ERR(vp_vdpa);
  413. }
  414. vp_vdpa_mgtdev->vp_vdpa = vp_vdpa;
  415. vp_vdpa->vdpa.dma_dev = &pdev->dev;
  416. vp_vdpa->queues = vp_modern_get_num_queues(mdev);
  417. vp_vdpa->mdev = mdev;
  418. device_features = vp_modern_get_features(mdev);
  419. if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
  420. if (add_config->device_features & ~device_features) {
  421. ret = -EINVAL;
  422. dev_err(&pdev->dev, "Try to provision features "
  423. "that are not supported by the device: "
  424. "device_features 0x%llx provisioned 0x%llx\n",
  425. device_features, add_config->device_features);
  426. goto err;
  427. }
  428. device_features = add_config->device_features;
  429. }
  430. vp_vdpa->device_features = device_features;
  431. ret = devm_add_action_or_reset(dev, vp_vdpa_free_irq_vectors, pdev);
  432. if (ret) {
  433. dev_err(&pdev->dev,
  434. "Failed for adding devres for freeing irq vectors\n");
  435. goto err;
  436. }
  437. vp_vdpa->vring = devm_kcalloc(&pdev->dev, vp_vdpa->queues,
  438. sizeof(*vp_vdpa->vring),
  439. GFP_KERNEL);
  440. if (!vp_vdpa->vring) {
  441. ret = -ENOMEM;
  442. dev_err(&pdev->dev, "Fail to allocate virtqueues\n");
  443. goto err;
  444. }
  445. for (i = 0; i < vp_vdpa->queues; i++) {
  446. vp_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
  447. vp_vdpa->vring[i].notify =
  448. vp_modern_map_vq_notify(mdev, i,
  449. &vp_vdpa->vring[i].notify_pa);
  450. if (!vp_vdpa->vring[i].notify) {
  451. ret = -EINVAL;
  452. dev_warn(&pdev->dev, "Fail to map vq notify %d\n", i);
  453. goto err;
  454. }
  455. }
  456. vp_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
  457. vp_vdpa->vdpa.mdev = &vp_vdpa_mgtdev->mgtdev;
  458. ret = _vdpa_register_device(&vp_vdpa->vdpa, vp_vdpa->queues);
  459. if (ret) {
  460. dev_err(&pdev->dev, "Failed to register to vdpa bus\n");
  461. goto err;
  462. }
  463. return 0;
  464. err:
  465. put_device(&vp_vdpa->vdpa.dev);
  466. return ret;
  467. }
  468. static void vp_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev,
  469. struct vdpa_device *dev)
  470. {
  471. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev =
  472. container_of(v_mdev, struct vp_vdpa_mgmtdev, mgtdev);
  473. struct vp_vdpa *vp_vdpa = vp_vdpa_mgtdev->vp_vdpa;
  474. _vdpa_unregister_device(&vp_vdpa->vdpa);
  475. vp_vdpa_mgtdev->vp_vdpa = NULL;
  476. }
  477. static const struct vdpa_mgmtdev_ops vp_vdpa_mdev_ops = {
  478. .dev_add = vp_vdpa_dev_add,
  479. .dev_del = vp_vdpa_dev_del,
  480. };
  481. static int vp_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  482. {
  483. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev = NULL;
  484. struct vdpa_mgmt_dev *mgtdev;
  485. struct device *dev = &pdev->dev;
  486. struct virtio_pci_modern_device *mdev = NULL;
  487. struct virtio_device_id *mdev_id = NULL;
  488. int err;
  489. vp_vdpa_mgtdev = kzalloc(sizeof(*vp_vdpa_mgtdev), GFP_KERNEL);
  490. if (!vp_vdpa_mgtdev)
  491. return -ENOMEM;
  492. mgtdev = &vp_vdpa_mgtdev->mgtdev;
  493. mgtdev->ops = &vp_vdpa_mdev_ops;
  494. mgtdev->device = dev;
  495. mdev = kzalloc(sizeof(struct virtio_pci_modern_device), GFP_KERNEL);
  496. if (!mdev) {
  497. err = -ENOMEM;
  498. goto mdev_err;
  499. }
  500. /*
  501. * id_table should be a null terminated array, so allocate one additional
  502. * entry here, see vdpa_mgmtdev_get_classes().
  503. */
  504. mdev_id = kcalloc(2, sizeof(struct virtio_device_id), GFP_KERNEL);
  505. if (!mdev_id) {
  506. err = -ENOMEM;
  507. goto mdev_id_err;
  508. }
  509. vp_vdpa_mgtdev->mdev = mdev;
  510. mdev->pci_dev = pdev;
  511. err = pcim_enable_device(pdev);
  512. if (err) {
  513. goto probe_err;
  514. }
  515. err = vp_modern_probe(mdev);
  516. if (err) {
  517. dev_err(&pdev->dev, "Failed to probe modern PCI device\n");
  518. goto probe_err;
  519. }
  520. mdev_id[0].device = mdev->id.device;
  521. mdev_id[0].vendor = mdev->id.vendor;
  522. mgtdev->id_table = mdev_id;
  523. mgtdev->max_supported_vqs = vp_modern_get_num_queues(mdev);
  524. mgtdev->supported_features = vp_modern_get_features(mdev);
  525. mgtdev->config_attr_mask = (1 << VDPA_ATTR_DEV_FEATURES);
  526. pci_set_master(pdev);
  527. pci_set_drvdata(pdev, vp_vdpa_mgtdev);
  528. err = vdpa_mgmtdev_register(mgtdev);
  529. if (err) {
  530. dev_err(&pdev->dev, "Failed to register vdpa mgmtdev device\n");
  531. goto register_err;
  532. }
  533. return 0;
  534. register_err:
  535. vp_modern_remove(vp_vdpa_mgtdev->mdev);
  536. probe_err:
  537. kfree(mdev_id);
  538. mdev_id_err:
  539. kfree(mdev);
  540. mdev_err:
  541. kfree(vp_vdpa_mgtdev);
  542. return err;
  543. }
  544. static void vp_vdpa_remove(struct pci_dev *pdev)
  545. {
  546. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev = pci_get_drvdata(pdev);
  547. struct virtio_pci_modern_device *mdev = NULL;
  548. mdev = vp_vdpa_mgtdev->mdev;
  549. vdpa_mgmtdev_unregister(&vp_vdpa_mgtdev->mgtdev);
  550. vp_modern_remove(mdev);
  551. kfree(vp_vdpa_mgtdev->mgtdev.id_table);
  552. kfree(mdev);
  553. kfree(vp_vdpa_mgtdev);
  554. }
  555. static struct pci_driver vp_vdpa_driver = {
  556. .name = "vp-vdpa",
  557. .id_table = NULL, /* only dynamic ids */
  558. .probe = vp_vdpa_probe,
  559. .remove = vp_vdpa_remove,
  560. };
  561. module_pci_driver(vp_vdpa_driver);
  562. MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
  563. MODULE_DESCRIPTION("vp-vdpa");
  564. MODULE_LICENSE("GPL");
  565. MODULE_VERSION("1");