1 /* $NetBSD: if_sn.c,v 1.25 1999/12/12 08:18:48 scottr Exp $ */ 2 3 /* 4 * National Semiconductor DP8393X SONIC Driver 5 * Copyright (c) 1991 Algorithmics Ltd (http://www.algor.co.uk) 6 * You may use, copy, and modify this program so long as you retain the 7 * copyright line. 8 * 9 * This driver has been substantially modified since Algorithmics donated 10 * it. 11 * 12 * Denton Gentry <denny1@home.com> 13 * and also 14 * Yanagisawa Takeshi <yanagisw@aa.ap.titech.ac.jp> 15 * did the work to get this running on the Macintosh. 16 */ 17 18 #include "opt_inet.h" 19 20 #include <sys/param.h> 21 #include <sys/systm.h> 22 #include <sys/mbuf.h> 23 #include <sys/buf.h> 24 #include <sys/protosw.h> 25 #include <sys/socket.h> 26 #include <sys/syslog.h> 27 #include <sys/ioctl.h> 28 #include <sys/errno.h> 29 #include <sys/device.h> 30 31 #include <net/if.h> 32 #include <net/if_dl.h> 33 #include <net/if_ether.h> 34 35 #ifdef INET 36 #include <netinet/in.h> 37 #include <netinet/in_systm.h> 38 #include <netinet/in_var.h> 39 #include <netinet/ip.h> 40 #include <netinet/if_inarp.h> 41 #endif 42 43 #include <vm/vm.h> 44 45 #include "bpfilter.h" 46 #if NBPFILTER > 0 47 #include <net/bpf.h> 48 #include <net/bpfdesc.h> 49 #endif 50 51 #include <machine/bus.h> 52 #include <machine/cpu.h> 53 #include <machine/viareg.h> 54 #include <mac68k/dev/if_snreg.h> 55 #include <mac68k/dev/if_snvar.h> 56 57 static void snwatchdog __P((struct ifnet *)); 58 static int sninit __P((struct sn_softc *sc)); 59 static int snstop __P((struct sn_softc *sc)); 60 static int snioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data)); 61 static void snstart __P((struct ifnet *ifp)); 62 static void snreset __P((struct sn_softc *sc)); 63 64 static void caminitialise __P((struct sn_softc *)); 65 static void camentry __P((struct sn_softc *, int, u_char *ea)); 66 static void camprogram __P((struct sn_softc *)); 67 static void initialise_tda __P((struct sn_softc *)); 68 static void initialise_rda __P((struct sn_softc *)); 69 static void initialise_rra __P((struct sn_softc *)); 70 #ifdef SNDEBUG 71 static void camdump __P((struct sn_softc *sc)); 72 #endif 73 74 static void sonictxint __P((struct sn_softc *)); 75 static void sonicrxint __P((struct sn_softc *)); 76 77 static __inline__ u_int sonicput __P((struct sn_softc *sc, struct mbuf *m0, 78 int mtd_next)); 79 static __inline__ int sonic_read __P((struct sn_softc *, caddr_t, int)); 80 static __inline__ struct mbuf *sonic_get __P((struct sn_softc *, caddr_t, int)); 81 82 #undef assert 83 #undef _assert 84 85 #ifdef NDEBUG 86 #define assert(e) ((void)0) 87 #define _assert(e) ((void)0) 88 #else 89 #define _assert(e) assert(e) 90 #ifdef __STDC__ 91 #define assert(e) ((e) ? (void)0 : __assert("sn ", __FILE__, __LINE__, #e)) 92 #else /* PCC */ 93 #define assert(e) ((e) ? (void)0 : __assert("sn "__FILE__, __LINE__, "e")) 94 #endif 95 #endif 96 97 int sndebug = 0; 98 99 /* 100 * SONIC buffers need to be aligned 16 or 32 bit aligned. 101 * These macros calculate and verify alignment. 102 */ 103 #define ROUNDUP(p, N) (((int) p + N - 1) & ~(N - 1)) 104 105 #define SOALIGN(m, array) (m ? (ROUNDUP(array, 4)) : (ROUNDUP(array, 2))) 106 107 #define LOWER(x) ((unsigned)(x) & 0xffff) 108 #define UPPER(x) ((unsigned)(x) >> 16) 109 110 /* 111 * Interface exists: make available by filling in network interface 112 * record. System will initialize the interface when it is ready 113 * to accept packets. 114 */ 115 int 116 snsetup(sc, lladdr) 117 struct sn_softc *sc; 118 u_int8_t *lladdr; 119 { 120 struct ifnet *ifp = &sc->sc_if; 121 u_char *p; 122 u_char *pp; 123 int i; 124 int offset; 125 126 /* 127 * XXX if_sn.c is intended to be MI. Should it allocate memory 128 * for its descriptor areas, or expect the MD attach code 129 * to do that? 130 */ 131 sc->space = malloc((SN_NPAGES + 1) * NBPG, M_DEVBUF, M_WAITOK); 132 if (sc->space == NULL) { 133 printf ("%s: memory allocation for descriptors failed\n", 134 sc->sc_dev.dv_xname); 135 return (1); 136 } 137 138 /* 139 * Put the pup in reset mode (sninit() will fix it later), 140 * stop the timer, disable all interrupts and clear any interrupts. 141 */ 142 NIC_PUT(sc, SNR_CR, CR_STP); 143 wbflush(); 144 NIC_PUT(sc, SNR_CR, CR_RST); 145 wbflush(); 146 NIC_PUT(sc, SNR_IMR, 0); 147 wbflush(); 148 NIC_PUT(sc, SNR_ISR, ISR_ALL); 149 wbflush(); 150 151 /* 152 * because the SONIC is basically 16bit device it 'concatenates' 153 * a higher buffer address to a 16 bit offset--this will cause wrap 154 * around problems near the end of 64k !! 155 */ 156 p = sc->space; 157 pp = (u_char *)ROUNDUP ((int)p, NBPG); 158 p = pp; 159 160 /* 161 * Disable caching on the SONIC's data space. 162 * The pages might not be physically contiguous, so set 163 * each page individually. 164 */ 165 for (i = 0; i < SN_NPAGES; i++) { 166 physaccess (p, (caddr_t)SONIC_GETDMA(p), NBPG, 167 PG_V | PG_RW | PG_CI); 168 p += NBPG; 169 } 170 p = pp; 171 172 for (i = 0; i < NRRA; i++) { 173 sc->p_rra[i] = (void *)p; 174 sc->v_rra[i] = SONIC_GETDMA(p); 175 p += RXRSRC_SIZE(sc); 176 } 177 sc->v_rea = SONIC_GETDMA(p); 178 179 p = (u_char *)SOALIGN(sc, p); 180 181 sc->p_cda = (void *)(p); 182 sc->v_cda = SONIC_GETDMA(p); 183 p += CDA_SIZE(sc); 184 185 p = (u_char *)SOALIGN(sc, p); 186 187 for (i = 0; i < NTDA; i++) { 188 struct mtd *mtdp = &sc->mtda[i]; 189 mtdp->mtd_txp = (void *)p; 190 mtdp->mtd_vtxp = SONIC_GETDMA(p); 191 p += TXP_SIZE(sc); 192 } 193 194 p = (u_char *)SOALIGN(sc, p); 195 196 if ((p - pp) > NBPG) { 197 printf ("%s: sizeof RRA (%ld) + CDA (%ld) +" 198 "TDA (%ld) > NBPG (%d). Punt!\n", 199 sc->sc_dev.dv_xname, 200 (ulong)sc->p_cda - (ulong)sc->p_rra[0], 201 (ulong)sc->mtda[0].mtd_txp - (ulong)sc->p_cda, 202 (ulong)p - (ulong)sc->mtda[0].mtd_txp, 203 NBPG); 204 return(1); 205 } 206 207 p = pp + NBPG; 208 pp = p; 209 210 sc->sc_nrda = NBPG / RXPKT_SIZE(sc); 211 sc->p_rda = (caddr_t) p; 212 sc->v_rda = SONIC_GETDMA(p); 213 214 p = pp + NBPG; 215 216 for (i = 0; i < NRBA; i++) { 217 sc->rbuf[i] = (caddr_t)p; 218 p += NBPG; 219 } 220 221 pp = p; 222 offset = TXBSIZE; 223 for (i = 0; i < NTDA; i++) { 224 struct mtd *mtdp = &sc->mtda[i]; 225 226 mtdp->mtd_buf = p; 227 mtdp->mtd_vbuf = SONIC_GETDMA(p); 228 offset += TXBSIZE; 229 if (offset < NBPG) { 230 p += TXBSIZE; 231 } else { 232 p = pp + NBPG; 233 pp = p; 234 offset = TXBSIZE; 235 } 236 } 237 238 #ifdef SNDEBUG 239 camdump(sc); 240 #endif 241 printf("%s: Ethernet address %s\n", 242 sc->sc_dev.dv_xname, ether_sprintf(lladdr)); 243 244 #ifdef SNDEBUG 245 printf("%s: buffers: rra=%p cda=%p rda=%p tda=%p\n", 246 sc->sc_dev.dv_xname, sc->p_rra[0], sc->p_cda, 247 sc->p_rda, sc->mtda[0].mtd_txp); 248 #endif 249 250 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); 251 ifp->if_softc = sc; 252 ifp->if_ioctl = snioctl; 253 ifp->if_start = snstart; 254 ifp->if_flags = 255 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 256 ifp->if_watchdog = snwatchdog; 257 #if NBPFILTER > 0 258 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); 259 #endif 260 if_attach(ifp); 261 ether_ifattach(ifp, lladdr); 262 263 return (0); 264 } 265 266 static int 267 snioctl(ifp, cmd, data) 268 struct ifnet *ifp; 269 u_long cmd; 270 caddr_t data; 271 { 272 struct ifaddr *ifa; 273 struct ifreq *ifr; 274 struct sn_softc *sc = ifp->if_softc; 275 int s = splnet(), err = 0; 276 int temp; 277 278 switch (cmd) { 279 280 case SIOCSIFADDR: 281 ifa = (struct ifaddr *)data; 282 ifp->if_flags |= IFF_UP; 283 switch (ifa->ifa_addr->sa_family) { 284 #ifdef INET 285 case AF_INET: 286 (void)sninit(sc); 287 arp_ifinit(ifp, ifa); 288 break; 289 #endif 290 default: 291 (void)sninit(sc); 292 break; 293 } 294 break; 295 296 case SIOCSIFFLAGS: 297 if ((ifp->if_flags & IFF_UP) == 0 && 298 (ifp->if_flags & IFF_RUNNING) != 0) { 299 /* 300 * If interface is marked down and it is running, 301 * then stop it. 302 */ 303 snstop(sc); 304 ifp->if_flags &= ~IFF_RUNNING; 305 } else if ((ifp->if_flags & IFF_UP) != 0 && 306 (ifp->if_flags & IFF_RUNNING) == 0) { 307 /* 308 * If interface is marked up and it is stopped, 309 * then start it. 310 */ 311 (void)sninit(sc); 312 } else { 313 /* 314 * reset the interface to pick up any other changes 315 * in flags 316 */ 317 temp = ifp->if_flags & IFF_UP; 318 snreset(sc); 319 ifp->if_flags |= temp; 320 snstart(ifp); 321 } 322 break; 323 324 case SIOCADDMULTI: 325 case SIOCDELMULTI: 326 ifr = (struct ifreq *) data; 327 if (cmd == SIOCADDMULTI) 328 err = ether_addmulti(ifr, &sc->sc_ethercom); 329 else 330 err = ether_delmulti(ifr, &sc->sc_ethercom); 331 332 if (err == ENETRESET) { 333 /* 334 * Multicast list has changed; set the hardware 335 * filter accordingly. But remember UP flag! 336 */ 337 temp = ifp->if_flags & IFF_UP; 338 snreset(sc); 339 ifp->if_flags |= temp; 340 err = 0; 341 } 342 break; 343 default: 344 err = EINVAL; 345 } 346 splx(s); 347 return (err); 348 } 349 350 /* 351 * Encapsulate a packet of type family for the local net. 352 */ 353 static void 354 snstart(ifp) 355 struct ifnet *ifp; 356 { 357 struct sn_softc *sc = ifp->if_softc; 358 struct mbuf *m; 359 int mtd_next; 360 361 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 362 return; 363 364 outloop: 365 /* Check for room in the xmit buffer. */ 366 if ((mtd_next = (sc->mtd_free + 1)) == NTDA) 367 mtd_next = 0; 368 369 if (mtd_next == sc->mtd_hw) { 370 ifp->if_flags |= IFF_OACTIVE; 371 return; 372 } 373 374 IF_DEQUEUE(&ifp->if_snd, m); 375 if (m == 0) 376 return; 377 378 /* We need the header for m_pkthdr.len. */ 379 if ((m->m_flags & M_PKTHDR) == 0) 380 panic("%s: snstart: no header mbuf", sc->sc_dev.dv_xname); 381 382 #if NBPFILTER > 0 383 /* 384 * If bpf is listening on this interface, let it 385 * see the packet before we commit it to the wire. 386 */ 387 if (ifp->if_bpf) 388 bpf_mtap(ifp->if_bpf, m); 389 #endif 390 391 /* 392 * If there is nothing in the o/p queue, and there is room in 393 * the Tx ring, then send the packet directly. Otherwise append 394 * it to the o/p queue. 395 */ 396 if ((sonicput(sc, m, mtd_next)) == 0) { 397 IF_PREPEND(&ifp->if_snd, m); 398 return; 399 } 400 401 sc->mtd_prev = sc->mtd_free; 402 sc->mtd_free = mtd_next; 403 404 ifp->if_opackets++; /* # of pkts */ 405 406 /* Jump back for possibly more punishment. */ 407 goto outloop; 408 } 409 410 /* 411 * reset and restart the SONIC. Called in case of fatal 412 * hardware/software errors. 413 */ 414 static void 415 snreset(sc) 416 struct sn_softc *sc; 417 { 418 snstop(sc); 419 sninit(sc); 420 } 421 422 static int 423 sninit(sc) 424 struct sn_softc *sc; 425 { 426 u_long s_rcr; 427 int s; 428 429 if (sc->sc_if.if_flags & IFF_RUNNING) 430 /* already running */ 431 return (0); 432 433 s = splnet(); 434 435 NIC_PUT(sc, SNR_CR, CR_RST); /* DCR only accessable in reset mode! */ 436 437 /* config it */ 438 NIC_PUT(sc, SNR_DCR, (sc->snr_dcr | 439 (sc->bitmode ? DCR_DW32 : DCR_DW16))); 440 NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2); 441 442 s_rcr = RCR_BRD | RCR_LBNONE; 443 if (sc->sc_if.if_flags & IFF_PROMISC) 444 s_rcr |= RCR_PRO; 445 if (sc->sc_if.if_flags & IFF_ALLMULTI) 446 s_rcr |= RCR_AMC; 447 NIC_PUT(sc, SNR_RCR, s_rcr); 448 449 NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN)); 450 451 /* clear pending interrupts */ 452 NIC_PUT(sc, SNR_ISR, ISR_ALL); 453 454 /* clear tally counters */ 455 NIC_PUT(sc, SNR_CRCT, -1); 456 NIC_PUT(sc, SNR_FAET, -1); 457 NIC_PUT(sc, SNR_MPT, -1); 458 459 initialise_tda(sc); 460 initialise_rda(sc); 461 initialise_rra(sc); 462 463 /* enable the chip */ 464 NIC_PUT(sc, SNR_CR, 0); 465 wbflush(); 466 467 /* program the CAM */ 468 camprogram(sc); 469 470 /* get it to read resource descriptors */ 471 NIC_PUT(sc, SNR_CR, CR_RRRA); 472 wbflush(); 473 while ((NIC_GET(sc, SNR_CR)) & CR_RRRA) 474 continue; 475 476 /* enable rx */ 477 NIC_PUT(sc, SNR_CR, CR_RXEN); 478 wbflush(); 479 480 /* flag interface as "running" */ 481 sc->sc_if.if_flags |= IFF_RUNNING; 482 sc->sc_if.if_flags &= ~IFF_OACTIVE; 483 484 splx(s); 485 return (0); 486 } 487 488 /* 489 * close down an interface and free its buffers 490 * Called on final close of device, or if sninit() fails 491 * part way through. 492 */ 493 static int 494 snstop(sc) 495 struct sn_softc *sc; 496 { 497 struct mtd *mtd; 498 int s = splnet(); 499 500 /* stick chip in reset */ 501 NIC_PUT(sc, SNR_CR, CR_RST); 502 wbflush(); 503 504 /* free all receive buffers (currently static so nothing to do) */ 505 506 /* free all pending transmit mbufs */ 507 while (sc->mtd_hw != sc->mtd_free) { 508 mtd = &sc->mtda[sc->mtd_hw]; 509 if (mtd->mtd_mbuf) 510 m_freem(mtd->mtd_mbuf); 511 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0; 512 } 513 514 sc->sc_if.if_timer = 0; 515 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP); 516 517 splx(s); 518 return (0); 519 } 520 521 /* 522 * Called if any Tx packets remain unsent after 5 seconds, 523 * In all cases we just reset the chip, and any retransmission 524 * will be handled by higher level protocol timeouts. 525 */ 526 static void 527 snwatchdog(ifp) 528 struct ifnet *ifp; 529 { 530 struct sn_softc *sc = ifp->if_softc; 531 struct mtd *mtd; 532 int temp; 533 534 if (sc->mtd_hw != sc->mtd_free) { 535 /* something still pending for transmit */ 536 mtd = &sc->mtda[sc->mtd_hw]; 537 if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0) 538 log(LOG_ERR, "%s: Tx - timeout\n", 539 sc->sc_dev.dv_xname); 540 else 541 log(LOG_ERR, "%s: Tx - lost interrupt\n", 542 sc->sc_dev.dv_xname); 543 temp = ifp->if_flags & IFF_UP; 544 snreset(sc); 545 ifp->if_flags |= temp; 546 } 547 } 548 549 /* 550 * stuff packet into sonic (at splnet) 551 */ 552 static __inline__ u_int 553 sonicput(sc, m0, mtd_next) 554 struct sn_softc *sc; 555 struct mbuf *m0; 556 int mtd_next; 557 { 558 struct mtd *mtdp; 559 struct mbuf *m; 560 u_char *buff; 561 void *txp; 562 u_int len = 0; 563 u_int totlen = 0; 564 565 #ifdef whyonearthwouldyoudothis 566 if (NIC_GET(sc, SNR_CR) & CR_TXP) 567 return (0); 568 #endif 569 570 /* grab the replacement mtd */ 571 mtdp = &sc->mtda[sc->mtd_free]; 572 573 buff = mtdp->mtd_buf; 574 575 /* this packet goes to mtdnext fill in the TDA */ 576 mtdp->mtd_mbuf = m0; 577 txp = mtdp->mtd_txp; 578 579 /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */ 580 if (sc->mtd_pint == 0) { 581 sc->mtd_pint = NTDA/2; 582 SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT); 583 } else { 584 sc->mtd_pint--; 585 SWO(sc->bitmode, txp, TXP_CONFIG, 0); 586 } 587 588 for (m = m0; m; m = m->m_next) { 589 u_char *data = mtod(m, u_char *); 590 len = m->m_len; 591 totlen += len; 592 bcopy(data, buff, len); 593 buff += len; 594 } 595 if (totlen >= TXBSIZE) { 596 panic("%s: sonicput: packet overflow", sc->sc_dev.dv_xname); 597 } 598 599 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO, 600 LOWER(mtdp->mtd_vbuf)); 601 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI, 602 UPPER(mtdp->mtd_vbuf)); 603 604 if (totlen < ETHERMIN + ETHER_HDR_LEN) { 605 int pad = ETHERMIN + ETHER_HDR_LEN - totlen; 606 bzero(mtdp->mtd_buf + totlen, pad); 607 totlen = ETHERMIN + ETHER_HDR_LEN; 608 } 609 610 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE, 611 totlen); 612 SWO(sc->bitmode, txp, TXP_FRAGCNT, 1); 613 SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen); 614 615 /* link onto the next mtd that will be used */ 616 SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO, 617 LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL); 618 619 /* 620 * The previous txp.tlink currently contains a pointer to 621 * our txp | EOL. Want to clear the EOL, so write our 622 * pointer to the previous txp. 623 */ 624 SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko, 625 LOWER(mtdp->mtd_vtxp)); 626 627 /* make sure chip is running */ 628 wbflush(); 629 NIC_PUT(sc, SNR_CR, CR_TXP); 630 wbflush(); 631 sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */ 632 633 return (totlen); 634 } 635 636 /* 637 * These are called from sonicioctl() when /etc/ifconfig is run to set 638 * the address or switch the i/f on. 639 */ 640 /* 641 * CAM support 642 */ 643 static void 644 caminitialise(sc) 645 struct sn_softc *sc; 646 { 647 void *p_cda = sc->p_cda; 648 int i; 649 int bitmode = sc->bitmode; 650 int camoffset; 651 652 for (i = 0; i < MAXCAM; i++) { 653 camoffset = i * CDA_CAMDESC; 654 SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i); 655 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0); 656 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0); 657 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0); 658 } 659 SWO(bitmode, p_cda, CDA_ENABLE, 0); 660 } 661 662 static void 663 camentry(sc, entry, ea) 664 int entry; 665 u_char *ea; 666 struct sn_softc *sc; 667 { 668 void *p_cda = sc->p_cda; 669 int bitmode = sc->bitmode; 670 int camoffset = entry * CDA_CAMDESC; 671 672 SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry); 673 SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]); 674 SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]); 675 SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]); 676 SWO(bitmode, p_cda, CDA_ENABLE, 677 (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry))); 678 } 679 680 static void 681 camprogram(sc) 682 struct sn_softc *sc; 683 { 684 struct ether_multistep step; 685 struct ether_multi *enm; 686 struct ifnet *ifp; 687 int timeout; 688 int mcount = 0; 689 690 caminitialise(sc); 691 692 ifp = &sc->sc_if; 693 694 /* Always load our own address first. */ 695 camentry (sc, mcount, LLADDR(ifp->if_sadl)); 696 mcount++; 697 698 /* Assume we won't need allmulti bit. */ 699 ifp->if_flags &= ~IFF_ALLMULTI; 700 701 /* Loop through multicast addresses */ 702 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm); 703 while (enm != NULL) { 704 if (mcount == MAXCAM) { 705 ifp->if_flags |= IFF_ALLMULTI; 706 break; 707 } 708 709 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 710 sizeof(enm->enm_addrlo)) != 0) { 711 /* 712 * SONIC's CAM is programmed with specific 713 * addresses. It has no way to specify a range. 714 * (Well, thats not exactly true. If the 715 * range is small one could program each addr 716 * within the range as a seperate CAM entry) 717 */ 718 ifp->if_flags |= IFF_ALLMULTI; 719 break; 720 } 721 722 /* program the CAM with the specified entry */ 723 camentry(sc, mcount, enm->enm_addrlo); 724 mcount++; 725 726 ETHER_NEXT_MULTI(step, enm); 727 } 728 729 NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda)); 730 NIC_PUT(sc, SNR_CDC, MAXCAM); 731 NIC_PUT(sc, SNR_CR, CR_LCAM); 732 wbflush(); 733 734 timeout = 10000; 735 while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--) 736 continue; 737 if (timeout == 0) { 738 /* XXX */ 739 panic("%s: CAM initialisation failed\n", sc->sc_dev.dv_xname); 740 } 741 timeout = 10000; 742 while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--) 743 continue; 744 745 if (NIC_GET(sc, SNR_ISR) & ISR_LCD) 746 NIC_PUT(sc, SNR_ISR, ISR_LCD); 747 else 748 printf("%s: CAM initialisation without interrupt\n", 749 sc->sc_dev.dv_xname); 750 } 751 752 #ifdef SNDEBUG 753 static void 754 camdump(sc) 755 struct sn_softc *sc; 756 { 757 int i; 758 759 printf("CAM entries:\n"); 760 NIC_PUT(sc, SNR_CR, CR_RST); 761 wbflush(); 762 763 for (i = 0; i < 16; i++) { 764 ushort ap2, ap1, ap0; 765 NIC_PUT(sc, SNR_CEP, i); 766 wbflush(); 767 ap2 = NIC_GET(sc, SNR_CAP2); 768 ap1 = NIC_GET(sc, SNR_CAP1); 769 ap0 = NIC_GET(sc, SNR_CAP0); 770 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0); 771 } 772 printf("CAM enable 0x%x\n", NIC_GET(sc, SNR_CEP)); 773 774 NIC_PUT(sc, SNR_CR, 0); 775 wbflush(); 776 } 777 #endif 778 779 static void 780 initialise_tda(sc) 781 struct sn_softc *sc; 782 { 783 struct mtd *mtd; 784 int i; 785 786 for (i = 0; i < NTDA; i++) { 787 mtd = &sc->mtda[i]; 788 mtd->mtd_mbuf = 0; 789 } 790 791 sc->mtd_hw = 0; 792 sc->mtd_prev = NTDA - 1; 793 sc->mtd_free = 0; 794 sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO; 795 sc->mtd_pint = NTDA/2; 796 797 NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp)); 798 NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp)); 799 } 800 801 static void 802 initialise_rda(sc) 803 struct sn_softc *sc; 804 { 805 int bitmode = sc->bitmode; 806 int i; 807 caddr_t p_rda = 0; 808 u_int32_t v_rda = 0; 809 810 /* link the RDA's together into a circular list */ 811 for (i = 0; i < (sc->sc_nrda - 1); i++) { 812 p_rda = sc->p_rda + (i * RXPKT_SIZE(sc)); 813 v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc)); 814 SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda)); 815 SWO(bitmode, p_rda, RXPKT_INUSE, 1); 816 } 817 p_rda = sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc)); 818 SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL); 819 SWO(bitmode, p_rda, RXPKT_INUSE, 1); 820 821 /* mark end of receive descriptor list */ 822 sc->sc_rdamark = sc->sc_nrda - 1; 823 824 sc->sc_rxmark = 0; 825 826 NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda)); 827 NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda)); 828 wbflush(); 829 } 830 831 static void 832 initialise_rra(sc) 833 struct sn_softc *sc; 834 { 835 int i; 836 u_int v; 837 int bitmode = sc->bitmode; 838 839 if (bitmode) 840 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2); 841 else 842 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1); 843 844 NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0])); 845 NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0])); 846 /* rea must point just past the end of the rra space */ 847 NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea)); 848 NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0])); 849 NIC_PUT(sc, SNR_RSC, 0); 850 851 /* fill up SOME of the rra with buffers */ 852 for (i = 0; i < NRBA; i++) { 853 v = SONIC_GETDMA(sc->rbuf[i]); 854 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v)); 855 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v)); 856 SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(NBPG/2)); 857 SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(NBPG/2)); 858 } 859 sc->sc_rramark = NRBA; 860 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark])); 861 wbflush(); 862 } 863 864 void 865 snintr(arg) 866 void *arg; 867 { 868 struct sn_softc *sc = (struct sn_softc *)arg; 869 int isr; 870 871 while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) { 872 /* scrub the interrupts that we are going to service */ 873 NIC_PUT(sc, SNR_ISR, isr); 874 wbflush(); 875 876 if (isr & (ISR_BR | ISR_LCD | ISR_TC)) 877 printf("%s: unexpected interrupt status 0x%x\n", 878 sc->sc_dev.dv_xname, isr); 879 880 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT)) 881 sonictxint(sc); 882 883 if (isr & ISR_PKTRX) 884 sonicrxint(sc); 885 886 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) { 887 if (isr & ISR_HBL) 888 /* 889 * The repeater is not providing a heartbeat. 890 * In itself this isn't harmful, lots of the 891 * cheap repeater hubs don't supply a heartbeat. 892 * So ignore the lack of heartbeat. Its only 893 * if we can't detect a carrier that we have a 894 * problem. 895 */ 896 ; 897 if (isr & ISR_RDE) 898 printf("%s: receive descriptors exhausted\n", 899 sc->sc_dev.dv_xname); 900 if (isr & ISR_RBE) 901 printf("%s: receive buffers exhausted\n", 902 sc->sc_dev.dv_xname); 903 if (isr & ISR_RBAE) 904 printf("%s: receive buffer area exhausted\n", 905 sc->sc_dev.dv_xname); 906 if (isr & ISR_RFO) 907 printf("%s: receive FIFO overrun\n", 908 sc->sc_dev.dv_xname); 909 } 910 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) { 911 #ifdef notdef 912 if (isr & ISR_CRC) 913 sc->sc_crctally++; 914 if (isr & ISR_FAE) 915 sc->sc_faetally++; 916 if (isr & ISR_MP) 917 sc->sc_mptally++; 918 #endif 919 } 920 snstart(&sc->sc_if); 921 } 922 return; 923 } 924 925 /* 926 * Transmit interrupt routine 927 */ 928 static void 929 sonictxint(sc) 930 struct sn_softc *sc; 931 { 932 struct mtd *mtd; 933 void *txp; 934 unsigned short txp_status; 935 int mtd_hw; 936 struct ifnet *ifp = &sc->sc_if; 937 938 mtd_hw = sc->mtd_hw; 939 940 if (mtd_hw == sc->mtd_free) 941 return; 942 943 while (mtd_hw != sc->mtd_free) { 944 mtd = &sc->mtda[mtd_hw]; 945 946 txp = mtd->mtd_txp; 947 948 if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) { 949 break; /* it hasn't really gone yet */ 950 } 951 952 #ifdef SNDEBUG 953 { 954 struct ether_header *eh; 955 956 eh = (struct ether_header *) mtd->mtd_buf; 957 printf("%s: xmit status=0x%x len=%d type=0x%x from %s", 958 sc->sc_dev.dv_xname, 959 SRO(sc->bitmode, txp, TXP_STATUS), 960 SRO(sc->bitmode, txp, TXP_PKTSIZE), 961 htons(eh->ether_type), 962 ether_sprintf(eh->ether_shost)); 963 printf(" (to %s)\n", ether_sprintf(eh->ether_dhost)); 964 } 965 #endif /* SNDEBUG */ 966 967 ifp->if_flags &= ~IFF_OACTIVE; 968 969 if (mtd->mtd_mbuf != 0) { 970 m_freem(mtd->mtd_mbuf); 971 mtd->mtd_mbuf = 0; 972 } 973 if (++mtd_hw == NTDA) mtd_hw = 0; 974 975 txp_status = SRO(sc->bitmode, txp, TXP_STATUS); 976 977 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 : 978 ((txp_status & TCR_NC) >> 12); 979 980 if ((txp_status & TCR_PTX) == 0) { 981 ifp->if_oerrors++; 982 printf("%s: Tx packet status=0x%x\n", 983 sc->sc_dev.dv_xname, txp_status); 984 985 /* XXX - DG This looks bogus */ 986 if (mtd_hw != sc->mtd_free) { 987 printf("resubmitting remaining packets\n"); 988 mtd = &sc->mtda[mtd_hw]; 989 NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp)); 990 NIC_PUT(sc, SNR_CR, CR_TXP); 991 wbflush(); 992 break; 993 } 994 } 995 } 996 997 sc->mtd_hw = mtd_hw; 998 return; 999 } 1000 1001 /* 1002 * Receive interrupt routine 1003 */ 1004 static void 1005 sonicrxint(sc) 1006 struct sn_softc *sc; 1007 { 1008 caddr_t rda; 1009 int orra; 1010 int len; 1011 int rramark; 1012 int rdamark; 1013 int bitmode = sc->bitmode; 1014 u_int16_t rxpkt_ptr; 1015 1016 rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc)); 1017 1018 while (SRO(bitmode, rda, RXPKT_INUSE) == 0) { 1019 u_int status = SRO(bitmode, rda, RXPKT_STATUS); 1020 1021 orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK; 1022 rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO); 1023 len = SRO(bitmode, rda, RXPKT_BYTEC) - FCSSIZE; 1024 if (status & RCR_PRX) { 1025 caddr_t pkt = sc->rbuf[orra & RBAMASK] + 1026 m68k_page_offset(rxpkt_ptr); 1027 if (sonic_read(sc, pkt, len)) 1028 sc->sc_if.if_ipackets++; 1029 else 1030 sc->sc_if.if_ierrors++; 1031 } else 1032 sc->sc_if.if_ierrors++; 1033 1034 /* 1035 * give receive buffer area back to chip. 1036 * 1037 * If this was the last packet in the RRA, give the RRA to 1038 * the chip again. 1039 * If sonic read didnt copy it out then we would have to 1040 * wait !! 1041 * (dont bother add it back in again straight away) 1042 * 1043 * Really, we're doing p_rra[rramark] = p_rra[orra] but 1044 * we have to use the macros because SONIC might be in 1045 * 16 or 32 bit mode. 1046 */ 1047 if (status & RCR_LPKT) { 1048 void *tmp1, *tmp2; 1049 1050 rramark = sc->sc_rramark; 1051 tmp1 = sc->p_rra[rramark]; 1052 tmp2 = sc->p_rra[orra]; 1053 SWO(bitmode, tmp1, RXRSRC_PTRLO, 1054 SRO(bitmode, tmp2, RXRSRC_PTRLO)); 1055 SWO(bitmode, tmp1, RXRSRC_PTRHI, 1056 SRO(bitmode, tmp2, RXRSRC_PTRHI)); 1057 SWO(bitmode, tmp1, RXRSRC_WCLO, 1058 SRO(bitmode, tmp2, RXRSRC_WCLO)); 1059 SWO(bitmode, tmp1, RXRSRC_WCHI, 1060 SRO(bitmode, tmp2, RXRSRC_WCHI)); 1061 1062 /* zap old rra for fun */ 1063 SWO(bitmode, tmp2, RXRSRC_WCHI, 0); 1064 SWO(bitmode, tmp2, RXRSRC_WCLO, 0); 1065 1066 sc->sc_rramark = (++rramark) & RRAMASK; 1067 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark])); 1068 wbflush(); 1069 } 1070 1071 /* 1072 * give receive descriptor back to chip simple 1073 * list is circular 1074 */ 1075 rdamark = sc->sc_rdamark; 1076 SWO(bitmode, rda, RXPKT_INUSE, 1); 1077 SWO(bitmode, rda, RXPKT_RLINK, 1078 SRO(bitmode, rda, RXPKT_RLINK) | EOL); 1079 SWO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK, 1080 SRO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), 1081 RXPKT_RLINK) & ~EOL); 1082 sc->sc_rdamark = sc->sc_rxmark; 1083 1084 if (++sc->sc_rxmark >= sc->sc_nrda) 1085 sc->sc_rxmark = 0; 1086 rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc)); 1087 } 1088 } 1089 1090 /* 1091 * sonic_read -- pull packet off interface and forward to 1092 * appropriate protocol handler 1093 */ 1094 static __inline__ int 1095 sonic_read(sc, pkt, len) 1096 struct sn_softc *sc; 1097 caddr_t pkt; 1098 int len; 1099 { 1100 struct ifnet *ifp = &sc->sc_if; 1101 struct ether_header *eh; 1102 struct mbuf *m; 1103 1104 /* 1105 * Get pointer to ethernet header (in input buffer). 1106 */ 1107 eh = (struct ether_header *)pkt; 1108 1109 #ifdef SNDEBUG 1110 { 1111 printf("%s: rcvd 0x%p len=%d type=0x%x from %s", 1112 sc->sc_dev.dv_xname, et, len, htons(et->ether_type), 1113 ether_sprintf(et->ether_shost)); 1114 printf(" (to %s)\n", ether_sprintf(et->ether_dhost)); 1115 } 1116 #endif /* SNDEBUG */ 1117 1118 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN) || 1119 len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) { 1120 printf("%s: invalid packet length %d bytes\n", 1121 sc->sc_dev.dv_xname, len); 1122 return (0); 1123 } 1124 1125 #if NBPFILTER > 0 1126 /* 1127 * Check if there's a bpf filter listening on this interface. 1128 * If so, hand off the raw packet to enet, then discard things 1129 * not destined for us (but be sure to keep broadcast/multicast). 1130 */ 1131 if (ifp->if_bpf) { 1132 bpf_tap(ifp->if_bpf, pkt, len); 1133 if ((ifp->if_flags & IFF_PROMISC) != 0 && 1134 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */ 1135 bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl), 1136 sizeof(eh->ether_dhost)) != 0) 1137 return (0); 1138 } 1139 #endif 1140 m = sonic_get(sc, pkt, len); 1141 if (m == NULL) 1142 return (0); 1143 (*ifp->if_input)(ifp, m); 1144 return (1); 1145 } 1146 1147 /* 1148 * munge the received packet into an mbuf chain 1149 */ 1150 static __inline__ struct mbuf * 1151 sonic_get(sc, pkt, datalen) 1152 struct sn_softc *sc; 1153 caddr_t pkt; 1154 int datalen; 1155 { 1156 struct mbuf *m, *top, **mp; 1157 int len; 1158 1159 MGETHDR(m, M_DONTWAIT, MT_DATA); 1160 if (m == 0) 1161 return (0); 1162 m->m_pkthdr.rcvif = &sc->sc_if; 1163 m->m_pkthdr.len = datalen; 1164 len = MHLEN; 1165 top = 0; 1166 mp = ⊤ 1167 1168 while (datalen > 0) { 1169 if (top) { 1170 MGET(m, M_DONTWAIT, MT_DATA); 1171 if (m == 0) { 1172 m_freem(top); 1173 return (0); 1174 } 1175 len = MLEN; 1176 } 1177 if (datalen >= MINCLSIZE) { 1178 MCLGET(m, M_DONTWAIT); 1179 if ((m->m_flags & M_EXT) == 0) { 1180 if (top) m_freem(top); 1181 return (0); 1182 } 1183 len = MCLBYTES; 1184 } 1185 1186 if (mp == &top) { 1187 caddr_t newdata = (caddr_t) 1188 ALIGN(m->m_data + sizeof(struct ether_header)) - 1189 sizeof(struct ether_header); 1190 len -= newdata - m->m_data; 1191 m->m_data = newdata; 1192 } 1193 1194 m->m_len = len = min(datalen, len); 1195 1196 bcopy(pkt, mtod(m, caddr_t), (unsigned) len); 1197 pkt += len; 1198 datalen -= len; 1199 *mp = m; 1200 mp = &m->m_next; 1201 } 1202 1203 return (top); 1204 } 1205 1206 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; 1207 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf]) 1208 1209 void 1210 sn_get_enaddr(t, h, o, dst) 1211 bus_space_tag_t t; 1212 bus_space_handle_t h; 1213 bus_size_t o; 1214 u_char *dst; 1215 { 1216 int i, do_bbr; 1217 u_char b; 1218 1219 /* 1220 * For reasons known only to Apple, MAC addresses in the ethernet 1221 * PROM are stored in Token Ring (IEEE 802.5) format, that is 1222 * with all of the bits in each byte reversed (canonical bit format). 1223 * When the address is read out it must be reversed to ethernet format 1224 * before use. 1225 * 1226 * Apple has been assigned OUI's 08:00:07 and 00:a0:40. All onboard 1227 * ethernet addresses on 68K machines should be in one of these 1228 * two ranges. 1229 * 1230 * Here is where it gets complicated. 1231 * 1232 * The PMac 7200, 7500, 8500, and 9500 accidentally had the PROM 1233 * written in standard ethernet format. The MacOS accounted for this 1234 * in these systems, and did not reverse the bytes. Some other 1235 * networking utilities were not so forgiving, and got confused. 1236 * "Some" of Apple's Nubus ethernet cards also had their bits 1237 * burned in ethernet format. 1238 * 1239 * Apple petitioned the IEEE and was granted the 00:05:02 (bit reversal 1240 * of 00:a0:40) as well. As of OpenTransport 1.1.1, Apple removed 1241 * their workaround and now reverses the bits regardless of 1242 * what kind of machine it is. So PMac systems and the affected 1243 * Nubus cards now use 00:05:02, instead of the 00:a0:40 for which they 1244 * were intended. 1245 * 1246 * See Apple Techinfo article TECHINFO-0020552, "OpenTransport 1.1.1 1247 * and MacOS System 7.5.3 FAQ (10/96)" for more details. 1248 */ 1249 do_bbr = 0; 1250 b = bus_space_read_1(t, h, o); 1251 if (b == 0x10) 1252 do_bbr = 1; 1253 dst[0] = (do_bbr) ? bbr(b) : b; 1254 1255 for (i = 1 ; i < ETHER_ADDR_LEN ; i++) { 1256 b = bus_space_read_1(t, h, o+i); 1257 dst[i] = (do_bbr) ? bbr(b) : b; 1258 } 1259 } 1260