xref: /dragonfly/sys/dev/netif/ath/ath/if_ath_pci.c (revision 03517d4e)
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, MERCHANTABILITY
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/errno.h>
45 
46 #if defined(__DragonFly__)
47 /* empty */
48 #else
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #endif
52 #include <sys/bus.h>
53 #include <sys/rman.h>
54 
55 #include <sys/socket.h>
56 
57 #include <net/if.h>
58 #include <net/if_media.h>
59 #include <net/if_arp.h>
60 #include <net/ethernet.h>
61 
62 #include <netproto/802_11/ieee80211_var.h>
63 
64 #include <dev/netif/ath/ath/if_athvar.h>
65 
66 #if defined(__DragonFly__)
67 #include <bus/pci/pcivar.h>
68 #include <bus/pci/pcireg.h>
69 #else
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcireg.h>
72 #endif
73 
74 /* For EEPROM firmware */
75 #ifdef	ATH_EEPROM_FIRMWARE
76 #include <sys/linker.h>
77 #include <sys/firmware.h>
78 #endif	/* ATH_EEPROM_FIRMWARE */
79 
80 /*
81  * PCI glue.
82  */
83 
84 struct ath_pci_softc {
85 	struct ath_softc	sc_sc;
86 	struct resource		*sc_sr;		/* memory resource */
87 	struct resource		*sc_irq;	/* irq resource */
88 	void			*sc_ih;		/* interrupt handler */
89 };
90 
91 /*
92  * XXX eventually this should be some system level definition
93  * so modules will hvae probe/attach information like USB.
94  * But for now..
95  */
96 struct pci_device_id {
97 	int vendor_id;
98 	int device_id;
99 
100 	int sub_vendor_id;
101 	int sub_device_id;
102 
103 	int driver_data;
104 
105 	int match_populated:1;
106 	int match_vendor_id:1;
107 	int match_device_id:1;
108 	int match_sub_vendor_id:1;
109 	int match_sub_device_id:1;
110 };
111 
112 #define	PCI_VDEVICE(v, s) \
113 	.vendor_id = (v), \
114 	.device_id = (s), \
115 	.match_populated = 1, \
116 	.match_vendor_id = 1, \
117 	.match_device_id = 1
118 
119 #define	PCI_DEVICE_SUB(v, d, dv, ds) \
120 	.match_populated = 1, \
121 	.vendor_id = (v), .match_vendor_id = 1, \
122 	.device_id = (d), .match_device_id = 1, \
123 	.sub_vendor_id = (dv), .match_sub_vendor_id = 1, \
124 	.sub_device_id = (ds), .match_sub_device_id = 1
125 
126 #define	PCI_VENDOR_ID_ATHEROS		0x168c
127 #define	PCI_VENDOR_ID_SAMSUNG		0x144d
128 #define	PCI_VENDOR_ID_AZWAVE		0x1a3b
129 #define	PCI_VENDOR_ID_FOXCONN		0x105b
130 #define	PCI_VENDOR_ID_ATTANSIC		0x1969
131 #define	PCI_VENDOR_ID_ASUSTEK		0x1043
132 #define	PCI_VENDOR_ID_DELL		0x1028
133 #define	PCI_VENDOR_ID_QMI		0x1a32
134 #define	PCI_VENDOR_ID_LENOVO		0x17aa
135 #define	PCI_VENDOR_ID_HP		0x103c
136 
137 #include "if_ath_pci_devlist.h"
138 
139 /*
140  * Attempt to find a match for the given device in
141  * the given device table.
142  *
143  * Returns the device structure or NULL if no matching
144  * PCI device is found.
145  */
146 static const struct pci_device_id *
147 ath_pci_probe_device(device_t dev, const struct pci_device_id *dev_table, int nentries)
148 {
149 	int i;
150 	int vendor_id, device_id;
151 	int sub_vendor_id, sub_device_id;
152 
153 	vendor_id = pci_get_vendor(dev);
154 	device_id = pci_get_device(dev);
155 	sub_vendor_id = pci_get_subvendor(dev);
156 	sub_device_id = pci_get_subdevice(dev);
157 
158 	for (i = 0; i < nentries; i++) {
159 		/* Don't match on non-populated (eg empty) entries */
160 		if (! dev_table[i].match_populated)
161 			continue;
162 
163 		if (dev_table[i].match_vendor_id &&
164 		    (dev_table[i].vendor_id != vendor_id))
165 			continue;
166 		if (dev_table[i].match_device_id &&
167 		    (dev_table[i].device_id != device_id))
168 			continue;
169 		if (dev_table[i].match_sub_vendor_id &&
170 		    (dev_table[i].sub_vendor_id != sub_vendor_id))
171 			continue;
172 		if (dev_table[i].match_sub_device_id &&
173 		    (dev_table[i].sub_device_id != sub_device_id))
174 			continue;
175 
176 		/* Match */
177 		return (&dev_table[i]);
178 	}
179 
180 	return (NULL);
181 }
182 
183 #define	BS_BAR	0x10
184 #define	PCIR_RETRY_TIMEOUT	0x41
185 #define	PCIR_CFG_PMCSR		0x48
186 
187 #define	DEFAULT_CACHESIZE	32
188 
189 static void
190 ath_pci_setup(device_t dev)
191 {
192 	uint8_t cz;
193 
194 	/* XXX TODO: need to override the _system_ saved copies of this */
195 
196 	/*
197 	 * If the cache line size is 0, force it to a reasonable
198 	 * value.
199 	 */
200 	cz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
201 	if (cz == 0) {
202 		pci_write_config(dev, PCIR_CACHELNSZ,
203 		    DEFAULT_CACHESIZE / 4, 1);
204 	}
205 
206 	/* Override the system latency timer */
207 	pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
208 
209 	/* If a PCI NIC, force wakeup */
210 #ifdef	ATH_PCI_WAKEUP_WAR
211 	/* XXX TODO: don't do this for non-PCI (ie, PCIe, Cardbus!) */
212 	if (1) {
213 		uint16_t pmcsr;
214 		pmcsr = pci_read_config(dev, PCIR_CFG_PMCSR, 2);
215 		pmcsr |= 3;
216 		pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
217 		pmcsr &= ~3;
218 		pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
219 	}
220 #endif
221 
222 	/*
223 	 * Disable retry timeout to keep PCI Tx retries from
224 	 * interfering with C3 CPU state.
225 	 */
226 	pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
227 }
228 
229 static int
230 ath_pci_probe(device_t dev)
231 {
232 	const char* devname;
233 
234 	devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
235 	if (devname != NULL) {
236 		device_set_desc(dev, devname);
237 		return BUS_PROBE_DEFAULT;
238 	}
239 	return ENXIO;
240 }
241 
242 static int
243 ath_pci_attach(device_t dev)
244 {
245 	struct ath_pci_softc *psc = device_get_softc(dev);
246 	struct ath_softc *sc = &psc->sc_sc;
247 	int error = ENXIO;
248 	int rid;
249 #ifdef	ATH_EEPROM_FIRMWARE
250 	const struct firmware *fw = NULL;
251 	const char *buf;
252 #endif
253 	const struct pci_device_id *pd;
254 
255 	sc->sc_dev = dev;
256 
257 	/* Do this lookup anyway; figure out what to do with it later */
258 	pd = ath_pci_probe_device(dev, ath_pci_id_table, nitems(ath_pci_id_table));
259 	if (pd)
260 		sc->sc_pci_devinfo = pd->driver_data;
261 
262 	/*
263 	 * Enable bus mastering.
264 	 */
265 	pci_enable_busmaster(dev);
266 
267 	/*
268 	 * Setup other PCI bus configuration parameters.
269 	 */
270 	ath_pci_setup(dev);
271 
272 	/*
273 	 * Setup memory-mapping of PCI registers.
274 	 */
275 	rid = BS_BAR;
276 	psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
277 					    RF_ACTIVE);
278 	if (psc->sc_sr == NULL) {
279 		device_printf(dev, "cannot map register space\n");
280 		goto bad;
281 	}
282 	sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
283 	sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
284 	/*
285 	 * Mark device invalid so any interrupts (shared or otherwise)
286 	 * that arrive before the HAL is setup are discarded.
287 	 */
288 	sc->sc_invalid = 1;
289 
290 	ATH_LOCK_INIT(sc);
291 	ATH_PCU_LOCK_INIT(sc);
292 	ATH_RX_LOCK_INIT(sc);
293 	ATH_TX_LOCK_INIT(sc);
294 	ATH_TXSTATUS_LOCK_INIT(sc);
295 
296 	/*
297 	 * Arrange interrupt line.
298 	 */
299 	rid = 0;
300 	psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
301 					     RF_SHAREABLE|RF_ACTIVE);
302 	if (psc->sc_irq == NULL) {
303 		device_printf(dev, "could not map interrupt\n");
304 		goto bad1;
305 	}
306 #if defined(__DragonFly__)
307 	if (bus_setup_intr(dev, psc->sc_irq,
308 			   INTR_MPSAFE,
309 			   ath_intr, sc, &psc->sc_ih,
310 			   &wlan_global_serializer)) {
311 		device_printf(dev, "could not establish interrupt\n");
312 		goto bad2;
313 	}
314 #else
315 	if (bus_setup_intr(dev, psc->sc_irq,
316 			   INTR_TYPE_NET | INTR_MPSAFE,
317 			   NULL, ath_intr, sc, &psc->sc_ih)) {
318 		device_printf(dev, "could not establish interrupt\n");
319 		goto bad2;
320 	}
321 #endif
322 
323 	/*
324 	 * Setup DMA descriptor area.
325 	 */
326 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
327 #if defined(__DragonFly__)
328 			       16, 0,			/* alignment, bounds */
329 #else
330 			       1, 0,			/* alignment, bounds */
331 #endif
332 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
333 			       BUS_SPACE_MAXADDR,	/* highaddr */
334 #if defined(__DragonFly__)
335 #else
336 			       NULL, NULL,		/* filter, filterarg */
337 #endif
338 			       0x3ffff,			/* maxsize XXX */
339 			       ATH_MAX_SCATTER,		/* nsegments */
340 			       0x3ffff,			/* maxsegsize XXX */
341 			       BUS_DMA_ALLOCNOW,	/* flags */
342 #if defined(__DragonFly__)
343 #else
344 			       NULL,			/* lockfunc */
345 			       NULL,			/* lockarg */
346 #endif
347 			       &sc->sc_dmat)) {
348 		device_printf(dev, "cannot allocate DMA tag\n");
349 		goto bad3;
350 	}
351 
352 #ifdef	ATH_EEPROM_FIRMWARE
353 	/*
354 	 * If there's an EEPROM firmware image, load that in.
355 	 */
356 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
357 	    "eeprom_firmware", &buf) == 0) {
358 		if (bootverbose)
359 			device_printf(dev, "%s: looking up firmware @ '%s'\n",
360 			    __func__, buf);
361 
362 		fw = firmware_get(buf);
363 		if (fw == NULL) {
364 			device_printf(dev, "%s: couldn't find firmware\n",
365 			    __func__);
366 			goto bad4;
367 		}
368 
369 		device_printf(dev, "%s: EEPROM firmware @ %p\n",
370 		    __func__, fw->data);
371 		sc->sc_eepromdata =
372 		    kmalloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
373 		if (! sc->sc_eepromdata) {
374 			device_printf(dev, "%s: can't malloc eepromdata\n",
375 			    __func__);
376 			goto bad4;
377 		}
378 		memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
379 		firmware_put(fw, 0);
380 	}
381 #endif /* ATH_EEPROM_FIRMWARE */
382 
383 	error = ath_attach(pci_get_device(dev), sc);
384 	if (error == 0)					/* success */
385 		return 0;
386 
387 #ifdef ATH_EEPROM_FIRMWARE
388 bad4:
389 #endif
390 	bus_dma_tag_destroy(sc->sc_dmat);
391 bad3:
392 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
393 bad2:
394 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
395 bad1:
396 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
397 
398 	ATH_TXSTATUS_LOCK_DESTROY(sc);
399 	ATH_PCU_LOCK_DESTROY(sc);
400 	ATH_RX_LOCK_DESTROY(sc);
401 	ATH_TX_LOCK_DESTROY(sc);
402 	ATH_LOCK_DESTROY(sc);
403 bad:
404 	return (error);
405 }
406 
407 static int
408 ath_pci_detach(device_t dev)
409 {
410 	struct ath_pci_softc *psc = device_get_softc(dev);
411 	struct ath_softc *sc = &psc->sc_sc;
412 
413 	/* check if device was removed */
414 	sc->sc_invalid = !bus_child_present(dev);
415 
416 	/*
417 	 * Do a config read to clear pre-existing pci error status.
418 	 */
419 	(void) pci_read_config(dev, PCIR_COMMAND, 4);
420 
421 	ath_detach(sc);
422 
423 	bus_generic_detach(dev);
424 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
425 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
426 
427 	bus_dma_tag_destroy(sc->sc_dmat);
428 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
429 
430 	if (sc->sc_eepromdata)
431 		kfree(sc->sc_eepromdata, M_TEMP);
432 
433 	ATH_TXSTATUS_LOCK_DESTROY(sc);
434 	ATH_PCU_LOCK_DESTROY(sc);
435 	ATH_RX_LOCK_DESTROY(sc);
436 	ATH_TX_LOCK_DESTROY(sc);
437 	ATH_LOCK_DESTROY(sc);
438 
439 	return (0);
440 }
441 
442 static int
443 ath_pci_shutdown(device_t dev)
444 {
445 	struct ath_pci_softc *psc = device_get_softc(dev);
446 
447 	ath_shutdown(&psc->sc_sc);
448 	return (0);
449 }
450 
451 static int
452 ath_pci_suspend(device_t dev)
453 {
454 	struct ath_pci_softc *psc = device_get_softc(dev);
455 
456 	ath_suspend(&psc->sc_sc);
457 
458 	return (0);
459 }
460 
461 static int
462 ath_pci_resume(device_t dev)
463 {
464 	struct ath_pci_softc *psc = device_get_softc(dev);
465 
466 	/*
467 	 * Suspend/resume resets the PCI configuration space.
468 	 */
469 	ath_pci_setup(dev);
470 
471 	ath_resume(&psc->sc_sc);
472 
473 	return (0);
474 }
475 
476 static device_method_t ath_pci_methods[] = {
477 	/* Device interface */
478 	DEVMETHOD(device_probe,		ath_pci_probe),
479 	DEVMETHOD(device_attach,	ath_pci_attach),
480 	DEVMETHOD(device_detach,	ath_pci_detach),
481 	DEVMETHOD(device_shutdown,	ath_pci_shutdown),
482 	DEVMETHOD(device_suspend,	ath_pci_suspend),
483 	DEVMETHOD(device_resume,	ath_pci_resume),
484 
485 	{ 0,0 }
486 };
487 static driver_t ath_pci_driver = {
488 	"ath",
489 	ath_pci_methods,
490 	sizeof (struct ath_pci_softc)
491 };
492 static	devclass_t ath_devclass;
493 DRIVER_MODULE(ath_pci, pci, ath_pci_driver, ath_devclass, NULL, NULL);
494 MODULE_VERSION(ath_pci, 1);
495 MODULE_DEPEND(ath_pci, wlan, 1, 1, 1);		/* 802.11 media layer */
496 MODULE_DEPEND(ath_pci, if_ath, 1, 1, 1);	/* if_ath driver */
497