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