Factor out an_detach, since the implementation for all busses is
[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.13 2005/07/27 21:56:32 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/pcireg.h>
83 #include <bus/pci/pcivar.h>
84
85 #include "if_aironet_ieee.h"
86 #include "if_anreg.h"
87
88 struct an_type {
89         u_int16_t               an_vid;
90         u_int16_t               an_did;
91         char                    *an_name;
92 };
93
94 #define AIRONET_VENDORID        0x14B9
95 #define AIRONET_DEVICEID_35x    0x0350
96 #define AIRONET_DEVICEID_4500   0x4500
97 #define AIRONET_DEVICEID_4800   0x4800
98 #define AIRONET_DEVICEID_4xxx   0x0001
99 #define AIRONET_DEVICEID_MPI350 0xA504
100 #define AN_PCI_PLX_LOIO         0x14    /* PLX chip iobase */
101 #define AN_PCI_LOIO             0x18    /* Aironet iobase */
102
103 static struct an_type an_devs[] = {
104         { AIRONET_VENDORID, AIRONET_DEVICEID_35x, "Cisco Aironet 350 Series" },
105         { AIRONET_VENDORID, AIRONET_DEVICEID_4500, "Aironet PCI4500" },
106         { AIRONET_VENDORID, AIRONET_DEVICEID_4800, "Aironet PCI4800" },
107         { AIRONET_VENDORID, AIRONET_DEVICEID_4xxx, "Aironet PCI4500/PCI4800" },
108         { AIRONET_VENDORID, AIRONET_DEVICEID_MPI350, "Cisco Aironet MPI350" },
109         { 0, 0, NULL }
110 };
111
112 static int an_probe_pci         (device_t);
113 static int an_attach_pci        (device_t);
114 static int an_suspend_pci       (device_t);
115 static int an_resume_pci        (device_t);
116
117 static int
118 an_probe_pci(device_t dev)
119 {
120         struct an_type          *t;
121
122         t = an_devs;
123
124         while (t->an_name != NULL) {
125                 if (pci_get_vendor(dev) == t->an_vid &&
126                     pci_get_device(dev) == t->an_did) {
127                         device_set_desc(dev, t->an_name);
128                         return(0);
129                 }
130                 t++;
131         }
132         return(ENXIO);
133 }
134
135 static int
136 an_attach_pci(dev)
137         device_t                dev;
138 {
139         u_int32_t               command;
140         struct an_softc         *sc;
141         int                     flags, error = 0;
142
143         sc = device_get_softc(dev);
144         flags = device_get_flags(dev);
145
146         if (pci_get_vendor(dev) == AIRONET_VENDORID &&
147             pci_get_device(dev) == AIRONET_DEVICEID_MPI350) {
148                 sc->mpi350 = 1;
149                 sc->port_rid = PCIR_MAPS;
150         } else {
151                 /*
152                  * Map control/status registers.
153                  */
154                 command = pci_read_config(dev, PCIR_COMMAND, 4);
155                 command |= PCIM_CMD_PORTEN;
156                 pci_write_config(dev, PCIR_COMMAND, command, 4);
157                 command = pci_read_config(dev, PCIR_COMMAND, 4);
158
159                 if (!(command & PCIM_CMD_PORTEN)) {
160                         device_printf(dev, "failed to enable I/O ports!\n");
161                         error = ENXIO;
162                         goto fail;
163                 }
164                 sc->port_rid = AN_PCI_LOIO;
165         }
166         error = an_alloc_port(dev, sc->port_rid, 1);
167
168         if (error) {
169                 device_printf(dev, "couldn't map ports\n");
170                 goto fail;
171         }
172
173         sc->an_btag = rman_get_bustag(sc->port_res);
174         sc->an_bhandle = rman_get_bushandle(sc->port_res);
175
176         /* Allocate memory for MPI350 */
177         if (sc->mpi350) {
178                 /* Allocate memory */
179                 sc->mem_rid = PCIR_MAPS + 4;
180                 error = an_alloc_memory(dev, sc->mem_rid, 1);
181                 if (error) {
182                         device_printf(dev, "couldn't map memory\n");
183                         goto fail;
184                 }
185                 sc->an_mem_btag = rman_get_bustag(sc->mem_res);
186                 sc->an_mem_bhandle = rman_get_bushandle(sc->mem_res);
187
188                 /* Allocate aux. memory */
189                 sc->mem_aux_rid = PCIR_MAPS + 8;
190                 error = an_alloc_aux_memory(dev, sc->mem_aux_rid, 
191                     AN_AUXMEMSIZE);
192                 if (error) {
193                         device_printf(dev, "couldn't map aux memory\n");
194                         goto fail;
195                 }
196                 sc->an_mem_aux_btag = rman_get_bustag(sc->mem_aux_res);
197                 sc->an_mem_aux_bhandle = rman_get_bushandle(sc->mem_aux_res);
198
199                 /* Allocate DMA region */
200                 error = bus_dma_tag_create(NULL,        /* parent */
201                                1, 0,                    /* alignment, bounds */
202                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
203                                BUS_SPACE_MAXADDR,       /* highaddr */
204                                NULL, NULL,              /* filter, filterarg */
205                                0x3ffff,                 /* maxsize XXX */
206                                1,                       /* nsegments */
207                                0xffff,                  /* maxsegsize XXX */
208                                BUS_DMA_ALLOCNOW,        /* flags */
209                                &sc->an_dtag);
210                 if (error) {
211                         device_printf(dev, "couldn't get DMA region\n");
212                         goto fail;
213                 }
214         }
215
216         /* Allocate interrupt */
217         error = an_alloc_irq(dev, 0, RF_SHAREABLE);
218         if (error)
219                 goto fail;
220
221         error = an_attach(sc, dev, flags);
222         if (error)
223                 goto fail;
224
225         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
226                                an_intr, sc, &sc->irq_handle, NULL);
227         if (error) {
228                 ifmedia_removeall(&sc->an_ifmedia);
229                 ether_ifdetach(&sc->arpcom.ac_if);
230                 goto fail;
231         }
232
233         return(0);
234
235 fail:
236         an_release_resources(dev);
237         return(error);
238 }
239
240 static int
241 an_suspend_pci(device_t dev)
242 {
243         an_shutdown(dev);
244         
245         return (0);
246 }
247
248 static int
249 an_resume_pci(device_t dev)
250 {
251         an_resume(dev);
252
253         return (0);
254 }
255
256 static device_method_t an_pci_methods[] = {
257         /* Device interface */
258         DEVMETHOD(device_probe,         an_probe_pci),
259         DEVMETHOD(device_attach,        an_attach_pci),
260         DEVMETHOD(device_detach,        an_detach),
261         DEVMETHOD(device_shutdown,      an_shutdown),
262         DEVMETHOD(device_suspend,       an_suspend_pci),
263         DEVMETHOD(device_resume,        an_resume_pci),
264         { 0, 0 }
265 };
266
267 static driver_t an_pci_driver = {
268         "an",
269         an_pci_methods,
270         sizeof(struct an_softc),
271 };
272
273 static devclass_t an_devclass;
274
275 DRIVER_MODULE(if_an, pci, an_pci_driver, an_devclass, 0, 0);