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