xref: /linux/drivers/net/wireless/ath/wil6210/pcie_bus.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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 
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/moduleparam.h>
20 #include <linux/interrupt.h>
21 #include <linux/suspend.h>
22 #include "wil6210.h"
23 #include <linux/rtnetlink.h>
24 
25 static bool use_msi = true;
26 module_param(use_msi, bool, 0444);
27 MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true");
28 
29 #ifdef CONFIG_PM
30 #ifdef CONFIG_PM_SLEEP
31 static int wil6210_pm_notify(struct notifier_block *notify_block,
32 			     unsigned long mode, void *unused);
33 #endif /* CONFIG_PM_SLEEP */
34 #endif /* CONFIG_PM */
35 
36 static
37 void wil_set_capabilities(struct wil6210_priv *wil)
38 {
39 	u32 jtag_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
40 	u8 chip_revision = (wil_r(wil, RGF_USER_REVISION_ID) &
41 			    RGF_USER_REVISION_ID_MASK);
42 
43 	bitmap_zero(wil->hw_capabilities, hw_capability_last);
44 	bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
45 	wil->wil_fw_name = WIL_FW_NAME_DEFAULT;
46 	wil->chip_revision = chip_revision;
47 
48 	switch (jtag_id) {
49 	case JTAG_DEV_ID_SPARROW:
50 		switch (chip_revision) {
51 		case REVISION_ID_SPARROW_D0:
52 			wil->hw_name = "Sparrow D0";
53 			wil->hw_version = HW_VER_SPARROW_D0;
54 			if (wil_fw_verify_file_exists(wil,
55 						      WIL_FW_NAME_SPARROW_PLUS))
56 				wil->wil_fw_name = WIL_FW_NAME_SPARROW_PLUS;
57 			break;
58 		case REVISION_ID_SPARROW_B0:
59 			wil->hw_name = "Sparrow B0";
60 			wil->hw_version = HW_VER_SPARROW_B0;
61 			break;
62 		default:
63 			wil->hw_name = "Unknown";
64 			wil->hw_version = HW_VER_UNKNOWN;
65 			break;
66 		}
67 		break;
68 	default:
69 		wil_err(wil, "Unknown board hardware, chip_id 0x%08x, chip_revision 0x%08x\n",
70 			jtag_id, chip_revision);
71 		wil->hw_name = "Unknown";
72 		wil->hw_version = HW_VER_UNKNOWN;
73 	}
74 
75 	wil_info(wil, "Board hardware is %s\n", wil->hw_name);
76 
77 	/* extract FW capabilities from file without loading the FW */
78 	wil_request_firmware(wil, wil->wil_fw_name, false);
79 }
80 
81 void wil_disable_irq(struct wil6210_priv *wil)
82 {
83 	disable_irq(wil->pdev->irq);
84 }
85 
86 void wil_enable_irq(struct wil6210_priv *wil)
87 {
88 	enable_irq(wil->pdev->irq);
89 }
90 
91 /* Bus ops */
92 static int wil_if_pcie_enable(struct wil6210_priv *wil)
93 {
94 	struct pci_dev *pdev = wil->pdev;
95 	int rc;
96 	/* on platforms with buggy ACPI, pdev->msi_enabled may be set to
97 	 * allow pci_enable_device to work. This indicates INTx was not routed
98 	 * and only MSI should be used
99 	 */
100 	int msi_only = pdev->msi_enabled;
101 	bool _use_msi = use_msi;
102 	bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
103 				 wil->fw_capabilities);
104 
105 	wil_dbg_misc(wil, "if_pcie_enable, wmi_only %d\n", wmi_only);
106 
107 	pdev->msi_enabled = 0;
108 
109 	pci_set_master(pdev);
110 
111 	wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
112 
113 	if (use_msi && pci_enable_msi(pdev)) {
114 		wil_err(wil, "pci_enable_msi failed, use INTx\n");
115 		_use_msi = false;
116 	}
117 
118 	if (!_use_msi && msi_only) {
119 		wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
120 		rc = -ENODEV;
121 		goto stop_master;
122 	}
123 
124 	rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
125 	if (rc)
126 		goto stop_master;
127 
128 	/* need reset here to obtain MAC or in case of WMI-only FW, full reset
129 	 * and fw loading takes place
130 	 */
131 	mutex_lock(&wil->mutex);
132 	rc = wil_reset(wil, wmi_only);
133 	mutex_unlock(&wil->mutex);
134 	if (rc)
135 		goto release_irq;
136 
137 	return 0;
138 
139  release_irq:
140 	wil6210_fini_irq(wil, pdev->irq);
141 	/* safe to call if no MSI */
142 	pci_disable_msi(pdev);
143  stop_master:
144 	pci_clear_master(pdev);
145 	return rc;
146 }
147 
148 static int wil_if_pcie_disable(struct wil6210_priv *wil)
149 {
150 	struct pci_dev *pdev = wil->pdev;
151 
152 	wil_dbg_misc(wil, "if_pcie_disable\n");
153 
154 	pci_clear_master(pdev);
155 	/* disable and release IRQ */
156 	wil6210_fini_irq(wil, pdev->irq);
157 	/* safe to call if no MSI */
158 	pci_disable_msi(pdev);
159 	/* TODO: disable HW */
160 
161 	return 0;
162 }
163 
164 static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size)
165 {
166 	struct wil6210_priv *wil = wil_handle;
167 
168 	if (!wil)
169 		return -EINVAL;
170 
171 	return wil_fw_copy_crash_dump(wil, buf, size);
172 }
173 
174 static int wil_platform_rop_fw_recovery(void *wil_handle)
175 {
176 	struct wil6210_priv *wil = wil_handle;
177 
178 	if (!wil)
179 		return -EINVAL;
180 
181 	wil_fw_error_recovery(wil);
182 
183 	return 0;
184 }
185 
186 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
187 {
188 	struct wil6210_priv *wil;
189 	struct device *dev = &pdev->dev;
190 	int rc;
191 	const struct wil_platform_rops rops = {
192 		.ramdump = wil_platform_rop_ramdump,
193 		.fw_recovery = wil_platform_rop_fw_recovery,
194 	};
195 
196 	/* check HW */
197 	dev_info(&pdev->dev, WIL_NAME
198 		 " device found [%04x:%04x] (rev %x)\n",
199 		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
200 
201 	if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
202 		dev_err(&pdev->dev, "Not " WIL_NAME "? "
203 			"BAR0 size is %lu while expecting %lu\n",
204 			(ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
205 		return -ENODEV;
206 	}
207 
208 	wil = wil_if_alloc(dev);
209 	if (IS_ERR(wil)) {
210 		rc = (int)PTR_ERR(wil);
211 		dev_err(dev, "wil_if_alloc failed: %d\n", rc);
212 		return rc;
213 	}
214 
215 	wil->pdev = pdev;
216 	pci_set_drvdata(pdev, wil);
217 	/* rollback to if_free */
218 
219 	wil->platform_handle =
220 		wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil);
221 	if (!wil->platform_handle) {
222 		rc = -ENODEV;
223 		wil_err(wil, "wil_platform_init failed\n");
224 		goto if_free;
225 	}
226 	/* rollback to err_plat */
227 
228 	/* device supports 48bit addresses */
229 	rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
230 	if (rc) {
231 		dev_err(dev, "dma_set_mask_and_coherent(48) failed: %d\n", rc);
232 		rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
233 		if (rc) {
234 			dev_err(dev,
235 				"dma_set_mask_and_coherent(32) failed: %d\n",
236 				rc);
237 			goto err_plat;
238 		}
239 	} else {
240 		wil->use_extended_dma_addr = 1;
241 	}
242 
243 	rc = pci_enable_device(pdev);
244 	if (rc) {
245 		wil_err(wil,
246 			"pci_enable_device failed, retry with MSI only\n");
247 		/* Work around for platforms that can't allocate IRQ:
248 		 * retry with MSI only
249 		 */
250 		pdev->msi_enabled = 1;
251 		rc = pci_enable_device(pdev);
252 	}
253 	if (rc) {
254 		wil_err(wil,
255 			"pci_enable_device failed, even with MSI only\n");
256 		goto err_plat;
257 	}
258 	/* rollback to err_disable_pdev */
259 
260 	rc = pci_request_region(pdev, 0, WIL_NAME);
261 	if (rc) {
262 		wil_err(wil, "pci_request_region failed\n");
263 		goto err_disable_pdev;
264 	}
265 	/* rollback to err_release_reg */
266 
267 	wil->csr = pci_ioremap_bar(pdev, 0);
268 	if (!wil->csr) {
269 		wil_err(wil, "pci_ioremap_bar failed\n");
270 		rc = -ENODEV;
271 		goto err_release_reg;
272 	}
273 	/* rollback to err_iounmap */
274 	wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
275 
276 	wil_set_capabilities(wil);
277 	wil6210_clear_irq(wil);
278 
279 	/* FW should raise IRQ when ready */
280 	rc = wil_if_pcie_enable(wil);
281 	if (rc) {
282 		wil_err(wil, "Enable device failed\n");
283 		goto err_iounmap;
284 	}
285 	/* rollback to bus_disable */
286 
287 	rc = wil_if_add(wil);
288 	if (rc) {
289 		wil_err(wil, "wil_if_add failed: %d\n", rc);
290 		goto bus_disable;
291 	}
292 
293 #ifdef CONFIG_PM
294 #ifdef CONFIG_PM_SLEEP
295 	wil->pm_notify.notifier_call = wil6210_pm_notify;
296 	rc = register_pm_notifier(&wil->pm_notify);
297 	if (rc)
298 		/* Do not fail the driver initialization, as suspend can
299 		 * be prevented in a later phase if needed
300 		 */
301 		wil_err(wil, "register_pm_notifier failed: %d\n", rc);
302 #endif /* CONFIG_PM_SLEEP */
303 #endif /* CONFIG_PM */
304 
305 	wil6210_debugfs_init(wil);
306 
307 
308 	return 0;
309 
310 bus_disable:
311 	wil_if_pcie_disable(wil);
312 err_iounmap:
313 	pci_iounmap(pdev, wil->csr);
314 err_release_reg:
315 	pci_release_region(pdev, 0);
316 err_disable_pdev:
317 	pci_disable_device(pdev);
318 err_plat:
319 	if (wil->platform_ops.uninit)
320 		wil->platform_ops.uninit(wil->platform_handle);
321 if_free:
322 	wil_if_free(wil);
323 
324 	return rc;
325 }
326 
327 static void wil_pcie_remove(struct pci_dev *pdev)
328 {
329 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
330 	void __iomem *csr = wil->csr;
331 
332 	wil_dbg_misc(wil, "pcie_remove\n");
333 
334 #ifdef CONFIG_PM
335 #ifdef CONFIG_PM_SLEEP
336 	unregister_pm_notifier(&wil->pm_notify);
337 #endif /* CONFIG_PM_SLEEP */
338 #endif /* CONFIG_PM */
339 
340 	wil6210_debugfs_remove(wil);
341 	rtnl_lock();
342 	wil_p2p_wdev_free(wil);
343 	rtnl_unlock();
344 	wil_if_remove(wil);
345 	wil_if_pcie_disable(wil);
346 	pci_iounmap(pdev, csr);
347 	pci_release_region(pdev, 0);
348 	pci_disable_device(pdev);
349 	if (wil->platform_ops.uninit)
350 		wil->platform_ops.uninit(wil->platform_handle);
351 	wil_if_free(wil);
352 }
353 
354 static const struct pci_device_id wil6210_pcie_ids[] = {
355 	{ PCI_DEVICE(0x1ae9, 0x0310) },
356 	{ PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
357 	{ /* end: all zeroes */	},
358 };
359 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
360 
361 #ifdef CONFIG_PM
362 #ifdef CONFIG_PM_SLEEP
363 
364 static int wil6210_suspend(struct device *dev, bool is_runtime)
365 {
366 	int rc = 0;
367 	struct pci_dev *pdev = to_pci_dev(dev);
368 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
369 
370 	wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
371 
372 	rc = wil_can_suspend(wil, is_runtime);
373 	if (rc)
374 		goto out;
375 
376 	rc = wil_suspend(wil, is_runtime);
377 	if (rc)
378 		goto out;
379 
380 	/* TODO: how do I bring card in low power state? */
381 
382 	/* disable bus mastering */
383 	pci_clear_master(pdev);
384 	/* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
385 
386 out:
387 	return rc;
388 }
389 
390 static int wil6210_resume(struct device *dev, bool is_runtime)
391 {
392 	int rc = 0;
393 	struct pci_dev *pdev = to_pci_dev(dev);
394 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
395 
396 	wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
397 
398 	/* allow master */
399 	pci_set_master(pdev);
400 
401 	rc = wil_resume(wil, is_runtime);
402 	if (rc)
403 		pci_clear_master(pdev);
404 
405 	return rc;
406 }
407 
408 static int wil6210_pm_notify(struct notifier_block *notify_block,
409 			     unsigned long mode, void *unused)
410 {
411 	struct wil6210_priv *wil = container_of(
412 		notify_block, struct wil6210_priv, pm_notify);
413 	int rc = 0;
414 	enum wil_platform_event evt;
415 
416 	wil_dbg_pm(wil, "pm_notify: mode (%ld)\n", mode);
417 
418 	switch (mode) {
419 	case PM_HIBERNATION_PREPARE:
420 	case PM_SUSPEND_PREPARE:
421 	case PM_RESTORE_PREPARE:
422 		rc = wil_can_suspend(wil, false);
423 		if (rc)
424 			break;
425 		evt = WIL_PLATFORM_EVT_PRE_SUSPEND;
426 		if (wil->platform_ops.notify)
427 			rc = wil->platform_ops.notify(wil->platform_handle,
428 						      evt);
429 		break;
430 	case PM_POST_SUSPEND:
431 	case PM_POST_HIBERNATION:
432 	case PM_POST_RESTORE:
433 		evt = WIL_PLATFORM_EVT_POST_SUSPEND;
434 		if (wil->platform_ops.notify)
435 			rc = wil->platform_ops.notify(wil->platform_handle,
436 						      evt);
437 		break;
438 	default:
439 		wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode);
440 		break;
441 	}
442 
443 	wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc);
444 	return rc;
445 }
446 
447 static int wil6210_pm_suspend(struct device *dev)
448 {
449 	return wil6210_suspend(dev, false);
450 }
451 
452 static int wil6210_pm_resume(struct device *dev)
453 {
454 	return wil6210_resume(dev, false);
455 }
456 #endif /* CONFIG_PM_SLEEP */
457 
458 #endif /* CONFIG_PM */
459 
460 static const struct dev_pm_ops wil6210_pm_ops = {
461 	SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
462 };
463 
464 static struct pci_driver wil6210_driver = {
465 	.probe		= wil_pcie_probe,
466 	.remove		= wil_pcie_remove,
467 	.id_table	= wil6210_pcie_ids,
468 	.name		= WIL_NAME,
469 	.driver		= {
470 		.pm = &wil6210_pm_ops,
471 	},
472 };
473 
474 static int __init wil6210_driver_init(void)
475 {
476 	int rc;
477 
478 	rc = wil_platform_modinit();
479 	if (rc)
480 		return rc;
481 
482 	rc = pci_register_driver(&wil6210_driver);
483 	if (rc)
484 		wil_platform_modexit();
485 	return rc;
486 }
487 module_init(wil6210_driver_init);
488 
489 static void __exit wil6210_driver_exit(void)
490 {
491 	pci_unregister_driver(&wil6210_driver);
492 	wil_platform_modexit();
493 }
494 module_exit(wil6210_driver_exit);
495 
496 MODULE_LICENSE("Dual BSD/GPL");
497 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
498 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");
499