1 /* $NetBSD: if_sn.c,v 1.31 2002/01/16 06:02:56 thorpej 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 <uvm/uvm_extern.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 258 if_attach(ifp); 259 ether_ifattach(ifp, lladdr); 260 261 return (0); 262 } 263 264 static int 265 snioctl(ifp, cmd, data) 266 struct ifnet *ifp; 267 u_long cmd; 268 caddr_t data; 269 { 270 struct ifaddr *ifa; 271 struct ifreq *ifr; 272 struct sn_softc *sc = ifp->if_softc; 273 int s = splnet(), err = 0; 274 int temp; 275 276 switch (cmd) { 277 278 case SIOCSIFADDR: 279 ifa = (struct ifaddr *)data; 280 ifp->if_flags |= IFF_UP; 281 switch (ifa->ifa_addr->sa_family) { 282 #ifdef INET 283 case AF_INET: 284 (void)sninit(sc); 285 arp_ifinit(ifp, ifa); 286 break; 287 #endif 288 default: 289 (void)sninit(sc); 290 break; 291 } 292 break; 293 294 case SIOCSIFFLAGS: 295 if ((ifp->if_flags & IFF_UP) == 0 && 296 (ifp->if_flags & IFF_RUNNING) != 0) { 297 /* 298 * If interface is marked down and it is running, 299 * then stop it. 300 */ 301 snstop(sc); 302 ifp->if_flags &= ~IFF_RUNNING; 303 } else if ((ifp->if_flags & IFF_UP) != 0 && 304 (ifp->if_flags & IFF_RUNNING) == 0) { 305 /* 306 * If interface is marked up and it is stopped, 307 * then start it. 308 */ 309 (void)sninit(sc); 310 } else { 311 /* 312 * reset the interface to pick up any other changes 313 * in flags 314 */ 315 temp = ifp->if_flags & IFF_UP; 316 snreset(sc); 317 ifp->if_flags |= temp; 318 snstart(ifp); 319 } 320 break; 321 322 case SIOCADDMULTI: 323 case SIOCDELMULTI: 324 ifr = (struct ifreq *) data; 325 if (cmd == SIOCADDMULTI) 326 err = ether_addmulti(ifr, &sc->sc_ethercom); 327 else 328 err = ether_delmulti(ifr, &sc->sc_ethercom); 329 330 if (err == ENETRESET) { 331 /* 332 * Multicast list has changed; set the hardware 333 * filter accordingly. But remember UP flag! 334 */ 335 temp = ifp->if_flags & IFF_UP; 336 snreset(sc); 337 ifp->if_flags |= temp; 338 err = 0; 339 } 340 break; 341 default: 342 err = EINVAL; 343 } 344 splx(s); 345 return (err); 346 } 347 348 /* 349 * Encapsulate a packet of type family for the local net. 350 */ 351 static void 352 snstart(ifp) 353 struct ifnet *ifp; 354 { 355 struct sn_softc *sc = ifp->if_softc; 356 struct mbuf *m; 357 int mtd_next; 358 359 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 360 return; 361 362 outloop: 363 /* Check for room in the xmit buffer. */ 364 if ((mtd_next = (sc->mtd_free + 1)) == NTDA) 365 mtd_next = 0; 366 367 if (mtd_next == sc->mtd_hw) { 368 ifp->if_flags |= IFF_OACTIVE; 369 return; 370 } 371 372 IF_DEQUEUE(&ifp->if_snd, m); 373 if (m == 0) 374 return; 375 376 /* We need the header for m_pkthdr.len. */ 377 if ((m->m_flags & M_PKTHDR) == 0) 378 panic("%s: snstart: no header mbuf", sc->sc_dev.dv_xname); 379 380 #if NBPFILTER > 0 381 /* 382 * If bpf is listening on this interface, let it 383 * see the packet before we commit it to the wire. 384 */ 385 if (ifp->if_bpf) 386 bpf_mtap(ifp->if_bpf, m); 387 #endif 388 389 /* 390 * If there is nothing in the o/p queue, and there is room in 391 * the Tx ring, then send the packet directly. Otherwise append 392 * it to the o/p queue. 393 */ 394 if ((sonicput(sc, m, mtd_next)) == 0) { 395 IF_PREPEND(&ifp->if_snd, m); 396 return; 397 } 398 399 sc->mtd_prev = sc->mtd_free; 400 sc->mtd_free = mtd_next; 401 402 ifp->if_opackets++; /* # of pkts */ 403 404 /* Jump back for possibly more punishment. */ 405 goto outloop; 406 } 407 408 /* 409 * reset and restart the SONIC. Called in case of fatal 410 * hardware/software errors. 411 */ 412 static void 413 snreset(sc) 414 struct sn_softc *sc; 415 { 416 snstop(sc); 417 sninit(sc); 418 } 419 420 static int 421 sninit(sc) 422 struct sn_softc *sc; 423 { 424 u_long s_rcr; 425 int s; 426 427 if (sc->sc_if.if_flags & IFF_RUNNING) 428 /* already running */ 429 return (0); 430 431 s = splnet(); 432 433 NIC_PUT(sc, SNR_CR, CR_RST); /* DCR only accessible in reset mode! */ 434 435 /* config it */ 436 NIC_PUT(sc, SNR_DCR, (sc->snr_dcr | 437 (sc->bitmode ? DCR_DW32 : DCR_DW16))); 438 NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2); 439 440 s_rcr = RCR_BRD | RCR_LBNONE; 441 if (sc->sc_if.if_flags & IFF_PROMISC) 442 s_rcr |= RCR_PRO; 443 if (sc->sc_if.if_flags & IFF_ALLMULTI) 444 s_rcr |= RCR_AMC; 445 NIC_PUT(sc, SNR_RCR, s_rcr); 446 447 NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN)); 448 449 /* clear pending interrupts */ 450 NIC_PUT(sc, SNR_ISR, ISR_ALL); 451 452 /* clear tally counters */ 453 NIC_PUT(sc, SNR_CRCT, -1); 454 NIC_PUT(sc, SNR_FAET, -1); 455 NIC_PUT(sc, SNR_MPT, -1); 456 457 initialise_tda(sc); 458 initialise_rda(sc); 459 initialise_rra(sc); 460 461 /* enable the chip */ 462 NIC_PUT(sc, SNR_CR, 0); 463 wbflush(); 464 465 /* program the CAM */ 466 camprogram(sc); 467 468 /* get it to read resource descriptors */ 469 NIC_PUT(sc, SNR_CR, CR_RRRA); 470 wbflush(); 471 while ((NIC_GET(sc, SNR_CR)) & CR_RRRA) 472 continue; 473 474 /* enable rx */ 475 NIC_PUT(sc, SNR_CR, CR_RXEN); 476 wbflush(); 477 478 /* flag interface as "running" */ 479 sc->sc_if.if_flags |= IFF_RUNNING; 480 sc->sc_if.if_flags &= ~IFF_OACTIVE; 481 482 splx(s); 483 return (0); 484 } 485 486 /* 487 * close down an interface and free its buffers 488 * Called on final close of device, or if sninit() fails 489 * part way through. 490 */ 491 static int 492 snstop(sc) 493 struct sn_softc *sc; 494 { 495 struct mtd *mtd; 496 int s = splnet(); 497 498 /* stick chip in reset */ 499 NIC_PUT(sc, SNR_CR, CR_RST); 500 wbflush(); 501 502 /* free all receive buffers (currently static so nothing to do) */ 503 504 /* free all pending transmit mbufs */ 505 while (sc->mtd_hw != sc->mtd_free) { 506 mtd = &sc->mtda[sc->mtd_hw]; 507 if (mtd->mtd_mbuf) 508 m_freem(mtd->mtd_mbuf); 509 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0; 510 } 511 512 sc->sc_if.if_timer = 0; 513 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP); 514 515 splx(s); 516 return (0); 517 } 518 519 /* 520 * Called if any Tx packets remain unsent after 5 seconds, 521 * In all cases we just reset the chip, and any retransmission 522 * will be handled by higher level protocol timeouts. 523 */ 524 static void 525 snwatchdog(ifp) 526 struct ifnet *ifp; 527 { 528 struct sn_softc *sc = ifp->if_softc; 529 struct mtd *mtd; 530 int temp; 531 532 if (sc->mtd_hw != sc->mtd_free) { 533 /* something still pending for transmit */ 534 mtd = &sc->mtda[sc->mtd_hw]; 535 if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0) 536 log(LOG_ERR, "%s: Tx - timeout\n", 537 sc->sc_dev.dv_xname); 538 else 539 log(LOG_ERR, "%s: Tx - lost interrupt\n", 540 sc->sc_dev.dv_xname); 541 temp = ifp->if_flags & IFF_UP; 542 snreset(sc); 543 ifp->if_flags |= temp; 544 } 545 } 546 547 /* 548 * stuff packet into sonic (at splnet) 549 */ 550 static __inline__ u_int 551 sonicput(sc, m0, mtd_next) 552 struct sn_softc *sc; 553 struct mbuf *m0; 554 int mtd_next; 555 { 556 struct mtd *mtdp; 557 struct mbuf *m; 558 u_char *buff; 559 void *txp; 560 u_int len = 0; 561 u_int totlen = 0; 562 563 #ifdef whyonearthwouldyoudothis 564 if (NIC_GET(sc, SNR_CR) & CR_TXP) 565 return (0); 566 #endif 567 568 /* grab the replacement mtd */ 569 mtdp = &sc->mtda[sc->mtd_free]; 570 571 buff = mtdp->mtd_buf; 572 573 /* this packet goes to mtdnext fill in the TDA */ 574 mtdp->mtd_mbuf = m0; 575 txp = mtdp->mtd_txp; 576 577 /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */ 578 if (sc->mtd_pint == 0) { 579 sc->mtd_pint = NTDA/2; 580 SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT); 581 } else { 582 sc->mtd_pint--; 583 SWO(sc->bitmode, txp, TXP_CONFIG, 0); 584 } 585 586 for (m = m0; m; m = m->m_next) { 587 u_char *data = mtod(m, u_char *); 588 len = m->m_len; 589 totlen += len; 590 bcopy(data, buff, len); 591 buff += len; 592 } 593 if (totlen >= TXBSIZE) { 594 panic("%s: sonicput: packet overflow", sc->sc_dev.dv_xname); 595 } 596 597 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO, 598 LOWER(mtdp->mtd_vbuf)); 599 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI, 600 UPPER(mtdp->mtd_vbuf)); 601 602 if (totlen < ETHERMIN + ETHER_HDR_LEN) { 603 int pad = ETHERMIN + ETHER_HDR_LEN - totlen; 604 bzero(mtdp->mtd_buf + totlen, pad); 605 totlen = ETHERMIN + ETHER_HDR_LEN; 606 } 607 608 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE, 609 totlen); 610 SWO(sc->bitmode, txp, TXP_FRAGCNT, 1); 611 SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen); 612 613 /* link onto the next mtd that will be used */ 614 SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO, 615 LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL); 616 617 /* 618 * The previous txp.tlink currently contains a pointer to 619 * our txp | EOL. Want to clear the EOL, so write our 620 * pointer to the previous txp. 621 */ 622 SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko, 623 LOWER(mtdp->mtd_vtxp)); 624 625 /* make sure chip is running */ 626 wbflush(); 627 NIC_PUT(sc, SNR_CR, CR_TXP); 628 wbflush(); 629 sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */ 630 631 return (totlen); 632 } 633 634 /* 635 * These are called from sonicioctl() when /etc/ifconfig is run to set 636 * the address or switch the i/f on. 637 */ 638 /* 639 * CAM support 640 */ 641 static void 642 caminitialise(sc) 643 struct sn_softc *sc; 644 { 645 void *p_cda = sc->p_cda; 646 int i; 647 int bitmode = sc->bitmode; 648 int camoffset; 649 650 for (i = 0; i < MAXCAM; i++) { 651 camoffset = i * CDA_CAMDESC; 652 SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i); 653 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0); 654 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0); 655 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0); 656 } 657 SWO(bitmode, p_cda, CDA_ENABLE, 0); 658 } 659 660 static void 661 camentry(sc, entry, ea) 662 int entry; 663 u_char *ea; 664 struct sn_softc *sc; 665 { 666 void *p_cda = sc->p_cda; 667 int bitmode = sc->bitmode; 668 int camoffset = entry * CDA_CAMDESC; 669 670 SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry); 671 SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]); 672 SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]); 673 SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]); 674 SWO(bitmode, p_cda, CDA_ENABLE, 675 (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry))); 676 } 677 678 static void 679 camprogram(sc) 680 struct sn_softc *sc; 681 { 682 struct ether_multistep step; 683 struct ether_multi *enm; 684 struct ifnet *ifp; 685 int timeout; 686 int mcount = 0; 687 688 caminitialise(sc); 689 690 ifp = &sc->sc_if; 691 692 /* Always load our own address first. */ 693 camentry (sc, mcount, LLADDR(ifp->if_sadl)); 694 mcount++; 695 696 /* Assume we won't need allmulti bit. */ 697 ifp->if_flags &= ~IFF_ALLMULTI; 698 699 /* Loop through multicast addresses */ 700 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm); 701 while (enm != NULL) { 702 if (mcount == MAXCAM) { 703 ifp->if_flags |= IFF_ALLMULTI; 704 break; 705 } 706 707 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 708 sizeof(enm->enm_addrlo)) != 0) { 709 /* 710 * SONIC's CAM is programmed with specific 711 * addresses. It has no way to specify a range. 712 * (Well, thats not exactly true. If the 713 * range is small one could program each addr 714 * within the range as a separate CAM entry) 715 */ 716 ifp->if_flags |= IFF_ALLMULTI; 717 break; 718 } 719 720 /* program the CAM with the specified entry */ 721 camentry(sc, mcount, enm->enm_addrlo); 722 mcount++; 723 724 ETHER_NEXT_MULTI(step, enm); 725 } 726 727 NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda)); 728 NIC_PUT(sc, SNR_CDC, MAXCAM); 729 NIC_PUT(sc, SNR_CR, CR_LCAM); 730 wbflush(); 731 732 timeout = 10000; 733 while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--) 734 continue; 735 if (timeout == 0) { 736 /* XXX */ 737 panic("%s: CAM initialisation failed\n", sc->sc_dev.dv_xname); 738 } 739 timeout = 10000; 740 while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--) 741 continue; 742 743 if (NIC_GET(sc, SNR_ISR) & ISR_LCD) 744 NIC_PUT(sc, SNR_ISR, ISR_LCD); 745 else 746 printf("%s: CAM initialisation without interrupt\n", 747 sc->sc_dev.dv_xname); 748 } 749 750 #ifdef SNDEBUG 751 static void 752 camdump(sc) 753 struct sn_softc *sc; 754 { 755 int i; 756 757 printf("CAM entries:\n"); 758 NIC_PUT(sc, SNR_CR, CR_RST); 759 wbflush(); 760 761 for (i = 0; i < 16; i++) { 762 ushort ap2, ap1, ap0; 763 NIC_PUT(sc, SNR_CEP, i); 764 wbflush(); 765 ap2 = NIC_GET(sc, SNR_CAP2); 766 ap1 = NIC_GET(sc, SNR_CAP1); 767 ap0 = NIC_GET(sc, SNR_CAP0); 768 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0); 769 } 770 printf("CAM enable 0x%x\n", NIC_GET(sc, SNR_CEP)); 771 772 NIC_PUT(sc, SNR_CR, 0); 773 wbflush(); 774 } 775 #endif 776 777 static void 778 initialise_tda(sc) 779 struct sn_softc *sc; 780 { 781 struct mtd *mtd; 782 int i; 783 784 for (i = 0; i < NTDA; i++) { 785 mtd = &sc->mtda[i]; 786 mtd->mtd_mbuf = 0; 787 } 788 789 sc->mtd_hw = 0; 790 sc->mtd_prev = NTDA - 1; 791 sc->mtd_free = 0; 792 sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO; 793 sc->mtd_pint = NTDA/2; 794 795 NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp)); 796 NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp)); 797 } 798 799 static void 800 initialise_rda(sc) 801 struct sn_softc *sc; 802 { 803 int bitmode = sc->bitmode; 804 int i; 805 caddr_t p_rda = 0; 806 u_int32_t v_rda = 0; 807 808 /* link the RDA's together into a circular list */ 809 for (i = 0; i < (sc->sc_nrda - 1); i++) { 810 p_rda = sc->p_rda + (i * RXPKT_SIZE(sc)); 811 v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc)); 812 SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda)); 813 SWO(bitmode, p_rda, RXPKT_INUSE, 1); 814 } 815 p_rda = sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc)); 816 SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL); 817 SWO(bitmode, p_rda, RXPKT_INUSE, 1); 818 819 /* mark end of receive descriptor list */ 820 sc->sc_rdamark = sc->sc_nrda - 1; 821 822 sc->sc_rxmark = 0; 823 824 NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda)); 825 NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda)); 826 wbflush(); 827 } 828 829 static void 830 initialise_rra(sc) 831 struct sn_softc *sc; 832 { 833 int i; 834 u_int v; 835 int bitmode = sc->bitmode; 836 837 if (bitmode) 838 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2); 839 else 840 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1); 841 842 NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0])); 843 NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0])); 844 /* rea must point just past the end of the rra space */ 845 NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea)); 846 NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0])); 847 NIC_PUT(sc, SNR_RSC, 0); 848 849 /* fill up SOME of the rra with buffers */ 850 for (i = 0; i < NRBA; i++) { 851 v = SONIC_GETDMA(sc->rbuf[i]); 852 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v)); 853 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v)); 854 SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(NBPG/2)); 855 SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(NBPG/2)); 856 } 857 sc->sc_rramark = NRBA; 858 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark])); 859 wbflush(); 860 } 861 862 void 863 snintr(arg) 864 void *arg; 865 { 866 struct sn_softc *sc = (struct sn_softc *)arg; 867 int isr; 868 869 while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) { 870 /* scrub the interrupts that we are going to service */ 871 NIC_PUT(sc, SNR_ISR, isr); 872 wbflush(); 873 874 if (isr & (ISR_BR | ISR_LCD | ISR_TC)) 875 printf("%s: unexpected interrupt status 0x%x\n", 876 sc->sc_dev.dv_xname, isr); 877 878 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT)) 879 sonictxint(sc); 880 881 if (isr & ISR_PKTRX) 882 sonicrxint(sc); 883 884 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) { 885 if (isr & ISR_HBL) 886 /* 887 * The repeater is not providing a heartbeat. 888 * In itself this isn't harmful, lots of the 889 * cheap repeater hubs don't supply a heartbeat. 890 * So ignore the lack of heartbeat. Its only 891 * if we can't detect a carrier that we have a 892 * problem. 893 */ 894 ; 895 if (isr & ISR_RDE) 896 printf("%s: receive descriptors exhausted\n", 897 sc->sc_dev.dv_xname); 898 if (isr & ISR_RBE) 899 printf("%s: receive buffers exhausted\n", 900 sc->sc_dev.dv_xname); 901 if (isr & ISR_RBAE) 902 printf("%s: receive buffer area exhausted\n", 903 sc->sc_dev.dv_xname); 904 if (isr & ISR_RFO) 905 printf("%s: receive FIFO overrun\n", 906 sc->sc_dev.dv_xname); 907 } 908 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) { 909 #ifdef notdef 910 if (isr & ISR_CRC) 911 sc->sc_crctally++; 912 if (isr & ISR_FAE) 913 sc->sc_faetally++; 914 if (isr & ISR_MP) 915 sc->sc_mptally++; 916 #endif 917 } 918 snstart(&sc->sc_if); 919 } 920 return; 921 } 922 923 /* 924 * Transmit interrupt routine 925 */ 926 static void 927 sonictxint(sc) 928 struct sn_softc *sc; 929 { 930 struct mtd *mtd; 931 void *txp; 932 unsigned short txp_status; 933 int mtd_hw; 934 struct ifnet *ifp = &sc->sc_if; 935 936 mtd_hw = sc->mtd_hw; 937 938 if (mtd_hw == sc->mtd_free) 939 return; 940 941 while (mtd_hw != sc->mtd_free) { 942 mtd = &sc->mtda[mtd_hw]; 943 944 txp = mtd->mtd_txp; 945 946 if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) { 947 break; /* it hasn't really gone yet */ 948 } 949 950 #ifdef SNDEBUG 951 { 952 struct ether_header *eh; 953 954 eh = (struct ether_header *) mtd->mtd_buf; 955 printf("%s: xmit status=0x%x len=%d type=0x%x from %s", 956 sc->sc_dev.dv_xname, 957 SRO(sc->bitmode, txp, TXP_STATUS), 958 SRO(sc->bitmode, txp, TXP_PKTSIZE), 959 htons(eh->ether_type), 960 ether_sprintf(eh->ether_shost)); 961 printf(" (to %s)\n", ether_sprintf(eh->ether_dhost)); 962 } 963 #endif /* SNDEBUG */ 964 965 ifp->if_flags &= ~IFF_OACTIVE; 966 967 if (mtd->mtd_mbuf != 0) { 968 m_freem(mtd->mtd_mbuf); 969 mtd->mtd_mbuf = 0; 970 } 971 if (++mtd_hw == NTDA) mtd_hw = 0; 972 973 txp_status = SRO(sc->bitmode, txp, TXP_STATUS); 974 975 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 : 976 ((txp_status & TCR_NC) >> 12); 977 978 if ((txp_status & TCR_PTX) == 0) { 979 ifp->if_oerrors++; 980 printf("%s: Tx packet status=0x%x\n", 981 sc->sc_dev.dv_xname, txp_status); 982 983 /* XXX - DG This looks bogus */ 984 if (mtd_hw != sc->mtd_free) { 985 printf("resubmitting remaining packets\n"); 986 mtd = &sc->mtda[mtd_hw]; 987 NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp)); 988 NIC_PUT(sc, SNR_CR, CR_TXP); 989 wbflush(); 990 break; 991 } 992 } 993 } 994 995 sc->mtd_hw = mtd_hw; 996 return; 997 } 998 999 /* 1000 * Receive interrupt routine 1001 */ 1002 static void 1003 sonicrxint(sc) 1004 struct sn_softc *sc; 1005 { 1006 caddr_t rda; 1007 int orra; 1008 int len; 1009 int rramark; 1010 int rdamark; 1011 int bitmode = sc->bitmode; 1012 u_int16_t rxpkt_ptr; 1013 1014 rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc)); 1015 1016 while (SRO(bitmode, rda, RXPKT_INUSE) == 0) { 1017 u_int status = SRO(bitmode, rda, RXPKT_STATUS); 1018 1019 orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK; 1020 rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO); 1021 len = SRO(bitmode, rda, RXPKT_BYTEC) - FCSSIZE; 1022 if (status & RCR_PRX) { 1023 caddr_t pkt = sc->rbuf[orra & RBAMASK] + 1024 m68k_page_offset(rxpkt_ptr); 1025 if (sonic_read(sc, pkt, len)) 1026 sc->sc_if.if_ipackets++; 1027 else 1028 sc->sc_if.if_ierrors++; 1029 } else 1030 sc->sc_if.if_ierrors++; 1031 1032 /* 1033 * give receive buffer area back to chip. 1034 * 1035 * If this was the last packet in the RRA, give the RRA to 1036 * the chip again. 1037 * If sonic read didnt copy it out then we would have to 1038 * wait !! 1039 * (dont bother add it back in again straight away) 1040 * 1041 * Really, we're doing p_rra[rramark] = p_rra[orra] but 1042 * we have to use the macros because SONIC might be in 1043 * 16 or 32 bit mode. 1044 */ 1045 if (status & RCR_LPKT) { 1046 void *tmp1, *tmp2; 1047 1048 rramark = sc->sc_rramark; 1049 tmp1 = sc->p_rra[rramark]; 1050 tmp2 = sc->p_rra[orra]; 1051 SWO(bitmode, tmp1, RXRSRC_PTRLO, 1052 SRO(bitmode, tmp2, RXRSRC_PTRLO)); 1053 SWO(bitmode, tmp1, RXRSRC_PTRHI, 1054 SRO(bitmode, tmp2, RXRSRC_PTRHI)); 1055 SWO(bitmode, tmp1, RXRSRC_WCLO, 1056 SRO(bitmode, tmp2, RXRSRC_WCLO)); 1057 SWO(bitmode, tmp1, RXRSRC_WCHI, 1058 SRO(bitmode, tmp2, RXRSRC_WCHI)); 1059 1060 /* zap old rra for fun */ 1061 SWO(bitmode, tmp2, RXRSRC_WCHI, 0); 1062 SWO(bitmode, tmp2, RXRSRC_WCLO, 0); 1063 1064 sc->sc_rramark = (++rramark) & RRAMASK; 1065 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark])); 1066 wbflush(); 1067 } 1068 1069 /* 1070 * give receive descriptor back to chip simple 1071 * list is circular 1072 */ 1073 rdamark = sc->sc_rdamark; 1074 SWO(bitmode, rda, RXPKT_INUSE, 1); 1075 SWO(bitmode, rda, RXPKT_RLINK, 1076 SRO(bitmode, rda, RXPKT_RLINK) | EOL); 1077 SWO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK, 1078 SRO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), 1079 RXPKT_RLINK) & ~EOL); 1080 sc->sc_rdamark = sc->sc_rxmark; 1081 1082 if (++sc->sc_rxmark >= sc->sc_nrda) 1083 sc->sc_rxmark = 0; 1084 rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc)); 1085 } 1086 } 1087 1088 /* 1089 * sonic_read -- pull packet off interface and forward to 1090 * appropriate protocol handler 1091 */ 1092 static __inline__ int 1093 sonic_read(sc, pkt, len) 1094 struct sn_softc *sc; 1095 caddr_t pkt; 1096 int len; 1097 { 1098 struct ifnet *ifp = &sc->sc_if; 1099 struct mbuf *m; 1100 1101 #ifdef SNDEBUG 1102 { 1103 printf("%s: rcvd 0x%p len=%d type=0x%x from %s", 1104 sc->sc_dev.dv_xname, et, len, htons(et->ether_type), 1105 ether_sprintf(et->ether_shost)); 1106 printf(" (to %s)\n", ether_sprintf(et->ether_dhost)); 1107 } 1108 #endif /* SNDEBUG */ 1109 1110 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN) || 1111 len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) { 1112 printf("%s: invalid packet length %d bytes\n", 1113 sc->sc_dev.dv_xname, len); 1114 return (0); 1115 } 1116 1117 m = sonic_get(sc, pkt, len); 1118 if (m == NULL) 1119 return (0); 1120 #if NBPFILTER > 0 1121 /* Pass this up to any BPF listeners. */ 1122 if (ifp->if_bpf) 1123 bpf_mtap(ifp->if_bpf, m); 1124 #endif 1125 (*ifp->if_input)(ifp, m); 1126 return (1); 1127 } 1128 1129 /* 1130 * munge the received packet into an mbuf chain 1131 */ 1132 static __inline__ struct mbuf * 1133 sonic_get(sc, pkt, datalen) 1134 struct sn_softc *sc; 1135 caddr_t pkt; 1136 int datalen; 1137 { 1138 struct mbuf *m, *top, **mp; 1139 int len; 1140 1141 MGETHDR(m, M_DONTWAIT, MT_DATA); 1142 if (m == 0) 1143 return (0); 1144 m->m_pkthdr.rcvif = &sc->sc_if; 1145 m->m_pkthdr.len = datalen; 1146 len = MHLEN; 1147 top = 0; 1148 mp = ⊤ 1149 1150 while (datalen > 0) { 1151 if (top) { 1152 MGET(m, M_DONTWAIT, MT_DATA); 1153 if (m == 0) { 1154 m_freem(top); 1155 return (0); 1156 } 1157 len = MLEN; 1158 } 1159 if (datalen >= MINCLSIZE) { 1160 MCLGET(m, M_DONTWAIT); 1161 if ((m->m_flags & M_EXT) == 0) { 1162 if (top) m_freem(top); 1163 return (0); 1164 } 1165 len = MCLBYTES; 1166 } 1167 1168 if (mp == &top) { 1169 caddr_t newdata = (caddr_t) 1170 ALIGN(m->m_data + sizeof(struct ether_header)) - 1171 sizeof(struct ether_header); 1172 len -= newdata - m->m_data; 1173 m->m_data = newdata; 1174 } 1175 1176 m->m_len = len = min(datalen, len); 1177 1178 bcopy(pkt, mtod(m, caddr_t), (unsigned) len); 1179 pkt += len; 1180 datalen -= len; 1181 *mp = m; 1182 mp = &m->m_next; 1183 } 1184 1185 return (top); 1186 } 1187 1188 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; 1189 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf]) 1190 1191 void 1192 sn_get_enaddr(t, h, o, dst) 1193 bus_space_tag_t t; 1194 bus_space_handle_t h; 1195 bus_size_t o; 1196 u_char *dst; 1197 { 1198 int i, do_bbr; 1199 u_char b; 1200 1201 /* 1202 * For reasons known only to Apple, MAC addresses in the ethernet 1203 * PROM are stored in Token Ring (IEEE 802.5) format, that is 1204 * with all of the bits in each byte reversed (canonical bit format). 1205 * When the address is read out it must be reversed to ethernet format 1206 * before use. 1207 * 1208 * Apple has been assigned OUI's 08:00:07 and 00:a0:40. All onboard 1209 * ethernet addresses on 68K machines should be in one of these 1210 * two ranges. 1211 * 1212 * Here is where it gets complicated. 1213 * 1214 * The PMac 7200, 7500, 8500, and 9500 accidentally had the PROM 1215 * written in standard ethernet format. The MacOS accounted for this 1216 * in these systems, and did not reverse the bytes. Some other 1217 * networking utilities were not so forgiving, and got confused. 1218 * "Some" of Apple's Nubus ethernet cards also had their bits 1219 * burned in ethernet format. 1220 * 1221 * Apple petitioned the IEEE and was granted the 00:05:02 (bit reversal 1222 * of 00:a0:40) as well. As of OpenTransport 1.1.1, Apple removed 1223 * their workaround and now reverses the bits regardless of 1224 * what kind of machine it is. So PMac systems and the affected 1225 * Nubus cards now use 00:05:02, instead of the 00:a0:40 for which they 1226 * were intended. 1227 * 1228 * See Apple Techinfo article TECHINFO-0020552, "OpenTransport 1.1.1 1229 * and MacOS System 7.5.3 FAQ (10/96)" for more details. 1230 */ 1231 do_bbr = 0; 1232 b = bus_space_read_1(t, h, o); 1233 if (b == 0x10) 1234 do_bbr = 1; 1235 dst[0] = (do_bbr) ? bbr(b) : b; 1236 1237 for (i = 1 ; i < ETHER_ADDR_LEN ; i++) { 1238 b = bus_space_read_1(t, h, o+i); 1239 dst[i] = (do_bbr) ? bbr(b) : b; 1240 } 1241 } 1242