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