ionic_bus_pci.c 12 KB

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