pcie_bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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, S_IRUGO);
  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 rev_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
  36. bitmap_zero(wil->hw_capabilities, hw_capability_last);
  37. bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
  38. switch (rev_id) {
  39. case JTAG_DEV_ID_SPARROW_B0:
  40. wil->hw_name = "Sparrow B0";
  41. wil->hw_version = HW_VER_SPARROW_B0;
  42. break;
  43. default:
  44. wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id);
  45. wil->hw_name = "Unknown";
  46. wil->hw_version = HW_VER_UNKNOWN;
  47. }
  48. wil_info(wil, "Board hardware is %s\n", wil->hw_name);
  49. /* extract FW capabilities from file without loading the FW */
  50. wil_request_firmware(wil, WIL_FW_NAME, false);
  51. }
  52. void wil_disable_irq(struct wil6210_priv *wil)
  53. {
  54. disable_irq(wil->pdev->irq);
  55. }
  56. void wil_enable_irq(struct wil6210_priv *wil)
  57. {
  58. enable_irq(wil->pdev->irq);
  59. }
  60. /* Bus ops */
  61. static int wil_if_pcie_enable(struct wil6210_priv *wil)
  62. {
  63. struct pci_dev *pdev = wil->pdev;
  64. int rc;
  65. /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
  66. * allow pci_enable_device to work. This indicates INTx was not routed
  67. * and only MSI should be used
  68. */
  69. int msi_only = pdev->msi_enabled;
  70. bool _use_msi = use_msi;
  71. wil_dbg_misc(wil, "%s()\n", __func__);
  72. pdev->msi_enabled = 0;
  73. pci_set_master(pdev);
  74. wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
  75. if (use_msi && pci_enable_msi(pdev)) {
  76. wil_err(wil, "pci_enable_msi failed, use INTx\n");
  77. _use_msi = false;
  78. }
  79. if (!_use_msi && msi_only) {
  80. wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
  81. rc = -ENODEV;
  82. goto stop_master;
  83. }
  84. rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
  85. if (rc)
  86. goto stop_master;
  87. /* need reset here to obtain MAC */
  88. mutex_lock(&wil->mutex);
  89. rc = wil_reset(wil, false);
  90. mutex_unlock(&wil->mutex);
  91. if (rc)
  92. goto release_irq;
  93. return 0;
  94. release_irq:
  95. wil6210_fini_irq(wil, pdev->irq);
  96. /* safe to call if no MSI */
  97. pci_disable_msi(pdev);
  98. stop_master:
  99. pci_clear_master(pdev);
  100. return rc;
  101. }
  102. static int wil_if_pcie_disable(struct wil6210_priv *wil)
  103. {
  104. struct pci_dev *pdev = wil->pdev;
  105. wil_dbg_misc(wil, "%s()\n", __func__);
  106. pci_clear_master(pdev);
  107. /* disable and release IRQ */
  108. wil6210_fini_irq(wil, pdev->irq);
  109. /* safe to call if no MSI */
  110. pci_disable_msi(pdev);
  111. /* TODO: disable HW */
  112. return 0;
  113. }
  114. static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size)
  115. {
  116. struct wil6210_priv *wil = wil_handle;
  117. if (!wil)
  118. return -EINVAL;
  119. return wil_fw_copy_crash_dump(wil, buf, size);
  120. }
  121. static int wil_platform_rop_fw_recovery(void *wil_handle)
  122. {
  123. struct wil6210_priv *wil = wil_handle;
  124. if (!wil)
  125. return -EINVAL;
  126. wil_fw_error_recovery(wil);
  127. return 0;
  128. }
  129. static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  130. {
  131. struct wil6210_priv *wil;
  132. struct device *dev = &pdev->dev;
  133. int rc;
  134. const struct wil_platform_rops rops = {
  135. .ramdump = wil_platform_rop_ramdump,
  136. .fw_recovery = wil_platform_rop_fw_recovery,
  137. };
  138. /* check HW */
  139. dev_info(&pdev->dev, WIL_NAME
  140. " device found [%04x:%04x] (rev %x)\n",
  141. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  142. if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
  143. dev_err(&pdev->dev, "Not " WIL_NAME "? "
  144. "BAR0 size is %lu while expecting %lu\n",
  145. (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
  146. return -ENODEV;
  147. }
  148. wil = wil_if_alloc(dev);
  149. if (IS_ERR(wil)) {
  150. rc = (int)PTR_ERR(wil);
  151. dev_err(dev, "wil_if_alloc failed: %d\n", rc);
  152. return rc;
  153. }
  154. wil->pdev = pdev;
  155. pci_set_drvdata(pdev, wil);
  156. /* rollback to if_free */
  157. wil->platform_handle =
  158. wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil);
  159. if (!wil->platform_handle) {
  160. rc = -ENODEV;
  161. wil_err(wil, "wil_platform_init failed\n");
  162. goto if_free;
  163. }
  164. /* rollback to err_plat */
  165. rc = pci_enable_device(pdev);
  166. if (rc) {
  167. wil_err(wil,
  168. "pci_enable_device failed, retry with MSI only\n");
  169. /* Work around for platforms that can't allocate IRQ:
  170. * retry with MSI only
  171. */
  172. pdev->msi_enabled = 1;
  173. rc = pci_enable_device(pdev);
  174. }
  175. if (rc) {
  176. wil_err(wil,
  177. "pci_enable_device failed, even with MSI only\n");
  178. goto err_plat;
  179. }
  180. /* rollback to err_disable_pdev */
  181. rc = pci_request_region(pdev, 0, WIL_NAME);
  182. if (rc) {
  183. wil_err(wil, "pci_request_region failed\n");
  184. goto err_disable_pdev;
  185. }
  186. /* rollback to err_release_reg */
  187. wil->csr = pci_ioremap_bar(pdev, 0);
  188. if (!wil->csr) {
  189. wil_err(wil, "pci_ioremap_bar failed\n");
  190. rc = -ENODEV;
  191. goto err_release_reg;
  192. }
  193. /* rollback to err_iounmap */
  194. wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
  195. wil_set_capabilities(wil);
  196. wil6210_clear_irq(wil);
  197. /* FW should raise IRQ when ready */
  198. rc = wil_if_pcie_enable(wil);
  199. if (rc) {
  200. wil_err(wil, "Enable device failed\n");
  201. goto err_iounmap;
  202. }
  203. /* rollback to bus_disable */
  204. rc = wil_if_add(wil);
  205. if (rc) {
  206. wil_err(wil, "wil_if_add failed: %d\n", rc);
  207. goto bus_disable;
  208. }
  209. #ifdef CONFIG_PM
  210. #ifdef CONFIG_PM_SLEEP
  211. wil->pm_notify.notifier_call = wil6210_pm_notify;
  212. rc = register_pm_notifier(&wil->pm_notify);
  213. if (rc)
  214. /* Do not fail the driver initialization, as suspend can
  215. * be prevented in a later phase if needed
  216. */
  217. wil_err(wil, "register_pm_notifier failed: %d\n", rc);
  218. #endif /* CONFIG_PM_SLEEP */
  219. #endif /* CONFIG_PM */
  220. wil6210_debugfs_init(wil);
  221. return 0;
  222. bus_disable:
  223. wil_if_pcie_disable(wil);
  224. err_iounmap:
  225. pci_iounmap(pdev, wil->csr);
  226. err_release_reg:
  227. pci_release_region(pdev, 0);
  228. err_disable_pdev:
  229. pci_disable_device(pdev);
  230. err_plat:
  231. if (wil->platform_ops.uninit)
  232. wil->platform_ops.uninit(wil->platform_handle);
  233. if_free:
  234. wil_if_free(wil);
  235. return rc;
  236. }
  237. static void wil_pcie_remove(struct pci_dev *pdev)
  238. {
  239. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  240. void __iomem *csr = wil->csr;
  241. wil_dbg_misc(wil, "%s()\n", __func__);
  242. #ifdef CONFIG_PM
  243. #ifdef CONFIG_PM_SLEEP
  244. unregister_pm_notifier(&wil->pm_notify);
  245. #endif /* CONFIG_PM_SLEEP */
  246. #endif /* CONFIG_PM */
  247. wil6210_debugfs_remove(wil);
  248. rtnl_lock();
  249. wil_p2p_wdev_free(wil);
  250. rtnl_unlock();
  251. wil_if_remove(wil);
  252. wil_if_pcie_disable(wil);
  253. pci_iounmap(pdev, csr);
  254. pci_release_region(pdev, 0);
  255. pci_disable_device(pdev);
  256. if (wil->platform_ops.uninit)
  257. wil->platform_ops.uninit(wil->platform_handle);
  258. wil_if_free(wil);
  259. }
  260. static const struct pci_device_id wil6210_pcie_ids[] = {
  261. { PCI_DEVICE(0x1ae9, 0x0310) },
  262. { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
  263. { /* end: all zeroes */ },
  264. };
  265. MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
  266. #ifdef CONFIG_PM
  267. #ifdef CONFIG_PM_SLEEP
  268. static int wil6210_suspend(struct device *dev, bool is_runtime)
  269. {
  270. int rc = 0;
  271. struct pci_dev *pdev = to_pci_dev(dev);
  272. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  273. wil_dbg_pm(wil, "%s(%s)\n", __func__,
  274. is_runtime ? "runtime" : "system");
  275. rc = wil_can_suspend(wil, is_runtime);
  276. if (rc)
  277. goto out;
  278. rc = wil_suspend(wil, is_runtime);
  279. if (rc)
  280. goto out;
  281. /* TODO: how do I bring card in low power state? */
  282. /* disable bus mastering */
  283. pci_clear_master(pdev);
  284. /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
  285. out:
  286. return rc;
  287. }
  288. static int wil6210_resume(struct device *dev, bool is_runtime)
  289. {
  290. int rc = 0;
  291. struct pci_dev *pdev = to_pci_dev(dev);
  292. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  293. wil_dbg_pm(wil, "%s(%s)\n", __func__,
  294. is_runtime ? "runtime" : "system");
  295. /* allow master */
  296. pci_set_master(pdev);
  297. rc = wil_resume(wil, is_runtime);
  298. if (rc)
  299. pci_clear_master(pdev);
  300. return rc;
  301. }
  302. static int wil6210_pm_notify(struct notifier_block *notify_block,
  303. unsigned long mode, void *unused)
  304. {
  305. struct wil6210_priv *wil = container_of(
  306. notify_block, struct wil6210_priv, pm_notify);
  307. int rc = 0;
  308. enum wil_platform_event evt;
  309. wil_dbg_pm(wil, "%s: mode (%ld)\n", __func__, mode);
  310. switch (mode) {
  311. case PM_HIBERNATION_PREPARE:
  312. case PM_SUSPEND_PREPARE:
  313. case PM_RESTORE_PREPARE:
  314. rc = wil_can_suspend(wil, false);
  315. if (rc)
  316. break;
  317. evt = WIL_PLATFORM_EVT_PRE_SUSPEND;
  318. if (wil->platform_ops.notify)
  319. rc = wil->platform_ops.notify(wil->platform_handle,
  320. evt);
  321. break;
  322. case PM_POST_SUSPEND:
  323. case PM_POST_HIBERNATION:
  324. case PM_POST_RESTORE:
  325. evt = WIL_PLATFORM_EVT_POST_SUSPEND;
  326. if (wil->platform_ops.notify)
  327. rc = wil->platform_ops.notify(wil->platform_handle,
  328. evt);
  329. break;
  330. default:
  331. wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode);
  332. break;
  333. }
  334. wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc);
  335. return rc;
  336. }
  337. static int wil6210_pm_suspend(struct device *dev)
  338. {
  339. return wil6210_suspend(dev, false);
  340. }
  341. static int wil6210_pm_resume(struct device *dev)
  342. {
  343. return wil6210_resume(dev, false);
  344. }
  345. #endif /* CONFIG_PM_SLEEP */
  346. #endif /* CONFIG_PM */
  347. static const struct dev_pm_ops wil6210_pm_ops = {
  348. SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
  349. };
  350. static struct pci_driver wil6210_driver = {
  351. .probe = wil_pcie_probe,
  352. .remove = wil_pcie_remove,
  353. .id_table = wil6210_pcie_ids,
  354. .name = WIL_NAME,
  355. .driver = {
  356. .pm = &wil6210_pm_ops,
  357. },
  358. };
  359. static int __init wil6210_driver_init(void)
  360. {
  361. int rc;
  362. rc = wil_platform_modinit();
  363. if (rc)
  364. return rc;
  365. rc = pci_register_driver(&wil6210_driver);
  366. if (rc)
  367. wil_platform_modexit();
  368. return rc;
  369. }
  370. module_init(wil6210_driver_init);
  371. static void __exit wil6210_driver_exit(void)
  372. {
  373. pci_unregister_driver(&wil6210_driver);
  374. wil_platform_modexit();
  375. }
  376. module_exit(wil6210_driver_exit);
  377. MODULE_LICENSE("Dual BSD/GPL");
  378. MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
  379. MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");