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