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