Home | History | Annotate | Line # | Download | only in boot
en.c revision 1.7
      1 /*      $NetBSD: en.c,v 1.7 2002/09/11 06:32:07 mycroft Exp $        */
      2 /*
      3  * Copyright (c) 1996 Rolf Grossmann
      4  * All rights reserved.
      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 by Rolf Grossmann.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/types.h>
     34 #include <netinet/in.h>
     35 #include <netinet/in_systm.h>
     36 #include <next68k/dev/enreg.h>
     37 #include <next68k/next68k/nextrom.h>
     38 #include "enreg.h"
     39 #include "dmareg.h"
     40 
     41 #include <stand.h>
     42 #include <netif.h>
     43 #include <net.h>
     44 #include <nfs.h>
     45 
     46 #include <lib/libkern/libkern.h>
     47 
     48 extern char *mg;
     49 #define	MON(type, off) (*(type *)((u_int) (mg) + off))
     50 
     51 #define PRINTF(x) printf x;
     52 #ifdef EN_DEBUG
     53 #define DPRINTF(x) printf x;
     54 #else
     55 #define DPRINTF(x)
     56 #endif
     57 
     58 #define	EN_TIMEOUT	2000000
     59 #define EN_RETRIES	10
     60 
     61 int en_match __P((struct netif *nif, void *machdep_hint));
     62 int en_probe __P((struct netif *nif, void *machdep_hint));
     63 void en_init __P((struct iodesc *desc, void *machdep_hint));
     64 int en_get __P((struct iodesc *a, void *b, size_t c, time_t d));
     65 int en_put __P((struct iodesc *a, void *b, size_t c));
     66 void en_end __P((struct netif *a));
     67 
     68 /* ### static int mountroot __P((int sock)); */
     69 static int en_wait_for_intr __P((int flag));
     70 
     71 struct netif_stats en_stats;
     72 
     73 struct netif_dif en_ifs[] = {
     74 /* dif_unit	dif_nsel	dif_stats	dif_private     */
     75 {  0,		1,		&en_stats,	NULL,	},
     76 };
     77 
     78 struct netif_driver en_driver = {
     79 	"en",
     80 	en_match, en_probe, en_init, en_get, en_put, en_end,
     81 	en_ifs, NENTS(en_ifs)
     82 };
     83 
     84 extern int turbo;
     85 
     86 /* ### int netdev_sock;
     87 static int open_count; */
     88 
     89 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
     90      dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
     91      *dma_buffers[2];
     92 int next_dma_buffer;
     93 
     94 
     95 int
     96 en_match(struct netif *nif, void *machdep_hint)
     97 {
     98 	/* we always match, because every NeXT has an ethernet interface
     99 	 * and we've checked the unit numbers before we even started this
    100 	 * search.
    101 	 * ### now that open is generic, we should check the params!
    102 	 */
    103 	return 1;
    104 }
    105 
    106 int
    107 en_probe(struct netif *nif, void *machdep_hint)
    108 {
    109 	/* we also always probe ok, see en_match. */
    110 	return 0;
    111 }
    112 
    113 void
    114 en_init(struct iodesc *desc, void *machdep_hint)
    115 {
    116 	volatile struct en_regs *er;
    117 	volatile u_int *bmap_chip;
    118 	int i;
    119 
    120 	DPRINTF(("en_init\n"));
    121 
    122 	er = (struct en_regs *)P_ENET;
    123 	bmap_chip = (u_int *)P_BMAP;
    124 
    125 	dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
    126 	dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
    127 
    128 	er->reset = EN_RST_RESET;
    129 /* 	if (turbo) */
    130 /* 		er->reset = 0; */
    131 
    132 	er->txmask = 0;
    133 	er->txstat = 0xff;
    134 	if (turbo)
    135 		er->txmode = 0 | EN_TMD_COLLSHIFT;
    136 	else
    137 		er->txmode = EN_TMD_LB_DISABLE;
    138 
    139 	/* setup for bnc/tp */
    140 	if (!turbo) {
    141 		DPRINTF (("en_media: %s\n",
    142 			  (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
    143 		if (!(bmap_chip[13] & 0x20000000)) {
    144 			bmap_chip[12] |= 0x90000000;
    145 			bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
    146 		}
    147 	}
    148 
    149 /* 	if (turbo) { */
    150 /* 		er->txmode |= EN_TMD_COLLSHIFT; */
    151 /* 	} else { */
    152 /* 		er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
    153 /* 	} */
    154 
    155 	er->rxmask = 0;
    156 	er->rxstat = 0xff;
    157 	if (turbo) {
    158 		er->rxmode = EN_RMD_TEST;
    159 	} else {
    160 		er->rxmode = EN_RMD_RECV_NORMAL;
    161 	}
    162 	for (i=0; i<6; i++)
    163 	  er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
    164 
    165 	DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
    166 			desc->myea[0],desc->myea[1],desc->myea[2],
    167 			desc->myea[3],desc->myea[4],desc->myea[5]));
    168 
    169 /* 	if (!turbo) */
    170 		er->reset = 0;
    171 }
    172 
    173 #if 0
    174 /* ### remove this when things work! */
    175 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    176 void
    177 dump_pkt(unsigned char *pkt, size_t len)
    178 {
    179 	size_t i, j;
    180 
    181 	printf("0000: ");
    182 	for(i=0; i<len; i++) {
    183 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    184 		if ((i+1) % 16 == 0) {
    185 			printf("  %c", '"');
    186 			for(j=0; j<16; j++)
    187 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    188 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
    189 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    190 		}
    191 	}
    192 	printf("\n");
    193 }
    194 #endif
    195 
    196 int
    197 en_put(struct iodesc *desc, void *pkt, size_t len)
    198 {
    199 	volatile struct en_regs *er;
    200 	volatile struct dma_dev *txdma;
    201 	int state, txs;
    202 	int retries;
    203 
    204 	DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
    205 #if 0
    206 	dump_pkt(pkt,len);
    207 #endif
    208 
    209 	er = (struct en_regs *)P_ENET;
    210 	txdma = (struct dma_dev *)P_ENETX_CSR;
    211 
    212 	DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
    213 
    214 	if (len > 1600) {
    215 		errno = EINVAL;
    216 		return -1;
    217 	}
    218 
    219 	if (!turbo) {
    220 		while ((er->txstat & EN_TXS_READY) == 0)
    221 			printf("en: tx not ready\n");
    222 	}
    223 
    224 	for (retries = 0; retries < EN_RETRIES; retries++) {
    225 		er->txstat = 0xff;
    226 		bcopy(pkt, dma_buffers[0], len);
    227 		txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
    228 			DMACSR_RESET | DMACSR_WRITE;
    229 		txdma->dd_csr = 0;
    230 		txdma->dd_next/* _initbuf */ = dma_buffers[0];
    231 		txdma->dd_start = (turbo ? dma_buffers[0] : 0);
    232 		txdma->dd_limit = ENDMA_ENDALIGN(char *,  dma_buffers[0]+len);
    233 		txdma->dd_stop = 0;
    234 		txdma->dd_csr = DMACSR_SETENABLE;
    235 		if (turbo)
    236 			er->txmode |= 0x80;
    237 
    238 		while(1) {
    239 			if (en_wait_for_intr(ENETX_DMA_INTR)) {
    240 				printf("en_put: timed out\n");
    241 				errno = EIO;
    242 				return -1;
    243 			}
    244 
    245 			state = txdma->dd_csr &
    246 				(DMACSR_BUSEXC | DMACSR_COMPLETE
    247 				 | DMACSR_SUPDATE | DMACSR_ENABLE);
    248 
    249 #if 01
    250 			DPRINTF(("en_put: dma state = 0x%x.\n", state));
    251 #endif
    252 			if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
    253 				txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
    254 				break;
    255 		}
    256 
    257 		txs = er->txstat;
    258 
    259 #if 01
    260 		DPRINTF(("en_put: done txstat=%x.\n", txs));
    261 #endif
    262 
    263 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
    264 		if ((state & DMACSR_COMPLETE) == 0 ||
    265 		    (txs & EN_TXS_ERROR) != 0) {
    266 			errno = EIO;
    267 			return -1;
    268 		}
    269 		if ((txs & EN_TXS_COLLERR) == 0)
    270 			return len;		/* success */
    271 	}
    272 
    273 	errno = EIO;		/* too many retries */
    274 	return -1;
    275 }
    276 
    277 int
    278 en_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
    279 {
    280 	volatile struct en_regs *er;
    281 	volatile struct dma_dev *rxdma;
    282 	volatile struct dma_dev *txdma;
    283 	int state, rxs;
    284 	size_t rlen;
    285 	char *gotpkt;
    286 
    287 	rxdma = (struct dma_dev *)P_ENETR_CSR;
    288 	txdma = (struct dma_dev *)P_ENETX_CSR;
    289 	er = (struct en_regs *)P_ENET;
    290 
    291 	DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));
    292 
    293 	er->rxstat = 0xff;
    294 
    295 	/* this is mouse's code now ... still doesn't work :( */
    296 	/* The previous comment is now a lie, this does work
    297 	 * Darrin B Jewell <jewell (at) mit.edu>  Sat Jan 24 21:44:56 1998
    298 	 */
    299 
    300 	rxdma->dd_csr = 0;
    301 	rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
    302 		DMACSR_READ | DMACSR_RESET;
    303 
    304 	if (!turbo) {
    305 		rxdma->dd_saved_next = 0;
    306 		rxdma->dd_saved_limit = 0;
    307 		rxdma->dd_saved_start = 0;
    308 		rxdma->dd_saved_stop = 0;
    309 	} else {
    310 		rxdma->dd_saved_next = dma_buffers[0];
    311 	}
    312 
    313 	rxdma->dd_next = dma_buffers[0];
    314 	rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
    315 #if 0
    316 	if (turbo) {
    317 		/* !!! not a typo: txdma */
    318 		txdma->dd_stop = dma_buffers[0];
    319 	}
    320 #endif
    321 	rxdma->dd_start = 0;
    322 	rxdma->dd_stop = 0;
    323 	rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
    324 	if (turbo) {
    325 		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_MULTI;
    326 	} else {
    327 		er->rxmode = EN_RMD_RECV_NORMAL;
    328 	}
    329 
    330 #if 01
    331 	DPRINTF(("en_get: blocking on rcv dma\n"));
    332 #endif
    333 
    334 	while(1) {
    335 		if (en_wait_for_intr(ENETR_DMA_INTR))	/* ### use timeout? */
    336 			return 0;
    337 
    338 		state = rxdma->dd_csr &
    339 			(DMACSR_BUSEXC | DMACSR_COMPLETE
    340 			 | DMACSR_SUPDATE | DMACSR_ENABLE);
    341 		DPRINTF(("en_get: dma state = 0x%x.\n", state));
    342 		if ((state & DMACSR_ENABLE) == 0) {
    343 			rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
    344 			break;
    345 		}
    346 
    347 		if (state & DMACSR_COMPLETE) {
    348 			PRINTF(("en_get: ending dma sequence\n"));
    349 			rxdma->dd_csr = DMACSR_CLRCOMPLETE;
    350 		}
    351 
    352 	}
    353 
    354 	rxs = er->rxstat;
    355 
    356 	if ((state & DMACSR_COMPLETE) == 0 ||
    357 	    (rxs & EN_RX_OK) == 0) {
    358 		errno = EIO;
    359 		return -1;	/* receive failed */
    360 	}
    361 
    362 	if (turbo) {
    363 		gotpkt = rxdma->dd_saved_next;
    364 		rlen = rxdma->dd_next - rxdma->dd_saved_next;
    365 	} else {
    366 		gotpkt = rxdma->dd_saved_next;
    367 		rlen = rxdma->dd_next - rxdma->dd_saved_next;
    368 	}
    369 
    370 	if (gotpkt != dma_buffers[0]) {
    371 		printf("Unexpected received packet location\n");
    372 		printf("DEBUG: rxstat=%x.\n", rxs);
    373 		printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
    374 		printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
    375 		printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
    376 		printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
    377 		printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
    378 		printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
    379 		printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
    380 		printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
    381 		printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
    382 		printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
    383 		printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
    384 	}
    385 
    386 #if 0
    387 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
    388 #endif
    389 
    390  DPRINTF(("en_get: done rxstat=%x.\n", rxs));
    391 
    392 	if (rlen > len) {
    393 		DPRINTF(("en_get: buffer too small. want %d, got %d\n",
    394 			 len, rlen));
    395 		rlen = len;
    396 	}
    397 
    398 
    399 	bcopy(gotpkt, pkt, rlen);
    400 
    401 #if 0
    402 	printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
    403 			(u_long)gotpkt,(u_long)pkt,rlen);
    404 	dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
    405 	dump_pkt(pkt, rlen < 255 ? rlen : 128);
    406 #endif
    407 
    408 	return rlen;
    409 }
    410 
    411 
    412 void
    413 en_end(struct netif *a)
    414 {
    415   DPRINTF(("en_end: WARNING not doing anything\n"));
    416 }
    417 
    418 #if 0
    419 
    420 #define MKPANIC(ret,name,args) \
    421 	ret name args { panic(#name ## ": not implemented.\n"); }
    422 
    423 MKPANIC(void, en_end, (struct netif *a));
    424 
    425 /* device functions */
    426 
    427 int
    428 enopen(struct open_file *f, char count, char lun, char part)
    429 {
    430 	int error;
    431 
    432 	DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
    433 
    434 	if (count != 0 || lun != 0 || part != 0)
    435 		return EUNIT;	/* there can be exactly one ethernet */
    436 
    437 	if (open_count == 0) {
    438 		/* Find network interface. */
    439 		if ((netdev_sock = netif_open(NULL)) < 0)
    440 			return errno;
    441 		if ((error = mountroot(netdev_sock)) != 0) {
    442 			if (open_count == 0)
    443 				netif_close(netdev_sock);
    444 			return error;
    445 		}
    446 	}
    447 	open_count++;
    448 	f->f_devdata = NULL; /* ### nfs_root_node ?! */
    449 	return 0;
    450 }
    451 
    452 int
    453 enclose(struct open_file *f)
    454 {
    455 	if (open_count > 0)
    456 		if (--open_count == 0)
    457 			netif_close(netdev_sock);
    458 	f->f_devdata = NULL;
    459 	return 0;
    460 }
    461 
    462 int
    463 enstrategy(void *devdata, int rw, daddr_t dblk,
    464 	   size_t size, void *buf, size_t *rsize)
    465 {
    466 	return ENXIO;		/* wrong access method */
    467 }
    468 
    469 /* private function */
    470 
    471 static int
    472 mountroot(int sock)
    473 {
    474 	/* Mount the root directory from a boot server */
    475 #if 0
    476 	struct in_addr in = {
    477 		0xc2793418
    478 	};
    479 	u_char *res;
    480 
    481 	res = arpwhohas(socktodesc(sock), in);
    482 	panic("arpwhohas returned %s", res);
    483 #endif
    484 	/* 1. use bootp. This does most of the work for us. */
    485 	bootp(sock);
    486 
    487 	if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
    488 		return ETIMEDOUT;
    489 
    490 	printf("Using IP address: %s\n", inet_ntoa(myip));
    491 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
    492 
    493 	/* 2. mount. */
    494 	if (nfs_mount(sock, rootip, rootpath) < 0)
    495 		return errno;
    496 
    497 	return 0;
    498 }
    499 #endif
    500 
    501 static int
    502 en_wait_for_intr(int flag)
    503 {
    504   volatile int *intrstat = MON(volatile int *,MG_intrstat);
    505 
    506 	int count;
    507 
    508 	for(count = 0; count < EN_TIMEOUT; count++)
    509 		if (*intrstat & flag)
    510 			return 0;
    511 
    512 	return -1;
    513 }
    514