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