xref: /netbsd-src/sys/dev/pci/if_jme.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: if_jme.c,v 1.9 2009/04/18 14:58:03 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Manuel Bouyer.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *  This product includes software developed by Manuel Bouyer.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice unmodified, this list of conditions, and the following
41  *    disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  */
58 
59 
60 /*
61  * Driver for JMicron Technologies JMC250 (Giganbit) and JMC260 (Fast)
62  * Ethernet Controllers.
63  */
64 
65 #include <sys/cdefs.h>
66 __KERNEL_RCSID(0, "$NetBSD: if_jme.c,v 1.9 2009/04/18 14:58:03 tsutsui Exp $");
67 
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/mbuf.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/ioctl.h>
75 #include <sys/errno.h>
76 #include <sys/malloc.h>
77 #include <sys/kernel.h>
78 #include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
79 #include <sys/device.h>
80 #include <sys/syslog.h>
81 #include <sys/sysctl.h>
82 
83 #include <net/if.h>
84 #if defined(SIOCSIFMEDIA)
85 #include <net/if_media.h>
86 #endif
87 #include <net/if_types.h>
88 #include <net/if_dl.h>
89 #include <net/route.h>
90 #include <net/netisr.h>
91 
92 #include "bpfilter.h"
93 #if NBPFILTER > 0
94 #include <net/bpf.h>
95 #include <net/bpfdesc.h>
96 #endif
97 
98 #include "rnd.h"
99 #if NRND > 0
100 #include <sys/rnd.h>
101 #endif
102 
103 #ifdef INET
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #include <netinet/tcp.h>
109 #endif
110 
111 
112 #include <net/if_ether.h>
113 #include <uvm/uvm_extern.h>
114 #if defined(INET)
115 #include <netinet/if_inarp.h>
116 #endif
117 
118 #include <sys/bus.h>
119 #include <sys/intr.h>
120 
121 #include <dev/pci/pcireg.h>
122 #include <dev/pci/pcivar.h>
123 #include <dev/pci/pcidevs.h>
124 #include <dev/pci/if_jmereg.h>
125 
126 #include <dev/mii/mii.h>
127 #include <dev/mii/miivar.h>
128 
129 struct jme_product_desc {
130 	u_int32_t jme_product;
131 	const char *jme_desc;
132 };
133 
134 /* number of entries in transmit and receive rings */
135 #define JME_NBUFS (PAGE_SIZE / sizeof(struct jme_desc))
136 
137 #define JME_DESC_INC(x, y)	((x) = ((x) + 1) % (y))
138 
139 /* Water mark to kick reclaiming Tx buffers. */
140 #define JME_TX_DESC_HIWAT	(JME_NBUFS - (((JME_NBUFS) * 3) / 10))
141 
142 
143 struct jme_softc {
144 	device_t jme_dev;		/* base device */
145 	bus_space_tag_t jme_bt_mac;
146 	bus_space_handle_t jme_bh_mac;  /* Mac registers */
147 	bus_space_tag_t jme_bt_phy;
148 	bus_space_handle_t jme_bh_phy;  /* PHY registers */
149 	bus_space_tag_t jme_bt_misc;
150 	bus_space_handle_t jme_bh_misc; /* Misc registers */
151 	bus_dma_tag_t jme_dmatag;
152 	bus_dma_segment_t jme_txseg;	/* transmit ring seg */
153 	bus_dmamap_t jme_txmap;		/* transmit ring DMA map */
154 	struct jme_desc* jme_txring;	/* transmit ring */
155 	bus_dmamap_t jme_txmbufm[JME_NBUFS]; /* transmit mbufs DMA map */
156 	struct mbuf *jme_txmbuf[JME_NBUFS]; /* mbufs being transmitted */
157 	int jme_tx_cons;		/* transmit ring consumer */
158 	int jme_tx_prod;		/* transmit ring producer */
159 	int jme_tx_cnt;			/* transmit ring active count */
160 	bus_dma_segment_t jme_rxseg;	/* receive ring seg */
161 	bus_dmamap_t jme_rxmap;		/* receive ring DMA map */
162 	struct jme_desc* jme_rxring;	/* receive ring */
163 	bus_dmamap_t jme_rxmbufm[JME_NBUFS]; /* receive mbufs DMA map */
164 	struct mbuf *jme_rxmbuf[JME_NBUFS]; /* mbufs being received */
165 	int jme_rx_cons;		/* receive ring consumer */
166 	int jme_rx_prod;		/* receive ring producer */
167 	void* jme_ih;			/* our interrupt */
168 	struct ethercom jme_ec;
169 	struct callout jme_tick_ch;	/* tick callout */
170 	u_int8_t jme_enaddr[ETHER_ADDR_LEN];/* hardware address */
171 	u_int8_t jme_phyaddr;		/* address of integrated phy */
172 	u_int8_t jme_chip_rev;		/* chip revision */
173 	u_int8_t jme_rev;		/* PCI revision */
174 	mii_data_t jme_mii;		/* mii bus */
175 	u_int32_t jme_flags;		/* device features, see below */
176 	uint32_t jme_txcsr;		/* TX config register */
177 	uint32_t jme_rxcsr;		/* RX config register */
178 #if NRND > 0
179 	rndsource_element_t rnd_source;
180 #endif
181 	/* interrupt coalition parameters */
182 	struct sysctllog *jme_clog;
183 	int jme_intrxto;		/* interrupt RX timeout */
184 	int jme_intrxct;		/* interrupt RX packets counter */
185 	int jme_inttxto;		/* interrupt TX timeout */
186 	int jme_inttxct;		/* interrupt TX packets counter */
187 };
188 
189 #define JME_FLAG_FPGA	0x0001 /* FPGA version */
190 #define JME_FLAG_GIGA	0x0002 /* giga Ethernet capable */
191 
192 
193 #define jme_if	jme_ec.ec_if
194 #define jme_bpf	jme_if.if_bpf
195 
196 typedef struct jme_softc jme_softc_t;
197 typedef u_long ioctl_cmd_t;
198 
199 static int jme_pci_match(device_t, cfdata_t, void *);
200 static void jme_pci_attach(device_t, device_t, void *);
201 static void jme_intr_rx(jme_softc_t *);
202 static int jme_intr(void *);
203 
204 static int jme_ifioctl(struct ifnet *, ioctl_cmd_t, void *);
205 static int jme_mediachange(struct ifnet *);
206 static void jme_ifwatchdog(struct ifnet *);
207 static void jme_shutdown(void *);
208 
209 static void jme_txeof(struct jme_softc *);
210 static void jme_ifstart(struct ifnet *);
211 static void jme_reset(jme_softc_t *);
212 static int  jme_ifinit(struct ifnet *);
213 static int  jme_init(struct ifnet *, int);
214 static void jme_stop(struct ifnet *, int);
215 // static void jme_restart(void *);
216 static void jme_ticks(void *);
217 static void jme_mac_config(jme_softc_t *);
218 static void jme_set_filter(jme_softc_t *);
219 
220 int jme_mii_read(device_t, int, int);
221 void jme_mii_write(device_t, int, int, int);
222 void jme_statchg(device_t);
223 
224 static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *);
225 static int jme_eeprom_macaddr(struct jme_softc *);
226 
227 #define JME_TIMEOUT		1000
228 #define JME_PHY_TIMEOUT		1000
229 #define JME_EEPROM_TIMEOUT	1000
230 
231 static int jme_sysctl_intrxto(SYSCTLFN_PROTO);
232 static int jme_sysctl_intrxct(SYSCTLFN_PROTO);
233 static int jme_sysctl_inttxto(SYSCTLFN_PROTO);
234 static int jme_sysctl_inttxct(SYSCTLFN_PROTO);
235 static int jme_root_num;
236 
237 
238 CFATTACH_DECL_NEW(jme, sizeof(jme_softc_t),
239     jme_pci_match, jme_pci_attach, NULL, NULL);
240 
241 static const struct jme_product_desc jme_products[] = {
242 	{ PCI_PRODUCT_JMICRON_JMC250,
243 	  "JMicron JMC250 Gigabit Ethernet Controller" },
244 	{ PCI_PRODUCT_JMICRON_JMC260,
245 	  "JMicron JMC260 Gigabit Ethernet Controller" },
246 	{ 0, NULL },
247 };
248 
249 static const struct jme_product_desc *jme_lookup_product(uint32_t);
250 
251 static const struct jme_product_desc *
252 jme_lookup_product(uint32_t id)
253 {
254 	const struct jme_product_desc *jp;
255 
256 	for (jp = jme_products ; jp->jme_desc != NULL; jp++)
257 		if (PCI_PRODUCT(id) == jp->jme_product)
258 			return jp;
259 
260 	return NULL;
261 }
262 
263 static int
264 jme_pci_match(device_t parent, cfdata_t cf, void *aux)
265 {
266 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
267 
268 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_JMICRON)
269 		return 0;
270 
271 	if (jme_lookup_product(pa->pa_id) != NULL)
272 		return 1;
273 
274 	return 0;
275 }
276 
277 static void
278 jme_pci_attach(device_t parent, device_t self, void *aux)
279 {
280 	jme_softc_t *sc = device_private(self);
281 	struct pci_attach_args * const pa = (struct pci_attach_args *)aux;
282 	const struct jme_product_desc *jp;
283 	struct ifnet * const ifp = &sc->jme_if;
284 	bus_space_tag_t iot1, iot2, memt;
285 	bus_space_handle_t ioh1, ioh2, memh;
286 	bus_size_t size, size2;
287 	pci_intr_handle_t intrhandle;
288 	const char *intrstr;
289 	pcireg_t csr;
290 	int nsegs, i;
291 	const struct sysctlnode *node;
292 	int jme_nodenum;
293 
294 	sc->jme_dev = self;
295 	aprint_normal("\n");
296 	callout_init(&sc->jme_tick_ch, 0);
297 
298 	jp = jme_lookup_product(pa->pa_id);
299 	if (jp == NULL)
300 		panic("jme_pci_attach: impossible");
301 
302 	if (jp->jme_product == PCI_PRODUCT_JMICRON_JMC250)
303 		sc->jme_flags = JME_FLAG_GIGA;
304 
305 	/*
306 	 * Map the card space. Try Mem first.
307 	 */
308 	if (pci_mapreg_map(pa, JME_PCI_BAR0,
309 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
310 	    0, &memt, &memh, NULL, &size) == 0) {
311 		sc->jme_bt_mac = memt;
312 		sc->jme_bh_mac = memh;
313 		sc->jme_bt_phy = memt;
314 		if (bus_space_subregion(memt, memh, JME_PHY_EEPROM_BASE_MEMOFF,
315 		    JME_PHY_EEPROM_SIZE, &sc->jme_bh_phy) != 0) {
316 			aprint_error_dev(self, "can't subregion PHY space\n");
317 			bus_space_unmap(memt, memh, size);
318 			return;
319 		}
320 		sc->jme_bt_misc = memt;
321 		if (bus_space_subregion(memt, memh, JME_MISC_BASE_MEMOFF,
322 		    JME_MISC_SIZE, &sc->jme_bh_misc) != 0) {
323 			aprint_error_dev(self, "can't subregion misc space\n");
324 			bus_space_unmap(memt, memh, size);
325 			return;
326 		}
327 	} else {
328 		if (pci_mapreg_map(pa, JME_PCI_BAR1, PCI_MAPREG_TYPE_IO,
329 		    0, &iot1, &ioh1, NULL, &size) != 0) {
330 			aprint_error_dev(self, "can't map I/O space 1\n");
331 			return;
332 		}
333 		sc->jme_bt_mac = iot1;
334 		sc->jme_bh_mac = ioh1;
335 		if (pci_mapreg_map(pa, JME_PCI_BAR2, PCI_MAPREG_TYPE_IO,
336 		    0, &iot2, &ioh2, NULL, &size2) != 0) {
337 			aprint_error_dev(self, "can't map I/O space 2\n");
338 			bus_space_unmap(iot1, ioh1, size);
339 			return;
340 		}
341 		sc->jme_bt_phy = iot2;
342 		sc->jme_bh_phy = ioh2;
343 		sc->jme_bt_misc = iot2;
344 		if (bus_space_subregion(iot2, ioh2, JME_MISC_BASE_IOOFF,
345 		    JME_MISC_SIZE, &sc->jme_bh_misc) != 0) {
346 			aprint_error_dev(self, "can't subregion misc space\n");
347 			bus_space_unmap(iot1, ioh1, size);
348 			bus_space_unmap(iot2, ioh2, size2);
349 			return;
350 		}
351 	}
352 
353 	if (pci_dma64_available(pa))
354 		sc->jme_dmatag = pa->pa_dmat64;
355 	else
356 		sc->jme_dmatag = pa->pa_dmat;
357 
358 	/* Enable the device. */
359 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
360 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
361 	    csr | PCI_COMMAND_MASTER_ENABLE);
362 
363 	aprint_normal_dev(self, "%s\n", jp->jme_desc);
364 
365 	sc->jme_rev = PCI_REVISION(pa->pa_class);
366 
367 	csr = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_CHIPMODE);
368 	if (((csr & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) !=
369 	    CHIPMODE_NOT_FPGA)
370 		sc->jme_flags |= JME_FLAG_FPGA;
371 	sc->jme_chip_rev = (csr & CHIPMODE_REV_MASK) >> CHIPMODE_REV_SHIFT;
372 	aprint_verbose_dev(self, "PCI device revision : 0x%x, Chip revision: "
373 	    "0x%x", sc->jme_rev, sc->jme_chip_rev);
374 	if (sc->jme_flags & JME_FLAG_FPGA)
375 		aprint_verbose(" FPGA revision: 0x%x",
376 		    (csr & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT);
377 	aprint_verbose("\n");
378 
379 	/*
380 	 * Save PHY address.
381 	 * Integrated JR0211 has fixed PHY address whereas FPGA version
382 	 * requires PHY probing to get correct PHY address.
383 	 */
384 	if ((sc->jme_flags & JME_FLAG_FPGA) == 0) {
385 		sc->jme_phyaddr =
386 		    bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc,
387 				     JME_GPREG0) & GPREG0_PHY_ADDR_MASK;
388 	} else
389 		sc->jme_phyaddr = 0;
390 
391 
392 	jme_reset(sc);
393 
394 	/* read mac addr */
395 	if (jme_eeprom_macaddr(sc)) {
396 		aprint_error_dev(self, "error reading Ethernet address\n");
397 		/* return; */
398 	}
399 	aprint_normal_dev(self, "Ethernet address %s\n",
400 	    ether_sprintf(sc->jme_enaddr));
401 
402 	/* Map and establish interrupts */
403 	if (pci_intr_map(pa, &intrhandle)) {
404 		aprint_error_dev(self, "couldn't map interrupt\n");
405 		return;
406 	}
407 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
408 	sc->jme_if.if_softc = sc;
409 	sc->jme_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
410 	    jme_intr, sc);
411 	if (sc->jme_ih == NULL) {
412 		aprint_error_dev(self, "couldn't establish interrupt");
413 		if (intrstr != NULL)
414 			aprint_error(" at %s", intrstr);
415 		aprint_error("\n");
416 		return;
417 	}
418 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
419 
420 	/* allocate and map DMA-safe memory for transmit ring */
421 	if (bus_dmamem_alloc(sc->jme_dmatag, PAGE_SIZE, 0, PAGE_SIZE,
422 	    &sc->jme_txseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0 ||
423 	    bus_dmamem_map(sc->jme_dmatag, &sc->jme_txseg,
424 	    nsegs, PAGE_SIZE, (void **)&sc->jme_txring,
425 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0 ||
426 	    bus_dmamap_create(sc->jme_dmatag, PAGE_SIZE, 1, PAGE_SIZE, 0,
427 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->jme_txmap) != 0 ||
428 	    bus_dmamap_load(sc->jme_dmatag, sc->jme_txmap, sc->jme_txring,
429 	    PAGE_SIZE, NULL, BUS_DMA_NOWAIT) != 0) {
430 		aprint_error_dev(self, "can't allocate DMA memory TX ring\n");
431 		return;
432 	}
433 	/* allocate and map DMA-safe memory for receive ring */
434 	if (bus_dmamem_alloc(sc->jme_dmatag, PAGE_SIZE, 0, PAGE_SIZE,
435 	      &sc->jme_rxseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0 ||
436 	    bus_dmamem_map(sc->jme_dmatag, &sc->jme_rxseg,
437 	      nsegs, PAGE_SIZE, (void **)&sc->jme_rxring,
438 	      BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0 ||
439 	    bus_dmamap_create(sc->jme_dmatag, PAGE_SIZE, 1, PAGE_SIZE, 0,
440 	      BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->jme_rxmap) != 0 ||
441 	    bus_dmamap_load(sc->jme_dmatag, sc->jme_rxmap, sc->jme_rxring,
442 	      PAGE_SIZE, NULL, BUS_DMA_NOWAIT) != 0) {
443 		aprint_error_dev(self, "can't allocate DMA memory RX ring\n");
444 		return;
445 	}
446 	for (i = 0; i < JME_NBUFS; i++) {
447 		sc->jme_txmbuf[i] = sc->jme_rxmbuf[i] = NULL;
448 		if (bus_dmamap_create(sc->jme_dmatag, JME_MAX_TX_LEN,
449 		    JME_NBUFS, JME_MAX_TX_LEN, 0, BUS_DMA_NOWAIT,
450 		    &sc->jme_txmbufm[i]) != 0) {
451 			aprint_error_dev(self, "can't allocate DMA TX map\n");
452 			return;
453 		}
454 		if (bus_dmamap_create(sc->jme_dmatag, JME_MAX_RX_LEN,
455 		    1, JME_MAX_RX_LEN, 0, BUS_DMA_NOWAIT,
456 		    &sc->jme_rxmbufm[i]) != 0) {
457 			aprint_error_dev(self, "can't allocate DMA RX map\n");
458 			return;
459 		}
460 	}
461 	/*
462 	 * Add shutdown hook so that DMA is disabled prior to reboot.
463 	 */
464 	(void)shutdownhook_establish(jme_shutdown, ifp);
465 
466 	/*
467 	 * Initialize our media structures and probe the MII.
468 	 *
469 	 * Note that we don't care about the media instance.  We
470 	 * are expecting to have multiple PHYs on the 10/100 cards,
471 	 * and on those cards we exclude the internal PHY from providing
472 	 * 10baseT.  By ignoring the instance, it allows us to not have
473 	 * to specify it on the command line when switching media.
474 	 */
475 	sc->jme_mii.mii_ifp = ifp;
476 	sc->jme_mii.mii_readreg = jme_mii_read;
477 	sc->jme_mii.mii_writereg = jme_mii_write;
478 	sc->jme_mii.mii_statchg = jme_statchg;
479 	sc->jme_ec.ec_mii = &sc->jme_mii;
480 	ifmedia_init(&sc->jme_mii.mii_media, IFM_IMASK, jme_mediachange,
481 	    ether_mediastatus);
482 	mii_attach(self, &sc->jme_mii, 0xffffffff, MII_PHY_ANY,
483 	    MII_OFFSET_ANY, 0);
484 	if (LIST_FIRST(&sc->jme_mii.mii_phys) == NULL) {
485 		ifmedia_add(&sc->jme_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
486 		ifmedia_set(&sc->jme_mii.mii_media, IFM_ETHER|IFM_NONE);
487 	} else
488 		ifmedia_set(&sc->jme_mii.mii_media, IFM_ETHER|IFM_AUTO);
489 
490 	/*
491 	 * We can support 802.1Q VLAN-sized frames.
492 	 */
493 	sc->jme_ec.ec_capabilities |=
494 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
495 
496 	if (sc->jme_flags & JME_FLAG_GIGA)
497 		sc->jme_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
498 
499 
500 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
501 	ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
502 	ifp->if_ioctl = jme_ifioctl;
503 	ifp->if_start = jme_ifstart;
504 	ifp->if_watchdog = jme_ifwatchdog;
505 	ifp->if_init = jme_ifinit;
506 	ifp->if_stop = jme_stop;
507 	ifp->if_timer = 0;
508 	ifp->if_capabilities |=
509 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
510 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
511 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
512 	    IFCAP_CSUM_TCPv6_Tx | /* IFCAP_CSUM_TCPv6_Rx | hardware bug */
513 	    IFCAP_CSUM_UDPv6_Tx | /* IFCAP_CSUM_UDPv6_Rx | hardware bug */
514 	    IFCAP_TSOv4 | IFCAP_TSOv6;
515 	IFQ_SET_READY(&ifp->if_snd);
516 	if_attach(ifp);
517 	ether_ifattach(&(sc)->jme_if, (sc)->jme_enaddr);
518 
519 #if NRND > 0
520 	rnd_attach_source(&sc->rnd_source, device_xname(self),
521 	    RND_TYPE_NET, 0);
522 #endif
523 	sc->jme_intrxto = PCCRX_COAL_TO_DEFAULT;
524 	sc->jme_intrxct = PCCRX_COAL_PKT_DEFAULT;
525 	sc->jme_inttxto = PCCTX_COAL_TO_DEFAULT;
526 	sc->jme_inttxct = PCCTX_COAL_PKT_DEFAULT;
527 	if (sysctl_createv(&sc->jme_clog, 0, NULL, &node,
528 	    0, CTLTYPE_NODE, device_xname(sc->jme_dev),
529 	    SYSCTL_DESCR("jme per-controller controls"),
530 	    NULL, 0, NULL, 0, CTL_HW, jme_root_num, CTL_CREATE,
531 	    CTL_EOL) != 0) {
532 		aprint_normal_dev(sc->jme_dev, "couldn't create sysctl node\n");
533 		return;
534 	}
535 	jme_nodenum = node->sysctl_num;
536 
537 	/* interrupt moderation sysctls */
538 	if (sysctl_createv(&sc->jme_clog, 0, NULL, &node,
539 	    CTLFLAG_READWRITE,
540 	    CTLTYPE_INT, "int_rxto",
541 	    SYSCTL_DESCR("jme RX interrupt moderation timer"),
542 	    jme_sysctl_intrxto, 0, sc,
543 	    0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE,
544 	    CTL_EOL) != 0) {
545 		aprint_normal_dev(sc->jme_dev,
546 		    "couldn't create int_rxto sysctl node\n");
547 	}
548 	if (sysctl_createv(&sc->jme_clog, 0, NULL, &node,
549 	    CTLFLAG_READWRITE,
550 	    CTLTYPE_INT, "int_rxct",
551 	    SYSCTL_DESCR("jme RX interrupt moderation packet counter"),
552 	    jme_sysctl_intrxct, 0, sc,
553 	    0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE,
554 	    CTL_EOL) != 0) {
555 		aprint_normal_dev(sc->jme_dev,
556 		    "couldn't create int_rxct sysctl node\n");
557 	}
558 	if (sysctl_createv(&sc->jme_clog, 0, NULL, &node,
559 	    CTLFLAG_READWRITE,
560 	    CTLTYPE_INT, "int_txto",
561 	    SYSCTL_DESCR("jme TX interrupt moderation timer"),
562 	    jme_sysctl_inttxto, 0, sc,
563 	    0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE,
564 	    CTL_EOL) != 0) {
565 		aprint_normal_dev(sc->jme_dev,
566 		    "couldn't create int_txto sysctl node\n");
567 	}
568 	if (sysctl_createv(&sc->jme_clog, 0, NULL, &node,
569 	    CTLFLAG_READWRITE,
570 	    CTLTYPE_INT, "int_txct",
571 	    SYSCTL_DESCR("jme TX interrupt moderation packet counter"),
572 	    jme_sysctl_inttxct, 0, sc,
573 	    0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE,
574 	    CTL_EOL) != 0) {
575 		aprint_normal_dev(sc->jme_dev,
576 		    "couldn't create int_txct sysctl node\n");
577 	}
578 }
579 
580 static void
581 jme_stop_rx(jme_softc_t *sc)
582 {
583 	uint32_t reg;
584 	int i;
585 
586 	reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR);
587 	if ((reg & RXCSR_RX_ENB) == 0)
588 		return;
589 	reg &= ~RXCSR_RX_ENB;
590 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR, reg);
591 	for (i = JME_TIMEOUT / 10; i > 0; i--) {
592 		DELAY(10);
593 		if ((bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac,
594 		    JME_RXCSR) & RXCSR_RX_ENB) == 0)
595 			break;
596 	}
597 	if (i == 0)
598 		aprint_error_dev(sc->jme_dev, "stopping recevier timeout!\n");
599 
600 }
601 
602 static void
603 jme_stop_tx(jme_softc_t *sc)
604 {
605 	uint32_t reg;
606 	int i;
607 
608 	reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR);
609 	if ((reg & TXCSR_TX_ENB) == 0)
610 		return;
611 	reg &= ~TXCSR_TX_ENB;
612 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, reg);
613 	for (i = JME_TIMEOUT / 10; i > 0; i--) {
614 		DELAY(10);
615 		if ((bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac,
616 		    JME_TXCSR) & TXCSR_TX_ENB) == 0)
617 			break;
618 	}
619 	if (i == 0)
620 		aprint_error_dev(sc->jme_dev,
621 		    "stopping transmitter timeout!\n");
622 }
623 
624 static void
625 jme_reset(jme_softc_t *sc)
626 {
627 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, GHC_RESET);
628 	DELAY(10);
629 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, 0);
630 }
631 
632 static void
633 jme_shutdown(void *v)
634 {
635 
636 	jme_stop(v, 1);
637 }
638 
639 static void
640 jme_stop(struct ifnet *ifp, int disable)
641 {
642 	jme_softc_t *sc = ifp->if_softc;
643 	int i;
644 	/* Stop receiver, transmitter. */
645 	jme_stop_rx(sc);
646 	jme_stop_tx(sc);
647 	/* free receive mbufs */
648 	for (i = 0; i < JME_NBUFS; i++) {
649 		if (sc->jme_rxmbuf[i]) {
650 			bus_dmamap_unload(sc->jme_dmatag, sc->jme_rxmbufm[i]);
651 			m_freem(sc->jme_rxmbuf[i]);
652 		}
653 		sc->jme_rxmbuf[i] = NULL;
654 	}
655 	/* process completed transmits */
656 	jme_txeof(sc);
657 	/* free abort pending transmits */
658 	for (i = 0; i < JME_NBUFS; i++) {
659 		if (sc->jme_txmbuf[i]) {
660 			bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[i]);
661 			m_freem(sc->jme_txmbuf[i]);
662 			sc->jme_txmbuf[i] = NULL;
663 		}
664 	}
665 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
666 	ifp->if_timer = 0;
667 }
668 
669 #if 0
670 static void
671 jme_restart(void *v)
672 {
673 
674 	jme_init(v);
675 }
676 #endif
677 
678 static int
679 jme_add_rxbuf(jme_softc_t *sc, struct mbuf *m)
680 {
681 	int error;
682 	bus_dmamap_t map;
683 	int i = sc->jme_rx_prod;
684 
685 	if (sc->jme_rxmbuf[i] != NULL) {
686 		aprint_error_dev(sc->jme_dev,
687 		    "mbuf already here: rxprod %d rxcons %d\n",
688 		    sc->jme_rx_prod, sc->jme_rx_cons);
689 		if (m)
690 			m_freem(m);
691 		return EINVAL;
692 	}
693 
694 	if (m == NULL) {
695 		sc->jme_rxmbuf[i] = NULL;
696 		MGETHDR(m, M_DONTWAIT, MT_DATA);
697 		if (m == NULL)
698 			return (ENOBUFS);
699 		MCLGET(m, M_DONTWAIT);
700 		if ((m->m_flags & M_EXT) == 0) {
701 			m_freem(m);
702 			return (ENOBUFS);
703 		}
704 	}
705 	map = sc->jme_rxmbufm[i];
706 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
707 	error = bus_dmamap_load_mbuf(sc->jme_dmatag, map, m,
708 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
709 	if (error) {
710 		sc->jme_rxmbuf[i] = NULL;
711 		aprint_error_dev(sc->jme_dev,
712 		    "unable to load rx DMA map %d, error = %d\n",
713 		    i, error);
714 		m_freem(m);
715 		return (error);
716 	}
717 	bus_dmamap_sync(sc->jme_dmatag, map, 0, map->dm_mapsize,
718 	    BUS_DMASYNC_PREREAD);
719 
720 	sc->jme_rxmbuf[i] = m;
721 
722 	sc->jme_rxring[i].buflen = htole32(map->dm_segs[0].ds_len);
723 	sc->jme_rxring[i].addr_lo =
724 	    htole32(JME_ADDR_LO(map->dm_segs[0].ds_addr));
725 	sc->jme_rxring[i].addr_hi =
726 	    htole32(JME_ADDR_HI(map->dm_segs[0].ds_addr));
727 	sc->jme_rxring[i].flags =
728 	    htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
729 	bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmap,
730 	    i * sizeof(struct jme_desc), sizeof(struct jme_desc),
731 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
732 	JME_DESC_INC(sc->jme_rx_prod, JME_NBUFS);
733 	return (0);
734 }
735 
736 static int
737 jme_ifinit(struct ifnet *ifp)
738 {
739 	return jme_init(ifp, 1);
740 }
741 
742 static int
743 jme_init(struct ifnet *ifp, int do_ifinit)
744 {
745 	jme_softc_t *sc = ifp->if_softc;
746 	int i, s;
747 	uint8_t eaddr[ETHER_ADDR_LEN];
748 	uint32_t reg;
749 
750 	s = splnet();
751 	/* cancel any pending IO */
752 	jme_stop(ifp, 1);
753 	jme_reset(sc);
754 	if ((sc->jme_if.if_flags & IFF_UP) == 0) {
755 		splx(s);
756 		return 0;
757 	}
758 	/* allocate receive ring */
759 	sc->jme_rx_prod = 0;
760 	for (i = 0; i < JME_NBUFS; i++) {
761 		if (jme_add_rxbuf(sc, NULL) < 0) {
762 			aprint_error_dev(sc->jme_dev,
763 			    "can't allocate rx mbuf\n");
764 			for (i--; i >= 0; i--) {
765 				bus_dmamap_unload(sc->jme_dmatag,
766 				    sc->jme_rxmbufm[i]);
767 				m_freem(sc->jme_rxmbuf[i]);
768 				sc->jme_rxmbuf[i] = NULL;
769 			}
770 			splx(s);
771 			return ENOMEM;
772 		}
773 	}
774 	/* init TX ring */
775 	memset(sc->jme_txring, 0, JME_NBUFS * sizeof(struct jme_desc));
776 	bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap,
777 	    0, JME_NBUFS * sizeof(struct jme_desc),
778 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
779 	for (i = 0; i < JME_NBUFS; i++)
780 		sc->jme_txmbuf[i] = NULL;
781 	sc->jme_tx_cons = sc->jme_tx_prod = sc->jme_tx_cnt = 0;
782 
783 	/* Reprogram the station address. */
784 	memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
785 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR0,
786 	    eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
787 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
788 	    JME_PAR1, eaddr[5] << 8 | eaddr[4]);
789 
790 	/*
791 	 * Configure Tx queue.
792 	 *  Tx priority queue weight value : 0
793 	 *  Tx FIFO threshold for processing next packet : 16QW
794 	 *  Maximum Tx DMA length : 512
795 	 *  Allow Tx DMA burst.
796 	 */
797 	sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
798 	sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
799 	sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
800 	sc->jme_txcsr |= TXCSR_DMA_SIZE_512;
801 	sc->jme_txcsr |= TXCSR_DMA_BURST;
802 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
803 	     JME_TXCSR, sc->jme_txcsr);
804 
805 	/* Set Tx descriptor counter. */
806 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
807 	     JME_TXQDC, JME_NBUFS);
808 
809 	/* Set Tx ring address to the hardware. */
810 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI,
811 	    JME_ADDR_HI(sc->jme_txmap->dm_segs[0].ds_addr));
812 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO,
813 	    JME_ADDR_LO(sc->jme_txmap->dm_segs[0].ds_addr));
814 
815 	/* Configure TxMAC parameters. */
816 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC,
817 	    TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB |
818 	    TXMAC_THRESH_1_PKT | TXMAC_CRC_ENB | TXMAC_PAD_ENB);
819 
820 	/*
821 	 * Configure Rx queue.
822 	 *  FIFO full threshold for transmitting Tx pause packet : 128T
823 	 *  FIFO threshold for processing next packet : 128QW
824 	 *  Rx queue 0 select
825 	 *  Max Rx DMA length : 128
826 	 *  Rx descriptor retry : 32
827 	 *  Rx descriptor retry time gap : 256ns
828 	 *  Don't receive runt/bad frame.
829 	 */
830 	sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
831 	/*
832 	 * Since Rx FIFO size is 4K bytes, receiving frames larger
833 	 * than 4K bytes will suffer from Rx FIFO overruns. So
834 	 * decrease FIFO threshold to reduce the FIFO overruns for
835 	 * frames larger than 4000 bytes.
836 	 * For best performance of standard MTU sized frames use
837 	 * maximum allowable FIFO threshold, 128QW.
838 	 */
839 	if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
840 	    ETHER_CRC_LEN) > JME_RX_FIFO_SIZE)
841 		sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
842 	else
843 		sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
844 	sc->jme_rxcsr |= RXCSR_DMA_SIZE_128 | RXCSR_RXQ_N_SEL(RXCSR_RXQ0);
845 	sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
846 	sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
847 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
848 	     JME_RXCSR, sc->jme_rxcsr);
849 
850 	/* Set Rx descriptor counter. */
851 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
852 	     JME_RXQDC, JME_NBUFS);
853 
854 	/* Set Rx ring address to the hardware. */
855 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_HI,
856 	    JME_ADDR_HI(sc->jme_rxmap->dm_segs[0].ds_addr));
857 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_LO,
858 	    JME_ADDR_LO(sc->jme_rxmap->dm_segs[0].ds_addr));
859 
860 	/* Clear receive filter. */
861 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, 0);
862 	/* Set up the receive filter. */
863 	jme_set_filter(sc);
864 
865 	/*
866 	 * Disable all WOL bits as WOL can interfere normal Rx
867 	 * operation. Also clear WOL detection status bits.
868 	 */
869 	reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PMCS);
870 	reg &= ~PMCS_WOL_ENB_MASK;
871 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PMCS, reg);
872 
873 	reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC);
874 	/*
875 	 * Pad 10bytes right before received frame. This will greatly
876 	 * help Rx performance on strict-alignment architectures as
877 	 * it does not need to copy the frame to align the payload.
878 	 */
879 	reg |= RXMAC_PAD_10BYTES;
880 	if ((ifp->if_capenable &
881 	    (IFCAP_CSUM_IPv4_Rx|IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx|
882 	     IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx)) != 0)
883 		reg |= RXMAC_CSUM_ENB;
884 	reg |= RXMAC_VLAN_ENB; /* enable hardware vlan */
885 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, reg);
886 
887 	/* Configure general purpose reg0 */
888 	reg = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_GPREG0);
889 	reg &= ~GPREG0_PCC_UNIT_MASK;
890 	/* Set PCC timer resolution to micro-seconds unit. */
891 	reg |= GPREG0_PCC_UNIT_US;
892 	/*
893 	 * Disable all shadow register posting as we have to read
894 	 * JME_INTR_STATUS register in jme_int_task. Also it seems
895 	 * that it's hard to synchronize interrupt status between
896 	 * hardware and software with shadow posting due to
897 	 * requirements of bus_dmamap_sync(9).
898 	 */
899 	reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
900 	    GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
901 	    GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
902 	    GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
903 	/* Disable posting of DW0. */
904 	reg &= ~GPREG0_POST_DW0_ENB;
905 	/* Clear PME message. */
906 	reg &= ~GPREG0_PME_ENB;
907 	/* Set PHY address. */
908 	reg &= ~GPREG0_PHY_ADDR_MASK;
909 	reg |= sc->jme_phyaddr;
910 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_GPREG0, reg);
911 
912 	/* Configure Tx queue 0 packet completion coalescing. */
913 	reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK;
914 	reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK;
915 	reg |= PCCTX_COAL_TXQ0;
916 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg);
917 
918 	/* Configure Rx queue 0 packet completion coalescing. */
919 	reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK;
920 	reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK;
921 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg);
922 
923 	/* Disable Timers */
924 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TMCSR, 0);
925 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TIMER1, 0);
926 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TIMER2, 0);
927 
928 	/* Configure retry transmit period, retry limit value. */
929 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD,
930 	    ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
931 	    TXTRHD_RT_PERIOD_MASK) |
932 	    ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
933 	    TXTRHD_RT_LIMIT_SHIFT));
934 
935 	/* Disable RSS. */
936 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
937 	    JME_RSSC, RSSC_DIS_RSS);
938 
939 	/* Initialize the interrupt mask. */
940 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
941 	     JME_INTR_MASK_SET, JME_INTRS_ENABLE);
942 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
943 	     JME_INTR_STATUS, 0xFFFFFFFF);
944 
945 	/* set media, if not already handling a media change */
946 	if (do_ifinit) {
947 		int error;
948 		if ((error = mii_mediachg(&sc->jme_mii)) == ENXIO)
949 			error = 0;
950 		else if (error != 0) {
951 			aprint_error_dev(sc->jme_dev, "could not set media\n");
952 			return error;
953 		}
954 	}
955 
956 	/* Program MAC with resolved speed/duplex/flow-control. */
957 	jme_mac_config(sc);
958 
959 	/* Start receiver/transmitter. */
960 	sc->jme_rx_cons = 0;
961 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR,
962 	    sc->jme_rxcsr | RXCSR_RX_ENB | RXCSR_RXQ_START);
963 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR,
964 	    sc->jme_txcsr | TXCSR_TX_ENB);
965 
966 	/* start ticks calls */
967 	callout_reset(&sc->jme_tick_ch, hz, jme_ticks, sc);
968 	sc->jme_if.if_flags |= IFF_RUNNING;
969 	sc->jme_if.if_flags &= ~IFF_OACTIVE;
970 	splx(s);
971 	return 0;
972 }
973 
974 
975 int
976 jme_mii_read(device_t self, int phy, int reg)
977 {
978 	struct jme_softc *sc = device_private(self);
979 	int val, i;
980 
981 	/* For FPGA version, PHY address 0 should be ignored. */
982 	if ((sc->jme_flags & JME_FLAG_FPGA) != 0) {
983 		if (phy == 0)
984 			return (0);
985 	} else {
986 		if (sc->jme_phyaddr != phy)
987 			return (0);
988 	}
989 
990 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_SMI,
991 	    SMI_OP_READ | SMI_OP_EXECUTE |
992 	    SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
993 	for (i = JME_PHY_TIMEOUT / 10; i > 0; i--) {
994 		delay(10);
995 		if (((val = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac,
996 		    JME_SMI)) & SMI_OP_EXECUTE) == 0)
997 			break;
998 	}
999 
1000 	if (i == 0) {
1001 		aprint_error_dev(sc->jme_dev, "phy read timeout : %d\n", reg);
1002 		return (0);
1003 	}
1004 
1005 	return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT);
1006 }
1007 
1008 void
1009 jme_mii_write(device_t self, int phy, int reg, int val)
1010 {
1011 	struct jme_softc *sc = device_private(self);
1012 	int i;
1013 
1014 	/* For FPGA version, PHY address 0 should be ignored. */
1015 	if ((sc->jme_flags & JME_FLAG_FPGA) != 0) {
1016 		if (phy == 0)
1017 			return;
1018 	} else {
1019 		if (sc->jme_phyaddr != phy)
1020 			return;
1021 	}
1022 
1023 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_SMI,
1024 	    SMI_OP_WRITE | SMI_OP_EXECUTE |
1025 	    ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
1026 	    SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
1027 	for (i = JME_PHY_TIMEOUT / 10; i > 0; i--) {
1028 		delay(10);
1029 		if (((val = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac,
1030 		    JME_SMI)) & SMI_OP_EXECUTE) == 0)
1031 			break;
1032 	}
1033 
1034 	if (i == 0)
1035 		aprint_error_dev(sc->jme_dev, "phy write timeout : %d\n", reg);
1036 
1037 	return;
1038 }
1039 
1040 void
1041 jme_statchg(device_t self)
1042 {
1043 	jme_softc_t *sc = device_private(self);
1044 	struct ifnet *ifp = &sc->jme_if;
1045 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))
1046 		jme_init(ifp, 0);
1047 }
1048 
1049 static void
1050 jme_intr_rx(jme_softc_t *sc) {
1051 	struct mbuf *m, *mhead;
1052 	struct ifnet *ifp = &sc->jme_if;
1053 	uint32_t flags,  buflen;
1054 	int i, ipackets, nsegs, seg, error;
1055 	struct jme_desc *desc;
1056 
1057 	bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmap, 0,
1058 	    sizeof(struct jme_desc) * JME_NBUFS,
1059 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1060 #ifdef JMEDEBUG_RX
1061 	printf("rxintr sc->jme_rx_cons %d flags 0x%x\n",
1062 	    sc->jme_rx_cons, le32toh(sc->jme_rxring[sc->jme_rx_cons].flags));
1063 #endif
1064 	ipackets = 0;
1065 	while((le32toh(sc->jme_rxring[ sc->jme_rx_cons].flags) & JME_RD_OWN)
1066 	    == 0) {
1067 		i = sc->jme_rx_cons;
1068 		desc = &sc->jme_rxring[i];
1069 #ifdef JMEDEBUG_RX
1070 		printf("rxintr i %d flags 0x%x buflen 0x%x\n",
1071 		    i,  le32toh(desc->flags), le32toh(desc->buflen));
1072 #endif
1073 		if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
1074 			break;
1075 		bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmbufm[i], 0,
1076 		    sc->jme_rxmbufm[i]->dm_mapsize, BUS_DMASYNC_POSTREAD);
1077 		bus_dmamap_unload(sc->jme_dmatag, sc->jme_rxmbufm[i]);
1078 
1079 		buflen = le32toh(desc->buflen);
1080 		nsegs = JME_RX_NSEGS(buflen);
1081 		flags = le32toh(desc->flags);
1082 		if ((buflen & JME_RX_ERR_STAT) != 0 ||
1083 		    JME_RX_BYTES(buflen) < sizeof(struct ether_header) ||
1084 		    JME_RX_BYTES(buflen) >
1085 		    (ifp->if_mtu + ETHER_HDR_LEN + JME_RX_PAD_BYTES)) {
1086 #ifdef JMEDEBUG_RX
1087 			printf("rx error flags 0x%x buflen 0x%x\n",
1088 			    flags, buflen);
1089 #endif
1090 			ifp->if_ierrors++;
1091 			/* reuse the mbufs */
1092 			for (seg = 0; seg < nsegs; seg++) {
1093 				m = sc->jme_rxmbuf[i];
1094 				sc->jme_rxmbuf[i] = NULL;
1095 				if ((error = jme_add_rxbuf(sc, m)) != 0)
1096 					aprint_error_dev(sc->jme_dev,
1097 					    "can't reuse mbuf: %d\n", error);
1098 				JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS);
1099 				i = sc->jme_rx_cons;
1100 			}
1101 			continue;
1102 		}
1103 		/* receive this packet */
1104 		mhead = m = sc->jme_rxmbuf[i];
1105 		sc->jme_rxmbuf[i] = NULL;
1106 		/* add a new buffer to chain */
1107 		if (jme_add_rxbuf(sc, NULL) == ENOBUFS) {
1108 			for (seg = 0; seg < nsegs; seg++) {
1109 				m = sc->jme_rxmbuf[i];
1110 				sc->jme_rxmbuf[i] = NULL;
1111 				if ((error = jme_add_rxbuf(sc, m)) != 0)
1112 					aprint_error_dev(sc->jme_dev,
1113 					    "can't reuse mbuf: %d\n", error);
1114 				JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS);
1115 				i = sc->jme_rx_cons;
1116 			}
1117 			ifp->if_ierrors++;
1118 			continue;
1119 		}
1120 
1121 		/* build mbuf chain: head, then remaining segments */
1122 		m->m_pkthdr.rcvif = ifp;
1123 		m->m_pkthdr.len = JME_RX_BYTES(buflen) - JME_RX_PAD_BYTES;
1124 		m->m_len = (nsegs > 1) ? (MCLBYTES - JME_RX_PAD_BYTES) :
1125 		    m->m_pkthdr.len;
1126 		m->m_data = m->m_ext.ext_buf + JME_RX_PAD_BYTES;
1127 		JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS);
1128 		for (seg = 1; seg < nsegs; seg++) {
1129 			i = sc->jme_rx_cons;
1130 			m = sc->jme_rxmbuf[i];
1131 			sc->jme_rxmbuf[i] = NULL;
1132 			(void)jme_add_rxbuf(sc, NULL);
1133 			m->m_flags &= ~M_PKTHDR;
1134 			m_cat(mhead, m);
1135 			JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS);
1136 		}
1137 		/* and adjust last mbuf's size */
1138 		if (nsegs > 1) {
1139 			m->m_len =
1140 			    JME_RX_BYTES(buflen) - (MCLBYTES * (nsegs - 1));
1141 		}
1142 		ifp->if_ipackets++;
1143 		ipackets++;
1144 #if NBPFILTER > 0
1145 		if (ifp->if_bpf)
1146 			bpf_mtap(ifp->if_bpf, mhead);
1147 #endif /* NBPFILTER > 0 */
1148 
1149 		if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) &&
1150 		    (flags & JME_RD_IPV4)) {
1151 			mhead->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1152 			if (!(flags & JME_RD_IPCSUM))
1153 				mhead->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1154 		}
1155 		if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) &&
1156 		    (flags & JME_RD_TCPV4) == JME_RD_TCPV4) {
1157 			mhead->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1158 			if (!(flags & JME_RD_TCPCSUM))
1159 				mhead->m_pkthdr.csum_flags |=
1160 				    M_CSUM_TCP_UDP_BAD;
1161 		}
1162 		if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) &&
1163 		    (flags & JME_RD_UDPV4) == JME_RD_UDPV4) {
1164 			mhead->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1165 			if (!(flags & JME_RD_UDPCSUM))
1166 				mhead->m_pkthdr.csum_flags |=
1167 				    M_CSUM_TCP_UDP_BAD;
1168 		}
1169 		if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) &&
1170 		    (flags & JME_RD_TCPV6) == JME_RD_TCPV6) {
1171 			mhead->m_pkthdr.csum_flags |= M_CSUM_TCPv6;
1172 			if (!(flags & JME_RD_TCPCSUM))
1173 				mhead->m_pkthdr.csum_flags |=
1174 				    M_CSUM_TCP_UDP_BAD;
1175 		}
1176 		if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) &&
1177 		    (flags & JME_RD_UDPV6) == JME_RD_UDPV6) {
1178 			m->m_pkthdr.csum_flags |= M_CSUM_UDPv6;
1179 			if (!(flags & JME_RD_UDPCSUM))
1180 				mhead->m_pkthdr.csum_flags |=
1181 				    M_CSUM_TCP_UDP_BAD;
1182 		}
1183 		if (flags & JME_RD_VLAN_TAG) {
1184 			/* pass to vlan_input() */
1185 			VLAN_INPUT_TAG(ifp, mhead,
1186 			    (flags & JME_RD_VLAN_MASK), continue);
1187 		}
1188 		(*ifp->if_input)(ifp, mhead);
1189 	}
1190 #if NRND > 0
1191 	if (ipackets && RND_ENABLED(&sc->rnd_source))
1192 		rnd_add_uint32(&sc->rnd_source, ipackets);
1193 #endif /* NRND > 0 */
1194 
1195 }
1196 
1197 static int
1198 jme_intr(void *v)
1199 {
1200 	jme_softc_t *sc = v;
1201 	uint32_t istatus;
1202 
1203 	istatus = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc,
1204 	     JME_INTR_STATUS);
1205 	if (istatus == 0 || istatus == 0xFFFFFFFF)
1206 		return 0;
1207 	/* Disable interrupts. */
1208 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
1209 	    JME_INTR_MASK_CLR, 0xFFFFFFFF);
1210 again:
1211 	/* and update istatus */
1212 	istatus = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc,
1213 	     JME_INTR_STATUS);
1214 	if ((istatus & JME_INTRS_CHECK) == 0)
1215 		goto done;
1216 	/* Reset PCC counter/timer and Ack interrupts. */
1217 	if ((istatus & (INTR_TXQ_COMP | INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) != 0)
1218 		istatus |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
1219 	if ((istatus & (INTR_RXQ_COMP | INTR_RXQ_COAL | INTR_RXQ_COAL_TO)) != 0)
1220 		istatus |= INTR_RXQ_COAL | INTR_RXQ_COAL_TO | INTR_RXQ_COMP;
1221 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
1222 	     JME_INTR_STATUS, istatus);
1223 
1224 	if ((sc->jme_if.if_flags & IFF_RUNNING) == 0)
1225 		goto done;
1226 #ifdef JMEDEBUG_RX
1227 	printf("jme_intr 0x%x RXCS 0x%x RXDBA 0x%x  0x%x RXQDC 0x%x RXNDA 0x%x RXMCS 0x%x\n", istatus,
1228 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR),
1229 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_LO),
1230 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_HI),
1231 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXQDC),
1232 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXNDA),
1233 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC));
1234 	printf("jme_intr RXUMA 0x%x 0x%x RXMCHT 0x%x 0x%x GHC 0x%x\n",
1235 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR0),
1236 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR1),
1237 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR0),
1238 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR1),
1239 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC));
1240 #endif
1241 	if ((istatus & (INTR_RXQ_COMP | INTR_RXQ_COAL | INTR_RXQ_COAL_TO)) != 0)
1242 		jme_intr_rx(sc);
1243 	if ((istatus & INTR_RXQ_DESC_EMPTY) != 0) {
1244 		/*
1245 		 * Notify hardware availability of new Rx
1246 		 * buffers.
1247 		 * Reading RXCSR takes very long time under
1248 		 * heavy load so cache RXCSR value and writes
1249 		 * the ORed value with the kick command to
1250 		 * the RXCSR. This saves one register access
1251 		 * cycle.
1252 		 */
1253 		sc->jme_rx_cons = 0;
1254 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
1255 		    JME_RXCSR,
1256 		    sc->jme_rxcsr | RXCSR_RX_ENB | RXCSR_RXQ_START);
1257 	}
1258 	if ((istatus & (INTR_TXQ_COMP | INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) != 0)
1259 		jme_ifstart(&sc->jme_if);
1260 
1261 	goto again;
1262 
1263 done:
1264 	/* enable interrupts. */
1265 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
1266 	    JME_INTR_MASK_SET, JME_INTRS_ENABLE);
1267 	return 1;
1268 }
1269 
1270 
1271 static int
1272 jme_ifioctl(struct ifnet *ifp, unsigned long cmd, void *data)
1273 {
1274 	struct jme_softc *sc = ifp->if_softc;
1275 	int s, error;
1276 	struct ifreq *ifr;
1277 	struct ifcapreq *ifcr;
1278 
1279 	s = splnet();
1280 	/*
1281 	 * we can't support at the same time jumbo frames and
1282 	 * TX checksums offload/TSO
1283 	 */
1284 	switch(cmd) {
1285 	case SIOCSIFMTU:
1286 		ifr = data;
1287 		if (ifr->ifr_mtu > JME_TX_FIFO_SIZE &&
1288 		    (ifp->if_capenable & (
1289 		    IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx|
1290 		    IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_UDPv6_Tx|
1291 		    IFCAP_TSOv4|IFCAP_TSOv6)) != 0) {
1292 			splx(s);
1293 			return EINVAL;
1294 		}
1295 		break;
1296 	case SIOCSIFCAP:
1297 		ifcr = data;
1298 		if (ifp->if_mtu > JME_TX_FIFO_SIZE &&
1299 		    (ifcr->ifcr_capenable & (
1300 		    IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx|
1301 		    IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_UDPv6_Tx|
1302 		    IFCAP_TSOv4|IFCAP_TSOv6)) != 0) {
1303 			splx(s);
1304 			return EINVAL;
1305 		}
1306 		break;
1307 	}
1308 
1309 	error = ether_ioctl(ifp, cmd, data);
1310 	if (error == ENETRESET && (ifp->if_flags & IFF_RUNNING)) {
1311 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
1312 			jme_set_filter(sc);
1313 			error = 0;
1314 		} else {
1315 			error = jme_init(ifp, 0);
1316 		}
1317 	}
1318 	splx(s);
1319 	return error;
1320 }
1321 
1322 static int
1323 jme_encap(struct jme_softc *sc, struct mbuf **m_head)
1324 {
1325 	struct jme_desc *txd;
1326 	struct jme_desc *desc;
1327 	struct mbuf *m;
1328 	struct m_tag *mtag;
1329 	int error, i, prod, headdsc, nsegs;
1330 	uint32_t cflags, tso_segsz;
1331 
1332 	if (((*m_head)->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6)) != 0){
1333 		/*
1334 		 * Due to the adherence to NDIS specification JMC250
1335 		 * assumes upper stack computed TCP pseudo checksum
1336 		 * without including payload length. This breaks
1337 		 * checksum offload for TSO case so recompute TCP
1338 		 * pseudo checksum for JMC250. Hopefully this wouldn't
1339 		 * be much burden on modern CPUs.
1340 		 */
1341 		bool v4 = ((*m_head)->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0;
1342 		int iphl = v4 ?
1343 		    M_CSUM_DATA_IPv4_IPHL((*m_head)->m_pkthdr.csum_data) :
1344 		    M_CSUM_DATA_IPv6_HL((*m_head)->m_pkthdr.csum_data);
1345 		/*
1346 		 * note: we support vlan offloading, so we should never have
1347 		 * a ETHERTYPE_VLAN packet here - so ETHER_HDR_LEN is always
1348 		 * right.
1349 		 */
1350 		int hlen = ETHER_HDR_LEN + iphl;
1351 
1352 		if (__predict_false((*m_head)->m_len <
1353 		    (hlen + sizeof(struct tcphdr)))) {
1354 			   /*
1355 			    * TCP/IP headers are not in the first mbuf; we need
1356 			    * to do this the slow and painful way.  Let's just
1357 			    * hope this doesn't happen very often.
1358 			    */
1359 			   struct tcphdr th;
1360 
1361 			   m_copydata((*m_head), hlen, sizeof(th), &th);
1362 			   if (v4) {
1363 				    struct ip ip;
1364 
1365 				    m_copydata((*m_head), ETHER_HDR_LEN,
1366 				    sizeof(ip), &ip);
1367 				    ip.ip_len = 0;
1368 				    m_copyback((*m_head),
1369 					 ETHER_HDR_LEN + offsetof(struct ip, ip_len),
1370 					 sizeof(ip.ip_len), &ip.ip_len);
1371 				    th.th_sum = in_cksum_phdr(ip.ip_src.s_addr,
1372 					 ip.ip_dst.s_addr, htons(IPPROTO_TCP));
1373 			   } else {
1374 #if INET6
1375 				    struct ip6_hdr ip6;
1376 
1377 				    m_copydata((*m_head), ETHER_HDR_LEN,
1378 				    sizeof(ip6), &ip6);
1379 				    ip6.ip6_plen = 0;
1380 				    m_copyback((*m_head), ETHER_HDR_LEN +
1381 				    offsetof(struct ip6_hdr, ip6_plen),
1382 					 sizeof(ip6.ip6_plen), &ip6.ip6_plen);
1383 				    th.th_sum = in6_cksum_phdr(&ip6.ip6_src,
1384 					 &ip6.ip6_dst, 0, htonl(IPPROTO_TCP));
1385 #endif /* INET6 */
1386 			   }
1387 			   m_copyback((*m_head),
1388 			    hlen + offsetof(struct tcphdr, th_sum),
1389 				sizeof(th.th_sum), &th.th_sum);
1390 
1391 			   hlen += th.th_off << 2;
1392 		} else {
1393 			   /*
1394 			    * TCP/IP headers are in the first mbuf; we can do
1395 			    * this the easy way.
1396 			    */
1397 			   struct tcphdr *th;
1398 
1399 			   if (v4) {
1400 				    struct ip *ip =
1401 					 (void *)(mtod((*m_head), char *) +
1402 					ETHER_HDR_LEN);
1403 				    th = (void *)(mtod((*m_head), char *) + hlen);
1404 
1405 				    ip->ip_len = 0;
1406 				    th->th_sum = in_cksum_phdr(ip->ip_src.s_addr,
1407 					 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
1408 			   } else {
1409 #if INET6
1410 				    struct ip6_hdr *ip6 =
1411 				    (void *)(mtod((*m_head), char *) +
1412 				    ETHER_HDR_LEN);
1413 				    th = (void *)(mtod((*m_head), char *) + hlen);
1414 
1415 				    ip6->ip6_plen = 0;
1416 				    th->th_sum = in6_cksum_phdr(&ip6->ip6_src,
1417 					 &ip6->ip6_dst, 0, htonl(IPPROTO_TCP));
1418 #endif /* INET6 */
1419 			   }
1420 			hlen += th->th_off << 2;
1421 		}
1422 
1423 	}
1424 
1425 	prod = sc->jme_tx_prod;
1426 	txd = &sc->jme_txring[prod];
1427 
1428 	error = bus_dmamap_load_mbuf(sc->jme_dmatag, sc->jme_txmbufm[prod],
1429 	    *m_head, BUS_DMA_WRITE);
1430 	if (error) {
1431 		if (error == EFBIG) {
1432 			log(LOG_ERR, "%s: Tx packet consumes too many "
1433 			    "DMA segments, dropping...\n",
1434 			    device_xname(sc->jme_dev));
1435 			m_freem(*m_head);
1436 			m_head = NULL;
1437 		}
1438 		return (error);
1439 	}
1440 	/*
1441 	 * Check descriptor overrun. Leave one free descriptor.
1442 	 * Since we always use 64bit address mode for transmitting,
1443 	 * each Tx request requires one more dummy descriptor.
1444 	 */
1445 	nsegs = sc->jme_txmbufm[prod]->dm_nsegs;
1446 #ifdef JMEDEBUG_TX
1447 	printf("jme_encap prod %d nsegs %d jme_tx_cnt %d\n", prod, nsegs, sc->jme_tx_cnt);
1448 #endif
1449 	if (sc->jme_tx_cnt + nsegs + 1 > JME_NBUFS - 1) {
1450 		bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[prod]);
1451 		return (ENOBUFS);
1452 	}
1453 	bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmbufm[prod],
1454 	    0, sc->jme_txmbufm[prod]->dm_mapsize, BUS_DMASYNC_PREWRITE);
1455 
1456 	m = *m_head;
1457 	cflags = 0;
1458 	tso_segsz = 0;
1459 	/* Configure checksum offload and TSO. */
1460 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6)) != 0) {
1461 		tso_segsz = (uint32_t)m->m_pkthdr.segsz << JME_TD_MSS_SHIFT;
1462 		cflags |= JME_TD_TSO;
1463 	} else {
1464 		if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0)
1465 			cflags |= JME_TD_IPCSUM;
1466 		if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_TCPv6)) != 0)
1467 			cflags |= JME_TD_TCPCSUM;
1468 		if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv4|M_CSUM_UDPv6)) != 0)
1469 			cflags |= JME_TD_UDPCSUM;
1470 	}
1471 	/* Configure VLAN. */
1472 	if ((mtag = VLAN_OUTPUT_TAG(&sc->jme_ec, m)) != NULL) {
1473 		cflags |= (VLAN_TAG_VALUE(mtag) & JME_TD_VLAN_MASK);
1474 		cflags |= JME_TD_VLAN_TAG;
1475 	}
1476 
1477 	desc = &sc->jme_txring[prod];
1478 	desc->flags = htole32(cflags);
1479 	desc->buflen = htole32(tso_segsz);
1480 	desc->addr_hi = htole32(m->m_pkthdr.len);
1481 	desc->addr_lo = 0;
1482 	headdsc = prod;
1483 	sc->jme_tx_cnt++;
1484 	JME_DESC_INC(prod, JME_NBUFS);
1485 	for (i = 0; i < nsegs; i++) {
1486 		desc = &sc->jme_txring[prod];
1487 		desc->flags = htole32(JME_TD_OWN | JME_TD_64BIT);
1488 		desc->buflen =
1489 		    htole32(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_len);
1490 		desc->addr_hi = htole32(
1491 		    JME_ADDR_HI(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_addr));
1492 		desc->addr_lo = htole32(
1493 		    JME_ADDR_LO(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_addr));
1494 		bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap,
1495 		    prod * sizeof(struct jme_desc), sizeof(struct jme_desc),
1496 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1497 		sc->jme_txmbuf[prod] = NULL;
1498 		sc->jme_tx_cnt++;
1499 		JME_DESC_INC(prod, JME_NBUFS);
1500 	}
1501 
1502 	/* Update producer index. */
1503 	sc->jme_tx_prod = prod;
1504 #ifdef JMEDEBUG_TX
1505 	printf("jme_encap prod now %d\n", sc->jme_tx_prod);
1506 #endif
1507 	/*
1508 	 * Finally request interrupt and give the first descriptor
1509 	 * owenership to hardware.
1510 	 */
1511 	desc = &sc->jme_txring[headdsc];
1512 	desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1513 	bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap,
1514 	    headdsc * sizeof(struct jme_desc), sizeof(struct jme_desc),
1515 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1516 
1517 	sc->jme_txmbuf[headdsc] = m;
1518 	return (0);
1519 }
1520 
1521 static void
1522 jme_txeof(struct jme_softc *sc)
1523 {
1524 	struct ifnet *ifp;
1525 	struct jme_desc *desc;
1526 	uint32_t status;
1527 	int cons, cons0, nsegs, seg;
1528 
1529 	ifp = &sc->jme_if;
1530 
1531 #ifdef JMEDEBUG_TX
1532 	printf("jme_txeof cons %d prod %d\n",
1533 	    sc->jme_tx_cons, sc->jme_tx_prod);
1534 	printf("jme_txeof JME_TXCSR 0x%x JME_TXDBA_LO 0x%x JME_TXDBA_HI 0x%x "
1535 	    "JME_TXQDC 0x%x JME_TXNDA 0x%x JME_TXMAC 0x%x JME_TXPFC 0x%x "
1536 	    "JME_TXTRHD 0x%x\n",
1537 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR),
1538 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO),
1539 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI),
1540 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXQDC),
1541 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXNDA),
1542 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC),
1543 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC),
1544 	    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD));
1545 	for (cons = sc->jme_tx_cons; cons != sc->jme_tx_prod; ) {
1546 		desc = &sc->jme_txring[cons];
1547 		printf("ring[%d] 0x%x 0x%x 0x%x 0x%x\n", cons,
1548 		    desc->flags, desc->buflen, desc->addr_hi, desc->addr_lo);
1549 		JME_DESC_INC(cons, JME_NBUFS);
1550 	}
1551 #endif
1552 
1553 	cons = sc->jme_tx_cons;
1554 	if (cons == sc->jme_tx_prod)
1555 		return;
1556 
1557 	/*
1558 	 * Go through our Tx list and free mbufs for those
1559 	 * frames which have been transmitted.
1560 	 */
1561 	for (; cons != sc->jme_tx_prod;) {
1562 		bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap,
1563 		    cons * sizeof(struct jme_desc), sizeof(struct jme_desc),
1564 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1565 
1566 		desc = &sc->jme_txring[cons];
1567 		status = le32toh(desc->flags);
1568 #ifdef JMEDEBUG_TX
1569 		printf("jme_txeof %i status 0x%x nsegs %d\n", cons, status,
1570 		    sc->jme_txmbufm[cons]->dm_nsegs);
1571 #endif
1572 		if (status & JME_TD_OWN)
1573 			break;
1574 
1575 		if ((status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) != 0)
1576 			ifp->if_oerrors++;
1577 		else {
1578 			ifp->if_opackets++;
1579 			if ((status & JME_TD_COLLISION) != 0)
1580 				ifp->if_collisions +=
1581 				    le32toh(desc->buflen) &
1582 				    JME_TD_BUF_LEN_MASK;
1583 		}
1584 		/*
1585 		 * Only the first descriptor of multi-descriptor
1586 		 * transmission is updated so driver have to skip entire
1587 		 * chained buffers for the transmiited frame. In other
1588 		 * words, JME_TD_OWN bit is valid only at the first
1589 		 * descriptor of a multi-descriptor transmission.
1590 		 */
1591 		nsegs = sc->jme_txmbufm[cons]->dm_nsegs;
1592 		cons0 = cons;
1593 		JME_DESC_INC(cons, JME_NBUFS);
1594 		for (seg = 1; seg < nsegs + 1; seg++) {
1595 			bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap,
1596 			    cons * sizeof(struct jme_desc),
1597 			    sizeof(struct jme_desc),
1598 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1599 			sc->jme_txring[cons].flags = 0;
1600 			JME_DESC_INC(cons, JME_NBUFS);
1601 		}
1602 		/* Reclaim transferred mbufs. */
1603 		bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmbufm[cons0],
1604 		    0, sc->jme_txmbufm[cons0]->dm_mapsize,
1605 		    BUS_DMASYNC_POSTWRITE);
1606 		bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[cons0]);
1607 
1608 		KASSERT(sc->jme_txmbuf[cons0] != NULL);
1609 		m_freem(sc->jme_txmbuf[cons0]);
1610 		sc->jme_txmbuf[cons0] = NULL;
1611 		sc->jme_tx_cnt -= nsegs + 1;
1612 		KASSERT(sc->jme_tx_cnt >= 0);
1613 		sc->jme_if.if_flags &= ~IFF_OACTIVE;
1614 	}
1615 	sc->jme_tx_cons = cons;
1616 	/* Unarm watchog timer when there is no pending descriptors in queue. */
1617 	if (sc->jme_tx_cnt == 0)
1618 		ifp->if_timer = 0;
1619 #ifdef JMEDEBUG_TX
1620 	printf("jme_txeof jme_tx_cnt %d\n", sc->jme_tx_cnt);
1621 #endif
1622 }
1623 
1624 static void
1625 jme_ifstart(struct ifnet *ifp)
1626 {
1627 	jme_softc_t *sc = ifp->if_softc;
1628 	struct mbuf *mb_head;
1629 	int enq;
1630 
1631 	/*
1632 	 * check if we can free some desc.
1633 	 * Clear TX interrupt status to reset TX coalescing counters.
1634 	 */
1635 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
1636 	     JME_INTR_STATUS, INTR_TXQ_COMP);
1637 	jme_txeof(sc);
1638 
1639 	if ((sc->jme_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
1640 		return;
1641 	for (enq = 0;; enq++) {
1642 nexttx:
1643 		/* Grab a paquet for output */
1644 		IFQ_DEQUEUE(&ifp->if_snd, mb_head);
1645 		if (mb_head == NULL) {
1646 #ifdef JMEDEBUG_TX
1647 			printf("%s: nothing to send\n", __func__);
1648 #endif
1649 			break;
1650 		}
1651 		/* try to add this mbuf to the TX ring */
1652 		if (jme_encap(sc, &mb_head)) {
1653 			if (mb_head == NULL) {
1654 				ifp->if_oerrors++;
1655 				/* packet dropped, try next one */
1656 				goto nexttx;
1657 			}
1658 			/* resource shortage, try again later */
1659 			IF_PREPEND(&ifp->if_snd, mb_head);
1660 			ifp->if_flags |= IFF_OACTIVE;
1661 			break;
1662 		}
1663 #if NBPFILTER > 0
1664 		/* Pass packet to bpf if there is a listener */
1665 		if (ifp->if_bpf)
1666 			bpf_mtap(ifp->if_bpf, mb_head);
1667 #endif
1668 	}
1669 #ifdef JMEDEBUG_TX
1670 	printf("jme_ifstart enq %d\n", enq);
1671 #endif
1672 	if (enq) {
1673 		/*
1674 		 * Set a 5 second timer just in case we don't hear from
1675 		 * the card again.
1676 		 */
1677 		ifp->if_timer = 5;
1678 		/*
1679 		 * Reading TXCSR takes very long time under heavy load
1680 		 * so cache TXCSR value and writes the ORed value with
1681 		 * the kick command to the TXCSR. This saves one register
1682 		 * access cycle.
1683 		 */
1684 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR,
1685 		  sc->jme_txcsr | TXCSR_TX_ENB | TXCSR_TXQ_N_START(TXCSR_TXQ0));
1686 #ifdef JMEDEBUG_TX
1687 		printf("jme_ifstart JME_TXCSR 0x%x JME_TXDBA_LO 0x%x JME_TXDBA_HI 0x%x "
1688 		    "JME_TXQDC 0x%x JME_TXNDA 0x%x JME_TXMAC 0x%x JME_TXPFC 0x%x "
1689 		    "JME_TXTRHD 0x%x\n",
1690 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR),
1691 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO),
1692 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI),
1693 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXQDC),
1694 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXNDA),
1695 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC),
1696 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC),
1697 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD));
1698 #endif
1699 	}
1700 }
1701 
1702 static void
1703 jme_ifwatchdog(struct ifnet *ifp)
1704 {
1705 	jme_softc_t *sc = ifp->if_softc;
1706 
1707 	if ((ifp->if_flags & IFF_RUNNING) == 0)
1708 		return;
1709 	printf("%s: device timeout\n", device_xname(sc->jme_dev));
1710 	ifp->if_oerrors++;
1711 	jme_init(ifp, 0);
1712 }
1713 
1714 static int
1715 jme_mediachange(struct ifnet *ifp)
1716 {
1717 	int error;
1718 	jme_softc_t *sc = ifp->if_softc;
1719 
1720 	if ((error = mii_mediachg(&sc->jme_mii)) == ENXIO)
1721 		error = 0;
1722 	else if (error != 0) {
1723 		aprint_error_dev(sc->jme_dev, "could not set media\n");
1724 		return error;
1725 	}
1726 	return 0;
1727 }
1728 
1729 static void
1730 jme_ticks(void *v)
1731 {
1732 	jme_softc_t *sc = v;
1733 	int s = splnet();
1734 
1735 	/* Tick the MII. */
1736 	mii_tick(&sc->jme_mii);
1737 
1738 	/* every seconds */
1739 	callout_reset(&sc->jme_tick_ch, hz, jme_ticks, sc);
1740 	splx(s);
1741 }
1742 
1743 static void
1744 jme_mac_config(jme_softc_t *sc)
1745 {
1746 	uint32_t ghc, gpreg, rxmac, txmac, txpause;
1747 	struct mii_data *mii = &sc->jme_mii;
1748 
1749 	ghc = 0;
1750 	rxmac = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC);
1751 	rxmac &= ~RXMAC_FC_ENB;
1752 	txmac = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC);
1753 	txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
1754 	txpause = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC);
1755 	txpause &= ~TXPFC_PAUSE_ENB;
1756 
1757 	if (mii->mii_media_active & IFM_FDX) {
1758 		ghc |= GHC_FULL_DUPLEX;
1759 		rxmac &= ~RXMAC_COLL_DET_ENB;
1760 		txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
1761 		    TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
1762 		    TXMAC_FRAME_BURST);
1763 		/* Disable retry transmit timer/retry limit. */
1764 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD,
1765 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD)
1766 		    & ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
1767 	} else {
1768 		rxmac |= RXMAC_COLL_DET_ENB;
1769 		txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
1770 		/* Enable retry transmit timer/retry limit. */
1771 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD,
1772 		    bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD)		    | TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
1773 	}
1774 	/* Reprogram Tx/Rx MACs with resolved speed/duplex. */
1775 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
1776 	case IFM_10_T:
1777 		ghc |= GHC_SPEED_10 | GHC_CLKSRC_10_100;
1778 		break;
1779 	case IFM_100_TX:
1780 		ghc |= GHC_SPEED_100 | GHC_CLKSRC_10_100;
1781 		break;
1782 	case IFM_1000_T:
1783 		ghc |= GHC_SPEED_1000 | GHC_CLKSRC_1000;
1784 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
1785 			txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
1786 		break;
1787 	default:
1788 		break;
1789 	}
1790 	if ((sc->jme_flags & JME_FLAG_GIGA) &&
1791 	    sc->jme_chip_rev == DEVICEREVID_JMC250_A2) {
1792 		/*
1793 		 * Workaround occasional packet loss issue of JMC250 A2
1794 		 * when it runs on half-duplex media.
1795 		 */
1796 #ifdef JMEDEBUG
1797 		printf("JME250 A2 workaround\n");
1798 #endif
1799 		gpreg = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc,
1800 		    JME_GPREG1);
1801 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1802 			gpreg &= ~GPREG1_HDPX_FIX;
1803 		else
1804 			gpreg |= GPREG1_HDPX_FIX;
1805 		bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc,
1806 		    JME_GPREG1, gpreg);
1807 		/* Workaround CRC errors at 100Mbps on JMC250 A2. */
1808 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
1809 			/* Extend interface FIFO depth. */
1810 			jme_mii_write(sc->jme_dev, sc->jme_phyaddr,
1811 			    0x1B, 0x0000);
1812 		} else {
1813 			/* Select default interface FIFO depth. */
1814 			jme_mii_write(sc->jme_dev, sc->jme_phyaddr,
1815 			    0x1B, 0x0004);
1816 		}
1817 	}
1818 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, ghc);
1819 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, rxmac);
1820 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC, txmac);
1821 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC, txpause);
1822 }
1823 
1824 static void
1825 jme_set_filter(jme_softc_t *sc)
1826 {
1827 	struct ifnet *ifp = &sc->jme_if;
1828 	struct ether_multistep step;
1829 	struct ether_multi *enm;
1830 	uint32_t hash[2] = {0, 0};
1831 	int i;
1832 	uint32_t rxcfg;
1833 
1834 	rxcfg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC);
1835 	rxcfg &= ~ (RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
1836 	    RXMAC_ALLMULTI);
1837 	/* Always accept frames destined to our station address. */
1838 	rxcfg |= RXMAC_UNICAST;
1839 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
1840 		rxcfg |= RXMAC_BROADCAST;
1841 	if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
1842 		if ((ifp->if_flags & IFF_PROMISC) != 0)
1843 			rxcfg |= RXMAC_PROMISC;
1844 		if ((ifp->if_flags & IFF_ALLMULTI) != 0)
1845 			rxcfg |= RXMAC_ALLMULTI;
1846 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
1847 		     JME_MAR0, 0xFFFFFFFF);
1848 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
1849 		     JME_MAR1, 0xFFFFFFFF);
1850 		bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac,
1851 		     JME_RXMAC, rxcfg);
1852 		return;
1853 	}
1854 	/*
1855 	 * Set up the multicast address filter by passing all multicast
1856 	 * addresses through a CRC generator, and then using the low-order
1857 	 * 6 bits as an index into the 64 bit multicast hash table.  The
1858 	 * high order bits select the register, while the rest of the bits
1859 	 * select the bit within the register.
1860 	 */
1861 	rxcfg |= RXMAC_MULTICAST;
1862 	memset(hash, 0, sizeof(hash));
1863 
1864 	ETHER_FIRST_MULTI(step, &sc->jme_ec, enm);
1865 	while (enm != NULL) {
1866 #ifdef JEMDBUG
1867 		printf("%s: addrs %s %s\n", __func__,
1868 		   ether_sprintf(enm->enm_addrlo),
1869 		   ether_sprintf(enm->enm_addrhi));
1870 #endif
1871 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
1872 			i = ether_crc32_be(enm->enm_addrlo, 6);
1873 			/* Just want the 6 least significant bits. */
1874 			i &= 0x3f;
1875 			hash[i / 32] |= 1 << (i%32);
1876 		} else {
1877 			hash[0] = hash[1] = 0xffffffff;
1878 			sc->jme_if.if_flags |= IFF_ALLMULTI;
1879 			break;
1880 		}
1881 		ETHER_NEXT_MULTI(step, enm);
1882 	}
1883 #ifdef JMEDEBUG
1884 	printf("%s: hash1 %x has2 %x\n", __func__, hash[0], hash[1]);
1885 #endif
1886 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR0, hash[0]);
1887 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR1, hash[1]);
1888 	bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, rxcfg);
1889 }
1890 
1891 #if 0
1892 static int
1893 jme_multicast_hash(uint8_t *a)
1894 {
1895 	int hash;
1896 
1897 #define DA(addr,bit) (addr[5 - (bit / 8)] & (1 << (bit % 8)))
1898 #define xor8(a,b,c,d,e,f,g,h)						\
1899 	(((a != 0) + (b != 0) + (c != 0) + (d != 0) + 			\
1900 	  (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1)
1901 
1902 	hash  = xor8(DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30),
1903 	    DA(a,36), DA(a,42));
1904 	hash |= xor8(DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31),
1905 	    DA(a,37), DA(a,43)) << 1;
1906 	hash |= xor8(DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32),
1907 	    DA(a,38), DA(a,44)) << 2;
1908 	hash |= xor8(DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33),
1909 	    DA(a,39), DA(a,45)) << 3;
1910 	hash |= xor8(DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34),
1911 	    DA(a,40), DA(a,46)) << 4;
1912 	hash |= xor8(DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35),
1913 	    DA(a,41), DA(a,47)) << 5;
1914 
1915 	return hash;
1916 }
1917 #endif
1918 
1919 static int
1920 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val)
1921 {
1922 	 uint32_t reg;
1923 	 int i;
1924 
1925 	 *val = 0;
1926 	 for (i = JME_EEPROM_TIMEOUT / 10; i > 0; i--) {
1927 		  reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy,
1928 		      JME_SMBCSR);
1929 		  if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE)
1930 			   break;
1931 		  delay(10);
1932 	 }
1933 
1934 	 if (i == 0) {
1935 		  aprint_error_dev(sc->jme_dev, "EEPROM idle timeout!\n");
1936 		  return (ETIMEDOUT);
1937 	 }
1938 
1939 	 reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK;
1940 	 bus_space_write_4(sc->jme_bt_phy, sc->jme_bh_phy,
1941 	     JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER);
1942 	 for (i = JME_EEPROM_TIMEOUT / 10; i > 0; i--) {
1943 		  delay(10);
1944 		  reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy,
1945 		      JME_SMBINTF);
1946 		  if ((reg & SMBINTF_CMD_TRIGGER) == 0)
1947 			   break;
1948 	 }
1949 
1950 	 if (i == 0) {
1951 		  aprint_error_dev(sc->jme_dev, "EEPROM read timeout!\n");
1952 		  return (ETIMEDOUT);
1953 	 }
1954 
1955 	 reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, JME_SMBINTF);
1956 	 *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT;
1957 	 return (0);
1958 }
1959 
1960 
1961 static int
1962 jme_eeprom_macaddr(struct jme_softc *sc)
1963 {
1964 	uint8_t eaddr[ETHER_ADDR_LEN];
1965 	uint8_t fup, reg, val;
1966 	uint32_t offset;
1967 	int match;
1968 
1969 	offset = 0;
1970 	if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
1971 	    fup != JME_EEPROM_SIG0)
1972 		return (ENOENT);
1973 	if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
1974 	    fup != JME_EEPROM_SIG1)
1975 		return (ENOENT);
1976 	match = 0;
1977 	do {
1978 		if (jme_eeprom_read_byte(sc, offset, &fup) != 0)
1979 			break;
1980 		if (JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, JME_EEPROM_PAGE_BAR1)
1981 		    == (fup & (JME_EEPROM_FUNC_MASK|JME_EEPROM_PAGE_MASK))) {
1982 			if (jme_eeprom_read_byte(sc, offset + 1, &reg) != 0)
1983 				break;
1984 			if (reg >= JME_PAR0 &&
1985 			    reg < JME_PAR0 + ETHER_ADDR_LEN) {
1986 				if (jme_eeprom_read_byte(sc, offset + 2,
1987 				    &val) != 0)
1988 					break;
1989 				eaddr[reg - JME_PAR0] = val;
1990 				match++;
1991 			}
1992 		}
1993 		if (fup & JME_EEPROM_DESC_END)
1994 			break;
1995 
1996 		/* Try next eeprom descriptor. */
1997 		offset += JME_EEPROM_DESC_BYTES;
1998 	} while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END);
1999 
2000 	if (match == ETHER_ADDR_LEN) {
2001 		memcpy(sc->jme_enaddr, eaddr, ETHER_ADDR_LEN);
2002 		return (0);
2003 	}
2004 
2005 	return (ENOENT);
2006 }
2007 
2008 /*
2009  * Set up sysctl(3) MIB, hw.jme.* - Individual controllers will be
2010  * set up in jme_pci_attach()
2011  */
2012 SYSCTL_SETUP(sysctl_jme, "sysctl jme subtree setup")
2013 {
2014 	int rc;
2015 	const struct sysctlnode *node;
2016 
2017 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
2018 	    0, CTLTYPE_NODE, "hw", NULL,
2019 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
2020 		goto err;
2021 	}
2022 
2023 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
2024 	    0, CTLTYPE_NODE, "jme",
2025 	    SYSCTL_DESCR("jme interface controls"),
2026 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
2027 		goto err;
2028 	}
2029 
2030 	jme_root_num = node->sysctl_num;
2031 	return;
2032 
2033 err:
2034 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
2035 }
2036 
2037 static int
2038 jme_sysctl_intrxto(SYSCTLFN_ARGS)
2039 {
2040 	int error, t;
2041 	struct sysctlnode node;
2042 	struct jme_softc *sc;
2043 	uint32_t reg;
2044 
2045 	node = *rnode;
2046 	sc = node.sysctl_data;
2047 	t = sc->jme_intrxto;
2048 	node.sysctl_data = &t;
2049 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2050 	if (error || newp == NULL)
2051 		return error;
2052 
2053 	if (t < PCCRX_COAL_TO_MIN || t > PCCRX_COAL_TO_MAX)
2054 		return EINVAL;
2055 
2056 	/*
2057 	 * update the softc with sysctl-changed value, and mark
2058 	 * for hardware update
2059 	 */
2060 	sc->jme_intrxto = t;
2061 	/* Configure Rx queue 0 packet completion coalescing. */
2062 	reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK;
2063 	reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK;
2064 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg);
2065 	return 0;
2066 }
2067 
2068 static int
2069 jme_sysctl_intrxct(SYSCTLFN_ARGS)
2070 {
2071 	int error, t;
2072 	struct sysctlnode node;
2073 	struct jme_softc *sc;
2074 	uint32_t reg;
2075 
2076 	node = *rnode;
2077 	sc = node.sysctl_data;
2078 	t = sc->jme_intrxct;
2079 	node.sysctl_data = &t;
2080 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2081 	if (error || newp == NULL)
2082 		return error;
2083 
2084 	if (t < PCCRX_COAL_PKT_MIN || t > PCCRX_COAL_PKT_MAX)
2085 		return EINVAL;
2086 
2087 	/*
2088 	 * update the softc with sysctl-changed value, and mark
2089 	 * for hardware update
2090 	 */
2091 	sc->jme_intrxct = t;
2092 	/* Configure Rx queue 0 packet completion coalescing. */
2093 	reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK;
2094 	reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK;
2095 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg);
2096 	return 0;
2097 }
2098 
2099 static int
2100 jme_sysctl_inttxto(SYSCTLFN_ARGS)
2101 {
2102 	int error, t;
2103 	struct sysctlnode node;
2104 	struct jme_softc *sc;
2105 	uint32_t reg;
2106 
2107 	node = *rnode;
2108 	sc = node.sysctl_data;
2109 	t = sc->jme_inttxto;
2110 	node.sysctl_data = &t;
2111 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2112 	if (error || newp == NULL)
2113 		return error;
2114 
2115 	if (t < PCCTX_COAL_TO_MIN || t > PCCTX_COAL_TO_MAX)
2116 		return EINVAL;
2117 
2118 	/*
2119 	 * update the softc with sysctl-changed value, and mark
2120 	 * for hardware update
2121 	 */
2122 	sc->jme_inttxto = t;
2123 	/* Configure Tx queue 0 packet completion coalescing. */
2124 	reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK;
2125 	reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK;
2126 	reg |= PCCTX_COAL_TXQ0;
2127 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg);
2128 	return 0;
2129 }
2130 
2131 static int
2132 jme_sysctl_inttxct(SYSCTLFN_ARGS)
2133 {
2134 	int error, t;
2135 	struct sysctlnode node;
2136 	struct jme_softc *sc;
2137 	uint32_t reg;
2138 
2139 	node = *rnode;
2140 	sc = node.sysctl_data;
2141 	t = sc->jme_inttxct;
2142 	node.sysctl_data = &t;
2143 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2144 	if (error || newp == NULL)
2145 		return error;
2146 
2147 	if (t < PCCTX_COAL_PKT_MIN || t > PCCTX_COAL_PKT_MAX)
2148 		return EINVAL;
2149 
2150 	/*
2151 	 * update the softc with sysctl-changed value, and mark
2152 	 * for hardware update
2153 	 */
2154 	sc->jme_inttxct = t;
2155 	/* Configure Tx queue 0 packet completion coalescing. */
2156 	reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK;
2157 	reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK;
2158 	reg |= PCCTX_COAL_TXQ0;
2159 	bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg);
2160 	return 0;
2161 }
2162