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