1 /* $NetBSD: en.c,v 1.20 2023/02/12 08:25:09 tsutsui 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 #include "samachdep.h" 41 42 #include <stand.h> 43 #include <netif.h> 44 #include <net.h> 45 #include <nfs.h> 46 47 #include <lib/libkern/libkern.h> 48 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(struct netif *, void *); 62 int en_probe(struct netif *, void *); 63 void en_init(struct iodesc *, void *); 64 int en_get(struct iodesc *, void *, size_t, saseconds_t); 65 int en_put(struct iodesc *, void *, size_t); 66 void en_end(struct netif *); 67 68 static int en_wait_for_intr(int); 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, sizeof(en_ifs) / sizeof(en_ifs[0]) 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 /* if (turbo) */ 127 /* er->reset = 0; */ 128 129 er->txmask = 0; 130 er->txstat = 0xff; 131 if (turbo) 132 er->txmode = 0 | EN_TMD_COLLSHIFT; 133 else 134 er->txmode = EN_TMD_LB_DISABLE; 135 136 /* setup for bnc/tp */ 137 if (!turbo) { 138 DPRINTF (("en_media: %s\n", 139 (bmap_chip[13] & 0x20000000) ? "BNC" : "TP")); 140 if (!(bmap_chip[13] & 0x20000000)) { 141 bmap_chip[12] |= 0x90000000; 142 bmap_chip[13] |= (0x80000000|0x10000000); /* TP */ 143 } 144 } 145 146 /* if (turbo) { */ 147 /* er->txmode |= EN_TMD_COLLSHIFT; */ 148 /* } else { */ 149 /* er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */ 150 /* } */ 151 152 er->rxmask = 0; 153 er->rxstat = 0xff; 154 if (turbo) 155 er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL; 156 else 157 er->rxmode = EN_RMD_RECV_NORMAL; 158 for (i=0; i<6; i++) 159 er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i]; 160 161 DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n", 162 desc->myea[0],desc->myea[1],desc->myea[2], 163 desc->myea[3],desc->myea[4],desc->myea[5])); 164 165 /* if (!turbo) */ 166 er->reset = 0; 167 } 168 169 #if 0 170 /* ### remove this when things work! */ 171 #define XCHR(x) hexdigits[(x) & 0xf] 172 void 173 dump_pkt(unsigned char *pkt, size_t len) 174 { 175 size_t i, j; 176 177 printf("0000: "); 178 for(i=0; i<len; i++) { 179 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i])); 180 if ((i+1) % 16 == 0) { 181 printf(" %c", '"'); 182 for(j=0; j<16; j++) 183 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.'); 184 printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12), 185 XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1)); 186 } 187 } 188 printf("\n"); 189 } 190 #endif 191 192 int 193 en_put(struct iodesc *desc, void *pkt, size_t len) 194 { 195 volatile struct en_regs *er; 196 volatile struct dma_dev *txdma; 197 int state, txs; 198 int retries; 199 200 DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt)); 201 #if 0 202 dump_pkt(pkt,len); 203 #endif 204 205 er = (struct en_regs *)P_ENET; 206 txdma = (struct dma_dev *)P_ENETX_CSR; 207 208 DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr)); 209 210 if (len > 1600) { 211 errno = EINVAL; 212 return -1; 213 } 214 215 if (!turbo) { 216 while ((er->txstat & EN_TXS_READY) == 0) 217 printf("en: tx not ready\n"); 218 } 219 220 for (retries = 0; retries < EN_RETRIES; retries++) { 221 er->txstat = 0xff; 222 memcpy(dma_buffers[0], pkt, len); 223 txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) | 224 DMACSR_RESET | DMACSR_WRITE; 225 txdma->dd_csr = 0; 226 txdma->dd_next/* _initbuf */ = dma_buffers[0]; 227 txdma->dd_start = (turbo ? dma_buffers[0] : 0); 228 txdma->dd_limit = ENDMA_ENDALIGN(char *, dma_buffers[0]+len); 229 txdma->dd_stop = 0; 230 txdma->dd_csr = DMACSR_SETENABLE; 231 if (turbo) 232 er->txmode |= 0x80; 233 234 while (1) { 235 if (en_wait_for_intr(ENETX_DMA_INTR)) { 236 printf("en_put: timed out\n"); 237 errno = EIO; 238 return -1; 239 } 240 241 state = txdma->dd_csr & 242 (DMACSR_BUSEXC | DMACSR_COMPLETE 243 | DMACSR_SUPDATE | DMACSR_ENABLE); 244 245 #if 01 246 DPRINTF(("en_put: DMA state = 0x%x.\n", state)); 247 #endif 248 if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC)) { 249 txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE; 250 break; 251 } 252 } 253 254 txs = er->txstat; 255 256 #if 01 257 DPRINTF(("en_put: done txstat=%x.\n", txs)); 258 #endif 259 260 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR) 261 if ((state & DMACSR_COMPLETE) == 0 || 262 (txs & EN_TXS_ERROR) != 0) { 263 errno = EIO; 264 return -1; 265 } 266 if ((txs & EN_TXS_COLLERR) == 0) 267 return len; /* success */ 268 } 269 270 errno = EIO; /* too many retries */ 271 return -1; 272 } 273 274 int 275 en_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout) 276 { 277 volatile struct en_regs *er; 278 volatile struct dma_dev *rxdma; 279 #if 0 280 volatile struct dma_dev *txdma; 281 #endif 282 int state, rxs; 283 size_t rlen; 284 char *gotpkt; 285 286 rxdma = (struct dma_dev *)P_ENETR_CSR; 287 #if 0 288 txdma = (struct dma_dev *)P_ENETX_CSR; 289 #endif 290 er = (struct en_regs *)P_ENET; 291 292 DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr)); 293 294 er->rxstat = 0xff; 295 296 /* this is mouse's code now ... still doesn't work :( */ 297 /* The previous comment is now a lie, this does work 298 * Darrin B Jewell <jewell@mit.edu> Sat Jan 24 21:44:56 1998 299 */ 300 301 rxdma->dd_csr = 0; 302 rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) | 303 DMACSR_READ | DMACSR_RESET; 304 305 if (!turbo) { 306 rxdma->dd_saved_next = 0; 307 rxdma->dd_saved_limit = 0; 308 rxdma->dd_saved_start = 0; 309 rxdma->dd_saved_stop = 0; 310 } else { 311 rxdma->dd_saved_next = dma_buffers[0]; 312 } 313 314 rxdma->dd_next = dma_buffers[0]; 315 rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE); 316 #if 0 317 if (turbo) { 318 /* !!! not a typo: txdma */ 319 txdma->dd_stop = dma_buffers[0]; 320 } 321 #endif 322 rxdma->dd_start = 0; 323 rxdma->dd_stop = 0; 324 rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ; 325 if (turbo) 326 er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL; 327 else 328 er->rxmode = EN_RMD_RECV_NORMAL; 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 rxs = er->rxstat; 354 355 if ((state & DMACSR_COMPLETE) == 0 || 356 (rxs & EN_RX_OK) == 0) { 357 errno = EIO; 358 return -1; /* receive failed */ 359 } 360 361 if (turbo) { 362 gotpkt = rxdma->dd_saved_next; 363 rlen = rxdma->dd_next - rxdma->dd_saved_next; 364 } else { 365 gotpkt = rxdma->dd_saved_next; 366 rlen = rxdma->dd_next - rxdma->dd_saved_next; 367 } 368 369 if (gotpkt != dma_buffers[0]) { 370 printf("Unexpected received packet location\n"); 371 printf("DEBUG: rxstat=%x.\n", rxs); 372 printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len); 373 printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr); 374 printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next); 375 printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit); 376 printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start); 377 printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop); 378 printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next); 379 printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit); 380 printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start); 381 printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop); 382 printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf); 383 } 384 385 #if 0 386 dump_pkt(gotpkt, rlen < 255 ? rlen : 128); 387 #endif 388 389 DPRINTF(("en_get: done rxstat=%x.\n", rxs)); 390 391 if (rlen > len) { 392 DPRINTF(("en_get: buffer too small. want %d, got %d\n", 393 len, rlen)); 394 rlen = len; 395 } 396 397 memcpy(pkt, gotpkt, rlen); 398 399 #if 0 400 printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n", 401 (u_long)gotpkt,(u_long)pkt,rlen); 402 dump_pkt(gotpkt, rlen < 255 ? rlen : 128); 403 dump_pkt(pkt, rlen < 255 ? rlen : 128); 404 #endif 405 406 return rlen; 407 } 408 409 410 void 411 en_end(struct netif *a) 412 { 413 DPRINTF(("en_end: WARNING not doing anything\n")); 414 } 415 416 #if 0 417 418 #define MKPANIC(ret,name,args) \ 419 ret name args { panic(#name ## ": not implemented.\n"); } 420 421 MKPANIC(void, en_end, (struct netif *a)); 422 423 /* device functions */ 424 425 int 426 enopen(struct open_file *f, char count, char lun, char part) 427 { 428 int error; 429 430 DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part)); 431 432 if (count != 0 || lun != 0 || part != 0) 433 return EUNIT; /* there can be exactly one ethernet */ 434 435 if (open_count == 0) { 436 /* Find network interface. */ 437 if ((netdev_sock = netif_open(NULL)) < 0) 438 return errno; 439 if ((error = mountroot(netdev_sock)) != 0) { 440 if (open_count == 0) 441 netif_close(netdev_sock); 442 return error; 443 } 444 } 445 open_count++; 446 f->f_devdata = NULL; /* ### nfs_root_node ?! */ 447 return 0; 448 } 449 450 int 451 enclose(struct open_file *f) 452 { 453 if (open_count > 0) 454 if (--open_count == 0) 455 netif_close(netdev_sock); 456 f->f_devdata = NULL; 457 return 0; 458 } 459 460 int 461 enstrategy(void *devdata, int rw, daddr_t dblk, 462 size_t size, void *buf, size_t *rsize) 463 { 464 return ENXIO; /* wrong access method */ 465 } 466 467 /* private function */ 468 469 static int 470 mountroot(int sock) 471 { 472 /* Mount the root directory from a boot server */ 473 #if 0 474 struct in_addr in = { 475 0xc2793418 476 }; 477 u_char *res; 478 479 res = arpwhohas(socktodesc(sock), in); 480 panic("arpwhohas returned %s", res); 481 #endif 482 /* 1. use bootp. This does most of the work for us. */ 483 bootp(sock); 484 485 if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0') 486 return ETIMEDOUT; 487 488 printf("Using IP address: %s\n", inet_ntoa(myip)); 489 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 490 491 /* 2. mount. */ 492 if (nfs_mount(sock, rootip, rootpath) < 0) 493 return errno; 494 495 return 0; 496 } 497 #endif 498 499 static int 500 en_wait_for_intr(int flag) 501 { 502 volatile int *intrstat = MON(volatile int *, MG_intrstat); 503 504 int count; 505 506 for (count = 0; count < EN_TIMEOUT; count++) 507 if (*intrstat & flag) 508 return 0; 509 510 return -1; 511 } 512