ionic_bus_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/module.h>
  4. #include <linux/netdevice.h>
  5. #include <linux/etherdevice.h>
  6. #include <linux/pci.h>
  7. #include "ionic.h"
  8. #include "ionic_bus.h"
  9. #include "ionic_lif.h"
  10. #include "ionic_debugfs.h"
  11. /* Supported devices */
  12. static const struct pci_device_id ionic_id_table[] = {
  13. { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_IONIC_ETH_PF) },
  14. { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_IONIC_ETH_VF) },
  15. { 0, } /* end of table */
  16. };
  17. MODULE_DEVICE_TABLE(pci, ionic_id_table);
  18. int ionic_bus_get_irq(struct ionic *ionic, unsigned int num)
  19. {
  20. return pci_irq_vector(ionic->pdev, num);
  21. }
  22. const char *ionic_bus_info(struct ionic *ionic)
  23. {
  24. return pci_name(ionic->pdev);
  25. }
  26. int ionic_bus_alloc_irq_vectors(struct ionic *ionic, unsigned int nintrs)
  27. {
  28. return pci_alloc_irq_vectors(ionic->pdev, nintrs, nintrs,
  29. PCI_IRQ_MSIX);
  30. }
  31. void ionic_bus_free_irq_vectors(struct ionic *ionic)
  32. {
  33. if (!ionic->nintrs)
  34. return;
  35. pci_free_irq_vectors(ionic->pdev);
  36. }
  37. static int ionic_map_bars(struct ionic *ionic)
  38. {
  39. struct pci_dev *pdev = ionic->pdev;
  40. struct device *dev = ionic->dev;
  41. struct ionic_dev_bar *bars;
  42. unsigned int i, j;
  43. bars = ionic->bars;
  44. ionic->num_bars = 0;
  45. for (i = 0, j = 0; i < IONIC_BARS_MAX; i++) {
  46. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  47. continue;
  48. bars[j].len = pci_resource_len(pdev, i);
  49. /* only map the whole bar 0 */
  50. if (j > 0) {
  51. bars[j].vaddr = NULL;
  52. } else {
  53. bars[j].vaddr = pci_iomap(pdev, i, bars[j].len);
  54. if (!bars[j].vaddr) {
  55. dev_err(dev,
  56. "Cannot memory-map BAR %d, aborting\n",
  57. i);
  58. return -ENODEV;
  59. }
  60. }
  61. bars[j].bus_addr = pci_resource_start(pdev, i);
  62. bars[j].res_index = i;
  63. ionic->num_bars++;
  64. j++;
  65. }
  66. return 0;
  67. }
  68. static void ionic_unmap_bars(struct ionic *ionic)
  69. {
  70. struct ionic_dev_bar *bars = ionic->bars;
  71. unsigned int i;
  72. for (i = 0; i < IONIC_BARS_MAX; i++) {
  73. if (bars[i].vaddr) {
  74. iounmap(bars[i].vaddr);
  75. bars[i].bus_addr = 0;
  76. bars[i].vaddr = NULL;
  77. bars[i].len = 0;
  78. }
  79. }
  80. ionic->num_bars = 0;
  81. }
  82. void __iomem *ionic_bus_map_dbpage(struct ionic *ionic, int page_num)
  83. {
  84. return pci_iomap_range(ionic->pdev,
  85. ionic->bars[IONIC_PCI_BAR_DBELL].res_index,
  86. (u64)page_num << PAGE_SHIFT, PAGE_SIZE);
  87. }
  88. void ionic_bus_unmap_dbpage(struct ionic *ionic, void __iomem *page)
  89. {
  90. iounmap(page);
  91. }
  92. static void ionic_vf_dealloc_locked(struct ionic *ionic)
  93. {
  94. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_STATSADDR };
  95. struct ionic_vf *v;
  96. int i;
  97. if (!ionic->vfs)
  98. return;
  99. for (i = ionic->num_vfs - 1; i >= 0; i--) {
  100. v = &ionic->vfs[i];
  101. if (v->stats_pa) {
  102. vfc.stats_pa = 0;
  103. ionic_set_vf_config(ionic, i, &vfc);
  104. dma_unmap_single(ionic->dev, v->stats_pa,
  105. sizeof(v->stats), DMA_FROM_DEVICE);
  106. v->stats_pa = 0;
  107. }
  108. }
  109. kfree(ionic->vfs);
  110. ionic->vfs = NULL;
  111. ionic->num_vfs = 0;
  112. }
  113. static void ionic_vf_dealloc(struct ionic *ionic)
  114. {
  115. down_write(&ionic->vf_op_lock);
  116. ionic_vf_dealloc_locked(ionic);
  117. up_write(&ionic->vf_op_lock);
  118. }
  119. static int ionic_vf_alloc(struct ionic *ionic, int num_vfs)
  120. {
  121. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_STATSADDR };
  122. struct ionic_vf *v;
  123. int err = 0;
  124. int i;
  125. down_write(&ionic->vf_op_lock);
  126. ionic->vfs = kcalloc(num_vfs, sizeof(struct ionic_vf), GFP_KERNEL);
  127. if (!ionic->vfs) {
  128. err = -ENOMEM;
  129. goto out;
  130. }
  131. for (i = 0; i < num_vfs; i++) {
  132. v = &ionic->vfs[i];
  133. v->stats_pa = dma_map_single(ionic->dev, &v->stats,
  134. sizeof(v->stats), DMA_FROM_DEVICE);
  135. if (dma_mapping_error(ionic->dev, v->stats_pa)) {
  136. v->stats_pa = 0;
  137. err = -ENODEV;
  138. goto out;
  139. }
  140. ionic->num_vfs++;
  141. /* ignore failures from older FW, we just won't get stats */
  142. vfc.stats_pa = cpu_to_le64(v->stats_pa);
  143. ionic_set_vf_config(ionic, i, &vfc);
  144. }
  145. out:
  146. if (err)
  147. ionic_vf_dealloc_locked(ionic);
  148. up_write(&ionic->vf_op_lock);
  149. return err;
  150. }
  151. static int ionic_sriov_configure(struct pci_dev *pdev, int num_vfs)
  152. {
  153. struct ionic *ionic = pci_get_drvdata(pdev);
  154. struct device *dev = ionic->dev;
  155. int ret = 0;
  156. if (ionic->lif &&
  157. test_bit(IONIC_LIF_F_FW_RESET, ionic->lif->state))
  158. return -EBUSY;
  159. if (num_vfs > 0) {
  160. ret = pci_enable_sriov(pdev, num_vfs);
  161. if (ret) {
  162. dev_err(dev, "Cannot enable SRIOV: %d\n", ret);
  163. goto out;
  164. }
  165. ret = ionic_vf_alloc(ionic, num_vfs);
  166. if (ret) {
  167. dev_err(dev, "Cannot alloc VFs: %d\n", ret);
  168. pci_disable_sriov(pdev);
  169. goto out;
  170. }
  171. ret = num_vfs;
  172. } else {
  173. pci_disable_sriov(pdev);
  174. ionic_vf_dealloc(ionic);
  175. }
  176. out:
  177. return ret;
  178. }
  179. static void ionic_clear_pci(struct ionic *ionic)
  180. {
  181. if (ionic->num_bars) {
  182. ionic->idev.dev_info_regs = NULL;
  183. ionic->idev.dev_cmd_regs = NULL;
  184. ionic->idev.intr_status = NULL;
  185. ionic->idev.intr_ctrl = NULL;
  186. ionic_unmap_bars(ionic);
  187. pci_release_regions(ionic->pdev);
  188. }
  189. if (pci_is_enabled(ionic->pdev))
  190. pci_disable_device(ionic->pdev);
  191. }
  192. static int ionic_setup_one(struct ionic *ionic)
  193. {
  194. struct pci_dev *pdev = ionic->pdev;
  195. struct device *dev = ionic->dev;
  196. int err;
  197. ionic_debugfs_add_dev(ionic);
  198. /* Setup PCI device */
  199. err = pci_enable_device_mem(pdev);
  200. if (err) {
  201. dev_err(dev, "Cannot enable PCI device: %d, aborting\n", err);
  202. goto err_out_debugfs_del_dev;
  203. }
  204. err = pci_request_regions(pdev, IONIC_DRV_NAME);
  205. if (err) {
  206. dev_err(dev, "Cannot request PCI regions: %d, aborting\n", err);
  207. goto err_out_clear_pci;
  208. }
  209. pcie_print_link_status(pdev);
  210. err = ionic_map_bars(ionic);
  211. if (err)
  212. goto err_out_clear_pci;
  213. /* Configure the device */
  214. err = ionic_setup(ionic);
  215. if (err) {
  216. dev_err(dev, "Cannot setup device: %d, aborting\n", err);
  217. goto err_out_clear_pci;
  218. }
  219. pci_set_master(pdev);
  220. err = ionic_identify(ionic);
  221. if (err) {
  222. dev_err(dev, "Cannot identify device: %d, aborting\n", err);
  223. goto err_out_teardown;
  224. }
  225. ionic_debugfs_add_ident(ionic);
  226. err = ionic_init(ionic);
  227. if (err) {
  228. dev_err(dev, "Cannot init device: %d, aborting\n", err);
  229. goto err_out_teardown;
  230. }
  231. /* Configure the port */
  232. err = ionic_port_identify(ionic);
  233. if (err) {
  234. dev_err(dev, "Cannot identify port: %d, aborting\n", err);
  235. goto err_out_teardown;
  236. }
  237. err = ionic_port_init(ionic);
  238. if (err) {
  239. dev_err(dev, "Cannot init port: %d, aborting\n", err);
  240. goto err_out_teardown;
  241. }
  242. return 0;
  243. err_out_teardown:
  244. ionic_dev_teardown(ionic);
  245. err_out_clear_pci:
  246. ionic_clear_pci(ionic);
  247. err_out_debugfs_del_dev:
  248. ionic_debugfs_del_dev(ionic);
  249. return err;
  250. }
  251. static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  252. {
  253. struct device *dev = &pdev->dev;
  254. struct ionic *ionic;
  255. int num_vfs;
  256. int err;
  257. ionic = ionic_devlink_alloc(dev);
  258. if (!ionic)
  259. return -ENOMEM;
  260. ionic->pdev = pdev;
  261. ionic->dev = dev;
  262. pci_set_drvdata(pdev, ionic);
  263. mutex_init(&ionic->dev_cmd_lock);
  264. /* Query system for DMA addressing limitation for the device. */
  265. err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(IONIC_ADDR_LEN));
  266. if (err) {
  267. dev_err(dev, "Unable to obtain 64-bit DMA for consistent allocations, aborting. err=%d\n",
  268. err);
  269. goto err_out;
  270. }
  271. #ifdef CONFIG_PPC64
  272. /* Ensure MSI/MSI-X interrupts lie within addressable physical memory */
  273. pdev->no_64bit_msi = 1;
  274. #endif
  275. err = ionic_setup_one(ionic);
  276. if (err)
  277. goto err_out;
  278. /* Allocate and init the LIF */
  279. err = ionic_lif_size(ionic);
  280. if (err) {
  281. dev_err(dev, "Cannot size LIF: %d, aborting\n", err);
  282. goto err_out_pci;
  283. }
  284. err = ionic_lif_alloc(ionic);
  285. if (err) {
  286. dev_err(dev, "Cannot allocate LIF: %d, aborting\n", err);
  287. goto err_out_free_irqs;
  288. }
  289. err = ionic_lif_init(ionic->lif);
  290. if (err) {
  291. dev_err(dev, "Cannot init LIF: %d, aborting\n", err);
  292. goto err_out_free_lifs;
  293. }
  294. init_rwsem(&ionic->vf_op_lock);
  295. num_vfs = pci_num_vf(pdev);
  296. if (num_vfs) {
  297. dev_info(dev, "%d VFs found already enabled\n", num_vfs);
  298. err = ionic_vf_alloc(ionic, num_vfs);
  299. if (err)
  300. dev_err(dev, "Cannot enable existing VFs: %d\n", err);
  301. }
  302. err = ionic_devlink_register(ionic);
  303. if (err) {
  304. dev_err(dev, "Cannot register devlink: %d\n", err);
  305. goto err_out_deinit_lifs;
  306. }
  307. err = ionic_lif_register(ionic->lif);
  308. if (err) {
  309. dev_err(dev, "Cannot register LIF: %d, aborting\n", err);
  310. goto err_out_deregister_devlink;
  311. }
  312. mod_timer(&ionic->watchdog_timer,
  313. round_jiffies(jiffies + ionic->watchdog_period));
  314. ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
  315. return 0;
  316. err_out_deregister_devlink:
  317. ionic_devlink_unregister(ionic);
  318. err_out_deinit_lifs:
  319. ionic_vf_dealloc(ionic);
  320. ionic_lif_deinit(ionic->lif);
  321. err_out_free_lifs:
  322. ionic_lif_free(ionic->lif);
  323. ionic->lif = NULL;
  324. err_out_free_irqs:
  325. ionic_bus_free_irq_vectors(ionic);
  326. err_out_pci:
  327. ionic_dev_teardown(ionic);
  328. ionic_clear_pci(ionic);
  329. ionic_debugfs_del_dev(ionic);
  330. err_out:
  331. mutex_destroy(&ionic->dev_cmd_lock);
  332. ionic_devlink_free(ionic);
  333. return err;
  334. }
  335. static void ionic_remove(struct pci_dev *pdev)
  336. {
  337. struct ionic *ionic = pci_get_drvdata(pdev);
  338. timer_shutdown_sync(&ionic->watchdog_timer);
  339. if (ionic->lif) {
  340. /* prevent adminq cmds if already known as down */
  341. if (test_and_clear_bit(IONIC_LIF_F_FW_RESET, ionic->lif->state))
  342. set_bit(IONIC_LIF_F_FW_STOPPING, ionic->lif->state);
  343. if (ionic->lif->doorbell_wa)
  344. cancel_delayed_work_sync(&ionic->doorbell_check_dwork);
  345. ionic_lif_unregister(ionic->lif);
  346. ionic_devlink_unregister(ionic);
  347. ionic_lif_deinit(ionic->lif);
  348. ionic_lif_free(ionic->lif);
  349. ionic->lif = NULL;
  350. ionic_bus_free_irq_vectors(ionic);
  351. }
  352. ionic_port_reset(ionic);
  353. ionic_reset(ionic);
  354. ionic_dev_teardown(ionic);
  355. ionic_clear_pci(ionic);
  356. ionic_debugfs_del_dev(ionic);
  357. mutex_destroy(&ionic->dev_cmd_lock);
  358. ionic_devlink_free(ionic);
  359. }
  360. static void ionic_reset_prepare(struct pci_dev *pdev)
  361. {
  362. struct ionic *ionic = pci_get_drvdata(pdev);
  363. struct ionic_lif *lif = ionic->lif;
  364. dev_dbg(ionic->dev, "%s: device stopping\n", __func__);
  365. set_bit(IONIC_LIF_F_FW_RESET, lif->state);
  366. del_timer_sync(&ionic->watchdog_timer);
  367. cancel_work_sync(&lif->deferred.work);
  368. mutex_lock(&lif->queue_lock);
  369. ionic_stop_queues_reconfig(lif);
  370. ionic_txrx_free(lif);
  371. ionic_lif_deinit(lif);
  372. ionic_qcqs_free(lif);
  373. ionic_debugfs_del_lif(lif);
  374. mutex_unlock(&lif->queue_lock);
  375. ionic_dev_teardown(ionic);
  376. ionic_clear_pci(ionic);
  377. ionic_debugfs_del_dev(ionic);
  378. }
  379. static void ionic_reset_done(struct pci_dev *pdev)
  380. {
  381. struct ionic *ionic = pci_get_drvdata(pdev);
  382. struct ionic_lif *lif = ionic->lif;
  383. int err;
  384. err = ionic_setup_one(ionic);
  385. if (err)
  386. goto err_out;
  387. ionic_debugfs_add_sizes(ionic);
  388. ionic_debugfs_add_lif(ionic->lif);
  389. err = ionic_restart_lif(lif);
  390. if (err)
  391. goto err_out;
  392. mod_timer(&ionic->watchdog_timer, jiffies + 1);
  393. err_out:
  394. dev_dbg(ionic->dev, "%s: device recovery %s\n",
  395. __func__, err ? "failed" : "done");
  396. }
  397. static pci_ers_result_t ionic_pci_error_detected(struct pci_dev *pdev,
  398. pci_channel_state_t error)
  399. {
  400. if (error == pci_channel_io_frozen) {
  401. ionic_reset_prepare(pdev);
  402. return PCI_ERS_RESULT_NEED_RESET;
  403. }
  404. return PCI_ERS_RESULT_NONE;
  405. }
  406. static void ionic_pci_error_resume(struct pci_dev *pdev)
  407. {
  408. struct ionic *ionic = pci_get_drvdata(pdev);
  409. struct ionic_lif *lif = ionic->lif;
  410. if (lif && test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  411. pci_reset_function_locked(pdev);
  412. }
  413. static const struct pci_error_handlers ionic_err_handler = {
  414. /* FLR handling */
  415. .reset_prepare = ionic_reset_prepare,
  416. .reset_done = ionic_reset_done,
  417. /* PCI bus error detected on this device */
  418. .error_detected = ionic_pci_error_detected,
  419. .resume = ionic_pci_error_resume,
  420. };
  421. static struct pci_driver ionic_driver = {
  422. .name = IONIC_DRV_NAME,
  423. .id_table = ionic_id_table,
  424. .probe = ionic_probe,
  425. .remove = ionic_remove,
  426. .sriov_configure = ionic_sriov_configure,
  427. .err_handler = &ionic_err_handler
  428. };
  429. int ionic_bus_register_driver(void)
  430. {
  431. return pci_register_driver(&ionic_driver);
  432. }
  433. void ionic_bus_unregister_driver(void)
  434. {
  435. pci_unregister_driver(&ionic_driver);
  436. }