1 /* if_il.c 6.5 84/08/29 */ 2 3 #include "il.h" 4 5 /* 6 * Interlan Ethernet Communications Controller interface 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 "ioctl.h" 18 #include "errno.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 "../netinet/if_ether.h" 28 #include "../netpup/pup.h" 29 30 #include "../vax/cpu.h" 31 #include "../vax/mtpr.h" 32 #include "if_il.h" 33 #include "if_ilreg.h" 34 #include "if_uba.h" 35 #include "../vaxuba/ubareg.h" 36 #include "../vaxuba/ubavar.h" 37 38 int ilprobe(), ilattach(), ilrint(), ilcint(); 39 struct uba_device *ilinfo[NIL]; 40 u_short ilstd[] = { 0 }; 41 struct uba_driver ildriver = 42 { ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo }; 43 #define ILUNIT(x) minor(x) 44 int ilinit(),iloutput(),ilioctl(),ilreset(),ilwatch(); 45 46 /* 47 * Ethernet software status per interface. 48 * 49 * Each interface is referenced by a network interface structure, 50 * is_if, which the routing code uses to locate the interface. 51 * This structure contains the output queue for the interface, its address, ... 52 * We also have, for each interface, a UBA interface structure, which 53 * contains information about the UNIBUS resources held by the interface: 54 * map registers, buffered data paths, etc. Information is cached in this 55 * structure for use by the if_uba.c routines in running the interface 56 * efficiently. 57 */ 58 struct il_softc { 59 struct arpcom is_ac; /* Ethernet common part */ 60 #define is_if is_ac.ac_if /* network-visible interface */ 61 #define is_addr is_ac.ac_enaddr /* hardware Ethernet address */ 62 struct ifuba is_ifuba; /* UNIBUS resources */ 63 int is_flags; 64 #define ILF_OACTIVE 0x1 /* output is active */ 65 #define ILF_RCVPENDING 0x2 /* start rcv in ilcint */ 66 #define ILF_STATPENDING 0x4 /* stat cmd pending */ 67 short is_lastcmd; /* can't read csr, so must save it */ 68 short is_scaninterval; /* interval of stat collection */ 69 #define ILWATCHINTERVAL 60 /* once every 60 seconds */ 70 struct il_stats is_stats; /* holds on-board statistics */ 71 struct il_stats is_sum; /* summation over time */ 72 int is_ubaddr; /* mapping registers of is_stats */ 73 } il_softc[NIL]; 74 75 ilprobe(reg) 76 caddr_t reg; 77 { 78 register int br, cvec; /* r11, r10 value-result */ 79 register struct ildevice *addr = (struct ildevice *)reg; 80 register i; 81 82 #ifdef lint 83 br = 0; cvec = br; br = cvec; 84 i = 0; ilrint(i); ilcint(i); ilwatch(i); 85 #endif 86 87 addr->il_csr = ILC_OFFLINE|IL_CIE; 88 DELAY(100000); 89 i = addr->il_csr; /* clear CDONE */ 90 if (cvec > 0 && cvec != 0x200) 91 cvec -= 4; 92 return (1); 93 } 94 95 /* 96 * Interface exists: make available by filling in network interface 97 * record. System will initialize the interface when it is ready 98 * to accept packets. A STATUS command is done to get the ethernet 99 * address and other interesting data. 100 */ 101 ilattach(ui) 102 struct uba_device *ui; 103 { 104 register struct il_softc *is = &il_softc[ui->ui_unit]; 105 register struct ifnet *ifp = &is->is_if; 106 register struct ildevice *addr = (struct ildevice *)ui->ui_addr; 107 struct sockaddr_in *sin; 108 109 ifp->if_unit = ui->ui_unit; 110 ifp->if_name = "il"; 111 ifp->if_mtu = ETHERMTU; 112 113 /* 114 * Reset the board and map the statistics 115 * buffer onto the Unibus. 116 */ 117 addr->il_csr = ILC_RESET; 118 while ((addr->il_csr&IL_CDONE) == 0) 119 ; 120 if (addr->il_csr&IL_STATUS) 121 printf("il%d: reset failed, csr=%b\n", ui->ui_unit, 122 addr->il_csr, IL_BITS); 123 124 is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats, 125 sizeof (struct il_stats), 0); 126 addr->il_bar = is->is_ubaddr & 0xffff; 127 addr->il_bcr = sizeof (struct il_stats); 128 addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT; 129 while ((addr->il_csr&IL_CDONE) == 0) 130 ; 131 if (addr->il_csr&IL_STATUS) 132 printf("il%d: status failed, csr=%b\n", ui->ui_unit, 133 addr->il_csr, IL_BITS); 134 ubarelse(ui->ui_ubanum, &is->is_ubaddr); 135 #ifdef notdef 136 printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n", 137 ui->ui_unit, 138 is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff, 139 is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff, 140 is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff, 141 is->is_stats.ils_module, is->is_stats.ils_firmware); 142 #endif 143 is->is_addr = is->is_stats.ils_addr; 144 sin = (struct sockaddr_in *)&ifp->if_addr; 145 sin->sin_family = AF_INET; 146 ifp->if_init = ilinit; 147 ifp->if_output = iloutput; 148 ifp->if_ioctl = ilioctl; 149 ifp->if_reset = ilreset; 150 is->is_ifuba.ifu_flags = UBA_CANTWAIT; 151 #ifdef notdef 152 is->is_ifuba.ifu_flags |= UBA_NEEDBDP; 153 #endif 154 if_attach(ifp); 155 } 156 157 /* 158 * Reset of interface after UNIBUS reset. 159 * If interface is on specified uba, reset its state. 160 */ 161 ilreset(unit, uban) 162 int unit, uban; 163 { 164 register struct uba_device *ui; 165 166 if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 || 167 ui->ui_ubanum != uban) 168 return; 169 printf(" il%d", unit); 170 ilinit(unit); 171 } 172 173 /* 174 * Initialization of interface; clear recorded pending 175 * operations, and reinitialize UNIBUS usage. 176 */ 177 ilinit(unit) 178 int unit; 179 { 180 register struct il_softc *is = &il_softc[unit]; 181 register struct uba_device *ui = ilinfo[unit]; 182 register struct ildevice *addr; 183 register struct ifnet *ifp = &is->is_if; 184 register struct sockaddr_in *sin; 185 int s; 186 187 sin = (struct sockaddr_in *)&ifp->if_addr; 188 if (sin->sin_addr.s_addr == 0) /* address still unknown */ 189 return; 190 191 if (ifp->if_flags & IFF_RUNNING) 192 goto justarp; 193 if (if_ubainit(&is->is_ifuba, ui->ui_ubanum, 194 sizeof (struct il_rheader), (int)btoc(ETHERMTU)) == 0) { 195 printf("il%d: can't initialize\n", unit); 196 is->is_if.if_flags &= ~IFF_UP; 197 return; 198 } 199 is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats, 200 sizeof (struct il_stats), 0); 201 ifp->if_watchdog = ilwatch; 202 is->is_scaninterval = ILWATCHINTERVAL; 203 ifp->if_timer = is->is_scaninterval; 204 addr = (struct ildevice *)ui->ui_addr; 205 206 /* 207 * Turn off source address insertion (it's faster this way), 208 * and set board online. Former doesn't work if board is 209 * already online (happens on ubareset), so we put it offline 210 * first. 211 */ 212 s = splimp(); 213 addr->il_csr = ILC_OFFLINE; 214 while ((addr->il_csr & IL_CDONE) == 0) 215 ; 216 addr->il_csr = ILC_CISA; 217 while ((addr->il_csr & IL_CDONE) == 0) 218 ; 219 /* 220 * Set board online. 221 * Hang receive buffer and start any pending 222 * writes by faking a transmit complete. 223 * Receive bcr is not a muliple of 4 so buffer 224 * chaining can't happen. 225 */ 226 addr->il_csr = ILC_ONLINE; 227 while ((addr->il_csr & IL_CDONE) == 0) 228 ; 229 addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff; 230 addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6; 231 addr->il_csr = 232 ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE; 233 while ((addr->il_csr & IL_CDONE) == 0) 234 ; 235 is->is_flags = ILF_OACTIVE; 236 is->is_if.if_flags |= IFF_UP|IFF_RUNNING; 237 is->is_lastcmd = 0; 238 ilcint(unit); 239 splx(s); 240 justarp: 241 if_rtinit(&is->is_if, RTF_UP); 242 arpwhohas(&is->is_ac, &sin->sin_addr); 243 } 244 245 /* 246 * Start output on interface. 247 * Get another datagram to send off of the interface queue, 248 * and map it to the interface before starting the output. 249 */ 250 ilstart(dev) 251 dev_t dev; 252 { 253 int unit = ILUNIT(dev), len; 254 struct uba_device *ui = ilinfo[unit]; 255 register struct il_softc *is = &il_softc[unit]; 256 register struct ildevice *addr; 257 struct mbuf *m; 258 short csr; 259 260 IF_DEQUEUE(&is->is_if.if_snd, m); 261 addr = (struct ildevice *)ui->ui_addr; 262 if (m == 0) { 263 if ((is->is_flags & ILF_STATPENDING) == 0) 264 return; 265 addr->il_bar = is->is_ubaddr & 0xffff; 266 addr->il_bcr = sizeof (struct il_stats); 267 csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE; 268 is->is_flags &= ~ILF_STATPENDING; 269 goto startcmd; 270 } 271 len = if_wubaput(&is->is_ifuba, m); 272 /* 273 * Ensure minimum packet length. 274 * This makes the safe assumtion that there are no virtual holes 275 * after the data. 276 * For security, it might be wise to zero out the added bytes, 277 * but we're mainly interested in speed at the moment. 278 */ 279 if (len - sizeof(struct ether_header) < ETHERMIN) 280 len = ETHERMIN + sizeof(struct ether_header); 281 if (is->is_ifuba.ifu_flags & UBA_NEEDBDP) 282 UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp); 283 addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff; 284 addr->il_bcr = len; 285 csr = 286 ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE; 287 288 startcmd: 289 is->is_lastcmd = csr & IL_CMD; 290 addr->il_csr = csr; 291 is->is_flags |= ILF_OACTIVE; 292 } 293 294 /* 295 * Command done interrupt. 296 */ 297 ilcint(unit) 298 int unit; 299 { 300 register struct il_softc *is = &il_softc[unit]; 301 struct uba_device *ui = ilinfo[unit]; 302 register struct ildevice *addr = (struct ildevice *)ui->ui_addr; 303 short csr; 304 305 if ((is->is_flags & ILF_OACTIVE) == 0) { 306 printf("il%d: stray xmit interrupt, csr=%b\n", unit, 307 addr->il_csr, IL_BITS); 308 return; 309 } 310 311 csr = addr->il_csr; 312 /* 313 * Hang receive buffer if it couldn't 314 * be done earlier (in ilrint). 315 */ 316 if (is->is_flags & ILF_RCVPENDING) { 317 addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff; 318 addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6; 319 addr->il_csr = 320 ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE; 321 while ((addr->il_csr & IL_CDONE) == 0) 322 ; 323 is->is_flags &= ~ILF_RCVPENDING; 324 } 325 is->is_flags &= ~ILF_OACTIVE; 326 csr &= IL_STATUS; 327 switch (is->is_lastcmd) { 328 329 case ILC_XMIT: 330 is->is_if.if_opackets++; 331 if (csr > ILERR_RETRIES) 332 is->is_if.if_oerrors++; 333 break; 334 335 case ILC_STAT: 336 if (csr == ILERR_SUCCESS) 337 iltotal(is); 338 break; 339 } 340 if (is->is_ifuba.ifu_xtofree) { 341 m_freem(is->is_ifuba.ifu_xtofree); 342 is->is_ifuba.ifu_xtofree = 0; 343 } 344 ilstart(unit); 345 } 346 347 /* 348 * Ethernet interface receiver interrupt. 349 * If input error just drop packet. 350 * Otherwise purge input buffered data path and examine 351 * packet to determine type. If can't determine length 352 * from type, then have to drop packet. Othewise decapsulate 353 * packet based on type and pass to type specific higher-level 354 * input routine. 355 */ 356 ilrint(unit) 357 int unit; 358 { 359 register struct il_softc *is = &il_softc[unit]; 360 struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr; 361 register struct il_rheader *il; 362 struct mbuf *m; 363 int len, off, resid, s; 364 register struct ifqueue *inq; 365 366 is->is_if.if_ipackets++; 367 if (is->is_ifuba.ifu_flags & UBA_NEEDBDP) 368 UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp); 369 il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr); 370 len = il->ilr_length - sizeof(struct il_rheader); 371 if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 || 372 len > ETHERMTU) { 373 is->is_if.if_ierrors++; 374 #ifdef notdef 375 if (is->is_if.if_ierrors % 100 == 0) 376 printf("il%d: += 100 input errors\n", unit); 377 #endif 378 goto setup; 379 } 380 381 /* 382 * Deal with trailer protocol: if type is PUP trailer 383 * get true type from first 16-bit word past data. 384 * Remember that type was trailer by setting off. 385 */ 386 il->ilr_type = ntohs((u_short)il->ilr_type); 387 #define ildataaddr(il, off, type) ((type)(((caddr_t)((il)+1)+(off)))) 388 if (il->ilr_type >= ETHERPUP_TRAIL && 389 il->ilr_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) { 390 off = (il->ilr_type - ETHERPUP_TRAIL) * 512; 391 if (off >= ETHERMTU) 392 goto setup; /* sanity */ 393 il->ilr_type = ntohs(*ildataaddr(il, off, u_short *)); 394 resid = ntohs(*(ildataaddr(il, off+2, u_short *))); 395 if (off + resid > len) 396 goto setup; /* sanity */ 397 len = off + resid; 398 } else 399 off = 0; 400 if (len == 0) 401 goto setup; 402 403 /* 404 * Pull packet off interface. Off is nonzero if packet 405 * has trailing header; ilget will then force this header 406 * information to be at the front, but we still have to drop 407 * the type and length which are at the front of any trailer data. 408 */ 409 m = if_rubaget(&is->is_ifuba, len, off); 410 if (m == 0) 411 goto setup; 412 if (off) { 413 m->m_off += 2 * sizeof (u_short); 414 m->m_len -= 2 * sizeof (u_short); 415 } 416 switch (il->ilr_type) { 417 418 #ifdef INET 419 case ETHERPUP_IPTYPE: 420 schednetisr(NETISR_IP); 421 inq = &ipintrq; 422 break; 423 424 case ETHERPUP_ARPTYPE: 425 arpinput(&is->is_ac, m); 426 goto setup; 427 #endif 428 default: 429 m_freem(m); 430 goto setup; 431 } 432 433 s = splimp(); 434 if (IF_QFULL(inq)) { 435 IF_DROP(inq); 436 m_freem(m); 437 } else 438 IF_ENQUEUE(inq, m); 439 splx(s); 440 441 setup: 442 /* 443 * Reset for next packet if possible. 444 * If waiting for transmit command completion, set flag 445 * and wait until command completes. 446 */ 447 if (is->is_flags & ILF_OACTIVE) { 448 is->is_flags |= ILF_RCVPENDING; 449 return; 450 } 451 addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff; 452 addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6; 453 addr->il_csr = 454 ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE; 455 while ((addr->il_csr & IL_CDONE) == 0) 456 ; 457 } 458 459 /* 460 * Ethernet output routine. 461 * Encapsulate a packet of type family for the local net. 462 * Use trailer local net encapsulation if enough data in first 463 * packet leaves a multiple of 512 bytes of data in remainder. 464 */ 465 iloutput(ifp, m0, dst) 466 struct ifnet *ifp; 467 struct mbuf *m0; 468 struct sockaddr *dst; 469 { 470 int type, s, error; 471 struct ether_addr edst; 472 struct in_addr idst; 473 register struct il_softc *is = &il_softc[ifp->if_unit]; 474 register struct mbuf *m = m0; 475 register struct ether_header *il; 476 register int off; 477 478 switch (dst->sa_family) { 479 480 #ifdef INET 481 case AF_INET: 482 idst = ((struct sockaddr_in *)dst)->sin_addr; 483 if (!arpresolve(&is->is_ac, m, &idst, &edst)) 484 return (0); /* if not yet resolved */ 485 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 486 /* need per host negotiation */ 487 if ((ifp->if_flags & IFF_NOTRAILERS) == 0) 488 if (off > 0 && (off & 0x1ff) == 0 && 489 m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 490 type = ETHERPUP_TRAIL + (off>>9); 491 m->m_off -= 2 * sizeof (u_short); 492 m->m_len += 2 * sizeof (u_short); 493 *mtod(m, u_short *) = htons((u_short)ETHERPUP_IPTYPE); 494 *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 495 goto gottrailertype; 496 } 497 type = ETHERPUP_IPTYPE; 498 off = 0; 499 goto gottype; 500 #endif 501 502 case AF_UNSPEC: 503 il = (struct ether_header *)dst->sa_data; 504 edst = il->ether_dhost; 505 type = il->ether_type; 506 goto gottype; 507 508 default: 509 printf("il%d: can't handle af%d\n", ifp->if_unit, 510 dst->sa_family); 511 error = EAFNOSUPPORT; 512 goto bad; 513 } 514 515 gottrailertype: 516 /* 517 * Packet to be sent as trailer: move first packet 518 * (control information) to end of chain. 519 */ 520 while (m->m_next) 521 m = m->m_next; 522 m->m_next = m0; 523 m = m0->m_next; 524 m0->m_next = 0; 525 m0 = m; 526 527 gottype: 528 /* 529 * Add local net header. If no space in first mbuf, 530 * allocate another. 531 */ 532 if (m->m_off > MMAXOFF || 533 MMINOFF + sizeof (struct ether_header) > m->m_off) { 534 m = m_get(M_DONTWAIT, MT_HEADER); 535 if (m == 0) { 536 error = ENOBUFS; 537 goto bad; 538 } 539 m->m_next = m0; 540 m->m_off = MMINOFF; 541 m->m_len = sizeof (struct ether_header); 542 } else { 543 m->m_off -= sizeof (struct ether_header); 544 m->m_len += sizeof (struct ether_header); 545 } 546 il = mtod(m, struct ether_header *); 547 il->ether_type = htons((u_short)type); 548 il->ether_dhost = edst; 549 il->ether_shost = is->is_addr; 550 551 /* 552 * Queue message on interface, and start output if interface 553 * not yet active. 554 */ 555 s = splimp(); 556 if (IF_QFULL(&ifp->if_snd)) { 557 IF_DROP(&ifp->if_snd); 558 splx(s); 559 m_freem(m); 560 return (ENOBUFS); 561 } 562 IF_ENQUEUE(&ifp->if_snd, m); 563 if ((is->is_flags & ILF_OACTIVE) == 0) 564 ilstart(ifp->if_unit); 565 splx(s); 566 return (0); 567 568 bad: 569 m_freem(m0); 570 return (error); 571 } 572 573 /* 574 * Watchdog routine, request statistics from board. 575 */ 576 ilwatch(unit) 577 int unit; 578 { 579 register struct il_softc *is = &il_softc[unit]; 580 register struct ifnet *ifp = &is->is_if; 581 int s; 582 583 if (is->is_flags & ILF_STATPENDING) { 584 ifp->if_timer = is->is_scaninterval; 585 return; 586 } 587 s = splimp(); 588 is->is_flags |= ILF_STATPENDING; 589 if ((is->is_flags & ILF_OACTIVE) == 0) 590 ilstart(ifp->if_unit); 591 splx(s); 592 ifp->if_timer = is->is_scaninterval; 593 } 594 595 /* 596 * Total up the on-board statistics. 597 */ 598 iltotal(is) 599 register struct il_softc *is; 600 { 601 register u_short *interval, *sum, *end; 602 603 interval = &is->is_stats.ils_frames; 604 sum = &is->is_sum.ils_frames; 605 end = is->is_sum.ils_fill2; 606 while (sum < end) 607 *sum++ += *interval++; 608 is->is_if.if_collisions = is->is_sum.ils_collis; 609 } 610 611 /* 612 * Process an ioctl request. 613 */ 614 ilioctl(ifp, cmd, data) 615 register struct ifnet *ifp; 616 int cmd; 617 caddr_t data; 618 { 619 register struct ifreq *ifr = (struct ifreq *)data; 620 int s = splimp(), error = 0; 621 622 switch (cmd) { 623 624 case SIOCSIFADDR: 625 if (ifp->if_flags & IFF_RUNNING) 626 if_rtinit(ifp, -1); /* delete previous route */ 627 ilsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); 628 ilinit(ifp->if_unit); 629 break; 630 631 default: 632 error = EINVAL; 633 } 634 splx(s); 635 return (error); 636 } 637 638 ilsetaddr(ifp, sin) 639 register struct ifnet *ifp; 640 register struct sockaddr_in *sin; 641 { 642 643 ifp->if_addr = *(struct sockaddr *)sin; 644 ifp->if_net = in_netof(sin->sin_addr); 645 ifp->if_host[0] = in_lnaof(sin->sin_addr); 646 sin = (struct sockaddr_in *)&ifp->if_broadaddr; 647 sin->sin_family = AF_INET; 648 sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); 649 ifp->if_flags |= IFF_BROADCAST; 650 } 651