Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / netif / an / if_an_pci.c
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/an/if_an_pci.c,v 1.2.2.8 2003/02/11 03:32:48 ambrisko Exp $
33  */
34
35 /*
36  * This is a PCI shim for the Aironet PC4500/4800 wireless network
37  * driver. Aironet makes PCMCIA, ISA and PCI versions of these devices,
38  * which all have basically the same interface. The ISA and PCI cards
39  * are actually bridge adapters with PCMCIA cards inserted into them,
40  * however they appear as normal PCI or ISA devices to the host.
41  *
42  * All we do here is handle the PCI probe and attach and set up an
43  * interrupt handler entry point. The PCI version of the card uses
44  * a PLX 9050 PCI to "dumb bus" bridge chip, which provides us with
45  * multiple PCI address space mappings. The primary mapping at PCI
46  * register 0x14 is for the PLX chip itself, *NOT* the Aironet card.
47  * The I/O address of the Aironet is actually at register 0x18, which
48  * is the local bus mapping register for bus space 0. There are also
49  * registers for additional register spaces at registers 0x1C and
50  * 0x20, but these are unused in the Aironet devices. To find out
51  * more, you need a datasheet for the 9050 from PLX, but you have
52  * to go through their sales office to get it. Bleh.
53  */
54
55 #include "opt_inet.h"
56
57 #ifdef INET
58 #define ANCACHE
59 #endif
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
64 #include <sys/mbuf.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67 #include <sys/socket.h>
68 #include <sys/interrupt.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/rman.h>
72
73 #include <net/if.h>
74 #include <net/if_arp.h>
75 #include <net/ethernet.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
78
79 #include <bus/pci/pcidevs.h>
80 #include <bus/pci/pcireg.h>
81 #include <bus/pci/pcivar.h>
82
83 #include "if_aironet_ieee.h"
84 #include "if_anreg.h"
85
86 struct an_type {
87         uint16_t                 an_vid;
88         uint16_t                 an_did;
89         int                      an_port_rid;
90         const char              *an_name;
91 };
92
93 static const struct an_type an_devs[] = {
94         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_350,
95           PCIR_BAR(2), "Cisco Aironet 350 Series" },
96         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_PC4500,
97           PCIR_BAR(2), "Aironet PCI4500" },
98         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_PC4800,
99           PCIR_BAR(2), "Aironet PCI4800" },
100         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_PC4xxx,
101           PCIR_BAR(2), "Aironet PCI4500/PCI4800" },
102         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_MPI350,
103           PCIR_BAR(0), "Cisco Aironet MPI350" },
104         { 0, 0, 0, NULL }
105 };
106
107 static int an_probe_pci         (device_t);
108 static int an_attach_pci        (device_t);
109 static int an_suspend_pci       (device_t);
110 static int an_resume_pci        (device_t);
111
112 static int
113 an_probe_pci(device_t dev)
114 {
115         const struct an_type *t;
116         uint16_t vid, did;
117
118         vid = pci_get_vendor(dev);
119         did = pci_get_device(dev);
120         for (t = an_devs; t->an_name != NULL; ++t) {
121                 if (vid == t->an_vid && did == t->an_did) {
122                         struct an_softc *sc;
123
124                         sc = device_get_softc(dev);
125                         sc->port_rid = t->an_port_rid;
126                         if (vid == PCI_VENDOR_AIRONET &&
127                             did == PCI_PRODUCT_AIRONET_MPI350)
128                                 sc->mpi350 = 1;
129
130                         device_set_desc(dev, t->an_name);
131                         return(0);
132                 }
133         }
134         return(ENXIO);
135 }
136
137 static int
138 an_attach_pci(device_t dev)
139 {
140         struct an_softc         *sc;
141         struct ifnet            *ifp;
142         int                     flags, error;
143
144         sc = device_get_softc(dev);
145         ifp = &sc->arpcom.ac_if;
146         flags = device_get_flags(dev);
147
148         error = an_alloc_port(dev, sc->port_rid, 1);
149         if (error) {
150                 device_printf(dev, "couldn't map ports\n");
151                 goto fail;
152         }
153
154         sc->an_btag = rman_get_bustag(sc->port_res);
155         sc->an_bhandle = rman_get_bushandle(sc->port_res);
156
157         /* Allocate memory for MPI350 */
158         if (sc->mpi350) {
159                 /* Allocate memory */
160                 sc->mem_rid = PCIR_MAPS + 4;
161                 error = an_alloc_memory(dev, sc->mem_rid, 1);
162                 if (error) {
163                         device_printf(dev, "couldn't map memory\n");
164                         goto fail;
165                 }
166                 sc->an_mem_btag = rman_get_bustag(sc->mem_res);
167                 sc->an_mem_bhandle = rman_get_bushandle(sc->mem_res);
168
169                 /* Allocate aux. memory */
170                 sc->mem_aux_rid = PCIR_MAPS + 8;
171                 error = an_alloc_aux_memory(dev, sc->mem_aux_rid, 
172                     AN_AUXMEMSIZE);
173                 if (error) {
174                         device_printf(dev, "couldn't map aux memory\n");
175                         goto fail;
176                 }
177                 sc->an_mem_aux_btag = rman_get_bustag(sc->mem_aux_res);
178                 sc->an_mem_aux_bhandle = rman_get_bushandle(sc->mem_aux_res);
179
180                 /* Allocate DMA region */
181                 error = bus_dma_tag_create(NULL,        /* parent */
182                                1, 0,                    /* alignment, bounds */
183                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
184                                BUS_SPACE_MAXADDR,       /* highaddr */
185                                NULL, NULL,              /* filter, filterarg */
186                                0x3ffff,                 /* maxsize XXX */
187                                1,                       /* nsegments */
188                                0xffff,                  /* maxsegsize XXX */
189                                BUS_DMA_ALLOCNOW,        /* flags */
190                                &sc->an_dtag);
191                 if (error) {
192                         device_printf(dev, "couldn't get DMA region\n");
193                         goto fail;
194                 }
195         }
196
197         /* Allocate interrupt */
198         error = an_alloc_irq(dev, 0, RF_SHAREABLE);
199         if (error)
200                 goto fail;
201
202         error = an_attach(sc, dev, flags);
203         if (error)
204                 goto fail;
205
206         error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
207                                an_intr, sc, &sc->irq_handle, 
208                                sc->arpcom.ac_if.if_serializer);
209         if (error) {
210                 ifmedia_removeall(&sc->an_ifmedia);
211                 ether_ifdetach(&sc->arpcom.ac_if);
212                 goto fail;
213         }
214
215         ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->irq_res));
216         KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
217
218         return(0);
219
220 fail:
221         an_release_resources(dev);
222         return(error);
223 }
224
225 static int
226 an_suspend_pci(device_t dev)
227 {
228         an_shutdown(dev);
229         
230         return (0);
231 }
232
233 static int
234 an_resume_pci(device_t dev)
235 {
236         an_resume(dev);
237
238         return (0);
239 }
240
241 static device_method_t an_pci_methods[] = {
242         /* Device interface */
243         DEVMETHOD(device_probe,         an_probe_pci),
244         DEVMETHOD(device_attach,        an_attach_pci),
245         DEVMETHOD(device_detach,        an_detach),
246         DEVMETHOD(device_shutdown,      an_shutdown),
247         DEVMETHOD(device_suspend,       an_suspend_pci),
248         DEVMETHOD(device_resume,        an_resume_pci),
249         { 0, 0 }
250 };
251
252 static driver_t an_pci_driver = {
253         "an",
254         an_pci_methods,
255         sizeof(struct an_softc),
256 };
257
258 static devclass_t an_devclass;
259
260 DRIVER_MODULE(if_an, pci, an_pci_driver, an_devclass, NULL, NULL);