xref: /freebsd/sys/dev/ath/if_ath_pci.c (revision 7afb8adff33d47f10a11368ff54bb2eec5b30165)
1 /*-
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
35  */
36 #include "opt_ath.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/errno.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/bus.h>
50 #include <sys/rman.h>
51 
52 #include <sys/socket.h>
53 
54 #include <net/if.h>
55 #include <net/if_media.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 
59 #include <net80211/ieee80211_var.h>
60 
61 #include <dev/ath/if_athvar.h>
62 
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pcireg.h>
65 
66 /* For EEPROM firmware */
67 #ifdef	ATH_EEPROM_FIRMWARE
68 #include <sys/linker.h>
69 #include <sys/firmware.h>
70 #endif	/* ATH_EEPROM_FIRMWARE */
71 
72 /*
73  * PCI glue.
74  */
75 
76 struct ath_pci_softc {
77 	struct ath_softc	sc_sc;
78 	struct resource		*sc_sr;		/* memory resource */
79 	struct resource		*sc_irq;	/* irq resource */
80 	void			*sc_ih;		/* interrupt handler */
81 };
82 
83 /*
84  * XXX eventually this should be some system level definition
85  * so modules will hvae probe/attach information like USB.
86  * But for now..
87  */
88 struct pci_device_id {
89 	int vendor_id;
90 	int device_id;
91 
92 	int sub_vendor_id;
93 	int sub_device_id;
94 
95 	int driver_data;
96 
97 	int match_populated:1;
98 	int match_vendor_id:1;
99 	int match_device_id:1;
100 	int match_sub_vendor_id:1;
101 	int match_sub_device_id:1;
102 };
103 
104 #define	PCI_VDEVICE(v, s) \
105 	.vendor_id = (v), \
106 	.device_id = (s), \
107 	.match_populated = 1, \
108 	.match_vendor_id = 1, \
109 	.match_device_id = 1
110 
111 #define	PCI_DEVICE_SUB(v, d, dv, ds) \
112 	.match_populated = 1, \
113 	.vendor_id = (v), .match_vendor_id = 1, \
114 	.device_id = (d), .match_device_id = 1, \
115 	.sub_vendor_id = (dv), .match_sub_vendor_id = 1, \
116 	.sub_device_id = (ds), .match_sub_device_id = 1
117 
118 #define	PCI_VENDOR_ID_ATHEROS		0x168c
119 #define	PCI_VENDOR_ID_SAMSUNG		0x144d
120 #define	PCI_VENDOR_ID_AZWAVE		0x1a3b
121 #define	PCI_VENDOR_ID_FOXCONN		0x105b
122 #define	PCI_VENDOR_ID_ATTANSIC		0x1969
123 #define	PCI_VENDOR_ID_ASUSTEK		0x1043
124 #define	PCI_VENDOR_ID_DELL		0x1028
125 #define	PCI_VENDOR_ID_QMI		0x1a32
126 #define	PCI_VENDOR_ID_LENOVO		0x17aa
127 #define	PCI_VENDOR_ID_HP		0x103c
128 
129 #include "if_ath_pci_devlist.h"
130 
131 /*
132  * Attempt to find a match for the given device in
133  * the given device table.
134  *
135  * Returns the device structure or NULL if no matching
136  * PCI device is found.
137  */
138 static const struct pci_device_id *
139 ath_pci_probe_device(device_t dev, const struct pci_device_id *dev_table, int nentries)
140 {
141 	int i;
142 	int vendor_id, device_id;
143 	int sub_vendor_id, sub_device_id;
144 
145 	vendor_id = pci_get_vendor(dev);
146 	device_id = pci_get_device(dev);
147 	sub_vendor_id = pci_get_subvendor(dev);
148 	sub_device_id = pci_get_subdevice(dev);
149 
150 	for (i = 0; i < nentries; i++) {
151 		/* Don't match on non-populated (eg empty) entries */
152 		if (! dev_table[i].match_populated)
153 			continue;
154 
155 		if (dev_table[i].match_vendor_id &&
156 		    (dev_table[i].vendor_id != vendor_id))
157 			continue;
158 		if (dev_table[i].match_device_id &&
159 		    (dev_table[i].device_id != device_id))
160 			continue;
161 		if (dev_table[i].match_sub_vendor_id &&
162 		    (dev_table[i].sub_vendor_id != sub_vendor_id))
163 			continue;
164 		if (dev_table[i].match_sub_device_id &&
165 		    (dev_table[i].sub_device_id != sub_device_id))
166 			continue;
167 
168 		/* Match */
169 		return (&dev_table[i]);
170 	}
171 
172 	return (NULL);
173 }
174 
175 #define	BS_BAR	0x10
176 #define	PCIR_RETRY_TIMEOUT	0x41
177 #define	PCIR_CFG_PMCSR		0x48
178 
179 #define	DEFAULT_CACHESIZE	32
180 
181 static void
182 ath_pci_setup(device_t dev)
183 {
184 	uint8_t cz;
185 
186 	/* XXX TODO: need to override the _system_ saved copies of this */
187 
188 	/*
189 	 * If the cache line size is 0, force it to a reasonable
190 	 * value.
191 	 */
192 	cz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
193 	if (cz == 0) {
194 		pci_write_config(dev, PCIR_CACHELNSZ,
195 		    DEFAULT_CACHESIZE / 4, 1);
196 	}
197 
198 	/* Override the system latency timer */
199 	pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
200 
201 	/* If a PCI NIC, force wakeup */
202 #ifdef	ATH_PCI_WAKEUP_WAR
203 	/* XXX TODO: don't do this for non-PCI (ie, PCIe, Cardbus!) */
204 	if (1) {
205 		uint16_t pmcsr;
206 		pmcsr = pci_read_config(dev, PCIR_CFG_PMCSR, 2);
207 		pmcsr |= 3;
208 		pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
209 		pmcsr &= ~3;
210 		pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
211 	}
212 #endif
213 
214 	/*
215 	 * Disable retry timeout to keep PCI Tx retries from
216 	 * interfering with C3 CPU state.
217 	 */
218 	pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
219 }
220 
221 static int
222 ath_pci_probe(device_t dev)
223 {
224 	const char* devname;
225 
226 	devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
227 	if (devname != NULL) {
228 		device_set_desc(dev, devname);
229 		return BUS_PROBE_DEFAULT;
230 	}
231 	return ENXIO;
232 }
233 
234 static int
235 ath_pci_attach(device_t dev)
236 {
237 	struct ath_pci_softc *psc = device_get_softc(dev);
238 	struct ath_softc *sc = &psc->sc_sc;
239 	int error = ENXIO;
240 	int rid;
241 #ifdef	ATH_EEPROM_FIRMWARE
242 	const struct firmware *fw = NULL;
243 	const char *buf;
244 #endif
245 	const struct pci_device_id *pd;
246 
247 	sc->sc_dev = dev;
248 
249 	/* Do this lookup anyway; figure out what to do with it later */
250 	pd = ath_pci_probe_device(dev, ath_pci_id_table, nitems(ath_pci_id_table));
251 	if (pd)
252 		sc->sc_pci_devinfo = pd->driver_data;
253 
254 	/*
255 	 * Enable bus mastering.
256 	 */
257 	pci_enable_busmaster(dev);
258 
259 	/*
260 	 * Setup other PCI bus configuration parameters.
261 	 */
262 	ath_pci_setup(dev);
263 
264 	/*
265 	 * Setup memory-mapping of PCI registers.
266 	 */
267 	rid = BS_BAR;
268 	psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
269 					    RF_ACTIVE);
270 	if (psc->sc_sr == NULL) {
271 		device_printf(dev, "cannot map register space\n");
272 		goto bad;
273 	}
274 	sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
275 	sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
276 	/*
277 	 * Mark device invalid so any interrupts (shared or otherwise)
278 	 * that arrive before the HAL is setup are discarded.
279 	 */
280 	sc->sc_invalid = 1;
281 
282 	ATH_LOCK_INIT(sc);
283 	ATH_PCU_LOCK_INIT(sc);
284 	ATH_RX_LOCK_INIT(sc);
285 	ATH_TX_LOCK_INIT(sc);
286 	ATH_TX_IC_LOCK_INIT(sc);
287 	ATH_TXSTATUS_LOCK_INIT(sc);
288 
289 	/*
290 	 * Arrange interrupt line.
291 	 */
292 	rid = 0;
293 	psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
294 					     RF_SHAREABLE|RF_ACTIVE);
295 	if (psc->sc_irq == NULL) {
296 		device_printf(dev, "could not map interrupt\n");
297 		goto bad1;
298 	}
299 	if (bus_setup_intr(dev, psc->sc_irq,
300 			   INTR_TYPE_NET | INTR_MPSAFE,
301 			   NULL, ath_intr, sc, &psc->sc_ih)) {
302 		device_printf(dev, "could not establish interrupt\n");
303 		goto bad2;
304 	}
305 
306 	/*
307 	 * Setup DMA descriptor area.
308 	 */
309 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
310 			       1, 0,			/* alignment, bounds */
311 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
312 			       BUS_SPACE_MAXADDR,	/* highaddr */
313 			       NULL, NULL,		/* filter, filterarg */
314 			       0x3ffff,			/* maxsize XXX */
315 			       ATH_MAX_SCATTER,		/* nsegments */
316 			       0x3ffff,			/* maxsegsize XXX */
317 			       BUS_DMA_ALLOCNOW,	/* flags */
318 			       NULL,			/* lockfunc */
319 			       NULL,			/* lockarg */
320 			       &sc->sc_dmat)) {
321 		device_printf(dev, "cannot allocate DMA tag\n");
322 		goto bad3;
323 	}
324 
325 #ifdef	ATH_EEPROM_FIRMWARE
326 	/*
327 	 * If there's an EEPROM firmware image, load that in.
328 	 */
329 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
330 	    "eeprom_firmware", &buf) == 0) {
331 		if (bootverbose)
332 			device_printf(dev, "%s: looking up firmware @ '%s'\n",
333 			    __func__, buf);
334 
335 		fw = firmware_get(buf);
336 		if (fw == NULL) {
337 			device_printf(dev, "%s: couldn't find firmware\n",
338 			    __func__);
339 			goto bad4;
340 		}
341 
342 		device_printf(dev, "%s: EEPROM firmware @ %p\n",
343 		    __func__, fw->data);
344 		sc->sc_eepromdata =
345 		    malloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
346 		if (! sc->sc_eepromdata) {
347 			device_printf(dev, "%s: can't malloc eepromdata\n",
348 			    __func__);
349 			goto bad4;
350 		}
351 		memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
352 		firmware_put(fw, 0);
353 	}
354 #endif /* ATH_EEPROM_FIRMWARE */
355 
356 	error = ath_attach(pci_get_device(dev), sc);
357 	if (error == 0)					/* success */
358 		return 0;
359 
360 #ifdef	ATH_EEPROM_FIRMWARE
361 bad4:
362 #endif
363 	bus_dma_tag_destroy(sc->sc_dmat);
364 bad3:
365 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
366 bad2:
367 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
368 bad1:
369 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
370 
371 	ATH_TXSTATUS_LOCK_DESTROY(sc);
372 	ATH_PCU_LOCK_DESTROY(sc);
373 	ATH_RX_LOCK_DESTROY(sc);
374 	ATH_TX_IC_LOCK_DESTROY(sc);
375 	ATH_TX_LOCK_DESTROY(sc);
376 	ATH_LOCK_DESTROY(sc);
377 
378 bad:
379 	return (error);
380 }
381 
382 static int
383 ath_pci_detach(device_t dev)
384 {
385 	struct ath_pci_softc *psc = device_get_softc(dev);
386 	struct ath_softc *sc = &psc->sc_sc;
387 
388 	/* check if device was removed */
389 	sc->sc_invalid = !bus_child_present(dev);
390 
391 	/*
392 	 * Do a config read to clear pre-existing pci error status.
393 	 */
394 	(void) pci_read_config(dev, PCIR_COMMAND, 4);
395 
396 	ath_detach(sc);
397 
398 	bus_generic_detach(dev);
399 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
400 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
401 
402 	bus_dma_tag_destroy(sc->sc_dmat);
403 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
404 
405 	if (sc->sc_eepromdata)
406 		free(sc->sc_eepromdata, M_TEMP);
407 
408 	ATH_TXSTATUS_LOCK_DESTROY(sc);
409 	ATH_PCU_LOCK_DESTROY(sc);
410 	ATH_RX_LOCK_DESTROY(sc);
411 	ATH_TX_IC_LOCK_DESTROY(sc);
412 	ATH_TX_LOCK_DESTROY(sc);
413 	ATH_LOCK_DESTROY(sc);
414 
415 	return (0);
416 }
417 
418 static int
419 ath_pci_shutdown(device_t dev)
420 {
421 	struct ath_pci_softc *psc = device_get_softc(dev);
422 
423 	ath_shutdown(&psc->sc_sc);
424 	return (0);
425 }
426 
427 static int
428 ath_pci_suspend(device_t dev)
429 {
430 	struct ath_pci_softc *psc = device_get_softc(dev);
431 
432 	ath_suspend(&psc->sc_sc);
433 
434 	return (0);
435 }
436 
437 static int
438 ath_pci_resume(device_t dev)
439 {
440 	struct ath_pci_softc *psc = device_get_softc(dev);
441 
442 	/*
443 	 * Suspend/resume resets the PCI configuration space.
444 	 */
445 	ath_pci_setup(dev);
446 
447 	ath_resume(&psc->sc_sc);
448 
449 	return (0);
450 }
451 
452 static device_method_t ath_pci_methods[] = {
453 	/* Device interface */
454 	DEVMETHOD(device_probe,		ath_pci_probe),
455 	DEVMETHOD(device_attach,	ath_pci_attach),
456 	DEVMETHOD(device_detach,	ath_pci_detach),
457 	DEVMETHOD(device_shutdown,	ath_pci_shutdown),
458 	DEVMETHOD(device_suspend,	ath_pci_suspend),
459 	DEVMETHOD(device_resume,	ath_pci_resume),
460 
461 	{ 0,0 }
462 };
463 static driver_t ath_pci_driver = {
464 	"ath",
465 	ath_pci_methods,
466 	sizeof (struct ath_pci_softc)
467 };
468 static	devclass_t ath_devclass;
469 DRIVER_MODULE(ath_pci, pci, ath_pci_driver, ath_devclass, 0, 0);
470 MODULE_VERSION(ath_pci, 1);
471 MODULE_DEPEND(ath_pci, wlan, 1, 1, 1);		/* 802.11 media layer */
472 MODULE_DEPEND(ath_pci, if_ath, 1, 1, 1);	/* if_ath driver */
473