Sync if_ath with FreeBSD.
[dragonfly.git] / sys / dev / netif / ath / ath / if_ath_pci.c
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  * $FreeBSD: head/sys/dev/ath/if_ath_pci.c 192147 2009-05-15 17:02:11Z imp $
30  * $DragonFly$
31  */
32
33 #include <sys/cdefs.h>
34
35 /*
36  * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.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_at386.h>
48 #include <sys/bus.h>
49 #include <sys/rman.h>
50
51 #include <sys/socket.h>
52  
53 #include <net/if.h>
54 #include <net/if_media.h>
55 #include <net/if_arp.h>
56
57 #include <netproto/802_11/ieee80211_var.h>
58
59 #include <dev/netif/ath/ath/if_athvar.h>
60
61 #include <bus/pci/pcivar.h>
62 #include <bus/pci/pcireg.h>
63
64 /*
65  * PCI glue.
66  */
67
68 struct ath_pci_softc {
69         struct ath_softc        sc_sc;
70         struct resource         *sc_sr;         /* memory resource */
71         struct resource         *sc_irq;        /* irq resource */
72         void                    *sc_ih;         /* interrupt handler */
73 };
74
75 #define BS_BAR  0x10
76 #define PCIR_RETRY_TIMEOUT      0x41
77
78 static int
79 ath_pci_probe(device_t dev)
80 {
81         const char* devname;
82
83         devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
84         if (devname != NULL) {
85                 device_set_desc(dev, devname);
86                 return BUS_PROBE_DEFAULT;
87         }
88         return ENXIO;
89 }
90
91 static int
92 ath_pci_attach(device_t dev)
93 {
94         struct ath_pci_softc *psc = device_get_softc(dev);
95         struct ath_softc *sc = &psc->sc_sc;
96         int error = ENXIO;
97         int rid;
98
99         sc->sc_dev = dev;
100
101         /*
102          * Enable bus mastering.
103          */
104         pci_enable_busmaster(dev);
105
106         /*
107          * Disable retry timeout to keep PCI Tx retries from
108          * interfering with C3 CPU state.
109          */
110         pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
111
112         /* 
113          * Setup memory-mapping of PCI registers.
114          */
115         rid = BS_BAR;
116         psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
117                                             RF_ACTIVE);
118         if (psc->sc_sr == NULL) {
119                 device_printf(dev, "cannot map register space\n");
120                 goto bad;
121         }
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);
125         /*
126          * Mark device invalid so any interrupts (shared or otherwise)
127          * that arrive before the HAL is setup are discarded.
128          */
129         sc->sc_invalid = 1;
130
131         /*
132          * Arrange interrupt line.
133          */
134         rid = 0;
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");
139                 goto bad1;
140         }
141         if (bus_setup_intr(dev, psc->sc_irq,
142                            INTR_MPSAFE,
143                            ath_intr, sc, &psc->sc_ih, NULL)) {
144                 device_printf(dev, "could not establish interrupt\n");
145                 goto bad2;
146         }
147
148         /*
149          * Setup DMA descriptor area.
150          */
151         if (bus_dma_tag_create(sc->sc_dmat,     /* parent */
152                                1, 0,                    /* alignment, bounds */
153                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
154                                BUS_SPACE_MAXADDR,       /* highaddr */
155                                NULL, NULL,              /* filter, filterarg */
156                                0x3ffff,                 /* maxsize XXX */
157                                ATH_MAX_SCATTER,         /* nsegments */
158                                0x3ffff,                 /* maxsegsize XXX */
159                                BUS_DMA_ALLOCNOW,        /* flags */
160                                &sc->sc_dmat)) {
161                 device_printf(dev, "cannot allocate DMA tag\n");
162                 goto bad3;
163         }
164
165         ATH_LOCK_INIT(sc);
166
167         error = ath_attach(pci_get_device(dev), sc);
168         if (error == 0)                                 /* success */
169                 return 0;
170
171         ATH_LOCK_DESTROY(sc);
172         bus_dma_tag_destroy(sc->sc_dmat);
173 bad3:
174         bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
175 bad2:
176         bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
177 bad1:
178         bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
179 bad:
180         return (error);
181 }
182
183 static int
184 ath_pci_detach(device_t dev)
185 {
186         struct ath_pci_softc *psc = device_get_softc(dev);
187         struct ath_softc *sc = &psc->sc_sc;
188
189         /* check if device was removed */
190         sc->sc_invalid = !bus_child_present(dev);
191
192         ath_detach(sc);
193
194         bus_generic_detach(dev);
195         bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
196         bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
197
198         bus_dma_tag_destroy(sc->sc_dmat);
199         bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
200
201         ATH_LOCK_DESTROY(sc);
202
203         return (0);
204 }
205
206 static int
207 ath_pci_shutdown(device_t dev)
208 {
209         struct ath_pci_softc *psc = device_get_softc(dev);
210
211         ath_shutdown(&psc->sc_sc);
212         return (0);
213 }
214
215 static int
216 ath_pci_suspend(device_t dev)
217 {
218         struct ath_pci_softc *psc = device_get_softc(dev);
219
220         ath_suspend(&psc->sc_sc);
221
222         return (0);
223 }
224
225 static int
226 ath_pci_resume(device_t dev)
227 {
228         struct ath_pci_softc *psc = device_get_softc(dev);
229
230         ath_resume(&psc->sc_sc);
231
232         return (0);
233 }
234
235 static device_method_t ath_pci_methods[] = {
236         /* Device interface */
237         DEVMETHOD(device_probe,         ath_pci_probe),
238         DEVMETHOD(device_attach,        ath_pci_attach),
239         DEVMETHOD(device_detach,        ath_pci_detach),
240         DEVMETHOD(device_shutdown,      ath_pci_shutdown),
241         DEVMETHOD(device_suspend,       ath_pci_suspend),
242         DEVMETHOD(device_resume,        ath_pci_resume),
243
244         { 0,0 }
245 };
246 static driver_t ath_pci_driver = {
247         "ath",
248         ath_pci_methods,
249         sizeof (struct ath_pci_softc)
250 };
251 static  devclass_t ath_devclass;
252 DRIVER_MODULE(ath, pci, ath_pci_driver, ath_devclass, 0, 0);
253 MODULE_VERSION(ath, 1);
254 MODULE_DEPEND(ath, wlan, 1, 1, 1);              /* 802.11 media layer */