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