vp_vdpa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 <[email protected]>
  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 = queues + 1;
  132. ret = pci_alloc_irq_vectors(pdev, vectors, vectors, PCI_IRQ_MSIX);
  133. if (ret != vectors) {
  134. dev_err(&pdev->dev,
  135. "vp_vdpa: fail to allocate irq vectors want %d but %d\n",
  136. vectors, ret);
  137. return ret;
  138. }
  139. vp_vdpa->vectors = vectors;
  140. for (i = 0; i < queues; i++) {
  141. snprintf(vp_vdpa->vring[i].msix_name, VP_VDPA_NAME_SIZE,
  142. "vp-vdpa[%s]-%d\n", pci_name(pdev), i);
  143. irq = pci_irq_vector(pdev, i);
  144. ret = devm_request_irq(&pdev->dev, irq,
  145. vp_vdpa_vq_handler,
  146. 0, vp_vdpa->vring[i].msix_name,
  147. &vp_vdpa->vring[i]);
  148. if (ret) {
  149. dev_err(&pdev->dev,
  150. "vp_vdpa: fail to request irq for vq %d\n", i);
  151. goto err;
  152. }
  153. vp_modern_queue_vector(mdev, i, i);
  154. vp_vdpa->vring[i].irq = irq;
  155. }
  156. snprintf(vp_vdpa->msix_name, VP_VDPA_NAME_SIZE, "vp-vdpa[%s]-config\n",
  157. pci_name(pdev));
  158. irq = pci_irq_vector(pdev, queues);
  159. ret = devm_request_irq(&pdev->dev, irq, vp_vdpa_config_handler, 0,
  160. vp_vdpa->msix_name, vp_vdpa);
  161. if (ret) {
  162. dev_err(&pdev->dev,
  163. "vp_vdpa: fail to request irq for vq %d\n", i);
  164. goto err;
  165. }
  166. vp_modern_config_vector(mdev, queues);
  167. vp_vdpa->config_irq = irq;
  168. return 0;
  169. err:
  170. vp_vdpa_free_irq(vp_vdpa);
  171. return ret;
  172. }
  173. static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
  174. {
  175. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  176. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  177. u8 s = vp_vdpa_get_status(vdpa);
  178. if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
  179. !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
  180. vp_vdpa_request_irq(vp_vdpa);
  181. }
  182. vp_modern_set_status(mdev, status);
  183. }
  184. static int vp_vdpa_reset(struct vdpa_device *vdpa)
  185. {
  186. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  187. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  188. u8 s = vp_vdpa_get_status(vdpa);
  189. vp_modern_set_status(mdev, 0);
  190. if (s & VIRTIO_CONFIG_S_DRIVER_OK)
  191. vp_vdpa_free_irq(vp_vdpa);
  192. return 0;
  193. }
  194. static u16 vp_vdpa_get_vq_num_max(struct vdpa_device *vdpa)
  195. {
  196. return VP_VDPA_QUEUE_MAX;
  197. }
  198. static int vp_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 qid,
  199. struct vdpa_vq_state *state)
  200. {
  201. /* Note that this is not supported by virtio specification, so
  202. * we return -EOPNOTSUPP here. This means we can't support live
  203. * migration, vhost device start/stop.
  204. */
  205. return -EOPNOTSUPP;
  206. }
  207. static int vp_vdpa_set_vq_state_split(struct vdpa_device *vdpa,
  208. const struct vdpa_vq_state *state)
  209. {
  210. const struct vdpa_vq_state_split *split = &state->split;
  211. if (split->avail_index == 0)
  212. return 0;
  213. return -EOPNOTSUPP;
  214. }
  215. static int vp_vdpa_set_vq_state_packed(struct vdpa_device *vdpa,
  216. const struct vdpa_vq_state *state)
  217. {
  218. const struct vdpa_vq_state_packed *packed = &state->packed;
  219. if (packed->last_avail_counter == 1 &&
  220. packed->last_avail_idx == 0 &&
  221. packed->last_used_counter == 1 &&
  222. packed->last_used_idx == 0)
  223. return 0;
  224. return -EOPNOTSUPP;
  225. }
  226. static int vp_vdpa_set_vq_state(struct vdpa_device *vdpa, u16 qid,
  227. const struct vdpa_vq_state *state)
  228. {
  229. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  230. /* Note that this is not supported by virtio specification.
  231. * But if the state is by chance equal to the device initial
  232. * state, we can let it go.
  233. */
  234. if ((vp_modern_get_status(mdev) & VIRTIO_CONFIG_S_FEATURES_OK) &&
  235. !vp_modern_get_queue_enable(mdev, qid)) {
  236. if (vp_modern_get_driver_features(mdev) &
  237. BIT_ULL(VIRTIO_F_RING_PACKED))
  238. return vp_vdpa_set_vq_state_packed(vdpa, state);
  239. else
  240. return vp_vdpa_set_vq_state_split(vdpa, state);
  241. }
  242. return -EOPNOTSUPP;
  243. }
  244. static void vp_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 qid,
  245. struct vdpa_callback *cb)
  246. {
  247. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  248. vp_vdpa->vring[qid].cb = *cb;
  249. }
  250. static void vp_vdpa_set_vq_ready(struct vdpa_device *vdpa,
  251. u16 qid, bool ready)
  252. {
  253. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  254. vp_modern_set_queue_enable(mdev, qid, ready);
  255. }
  256. static bool vp_vdpa_get_vq_ready(struct vdpa_device *vdpa, u16 qid)
  257. {
  258. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  259. return vp_modern_get_queue_enable(mdev, qid);
  260. }
  261. static void vp_vdpa_set_vq_num(struct vdpa_device *vdpa, u16 qid,
  262. u32 num)
  263. {
  264. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  265. vp_modern_set_queue_size(mdev, qid, num);
  266. }
  267. static int vp_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 qid,
  268. u64 desc_area, u64 driver_area,
  269. u64 device_area)
  270. {
  271. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  272. vp_modern_queue_address(mdev, qid, desc_area,
  273. driver_area, device_area);
  274. return 0;
  275. }
  276. static void vp_vdpa_kick_vq(struct vdpa_device *vdpa, u16 qid)
  277. {
  278. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  279. vp_iowrite16(qid, vp_vdpa->vring[qid].notify);
  280. }
  281. static u32 vp_vdpa_get_generation(struct vdpa_device *vdpa)
  282. {
  283. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  284. return vp_modern_generation(mdev);
  285. }
  286. static u32 vp_vdpa_get_device_id(struct vdpa_device *vdpa)
  287. {
  288. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  289. return mdev->id.device;
  290. }
  291. static u32 vp_vdpa_get_vendor_id(struct vdpa_device *vdpa)
  292. {
  293. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  294. return mdev->id.vendor;
  295. }
  296. static u32 vp_vdpa_get_vq_align(struct vdpa_device *vdpa)
  297. {
  298. return PAGE_SIZE;
  299. }
  300. static size_t vp_vdpa_get_config_size(struct vdpa_device *vdpa)
  301. {
  302. struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);
  303. return mdev->device_len;
  304. }
  305. static void vp_vdpa_get_config(struct vdpa_device *vdpa,
  306. unsigned int offset,
  307. void *buf, unsigned int len)
  308. {
  309. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  310. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  311. u8 old, new;
  312. u8 *p;
  313. int i;
  314. do {
  315. old = vp_ioread8(&mdev->common->config_generation);
  316. p = buf;
  317. for (i = 0; i < len; i++)
  318. *p++ = vp_ioread8(mdev->device + offset + i);
  319. new = vp_ioread8(&mdev->common->config_generation);
  320. } while (old != new);
  321. }
  322. static void vp_vdpa_set_config(struct vdpa_device *vdpa,
  323. unsigned int offset, const void *buf,
  324. unsigned int len)
  325. {
  326. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  327. struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
  328. const u8 *p = buf;
  329. int i;
  330. for (i = 0; i < len; i++)
  331. vp_iowrite8(*p++, mdev->device + offset + i);
  332. }
  333. static void vp_vdpa_set_config_cb(struct vdpa_device *vdpa,
  334. struct vdpa_callback *cb)
  335. {
  336. struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
  337. vp_vdpa->config_cb = *cb;
  338. }
  339. static struct vdpa_notification_area
  340. vp_vdpa_get_vq_notification(struct vdpa_device *vdpa, u16 qid)
  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. struct vdpa_notification_area notify;
  345. notify.addr = vp_vdpa->vring[qid].notify_pa;
  346. notify.size = mdev->notify_offset_multiplier;
  347. return notify;
  348. }
  349. static const struct vdpa_config_ops vp_vdpa_ops = {
  350. .get_device_features = vp_vdpa_get_device_features,
  351. .set_driver_features = vp_vdpa_set_driver_features,
  352. .get_driver_features = vp_vdpa_get_driver_features,
  353. .get_status = vp_vdpa_get_status,
  354. .set_status = vp_vdpa_set_status,
  355. .reset = vp_vdpa_reset,
  356. .get_vq_num_max = vp_vdpa_get_vq_num_max,
  357. .get_vq_state = vp_vdpa_get_vq_state,
  358. .get_vq_notification = vp_vdpa_get_vq_notification,
  359. .set_vq_state = vp_vdpa_set_vq_state,
  360. .set_vq_cb = vp_vdpa_set_vq_cb,
  361. .set_vq_ready = vp_vdpa_set_vq_ready,
  362. .get_vq_ready = vp_vdpa_get_vq_ready,
  363. .set_vq_num = vp_vdpa_set_vq_num,
  364. .set_vq_address = vp_vdpa_set_vq_address,
  365. .kick_vq = vp_vdpa_kick_vq,
  366. .get_generation = vp_vdpa_get_generation,
  367. .get_device_id = vp_vdpa_get_device_id,
  368. .get_vendor_id = vp_vdpa_get_vendor_id,
  369. .get_vq_align = vp_vdpa_get_vq_align,
  370. .get_config_size = vp_vdpa_get_config_size,
  371. .get_config = vp_vdpa_get_config,
  372. .set_config = vp_vdpa_set_config,
  373. .set_config_cb = vp_vdpa_set_config_cb,
  374. .get_vq_irq = vp_vdpa_get_vq_irq,
  375. };
  376. static void vp_vdpa_free_irq_vectors(void *data)
  377. {
  378. pci_free_irq_vectors(data);
  379. }
  380. static int vp_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
  381. const struct vdpa_dev_set_config *add_config)
  382. {
  383. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev =
  384. container_of(v_mdev, struct vp_vdpa_mgmtdev, mgtdev);
  385. struct virtio_pci_modern_device *mdev = vp_vdpa_mgtdev->mdev;
  386. struct pci_dev *pdev = mdev->pci_dev;
  387. struct device *dev = &pdev->dev;
  388. struct vp_vdpa *vp_vdpa = NULL;
  389. u64 device_features;
  390. int ret, i;
  391. vp_vdpa = vdpa_alloc_device(struct vp_vdpa, vdpa,
  392. dev, &vp_vdpa_ops, 1, 1, name, false);
  393. if (IS_ERR(vp_vdpa)) {
  394. dev_err(dev, "vp_vdpa: Failed to allocate vDPA structure\n");
  395. return PTR_ERR(vp_vdpa);
  396. }
  397. vp_vdpa_mgtdev->vp_vdpa = vp_vdpa;
  398. vp_vdpa->vdpa.dma_dev = &pdev->dev;
  399. vp_vdpa->queues = vp_modern_get_num_queues(mdev);
  400. vp_vdpa->mdev = mdev;
  401. device_features = vp_modern_get_features(mdev);
  402. if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
  403. if (add_config->device_features & ~device_features) {
  404. ret = -EINVAL;
  405. dev_err(&pdev->dev, "Try to provision features "
  406. "that are not supported by the device: "
  407. "device_features 0x%llx provisioned 0x%llx\n",
  408. device_features, add_config->device_features);
  409. goto err;
  410. }
  411. device_features = add_config->device_features;
  412. }
  413. vp_vdpa->device_features = device_features;
  414. ret = devm_add_action_or_reset(dev, vp_vdpa_free_irq_vectors, pdev);
  415. if (ret) {
  416. dev_err(&pdev->dev,
  417. "Failed for adding devres for freeing irq vectors\n");
  418. goto err;
  419. }
  420. vp_vdpa->vring = devm_kcalloc(&pdev->dev, vp_vdpa->queues,
  421. sizeof(*vp_vdpa->vring),
  422. GFP_KERNEL);
  423. if (!vp_vdpa->vring) {
  424. ret = -ENOMEM;
  425. dev_err(&pdev->dev, "Fail to allocate virtqueues\n");
  426. goto err;
  427. }
  428. for (i = 0; i < vp_vdpa->queues; i++) {
  429. vp_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
  430. vp_vdpa->vring[i].notify =
  431. vp_modern_map_vq_notify(mdev, i,
  432. &vp_vdpa->vring[i].notify_pa);
  433. if (!vp_vdpa->vring[i].notify) {
  434. ret = -EINVAL;
  435. dev_warn(&pdev->dev, "Fail to map vq notify %d\n", i);
  436. goto err;
  437. }
  438. }
  439. vp_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
  440. vp_vdpa->vdpa.mdev = &vp_vdpa_mgtdev->mgtdev;
  441. ret = _vdpa_register_device(&vp_vdpa->vdpa, vp_vdpa->queues);
  442. if (ret) {
  443. dev_err(&pdev->dev, "Failed to register to vdpa bus\n");
  444. goto err;
  445. }
  446. return 0;
  447. err:
  448. put_device(&vp_vdpa->vdpa.dev);
  449. return ret;
  450. }
  451. static void vp_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev,
  452. struct vdpa_device *dev)
  453. {
  454. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev =
  455. container_of(v_mdev, struct vp_vdpa_mgmtdev, mgtdev);
  456. struct vp_vdpa *vp_vdpa = vp_vdpa_mgtdev->vp_vdpa;
  457. _vdpa_unregister_device(&vp_vdpa->vdpa);
  458. vp_vdpa_mgtdev->vp_vdpa = NULL;
  459. }
  460. static const struct vdpa_mgmtdev_ops vp_vdpa_mdev_ops = {
  461. .dev_add = vp_vdpa_dev_add,
  462. .dev_del = vp_vdpa_dev_del,
  463. };
  464. static int vp_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  465. {
  466. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev = NULL;
  467. struct vdpa_mgmt_dev *mgtdev;
  468. struct device *dev = &pdev->dev;
  469. struct virtio_pci_modern_device *mdev = NULL;
  470. struct virtio_device_id *mdev_id = NULL;
  471. int err;
  472. vp_vdpa_mgtdev = kzalloc(sizeof(*vp_vdpa_mgtdev), GFP_KERNEL);
  473. if (!vp_vdpa_mgtdev)
  474. return -ENOMEM;
  475. mgtdev = &vp_vdpa_mgtdev->mgtdev;
  476. mgtdev->ops = &vp_vdpa_mdev_ops;
  477. mgtdev->device = dev;
  478. mdev = kzalloc(sizeof(struct virtio_pci_modern_device), GFP_KERNEL);
  479. if (!mdev) {
  480. err = -ENOMEM;
  481. goto mdev_err;
  482. }
  483. mdev_id = kzalloc(sizeof(struct virtio_device_id), GFP_KERNEL);
  484. if (!mdev_id) {
  485. err = -ENOMEM;
  486. goto mdev_id_err;
  487. }
  488. vp_vdpa_mgtdev->mdev = mdev;
  489. mdev->pci_dev = pdev;
  490. err = pcim_enable_device(pdev);
  491. if (err) {
  492. goto probe_err;
  493. }
  494. err = vp_modern_probe(mdev);
  495. if (err) {
  496. dev_err(&pdev->dev, "Failed to probe modern PCI device\n");
  497. goto probe_err;
  498. }
  499. mdev_id->device = mdev->id.device;
  500. mdev_id->vendor = mdev->id.vendor;
  501. mgtdev->id_table = mdev_id;
  502. mgtdev->max_supported_vqs = vp_modern_get_num_queues(mdev);
  503. mgtdev->supported_features = vp_modern_get_features(mdev);
  504. mgtdev->config_attr_mask = (1 << VDPA_ATTR_DEV_FEATURES);
  505. pci_set_master(pdev);
  506. pci_set_drvdata(pdev, vp_vdpa_mgtdev);
  507. err = vdpa_mgmtdev_register(mgtdev);
  508. if (err) {
  509. dev_err(&pdev->dev, "Failed to register vdpa mgmtdev device\n");
  510. goto register_err;
  511. }
  512. return 0;
  513. register_err:
  514. vp_modern_remove(vp_vdpa_mgtdev->mdev);
  515. probe_err:
  516. kfree(mdev_id);
  517. mdev_id_err:
  518. kfree(mdev);
  519. mdev_err:
  520. kfree(vp_vdpa_mgtdev);
  521. return err;
  522. }
  523. static void vp_vdpa_remove(struct pci_dev *pdev)
  524. {
  525. struct vp_vdpa_mgmtdev *vp_vdpa_mgtdev = pci_get_drvdata(pdev);
  526. struct virtio_pci_modern_device *mdev = NULL;
  527. mdev = vp_vdpa_mgtdev->mdev;
  528. vdpa_mgmtdev_unregister(&vp_vdpa_mgtdev->mgtdev);
  529. vp_modern_remove(mdev);
  530. kfree(vp_vdpa_mgtdev->mgtdev.id_table);
  531. kfree(mdev);
  532. kfree(vp_vdpa_mgtdev);
  533. }
  534. static struct pci_driver vp_vdpa_driver = {
  535. .name = "vp-vdpa",
  536. .id_table = NULL, /* only dynamic ids */
  537. .probe = vp_vdpa_probe,
  538. .remove = vp_vdpa_remove,
  539. };
  540. module_pci_driver(vp_vdpa_driver);
  541. MODULE_AUTHOR("Jason Wang <[email protected]>");
  542. MODULE_DESCRIPTION("vp-vdpa");
  543. MODULE_LICENSE("GPL");
  544. MODULE_VERSION("1");