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