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