pcie_bus.c 5.2 KB

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