xref: /dragonfly/sys/dev/netif/lnc/lance.c (revision 685c703c)
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.2 2006/08/06 12:49:05 swildner 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 <machine/bus.h>
95 
96 #include <dev/netif/lnc/lancereg.h>
97 #include <dev/netif/lnc/lancevar.h>
98 
99 devclass_t le_devclass;
100 
101 static void lance_start(struct ifnet *);
102 static void lance_init(void *);
103 static void lance_watchdog(struct ifnet *);
104 static int lance_mediachange(struct ifnet *);
105 static void lance_mediastatus(struct ifnet *, struct ifmediareq *);
106 static int lance_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
107 
108 int
109 lance_config(struct lance_softc *sc, const char* name, int unit)
110 {
111 	struct ifnet *ifp;
112 	int i, nbuf;
113 
114 	ifp = sc->ifp = &sc->sc_if;
115 
116 	/* Initialize ifnet structure. */
117 	ifp->if_softc = sc;
118 	if_initname(ifp, name, unit);
119 	ifp->if_start = lance_start;
120 	ifp->if_ioctl = lance_ioctl;
121 	ifp->if_watchdog = lance_watchdog;
122 	ifp->if_init = lance_init;
123 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
124 #ifdef LANCE_REVC_BUG
125 	ifp->if_flags &= ~IFF_MULTICAST;
126 #endif
127 	ifp->if_baudrate = IF_Mbps(10);
128 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
129 	ifq_set_ready(&ifp->if_snd);
130 
131 	/* Initialize ifmedia structures. */
132 	ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
133 	if (sc->sc_supmedia != NULL) {
134 		for (i = 0; i < sc->sc_nsupmedia; i++)
135 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
136 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
137 	} else {
138 		ifmedia_add(&sc->sc_media,
139 		    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
140 		ifmedia_set(&sc->sc_media,
141 		    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
142 	}
143 
144 	switch (sc->sc_memsize) {
145 	case 8192:
146 		sc->sc_nrbuf = 4;
147 		sc->sc_ntbuf = 1;
148 		break;
149 	case 16384:
150 		sc->sc_nrbuf = 8;
151 		sc->sc_ntbuf = 2;
152 		break;
153 	case 32768:
154 		sc->sc_nrbuf = 16;
155 		sc->sc_ntbuf = 4;
156 		break;
157 	case 65536:
158 		sc->sc_nrbuf = 32;
159 		sc->sc_ntbuf = 8;
160 		break;
161 	case 131072:
162 		sc->sc_nrbuf = 64;
163 		sc->sc_ntbuf = 16;
164 		break;
165 	case 262144:
166 		sc->sc_nrbuf = 128;
167 		sc->sc_ntbuf = 32;
168 		break;
169 	default:
170 		/* weird memory size; cope with it */
171 		nbuf = sc->sc_memsize / LEBLEN;
172 		sc->sc_ntbuf = nbuf / 5;
173 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
174 	}
175 
176 	if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
177 	    sc->sc_nrbuf, sc->sc_ntbuf);
178 
179 	/* Make sure the chip is stopped. */
180 	lance_stop(sc);
181 
182 	return (0);
183 }
184 
185 void
186 lance_attach(struct lance_softc *sc)
187 {
188 	struct ifnet *ifp = sc->ifp;
189 
190 	/* Attach the interface. */
191 	ether_ifattach(ifp, sc->sc_enaddr, NULL);
192 
193 	/* Claim 802.1q capability. */
194 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
195 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
196 	ifp->if_capenable |= IFCAP_VLAN_MTU;
197 }
198 
199 void
200 lance_suspend(struct lance_softc *sc)
201 {
202 
203 	lwkt_serialize_enter(sc->ifp->if_serializer);
204 	lance_stop(sc);
205 	lwkt_serialize_exit(sc->ifp->if_serializer);
206 }
207 
208 void
209 lance_resume(struct lance_softc *sc)
210 {
211 
212 	lwkt_serialize_enter(sc->ifp->if_serializer);
213 	if (sc->ifp->if_flags & IFF_UP)
214 		lance_init_locked(sc);
215 	lwkt_serialize_exit(sc->ifp->if_serializer);
216 }
217 
218 static void
219 lance_start(struct ifnet *ifp)
220 {
221 	struct lance_softc *sc = ifp->if_softc;
222 
223 	(*sc->sc_start_locked)(sc);
224 }
225 
226 void
227 lance_stop(struct lance_softc *sc)
228 {
229 	struct ifnet *ifp = sc->ifp;
230 
231 	/*
232 	 * Mark the interface down and cancel the watchdog timer.
233 	 */
234 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
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 		ifp->if_flags &= ~IFF_OACTIVE;
303 		ifp->if_timer = 0;
304 		(*sc->sc_start_locked)(sc);
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 	struct mbuf *n;
320 	int len, tlen = 0;
321 
322 	for (; m; m = n) {
323 		len = m->m_len;
324 		if (len == 0) {
325 			n = m_free(m);
326 			m = NULL;
327 			continue;
328 		}
329 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
330 		boff += len;
331 		tlen += len;
332 		n = m_free(m);
333 		m = NULL;
334 	}
335 	if (tlen < LEMINSIZE) {
336 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
337 		tlen = LEMINSIZE;
338 	}
339 	return (tlen);
340 }
341 
342 /*
343  * Pull data off an interface.
344  * Len is length of data, with local net header stripped.
345  * We copy the data into mbufs.  When full cluster sized units are present
346  * we copy into clusters.
347  */
348 struct mbuf *
349 lance_get(struct lance_softc *sc, int boff, int totlen)
350 {
351 	struct ifnet *ifp = sc->ifp;
352 	struct mbuf *m, *m0, *newm;
353 	caddr_t newdata;
354 	int len;
355 
356 	if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) {
357 #ifdef LEDEBUG
358 		if_printf(ifp, "invalid packet size %d; dropping\n", totlen);
359 #endif
360 		return (NULL);
361 	}
362 
363 	MGETHDR(m0, MB_DONTWAIT, MT_DATA);
364 	if (m0 == NULL)
365 		return (NULL);
366 	m0->m_pkthdr.rcvif = ifp;
367 	m0->m_pkthdr.len = totlen;
368 	len = MHLEN;
369 	m = m0;
370 
371 	while (totlen > 0) {
372 		if (totlen >= MINCLSIZE) {
373 			MCLGET(m, MB_DONTWAIT);
374 			if ((m->m_flags & M_EXT) == 0)
375 				goto bad;
376 			len = MCLBYTES;
377 		}
378 
379 		if (m == m0) {
380 			newdata = (caddr_t)
381 			    ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
382 			len -= newdata - m->m_data;
383 			m->m_data = newdata;
384 		}
385 
386 		m->m_len = len = min(totlen, len);
387 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
388 		boff += len;
389 
390 		totlen -= len;
391 		if (totlen > 0) {
392 			MGET(newm, MB_DONTWAIT, MT_DATA);
393 			if (newm == 0)
394 				goto bad;
395 			len = MLEN;
396 			m = m->m_next = newm;
397 		}
398 	}
399 
400 	return (m0);
401 
402  bad:
403 	m_freem(m0);
404 	return (NULL);
405 }
406 
407 static void
408 lance_watchdog(struct ifnet *ifp)
409 {
410 	struct lance_softc *sc = ifp->if_softc;
411 
412 	if_printf(ifp, "device timeout\n");
413 	++ifp->if_oerrors;
414 	lance_init_locked(sc);
415 }
416 
417 static int
418 lance_mediachange(struct ifnet *ifp)
419 {
420 	struct lance_softc *sc = ifp->if_softc;
421 	int error;
422 
423 	if (sc->sc_mediachange) {
424 		error = (*sc->sc_mediachange)(sc);
425 		return (error);
426 	}
427 	return (0);
428 }
429 
430 static void
431 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
432 {
433 	struct lance_softc *sc = ifp->if_softc;
434 
435 	if (!(ifp->if_flags & IFF_UP)) {
436 		return;
437 	}
438 
439 	ifmr->ifm_status = IFM_AVALID;
440 	if (sc->sc_flags & LE_CARRIER)
441 		ifmr->ifm_status |= IFM_ACTIVE;
442 
443 	if (sc->sc_mediastatus)
444 		(*sc->sc_mediastatus)(sc, ifmr);
445 }
446 
447 /*
448  * Process an ioctl request.
449  */
450 static int
451 lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
452 {
453 	struct lance_softc *sc = ifp->if_softc;
454 	struct ifreq *ifr = (struct ifreq *)data;
455 	int error = 0;
456 
457 	crit_enter();
458 
459 	switch (cmd) {
460 	case SIOCSIFFLAGS:
461 		if (ifp->if_flags & IFF_PROMISC) {
462 			if (!(sc->sc_flags & LE_PROMISC)) {
463 				sc->sc_flags |= LE_PROMISC;
464 				lance_init_locked(sc);
465 			}
466 		} else if (sc->sc_flags & LE_PROMISC) {
467 			sc->sc_flags &= ~LE_PROMISC;
468 			lance_init_locked(sc);
469 		}
470 
471 		if ((ifp->if_flags & IFF_ALLMULTI) &&
472 		    !(sc->sc_flags & LE_ALLMULTI)) {
473 			sc->sc_flags |= LE_ALLMULTI;
474 			lance_init_locked(sc);
475 		} else if (!(ifp->if_flags & IFF_ALLMULTI) &&
476 		    (sc->sc_flags & LE_ALLMULTI)) {
477 			sc->sc_flags &= ~LE_ALLMULTI;
478 			lance_init_locked(sc);
479 		}
480 
481 		if (!(ifp->if_flags & IFF_UP) &&
482 		    ifp->if_flags & IFF_RUNNING) {
483 			/*
484 			 * If interface is marked down and it is running, then
485 			 * stop it.
486 			 */
487 			lance_stop(sc);
488 		} else if (ifp->if_flags & IFF_UP &&
489 		    !(ifp->if_flags & IFF_RUNNING)) {
490 			/*
491 			 * If interface is marked up and it is stopped, then
492 			 * start it.
493 			 */
494 			lance_init_locked(sc);
495 		}
496 #ifdef LEDEBUG
497 		if (ifp->if_flags & IFF_DEBUG)
498 			sc->sc_flags |= LE_DEBUG;
499 		else
500 			sc->sc_flags &= ~LE_DEBUG;
501 #endif
502 		break;
503 
504 	case SIOCADDMULTI:
505 	case SIOCDELMULTI:
506 		/*
507 		 * Multicast list has changed; set the hardware filter
508 		 * accordingly.
509 		 */
510 		if (ifp->if_flags & IFF_RUNNING)
511 			lance_init_locked(sc);
512 		break;
513 
514 	case SIOCGIFMEDIA:
515 	case SIOCSIFMEDIA:
516 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
517 		break;
518 
519 	default:
520 		error = ether_ioctl(ifp, cmd, data);
521 		break;
522 	}
523 
524 	crit_exit();
525 
526 	return (error);
527 }
528 
529 /*
530  * Set up the logical address filter.
531  */
532 void
533 lance_setladrf(struct lance_softc *sc, uint16_t *af)
534 {
535 	struct ifnet *ifp = sc->ifp;
536 	struct ifmultiaddr *ifma;
537 	uint32_t crc;
538 
539 	/*
540 	 * Set up multicast address filter by passing all multicast addresses
541 	 * through a crc generator, and then using the high order 6 bits as an
542 	 * index into the 64 bit logical address filter.  The high order bit
543 	 * selects the word, while the rest of the bits select the bit within
544 	 * the word.
545 	 */
546 
547 	if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
548 		af[0] = af[1] = af[2] = af[3] = 0xffff;
549 		return;
550 	}
551 
552 	af[0] = af[1] = af[2] = af[3] = 0x0000;
553 
554 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
555 		if (ifma->ifma_addr->sa_family != AF_LINK)
556 			continue;
557 
558 		crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
559 		    ifma->ifma_addr), ETHER_ADDR_LEN);
560 
561 		/* Just want the 6 most significant bits. */
562 		crc >>= 26;
563 
564 		/* Set the corresponding bit in the filter. */
565 		af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf));
566 	}
567 }
568 
569 /*
570  * Routines for accessing the transmit and receive buffers.
571  * The various CPU and adapter configurations supported by this
572  * driver require three different access methods for buffers
573  * and descriptors:
574  *	(1) contig (contiguous data; no padding),
575  *	(2) gap2 (two bytes of data followed by two bytes of padding),
576  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
577  */
578 
579 /*
580  * contig: contiguous data with no padding.
581  *
582  * Buffers may have any alignment.
583  */
584 
585 void
586 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
587 {
588 	volatile caddr_t buf = sc->sc_mem;
589 
590 	/*
591 	 * Just call memcpy() to do the work.
592 	 */
593 	memcpy(buf + boff, from, len);
594 }
595 
596 void
597 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
598 {
599 	volatile caddr_t buf = sc->sc_mem;
600 
601 	/*
602 	 * Just call memcpy() to do the work.
603 	 */
604 	memcpy(to, buf + boff, len);
605 }
606 
607 void
608 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
609 {
610 	volatile caddr_t buf = sc->sc_mem;
611 
612 	/*
613 	 * Just let memset() do the work
614 	 */
615 	memset(buf + boff, 0, len);
616 }
617 
618 #if 0
619 /*
620  * Examples only; duplicate these and tweak (if necessary) in
621  * machine-specific front-ends.
622  */
623 
624 /*
625  * gap2: two bytes of data followed by two bytes of pad.
626  *
627  * Buffers must be 4-byte aligned.  The code doesn't worry about
628  * doing an extra byte.
629  */
630 
631 static void
632 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
633 {
634 	volatile caddr_t buf = sc->sc_mem;
635 	caddr_t from = fromv;
636 	volatile uint16_t *bptr;
637 
638 	if (boff & 0x1) {
639 		/* Handle unaligned first byte. */
640 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
641 		*bptr = (*from++ << 8) | (*bptr & 0xff);
642 		bptr += 2;
643 		len--;
644 	} else
645 		bptr = ((volatile uint16_t *)buf) + boff;
646 	while (len > 1) {
647 		*bptr = (from[1] << 8) | (from[0] & 0xff);
648 		bptr += 2;
649 		from += 2;
650 		len -= 2;
651 	}
652 	if (len == 1)
653 		*bptr = (uint16_t)*from;
654 }
655 
656 static void
657 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
658 {
659 	volatile caddr_t buf = sc->sc_mem;
660 	caddr_t to = tov;
661 	volatile uint16_t *bptr;
662 	uint16_t tmp;
663 
664 	if (boff & 0x1) {
665 		/* Handle unaligned first byte. */
666 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
667 		*to++ = (*bptr >> 8) & 0xff;
668 		bptr += 2;
669 		len--;
670 	} else
671 		bptr = ((volatile uint16_t *)buf) + boff;
672 	while (len > 1) {
673 		tmp = *bptr;
674 		*to++ = tmp & 0xff;
675 		*to++ = (tmp >> 8) & 0xff;
676 		bptr += 2;
677 		len -= 2;
678 	}
679 	if (len == 1)
680 		*to = *bptr & 0xff;
681 }
682 
683 static void
684 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
685 {
686 	volatile caddr_t buf = sc->sc_mem;
687 	volatile uint16_t *bptr;
688 
689 	if ((unsigned)boff & 0x1) {
690 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
691 		*bptr &= 0xff;
692 		bptr += 2;
693 		len--;
694 	} else
695 		bptr = ((volatile uint16_t *)buf) + boff;
696 	while (len > 0) {
697 		*bptr = 0;
698 		bptr += 2;
699 		len -= 2;
700 	}
701 }
702 
703 /*
704  * gap16: 16 bytes of data followed by 16 bytes of pad.
705  *
706  * Buffers must be 32-byte aligned.
707  */
708 
709 static void
710 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
711 {
712 	volatile caddr_t buf = sc->sc_mem;
713 	caddr_t bptr, from = fromv;
714 	int xfer;
715 
716 	bptr = buf + ((boff << 1) & ~0x1f);
717 	boff &= 0xf;
718 	xfer = min(len, 16 - boff);
719 	while (len > 0) {
720 		memcpy(bptr + boff, from, xfer);
721 		from += xfer;
722 		bptr += 32;
723 		boff = 0;
724 		len -= xfer;
725 		xfer = min(len, 16);
726 	}
727 }
728 
729 static void
730 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
731 {
732 	volatile caddr_t buf = sc->sc_mem;
733 	caddr_t bptr, to = tov;
734 	int xfer;
735 
736 	bptr = buf + ((boff << 1) & ~0x1f);
737 	boff &= 0xf;
738 	xfer = min(len, 16 - boff);
739 	while (len > 0) {
740 		memcpy(to, bptr + boff, xfer);
741 		to += xfer;
742 		bptr += 32;
743 		boff = 0;
744 		len -= xfer;
745 		xfer = min(len, 16);
746 	}
747 }
748 
749 static void
750 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
751 {
752 	volatile caddr_t buf = sc->sc_mem;
753 	caddr_t bptr;
754 	int xfer;
755 
756 	bptr = buf + ((boff << 1) & ~0x1f);
757 	boff &= 0xf;
758 	xfer = min(len, 16 - boff);
759 	while (len > 0) {
760 		memset(bptr + boff, 0, xfer);
761 		bptr += 32;
762 		boff = 0;
763 		len -= xfer;
764 		xfer = min(len, 16);
765 	}
766 }
767 #endif /* Example only */
768