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