1 /* if_en.c 6.2 84/04/12 */ 2 3 #include "en.h" 4 5 /* 6 * Xerox prototype (3 Mb) Ethernet interface driver. 7 */ 8 #include "../machine/pte.h" 9 10 #include "../h/param.h" 11 #include "../h/systm.h" 12 #include "../h/mbuf.h" 13 #include "../h/buf.h" 14 #include "../h/protosw.h" 15 #include "../h/socket.h" 16 #include "../h/vmmac.h" 17 #include "../h/errno.h" 18 #include "../h/ioctl.h" 19 20 #include "../net/if.h" 21 #include "../net/netisr.h" 22 #include "../net/route.h" 23 #include "../netinet/in.h" 24 #include "../netinet/in_systm.h" 25 #include "../netinet/ip.h" 26 #include "../netinet/ip_var.h" 27 #include "../netpup/pup.h" 28 #include "../netpup/ether.h" 29 30 #include "../vax/cpu.h" 31 #include "../vax/mtpr.h" 32 #include "../vaxif/if_en.h" 33 #include "../vaxif/if_enreg.h" 34 #include "../vaxif/if_uba.h" 35 #include "../vaxuba/ubareg.h" 36 #include "../vaxuba/ubavar.h" 37 38 #define ENMTU (1024+512) 39 #define ENMRU (1024+512+16) /* 16 is enough to receive trailer */ 40 41 int enprobe(), enattach(), enrint(), enxint(), encollide(); 42 struct uba_device *eninfo[NEN]; 43 u_short enstd[] = { 0 }; 44 struct uba_driver endriver = 45 { enprobe, 0, enattach, 0, enstd, "en", eninfo }; 46 #define ENUNIT(x) minor(x) 47 48 int eninit(),enoutput(),enreset(),enioctl(); 49 50 #ifdef notdef 51 /* 52 * If you need to byte swap IP's in the system, define 53 * this and do a SIOCSIFFLAGS at boot time. 54 */ 55 #define ENF_SWABIPS 0x100 56 #endif 57 58 /* 59 * Ethernet software status per interface. 60 * 61 * Each interface is referenced by a network interface structure, 62 * es_if, which the routing code uses to locate the interface. 63 * This structure contains the output queue for the interface, its address, ... 64 * We also have, for each interface, a UBA interface structure, which 65 * contains information about the UNIBUS resources held by the interface: 66 * map registers, buffered data paths, etc. Information is cached in this 67 * structure for use by the if_uba.c routines in running the interface 68 * efficiently. 69 */ 70 struct en_softc { 71 struct ifnet es_if; /* network-visible interface */ 72 struct ifuba es_ifuba; /* UNIBUS resources */ 73 short es_host; /* hardware host number */ 74 short es_delay; /* current output delay */ 75 short es_mask; /* mask for current output delay */ 76 short es_lastx; /* host last transmitted to */ 77 short es_oactive; /* is output active? */ 78 short es_olen; /* length of last output */ 79 } en_softc[NEN]; 80 81 /* 82 * Do output DMA to determine interface presence and 83 * interrupt vector. DMA is too short to disturb other hosts. 84 */ 85 enprobe(reg) 86 caddr_t reg; 87 { 88 register int br, cvec; /* r11, r10 value-result */ 89 register struct endevice *addr = (struct endevice *)reg; 90 91 #ifdef lint 92 br = 0; cvec = br; br = cvec; 93 enrint(0); enxint(0); encollide(0); 94 #endif 95 addr->en_istat = 0; 96 addr->en_owc = -1; 97 addr->en_oba = 0; 98 addr->en_ostat = EN_IEN|EN_GO; 99 DELAY(100000); 100 addr->en_ostat = 0; 101 return (1); 102 } 103 104 /* 105 * Interface exists: make available by filling in network interface 106 * record. System will initialize the interface when it is ready 107 * to accept packets. 108 */ 109 enattach(ui) 110 struct uba_device *ui; 111 { 112 register struct en_softc *es = &en_softc[ui->ui_unit]; 113 114 es->es_if.if_unit = ui->ui_unit; 115 es->es_if.if_name = "en"; 116 es->es_if.if_mtu = ENMTU; 117 es->es_if.if_init = eninit; 118 es->es_if.if_output = enoutput; 119 es->es_if.if_ioctl = enioctl; 120 es->es_if.if_reset = enreset; 121 es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT; 122 #if defined(VAX750) 123 /* don't chew up 750 bdp's */ 124 if (cpu == VAX_750 && ui->ui_unit > 0) 125 es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP; 126 #endif 127 if_attach(&es->es_if); 128 } 129 130 /* 131 * Reset of interface after UNIBUS reset. 132 * If interface is on specified uba, reset its state. 133 */ 134 enreset(unit, uban) 135 int unit, uban; 136 { 137 register struct uba_device *ui; 138 139 if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || 140 ui->ui_ubanum != uban) 141 return; 142 printf(" en%d", unit); 143 eninit(unit); 144 } 145 146 /* 147 * Initialization of interface; clear recorded pending 148 * operations, and reinitialize UNIBUS usage. 149 */ 150 eninit(unit) 151 int unit; 152 { 153 register struct en_softc *es = &en_softc[unit]; 154 register struct uba_device *ui = eninfo[unit]; 155 register struct endevice *addr; 156 struct sockaddr_in *sin = (struct sockaddr_in *)&es->es_if.if_addr; 157 int s; 158 159 if (in_netof(sin->sin_addr) == 0) 160 return; 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|IFF_RUNNING; 180 enxint(unit); 181 splx(s); 182 if_rtinit(&es->es_if, 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 register struct en_header *en; 205 struct mbuf *m; 206 int dest; 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 en = mtod(m, struct en_header *); 222 dest = en->en_dhost; 223 en->en_shost = es->es_host; 224 es->es_olen = if_wubaput(&es->es_ifuba, m); 225 #ifdef ENF_SWABIPS 226 /* 227 * The Xerox interface does word at a time DMA, so 228 * someone must do byte swapping of user data if high 229 * and low ender machines are to communicate. It doesn't 230 * belong here, but certain people depend on it, so... 231 * 232 * Should swab everybody, but this is a kludge anyway. 233 */ 234 if (es->es_if.if_flags & ENF_SWABIPS) { 235 en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr; 236 if (en->en_type == ENTYPE_IP) 237 enswab((caddr_t)(en + 1), (caddr_t)(en + 1), 238 es->es_olen - sizeof (struct en_header) + 1); 239 } 240 #endif 241 242 /* 243 * Ethernet cannot take back-to-back packets (no 244 * buffering in interface. To help avoid overrunning 245 * receivers, enforce a small delay (about 1ms) in interface: 246 * * between all packets when enalldelay 247 * * whenever last packet was broadcast 248 * * whenever this packet is to same host as last packet 249 */ 250 if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) { 251 es->es_delay = enlastdel; 252 es->es_mask = enlastmask; 253 } 254 es->es_lastx = dest; 255 256 restart: 257 /* 258 * Have request mapped to UNIBUS for transmission. 259 * Purge any stale data from this BDP, and start the otput. 260 */ 261 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 262 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); 263 addr = (struct endevice *)ui->ui_addr; 264 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; 265 addr->en_odelay = es->es_delay; 266 addr->en_owc = -((es->es_olen + 1) >> 1); 267 addr->en_ostat = EN_IEN|EN_GO; 268 es->es_oactive = 1; 269 } 270 271 /* 272 * Ethernet interface transmitter interrupt. 273 * Start another output if more data to send. 274 */ 275 enxint(unit) 276 int unit; 277 { 278 register struct uba_device *ui = eninfo[unit]; 279 register struct en_softc *es = &en_softc[unit]; 280 register struct endevice *addr = (struct endevice *)ui->ui_addr; 281 282 if (es->es_oactive == 0) 283 return; 284 if (es->es_mask && (addr->en_ostat&EN_OERROR)) { 285 es->es_if.if_oerrors++; 286 endocoll(unit); 287 return; 288 } 289 es->es_if.if_opackets++; 290 es->es_oactive = 0; 291 es->es_delay = 0; 292 es->es_mask = ~0; 293 if (es->es_ifuba.ifu_xtofree) { 294 m_freem(es->es_ifuba.ifu_xtofree); 295 es->es_ifuba.ifu_xtofree = 0; 296 } 297 if (es->es_if.if_snd.ifq_head == 0) { 298 es->es_lastx = 256; /* putatively illegal */ 299 return; 300 } 301 enstart(unit); 302 } 303 304 /* 305 * Collision on ethernet interface. Do exponential 306 * backoff, and retransmit. If have backed off all 307 * the way print warning diagnostic, and drop packet. 308 */ 309 encollide(unit) 310 int unit; 311 { 312 struct en_softc *es = &en_softc[unit]; 313 314 es->es_if.if_collisions++; 315 if (es->es_oactive == 0) 316 return; 317 endocoll(unit); 318 } 319 320 endocoll(unit) 321 int unit; 322 { 323 register struct en_softc *es = &en_softc[unit]; 324 325 /* 326 * Es_mask is a 16 bit number with n low zero bits, with 327 * n the number of backoffs. When es_mask is 0 we have 328 * backed off 16 times, and give up. 329 */ 330 if (es->es_mask == 0) { 331 printf("en%d: send error\n", unit); 332 enxint(unit); 333 return; 334 } 335 /* 336 * Another backoff. Restart with delay based on n low bits 337 * of the interval timer. 338 */ 339 es->es_mask <<= 1; 340 es->es_delay = mfpr(ICR) &~ es->es_mask; 341 enstart(unit); 342 } 343 344 #ifdef notdef 345 struct sockproto enproto = { AF_ETHERLINK }; 346 struct sockaddr_en endst = { AF_ETHERLINK }; 347 struct sockaddr_en ensrc = { AF_ETHERLINK }; 348 #endif 349 /* 350 * Ethernet interface receiver interrupt. 351 * If input error just drop packet. 352 * Otherwise purge input buffered data path and examine 353 * packet to determine type. If can't determine length 354 * from type, then have to drop packet. Othewise decapsulate 355 * packet based on type and pass to type specific higher-level 356 * input routine. 357 */ 358 enrint(unit) 359 int unit; 360 { 361 register struct en_softc *es = &en_softc[unit]; 362 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 363 register struct en_header *en; 364 struct mbuf *m; 365 int len; short resid; 366 register struct ifqueue *inq; 367 int off; 368 369 es->es_if.if_ipackets++; 370 371 /* 372 * Purge BDP; drop if input error indicated. 373 */ 374 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 375 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 376 if (addr->en_istat&EN_IERROR) { 377 es->es_if.if_ierrors++; 378 goto setup; 379 } 380 381 /* 382 * Calculate input data length. 383 * Get pointer to ethernet header (in input buffer). 384 * Deal with trailer protocol: if type is PUP trailer 385 * get true type from first 16-bit word past data. 386 * Remember that type was trailer by setting off. 387 */ 388 resid = addr->en_iwc; 389 if (resid) 390 resid |= 0176000; 391 len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1; 392 len -= sizeof (struct en_header); 393 if (len > ENMRU) 394 goto setup; /* sanity */ 395 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 396 en->en_type = ntohs(en->en_type); 397 #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 398 if (en->en_type >= ENTYPE_TRAIL && 399 en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) { 400 off = (en->en_type - ENTYPE_TRAIL) * 512; 401 if (off > ENMTU) 402 goto setup; /* sanity */ 403 en->en_type = ntohs(*endataaddr(en, off, u_short *)); 404 resid = ntohs(*(endataaddr(en, off+2, u_short *))); 405 if (off + resid > len) 406 goto setup; /* sanity */ 407 len = off + resid; 408 } else 409 off = 0; 410 if (len == 0) 411 goto setup; 412 #ifdef ENF_SWABIPS 413 if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP) 414 enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len); 415 #endif 416 /* 417 * Pull packet off interface. Off is nonzero if packet 418 * has trailing header; if_rubaget will then force this header 419 * information to be at the front, but we still have to drop 420 * the type and length which are at the front of any trailer data. 421 */ 422 m = if_rubaget(&es->es_ifuba, len, off); 423 if (m == 0) 424 goto setup; 425 if (off) { 426 m->m_off += 2 * sizeof (u_short); 427 m->m_len -= 2 * sizeof (u_short); 428 } 429 switch (en->en_type) { 430 431 #ifdef INET 432 case ENTYPE_IP: 433 schednetisr(NETISR_IP); 434 inq = &ipintrq; 435 break; 436 #endif 437 #ifdef PUP 438 case ENTYPE_PUP: 439 rpup_input(m); 440 goto setup; 441 #endif 442 default: 443 #ifdef notdef 444 enproto.sp_protocol = en->en_type; 445 endst.sen_host = en->en_dhost; 446 endst.sen_net = ensrc.sen_net = es->es_if.if_net; 447 ensrc.sen_host = en->en_shost; 448 raw_input(m, &enproto, 449 (struct sockaddr *)&ensrc, (struct sockaddr *)&endst); 450 #else 451 m_freem(m); 452 #endif 453 goto setup; 454 } 455 456 if (IF_QFULL(inq)) { 457 IF_DROP(inq); 458 m_freem(m); 459 } else 460 IF_ENQUEUE(inq, m); 461 462 setup: 463 /* 464 * Reset for next packet. 465 */ 466 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 467 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 468 addr->en_istat = EN_IEN|EN_GO; 469 } 470 471 /* 472 * Ethernet output routine. 473 * Encapsulate a packet of type family for the local net. 474 * Use trailer local net encapsulation if enough data in first 475 * packet leaves a multiple of 512 bytes of data in remainder. 476 */ 477 enoutput(ifp, m0, dst) 478 struct ifnet *ifp; 479 struct mbuf *m0; 480 struct sockaddr *dst; 481 { 482 int type, dest, s, error; 483 register struct mbuf *m = m0; 484 register struct en_header *en; 485 register int off; 486 487 switch (dst->sa_family) { 488 489 #ifdef INET 490 case AF_INET: 491 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 492 if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) { 493 error = EPERM; /* ??? */ 494 goto bad; 495 } 496 dest = (dest >> 24) & 0xff; 497 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 498 /* need per host negotiation */ 499 if ((ifp->if_flags & IFF_NOTRAILERS) == 0) 500 if (off > 0 && (off & 0x1ff) == 0 && 501 m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 502 type = ENTYPE_TRAIL + (off>>9); 503 m->m_off -= 2 * sizeof (u_short); 504 m->m_len += 2 * sizeof (u_short); 505 *mtod(m, u_short *) = htons((u_short)ENTYPE_IP); 506 *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len); 507 goto gottrailertype; 508 } 509 type = ENTYPE_IP; 510 off = 0; 511 goto gottype; 512 #endif 513 #ifdef PUP 514 case AF_PUP: 515 dest = ((struct sockaddr_pup *)dst)->spup_host; 516 type = ENTYPE_PUP; 517 off = 0; 518 goto gottype; 519 #endif 520 521 #ifdef notdef 522 case AF_ETHERLINK: 523 goto gotheader; 524 #endif 525 526 default: 527 printf("en%d: can't handle af%d\n", ifp->if_unit, 528 dst->sa_family); 529 error = EAFNOSUPPORT; 530 goto bad; 531 } 532 533 gottrailertype: 534 /* 535 * Packet to be sent as trailer: move first packet 536 * (control information) to end of chain. 537 */ 538 while (m->m_next) 539 m = m->m_next; 540 m->m_next = m0; 541 m = m0->m_next; 542 m0->m_next = 0; 543 m0 = m; 544 545 gottype: 546 /* 547 * Add local net header. If no space in first mbuf, 548 * allocate another. 549 */ 550 if (m->m_off > MMAXOFF || 551 MMINOFF + sizeof (struct en_header) > m->m_off) { 552 m = m_get(M_DONTWAIT, MT_HEADER); 553 if (m == 0) { 554 error = ENOBUFS; 555 goto bad; 556 } 557 m->m_next = m0; 558 m->m_off = MMINOFF; 559 m->m_len = sizeof (struct en_header); 560 } else { 561 m->m_off -= sizeof (struct en_header); 562 m->m_len += sizeof (struct en_header); 563 } 564 en = mtod(m, struct en_header *); 565 /* add en_shost later */ 566 en->en_dhost = dest; 567 en->en_type = htons((u_short)type); 568 569 gotheader: 570 /* 571 * Queue message on interface, and start output if interface 572 * not yet active. 573 */ 574 s = splimp(); 575 if (IF_QFULL(&ifp->if_snd)) { 576 IF_DROP(&ifp->if_snd); 577 error = ENOBUFS; 578 goto qfull; 579 } 580 IF_ENQUEUE(&ifp->if_snd, m); 581 if (en_softc[ifp->if_unit].es_oactive == 0) 582 enstart(ifp->if_unit); 583 splx(s); 584 return (0); 585 qfull: 586 m0 = m; 587 splx(s); 588 bad: 589 m_freem(m0); 590 return (error); 591 } 592 593 /* 594 * Process an ioctl request. 595 */ 596 enioctl(ifp, cmd, data) 597 register struct ifnet *ifp; 598 int cmd; 599 caddr_t data; 600 { 601 struct ifreq *ifr = (struct ifreq *)data; 602 int s = splimp(), error = 0; 603 604 switch (cmd) { 605 606 case SIOCSIFADDR: 607 if (ifp->if_flags & IFF_RUNNING) 608 if_rtinit(ifp, -1); /* delete previous route */ 609 ensetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); 610 if (ifp->if_flags & IFF_RUNNING) 611 if_rtinit(ifp, RTF_UP); 612 else 613 eninit(ifp->if_unit); 614 break; 615 616 default: 617 error = EINVAL; 618 } 619 splx(s); 620 return (error); 621 } 622 623 ensetaddr(ifp, sin) 624 register struct ifnet *ifp; 625 register struct sockaddr_in *sin; 626 { 627 struct endevice *enaddr; 628 629 /* set address once for in_netof, so subnets will be recognized */ 630 ifp->if_addr = *(struct sockaddr *)sin; 631 ifp->if_net = in_netof(sin->sin_addr); 632 enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr; 633 ((struct en_softc *) ifp)->es_host = (~enaddr->en_addr) & 0xff; 634 sin = (struct sockaddr_in *)&ifp->if_addr; 635 sin->sin_family = AF_INET; 636 sin->sin_addr = if_makeaddr(ifp->if_net, 637 ((struct en_softc *)ifp)->es_host); 638 sin = (struct sockaddr_in *)&ifp->if_broadaddr; 639 sin->sin_family = AF_INET; 640 sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); 641 ifp->if_flags |= IFF_BROADCAST; 642 } 643 644 #ifdef ENF_SWABIPS 645 /* 646 * Swab bytes 647 * Jeffrey Mogul, Stanford 648 */ 649 enswab(from, to, n) 650 register caddr_t *from, *to; 651 register int n; 652 { 653 register unsigned long temp; 654 655 n >>= 1; n++; 656 #define STEP temp = *from++,*to++ = *from++,*to++ = temp 657 /* round to multiple of 8 */ 658 while ((--n) & 07) 659 STEP; 660 n >>= 3; 661 while (--n >= 0) { 662 STEP; STEP; STEP; STEP; 663 STEP; STEP; STEP; STEP; 664 } 665 } 666 #endif 667