if_an_pci.c revision 166901
1119418Sobrien/*-
255992Swpaul * Copyright (c) 1997, 1998, 1999
355992Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
455992Swpaul *
555992Swpaul * Redistribution and use in source and binary forms, with or without
655992Swpaul * modification, are permitted provided that the following conditions
755992Swpaul * are met:
855992Swpaul * 1. Redistributions of source code must retain the above copyright
955992Swpaul *    notice, this list of conditions and the following disclaimer.
1055992Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1155992Swpaul *    notice, this list of conditions and the following disclaimer in the
1255992Swpaul *    documentation and/or other materials provided with the distribution.
1355992Swpaul * 3. All advertising materials mentioning features or use of this software
1455992Swpaul *    must display the following acknowledgement:
1555992Swpaul *	This product includes software developed by Bill Paul.
1655992Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1755992Swpaul *    may be used to endorse or promote products derived from this software
1855992Swpaul *    without specific prior written permission.
1955992Swpaul *
2055992Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2155992Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2255992Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2355992Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2455992Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2555992Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2655992Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2755992Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2855992Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2955992Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3055992Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3155992Swpaul */
3255992Swpaul
33119418Sobrien#include <sys/cdefs.h>
34119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/an/if_an_pci.c 166901 2007-02-23 12:19:07Z piso $");
35119418Sobrien
3655992Swpaul/*
3755992Swpaul * This is a PCI shim for the Aironet PC4500/4800 wireless network
3855992Swpaul * driver. Aironet makes PCMCIA, ISA and PCI versions of these devices,
3955992Swpaul * which all have basically the same interface. The ISA and PCI cards
4055992Swpaul * are actually bridge adapters with PCMCIA cards inserted into them,
4155992Swpaul * however they appear as normal PCI or ISA devices to the host.
4255992Swpaul *
4355992Swpaul * All we do here is handle the PCI probe and attach and set up an
4455992Swpaul * interrupt handler entry point. The PCI version of the card uses
4555992Swpaul * a PLX 9050 PCI to "dumb bus" bridge chip, which provides us with
4655992Swpaul * multiple PCI address space mappings. The primary mapping at PCI
4755992Swpaul * register 0x14 is for the PLX chip itself, *NOT* the Aironet card.
4855992Swpaul * The I/O address of the Aironet is actually at register 0x18, which
4955992Swpaul * is the local bus mapping register for bus space 0. There are also
5055992Swpaul * registers for additional register spaces at registers 0x1C and
5155992Swpaul * 0x20, but these are unused in the Aironet devices. To find out
5255992Swpaul * more, you need a datasheet for the 9050 from PLX, but you have
5355992Swpaul * to go through their sales office to get it. Bleh.
5455992Swpaul */
5555992Swpaul
5655992Swpaul#include "opt_inet.h"
5755992Swpaul
5855992Swpaul#ifdef INET
5955992Swpaul#define ANCACHE
6055992Swpaul#endif
6155992Swpaul
6255992Swpaul#include <sys/param.h>
6355992Swpaul#include <sys/systm.h>
6455992Swpaul#include <sys/sockio.h>
6555992Swpaul#include <sys/mbuf.h>
6655992Swpaul#include <sys/kernel.h>
6755992Swpaul#include <sys/socket.h>
6855992Swpaul
6955992Swpaul#include <sys/module.h>
7055992Swpaul#include <sys/bus.h>
7155992Swpaul#include <machine/bus.h>
7255992Swpaul#include <sys/rman.h>
7384811Sjhb#include <sys/lock.h>
7467365Sjhb#include <sys/mutex.h>
7555992Swpaul#include <machine/resource.h>
7655992Swpaul
7755992Swpaul#include <net/if.h>
7855992Swpaul#include <net/if_arp.h>
7955992Swpaul#include <net/ethernet.h>
8055992Swpaul#include <net/if_dl.h>
8177217Sphk#include <net/if_media.h>
8255992Swpaul
83119277Simp#include <dev/pci/pcireg.h>
84119277Simp#include <dev/pci/pcivar.h>
8555992Swpaul
8655992Swpaul#include <dev/an/if_aironet_ieee.h>
8755992Swpaul#include <dev/an/if_anreg.h>
8855992Swpaul
8955992Swpaulstruct an_type {
9055992Swpaul	u_int16_t		an_vid;
9155992Swpaul	u_int16_t		an_did;
9255992Swpaul	char			*an_name;
9355992Swpaul};
9455992Swpaul
9555992Swpaul#define AIRONET_VENDORID	0x14B9
9681221Sbrooks#define AIRONET_DEVICEID_35x	0x0350
9755992Swpaul#define AIRONET_DEVICEID_4500	0x4500
9855992Swpaul#define AIRONET_DEVICEID_4800	0x4800
9955992Swpaul#define AIRONET_DEVICEID_4xxx	0x0001
100108401Sambrisko#define AIRONET_DEVICEID_MPI350	0xA504
10155992Swpaul#define AN_PCI_PLX_LOIO		0x14	/* PLX chip iobase */
10255992Swpaul#define AN_PCI_LOIO		0x18	/* Aironet iobase */
10355992Swpaul
10455992Swpaulstatic struct an_type an_devs[] = {
10581221Sbrooks	{ AIRONET_VENDORID, AIRONET_DEVICEID_35x, "Cisco Aironet 350 Series" },
10655992Swpaul	{ AIRONET_VENDORID, AIRONET_DEVICEID_4500, "Aironet PCI4500" },
10755992Swpaul	{ AIRONET_VENDORID, AIRONET_DEVICEID_4800, "Aironet PCI4800" },
10855992Swpaul	{ AIRONET_VENDORID, AIRONET_DEVICEID_4xxx, "Aironet PCI4500/PCI4800" },
10955992Swpaul	{ 0, 0, NULL }
11055992Swpaul};
11155992Swpaul
11292739Salfredstatic int an_probe_pci		(device_t);
11392739Salfredstatic int an_attach_pci	(device_t);
114110362Sambriskostatic int an_suspend_pci	(device_t);
115110362Sambriskostatic int an_resume_pci	(device_t);
11655992Swpaul
11783270Sbrooksstatic int
11883270Sbrooksan_probe_pci(device_t dev)
11955992Swpaul{
12055992Swpaul	struct an_type		*t;
12155992Swpaul
12255992Swpaul	t = an_devs;
12355992Swpaul
12483270Sbrooks	while (t->an_name != NULL) {
12555992Swpaul		if (pci_get_vendor(dev) == t->an_vid &&
12656051Swpaul		    pci_get_device(dev) == t->an_did) {
12755992Swpaul			device_set_desc(dev, t->an_name);
128143163Simp			return(BUS_PROBE_DEFAULT);
12955992Swpaul		}
13055992Swpaul		t++;
13155992Swpaul	}
13255992Swpaul
133108401Sambrisko	if (pci_get_vendor(dev) == AIRONET_VENDORID &&
134108401Sambrisko	    pci_get_device(dev) == AIRONET_DEVICEID_MPI350) {
135108401Sambrisko		device_set_desc(dev, "Cisco Aironet MPI350");
136143163Simp		return(BUS_PROBE_DEFAULT);
137108401Sambrisko	}
138108401Sambrisko
13955992Swpaul	return(ENXIO);
14055992Swpaul}
14155992Swpaul
14283270Sbrooksstatic int
14383270Sbrooksan_attach_pci(dev)
14455992Swpaul	device_t		dev;
14555992Swpaul{
14655992Swpaul	u_int32_t		command;
14755992Swpaul	struct an_softc		*sc;
14855992Swpaul	int 			unit, flags, error = 0;
14955992Swpaul
15055992Swpaul	sc = device_get_softc(dev);
15155992Swpaul	unit = device_get_unit(dev);
15255992Swpaul	flags = device_get_flags(dev);
15355992Swpaul	bzero(sc, sizeof(struct an_softc));
15483270Sbrooks
155108401Sambrisko	if (pci_get_vendor(dev) == AIRONET_VENDORID &&
156108401Sambrisko	    pci_get_device(dev) == AIRONET_DEVICEID_MPI350) {
157108401Sambrisko		sc->mpi350 = 1;
158119690Sjhb		sc->port_rid = PCIR_BAR(0);
159108401Sambrisko	} else {
160108401Sambrisko		/*
161108401Sambrisko		 * Map control/status registers.
162108401Sambrisko	 	 */
163108401Sambrisko		command = pci_read_config(dev, PCIR_COMMAND, 4);
164108401Sambrisko		command |= PCIM_CMD_PORTEN;
165108401Sambrisko		pci_write_config(dev, PCIR_COMMAND, command, 4);
166108401Sambrisko		command = pci_read_config(dev, PCIR_COMMAND, 4);
16755992Swpaul
168108401Sambrisko		if (!(command & PCIM_CMD_PORTEN)) {
169108401Sambrisko			printf("an%d: failed to enable I/O ports!\n", unit);
170108401Sambrisko			error = ENXIO;
171108401Sambrisko			goto fail;
172108401Sambrisko		}
173108401Sambrisko		sc->port_rid = AN_PCI_LOIO;
17455992Swpaul	}
17555992Swpaul	error = an_alloc_port(dev, sc->port_rid, 1);
17655992Swpaul
17755992Swpaul	if (error) {
17855992Swpaul		printf("an%d: couldn't map ports\n", unit);
17955992Swpaul		goto fail;
18055992Swpaul	}
18155992Swpaul
18255992Swpaul	sc->an_btag = rman_get_bustag(sc->port_res);
18355992Swpaul	sc->an_bhandle = rman_get_bushandle(sc->port_res);
18455992Swpaul
185108401Sambrisko	/* Allocate memory for MPI350 */
186108401Sambrisko	if (sc->mpi350) {
187108401Sambrisko		/* Allocate memory */
188119690Sjhb		sc->mem_rid = PCIR_BAR(1);
189108401Sambrisko		error = an_alloc_memory(dev, sc->mem_rid, 1);
190108401Sambrisko		if (error) {
191108401Sambrisko			printf("an%d: couldn't map memory\n", unit);
192108401Sambrisko			goto fail;
193108401Sambrisko		}
194108401Sambrisko		sc->an_mem_btag = rman_get_bustag(sc->mem_res);
195108401Sambrisko		sc->an_mem_bhandle = rman_get_bushandle(sc->mem_res);
196108401Sambrisko
197108401Sambrisko		/* Allocate aux. memory */
198119690Sjhb		sc->mem_aux_rid = PCIR_BAR(2);
199108401Sambrisko		error = an_alloc_aux_memory(dev, sc->mem_aux_rid,
200119156Sambrisko		    AN_AUX_MEM_SIZE);
201108401Sambrisko		if (error) {
202108401Sambrisko			printf("an%d: couldn't map aux memory\n", unit);
203108401Sambrisko			goto fail;
204108401Sambrisko		}
205108401Sambrisko		sc->an_mem_aux_btag = rman_get_bustag(sc->mem_aux_res);
206108401Sambrisko		sc->an_mem_aux_bhandle = rman_get_bushandle(sc->mem_aux_res);
207108401Sambrisko
208108401Sambrisko		/* Allocate DMA region */
209108401Sambrisko		error = bus_dma_tag_create(NULL,	/* parent */
210108401Sambrisko			       1, 0,			/* alignment, bounds */
211108401Sambrisko			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
212108401Sambrisko			       BUS_SPACE_MAXADDR,	/* highaddr */
213108401Sambrisko			       NULL, NULL,		/* filter, filterarg */
214108401Sambrisko			       0x3ffff,			/* maxsize XXX */
215108401Sambrisko			       1,			/* nsegments */
216108401Sambrisko			       0xffff,			/* maxsegsize XXX */
217108401Sambrisko			       BUS_DMA_ALLOCNOW,	/* flags */
218117126Sscottl			       NULL,			/* lockfunc */
219117126Sscottl			       NULL,			/* lockarg */
220108401Sambrisko			       &sc->an_dtag);
221108401Sambrisko		if (error) {
222108401Sambrisko			printf("an%d: couldn't get DMA region\n", unit);
223108401Sambrisko			goto fail;
224108401Sambrisko		}
225108401Sambrisko	}
226108401Sambrisko
22755992Swpaul	/* Allocate interrupt */
22855992Swpaul	error = an_alloc_irq(dev, 0, RF_SHAREABLE);
22955992Swpaul	if (error) {
23055992Swpaul		goto fail;
23155992Swpaul        }
23283270Sbrooks
233113316Simp	sc->an_dev = dev;
234113316Simp	error = an_attach(sc, device_get_unit(dev), flags);
23555992Swpaul	if (error) {
23655992Swpaul		goto fail;
23755992Swpaul	}
23855992Swpaul
239113316Simp	/*
240113316Simp	 * Must setup the interrupt after the an_attach to prevent racing.
241113316Simp	 */
242113316Simp	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
243166901Spiso	    NULL, an_intr, sc, &sc->irq_handle);
24455992Swpaul
24555992Swpaulfail:
246108401Sambrisko	if (error)
247108401Sambrisko		an_release_resources(dev);
24855992Swpaul	return(error);
24955992Swpaul}
25055992Swpaul
25183270Sbrooksstatic int
252110362Sambriskoan_suspend_pci(device_t dev)
253110362Sambrisko{
254110362Sambrisko	an_shutdown(dev);
255110362Sambrisko
256110362Sambrisko	return (0);
257110362Sambrisko}
258110362Sambrisko
259110362Sambriskostatic int
260110362Sambriskoan_resume_pci(device_t dev)
261110362Sambrisko{
262110362Sambrisko	an_resume(dev);
263110362Sambrisko
264110362Sambrisko	return (0);
265110362Sambrisko}
266110362Sambrisko
26755992Swpaulstatic device_method_t an_pci_methods[] = {
26855992Swpaul        /* Device interface */
26955992Swpaul        DEVMETHOD(device_probe,         an_probe_pci),
27055992Swpaul        DEVMETHOD(device_attach,        an_attach_pci),
271123978Sambrisko	DEVMETHOD(device_detach,	an_detach),
27255992Swpaul	DEVMETHOD(device_shutdown,	an_shutdown),
273110362Sambrisko	DEVMETHOD(device_suspend,	an_suspend_pci),
274110362Sambrisko	DEVMETHOD(device_resume,	an_resume_pci),
27555992Swpaul        { 0, 0 }
27655992Swpaul};
27755992Swpaul
27855992Swpaulstatic driver_t an_pci_driver = {
27955992Swpaul        "an",
28055992Swpaul        an_pci_methods,
28155992Swpaul        sizeof(struct an_softc),
28255992Swpaul};
28355992Swpaul
28455992Swpaulstatic devclass_t an_devclass;
28555992Swpaul
286113506SmdoddDRIVER_MODULE(an, pci, an_pci_driver, an_devclass, 0, 0);
287113506SmdoddMODULE_DEPEND(an, pci, 1, 1, 1);
288113506SmdoddMODULE_DEPEND(an, wlan, 1, 1, 1);
289