pcie_bus.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2012 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/debugfs.h>
  18. #include <linux/pci.h>
  19. #include <linux/moduleparam.h>
  20. #include "wil6210.h"
  21. static int use_msi = 1;
  22. module_param(use_msi, int, S_IRUGO);
  23. MODULE_PARM_DESC(use_msi,
  24. " Use MSI interrupt: "
  25. "0 - don't, 1 - (default) - single, or 3");
  26. /* Bus ops */
  27. static int wil_if_pcie_enable(struct wil6210_priv *wil)
  28. {
  29. struct pci_dev *pdev = wil->pdev;
  30. int rc;
  31. pci_set_master(pdev);
  32. /*
  33. * how many MSI interrupts to request?
  34. */
  35. switch (use_msi) {
  36. case 3:
  37. case 1:
  38. wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
  39. break;
  40. case 0:
  41. wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
  42. break;
  43. default:
  44. wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
  45. use_msi = 1;
  46. }
  47. if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
  48. wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
  49. use_msi = 1;
  50. }
  51. if (use_msi == 1 && pci_enable_msi(pdev)) {
  52. wil_err(wil, "pci_enable_msi failed, use INTx\n");
  53. use_msi = 0;
  54. }
  55. wil->n_msi = use_msi;
  56. rc = wil6210_init_irq(wil, pdev->irq);
  57. if (rc)
  58. goto stop_master;
  59. /* need reset here to obtain MAC */
  60. mutex_lock(&wil->mutex);
  61. rc = wil_reset(wil);
  62. mutex_unlock(&wil->mutex);
  63. if (rc)
  64. goto release_irq;
  65. return 0;
  66. release_irq:
  67. wil6210_fini_irq(wil, pdev->irq);
  68. /* safe to call if no MSI */
  69. pci_disable_msi(pdev);
  70. stop_master:
  71. pci_clear_master(pdev);
  72. return rc;
  73. }
  74. static int wil_if_pcie_disable(struct wil6210_priv *wil)
  75. {
  76. struct pci_dev *pdev = wil->pdev;
  77. pci_clear_master(pdev);
  78. /* disable and release IRQ */
  79. wil6210_fini_irq(wil, pdev->irq);
  80. /* safe to call if no MSI */
  81. pci_disable_msi(pdev);
  82. /* TODO: disable HW */
  83. return 0;
  84. }
  85. static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  86. {
  87. struct wil6210_priv *wil;
  88. struct device *dev = &pdev->dev;
  89. void __iomem *csr;
  90. int rc;
  91. /* check HW */
  92. dev_info(&pdev->dev, WIL_NAME " device found [%04x:%04x] (rev %x)\n",
  93. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  94. if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
  95. dev_err(&pdev->dev, "Not " WIL_NAME "? "
  96. "BAR0 size is %lu while expecting %lu\n",
  97. (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
  98. return -ENODEV;
  99. }
  100. rc = pci_enable_device(pdev);
  101. if (rc) {
  102. dev_err(&pdev->dev, "pci_enable_device failed\n");
  103. return -ENODEV;
  104. }
  105. /* rollback to err_disable_pdev */
  106. rc = pci_request_region(pdev, 0, WIL_NAME);
  107. if (rc) {
  108. dev_err(&pdev->dev, "pci_request_region failed\n");
  109. goto err_disable_pdev;
  110. }
  111. /* rollback to err_release_reg */
  112. csr = pci_ioremap_bar(pdev, 0);
  113. if (!csr) {
  114. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  115. rc = -ENODEV;
  116. goto err_release_reg;
  117. }
  118. /* rollback to err_iounmap */
  119. dev_info(&pdev->dev, "CSR at %pR -> 0x%p\n", &pdev->resource[0], csr);
  120. wil = wil_if_alloc(dev, csr);
  121. if (IS_ERR(wil)) {
  122. rc = (int)PTR_ERR(wil);
  123. dev_err(dev, "wil_if_alloc failed: %d\n", rc);
  124. goto err_iounmap;
  125. }
  126. /* rollback to if_free */
  127. pci_set_drvdata(pdev, wil);
  128. wil->pdev = pdev;
  129. wil6210_clear_irq(wil);
  130. /* FW should raise IRQ when ready */
  131. rc = wil_if_pcie_enable(wil);
  132. if (rc) {
  133. wil_err(wil, "Enable device failed\n");
  134. goto if_free;
  135. }
  136. /* rollback to bus_disable */
  137. rc = wil_if_add(wil);
  138. if (rc) {
  139. wil_err(wil, "wil_if_add failed: %d\n", rc);
  140. goto bus_disable;
  141. }
  142. wil6210_debugfs_init(wil);
  143. /* check FW is alive */
  144. wmi_echo(wil);
  145. return 0;
  146. bus_disable:
  147. wil_if_pcie_disable(wil);
  148. if_free:
  149. wil_if_free(wil);
  150. err_iounmap:
  151. pci_iounmap(pdev, csr);
  152. err_release_reg:
  153. pci_release_region(pdev, 0);
  154. err_disable_pdev:
  155. pci_disable_device(pdev);
  156. return rc;
  157. }
  158. static void wil_pcie_remove(struct pci_dev *pdev)
  159. {
  160. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  161. wil6210_debugfs_remove(wil);
  162. wil_if_pcie_disable(wil);
  163. wil_if_remove(wil);
  164. wil_if_free(wil);
  165. pci_iounmap(pdev, wil->csr);
  166. pci_release_region(pdev, 0);
  167. pci_disable_device(pdev);
  168. }
  169. static DEFINE_PCI_DEVICE_TABLE(wil6210_pcie_ids) = {
  170. { PCI_DEVICE(0x1ae9, 0x0301) },
  171. { /* end: all zeroes */ },
  172. };
  173. MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
  174. static struct pci_driver wil6210_driver = {
  175. .probe = wil_pcie_probe,
  176. .remove = wil_pcie_remove,
  177. .id_table = wil6210_pcie_ids,
  178. .name = WIL_NAME,
  179. };
  180. module_pci_driver(wil6210_driver);
  181. MODULE_LICENSE("Dual BSD/GPL");
  182. MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
  183. MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");