vdpa_dev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2023 Advanced Micro Devices, Inc */
  3. #include <linux/pci.h>
  4. #include <linux/vdpa.h>
  5. #include <uapi/linux/vdpa.h>
  6. #include <linux/virtio_pci_modern.h>
  7. #include <linux/pds/pds_common.h>
  8. #include <linux/pds/pds_core_if.h>
  9. #include <linux/pds/pds_adminq.h>
  10. #include <linux/pds/pds_auxbus.h>
  11. #include "vdpa_dev.h"
  12. #include "aux_drv.h"
  13. #include "cmds.h"
  14. #include "debugfs.h"
  15. static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev);
  16. static struct pds_vdpa_device *vdpa_to_pdsv(struct vdpa_device *vdpa_dev)
  17. {
  18. return container_of(vdpa_dev, struct pds_vdpa_device, vdpa_dev);
  19. }
  20. static int pds_vdpa_notify_handler(struct notifier_block *nb,
  21. unsigned long ecode,
  22. void *data)
  23. {
  24. struct pds_vdpa_device *pdsv = container_of(nb, struct pds_vdpa_device, nb);
  25. struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
  26. dev_dbg(dev, "%s: event code %lu\n", __func__, ecode);
  27. if (ecode == PDS_EVENT_RESET || ecode == PDS_EVENT_LINK_CHANGE) {
  28. if (pdsv->config_cb.callback)
  29. pdsv->config_cb.callback(pdsv->config_cb.private);
  30. }
  31. return 0;
  32. }
  33. static int pds_vdpa_register_event_handler(struct pds_vdpa_device *pdsv)
  34. {
  35. struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
  36. struct notifier_block *nb = &pdsv->nb;
  37. int err;
  38. if (!nb->notifier_call) {
  39. nb->notifier_call = pds_vdpa_notify_handler;
  40. err = pdsc_register_notify(nb);
  41. if (err) {
  42. nb->notifier_call = NULL;
  43. dev_err(dev, "failed to register pds event handler: %pe\n",
  44. ERR_PTR(err));
  45. return -EINVAL;
  46. }
  47. dev_dbg(dev, "pds event handler registered\n");
  48. }
  49. return 0;
  50. }
  51. static void pds_vdpa_unregister_event_handler(struct pds_vdpa_device *pdsv)
  52. {
  53. if (pdsv->nb.notifier_call) {
  54. pdsc_unregister_notify(&pdsv->nb);
  55. pdsv->nb.notifier_call = NULL;
  56. }
  57. }
  58. static int pds_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid,
  59. u64 desc_addr, u64 driver_addr, u64 device_addr)
  60. {
  61. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  62. pdsv->vqs[qid].desc_addr = desc_addr;
  63. pdsv->vqs[qid].avail_addr = driver_addr;
  64. pdsv->vqs[qid].used_addr = device_addr;
  65. return 0;
  66. }
  67. static void pds_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, u32 num)
  68. {
  69. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  70. pdsv->vqs[qid].q_len = num;
  71. }
  72. static void pds_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid)
  73. {
  74. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  75. iowrite16(qid, pdsv->vqs[qid].notify);
  76. }
  77. static void pds_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid,
  78. struct vdpa_callback *cb)
  79. {
  80. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  81. pdsv->vqs[qid].event_cb = *cb;
  82. }
  83. static irqreturn_t pds_vdpa_isr(int irq, void *data)
  84. {
  85. struct pds_vdpa_vq_info *vq;
  86. vq = data;
  87. if (vq->event_cb.callback)
  88. vq->event_cb.callback(vq->event_cb.private);
  89. return IRQ_HANDLED;
  90. }
  91. static void pds_vdpa_release_irq(struct pds_vdpa_device *pdsv, int qid)
  92. {
  93. if (pdsv->vqs[qid].irq == VIRTIO_MSI_NO_VECTOR)
  94. return;
  95. free_irq(pdsv->vqs[qid].irq, &pdsv->vqs[qid]);
  96. pdsv->vqs[qid].irq = VIRTIO_MSI_NO_VECTOR;
  97. }
  98. static void pds_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, u16 qid, bool ready)
  99. {
  100. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  101. struct device *dev = &pdsv->vdpa_dev.dev;
  102. u64 driver_features;
  103. u16 invert_idx = 0;
  104. int err;
  105. dev_dbg(dev, "%s: qid %d ready %d => %d\n",
  106. __func__, qid, pdsv->vqs[qid].ready, ready);
  107. if (ready == pdsv->vqs[qid].ready)
  108. return;
  109. driver_features = pds_vdpa_get_driver_features(vdpa_dev);
  110. if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED))
  111. invert_idx = PDS_VDPA_PACKED_INVERT_IDX;
  112. if (ready) {
  113. /* Pass vq setup info to DSC using adminq to gather up and
  114. * send all info at once so FW can do its full set up in
  115. * one easy operation
  116. */
  117. err = pds_vdpa_cmd_init_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]);
  118. if (err) {
  119. dev_err(dev, "Failed to init vq %d: %pe\n",
  120. qid, ERR_PTR(err));
  121. ready = false;
  122. }
  123. } else {
  124. err = pds_vdpa_cmd_reset_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]);
  125. if (err)
  126. dev_err(dev, "%s: reset_vq failed qid %d: %pe\n",
  127. __func__, qid, ERR_PTR(err));
  128. }
  129. pdsv->vqs[qid].ready = ready;
  130. }
  131. static bool pds_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid)
  132. {
  133. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  134. return pdsv->vqs[qid].ready;
  135. }
  136. static int pds_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
  137. const struct vdpa_vq_state *state)
  138. {
  139. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  140. struct pds_auxiliary_dev *padev = pdsv->vdpa_aux->padev;
  141. struct device *dev = &padev->aux_dev.dev;
  142. u64 driver_features;
  143. u16 avail;
  144. u16 used;
  145. if (pdsv->vqs[qid].ready) {
  146. dev_err(dev, "Setting device position is denied while vq is enabled\n");
  147. return -EINVAL;
  148. }
  149. driver_features = pds_vdpa_get_driver_features(vdpa_dev);
  150. if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) {
  151. avail = state->packed.last_avail_idx |
  152. (state->packed.last_avail_counter << 15);
  153. used = state->packed.last_used_idx |
  154. (state->packed.last_used_counter << 15);
  155. /* The avail and used index are stored with the packed wrap
  156. * counter bit inverted. This way, in case set_vq_state is
  157. * not called, the initial value can be set to zero prior to
  158. * feature negotiation, and it is good for both packed and
  159. * split vq.
  160. */
  161. avail ^= PDS_VDPA_PACKED_INVERT_IDX;
  162. used ^= PDS_VDPA_PACKED_INVERT_IDX;
  163. } else {
  164. avail = state->split.avail_index;
  165. /* state->split does not provide a used_index:
  166. * the vq will be set to "empty" here, and the vq will read
  167. * the current used index the next time the vq is kicked.
  168. */
  169. used = avail;
  170. }
  171. if (used != avail) {
  172. dev_dbg(dev, "Setting used equal to avail, for interoperability\n");
  173. used = avail;
  174. }
  175. pdsv->vqs[qid].avail_idx = avail;
  176. pdsv->vqs[qid].used_idx = used;
  177. return 0;
  178. }
  179. static int pds_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
  180. struct vdpa_vq_state *state)
  181. {
  182. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  183. struct pds_auxiliary_dev *padev = pdsv->vdpa_aux->padev;
  184. struct device *dev = &padev->aux_dev.dev;
  185. u64 driver_features;
  186. u16 avail;
  187. u16 used;
  188. if (pdsv->vqs[qid].ready) {
  189. dev_err(dev, "Getting device position is denied while vq is enabled\n");
  190. return -EINVAL;
  191. }
  192. avail = pdsv->vqs[qid].avail_idx;
  193. used = pdsv->vqs[qid].used_idx;
  194. driver_features = pds_vdpa_get_driver_features(vdpa_dev);
  195. if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) {
  196. avail ^= PDS_VDPA_PACKED_INVERT_IDX;
  197. used ^= PDS_VDPA_PACKED_INVERT_IDX;
  198. state->packed.last_avail_idx = avail & 0x7fff;
  199. state->packed.last_avail_counter = avail >> 15;
  200. state->packed.last_used_idx = used & 0x7fff;
  201. state->packed.last_used_counter = used >> 15;
  202. } else {
  203. state->split.avail_index = avail;
  204. /* state->split does not provide a used_index. */
  205. }
  206. return 0;
  207. }
  208. static struct vdpa_notification_area
  209. pds_vdpa_get_vq_notification(struct vdpa_device *vdpa_dev, u16 qid)
  210. {
  211. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  212. struct virtio_pci_modern_device *vd_mdev;
  213. struct vdpa_notification_area area;
  214. area.addr = pdsv->vqs[qid].notify_pa;
  215. vd_mdev = &pdsv->vdpa_aux->vd_mdev;
  216. if (!vd_mdev->notify_offset_multiplier)
  217. area.size = PDS_PAGE_SIZE;
  218. else
  219. area.size = vd_mdev->notify_offset_multiplier;
  220. return area;
  221. }
  222. static int pds_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev, u16 qid)
  223. {
  224. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  225. return pdsv->vqs[qid].irq;
  226. }
  227. static u32 pds_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
  228. {
  229. return PDS_PAGE_SIZE;
  230. }
  231. static u32 pds_vdpa_get_vq_group(struct vdpa_device *vdpa_dev, u16 idx)
  232. {
  233. return 0;
  234. }
  235. static u64 pds_vdpa_get_device_features(struct vdpa_device *vdpa_dev)
  236. {
  237. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  238. return pdsv->supported_features;
  239. }
  240. static int pds_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features)
  241. {
  242. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  243. struct device *dev = &pdsv->vdpa_dev.dev;
  244. u64 driver_features;
  245. u64 nego_features;
  246. u64 hw_features;
  247. u64 missing;
  248. if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)) && features) {
  249. dev_err(dev, "VIRTIO_F_ACCESS_PLATFORM is not negotiated\n");
  250. return -EOPNOTSUPP;
  251. }
  252. /* Check for valid feature bits */
  253. nego_features = features & pdsv->supported_features;
  254. missing = features & ~nego_features;
  255. if (missing) {
  256. dev_err(dev, "Can't support all requested features in %#llx, missing %#llx features\n",
  257. features, missing);
  258. return -EOPNOTSUPP;
  259. }
  260. driver_features = pds_vdpa_get_driver_features(vdpa_dev);
  261. pdsv->negotiated_features = nego_features;
  262. dev_dbg(dev, "%s: %#llx => %#llx\n",
  263. __func__, driver_features, nego_features);
  264. /* if we're faking the F_MAC, strip it before writing to device */
  265. hw_features = le64_to_cpu(pdsv->vdpa_aux->ident.hw_features);
  266. if (!(hw_features & BIT_ULL(VIRTIO_NET_F_MAC)))
  267. nego_features &= ~BIT_ULL(VIRTIO_NET_F_MAC);
  268. if (driver_features == nego_features)
  269. return 0;
  270. vp_modern_set_features(&pdsv->vdpa_aux->vd_mdev, nego_features);
  271. return 0;
  272. }
  273. static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev)
  274. {
  275. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  276. return pdsv->negotiated_features;
  277. }
  278. static void pds_vdpa_set_config_cb(struct vdpa_device *vdpa_dev,
  279. struct vdpa_callback *cb)
  280. {
  281. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  282. pdsv->config_cb.callback = cb->callback;
  283. pdsv->config_cb.private = cb->private;
  284. }
  285. static u16 pds_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev)
  286. {
  287. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  288. /* qemu has assert() that vq_num_max <= VIRTQUEUE_MAX_SIZE (1024) */
  289. return min_t(u16, 1024, BIT(le16_to_cpu(pdsv->vdpa_aux->ident.max_qlen)));
  290. }
  291. static u32 pds_vdpa_get_device_id(struct vdpa_device *vdpa_dev)
  292. {
  293. return VIRTIO_ID_NET;
  294. }
  295. static u32 pds_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev)
  296. {
  297. return PCI_VENDOR_ID_PENSANDO;
  298. }
  299. static u8 pds_vdpa_get_status(struct vdpa_device *vdpa_dev)
  300. {
  301. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  302. return vp_modern_get_status(&pdsv->vdpa_aux->vd_mdev);
  303. }
  304. static int pds_vdpa_request_irqs(struct pds_vdpa_device *pdsv)
  305. {
  306. struct pci_dev *pdev = pdsv->vdpa_aux->padev->vf_pdev;
  307. struct pds_vdpa_aux *vdpa_aux = pdsv->vdpa_aux;
  308. struct device *dev = &pdsv->vdpa_dev.dev;
  309. int max_vq, nintrs, qid, err;
  310. max_vq = vdpa_aux->vdpa_mdev.max_supported_vqs;
  311. nintrs = pci_alloc_irq_vectors(pdev, max_vq, max_vq, PCI_IRQ_MSIX);
  312. if (nintrs < 0) {
  313. dev_err(dev, "Couldn't get %d msix vectors: %pe\n",
  314. max_vq, ERR_PTR(nintrs));
  315. return nintrs;
  316. }
  317. for (qid = 0; qid < pdsv->num_vqs; ++qid) {
  318. int irq = pci_irq_vector(pdev, qid);
  319. snprintf(pdsv->vqs[qid].irq_name, sizeof(pdsv->vqs[qid].irq_name),
  320. "vdpa-%s-%d", dev_name(dev), qid);
  321. err = request_irq(irq, pds_vdpa_isr, 0,
  322. pdsv->vqs[qid].irq_name,
  323. &pdsv->vqs[qid]);
  324. if (err) {
  325. dev_err(dev, "%s: no irq for qid %d: %pe\n",
  326. __func__, qid, ERR_PTR(err));
  327. goto err_release;
  328. }
  329. pdsv->vqs[qid].irq = irq;
  330. }
  331. vdpa_aux->nintrs = nintrs;
  332. return 0;
  333. err_release:
  334. while (qid--)
  335. pds_vdpa_release_irq(pdsv, qid);
  336. pci_free_irq_vectors(pdev);
  337. vdpa_aux->nintrs = 0;
  338. return err;
  339. }
  340. void pds_vdpa_release_irqs(struct pds_vdpa_device *pdsv)
  341. {
  342. struct pds_vdpa_aux *vdpa_aux;
  343. struct pci_dev *pdev;
  344. int qid;
  345. if (!pdsv)
  346. return;
  347. pdev = pdsv->vdpa_aux->padev->vf_pdev;
  348. vdpa_aux = pdsv->vdpa_aux;
  349. if (!vdpa_aux->nintrs)
  350. return;
  351. for (qid = 0; qid < pdsv->num_vqs; qid++)
  352. pds_vdpa_release_irq(pdsv, qid);
  353. pci_free_irq_vectors(pdev);
  354. vdpa_aux->nintrs = 0;
  355. }
  356. static void pds_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status)
  357. {
  358. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  359. struct device *dev = &pdsv->vdpa_dev.dev;
  360. u8 old_status;
  361. int i;
  362. old_status = pds_vdpa_get_status(vdpa_dev);
  363. dev_dbg(dev, "%s: old %#x new %#x\n", __func__, old_status, status);
  364. if (status & ~old_status & VIRTIO_CONFIG_S_DRIVER_OK) {
  365. if (pds_vdpa_request_irqs(pdsv))
  366. status = old_status | VIRTIO_CONFIG_S_FAILED;
  367. }
  368. pds_vdpa_cmd_set_status(pdsv, status);
  369. if (status == 0) {
  370. struct vdpa_callback null_cb = { };
  371. pds_vdpa_set_config_cb(vdpa_dev, &null_cb);
  372. pds_vdpa_cmd_reset(pdsv);
  373. for (i = 0; i < pdsv->num_vqs; i++) {
  374. pdsv->vqs[i].avail_idx = 0;
  375. pdsv->vqs[i].used_idx = 0;
  376. }
  377. pds_vdpa_cmd_set_mac(pdsv, pdsv->mac);
  378. }
  379. if (status & ~old_status & VIRTIO_CONFIG_S_FEATURES_OK) {
  380. for (i = 0; i < pdsv->num_vqs; i++) {
  381. pdsv->vqs[i].notify =
  382. vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev,
  383. i, &pdsv->vqs[i].notify_pa);
  384. }
  385. }
  386. if (old_status & ~status & VIRTIO_CONFIG_S_DRIVER_OK)
  387. pds_vdpa_release_irqs(pdsv);
  388. }
  389. static void pds_vdpa_init_vqs_entry(struct pds_vdpa_device *pdsv, int qid,
  390. void __iomem *notify)
  391. {
  392. memset(&pdsv->vqs[qid], 0, sizeof(pdsv->vqs[0]));
  393. pdsv->vqs[qid].qid = qid;
  394. pdsv->vqs[qid].pdsv = pdsv;
  395. pdsv->vqs[qid].ready = false;
  396. pdsv->vqs[qid].irq = VIRTIO_MSI_NO_VECTOR;
  397. pdsv->vqs[qid].notify = notify;
  398. }
  399. static int pds_vdpa_reset(struct vdpa_device *vdpa_dev)
  400. {
  401. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  402. struct device *dev;
  403. int err = 0;
  404. u8 status;
  405. int i;
  406. dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
  407. status = pds_vdpa_get_status(vdpa_dev);
  408. if (status == 0)
  409. return 0;
  410. if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
  411. /* Reset the vqs */
  412. for (i = 0; i < pdsv->num_vqs && !err; i++) {
  413. err = pds_vdpa_cmd_reset_vq(pdsv, i, 0, &pdsv->vqs[i]);
  414. if (err)
  415. dev_err(dev, "%s: reset_vq failed qid %d: %pe\n",
  416. __func__, i, ERR_PTR(err));
  417. }
  418. }
  419. pds_vdpa_set_status(vdpa_dev, 0);
  420. if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
  421. /* Reset the vq info */
  422. for (i = 0; i < pdsv->num_vqs && !err; i++)
  423. pds_vdpa_init_vqs_entry(pdsv, i, pdsv->vqs[i].notify);
  424. }
  425. return 0;
  426. }
  427. static size_t pds_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
  428. {
  429. return sizeof(struct virtio_net_config);
  430. }
  431. static void pds_vdpa_get_config(struct vdpa_device *vdpa_dev,
  432. unsigned int offset,
  433. void *buf, unsigned int len)
  434. {
  435. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  436. void __iomem *device;
  437. if (offset + len > sizeof(struct virtio_net_config)) {
  438. WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len);
  439. return;
  440. }
  441. device = pdsv->vdpa_aux->vd_mdev.device;
  442. memcpy_fromio(buf, device + offset, len);
  443. }
  444. static void pds_vdpa_set_config(struct vdpa_device *vdpa_dev,
  445. unsigned int offset, const void *buf,
  446. unsigned int len)
  447. {
  448. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  449. void __iomem *device;
  450. if (offset + len > sizeof(struct virtio_net_config)) {
  451. WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len);
  452. return;
  453. }
  454. device = pdsv->vdpa_aux->vd_mdev.device;
  455. memcpy_toio(device + offset, buf, len);
  456. }
  457. static const struct vdpa_config_ops pds_vdpa_ops = {
  458. .set_vq_address = pds_vdpa_set_vq_address,
  459. .set_vq_num = pds_vdpa_set_vq_num,
  460. .kick_vq = pds_vdpa_kick_vq,
  461. .set_vq_cb = pds_vdpa_set_vq_cb,
  462. .set_vq_ready = pds_vdpa_set_vq_ready,
  463. .get_vq_ready = pds_vdpa_get_vq_ready,
  464. .set_vq_state = pds_vdpa_set_vq_state,
  465. .get_vq_state = pds_vdpa_get_vq_state,
  466. .get_vq_notification = pds_vdpa_get_vq_notification,
  467. .get_vq_irq = pds_vdpa_get_vq_irq,
  468. .get_vq_align = pds_vdpa_get_vq_align,
  469. .get_vq_group = pds_vdpa_get_vq_group,
  470. .get_device_features = pds_vdpa_get_device_features,
  471. .set_driver_features = pds_vdpa_set_driver_features,
  472. .get_driver_features = pds_vdpa_get_driver_features,
  473. .set_config_cb = pds_vdpa_set_config_cb,
  474. .get_vq_num_max = pds_vdpa_get_vq_num_max,
  475. .get_device_id = pds_vdpa_get_device_id,
  476. .get_vendor_id = pds_vdpa_get_vendor_id,
  477. .get_status = pds_vdpa_get_status,
  478. .set_status = pds_vdpa_set_status,
  479. .reset = pds_vdpa_reset,
  480. .get_config_size = pds_vdpa_get_config_size,
  481. .get_config = pds_vdpa_get_config,
  482. .set_config = pds_vdpa_set_config,
  483. };
  484. static struct virtio_device_id pds_vdpa_id_table[] = {
  485. {VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID},
  486. {0},
  487. };
  488. static int pds_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
  489. const struct vdpa_dev_set_config *add_config)
  490. {
  491. struct pds_vdpa_aux *vdpa_aux;
  492. struct pds_vdpa_device *pdsv;
  493. struct vdpa_mgmt_dev *mgmt;
  494. u16 fw_max_vqs, vq_pairs;
  495. struct device *dma_dev;
  496. struct pci_dev *pdev;
  497. struct device *dev;
  498. u8 status;
  499. int err;
  500. int i;
  501. vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev);
  502. dev = &vdpa_aux->padev->aux_dev.dev;
  503. mgmt = &vdpa_aux->vdpa_mdev;
  504. if (vdpa_aux->pdsv) {
  505. dev_warn(dev, "Multiple vDPA devices on a VF is not supported.\n");
  506. return -EOPNOTSUPP;
  507. }
  508. pdsv = vdpa_alloc_device(struct pds_vdpa_device, vdpa_dev,
  509. dev, &pds_vdpa_ops, NULL,
  510. 1, 1, name, false);
  511. if (IS_ERR(pdsv)) {
  512. dev_err(dev, "Failed to allocate vDPA structure: %pe\n", pdsv);
  513. return PTR_ERR(pdsv);
  514. }
  515. vdpa_aux->pdsv = pdsv;
  516. pdsv->vdpa_aux = vdpa_aux;
  517. pdev = vdpa_aux->padev->vf_pdev;
  518. dma_dev = &pdev->dev;
  519. pdsv->vdpa_dev.vmap.dma_dev = dma_dev;
  520. status = pds_vdpa_get_status(&pdsv->vdpa_dev);
  521. if (status == 0xff) {
  522. dev_err(dev, "Broken PCI - status %#x\n", status);
  523. err = -ENXIO;
  524. goto err_unmap;
  525. }
  526. pdsv->supported_features = mgmt->supported_features;
  527. if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
  528. u64 unsupp_features =
  529. add_config->device_features & ~pdsv->supported_features;
  530. if (unsupp_features) {
  531. dev_err(dev, "Unsupported features: %#llx\n", unsupp_features);
  532. err = -EOPNOTSUPP;
  533. goto err_unmap;
  534. }
  535. pdsv->supported_features = add_config->device_features;
  536. }
  537. err = pds_vdpa_cmd_reset(pdsv);
  538. if (err) {
  539. dev_err(dev, "Failed to reset hw: %pe\n", ERR_PTR(err));
  540. goto err_unmap;
  541. }
  542. err = pds_vdpa_init_hw(pdsv);
  543. if (err) {
  544. dev_err(dev, "Failed to init hw: %pe\n", ERR_PTR(err));
  545. goto err_unmap;
  546. }
  547. fw_max_vqs = le16_to_cpu(pdsv->vdpa_aux->ident.max_vqs);
  548. vq_pairs = fw_max_vqs / 2;
  549. /* Make sure we have the queues being requested */
  550. if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
  551. vq_pairs = add_config->net.max_vq_pairs;
  552. pdsv->num_vqs = 2 * vq_pairs;
  553. if (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ))
  554. pdsv->num_vqs++;
  555. if (pdsv->num_vqs > fw_max_vqs) {
  556. dev_err(dev, "%s: queue count requested %u greater than max %u\n",
  557. __func__, pdsv->num_vqs, fw_max_vqs);
  558. err = -ENOSPC;
  559. goto err_unmap;
  560. }
  561. if (pdsv->num_vqs != fw_max_vqs) {
  562. err = pds_vdpa_cmd_set_max_vq_pairs(pdsv, vq_pairs);
  563. if (err) {
  564. dev_err(dev, "Failed to set max_vq_pairs: %pe\n",
  565. ERR_PTR(err));
  566. goto err_unmap;
  567. }
  568. }
  569. /* Set a mac, either from the user config if provided
  570. * or use the device's mac if not 00:..:00
  571. * or set a random mac
  572. */
  573. if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
  574. ether_addr_copy(pdsv->mac, add_config->net.mac);
  575. } else {
  576. struct virtio_net_config __iomem *vc;
  577. vc = pdsv->vdpa_aux->vd_mdev.device;
  578. memcpy_fromio(pdsv->mac, vc->mac, sizeof(pdsv->mac));
  579. if (is_zero_ether_addr(pdsv->mac) &&
  580. (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_MAC))) {
  581. eth_random_addr(pdsv->mac);
  582. dev_info(dev, "setting random mac %pM\n", pdsv->mac);
  583. }
  584. }
  585. pds_vdpa_cmd_set_mac(pdsv, pdsv->mac);
  586. for (i = 0; i < pdsv->num_vqs; i++) {
  587. void __iomem *notify;
  588. notify = vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev,
  589. i, &pdsv->vqs[i].notify_pa);
  590. pds_vdpa_init_vqs_entry(pdsv, i, notify);
  591. }
  592. pdsv->vdpa_dev.mdev = &vdpa_aux->vdpa_mdev;
  593. err = pds_vdpa_register_event_handler(pdsv);
  594. if (err) {
  595. dev_err(dev, "Failed to register for PDS events: %pe\n", ERR_PTR(err));
  596. goto err_unmap;
  597. }
  598. /* We use the _vdpa_register_device() call rather than the
  599. * vdpa_register_device() to avoid a deadlock because our
  600. * dev_add() is called with the vdpa_dev_lock already set
  601. * by vdpa_nl_cmd_dev_add_set_doit()
  602. */
  603. err = _vdpa_register_device(&pdsv->vdpa_dev, pdsv->num_vqs);
  604. if (err) {
  605. dev_err(dev, "Failed to register to vDPA bus: %pe\n", ERR_PTR(err));
  606. goto err_unevent;
  607. }
  608. pds_vdpa_debugfs_add_vdpadev(vdpa_aux);
  609. return 0;
  610. err_unevent:
  611. pds_vdpa_unregister_event_handler(pdsv);
  612. err_unmap:
  613. put_device(&pdsv->vdpa_dev.dev);
  614. vdpa_aux->pdsv = NULL;
  615. return err;
  616. }
  617. static void pds_vdpa_dev_del(struct vdpa_mgmt_dev *mdev,
  618. struct vdpa_device *vdpa_dev)
  619. {
  620. struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
  621. struct pds_vdpa_aux *vdpa_aux;
  622. pds_vdpa_unregister_event_handler(pdsv);
  623. vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev);
  624. _vdpa_unregister_device(vdpa_dev);
  625. pds_vdpa_cmd_reset(vdpa_aux->pdsv);
  626. pds_vdpa_debugfs_reset_vdpadev(vdpa_aux);
  627. vdpa_aux->pdsv = NULL;
  628. dev_info(&vdpa_aux->padev->aux_dev.dev, "Removed vdpa device\n");
  629. }
  630. static const struct vdpa_mgmtdev_ops pds_vdpa_mgmt_dev_ops = {
  631. .dev_add = pds_vdpa_dev_add,
  632. .dev_del = pds_vdpa_dev_del
  633. };
  634. int pds_vdpa_get_mgmt_info(struct pds_vdpa_aux *vdpa_aux)
  635. {
  636. union pds_core_adminq_cmd cmd = {
  637. .vdpa_ident.opcode = PDS_VDPA_CMD_IDENT,
  638. .vdpa_ident.vf_id = cpu_to_le16(vdpa_aux->vf_id),
  639. };
  640. union pds_core_adminq_comp comp = {};
  641. struct vdpa_mgmt_dev *mgmt;
  642. struct pci_dev *pf_pdev;
  643. struct device *pf_dev;
  644. struct pci_dev *pdev;
  645. dma_addr_t ident_pa;
  646. struct device *dev;
  647. u16 dev_intrs;
  648. u16 max_vqs;
  649. int err;
  650. dev = &vdpa_aux->padev->aux_dev.dev;
  651. pdev = vdpa_aux->padev->vf_pdev;
  652. mgmt = &vdpa_aux->vdpa_mdev;
  653. /* Get resource info through the PF's adminq. It is a block of info,
  654. * so we need to map some memory for PF to make available to the
  655. * firmware for writing the data.
  656. */
  657. pf_pdev = pci_physfn(vdpa_aux->padev->vf_pdev);
  658. pf_dev = &pf_pdev->dev;
  659. ident_pa = dma_map_single(pf_dev, &vdpa_aux->ident,
  660. sizeof(vdpa_aux->ident), DMA_FROM_DEVICE);
  661. if (dma_mapping_error(pf_dev, ident_pa)) {
  662. dev_err(dev, "Failed to map ident space\n");
  663. return -ENOMEM;
  664. }
  665. cmd.vdpa_ident.ident_pa = cpu_to_le64(ident_pa);
  666. cmd.vdpa_ident.len = cpu_to_le32(sizeof(vdpa_aux->ident));
  667. err = pds_client_adminq_cmd(vdpa_aux->padev, &cmd,
  668. sizeof(cmd.vdpa_ident), &comp, 0);
  669. dma_unmap_single(pf_dev, ident_pa,
  670. sizeof(vdpa_aux->ident), DMA_FROM_DEVICE);
  671. if (err) {
  672. dev_err(dev, "Failed to ident hw, status %d: %pe\n",
  673. comp.status, ERR_PTR(err));
  674. return err;
  675. }
  676. max_vqs = le16_to_cpu(vdpa_aux->ident.max_vqs);
  677. dev_intrs = pci_msix_vec_count(pdev);
  678. dev_dbg(dev, "ident.max_vqs %d dev_intrs %d\n", max_vqs, dev_intrs);
  679. max_vqs = min_t(u16, dev_intrs, max_vqs);
  680. mgmt->max_supported_vqs = min_t(u16, PDS_VDPA_MAX_QUEUES, max_vqs);
  681. vdpa_aux->nintrs = 0;
  682. mgmt->ops = &pds_vdpa_mgmt_dev_ops;
  683. mgmt->id_table = pds_vdpa_id_table;
  684. mgmt->device = dev;
  685. mgmt->supported_features = le64_to_cpu(vdpa_aux->ident.hw_features);
  686. /* advertise F_MAC even if the device doesn't */
  687. mgmt->supported_features |= BIT_ULL(VIRTIO_NET_F_MAC);
  688. mgmt->config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
  689. mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
  690. mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_FEATURES);
  691. return 0;
  692. }