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 #include <linux/pm_runtime.h> 25 26 static bool use_msi = true; 27 module_param(use_msi, bool, 0444); 28 MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true"); 29 30 static bool ftm_mode; 31 module_param(ftm_mode, bool, 0444); 32 MODULE_PARM_DESC(ftm_mode, " Set factory test mode, default - false"); 33 34 #ifdef CONFIG_PM 35 static int wil6210_pm_notify(struct notifier_block *notify_block, 36 unsigned long mode, void *unused); 37 #endif /* CONFIG_PM */ 38 39 static 40 void wil_set_capabilities(struct wil6210_priv *wil) 41 { 42 const char *wil_fw_name; 43 u32 jtag_id = wil_r(wil, RGF_USER_JTAG_DEV_ID); 44 u8 chip_revision = (wil_r(wil, RGF_USER_REVISION_ID) & 45 RGF_USER_REVISION_ID_MASK); 46 47 bitmap_zero(wil->hw_capabilities, hw_capability_last); 48 bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX); 49 wil->wil_fw_name = ftm_mode ? WIL_FW_NAME_FTM_DEFAULT : 50 WIL_FW_NAME_DEFAULT; 51 wil->chip_revision = chip_revision; 52 53 switch (jtag_id) { 54 case JTAG_DEV_ID_SPARROW: 55 switch (chip_revision) { 56 case REVISION_ID_SPARROW_D0: 57 wil->hw_name = "Sparrow D0"; 58 wil->hw_version = HW_VER_SPARROW_D0; 59 wil_fw_name = ftm_mode ? WIL_FW_NAME_FTM_SPARROW_PLUS : 60 WIL_FW_NAME_SPARROW_PLUS; 61 62 if (wil_fw_verify_file_exists(wil, wil_fw_name)) 63 wil->wil_fw_name = wil_fw_name; 64 break; 65 case REVISION_ID_SPARROW_B0: 66 wil->hw_name = "Sparrow B0"; 67 wil->hw_version = HW_VER_SPARROW_B0; 68 break; 69 default: 70 wil->hw_name = "Unknown"; 71 wil->hw_version = HW_VER_UNKNOWN; 72 break; 73 } 74 break; 75 default: 76 wil_err(wil, "Unknown board hardware, chip_id 0x%08x, chip_revision 0x%08x\n", 77 jtag_id, chip_revision); 78 wil->hw_name = "Unknown"; 79 wil->hw_version = HW_VER_UNKNOWN; 80 } 81 82 wil_info(wil, "Board hardware is %s\n", wil->hw_name); 83 84 /* extract FW capabilities from file without loading the FW */ 85 wil_request_firmware(wil, wil->wil_fw_name, false); 86 wil_refresh_fw_capabilities(wil); 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 /* FW should raise IRQ when ready */ 297 rc = wil_if_pcie_enable(wil); 298 if (rc) { 299 wil_err(wil, "Enable device failed\n"); 300 goto err_iounmap; 301 } 302 /* rollback to bus_disable */ 303 304 rc = wil_if_add(wil); 305 if (rc) { 306 wil_err(wil, "wil_if_add failed: %d\n", rc); 307 goto bus_disable; 308 } 309 310 #ifdef CONFIG_PM 311 wil->pm_notify.notifier_call = wil6210_pm_notify; 312 rc = register_pm_notifier(&wil->pm_notify); 313 if (rc) 314 /* Do not fail the driver initialization, as suspend can 315 * be prevented in a later phase if needed 316 */ 317 wil_err(wil, "register_pm_notifier failed: %d\n", rc); 318 #endif /* CONFIG_PM */ 319 320 wil6210_debugfs_init(wil); 321 322 wil_pm_runtime_allow(wil); 323 324 return 0; 325 326 bus_disable: 327 wil_if_pcie_disable(wil); 328 err_iounmap: 329 pci_iounmap(pdev, wil->csr); 330 err_release_reg: 331 pci_release_region(pdev, 0); 332 err_disable_pdev: 333 pci_disable_device(pdev); 334 err_plat: 335 wil_platform_ops_uninit(wil); 336 if_free: 337 wil_if_free(wil); 338 339 return rc; 340 } 341 342 static void wil_pcie_remove(struct pci_dev *pdev) 343 { 344 struct wil6210_priv *wil = pci_get_drvdata(pdev); 345 void __iomem *csr = wil->csr; 346 347 wil_dbg_misc(wil, "pcie_remove\n"); 348 349 #ifdef CONFIG_PM 350 unregister_pm_notifier(&wil->pm_notify); 351 #endif /* CONFIG_PM */ 352 353 wil_pm_runtime_forbid(wil); 354 355 wil6210_debugfs_remove(wil); 356 rtnl_lock(); 357 wil_p2p_wdev_free(wil); 358 rtnl_unlock(); 359 wil_if_remove(wil); 360 wil_if_pcie_disable(wil); 361 pci_iounmap(pdev, csr); 362 pci_release_region(pdev, 0); 363 pci_disable_device(pdev); 364 wil_platform_ops_uninit(wil); 365 wil_if_free(wil); 366 } 367 368 static const struct pci_device_id wil6210_pcie_ids[] = { 369 { PCI_DEVICE(0x1ae9, 0x0310) }, 370 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */ 371 { /* end: all zeroes */ }, 372 }; 373 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids); 374 375 #ifdef CONFIG_PM 376 377 static int wil6210_suspend(struct device *dev, bool is_runtime) 378 { 379 int rc = 0; 380 struct pci_dev *pdev = to_pci_dev(dev); 381 struct wil6210_priv *wil = pci_get_drvdata(pdev); 382 struct net_device *ndev = wil_to_ndev(wil); 383 bool keep_radio_on = ndev->flags & IFF_UP && 384 wil->keep_radio_on_during_sleep; 385 386 wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system"); 387 388 rc = wil_can_suspend(wil, is_runtime); 389 if (rc) 390 goto out; 391 392 rc = wil_suspend(wil, is_runtime, keep_radio_on); 393 if (!rc) { 394 /* In case radio stays on, platform device will control 395 * PCIe master 396 */ 397 if (!keep_radio_on) { 398 /* disable bus mastering */ 399 pci_clear_master(pdev); 400 wil->suspend_stats.r_off.successful_suspends++; 401 } else { 402 wil->suspend_stats.r_on.successful_suspends++; 403 } 404 } 405 out: 406 return rc; 407 } 408 409 static int wil6210_resume(struct device *dev, bool is_runtime) 410 { 411 int rc = 0; 412 struct pci_dev *pdev = to_pci_dev(dev); 413 struct wil6210_priv *wil = pci_get_drvdata(pdev); 414 struct net_device *ndev = wil_to_ndev(wil); 415 bool keep_radio_on = ndev->flags & IFF_UP && 416 wil->keep_radio_on_during_sleep; 417 418 wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system"); 419 420 /* In case radio stays on, platform device will control 421 * PCIe master 422 */ 423 if (!keep_radio_on) 424 /* allow master */ 425 pci_set_master(pdev); 426 rc = wil_resume(wil, is_runtime, keep_radio_on); 427 if (rc) { 428 wil_err(wil, "device failed to resume (%d)\n", rc); 429 if (!keep_radio_on) { 430 pci_clear_master(pdev); 431 wil->suspend_stats.r_off.failed_resumes++; 432 } else { 433 wil->suspend_stats.r_on.failed_resumes++; 434 } 435 } else { 436 if (keep_radio_on) 437 wil->suspend_stats.r_on.successful_resumes++; 438 else 439 wil->suspend_stats.r_off.successful_resumes++; 440 } 441 442 return rc; 443 } 444 445 static int wil6210_pm_notify(struct notifier_block *notify_block, 446 unsigned long mode, void *unused) 447 { 448 struct wil6210_priv *wil = container_of( 449 notify_block, struct wil6210_priv, pm_notify); 450 int rc = 0; 451 enum wil_platform_event evt; 452 453 wil_dbg_pm(wil, "pm_notify: mode (%ld)\n", mode); 454 455 switch (mode) { 456 case PM_HIBERNATION_PREPARE: 457 case PM_SUSPEND_PREPARE: 458 case PM_RESTORE_PREPARE: 459 rc = wil_can_suspend(wil, false); 460 if (rc) 461 break; 462 evt = WIL_PLATFORM_EVT_PRE_SUSPEND; 463 if (wil->platform_ops.notify) 464 rc = wil->platform_ops.notify(wil->platform_handle, 465 evt); 466 break; 467 case PM_POST_SUSPEND: 468 case PM_POST_HIBERNATION: 469 case PM_POST_RESTORE: 470 evt = WIL_PLATFORM_EVT_POST_SUSPEND; 471 if (wil->platform_ops.notify) 472 rc = wil->platform_ops.notify(wil->platform_handle, 473 evt); 474 break; 475 default: 476 wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode); 477 break; 478 } 479 480 wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc); 481 return rc; 482 } 483 484 static int wil6210_pm_suspend(struct device *dev) 485 { 486 return wil6210_suspend(dev, false); 487 } 488 489 static int wil6210_pm_resume(struct device *dev) 490 { 491 return wil6210_resume(dev, false); 492 } 493 494 static int wil6210_pm_runtime_idle(struct device *dev) 495 { 496 struct pci_dev *pdev = to_pci_dev(dev); 497 struct wil6210_priv *wil = pci_get_drvdata(pdev); 498 499 wil_dbg_pm(wil, "Runtime idle\n"); 500 501 return wil_can_suspend(wil, true); 502 } 503 504 static int wil6210_pm_runtime_resume(struct device *dev) 505 { 506 return wil6210_resume(dev, true); 507 } 508 509 static int wil6210_pm_runtime_suspend(struct device *dev) 510 { 511 struct pci_dev *pdev = to_pci_dev(dev); 512 struct wil6210_priv *wil = pci_get_drvdata(pdev); 513 514 if (test_bit(wil_status_suspended, wil->status)) { 515 wil_dbg_pm(wil, "trying to suspend while suspended\n"); 516 return 1; 517 } 518 519 return wil6210_suspend(dev, true); 520 } 521 #endif /* CONFIG_PM */ 522 523 static const struct dev_pm_ops wil6210_pm_ops = { 524 #ifdef CONFIG_PM 525 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume) 526 SET_RUNTIME_PM_OPS(wil6210_pm_runtime_suspend, 527 wil6210_pm_runtime_resume, 528 wil6210_pm_runtime_idle) 529 #endif /* CONFIG_PM */ 530 }; 531 532 static struct pci_driver wil6210_driver = { 533 .probe = wil_pcie_probe, 534 .remove = wil_pcie_remove, 535 .id_table = wil6210_pcie_ids, 536 .name = WIL_NAME, 537 .driver = { 538 .pm = &wil6210_pm_ops, 539 }, 540 }; 541 542 static int __init wil6210_driver_init(void) 543 { 544 int rc; 545 546 rc = wil_platform_modinit(); 547 if (rc) 548 return rc; 549 550 rc = pci_register_driver(&wil6210_driver); 551 if (rc) 552 wil_platform_modexit(); 553 return rc; 554 } 555 module_init(wil6210_driver_init); 556 557 static void __exit wil6210_driver_exit(void) 558 { 559 pci_unregister_driver(&wil6210_driver); 560 wil_platform_modexit(); 561 } 562 module_exit(wil6210_driver_exit); 563 564 MODULE_LICENSE("Dual BSD/GPL"); 565 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>"); 566 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card"); 567