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