Merge branch 'vendor/TCSH'
[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  */
31
32 /*
33  * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h> 
38 #include <sys/module.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/errno.h>
43 #include <sys/bus.h>
44 #include <sys/rman.h>
45
46 #include <sys/socket.h>
47  
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/if_arp.h>
51
52 #include <netproto/802_11/ieee80211_var.h>
53
54 #include <dev/netif/ath/ath/if_athvar.h>
55
56 #include <bus/pci/pcivar.h>
57 #include <bus/pci/pcireg.h>
58
59 /*
60  * PCI glue.
61  */
62
63 struct ath_pci_softc {
64         struct ath_softc        sc_sc;
65         struct resource         *sc_sr;         /* memory resource */
66         struct resource         *sc_irq;        /* irq resource */
67         void                    *sc_ih;         /* interrupt handler */
68 };
69
70 #define BS_BAR  0x10
71 #define PCIR_RETRY_TIMEOUT      0x41
72
73 static int
74 ath_pci_probe(device_t dev)
75 {
76         const char* devname;
77
78         wlan_serialize_enter();
79         devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
80         if (devname != NULL) {
81                 device_set_desc(dev, devname);
82                 wlan_serialize_exit();
83                 return BUS_PROBE_DEFAULT;
84         }
85         wlan_serialize_exit();
86         return ENXIO;
87 }
88
89 static int
90 ath_pci_attach(device_t dev)
91 {
92         struct ath_pci_softc *psc = device_get_softc(dev);
93         struct ath_softc *sc = &psc->sc_sc;
94         int error = ENXIO;
95         int rid;
96
97         wlan_serialize_enter();
98         sc->sc_dev = dev;
99
100         /*
101          * Enable bus mastering.
102          */
103         pci_enable_busmaster(dev);
104
105         /*
106          * Disable retry timeout to keep PCI Tx retries from
107          * interfering with C3 CPU state.
108          */
109         pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
110
111         /* 
112          * Setup memory-mapping of PCI registers.
113          */
114         rid = BS_BAR;
115         psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
116                                             RF_ACTIVE);
117         if (psc->sc_sr == NULL) {
118                 device_printf(dev, "cannot map register space\n");
119                 goto bad;
120         }
121         /* XXX uintptr_t is a bandaid for ia64; to be fixed */
122         sc->sc_st = (HAL_BUS_TAG)(uintptr_t) rman_get_bustag(psc->sc_sr);
123         sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
124         /*
125          * Mark device invalid so any interrupts (shared or otherwise)
126          * that arrive before the HAL is setup are discarded.
127          */
128         sc->sc_invalid = 1;
129
130         /*
131          * Arrange interrupt line.
132          */
133         rid = 0;
134         psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
135                                              RF_SHAREABLE|RF_ACTIVE);
136         if (psc->sc_irq == NULL) {
137                 device_printf(dev, "could not map interrupt\n");
138                 goto bad1;
139         }
140         if (bus_setup_intr(dev, psc->sc_irq,
141                            INTR_MPSAFE,
142                            ath_intr, sc, &psc->sc_ih,
143                            &wlan_global_serializer)) {
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                                4, 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         error = ath_attach(pci_get_device(dev), sc);
166         if (error == 0) {                               /* success */
167                 wlan_serialize_exit();
168                 return 0;
169         }
170
171         bus_dma_tag_destroy(sc->sc_dmat);
172 bad3:
173         bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
174 bad2:
175         bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
176 bad1:
177         bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
178 bad:
179         wlan_serialize_exit();
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         wlan_serialize_enter();
190         /* check if device was removed */
191         sc->sc_invalid = !bus_child_present(dev);
192
193         ath_detach(sc);
194
195         bus_generic_detach(dev);
196         bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
197         bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
198
199         bus_dma_tag_destroy(sc->sc_dmat);
200         bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
201
202         wlan_serialize_exit();
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         wlan_serialize_enter();
212         ath_shutdown(&psc->sc_sc);
213         wlan_serialize_exit();
214         return (0);
215 }
216
217 static int
218 ath_pci_suspend(device_t dev)
219 {
220         struct ath_pci_softc *psc = device_get_softc(dev);
221
222         wlan_serialize_enter();
223         ath_suspend(&psc->sc_sc);
224         wlan_serialize_exit();
225
226         return (0);
227 }
228
229 static int
230 ath_pci_resume(device_t dev)
231 {
232         struct ath_pci_softc *psc = device_get_softc(dev);
233
234         wlan_serialize_enter();
235         ath_resume(&psc->sc_sc);
236         wlan_serialize_exit();
237
238         return (0);
239 }
240
241 static device_method_t ath_pci_methods[] = {
242         /* Device interface */
243         DEVMETHOD(device_probe,         ath_pci_probe),
244         DEVMETHOD(device_attach,        ath_pci_attach),
245         DEVMETHOD(device_detach,        ath_pci_detach),
246         DEVMETHOD(device_shutdown,      ath_pci_shutdown),
247         DEVMETHOD(device_suspend,       ath_pci_suspend),
248         DEVMETHOD(device_resume,        ath_pci_resume),
249
250         DEVMETHOD_END
251 };
252 static driver_t ath_pci_driver = {
253         "ath",
254         ath_pci_methods,
255         sizeof (struct ath_pci_softc)
256 };
257 static  devclass_t ath_devclass;
258 DRIVER_MODULE(ath, pci, ath_pci_driver, ath_devclass, NULL, NULL);
259 MODULE_VERSION(ath, 1);
260
261 MODULE_DEPEND(ath, ath_hal, 1, 1, 1);   /* Atheros HAL */
262 MODULE_DEPEND(ath, ath_rate, 1, 1, 1);  /* rate control algorithm */
263 MODULE_DEPEND(ath, wlan, 1, 1, 1);      /* 802.11 media layer */