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