pcie_bus.c 10 KB

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