Remove unnecessary initialisations. Return ENXIO instead of 1 in
[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  * $DragonFly: src/sys/dev/netif/an/if_an_pci.c,v 1.15 2005/07/28 16:33:25 joerg Exp $
34  */
35
36 /*
37  * This is a PCI shim for the Aironet PC4500/4800 wireless network
38  * driver. Aironet makes PCMCIA, ISA and PCI versions of these devices,
39  * which all have basically the same interface. The ISA and PCI cards
40  * are actually bridge adapters with PCMCIA cards inserted into them,
41  * however they appear as normal PCI or ISA devices to the host.
42  *
43  * All we do here is handle the PCI probe and attach and set up an
44  * interrupt handler entry point. The PCI version of the card uses
45  * a PLX 9050 PCI to "dumb bus" bridge chip, which provides us with
46  * multiple PCI address space mappings. The primary mapping at PCI
47  * register 0x14 is for the PLX chip itself, *NOT* the Aironet card.
48  * The I/O address of the Aironet is actually at register 0x18, which
49  * is the local bus mapping register for bus space 0. There are also
50  * registers for additional register spaces at registers 0x1C and
51  * 0x20, but these are unused in the Aironet devices. To find out
52  * more, you need a datasheet for the 9050 from PLX, but you have
53  * to go through their sales office to get it. Bleh.
54  */
55
56 #include "opt_inet.h"
57
58 #ifdef INET
59 #define ANCACHE
60 #endif
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/sockio.h>
65 #include <sys/mbuf.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
69
70 #include <sys/module.h>
71 #include <sys/bus.h>
72 #include <machine/bus.h>
73 #include <sys/rman.h>
74 #include <machine/resource.h>
75
76 #include <net/if.h>
77 #include <net/if_arp.h>
78 #include <net/ethernet.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
81
82 #include <bus/pci/pcidevs.h>
83 #include <bus/pci/pcireg.h>
84 #include <bus/pci/pcivar.h>
85
86 #include "if_aironet_ieee.h"
87 #include "if_anreg.h"
88
89 struct an_type {
90         u_int16_t               an_vid;
91         u_int16_t               an_did;
92         const char              *an_name;
93 };
94
95 #define AN_PCI_LOIO             0x18    /* Aironet iobase */
96
97 static const struct an_type an_devs[] = {
98         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_350,
99           "Cisco Aironet 350 Series" },
100         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_PC4500,
101           "Aironet PCI4500" },
102         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_PC4800,
103           "Aironet PCI4800" },
104         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_PC4xxx,
105           "Aironet PCI4500/PCI4800" },
106         { PCI_VENDOR_AIRONET, PCI_PRODUCT_AIRONET_MPI350,
107           "Cisco Aironet MPI350" },
108         { 0, 0, NULL }
109 };
110
111 static int an_probe_pci         (device_t);
112 static int an_attach_pci        (device_t);
113 static int an_suspend_pci       (device_t);
114 static int an_resume_pci        (device_t);
115
116 static int
117 an_probe_pci(device_t dev)
118 {
119         const struct an_type *t;
120         uint16_t vid, did;
121
122         vid = pci_get_vendor(dev);
123         did = pci_get_device(dev);
124         for (t = an_devs; t->an_name != NULL; ++t) {
125                 if (vid == t->an_vid && did == t->an_did) {
126                         device_set_desc(dev, t->an_name);
127                         return(0);
128                 }
129         }
130         return(ENXIO);
131 }
132
133 static int
134 an_attach_pci(dev)
135         device_t                dev;
136 {
137         struct an_softc         *sc;
138         int                     flags, error;
139
140         sc = device_get_softc(dev);
141         flags = device_get_flags(dev);
142
143         if (pci_get_vendor(dev) == PCI_VENDOR_AIRONET &&
144             pci_get_device(dev) == PCI_PRODUCT_AIRONET_MPI350) {
145                 sc->mpi350 = 1;
146                 sc->port_rid = PCIR_MAPS;
147         } else {
148                 sc->port_rid = AN_PCI_LOIO;
149         }
150         error = an_alloc_port(dev, sc->port_rid, 1);
151
152         if (error) {
153                 device_printf(dev, "couldn't map ports\n");
154                 goto fail;
155         }
156
157         sc->an_btag = rman_get_bustag(sc->port_res);
158         sc->an_bhandle = rman_get_bushandle(sc->port_res);
159
160         /* Allocate memory for MPI350 */
161         if (sc->mpi350) {
162                 /* Allocate memory */
163                 sc->mem_rid = PCIR_MAPS + 4;
164                 error = an_alloc_memory(dev, sc->mem_rid, 1);
165                 if (error) {
166                         device_printf(dev, "couldn't map memory\n");
167                         goto fail;
168                 }
169                 sc->an_mem_btag = rman_get_bustag(sc->mem_res);
170                 sc->an_mem_bhandle = rman_get_bushandle(sc->mem_res);
171
172                 /* Allocate aux. memory */
173                 sc->mem_aux_rid = PCIR_MAPS + 8;
174                 error = an_alloc_aux_memory(dev, sc->mem_aux_rid, 
175                     AN_AUXMEMSIZE);
176                 if (error) {
177                         device_printf(dev, "couldn't map aux memory\n");
178                         goto fail;
179                 }
180                 sc->an_mem_aux_btag = rman_get_bustag(sc->mem_aux_res);
181                 sc->an_mem_aux_bhandle = rman_get_bushandle(sc->mem_aux_res);
182
183                 /* Allocate DMA region */
184                 error = bus_dma_tag_create(NULL,        /* parent */
185                                1, 0,                    /* alignment, bounds */
186                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
187                                BUS_SPACE_MAXADDR,       /* highaddr */
188                                NULL, NULL,              /* filter, filterarg */
189                                0x3ffff,                 /* maxsize XXX */
190                                1,                       /* nsegments */
191                                0xffff,                  /* maxsegsize XXX */
192                                BUS_DMA_ALLOCNOW,        /* flags */
193                                &sc->an_dtag);
194                 if (error) {
195                         device_printf(dev, "couldn't get DMA region\n");
196                         goto fail;
197                 }
198         }
199
200         /* Allocate interrupt */
201         error = an_alloc_irq(dev, 0, RF_SHAREABLE);
202         if (error)
203                 goto fail;
204
205         error = an_attach(sc, dev, flags);
206         if (error)
207                 goto fail;
208
209         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
210                                an_intr, sc, &sc->irq_handle, NULL);
211         if (error) {
212                 ifmedia_removeall(&sc->an_ifmedia);
213                 ether_ifdetach(&sc->arpcom.ac_if);
214                 goto fail;
215         }
216
217         return(0);
218
219 fail:
220         an_release_resources(dev);
221         return(error);
222 }
223
224 static int
225 an_suspend_pci(device_t dev)
226 {
227         an_shutdown(dev);
228         
229         return (0);
230 }
231
232 static int
233 an_resume_pci(device_t dev)
234 {
235         an_resume(dev);
236
237         return (0);
238 }
239
240 static device_method_t an_pci_methods[] = {
241         /* Device interface */
242         DEVMETHOD(device_probe,         an_probe_pci),
243         DEVMETHOD(device_attach,        an_attach_pci),
244         DEVMETHOD(device_detach,        an_detach),
245         DEVMETHOD(device_shutdown,      an_shutdown),
246         DEVMETHOD(device_suspend,       an_suspend_pci),
247         DEVMETHOD(device_resume,        an_resume_pci),
248         { 0, 0 }
249 };
250
251 static driver_t an_pci_driver = {
252         "an",
253         an_pci_methods,
254         sizeof(struct an_softc),
255 };
256
257 static devclass_t an_devclass;
258
259 DRIVER_MODULE(if_an, pci, an_pci_driver, an_devclass, 0, 0);