xref: /netbsd-src/sys/dev/ic/lance.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: lance.c,v 1.44 2010/01/19 22:06:24 pooka 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.44 2010/01/19 22:06:24 pooka Exp $");
69 
70 #include "rnd.h"
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mbuf.h>
75 #include <sys/syslog.h>
76 #include <sys/socket.h>
77 #include <sys/device.h>
78 #include <sys/malloc.h>
79 #include <sys/ioctl.h>
80 #include <sys/errno.h>
81 #if NRND > 0
82 #include <sys/rnd.h>
83 #endif
84 
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_ether.h>
88 #include <net/if_media.h>
89 
90 
91 #include <net/bpf.h>
92 #include <net/bpfdesc.h>
93 
94 #include <dev/ic/lancereg.h>
95 #include <dev/ic/lancevar.h>
96 
97 #if defined(_KERNEL_OPT)
98 #include "opt_ddb.h"
99 #endif
100 
101 #ifdef DDB
102 #define	integrate
103 #define hide
104 #else
105 #define	integrate	static inline
106 #define hide		static
107 #endif
108 
109 integrate struct mbuf *lance_get(struct lance_softc *, int, int);
110 
111 hide bool lance_shutdown(device_t, int);
112 
113 int lance_mediachange(struct ifnet *);
114 void lance_mediastatus(struct ifnet *, struct ifmediareq *);
115 
116 static inline u_int16_t ether_cmp(void *, void *);
117 
118 void lance_stop(struct ifnet *, int);
119 int lance_ioctl(struct ifnet *, u_long, void *);
120 void lance_watchdog(struct ifnet *);
121 
122 /*
123  * Compare two Ether/802 addresses for equality, inlined and
124  * unrolled for speed.  Use this like memcmp().
125  *
126  * XXX: Add <machine/inlines.h> for stuff like this?
127  * XXX: or maybe add it to libkern.h instead?
128  *
129  * "I'd love to have an inline assembler version of this."
130  * XXX: Who wanted that? mycroft?  I wrote one, but this
131  * version in C is as good as hand-coded assembly. -gwr
132  *
133  * Please do NOT tweak this without looking at the actual
134  * assembly code generated before and after your tweaks!
135  */
136 static inline uint16_t
137 ether_cmp(void *one, void *two)
138 {
139 	uint16_t *a = (uint16_t *)one;
140 	uint16_t *b = (uint16_t *)two;
141 	uint16_t diff;
142 
143 #ifdef	m68k
144 	/*
145 	 * The post-increment-pointer form produces the best
146 	 * machine code for m68k.  This was carefully tuned
147 	 * so it compiles to just 8 short (2-byte) op-codes!
148 	 */
149 	diff  = *a++ - *b++;
150 	diff |= *a++ - *b++;
151 	diff |= *a++ - *b++;
152 #else
153 	/*
154 	 * Most modern CPUs do better with a single expresion.
155 	 * Note that short-cut evaluation is NOT helpful here,
156 	 * because it just makes the code longer, not faster!
157 	 */
158 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
159 #endif
160 
161 	return (diff);
162 }
163 
164 #define ETHER_CMP	ether_cmp
165 
166 #ifdef LANCE_REVC_BUG
167 /* Make sure this is short-aligned, for ether_cmp(). */
168 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
169 #endif
170 
171 void
172 lance_config(struct lance_softc *sc)
173 {
174 	int i, nbuf;
175 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
176 
177 	/* Initialize ifnet structure. */
178 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
179 	ifp->if_softc = sc;
180 	ifp->if_start = sc->sc_start;
181 	ifp->if_ioctl = lance_ioctl;
182 	ifp->if_watchdog = lance_watchdog;
183 	ifp->if_init = lance_init;
184 	ifp->if_stop = lance_stop;
185 	ifp->if_flags =
186 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
187 #ifdef LANCE_REVC_BUG
188 	ifp->if_flags &= ~IFF_MULTICAST;
189 #endif
190 	IFQ_SET_READY(&ifp->if_snd);
191 
192 	/* Initialize ifmedia structures. */
193 	ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
194 	if (sc->sc_supmedia != NULL) {
195 		for (i = 0; i < sc->sc_nsupmedia; i++)
196 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
197 			   0, NULL);
198 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
199 	} else {
200 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
201 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
202 	}
203 
204 	switch (sc->sc_memsize) {
205 	case 8192:
206 		sc->sc_nrbuf = 4;
207 		sc->sc_ntbuf = 1;
208 		break;
209 	case 16384:
210 		sc->sc_nrbuf = 8;
211 		sc->sc_ntbuf = 2;
212 		break;
213 	case 32768:
214 		sc->sc_nrbuf = 16;
215 		sc->sc_ntbuf = 4;
216 		break;
217 	case 65536:
218 		sc->sc_nrbuf = 32;
219 		sc->sc_ntbuf = 8;
220 		break;
221 	case 131072:
222 		sc->sc_nrbuf = 64;
223 		sc->sc_ntbuf = 16;
224 		break;
225 	case 262144:
226 		sc->sc_nrbuf = 128;
227 		sc->sc_ntbuf = 32;
228 		break;
229 	default:
230 		/* weird memory size; cope with it */
231 		nbuf = sc->sc_memsize / LEBLEN;
232 		sc->sc_ntbuf = nbuf / 5;
233 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
234 	}
235 
236 	aprint_normal(": address %s\n", ether_sprintf(sc->sc_enaddr));
237 	aprint_normal_dev(sc->sc_dev,
238 	    "%d receive buffers, %d transmit buffers\n",
239 	    sc->sc_nrbuf, sc->sc_ntbuf);
240 
241 	/* Make sure the chip is stopped. */
242 	lance_stop(ifp, 0);
243 
244 	/* claim 802.1q capability */
245 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
246 	/* Attach the interface. */
247 	if_attach(ifp);
248 	ether_ifattach(ifp, sc->sc_enaddr);
249 
250 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, lance_shutdown))
251 		pmf_class_network_register(sc->sc_dev, ifp);
252 	else
253 		aprint_error_dev(sc->sc_dev,
254 		    "couldn't establish power handler\n");
255 
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 	/*
482 	 * Check if there's a BPF listener on this interface.
483 	 * If so, hand off the raw packet to BPF.
484 	 */
485 	if (ifp->if_bpf)
486 		bpf_ops->bpf_mtap(ifp->if_bpf, m);
487 
488 	/* Pass the packet up. */
489 	(*ifp->if_input)(ifp, m);
490 }
491 
492 #undef	ifp
493 
494 void
495 lance_watchdog(struct ifnet *ifp)
496 {
497 	struct lance_softc *sc = ifp->if_softc;
498 
499 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
500 	++ifp->if_oerrors;
501 
502 	lance_reset(sc);
503 }
504 
505 int
506 lance_mediachange(struct ifnet *ifp)
507 {
508 	struct lance_softc *sc = ifp->if_softc;
509 
510 	if (sc->sc_mediachange)
511 		return ((*sc->sc_mediachange)(sc));
512 	return (0);
513 }
514 
515 void
516 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
517 {
518 	struct lance_softc *sc = ifp->if_softc;
519 
520 	if ((ifp->if_flags & IFF_UP) == 0)
521 		return;
522 
523 	ifmr->ifm_status = IFM_AVALID;
524 	if (sc->sc_havecarrier)
525 		ifmr->ifm_status |= IFM_ACTIVE;
526 
527 	if (sc->sc_mediastatus)
528 		(*sc->sc_mediastatus)(sc, ifmr);
529 }
530 
531 /*
532  * Process an ioctl request.
533  */
534 int
535 lance_ioctl(struct ifnet *ifp, u_long cmd, void *data)
536 {
537 	struct lance_softc *sc = ifp->if_softc;
538 	struct ifreq *ifr = (struct ifreq *)data;
539 	int s, error = 0;
540 
541 	s = splnet();
542 
543 	switch (cmd) {
544 	case SIOCGIFMEDIA:
545 	case SIOCSIFMEDIA:
546 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
547 		break;
548 	default:
549 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
550 			break;
551 		error = 0;
552 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
553 			break;
554 		if (ifp->if_flags & IFF_RUNNING) {
555 			/*
556 			 * Multicast list has changed; set the hardware filter
557 			 * accordingly.
558 			 */
559 			lance_reset(sc);
560 		}
561 		break;
562 
563 	}
564 
565 	splx(s);
566 	return (error);
567 }
568 
569 hide bool
570 lance_shutdown(device_t self, int howto)
571 {
572 	struct lance_softc *sc = device_private(self);
573 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
574 
575 	lance_stop(ifp, 0);
576 
577 	return true;
578 }
579 
580 /*
581  * Set up the logical address filter.
582  */
583 void
584 lance_setladrf(struct ethercom *ac, uint16_t *af)
585 {
586 	struct ifnet *ifp = &ac->ec_if;
587 	struct ether_multi *enm;
588 	uint32_t crc;
589 	struct ether_multistep step;
590 
591 	/*
592 	 * Set up multicast address filter by passing all multicast addresses
593 	 * through a crc generator, and then using the high order 6 bits as an
594 	 * index into the 64 bit logical address filter.  The high order bit
595 	 * selects the word, while the rest of the bits select the bit within
596 	 * the word.
597 	 */
598 
599 	if (ifp->if_flags & IFF_PROMISC)
600 		goto allmulti;
601 
602 	af[0] = af[1] = af[2] = af[3] = 0x0000;
603 	ETHER_FIRST_MULTI(step, ac, enm);
604 	while (enm != NULL) {
605 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
606 			/*
607 			 * We must listen to a range of multicast addresses.
608 			 * For now, just accept all multicasts, rather than
609 			 * trying to set only those filter bits needed to match
610 			 * the range.  (At this time, the only use of address
611 			 * ranges is for IP multicast routing, for which the
612 			 * range is big enough to require all bits set.)
613 			 */
614 			goto allmulti;
615 		}
616 
617 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
618 
619 		/* Just want the 6 most significant bits. */
620 		crc >>= 26;
621 
622 		/* Set the corresponding bit in the filter. */
623 		af[crc >> 4] |= 1 << (crc & 0xf);
624 
625 		ETHER_NEXT_MULTI(step, enm);
626 	}
627 	ifp->if_flags &= ~IFF_ALLMULTI;
628 	return;
629 
630 allmulti:
631 	ifp->if_flags |= IFF_ALLMULTI;
632 	af[0] = af[1] = af[2] = af[3] = 0xffff;
633 }
634 
635 /*
636  * Routines for accessing the transmit and receive buffers.
637  * The various CPU and adapter configurations supported by this
638  * driver require three different access methods for buffers
639  * and descriptors:
640  *	(1) contig (contiguous data; no padding),
641  *	(2) gap2 (two bytes of data followed by two bytes of padding),
642  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
643  */
644 
645 /*
646  * contig: contiguous data with no padding.
647  *
648  * Buffers may have any alignment.
649  */
650 
651 void
652 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
653 {
654 	uint8_t *buf = sc->sc_mem;
655 
656 	/*
657 	 * Just call memcpy() to do the work.
658 	 */
659 	memcpy(buf + boff, from, len);
660 }
661 
662 void
663 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
664 {
665 	uint8_t *buf = sc->sc_mem;
666 
667 	/*
668 	 * Just call memcpy() to do the work.
669 	 */
670 	memcpy(to, buf + boff, len);
671 }
672 
673 void
674 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
675 {
676 	uint8_t *buf = sc->sc_mem;
677 
678 	/*
679 	 * Just let memset() do the work
680 	 */
681 	memset(buf + boff, 0, len);
682 }
683 
684 #if 0
685 /*
686  * Examples only; duplicate these and tweak (if necessary) in
687  * machine-specific front-ends.
688  */
689 
690 /*
691  * gap2: two bytes of data followed by two bytes of pad.
692  *
693  * Buffers must be 4-byte aligned.  The code doesn't worry about
694  * doing an extra byte.
695  */
696 
697 void
698 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
699 {
700 	volatile void *buf = sc->sc_mem;
701 	void *from = fromv;
702 	volatile uint16_t *bptr;
703 
704 	if (boff & 0x1) {
705 		/* handle unaligned first byte */
706 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
707 		*bptr = (*from++ << 8) | (*bptr & 0xff);
708 		bptr += 2;
709 		len--;
710 	} else
711 		bptr = ((volatile uint16_t *)buf) + boff;
712 	while (len > 1) {
713 		*bptr = (from[1] << 8) | (from[0] & 0xff);
714 		bptr += 2;
715 		from += 2;
716 		len -= 2;
717 	}
718 	if (len == 1)
719 		*bptr = (uint16_t)*from;
720 }
721 
722 void
723 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
724 {
725 	volatile void *buf = sc->sc_mem;
726 	void *to = tov;
727 	volatile uint16_t *bptr;
728 	uint16_t tmp;
729 
730 	if (boff & 0x1) {
731 		/* handle unaligned first byte */
732 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
733 		*to++ = (*bptr >> 8) & 0xff;
734 		bptr += 2;
735 		len--;
736 	} else
737 		bptr = ((volatile uint16_t *)buf) + boff;
738 	while (len > 1) {
739 		tmp = *bptr;
740 		*to++ = tmp & 0xff;
741 		*to++ = (tmp >> 8) & 0xff;
742 		bptr += 2;
743 		len -= 2;
744 	}
745 	if (len == 1)
746 		*to = *bptr & 0xff;
747 }
748 
749 void
750 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
751 {
752 	volatile void *buf = sc->sc_mem;
753 	volatile uint16_t *bptr;
754 
755 	if ((unsigned int)boff & 0x1) {
756 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
757 		*bptr &= 0xff;
758 		bptr += 2;
759 		len--;
760 	} else
761 		bptr = ((volatile uint16_t *)buf) + boff;
762 	while (len > 0) {
763 		*bptr = 0;
764 		bptr += 2;
765 		len -= 2;
766 	}
767 }
768 
769 /*
770  * gap16: 16 bytes of data followed by 16 bytes of pad.
771  *
772  * Buffers must be 32-byte aligned.
773  */
774 
775 void
776 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
777 {
778 	volatile uint8_t *buf = sc->sc_mem;
779 	void *from = fromv;
780 	uint8_t *bptr;
781 	int xfer;
782 
783 	bptr = buf + ((boff << 1) & ~0x1f);
784 	boff &= 0xf;
785 	xfer = min(len, 16 - boff);
786 	while (len > 0) {
787 		memcpy(bptr + boff, from, xfer);
788 		from += xfer;
789 		bptr += 32;
790 		boff = 0;
791 		len -= xfer;
792 		xfer = min(len, 16);
793 	}
794 }
795 
796 void
797 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
798 {
799 	volatile uint8_t *buf = sc->sc_mem;
800 	void *to = tov;
801 	uint8_t *bptr;
802 	int xfer;
803 
804 	bptr = buf + ((boff << 1) & ~0x1f);
805 	boff &= 0xf;
806 	xfer = min(len, 16 - boff);
807 	while (len > 0) {
808 		memcpy(to, bptr + boff, xfer);
809 		to += xfer;
810 		bptr += 32;
811 		boff = 0;
812 		len -= xfer;
813 		xfer = min(len, 16);
814 	}
815 }
816 
817 void
818 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
819 {
820 	volatile uint8_t *buf = sc->sc_mem;
821 	uint8_t *bptr;
822 	int xfer;
823 
824 	bptr = buf + ((boff << 1) & ~0x1f);
825 	boff &= 0xf;
826 	xfer = min(len, 16 - boff);
827 	while (len > 0) {
828 		memset(bptr + boff, 0, xfer);
829 		bptr += 32;
830 		boff = 0;
831 		len -= xfer;
832 		xfer = min(len, 16);
833 	}
834 }
835 #endif /* Example only */
836