ifcvf_main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel IFC VF NIC driver for virtio dataplane offloading
  4. *
  5. * Copyright (C) 2020 Intel Corporation.
  6. *
  7. * Author: Zhu Lingshan <lingshan.zhu@intel.com>
  8. *
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/sysfs.h>
  14. #include "ifcvf_base.h"
  15. #define DRIVER_AUTHOR "Intel Corporation"
  16. #define IFCVF_DRIVER_NAME "ifcvf"
  17. static irqreturn_t ifcvf_config_changed(int irq, void *arg)
  18. {
  19. struct ifcvf_hw *vf = arg;
  20. if (vf->config_cb.callback)
  21. return vf->config_cb.callback(vf->config_cb.private);
  22. return IRQ_HANDLED;
  23. }
  24. static irqreturn_t ifcvf_vq_intr_handler(int irq, void *arg)
  25. {
  26. struct vring_info *vring = arg;
  27. if (vring->cb.callback)
  28. return vring->cb.callback(vring->cb.private);
  29. return IRQ_HANDLED;
  30. }
  31. static irqreturn_t ifcvf_vqs_reused_intr_handler(int irq, void *arg)
  32. {
  33. struct ifcvf_hw *vf = arg;
  34. struct vring_info *vring;
  35. int i;
  36. for (i = 0; i < vf->nr_vring; i++) {
  37. vring = &vf->vring[i];
  38. if (vring->cb.callback)
  39. vring->cb.callback(vring->cb.private);
  40. }
  41. return IRQ_HANDLED;
  42. }
  43. static irqreturn_t ifcvf_dev_intr_handler(int irq, void *arg)
  44. {
  45. struct ifcvf_hw *vf = arg;
  46. u8 isr;
  47. isr = vp_ioread8(vf->isr);
  48. if (isr & VIRTIO_PCI_ISR_CONFIG)
  49. ifcvf_config_changed(irq, arg);
  50. return ifcvf_vqs_reused_intr_handler(irq, arg);
  51. }
  52. static void ifcvf_free_irq_vectors(void *data)
  53. {
  54. pci_free_irq_vectors(data);
  55. }
  56. static void ifcvf_free_per_vq_irq(struct ifcvf_hw *vf)
  57. {
  58. struct pci_dev *pdev = vf->pdev;
  59. int i;
  60. for (i = 0; i < vf->nr_vring; i++) {
  61. if (vf->vring[i].irq != -EINVAL) {
  62. devm_free_irq(&pdev->dev, vf->vring[i].irq, &vf->vring[i]);
  63. vf->vring[i].irq = -EINVAL;
  64. }
  65. }
  66. }
  67. static void ifcvf_free_vqs_reused_irq(struct ifcvf_hw *vf)
  68. {
  69. struct pci_dev *pdev = vf->pdev;
  70. if (vf->vqs_reused_irq != -EINVAL) {
  71. devm_free_irq(&pdev->dev, vf->vqs_reused_irq, vf);
  72. vf->vqs_reused_irq = -EINVAL;
  73. }
  74. }
  75. static void ifcvf_free_vq_irq(struct ifcvf_hw *vf)
  76. {
  77. if (vf->msix_vector_status == MSIX_VECTOR_PER_VQ_AND_CONFIG)
  78. ifcvf_free_per_vq_irq(vf);
  79. else
  80. ifcvf_free_vqs_reused_irq(vf);
  81. }
  82. static void ifcvf_free_config_irq(struct ifcvf_hw *vf)
  83. {
  84. struct pci_dev *pdev = vf->pdev;
  85. if (vf->config_irq == -EINVAL)
  86. return;
  87. /* If the irq is shared by all vqs and the config interrupt,
  88. * it is already freed in ifcvf_free_vq_irq, so here only
  89. * need to free config irq when msix_vector_status != MSIX_VECTOR_DEV_SHARED
  90. */
  91. if (vf->msix_vector_status != MSIX_VECTOR_DEV_SHARED) {
  92. devm_free_irq(&pdev->dev, vf->config_irq, vf);
  93. vf->config_irq = -EINVAL;
  94. }
  95. }
  96. static void ifcvf_free_irq(struct ifcvf_hw *vf)
  97. {
  98. struct pci_dev *pdev = vf->pdev;
  99. ifcvf_free_vq_irq(vf);
  100. ifcvf_free_config_irq(vf);
  101. ifcvf_free_irq_vectors(pdev);
  102. vf->num_msix_vectors = 0;
  103. }
  104. /* ifcvf MSIX vectors allocator, this helper tries to allocate
  105. * vectors for all virtqueues and the config interrupt.
  106. * It returns the number of allocated vectors, negative
  107. * return value when fails.
  108. */
  109. static int ifcvf_alloc_vectors(struct ifcvf_hw *vf)
  110. {
  111. struct pci_dev *pdev = vf->pdev;
  112. int max_intr, ret;
  113. /* all queues and config interrupt */
  114. max_intr = vf->nr_vring + 1;
  115. ret = pci_alloc_irq_vectors(pdev, 1, max_intr, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
  116. if (ret < 0) {
  117. IFCVF_ERR(pdev, "Failed to alloc IRQ vectors\n");
  118. return ret;
  119. }
  120. if (ret < max_intr)
  121. IFCVF_INFO(pdev,
  122. "Requested %u vectors, however only %u allocated, lower performance\n",
  123. max_intr, ret);
  124. return ret;
  125. }
  126. static int ifcvf_request_per_vq_irq(struct ifcvf_hw *vf)
  127. {
  128. struct pci_dev *pdev = vf->pdev;
  129. int i, vector, ret, irq;
  130. vf->vqs_reused_irq = -EINVAL;
  131. for (i = 0; i < vf->nr_vring; i++) {
  132. snprintf(vf->vring[i].msix_name, 256, "ifcvf[%s]-%d\n", pci_name(pdev), i);
  133. vector = i;
  134. irq = pci_irq_vector(pdev, vector);
  135. ret = devm_request_irq(&pdev->dev, irq,
  136. ifcvf_vq_intr_handler, 0,
  137. vf->vring[i].msix_name,
  138. &vf->vring[i]);
  139. if (ret) {
  140. IFCVF_ERR(pdev, "Failed to request irq for vq %d\n", i);
  141. goto err;
  142. }
  143. vf->vring[i].irq = irq;
  144. ret = ifcvf_set_vq_vector(vf, i, vector);
  145. if (ret == VIRTIO_MSI_NO_VECTOR) {
  146. IFCVF_ERR(pdev, "No msix vector for vq %u\n", i);
  147. goto err;
  148. }
  149. }
  150. return 0;
  151. err:
  152. ifcvf_free_irq(vf);
  153. return -EFAULT;
  154. }
  155. static int ifcvf_request_vqs_reused_irq(struct ifcvf_hw *vf)
  156. {
  157. struct pci_dev *pdev = vf->pdev;
  158. int i, vector, ret, irq;
  159. vector = 0;
  160. snprintf(vf->vring[0].msix_name, 256, "ifcvf[%s]-vqs-reused-irq\n", pci_name(pdev));
  161. irq = pci_irq_vector(pdev, vector);
  162. ret = devm_request_irq(&pdev->dev, irq,
  163. ifcvf_vqs_reused_intr_handler, 0,
  164. vf->vring[0].msix_name, vf);
  165. if (ret) {
  166. IFCVF_ERR(pdev, "Failed to request reused irq for the device\n");
  167. goto err;
  168. }
  169. vf->vqs_reused_irq = irq;
  170. for (i = 0; i < vf->nr_vring; i++) {
  171. vf->vring[i].irq = -EINVAL;
  172. ret = ifcvf_set_vq_vector(vf, i, vector);
  173. if (ret == VIRTIO_MSI_NO_VECTOR) {
  174. IFCVF_ERR(pdev, "No msix vector for vq %u\n", i);
  175. goto err;
  176. }
  177. }
  178. return 0;
  179. err:
  180. ifcvf_free_irq(vf);
  181. return -EFAULT;
  182. }
  183. static int ifcvf_request_dev_irq(struct ifcvf_hw *vf)
  184. {
  185. struct pci_dev *pdev = vf->pdev;
  186. int i, vector, ret, irq;
  187. vector = 0;
  188. snprintf(vf->vring[0].msix_name, 256, "ifcvf[%s]-dev-irq\n", pci_name(pdev));
  189. irq = pci_irq_vector(pdev, vector);
  190. ret = devm_request_irq(&pdev->dev, irq,
  191. ifcvf_dev_intr_handler, 0,
  192. vf->vring[0].msix_name, vf);
  193. if (ret) {
  194. IFCVF_ERR(pdev, "Failed to request irq for the device\n");
  195. goto err;
  196. }
  197. vf->vqs_reused_irq = irq;
  198. for (i = 0; i < vf->nr_vring; i++) {
  199. vf->vring[i].irq = -EINVAL;
  200. ret = ifcvf_set_vq_vector(vf, i, vector);
  201. if (ret == VIRTIO_MSI_NO_VECTOR) {
  202. IFCVF_ERR(pdev, "No msix vector for vq %u\n", i);
  203. goto err;
  204. }
  205. }
  206. vf->config_irq = irq;
  207. ret = ifcvf_set_config_vector(vf, vector);
  208. if (ret == VIRTIO_MSI_NO_VECTOR) {
  209. IFCVF_ERR(pdev, "No msix vector for device config\n");
  210. goto err;
  211. }
  212. return 0;
  213. err:
  214. ifcvf_free_irq(vf);
  215. return -EFAULT;
  216. }
  217. static int ifcvf_request_vq_irq(struct ifcvf_hw *vf)
  218. {
  219. int ret;
  220. if (vf->msix_vector_status == MSIX_VECTOR_PER_VQ_AND_CONFIG)
  221. ret = ifcvf_request_per_vq_irq(vf);
  222. else
  223. ret = ifcvf_request_vqs_reused_irq(vf);
  224. return ret;
  225. }
  226. static int ifcvf_request_config_irq(struct ifcvf_hw *vf)
  227. {
  228. struct pci_dev *pdev = vf->pdev;
  229. int config_vector, ret;
  230. if (vf->msix_vector_status == MSIX_VECTOR_PER_VQ_AND_CONFIG)
  231. config_vector = vf->nr_vring;
  232. else if (vf->msix_vector_status == MSIX_VECTOR_SHARED_VQ_AND_CONFIG)
  233. /* vector 0 for vqs and 1 for config interrupt */
  234. config_vector = 1;
  235. else if (vf->msix_vector_status == MSIX_VECTOR_DEV_SHARED)
  236. /* re-use the vqs vector */
  237. return 0;
  238. else
  239. return -EINVAL;
  240. snprintf(vf->config_msix_name, 256, "ifcvf[%s]-config\n",
  241. pci_name(pdev));
  242. vf->config_irq = pci_irq_vector(pdev, config_vector);
  243. ret = devm_request_irq(&pdev->dev, vf->config_irq,
  244. ifcvf_config_changed, 0,
  245. vf->config_msix_name, vf);
  246. if (ret) {
  247. IFCVF_ERR(pdev, "Failed to request config irq\n");
  248. goto err;
  249. }
  250. ret = ifcvf_set_config_vector(vf, config_vector);
  251. if (ret == VIRTIO_MSI_NO_VECTOR) {
  252. IFCVF_ERR(pdev, "No msix vector for device config\n");
  253. goto err;
  254. }
  255. return 0;
  256. err:
  257. ifcvf_free_irq(vf);
  258. return -EFAULT;
  259. }
  260. static int ifcvf_request_irq(struct ifcvf_hw *vf)
  261. {
  262. int nvectors, ret, max_intr;
  263. nvectors = ifcvf_alloc_vectors(vf);
  264. if (nvectors <= 0)
  265. return -EFAULT;
  266. vf->msix_vector_status = MSIX_VECTOR_PER_VQ_AND_CONFIG;
  267. max_intr = vf->nr_vring + 1;
  268. if (nvectors < max_intr)
  269. vf->msix_vector_status = MSIX_VECTOR_SHARED_VQ_AND_CONFIG;
  270. if (nvectors == 1) {
  271. vf->msix_vector_status = MSIX_VECTOR_DEV_SHARED;
  272. ret = ifcvf_request_dev_irq(vf);
  273. return ret;
  274. }
  275. ret = ifcvf_request_vq_irq(vf);
  276. if (ret)
  277. return ret;
  278. ret = ifcvf_request_config_irq(vf);
  279. if (ret)
  280. return ret;
  281. vf->num_msix_vectors = nvectors;
  282. return 0;
  283. }
  284. static struct ifcvf_adapter *vdpa_to_adapter(struct vdpa_device *vdpa_dev)
  285. {
  286. return container_of(vdpa_dev, struct ifcvf_adapter, vdpa);
  287. }
  288. static struct ifcvf_hw *vdpa_to_vf(struct vdpa_device *vdpa_dev)
  289. {
  290. struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev);
  291. return adapter->vf;
  292. }
  293. static u64 ifcvf_vdpa_get_device_features(struct vdpa_device *vdpa_dev)
  294. {
  295. struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev);
  296. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  297. struct pci_dev *pdev = adapter->pdev;
  298. u32 type = vf->dev_type;
  299. u64 features;
  300. if (type == VIRTIO_ID_NET || type == VIRTIO_ID_BLOCK)
  301. features = ifcvf_get_dev_features(vf);
  302. else {
  303. features = 0;
  304. IFCVF_ERR(pdev, "VIRTIO ID %u not supported\n", vf->dev_type);
  305. }
  306. return features;
  307. }
  308. static int ifcvf_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features)
  309. {
  310. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  311. int ret;
  312. ret = ifcvf_verify_min_features(vf, features);
  313. if (ret)
  314. return ret;
  315. ifcvf_set_driver_features(vf, features);
  316. return 0;
  317. }
  318. static u64 ifcvf_vdpa_get_driver_features(struct vdpa_device *vdpa_dev)
  319. {
  320. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  321. u64 features;
  322. features = ifcvf_get_driver_features(vf);
  323. return features;
  324. }
  325. static u8 ifcvf_vdpa_get_status(struct vdpa_device *vdpa_dev)
  326. {
  327. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  328. return ifcvf_get_status(vf);
  329. }
  330. static void ifcvf_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status)
  331. {
  332. struct ifcvf_hw *vf;
  333. u8 status_old;
  334. int ret;
  335. vf = vdpa_to_vf(vdpa_dev);
  336. status_old = ifcvf_get_status(vf);
  337. if (status_old == status)
  338. return;
  339. if ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
  340. !(status_old & VIRTIO_CONFIG_S_DRIVER_OK)) {
  341. ret = ifcvf_request_irq(vf);
  342. if (ret) {
  343. IFCVF_ERR(vf->pdev, "failed to request irq with error %d\n", ret);
  344. return;
  345. }
  346. }
  347. ifcvf_set_status(vf, status);
  348. }
  349. static int ifcvf_vdpa_reset(struct vdpa_device *vdpa_dev)
  350. {
  351. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  352. u8 status = ifcvf_get_status(vf);
  353. ifcvf_stop(vf);
  354. if (status & VIRTIO_CONFIG_S_DRIVER_OK)
  355. ifcvf_free_irq(vf);
  356. ifcvf_reset(vf);
  357. return 0;
  358. }
  359. static u16 ifcvf_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev)
  360. {
  361. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  362. return ifcvf_get_max_vq_size(vf);
  363. }
  364. static u16 ifcvf_vdpa_get_vq_num_min(struct vdpa_device *vdpa_dev)
  365. {
  366. return IFCVF_MIN_VQ_SIZE;
  367. }
  368. static int ifcvf_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
  369. struct vdpa_vq_state *state)
  370. {
  371. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  372. state->split.avail_index = ifcvf_get_vq_state(vf, qid);
  373. return 0;
  374. }
  375. static int ifcvf_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
  376. const struct vdpa_vq_state *state)
  377. {
  378. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  379. return ifcvf_set_vq_state(vf, qid, state->split.avail_index);
  380. }
  381. static void ifcvf_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid,
  382. struct vdpa_callback *cb)
  383. {
  384. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  385. vf->vring[qid].cb = *cb;
  386. }
  387. static void ifcvf_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev,
  388. u16 qid, bool ready)
  389. {
  390. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  391. ifcvf_set_vq_ready(vf, qid, ready);
  392. }
  393. static bool ifcvf_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid)
  394. {
  395. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  396. return ifcvf_get_vq_ready(vf, qid);
  397. }
  398. static void ifcvf_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid,
  399. u32 num)
  400. {
  401. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  402. ifcvf_set_vq_num(vf, qid, num);
  403. }
  404. static int ifcvf_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid,
  405. u64 desc_area, u64 driver_area,
  406. u64 device_area)
  407. {
  408. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  409. return ifcvf_set_vq_address(vf, qid, desc_area, driver_area, device_area);
  410. }
  411. static void ifcvf_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid)
  412. {
  413. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  414. ifcvf_notify_queue(vf, qid);
  415. }
  416. static u32 ifcvf_vdpa_get_generation(struct vdpa_device *vdpa_dev)
  417. {
  418. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  419. return vp_ioread8(&vf->common_cfg->config_generation);
  420. }
  421. static u32 ifcvf_vdpa_get_device_id(struct vdpa_device *vdpa_dev)
  422. {
  423. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  424. return vf->dev_type;
  425. }
  426. static u32 ifcvf_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev)
  427. {
  428. struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev);
  429. struct pci_dev *pdev = adapter->pdev;
  430. return pdev->subsystem_vendor;
  431. }
  432. static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
  433. {
  434. return IFCVF_QUEUE_ALIGNMENT;
  435. }
  436. static size_t ifcvf_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
  437. {
  438. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  439. return vf->config_size;
  440. }
  441. static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
  442. {
  443. return 0;
  444. }
  445. static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
  446. unsigned int offset,
  447. void *buf, unsigned int len)
  448. {
  449. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  450. ifcvf_read_dev_config(vf, offset, buf, len);
  451. }
  452. static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev,
  453. unsigned int offset, const void *buf,
  454. unsigned int len)
  455. {
  456. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  457. ifcvf_write_dev_config(vf, offset, buf, len);
  458. }
  459. static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev,
  460. struct vdpa_callback *cb)
  461. {
  462. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  463. vf->config_cb.callback = cb->callback;
  464. vf->config_cb.private = cb->private;
  465. }
  466. static int ifcvf_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev,
  467. u16 qid)
  468. {
  469. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  470. if (vf->vqs_reused_irq < 0)
  471. return vf->vring[qid].irq;
  472. else
  473. return -EINVAL;
  474. }
  475. static u16 ifcvf_vdpa_get_vq_size(struct vdpa_device *vdpa_dev,
  476. u16 qid)
  477. {
  478. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  479. return ifcvf_get_vq_size(vf, qid);
  480. }
  481. static struct vdpa_notification_area ifcvf_get_vq_notification(struct vdpa_device *vdpa_dev,
  482. u16 idx)
  483. {
  484. struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
  485. struct vdpa_notification_area area;
  486. area.addr = vf->vring[idx].notify_pa;
  487. if (!vf->notify_off_multiplier)
  488. area.size = PAGE_SIZE;
  489. else
  490. area.size = vf->notify_off_multiplier;
  491. return area;
  492. }
  493. /*
  494. * IFCVF currently doesn't have on-chip IOMMU, so not
  495. * implemented set_map()/dma_map()/dma_unmap()
  496. */
  497. static const struct vdpa_config_ops ifc_vdpa_ops = {
  498. .get_device_features = ifcvf_vdpa_get_device_features,
  499. .set_driver_features = ifcvf_vdpa_set_driver_features,
  500. .get_driver_features = ifcvf_vdpa_get_driver_features,
  501. .get_status = ifcvf_vdpa_get_status,
  502. .set_status = ifcvf_vdpa_set_status,
  503. .reset = ifcvf_vdpa_reset,
  504. .get_vq_num_max = ifcvf_vdpa_get_vq_num_max,
  505. .get_vq_num_min = ifcvf_vdpa_get_vq_num_min,
  506. .get_vq_state = ifcvf_vdpa_get_vq_state,
  507. .set_vq_state = ifcvf_vdpa_set_vq_state,
  508. .set_vq_cb = ifcvf_vdpa_set_vq_cb,
  509. .set_vq_ready = ifcvf_vdpa_set_vq_ready,
  510. .get_vq_ready = ifcvf_vdpa_get_vq_ready,
  511. .set_vq_num = ifcvf_vdpa_set_vq_num,
  512. .set_vq_address = ifcvf_vdpa_set_vq_address,
  513. .get_vq_irq = ifcvf_vdpa_get_vq_irq,
  514. .get_vq_size = ifcvf_vdpa_get_vq_size,
  515. .kick_vq = ifcvf_vdpa_kick_vq,
  516. .get_generation = ifcvf_vdpa_get_generation,
  517. .get_device_id = ifcvf_vdpa_get_device_id,
  518. .get_vendor_id = ifcvf_vdpa_get_vendor_id,
  519. .get_vq_align = ifcvf_vdpa_get_vq_align,
  520. .get_vq_group = ifcvf_vdpa_get_vq_group,
  521. .get_config_size = ifcvf_vdpa_get_config_size,
  522. .get_config = ifcvf_vdpa_get_config,
  523. .set_config = ifcvf_vdpa_set_config,
  524. .set_config_cb = ifcvf_vdpa_set_config_cb,
  525. .get_vq_notification = ifcvf_get_vq_notification,
  526. };
  527. static struct virtio_device_id id_table_net[] = {
  528. {VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID},
  529. {0},
  530. };
  531. static struct virtio_device_id id_table_blk[] = {
  532. {VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID},
  533. {0},
  534. };
  535. static u32 get_dev_type(struct pci_dev *pdev)
  536. {
  537. u32 dev_type;
  538. /* This drirver drives both modern virtio devices and transitional
  539. * devices in modern mode.
  540. * vDPA requires feature bit VIRTIO_F_ACCESS_PLATFORM,
  541. * so legacy devices and transitional devices in legacy
  542. * mode will not work for vDPA, this driver will not
  543. * drive devices with legacy interface.
  544. */
  545. if (pdev->device < 0x1040)
  546. dev_type = pdev->subsystem_device;
  547. else
  548. dev_type = pdev->device - 0x1040;
  549. return dev_type;
  550. }
  551. static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
  552. const struct vdpa_dev_set_config *config)
  553. {
  554. struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev;
  555. struct ifcvf_adapter *adapter;
  556. struct vdpa_device *vdpa_dev;
  557. struct pci_dev *pdev;
  558. struct ifcvf_hw *vf;
  559. u64 device_features;
  560. int ret;
  561. ifcvf_mgmt_dev = container_of(mdev, struct ifcvf_vdpa_mgmt_dev, mdev);
  562. vf = &ifcvf_mgmt_dev->vf;
  563. pdev = vf->pdev;
  564. adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
  565. &pdev->dev, &ifc_vdpa_ops,
  566. NULL, 1, 1, NULL, false);
  567. if (IS_ERR(adapter)) {
  568. IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
  569. return PTR_ERR(adapter);
  570. }
  571. ifcvf_mgmt_dev->adapter = adapter;
  572. adapter->pdev = pdev;
  573. adapter->vdpa.vmap.dma_dev = &pdev->dev;
  574. adapter->vdpa.mdev = mdev;
  575. adapter->vf = vf;
  576. vdpa_dev = &adapter->vdpa;
  577. device_features = vf->hw_features;
  578. if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
  579. if (config->device_features & ~device_features) {
  580. IFCVF_ERR(pdev, "The provisioned features 0x%llx are not supported by this device with features 0x%llx\n",
  581. config->device_features, device_features);
  582. return -EINVAL;
  583. }
  584. device_features &= config->device_features;
  585. }
  586. vf->dev_features = device_features;
  587. if (name)
  588. ret = dev_set_name(&vdpa_dev->dev, "%s", name);
  589. else
  590. ret = dev_set_name(&vdpa_dev->dev, "vdpa%u", vdpa_dev->index);
  591. ret = _vdpa_register_device(&adapter->vdpa, vf->nr_vring);
  592. if (ret) {
  593. put_device(&adapter->vdpa.dev);
  594. IFCVF_ERR(pdev, "Failed to register to vDPA bus");
  595. return ret;
  596. }
  597. return 0;
  598. }
  599. static void ifcvf_vdpa_dev_del(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev)
  600. {
  601. struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev;
  602. ifcvf_mgmt_dev = container_of(mdev, struct ifcvf_vdpa_mgmt_dev, mdev);
  603. _vdpa_unregister_device(dev);
  604. ifcvf_mgmt_dev->adapter = NULL;
  605. }
  606. static const struct vdpa_mgmtdev_ops ifcvf_vdpa_mgmt_dev_ops = {
  607. .dev_add = ifcvf_vdpa_dev_add,
  608. .dev_del = ifcvf_vdpa_dev_del
  609. };
  610. static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  611. {
  612. struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev;
  613. struct device *dev = &pdev->dev;
  614. struct ifcvf_hw *vf;
  615. u32 dev_type;
  616. int ret, i;
  617. ret = pcim_enable_device(pdev);
  618. if (ret) {
  619. IFCVF_ERR(pdev, "Failed to enable device\n");
  620. return ret;
  621. }
  622. ret = pcim_iomap_regions(pdev, BIT(0) | BIT(2) | BIT(4),
  623. IFCVF_DRIVER_NAME);
  624. if (ret) {
  625. IFCVF_ERR(pdev, "Failed to request MMIO region\n");
  626. return ret;
  627. }
  628. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  629. if (ret) {
  630. IFCVF_ERR(pdev, "No usable DMA configuration\n");
  631. return ret;
  632. }
  633. ret = devm_add_action_or_reset(dev, ifcvf_free_irq_vectors, pdev);
  634. if (ret) {
  635. IFCVF_ERR(pdev,
  636. "Failed for adding devres for freeing irq vectors\n");
  637. return ret;
  638. }
  639. pci_set_master(pdev);
  640. ifcvf_mgmt_dev = kzalloc_obj(struct ifcvf_vdpa_mgmt_dev);
  641. if (!ifcvf_mgmt_dev) {
  642. IFCVF_ERR(pdev, "Failed to alloc memory for the vDPA management device\n");
  643. return -ENOMEM;
  644. }
  645. vf = &ifcvf_mgmt_dev->vf;
  646. vf->dev_type = get_dev_type(pdev);
  647. vf->base = pcim_iomap_table(pdev);
  648. vf->pdev = pdev;
  649. ret = ifcvf_init_hw(vf, pdev);
  650. if (ret) {
  651. IFCVF_ERR(pdev, "Failed to init IFCVF hw\n");
  652. goto err;
  653. }
  654. for (i = 0; i < vf->nr_vring; i++)
  655. vf->vring[i].irq = -EINVAL;
  656. vf->hw_features = ifcvf_get_hw_features(vf);
  657. vf->config_size = ifcvf_get_config_size(vf);
  658. dev_type = get_dev_type(pdev);
  659. switch (dev_type) {
  660. case VIRTIO_ID_NET:
  661. ifcvf_mgmt_dev->mdev.id_table = id_table_net;
  662. break;
  663. case VIRTIO_ID_BLOCK:
  664. ifcvf_mgmt_dev->mdev.id_table = id_table_blk;
  665. break;
  666. default:
  667. IFCVF_ERR(pdev, "VIRTIO ID %u not supported\n", dev_type);
  668. ret = -EOPNOTSUPP;
  669. goto err;
  670. }
  671. ifcvf_mgmt_dev->mdev.ops = &ifcvf_vdpa_mgmt_dev_ops;
  672. ifcvf_mgmt_dev->mdev.device = dev;
  673. ifcvf_mgmt_dev->mdev.max_supported_vqs = vf->nr_vring;
  674. ifcvf_mgmt_dev->mdev.supported_features = vf->hw_features;
  675. ifcvf_mgmt_dev->mdev.config_attr_mask = (1 << VDPA_ATTR_DEV_FEATURES);
  676. ret = vdpa_mgmtdev_register(&ifcvf_mgmt_dev->mdev);
  677. if (ret) {
  678. IFCVF_ERR(pdev,
  679. "Failed to initialize the management interfaces\n");
  680. goto err;
  681. }
  682. pci_set_drvdata(pdev, ifcvf_mgmt_dev);
  683. return 0;
  684. err:
  685. kfree(ifcvf_mgmt_dev->vf.vring);
  686. kfree(ifcvf_mgmt_dev);
  687. return ret;
  688. }
  689. static void ifcvf_remove(struct pci_dev *pdev)
  690. {
  691. struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev;
  692. ifcvf_mgmt_dev = pci_get_drvdata(pdev);
  693. vdpa_mgmtdev_unregister(&ifcvf_mgmt_dev->mdev);
  694. kfree(ifcvf_mgmt_dev->vf.vring);
  695. kfree(ifcvf_mgmt_dev);
  696. }
  697. static struct pci_device_id ifcvf_pci_ids[] = {
  698. /* N3000 network device */
  699. { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET,
  700. N3000_DEVICE_ID,
  701. PCI_VENDOR_ID_INTEL,
  702. N3000_SUBSYS_DEVICE_ID) },
  703. /* C5000X-PL network device
  704. * F2000X-PL network device
  705. */
  706. { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET,
  707. VIRTIO_TRANS_ID_NET,
  708. PCI_VENDOR_ID_INTEL,
  709. VIRTIO_ID_NET) },
  710. /* C5000X-PL block device */
  711. { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET,
  712. VIRTIO_TRANS_ID_BLOCK,
  713. PCI_VENDOR_ID_INTEL,
  714. VIRTIO_ID_BLOCK) },
  715. { 0 },
  716. };
  717. MODULE_DEVICE_TABLE(pci, ifcvf_pci_ids);
  718. static struct pci_driver ifcvf_driver = {
  719. .name = IFCVF_DRIVER_NAME,
  720. .id_table = ifcvf_pci_ids,
  721. .probe = ifcvf_probe,
  722. .remove = ifcvf_remove,
  723. };
  724. module_pci_driver(ifcvf_driver);
  725. MODULE_DESCRIPTION("Intel IFC VF NIC driver for virtio dataplane offloading");
  726. MODULE_LICENSE("GPL v2");