Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.25
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.25 2000/02/02 07:23:28 augustss Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Marc Horowitz.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 #include "opt_inet.h"
     70 #include "opt_ns.h"
     71 #include "bpfilter.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/socket.h>
     77 #include <sys/ioctl.h>
     78 #include <sys/errno.h>
     79 #include <sys/syslog.h>
     80 #include <sys/select.h>
     81 #include <sys/device.h>
     82 
     83 #include <net/if.h>
     84 #include <net/if_dl.h>
     85 #include <net/if_ether.h>
     86 #include <net/if_media.h>
     87 
     88 #ifdef INET
     89 #include <netinet/in.h>
     90 #include <netinet/in_systm.h>
     91 #include <netinet/in_var.h>
     92 #include <netinet/ip.h>
     93 #include <netinet/if_inarp.h>
     94 #endif
     95 
     96 #ifdef NS
     97 #include <netns/ns.h>
     98 #include <netns/ns_if.h>
     99 #endif
    100 
    101 #if NBPFILTER > 0
    102 #include <net/bpf.h>
    103 #include <net/bpfdesc.h>
    104 #endif
    105 
    106 #include <machine/cpu.h>
    107 #include <machine/bus.h>
    108 #include <machine/intr.h>
    109 
    110 #include <dev/mii/miivar.h>
    111 
    112 #include <dev/ic/elink3var.h>
    113 #include <dev/ic/elink3reg.h>
    114 
    115 #include <dev/pcmcia/pcmciareg.h>
    116 #include <dev/pcmcia/pcmciavar.h>
    117 #include <dev/pcmcia/pcmciadevs.h>
    118 
    119 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
    120 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
    121 int	ep_pcmcia_detach __P((struct device *, int));
    122 int	ep_pcmcia_activate __P((struct device *, enum devact));
    123 
    124 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
    125 int	ep_pcmcia_enable __P((struct ep_softc *));
    126 void	ep_pcmcia_disable __P((struct ep_softc *));
    127 
    128 int	ep_pcmcia_enable1 __P((struct ep_softc *));
    129 void	ep_pcmcia_disable1 __P((struct ep_softc *));
    130 
    131 struct ep_pcmcia_softc {
    132 	struct ep_softc sc_ep;			/* real "ep" softc */
    133 
    134 	/* PCMCIA-specific goo */
    135 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    136 	int sc_io_window;			/* our i/o window */
    137 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    138 };
    139 
    140 struct cfattach ep_pcmcia_ca = {
    141 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach,
    142 	    ep_pcmcia_detach, ep_pcmcia_activate
    143 };
    144 
    145 struct ep_pcmcia_product {
    146 	u_int32_t	epp_product;	/* PCMCIA product ID */
    147 	u_short		epp_chipset;	/* 3Com chipset used */
    148 	int		epp_flags;	/* initial softc flags */
    149 	int		epp_expfunc;	/* expected function */
    150 	const char	*epp_name;	/* device name */
    151 } ep_pcmcia_products[] = {
    152 	{ PCMCIA_PRODUCT_3COM_3C562,		ELINK_CHIPSET_3C509,
    153 	  0,					0,
    154 	  PCMCIA_STR_3COM_3C562 },
    155 	{ PCMCIA_PRODUCT_3COM_3C589,		ELINK_CHIPSET_3C509,
    156 	  0,					0,
    157 	  PCMCIA_STR_3COM_3C589 },
    158 
    159 	{ PCMCIA_PRODUCT_3COM_3CXEM556,		ELINK_CHIPSET_3C509,
    160 	  0,					0,
    161 	  PCMCIA_STR_3COM_3CXEM556 },
    162 
    163 	{ PCMCIA_PRODUCT_3COM_3CXEM556INT,	ELINK_CHIPSET_3C509,
    164 	  0,					0,
    165 	  PCMCIA_STR_3COM_3CXEM556INT },
    166 
    167 	{ PCMCIA_PRODUCT_3COM_3C574,		ELINK_CHIPSET_ROADRUNNER,
    168 	  ELINK_FLAGS_MII,			0,
    169 	  PCMCIA_STR_3COM_3C574 },
    170 
    171 	{ PCMCIA_PRODUCT_3COM_3CCFEM556BI,	ELINK_CHIPSET_ROADRUNNER,
    172 	  ELINK_FLAGS_MII,			0,
    173 	  PCMCIA_STR_3COM_3CCFEM556BI },
    174 
    175 	{ 0,					0,
    176 	  0,					0,
    177 	  NULL },
    178 };
    179 
    180 struct ep_pcmcia_product *ep_pcmcia_lookup __P((struct pcmcia_attach_args *));
    181 
    182 struct ep_pcmcia_product *
    183 ep_pcmcia_lookup(pa)
    184 	struct pcmcia_attach_args *pa;
    185 {
    186 	struct ep_pcmcia_product *epp;
    187 
    188 	for (epp = ep_pcmcia_products; epp->epp_name != NULL; epp++)
    189 		if (pa->product == epp->epp_product &&
    190 		    pa->pf->number == epp->epp_expfunc)
    191 			return (epp);
    192 
    193 	return (NULL);
    194 }
    195 
    196 int
    197 ep_pcmcia_match(parent, match, aux)
    198 	struct device *parent;
    199 	struct cfdata *match;
    200 	void *aux;
    201 {
    202 	struct pcmcia_attach_args *pa = aux;
    203 
    204 	if (pa->manufacturer != PCMCIA_VENDOR_3COM)
    205 		return (0);
    206 
    207 	if (ep_pcmcia_lookup(pa) != NULL)
    208 		return (1);
    209 
    210 	return (0);
    211 }
    212 
    213 int
    214 ep_pcmcia_enable(sc)
    215 	struct ep_softc *sc;
    216 {
    217 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    218 	struct pcmcia_function *pf = psc->sc_pf;
    219 
    220 	/* establish the interrupt. */
    221 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
    222 	if (sc->sc_ih == NULL) {
    223 		printf("%s: couldn't establish interrupt\n",
    224 		    sc->sc_dev.dv_xname);
    225 		return (1);
    226 	}
    227 
    228 	return (ep_pcmcia_enable1(sc));
    229 }
    230 
    231 int
    232 ep_pcmcia_enable1(sc)
    233 	struct ep_softc *sc;
    234 {
    235 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    236 	struct pcmcia_function *pf = psc->sc_pf;
    237 	int ret;
    238 
    239 	if ((ret = pcmcia_function_enable(pf)))
    240 		return (ret);
    241 
    242 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    243 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    244 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    245 		int reg;
    246 
    247 		/* turn off the serial-disable bit */
    248 
    249 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    250 		if (reg & 0x08) {
    251 			reg &= ~0x08;
    252 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    253 		}
    254 
    255 	}
    256 
    257 	return (ret);
    258 }
    259 
    260 void
    261 ep_pcmcia_disable(sc)
    262 	struct ep_softc *sc;
    263 {
    264 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    265 
    266 	ep_pcmcia_disable1(sc);
    267 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    268 	sc->sc_ih = 0;
    269 }
    270 
    271 void
    272 ep_pcmcia_disable1(sc)
    273 	struct ep_softc *sc;
    274 {
    275 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    276 
    277 	pcmcia_function_disable(psc->sc_pf);
    278 }
    279 
    280 void
    281 ep_pcmcia_attach(parent, self, aux)
    282 	struct device  *parent, *self;
    283 	void           *aux;
    284 {
    285 	struct ep_pcmcia_softc *psc = (void *) self;
    286 	struct ep_softc *sc = &psc->sc_ep;
    287 	struct pcmcia_attach_args *pa = aux;
    288 	struct pcmcia_config_entry *cfe;
    289 	struct ep_pcmcia_product *epp;
    290 	u_int8_t myla[ETHER_ADDR_LEN];
    291 	u_int8_t *enaddr = NULL;
    292 	int i;
    293 
    294 	psc->sc_pf = pa->pf;
    295 	cfe = pa->pf->cfe_head.sqh_first;
    296 
    297 	/* Enable the card. */
    298 	pcmcia_function_init(pa->pf, cfe);
    299 	if (ep_pcmcia_enable1(sc))
    300 		printf(": function enable failed\n");
    301 
    302 	sc->enabled = 1;
    303 
    304 	if (cfe->num_memspace != 0)
    305 		printf(": unexpected number of memory spaces %d should be 0\n",
    306 		    cfe->num_memspace);
    307 
    308 	if (cfe->num_iospace != 1)
    309 		printf(": unexpected number of I/O spaces %d should be 1\n",
    310 		    cfe->num_iospace);
    311 
    312 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    313 		bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
    314 
    315 		for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
    316 			/*
    317 			 * the 3c562 can only use 0x??00-0x??7f
    318 			 * according to the Linux driver
    319 			 */
    320 			if (i & 0x80)
    321 				continue;
    322 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    323 			    0, &psc->sc_pcioh) == 0)
    324 				break;
    325 		}
    326 		if (i >= maxaddr) {
    327 			printf(": can't allocate i/o space\n");
    328 			return;
    329 		}
    330 	} else {
    331 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    332 		    cfe->iospace[0].length, &psc->sc_pcioh))
    333 			printf(": can't allocate i/o space\n");
    334 	}
    335 
    336 	sc->sc_iot = psc->sc_pcioh.iot;
    337 	sc->sc_ioh = psc->sc_pcioh.ioh;
    338 
    339 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    340 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    341 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    342 		printf(": can't map i/o space\n");
    343 		return;
    344 	}
    345 
    346 	switch (pa->product) {
    347 	case PCMCIA_PRODUCT_3COM_3C562:
    348 		/*
    349 		 * 3c562a-c use this; 3c562d does it in the regular way.
    350 		 * we might want to check the revision and produce a warning
    351 		 * in the future.
    352 		 */
    353 		/* FALLTHROUGH */
    354 	case PCMCIA_PRODUCT_3COM_3C574:
    355 	case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
    356 		/*
    357 		 * Apparently, some 3c574s do it this way, as well.
    358 		 */
    359 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
    360 			enaddr = myla;
    361 		break;
    362 	}
    363 
    364 	epp = ep_pcmcia_lookup(pa);
    365 	if (epp == NULL)
    366 		panic("ep_pcmcia_attach: impossible");
    367 
    368 	printf(": %s\n", epp->epp_name);
    369 
    370 	sc->bustype = ELINK_BUS_PCMCIA;
    371 	sc->ep_flags = epp->epp_flags;
    372 
    373 	sc->enable = ep_pcmcia_enable;
    374 	sc->disable = ep_pcmcia_disable;
    375 
    376 	epconfig(sc, epp->epp_chipset, enaddr);
    377 
    378 	sc->enabled = 0;
    379 
    380 	ep_pcmcia_disable1(sc);
    381 }
    382 
    383 int
    384 ep_pcmcia_activate(dev, act)
    385 	struct device *dev;
    386 	enum devact act;
    387 {
    388 	struct ep_pcmcia_softc *sc = (struct ep_pcmcia_softc *)dev;
    389 	int s;
    390 	int rv = 0;
    391 
    392 	s = splnet();
    393 	switch (act) {
    394 	case DVACT_ACTIVATE:
    395 		rv = EOPNOTSUPP;
    396 		break;
    397 
    398 	case DVACT_DEACTIVATE:
    399 		if (sc->sc_ep.sc_ih != NULL)
    400 			ep_pcmcia_disable((void *)sc);
    401 		break;
    402 	}
    403 	splx(s);
    404 	return (rv);
    405 }
    406 
    407 int
    408 ep_pcmcia_detach(self, flags)
    409 	struct device *self;
    410 	int flags;
    411 {
    412 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
    413 	struct ep_softc *sc = &psc->sc_ep;
    414 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    415 
    416 	/* Unmap our i/o window. */
    417 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    418 
    419 	/* Free our i/o space. */
    420 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    421 
    422 	bpfdetach(ifp);
    423 	ether_ifdetach(ifp);
    424 	if_detach(ifp);
    425 
    426 	return (0);
    427 }
    428 
    429 int
    430 ep_pcmcia_get_enaddr(tuple, arg)
    431 	struct pcmcia_tuple *tuple;
    432 	void *arg;
    433 {
    434 	u_int8_t *myla = arg;
    435 	int i;
    436 
    437 	/* this is 3c562a-c magic */
    438 	if (tuple->code == 0x88) {
    439 		if (tuple->length < ETHER_ADDR_LEN)
    440 			return (0);
    441 
    442 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    443 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    444 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    445 		}
    446 
    447 		return (1);
    448 	}
    449 	return (0);
    450 }
    451