1 /* if_en.c 4.62 82/05/27 */ 2 3 #include "en.h" 4 #include "imp.h" 5 6 /* 7 * Xerox prototype (3 Mb) Ethernet interface driver. 8 */ 9 10 #include "../h/param.h" 11 #include "../h/systm.h" 12 #include "../h/mbuf.h" 13 #include "../h/pte.h" 14 #include "../h/buf.h" 15 #include "../h/protosw.h" 16 #include "../h/socket.h" 17 #include "../h/ubareg.h" 18 #include "../h/ubavar.h" 19 #include "../h/enreg.h" 20 #include "../h/cpu.h" 21 #include "../h/mtpr.h" 22 #include "../h/vmmac.h" 23 #include "../net/in.h" 24 #include "../net/in_systm.h" 25 #include "../net/if.h" 26 #include "../net/if_en.h" 27 #include "../net/if_uba.h" 28 #include "../net/ip.h" 29 #include "../net/ip_var.h" 30 #include "../net/pup.h" 31 #include "../net/route.h" 32 #include <errno.h> 33 34 #define ENMTU (1024+512) 35 #define ENMRU (1024+512+16) /* 16 is enough to receive trailer */ 36 37 int enprobe(), enattach(), enrint(), enxint(), encollide(); 38 struct uba_device *eninfo[NEN]; 39 u_short enstd[] = { 0 }; 40 struct uba_driver endriver = 41 { enprobe, 0, enattach, 0, enstd, "en", eninfo }; 42 #define ENUNIT(x) minor(x) 43 44 int eninit(),enoutput(),enreset(); 45 46 /* 47 * Ethernet software status per interface. 48 * 49 * Each interface is referenced by a network interface structure, 50 * es_if, which the routing code uses to locate the interface. 51 * This structure contains the output queue for the interface, its address, ... 52 * We also have, for each interface, a UBA interface structure, which 53 * contains information about the UNIBUS resources held by the interface: 54 * map registers, buffered data paths, etc. Information is cached in this 55 * structure for use by the if_uba.c routines in running the interface 56 * efficiently. 57 */ 58 struct en_softc { 59 struct ifnet es_if; /* network-visible interface */ 60 struct ifuba es_ifuba; /* UNIBUS resources */ 61 short es_delay; /* current output delay */ 62 short es_mask; /* mask for current output delay */ 63 short es_lastx; /* host last transmitted to */ 64 short es_oactive; /* is output active? */ 65 short es_olen; /* length of last output */ 66 } en_softc[NEN]; 67 68 /* 69 * Do output DMA to determine interface presence and 70 * interrupt vector. DMA is too short to disturb other hosts. 71 */ 72 enprobe(reg) 73 caddr_t reg; 74 { 75 register int br, cvec; /* r11, r10 value-result */ 76 register struct endevice *addr = (struct endevice *)reg; 77 78 COUNT(ENPROBE); 79 #ifdef lint 80 br = 0; cvec = br; br = cvec; 81 enrint(0); enxint(0); encollide(0); 82 #endif 83 addr->en_istat = 0; 84 addr->en_owc = -1; 85 addr->en_oba = 0; 86 addr->en_ostat = EN_IEN|EN_GO; 87 DELAY(100000); 88 addr->en_ostat = 0; 89 #ifdef ECHACK 90 br = 0x16; 91 #endif 92 return (1); 93 } 94 95 /* 96 * Interface exists: make available by filling in network interface 97 * record. System will initialize the interface when it is ready 98 * to accept packets. 99 */ 100 enattach(ui) 101 struct uba_device *ui; 102 { 103 register struct en_softc *es = &en_softc[ui->ui_unit]; 104 register struct sockaddr_in *sin; 105 COUNT(ENATTACH); 106 107 es->es_if.if_unit = ui->ui_unit; 108 es->es_if.if_name = "en"; 109 es->es_if.if_mtu = ENMTU; 110 es->es_if.if_net = ui->ui_flags & 0xff; 111 es->es_if.if_host[0] = 112 (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff; 113 sin = (struct sockaddr_in *)&es->es_if.if_addr; 114 sin->sin_family = AF_INET; 115 sin->sin_addr = if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]); 116 sin = (struct sockaddr_in *)&es->es_if.if_broadaddr; 117 sin->sin_family = AF_INET; 118 sin->sin_addr = if_makeaddr(es->es_if.if_net, 0); 119 es->es_if.if_flags = IFF_BROADCAST; 120 es->es_if.if_init = eninit; 121 es->es_if.if_output = enoutput; 122 es->es_if.if_ubareset = enreset; 123 es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT; 124 if_attach(&es->es_if); 125 #if NIMP == 0 126 /* here's one for you john baby.... */ 127 if (ui->ui_flags &~ 0xff) 128 enlhinit(&es->es_if, (ui->ui_flags &~ 0xff) | 0x0a); 129 #endif 130 } 131 132 /* 133 * Reset of interface after UNIBUS reset. 134 * If interface is on specified uba, reset its state. 135 */ 136 enreset(unit, uban) 137 int unit, uban; 138 { 139 register struct uba_device *ui; 140 COUNT(ENRESET); 141 142 if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || 143 ui->ui_ubanum != uban) 144 return; 145 printf(" en%d", unit); 146 eninit(unit); 147 } 148 149 /* 150 * Initialization of interface; clear recorded pending 151 * operations, and reinitialize UNIBUS usage. 152 */ 153 eninit(unit) 154 int unit; 155 { 156 register struct en_softc *es = &en_softc[unit]; 157 register struct uba_device *ui = eninfo[unit]; 158 register struct endevice *addr; 159 int s; 160 161 if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, 162 sizeof (struct en_header), (int)btoc(ENMRU)) == 0) { 163 printf("en%d: can't initialize\n", unit); 164 es->es_if.if_flags &= ~IFF_UP; 165 return; 166 } 167 addr = (struct endevice *)ui->ui_addr; 168 addr->en_istat = addr->en_ostat = 0; 169 170 /* 171 * Hang a receive and start any 172 * pending writes by faking a transmit complete. 173 */ 174 s = splimp(); 175 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 176 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 177 addr->en_istat = EN_IEN|EN_GO; 178 es->es_oactive = 1; 179 es->es_if.if_flags |= IFF_UP; 180 enxint(unit); 181 splx(s); 182 if_rtinit(&es->es_if, RTF_DIRECT|RTF_UP); 183 } 184 185 int enalldelay = 0; 186 int enlastdel = 50; 187 int enlastmask = (~0) << 5; 188 189 /* 190 * Start or restart output on interface. 191 * If interface is already active, then this is a retransmit 192 * after a collision, and just restuff registers and delay. 193 * If interface is not already active, get another datagram 194 * to send off of the interface queue, and map it to the interface 195 * before starting the output. 196 */ 197 enstart(dev) 198 dev_t dev; 199 { 200 int unit = ENUNIT(dev); 201 struct uba_device *ui = eninfo[unit]; 202 register struct en_softc *es = &en_softc[unit]; 203 register struct endevice *addr; 204 struct mbuf *m; 205 int dest; 206 COUNT(ENSTART); 207 208 if (es->es_oactive) 209 goto restart; 210 211 /* 212 * Not already active: dequeue another request 213 * and map it to the UNIBUS. If no more requests, 214 * just return. 215 */ 216 IF_DEQUEUE(&es->es_if.if_snd, m); 217 if (m == 0) { 218 es->es_oactive = 0; 219 return; 220 } 221 dest = mtod(m, struct en_header *)->en_dhost; 222 es->es_olen = if_wubaput(&es->es_ifuba, m); 223 224 /* 225 * Ethernet cannot take back-to-back packets (no 226 * buffering in interface. To help avoid overrunning 227 * receivers, enforce a small delay (about 1ms) in interface: 228 * * between all packets when enalldelay 229 * * whenever last packet was broadcast 230 * * whenever this packet is to same host as last packet 231 */ 232 if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) { 233 es->es_delay = enlastdel; 234 es->es_mask = enlastmask; 235 } 236 es->es_lastx = dest; 237 238 restart: 239 /* 240 * Have request mapped to UNIBUS for transmission. 241 * Purge any stale data from this BDP, and start the otput. 242 */ 243 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 244 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); 245 addr = (struct endevice *)ui->ui_addr; 246 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; 247 addr->en_odelay = es->es_delay; 248 addr->en_owc = -((es->es_olen + 1) >> 1); 249 addr->en_ostat = EN_IEN|EN_GO; 250 es->es_oactive = 1; 251 } 252 253 /* 254 * Ethernet interface transmitter interrupt. 255 * Start another output if more data to send. 256 */ 257 enxint(unit) 258 int unit; 259 { 260 register struct uba_device *ui = eninfo[unit]; 261 register struct en_softc *es = &en_softc[unit]; 262 register struct endevice *addr = (struct endevice *)ui->ui_addr; 263 COUNT(ENXINT); 264 265 if (es->es_oactive == 0) 266 return; 267 if (es->es_mask && (addr->en_ostat&EN_OERROR)) { 268 es->es_if.if_oerrors++; 269 if (es->es_if.if_oerrors % 100 == 0) 270 printf("en%d: += 100 output errors\n", unit); 271 endocoll(unit); 272 return; 273 } 274 es->es_if.if_opackets++; 275 es->es_oactive = 0; 276 es->es_delay = 0; 277 es->es_mask = ~0; 278 if (es->es_ifuba.ifu_xtofree) { 279 m_freem(es->es_ifuba.ifu_xtofree); 280 es->es_ifuba.ifu_xtofree = 0; 281 } 282 if (es->es_if.if_snd.ifq_head == 0) { 283 es->es_lastx = 256; /* putatively illegal */ 284 return; 285 } 286 enstart(unit); 287 } 288 289 /* 290 * Collision on ethernet interface. Do exponential 291 * backoff, and retransmit. If have backed off all 292 * the way print warning diagnostic, and drop packet. 293 */ 294 encollide(unit) 295 int unit; 296 { 297 struct en_softc *es = &en_softc[unit]; 298 COUNT(ENCOLLIDE); 299 300 es->es_if.if_collisions++; 301 if (es->es_oactive == 0) 302 return; 303 endocoll(unit); 304 } 305 306 endocoll(unit) 307 int unit; 308 { 309 register struct en_softc *es = &en_softc[unit]; 310 311 /* 312 * Es_mask is a 16 bit number with n low zero bits, with 313 * n the number of backoffs. When es_mask is 0 we have 314 * backed off 16 times, and give up. 315 */ 316 if (es->es_mask == 0) { 317 printf("en%d: send error\n", unit); 318 enxint(unit); 319 return; 320 } 321 /* 322 * Another backoff. Restart with delay based on n low bits 323 * of the interval timer. 324 */ 325 es->es_mask <<= 1; 326 es->es_delay = mfpr(ICR) &~ es->es_mask; 327 enstart(unit); 328 } 329 330 struct sockaddr_pup pupsrc = { AF_PUP }; 331 struct sockaddr_pup pupdst = { AF_PUP }; 332 struct sockproto pupproto = { PF_PUP }; 333 /* 334 * Ethernet interface receiver interrupt. 335 * If input error just drop packet. 336 * Otherwise purge input buffered data path and examine 337 * packet to determine type. If can't determine length 338 * from type, then have to drop packet. Othewise decapsulate 339 * packet based on type and pass to type specific higher-level 340 * input routine. 341 */ 342 enrint(unit) 343 int unit; 344 { 345 register struct en_softc *es = &en_softc[unit]; 346 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 347 register struct en_header *en; 348 struct mbuf *m; 349 int len, plen; short resid; 350 register struct ifqueue *inq; 351 int off; 352 COUNT(ENRINT); 353 354 es->es_if.if_ipackets++; 355 356 /* 357 * Purge BDP; drop if input error indicated. 358 */ 359 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 360 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 361 if (addr->en_istat&EN_IERROR) { 362 es->es_if.if_ierrors++; 363 if (es->es_if.if_ierrors % 100 == 0) 364 printf("en%d: += 100 input errors\n", unit); 365 goto setup; 366 } 367 368 /* 369 * Calculate input data length. 370 * Get pointer to ethernet header (in input buffer). 371 * Deal with trailer protocol: if type is PUP trailer 372 * get true type from first 16-bit word past data. 373 * Remember that type was trailer by setting off. 374 */ 375 resid = addr->en_iwc; 376 if (resid) 377 resid |= 0176000; 378 len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1; 379 len -= sizeof (struct en_header); 380 if (len > ENMRU) 381 goto setup; /* sanity */ 382 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 383 #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 384 if (en->en_type >= ENPUP_TRAIL && 385 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { 386 off = (en->en_type - ENPUP_TRAIL) * 512; 387 if (off > ENMTU) 388 goto setup; /* sanity */ 389 en->en_type = *endataaddr(en, off, u_short *); 390 resid = *(endataaddr(en, off+2, u_short *)); 391 if (off + resid > len) 392 goto setup; /* sanity */ 393 len = off + resid; 394 } else 395 off = 0; 396 if (len == 0) 397 goto setup; 398 /* 399 * Pull packet off interface. Off is nonzero if packet 400 * has trailing header; if_rubaget will then force this header 401 * information to be at the front, but we still have to drop 402 * the type and length which are at the front of any trailer data. 403 */ 404 m = if_rubaget(&es->es_ifuba, len, off); 405 if (m == 0) 406 goto setup; 407 if (off) { 408 m->m_off += 2 * sizeof (u_short); 409 m->m_len -= 2 * sizeof (u_short); 410 } 411 switch (en->en_type) { 412 413 #ifdef INET 414 case ENPUP_IPTYPE: 415 schednetisr(NETISR_IP); 416 inq = &ipintrq; 417 break; 418 #endif 419 #ifdef PUP 420 case ENPUP_PUPTYPE: { 421 struct pup_header *pup = mtod(m, struct pup_header *); 422 423 pupproto.sp_protocol = pup->pup_type; 424 pupdst.spup_addr = pup->pup_dport; 425 pupsrc.spup_addr = pup->pup_sport; 426 raw_input(m, &pupproto, (struct sockaddr *)&pupsrc, 427 (struct sockaddr *)&pupdst); 428 goto setup; 429 } 430 #endif 431 default: 432 m_freem(m); 433 goto setup; 434 } 435 436 if (IF_QFULL(inq)) { 437 IF_DROP(inq); 438 m_freem(m); 439 } else 440 IF_ENQUEUE(inq, m); 441 442 setup: 443 /* 444 * Reset for next packet. 445 */ 446 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 447 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 448 addr->en_istat = EN_IEN|EN_GO; 449 } 450 451 /* 452 * Ethernet output routine. 453 * Encapsulate a packet of type family for the local net. 454 * Use trailer local net encapsulation if enough data in first 455 * packet leaves a multiple of 512 bytes of data in remainder. 456 */ 457 enoutput(ifp, m0, dst) 458 struct ifnet *ifp; 459 struct mbuf *m0; 460 struct sockaddr *dst; 461 { 462 int type, dest, s, error; 463 register struct mbuf *m = m0; 464 register struct en_header *en; 465 register int off; 466 467 COUNT(ENOUTPUT); 468 switch (dst->sa_family) { 469 470 #ifdef INET 471 case AF_INET: 472 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 473 if (dest & 0x00ffff00) { 474 error = EPERM; /* ??? */ 475 goto bad; 476 } 477 dest = (dest >> 24) & 0xff; 478 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 479 if (off > 0 && (off & 0x1ff) == 0 && 480 m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 481 type = ENPUP_TRAIL + (off>>9); 482 m->m_off -= 2 * sizeof (u_short); 483 m->m_len += 2 * sizeof (u_short); 484 *mtod(m, u_short *) = ENPUP_IPTYPE; 485 *(mtod(m, u_short *) + 1) = m->m_len; 486 goto gottrailertype; 487 } 488 type = ENPUP_IPTYPE; 489 off = 0; 490 goto gottype; 491 #endif 492 #ifdef PUP 493 case AF_PUP: 494 dest = ((struct sockaddr_pup *)dst)->spup_addr.pp_host; 495 type = ENPUP_PUPTYPE; 496 off = 0; 497 goto gottype; 498 #endif 499 500 default: 501 printf("en%d: can't handle af%d\n", ifp->if_unit, 502 dst->sa_family); 503 error = EAFNOSUPPORT; 504 goto bad; 505 } 506 507 gottrailertype: 508 /* 509 * Packet to be sent as trailer: move first packet 510 * (control information) to end of chain. 511 */ 512 while (m->m_next) 513 m = m->m_next; 514 m->m_next = m0; 515 m = m0->m_next; 516 m0->m_next = 0; 517 m0 = m; 518 519 gottype: 520 /* 521 * Add local net header. If no space in first mbuf, 522 * allocate another. 523 */ 524 if (m->m_off > MMAXOFF || 525 MMINOFF + sizeof (struct en_header) > m->m_off) { 526 m = m_get(M_DONTWAIT); 527 if (m == 0) { 528 error = ENOBUFS; 529 goto bad; 530 } 531 m->m_next = m0; 532 m->m_off = MMINOFF; 533 m->m_len = sizeof (struct en_header); 534 } else { 535 m->m_off -= sizeof (struct en_header); 536 m->m_len += sizeof (struct en_header); 537 } 538 en = mtod(m, struct en_header *); 539 en->en_shost = ifp->if_host[0]; 540 en->en_dhost = dest; 541 en->en_type = type; 542 543 /* 544 * Queue message on interface, and start output if interface 545 * not yet active. 546 */ 547 s = splimp(); 548 if (IF_QFULL(&ifp->if_snd)) { 549 IF_DROP(&ifp->if_snd); 550 error = ENOBUFS; 551 goto qfull; 552 } 553 IF_ENQUEUE(&ifp->if_snd, m); 554 if (en_softc[ifp->if_unit].es_oactive == 0) 555 enstart(ifp->if_unit); 556 splx(s); 557 return (0); 558 qfull: 559 m0 = m; 560 splx(s); 561 bad: 562 m_freem(m0); 563 return (error); 564 } 565 566 #if NIMP == 0 && NEN > 0 567 /* 568 * Logical host interface driver. 569 * Allows host to appear as an ARPAnet 570 * logical host. Must also have routing 571 * table entry set up to forward packets 572 * to appropriate gateway on localnet. 573 */ 574 575 struct ifnet enlhif; 576 int looutput(); 577 578 /* 579 * Called by localnet interface to allow logical 580 * host interface to "attach", it's purpose 581 * is simply to establish the host's arpanet address. 582 */ 583 enlhinit(esifp, addr) 584 struct ifnet *esifp; 585 int addr; 586 { 587 register struct ifnet *ifp = &enlhif; 588 register struct sockaddr_in *sin; 589 590 COUNT(ENLHINIT); 591 ifp->if_name = "lh"; 592 ifp->if_mtu = ENMTU; 593 sin = (struct sockaddr_in *)&ifp->if_addr; 594 sin->sin_family = AF_INET; 595 sin->sin_addr.s_addr = addr; 596 ifp->if_net = sin->sin_addr.s_net; 597 ifp->if_flags = IFF_UP|IFF_POINTOPOINT; 598 ifp->if_dstaddr = esifp->if_addr; 599 ifp->if_output = looutput; 600 if_attach(ifp); 601 rtinit(&ifp->if_addr, &ifp->if_addr, RTF_UP|RTF_DIRECT|RTF_HOST); 602 } 603 #endif 604