1 /*
2  * Copyright (c) 2012 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/debugfs.h>
19 #include <linux/pci.h>
20 #include <linux/moduleparam.h>
21 
22 #include "wil6210.h"
23 
24 static int use_msi = 1;
25 module_param(use_msi, int, S_IRUGO);
26 MODULE_PARM_DESC(use_msi,
27 		 " Use MSI interrupt: "
28 		 "0 - don't, 1 - (default) - single, or 3");
29 
30 /* Bus ops */
31 static int wil_if_pcie_enable(struct wil6210_priv *wil)
32 {
33 	struct pci_dev *pdev = wil->pdev;
34 	int rc;
35 
36 	pci_set_master(pdev);
37 
38 	/*
39 	 * how many MSI interrupts to request?
40 	 */
41 	switch (use_msi) {
42 	case 3:
43 	case 1:
44 	case 0:
45 		break;
46 	default:
47 		wil_err(wil, "Invalid use_msi=%d, default to 1\n",
48 			use_msi);
49 		use_msi = 1;
50 	}
51 	wil->n_msi = use_msi;
52 	if (wil->n_msi) {
53 		wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
54 		rc = pci_enable_msi_block(pdev, wil->n_msi);
55 		if (rc && (wil->n_msi == 3)) {
56 			wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
57 			wil->n_msi = 1;
58 			rc = pci_enable_msi_block(pdev, wil->n_msi);
59 		}
60 		if (rc) {
61 			wil_err(wil, "pci_enable_msi failed, use INTx\n");
62 			wil->n_msi = 0;
63 		}
64 	} else {
65 		wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
66 	}
67 
68 	rc = wil6210_init_irq(wil, pdev->irq);
69 	if (rc)
70 		goto stop_master;
71 
72 	/* need reset here to obtain MAC */
73 	rc = wil_reset(wil);
74 	if (rc)
75 		goto release_irq;
76 
77 	return 0;
78 
79  release_irq:
80 	wil6210_fini_irq(wil, pdev->irq);
81 	/* safe to call if no MSI */
82 	pci_disable_msi(pdev);
83  stop_master:
84 	pci_clear_master(pdev);
85 	return rc;
86 }
87 
88 static int wil_if_pcie_disable(struct wil6210_priv *wil)
89 {
90 	struct pci_dev *pdev = wil->pdev;
91 
92 	pci_clear_master(pdev);
93 	/* disable and release IRQ */
94 	wil6210_fini_irq(wil, pdev->irq);
95 	/* safe to call if no MSI */
96 	pci_disable_msi(pdev);
97 	/* TODO: disable HW */
98 
99 	return 0;
100 }
101 
102 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
103 {
104 	struct wil6210_priv *wil;
105 	struct device *dev = &pdev->dev;
106 	void __iomem *csr;
107 	int rc;
108 
109 	/* check HW */
110 	dev_info(&pdev->dev, WIL_NAME " device found [%04x:%04x] (rev %x)\n",
111 		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
112 
113 	if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
114 		dev_err(&pdev->dev, "Not " WIL_NAME "? "
115 			"BAR0 size is %lu while expecting %lu\n",
116 			(ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
117 		return -ENODEV;
118 	}
119 
120 	rc = pci_enable_device(pdev);
121 	if (rc) {
122 		dev_err(&pdev->dev, "pci_enable_device failed\n");
123 		return -ENODEV;
124 	}
125 	/* rollback to err_disable_pdev */
126 
127 	rc = pci_request_region(pdev, 0, WIL_NAME);
128 	if (rc) {
129 		dev_err(&pdev->dev, "pci_request_region failed\n");
130 		goto err_disable_pdev;
131 	}
132 	/* rollback to err_release_reg */
133 
134 	csr = pci_ioremap_bar(pdev, 0);
135 	if (!csr) {
136 		dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
137 		rc = -ENODEV;
138 		goto err_release_reg;
139 	}
140 	/* rollback to err_iounmap */
141 	dev_info(&pdev->dev, "CSR at %pR -> %p\n", &pdev->resource[0], csr);
142 
143 	wil = wil_if_alloc(dev, csr);
144 	if (IS_ERR(wil)) {
145 		rc = (int)PTR_ERR(wil);
146 		dev_err(dev, "wil_if_alloc failed: %d\n", rc);
147 		goto err_iounmap;
148 	}
149 	/* rollback to if_free */
150 
151 	pci_set_drvdata(pdev, wil);
152 	wil->pdev = pdev;
153 
154 	/* FW should raise IRQ when ready */
155 	rc = wil_if_pcie_enable(wil);
156 	if (rc) {
157 		wil_err(wil, "Enable device failed\n");
158 		goto if_free;
159 	}
160 	/* rollback to bus_disable */
161 
162 	rc = wil_if_add(wil);
163 	if (rc) {
164 		wil_err(wil, "wil_if_add failed: %d\n", rc);
165 		goto bus_disable;
166 	}
167 
168 	wil6210_debugfs_init(wil);
169 
170 	/* check FW is alive */
171 	wmi_echo(wil);
172 
173 	return 0;
174 
175  bus_disable:
176 	wil_if_pcie_disable(wil);
177  if_free:
178 	wil_if_free(wil);
179  err_iounmap:
180 	pci_iounmap(pdev, csr);
181  err_release_reg:
182 	pci_release_region(pdev, 0);
183  err_disable_pdev:
184 	pci_disable_device(pdev);
185 
186 	return rc;
187 }
188 
189 static void wil_pcie_remove(struct pci_dev *pdev)
190 {
191 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
192 
193 	wil6210_debugfs_remove(wil);
194 	wil_if_pcie_disable(wil);
195 	wil_if_remove(wil);
196 	wil_if_free(wil);
197 	pci_iounmap(pdev, wil->csr);
198 	pci_release_region(pdev, 0);
199 	pci_disable_device(pdev);
200 }
201 
202 static DEFINE_PCI_DEVICE_TABLE(wil6210_pcie_ids) = {
203 	{ PCI_DEVICE(0x1ae9, 0x0301) },
204 	{ /* end: all zeroes */	},
205 };
206 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
207 
208 static struct pci_driver wil6210_driver = {
209 	.probe		= wil_pcie_probe,
210 	.remove		= wil_pcie_remove,
211 	.id_table	= wil6210_pcie_ids,
212 	.name		= WIL_NAME,
213 };
214 
215 module_pci_driver(wil6210_driver);
216 
217 MODULE_LICENSE("Dual BSD/GPL");
218 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
219 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");
220