pcie_bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2012-2016 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/suspend.h>
  21. #include "wil6210.h"
  22. #include <linux/rtnetlink.h>
  23. static bool use_msi = true;
  24. module_param(use_msi, bool, 0444);
  25. MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true");
  26. #ifdef CONFIG_PM
  27. #ifdef CONFIG_PM_SLEEP
  28. static int wil6210_pm_notify(struct notifier_block *notify_block,
  29. unsigned long mode, void *unused);
  30. #endif /* CONFIG_PM_SLEEP */
  31. #endif /* CONFIG_PM */
  32. static
  33. void wil_set_capabilities(struct wil6210_priv *wil)
  34. {
  35. u32 jtag_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
  36. u8 chip_revision = (wil_r(wil, RGF_USER_REVISION_ID) &
  37. RGF_USER_REVISION_ID_MASK);
  38. bitmap_zero(wil->hw_capabilities, hw_capability_last);
  39. bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
  40. wil->wil_fw_name = WIL_FW_NAME_DEFAULT;
  41. wil->chip_revision = chip_revision;
  42. switch (jtag_id) {
  43. case JTAG_DEV_ID_SPARROW:
  44. switch (chip_revision) {
  45. case REVISION_ID_SPARROW_D0:
  46. wil->hw_name = "Sparrow D0";
  47. wil->hw_version = HW_VER_SPARROW_D0;
  48. if (wil_fw_verify_file_exists(wil,
  49. WIL_FW_NAME_SPARROW_PLUS))
  50. wil->wil_fw_name = WIL_FW_NAME_SPARROW_PLUS;
  51. break;
  52. case REVISION_ID_SPARROW_B0:
  53. wil->hw_name = "Sparrow B0";
  54. wil->hw_version = HW_VER_SPARROW_B0;
  55. break;
  56. default:
  57. wil->hw_name = "Unknown";
  58. wil->hw_version = HW_VER_UNKNOWN;
  59. break;
  60. }
  61. break;
  62. default:
  63. wil_err(wil, "Unknown board hardware, chip_id 0x%08x, chip_revision 0x%08x\n",
  64. jtag_id, chip_revision);
  65. wil->hw_name = "Unknown";
  66. wil->hw_version = HW_VER_UNKNOWN;
  67. }
  68. wil_info(wil, "Board hardware is %s\n", wil->hw_name);
  69. /* extract FW capabilities from file without loading the FW */
  70. wil_request_firmware(wil, wil->wil_fw_name, false);
  71. }
  72. void wil_disable_irq(struct wil6210_priv *wil)
  73. {
  74. disable_irq(wil->pdev->irq);
  75. }
  76. void wil_enable_irq(struct wil6210_priv *wil)
  77. {
  78. enable_irq(wil->pdev->irq);
  79. }
  80. /* Bus ops */
  81. static int wil_if_pcie_enable(struct wil6210_priv *wil)
  82. {
  83. struct pci_dev *pdev = wil->pdev;
  84. int rc;
  85. /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
  86. * allow pci_enable_device to work. This indicates INTx was not routed
  87. * and only MSI should be used
  88. */
  89. int msi_only = pdev->msi_enabled;
  90. bool _use_msi = use_msi;
  91. bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
  92. wil->fw_capabilities);
  93. wil_dbg_misc(wil, "if_pcie_enable, wmi_only %d\n", wmi_only);
  94. pdev->msi_enabled = 0;
  95. pci_set_master(pdev);
  96. wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
  97. if (use_msi && pci_enable_msi(pdev)) {
  98. wil_err(wil, "pci_enable_msi failed, use INTx\n");
  99. _use_msi = false;
  100. }
  101. if (!_use_msi && msi_only) {
  102. wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
  103. rc = -ENODEV;
  104. goto stop_master;
  105. }
  106. rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
  107. if (rc)
  108. goto stop_master;
  109. /* need reset here to obtain MAC or in case of WMI-only FW, full reset
  110. * and fw loading takes place
  111. */
  112. mutex_lock(&wil->mutex);
  113. rc = wil_reset(wil, wmi_only);
  114. mutex_unlock(&wil->mutex);
  115. if (rc)
  116. goto release_irq;
  117. return 0;
  118. release_irq:
  119. wil6210_fini_irq(wil, pdev->irq);
  120. /* safe to call if no MSI */
  121. pci_disable_msi(pdev);
  122. stop_master:
  123. pci_clear_master(pdev);
  124. return rc;
  125. }
  126. static int wil_if_pcie_disable(struct wil6210_priv *wil)
  127. {
  128. struct pci_dev *pdev = wil->pdev;
  129. wil_dbg_misc(wil, "if_pcie_disable\n");
  130. pci_clear_master(pdev);
  131. /* disable and release IRQ */
  132. wil6210_fini_irq(wil, pdev->irq);
  133. /* safe to call if no MSI */
  134. pci_disable_msi(pdev);
  135. /* TODO: disable HW */
  136. return 0;
  137. }
  138. static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size)
  139. {
  140. struct wil6210_priv *wil = wil_handle;
  141. if (!wil)
  142. return -EINVAL;
  143. return wil_fw_copy_crash_dump(wil, buf, size);
  144. }
  145. static int wil_platform_rop_fw_recovery(void *wil_handle)
  146. {
  147. struct wil6210_priv *wil = wil_handle;
  148. if (!wil)
  149. return -EINVAL;
  150. wil_fw_error_recovery(wil);
  151. return 0;
  152. }
  153. static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  154. {
  155. struct wil6210_priv *wil;
  156. struct device *dev = &pdev->dev;
  157. int rc;
  158. const struct wil_platform_rops rops = {
  159. .ramdump = wil_platform_rop_ramdump,
  160. .fw_recovery = wil_platform_rop_fw_recovery,
  161. };
  162. /* check HW */
  163. dev_info(&pdev->dev, WIL_NAME
  164. " device found [%04x:%04x] (rev %x)\n",
  165. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  166. if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
  167. dev_err(&pdev->dev, "Not " WIL_NAME "? "
  168. "BAR0 size is %lu while expecting %lu\n",
  169. (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
  170. return -ENODEV;
  171. }
  172. wil = wil_if_alloc(dev);
  173. if (IS_ERR(wil)) {
  174. rc = (int)PTR_ERR(wil);
  175. dev_err(dev, "wil_if_alloc failed: %d\n", rc);
  176. return rc;
  177. }
  178. wil->pdev = pdev;
  179. pci_set_drvdata(pdev, wil);
  180. /* rollback to if_free */
  181. wil->platform_handle =
  182. wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil);
  183. if (!wil->platform_handle) {
  184. rc = -ENODEV;
  185. wil_err(wil, "wil_platform_init failed\n");
  186. goto if_free;
  187. }
  188. /* rollback to err_plat */
  189. rc = pci_enable_device(pdev);
  190. if (rc) {
  191. wil_err(wil,
  192. "pci_enable_device failed, retry with MSI only\n");
  193. /* Work around for platforms that can't allocate IRQ:
  194. * retry with MSI only
  195. */
  196. pdev->msi_enabled = 1;
  197. rc = pci_enable_device(pdev);
  198. }
  199. if (rc) {
  200. wil_err(wil,
  201. "pci_enable_device failed, even with MSI only\n");
  202. goto err_plat;
  203. }
  204. /* rollback to err_disable_pdev */
  205. rc = pci_request_region(pdev, 0, WIL_NAME);
  206. if (rc) {
  207. wil_err(wil, "pci_request_region failed\n");
  208. goto err_disable_pdev;
  209. }
  210. /* rollback to err_release_reg */
  211. wil->csr = pci_ioremap_bar(pdev, 0);
  212. if (!wil->csr) {
  213. wil_err(wil, "pci_ioremap_bar failed\n");
  214. rc = -ENODEV;
  215. goto err_release_reg;
  216. }
  217. /* rollback to err_iounmap */
  218. wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
  219. wil_set_capabilities(wil);
  220. wil6210_clear_irq(wil);
  221. /* FW should raise IRQ when ready */
  222. rc = wil_if_pcie_enable(wil);
  223. if (rc) {
  224. wil_err(wil, "Enable device failed\n");
  225. goto err_iounmap;
  226. }
  227. /* rollback to bus_disable */
  228. rc = wil_if_add(wil);
  229. if (rc) {
  230. wil_err(wil, "wil_if_add failed: %d\n", rc);
  231. goto bus_disable;
  232. }
  233. #ifdef CONFIG_PM
  234. #ifdef CONFIG_PM_SLEEP
  235. wil->pm_notify.notifier_call = wil6210_pm_notify;
  236. rc = register_pm_notifier(&wil->pm_notify);
  237. if (rc)
  238. /* Do not fail the driver initialization, as suspend can
  239. * be prevented in a later phase if needed
  240. */
  241. wil_err(wil, "register_pm_notifier failed: %d\n", rc);
  242. #endif /* CONFIG_PM_SLEEP */
  243. #endif /* CONFIG_PM */
  244. wil6210_debugfs_init(wil);
  245. return 0;
  246. bus_disable:
  247. wil_if_pcie_disable(wil);
  248. err_iounmap:
  249. pci_iounmap(pdev, wil->csr);
  250. err_release_reg:
  251. pci_release_region(pdev, 0);
  252. err_disable_pdev:
  253. pci_disable_device(pdev);
  254. err_plat:
  255. if (wil->platform_ops.uninit)
  256. wil->platform_ops.uninit(wil->platform_handle);
  257. if_free:
  258. wil_if_free(wil);
  259. return rc;
  260. }
  261. static void wil_pcie_remove(struct pci_dev *pdev)
  262. {
  263. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  264. void __iomem *csr = wil->csr;
  265. wil_dbg_misc(wil, "pcie_remove\n");
  266. #ifdef CONFIG_PM
  267. #ifdef CONFIG_PM_SLEEP
  268. unregister_pm_notifier(&wil->pm_notify);
  269. #endif /* CONFIG_PM_SLEEP */
  270. #endif /* CONFIG_PM */
  271. wil6210_debugfs_remove(wil);
  272. rtnl_lock();
  273. wil_p2p_wdev_free(wil);
  274. rtnl_unlock();
  275. wil_if_remove(wil);
  276. wil_if_pcie_disable(wil);
  277. pci_iounmap(pdev, csr);
  278. pci_release_region(pdev, 0);
  279. pci_disable_device(pdev);
  280. if (wil->platform_ops.uninit)
  281. wil->platform_ops.uninit(wil->platform_handle);
  282. wil_if_free(wil);
  283. }
  284. static const struct pci_device_id wil6210_pcie_ids[] = {
  285. { PCI_DEVICE(0x1ae9, 0x0310) },
  286. { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
  287. { /* end: all zeroes */ },
  288. };
  289. MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
  290. #ifdef CONFIG_PM
  291. #ifdef CONFIG_PM_SLEEP
  292. static int wil6210_suspend(struct device *dev, bool is_runtime)
  293. {
  294. int rc = 0;
  295. struct pci_dev *pdev = to_pci_dev(dev);
  296. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  297. wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
  298. rc = wil_can_suspend(wil, is_runtime);
  299. if (rc)
  300. goto out;
  301. rc = wil_suspend(wil, is_runtime);
  302. if (rc)
  303. goto out;
  304. /* TODO: how do I bring card in low power state? */
  305. /* disable bus mastering */
  306. pci_clear_master(pdev);
  307. /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
  308. out:
  309. return rc;
  310. }
  311. static int wil6210_resume(struct device *dev, bool is_runtime)
  312. {
  313. int rc = 0;
  314. struct pci_dev *pdev = to_pci_dev(dev);
  315. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  316. wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
  317. /* allow master */
  318. pci_set_master(pdev);
  319. rc = wil_resume(wil, is_runtime);
  320. if (rc)
  321. pci_clear_master(pdev);
  322. return rc;
  323. }
  324. static int wil6210_pm_notify(struct notifier_block *notify_block,
  325. unsigned long mode, void *unused)
  326. {
  327. struct wil6210_priv *wil = container_of(
  328. notify_block, struct wil6210_priv, pm_notify);
  329. int rc = 0;
  330. enum wil_platform_event evt;
  331. wil_dbg_pm(wil, "pm_notify: mode (%ld)\n", mode);
  332. switch (mode) {
  333. case PM_HIBERNATION_PREPARE:
  334. case PM_SUSPEND_PREPARE:
  335. case PM_RESTORE_PREPARE:
  336. rc = wil_can_suspend(wil, false);
  337. if (rc)
  338. break;
  339. evt = WIL_PLATFORM_EVT_PRE_SUSPEND;
  340. if (wil->platform_ops.notify)
  341. rc = wil->platform_ops.notify(wil->platform_handle,
  342. evt);
  343. break;
  344. case PM_POST_SUSPEND:
  345. case PM_POST_HIBERNATION:
  346. case PM_POST_RESTORE:
  347. evt = WIL_PLATFORM_EVT_POST_SUSPEND;
  348. if (wil->platform_ops.notify)
  349. rc = wil->platform_ops.notify(wil->platform_handle,
  350. evt);
  351. break;
  352. default:
  353. wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode);
  354. break;
  355. }
  356. wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc);
  357. return rc;
  358. }
  359. static int wil6210_pm_suspend(struct device *dev)
  360. {
  361. return wil6210_suspend(dev, false);
  362. }
  363. static int wil6210_pm_resume(struct device *dev)
  364. {
  365. return wil6210_resume(dev, false);
  366. }
  367. #endif /* CONFIG_PM_SLEEP */
  368. #endif /* CONFIG_PM */
  369. static const struct dev_pm_ops wil6210_pm_ops = {
  370. SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
  371. };
  372. static struct pci_driver wil6210_driver = {
  373. .probe = wil_pcie_probe,
  374. .remove = wil_pcie_remove,
  375. .id_table = wil6210_pcie_ids,
  376. .name = WIL_NAME,
  377. .driver = {
  378. .pm = &wil6210_pm_ops,
  379. },
  380. };
  381. static int __init wil6210_driver_init(void)
  382. {
  383. int rc;
  384. rc = wil_platform_modinit();
  385. if (rc)
  386. return rc;
  387. rc = pci_register_driver(&wil6210_driver);
  388. if (rc)
  389. wil_platform_modexit();
  390. return rc;
  391. }
  392. module_init(wil6210_driver_init);
  393. static void __exit wil6210_driver_exit(void)
  394. {
  395. pci_unregister_driver(&wil6210_driver);
  396. wil_platform_modexit();
  397. }
  398. module_exit(wil6210_driver_exit);
  399. MODULE_LICENSE("Dual BSD/GPL");
  400. MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
  401. MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");