xref: /netbsd-src/sys/dev/ic/hme.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: hme.c,v 1.85 2010/01/19 22:06:24 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * HME Ethernet module driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.85 2010/01/19 22:06:24 pooka Exp $");
38 
39 /* #define HMEDEBUG */
40 
41 #include "opt_inet.h"
42 #include "rnd.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/syslog.h>
49 #include <sys/socket.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54 #if NRND > 0
55 #include <sys/rnd.h>
56 #endif
57 
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_ether.h>
61 #include <net/if_media.h>
62 
63 #ifdef INET
64 #include <net/if_vlanvar.h>
65 #include <netinet/in.h>
66 #include <netinet/if_inarp.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #include <netinet/tcp.h>
71 #include <netinet/udp.h>
72 #endif
73 
74 
75 #include <net/bpf.h>
76 #include <net/bpfdesc.h>
77 
78 #include <dev/mii/mii.h>
79 #include <dev/mii/miivar.h>
80 
81 #include <sys/bus.h>
82 
83 #include <dev/ic/hmereg.h>
84 #include <dev/ic/hmevar.h>
85 
86 static void	hme_start(struct ifnet *);
87 static void	hme_stop(struct ifnet *, int);
88 static int	hme_ioctl(struct ifnet *, u_long, void *);
89 static void	hme_tick(void *);
90 static void	hme_watchdog(struct ifnet *);
91 static bool	hme_shutdown(device_t, int);
92 static int	hme_init(struct ifnet *);
93 static void	hme_meminit(struct hme_softc *);
94 static void	hme_mifinit(struct hme_softc *);
95 static void	hme_reset(struct hme_softc *);
96 static void	hme_chipreset(struct hme_softc *);
97 static void	hme_setladrf(struct hme_softc *);
98 
99 /* MII methods & callbacks */
100 static int	hme_mii_readreg(device_t, int, int);
101 static void	hme_mii_writereg(device_t, int, int, int);
102 static void	hme_mii_statchg(device_t);
103 
104 static int	hme_mediachange(struct ifnet *);
105 
106 static struct mbuf *hme_get(struct hme_softc *, int, uint32_t);
107 static int	hme_put(struct hme_softc *, int, struct mbuf *);
108 static void	hme_read(struct hme_softc *, int, uint32_t);
109 static int	hme_eint(struct hme_softc *, u_int);
110 static int	hme_rint(struct hme_softc *);
111 static int	hme_tint(struct hme_softc *);
112 
113 #if 0
114 /* Default buffer copy routines */
115 static void	hme_copytobuf_contig(struct hme_softc *, void *, int, int);
116 static void	hme_copyfrombuf_contig(struct hme_softc *, void *, int, int);
117 #endif
118 
119 void
120 hme_config(struct hme_softc *sc)
121 {
122 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
123 	struct mii_data *mii = &sc->sc_mii;
124 	struct mii_softc *child;
125 	bus_dma_tag_t dmatag = sc->sc_dmatag;
126 	bus_dma_segment_t seg;
127 	bus_size_t size;
128 	int rseg, error;
129 
130 	/*
131 	 * HME common initialization.
132 	 *
133 	 * hme_softc fields that must be initialized by the front-end:
134 	 *
135 	 * the bus tag:
136 	 *	sc_bustag
137 	 *
138 	 * the DMA bus tag:
139 	 *	sc_dmatag
140 	 *
141 	 * the bus handles:
142 	 *	sc_seb		(Shared Ethernet Block registers)
143 	 *	sc_erx		(Receiver Unit registers)
144 	 *	sc_etx		(Transmitter Unit registers)
145 	 *	sc_mac		(MAC registers)
146 	 *	sc_mif		(Management Interface registers)
147 	 *
148 	 * the maximum bus burst size:
149 	 *	sc_burst
150 	 *
151 	 * (notyet:DMA capable memory for the ring descriptors & packet buffers:
152 	 *	rb_membase, rb_dmabase)
153 	 *
154 	 * the local Ethernet address:
155 	 *	sc_enaddr
156 	 *
157 	 */
158 
159 	/* Make sure the chip is stopped. */
160 	hme_chipreset(sc);
161 
162 	/*
163 	 * Allocate descriptors and buffers
164 	 * XXX - do all this differently.. and more configurably,
165 	 * eg. use things as `dma_load_mbuf()' on transmit,
166 	 *     and a pool of `EXTMEM' mbufs (with buffers DMA-mapped
167 	 *     all the time) on the receiver side.
168 	 *
169 	 * Note: receive buffers must be 64-byte aligned.
170 	 * Also, apparently, the buffers must extend to a DMA burst
171 	 * boundary beyond the maximum packet size.
172 	 */
173 #define _HME_NDESC	128
174 #define _HME_BUFSZ	1600
175 
176 	/* Note: the # of descriptors must be a multiple of 16 */
177 	sc->sc_rb.rb_ntbuf = _HME_NDESC;
178 	sc->sc_rb.rb_nrbuf = _HME_NDESC;
179 
180 	/*
181 	 * Allocate DMA capable memory
182 	 * Buffer descriptors must be aligned on a 2048 byte boundary;
183 	 * take this into account when calculating the size. Note that
184 	 * the maximum number of descriptors (256) occupies 2048 bytes,
185 	 * so we allocate that much regardless of _HME_NDESC.
186 	 */
187 	size =	2048 +					/* TX descriptors */
188 		2048 +					/* RX descriptors */
189 		sc->sc_rb.rb_ntbuf * _HME_BUFSZ +	/* TX buffers */
190 		sc->sc_rb.rb_nrbuf * _HME_BUFSZ;	/* RX buffers */
191 
192 	/* Allocate DMA buffer */
193 	if ((error = bus_dmamem_alloc(dmatag, size,
194 				      2048, 0,
195 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
196 		aprint_error_dev(sc->sc_dev, "DMA buffer alloc error %d\n",
197 			error);
198 		return;
199 	}
200 
201 	/* Map DMA memory in CPU addressable space */
202 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
203 				    &sc->sc_rb.rb_membase,
204 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
205 		aprint_error_dev(sc->sc_dev, "DMA buffer map error %d\n",
206 			error);
207 		bus_dmamap_unload(dmatag, sc->sc_dmamap);
208 		bus_dmamem_free(dmatag, &seg, rseg);
209 		return;
210 	}
211 
212 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
213 				    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
214 		aprint_error_dev(sc->sc_dev, "DMA map create error %d\n",
215 			error);
216 		return;
217 	}
218 
219 	/* Load the buffer */
220 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
221 	    sc->sc_rb.rb_membase, size, NULL,
222 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
223 		aprint_error_dev(sc->sc_dev, "DMA buffer map load error %d\n",
224 			error);
225 		bus_dmamem_free(dmatag, &seg, rseg);
226 		return;
227 	}
228 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
229 
230 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
231 	    ether_sprintf(sc->sc_enaddr));
232 
233 	/* Initialize ifnet structure. */
234 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
235 	ifp->if_softc = sc;
236 	ifp->if_start = hme_start;
237 	ifp->if_stop = hme_stop;
238 	ifp->if_ioctl = hme_ioctl;
239 	ifp->if_init = hme_init;
240 	ifp->if_watchdog = hme_watchdog;
241 	ifp->if_flags =
242 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
243 	sc->sc_if_flags = ifp->if_flags;
244 	ifp->if_capabilities |=
245 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
246 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
247 	IFQ_SET_READY(&ifp->if_snd);
248 
249 	/* Initialize ifmedia structures and MII info */
250 	mii->mii_ifp = ifp;
251 	mii->mii_readreg = hme_mii_readreg;
252 	mii->mii_writereg = hme_mii_writereg;
253 	mii->mii_statchg = hme_mii_statchg;
254 
255 	sc->sc_ethercom.ec_mii = mii;
256 	ifmedia_init(&mii->mii_media, 0, hme_mediachange, ether_mediastatus);
257 
258 	hme_mifinit(sc);
259 
260 	/*
261 	 * Some HME's have an MII connector, as well as RJ45.  Try attaching
262 	 * the RJ45 (internal) PHY first, so that the MII PHY is always
263 	 * instance 1.
264 	 */
265 	mii_attach(sc->sc_dev, mii, 0xffffffff,
266 			HME_PHYAD_INTERNAL, MII_OFFSET_ANY, MIIF_FORCEANEG);
267 	mii_attach(sc->sc_dev, mii, 0xffffffff,
268 			HME_PHYAD_EXTERNAL, MII_OFFSET_ANY, MIIF_FORCEANEG);
269 
270 	child = LIST_FIRST(&mii->mii_phys);
271 	if (child == NULL) {
272 		/* No PHY attached */
273 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
274 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
275 	} else {
276 		/*
277 		 * Walk along the list of attached MII devices and
278 		 * establish an `MII instance' to `phy number'
279 		 * mapping. We'll use this mapping in media change
280 		 * requests to determine which phy to use to program
281 		 * the MIF configuration register.
282 		 */
283 		for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
284 			/*
285 			 * Note: we support just two PHYs: the built-in
286 			 * internal device and an external on the MII
287 			 * connector.
288 			 */
289 			if (child->mii_phy > 1 || child->mii_inst > 1) {
290 				aprint_error_dev(sc->sc_dev,
291 				    "cannot accommodate MII device %s"
292 				       " at phy %d, instance %d\n",
293 				       device_xname(child->mii_dev),
294 				       child->mii_phy, child->mii_inst);
295 				continue;
296 			}
297 
298 			sc->sc_phys[child->mii_inst] = child->mii_phy;
299 		}
300 
301 		/*
302 		 * Set the default media to auto negotiation if the phy has
303 		 * the auto negotiation capability.
304 		 * XXX; What to do otherwise?
305 		 */
306 		if (ifmedia_match(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO, 0))
307 			ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
308 /*
309 		else
310 			ifmedia_set(&sc->sc_mii.mii_media, sc->sc_defaultmedia);
311 */
312 	}
313 
314 	/* claim 802.1q capability */
315 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
316 
317 	/* Attach the interface. */
318 	if_attach(ifp);
319 	ether_ifattach(ifp, sc->sc_enaddr);
320 
321 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, hme_shutdown))
322 		pmf_class_network_register(sc->sc_dev, ifp);
323 	else
324 		aprint_error_dev(sc->sc_dev,
325 		    "couldn't establish power handler\n");
326 
327 #if NRND > 0
328 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
329 			  RND_TYPE_NET, 0);
330 #endif
331 
332 	callout_init(&sc->sc_tick_ch, 0);
333 }
334 
335 void
336 hme_tick(void *arg)
337 {
338 	struct hme_softc *sc = arg;
339 	int s;
340 
341 	s = splnet();
342 	mii_tick(&sc->sc_mii);
343 	splx(s);
344 
345 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
346 }
347 
348 void
349 hme_reset(struct hme_softc *sc)
350 {
351 	int s;
352 
353 	s = splnet();
354 	(void)hme_init(&sc->sc_ethercom.ec_if);
355 	splx(s);
356 }
357 
358 void
359 hme_chipreset(struct hme_softc *sc)
360 {
361 	bus_space_tag_t t = sc->sc_bustag;
362 	bus_space_handle_t seb = sc->sc_seb;
363 	int n;
364 
365 	/* Mask all interrupts */
366 	bus_space_write_4(t, seb, HME_SEBI_IMASK, 0xffffffff);
367 
368 	/* Reset transmitter and receiver */
369 	bus_space_write_4(t, seb, HME_SEBI_RESET,
370 			  (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX));
371 
372 	for (n = 0; n < 20; n++) {
373 		uint32_t v = bus_space_read_4(t, seb, HME_SEBI_RESET);
374 		if ((v & (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX)) == 0)
375 			return;
376 		DELAY(20);
377 	}
378 
379 	printf("%s: %s: reset failed\n", device_xname(sc->sc_dev), __func__);
380 }
381 
382 void
383 hme_stop(struct ifnet *ifp, int disable)
384 {
385 	struct hme_softc *sc;
386 
387 	sc = ifp->if_softc;
388 
389 	ifp->if_timer = 0;
390 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
391 
392 	callout_stop(&sc->sc_tick_ch);
393 	mii_down(&sc->sc_mii);
394 
395 	hme_chipreset(sc);
396 }
397 
398 void
399 hme_meminit(struct hme_softc *sc)
400 {
401 	bus_addr_t txbufdma, rxbufdma;
402 	bus_addr_t dma;
403 	char *p;
404 	unsigned int ntbuf, nrbuf, i;
405 	struct hme_ring *hr = &sc->sc_rb;
406 
407 	p = hr->rb_membase;
408 	dma = hr->rb_dmabase;
409 
410 	ntbuf = hr->rb_ntbuf;
411 	nrbuf = hr->rb_nrbuf;
412 
413 	/*
414 	 * Allocate transmit descriptors
415 	 */
416 	hr->rb_txd = p;
417 	hr->rb_txddma = dma;
418 	p += ntbuf * HME_XD_SIZE;
419 	dma += ntbuf * HME_XD_SIZE;
420 	/* We have reserved descriptor space until the next 2048 byte boundary.*/
421 	dma = (bus_addr_t)roundup((u_long)dma, 2048);
422 	p = (void *)roundup((u_long)p, 2048);
423 
424 	/*
425 	 * Allocate receive descriptors
426 	 */
427 	hr->rb_rxd = p;
428 	hr->rb_rxddma = dma;
429 	p += nrbuf * HME_XD_SIZE;
430 	dma += nrbuf * HME_XD_SIZE;
431 	/* Again move forward to the next 2048 byte boundary.*/
432 	dma = (bus_addr_t)roundup((u_long)dma, 2048);
433 	p = (void *)roundup((u_long)p, 2048);
434 
435 
436 	/*
437 	 * Allocate transmit buffers
438 	 */
439 	hr->rb_txbuf = p;
440 	txbufdma = dma;
441 	p += ntbuf * _HME_BUFSZ;
442 	dma += ntbuf * _HME_BUFSZ;
443 
444 	/*
445 	 * Allocate receive buffers
446 	 */
447 	hr->rb_rxbuf = p;
448 	rxbufdma = dma;
449 	p += nrbuf * _HME_BUFSZ;
450 	dma += nrbuf * _HME_BUFSZ;
451 
452 	/*
453 	 * Initialize transmit buffer descriptors
454 	 */
455 	for (i = 0; i < ntbuf; i++) {
456 		HME_XD_SETADDR(sc->sc_pci, hr->rb_txd, i, txbufdma + i * _HME_BUFSZ);
457 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, i, 0);
458 	}
459 
460 	/*
461 	 * Initialize receive buffer descriptors
462 	 */
463 	for (i = 0; i < nrbuf; i++) {
464 		HME_XD_SETADDR(sc->sc_pci, hr->rb_rxd, i, rxbufdma + i * _HME_BUFSZ);
465 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_rxd, i,
466 				HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
467 	}
468 
469 	hr->rb_tdhead = hr->rb_tdtail = 0;
470 	hr->rb_td_nbusy = 0;
471 	hr->rb_rdtail = 0;
472 }
473 
474 /*
475  * Initialization of interface; set up initialization block
476  * and transmit/receive descriptor rings.
477  */
478 int
479 hme_init(struct ifnet *ifp)
480 {
481 	struct hme_softc *sc = ifp->if_softc;
482 	bus_space_tag_t t = sc->sc_bustag;
483 	bus_space_handle_t seb = sc->sc_seb;
484 	bus_space_handle_t etx = sc->sc_etx;
485 	bus_space_handle_t erx = sc->sc_erx;
486 	bus_space_handle_t mac = sc->sc_mac;
487 	uint8_t *ea;
488 	uint32_t v;
489 	int rc;
490 
491 	/*
492 	 * Initialization sequence. The numbered steps below correspond
493 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
494 	 * Channel Engine manual (part of the PCIO manual).
495 	 * See also the STP2002-STQ document from Sun Microsystems.
496 	 */
497 
498 	/* step 1 & 2. Reset the Ethernet Channel */
499 	hme_stop(ifp, 0);
500 
501 	/* Re-initialize the MIF */
502 	hme_mifinit(sc);
503 
504 	/* Call MI reset function if any */
505 	if (sc->sc_hwreset)
506 		(*sc->sc_hwreset)(sc);
507 
508 #if 0
509 	/* Mask all MIF interrupts, just in case */
510 	bus_space_write_4(t, mif, HME_MIFI_IMASK, 0xffff);
511 #endif
512 
513 	/* step 3. Setup data structures in host memory */
514 	hme_meminit(sc);
515 
516 	/* step 4. TX MAC registers & counters */
517 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
518 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
519 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
520 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
521 	bus_space_write_4(t, mac, HME_MACI_TXSIZE,
522 	    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
523 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN : ETHER_MAX_LEN);
524 	sc->sc_ec_capenable = sc->sc_ethercom.ec_capenable;
525 
526 	/* Load station MAC address */
527 	ea = sc->sc_enaddr;
528 	bus_space_write_4(t, mac, HME_MACI_MACADDR0, (ea[0] << 8) | ea[1]);
529 	bus_space_write_4(t, mac, HME_MACI_MACADDR1, (ea[2] << 8) | ea[3]);
530 	bus_space_write_4(t, mac, HME_MACI_MACADDR2, (ea[4] << 8) | ea[5]);
531 
532 	/*
533 	 * Init seed for backoff
534 	 * (source suggested by manual: low 10 bits of MAC address)
535 	 */
536 	v = ((ea[4] << 8) | ea[5]) & 0x3fff;
537 	bus_space_write_4(t, mac, HME_MACI_RANDSEED, v);
538 
539 
540 	/* Note: Accepting power-on default for other MAC registers here.. */
541 
542 
543 	/* step 5. RX MAC registers & counters */
544 	hme_setladrf(sc);
545 
546 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
547 	bus_space_write_4(t, etx, HME_ETXI_RING, sc->sc_rb.rb_txddma);
548 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, sc->sc_rb.rb_ntbuf);
549 
550 	bus_space_write_4(t, erx, HME_ERXI_RING, sc->sc_rb.rb_rxddma);
551 	bus_space_write_4(t, mac, HME_MACI_RXSIZE,
552 	    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
553 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN : ETHER_MAX_LEN);
554 
555 	/* step 8. Global Configuration & Interrupt Mask */
556 	bus_space_write_4(t, seb, HME_SEBI_IMASK,
557 			~(
558 			  /*HME_SEB_STAT_GOTFRAME | HME_SEB_STAT_SENTFRAME |*/
559 			  HME_SEB_STAT_HOSTTOTX |
560 			  HME_SEB_STAT_RXTOHOST |
561 			  HME_SEB_STAT_TXALL |
562 			  HME_SEB_STAT_TXPERR |
563 			  HME_SEB_STAT_RCNTEXP |
564 			  HME_SEB_STAT_MIFIRQ |
565 			  HME_SEB_STAT_ALL_ERRORS ));
566 
567 	switch (sc->sc_burst) {
568 	default:
569 		v = 0;
570 		break;
571 	case 16:
572 		v = HME_SEB_CFG_BURST16;
573 		break;
574 	case 32:
575 		v = HME_SEB_CFG_BURST32;
576 		break;
577 	case 64:
578 		v = HME_SEB_CFG_BURST64;
579 		break;
580 	}
581 	bus_space_write_4(t, seb, HME_SEBI_CFG, v);
582 
583 	/* step 9. ETX Configuration: use mostly default values */
584 
585 	/* Enable DMA */
586 	v = bus_space_read_4(t, etx, HME_ETXI_CFG);
587 	v |= HME_ETX_CFG_DMAENABLE;
588 	bus_space_write_4(t, etx, HME_ETXI_CFG, v);
589 
590 	/* Transmit Descriptor ring size: in increments of 16 */
591 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
592 
593 
594 	/* step 10. ERX Configuration */
595 	v = bus_space_read_4(t, erx, HME_ERXI_CFG);
596 
597 	/* Encode Receive Descriptor ring size: four possible values */
598 	switch (_HME_NDESC /*XXX*/) {
599 	case 32:
600 		v |= HME_ERX_CFG_RINGSIZE32;
601 		break;
602 	case 64:
603 		v |= HME_ERX_CFG_RINGSIZE64;
604 		break;
605 	case 128:
606 		v |= HME_ERX_CFG_RINGSIZE128;
607 		break;
608 	case 256:
609 		v |= HME_ERX_CFG_RINGSIZE256;
610 		break;
611 	default:
612 		printf("hme: invalid Receive Descriptor ring size\n");
613 		break;
614 	}
615 
616 	/* Enable DMA */
617 	v |= HME_ERX_CFG_DMAENABLE;
618 
619 	/* set h/w rx checksum start offset (# of half-words) */
620 #ifdef INET
621 	v |= (((ETHER_HDR_LEN + sizeof(struct ip)) / sizeof(uint16_t))
622 		<< HME_ERX_CFG_CSUMSHIFT) &
623 		HME_ERX_CFG_CSUMSTART;
624 #endif
625 	bus_space_write_4(t, erx, HME_ERXI_CFG, v);
626 
627 	/* step 11. XIF Configuration */
628 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
629 	v |= HME_MAC_XIF_OE;
630 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
631 
632 	/* step 12. RX_MAC Configuration Register */
633 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
634 	v |= HME_MAC_RXCFG_ENABLE | HME_MAC_RXCFG_PSTRIP;
635 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
636 
637 	/* step 13. TX_MAC Configuration Register */
638 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
639 	v |= (HME_MAC_TXCFG_ENABLE | HME_MAC_TXCFG_DGIVEUP);
640 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
641 
642 	/* step 14. Issue Transmit Pending command */
643 
644 	/* Call MI initialization function if any */
645 	if (sc->sc_hwinit)
646 		(*sc->sc_hwinit)(sc);
647 
648 	/* Set the current media. */
649 	if ((rc = hme_mediachange(ifp)) != 0)
650 		return rc;
651 
652 	/* Start the one second timer. */
653 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
654 
655 	ifp->if_flags |= IFF_RUNNING;
656 	ifp->if_flags &= ~IFF_OACTIVE;
657 	sc->sc_if_flags = ifp->if_flags;
658 	ifp->if_timer = 0;
659 	hme_start(ifp);
660 	return 0;
661 }
662 
663 /*
664  * Routine to copy from mbuf chain to transmit buffer in
665  * network buffer memory.
666  * Returns the amount of data copied.
667  */
668 int
669 hme_put(struct hme_softc *sc, int ri, struct mbuf *m)
670 	/* ri:			 Ring index */
671 {
672 	struct mbuf *n;
673 	int len, tlen = 0;
674 	char *bp;
675 
676 	bp = (char *)sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
677 	for (; m; m = n) {
678 		len = m->m_len;
679 		if (len == 0) {
680 			MFREE(m, n);
681 			continue;
682 		}
683 		memcpy(bp, mtod(m, void *), len);
684 		bp += len;
685 		tlen += len;
686 		MFREE(m, n);
687 	}
688 	return (tlen);
689 }
690 
691 /*
692  * Pull data off an interface.
693  * Len is length of data, with local net header stripped.
694  * We copy the data into mbufs.  When full cluster sized units are present
695  * we copy into clusters.
696  */
697 struct mbuf *
698 hme_get(struct hme_softc *sc, int ri, uint32_t flags)
699 {
700 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
701 	struct mbuf *m, *m0, *newm;
702 	char *bp;
703 	int len, totlen;
704 #ifdef INET
705 	int csum_flags;
706 #endif
707 
708 	totlen = HME_XD_DECODE_RSIZE(flags);
709 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
710 	if (m0 == 0)
711 		return (0);
712 	m0->m_pkthdr.rcvif = ifp;
713 	m0->m_pkthdr.len = totlen;
714 	len = MHLEN;
715 	m = m0;
716 
717 	bp = (char *)sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
718 
719 	while (totlen > 0) {
720 		if (totlen >= MINCLSIZE) {
721 			MCLGET(m, M_DONTWAIT);
722 			if ((m->m_flags & M_EXT) == 0)
723 				goto bad;
724 			len = MCLBYTES;
725 		}
726 
727 		if (m == m0) {
728 			char *newdata = (char *)
729 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
730 			    sizeof(struct ether_header);
731 			len -= newdata - m->m_data;
732 			m->m_data = newdata;
733 		}
734 
735 		m->m_len = len = min(totlen, len);
736 		memcpy(mtod(m, void *), bp, len);
737 		bp += len;
738 
739 		totlen -= len;
740 		if (totlen > 0) {
741 			MGET(newm, M_DONTWAIT, MT_DATA);
742 			if (newm == 0)
743 				goto bad;
744 			len = MLEN;
745 			m = m->m_next = newm;
746 		}
747 	}
748 
749 #ifdef INET
750 	/* hardware checksum */
751 	csum_flags = 0;
752 	if (ifp->if_csum_flags_rx & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
753 		struct ether_header *eh;
754 		struct ether_vlan_header *evh;
755 		struct ip *ip;
756 		struct udphdr *uh;
757 		uint16_t *opts;
758 		int32_t hlen, pktlen;
759 		uint32_t csum_data;
760 
761 		eh = mtod(m0, struct ether_header *);
762 		if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
763 			ip = (struct ip *)((char *)eh + ETHER_HDR_LEN);
764 			pktlen = m0->m_pkthdr.len - ETHER_HDR_LEN;
765 		} else if (ntohs(eh->ether_type) == ETHERTYPE_VLAN) {
766 			evh = (struct ether_vlan_header *)eh;
767 			if (ntohs(evh->evl_proto != ETHERTYPE_IP))
768 				goto swcsum;
769 			ip = (struct ip *)((char *)eh + ETHER_HDR_LEN +
770 			    ETHER_VLAN_ENCAP_LEN);
771 			pktlen = m0->m_pkthdr.len -
772 			    ETHER_HDR_LEN - ETHER_VLAN_ENCAP_LEN;
773 		} else
774 			goto swcsum;
775 
776 		/* IPv4 only */
777 		if (ip->ip_v != IPVERSION)
778 			goto swcsum;
779 
780 		hlen = ip->ip_hl << 2;
781 		if (hlen < sizeof(struct ip))
782 			goto swcsum;
783 
784 		/*
785 		 * bail if too short, has random trailing garbage, truncated,
786 		 * fragment, or has ethernet pad.
787 		 */
788 		if (ntohs(ip->ip_len) < hlen ||
789 		    ntohs(ip->ip_len) != pktlen ||
790 		    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0)
791 			goto swcsum;
792 
793 		switch (ip->ip_p) {
794 		case IPPROTO_TCP:
795 			if ((ifp->if_csum_flags_rx & M_CSUM_TCPv4) == 0)
796 				goto swcsum;
797 			if (pktlen < (hlen + sizeof(struct tcphdr)))
798 				goto swcsum;
799 			csum_flags =
800 			    M_CSUM_TCPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
801 			break;
802 		case IPPROTO_UDP:
803 			if ((ifp->if_csum_flags_rx & M_CSUM_UDPv4) == 0)
804 				goto swcsum;
805 			if (pktlen < (hlen + sizeof(struct udphdr)))
806 				goto swcsum;
807 			uh = (struct udphdr *)((char *)ip + hlen);
808 			/* no checksum */
809 			if (uh->uh_sum == 0)
810 				goto swcsum;
811 			csum_flags =
812 			    M_CSUM_UDPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
813 			break;
814 		default:
815 			goto swcsum;
816 		}
817 
818 		/* w/ M_CSUM_NO_PSEUDOHDR, the uncomplemented sum is expected */
819 		csum_data = ~flags & HME_XD_RXCKSUM;
820 
821 		/*
822 		 * If data offset is different from RX cksum start offset,
823 		 * we have to deduct them.
824 		 */
825 		hlen = ((char *)ip + hlen) -
826 		    ((char *)eh + ETHER_HDR_LEN + sizeof(struct ip));
827 		if (hlen > 1) {
828 			uint32_t optsum;
829 
830 			optsum = 0;
831 			opts = (uint16_t *)((char *)eh +
832 			    ETHER_HDR_LEN + sizeof(struct ip));
833 
834 			while (hlen > 1) {
835 				optsum += ntohs(*opts++);
836 				hlen -= 2;
837 			}
838 			while (optsum >> 16)
839 				optsum = (optsum >> 16) + (optsum & 0xffff);
840 
841 			/* Deduct the ip opts sum from the hwsum. */
842 			csum_data += (uint16_t)~optsum;
843 
844 			while (csum_data >> 16)
845 				csum_data =
846 				    (csum_data >> 16) + (csum_data & 0xffff);
847 		}
848 		m0->m_pkthdr.csum_data = csum_data;
849 	}
850 swcsum:
851 	m0->m_pkthdr.csum_flags = csum_flags;
852 #endif
853 
854 	return (m0);
855 
856 bad:
857 	m_freem(m0);
858 	return (0);
859 }
860 
861 /*
862  * Pass a packet to the higher levels.
863  */
864 void
865 hme_read(struct hme_softc *sc, int ix, uint32_t flags)
866 {
867 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
868 	struct mbuf *m;
869 	int len;
870 
871 	len = HME_XD_DECODE_RSIZE(flags);
872 	if (len <= sizeof(struct ether_header) ||
873 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
874 	    ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
875 	    ETHERMTU + sizeof(struct ether_header))) {
876 #ifdef HMEDEBUG
877 		printf("%s: invalid packet size %d; dropping\n",
878 		    device_xname(sc->sc_dev), len);
879 #endif
880 		ifp->if_ierrors++;
881 		return;
882 	}
883 
884 	/* Pull packet off interface. */
885 	m = hme_get(sc, ix, flags);
886 	if (m == 0) {
887 		ifp->if_ierrors++;
888 		return;
889 	}
890 
891 	ifp->if_ipackets++;
892 
893 	/*
894 	 * Check if there's a BPF listener on this interface.
895 	 * If so, hand off the raw packet to BPF.
896 	 */
897 	if (ifp->if_bpf)
898 		bpf_ops->bpf_mtap(ifp->if_bpf, m);
899 
900 	/* Pass the packet up. */
901 	(*ifp->if_input)(ifp, m);
902 }
903 
904 void
905 hme_start(struct ifnet *ifp)
906 {
907 	struct hme_softc *sc = ifp->if_softc;
908 	void *txd = sc->sc_rb.rb_txd;
909 	struct mbuf *m;
910 	unsigned int txflags;
911 	unsigned int ri, len, obusy;
912 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
913 
914 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
915 		return;
916 
917 	ri = sc->sc_rb.rb_tdhead;
918 	obusy = sc->sc_rb.rb_td_nbusy;
919 
920 	for (;;) {
921 		IFQ_DEQUEUE(&ifp->if_snd, m);
922 		if (m == 0)
923 			break;
924 
925 		/*
926 		 * If BPF is listening on this interface, let it see the
927 		 * packet before we commit it to the wire.
928 		 */
929 		if (ifp->if_bpf)
930 			bpf_ops->bpf_mtap(ifp->if_bpf, m);
931 
932 #ifdef INET
933 		/* collect bits for h/w csum, before hme_put frees the mbuf */
934 		if (ifp->if_csum_flags_tx & (M_CSUM_TCPv4 | M_CSUM_UDPv4) &&
935 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
936 			struct ether_header *eh;
937 			uint16_t offset, start;
938 
939 			eh = mtod(m, struct ether_header *);
940 			switch (ntohs(eh->ether_type)) {
941 			case ETHERTYPE_IP:
942 				start = ETHER_HDR_LEN;
943 				break;
944 			case ETHERTYPE_VLAN:
945 				start = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
946 				break;
947 			default:
948 				/* unsupported, drop it */
949 				m_free(m);
950 				continue;
951 			}
952 			start += M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
953 			offset = M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data)
954 			    + start;
955 			txflags = HME_XD_TXCKSUM |
956 				  (offset << HME_XD_TXCSSTUFFSHIFT) |
957 		  		  (start << HME_XD_TXCSSTARTSHIFT);
958 		} else
959 #endif
960 			txflags = 0;
961 
962 		/*
963 		 * Copy the mbuf chain into the transmit buffer.
964 		 */
965 		len = hme_put(sc, ri, m);
966 
967 		/*
968 		 * Initialize transmit registers and start transmission
969 		 */
970 		HME_XD_SETFLAGS(sc->sc_pci, txd, ri,
971 			HME_XD_OWN | HME_XD_SOP | HME_XD_EOP |
972 			HME_XD_ENCODE_TSIZE(len) | txflags);
973 
974 		/*if (sc->sc_rb.rb_td_nbusy <= 0)*/
975 		bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
976 				  HME_ETX_TP_DMAWAKEUP);
977 
978 		if (++ri == ntbuf)
979 			ri = 0;
980 
981 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
982 			ifp->if_flags |= IFF_OACTIVE;
983 			break;
984 		}
985 	}
986 
987 	if (obusy != sc->sc_rb.rb_td_nbusy) {
988 		sc->sc_rb.rb_tdhead = ri;
989 		ifp->if_timer = 5;
990 	}
991 }
992 
993 /*
994  * Transmit interrupt.
995  */
996 int
997 hme_tint(struct hme_softc *sc)
998 {
999 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1000 	bus_space_tag_t t = sc->sc_bustag;
1001 	bus_space_handle_t mac = sc->sc_mac;
1002 	unsigned int ri, txflags;
1003 
1004 	/*
1005 	 * Unload collision counters
1006 	 */
1007 	ifp->if_collisions +=
1008 		bus_space_read_4(t, mac, HME_MACI_NCCNT) +
1009 		bus_space_read_4(t, mac, HME_MACI_FCCNT);
1010 	ifp->if_oerrors +=
1011 		bus_space_read_4(t, mac, HME_MACI_EXCNT) +
1012 		bus_space_read_4(t, mac, HME_MACI_LTCNT);
1013 
1014 	/*
1015 	 * then clear the hardware counters.
1016 	 */
1017 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
1018 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
1019 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
1020 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
1021 
1022 	/* Fetch current position in the transmit ring */
1023 	ri = sc->sc_rb.rb_tdtail;
1024 
1025 	for (;;) {
1026 		if (sc->sc_rb.rb_td_nbusy <= 0)
1027 			break;
1028 
1029 		txflags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_txd, ri);
1030 
1031 		if (txflags & HME_XD_OWN)
1032 			break;
1033 
1034 		ifp->if_flags &= ~IFF_OACTIVE;
1035 		ifp->if_opackets++;
1036 
1037 		if (++ri == sc->sc_rb.rb_ntbuf)
1038 			ri = 0;
1039 
1040 		--sc->sc_rb.rb_td_nbusy;
1041 	}
1042 
1043 	/* Update ring */
1044 	sc->sc_rb.rb_tdtail = ri;
1045 
1046 	hme_start(ifp);
1047 
1048 	if (sc->sc_rb.rb_td_nbusy == 0)
1049 		ifp->if_timer = 0;
1050 
1051 	return (1);
1052 }
1053 
1054 /*
1055  * Receive interrupt.
1056  */
1057 int
1058 hme_rint(struct hme_softc *sc)
1059 {
1060 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1061 	bus_space_tag_t t = sc->sc_bustag;
1062 	bus_space_handle_t mac = sc->sc_mac;
1063 	void *xdr = sc->sc_rb.rb_rxd;
1064 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
1065 	unsigned int ri;
1066 	uint32_t flags;
1067 
1068 	ri = sc->sc_rb.rb_rdtail;
1069 
1070 	/*
1071 	 * Process all buffers with valid data.
1072 	 */
1073 	for (;;) {
1074 		flags = HME_XD_GETFLAGS(sc->sc_pci, xdr, ri);
1075 		if (flags & HME_XD_OWN)
1076 			break;
1077 
1078 		if (flags & HME_XD_OFL) {
1079 			printf("%s: buffer overflow, ri=%d; flags=0x%x\n",
1080 					device_xname(sc->sc_dev), ri, flags);
1081 		} else
1082 			hme_read(sc, ri, flags);
1083 
1084 		/* This buffer can be used by the hardware again */
1085 		HME_XD_SETFLAGS(sc->sc_pci, xdr, ri,
1086 				HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
1087 
1088 		if (++ri == nrbuf)
1089 			ri = 0;
1090 	}
1091 
1092 	sc->sc_rb.rb_rdtail = ri;
1093 
1094 	/* Read error counters ... */
1095 	ifp->if_ierrors +=
1096 	    bus_space_read_4(t, mac, HME_MACI_STAT_LCNT) +
1097 	    bus_space_read_4(t, mac, HME_MACI_STAT_ACNT) +
1098 	    bus_space_read_4(t, mac, HME_MACI_STAT_CCNT) +
1099 	    bus_space_read_4(t, mac, HME_MACI_STAT_CVCNT);
1100 
1101 	/* ... then clear the hardware counters. */
1102 	bus_space_write_4(t, mac, HME_MACI_STAT_LCNT, 0);
1103 	bus_space_write_4(t, mac, HME_MACI_STAT_ACNT, 0);
1104 	bus_space_write_4(t, mac, HME_MACI_STAT_CCNT, 0);
1105 	bus_space_write_4(t, mac, HME_MACI_STAT_CVCNT, 0);
1106 	return (1);
1107 }
1108 
1109 int
1110 hme_eint(struct hme_softc *sc, u_int status)
1111 {
1112 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1113 	char bits[128];
1114 
1115 	if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
1116 		bus_space_tag_t t = sc->sc_bustag;
1117 		bus_space_handle_t mif = sc->sc_mif;
1118 		uint32_t cf, st, sm;
1119 		cf = bus_space_read_4(t, mif, HME_MIFI_CFG);
1120 		st = bus_space_read_4(t, mif, HME_MIFI_STAT);
1121 		sm = bus_space_read_4(t, mif, HME_MIFI_SM);
1122 		printf("%s: XXXlink status changed: cfg=%x, stat %x, sm %x\n",
1123 			device_xname(sc->sc_dev), cf, st, sm);
1124 		return (1);
1125 	}
1126 
1127 	/* Receive error counters rolled over */
1128 	if (status & HME_SEB_STAT_ACNTEXP)
1129 		ifp->if_ierrors += 0xff;
1130 	if (status & HME_SEB_STAT_CCNTEXP)
1131 		ifp->if_ierrors += 0xff;
1132 	if (status & HME_SEB_STAT_LCNTEXP)
1133 		ifp->if_ierrors += 0xff;
1134 	if (status & HME_SEB_STAT_CVCNTEXP)
1135 		ifp->if_ierrors += 0xff;
1136 
1137 	/* RXTERR locks up the interface, so do a reset */
1138 	if (status & HME_SEB_STAT_RXTERR)
1139 		hme_reset(sc);
1140 
1141 	snprintb(bits, sizeof(bits), HME_SEB_STAT_BITS, status);
1142 	printf("%s: status=%s\n", device_xname(sc->sc_dev), bits);
1143 
1144 	return (1);
1145 }
1146 
1147 int
1148 hme_intr(void *v)
1149 {
1150 	struct hme_softc *sc = v;
1151 	bus_space_tag_t t = sc->sc_bustag;
1152 	bus_space_handle_t seb = sc->sc_seb;
1153 	uint32_t status;
1154 	int r = 0;
1155 
1156 	status = bus_space_read_4(t, seb, HME_SEBI_STAT);
1157 
1158 	if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
1159 		r |= hme_eint(sc, status);
1160 
1161 	if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
1162 		r |= hme_tint(sc);
1163 
1164 	if ((status & HME_SEB_STAT_RXTOHOST) != 0)
1165 		r |= hme_rint(sc);
1166 
1167 #if NRND > 0
1168 	rnd_add_uint32(&sc->rnd_source, status);
1169 #endif
1170 
1171 	return (r);
1172 }
1173 
1174 
1175 void
1176 hme_watchdog(struct ifnet *ifp)
1177 {
1178 	struct hme_softc *sc = ifp->if_softc;
1179 
1180 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
1181 	++ifp->if_oerrors;
1182 
1183 	hme_reset(sc);
1184 }
1185 
1186 /*
1187  * Initialize the MII Management Interface
1188  */
1189 void
1190 hme_mifinit(struct hme_softc *sc)
1191 {
1192 	bus_space_tag_t t = sc->sc_bustag;
1193 	bus_space_handle_t mif = sc->sc_mif;
1194 	bus_space_handle_t mac = sc->sc_mac;
1195 	int instance, phy;
1196 	uint32_t v;
1197 
1198 	if (sc->sc_mii.mii_media.ifm_cur != NULL) {
1199 		instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1200 		phy = sc->sc_phys[instance];
1201 	} else
1202 		/* No media set yet, pick phy arbitrarily.. */
1203 		phy = HME_PHYAD_EXTERNAL;
1204 
1205 	/* Configure the MIF in frame mode, no poll, current phy select */
1206 	v = 0;
1207 	if (phy == HME_PHYAD_EXTERNAL)
1208 		v |= HME_MIF_CFG_PHY;
1209 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1210 
1211 	/* If an external transceiver is selected, enable its MII drivers */
1212 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
1213 	v &= ~HME_MAC_XIF_MIIENABLE;
1214 	if (phy == HME_PHYAD_EXTERNAL)
1215 		v |= HME_MAC_XIF_MIIENABLE;
1216 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
1217 }
1218 
1219 /*
1220  * MII interface
1221  */
1222 static int
1223 hme_mii_readreg(device_t self, int phy, int reg)
1224 {
1225 	struct hme_softc *sc = device_private(self);
1226 	bus_space_tag_t t = sc->sc_bustag;
1227 	bus_space_handle_t mif = sc->sc_mif;
1228 	bus_space_handle_t mac = sc->sc_mac;
1229 	uint32_t v, xif_cfg, mifi_cfg;
1230 	int n;
1231 
1232 	/* We can at most have two PHYs */
1233 	if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
1234 		return (0);
1235 
1236 	/* Select the desired PHY in the MIF configuration register */
1237 	v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
1238 	v &= ~HME_MIF_CFG_PHY;
1239 	if (phy == HME_PHYAD_EXTERNAL)
1240 		v |= HME_MIF_CFG_PHY;
1241 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1242 
1243 	/* Enable MII drivers on external transceiver */
1244 	v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
1245 	if (phy == HME_PHYAD_EXTERNAL)
1246 		v |= HME_MAC_XIF_MIIENABLE;
1247 	else
1248 		v &= ~HME_MAC_XIF_MIIENABLE;
1249 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
1250 
1251 #if 0
1252 /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
1253 	/*
1254 	 * Check whether a transceiver is connected by testing
1255 	 * the MIF configuration register's MDI_X bits. Note that
1256 	 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
1257 	 */
1258 	mif_mdi_bit = 1 << (8 + (1 - phy));
1259 	delay(100);
1260 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
1261 	if ((v & mif_mdi_bit) == 0)
1262 		return (0);
1263 #endif
1264 
1265 	/* Construct the frame command */
1266 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
1267 	    HME_MIF_FO_TAMSB |
1268 	    (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
1269 	    (phy << HME_MIF_FO_PHYAD_SHIFT) |
1270 	    (reg << HME_MIF_FO_REGAD_SHIFT);
1271 
1272 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
1273 	for (n = 0; n < 100; n++) {
1274 		DELAY(1);
1275 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
1276 		if (v & HME_MIF_FO_TALSB) {
1277 			v &= HME_MIF_FO_DATA;
1278 			goto out;
1279 		}
1280 	}
1281 
1282 	v = 0;
1283 	printf("%s: mii_read timeout\n", device_xname(sc->sc_dev));
1284 
1285 out:
1286 	/* Restore MIFI_CFG register */
1287 	bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
1288 	/* Restore XIF register */
1289 	bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
1290 	return (v);
1291 }
1292 
1293 static void
1294 hme_mii_writereg(device_t self, int phy, int reg, int val)
1295 {
1296 	struct hme_softc *sc = device_private(self);
1297 	bus_space_tag_t t = sc->sc_bustag;
1298 	bus_space_handle_t mif = sc->sc_mif;
1299 	bus_space_handle_t mac = sc->sc_mac;
1300 	uint32_t v, xif_cfg, mifi_cfg;
1301 	int n;
1302 
1303 	/* We can at most have two PHYs */
1304 	if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
1305 		return;
1306 
1307 	/* Select the desired PHY in the MIF configuration register */
1308 	v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
1309 	v &= ~HME_MIF_CFG_PHY;
1310 	if (phy == HME_PHYAD_EXTERNAL)
1311 		v |= HME_MIF_CFG_PHY;
1312 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1313 
1314 	/* Enable MII drivers on external transceiver */
1315 	v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
1316 	if (phy == HME_PHYAD_EXTERNAL)
1317 		v |= HME_MAC_XIF_MIIENABLE;
1318 	else
1319 		v &= ~HME_MAC_XIF_MIIENABLE;
1320 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
1321 
1322 #if 0
1323 /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
1324 	/*
1325 	 * Check whether a transceiver is connected by testing
1326 	 * the MIF configuration register's MDI_X bits. Note that
1327 	 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
1328 	 */
1329 	mif_mdi_bit = 1 << (8 + (1 - phy));
1330 	delay(100);
1331 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
1332 	if ((v & mif_mdi_bit) == 0)
1333 		return;
1334 #endif
1335 
1336 	/* Construct the frame command */
1337 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT)	|
1338 	    HME_MIF_FO_TAMSB				|
1339 	    (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT)	|
1340 	    (phy << HME_MIF_FO_PHYAD_SHIFT)		|
1341 	    (reg << HME_MIF_FO_REGAD_SHIFT)		|
1342 	    (val & HME_MIF_FO_DATA);
1343 
1344 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
1345 	for (n = 0; n < 100; n++) {
1346 		DELAY(1);
1347 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
1348 		if (v & HME_MIF_FO_TALSB)
1349 			goto out;
1350 	}
1351 
1352 	printf("%s: mii_write timeout\n", device_xname(sc->sc_dev));
1353 out:
1354 	/* Restore MIFI_CFG register */
1355 	bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
1356 	/* Restore XIF register */
1357 	bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
1358 }
1359 
1360 static void
1361 hme_mii_statchg(device_t dev)
1362 {
1363 	struct hme_softc *sc = device_private(dev);
1364 	bus_space_tag_t t = sc->sc_bustag;
1365 	bus_space_handle_t mac = sc->sc_mac;
1366 	uint32_t v;
1367 
1368 #ifdef HMEDEBUG
1369 	if (sc->sc_debug)
1370 		printf("hme_mii_statchg: status change\n");
1371 #endif
1372 
1373 	/* Set the MAC Full Duplex bit appropriately */
1374 	/* Apparently the hme chip is SIMPLEX if working in full duplex mode,
1375 	   but not otherwise. */
1376 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
1377 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
1378 		v |= HME_MAC_TXCFG_FULLDPLX;
1379 		sc->sc_ethercom.ec_if.if_flags |= IFF_SIMPLEX;
1380 	} else {
1381 		v &= ~HME_MAC_TXCFG_FULLDPLX;
1382 		sc->sc_ethercom.ec_if.if_flags &= ~IFF_SIMPLEX;
1383 	}
1384 	sc->sc_if_flags = sc->sc_ethercom.ec_if.if_flags;
1385 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
1386 }
1387 
1388 int
1389 hme_mediachange(struct ifnet *ifp)
1390 {
1391 	struct hme_softc *sc = ifp->if_softc;
1392 	bus_space_tag_t t = sc->sc_bustag;
1393 	bus_space_handle_t mif = sc->sc_mif;
1394 	bus_space_handle_t mac = sc->sc_mac;
1395 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1396 	int phy = sc->sc_phys[instance];
1397 	int rc;
1398 	uint32_t v;
1399 
1400 #ifdef HMEDEBUG
1401 	if (sc->sc_debug)
1402 		printf("hme_mediachange: phy = %d\n", phy);
1403 #endif
1404 
1405 	/* Select the current PHY in the MIF configuration register */
1406 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
1407 	v &= ~HME_MIF_CFG_PHY;
1408 	if (phy == HME_PHYAD_EXTERNAL)
1409 		v |= HME_MIF_CFG_PHY;
1410 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1411 
1412 	/* If an external transceiver is selected, enable its MII drivers */
1413 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
1414 	v &= ~HME_MAC_XIF_MIIENABLE;
1415 	if (phy == HME_PHYAD_EXTERNAL)
1416 		v |= HME_MAC_XIF_MIIENABLE;
1417 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
1418 
1419 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
1420 		return 0;
1421 	return rc;
1422 }
1423 
1424 /*
1425  * Process an ioctl request.
1426  */
1427 int
1428 hme_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
1429 {
1430 	struct hme_softc *sc = ifp->if_softc;
1431 	struct ifaddr *ifa = (struct ifaddr *)data;
1432 	int s, error = 0;
1433 
1434 	s = splnet();
1435 
1436 	switch (cmd) {
1437 
1438 	case SIOCINITIFADDR:
1439 		switch (ifa->ifa_addr->sa_family) {
1440 #ifdef INET
1441 		case AF_INET:
1442 			if (ifp->if_flags & IFF_UP)
1443 				hme_setladrf(sc);
1444 			else {
1445 				ifp->if_flags |= IFF_UP;
1446 				error = hme_init(ifp);
1447 			}
1448 			arp_ifinit(ifp, ifa);
1449 			break;
1450 #endif
1451 		default:
1452 			ifp->if_flags |= IFF_UP;
1453 			error = hme_init(ifp);
1454 			break;
1455 		}
1456 		break;
1457 
1458 	case SIOCSIFFLAGS:
1459 #ifdef HMEDEBUG
1460 		{
1461 			struct ifreq *ifr = data;
1462 			sc->sc_debug =
1463 			    (ifr->ifr_flags & IFF_DEBUG) != 0 ? 1 : 0;
1464 		}
1465 #endif
1466 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1467 			break;
1468 
1469 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1470 		case IFF_RUNNING:
1471 			/*
1472 			 * If interface is marked down and it is running, then
1473 			 * stop it.
1474 			 */
1475 			hme_stop(ifp, 0);
1476 			ifp->if_flags &= ~IFF_RUNNING;
1477 			break;
1478 		case IFF_UP:
1479 			/*
1480 			 * If interface is marked up and it is stopped, then
1481 			 * start it.
1482 			 */
1483 			error = hme_init(ifp);
1484 			break;
1485 		case IFF_UP|IFF_RUNNING:
1486 			/*
1487 			 * If setting debug or promiscuous mode, do not reset
1488 			 * the chip; for everything else, call hme_init()
1489 			 * which will trigger a reset.
1490 			 */
1491 #define RESETIGN (IFF_CANTCHANGE | IFF_DEBUG)
1492 			if (ifp->if_flags != sc->sc_if_flags) {
1493 				if ((ifp->if_flags & (~RESETIGN))
1494 				    == (sc->sc_if_flags & (~RESETIGN)))
1495 					hme_setladrf(sc);
1496 				else
1497 					error = hme_init(ifp);
1498 			}
1499 #undef RESETIGN
1500 			break;
1501 		case 0:
1502 			break;
1503 		}
1504 
1505 		if (sc->sc_ec_capenable != sc->sc_ethercom.ec_capenable)
1506 			error = hme_init(ifp);
1507 
1508 		break;
1509 
1510 	default:
1511 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
1512 			break;
1513 
1514 		error = 0;
1515 
1516 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
1517 			;
1518 		else if (ifp->if_flags & IFF_RUNNING) {
1519 			/*
1520 			 * Multicast list has changed; set the hardware filter
1521 			 * accordingly.
1522 			 */
1523 			hme_setladrf(sc);
1524 		}
1525 		break;
1526 	}
1527 
1528 	sc->sc_if_flags = ifp->if_flags;
1529 	splx(s);
1530 	return (error);
1531 }
1532 
1533 bool
1534 hme_shutdown(device_t self, int howto)
1535 {
1536 	struct hme_softc *sc;
1537 	struct ifnet *ifp;
1538 
1539 	sc = device_private(self);
1540 	ifp = &sc->sc_ethercom.ec_if;
1541 	hme_stop(ifp, 1);
1542 
1543 	return true;
1544 }
1545 
1546 /*
1547  * Set up the logical address filter.
1548  */
1549 void
1550 hme_setladrf(struct hme_softc *sc)
1551 {
1552 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1553 	struct ether_multi *enm;
1554 	struct ether_multistep step;
1555 	struct ethercom *ec = &sc->sc_ethercom;
1556 	bus_space_tag_t t = sc->sc_bustag;
1557 	bus_space_handle_t mac = sc->sc_mac;
1558 	uint32_t v;
1559 	uint32_t crc;
1560 	uint32_t hash[4];
1561 
1562 	/* Clear hash table */
1563 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
1564 
1565 	/* Get current RX configuration */
1566 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
1567 
1568 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
1569 		/* Turn on promiscuous mode; turn off the hash filter */
1570 		v |= HME_MAC_RXCFG_PMISC;
1571 		v &= ~HME_MAC_RXCFG_HENABLE;
1572 		ifp->if_flags |= IFF_ALLMULTI;
1573 		goto chipit;
1574 	}
1575 
1576 	/* Turn off promiscuous mode; turn on the hash filter */
1577 	v &= ~HME_MAC_RXCFG_PMISC;
1578 	v |= HME_MAC_RXCFG_HENABLE;
1579 
1580 	/*
1581 	 * Set up multicast address filter by passing all multicast addresses
1582 	 * through a crc generator, and then using the high order 6 bits as an
1583 	 * index into the 64 bit logical address filter.  The high order bit
1584 	 * selects the word, while the rest of the bits select the bit within
1585 	 * the word.
1586 	 */
1587 
1588 	ETHER_FIRST_MULTI(step, ec, enm);
1589 	while (enm != NULL) {
1590 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1591 			/*
1592 			 * We must listen to a range of multicast addresses.
1593 			 * For now, just accept all multicasts, rather than
1594 			 * trying to set only those filter bits needed to match
1595 			 * the range.  (At this time, the only use of address
1596 			 * ranges is for IP multicast routing, for which the
1597 			 * range is big enough to require all bits set.)
1598 			 */
1599 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1600 			ifp->if_flags |= IFF_ALLMULTI;
1601 			goto chipit;
1602 		}
1603 
1604 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1605 
1606 		/* Just want the 6 most significant bits. */
1607 		crc >>= 26;
1608 
1609 		/* Set the corresponding bit in the filter. */
1610 		hash[crc >> 4] |= 1 << (crc & 0xf);
1611 
1612 		ETHER_NEXT_MULTI(step, enm);
1613 	}
1614 
1615 	ifp->if_flags &= ~IFF_ALLMULTI;
1616 
1617 chipit:
1618 	/* Now load the hash table into the chip */
1619 	bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
1620 	bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
1621 	bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
1622 	bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
1623 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
1624 }
1625 
1626 /*
1627  * Routines for accessing the transmit and receive buffers.
1628  * The various CPU and adapter configurations supported by this
1629  * driver require three different access methods for buffers
1630  * and descriptors:
1631  *	(1) contig (contiguous data; no padding),
1632  *	(2) gap2 (two bytes of data followed by two bytes of padding),
1633  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
1634  */
1635 
1636 #if 0
1637 /*
1638  * contig: contiguous data with no padding.
1639  *
1640  * Buffers may have any alignment.
1641  */
1642 
1643 void
1644 hme_copytobuf_contig(struct hme_softc *sc, void *from, int ri, int len)
1645 {
1646 	volatile void *buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
1647 
1648 	/*
1649 	 * Just call memcpy() to do the work.
1650 	 */
1651 	memcpy(buf, from, len);
1652 }
1653 
1654 void
1655 hme_copyfrombuf_contig(struct hme_softc *sc, void *to, int boff, int len)
1656 {
1657 	volatile void *buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
1658 
1659 	/*
1660 	 * Just call memcpy() to do the work.
1661 	 */
1662 	memcpy(to, buf, len);
1663 }
1664 #endif
1665