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