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