Home | History | Annotate | Line # | Download | only in netboot
if_le.c revision 1.1
      1 /*	$NetBSD: if_le.c,v 1.1 1996/05/17 21:18:34 chuck Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Theo de Raadt
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed under OpenBSD by
     17  *	Theo de Raadt for Willowglen Singapore.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  * Copyright (c) 1993 Adam Glass
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by Adam Glass.
     47  * 4. The name of the Author may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/param.h>
     64 #include <sys/types.h>
     65 
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 
     69 #include <machine/prom.h>
     70 
     71 #include "stand.h"
     72 #include "libsa.h"
     73 #include "netif.h"
     74 #include "config.h"
     75 
     76 #include "if_lereg.h"
     77 
     78 int     le_debug = 0;
     79 
     80 void le_end __P((struct netif *));
     81 void le_error __P((struct netif *, char *, volatile struct lereg1 *));
     82 int le_get __P((struct iodesc *, void *, size_t, time_t));
     83 void le_init __P((struct iodesc *, void *));
     84 int le_match __P((struct netif *, void *));
     85 int le_poll __P((struct iodesc *, void *, int));
     86 int le_probe __P((struct netif *, void *));
     87 int le_put __P((struct iodesc *, void *, size_t));
     88 void le_reset __P((struct netif *, u_char *));
     89 
     90 struct netif_stats le_stats;
     91 
     92 struct netif_dif le0_dif = {
     93 	0,			/* unit */
     94 	1,			/* nsel */
     95 	&le_stats,
     96 	0,
     97 	0,
     98 };
     99 
    100 struct netif_driver le_driver = {
    101 	"le",			/* netif_bname */
    102 	le_match,		/* match */
    103 	le_probe,		/* probe */
    104 	le_init,		/* init */
    105 	le_get,			/* get */
    106 	le_put,			/* put */
    107 	le_end,			/* end */
    108 	&le0_dif,		/* netif_ifs */
    109 	1,			/* netif_nifs */
    110 };
    111 
    112 struct le_configuration {
    113 	unsigned int phys_addr;
    114 	int     used;
    115 } le_config[] = {
    116 	{ LANCE_REG_ADDR, 0 }
    117 };
    118 
    119 int     nle_config = sizeof(le_config) / (sizeof(le_config[0]));
    120 
    121 struct {
    122 	struct lereg1 *sc_r1;	/* LANCE registers */
    123 	struct lereg2 *sc_r2;	/* RAM */
    124 	int     next_rmd;
    125 	int     next_tmd;
    126 }       le_softc;
    127 
    128 int
    129 le_match(nif, machdep_hint)
    130 	struct netif *nif;
    131 	void   *machdep_hint;
    132 {
    133 	char   *name;
    134 	int     i, val = 0;
    135 
    136 	if (bugargs.cputyp != CPU_147)
    137 		return (0);
    138 	name = machdep_hint;
    139 	if (name && !bcmp(le_driver.netif_bname, name, 2))
    140 		val += 10;
    141 	for (i = 0; i < nle_config; i++) {
    142 		if (le_config[i].used)
    143 			continue;
    144 		if (le_debug)
    145 			printf("le%d: le_match --> %d\n", i, val + 1);
    146 		le_config[i].used++;
    147 		return val + 1;
    148 	}
    149 	if (le_debug)
    150 		printf("le%d: le_match --> 0\n", i);
    151 	return 0;
    152 }
    153 
    154 int
    155 le_probe(nif, machdep_hint)
    156 	struct netif *nif;
    157 	void   *machdep_hint;
    158 {
    159 
    160 	/* the set unit is the current unit */
    161 	if (le_debug)
    162 		printf("le%d: le_probe called\n", nif->nif_unit);
    163 
    164 	if (bugargs.cputyp == CPU_147)
    165 		return 0;
    166 	return 1;
    167 }
    168 
    169 void
    170 le_error(nif, str, ler1)
    171 	struct netif *nif;
    172 	char   *str;
    173 	volatile struct lereg1 *ler1;
    174 {
    175 	/* ler1->ler1_rap = LE_CSRO done in caller */
    176 	if (ler1->ler1_rdp & LE_C0_BABL)
    177 		panic("le%d: been babbling, found by '%s'\n", nif->nif_unit, str);
    178 	if (ler1->ler1_rdp & LE_C0_CERR) {
    179 		le_stats.collision_error++;
    180 		ler1->ler1_rdp = LE_C0_CERR;
    181 	}
    182 	if (ler1->ler1_rdp & LE_C0_MISS) {
    183 		le_stats.missed++;
    184 		ler1->ler1_rdp = LE_C0_MISS;
    185 	}
    186 	if (ler1->ler1_rdp & LE_C0_MERR) {
    187 		printf("le%d: memory error in '%s'\n", nif->nif_unit, str);
    188 		panic("memory error");
    189 	}
    190 }
    191 
    192 void
    193 le_reset(nif, myea)
    194 	struct netif *nif;
    195 	u_char *myea;
    196 {
    197 	struct lereg1 *ler1 = le_softc.sc_r1;
    198 	struct lereg2 *ler2 = le_softc.sc_r2;
    199 	unsigned int a;
    200 	int     timo = 100000, stat, i;
    201 
    202 	if (le_debug)
    203 		printf("le%d: le_reset called\n", nif->nif_unit);
    204 	ler1->ler1_rap = LE_CSR0;
    205 	ler1->ler1_rdp = LE_C0_STOP;	/* do nothing until we are finished */
    206 
    207 	bzero(ler2, sizeof(*ler2));
    208 
    209 	ler2->ler2_mode = LE_MODE_NORMAL;
    210 	ler2->ler2_padr[0] = myea[1];
    211 	ler2->ler2_padr[1] = myea[0];
    212 	ler2->ler2_padr[2] = myea[3];
    213 	ler2->ler2_padr[3] = myea[2];
    214 	ler2->ler2_padr[4] = myea[5];
    215 	ler2->ler2_padr[5] = myea[4];
    216 
    217 
    218 	ler2->ler2_ladrf0 = 0;
    219 	ler2->ler2_ladrf1 = 0;
    220 
    221 	a = (u_int) ler2->ler2_rmd;
    222 	ler2->ler2_rlen = LE_RLEN | (a >> 16);
    223 	ler2->ler2_rdra = a & LE_ADDR_LOW_MASK;
    224 
    225 	a = (u_int) ler2->ler2_tmd;
    226 	ler2->ler2_tlen = LE_TLEN | (a >> 16);
    227 	ler2->ler2_tdra = a & LE_ADDR_LOW_MASK;
    228 
    229 	ler1->ler1_rap = LE_CSR1;
    230 	a = (u_int) ler2;
    231 	ler1->ler1_rdp = a & LE_ADDR_LOW_MASK;
    232 	ler1->ler1_rap = LE_CSR2;
    233 	ler1->ler1_rdp = a >> 16;
    234 
    235 	for (i = 0; i < LERBUF; i++) {
    236 		a = (u_int) & ler2->ler2_rbuf[i];
    237 		ler2->ler2_rmd[i].rmd0 = a & LE_ADDR_LOW_MASK;
    238 		ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
    239 		ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
    240 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
    241 		ler2->ler2_rmd[i].rmd3 = 0;
    242 	}
    243 	for (i = 0; i < LETBUF; i++) {
    244 		a = (u_int) & ler2->ler2_tbuf[i];
    245 		ler2->ler2_tmd[i].tmd0 = a & LE_ADDR_LOW_MASK;
    246 		ler2->ler2_tmd[i].tmd1_bits = 0;
    247 		ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
    248 		ler2->ler2_tmd[i].tmd2 = 0;
    249 		ler2->ler2_tmd[i].tmd3 = 0;
    250 	}
    251 
    252 	ler1->ler1_rap = LE_CSR3;
    253 	ler1->ler1_rdp = LE_C3_BSWP;
    254 
    255 	ler1->ler1_rap = LE_CSR0;
    256 	ler1->ler1_rdp = LE_C0_INIT;
    257 	do {
    258 		if (--timo == 0) {
    259 			printf("le%d: init timeout, stat = 0x%x\n",
    260 			    nif->nif_unit, stat);
    261 			break;
    262 		}
    263 		stat = ler1->ler1_rdp;
    264 	} while ((stat & LE_C0_IDON) == 0);
    265 
    266 	ler1->ler1_rdp = LE_C0_IDON;
    267 	le_softc.next_rmd = 0;
    268 	le_softc.next_tmd = 0;
    269 	ler1->ler1_rap = LE_CSR0;
    270 	ler1->ler1_rdp = LE_C0_STRT;
    271 }
    272 
    273 int
    274 le_poll(desc, pkt, len)
    275 	struct iodesc *desc;
    276 	void   *pkt;
    277 	int     len;
    278 {
    279 	struct lereg1 *ler1 = le_softc.sc_r1;
    280 	struct lereg2 *ler2 = le_softc.sc_r2;
    281 	unsigned int a;
    282 	int     length;
    283 	struct lermd *rmd;
    284 
    285 
    286 	ler1->ler1_rap = LE_CSR0;
    287 	if ((ler1->ler1_rdp & LE_C0_RINT) != 0)
    288 		ler1->ler1_rdp = LE_C0_RINT;
    289 	rmd = &ler2->ler2_rmd[le_softc.next_rmd];
    290 	if (rmd->rmd1_bits & LE_R1_OWN) {
    291 		return (0);
    292 	}
    293 	if (ler1->ler1_rdp & LE_C0_ERR)
    294 		le_error(desc->io_netif, "le_poll", ler1);
    295 	if (rmd->rmd1_bits & LE_R1_ERR) {
    296 		printf("le%d_poll: rmd status 0x%x\n", desc->io_netif->nif_unit,
    297 		    rmd->rmd1_bits);
    298 		length = 0;
    299 		goto cleanup;
    300 	}
    301 	if ((rmd->rmd1_bits & (LE_R1_STP | LE_R1_ENP)) != (LE_R1_STP | LE_R1_ENP))
    302 		panic("le_poll: chained packet\n");
    303 
    304 	length = rmd->rmd3;
    305 	if (length >= LEMTU) {
    306 		length = 0;
    307 		panic("csr0 when bad things happen: %x\n", ler1->ler1_rdp);
    308 		goto cleanup;
    309 	}
    310 	if (!length)
    311 		goto cleanup;
    312 	length -= 4;
    313 	if (length > 0) {
    314 
    315 		/*
    316 	         * if buffer is smaller than the packet truncate it.
    317 	         * (is this wise?)
    318 	         */
    319 		if (length > len)
    320 			length = len;
    321 
    322 		bcopy((void *)&ler2->ler2_rbuf[le_softc.next_rmd], pkt, length);
    323 	}
    324 cleanup:
    325 	a = (u_int) & ler2->ler2_rbuf[le_softc.next_rmd];
    326 	rmd->rmd0 = a & LE_ADDR_LOW_MASK;
    327 	rmd->rmd1_hadr = a >> 16;
    328 	rmd->rmd2 = -LEMTU;
    329 	le_softc.next_rmd =
    330 	    (le_softc.next_rmd == (LERBUF - 1)) ? 0 : (le_softc.next_rmd + 1);
    331 	rmd->rmd1_bits = LE_R1_OWN;
    332 	return length;
    333 }
    334 
    335 int
    336 le_put(desc, pkt, len)
    337 	struct	iodesc *desc;
    338 	void	*pkt;
    339 	size_t	len;
    340 {
    341 	volatile struct lereg1 *ler1 = le_softc.sc_r1;
    342 	volatile struct lereg2 *ler2 = le_softc.sc_r2;
    343 	volatile struct letmd *tmd;
    344 	int     timo = 100000, stat, i;
    345 	unsigned int a;
    346 
    347 	ler1->ler1_rap = LE_CSR0;
    348 	if (ler1->ler1_rdp & LE_C0_ERR)
    349 		le_error(desc->io_netif, "le_put(way before xmit)", ler1);
    350 	tmd = &ler2->ler2_tmd[le_softc.next_tmd];
    351 	while (tmd->tmd1_bits & LE_T1_OWN) {
    352 		printf("le%d: output buffer busy\n", desc->io_netif->nif_unit);
    353 	}
    354 	bcopy(pkt, (void *)ler2->ler2_tbuf[le_softc.next_tmd], len);
    355 	if (len < 64)
    356 		tmd->tmd2 = -64;
    357 	else
    358 		tmd->tmd2 = -len;
    359 	tmd->tmd3 = 0;
    360 	if (ler1->ler1_rdp & LE_C0_ERR)
    361 		le_error(desc->io_netif, "le_put(before xmit)", ler1);
    362 	tmd->tmd1_bits = LE_T1_STP | LE_T1_ENP | LE_T1_OWN;
    363 	a = (u_int) & ler2->ler2_tbuf[le_softc.next_tmd];
    364 	tmd->tmd0 = a & LE_ADDR_LOW_MASK;
    365 	tmd->tmd1_hadr = a >> 16;
    366 	ler1->ler1_rdp = LE_C0_TDMD;
    367 	if (ler1->ler1_rdp & LE_C0_ERR)
    368 		le_error(desc->io_netif, "le_put(after xmit)", ler1);
    369 	do {
    370 		if (--timo == 0) {
    371 			printf("le%d: transmit timeout, stat = 0x%x\n",
    372 			    desc->io_netif->nif_unit, stat);
    373 			if (ler1->ler1_rdp & LE_C0_ERR)
    374 				le_error(desc->io_netif, "le_put(timeout)", ler1);
    375 			break;
    376 		}
    377 		stat = ler1->ler1_rdp;
    378 	} while ((stat & LE_C0_TINT) == 0);
    379 	ler1->ler1_rdp = LE_C0_TINT;
    380 	if (ler1->ler1_rdp & LE_C0_ERR) {
    381 		if ((ler1->ler1_rdp & (LE_C0_BABL | LE_C0_CERR | LE_C0_MISS |
    382 		    LE_C0_MERR)) !=
    383 		    LE_C0_CERR)
    384 			printf("le_put: xmit error, buf %d\n", le_softc.next_tmd);
    385 		le_error(desc->io_netif, "le_put(xmit error)", ler1);
    386 	}
    387 	le_softc.next_tmd = 0;
    388 /*	(le_softc.next_tmd == (LETBUF - 1)) ? 0 : le_softc.next_tmd + 1;*/
    389 	if (tmd->tmd1_bits & LE_T1_DEF)
    390 		le_stats.deferred++;
    391 	if (tmd->tmd1_bits & LE_T1_ONE)
    392 		le_stats.collisions++;
    393 	if (tmd->tmd1_bits & LE_T1_MORE)
    394 		le_stats.collisions += 2;
    395 	if (tmd->tmd1_bits & LE_T1_ERR) {
    396 		printf("le%d: transmit error, error = 0x%x\n", desc->io_netif->nif_unit,
    397 		    tmd->tmd3);
    398 		return -1;
    399 	}
    400 	if (le_debug) {
    401 		printf("le%d: le_put() successful: sent %d\n",
    402 		    desc->io_netif->nif_unit, len);
    403 		printf("le%d: le_put(): tmd1_bits: %x tmd3: %x\n",
    404 		    desc->io_netif->nif_unit,
    405 		    (unsigned int) tmd->tmd1_bits,
    406 		    (unsigned int) tmd->tmd3);
    407 	}
    408 	return len;
    409 }
    410 
    411 int
    412 le_get(desc, pkt, len, timeout)
    413 	struct	iodesc *desc;
    414 	void	*pkt;
    415 	size_t	len;
    416 	time_t	timeout;
    417 {
    418 	time_t  t;
    419 	int     cc;
    420 
    421 	t = getsecs();
    422 	cc = 0;
    423 	while (((getsecs() - t) < timeout) && !cc) {
    424 		cc = le_poll(desc, pkt, len);
    425 	}
    426 	return cc;
    427 }
    428 /*
    429  * init le device.   return 0 on failure, 1 if ok.
    430  */
    431 void
    432 le_init(desc, machdep_hint)
    433 	struct iodesc *desc;
    434 	void   *machdep_hint;
    435 {
    436 	u_long eram = 4*1024*1024;
    437 	struct netif *nif = desc->io_netif;
    438 
    439 	if (le_debug)
    440 		printf("le%d: le_init called\n", desc->io_netif->nif_unit);
    441 	machdep_common_ether(desc->myea);
    442 	bzero(&le_softc, sizeof(le_softc));
    443 	le_softc.sc_r1 =
    444 	    (struct lereg1 *) le_config[desc->io_netif->nif_unit].phys_addr;
    445 	le_softc.sc_r2 = (struct lereg2 *) (eram - (1024 * 1024));
    446 	le_reset(desc->io_netif, desc->myea);
    447 	printf("device: %s%d attached to %s\n", nif->nif_driver->netif_bname,
    448 	    nif->nif_unit, ether_sprintf(desc->myea));
    449 }
    450 
    451 void
    452 le_end(nif)
    453 	struct netif *nif;
    454 {
    455 	struct lereg1 *ler1 = le_softc.sc_r1;
    456 
    457 	if (le_debug)
    458 		printf("le%d: le_end called\n", nif->nif_unit);
    459 	ler1->ler1_rap = LE_CSR0;
    460 	ler1->ler1_rdp = LE_C0_STOP;
    461 }
    462