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