2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 * $FreeBSD: head/sys/dev/ath/if_ath_pci.c 192147 2009-05-15 17:02:11Z imp $
34 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
40 #include <sys/kernel.h>
42 #include <sys/mutex.h>
43 #include <sys/errno.h>
47 #include <sys/socket.h>
50 #include <net/if_media.h>
51 #include <net/if_arp.h>
53 #include <netproto/802_11/ieee80211_var.h>
55 #include <dev/netif/ath/ath/if_athvar.h>
57 #include <bus/pci/pcivar.h>
58 #include <bus/pci/pcireg.h>
64 struct ath_pci_softc {
65 struct ath_softc sc_sc;
66 struct resource *sc_sr; /* memory resource */
67 struct resource *sc_irq; /* irq resource */
68 void *sc_ih; /* interrupt handler */
72 #define PCIR_RETRY_TIMEOUT 0x41
75 ath_pci_probe(device_t dev)
79 wlan_serialize_enter();
80 devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
81 if (devname != NULL) {
82 device_set_desc(dev, devname);
83 wlan_serialize_exit();
84 return BUS_PROBE_DEFAULT;
86 wlan_serialize_exit();
91 ath_pci_attach(device_t dev)
93 struct ath_pci_softc *psc = device_get_softc(dev);
94 struct ath_softc *sc = &psc->sc_sc;
98 wlan_serialize_enter();
102 * Enable bus mastering.
104 pci_enable_busmaster(dev);
107 * Disable retry timeout to keep PCI Tx retries from
108 * interfering with C3 CPU state.
110 pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
113 * Setup memory-mapping of PCI registers.
116 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
118 if (psc->sc_sr == NULL) {
119 device_printf(dev, "cannot map register space\n");
122 /* XXX uintptr_t is a bandaid for ia64; to be fixed */
123 sc->sc_st = (HAL_BUS_TAG)(uintptr_t) rman_get_bustag(psc->sc_sr);
124 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
126 * Mark device invalid so any interrupts (shared or otherwise)
127 * that arrive before the HAL is setup are discarded.
132 * Arrange interrupt line.
135 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
136 RF_SHAREABLE|RF_ACTIVE);
137 if (psc->sc_irq == NULL) {
138 device_printf(dev, "could not map interrupt\n");
141 if (bus_setup_intr(dev, psc->sc_irq,
143 ath_intr, sc, &psc->sc_ih,
144 &wlan_global_serializer)) {
145 device_printf(dev, "could not establish interrupt\n");
150 * Setup DMA descriptor area.
152 if (bus_dma_tag_create(sc->sc_dmat, /* parent */
153 4, 0, /* alignment, bounds */
154 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
155 BUS_SPACE_MAXADDR, /* highaddr */
156 NULL, NULL, /* filter, filterarg */
157 0x3ffff, /* maxsize XXX */
158 ATH_MAX_SCATTER, /* nsegments */
159 0x3ffff, /* maxsegsize XXX */
160 BUS_DMA_ALLOCNOW, /* flags */
162 device_printf(dev, "cannot allocate DMA tag\n");
166 error = ath_attach(pci_get_device(dev), sc);
167 if (error == 0) { /* success */
168 wlan_serialize_exit();
172 bus_dma_tag_destroy(sc->sc_dmat);
174 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
176 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
178 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
180 wlan_serialize_exit();
185 ath_pci_detach(device_t dev)
187 struct ath_pci_softc *psc = device_get_softc(dev);
188 struct ath_softc *sc = &psc->sc_sc;
190 wlan_serialize_enter();
191 /* check if device was removed */
192 sc->sc_invalid = !bus_child_present(dev);
196 bus_generic_detach(dev);
197 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
198 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
200 bus_dma_tag_destroy(sc->sc_dmat);
201 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
203 wlan_serialize_exit();
208 ath_pci_shutdown(device_t dev)
210 struct ath_pci_softc *psc = device_get_softc(dev);
212 wlan_serialize_enter();
213 ath_shutdown(&psc->sc_sc);
214 wlan_serialize_exit();
219 ath_pci_suspend(device_t dev)
221 struct ath_pci_softc *psc = device_get_softc(dev);
223 wlan_serialize_enter();
224 ath_suspend(&psc->sc_sc);
225 wlan_serialize_exit();
231 ath_pci_resume(device_t dev)
233 struct ath_pci_softc *psc = device_get_softc(dev);
235 wlan_serialize_enter();
236 ath_resume(&psc->sc_sc);
237 wlan_serialize_exit();
242 static device_method_t ath_pci_methods[] = {
243 /* Device interface */
244 DEVMETHOD(device_probe, ath_pci_probe),
245 DEVMETHOD(device_attach, ath_pci_attach),
246 DEVMETHOD(device_detach, ath_pci_detach),
247 DEVMETHOD(device_shutdown, ath_pci_shutdown),
248 DEVMETHOD(device_suspend, ath_pci_suspend),
249 DEVMETHOD(device_resume, ath_pci_resume),
253 static driver_t ath_pci_driver = {
256 sizeof (struct ath_pci_softc)
258 static devclass_t ath_devclass;
259 DRIVER_MODULE(ath, pci, ath_pci_driver, ath_devclass, NULL, NULL);
260 MODULE_VERSION(ath, 1);
262 MODULE_DEPEND(ath, ath_hal, 1, 1, 1); /* Atheros HAL */
263 MODULE_DEPEND(ath, ath_rate, 1, 1, 1); /* rate control algorithm */
264 MODULE_DEPEND(ath, wlan, 1, 1, 1); /* 802.11 media layer */