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