Home | History | Annotate | Line # | Download | only in pci
if_tlp_pci.c revision 1.36
      1 /*	$NetBSD: if_tlp_pci.c,v 1.36 2000/03/26 10:53:39 soren Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 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  * PCI bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
     42  * Ethernet controller family driver.
     43  */
     44 
     45 #include "opt_inet.h"
     46 #include "opt_ns.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/socket.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/errno.h>
     57 #include <sys/device.h>
     58 
     59 #include <machine/endian.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_dl.h>
     63 #include <net/if_media.h>
     64 #include <net/if_ether.h>
     65 
     66 #if NBPFILTER > 0
     67 #include <net/bpf.h>
     68 #endif
     69 
     70 #ifdef INET
     71 #include <netinet/in.h>
     72 #include <netinet/if_inarp.h>
     73 #endif
     74 
     75 #ifdef NS
     76 #include <netns/ns.h>
     77 #include <netns/ns_if.h>
     78 #endif
     79 
     80 #include <machine/bus.h>
     81 #include <machine/intr.h>
     82 
     83 #include <dev/mii/miivar.h>
     84 #include <dev/mii/mii_bitbang.h>
     85 
     86 #include <dev/ic/tulipreg.h>
     87 #include <dev/ic/tulipvar.h>
     88 
     89 #include <dev/pci/pcivar.h>
     90 #include <dev/pci/pcireg.h>
     91 #include <dev/pci/pcidevs.h>
     92 
     93 /*
     94  * PCI configuration space registers used by the Tulip.
     95  */
     96 #define	TULIP_PCI_IOBA		0x10	/* i/o mapped base */
     97 #define	TULIP_PCI_MMBA		0x14	/* memory mapped base */
     98 #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
     99 
    100 #define	CFDA_SLEEP		0x80000000	/* sleep mode */
    101 #define	CFDA_SNOOZE		0x40000000	/* snooze mode */
    102 
    103 struct tulip_pci_softc {
    104 	struct tulip_softc sc_tulip;	/* real Tulip softc */
    105 
    106 	/* PCI-specific goo. */
    107 	void	*sc_ih;			/* interrupt handle */
    108 
    109 	pci_chipset_tag_t sc_pc;	/* our PCI chipset */
    110 	pcitag_t sc_pcitag;		/* our PCI tag */
    111 
    112 	int	sc_flags;		/* flags; see below */
    113 
    114 	LIST_HEAD(, tulip_pci_softc) sc_intrslaves;
    115 	LIST_ENTRY(tulip_pci_softc) sc_intrq;
    116 
    117 	/* Our {ROM,interrupt} master. */
    118 	struct tulip_pci_softc *sc_master;
    119 };
    120 
    121 /* sc_flags */
    122 #define	TULIP_PCI_SHAREDINTR	0x01	/* interrupt is shared */
    123 #define	TULIP_PCI_SLAVEINTR	0x02	/* interrupt is slave */
    124 #define	TULIP_PCI_SHAREDROM	0x04	/* ROM is shared */
    125 #define	TULIP_PCI_SLAVEROM	0x08	/* slave of shared ROM */
    126 
    127 int	tlp_pci_match __P((struct device *, struct cfdata *, void *));
    128 void	tlp_pci_attach __P((struct device *, struct device *, void *));
    129 
    130 struct cfattach tlp_pci_ca = {
    131 	sizeof(struct tulip_pci_softc), tlp_pci_match, tlp_pci_attach,
    132 };
    133 
    134 const struct tulip_pci_product {
    135 	u_int32_t	tpp_vendor;	/* PCI vendor ID */
    136 	u_int32_t	tpp_product;	/* PCI product ID */
    137 	tulip_chip_t	tpp_chip;	/* base Tulip chip type */
    138 } tlp_pci_products[] = {
    139 #ifdef TLP_MATCH_21040
    140 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21040,
    141 	  TULIP_CHIP_21040 },
    142 #endif
    143 #ifdef TLP_MATCH_21041
    144 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21041,
    145 	  TULIP_CHIP_21041 },
    146 #endif
    147 #ifdef TLP_MATCH_21140
    148 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21140,
    149 	  TULIP_CHIP_21140 },
    150 #endif
    151 #ifdef TLP_MATCH_21142
    152 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21142,
    153 	  TULIP_CHIP_21142 },
    154 #endif
    155 
    156 	{ PCI_VENDOR_LITEON,		PCI_PRODUCT_LITEON_82C168,
    157 	  TULIP_CHIP_82C168 },
    158 
    159 	/*
    160 	 * Note: This is like a MX98725 with Wake-On-LAN and a
    161 	 * 128-bit multicast hash table.
    162 	 */
    163 	{ PCI_VENDOR_LITEON,		PCI_PRODUCT_LITEON_82C115,
    164 	  TULIP_CHIP_82C115 },
    165 
    166 	{ PCI_VENDOR_MACRONIX,		PCI_PRODUCT_MACRONIX_MX98713,
    167 	  TULIP_CHIP_MX98713 },
    168 	{ PCI_VENDOR_MACRONIX,		PCI_PRODUCT_MACRONIX_MX987x5,
    169 	  TULIP_CHIP_MX98715 },
    170 
    171 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_RL100TX,
    172 	  TULIP_CHIP_MX98713 },
    173 
    174 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C840F,
    175 	  TULIP_CHIP_WB89C840F },
    176 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_RL100ATX,
    177 	  TULIP_CHIP_WB89C840F },
    178 
    179 #if 0
    180 	{ PCI_VENDOR_DAVICOM,		PCI_PRODUCT_DAVICOM_DM9102,
    181 	  TULIP_CHIP_DM9102 },
    182 #endif
    183 
    184 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_AL981,
    185 	  TULIP_CHIP_AL981 },
    186 
    187 #if 0
    188 	{ PCI_VENDOR_ASIX,		PCI_PRODUCT_ASIX_AX88140A,
    189 	  TULIP_CHIP_AX88140 },
    190 #endif
    191 
    192 	{ 0,				0,
    193 	  TULIP_CHIP_INVALID },
    194 };
    195 
    196 struct tlp_pci_quirks {
    197 	void		(*tpq_func) __P((struct tulip_pci_softc *,
    198 			    const u_int8_t *));
    199 	u_int8_t	tpq_oui[3];
    200 };
    201 
    202 void	tlp_pci_dec_quirks __P((struct tulip_pci_softc *,
    203 	    const u_int8_t *));
    204 
    205 void	tlp_pci_znyx_21040_quirks __P((struct tulip_pci_softc *,
    206 	    const u_int8_t *));
    207 void	tlp_pci_smc_21040_quirks __P((struct tulip_pci_softc *,
    208 	    const u_int8_t *));
    209 void	tlp_pci_cogent_21040_quirks __P((struct tulip_pci_softc *,
    210 	    const u_int8_t *));
    211 void	tlp_pci_accton_21040_quirks __P((struct tulip_pci_softc *,
    212 	    const u_int8_t *));
    213 
    214 void	tlp_pci_cobalt_21142_quirks __P((struct tulip_pci_softc *,
    215 	    const u_int8_t *));
    216 
    217 const struct tlp_pci_quirks tlp_pci_21040_quirks[] = {
    218 	{ tlp_pci_znyx_21040_quirks,	{ 0x00, 0xc0, 0x95 } },
    219 	{ tlp_pci_smc_21040_quirks,	{ 0x00, 0x00, 0xc0 } },
    220 	{ tlp_pci_cogent_21040_quirks,	{ 0x00, 0x00, 0x92 } },
    221 	{ tlp_pci_accton_21040_quirks,	{ 0x00, 0x00, 0xe8 } },
    222 	{ NULL,				{ 0, 0, 0 } }
    223 };
    224 
    225 const struct tlp_pci_quirks tlp_pci_21041_quirks[] = {
    226 	{ tlp_pci_dec_quirks,		{ 0x08, 0x00, 0x2b } },
    227 	{ tlp_pci_dec_quirks,		{ 0x00, 0x00, 0xf8 } },
    228 	{ NULL,				{ 0, 0, 0 } }
    229 };
    230 
    231 void	tlp_pci_asante_21140_quirks __P((struct tulip_pci_softc *,
    232 	    const u_int8_t *));
    233 
    234 const struct tlp_pci_quirks tlp_pci_21140_quirks[] = {
    235 	{ tlp_pci_dec_quirks,		{ 0x08, 0x00, 0x2b } },
    236 	{ tlp_pci_dec_quirks,		{ 0x00, 0x00, 0xf8 } },
    237 	{ tlp_pci_asante_21140_quirks,	{ 0x00, 0x00, 0x94 } },
    238 	{ NULL,				{ 0, 0, 0 } }
    239 };
    240 
    241 const struct tlp_pci_quirks tlp_pci_21142_quirks[] = {
    242 	{ tlp_pci_dec_quirks,		{ 0x08, 0x00, 0x2b } },
    243 	{ tlp_pci_dec_quirks,		{ 0x00, 0x00, 0xf8 } },
    244 	{ tlp_pci_cobalt_21142_quirks,	{ 0x00, 0x10, 0xe0 } },
    245 	{ NULL,				{ 0, 0, 0 } }
    246 };
    247 
    248 int	tlp_pci_shared_intr __P((void *));
    249 
    250 const struct tulip_pci_product *tlp_pci_lookup
    251     __P((const struct pci_attach_args *));
    252 void tlp_pci_get_quirks __P((struct tulip_pci_softc *, const u_int8_t *,
    253     const struct tlp_pci_quirks *));
    254 void tlp_pci_check_slaved __P((struct tulip_pci_softc *, int, int));
    255 
    256 const struct tulip_pci_product *
    257 tlp_pci_lookup(pa)
    258 	const struct pci_attach_args *pa;
    259 {
    260 	const struct tulip_pci_product *tpp;
    261 
    262 	for (tpp = tlp_pci_products;
    263 	     tlp_chip_names[tpp->tpp_chip] != NULL;
    264 	     tpp++) {
    265 		if (PCI_VENDOR(pa->pa_id) == tpp->tpp_vendor &&
    266 		    PCI_PRODUCT(pa->pa_id) == tpp->tpp_product)
    267 			return (tpp);
    268 	}
    269 	return (NULL);
    270 }
    271 
    272 void
    273 tlp_pci_get_quirks(psc, enaddr, tpq)
    274 	struct tulip_pci_softc *psc;
    275 	const u_int8_t *enaddr;
    276 	const struct tlp_pci_quirks *tpq;
    277 {
    278 
    279 	for (; tpq->tpq_func != NULL; tpq++) {
    280 		if (tpq->tpq_oui[0] == enaddr[0] &&
    281 		    tpq->tpq_oui[1] == enaddr[1] &&
    282 		    tpq->tpq_oui[2] == enaddr[2]) {
    283 			(*tpq->tpq_func)(psc, enaddr);
    284 			return;
    285 		}
    286 	}
    287 }
    288 
    289 void
    290 tlp_pci_check_slaved(psc, shared, slaved)
    291 	struct tulip_pci_softc *psc;
    292 	int shared, slaved;
    293 {
    294 	extern struct cfdriver tlp_cd;
    295 	struct tulip_pci_softc *cur, *best = NULL;
    296 	struct tulip_softc *sc = &psc->sc_tulip;
    297 	int i;
    298 
    299 	/*
    300 	 * First of all, find the lowest pcidev numbered device on our
    301 	 * bus marked as shared.  That should be our master.
    302 	 */
    303 	for (i = 0; i < tlp_cd.cd_ndevs; i++) {
    304 		if ((cur = tlp_cd.cd_devs[i]) == NULL)
    305 			continue;
    306 		if (cur->sc_tulip.sc_dev.dv_parent != sc->sc_dev.dv_parent)
    307 			continue;
    308 		if ((cur->sc_flags & shared) == 0)
    309 			continue;
    310 		if (cur == psc)
    311 			continue;
    312 		if (best == NULL ||
    313 		    best->sc_tulip.sc_devno > cur->sc_tulip.sc_devno)
    314 			best = cur;
    315 	}
    316 
    317 	if (best != NULL) {
    318 		psc->sc_master = best;
    319 		psc->sc_flags |= (shared | slaved);
    320 	}
    321 }
    322 
    323 int
    324 tlp_pci_match(parent, match, aux)
    325 	struct device *parent;
    326 	struct cfdata *match;
    327 	void *aux;
    328 {
    329 	struct pci_attach_args *pa = aux;
    330 
    331 	if (tlp_pci_lookup(pa) != NULL)
    332 		return (10);	/* beat if_de.c */
    333 
    334 	return (0);
    335 }
    336 
    337 void
    338 tlp_pci_attach(parent, self, aux)
    339 	struct device *parent, *self;
    340 	void *aux;
    341 {
    342 	struct tulip_pci_softc *psc = (void *) self;
    343 	struct tulip_softc *sc = &psc->sc_tulip;
    344 	struct pci_attach_args *pa = aux;
    345 	pci_chipset_tag_t pc = pa->pa_pc;
    346 	pci_intr_handle_t ih;
    347 	const char *intrstr = NULL;
    348 	bus_space_tag_t iot, memt;
    349 	bus_space_handle_t ioh, memh;
    350 	int ioh_valid, memh_valid, i, j;
    351 	const struct tulip_pci_product *tpp;
    352 	u_int8_t enaddr[ETHER_ADDR_LEN];
    353 	u_int32_t val;
    354 	pcireg_t reg;
    355 	int pmreg;
    356 
    357 	sc->sc_devno = pa->pa_device;
    358 	psc->sc_pc = pa->pa_pc;
    359 	psc->sc_pcitag = pa->pa_tag;
    360 
    361 	LIST_INIT(&psc->sc_intrslaves);
    362 
    363 	tpp = tlp_pci_lookup(pa);
    364 	if (tpp == NULL) {
    365 		printf("\n");
    366 		panic("tlp_pci_attach: impossible");
    367 	}
    368 	sc->sc_chip = tpp->tpp_chip;
    369 
    370 	/*
    371 	 * By default, Tulip registers are 8 bytes long (4 bytes
    372 	 * followed by a 4 byte pad).
    373 	 */
    374 	sc->sc_regshift = 3;
    375 
    376 	/*
    377 	 * No power management hooks.
    378 	 * XXX Maybe we should add some!
    379 	 */
    380 	sc->sc_flags |= TULIPF_ENABLED;
    381 
    382 	/*
    383 	 * Get revision info, and set some chip-specific variables.
    384 	 */
    385 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    386 	switch (sc->sc_chip) {
    387 	case TULIP_CHIP_21140:
    388 		if (sc->sc_rev >= 0x20)
    389 			sc->sc_chip = TULIP_CHIP_21140A;
    390 		break;
    391 
    392 	case TULIP_CHIP_21142:
    393 		if (sc->sc_rev >= 0x20)
    394 			sc->sc_chip = TULIP_CHIP_21143;
    395 		break;
    396 
    397 	case TULIP_CHIP_82C168:
    398 		if (sc->sc_rev >= 0x20)
    399 			sc->sc_chip = TULIP_CHIP_82C169;
    400 		break;
    401 
    402 	case TULIP_CHIP_MX98713:
    403 		if (sc->sc_rev >= 0x10)
    404 			sc->sc_chip = TULIP_CHIP_MX98713A;
    405 		break;
    406 
    407 	case TULIP_CHIP_MX98715:
    408 		if (sc->sc_rev >= 0x20)
    409 			sc->sc_chip = TULIP_CHIP_MX98715A;
    410 		if (sc->sc_rev >= 0x30)
    411 			sc->sc_chip = TULIP_CHIP_MX98725;
    412 		break;
    413 
    414 	case TULIP_CHIP_WB89C840F:
    415 		sc->sc_regshift = 2;
    416 		break;
    417 
    418 	case TULIP_CHIP_AX88140:
    419 		if (sc->sc_rev >= 0x10)
    420 			sc->sc_chip = TULIP_CHIP_AX88141;
    421 		break;
    422 
    423 	default:
    424 		/* Nothing. */
    425 	}
    426 
    427 	printf(": %s Ethernet, pass %d.%d\n",
    428 	    tlp_chip_names[sc->sc_chip],
    429 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    430 
    431 	switch (sc->sc_chip) {
    432 	case TULIP_CHIP_21040:
    433 		if (sc->sc_rev < 0x20) {
    434 			printf("%s: 21040 must be at least pass 2.0\n",
    435 			    sc->sc_dev.dv_xname);
    436 			return;
    437 		}
    438 		break;
    439 
    440 	case TULIP_CHIP_21140:
    441 		if (sc->sc_rev < 0x11) {
    442 			printf("%s: 21140 must be at least pass 1.1\n",
    443 			    sc->sc_dev.dv_xname);
    444 			return;
    445 		}
    446 		break;
    447 
    448 	default:
    449 		/* Nothing. */
    450 	}
    451 
    452 	/*
    453 	 * Check to see if the device is in power-save mode, and
    454 	 * being it out if necessary.
    455 	 */
    456 	switch (sc->sc_chip) {
    457 	case TULIP_CHIP_21140:
    458 	case TULIP_CHIP_21140A:
    459 	case TULIP_CHIP_21142:
    460 	case TULIP_CHIP_21143:
    461 	case TULIP_CHIP_MX98713A:
    462 	case TULIP_CHIP_MX98715:
    463 	case TULIP_CHIP_MX98715A:
    464 	case TULIP_CHIP_MX98725:
    465 		/*
    466 		 * Clear the "sleep mode" bit in the CFDA register.
    467 		 */
    468 		reg = pci_conf_read(pc, pa->pa_tag, TULIP_PCI_CFDA);
    469 		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
    470 			pci_conf_write(pc, pa->pa_tag, TULIP_PCI_CFDA,
    471 			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
    472 		break;
    473 
    474 	default:
    475 		/* Nothing. */
    476 	}
    477 
    478 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    479 		reg = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
    480 		if (reg == 3) {
    481 			/*
    482 			 * The card has lost all configuration data in
    483 			 * this state, so punt.
    484 			 */
    485 			printf("%s: unable to wake up from power state D3\n",
    486 			    sc->sc_dev.dv_xname);
    487 			return;
    488 		}
    489 		if (reg != 0) {
    490 			printf("%s: waking up from power state D%d\n",
    491 			    sc->sc_dev.dv_xname, reg);
    492 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
    493 		}
    494 	}
    495 
    496 	/*
    497 	 * Map the device.
    498 	 */
    499 	ioh_valid = (pci_mapreg_map(pa, TULIP_PCI_IOBA,
    500 	    PCI_MAPREG_TYPE_IO, 0,
    501 	    &iot, &ioh, NULL, NULL) == 0);
    502 	memh_valid = (pci_mapreg_map(pa, TULIP_PCI_MMBA,
    503 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    504 	    &memt, &memh, NULL, NULL) == 0);
    505 
    506 	if (memh_valid) {
    507 		sc->sc_st = memt;
    508 		sc->sc_sh = memh;
    509 	} else if (ioh_valid) {
    510 		sc->sc_st = iot;
    511 		sc->sc_sh = ioh;
    512 	} else {
    513 		printf(": unable to map device registers\n");
    514 		return;
    515 	}
    516 
    517 	sc->sc_dmat = pa->pa_dmat;
    518 
    519 	/*
    520 	 * Make sure bus mastering is enabled.
    521 	 */
    522 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    523 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    524 	    PCI_COMMAND_MASTER_ENABLE);
    525 
    526 	/*
    527 	 * Get the cacheline size.
    528 	 */
    529 	sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
    530 	    PCI_BHLC_REG));
    531 
    532 	/*
    533 	 * Get PCI data moving command info.
    534 	 */
    535 	if (pa->pa_flags & PCI_FLAGS_MRL_OKAY)
    536 		sc->sc_flags |= TULIPF_MRL;
    537 	if (pa->pa_flags & PCI_FLAGS_MRM_OKAY)
    538 		sc->sc_flags |= TULIPF_MRM;
    539 	if (pa->pa_flags & PCI_FLAGS_MWI_OKAY)
    540 		sc->sc_flags |= TULIPF_MWI;
    541 
    542 	/*
    543 	 * Read the contents of the Ethernet Address ROM/SROM.
    544 	 */
    545 	switch (sc->sc_chip) {
    546 	case TULIP_CHIP_21040:
    547 		sc->sc_srom_addrbits = 6;
    548 		sc->sc_srom = malloc(TULIP_ROM_SIZE(6), M_DEVBUF, M_NOWAIT);
    549 		TULIP_WRITE(sc, CSR_MIIROM, MIIROM_SROMCS);
    550 		for (i = 0; i < TULIP_ROM_SIZE(6); i++) {
    551 			for (j = 0; j < 10000; j++) {
    552 				val = TULIP_READ(sc, CSR_MIIROM);
    553 				if ((val & MIIROM_DN) == 0)
    554 					break;
    555 			}
    556 			sc->sc_srom[i] = val & MIIROM_DATA;
    557 		}
    558 		break;
    559 
    560 	case TULIP_CHIP_82C168:
    561 	case TULIP_CHIP_82C169:
    562 	    {
    563 		sc->sc_srom_addrbits = 2;
    564 		sc->sc_srom = malloc(TULIP_ROM_SIZE(2), M_DEVBUF, M_NOWAIT);
    565 
    566 		/*
    567 		 * The Lite-On PNIC stores the Ethernet address in
    568 		 * the first 3 words of the EEPROM.  EEPROM access
    569 		 * is not like the other Tulip chips.
    570 		 */
    571 		for (i = 0; i < 6; i += 2) {
    572 			TULIP_WRITE(sc, CSR_PNIC_SROMCTL,
    573 			    PNIC_SROMCTL_READ | (i >> 1));
    574 			for (j = 0; j < 500; j++) {
    575 				delay(2);
    576 				val = TULIP_READ(sc, CSR_MIIROM);
    577 				if ((val & PNIC_MIIROM_BUSY) == 0)
    578 					break;
    579 			}
    580 			if (val & PNIC_MIIROM_BUSY) {
    581 				printf("%s: EEPROM timed out\n",
    582 				    sc->sc_dev.dv_xname);
    583 				return;
    584 			}
    585 			val &= PNIC_MIIROM_DATA;
    586 			sc->sc_srom[i] = val >> 8;
    587 			sc->sc_srom[i + 1] = val & 0xff;
    588 		}
    589 		break;
    590 	    }
    591 
    592 	default:
    593 		if (tlp_read_srom(sc) == 0)
    594 			goto cant_cope;
    595 		break;
    596 	}
    597 
    598 	/*
    599 	 * Deal with chip/board quirks.  This includes setting up
    600 	 * the mediasw, and extracting the Ethernet address from
    601 	 * the rombuf.
    602 	 */
    603 	switch (sc->sc_chip) {
    604 	case TULIP_CHIP_21040:
    605 		/* Check for a slaved ROM on a multi-port board. */
    606 		tlp_pci_check_slaved(psc, TULIP_PCI_SHAREDROM,
    607 		    TULIP_PCI_SLAVEROM);
    608 		if (psc->sc_flags & TULIP_PCI_SLAVEROM)
    609 			memcpy(sc->sc_srom, psc->sc_master->sc_tulip.sc_srom,
    610 			    sizeof(sc->sc_srom));
    611 
    612 		/*
    613 		 * Parse the Ethernet Address ROM.
    614 		 */
    615 		if (tlp_parse_old_srom(sc, enaddr) == 0)
    616 			goto cant_cope;
    617 
    618 		/*
    619 		 * If we have a slaved ROM, adjust the Ethernet address.
    620 		 */
    621 		if (psc->sc_flags & TULIP_PCI_SLAVEROM)
    622 			enaddr[5] +=
    623 			    sc->sc_devno - psc->sc_master->sc_tulip.sc_devno;
    624 
    625 		/*
    626 		 * All 21040 boards start out with the same
    627 		 * media switch.
    628 		 */
    629 		sc->sc_mediasw = &tlp_21040_mediasw;
    630 
    631 		/*
    632 		 * Deal with any quirks this board might have.
    633 		 */
    634 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21040_quirks);
    635 		break;
    636 
    637 	case TULIP_CHIP_21041:
    638 		/* Check for a slaved ROM on a multi-port board. */
    639 		tlp_pci_check_slaved(psc, TULIP_PCI_SHAREDROM,
    640 		    TULIP_PCI_SLAVEROM);
    641 		if (psc->sc_flags & TULIP_PCI_SLAVEROM)
    642 			memcpy(sc->sc_srom, psc->sc_master->sc_tulip.sc_srom,
    643 			    sizeof(sc->sc_srom));
    644 
    645 		/* Check for new format SROM. */
    646 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    647 			/*
    648 			 * Not an ISV SROM; try the old DEC Ethernet Address
    649 			 * ROM format.
    650 			 */
    651 			if (tlp_parse_old_srom(sc, enaddr) == 0)
    652 				goto cant_cope;
    653 		}
    654 
    655 		/*
    656 		 * All 21041 boards use the same media switch; they all
    657 		 * work basically the same!  Yippee!
    658 		 */
    659 		sc->sc_mediasw = &tlp_21041_mediasw;
    660 
    661 		/*
    662 		 * Deal with any quirks this board might have.
    663 		 */
    664 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21041_quirks);
    665 		break;
    666 
    667 	case TULIP_CHIP_21140:
    668 	case TULIP_CHIP_21140A:
    669 		/* Check for new format SROM. */
    670 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    671 			/*
    672 			 * Not an ISV SROM; try the old DEC Ethernet Address
    673 			 * ROM format.
    674 			 */
    675 			if (tlp_parse_old_srom(sc, enaddr) == 0)
    676 				goto cant_cope;
    677 		} else {
    678 			/*
    679 			 * We start out with the 2114x ISV media switch.
    680 			 * When we search for quirks, we may change to
    681 			 * a different switch.
    682 			 */
    683 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    684 		}
    685 
    686 		/*
    687 		 * Deal with any quirks this board might have.
    688 		 */
    689 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21140_quirks);
    690 
    691 		/*
    692 		 * Bail out now if we can't deal with this board.
    693 		 */
    694 		if (sc->sc_mediasw == NULL)
    695 			goto cant_cope;
    696 		break;
    697 
    698 	case TULIP_CHIP_21142:
    699 	case TULIP_CHIP_21143:
    700 		/* Check for new format SROM. */
    701 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    702 			/*
    703 			 * Not an ISV SROM; try the old DEC Ethernet Address
    704 			 * ROM format.
    705 			 */
    706 			if (tlp_parse_old_srom(sc, enaddr) == 0)
    707 				goto cant_cope;
    708 		} else {
    709 			/*
    710 			 * We start out with the 2114x ISV media switch.
    711 			 * When we search for quirks, we may change to
    712 			 * a different switch.
    713 			 */
    714 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    715 		}
    716 
    717 		/*
    718 		 * Deal with any quirks this board might have.
    719 		 */
    720 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21142_quirks);
    721 
    722 		/*
    723 		 * Bail out now if we can't deal with this board.
    724 		 */
    725 		if (sc->sc_mediasw == NULL)
    726 			goto cant_cope;
    727 		break;
    728 
    729 	case TULIP_CHIP_82C168:
    730 	case TULIP_CHIP_82C169:
    731 		/*
    732 		 * Lite-On PNIC's Ethernet address is the first 6
    733 		 * bytes of its EEPROM.
    734 		 */
    735 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
    736 
    737 		/*
    738 		 * Lite-On PNICs always use the same mediasw; we
    739 		 * select MII vs. internal NWAY automatically.
    740 		 */
    741 		sc->sc_mediasw = &tlp_pnic_mediasw;
    742 		break;
    743 
    744 	case TULIP_CHIP_MX98713:
    745 		/*
    746 		 * The Macronix MX98713 has an MII and GPIO, but no
    747 		 * internal Nway block.  This chip is basically a
    748 		 * perfect 21140A clone, with the exception of the
    749 		 * a magic register frobbing in order to make the
    750 		 * interface function.
    751 		 */
    752 		if (tlp_isv_srom_enaddr(sc, enaddr)) {
    753 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    754 			break;
    755 		}
    756 		/* FALLTHROUGH */
    757 
    758 	case TULIP_CHIP_82C115:
    759 		/*
    760 		 * Yippee!  The Lite-On 82C115 is a clone of
    761 		 * the MX98725 (the data sheet even says `MXIC'
    762 		 * on it)!  Imagine that, a clone of a clone.
    763 		 *
    764 		 * The differences are really minimal:
    765 		 *
    766 		 *	- Wake-On-LAN support
    767 		 *	- 128-bit multicast hash table, rather than
    768 		 *	  the standard 512-bit hash table
    769 		 */
    770 		/* FALLTHROUGH */
    771 
    772 	case TULIP_CHIP_MX98713A:
    773 	case TULIP_CHIP_MX98715A:
    774 	case TULIP_CHIP_MX98725:
    775 		/*
    776 		 * The MX98713A has an MII as well as an internal Nway block,
    777 		 * but no GPIO.  The MX98715 and MX98725 have an internal
    778 		 * Nway block only.
    779 		 *
    780 		 * The internal Nway block, unlike the Lite-On PNIC's, does
    781 		 * just that - performs Nway.  Once autonegotiation completes,
    782 		 * we must program the GPR media information into the chip.
    783 		 *
    784 		 * The byte offset of the Ethernet address is stored at
    785 		 * offset 0x70.
    786 		 */
    787 		memcpy(enaddr, &sc->sc_srom[sc->sc_srom[0x70]], ETHER_ADDR_LEN);
    788 		sc->sc_mediasw = &tlp_pmac_mediasw;
    789 		break;
    790 
    791 	case TULIP_CHIP_WB89C840F:
    792 		/*
    793 		 * Winbond 89C840F's Ethernet address is the first
    794 		 * 6 bytes of its EEPROM.
    795 		 */
    796 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
    797 
    798 		/*
    799 		 * Winbond 89C840F has an MII attached to the SIO.
    800 		 */
    801 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
    802 		break;
    803 
    804 	case TULIP_CHIP_AL981:
    805 		/*
    806 		 * The ADMtek AL981's Ethernet address is located
    807 		 * at offset 8 of its EEPROM.
    808 		 */
    809 		memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
    810 
    811 		/*
    812 		 * ADMtek AL981 has a built-in PHY accessed through
    813 		 * special registers.
    814 		 */
    815 		sc->sc_mediasw = &tlp_al981_mediasw;
    816 		break;
    817 
    818 	default:
    819  cant_cope:
    820 		printf("%s: sorry, unable to handle your board\n",
    821 		    sc->sc_dev.dv_xname);
    822 		return;
    823 	}
    824 
    825 	/*
    826 	 * Handle shared interrupts.
    827 	 */
    828 	if (psc->sc_flags & TULIP_PCI_SHAREDINTR) {
    829 		if (psc->sc_master)
    830 			psc->sc_flags |= TULIP_PCI_SLAVEINTR;
    831 		else {
    832 			tlp_pci_check_slaved(psc, TULIP_PCI_SHAREDINTR,
    833 			    TULIP_PCI_SLAVEINTR);
    834 			if (psc->sc_master == NULL)
    835 				psc->sc_master = psc;
    836 		}
    837 		LIST_INSERT_HEAD(&psc->sc_master->sc_intrslaves,
    838 		    psc, sc_intrq);
    839 	}
    840 
    841 	if (psc->sc_flags & TULIP_PCI_SLAVEINTR) {
    842 		printf("%s: sharing interrupt with %s\n",
    843 		    sc->sc_dev.dv_xname,
    844 		    psc->sc_master->sc_tulip.sc_dev.dv_xname);
    845 	} else {
    846 		/*
    847 		 * Map and establish our interrupt.
    848 		 */
    849 		if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    850 		    pa->pa_intrline, &ih)) {
    851 			printf("%s: unable to map interrupt\n",
    852 			    sc->sc_dev.dv_xname);
    853 			return;
    854 		}
    855 		intrstr = pci_intr_string(pc, ih);
    856 		psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET,
    857 		    (psc->sc_flags & TULIP_PCI_SHAREDINTR) ?
    858 		    tlp_pci_shared_intr : tlp_intr, sc);
    859 		if (psc->sc_ih == NULL) {
    860 			printf("%s: unable to establish interrupt",
    861 			    sc->sc_dev.dv_xname);
    862 			if (intrstr != NULL)
    863 				printf(" at %s", intrstr);
    864 			printf("\n");
    865 			return;
    866 		}
    867 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    868 		    intrstr);
    869 	}
    870 
    871 	/*
    872 	 * Finish off the attach.
    873 	 */
    874 	tlp_attach(sc, enaddr);
    875 }
    876 
    877 int
    878 tlp_pci_shared_intr(arg)
    879 	void *arg;
    880 {
    881 	struct tulip_pci_softc *master = arg, *slave;
    882 	int rv = 0;
    883 
    884 	for (slave = LIST_FIRST(&master->sc_intrslaves);
    885 	     slave != NULL;
    886 	     slave = LIST_NEXT(slave, sc_intrq))
    887 		rv |= tlp_intr(&slave->sc_tulip);
    888 
    889 	return (rv);
    890 }
    891 
    892 void
    893 tlp_pci_dec_quirks(psc, enaddr)
    894 	struct tulip_pci_softc *psc;
    895 	const u_int8_t *enaddr;
    896 {
    897 	struct tulip_softc *sc = &psc->sc_tulip;
    898 
    899 	/*
    900 	 * This isn't really a quirk-gathering device, really.  We
    901 	 * just want to get the spiffy DEC board name from the SROM.
    902 	 */
    903 	strcpy(sc->sc_name, "DEC ");
    904 
    905 	if (memcmp(&sc->sc_srom[29], "DE500", 5) == 0 ||
    906 	    memcmp(&sc->sc_srom[29], "DE450", 5) == 0)
    907 		memcpy(&sc->sc_name[4], &sc->sc_srom[29], 8);
    908 }
    909 
    910 void
    911 tlp_pci_znyx_21040_quirks(psc, enaddr)
    912 	struct tulip_pci_softc *psc;
    913 	const u_int8_t *enaddr;
    914 {
    915 	struct tulip_softc *sc = &psc->sc_tulip;
    916 	u_int16_t id = 0;
    917 
    918 	/*
    919 	 * If we have a slaved ROM, just copy the bits from the master.
    920 	 * This is in case we fail the ROM ID check (older boards) and
    921 	 * need to fall back on Ethernet address model checking; that
    922 	 * will fail for slave chips.
    923 	 */
    924 	if (psc->sc_flags & TULIP_PCI_SLAVEROM) {
    925 		strcpy(sc->sc_name, psc->sc_master->sc_tulip.sc_name);
    926 		sc->sc_mediasw = psc->sc_master->sc_tulip.sc_mediasw;
    927 		psc->sc_flags |=
    928 		    psc->sc_master->sc_flags & TULIP_PCI_SHAREDINTR;
    929 		return;
    930 	}
    931 
    932 	if (sc->sc_srom[32] == 0x4a && sc->sc_srom[33] == 0x52) {
    933 		id = sc->sc_srom[37] | (sc->sc_srom[36] << 8);
    934 		switch (id) {
    935  zx312:
    936 		case 0x0602:	/* ZX312 */
    937 			strcpy(sc->sc_name, "ZNYX ZX312");
    938 			return;
    939 
    940 		case 0x0622:	/* ZX312T */
    941 			strcpy(sc->sc_name, "ZNYX ZX312T");
    942 			sc->sc_mediasw = &tlp_21040_tp_mediasw;
    943 			return;
    944 
    945  zx314_inta:
    946 		case 0x0701:	/* ZX314 INTA */
    947 			psc->sc_flags |= TULIP_PCI_SHAREDINTR;
    948 			/* FALLTHROUGH */
    949 		case 0x0711:	/* ZX314 */
    950 			strcpy(sc->sc_name, "ZNYX ZX314");
    951 			psc->sc_flags |= TULIP_PCI_SHAREDROM;
    952 			sc->sc_mediasw = &tlp_21040_tp_mediasw;
    953 			return;
    954 
    955  zx315_inta:
    956 		case 0x0801:	/* ZX315 INTA */
    957 			psc->sc_flags |= TULIP_PCI_SHAREDINTR;
    958 			/* FALLTHROUGH */
    959 		case 0x0811:	/* ZX315 */
    960 			strcpy(sc->sc_name, "ZNYX ZX315");
    961 			psc->sc_flags |= TULIP_PCI_SHAREDROM;
    962 			return;
    963 
    964 		default:
    965 			id = 0;
    966 		}
    967 	}
    968 
    969 	/*
    970 	 * Deal with boards that have broken ROMs.
    971 	 */
    972 	if (id == 0) {
    973 		if ((enaddr[3] & ~3) == 0xf0 && (enaddr[5] & 3) == 0x00)
    974 			goto zx314_inta;
    975 		if ((enaddr[3] & ~3) == 0xf4 && (enaddr[5] & 1) == 0x00)
    976 			goto zx315_inta;
    977 		if ((enaddr[3] & ~3) == 0xec)
    978 			goto zx312;
    979 	}
    980 
    981 	strcpy(sc->sc_name, "ZNYX ZX31x");
    982 }
    983 
    984 void
    985 tlp_pci_smc_21040_quirks(psc, enaddr)
    986 	struct tulip_pci_softc *psc;
    987 	const u_int8_t *enaddr;
    988 {
    989 	struct tulip_softc *sc = &psc->sc_tulip;
    990 	u_int16_t id1, id2, ei;
    991 	int auibnc = 0, utp = 0;
    992 	char *cp;
    993 
    994 	id1 = sc->sc_srom[0x60] | (sc->sc_srom[0x61] << 8);
    995 	id2 = sc->sc_srom[0x62] | (sc->sc_srom[0x63] << 8);
    996 	ei  = sc->sc_srom[0x66] | (sc->sc_srom[0x67] << 8);
    997 
    998 	strcpy(sc->sc_name, "SMC 8432");
    999 	cp = &sc->sc_name[8];
   1000 
   1001 	if ((id1 & 1) == 0) {
   1002 		*cp++ = 'B';
   1003 		auibnc = 1;
   1004 	}
   1005 	if ((id1 & 0xff) > 0x32) {
   1006 		*cp++ = 'T';
   1007 		utp = 1;
   1008 	}
   1009 	if ((id1 & 0x4000) == 0) {
   1010 		*cp++ = 'A';
   1011 		auibnc = 1;
   1012 	}
   1013 	if (id2 == 0x15) {
   1014 		sc->sc_name[7] = '4';
   1015 		*cp++ = '-';
   1016 		*cp++ = 'C';
   1017 		*cp++ = 'H';
   1018 		*cp++ = ei ? '2' : '1';
   1019 	}
   1020 	*cp = '\0';
   1021 
   1022 	if (utp != 0 && auibnc == 0)
   1023 		sc->sc_mediasw = &tlp_21040_tp_mediasw;
   1024 	else if (utp == 0 && auibnc != 0)
   1025 		sc->sc_mediasw = &tlp_21040_auibnc_mediasw;
   1026 }
   1027 
   1028 void
   1029 tlp_pci_cogent_21040_quirks(psc, enaddr)
   1030 	struct tulip_pci_softc *psc;
   1031 	const u_int8_t *enaddr;
   1032 {
   1033 
   1034 	strcpy(psc->sc_tulip.sc_name, "Cogent multi-port");
   1035 	psc->sc_flags |= TULIP_PCI_SHAREDINTR|TULIP_PCI_SHAREDROM;
   1036 }
   1037 
   1038 void
   1039 tlp_pci_accton_21040_quirks(psc, enaddr)
   1040 	struct tulip_pci_softc *psc;
   1041 	const u_int8_t *enaddr;
   1042 {
   1043 
   1044 	strcpy(psc->sc_tulip.sc_name, "ACCTON EN1203");
   1045 }
   1046 
   1047 void	tlp_pci_asante_21140_reset __P((struct tulip_softc *));
   1048 
   1049 void
   1050 tlp_pci_asante_21140_quirks(psc, enaddr)
   1051 	struct tulip_pci_softc *psc;
   1052 	const u_int8_t *enaddr;
   1053 {
   1054 	struct tulip_softc *sc = &psc->sc_tulip;
   1055 
   1056 	/*
   1057 	 * Some Asante boards don't use the ISV SROM format.  For
   1058 	 * those that don't, we initialize the GPIO direction bits,
   1059 	 * and provide our own reset hook, which resets the MII.
   1060 	 *
   1061 	 * All of these boards use SIO-attached-MII media.
   1062 	 */
   1063 	if (sc->sc_mediasw == &tlp_2114x_isv_mediasw)
   1064 		return;
   1065 
   1066 	strcpy(sc->sc_name, "Asante");
   1067 
   1068 	sc->sc_gp_dir = 0xbf;
   1069 	sc->sc_reset = tlp_pci_asante_21140_reset;
   1070 	sc->sc_mediasw = &tlp_sio_mii_mediasw;
   1071 }
   1072 
   1073 void
   1074 tlp_pci_asante_21140_reset(sc)
   1075 	struct tulip_softc *sc;
   1076 {
   1077 
   1078 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);
   1079 	TULIP_WRITE(sc, CSR_GPP, 0x8);
   1080 	delay(100);
   1081 	TULIP_WRITE(sc, CSR_GPP, 0);
   1082 }
   1083 
   1084 void	tlp_pci_cobalt_21142_reset __P((struct tulip_softc *));
   1085 
   1086 void
   1087 tlp_pci_cobalt_21142_quirks(psc, enaddr)
   1088 	struct tulip_pci_softc *psc;
   1089 	const u_int8_t *enaddr;
   1090 {
   1091 	struct tulip_softc *sc = &psc->sc_tulip;
   1092 
   1093 	/*
   1094 	 * Cobalt Networks interfaces are just MII-on-SIO.
   1095 	 */
   1096 	sc->sc_reset = tlp_pci_cobalt_21142_reset;
   1097 	sc->sc_mediasw = &tlp_sio_mii_mediasw;
   1098 
   1099 	/*
   1100 	 * The Cobalt systems tend to fall back to store-and-forward
   1101 	 * pretty quickly, so we select that from the beginning to
   1102 	 * avoid initial timeouts.
   1103 	 */
   1104 #define TXTH_SF		4
   1105 	sc->sc_txthresh = TXTH_SF;
   1106 }
   1107 
   1108 void
   1109 tlp_pci_cobalt_21142_reset(sc)
   1110 	struct tulip_softc *sc;
   1111 {
   1112 	/*
   1113 	 * Reset PHY.
   1114 	 */
   1115 	TULIP_WRITE(sc, CSR_SIAGEN, SIAGEN_CWE | (1 << 16));
   1116 	delay(10);
   1117 	TULIP_WRITE(sc, CSR_SIAGEN, SIAGEN_CWE);
   1118 	delay(10);
   1119 }
   1120