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