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 static void wil_remove_all_additional_vifs(struct wil6210_priv *wil) 141 { 142 struct wil6210_vif *vif; 143 int i; 144 145 for (i = 1; i < wil->max_vifs; i++) { 146 vif = wil->vifs[i]; 147 if (vif) { 148 wil_vif_prepare_stop(vif); 149 wil_vif_remove(wil, vif->mid); 150 } 151 } 152 } 153 154 /* Bus ops */ 155 static int wil_if_pcie_enable(struct wil6210_priv *wil) 156 { 157 struct pci_dev *pdev = wil->pdev; 158 int rc; 159 /* on platforms with buggy ACPI, pdev->msi_enabled may be set to 160 * allow pci_enable_device to work. This indicates INTx was not routed 161 * and only MSI should be used 162 */ 163 int msi_only = pdev->msi_enabled; 164 bool _use_msi = use_msi; 165 166 wil_dbg_misc(wil, "if_pcie_enable\n"); 167 168 pci_set_master(pdev); 169 170 wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx"); 171 172 if (use_msi && pci_enable_msi(pdev)) { 173 wil_err(wil, "pci_enable_msi failed, use INTx\n"); 174 _use_msi = false; 175 } 176 177 if (!_use_msi && msi_only) { 178 wil_err(wil, "Interrupt pin not routed, unable to use INTx\n"); 179 rc = -ENODEV; 180 goto stop_master; 181 } 182 183 rc = wil6210_init_irq(wil, pdev->irq, _use_msi); 184 if (rc) 185 goto stop_master; 186 187 /* need reset here to obtain MAC */ 188 mutex_lock(&wil->mutex); 189 rc = wil_reset(wil, false); 190 mutex_unlock(&wil->mutex); 191 if (rc) 192 goto release_irq; 193 194 return 0; 195 196 release_irq: 197 wil6210_fini_irq(wil, pdev->irq); 198 /* safe to call if no MSI */ 199 pci_disable_msi(pdev); 200 stop_master: 201 pci_clear_master(pdev); 202 return rc; 203 } 204 205 static int wil_if_pcie_disable(struct wil6210_priv *wil) 206 { 207 struct pci_dev *pdev = wil->pdev; 208 209 wil_dbg_misc(wil, "if_pcie_disable\n"); 210 211 pci_clear_master(pdev); 212 /* disable and release IRQ */ 213 wil6210_fini_irq(wil, pdev->irq); 214 /* safe to call if no MSI */ 215 pci_disable_msi(pdev); 216 /* TODO: disable HW */ 217 218 return 0; 219 } 220 221 static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size) 222 { 223 struct wil6210_priv *wil = wil_handle; 224 225 if (!wil) 226 return -EINVAL; 227 228 return wil_fw_copy_crash_dump(wil, buf, size); 229 } 230 231 static int wil_platform_rop_fw_recovery(void *wil_handle) 232 { 233 struct wil6210_priv *wil = wil_handle; 234 235 if (!wil) 236 return -EINVAL; 237 238 wil_fw_error_recovery(wil); 239 240 return 0; 241 } 242 243 static void wil_platform_ops_uninit(struct wil6210_priv *wil) 244 { 245 if (wil->platform_ops.uninit) 246 wil->platform_ops.uninit(wil->platform_handle); 247 memset(&wil->platform_ops, 0, sizeof(wil->platform_ops)); 248 } 249 250 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id) 251 { 252 struct wil6210_priv *wil; 253 struct device *dev = &pdev->dev; 254 int rc; 255 const struct wil_platform_rops rops = { 256 .ramdump = wil_platform_rop_ramdump, 257 .fw_recovery = wil_platform_rop_fw_recovery, 258 }; 259 u32 bar_size = pci_resource_len(pdev, 0); 260 int dma_addr_size[] = {48, 40, 32}; /* keep descending order */ 261 int i; 262 263 /* check HW */ 264 dev_info(&pdev->dev, WIL_NAME 265 " device found [%04x:%04x] (rev %x) bar size 0x%x\n", 266 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision, 267 bar_size); 268 269 if ((bar_size < WIL6210_MIN_MEM_SIZE) || 270 (bar_size > WIL6210_MAX_MEM_SIZE)) { 271 dev_err(&pdev->dev, "Unexpected BAR0 size 0x%x\n", 272 bar_size); 273 return -ENODEV; 274 } 275 276 wil = wil_if_alloc(dev); 277 if (IS_ERR(wil)) { 278 rc = (int)PTR_ERR(wil); 279 dev_err(dev, "wil_if_alloc failed: %d\n", rc); 280 return rc; 281 } 282 283 wil->pdev = pdev; 284 pci_set_drvdata(pdev, wil); 285 wil->bar_size = bar_size; 286 /* rollback to if_free */ 287 288 wil->platform_handle = 289 wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil); 290 if (!wil->platform_handle) { 291 rc = -ENODEV; 292 wil_err(wil, "wil_platform_init failed\n"); 293 goto if_free; 294 } 295 /* rollback to err_plat */ 296 297 /* device supports >32bit addresses */ 298 for (i = 0; i < ARRAY_SIZE(dma_addr_size); i++) { 299 rc = dma_set_mask_and_coherent(dev, 300 DMA_BIT_MASK(dma_addr_size[i])); 301 if (rc) { 302 dev_err(dev, "dma_set_mask_and_coherent(%d) failed: %d\n", 303 dma_addr_size[i], rc); 304 continue; 305 } 306 dev_info(dev, "using dma mask %d", dma_addr_size[i]); 307 wil->dma_addr_size = dma_addr_size[i]; 308 break; 309 } 310 311 if (wil->dma_addr_size == 0) 312 goto err_plat; 313 314 rc = pci_enable_device(pdev); 315 if (rc && pdev->msi_enabled == 0) { 316 wil_err(wil, 317 "pci_enable_device failed, retry with MSI only\n"); 318 /* Work around for platforms that can't allocate IRQ: 319 * retry with MSI only 320 */ 321 pdev->msi_enabled = 1; 322 rc = pci_enable_device(pdev); 323 } 324 if (rc) { 325 wil_err(wil, 326 "pci_enable_device failed, even with MSI only\n"); 327 goto err_plat; 328 } 329 /* rollback to err_disable_pdev */ 330 pci_set_power_state(pdev, PCI_D0); 331 332 rc = pci_request_region(pdev, 0, WIL_NAME); 333 if (rc) { 334 wil_err(wil, "pci_request_region failed\n"); 335 goto err_disable_pdev; 336 } 337 /* rollback to err_release_reg */ 338 339 wil->csr = pci_ioremap_bar(pdev, 0); 340 if (!wil->csr) { 341 wil_err(wil, "pci_ioremap_bar failed\n"); 342 rc = -ENODEV; 343 goto err_release_reg; 344 } 345 /* rollback to err_iounmap */ 346 wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr); 347 348 rc = wil_set_capabilities(wil); 349 if (rc) { 350 wil_err(wil, "wil_set_capabilities failed, rc %d\n", rc); 351 goto err_iounmap; 352 } 353 wil6210_clear_irq(wil); 354 355 /* FW should raise IRQ when ready */ 356 rc = wil_if_pcie_enable(wil); 357 if (rc) { 358 wil_err(wil, "Enable device failed\n"); 359 goto err_iounmap; 360 } 361 /* rollback to bus_disable */ 362 363 rc = wil_if_add(wil); 364 if (rc) { 365 wil_err(wil, "wil_if_add failed: %d\n", rc); 366 goto bus_disable; 367 } 368 369 /* in case of WMI-only FW, perform full reset and FW loading */ 370 if (test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) { 371 wil_dbg_misc(wil, "Loading WMI only FW\n"); 372 mutex_lock(&wil->mutex); 373 rc = wil_reset(wil, true); 374 mutex_unlock(&wil->mutex); 375 if (rc) { 376 wil_err(wil, "failed to load WMI only FW\n"); 377 goto if_remove; 378 } 379 } 380 381 if (IS_ENABLED(CONFIG_PM)) 382 wil->pm_notify.notifier_call = wil6210_pm_notify; 383 384 rc = register_pm_notifier(&wil->pm_notify); 385 if (rc) 386 /* Do not fail the driver initialization, as suspend can 387 * be prevented in a later phase if needed 388 */ 389 wil_err(wil, "register_pm_notifier failed: %d\n", rc); 390 391 wil6210_debugfs_init(wil); 392 393 wil_pm_runtime_allow(wil); 394 395 return 0; 396 397 if_remove: 398 wil_if_remove(wil); 399 bus_disable: 400 wil_if_pcie_disable(wil); 401 err_iounmap: 402 pci_iounmap(pdev, wil->csr); 403 err_release_reg: 404 pci_release_region(pdev, 0); 405 err_disable_pdev: 406 pci_disable_device(pdev); 407 err_plat: 408 wil_platform_ops_uninit(wil); 409 if_free: 410 wil_if_free(wil); 411 412 return rc; 413 } 414 415 static void wil_pcie_remove(struct pci_dev *pdev) 416 { 417 struct wil6210_priv *wil = pci_get_drvdata(pdev); 418 void __iomem *csr = wil->csr; 419 420 wil_dbg_misc(wil, "pcie_remove\n"); 421 422 unregister_pm_notifier(&wil->pm_notify); 423 424 wil_pm_runtime_forbid(wil); 425 426 wil6210_debugfs_remove(wil); 427 rtnl_lock(); 428 wil_p2p_wdev_free(wil); 429 wil_remove_all_additional_vifs(wil); 430 rtnl_unlock(); 431 wil_if_remove(wil); 432 wil_if_pcie_disable(wil); 433 pci_iounmap(pdev, csr); 434 pci_release_region(pdev, 0); 435 pci_disable_device(pdev); 436 wil_platform_ops_uninit(wil); 437 wil_if_free(wil); 438 } 439 440 static const struct pci_device_id wil6210_pcie_ids[] = { 441 { PCI_DEVICE(0x1ae9, 0x0310) }, 442 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */ 443 { PCI_DEVICE(0x17cb, 0x1201) }, /* Talyn */ 444 { /* end: all zeroes */ }, 445 }; 446 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids); 447 448 static int wil6210_suspend(struct device *dev, bool is_runtime) 449 { 450 int rc = 0; 451 struct pci_dev *pdev = to_pci_dev(dev); 452 struct wil6210_priv *wil = pci_get_drvdata(pdev); 453 bool keep_radio_on, active_ifaces; 454 455 wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system"); 456 457 mutex_lock(&wil->vif_mutex); 458 active_ifaces = wil_has_active_ifaces(wil, true, false); 459 mutex_unlock(&wil->vif_mutex); 460 keep_radio_on = active_ifaces && wil->keep_radio_on_during_sleep; 461 462 rc = wil_can_suspend(wil, is_runtime); 463 if (rc) 464 goto out; 465 466 rc = wil_suspend(wil, is_runtime, keep_radio_on); 467 if (!rc) { 468 /* In case radio stays on, platform device will control 469 * PCIe master 470 */ 471 if (!keep_radio_on) { 472 /* disable bus mastering */ 473 pci_clear_master(pdev); 474 wil->suspend_stats.r_off.successful_suspends++; 475 } else { 476 wil->suspend_stats.r_on.successful_suspends++; 477 } 478 } 479 out: 480 return rc; 481 } 482 483 static int wil6210_resume(struct device *dev, bool is_runtime) 484 { 485 int rc = 0; 486 struct pci_dev *pdev = to_pci_dev(dev); 487 struct wil6210_priv *wil = pci_get_drvdata(pdev); 488 bool keep_radio_on, active_ifaces; 489 490 wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system"); 491 492 mutex_lock(&wil->vif_mutex); 493 active_ifaces = wil_has_active_ifaces(wil, true, false); 494 mutex_unlock(&wil->vif_mutex); 495 keep_radio_on = active_ifaces && wil->keep_radio_on_during_sleep; 496 497 /* In case radio stays on, platform device will control 498 * PCIe master 499 */ 500 if (!keep_radio_on) 501 /* allow master */ 502 pci_set_master(pdev); 503 rc = wil_resume(wil, is_runtime, keep_radio_on); 504 if (rc) { 505 wil_err(wil, "device failed to resume (%d)\n", rc); 506 if (!keep_radio_on) { 507 pci_clear_master(pdev); 508 wil->suspend_stats.r_off.failed_resumes++; 509 } else { 510 wil->suspend_stats.r_on.failed_resumes++; 511 } 512 } else { 513 if (keep_radio_on) 514 wil->suspend_stats.r_on.successful_resumes++; 515 else 516 wil->suspend_stats.r_off.successful_resumes++; 517 } 518 519 return rc; 520 } 521 522 static int wil6210_pm_notify(struct notifier_block *notify_block, 523 unsigned long mode, void *unused) 524 { 525 struct wil6210_priv *wil = container_of( 526 notify_block, struct wil6210_priv, pm_notify); 527 int rc = 0; 528 enum wil_platform_event evt; 529 530 wil_dbg_pm(wil, "pm_notify: mode (%ld)\n", mode); 531 532 switch (mode) { 533 case PM_HIBERNATION_PREPARE: 534 case PM_SUSPEND_PREPARE: 535 case PM_RESTORE_PREPARE: 536 rc = wil_can_suspend(wil, false); 537 if (rc) 538 break; 539 evt = WIL_PLATFORM_EVT_PRE_SUSPEND; 540 if (wil->platform_ops.notify) 541 rc = wil->platform_ops.notify(wil->platform_handle, 542 evt); 543 break; 544 case PM_POST_SUSPEND: 545 case PM_POST_HIBERNATION: 546 case PM_POST_RESTORE: 547 evt = WIL_PLATFORM_EVT_POST_SUSPEND; 548 if (wil->platform_ops.notify) 549 rc = wil->platform_ops.notify(wil->platform_handle, 550 evt); 551 break; 552 default: 553 wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode); 554 break; 555 } 556 557 wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc); 558 return rc; 559 } 560 561 static int __maybe_unused wil6210_pm_suspend(struct device *dev) 562 { 563 return wil6210_suspend(dev, false); 564 } 565 566 static int __maybe_unused wil6210_pm_resume(struct device *dev) 567 { 568 return wil6210_resume(dev, false); 569 } 570 571 static int __maybe_unused wil6210_pm_runtime_idle(struct device *dev) 572 { 573 struct pci_dev *pdev = to_pci_dev(dev); 574 struct wil6210_priv *wil = pci_get_drvdata(pdev); 575 576 wil_dbg_pm(wil, "Runtime idle\n"); 577 578 return wil_can_suspend(wil, true); 579 } 580 581 static int __maybe_unused wil6210_pm_runtime_resume(struct device *dev) 582 { 583 return wil6210_resume(dev, true); 584 } 585 586 static int __maybe_unused wil6210_pm_runtime_suspend(struct device *dev) 587 { 588 struct pci_dev *pdev = to_pci_dev(dev); 589 struct wil6210_priv *wil = pci_get_drvdata(pdev); 590 591 if (test_bit(wil_status_suspended, wil->status)) { 592 wil_dbg_pm(wil, "trying to suspend while suspended\n"); 593 return 1; 594 } 595 596 return wil6210_suspend(dev, true); 597 } 598 599 static const struct dev_pm_ops wil6210_pm_ops = { 600 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume) 601 SET_RUNTIME_PM_OPS(wil6210_pm_runtime_suspend, 602 wil6210_pm_runtime_resume, 603 wil6210_pm_runtime_idle) 604 }; 605 606 static struct pci_driver wil6210_driver = { 607 .probe = wil_pcie_probe, 608 .remove = wil_pcie_remove, 609 .id_table = wil6210_pcie_ids, 610 .name = WIL_NAME, 611 .driver = { 612 .pm = &wil6210_pm_ops, 613 }, 614 }; 615 616 static int __init wil6210_driver_init(void) 617 { 618 int rc; 619 620 rc = wil_platform_modinit(); 621 if (rc) 622 return rc; 623 624 rc = pci_register_driver(&wil6210_driver); 625 if (rc) 626 wil_platform_modexit(); 627 return rc; 628 } 629 module_init(wil6210_driver_init); 630 631 static void __exit wil6210_driver_exit(void) 632 { 633 pci_unregister_driver(&wil6210_driver); 634 wil_platform_modexit(); 635 } 636 module_exit(wil6210_driver_exit); 637 638 MODULE_LICENSE("Dual BSD/GPL"); 639 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>"); 640 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card"); 641