1 /* if_en.c 4.76 83/05/10 */ 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 #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 400 if (en->en_type >= ENPUP_TRAIL && 401 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { 402 off = (en->en_type - ENPUP_TRAIL) * 512; 403 if (off > ENMTU) 404 goto setup; /* sanity */ 405 en->en_type = *endataaddr(en, off, u_short *); 406 resid = *(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 /* 415 * Pull packet off interface. Off is nonzero if packet 416 * has trailing header; if_rubaget will then force this header 417 * information to be at the front, but we still have to drop 418 * the type and length which are at the front of any trailer data. 419 */ 420 m = if_rubaget(&es->es_ifuba, len, off); 421 if (m == 0) 422 goto setup; 423 if (off) { 424 m->m_off += 2 * sizeof (u_short); 425 m->m_len -= 2 * sizeof (u_short); 426 } 427 switch (en->en_type) { 428 429 #ifdef INET 430 case ENPUP_IPTYPE: 431 schednetisr(NETISR_IP); 432 inq = &ipintrq; 433 break; 434 #endif 435 #ifdef PUP 436 case ENPUP_PUPTYPE: { 437 struct pup_header *pup = mtod(m, struct pup_header *); 438 439 pupproto.sp_protocol = pup->pup_type; 440 pupdst.spup_addr = pup->pup_dport; 441 pupsrc.spup_addr = pup->pup_sport; 442 raw_input(m, &pupproto, (struct sockaddr *)&pupsrc, 443 (struct sockaddr *)&pupdst); 444 goto setup; 445 } 446 #endif 447 default: 448 m_freem(m); 449 goto setup; 450 } 451 452 if (IF_QFULL(inq)) { 453 IF_DROP(inq); 454 m_freem(m); 455 } else 456 IF_ENQUEUE(inq, m); 457 458 setup: 459 /* 460 * Reset for next packet. 461 */ 462 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 463 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 464 addr->en_istat = EN_IEN|EN_GO; 465 } 466 467 /* 468 * Ethernet output routine. 469 * Encapsulate a packet of type family for the local net. 470 * Use trailer local net encapsulation if enough data in first 471 * packet leaves a multiple of 512 bytes of data in remainder. 472 */ 473 enoutput(ifp, m0, dst) 474 struct ifnet *ifp; 475 struct mbuf *m0; 476 struct sockaddr *dst; 477 { 478 int type, dest, s, error; 479 register struct mbuf *m = m0; 480 register struct en_header *en; 481 register int off; 482 483 switch (dst->sa_family) { 484 485 #ifdef INET 486 case AF_INET: 487 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 488 if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) { 489 error = EPERM; /* ??? */ 490 goto bad; 491 } 492 dest = (dest >> 24) & 0xff; 493 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 494 if (off > 0 && (off & 0x1ff) == 0 && 495 m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 496 type = ENPUP_TRAIL + (off>>9); 497 m->m_off -= 2 * sizeof (u_short); 498 m->m_len += 2 * sizeof (u_short); 499 *mtod(m, u_short *) = ENPUP_IPTYPE; 500 *(mtod(m, u_short *) + 1) = m->m_len; 501 goto gottrailertype; 502 } 503 type = ENPUP_IPTYPE; 504 off = 0; 505 goto gottype; 506 #endif 507 #ifdef PUP 508 case AF_PUP: 509 dest = ((struct sockaddr_pup *)dst)->spup_addr.pp_host; 510 type = ENPUP_PUPTYPE; 511 off = 0; 512 goto gottype; 513 #endif 514 515 default: 516 printf("en%d: can't handle af%d\n", ifp->if_unit, 517 dst->sa_family); 518 error = EAFNOSUPPORT; 519 goto bad; 520 } 521 522 gottrailertype: 523 /* 524 * Packet to be sent as trailer: move first packet 525 * (control information) to end of chain. 526 */ 527 while (m->m_next) 528 m = m->m_next; 529 m->m_next = m0; 530 m = m0->m_next; 531 m0->m_next = 0; 532 m0 = m; 533 534 gottype: 535 /* 536 * Add local net header. If no space in first mbuf, 537 * allocate another. 538 */ 539 if (m->m_off > MMAXOFF || 540 MMINOFF + sizeof (struct en_header) > m->m_off) { 541 m = m_get(M_DONTWAIT, MT_HEADER); 542 if (m == 0) { 543 error = ENOBUFS; 544 goto bad; 545 } 546 m->m_next = m0; 547 m->m_off = MMINOFF; 548 m->m_len = sizeof (struct en_header); 549 } else { 550 m->m_off -= sizeof (struct en_header); 551 m->m_len += sizeof (struct en_header); 552 } 553 en = mtod(m, struct en_header *); 554 en->en_shost = ifp->if_host[0]; 555 en->en_dhost = dest; 556 en->en_type = type; 557 558 /* 559 * Queue message on interface, and start output if interface 560 * not yet active. 561 */ 562 s = splimp(); 563 if (IF_QFULL(&ifp->if_snd)) { 564 IF_DROP(&ifp->if_snd); 565 error = ENOBUFS; 566 goto qfull; 567 } 568 IF_ENQUEUE(&ifp->if_snd, m); 569 if (en_softc[ifp->if_unit].es_oactive == 0) 570 enstart(ifp->if_unit); 571 splx(s); 572 return (0); 573 qfull: 574 m0 = m; 575 splx(s); 576 bad: 577 m_freem(m0); 578 return (error); 579 } 580