1 /* $NetBSD: lance.c,v 1.34 2005/12/24 20:27:30 perry Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 9 * Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /*- 41 * Copyright (c) 1992, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * This code is derived from software contributed to Berkeley by 45 * Ralph Campbell and Rick Macklem. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)if_le.c 8.2 (Berkeley) 11/16/93 72 */ 73 74 #include <sys/cdefs.h> 75 __FBSDID("$FreeBSD$"); 76 77 #include <sys/param.h> 78 #include <sys/bus.h> 79 #include <sys/endian.h> 80 #include <sys/lock.h> 81 #include <sys/mbuf.h> 82 #include <sys/mutex.h> 83 #include <sys/socket.h> 84 #include <sys/sockio.h> 85 86 #include <net/ethernet.h> 87 #include <net/if.h> 88 #include <net/if_arp.h> 89 #include <net/if_dl.h> 90 #include <net/if_media.h> 91 #include <net/if_types.h> 92 #include <net/if_vlan_var.h> 93 94 #include <dev/le/lancereg.h> 95 #include <dev/le/lancevar.h> 96 97 devclass_t le_devclass; 98 99 static inline uint16_t ether_cmp(void *, void *); 100 101 static void lance_start(struct ifnet *); 102 static void lance_stop(struct lance_softc *); 103 static void lance_init(void *); 104 static struct mbuf *lance_get(struct lance_softc *, int, int); 105 static void lance_watchdog(struct ifnet *); 106 static int lance_mediachange(struct ifnet *); 107 static void lance_mediastatus(struct ifnet *, struct ifmediareq *); 108 static int lance_ioctl(struct ifnet *, u_long, caddr_t); 109 110 /* 111 * Compare two Ether/802 addresses for equality, inlined and 112 * unrolled for speed. Use this like memcmp(). 113 * 114 * XXX: Add <machine/inlines.h> for stuff like this? 115 * XXX: or maybe add it to libkern.h instead? 116 * 117 * "I'd love to have an inline assembler version of this." 118 * XXX: Who wanted that? mycroft? I wrote one, but this 119 * version in C is as good as hand-coded assembly. -gwr 120 * 121 * Please do NOT tweak this without looking at the actual 122 * assembly code generated before and after your tweaks! 123 */ 124 static inline uint16_t 125 ether_cmp(void *one, void *two) 126 { 127 uint16_t *a = (u_short *)one; 128 uint16_t *b = (u_short *)two; 129 uint16_t diff; 130 131 #ifdef m68k 132 /* 133 * The post-increment-pointer form produces the best 134 * machine code for m68k. This was carefully tuned 135 * so it compiles to just 8 short (2-byte) op-codes! 136 */ 137 diff = *a++ - *b++; 138 diff |= *a++ - *b++; 139 diff |= *a++ - *b++; 140 #else 141 /* 142 * Most modern CPUs do better with a single expresion. 143 * Note that short-cut evaluation is NOT helpful here, 144 * because it just makes the code longer, not faster! 145 */ 146 diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]); 147 #endif 148 149 return (diff); 150 } 151 152 #define ETHER_CMP ether_cmp 153 154 #ifdef LANCE_REVC_BUG 155 /* Make sure this is short-aligned, for ether_cmp(). */ 156 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 }; 157 #endif 158 159 int 160 lance_config(struct lance_softc *sc, const char* name, int unit) 161 { 162 struct ifnet *ifp; 163 int i, nbuf; 164 165 if (LE_LOCK_INITIALIZED(sc) == 0) 166 return (ENXIO); 167 168 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 169 if (ifp == NULL) 170 return (ENOSPC); 171 172 /* Initialize ifnet structure. */ 173 ifp->if_softc = sc; 174 if_initname(ifp, name, unit); 175 ifp->if_start = lance_start; 176 ifp->if_ioctl = lance_ioctl; 177 ifp->if_watchdog = lance_watchdog; 178 ifp->if_init = lance_init; 179 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 180 #ifdef LANCE_REVC_BUG 181 ifp->if_flags &= ~IFF_MULTICAST; 182 #endif 183 ifp->if_baudrate = IF_Mbps(10); 184 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 185 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 186 IFQ_SET_READY(&ifp->if_snd); 187 188 /* Initialize ifmedia structures. */ 189 ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus); 190 if (sc->sc_supmedia != NULL) { 191 for (i = 0; i < sc->sc_nsupmedia; i++) 192 ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL); 193 ifmedia_set(&sc->sc_media, sc->sc_defaultmedia); 194 } else { 195 ifmedia_add(&sc->sc_media, 196 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL); 197 ifmedia_set(&sc->sc_media, 198 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0)); 199 } 200 201 switch (sc->sc_memsize) { 202 case 8192: 203 sc->sc_nrbuf = 4; 204 sc->sc_ntbuf = 1; 205 break; 206 case 16384: 207 sc->sc_nrbuf = 8; 208 sc->sc_ntbuf = 2; 209 break; 210 case 32768: 211 sc->sc_nrbuf = 16; 212 sc->sc_ntbuf = 4; 213 break; 214 case 65536: 215 sc->sc_nrbuf = 32; 216 sc->sc_ntbuf = 8; 217 break; 218 case 131072: 219 sc->sc_nrbuf = 64; 220 sc->sc_ntbuf = 16; 221 break; 222 case 262144: 223 sc->sc_nrbuf = 128; 224 sc->sc_ntbuf = 32; 225 break; 226 default: 227 /* weird memory size; cope with it */ 228 nbuf = sc->sc_memsize / LEBLEN; 229 sc->sc_ntbuf = nbuf / 5; 230 sc->sc_nrbuf = nbuf - sc->sc_ntbuf; 231 } 232 233 if_printf(ifp, "%d receive buffers, %d transmit buffers\n", 234 sc->sc_nrbuf, sc->sc_ntbuf); 235 236 /* Make sure the chip is stopped. */ 237 LE_LOCK(sc); 238 lance_stop(sc); 239 LE_UNLOCK(sc); 240 241 return (0); 242 } 243 244 void 245 lance_attach(struct lance_softc *sc) 246 { 247 struct ifnet *ifp = sc->sc_ifp; 248 249 /* Attach the interface. */ 250 ether_ifattach(ifp, sc->sc_enaddr); 251 252 /* Claim 802.1q capability. */ 253 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 254 ifp->if_capabilities |= IFCAP_VLAN_MTU; 255 ifp->if_capenable |= IFCAP_VLAN_MTU; 256 } 257 258 void 259 lance_detach(struct lance_softc *sc) 260 { 261 struct ifnet *ifp = sc->sc_ifp; 262 263 LE_LOCK(sc); 264 lance_stop(sc); 265 LE_UNLOCK(sc); 266 ether_ifdetach(ifp); 267 if_free(ifp); 268 } 269 270 void 271 lance_suspend(struct lance_softc *sc) 272 { 273 274 LE_LOCK(sc); 275 lance_stop(sc); 276 LE_UNLOCK(sc); 277 } 278 279 void 280 lance_resume(struct lance_softc *sc) 281 { 282 283 LE_LOCK(sc); 284 if (sc->sc_ifp->if_flags & IFF_UP) 285 lance_init_locked(sc); 286 LE_UNLOCK(sc); 287 } 288 289 static void 290 lance_start(struct ifnet *ifp) 291 { 292 struct lance_softc *sc = ifp->if_softc; 293 294 LE_LOCK(sc); 295 (*sc->sc_start_locked)(sc); 296 LE_UNLOCK(sc); 297 } 298 299 static void 300 lance_stop(struct lance_softc *sc) 301 { 302 struct ifnet *ifp = sc->sc_ifp; 303 304 LE_LOCK_ASSERT(sc, MA_OWNED); 305 306 /* 307 * Mark the interface down and cancel the watchdog timer. 308 */ 309 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 310 ifp->if_timer = 0; 311 312 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); 313 } 314 315 static void 316 lance_init(void *xsc) 317 { 318 struct lance_softc *sc = (struct lance_softc *)xsc; 319 320 LE_LOCK(sc); 321 lance_init_locked(sc); 322 LE_UNLOCK(sc); 323 } 324 325 /* 326 * Initialization of interface; set up initialization block 327 * and transmit/receive descriptor rings. 328 */ 329 void 330 lance_init_locked(struct lance_softc *sc) 331 { 332 struct ifnet *ifp = sc->sc_ifp; 333 u_long a; 334 int timo; 335 336 LE_LOCK_ASSERT(sc, MA_OWNED); 337 338 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); 339 DELAY(100); 340 341 /* Newer LANCE chips have a reset register. */ 342 if (sc->sc_hwreset) 343 (*sc->sc_hwreset)(sc); 344 345 /* Set the correct byte swapping mode, etc. */ 346 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3); 347 348 /* 349 * Update our private copy of the Ethernet address. 350 * We NEED the copy so we can ensure its alignment! 351 */ 352 memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 353 354 /* Set up LANCE init block. */ 355 (*sc->sc_meminit)(sc); 356 357 /* Give LANCE the physical address of its init block. */ 358 a = sc->sc_addr + LE_INITADDR(sc); 359 (*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff); 360 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16); 361 362 /* Try to initialize the LANCE. */ 363 DELAY(100); 364 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT); 365 366 /* Wait for initialization to finish. */ 367 for (timo = 100000; timo; timo--) 368 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) 369 break; 370 371 /* Set the current media. */ 372 if (sc->sc_mediachange) 373 (void)(*sc->sc_mediachange)(sc); 374 375 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) { 376 /* Start the LANCE. */ 377 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT); 378 ifp->if_drv_flags |= IFF_DRV_RUNNING; 379 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 380 ifp->if_timer = 0; 381 (*sc->sc_start_locked)(sc); 382 } else 383 if_printf(ifp, "controller failed to initialize\n"); 384 385 if (sc->sc_hwinit) 386 (*sc->sc_hwinit)(sc); 387 } 388 389 /* 390 * Routine to copy from mbuf chain to transmit buffer in 391 * network buffer memory. 392 */ 393 int 394 lance_put(struct lance_softc *sc, int boff, struct mbuf *m) 395 { 396 struct mbuf *n; 397 int len, tlen = 0; 398 399 LE_LOCK_ASSERT(sc, MA_OWNED); 400 401 for (; m; m = n) { 402 len = m->m_len; 403 if (len == 0) { 404 n = m_free(m); 405 m = NULL; 406 continue; 407 } 408 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len); 409 boff += len; 410 tlen += len; 411 n = m_free(m); 412 m = NULL; 413 } 414 if (tlen < LEMINSIZE) { 415 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen); 416 tlen = LEMINSIZE; 417 } 418 return (tlen); 419 } 420 421 /* 422 * Pull data off an interface. 423 * Len is length of data, with local net header stripped. 424 * We copy the data into mbufs. When full cluster sized units are present 425 * we copy into clusters. 426 */ 427 static struct mbuf * 428 lance_get(struct lance_softc *sc, int boff, int totlen) 429 { 430 struct ifnet *ifp = sc->sc_ifp; 431 struct mbuf *m, *m0, *newm; 432 caddr_t newdata; 433 int len; 434 435 MGETHDR(m0, M_DONTWAIT, MT_DATA); 436 if (m0 == 0) 437 return (0); 438 m0->m_pkthdr.rcvif = ifp; 439 m0->m_pkthdr.len = totlen; 440 len = MHLEN; 441 m = m0; 442 443 while (totlen > 0) { 444 if (totlen >= MINCLSIZE) { 445 MCLGET(m, M_DONTWAIT); 446 if ((m->m_flags & M_EXT) == 0) 447 goto bad; 448 len = MCLBYTES; 449 } 450 451 if (m == m0) { 452 newdata = (caddr_t) 453 ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN; 454 len -= newdata - m->m_data; 455 m->m_data = newdata; 456 } 457 458 m->m_len = len = min(totlen, len); 459 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len); 460 boff += len; 461 462 totlen -= len; 463 if (totlen > 0) { 464 MGET(newm, M_DONTWAIT, MT_DATA); 465 if (newm == 0) 466 goto bad; 467 len = MLEN; 468 m = m->m_next = newm; 469 } 470 } 471 472 return (m0); 473 474 bad: 475 m_freem(m0); 476 return (0); 477 } 478 479 /* 480 * Pass a packet to the higher levels. 481 */ 482 void 483 lance_read(struct lance_softc *sc, int boff, int len) 484 { 485 struct ifnet *ifp = sc->sc_ifp; 486 struct ether_header *eh; 487 struct mbuf *m; 488 489 LE_LOCK_ASSERT(sc, MA_OWNED); 490 491 if (len <= ETHER_HDR_LEN || len > LEBLEN - ETHER_CRC_LEN) { 492 #ifdef LEDEBUG 493 if_printf(ifp, "invalid packet size %d; dropping\n", len); 494 #endif 495 ifp->if_ierrors++; 496 return; 497 } 498 499 /* Pull packet off interface. */ 500 m = lance_get(sc, boff, len); 501 if (m == 0) { 502 ifp->if_ierrors++; 503 return; 504 } 505 506 ifp->if_ipackets++; 507 508 eh = mtod(m, struct ether_header *); 509 510 #ifdef LANCE_REVC_BUG 511 /* 512 * The old LANCE (Rev. C) chips have a bug which causes 513 * garbage to be inserted in front of the received packet. 514 * The work-around is to ignore packets with an invalid 515 * destination address (garbage will usually not match). 516 * Of course, this precludes multicast support... 517 */ 518 if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) && 519 ETHER_CMP(eh->ether_dhost, bcast_enaddr)) { 520 m_freem(m); 521 return; 522 } 523 #endif 524 525 /* 526 * Some lance device does not present IFF_SIMPLEX behavior on multicast 527 * packets. Make sure to drop it if it is from ourselves. 528 */ 529 if (!ETHER_CMP(eh->ether_shost, sc->sc_enaddr)) { 530 m_freem(m); 531 return; 532 } 533 534 /* Pass the packet up. */ 535 LE_UNLOCK(sc); 536 (*ifp->if_input)(ifp, m); 537 LE_LOCK(sc); 538 } 539 540 static void 541 lance_watchdog(struct ifnet *ifp) 542 { 543 struct lance_softc *sc = ifp->if_softc; 544 545 LE_LOCK(sc); 546 if_printf(ifp, "device timeout\n"); 547 ++ifp->if_oerrors; 548 lance_init_locked(sc); 549 LE_UNLOCK(sc); 550 } 551 552 static int 553 lance_mediachange(struct ifnet *ifp) 554 { 555 struct lance_softc *sc = ifp->if_softc; 556 int error; 557 558 if (sc->sc_mediachange) { 559 LE_LOCK(sc); 560 error = (*sc->sc_mediachange)(sc); 561 LE_UNLOCK(sc); 562 return (error); 563 } 564 return (0); 565 } 566 567 static void 568 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 569 { 570 struct lance_softc *sc = ifp->if_softc; 571 572 LE_LOCK(sc); 573 if (!(ifp->if_flags & IFF_UP)) { 574 LE_UNLOCK(sc); 575 return; 576 } 577 578 ifmr->ifm_status = IFM_AVALID; 579 if (sc->sc_flags & LE_CARRIER) 580 ifmr->ifm_status |= IFM_ACTIVE; 581 582 if (sc->sc_mediastatus) 583 (*sc->sc_mediastatus)(sc, ifmr); 584 LE_UNLOCK(sc); 585 } 586 587 /* 588 * Process an ioctl request. 589 */ 590 static int 591 lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 592 { 593 struct lance_softc *sc = ifp->if_softc; 594 struct ifreq *ifr = (struct ifreq *)data; 595 int error = 0; 596 597 switch (cmd) { 598 case SIOCSIFFLAGS: 599 LE_LOCK(sc); 600 if (ifp->if_flags & IFF_PROMISC) { 601 if (!(sc->sc_flags & LE_PROMISC)) { 602 sc->sc_flags |= LE_PROMISC; 603 lance_init_locked(sc); 604 } 605 } else if (sc->sc_flags & LE_PROMISC) { 606 sc->sc_flags &= ~LE_PROMISC; 607 lance_init_locked(sc); 608 } 609 610 if ((ifp->if_flags & IFF_ALLMULTI) && 611 !(sc->sc_flags & LE_ALLMULTI)) { 612 sc->sc_flags |= LE_ALLMULTI; 613 lance_init_locked(sc); 614 } else if (!(ifp->if_flags & IFF_ALLMULTI) && 615 (sc->sc_flags & LE_ALLMULTI)) { 616 sc->sc_flags &= ~LE_ALLMULTI; 617 lance_init_locked(sc); 618 } 619 620 if (!(ifp->if_flags & IFF_UP) && 621 ifp->if_drv_flags & IFF_DRV_RUNNING) { 622 /* 623 * If interface is marked down and it is running, then 624 * stop it. 625 */ 626 lance_stop(sc); 627 } else if (ifp->if_flags & IFF_UP && 628 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 629 /* 630 * If interface is marked up and it is stopped, then 631 * start it. 632 */ 633 lance_init_locked(sc); 634 } 635 #ifdef LEDEBUG 636 if (ifp->if_flags & IFF_DEBUG) 637 sc->sc_flags |= LE_DEBUG; 638 else 639 sc->sc_flags &= ~LE_DEBUG; 640 #endif 641 LE_UNLOCK(sc); 642 break; 643 644 case SIOCADDMULTI: 645 case SIOCDELMULTI: 646 /* 647 * Multicast list has changed; set the hardware filter 648 * accordingly. 649 */ 650 LE_LOCK(sc); 651 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 652 lance_init_locked(sc); 653 LE_UNLOCK(sc); 654 break; 655 656 case SIOCGIFMEDIA: 657 case SIOCSIFMEDIA: 658 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 659 break; 660 661 default: 662 error = ether_ioctl(ifp, cmd, data); 663 break; 664 } 665 666 return (error); 667 } 668 669 /* 670 * Set up the logical address filter. 671 */ 672 void 673 lance_setladrf(struct lance_softc *sc, uint16_t *af) 674 { 675 struct ifnet *ifp = sc->sc_ifp; 676 struct ifmultiaddr *ifma; 677 uint32_t crc; 678 679 /* 680 * Set up multicast address filter by passing all multicast addresses 681 * through a crc generator, and then using the high order 6 bits as an 682 * index into the 64 bit logical address filter. The high order bit 683 * selects the word, while the rest of the bits select the bit within 684 * the word. 685 */ 686 687 if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) { 688 af[0] = af[1] = af[2] = af[3] = 0xffff; 689 return; 690 } 691 692 af[0] = af[1] = af[2] = af[3] = 0x0000; 693 IF_ADDR_LOCK(ifp); 694 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 695 if (ifma->ifma_addr->sa_family != AF_LINK) 696 continue; 697 698 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *) 699 ifma->ifma_addr), ETHER_ADDR_LEN); 700 701 /* Just want the 6 most significant bits. */ 702 crc >>= 26; 703 704 /* Set the corresponding bit in the filter. */ 705 af[crc >> 4] |= LE_HTOLE16(1U << (crc & 0xf)); 706 } 707 IF_ADDR_UNLOCK(ifp); 708 } 709 710 /* 711 * Routines for accessing the transmit and receive buffers. 712 * The various CPU and adapter configurations supported by this 713 * driver require three different access methods for buffers 714 * and descriptors: 715 * (1) contig (contiguous data; no padding), 716 * (2) gap2 (two bytes of data followed by two bytes of padding), 717 * (3) gap16 (16 bytes of data followed by 16 bytes of padding). 718 */ 719 720 /* 721 * contig: contiguous data with no padding. 722 * 723 * Buffers may have any alignment. 724 */ 725 726 void 727 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len) 728 { 729 volatile caddr_t buf = sc->sc_mem; 730 731 /* 732 * Just call memcpy() to do the work. 733 */ 734 memcpy(buf + boff, from, len); 735 } 736 737 void 738 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len) 739 { 740 volatile caddr_t buf = sc->sc_mem; 741 742 /* 743 * Just call memcpy() to do the work. 744 */ 745 memcpy(to, buf + boff, len); 746 } 747 748 void 749 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len) 750 { 751 volatile caddr_t buf = sc->sc_mem; 752 753 /* 754 * Just let memset() do the work 755 */ 756 memset(buf + boff, 0, len); 757 } 758 759 #if 0 760 /* 761 * Examples only; duplicate these and tweak (if necessary) in 762 * machine-specific front-ends. 763 */ 764 765 /* 766 * gap2: two bytes of data followed by two bytes of pad. 767 * 768 * Buffers must be 4-byte aligned. The code doesn't worry about 769 * doing an extra byte. 770 */ 771 772 static void 773 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len) 774 { 775 volatile caddr_t buf = sc->sc_mem; 776 caddr_t from = fromv; 777 volatile uint16_t *bptr; 778 779 if (boff & 0x1) { 780 /* Handle unaligned first byte. */ 781 bptr = ((volatile uint16_t *)buf) + (boff - 1); 782 *bptr = (*from++ << 8) | (*bptr & 0xff); 783 bptr += 2; 784 len--; 785 } else 786 bptr = ((volatile uint16_t *)buf) + boff; 787 while (len > 1) { 788 *bptr = (from[1] << 8) | (from[0] & 0xff); 789 bptr += 2; 790 from += 2; 791 len -= 2; 792 } 793 if (len == 1) 794 *bptr = (uint16_t)*from; 795 } 796 797 static void 798 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len) 799 { 800 volatile caddr_t buf = sc->sc_mem; 801 caddr_t to = tov; 802 volatile uint16_t *bptr; 803 uint16_t tmp; 804 805 if (boff & 0x1) { 806 /* Handle unaligned first byte. */ 807 bptr = ((volatile uint16_t *)buf) + (boff - 1); 808 *to++ = (*bptr >> 8) & 0xff; 809 bptr += 2; 810 len--; 811 } else 812 bptr = ((volatile uint16_t *)buf) + boff; 813 while (len > 1) { 814 tmp = *bptr; 815 *to++ = tmp & 0xff; 816 *to++ = (tmp >> 8) & 0xff; 817 bptr += 2; 818 len -= 2; 819 } 820 if (len == 1) 821 *to = *bptr & 0xff; 822 } 823 824 static void 825 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len) 826 { 827 volatile caddr_t buf = sc->sc_mem; 828 volatile uint16_t *bptr; 829 830 if ((unsigned)boff & 0x1) { 831 bptr = ((volatile uint16_t *)buf) + (boff - 1); 832 *bptr &= 0xff; 833 bptr += 2; 834 len--; 835 } else 836 bptr = ((volatile uint16_t *)buf) + boff; 837 while (len > 0) { 838 *bptr = 0; 839 bptr += 2; 840 len -= 2; 841 } 842 } 843 844 /* 845 * gap16: 16 bytes of data followed by 16 bytes of pad. 846 * 847 * Buffers must be 32-byte aligned. 848 */ 849 850 static void 851 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len) 852 { 853 volatile caddr_t buf = sc->sc_mem; 854 caddr_t bptr, from = fromv; 855 int xfer; 856 857 bptr = buf + ((boff << 1) & ~0x1f); 858 boff &= 0xf; 859 xfer = min(len, 16 - boff); 860 while (len > 0) { 861 memcpy(bptr + boff, from, xfer); 862 from += xfer; 863 bptr += 32; 864 boff = 0; 865 len -= xfer; 866 xfer = min(len, 16); 867 } 868 } 869 870 static void 871 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len) 872 { 873 volatile caddr_t buf = sc->sc_mem; 874 caddr_t bptr, to = tov; 875 int xfer; 876 877 bptr = buf + ((boff << 1) & ~0x1f); 878 boff &= 0xf; 879 xfer = min(len, 16 - boff); 880 while (len > 0) { 881 memcpy(to, bptr + boff, xfer); 882 to += xfer; 883 bptr += 32; 884 boff = 0; 885 len -= xfer; 886 xfer = min(len, 16); 887 } 888 } 889 890 static void 891 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len) 892 { 893 volatile caddr_t buf = sc->sc_mem; 894 caddr_t bptr; 895 int xfer; 896 897 bptr = buf + ((boff << 1) & ~0x1f); 898 boff &= 0xf; 899 xfer = min(len, 16 - boff); 900 while (len > 0) { 901 memset(bptr + boff, 0, xfer); 902 bptr += 32; 903 boff = 0; 904 len -= xfer; 905 xfer = min(len, 16); 906 } 907 } 908 #endif /* Example only */ 909