xref: /haiku/src/add-ons/kernel/drivers/network/wlan/aironetwifi/dev/an/if_an_pci.c (revision 5a39afd3522fcf12c1c898ea391e9d86d4ab463c)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1997, 1998, 1999
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /*
39  * This is a PCI shim for the Aironet PC4500/4800 wireless network
40  * driver. Aironet makes PCMCIA, ISA and PCI versions of these devices,
41  * which all have basically the same interface. The ISA and PCI cards
42  * are actually bridge adapters with PCMCIA cards inserted into them,
43  * however they appear as normal PCI or ISA devices to the host.
44  *
45  * All we do here is handle the PCI probe and attach and set up an
46  * interrupt handler entry point. The PCI version of the card uses
47  * a PLX 9050 PCI to "dumb bus" bridge chip, which provides us with
48  * multiple PCI address space mappings. The primary mapping at PCI
49  * register 0x14 is for the PLX chip itself, *NOT* the Aironet card.
50  * The I/O address of the Aironet is actually at register 0x18, which
51  * is the local bus mapping register for bus space 0. There are also
52  * registers for additional register spaces at registers 0x1C and
53  * 0x20, but these are unused in the Aironet devices. To find out
54  * more, you need a datasheet for the 9050 from PLX, but you have
55  * to go through their sales office to get it. Bleh.
56  */
57 
58 #include "opt_inet.h"
59 
60 #ifdef INET
61 #define ANCACHE
62 #endif
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/sockio.h>
67 #include <sys/mbuf.h>
68 #include <sys/kernel.h>
69 #include <sys/socket.h>
70 
71 #include <sys/module.h>
72 #include <sys/bus.h>
73 #include <machine/bus.h>
74 #include <sys/rman.h>
75 #include <sys/lock.h>
76 #include <sys/mutex.h>
77 #include <machine/resource.h>
78 
79 #include <net/if.h>
80 #include <net/if_arp.h>
81 #include <net/ethernet.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 
85 #include <dev/pci/pcireg.h>
86 #include <dev/pci/pcivar.h>
87 
88 #include <dev/an/if_aironet_ieee.h>
89 #include <dev/an/if_anreg.h>
90 
91 struct an_type {
92 	u_int16_t		an_vid;
93 	u_int16_t		an_did;
94 	char			*an_name;
95 };
96 
97 #define AIRONET_VENDORID	0x14B9
98 #define AIRONET_DEVICEID_35x	0x0350
99 #define AIRONET_DEVICEID_4500	0x4500
100 #define AIRONET_DEVICEID_4800	0x4800
101 #define AIRONET_DEVICEID_4xxx	0x0001
102 #define AIRONET_DEVICEID_MPI350	0xA504
103 #define AN_PCI_PLX_LOIO		0x14	/* PLX chip iobase */
104 #define AN_PCI_LOIO		0x18	/* Aironet iobase */
105 
106 static struct an_type an_devs[] = {
107 	{ AIRONET_VENDORID, AIRONET_DEVICEID_35x, "Cisco Aironet 350 Series" },
108 	{ AIRONET_VENDORID, AIRONET_DEVICEID_MPI350, "Cisco Aironet MPI350" },
109 	{ AIRONET_VENDORID, AIRONET_DEVICEID_4500, "Aironet PCI4500" },
110 	{ AIRONET_VENDORID, AIRONET_DEVICEID_4800, "Aironet PCI4800" },
111 	{ AIRONET_VENDORID, AIRONET_DEVICEID_4xxx, "Aironet PCI4500/PCI4800" },
112 	{ 0, 0, NULL }
113 };
114 
115 static int an_probe_pci		(device_t);
116 static int an_attach_pci	(device_t);
117 static int an_suspend_pci	(device_t);
118 static int an_resume_pci	(device_t);
119 
120 static int
an_probe_pci(device_t dev)121 an_probe_pci(device_t dev)
122 {
123 	struct an_type		*t;
124 	uint16_t vid, did;
125 
126 	t = an_devs;
127 	vid = pci_get_vendor(dev);
128 	did = pci_get_device(dev);
129 
130 	while (t->an_name != NULL) {
131 		if (vid == t->an_vid &&
132 		    did == t->an_did) {
133 			device_set_desc(dev, t->an_name);
134 			return(BUS_PROBE_DEFAULT);
135 		}
136 		t++;
137 	}
138 
139 	return(ENXIO);
140 }
141 
142 static int
an_attach_pci(dev)143 an_attach_pci(dev)
144 	device_t		dev;
145 {
146 	struct an_softc		*sc;
147 	int 			flags, error = 0;
148 
149 	sc = device_get_softc(dev);
150 	bzero(sc, sizeof(struct an_softc));
151 	flags = device_get_flags(dev);
152 
153 	/*
154 	 * Setup the lock in PCI attachment since it skips the an_probe
155 	 * function.
156 	 */
157 	mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
158 	    MTX_DEF);
159 
160 	if (pci_get_vendor(dev) == AIRONET_VENDORID &&
161 	    pci_get_device(dev) == AIRONET_DEVICEID_MPI350) {
162 		sc->mpi350 = 1;
163 		sc->port_rid = PCIR_BAR(0);
164 	} else {
165 		sc->port_rid = AN_PCI_LOIO;
166 	}
167 	error = an_alloc_port(dev, sc->port_rid, 1);
168 
169 	if (error) {
170 		device_printf(dev, "couldn't map ports\n");
171 		goto fail;
172 	}
173 
174 	/* Allocate memory for MPI350 */
175 	if (sc->mpi350) {
176 		/* Allocate memory */
177 		sc->mem_rid = PCIR_BAR(1);
178 		error = an_alloc_memory(dev, sc->mem_rid, 1);
179 		if (error) {
180 			device_printf(dev, "couldn't map memory\n");
181 			goto fail;
182 		}
183 
184 		/* Allocate aux. memory */
185 		sc->mem_aux_rid = PCIR_BAR(2);
186 		error = an_alloc_aux_memory(dev, sc->mem_aux_rid,
187 		    AN_AUX_MEM_SIZE);
188 		if (error) {
189 			device_printf(dev, "couldn't map aux memory\n");
190 			goto fail;
191 		}
192 
193 		/* Allocate DMA region */
194 		error = bus_dma_tag_create(bus_get_dma_tag(dev),/* parent */
195 			       1, 0,			/* alignment, bounds */
196 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
197 			       BUS_SPACE_MAXADDR,	/* highaddr */
198 			       NULL, NULL,		/* filter, filterarg */
199 			       0x3ffff,			/* maxsize XXX */
200 			       1,			/* nsegments */
201 			       0xffff,			/* maxsegsize XXX */
202 			       BUS_DMA_ALLOCNOW,	/* flags */
203 			       NULL,			/* lockfunc */
204 			       NULL,			/* lockarg */
205 			       &sc->an_dtag);
206 		if (error) {
207 			device_printf(dev, "couldn't get DMA region\n");
208 			goto fail;
209 		}
210 	}
211 
212 	/* Allocate interrupt */
213 	error = an_alloc_irq(dev, 0, RF_SHAREABLE);
214 	if (error) {
215 		device_printf(dev, "couldn't get interrupt\n");
216 		goto fail;
217 	}
218 
219 	sc->an_dev = dev;
220 	error = an_attach(sc, flags);
221 	if (error) {
222 		device_printf(dev, "couldn't attach\n");
223 		goto fail;
224 	}
225 
226 	/*
227 	 * Must setup the interrupt after the an_attach to prevent racing.
228 	 */
229 	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
230 	    NULL, an_intr, sc, &sc->irq_handle);
231 	if (error)
232 		device_printf(dev, "couldn't setup interrupt\n");
233 
234 fail:
235 	if (error)
236 		an_release_resources(dev);
237 	return(error);
238 }
239 
240 static int
an_suspend_pci(device_t dev)241 an_suspend_pci(device_t dev)
242 {
243 	an_shutdown(dev);
244 
245 	return (0);
246 }
247 
248 static int
an_resume_pci(device_t dev)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 DRIVER_MODULE(an, pci, an_pci_driver, 0, 0);
274 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, an,
275     an_devs, nitems(an_devs) - 1);
276 MODULE_DEPEND(an, pci, 1, 1, 1);
277 MODULE_DEPEND(an, wlan, 1, 1, 1);
278