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