1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/moduleparam.h>
21 #include <linux/interrupt.h>
22 #include <linux/suspend.h>
23 #include "wil6210.h"
24 #include <linux/rtnetlink.h>
25 #include <linux/pm_runtime.h>
26 
27 static bool use_msi = true;
28 module_param(use_msi, bool, 0444);
29 MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true");
30 
31 static bool ftm_mode;
32 module_param(ftm_mode, bool, 0444);
33 MODULE_PARM_DESC(ftm_mode, " Set factory test mode, default - false");
34 
35 static int wil6210_pm_notify(struct notifier_block *notify_block,
36 			     unsigned long mode, void *unused);
37 
38 static
39 int wil_set_capabilities(struct wil6210_priv *wil)
40 {
41 	const char *wil_fw_name;
42 	u32 jtag_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
43 	u8 chip_revision = (wil_r(wil, RGF_USER_REVISION_ID) &
44 			    RGF_USER_REVISION_ID_MASK);
45 	int platform_capa;
46 	struct fw_map *iccm_section, *sct;
47 
48 	bitmap_zero(wil->hw_capa, hw_capa_last);
49 	bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
50 	bitmap_zero(wil->platform_capa, WIL_PLATFORM_CAPA_MAX);
51 	wil->wil_fw_name = ftm_mode ? WIL_FW_NAME_FTM_DEFAULT :
52 			   WIL_FW_NAME_DEFAULT;
53 	wil->chip_revision = chip_revision;
54 
55 	switch (jtag_id) {
56 	case JTAG_DEV_ID_SPARROW:
57 		memcpy(fw_mapping, sparrow_fw_mapping,
58 		       sizeof(sparrow_fw_mapping));
59 		switch (chip_revision) {
60 		case REVISION_ID_SPARROW_D0:
61 			wil->hw_name = "Sparrow D0";
62 			wil->hw_version = HW_VER_SPARROW_D0;
63 			wil_fw_name = ftm_mode ? WIL_FW_NAME_FTM_SPARROW_PLUS :
64 				      WIL_FW_NAME_SPARROW_PLUS;
65 
66 			if (wil_fw_verify_file_exists(wil, wil_fw_name))
67 				wil->wil_fw_name = wil_fw_name;
68 			sct = wil_find_fw_mapping("mac_rgf_ext");
69 			if (!sct) {
70 				wil_err(wil, "mac_rgf_ext section not found in fw_mapping\n");
71 				return -EINVAL;
72 			}
73 			memcpy(sct, &sparrow_d0_mac_rgf_ext, sizeof(*sct));
74 			break;
75 		case REVISION_ID_SPARROW_B0:
76 			wil->hw_name = "Sparrow B0";
77 			wil->hw_version = HW_VER_SPARROW_B0;
78 			break;
79 		default:
80 			wil->hw_name = "Unknown";
81 			wil->hw_version = HW_VER_UNKNOWN;
82 			break;
83 		}
84 		wil->rgf_fw_assert_code_addr = SPARROW_RGF_FW_ASSERT_CODE;
85 		wil->rgf_ucode_assert_code_addr = SPARROW_RGF_UCODE_ASSERT_CODE;
86 		break;
87 	case JTAG_DEV_ID_TALYN:
88 		wil->hw_name = "Talyn";
89 		wil->hw_version = HW_VER_TALYN;
90 		memcpy(fw_mapping, talyn_fw_mapping, sizeof(talyn_fw_mapping));
91 		wil->rgf_fw_assert_code_addr = TALYN_RGF_FW_ASSERT_CODE;
92 		wil->rgf_ucode_assert_code_addr = TALYN_RGF_UCODE_ASSERT_CODE;
93 		if (wil_r(wil, RGF_USER_OTP_HW_RD_MACHINE_1) &
94 		    BIT_NO_FLASH_INDICATION)
95 			set_bit(hw_capa_no_flash, wil->hw_capa);
96 		break;
97 	default:
98 		wil_err(wil, "Unknown board hardware, chip_id 0x%08x, chip_revision 0x%08x\n",
99 			jtag_id, chip_revision);
100 		wil->hw_name = "Unknown";
101 		wil->hw_version = HW_VER_UNKNOWN;
102 		return -EINVAL;
103 	}
104 
105 	iccm_section = wil_find_fw_mapping("fw_code");
106 	if (!iccm_section) {
107 		wil_err(wil, "fw_code section not found in fw_mapping\n");
108 		return -EINVAL;
109 	}
110 	wil->iccm_base = iccm_section->host;
111 
112 	wil_info(wil, "Board hardware is %s, flash %sexist\n", wil->hw_name,
113 		 test_bit(hw_capa_no_flash, wil->hw_capa) ? "doesn't " : "");
114 
115 	/* Get platform capabilities */
116 	if (wil->platform_ops.get_capa) {
117 		platform_capa =
118 			wil->platform_ops.get_capa(wil->platform_handle);
119 		memcpy(wil->platform_capa, &platform_capa,
120 		       min(sizeof(wil->platform_capa), sizeof(platform_capa)));
121 	}
122 
123 	/* extract FW capabilities from file without loading the FW */
124 	wil_request_firmware(wil, wil->wil_fw_name, false);
125 	wil_refresh_fw_capabilities(wil);
126 
127 	return 0;
128 }
129 
130 void wil_disable_irq(struct wil6210_priv *wil)
131 {
132 	disable_irq(wil->pdev->irq);
133 }
134 
135 void wil_enable_irq(struct wil6210_priv *wil)
136 {
137 	enable_irq(wil->pdev->irq);
138 }
139 
140 /* Bus ops */
141 static int wil_if_pcie_enable(struct wil6210_priv *wil)
142 {
143 	struct pci_dev *pdev = wil->pdev;
144 	int rc;
145 	/* on platforms with buggy ACPI, pdev->msi_enabled may be set to
146 	 * allow pci_enable_device to work. This indicates INTx was not routed
147 	 * and only MSI should be used
148 	 */
149 	int msi_only = pdev->msi_enabled;
150 	bool _use_msi = use_msi;
151 	bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
152 				 wil->fw_capabilities);
153 
154 	wil_dbg_misc(wil, "if_pcie_enable, wmi_only %d\n", wmi_only);
155 
156 	pci_set_master(pdev);
157 
158 	wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
159 
160 	if (use_msi && pci_enable_msi(pdev)) {
161 		wil_err(wil, "pci_enable_msi failed, use INTx\n");
162 		_use_msi = false;
163 	}
164 
165 	if (!_use_msi && msi_only) {
166 		wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
167 		rc = -ENODEV;
168 		goto stop_master;
169 	}
170 
171 	rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
172 	if (rc)
173 		goto stop_master;
174 
175 	/* need reset here to obtain MAC or in case of WMI-only FW, full reset
176 	 * and fw loading takes place
177 	 */
178 	mutex_lock(&wil->mutex);
179 	rc = wil_reset(wil, wmi_only);
180 	mutex_unlock(&wil->mutex);
181 	if (rc)
182 		goto release_irq;
183 
184 	return 0;
185 
186  release_irq:
187 	wil6210_fini_irq(wil, pdev->irq);
188 	/* safe to call if no MSI */
189 	pci_disable_msi(pdev);
190  stop_master:
191 	pci_clear_master(pdev);
192 	return rc;
193 }
194 
195 static int wil_if_pcie_disable(struct wil6210_priv *wil)
196 {
197 	struct pci_dev *pdev = wil->pdev;
198 
199 	wil_dbg_misc(wil, "if_pcie_disable\n");
200 
201 	pci_clear_master(pdev);
202 	/* disable and release IRQ */
203 	wil6210_fini_irq(wil, pdev->irq);
204 	/* safe to call if no MSI */
205 	pci_disable_msi(pdev);
206 	/* TODO: disable HW */
207 
208 	return 0;
209 }
210 
211 static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size)
212 {
213 	struct wil6210_priv *wil = wil_handle;
214 
215 	if (!wil)
216 		return -EINVAL;
217 
218 	return wil_fw_copy_crash_dump(wil, buf, size);
219 }
220 
221 static int wil_platform_rop_fw_recovery(void *wil_handle)
222 {
223 	struct wil6210_priv *wil = wil_handle;
224 
225 	if (!wil)
226 		return -EINVAL;
227 
228 	wil_fw_error_recovery(wil);
229 
230 	return 0;
231 }
232 
233 static void wil_platform_ops_uninit(struct wil6210_priv *wil)
234 {
235 	if (wil->platform_ops.uninit)
236 		wil->platform_ops.uninit(wil->platform_handle);
237 	memset(&wil->platform_ops, 0, sizeof(wil->platform_ops));
238 }
239 
240 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
241 {
242 	struct wil6210_priv *wil;
243 	struct device *dev = &pdev->dev;
244 	int rc;
245 	const struct wil_platform_rops rops = {
246 		.ramdump = wil_platform_rop_ramdump,
247 		.fw_recovery = wil_platform_rop_fw_recovery,
248 	};
249 	u32 bar_size = pci_resource_len(pdev, 0);
250 	int dma_addr_size[] = {48, 40, 32}; /* keep descending order */
251 	int i;
252 
253 	/* check HW */
254 	dev_info(&pdev->dev, WIL_NAME
255 		 " device found [%04x:%04x] (rev %x) bar size 0x%x\n",
256 		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision,
257 		 bar_size);
258 
259 	if ((bar_size < WIL6210_MIN_MEM_SIZE) ||
260 	    (bar_size > WIL6210_MAX_MEM_SIZE)) {
261 		dev_err(&pdev->dev, "Unexpected BAR0 size 0x%x\n",
262 			bar_size);
263 		return -ENODEV;
264 	}
265 
266 	wil = wil_if_alloc(dev);
267 	if (IS_ERR(wil)) {
268 		rc = (int)PTR_ERR(wil);
269 		dev_err(dev, "wil_if_alloc failed: %d\n", rc);
270 		return rc;
271 	}
272 
273 	wil->pdev = pdev;
274 	pci_set_drvdata(pdev, wil);
275 	wil->bar_size = bar_size;
276 	/* rollback to if_free */
277 
278 	wil->platform_handle =
279 		wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil);
280 	if (!wil->platform_handle) {
281 		rc = -ENODEV;
282 		wil_err(wil, "wil_platform_init failed\n");
283 		goto if_free;
284 	}
285 	/* rollback to err_plat */
286 
287 	/* device supports >32bit addresses */
288 	for (i = 0; i < ARRAY_SIZE(dma_addr_size); i++) {
289 		rc = dma_set_mask_and_coherent(dev,
290 					       DMA_BIT_MASK(dma_addr_size[i]));
291 		if (rc) {
292 			dev_err(dev, "dma_set_mask_and_coherent(%d) failed: %d\n",
293 				dma_addr_size[i], rc);
294 			continue;
295 		}
296 		dev_info(dev, "using dma mask %d", dma_addr_size[i]);
297 		wil->dma_addr_size = dma_addr_size[i];
298 		break;
299 	}
300 
301 	if (wil->dma_addr_size == 0)
302 		goto err_plat;
303 
304 	rc = pci_enable_device(pdev);
305 	if (rc && pdev->msi_enabled == 0) {
306 		wil_err(wil,
307 			"pci_enable_device failed, retry with MSI only\n");
308 		/* Work around for platforms that can't allocate IRQ:
309 		 * retry with MSI only
310 		 */
311 		pdev->msi_enabled = 1;
312 		rc = pci_enable_device(pdev);
313 	}
314 	if (rc) {
315 		wil_err(wil,
316 			"pci_enable_device failed, even with MSI only\n");
317 		goto err_plat;
318 	}
319 	/* rollback to err_disable_pdev */
320 	pci_set_power_state(pdev, PCI_D0);
321 
322 	rc = pci_request_region(pdev, 0, WIL_NAME);
323 	if (rc) {
324 		wil_err(wil, "pci_request_region failed\n");
325 		goto err_disable_pdev;
326 	}
327 	/* rollback to err_release_reg */
328 
329 	wil->csr = pci_ioremap_bar(pdev, 0);
330 	if (!wil->csr) {
331 		wil_err(wil, "pci_ioremap_bar failed\n");
332 		rc = -ENODEV;
333 		goto err_release_reg;
334 	}
335 	/* rollback to err_iounmap */
336 	wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
337 
338 	rc = wil_set_capabilities(wil);
339 	if (rc) {
340 		wil_err(wil, "wil_set_capabilities failed, rc %d\n", rc);
341 		goto err_iounmap;
342 	}
343 	wil6210_clear_irq(wil);
344 
345 	/* FW should raise IRQ when ready */
346 	rc = wil_if_pcie_enable(wil);
347 	if (rc) {
348 		wil_err(wil, "Enable device failed\n");
349 		goto err_iounmap;
350 	}
351 	/* rollback to bus_disable */
352 
353 	rc = wil_if_add(wil);
354 	if (rc) {
355 		wil_err(wil, "wil_if_add failed: %d\n", rc);
356 		goto bus_disable;
357 	}
358 
359 	if (IS_ENABLED(CONFIG_PM))
360 		wil->pm_notify.notifier_call = wil6210_pm_notify;
361 
362 	rc = register_pm_notifier(&wil->pm_notify);
363 	if (rc)
364 		/* Do not fail the driver initialization, as suspend can
365 		 * be prevented in a later phase if needed
366 		 */
367 		wil_err(wil, "register_pm_notifier failed: %d\n", rc);
368 
369 	wil6210_debugfs_init(wil);
370 
371 	wil_pm_runtime_allow(wil);
372 
373 	return 0;
374 
375 bus_disable:
376 	wil_if_pcie_disable(wil);
377 err_iounmap:
378 	pci_iounmap(pdev, wil->csr);
379 err_release_reg:
380 	pci_release_region(pdev, 0);
381 err_disable_pdev:
382 	pci_disable_device(pdev);
383 err_plat:
384 	wil_platform_ops_uninit(wil);
385 if_free:
386 	wil_if_free(wil);
387 
388 	return rc;
389 }
390 
391 static void wil_pcie_remove(struct pci_dev *pdev)
392 {
393 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
394 	void __iomem *csr = wil->csr;
395 
396 	wil_dbg_misc(wil, "pcie_remove\n");
397 
398 	unregister_pm_notifier(&wil->pm_notify);
399 
400 	wil_pm_runtime_forbid(wil);
401 
402 	wil6210_debugfs_remove(wil);
403 	rtnl_lock();
404 	wil_p2p_wdev_free(wil);
405 	rtnl_unlock();
406 	wil_if_remove(wil);
407 	wil_if_pcie_disable(wil);
408 	pci_iounmap(pdev, csr);
409 	pci_release_region(pdev, 0);
410 	pci_disable_device(pdev);
411 	wil_platform_ops_uninit(wil);
412 	wil_if_free(wil);
413 }
414 
415 static const struct pci_device_id wil6210_pcie_ids[] = {
416 	{ PCI_DEVICE(0x1ae9, 0x0310) },
417 	{ PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
418 	{ PCI_DEVICE(0x17cb, 0x1201) }, /* Talyn */
419 	{ /* end: all zeroes */	},
420 };
421 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
422 
423 static int wil6210_suspend(struct device *dev, bool is_runtime)
424 {
425 	int rc = 0;
426 	struct pci_dev *pdev = to_pci_dev(dev);
427 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
428 	struct net_device *ndev = wil_to_ndev(wil);
429 	bool keep_radio_on = ndev->flags & IFF_UP &&
430 			     wil->keep_radio_on_during_sleep;
431 
432 	wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
433 
434 	rc = wil_can_suspend(wil, is_runtime);
435 	if (rc)
436 		goto out;
437 
438 	rc = wil_suspend(wil, is_runtime, keep_radio_on);
439 	if (!rc) {
440 		/* In case radio stays on, platform device will control
441 		 * PCIe master
442 		 */
443 		if (!keep_radio_on) {
444 			/* disable bus mastering */
445 			pci_clear_master(pdev);
446 			wil->suspend_stats.r_off.successful_suspends++;
447 		} else {
448 			wil->suspend_stats.r_on.successful_suspends++;
449 		}
450 	}
451 out:
452 	return rc;
453 }
454 
455 static int wil6210_resume(struct device *dev, bool is_runtime)
456 {
457 	int rc = 0;
458 	struct pci_dev *pdev = to_pci_dev(dev);
459 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
460 	struct net_device *ndev = wil_to_ndev(wil);
461 	bool keep_radio_on = ndev->flags & IFF_UP &&
462 			     wil->keep_radio_on_during_sleep;
463 
464 	wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
465 
466 	/* In case radio stays on, platform device will control
467 	 * PCIe master
468 	 */
469 	if (!keep_radio_on)
470 		/* allow master */
471 		pci_set_master(pdev);
472 	rc = wil_resume(wil, is_runtime, keep_radio_on);
473 	if (rc) {
474 		wil_err(wil, "device failed to resume (%d)\n", rc);
475 		if (!keep_radio_on) {
476 			pci_clear_master(pdev);
477 			wil->suspend_stats.r_off.failed_resumes++;
478 		} else {
479 			wil->suspend_stats.r_on.failed_resumes++;
480 		}
481 	} else {
482 		if (keep_radio_on)
483 			wil->suspend_stats.r_on.successful_resumes++;
484 		else
485 			wil->suspend_stats.r_off.successful_resumes++;
486 	}
487 
488 	return rc;
489 }
490 
491 static int wil6210_pm_notify(struct notifier_block *notify_block,
492 			     unsigned long mode, void *unused)
493 {
494 	struct wil6210_priv *wil = container_of(
495 		notify_block, struct wil6210_priv, pm_notify);
496 	int rc = 0;
497 	enum wil_platform_event evt;
498 
499 	wil_dbg_pm(wil, "pm_notify: mode (%ld)\n", mode);
500 
501 	switch (mode) {
502 	case PM_HIBERNATION_PREPARE:
503 	case PM_SUSPEND_PREPARE:
504 	case PM_RESTORE_PREPARE:
505 		rc = wil_can_suspend(wil, false);
506 		if (rc)
507 			break;
508 		evt = WIL_PLATFORM_EVT_PRE_SUSPEND;
509 		if (wil->platform_ops.notify)
510 			rc = wil->platform_ops.notify(wil->platform_handle,
511 						      evt);
512 		break;
513 	case PM_POST_SUSPEND:
514 	case PM_POST_HIBERNATION:
515 	case PM_POST_RESTORE:
516 		evt = WIL_PLATFORM_EVT_POST_SUSPEND;
517 		if (wil->platform_ops.notify)
518 			rc = wil->platform_ops.notify(wil->platform_handle,
519 						      evt);
520 		break;
521 	default:
522 		wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode);
523 		break;
524 	}
525 
526 	wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc);
527 	return rc;
528 }
529 
530 static int __maybe_unused wil6210_pm_suspend(struct device *dev)
531 {
532 	return wil6210_suspend(dev, false);
533 }
534 
535 static int __maybe_unused wil6210_pm_resume(struct device *dev)
536 {
537 	return wil6210_resume(dev, false);
538 }
539 
540 static int __maybe_unused wil6210_pm_runtime_idle(struct device *dev)
541 {
542 	struct pci_dev *pdev = to_pci_dev(dev);
543 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
544 
545 	wil_dbg_pm(wil, "Runtime idle\n");
546 
547 	return wil_can_suspend(wil, true);
548 }
549 
550 static int __maybe_unused wil6210_pm_runtime_resume(struct device *dev)
551 {
552 	return wil6210_resume(dev, true);
553 }
554 
555 static int __maybe_unused wil6210_pm_runtime_suspend(struct device *dev)
556 {
557 	struct pci_dev *pdev = to_pci_dev(dev);
558 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
559 
560 	if (test_bit(wil_status_suspended, wil->status)) {
561 		wil_dbg_pm(wil, "trying to suspend while suspended\n");
562 		return 1;
563 	}
564 
565 	return wil6210_suspend(dev, true);
566 }
567 
568 static const struct dev_pm_ops wil6210_pm_ops = {
569 	SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
570 	SET_RUNTIME_PM_OPS(wil6210_pm_runtime_suspend,
571 			   wil6210_pm_runtime_resume,
572 			   wil6210_pm_runtime_idle)
573 };
574 
575 static struct pci_driver wil6210_driver = {
576 	.probe		= wil_pcie_probe,
577 	.remove		= wil_pcie_remove,
578 	.id_table	= wil6210_pcie_ids,
579 	.name		= WIL_NAME,
580 	.driver		= {
581 		.pm = &wil6210_pm_ops,
582 	},
583 };
584 
585 static int __init wil6210_driver_init(void)
586 {
587 	int rc;
588 
589 	rc = wil_platform_modinit();
590 	if (rc)
591 		return rc;
592 
593 	rc = pci_register_driver(&wil6210_driver);
594 	if (rc)
595 		wil_platform_modexit();
596 	return rc;
597 }
598 module_init(wil6210_driver_init);
599 
600 static void __exit wil6210_driver_exit(void)
601 {
602 	pci_unregister_driver(&wil6210_driver);
603 	wil_platform_modexit();
604 }
605 module_exit(wil6210_driver_exit);
606 
607 MODULE_LICENSE("Dual BSD/GPL");
608 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
609 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");
610