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