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 
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/moduleparam.h>
20 
21 #include "wil6210.h"
22 
23 static int use_msi = 1;
24 module_param(use_msi, int, S_IRUGO);
25 MODULE_PARM_DESC(use_msi,
26 		 " Use MSI interrupt: "
27 		 "0 - don't, 1 - (default) - single, or 3");
28 
29 static bool debug_fw; /* = false; */
30 module_param(debug_fw, bool, S_IRUGO);
31 MODULE_PARM_DESC(debug_fw, " load driver if FW not ready. For FW debug");
32 
33 /* Bus ops */
34 static int wil_if_pcie_enable(struct wil6210_priv *wil)
35 {
36 	struct pci_dev *pdev = wil->pdev;
37 	int rc;
38 	/* on platforms with buggy ACPI, pdev->msi_enabled may be set to
39 	 * allow pci_enable_device to work. This indicates INTx was not routed
40 	 * and only MSI should be used
41 	 */
42 	int msi_only = pdev->msi_enabled;
43 
44 	pdev->msi_enabled = 0;
45 
46 	pci_set_master(pdev);
47 
48 	/*
49 	 * how many MSI interrupts to request?
50 	 */
51 	switch (use_msi) {
52 	case 3:
53 	case 1:
54 		wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
55 		break;
56 	case 0:
57 		wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
58 		break;
59 	default:
60 		wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
61 		use_msi = 1;
62 	}
63 
64 	if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
65 		wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
66 		use_msi = 1;
67 	}
68 
69 	if (use_msi == 1 && pci_enable_msi(pdev)) {
70 		wil_err(wil, "pci_enable_msi failed, use INTx\n");
71 		use_msi = 0;
72 	}
73 
74 	wil->n_msi = use_msi;
75 
76 	if ((wil->n_msi == 0) && msi_only) {
77 		wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
78 		rc = -ENODEV;
79 		goto stop_master;
80 	}
81 
82 	rc = wil6210_init_irq(wil, pdev->irq);
83 	if (rc)
84 		goto stop_master;
85 
86 	/* need reset here to obtain MAC */
87 	mutex_lock(&wil->mutex);
88 	rc = wil_reset(wil);
89 	mutex_unlock(&wil->mutex);
90 	if (debug_fw)
91 		rc = 0;
92 	if (rc)
93 		goto release_irq;
94 
95 	return 0;
96 
97  release_irq:
98 	wil6210_fini_irq(wil, pdev->irq);
99 	/* safe to call if no MSI */
100 	pci_disable_msi(pdev);
101  stop_master:
102 	pci_clear_master(pdev);
103 	return rc;
104 }
105 
106 static int wil_if_pcie_disable(struct wil6210_priv *wil)
107 {
108 	struct pci_dev *pdev = wil->pdev;
109 
110 	pci_clear_master(pdev);
111 	/* disable and release IRQ */
112 	wil6210_fini_irq(wil, pdev->irq);
113 	/* safe to call if no MSI */
114 	pci_disable_msi(pdev);
115 	/* TODO: disable HW */
116 
117 	return 0;
118 }
119 
120 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
121 {
122 	struct wil6210_priv *wil;
123 	struct device *dev = &pdev->dev;
124 	void __iomem *csr;
125 	struct wil_board *board = (struct wil_board *)id->driver_data;
126 	int rc;
127 
128 	/* check HW */
129 	dev_info(&pdev->dev, WIL_NAME
130 		 " \"%s\" device found [%04x:%04x] (rev %x)\n", board->name,
131 		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
132 
133 	if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
134 		dev_err(&pdev->dev, "Not " WIL_NAME "? "
135 			"BAR0 size is %lu while expecting %lu\n",
136 			(ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
137 		return -ENODEV;
138 	}
139 
140 	rc = pci_enable_device(pdev);
141 	if (rc) {
142 		dev_err(&pdev->dev,
143 			"pci_enable_device failed, retry with MSI only\n");
144 		/* Work around for platforms that can't allocate IRQ:
145 		 * retry with MSI only
146 		 */
147 		pdev->msi_enabled = 1;
148 		rc = pci_enable_device(pdev);
149 	}
150 	if (rc)
151 		return -ENODEV;
152 	/* rollback to err_disable_pdev */
153 
154 	rc = pci_request_region(pdev, 0, WIL_NAME);
155 	if (rc) {
156 		dev_err(&pdev->dev, "pci_request_region failed\n");
157 		goto err_disable_pdev;
158 	}
159 	/* rollback to err_release_reg */
160 
161 	csr = pci_ioremap_bar(pdev, 0);
162 	if (!csr) {
163 		dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
164 		rc = -ENODEV;
165 		goto err_release_reg;
166 	}
167 	/* rollback to err_iounmap */
168 	dev_info(&pdev->dev, "CSR at %pR -> 0x%p\n", &pdev->resource[0], csr);
169 
170 	wil = wil_if_alloc(dev, csr);
171 	if (IS_ERR(wil)) {
172 		rc = (int)PTR_ERR(wil);
173 		dev_err(dev, "wil_if_alloc failed: %d\n", rc);
174 		goto err_iounmap;
175 	}
176 	/* rollback to if_free */
177 
178 	pci_set_drvdata(pdev, wil);
179 	wil->pdev = pdev;
180 	wil->board = board;
181 
182 	wil6210_clear_irq(wil);
183 	/* FW should raise IRQ when ready */
184 	rc = wil_if_pcie_enable(wil);
185 	if (rc) {
186 		wil_err(wil, "Enable device failed\n");
187 		goto if_free;
188 	}
189 	/* rollback to bus_disable */
190 
191 	rc = wil_if_add(wil);
192 	if (rc) {
193 		wil_err(wil, "wil_if_add failed: %d\n", rc);
194 		goto bus_disable;
195 	}
196 
197 	wil6210_debugfs_init(wil);
198 
199 	/* check FW is alive */
200 	wmi_echo(wil);
201 
202 	return 0;
203 
204  bus_disable:
205 	wil_if_pcie_disable(wil);
206  if_free:
207 	wil_if_free(wil);
208  err_iounmap:
209 	pci_iounmap(pdev, csr);
210  err_release_reg:
211 	pci_release_region(pdev, 0);
212  err_disable_pdev:
213 	pci_disable_device(pdev);
214 
215 	return rc;
216 }
217 
218 static void wil_pcie_remove(struct pci_dev *pdev)
219 {
220 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
221 
222 	wil6210_debugfs_remove(wil);
223 	wil_if_pcie_disable(wil);
224 	wil_if_remove(wil);
225 	wil_if_free(wil);
226 	pci_iounmap(pdev, wil->csr);
227 	pci_release_region(pdev, 0);
228 	pci_disable_device(pdev);
229 }
230 
231 static const struct wil_board wil_board_marlon = {
232 	.board = WIL_BOARD_MARLON,
233 	.name = "marlon",
234 };
235 
236 static const struct wil_board wil_board_sparrow = {
237 	.board = WIL_BOARD_SPARROW,
238 	.name = "sparrow",
239 };
240 
241 static const struct pci_device_id wil6210_pcie_ids[] = {
242 	{ PCI_DEVICE(0x1ae9, 0x0301),
243 	  .driver_data = (kernel_ulong_t)&wil_board_marlon },
244 	{ PCI_DEVICE(0x1ae9, 0x0310),
245 	  .driver_data = (kernel_ulong_t)&wil_board_sparrow },
246 	{ /* end: all zeroes */	},
247 };
248 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
249 
250 static struct pci_driver wil6210_driver = {
251 	.probe		= wil_pcie_probe,
252 	.remove		= wil_pcie_remove,
253 	.id_table	= wil6210_pcie_ids,
254 	.name		= WIL_NAME,
255 };
256 
257 module_pci_driver(wil6210_driver);
258 
259 MODULE_LICENSE("Dual BSD/GPL");
260 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
261 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");
262