Home | History | Annotate | Line # | Download | only in boot
en.c revision 1.2
      1 /*      $NetBSD: en.c,v 1.2 1999/03/26 06:54:40 dbj 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 extern char *mg;
     47 #define	MON(type, off) (*(type *)((u_int) (mg) + off))
     48 
     49 #ifdef EN_DEBUG
     50 #define DPRINTF(x) printf x;
     51 #else
     52 #define DPRINTF(x)
     53 #endif
     54 
     55 #define	EN_TIMEOUT	2000000
     56 #define EN_RETRIES	10
     57 
     58 int en_match __P((struct netif *nif, void *machdep_hint));
     59 int en_probe __P((struct netif *nif, void *machdep_hint));
     60 void en_init __P((struct iodesc *desc, void *machdep_hint));
     61 int en_get __P((struct iodesc *a, void *b, size_t c, time_t d));
     62 int en_put __P((struct iodesc *a, void *b, size_t c));
     63 void en_end __P((struct netif *a));
     64 
     65 /* ### static int mountroot __P((int sock)); */
     66 static int en_wait_for_intr __P((int flag));
     67 
     68 struct netif_stats en_stats;
     69 
     70 struct netif_dif en_ifs[] = {
     71 /* dif_unit	dif_nsel	dif_stats	dif_private     */
     72 {  0,		1,		&en_stats,	NULL,	},
     73 };
     74 
     75 struct netif_driver en_driver = {
     76 	"en",
     77 	en_match, en_probe, en_init, en_get, en_put, en_end,
     78 	en_ifs, NENTS(en_ifs)
     79 };
     80 
     81 /* ### int netdev_sock;
     82 static int open_count; */
     83 
     84 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
     85      dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
     86      *dma_buffers[2];
     87 int next_dma_buffer;
     88 
     89 
     90 int
     91 en_match(struct netif *nif, void *machdep_hint)
     92 {
     93 	/* we always match, because every NeXT has an ethernet interface
     94 	 * and we've checked the unit numbers before we even started this
     95 	 * search.
     96 	 * ### now that open is generic, we should check the params!
     97 	 */
     98 	return 1;
     99 }
    100 
    101 int
    102 en_probe(struct netif *nif, void *machdep_hint)
    103 {
    104 	/* we also always probe ok, see en_match. */
    105 	return 0;
    106 }
    107 
    108 void
    109 en_init(struct iodesc *desc, void *machdep_hint)
    110 {
    111 	volatile struct en_regs *er;
    112 	volatile u_int *bmap_chip;
    113 	int i;
    114 
    115 	DPRINTF(("en_init\n"));
    116 
    117 	er = (struct en_regs *)P_ENET;
    118 	bmap_chip = (u_int *)P_BMAP;
    119 
    120 	dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
    121 	dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
    122 
    123 	er->reset = EN_RST_RESET;
    124 
    125 	/* ### we'll need this when we need to decide which interface to use
    126 	 bmap_chip[12] = 0x90000000;
    127 	 bmap_chip[13] = ~(0x80000000|0x10000000); * BMAP_TPE  ???
    128 	 */
    129 
    130 	er->txmode = EN_TMD_LB_DISABLE;
    131 	er->txmask = 0;
    132 	er->txstat = 0xff;
    133 	er->rxmode = EN_RMD_RECV_NORMAL;
    134 	er->rxmask = 0;
    135 	er->rxstat = 0xff;
    136 	for (i=0; i<6; i++)
    137 	  er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
    138 
    139 	DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
    140 			desc->myea[0],desc->myea[1],desc->myea[2],
    141 			desc->myea[3],desc->myea[4],desc->myea[5]));
    142 
    143 	er->reset = 0;
    144 }
    145 
    146 #if 0
    147 /* ### remove this when things work! */
    148 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    149 void
    150 dump_pkt(unsigned char *pkt, size_t len)
    151 {
    152 	size_t i, j;
    153 
    154 	printf("0000: ");
    155 	for(i=0; i<len; i++) {
    156 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    157 		if ((i+1) % 16 == 0) {
    158 			printf("  %c", '"');
    159 			for(j=0; j<16; j++)
    160 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    161 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
    162 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    163 		}
    164 	}
    165 	printf("\n");
    166 }
    167 #endif
    168 
    169 int
    170 en_put(struct iodesc *desc, void *pkt, size_t len)
    171 {
    172 	volatile struct en_regs *er;
    173 	volatile struct dma_dev *txdma;
    174 	int state, txs;
    175 	int retries;
    176 
    177 	DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
    178 #if 0
    179 	dump_pkt(pkt,len);
    180 #endif
    181 
    182 	er = (struct en_regs *)P_ENET;
    183 	txdma = (struct dma_dev *)P_ENETX_CSR;
    184 
    185 	DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
    186 
    187 	if (len > 1600) {
    188 		errno = EINVAL;
    189 		return -1;
    190 	}
    191 
    192 	while ((er->txstat & EN_TXS_READY) == 0)
    193 		printf("en: tx not ready\n");
    194 
    195 	for (retries = 0; retries < EN_RETRIES; retries++) {
    196 		er->txstat = 0xff;
    197 		bcopy(pkt, dma_buffers[0], len);
    198 		txdma->dd_csr = 0;
    199 		txdma->dd_csr = DMACSR_INITBUF | DMACSR_RESET | DMACSR_WRITE;
    200 		txdma->dd_next_initbuf = dma_buffers[0];
    201 		txdma->dd_limit = ENDMA_ENDALIGN(char *,  dma_buffers[0]+len);
    202 		txdma->dd_start = 0;
    203 		txdma->dd_stop = 0;
    204 		txdma->dd_csr = DMACSR_SETENABLE;
    205 
    206 		while(1) {
    207 			if (en_wait_for_intr(ENETX_DMA_INTR)) {
    208 				errno = EIO;
    209 				return -1;
    210 			}
    211 
    212 			state = txdma->dd_csr &
    213 				(DMACSR_BUSEXC | DMACSR_COMPLETE
    214 				 | DMACSR_SUPDATE | DMACSR_ENABLE);
    215 
    216 #if 0
    217 			DPRINTF(("en_put: dma state = 0x%x.\n", state));
    218 #endif
    219 			if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
    220 				txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
    221 				break;
    222 		}
    223 
    224 		txs = er->txstat;
    225 
    226 #if 0
    227 		DPRINTF(("en_put: done txstat=%x.\n", txs));
    228 #endif
    229 
    230 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
    231 		if ((state & DMACSR_COMPLETE) == 0 ||
    232 		    (txs & EN_TXS_ERROR) != 0) {
    233 			errno = EIO;
    234 			return -1;
    235 		}
    236 		if ((txs & EN_TXS_COLLERR) == 0)
    237 			return len;		/* success */
    238 	}
    239 
    240 	errno = EIO;		/* too many retries */
    241 	return -1;
    242 }
    243 
    244 int
    245 en_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
    246 {
    247 	volatile struct en_regs *er;
    248 	volatile struct dma_dev *rxdma;
    249 	int state, rxs;
    250 	size_t rlen;
    251 	char *gotpkt;
    252 
    253 	rxdma = (struct dma_dev *)P_ENETR_CSR;
    254 	er = (struct en_regs *)P_ENET;
    255 
    256 	er->rxstat = 0xff;
    257 
    258 	/* this is mouse's code now ... still doesn't work :( */
    259 	/* The previous comment is now a lie, this does work
    260 	 * Darrin B Jewell <jewell (at) mit.edu>  Sat Jan 24 21:44:56 1998
    261 	 */
    262 
    263 	rxdma->dd_csr = 0;
    264 	rxdma->dd_csr = DMACSR_INITBUF | DMACSR_READ | DMACSR_RESET;
    265 
    266 	rxdma->dd_saved_next = 0;
    267 	rxdma->dd_saved_limit = 0;
    268 	rxdma->dd_saved_start = 0;
    269 	rxdma->dd_saved_stop = 0;
    270 
    271 	rxdma->dd_next = dma_buffers[0];
    272 	rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
    273 	rxdma->dd_start = 0;
    274 	rxdma->dd_stop = 0;
    275 	rxdma->dd_csr = DMACSR_SETENABLE;
    276 
    277 #if 0
    278 	DPRINTF(("en_get: blocking on rcv dma\n"));
    279 #endif
    280 
    281 	while(1) {
    282 		if (en_wait_for_intr(ENETR_DMA_INTR)) {	/* ### use timeout? */
    283 			errno = EIO;
    284  			return -1;
    285 		}
    286 
    287 		state = rxdma->dd_csr &
    288 			(DMACSR_BUSEXC | DMACSR_COMPLETE
    289 			 | DMACSR_SUPDATE | DMACSR_ENABLE);
    290 		DPRINTF(("en_get: dma state = 0x%x.\n", state));
    291 		if ((state & DMACSR_ENABLE) == 0) {
    292 			rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
    293 			break;
    294 		}
    295 
    296 		if (state & DMACSR_COMPLETE) {
    297 			DPRINTF(("en_get: ending dma sequence\n"));
    298 			rxdma->dd_csr = DMACSR_CLRCOMPLETE;
    299 		}
    300 
    301 	}
    302 
    303 	rxs = er->rxstat;
    304 
    305 	if ((state & DMACSR_COMPLETE) == 0 ||
    306 	    (rxs & EN_RX_OK) == 0) {
    307 		errno = EIO;
    308 		return -1;	/* receive failed */
    309 	}
    310 
    311 	gotpkt = rxdma->dd_saved_next;
    312 	rlen = rxdma->dd_next - rxdma->dd_saved_next;
    313 
    314 	if (gotpkt != dma_buffers[0]) {
    315 		printf("Unexpected received packet location\n");
    316 		printf("DEBUG: rxstat=%x.\n", rxs);
    317 		printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
    318 		printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
    319 		printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
    320 		printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
    321 		printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
    322 		printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
    323 		printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
    324 		printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
    325 		printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
    326 		printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
    327 		printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
    328 	}
    329 
    330 #if 0
    331 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
    332 #endif
    333 
    334  DPRINTF(("en_get: done rxstat=%x.\n", rxs));
    335 
    336 	if (rlen > len) {
    337 		DPRINTF(("en_get: buffer too small. want %d, got %d\n",
    338 			 len, rlen));
    339 		rlen = len;
    340 	}
    341 
    342 
    343 	bcopy(gotpkt, pkt, rlen);
    344 
    345 #if 0
    346 	printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
    347 			(u_long)gotpkt,(u_long)pkt,rlen);
    348 	dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
    349 	dump_pkt(pkt, rlen < 255 ? rlen : 128);
    350 #endif
    351 
    352 	return rlen;
    353 }
    354 
    355 
    356 void
    357 en_end(struct netif *a)
    358 {
    359   DPRINTF(("en_end: WARNING not doing anything\n"));
    360 }
    361 
    362 #if 0
    363 
    364 #define MKPANIC(ret,name,args) \
    365 	ret name args { panic(#name ## ": not implemented.\n"); }
    366 
    367 MKPANIC(void, en_end, (struct netif *a));
    368 
    369 /* device functions */
    370 
    371 int
    372 enopen(struct open_file *f, char count, char lun, char part)
    373 {
    374 	int error;
    375 
    376 	DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
    377 
    378 	if (count != 0 || lun != 0 || part != 0)
    379 		return EUNIT;	/* there can be exactly one ethernet */
    380 
    381 	if (open_count == 0) {
    382 		/* Find network interface. */
    383 		if ((netdev_sock = netif_open(NULL)) < 0)
    384 			return errno;
    385 		if ((error = mountroot(netdev_sock)) != 0) {
    386 			if (open_count == 0)
    387 				netif_close(netdev_sock);
    388 			return error;
    389 		}
    390 	}
    391 	open_count++;
    392 	f->f_devdata = NULL; /* ### nfs_root_node ?! */
    393 	return 0;
    394 }
    395 
    396 int
    397 enclose(struct open_file *f)
    398 {
    399 	if (open_count > 0)
    400 		if (--open_count == 0)
    401 			netif_close(netdev_sock);
    402 	f->f_devdata = NULL;
    403 	return 0;
    404 }
    405 
    406 int
    407 enstrategy(void *devdata, int rw, daddr_t dblk,
    408 	   size_t size, void *buf, size_t *rsize)
    409 {
    410 	return ENXIO;		/* wrong access method */
    411 }
    412 
    413 /* private function */
    414 
    415 static int
    416 mountroot(int sock)
    417 {
    418 	/* Mount the root directory from a boot server */
    419 #if 0
    420 	struct in_addr in = {
    421 		0xc2793418
    422 	};
    423 	u_char *res;
    424 
    425 	res = arpwhohas(socktodesc(sock), in);
    426 	panic("arpwhohas returned %s", res);
    427 #endif
    428 	/* 1. use bootp. This does most of the work for us. */
    429 	bootp(sock);
    430 
    431 	if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
    432 		return ETIMEDOUT;
    433 
    434 	printf("Using IP address: %s\n", inet_ntoa(myip));
    435 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
    436 
    437 	/* 2. mount. */
    438 	if (nfs_mount(sock, rootip, rootpath) < 0)
    439 		return errno;
    440 
    441 	return 0;
    442 }
    443 #endif
    444 
    445 static int
    446 en_wait_for_intr(int flag)
    447 {
    448   volatile int *intrstat = MON(volatile int *,MG_intrstat);
    449 
    450 	int count;
    451 
    452 	for(count = 0; count < EN_TIMEOUT; count++)
    453 		if (*intrstat & flag)
    454 			return 0;
    455 
    456 	printf("enintr: timed out.\n");
    457 	return -1;
    458 }
    459