1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
7 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/interrupt.h>
11 #include <linux/suspend.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/pm_runtime.h>
17 module_param(n_msi
, int, 0444);
18 MODULE_PARM_DESC(n_msi
, " Use MSI interrupt: 0 - use INTx, 1 - single, or 3 - (default) ");
21 module_param(ftm_mode
, bool, 0444);
22 MODULE_PARM_DESC(ftm_mode
, " Set factory test mode, default - false");
24 static int wil6210_pm_notify(struct notifier_block
*notify_block
,
25 unsigned long mode
, void *unused
);
28 int wil_set_capabilities(struct wil6210_priv
*wil
)
30 const char *wil_fw_name
;
31 u32 jtag_id
= wil_r(wil
, RGF_USER_JTAG_DEV_ID
);
32 u8 chip_revision
= (wil_r(wil
, RGF_USER_REVISION_ID
) &
33 RGF_USER_REVISION_ID_MASK
);
35 struct fw_map
*iccm_section
, *sct
;
37 bitmap_zero(wil
->hw_capa
, hw_capa_last
);
38 bitmap_zero(wil
->fw_capabilities
, WMI_FW_CAPABILITY_MAX
);
39 bitmap_zero(wil
->platform_capa
, WIL_PLATFORM_CAPA_MAX
);
40 wil
->wil_fw_name
= ftm_mode
? WIL_FW_NAME_FTM_DEFAULT
:
42 wil
->chip_revision
= chip_revision
;
45 case JTAG_DEV_ID_SPARROW
:
46 memcpy(fw_mapping
, sparrow_fw_mapping
,
47 sizeof(sparrow_fw_mapping
));
48 switch (chip_revision
) {
49 case REVISION_ID_SPARROW_D0
:
50 wil
->hw_name
= "Sparrow D0";
51 wil
->hw_version
= HW_VER_SPARROW_D0
;
52 wil_fw_name
= ftm_mode
? WIL_FW_NAME_FTM_SPARROW_PLUS
:
53 WIL_FW_NAME_SPARROW_PLUS
;
55 if (wil_fw_verify_file_exists(wil
, wil_fw_name
))
56 wil
->wil_fw_name
= wil_fw_name
;
57 sct
= wil_find_fw_mapping("mac_rgf_ext");
59 wil_err(wil
, "mac_rgf_ext section not found in fw_mapping\n");
62 memcpy(sct
, &sparrow_d0_mac_rgf_ext
, sizeof(*sct
));
64 case REVISION_ID_SPARROW_B0
:
65 wil
->hw_name
= "Sparrow B0";
66 wil
->hw_version
= HW_VER_SPARROW_B0
;
69 wil
->hw_name
= "Unknown";
70 wil
->hw_version
= HW_VER_UNKNOWN
;
73 wil
->rgf_fw_assert_code_addr
= SPARROW_RGF_FW_ASSERT_CODE
;
74 wil
->rgf_ucode_assert_code_addr
= SPARROW_RGF_UCODE_ASSERT_CODE
;
76 case JTAG_DEV_ID_TALYN
:
77 wil
->hw_name
= "Talyn-MA";
78 wil
->hw_version
= HW_VER_TALYN
;
79 memcpy(fw_mapping
, talyn_fw_mapping
, sizeof(talyn_fw_mapping
));
80 wil
->rgf_fw_assert_code_addr
= TALYN_RGF_FW_ASSERT_CODE
;
81 wil
->rgf_ucode_assert_code_addr
= TALYN_RGF_UCODE_ASSERT_CODE
;
82 if (wil_r(wil
, RGF_USER_OTP_HW_RD_MACHINE_1
) &
83 BIT_NO_FLASH_INDICATION
)
84 set_bit(hw_capa_no_flash
, wil
->hw_capa
);
85 wil_fw_name
= ftm_mode
? WIL_FW_NAME_FTM_TALYN
:
87 if (wil_fw_verify_file_exists(wil
, wil_fw_name
))
88 wil
->wil_fw_name
= wil_fw_name
;
90 case JTAG_DEV_ID_TALYN_MB
:
91 wil
->hw_name
= "Talyn-MB";
92 wil
->hw_version
= HW_VER_TALYN_MB
;
93 memcpy(fw_mapping
, talyn_mb_fw_mapping
,
94 sizeof(talyn_mb_fw_mapping
));
95 wil
->rgf_fw_assert_code_addr
= TALYN_RGF_FW_ASSERT_CODE
;
96 wil
->rgf_ucode_assert_code_addr
= TALYN_RGF_UCODE_ASSERT_CODE
;
97 set_bit(hw_capa_no_flash
, wil
->hw_capa
);
98 wil
->use_enhanced_dma_hw
= true;
99 wil
->use_rx_hw_reordering
= true;
100 wil
->use_compressed_rx_status
= true;
101 wil_fw_name
= ftm_mode
? WIL_FW_NAME_FTM_TALYN
:
103 if (wil_fw_verify_file_exists(wil
, wil_fw_name
))
104 wil
->wil_fw_name
= wil_fw_name
;
107 wil_err(wil
, "Unknown board hardware, chip_id 0x%08x, chip_revision 0x%08x\n",
108 jtag_id
, chip_revision
);
109 wil
->hw_name
= "Unknown";
110 wil
->hw_version
= HW_VER_UNKNOWN
;
114 wil_init_txrx_ops(wil
);
116 iccm_section
= wil_find_fw_mapping("fw_code");
118 wil_err(wil
, "fw_code section not found in fw_mapping\n");
121 wil
->iccm_base
= iccm_section
->host
;
123 wil_info(wil
, "Board hardware is %s, flash %sexist\n", wil
->hw_name
,
124 test_bit(hw_capa_no_flash
, wil
->hw_capa
) ? "doesn't " : "");
126 /* Get platform capabilities */
127 if (wil
->platform_ops
.get_capa
) {
129 wil
->platform_ops
.get_capa(wil
->platform_handle
);
130 memcpy(wil
->platform_capa
, &platform_capa
,
131 min(sizeof(wil
->platform_capa
), sizeof(platform_capa
)));
134 wil_info(wil
, "platform_capa 0x%lx\n", *wil
->platform_capa
);
136 /* extract FW capabilities from file without loading the FW */
137 wil_request_firmware(wil
, wil
->wil_fw_name
, false);
138 wil_refresh_fw_capabilities(wil
);
143 void wil_disable_irq(struct wil6210_priv
*wil
)
145 int irq
= wil
->pdev
->irq
;
148 if (wil
->n_msi
== 3) {
149 disable_irq(irq
+ 1);
150 disable_irq(irq
+ 2);
154 void wil_enable_irq(struct wil6210_priv
*wil
)
156 int irq
= wil
->pdev
->irq
;
159 if (wil
->n_msi
== 3) {
165 static void wil_remove_all_additional_vifs(struct wil6210_priv
*wil
)
167 struct wil6210_vif
*vif
;
170 for (i
= 1; i
< GET_MAX_VIFS(wil
); i
++) {
173 wil_vif_prepare_stop(vif
);
174 wil_vif_remove(wil
, vif
->mid
);
180 static int wil_if_pcie_enable(struct wil6210_priv
*wil
)
182 struct pci_dev
*pdev
= wil
->pdev
;
184 /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
185 * allow pci_enable_device to work. This indicates INTx was not routed
186 * and only MSI should be used
188 int msi_only
= pdev
->msi_enabled
;
190 wil_dbg_misc(wil
, "if_pcie_enable\n");
192 pci_set_master(pdev
);
194 /* how many MSI interrupts to request? */
198 wil_dbg_misc(wil
, "Setup %d MSI interrupts\n", n_msi
);
201 wil_dbg_misc(wil
, "MSI interrupts disabled, use INTx\n");
204 wil_err(wil
, "Invalid n_msi=%d, default to 1\n", n_msi
);
209 pci_alloc_irq_vectors(pdev
, n_msi
, n_msi
, PCI_IRQ_MSI
) < n_msi
) {
210 wil_err(wil
, "3 MSI mode failed, try 1 MSI\n");
214 if (n_msi
== 1 && pci_enable_msi(pdev
)) {
215 wil_err(wil
, "pci_enable_msi failed, use INTx\n");
221 if (wil
->n_msi
== 0 && msi_only
) {
222 wil_err(wil
, "Interrupt pin not routed, unable to use INTx\n");
227 rc
= wil6210_init_irq(wil
, pdev
->irq
);
229 goto release_vectors
;
231 /* need reset here to obtain MAC */
232 mutex_lock(&wil
->mutex
);
233 rc
= wil_reset(wil
, false);
234 mutex_unlock(&wil
->mutex
);
241 wil6210_fini_irq(wil
, pdev
->irq
);
243 /* safe to call if no allocation */
244 pci_free_irq_vectors(pdev
);
246 pci_clear_master(pdev
);
250 static int wil_if_pcie_disable(struct wil6210_priv
*wil
)
252 struct pci_dev
*pdev
= wil
->pdev
;
254 wil_dbg_misc(wil
, "if_pcie_disable\n");
256 pci_clear_master(pdev
);
257 /* disable and release IRQ */
258 wil6210_fini_irq(wil
, pdev
->irq
);
259 /* safe to call if no MSI */
260 pci_disable_msi(pdev
);
261 /* TODO: disable HW */
266 static int wil_platform_rop_ramdump(void *wil_handle
, void *buf
, uint32_t size
)
268 struct wil6210_priv
*wil
= wil_handle
;
273 return wil_fw_copy_crash_dump(wil
, buf
, size
);
276 static int wil_platform_rop_fw_recovery(void *wil_handle
)
278 struct wil6210_priv
*wil
= wil_handle
;
283 wil_fw_error_recovery(wil
);
288 static void wil_platform_ops_uninit(struct wil6210_priv
*wil
)
290 if (wil
->platform_ops
.uninit
)
291 wil
->platform_ops
.uninit(wil
->platform_handle
);
292 memset(&wil
->platform_ops
, 0, sizeof(wil
->platform_ops
));
295 static int wil_pcie_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
297 struct wil6210_priv
*wil
;
298 struct device
*dev
= &pdev
->dev
;
300 const struct wil_platform_rops rops
= {
301 .ramdump
= wil_platform_rop_ramdump
,
302 .fw_recovery
= wil_platform_rop_fw_recovery
,
304 u32 bar_size
= pci_resource_len(pdev
, 0);
305 int dma_addr_size
[] = {64, 48, 40, 32}; /* keep descending order */
309 dev_info(&pdev
->dev
, WIL_NAME
310 " device found [%04x:%04x] (rev %x) bar size 0x%x\n",
311 (int)pdev
->vendor
, (int)pdev
->device
, (int)pdev
->revision
,
314 if ((bar_size
< WIL6210_MIN_MEM_SIZE
) ||
315 (bar_size
> WIL6210_MAX_MEM_SIZE
)) {
316 dev_err(&pdev
->dev
, "Unexpected BAR0 size 0x%x\n",
321 wil
= wil_if_alloc(dev
);
323 rc
= (int)PTR_ERR(wil
);
324 dev_err(dev
, "wil_if_alloc failed: %d\n", rc
);
329 pci_set_drvdata(pdev
, wil
);
330 wil
->bar_size
= bar_size
;
331 /* rollback to if_free */
333 wil
->platform_handle
=
334 wil_platform_init(&pdev
->dev
, &wil
->platform_ops
, &rops
, wil
);
335 if (!wil
->platform_handle
) {
337 wil_err(wil
, "wil_platform_init failed\n");
340 /* rollback to err_plat */
341 rc
= pci_enable_device(pdev
);
342 if (rc
&& pdev
->msi_enabled
== 0) {
344 "pci_enable_device failed, retry with MSI only\n");
345 /* Work around for platforms that can't allocate IRQ:
346 * retry with MSI only
348 pdev
->msi_enabled
= 1;
349 rc
= pci_enable_device(pdev
);
353 "pci_enable_device failed, even with MSI only\n");
356 /* rollback to err_disable_pdev */
357 pci_set_power_state(pdev
, PCI_D0
);
359 rc
= pci_request_region(pdev
, 0, WIL_NAME
);
361 wil_err(wil
, "pci_request_region failed\n");
362 goto err_disable_pdev
;
364 /* rollback to err_release_reg */
366 wil
->csr
= pci_ioremap_bar(pdev
, 0);
368 wil_err(wil
, "pci_ioremap_bar failed\n");
370 goto err_release_reg
;
372 /* rollback to err_iounmap */
373 wil_info(wil
, "CSR at %pR -> 0x%p\n", &pdev
->resource
[0], wil
->csr
);
375 rc
= wil_set_capabilities(wil
);
377 wil_err(wil
, "wil_set_capabilities failed, rc %d\n", rc
);
381 /* device supports >32bit addresses.
382 * for legacy DMA start from 48 bit.
384 start_idx
= wil
->use_enhanced_dma_hw
? 0 : 1;
386 for (i
= start_idx
; i
< ARRAY_SIZE(dma_addr_size
); i
++) {
387 rc
= dma_set_mask_and_coherent(dev
,
388 DMA_BIT_MASK(dma_addr_size
[i
]));
390 dev_err(dev
, "dma_set_mask_and_coherent(%d) failed: %d\n",
391 dma_addr_size
[i
], rc
);
394 dev_info(dev
, "using dma mask %d", dma_addr_size
[i
]);
395 wil
->dma_addr_size
= dma_addr_size
[i
];
399 if (wil
->dma_addr_size
== 0)
402 wil6210_clear_irq(wil
);
404 /* FW should raise IRQ when ready */
405 rc
= wil_if_pcie_enable(wil
);
407 wil_err(wil
, "Enable device failed\n");
410 /* rollback to bus_disable */
412 wil_clear_fw_log_addr(wil
);
413 rc
= wil_if_add(wil
);
415 wil_err(wil
, "wil_if_add failed: %d\n", rc
);
419 /* in case of WMI-only FW, perform full reset and FW loading */
420 if (test_bit(WMI_FW_CAPABILITY_WMI_ONLY
, wil
->fw_capabilities
)) {
421 wil_dbg_misc(wil
, "Loading WMI only FW\n");
422 mutex_lock(&wil
->mutex
);
423 rc
= wil_reset(wil
, true);
424 mutex_unlock(&wil
->mutex
);
426 wil_err(wil
, "failed to load WMI only FW\n");
427 /* ignore the error to allow debugging */
431 if (IS_ENABLED(CONFIG_PM
))
432 wil
->pm_notify
.notifier_call
= wil6210_pm_notify
;
434 rc
= register_pm_notifier(&wil
->pm_notify
);
436 /* Do not fail the driver initialization, as suspend can
437 * be prevented in a later phase if needed
439 wil_err(wil
, "register_pm_notifier failed: %d\n", rc
);
441 wil6210_debugfs_init(wil
);
443 wil_pm_runtime_allow(wil
);
448 wil_if_pcie_disable(wil
);
450 pci_iounmap(pdev
, wil
->csr
);
452 pci_release_region(pdev
, 0);
454 pci_disable_device(pdev
);
456 wil_platform_ops_uninit(wil
);
463 static void wil_pcie_remove(struct pci_dev
*pdev
)
465 struct wil6210_priv
*wil
= pci_get_drvdata(pdev
);
466 void __iomem
*csr
= wil
->csr
;
468 wil_dbg_misc(wil
, "pcie_remove\n");
470 unregister_pm_notifier(&wil
->pm_notify
);
472 wil_pm_runtime_forbid(wil
);
474 wil6210_debugfs_remove(wil
);
476 wiphy_lock(wil
->wiphy
);
477 wil_p2p_wdev_free(wil
);
478 wil_remove_all_additional_vifs(wil
);
479 wiphy_unlock(wil
->wiphy
);
482 wil_if_pcie_disable(wil
);
483 pci_iounmap(pdev
, csr
);
484 pci_release_region(pdev
, 0);
485 pci_disable_device(pdev
);
486 wil_platform_ops_uninit(wil
);
490 static const struct pci_device_id wil6210_pcie_ids
[] = {
491 { PCI_DEVICE(0x1ae9, 0x0310) },
492 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
493 { PCI_DEVICE(0x17cb, 0x1201) }, /* Talyn */
494 { /* end: all zeroes */ },
496 MODULE_DEVICE_TABLE(pci
, wil6210_pcie_ids
);
498 static int wil6210_suspend(struct device
*dev
, bool is_runtime
)
501 struct pci_dev
*pdev
= to_pci_dev(dev
);
502 struct wil6210_priv
*wil
= pci_get_drvdata(pdev
);
503 bool keep_radio_on
, active_ifaces
;
505 wil_dbg_pm(wil
, "suspend: %s\n", is_runtime
? "runtime" : "system");
507 mutex_lock(&wil
->vif_mutex
);
508 active_ifaces
= wil_has_active_ifaces(wil
, true, false);
509 mutex_unlock(&wil
->vif_mutex
);
510 keep_radio_on
= active_ifaces
&& wil
->keep_radio_on_during_sleep
;
512 rc
= wil_can_suspend(wil
, is_runtime
);
516 rc
= wil_suspend(wil
, is_runtime
, keep_radio_on
);
518 /* In case radio stays on, platform device will control
521 if (!keep_radio_on
) {
522 /* disable bus mastering */
523 pci_clear_master(pdev
);
524 wil
->suspend_stats
.r_off
.successful_suspends
++;
526 wil
->suspend_stats
.r_on
.successful_suspends
++;
533 static int wil6210_resume(struct device
*dev
, bool is_runtime
)
536 struct pci_dev
*pdev
= to_pci_dev(dev
);
537 struct wil6210_priv
*wil
= pci_get_drvdata(pdev
);
538 bool keep_radio_on
, active_ifaces
;
540 wil_dbg_pm(wil
, "resume: %s\n", is_runtime
? "runtime" : "system");
542 mutex_lock(&wil
->vif_mutex
);
543 active_ifaces
= wil_has_active_ifaces(wil
, true, false);
544 mutex_unlock(&wil
->vif_mutex
);
545 keep_radio_on
= active_ifaces
&& wil
->keep_radio_on_during_sleep
;
547 /* In case radio stays on, platform device will control
552 pci_set_master(pdev
);
553 rc
= wil_resume(wil
, is_runtime
, keep_radio_on
);
555 wil_err(wil
, "device failed to resume (%d)\n", rc
);
556 if (!keep_radio_on
) {
557 pci_clear_master(pdev
);
558 wil
->suspend_stats
.r_off
.failed_resumes
++;
560 wil
->suspend_stats
.r_on
.failed_resumes
++;
564 wil
->suspend_stats
.r_on
.successful_resumes
++;
566 wil
->suspend_stats
.r_off
.successful_resumes
++;
572 static int wil6210_pm_notify(struct notifier_block
*notify_block
,
573 unsigned long mode
, void *unused
)
575 struct wil6210_priv
*wil
= container_of(
576 notify_block
, struct wil6210_priv
, pm_notify
);
578 enum wil_platform_event evt
;
580 wil_dbg_pm(wil
, "pm_notify: mode (%ld)\n", mode
);
583 case PM_HIBERNATION_PREPARE
:
584 case PM_SUSPEND_PREPARE
:
585 case PM_RESTORE_PREPARE
:
586 rc
= wil_can_suspend(wil
, false);
589 evt
= WIL_PLATFORM_EVT_PRE_SUSPEND
;
590 if (wil
->platform_ops
.notify
)
591 rc
= wil
->platform_ops
.notify(wil
->platform_handle
,
594 case PM_POST_SUSPEND
:
595 case PM_POST_HIBERNATION
:
596 case PM_POST_RESTORE
:
597 evt
= WIL_PLATFORM_EVT_POST_SUSPEND
;
598 if (wil
->platform_ops
.notify
)
599 rc
= wil
->platform_ops
.notify(wil
->platform_handle
,
603 wil_dbg_pm(wil
, "unhandled notify mode %ld\n", mode
);
607 wil_dbg_pm(wil
, "notification mode %ld: rc (%d)\n", mode
, rc
);
611 static int __maybe_unused
wil6210_pm_suspend(struct device
*dev
)
613 return wil6210_suspend(dev
, false);
616 static int __maybe_unused
wil6210_pm_resume(struct device
*dev
)
618 return wil6210_resume(dev
, false);
621 static int __maybe_unused
wil6210_pm_runtime_idle(struct device
*dev
)
623 struct wil6210_priv
*wil
= dev_get_drvdata(dev
);
625 wil_dbg_pm(wil
, "Runtime idle\n");
627 return wil_can_suspend(wil
, true);
630 static int __maybe_unused
wil6210_pm_runtime_resume(struct device
*dev
)
632 return wil6210_resume(dev
, true);
635 static int __maybe_unused
wil6210_pm_runtime_suspend(struct device
*dev
)
637 struct wil6210_priv
*wil
= dev_get_drvdata(dev
);
639 if (test_bit(wil_status_suspended
, wil
->status
)) {
640 wil_dbg_pm(wil
, "trying to suspend while suspended\n");
644 return wil6210_suspend(dev
, true);
647 static const struct dev_pm_ops wil6210_pm_ops
= {
648 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend
, wil6210_pm_resume
)
649 SET_RUNTIME_PM_OPS(wil6210_pm_runtime_suspend
,
650 wil6210_pm_runtime_resume
,
651 wil6210_pm_runtime_idle
)
654 static struct pci_driver wil6210_driver
= {
655 .probe
= wil_pcie_probe
,
656 .remove
= wil_pcie_remove
,
657 .id_table
= wil6210_pcie_ids
,
660 .pm
= &wil6210_pm_ops
,
664 static int __init
wil6210_driver_init(void)
668 rc
= wil_platform_modinit();
672 rc
= pci_register_driver(&wil6210_driver
);
674 wil_platform_modexit();
677 module_init(wil6210_driver_init
);
679 static void __exit
wil6210_driver_exit(void)
681 pci_unregister_driver(&wil6210_driver
);
682 wil_platform_modexit();
684 module_exit(wil6210_driver_exit
);
686 MODULE_LICENSE("Dual BSD/GPL");
687 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
688 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");