xref: /netbsd-src/sys/dev/pci/if_sk.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: if_sk.c,v 1.59 2009/03/18 16:00:19 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*	$OpenBSD: if_sk.c,v 1.116 2006/06/22 23:06:03 brad Exp $	*/
30 
31 /*
32  * Copyright (c) 1997, 1998, 1999, 2000
33  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *	This product includes software developed by Bill Paul.
46  * 4. Neither the name of the author nor the names of any co-contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
60  * THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  * $FreeBSD: /c/ncvs/src/sys/pci/if_sk.c,v 1.20 2000/04/22 02:16:37 wpaul Exp $
63  */
64 
65 /*
66  * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
67  *
68  * Permission to use, copy, modify, and distribute this software for any
69  * purpose with or without fee is hereby granted, provided that the above
70  * copyright notice and this permission notice appear in all copies.
71  *
72  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
73  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
74  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
75  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
76  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
77  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
78  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
79  */
80 
81 /*
82  * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports
83  * the SK-984x series adapters, both single port and dual port.
84  * References:
85  * 	The XaQti XMAC II datasheet,
86  * http://www.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
87  *	The SysKonnect GEnesis manual, http://www.syskonnect.com
88  *
89  * Note: XaQti has been acquired by Vitesse, and Vitesse does not have the
90  * XMAC II datasheet online. I have put my copy at people.freebsd.org as a
91  * convenience to others until Vitesse corrects this problem:
92  *
93  * http://people.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
94  *
95  * Written by Bill Paul <wpaul@ee.columbia.edu>
96  * Department of Electrical Engineering
97  * Columbia University, New York City
98  */
99 
100 /*
101  * The SysKonnect gigabit ethernet adapters consist of two main
102  * components: the SysKonnect GEnesis controller chip and the XaQti Corp.
103  * XMAC II gigabit ethernet MAC. The XMAC provides all of the MAC
104  * components and a PHY while the GEnesis controller provides a PCI
105  * interface with DMA support. Each card may have between 512K and
106  * 2MB of SRAM on board depending on the configuration.
107  *
108  * The SysKonnect GEnesis controller can have either one or two XMAC
109  * chips connected to it, allowing single or dual port NIC configurations.
110  * SysKonnect has the distinction of being the only vendor on the market
111  * with a dual port gigabit ethernet NIC. The GEnesis provides dual FIFOs,
112  * dual DMA queues, packet/MAC/transmit arbiters and direct access to the
113  * XMAC registers. This driver takes advantage of these features to allow
114  * both XMACs to operate as independent interfaces.
115  */
116 
117 #include <sys/cdefs.h>
118 __KERNEL_RCSID(0, "$NetBSD: if_sk.c,v 1.59 2009/03/18 16:00:19 cegger Exp $");
119 
120 #include "bpfilter.h"
121 #include "rnd.h"
122 
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/sockio.h>
126 #include <sys/mbuf.h>
127 #include <sys/malloc.h>
128 #include <sys/mutex.h>
129 #include <sys/kernel.h>
130 #include <sys/socket.h>
131 #include <sys/device.h>
132 #include <sys/queue.h>
133 #include <sys/callout.h>
134 #include <sys/sysctl.h>
135 #include <sys/endian.h>
136 
137 #include <net/if.h>
138 #include <net/if_dl.h>
139 #include <net/if_types.h>
140 
141 #include <net/if_media.h>
142 
143 #if NBPFILTER > 0
144 #include <net/bpf.h>
145 #endif
146 #if NRND > 0
147 #include <sys/rnd.h>
148 #endif
149 
150 #include <dev/mii/mii.h>
151 #include <dev/mii/miivar.h>
152 #include <dev/mii/brgphyreg.h>
153 
154 #include <dev/pci/pcireg.h>
155 #include <dev/pci/pcivar.h>
156 #include <dev/pci/pcidevs.h>
157 
158 /* #define SK_USEIOSPACE */
159 
160 #include <dev/pci/if_skreg.h>
161 #include <dev/pci/if_skvar.h>
162 
163 int skc_probe(device_t, cfdata_t, void *);
164 void skc_attach(device_t, device_t, void *aux);
165 int sk_probe(device_t, cfdata_t, void *);
166 void sk_attach(device_t, device_t, void *aux);
167 int skcprint(void *, const char *);
168 int sk_intr(void *);
169 void sk_intr_bcom(struct sk_if_softc *);
170 void sk_intr_xmac(struct sk_if_softc *);
171 void sk_intr_yukon(struct sk_if_softc *);
172 void sk_rxeof(struct sk_if_softc *);
173 void sk_txeof(struct sk_if_softc *);
174 int sk_encap(struct sk_if_softc *, struct mbuf *, u_int32_t *);
175 void sk_start(struct ifnet *);
176 int sk_ioctl(struct ifnet *, u_long, void *);
177 int sk_init(struct ifnet *);
178 void sk_init_xmac(struct sk_if_softc *);
179 void sk_init_yukon(struct sk_if_softc *);
180 void sk_stop(struct ifnet *, int);
181 void sk_watchdog(struct ifnet *);
182 void sk_shutdown(void *);
183 int sk_ifmedia_upd(struct ifnet *);
184 void sk_reset(struct sk_softc *);
185 int sk_newbuf(struct sk_if_softc *, int, struct mbuf *, bus_dmamap_t);
186 int sk_alloc_jumbo_mem(struct sk_if_softc *);
187 void sk_free_jumbo_mem(struct sk_if_softc *);
188 void *sk_jalloc(struct sk_if_softc *);
189 void sk_jfree(struct mbuf *, void *, size_t, void *);
190 int sk_init_rx_ring(struct sk_if_softc *);
191 int sk_init_tx_ring(struct sk_if_softc *);
192 u_int8_t sk_vpd_readbyte(struct sk_softc *, int);
193 void sk_vpd_read_res(struct sk_softc *,
194 					struct vpd_res *, int);
195 void sk_vpd_read(struct sk_softc *);
196 
197 void sk_update_int_mod(struct sk_softc *);
198 
199 int sk_xmac_miibus_readreg(device_t, int, int);
200 void sk_xmac_miibus_writereg(device_t, int, int, int);
201 void sk_xmac_miibus_statchg(device_t);
202 
203 int sk_marv_miibus_readreg(device_t, int, int);
204 void sk_marv_miibus_writereg(device_t, int, int, int);
205 void sk_marv_miibus_statchg(device_t);
206 
207 u_int32_t sk_xmac_hash(void *);
208 u_int32_t sk_yukon_hash(void *);
209 void sk_setfilt(struct sk_if_softc *, void *, int);
210 void sk_setmulti(struct sk_if_softc *);
211 void sk_tick(void *);
212 
213 /* #define SK_DEBUG 2 */
214 #ifdef SK_DEBUG
215 #define DPRINTF(x)	if (skdebug) printf x
216 #define DPRINTFN(n,x)	if (skdebug >= (n)) printf x
217 int	skdebug = SK_DEBUG;
218 
219 void sk_dump_txdesc(struct sk_tx_desc *, int);
220 void sk_dump_mbuf(struct mbuf *);
221 void sk_dump_bytes(const char *, int);
222 #else
223 #define DPRINTF(x)
224 #define DPRINTFN(n,x)
225 #endif
226 
227 static int sk_sysctl_handler(SYSCTLFN_PROTO);
228 static int sk_root_num;
229 
230 /* supported device vendors */
231 /* PCI_PRODUCT_DLINK_DGE560T_2 might belong in if_msk instead */
232 static const struct sk_product {
233 	pci_vendor_id_t		sk_vendor;
234 	pci_product_id_t	sk_product;
235 } sk_products[] = {
236 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C940, },
237 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE530T, },
238 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE560T_2, },
239 	{ PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1064, },
240 	{ PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE, },
241 	{ PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2, },
242 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_SKNET, },
243 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_BELKIN, },
244 	{ 0, 0, }
245 };
246 
247 #define SK_LINKSYS_EG1032_SUBID	0x00151737
248 
249 static inline u_int32_t
250 sk_win_read_4(struct sk_softc *sc, u_int32_t reg)
251 {
252 #ifdef SK_USEIOSPACE
253 	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
254 	return CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg));
255 #else
256 	return CSR_READ_4(sc, reg);
257 #endif
258 }
259 
260 static inline u_int16_t
261 sk_win_read_2(struct sk_softc *sc, u_int32_t reg)
262 {
263 #ifdef SK_USEIOSPACE
264 	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
265 	return CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg));
266 #else
267 	return CSR_READ_2(sc, reg);
268 #endif
269 }
270 
271 static inline u_int8_t
272 sk_win_read_1(struct sk_softc *sc, u_int32_t reg)
273 {
274 #ifdef SK_USEIOSPACE
275 	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
276 	return CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg));
277 #else
278 	return CSR_READ_1(sc, reg);
279 #endif
280 }
281 
282 static inline void
283 sk_win_write_4(struct sk_softc *sc, u_int32_t reg, u_int32_t x)
284 {
285 #ifdef SK_USEIOSPACE
286 	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
287 	CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), x);
288 #else
289 	CSR_WRITE_4(sc, reg, x);
290 #endif
291 }
292 
293 static inline void
294 sk_win_write_2(struct sk_softc *sc, u_int32_t reg, u_int16_t x)
295 {
296 #ifdef SK_USEIOSPACE
297 	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
298 	CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), x);
299 #else
300 	CSR_WRITE_2(sc, reg, x);
301 #endif
302 }
303 
304 static inline void
305 sk_win_write_1(struct sk_softc *sc, u_int32_t reg, u_int8_t x)
306 {
307 #ifdef SK_USEIOSPACE
308 	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
309 	CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), x);
310 #else
311 	CSR_WRITE_1(sc, reg, x);
312 #endif
313 }
314 
315 /*
316  * The VPD EEPROM contains Vital Product Data, as suggested in
317  * the PCI 2.1 specification. The VPD data is separared into areas
318  * denoted by resource IDs. The SysKonnect VPD contains an ID string
319  * resource (the name of the adapter), a read-only area resource
320  * containing various key/data fields and a read/write area which
321  * can be used to store asset management information or log messages.
322  * We read the ID string and read-only into buffers attached to
323  * the controller softc structure for later use. At the moment,
324  * we only use the ID string during sk_attach().
325  */
326 u_int8_t
327 sk_vpd_readbyte(struct sk_softc *sc, int addr)
328 {
329 	int			i;
330 
331 	sk_win_write_2(sc, SK_PCI_REG(SK_PCI_VPD_ADDR), addr);
332 	for (i = 0; i < SK_TIMEOUT; i++) {
333 		DELAY(1);
334 		if (sk_win_read_2(sc,
335 		    SK_PCI_REG(SK_PCI_VPD_ADDR)) & SK_VPD_FLAG)
336 			break;
337 	}
338 
339 	if (i == SK_TIMEOUT)
340 		return 0;
341 
342 	return sk_win_read_1(sc, SK_PCI_REG(SK_PCI_VPD_DATA));
343 }
344 
345 void
346 sk_vpd_read_res(struct sk_softc *sc, struct vpd_res *res, int addr)
347 {
348 	int			i;
349 	u_int8_t		*ptr;
350 
351 	ptr = (u_int8_t *)res;
352 	for (i = 0; i < sizeof(struct vpd_res); i++)
353 		ptr[i] = sk_vpd_readbyte(sc, i + addr);
354 }
355 
356 void
357 sk_vpd_read(struct sk_softc *sc)
358 {
359 	int			pos = 0, i;
360 	struct vpd_res		res;
361 
362 	if (sc->sk_vpd_prodname != NULL)
363 		free(sc->sk_vpd_prodname, M_DEVBUF);
364 	if (sc->sk_vpd_readonly != NULL)
365 		free(sc->sk_vpd_readonly, M_DEVBUF);
366 	sc->sk_vpd_prodname = NULL;
367 	sc->sk_vpd_readonly = NULL;
368 
369 	sk_vpd_read_res(sc, &res, pos);
370 
371 	if (res.vr_id != VPD_RES_ID) {
372 		aprint_error_dev(sc->sk_dev,
373 		    "bad VPD resource id: expected %x got %x\n",
374 		    VPD_RES_ID, res.vr_id);
375 		return;
376 	}
377 
378 	pos += sizeof(res);
379 	sc->sk_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
380 	if (sc->sk_vpd_prodname == NULL)
381 		panic("sk_vpd_read");
382 	for (i = 0; i < res.vr_len; i++)
383 		sc->sk_vpd_prodname[i] = sk_vpd_readbyte(sc, i + pos);
384 	sc->sk_vpd_prodname[i] = '\0';
385 	pos += i;
386 
387 	sk_vpd_read_res(sc, &res, pos);
388 
389 	if (res.vr_id != VPD_RES_READ) {
390 		aprint_error_dev(sc->sk_dev,
391 		    "bad VPD resource id: expected %x got %x\n",
392 		    VPD_RES_READ, res.vr_id);
393 		return;
394 	}
395 
396 	pos += sizeof(res);
397 	sc->sk_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
398 	if (sc->sk_vpd_readonly == NULL)
399 		panic("sk_vpd_read");
400 	for (i = 0; i < res.vr_len ; i++)
401 		sc->sk_vpd_readonly[i] = sk_vpd_readbyte(sc, i + pos);
402 }
403 
404 int
405 sk_xmac_miibus_readreg(device_t dev, int phy, int reg)
406 {
407 	struct sk_if_softc *sc_if = device_private(dev);
408 	int i;
409 
410 	DPRINTFN(9, ("sk_xmac_miibus_readreg\n"));
411 
412 	if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0)
413 		return 0;
414 
415 	SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
416 	SK_XM_READ_2(sc_if, XM_PHY_DATA);
417 	if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
418 		for (i = 0; i < SK_TIMEOUT; i++) {
419 			DELAY(1);
420 			if (SK_XM_READ_2(sc_if, XM_MMUCMD) &
421 			    XM_MMUCMD_PHYDATARDY)
422 				break;
423 		}
424 
425 		if (i == SK_TIMEOUT) {
426 			aprint_error_dev(sc_if->sk_dev,
427 			    "phy failed to come ready\n");
428 			return 0;
429 		}
430 	}
431 	DELAY(1);
432 	return SK_XM_READ_2(sc_if, XM_PHY_DATA);
433 }
434 
435 void
436 sk_xmac_miibus_writereg(device_t dev, int phy, int reg, int val)
437 {
438 	struct sk_if_softc *sc_if = device_private(dev);
439 	int i;
440 
441 	DPRINTFN(9, ("sk_xmac_miibus_writereg\n"));
442 
443 	SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
444 	for (i = 0; i < SK_TIMEOUT; i++) {
445 		if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
446 			break;
447 	}
448 
449 	if (i == SK_TIMEOUT) {
450 		aprint_error_dev(sc_if->sk_dev, "phy failed to come ready\n");
451 		return;
452 	}
453 
454 	SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val);
455 	for (i = 0; i < SK_TIMEOUT; i++) {
456 		DELAY(1);
457 		if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
458 			break;
459 	}
460 
461 	if (i == SK_TIMEOUT)
462 		aprint_error_dev(sc_if->sk_dev, "phy write timed out\n");
463 }
464 
465 void
466 sk_xmac_miibus_statchg(device_t dev)
467 {
468 	struct sk_if_softc *sc_if = device_private(dev);
469 	struct mii_data *mii = &sc_if->sk_mii;
470 
471 	DPRINTFN(9, ("sk_xmac_miibus_statchg\n"));
472 
473 	/*
474 	 * If this is a GMII PHY, manually set the XMAC's
475 	 * duplex mode accordingly.
476 	 */
477 	if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
478 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
479 			SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
480 		else
481 			SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
482 	}
483 }
484 
485 int
486 sk_marv_miibus_readreg(device_t dev, int phy, int reg)
487 {
488 	struct sk_if_softc *sc_if = device_private(dev);
489 	u_int16_t val;
490 	int i;
491 
492 	if (phy != 0 ||
493 	    (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER &&
494 	     sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) {
495 		DPRINTFN(9, ("sk_marv_miibus_readreg (skip) phy=%d, reg=%#x\n",
496 			     phy, reg));
497 		return 0;
498 	}
499 
500         SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
501 		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
502 
503 	for (i = 0; i < SK_TIMEOUT; i++) {
504 		DELAY(1);
505 		val = SK_YU_READ_2(sc_if, YUKON_SMICR);
506 		if (val & YU_SMICR_READ_VALID)
507 			break;
508 	}
509 
510 	if (i == SK_TIMEOUT) {
511 		aprint_error_dev(sc_if->sk_dev, "phy failed to come ready\n");
512 		return 0;
513 	}
514 
515  	DPRINTFN(9, ("sk_marv_miibus_readreg: i=%d, timeout=%d\n", i,
516 		     SK_TIMEOUT));
517 
518         val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
519 
520 	DPRINTFN(9, ("sk_marv_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
521 		     phy, reg, val));
522 
523 	return val;
524 }
525 
526 void
527 sk_marv_miibus_writereg(device_t dev, int phy, int reg, int val)
528 {
529 	struct sk_if_softc *sc_if = device_private(dev);
530 	int i;
531 
532 	DPRINTFN(9, ("sk_marv_miibus_writereg phy=%d reg=%#x val=%#x\n",
533 		     phy, reg, val));
534 
535 	SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
536 	SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
537 		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
538 
539 	for (i = 0; i < SK_TIMEOUT; i++) {
540 		DELAY(1);
541 		if (!(SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY))
542 			break;
543 	}
544 
545 	if (i == SK_TIMEOUT)
546 		printf("%s: phy write timed out\n",
547 		    device_xname(sc_if->sk_dev));
548 }
549 
550 void
551 sk_marv_miibus_statchg(device_t dev)
552 {
553 	DPRINTFN(9, ("sk_marv_miibus_statchg: gpcr=%x\n",
554 		     SK_YU_READ_2(((struct sk_if_softc *)device_private(dev)),
555 		     YUKON_GPCR)));
556 }
557 
558 #define SK_HASH_BITS		6
559 
560 u_int32_t
561 sk_xmac_hash(void *addr)
562 {
563 	u_int32_t		crc;
564 
565 	crc = ether_crc32_le(addr,ETHER_ADDR_LEN);
566 	crc = ~crc & ((1<< SK_HASH_BITS) - 1);
567 	DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
568 	return crc;
569 }
570 
571 u_int32_t
572 sk_yukon_hash(void *addr)
573 {
574 	u_int32_t		crc;
575 
576 	crc = ether_crc32_be(addr,ETHER_ADDR_LEN);
577 	crc &= ((1 << SK_HASH_BITS) - 1);
578 	DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
579 	return crc;
580 }
581 
582 void
583 sk_setfilt(struct sk_if_softc *sc_if, void *addrv, int slot)
584 {
585 	char *addr = addrv;
586 	int base = XM_RXFILT_ENTRY(slot);
587 
588 	SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
589 	SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
590 	SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
591 }
592 
593 void
594 sk_setmulti(struct sk_if_softc *sc_if)
595 {
596 	struct sk_softc *sc = sc_if->sk_softc;
597 	struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
598 	u_int32_t hashes[2] = { 0, 0 };
599 	int h = 0, i;
600 	struct ethercom *ec = &sc_if->sk_ethercom;
601 	struct ether_multi *enm;
602 	struct ether_multistep step;
603 	u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 };
604 
605 	/* First, zot all the existing filters. */
606 	switch (sc->sk_type) {
607 	case SK_GENESIS:
608 		for (i = 1; i < XM_RXFILT_MAX; i++)
609 			sk_setfilt(sc_if, (void *)&dummy, i);
610 
611 		SK_XM_WRITE_4(sc_if, XM_MAR0, 0);
612 		SK_XM_WRITE_4(sc_if, XM_MAR2, 0);
613 		break;
614 	case SK_YUKON:
615 	case SK_YUKON_LITE:
616 	case SK_YUKON_LP:
617 		SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
618 		SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
619 		SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
620 		SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
621 		break;
622 	}
623 
624 	/* Now program new ones. */
625 allmulti:
626 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
627 		hashes[0] = 0xFFFFFFFF;
628 		hashes[1] = 0xFFFFFFFF;
629 	} else {
630 		i = 1;
631 		/* First find the tail of the list. */
632 		ETHER_FIRST_MULTI(step, ec, enm);
633 		while (enm != NULL) {
634 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
635 				 ETHER_ADDR_LEN)) {
636 				ifp->if_flags |= IFF_ALLMULTI;
637 				goto allmulti;
638 			}
639 			DPRINTFN(2,("multicast address %s\n",
640 	    			ether_sprintf(enm->enm_addrlo)));
641 			/*
642 			 * Program the first XM_RXFILT_MAX multicast groups
643 			 * into the perfect filter. For all others,
644 			 * use the hash table.
645 			 */
646 			if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) {
647 				sk_setfilt(sc_if, enm->enm_addrlo, i);
648 				i++;
649 			}
650 			else {
651 				switch (sc->sk_type) {
652 				case SK_GENESIS:
653 					h = sk_xmac_hash(enm->enm_addrlo);
654 					break;
655 				case SK_YUKON:
656 				case SK_YUKON_LITE:
657 				case SK_YUKON_LP:
658 					h = sk_yukon_hash(enm->enm_addrlo);
659 					break;
660 				}
661 				if (h < 32)
662 					hashes[0] |= (1 << h);
663 				else
664 					hashes[1] |= (1 << (h - 32));
665 			}
666 
667 			ETHER_NEXT_MULTI(step, enm);
668 		}
669 	}
670 
671 	switch (sc->sk_type) {
672 	case SK_GENESIS:
673 		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH|
674 			       XM_MODE_RX_USE_PERFECT);
675 		SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]);
676 		SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]);
677 		break;
678 	case SK_YUKON:
679 	case SK_YUKON_LITE:
680 	case SK_YUKON_LP:
681 		SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
682 		SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
683 		SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
684 		SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
685 		break;
686 	}
687 }
688 
689 int
690 sk_init_rx_ring(struct sk_if_softc *sc_if)
691 {
692 	struct sk_chain_data	*cd = &sc_if->sk_cdata;
693 	struct sk_ring_data	*rd = sc_if->sk_rdata;
694 	int			i;
695 
696 	memset((char *)rd->sk_rx_ring, 0,
697 	    sizeof(struct sk_rx_desc) * SK_RX_RING_CNT);
698 
699 	for (i = 0; i < SK_RX_RING_CNT; i++) {
700 		cd->sk_rx_chain[i].sk_desc = &rd->sk_rx_ring[i];
701 		if (i == (SK_RX_RING_CNT - 1)) {
702 			cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[0];
703 			rd->sk_rx_ring[i].sk_next =
704 				htole32(SK_RX_RING_ADDR(sc_if, 0));
705 		} else {
706 			cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[i + 1];
707 			rd->sk_rx_ring[i].sk_next =
708 				htole32(SK_RX_RING_ADDR(sc_if,i+1));
709 		}
710 	}
711 
712 	for (i = 0; i < SK_RX_RING_CNT; i++) {
713 		if (sk_newbuf(sc_if, i, NULL,
714 		    sc_if->sk_cdata.sk_rx_jumbo_map) == ENOBUFS) {
715 			aprint_error_dev(sc_if->sk_dev,
716 			    "failed alloc of %dth mbuf\n", i);
717 			return ENOBUFS;
718 		}
719 	}
720 	sc_if->sk_cdata.sk_rx_prod = 0;
721 	sc_if->sk_cdata.sk_rx_cons = 0;
722 
723 	return 0;
724 }
725 
726 int
727 sk_init_tx_ring(struct sk_if_softc *sc_if)
728 {
729 	struct sk_chain_data	*cd = &sc_if->sk_cdata;
730 	struct sk_ring_data	*rd = sc_if->sk_rdata;
731 	int			i;
732 
733 	memset(sc_if->sk_rdata->sk_tx_ring, 0,
734 	    sizeof(struct sk_tx_desc) * SK_TX_RING_CNT);
735 
736 	for (i = 0; i < SK_TX_RING_CNT; i++) {
737 		cd->sk_tx_chain[i].sk_desc = &rd->sk_tx_ring[i];
738 		if (i == (SK_TX_RING_CNT - 1)) {
739 			cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[0];
740 			rd->sk_tx_ring[i].sk_next =
741 				htole32(SK_TX_RING_ADDR(sc_if, 0));
742 		} else {
743 			cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[i + 1];
744 			rd->sk_tx_ring[i].sk_next =
745 				htole32(SK_TX_RING_ADDR(sc_if,i+1));
746 		}
747 	}
748 
749 	sc_if->sk_cdata.sk_tx_prod = 0;
750 	sc_if->sk_cdata.sk_tx_cons = 0;
751 	sc_if->sk_cdata.sk_tx_cnt = 0;
752 
753 	SK_CDTXSYNC(sc_if, 0, SK_TX_RING_CNT,
754 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
755 
756 	return 0;
757 }
758 
759 int
760 sk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
761 	  bus_dmamap_t dmamap)
762 {
763 	struct mbuf		*m_new = NULL;
764 	struct sk_chain		*c;
765 	struct sk_rx_desc	*r;
766 
767 	if (m == NULL) {
768 		void *buf = NULL;
769 
770 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
771 		if (m_new == NULL) {
772 			aprint_error_dev(sc_if->sk_dev,
773 			    "no memory for rx list -- packet dropped!\n");
774 			return ENOBUFS;
775 		}
776 
777 		/* Allocate the jumbo buffer */
778 		buf = sk_jalloc(sc_if);
779 		if (buf == NULL) {
780 			m_freem(m_new);
781 			DPRINTFN(1, ("%s jumbo allocation failed -- packet "
782 			    "dropped!\n", sc_if->sk_ethercom.ec_if.if_xname));
783 			return ENOBUFS;
784 		}
785 
786 		/* Attach the buffer to the mbuf */
787 		m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
788 		MEXTADD(m_new, buf, SK_JLEN, 0, sk_jfree, sc_if);
789 
790 	} else {
791 		/*
792 	 	 * We're re-using a previously allocated mbuf;
793 		 * be sure to re-init pointers and lengths to
794 		 * default values.
795 		 */
796 		m_new = m;
797 		m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
798 		m_new->m_data = m_new->m_ext.ext_buf;
799 	}
800 	m_adj(m_new, ETHER_ALIGN);
801 
802 	c = &sc_if->sk_cdata.sk_rx_chain[i];
803 	r = c->sk_desc;
804 	c->sk_mbuf = m_new;
805 	r->sk_data_lo = htole32(dmamap->dm_segs[0].ds_addr +
806 	    (((vaddr_t)m_new->m_data
807 		- (vaddr_t)sc_if->sk_cdata.sk_jumbo_buf)));
808 	r->sk_ctl = htole32(SK_JLEN | SK_RXSTAT);
809 
810 	SK_CDRXSYNC(sc_if, i, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
811 
812 	return 0;
813 }
814 
815 /*
816  * Memory management for jumbo frames.
817  */
818 
819 int
820 sk_alloc_jumbo_mem(struct sk_if_softc *sc_if)
821 {
822 	struct sk_softc		*sc = sc_if->sk_softc;
823 	char *ptr, *kva;
824 	bus_dma_segment_t	seg;
825 	int		i, rseg, state, error;
826 	struct sk_jpool_entry   *entry;
827 
828 	state = error = 0;
829 
830 	/* Grab a big chunk o' storage. */
831 	if (bus_dmamem_alloc(sc->sc_dmatag, SK_JMEM, PAGE_SIZE, 0,
832 			     &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
833 		aprint_error_dev(sc->sk_dev, "can't alloc rx buffers\n");
834 		return ENOBUFS;
835 	}
836 
837 	state = 1;
838 	if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, SK_JMEM, (void **)&kva,
839 			   BUS_DMA_NOWAIT)) {
840 		aprint_error_dev(sc->sk_dev,
841 		    "can't map dma buffers (%d bytes)\n",
842 		    SK_JMEM);
843 		error = ENOBUFS;
844 		goto out;
845 	}
846 
847 	state = 2;
848 	if (bus_dmamap_create(sc->sc_dmatag, SK_JMEM, 1, SK_JMEM, 0,
849 	    BUS_DMA_NOWAIT, &sc_if->sk_cdata.sk_rx_jumbo_map)) {
850 		aprint_error_dev(sc->sk_dev, "can't create dma map\n");
851 		error = ENOBUFS;
852 		goto out;
853 	}
854 
855 	state = 3;
856 	if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map,
857 			    kva, SK_JMEM, NULL, BUS_DMA_NOWAIT)) {
858 		aprint_error_dev(sc->sk_dev, "can't load dma map\n");
859 		error = ENOBUFS;
860 		goto out;
861 	}
862 
863 	state = 4;
864 	sc_if->sk_cdata.sk_jumbo_buf = (void *)kva;
865 	DPRINTFN(1,("sk_jumbo_buf = 0x%p\n", sc_if->sk_cdata.sk_jumbo_buf));
866 
867 	LIST_INIT(&sc_if->sk_jfree_listhead);
868 	LIST_INIT(&sc_if->sk_jinuse_listhead);
869 	mutex_init(&sc_if->sk_jpool_mtx, MUTEX_DEFAULT, IPL_NET);
870 
871 	/*
872 	 * Now divide it up into 9K pieces and save the addresses
873 	 * in an array.
874 	 */
875 	ptr = sc_if->sk_cdata.sk_jumbo_buf;
876 	for (i = 0; i < SK_JSLOTS; i++) {
877 		sc_if->sk_cdata.sk_jslots[i] = ptr;
878 		ptr += SK_JLEN;
879 		entry = malloc(sizeof(struct sk_jpool_entry),
880 		    M_DEVBUF, M_NOWAIT);
881 		if (entry == NULL) {
882 			aprint_error_dev(sc->sk_dev,
883 			    "no memory for jumbo buffer queue!\n");
884 			error = ENOBUFS;
885 			goto out;
886 		}
887 		entry->slot = i;
888 		if (i)
889 			LIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
890 				 entry, jpool_entries);
891 		else
892 			LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead,
893 				 entry, jpool_entries);
894 	}
895 out:
896 	if (error != 0) {
897 		switch (state) {
898 		case 4:
899 			bus_dmamap_unload(sc->sc_dmatag,
900 			    sc_if->sk_cdata.sk_rx_jumbo_map);
901 		case 3:
902 			bus_dmamap_destroy(sc->sc_dmatag,
903 			    sc_if->sk_cdata.sk_rx_jumbo_map);
904 		case 2:
905 			bus_dmamem_unmap(sc->sc_dmatag, kva, SK_JMEM);
906 		case 1:
907 			bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
908 			break;
909 		default:
910 			break;
911 		}
912 	}
913 
914 	return error;
915 }
916 
917 /*
918  * Allocate a jumbo buffer.
919  */
920 void *
921 sk_jalloc(struct sk_if_softc *sc_if)
922 {
923 	struct sk_jpool_entry   *entry;
924 
925 	mutex_enter(&sc_if->sk_jpool_mtx);
926 	entry = LIST_FIRST(&sc_if->sk_jfree_listhead);
927 
928 	if (entry == NULL) {
929 		mutex_exit(&sc_if->sk_jpool_mtx);
930 		return NULL;
931 	}
932 
933 	LIST_REMOVE(entry, jpool_entries);
934 	LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
935 	mutex_exit(&sc_if->sk_jpool_mtx);
936 	return sc_if->sk_cdata.sk_jslots[entry->slot];
937 }
938 
939 /*
940  * Release a jumbo buffer.
941  */
942 void
943 sk_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
944 {
945 	struct sk_jpool_entry *entry;
946 	struct sk_if_softc *sc;
947 	int i;
948 
949 	/* Extract the softc struct pointer. */
950 	sc = (struct sk_if_softc *)arg;
951 
952 	if (sc == NULL)
953 		panic("sk_jfree: can't find softc pointer!");
954 
955 	/* calculate the slot this buffer belongs to */
956 
957 	i = ((vaddr_t)buf
958 	     - (vaddr_t)sc->sk_cdata.sk_jumbo_buf) / SK_JLEN;
959 
960 	if ((i < 0) || (i >= SK_JSLOTS))
961 		panic("sk_jfree: asked to free buffer that we don't manage!");
962 
963 	mutex_enter(&sc->sk_jpool_mtx);
964 	entry = LIST_FIRST(&sc->sk_jinuse_listhead);
965 	if (entry == NULL)
966 		panic("sk_jfree: buffer not in use!");
967 	entry->slot = i;
968 	LIST_REMOVE(entry, jpool_entries);
969 	LIST_INSERT_HEAD(&sc->sk_jfree_listhead, entry, jpool_entries);
970 	mutex_exit(&sc->sk_jpool_mtx);
971 
972 	if (__predict_true(m != NULL))
973 		pool_cache_put(mb_cache, m);
974 }
975 
976 /*
977  * Set media options.
978  */
979 int
980 sk_ifmedia_upd(struct ifnet *ifp)
981 {
982 	struct sk_if_softc *sc_if = ifp->if_softc;
983 	int rc;
984 
985 	(void) sk_init(ifp);
986 	if ((rc = mii_mediachg(&sc_if->sk_mii)) == ENXIO)
987 		return 0;
988 	return rc;
989 }
990 
991 int
992 sk_ioctl(struct ifnet *ifp, u_long command, void *data)
993 {
994 	struct sk_if_softc *sc_if = ifp->if_softc;
995 	struct sk_softc *sc = sc_if->sk_softc;
996 	int s, error = 0;
997 
998 	/* DPRINTFN(2, ("sk_ioctl\n")); */
999 
1000 	s = splnet();
1001 
1002 	switch (command) {
1003 
1004 	case SIOCSIFFLAGS:
1005 	        DPRINTFN(2, ("sk_ioctl IFFLAGS\n"));
1006 		if ((error = ifioctl_common(ifp, command, data)) != 0)
1007 			break;
1008 		if (ifp->if_flags & IFF_UP) {
1009 			if (ifp->if_flags & IFF_RUNNING &&
1010 			    ifp->if_flags & IFF_PROMISC &&
1011 			    !(sc_if->sk_if_flags & IFF_PROMISC)) {
1012 				switch (sc->sk_type) {
1013 				case SK_GENESIS:
1014 					SK_XM_SETBIT_4(sc_if, XM_MODE,
1015 					    XM_MODE_RX_PROMISC);
1016 					break;
1017 				case SK_YUKON:
1018 				case SK_YUKON_LITE:
1019 				case SK_YUKON_LP:
1020 					SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
1021 					    YU_RCR_UFLEN | YU_RCR_MUFLEN);
1022 					break;
1023 				}
1024 				sk_setmulti(sc_if);
1025 			} else if (ifp->if_flags & IFF_RUNNING &&
1026 			    !(ifp->if_flags & IFF_PROMISC) &&
1027 			    sc_if->sk_if_flags & IFF_PROMISC) {
1028 				switch (sc->sk_type) {
1029 				case SK_GENESIS:
1030 					SK_XM_CLRBIT_4(sc_if, XM_MODE,
1031 					    XM_MODE_RX_PROMISC);
1032 					break;
1033 				case SK_YUKON:
1034 				case SK_YUKON_LITE:
1035 				case SK_YUKON_LP:
1036 					SK_YU_SETBIT_2(sc_if, YUKON_RCR,
1037 					    YU_RCR_UFLEN | YU_RCR_MUFLEN);
1038 					break;
1039 				}
1040 
1041 				sk_setmulti(sc_if);
1042 			} else
1043 				(void) sk_init(ifp);
1044 		} else {
1045 			if (ifp->if_flags & IFF_RUNNING)
1046 				sk_stop(ifp,0);
1047 		}
1048 		sc_if->sk_if_flags = ifp->if_flags;
1049 		error = 0;
1050 		break;
1051 
1052 	default:
1053 	        DPRINTFN(2, ("sk_ioctl ETHER\n"));
1054 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
1055 			break;
1056 
1057 		error = 0;
1058 
1059 		if (command != SIOCADDMULTI && command != SIOCDELMULTI)
1060 			;
1061 		else if (ifp->if_flags & IFF_RUNNING) {
1062 			sk_setmulti(sc_if);
1063 			DPRINTFN(2, ("sk_ioctl setmulti called\n"));
1064 		}
1065 		break;
1066 	}
1067 
1068 	splx(s);
1069 	return error;
1070 }
1071 
1072 void
1073 sk_update_int_mod(struct sk_softc *sc)
1074 {
1075 	u_int32_t imtimer_ticks;
1076 
1077 	/*
1078          * Configure interrupt moderation. The moderation timer
1079 	 * defers interrupts specified in the interrupt moderation
1080 	 * timer mask based on the timeout specified in the interrupt
1081 	 * moderation timer init register. Each bit in the timer
1082 	 * register represents one tick, so to specify a timeout in
1083 	 * microseconds, we have to multiply by the correct number of
1084 	 * ticks-per-microsecond.
1085 	 */
1086 	switch (sc->sk_type) {
1087 	case SK_GENESIS:
1088 		imtimer_ticks = SK_IMTIMER_TICKS_GENESIS;
1089 		break;
1090 	case SK_YUKON_EC:
1091 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
1092 		break;
1093 	default:
1094 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
1095 	}
1096 	aprint_verbose_dev(sc->sk_dev, "interrupt moderation is %d us\n",
1097 	    sc->sk_int_mod);
1098         sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod));
1099         sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
1100 	    SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
1101         sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
1102 	sc->sk_int_mod_pending = 0;
1103 }
1104 
1105 /*
1106  * Lookup: Check the PCI vendor and device, and return a pointer to
1107  * The structure if the IDs match against our list.
1108  */
1109 
1110 static const struct sk_product *
1111 sk_lookup(const struct pci_attach_args *pa)
1112 {
1113 	const struct sk_product *psk;
1114 
1115 	for ( psk = &sk_products[0]; psk->sk_vendor != 0; psk++ ) {
1116 		if (PCI_VENDOR(pa->pa_id) == psk->sk_vendor &&
1117 		    PCI_PRODUCT(pa->pa_id) == psk->sk_product)
1118 			return psk;
1119 	}
1120 	return NULL;
1121 }
1122 
1123 /*
1124  * Probe for a SysKonnect GEnesis chip.
1125  */
1126 
1127 int
1128 skc_probe(device_t parent, cfdata_t match, void *aux)
1129 {
1130 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1131 	const struct sk_product *psk;
1132 	pcireg_t subid;
1133 
1134 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
1135 
1136 	/* special-case Linksys EG1032, since rev 3 uses re(4) */
1137 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
1138 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032 &&
1139 	    subid == SK_LINKSYS_EG1032_SUBID)
1140 		return 1;
1141 
1142 	if ((psk = sk_lookup(pa))) {
1143 		return 1;
1144 	}
1145 	return 0;
1146 }
1147 
1148 /*
1149  * Force the GEnesis into reset, then bring it out of reset.
1150  */
1151 void sk_reset(struct sk_softc *sc)
1152 {
1153 	DPRINTFN(2, ("sk_reset\n"));
1154 
1155 	CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET);
1156 	CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET);
1157 	if (SK_YUKON_FAMILY(sc->sk_type))
1158 		CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
1159 
1160 	DELAY(1000);
1161 	CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET);
1162 	DELAY(2);
1163 	CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
1164 	if (SK_YUKON_FAMILY(sc->sk_type))
1165 		CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
1166 
1167 	DPRINTFN(2, ("sk_reset: sk_csr=%x\n", CSR_READ_2(sc, SK_CSR)));
1168 	DPRINTFN(2, ("sk_reset: sk_link_ctrl=%x\n",
1169 		     CSR_READ_2(sc, SK_LINK_CTRL)));
1170 
1171 	if (sc->sk_type == SK_GENESIS) {
1172 		/* Configure packet arbiter */
1173 		sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET);
1174 		sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT);
1175 		sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT);
1176 		sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT);
1177 		sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT);
1178 	}
1179 
1180 	/* Enable RAM interface */
1181 	sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
1182 
1183 	sk_update_int_mod(sc);
1184 }
1185 
1186 int
1187 sk_probe(device_t parent, cfdata_t match, void *aux)
1188 {
1189 	struct skc_attach_args *sa = aux;
1190 
1191 	if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
1192 		return 0;
1193 
1194 	return 1;
1195 }
1196 
1197 /*
1198  * Each XMAC chip is attached as a separate logical IP interface.
1199  * Single port cards will have only one logical interface of course.
1200  */
1201 void
1202 sk_attach(device_t parent, device_t self, void *aux)
1203 {
1204 	struct sk_if_softc *sc_if = device_private(self);
1205 	struct sk_softc *sc = device_private(parent);
1206 	struct skc_attach_args *sa = aux;
1207 	struct sk_txmap_entry	*entry;
1208 	struct ifnet *ifp;
1209 	bus_dma_segment_t seg;
1210 	bus_dmamap_t dmamap;
1211 	void *kva;
1212 	int i, rseg;
1213 	int mii_flags = 0;
1214 
1215 	aprint_naive("\n");
1216 
1217 	sc_if->sk_dev = self;
1218 	sc_if->sk_port = sa->skc_port;
1219 	sc_if->sk_softc = sc;
1220 	sc->sk_if[sa->skc_port] = sc_if;
1221 
1222 	if (sa->skc_port == SK_PORT_A)
1223 		sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0;
1224 	if (sa->skc_port == SK_PORT_B)
1225 		sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1;
1226 
1227 	DPRINTFN(2, ("begin sk_attach: port=%d\n", sc_if->sk_port));
1228 
1229 	/*
1230 	 * Get station address for this interface. Note that
1231 	 * dual port cards actually come with three station
1232 	 * addresses: one for each port, plus an extra. The
1233 	 * extra one is used by the SysKonnect driver software
1234 	 * as a 'virtual' station address for when both ports
1235 	 * are operating in failover mode. Currently we don't
1236 	 * use this extra address.
1237 	 */
1238 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1239 		sc_if->sk_enaddr[i] =
1240 			sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
1241 
1242 
1243 	aprint_normal(": Ethernet address %s\n",
1244 	    ether_sprintf(sc_if->sk_enaddr));
1245 
1246 	/*
1247 	 * Set up RAM buffer addresses. The NIC will have a certain
1248 	 * amount of SRAM on it, somewhere between 512K and 2MB. We
1249 	 * need to divide this up a) between the transmitter and
1250  	 * receiver and b) between the two XMACs, if this is a
1251 	 * dual port NIC. Our algorithm is to divide up the memory
1252 	 * evenly so that everyone gets a fair share.
1253 	 */
1254 	if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) {
1255 		u_int32_t		chunk, val;
1256 
1257 		chunk = sc->sk_ramsize / 2;
1258 		val = sc->sk_rboff / sizeof(u_int64_t);
1259 		sc_if->sk_rx_ramstart = val;
1260 		val += (chunk / sizeof(u_int64_t));
1261 		sc_if->sk_rx_ramend = val - 1;
1262 		sc_if->sk_tx_ramstart = val;
1263 		val += (chunk / sizeof(u_int64_t));
1264 		sc_if->sk_tx_ramend = val - 1;
1265 	} else {
1266 		u_int32_t		chunk, val;
1267 
1268 		chunk = sc->sk_ramsize / 4;
1269 		val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) /
1270 		    sizeof(u_int64_t);
1271 		sc_if->sk_rx_ramstart = val;
1272 		val += (chunk / sizeof(u_int64_t));
1273 		sc_if->sk_rx_ramend = val - 1;
1274 		sc_if->sk_tx_ramstart = val;
1275 		val += (chunk / sizeof(u_int64_t));
1276 		sc_if->sk_tx_ramend = val - 1;
1277 	}
1278 
1279 	DPRINTFN(2, ("sk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
1280 		     "           tx_ramstart=%#x tx_ramend=%#x\n",
1281 		     sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
1282 		     sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
1283 
1284 	/* Read and save PHY type and set PHY address */
1285 	sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF;
1286 	switch (sc_if->sk_phytype) {
1287 	case SK_PHYTYPE_XMAC:
1288 		sc_if->sk_phyaddr = SK_PHYADDR_XMAC;
1289 		break;
1290 	case SK_PHYTYPE_BCOM:
1291 		sc_if->sk_phyaddr = SK_PHYADDR_BCOM;
1292 		break;
1293 	case SK_PHYTYPE_MARV_COPPER:
1294 		sc_if->sk_phyaddr = SK_PHYADDR_MARV;
1295 		break;
1296 	default:
1297 		aprint_error_dev(sc->sk_dev, "unsupported PHY type: %d\n",
1298 		    sc_if->sk_phytype);
1299 		return;
1300 	}
1301 
1302 	/* Allocate the descriptor queues. */
1303 	if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct sk_ring_data),
1304 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1305 		aprint_error_dev(sc->sk_dev, "can't alloc rx buffers\n");
1306 		goto fail;
1307 	}
1308 	if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
1309 	    sizeof(struct sk_ring_data), &kva, BUS_DMA_NOWAIT)) {
1310 		aprint_error_dev(sc_if->sk_dev,
1311 		    "can't map dma buffers (%lu bytes)\n",
1312 		    (u_long) sizeof(struct sk_ring_data));
1313 		bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1314 		goto fail;
1315 	}
1316 	if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct sk_ring_data), 1,
1317 	    sizeof(struct sk_ring_data), 0, BUS_DMA_NOWAIT,
1318             &sc_if->sk_ring_map)) {
1319 		aprint_error_dev(sc_if->sk_dev, "can't create dma map\n");
1320 		bus_dmamem_unmap(sc->sc_dmatag, kva,
1321 		    sizeof(struct sk_ring_data));
1322 		bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1323 		goto fail;
1324 	}
1325 	if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
1326 	    sizeof(struct sk_ring_data), NULL, BUS_DMA_NOWAIT)) {
1327 		aprint_error_dev(sc_if->sk_dev, "can't load dma map\n");
1328 		bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1329 		bus_dmamem_unmap(sc->sc_dmatag, kva,
1330 		    sizeof(struct sk_ring_data));
1331 		bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1332 		goto fail;
1333 	}
1334 
1335 	for (i = 0; i < SK_RX_RING_CNT; i++)
1336 		sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
1337 
1338 	SIMPLEQ_INIT(&sc_if->sk_txmap_head);
1339 	for (i = 0; i < SK_TX_RING_CNT; i++) {
1340 		sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
1341 
1342 		if (bus_dmamap_create(sc->sc_dmatag, SK_JLEN, SK_NTXSEG,
1343 		    SK_JLEN, 0, BUS_DMA_NOWAIT, &dmamap)) {
1344 			aprint_error_dev(sc_if->sk_dev,
1345 			    "Can't create TX dmamap\n");
1346 			bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_ring_map);
1347 			bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1348 			bus_dmamem_unmap(sc->sc_dmatag, kva,
1349 			    sizeof(struct sk_ring_data));
1350 			bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1351 			goto fail;
1352 		}
1353 
1354 		entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
1355 		if (!entry) {
1356 			aprint_error_dev(sc_if->sk_dev,
1357 			    "Can't alloc txmap entry\n");
1358 			bus_dmamap_destroy(sc->sc_dmatag, dmamap);
1359 			bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_ring_map);
1360 			bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
1361 			bus_dmamem_unmap(sc->sc_dmatag, kva,
1362 			    sizeof(struct sk_ring_data));
1363 			bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
1364 			goto fail;
1365 		}
1366 		entry->dmamap = dmamap;
1367 		SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
1368 	}
1369 
1370         sc_if->sk_rdata = (struct sk_ring_data *)kva;
1371 	memset(sc_if->sk_rdata, 0, sizeof(struct sk_ring_data));
1372 
1373 	ifp = &sc_if->sk_ethercom.ec_if;
1374 	/* Try to allocate memory for jumbo buffers. */
1375 	if (sk_alloc_jumbo_mem(sc_if)) {
1376 		aprint_error("%s: jumbo buffer allocation failed\n", ifp->if_xname);
1377 		goto fail;
1378 	}
1379 	sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU
1380 		| ETHERCAP_JUMBO_MTU;
1381 
1382 	ifp->if_softc = sc_if;
1383 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1384 	ifp->if_ioctl = sk_ioctl;
1385 	ifp->if_start = sk_start;
1386 	ifp->if_stop = sk_stop;
1387 	ifp->if_init = sk_init;
1388 	ifp->if_watchdog = sk_watchdog;
1389 	ifp->if_capabilities = 0;
1390 	IFQ_SET_MAXLEN(&ifp->if_snd, SK_TX_RING_CNT - 1);
1391 	IFQ_SET_READY(&ifp->if_snd);
1392 	strlcpy(ifp->if_xname, device_xname(sc_if->sk_dev), IFNAMSIZ);
1393 
1394 	/*
1395 	 * Do miibus setup.
1396 	 */
1397 	switch (sc->sk_type) {
1398 	case SK_GENESIS:
1399 		sk_init_xmac(sc_if);
1400 		break;
1401 	case SK_YUKON:
1402 	case SK_YUKON_LITE:
1403 	case SK_YUKON_LP:
1404 		sk_init_yukon(sc_if);
1405 		break;
1406 	default:
1407 		aprint_error_dev(sc->sk_dev, "unknown device type %d\n",
1408 			sc->sk_type);
1409 		goto fail;
1410 	}
1411 
1412  	DPRINTFN(2, ("sk_attach: 1\n"));
1413 
1414 	sc_if->sk_mii.mii_ifp = ifp;
1415 	switch (sc->sk_type) {
1416 	case SK_GENESIS:
1417 		sc_if->sk_mii.mii_readreg = sk_xmac_miibus_readreg;
1418 		sc_if->sk_mii.mii_writereg = sk_xmac_miibus_writereg;
1419 		sc_if->sk_mii.mii_statchg = sk_xmac_miibus_statchg;
1420 		break;
1421 	case SK_YUKON:
1422 	case SK_YUKON_LITE:
1423 	case SK_YUKON_LP:
1424 		sc_if->sk_mii.mii_readreg = sk_marv_miibus_readreg;
1425 		sc_if->sk_mii.mii_writereg = sk_marv_miibus_writereg;
1426 		sc_if->sk_mii.mii_statchg = sk_marv_miibus_statchg;
1427 		mii_flags = MIIF_DOPAUSE;
1428 		break;
1429 	}
1430 
1431 	sc_if->sk_ethercom.ec_mii = &sc_if->sk_mii;
1432 	ifmedia_init(&sc_if->sk_mii.mii_media, 0,
1433 	    sk_ifmedia_upd, ether_mediastatus);
1434 	mii_attach(self, &sc_if->sk_mii, 0xffffffff, MII_PHY_ANY,
1435 	    MII_OFFSET_ANY, mii_flags);
1436 	if (LIST_EMPTY(&sc_if->sk_mii.mii_phys)) {
1437 		aprint_error_dev(sc_if->sk_dev, "no PHY found!\n");
1438 		ifmedia_add(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL,
1439 			    0, NULL);
1440 		ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL);
1441 	} else
1442 		ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_AUTO);
1443 
1444 	callout_init(&sc_if->sk_tick_ch, 0);
1445 	callout_reset(&sc_if->sk_tick_ch,hz,sk_tick,sc_if);
1446 
1447 	DPRINTFN(2, ("sk_attach: 1\n"));
1448 
1449 	/*
1450 	 * Call MI attach routines.
1451 	 */
1452 	if_attach(ifp);
1453 
1454 	ether_ifattach(ifp, sc_if->sk_enaddr);
1455 
1456 #if NRND > 0
1457         rnd_attach_source(&sc->rnd_source, device_xname(sc->sk_dev),
1458             RND_TYPE_NET, 0);
1459 #endif
1460 
1461 	DPRINTFN(2, ("sk_attach: end\n"));
1462 
1463 	return;
1464 
1465 fail:
1466 	sc->sk_if[sa->skc_port] = NULL;
1467 }
1468 
1469 int
1470 skcprint(void *aux, const char *pnp)
1471 {
1472 	struct skc_attach_args *sa = aux;
1473 
1474 	if (pnp)
1475 		aprint_normal("sk port %c at %s",
1476 		    (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
1477 	else
1478 		aprint_normal(" port %c",
1479 		    (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
1480 	return UNCONF;
1481 }
1482 
1483 /*
1484  * Attach the interface. Allocate softc structures, do ifmedia
1485  * setup and ethernet/BPF attach.
1486  */
1487 void
1488 skc_attach(device_t parent, device_t self, void *aux)
1489 {
1490 	struct sk_softc *sc = device_private(self);
1491 	struct pci_attach_args *pa = aux;
1492 	struct skc_attach_args skca;
1493 	pci_chipset_tag_t pc = pa->pa_pc;
1494 #ifndef SK_USEIOSPACE
1495 	pcireg_t memtype;
1496 #endif
1497 	pci_intr_handle_t ih;
1498 	const char *intrstr = NULL;
1499 	bus_addr_t iobase;
1500 	bus_size_t iosize;
1501 	int rc, sk_nodenum;
1502 	u_int32_t command;
1503 	const char *revstr;
1504 	const struct sysctlnode *node;
1505 
1506 	sc->sk_dev = self;
1507 	aprint_naive("\n");
1508 
1509 	DPRINTFN(2, ("begin skc_attach\n"));
1510 
1511 	/*
1512 	 * Handle power management nonsense.
1513 	 */
1514 	command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
1515 
1516 	if (command == 0x01) {
1517 		command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
1518 		if (command & SK_PSTATE_MASK) {
1519 			u_int32_t		xiobase, membase, irq;
1520 
1521 			/* Save important PCI config data. */
1522 			xiobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
1523 			membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
1524 			irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
1525 
1526 			/* Reset the power state. */
1527 			aprint_normal_dev(sc->sk_dev,
1528 			    "chip is in D%d power mode -- setting to D0\n",
1529 			    command & SK_PSTATE_MASK);
1530 			command &= 0xFFFFFFFC;
1531 			pci_conf_write(pc, pa->pa_tag,
1532 			    SK_PCI_PWRMGMTCTRL, command);
1533 
1534 			/* Restore PCI config data. */
1535 			pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, xiobase);
1536 			pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
1537 			pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
1538 		}
1539 	}
1540 
1541 	/*
1542 	 * Map control/status registers.
1543 	 */
1544 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1545 	command |= PCI_COMMAND_IO_ENABLE |
1546 	    PCI_COMMAND_MEM_ENABLE |
1547 	    PCI_COMMAND_MASTER_ENABLE;
1548 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1549 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1550 
1551 #ifdef SK_USEIOSPACE
1552 	if (!(command & PCI_COMMAND_IO_ENABLE)) {
1553 		aprint_error(": failed to enable I/O ports!\n");
1554 		return;
1555 	}
1556 	/*
1557 	 * Map control/status registers.
1558 	 */
1559 	if (pci_mapreg_map(pa, SK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
1560 			&sc->sk_btag, &sc->sk_bhandle,
1561 			&iobase, &iosize)) {
1562 		aprint_error(": can't find i/o space\n");
1563 		return;
1564 	}
1565 #else
1566 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
1567 		aprint_error(": failed to enable memory mapping!\n");
1568 		return;
1569 	}
1570 	memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
1571 	switch (memtype) {
1572         case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1573         case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1574                 if (pci_mapreg_map(pa, SK_PCI_LOMEM,
1575 				   memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
1576 				   &iobase, &iosize) == 0)
1577                         break;
1578         default:
1579                 aprint_error_dev(sc->sk_dev, "can't find mem space\n");
1580                 return;
1581 	}
1582 
1583 	DPRINTFN(2, ("skc_attach: iobase=%lx, iosize=%lx\n", iobase,
1584 	    (u_long)iosize));
1585 #endif
1586 	sc->sc_dmatag = pa->pa_dmat;
1587 
1588 	sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
1589 	sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4);
1590 
1591 	/* bail out here if chip is not recognized */
1592 	if ( sc->sk_type != SK_GENESIS && ! SK_YUKON_FAMILY(sc->sk_type)) {
1593 		aprint_error_dev(sc->sk_dev, "unknown chip type\n");
1594 		goto fail;
1595 	}
1596 	if (SK_IS_YUKON2(sc)) {
1597 		aprint_error_dev(sc->sk_dev,
1598 		    "Does not support Yukon2--try msk(4).\n");
1599 		goto fail;
1600 	}
1601 	DPRINTFN(2, ("skc_attach: allocate interrupt\n"));
1602 
1603 	/* Allocate interrupt */
1604 	if (pci_intr_map(pa, &ih)) {
1605 		aprint_error(": couldn't map interrupt\n");
1606 		goto fail;
1607 	}
1608 
1609 	intrstr = pci_intr_string(pc, ih);
1610 	sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, sk_intr, sc);
1611 	if (sc->sk_intrhand == NULL) {
1612 		aprint_error(": couldn't establish interrupt");
1613 		if (intrstr != NULL)
1614 			aprint_normal(" at %s", intrstr);
1615 		goto fail;
1616 	}
1617 	aprint_normal(": %s\n", intrstr);
1618 
1619 	/* Reset the adapter. */
1620 	sk_reset(sc);
1621 
1622 	/* Read and save vital product data from EEPROM. */
1623 	sk_vpd_read(sc);
1624 
1625 	if (sc->sk_type == SK_GENESIS) {
1626 		u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1627 		/* Read and save RAM size and RAMbuffer offset */
1628 		switch (val) {
1629 		case SK_RAMSIZE_512K_64:
1630 			sc->sk_ramsize = 0x80000;
1631 			sc->sk_rboff = SK_RBOFF_0;
1632 			break;
1633 		case SK_RAMSIZE_1024K_64:
1634 			sc->sk_ramsize = 0x100000;
1635 			sc->sk_rboff = SK_RBOFF_80000;
1636 			break;
1637 		case SK_RAMSIZE_1024K_128:
1638 			sc->sk_ramsize = 0x100000;
1639 			sc->sk_rboff = SK_RBOFF_0;
1640 			break;
1641 		case SK_RAMSIZE_2048K_128:
1642 			sc->sk_ramsize = 0x200000;
1643 			sc->sk_rboff = SK_RBOFF_0;
1644 			break;
1645 		default:
1646 			aprint_error_dev(sc->sk_dev, "unknown ram size: %d\n",
1647 			       val);
1648 			goto fail_1;
1649 			break;
1650 		}
1651 
1652 		DPRINTFN(2, ("skc_attach: ramsize=%d(%dk), rboff=%d\n",
1653 			     sc->sk_ramsize, sc->sk_ramsize / 1024,
1654 			     sc->sk_rboff));
1655 	} else {
1656 	  	u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
1657 		sc->sk_ramsize =  ( val == 0 ) ?  0x20000 : (( val * 4 )*1024);
1658 		sc->sk_rboff = SK_RBOFF_0;
1659 
1660 		DPRINTFN(2, ("skc_attach: ramsize=%dk (%d), rboff=%d\n",
1661 			     sc->sk_ramsize / 1024, sc->sk_ramsize,
1662 			     sc->sk_rboff));
1663 	}
1664 
1665 	/* Read and save physical media type */
1666 	switch (sk_win_read_1(sc, SK_PMDTYPE)) {
1667 	case SK_PMD_1000BASESX:
1668 		sc->sk_pmd = IFM_1000_SX;
1669 		break;
1670 	case SK_PMD_1000BASELX:
1671 		sc->sk_pmd = IFM_1000_LX;
1672 		break;
1673 	case SK_PMD_1000BASECX:
1674 		sc->sk_pmd = IFM_1000_CX;
1675 		break;
1676 	case SK_PMD_1000BASETX:
1677 	case SK_PMD_1000BASETX_ALT:
1678 		sc->sk_pmd = IFM_1000_T;
1679 		break;
1680 	default:
1681 		aprint_error_dev(sc->sk_dev, "unknown media type: 0x%x\n",
1682 		    sk_win_read_1(sc, SK_PMDTYPE));
1683 		goto fail_1;
1684 	}
1685 
1686 	/* determine whether to name it with vpd or just make it up */
1687 	/* Marvell Yukon VPD's can freqently be bogus */
1688 
1689 	switch (pa->pa_id) {
1690 	case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
1691 			 PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE):
1692 	case PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2:
1693 	case PCI_PRODUCT_3COM_3C940:
1694 	case PCI_PRODUCT_DLINK_DGE530T:
1695 	case PCI_PRODUCT_DLINK_DGE560T:
1696 	case PCI_PRODUCT_DLINK_DGE560T_2:
1697 	case PCI_PRODUCT_LINKSYS_EG1032:
1698 	case PCI_PRODUCT_LINKSYS_EG1064:
1699 	case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
1700 			 PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2):
1701 	case PCI_ID_CODE(PCI_VENDOR_3COM,PCI_PRODUCT_3COM_3C940):
1702 	case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE530T):
1703 	case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE560T):
1704 	case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE560T_2):
1705 	case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1032):
1706 	case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1064):
1707  		sc->sk_name = sc->sk_vpd_prodname;
1708  		break;
1709 	case PCI_ID_CODE(PCI_VENDOR_MARVELL,PCI_PRODUCT_MARVELL_SKNET):
1710 	/* whoops yukon vpd prodname bears no resemblance to reality */
1711 		switch (sc->sk_type) {
1712 		case SK_GENESIS:
1713 			sc->sk_name = sc->sk_vpd_prodname;
1714 			break;
1715 		case SK_YUKON:
1716 			sc->sk_name = "Marvell Yukon Gigabit Ethernet";
1717 			break;
1718 		case SK_YUKON_LITE:
1719 			sc->sk_name = "Marvell Yukon Lite Gigabit Ethernet";
1720 			break;
1721 		case SK_YUKON_LP:
1722 			sc->sk_name = "Marvell Yukon LP Gigabit Ethernet";
1723 			break;
1724 		default:
1725 			sc->sk_name = "Marvell Yukon (Unknown) Gigabit Ethernet";
1726 		}
1727 
1728 	/* Yukon Lite Rev A0 needs special test, from sk98lin driver */
1729 
1730 		if ( sc->sk_type == SK_YUKON ) {
1731 			uint32_t flashaddr;
1732 			uint8_t testbyte;
1733 
1734 			flashaddr = sk_win_read_4(sc,SK_EP_ADDR);
1735 
1736 			/* test Flash-Address Register */
1737 			sk_win_write_1(sc,SK_EP_ADDR+3, 0xff);
1738 			testbyte = sk_win_read_1(sc, SK_EP_ADDR+3);
1739 
1740 			if (testbyte != 0) {
1741 				/* this is yukon lite Rev. A0 */
1742 				sc->sk_type = SK_YUKON_LITE;
1743 				sc->sk_rev = SK_YUKON_LITE_REV_A0;
1744 				/* restore Flash-Address Register */
1745 				sk_win_write_4(sc,SK_EP_ADDR,flashaddr);
1746 			}
1747 		}
1748 		break;
1749 	case PCI_ID_CODE(PCI_VENDOR_MARVELL,PCI_PRODUCT_MARVELL_BELKIN):
1750 		sc->sk_name = sc->sk_vpd_prodname;
1751 		break;
1752  	default:
1753 		sc->sk_name = "Unknown Marvell";
1754 	}
1755 
1756 
1757 	if ( sc->sk_type == SK_YUKON_LITE ) {
1758 		switch (sc->sk_rev) {
1759 		case SK_YUKON_LITE_REV_A0:
1760 			revstr = "A0";
1761 			break;
1762 		case SK_YUKON_LITE_REV_A1:
1763 			revstr = "A1";
1764 			break;
1765 		case SK_YUKON_LITE_REV_A3:
1766 			revstr = "A3";
1767 			break;
1768 		default:
1769 			revstr = "";
1770 		}
1771 	} else {
1772 		revstr = "";
1773 	}
1774 
1775 	/* Announce the product name. */
1776 	aprint_normal_dev(sc->sk_dev, "%s rev. %s(0x%x)\n",
1777 			      sc->sk_name, revstr, sc->sk_rev);
1778 
1779 	skca.skc_port = SK_PORT_A;
1780 	(void)config_found(sc->sk_dev, &skca, skcprint);
1781 
1782 	if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
1783 		skca.skc_port = SK_PORT_B;
1784 		(void)config_found(sc->sk_dev, &skca, skcprint);
1785 	}
1786 
1787 	/* Turn on the 'driver is loaded' LED. */
1788 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1789 
1790 	/* skc sysctl setup */
1791 
1792 	sc->sk_int_mod = SK_IM_DEFAULT;
1793 	sc->sk_int_mod_pending = 0;
1794 
1795 	if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1796 	    0, CTLTYPE_NODE, device_xname(sc->sk_dev),
1797 	    SYSCTL_DESCR("skc per-controller controls"),
1798 	    NULL, 0, NULL, 0, CTL_HW, sk_root_num, CTL_CREATE,
1799 	    CTL_EOL)) != 0) {
1800 		aprint_normal_dev(sc->sk_dev, "couldn't create sysctl node\n");
1801 		goto fail_1;
1802 	}
1803 
1804 	sk_nodenum = node->sysctl_num;
1805 
1806 	/* interrupt moderation time in usecs */
1807 	if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
1808 	    CTLFLAG_READWRITE,
1809 	    CTLTYPE_INT, "int_mod",
1810 	    SYSCTL_DESCR("sk interrupt moderation timer"),
1811 	    sk_sysctl_handler, 0, sc,
1812 	    0, CTL_HW, sk_root_num, sk_nodenum, CTL_CREATE,
1813 	    CTL_EOL)) != 0) {
1814 		aprint_normal_dev(sc->sk_dev, "couldn't create int_mod sysctl node\n");
1815 		goto fail_1;
1816 	}
1817 
1818 	return;
1819 
1820 fail_1:
1821 	pci_intr_disestablish(pc, sc->sk_intrhand);
1822 fail:
1823 	bus_space_unmap(sc->sk_btag, sc->sk_bhandle, iosize);
1824 }
1825 
1826 int
1827 sk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
1828 {
1829 	struct sk_softc		*sc = sc_if->sk_softc;
1830 	struct sk_tx_desc	*f = NULL;
1831 	u_int32_t		frag, cur, cnt = 0, sk_ctl;
1832 	int			i;
1833 	struct sk_txmap_entry	*entry;
1834 	bus_dmamap_t		txmap;
1835 
1836 	DPRINTFN(3, ("sk_encap\n"));
1837 
1838 	entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
1839 	if (entry == NULL) {
1840 		DPRINTFN(3, ("sk_encap: no txmap available\n"));
1841 		return ENOBUFS;
1842 	}
1843 	txmap = entry->dmamap;
1844 
1845 	cur = frag = *txidx;
1846 
1847 #ifdef SK_DEBUG
1848 	if (skdebug >= 3)
1849 		sk_dump_mbuf(m_head);
1850 #endif
1851 
1852 	/*
1853 	 * Start packing the mbufs in this chain into
1854 	 * the fragment pointers. Stop when we run out
1855 	 * of fragments or hit the end of the mbuf chain.
1856 	 */
1857 	if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
1858 	    BUS_DMA_NOWAIT)) {
1859 		DPRINTFN(1, ("sk_encap: dmamap failed\n"));
1860 		return ENOBUFS;
1861 	}
1862 
1863 	DPRINTFN(3, ("sk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1864 
1865 	/* Sync the DMA map. */
1866 	bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
1867 	    BUS_DMASYNC_PREWRITE);
1868 
1869 	for (i = 0; i < txmap->dm_nsegs; i++) {
1870 		if ((SK_TX_RING_CNT - (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2) {
1871 			DPRINTFN(1, ("sk_encap: too few descriptors free\n"));
1872 			return ENOBUFS;
1873 		}
1874 		f = &sc_if->sk_rdata->sk_tx_ring[frag];
1875 		f->sk_data_lo = htole32(txmap->dm_segs[i].ds_addr);
1876 		sk_ctl = txmap->dm_segs[i].ds_len | SK_OPCODE_DEFAULT;
1877 		if (cnt == 0)
1878 			sk_ctl |= SK_TXCTL_FIRSTFRAG;
1879 		else
1880 			sk_ctl |= SK_TXCTL_OWN;
1881 		f->sk_ctl = htole32(sk_ctl);
1882 		cur = frag;
1883 		SK_INC(frag, SK_TX_RING_CNT);
1884 		cnt++;
1885 	}
1886 
1887 	sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1888 	SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
1889 
1890 	sc_if->sk_cdata.sk_tx_map[cur] = entry;
1891 	sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
1892 		htole32(SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR);
1893 
1894 	/* Sync descriptors before handing to chip */
1895 	SK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
1896 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1897 
1898 	sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |=
1899 		htole32(SK_TXCTL_OWN);
1900 
1901 	/* Sync first descriptor to hand it off */
1902 	SK_CDTXSYNC(sc_if, *txidx, 1, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1903 
1904 	sc_if->sk_cdata.sk_tx_cnt += cnt;
1905 
1906 #ifdef SK_DEBUG
1907 	if (skdebug >= 3) {
1908 		struct sk_tx_desc *desc;
1909 		u_int32_t idx;
1910 		for (idx = *txidx; idx != frag; SK_INC(idx, SK_TX_RING_CNT)) {
1911 			desc = &sc_if->sk_rdata->sk_tx_ring[idx];
1912 			sk_dump_txdesc(desc, idx);
1913 		}
1914 	}
1915 #endif
1916 
1917 	*txidx = frag;
1918 
1919 	DPRINTFN(3, ("sk_encap: completed successfully\n"));
1920 
1921 	return 0;
1922 }
1923 
1924 void
1925 sk_start(struct ifnet *ifp)
1926 {
1927         struct sk_if_softc	*sc_if = ifp->if_softc;
1928         struct sk_softc		*sc = sc_if->sk_softc;
1929         struct mbuf		*m_head = NULL;
1930         u_int32_t		idx = sc_if->sk_cdata.sk_tx_prod;
1931 	int			pkts = 0;
1932 
1933 	DPRINTFN(3, ("sk_start (idx %d, tx_chain[idx] %p)\n", idx,
1934 		sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf));
1935 
1936 	while (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1937 		IFQ_POLL(&ifp->if_snd, m_head);
1938 		if (m_head == NULL)
1939 			break;
1940 
1941 		/*
1942 		 * Pack the data into the transmit ring. If we
1943 		 * don't have room, set the OACTIVE flag and wait
1944 		 * for the NIC to drain the ring.
1945 		 */
1946 		if (sk_encap(sc_if, m_head, &idx)) {
1947 			ifp->if_flags |= IFF_OACTIVE;
1948 			break;
1949 		}
1950 
1951 		/* now we are committed to transmit the packet */
1952 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
1953 		pkts++;
1954 
1955 		/*
1956 		 * If there's a BPF listener, bounce a copy of this frame
1957 		 * to him.
1958 		 */
1959 #if NBPFILTER > 0
1960 		if (ifp->if_bpf)
1961 			bpf_mtap(ifp->if_bpf, m_head);
1962 #endif
1963 	}
1964 	if (pkts == 0)
1965 		return;
1966 
1967 	/* Transmit */
1968 	if (idx != sc_if->sk_cdata.sk_tx_prod) {
1969 		sc_if->sk_cdata.sk_tx_prod = idx;
1970 		CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
1971 
1972 		/* Set a timeout in case the chip goes out to lunch. */
1973 		ifp->if_timer = 5;
1974 	}
1975 }
1976 
1977 
1978 void
1979 sk_watchdog(struct ifnet *ifp)
1980 {
1981 	struct sk_if_softc *sc_if = ifp->if_softc;
1982 
1983 	/*
1984 	 * Reclaim first as there is a possibility of losing Tx completion
1985 	 * interrupts.
1986 	 */
1987 	sk_txeof(sc_if);
1988 	if (sc_if->sk_cdata.sk_tx_cnt != 0) {
1989 		aprint_error_dev(sc_if->sk_dev, "watchdog timeout\n");
1990 
1991 		ifp->if_oerrors++;
1992 
1993 		sk_init(ifp);
1994 	}
1995 }
1996 
1997 void
1998 sk_shutdown(void *v)
1999 {
2000 	struct sk_if_softc	*sc_if = (struct sk_if_softc *)v;
2001 	struct sk_softc		*sc = sc_if->sk_softc;
2002 	struct ifnet 		*ifp = &sc_if->sk_ethercom.ec_if;
2003 
2004 	DPRINTFN(2, ("sk_shutdown\n"));
2005 	sk_stop(ifp,1);
2006 
2007 	/* Turn off the 'driver is loaded' LED. */
2008 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
2009 
2010 	/*
2011 	 * Reset the GEnesis controller. Doing this should also
2012 	 * assert the resets on the attached XMAC(s).
2013 	 */
2014 	sk_reset(sc);
2015 }
2016 
2017 void
2018 sk_rxeof(struct sk_if_softc *sc_if)
2019 {
2020 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
2021 	struct mbuf		*m;
2022 	struct sk_chain		*cur_rx;
2023 	struct sk_rx_desc	*cur_desc;
2024 	int			i, cur, total_len = 0;
2025 	u_int32_t		rxstat, sk_ctl;
2026 	bus_dmamap_t		dmamap;
2027 
2028 	i = sc_if->sk_cdata.sk_rx_prod;
2029 
2030 	DPRINTFN(3, ("sk_rxeof %d\n", i));
2031 
2032 	for (;;) {
2033 		cur = i;
2034 
2035 		/* Sync the descriptor */
2036 		SK_CDRXSYNC(sc_if, cur,
2037 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2038 
2039 		sk_ctl = le32toh(sc_if->sk_rdata->sk_rx_ring[cur].sk_ctl);
2040 		if (sk_ctl & SK_RXCTL_OWN) {
2041 			/* Invalidate the descriptor -- it's not ready yet */
2042 			SK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_PREREAD);
2043 			sc_if->sk_cdata.sk_rx_prod = i;
2044 			break;
2045 		}
2046 
2047 		cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
2048 		cur_desc = &sc_if->sk_rdata->sk_rx_ring[cur];
2049 		dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
2050 
2051 		bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
2052 		    dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2053 
2054 		rxstat = le32toh(cur_desc->sk_xmac_rxstat);
2055 		m = cur_rx->sk_mbuf;
2056 		cur_rx->sk_mbuf = NULL;
2057 		total_len = SK_RXBYTES(le32toh(cur_desc->sk_ctl));
2058 
2059 		sc_if->sk_cdata.sk_rx_map[cur] = 0;
2060 
2061 		SK_INC(i, SK_RX_RING_CNT);
2062 
2063 		if (rxstat & XM_RXSTAT_ERRFRAME) {
2064 			ifp->if_ierrors++;
2065 			sk_newbuf(sc_if, cur, m, dmamap);
2066 			continue;
2067 		}
2068 
2069 		/*
2070 		 * Try to allocate a new jumbo buffer. If that
2071 		 * fails, copy the packet to mbufs and put the
2072 		 * jumbo buffer back in the ring so it can be
2073 		 * re-used. If allocating mbufs fails, then we
2074 		 * have to drop the packet.
2075 		 */
2076 		if (sk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
2077 			struct mbuf		*m0;
2078 			m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
2079 			    total_len + ETHER_ALIGN, 0, ifp, NULL);
2080 			sk_newbuf(sc_if, cur, m, dmamap);
2081 			if (m0 == NULL) {
2082 				aprint_error_dev(sc_if->sk_dev, "no receive "
2083 				    "buffers available -- packet dropped!\n");
2084 				ifp->if_ierrors++;
2085 				continue;
2086 			}
2087 			m_adj(m0, ETHER_ALIGN);
2088 			m = m0;
2089 		} else {
2090 			m->m_pkthdr.rcvif = ifp;
2091 			m->m_pkthdr.len = m->m_len = total_len;
2092 		}
2093 
2094 		ifp->if_ipackets++;
2095 
2096 #if NBPFILTER > 0
2097 		if (ifp->if_bpf)
2098 			bpf_mtap(ifp->if_bpf, m);
2099 #endif
2100 		/* pass it on. */
2101 		(*ifp->if_input)(ifp, m);
2102 	}
2103 }
2104 
2105 void
2106 sk_txeof(struct sk_if_softc *sc_if)
2107 {
2108 	struct sk_softc		*sc = sc_if->sk_softc;
2109 	struct sk_tx_desc	*cur_tx;
2110 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
2111 	u_int32_t		idx, sk_ctl;
2112 	struct sk_txmap_entry	*entry;
2113 
2114 	DPRINTFN(3, ("sk_txeof\n"));
2115 
2116 	/*
2117 	 * Go through our tx ring and free mbufs for those
2118 	 * frames that have been sent.
2119 	 */
2120 	idx = sc_if->sk_cdata.sk_tx_cons;
2121 	while (idx != sc_if->sk_cdata.sk_tx_prod) {
2122 		SK_CDTXSYNC(sc_if, idx, 1,
2123 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2124 
2125 		cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
2126 		sk_ctl = le32toh(cur_tx->sk_ctl);
2127 #ifdef SK_DEBUG
2128 		if (skdebug >= 3)
2129 			sk_dump_txdesc(cur_tx, idx);
2130 #endif
2131 		if (sk_ctl & SK_TXCTL_OWN) {
2132 			SK_CDTXSYNC(sc_if, idx, 1, BUS_DMASYNC_PREREAD);
2133 			break;
2134 		}
2135 		if (sk_ctl & SK_TXCTL_LASTFRAG)
2136 			ifp->if_opackets++;
2137 		if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
2138 			entry = sc_if->sk_cdata.sk_tx_map[idx];
2139 
2140 			m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
2141 			sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
2142 
2143 			bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
2144 			    entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2145 
2146 			bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
2147 			SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
2148 					  link);
2149 			sc_if->sk_cdata.sk_tx_map[idx] = NULL;
2150 		}
2151 		sc_if->sk_cdata.sk_tx_cnt--;
2152 		SK_INC(idx, SK_TX_RING_CNT);
2153 	}
2154 	if (sc_if->sk_cdata.sk_tx_cnt == 0)
2155 		ifp->if_timer = 0;
2156 	else /* nudge chip to keep tx ring moving */
2157 		CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
2158 
2159 	if (sc_if->sk_cdata.sk_tx_cnt < SK_TX_RING_CNT - 2)
2160 		ifp->if_flags &= ~IFF_OACTIVE;
2161 
2162 	sc_if->sk_cdata.sk_tx_cons = idx;
2163 }
2164 
2165 void
2166 sk_tick(void *xsc_if)
2167 {
2168 	struct sk_if_softc *sc_if = xsc_if;
2169 	struct mii_data *mii = &sc_if->sk_mii;
2170 	struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2171 	int i;
2172 
2173 	DPRINTFN(3, ("sk_tick\n"));
2174 
2175 	if (!(ifp->if_flags & IFF_UP))
2176 		return;
2177 
2178 	if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2179 		sk_intr_bcom(sc_if);
2180 		return;
2181 	}
2182 
2183 	/*
2184 	 * According to SysKonnect, the correct way to verify that
2185 	 * the link has come back up is to poll bit 0 of the GPIO
2186 	 * register three times. This pin has the signal from the
2187 	 * link sync pin connected to it; if we read the same link
2188 	 * state 3 times in a row, we know the link is up.
2189 	 */
2190 	for (i = 0; i < 3; i++) {
2191 		if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
2192 			break;
2193 	}
2194 
2195 	if (i != 3) {
2196 		callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2197 		return;
2198 	}
2199 
2200 	/* Turn the GP0 interrupt back on. */
2201 	SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2202 	SK_XM_READ_2(sc_if, XM_ISR);
2203 	mii_tick(mii);
2204 	mii_pollstat(mii);
2205 	callout_stop(&sc_if->sk_tick_ch);
2206 }
2207 
2208 void
2209 sk_intr_bcom(struct sk_if_softc *sc_if)
2210 {
2211 	struct mii_data *mii = &sc_if->sk_mii;
2212 	struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
2213 	int status;
2214 
2215 
2216 	DPRINTFN(3, ("sk_intr_bcom\n"));
2217 
2218 	SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2219 
2220 	/*
2221 	 * Read the PHY interrupt register to make sure
2222 	 * we clear any pending interrupts.
2223 	 */
2224 	status = sk_xmac_miibus_readreg(sc_if->sk_dev,
2225 	    SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
2226 
2227 	if (!(ifp->if_flags & IFF_RUNNING)) {
2228 		sk_init_xmac(sc_if);
2229 		return;
2230 	}
2231 
2232 	if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
2233 		int lstat;
2234 		lstat = sk_xmac_miibus_readreg(sc_if->sk_dev,
2235 		    SK_PHYADDR_BCOM, BRGPHY_MII_AUXSTS);
2236 
2237 		if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
2238 			(void)mii_mediachg(mii);
2239 			/* Turn off the link LED. */
2240 			SK_IF_WRITE_1(sc_if, 0,
2241 			    SK_LINKLED1_CTL, SK_LINKLED_OFF);
2242 			sc_if->sk_link = 0;
2243 		} else if (status & BRGPHY_ISR_LNK_CHG) {
2244 			sk_xmac_miibus_writereg(sc_if->sk_dev,
2245 			    SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFF00);
2246 			mii_tick(mii);
2247 			sc_if->sk_link = 1;
2248 			/* Turn on the link LED. */
2249 			SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2250 			    SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
2251 			    SK_LINKLED_BLINK_OFF);
2252 			mii_pollstat(mii);
2253 		} else {
2254 			mii_tick(mii);
2255 			callout_reset(&sc_if->sk_tick_ch, hz, sk_tick,sc_if);
2256 		}
2257 	}
2258 
2259 	SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2260 }
2261 
2262 void
2263 sk_intr_xmac(struct sk_if_softc	*sc_if)
2264 {
2265 	u_int16_t status = SK_XM_READ_2(sc_if, XM_ISR);
2266 
2267 	DPRINTFN(3, ("sk_intr_xmac\n"));
2268 
2269 	if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
2270 		if (status & XM_ISR_GP0_SET) {
2271 			SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2272 			callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2273 		}
2274 
2275 		if (status & XM_ISR_AUTONEG_DONE) {
2276 			callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
2277 		}
2278 	}
2279 
2280 	if (status & XM_IMR_TX_UNDERRUN)
2281 		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
2282 
2283 	if (status & XM_IMR_RX_OVERRUN)
2284 		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
2285 }
2286 
2287 void
2288 sk_intr_yukon(struct sk_if_softc *sc_if)
2289 {
2290 	int status;
2291 
2292 	status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2293 
2294 	DPRINTFN(3, ("sk_intr_yukon status=%#x\n", status));
2295 }
2296 
2297 int
2298 sk_intr(void *xsc)
2299 {
2300 	struct sk_softc		*sc = xsc;
2301 	struct sk_if_softc	*sc_if0 = sc->sk_if[SK_PORT_A];
2302 	struct sk_if_softc	*sc_if1 = sc->sk_if[SK_PORT_B];
2303 	struct ifnet		*ifp0 = NULL, *ifp1 = NULL;
2304 	u_int32_t		status;
2305 	int			claimed = 0;
2306 
2307 	if (sc_if0 != NULL)
2308 		ifp0 = &sc_if0->sk_ethercom.ec_if;
2309 	if (sc_if1 != NULL)
2310 		ifp1 = &sc_if1->sk_ethercom.ec_if;
2311 
2312 	for (;;) {
2313 		status = CSR_READ_4(sc, SK_ISSR);
2314 		DPRINTFN(3, ("sk_intr: status=%#x\n", status));
2315 
2316 		if (!(status & sc->sk_intrmask))
2317 			break;
2318 
2319 		claimed = 1;
2320 
2321 		/* Handle receive interrupts first. */
2322 		if (sc_if0 && (status & SK_ISR_RX1_EOF)) {
2323 			sk_rxeof(sc_if0);
2324 			CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
2325 			    SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2326 		}
2327 		if (sc_if1 && (status & SK_ISR_RX2_EOF)) {
2328 			sk_rxeof(sc_if1);
2329 			CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
2330 			    SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2331 		}
2332 
2333 		/* Then transmit interrupts. */
2334 		if (sc_if0 && (status & SK_ISR_TX1_S_EOF)) {
2335 			sk_txeof(sc_if0);
2336 			CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
2337 			    SK_TXBMU_CLR_IRQ_EOF);
2338 		}
2339 		if (sc_if1 && (status & SK_ISR_TX2_S_EOF)) {
2340 			sk_txeof(sc_if1);
2341 			CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
2342 			    SK_TXBMU_CLR_IRQ_EOF);
2343 		}
2344 
2345 		/* Then MAC interrupts. */
2346 		if (sc_if0 && (status & SK_ISR_MAC1) &&
2347 		    (ifp0->if_flags & IFF_RUNNING)) {
2348 			if (sc->sk_type == SK_GENESIS)
2349 				sk_intr_xmac(sc_if0);
2350 			else
2351 				sk_intr_yukon(sc_if0);
2352 		}
2353 
2354 		if (sc_if1 && (status & SK_ISR_MAC2) &&
2355 		    (ifp1->if_flags & IFF_RUNNING)) {
2356 			if (sc->sk_type == SK_GENESIS)
2357 				sk_intr_xmac(sc_if1);
2358 			else
2359 				sk_intr_yukon(sc_if1);
2360 
2361 		}
2362 
2363 		if (status & SK_ISR_EXTERNAL_REG) {
2364 			if (sc_if0 != NULL &&
2365 			    sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
2366 				sk_intr_bcom(sc_if0);
2367 
2368 			if (sc_if1 != NULL &&
2369 			    sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
2370 				sk_intr_bcom(sc_if1);
2371 		}
2372 	}
2373 
2374 	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2375 
2376 	if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
2377 		sk_start(ifp0);
2378 	if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
2379 		sk_start(ifp1);
2380 
2381 #if NRND > 0
2382 	if (RND_ENABLED(&sc->rnd_source))
2383 		rnd_add_uint32(&sc->rnd_source, status);
2384 #endif
2385 
2386 	if (sc->sk_int_mod_pending)
2387 		sk_update_int_mod(sc);
2388 
2389 	return claimed;
2390 }
2391 
2392 void
2393 sk_init_xmac(struct sk_if_softc	*sc_if)
2394 {
2395 	struct sk_softc		*sc = sc_if->sk_softc;
2396 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
2397 	static const struct sk_bcom_hack     bhack[] = {
2398 	{ 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
2399 	{ 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
2400 	{ 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
2401 	{ 0, 0 } };
2402 
2403 	DPRINTFN(1, ("sk_init_xmac\n"));
2404 
2405 	/* Unreset the XMAC. */
2406 	SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
2407 	DELAY(1000);
2408 
2409 	/* Reset the XMAC's internal state. */
2410 	SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2411 
2412 	/* Save the XMAC II revision */
2413 	sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
2414 
2415 	/*
2416 	 * Perform additional initialization for external PHYs,
2417 	 * namely for the 1000baseTX cards that use the XMAC's
2418 	 * GMII mode.
2419 	 */
2420 	if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2421 		int			i = 0;
2422 		u_int32_t		val;
2423 
2424 		/* Take PHY out of reset. */
2425 		val = sk_win_read_4(sc, SK_GPIO);
2426 		if (sc_if->sk_port == SK_PORT_A)
2427 			val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
2428 		else
2429 			val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
2430 		sk_win_write_4(sc, SK_GPIO, val);
2431 
2432 		/* Enable GMII mode on the XMAC. */
2433 		SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
2434 
2435 		sk_xmac_miibus_writereg(sc_if->sk_dev,
2436 		    SK_PHYADDR_BCOM, MII_BMCR, BMCR_RESET);
2437 		DELAY(10000);
2438 		sk_xmac_miibus_writereg(sc_if->sk_dev,
2439 		    SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFFF0);
2440 
2441 		/*
2442 		 * Early versions of the BCM5400 apparently have
2443 		 * a bug that requires them to have their reserved
2444 		 * registers initialized to some magic values. I don't
2445 		 * know what the numbers do, I'm just the messenger.
2446 		 */
2447 		if (sk_xmac_miibus_readreg(sc_if->sk_dev,
2448 		    SK_PHYADDR_BCOM, 0x03) == 0x6041) {
2449 			while (bhack[i].reg) {
2450 				sk_xmac_miibus_writereg(sc_if->sk_dev,
2451 				    SK_PHYADDR_BCOM, bhack[i].reg,
2452 				    bhack[i].val);
2453 				i++;
2454 			}
2455 		}
2456 	}
2457 
2458 	/* Set station address */
2459 	SK_XM_WRITE_2(sc_if, XM_PAR0,
2460 		      *(u_int16_t *)(&sc_if->sk_enaddr[0]));
2461 	SK_XM_WRITE_2(sc_if, XM_PAR1,
2462 		      *(u_int16_t *)(&sc_if->sk_enaddr[2]));
2463 	SK_XM_WRITE_2(sc_if, XM_PAR2,
2464 		      *(u_int16_t *)(&sc_if->sk_enaddr[4]));
2465 	SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
2466 
2467 	if (ifp->if_flags & IFF_PROMISC)
2468 		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2469 	else
2470 		SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
2471 
2472 	if (ifp->if_flags & IFF_BROADCAST)
2473 		SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2474 	else
2475 		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2476 
2477 	/* We don't need the FCS appended to the packet. */
2478 	SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
2479 
2480 	/* We want short frames padded to 60 bytes. */
2481 	SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
2482 
2483 	/*
2484 	 * Enable the reception of all error frames. This is is
2485 	 * a necessary evil due to the design of the XMAC. The
2486 	 * XMAC's receive FIFO is only 8K in size, however jumbo
2487 	 * frames can be up to 9000 bytes in length. When bad
2488 	 * frame filtering is enabled, the XMAC's RX FIFO operates
2489 	 * in 'store and forward' mode. For this to work, the
2490 	 * entire frame has to fit into the FIFO, but that means
2491 	 * that jumbo frames larger than 8192 bytes will be
2492 	 * truncated. Disabling all bad frame filtering causes
2493 	 * the RX FIFO to operate in streaming mode, in which
2494 	 * case the XMAC will start transfering frames out of the
2495 	 * RX FIFO as soon as the FIFO threshold is reached.
2496 	 */
2497 	SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
2498 	    XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
2499 	    XM_MODE_RX_INRANGELEN);
2500 
2501 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2502 		SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2503 	else
2504 		SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2505 
2506 	/*
2507 	 * Bump up the transmit threshold. This helps hold off transmit
2508 	 * underruns when we're blasting traffic from both ports at once.
2509 	 */
2510 	SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
2511 
2512 	/* Set multicast filter */
2513 	sk_setmulti(sc_if);
2514 
2515 	/* Clear and enable interrupts */
2516 	SK_XM_READ_2(sc_if, XM_ISR);
2517 	if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
2518 		SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
2519 	else
2520 		SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2521 
2522 	/* Configure MAC arbiter */
2523 	switch (sc_if->sk_xmac_rev) {
2524 	case XM_XMAC_REV_B2:
2525 		sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
2526 		sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
2527 		sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
2528 		sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
2529 		sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
2530 		sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
2531 		sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
2532 		sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
2533 		sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2534 		break;
2535 	case XM_XMAC_REV_C1:
2536 		sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
2537 		sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
2538 		sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
2539 		sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
2540 		sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
2541 		sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
2542 		sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
2543 		sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
2544 		sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2545 		break;
2546 	default:
2547 		break;
2548 	}
2549 	sk_win_write_2(sc, SK_MACARB_CTL,
2550 	    SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
2551 
2552 	sc_if->sk_link = 1;
2553 }
2554 
2555 void sk_init_yukon(struct sk_if_softc *sc_if)
2556 {
2557 	u_int32_t		/*mac, */phy;
2558 	u_int16_t		reg;
2559 	struct sk_softc		*sc;
2560 	int			i;
2561 
2562 	DPRINTFN(1, ("sk_init_yukon: start: sk_csr=%#x\n",
2563 		     CSR_READ_4(sc_if->sk_softc, SK_CSR)));
2564 
2565 	sc = sc_if->sk_softc;
2566 	if (sc->sk_type == SK_YUKON_LITE &&
2567 	    sc->sk_rev >= SK_YUKON_LITE_REV_A3) {
2568 		/* Take PHY out of reset. */
2569 		sk_win_write_4(sc, SK_GPIO,
2570 			(sk_win_read_4(sc, SK_GPIO) | SK_GPIO_DIR9) & ~SK_GPIO_DAT9);
2571 	}
2572 
2573 
2574 	/* GMAC and GPHY Reset */
2575 	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
2576 
2577 	DPRINTFN(6, ("sk_init_yukon: 1\n"));
2578 
2579 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2580 	DELAY(1000);
2581 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
2582 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2583 	DELAY(1000);
2584 
2585 
2586 	DPRINTFN(6, ("sk_init_yukon: 2\n"));
2587 
2588 	phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
2589 		SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
2590 
2591 	switch (sc_if->sk_softc->sk_pmd) {
2592 	case IFM_1000_SX:
2593 	case IFM_1000_LX:
2594 		phy |= SK_GPHY_FIBER;
2595 		break;
2596 
2597 	case IFM_1000_CX:
2598 	case IFM_1000_T:
2599 		phy |= SK_GPHY_COPPER;
2600 		break;
2601 	}
2602 
2603 	DPRINTFN(3, ("sk_init_yukon: phy=%#x\n", phy));
2604 
2605 	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
2606 	DELAY(1000);
2607 	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
2608 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
2609 		      SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
2610 
2611 	DPRINTFN(3, ("sk_init_yukon: gmac_ctrl=%#x\n",
2612 		     SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
2613 
2614 	DPRINTFN(6, ("sk_init_yukon: 3\n"));
2615 
2616 	/* unused read of the interrupt source register */
2617 	DPRINTFN(6, ("sk_init_yukon: 4\n"));
2618 	SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2619 
2620 	DPRINTFN(6, ("sk_init_yukon: 4a\n"));
2621 	reg = SK_YU_READ_2(sc_if, YUKON_PAR);
2622 	DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2623 
2624 	/* MIB Counter Clear Mode set */
2625         reg |= YU_PAR_MIB_CLR;
2626 	DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
2627 	DPRINTFN(6, ("sk_init_yukon: 4b\n"));
2628 	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2629 
2630 	/* MIB Counter Clear Mode clear */
2631 	DPRINTFN(6, ("sk_init_yukon: 5\n"));
2632         reg &= ~YU_PAR_MIB_CLR;
2633 	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2634 
2635 	/* receive control reg */
2636 	DPRINTFN(6, ("sk_init_yukon: 7\n"));
2637 	SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_UFLEN | YU_RCR_MUFLEN |
2638 		      YU_RCR_CRCR);
2639 
2640 	/* transmit parameter register */
2641 	DPRINTFN(6, ("sk_init_yukon: 8\n"));
2642 	SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
2643 		      YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
2644 
2645 	/* serial mode register */
2646 	DPRINTFN(6, ("sk_init_yukon: 9\n"));
2647 	SK_YU_WRITE_2(sc_if, YUKON_SMR, YU_SMR_DATA_BLIND(0x1c) |
2648 		      YU_SMR_MFL_VLAN | YU_SMR_MFL_JUMBO |
2649 		      YU_SMR_IPG_DATA(0x1e));
2650 
2651 	DPRINTFN(6, ("sk_init_yukon: 10\n"));
2652 	/* Setup Yukon's address */
2653 	for (i = 0; i < 3; i++) {
2654 		/* Write Source Address 1 (unicast filter) */
2655 		SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2656 			      sc_if->sk_enaddr[i * 2] |
2657 			      sc_if->sk_enaddr[i * 2 + 1] << 8);
2658 	}
2659 
2660 	for (i = 0; i < 3; i++) {
2661 		reg = sk_win_read_2(sc_if->sk_softc,
2662 				    SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2663 		SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2664 	}
2665 
2666 	/* Set multicast filter */
2667 	DPRINTFN(6, ("sk_init_yukon: 11\n"));
2668 	sk_setmulti(sc_if);
2669 
2670 	/* enable interrupt mask for counter overflows */
2671 	DPRINTFN(6, ("sk_init_yukon: 12\n"));
2672 	SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2673 	SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2674 	SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2675 
2676 	/* Configure RX MAC FIFO */
2677 	SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2678 	SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
2679 
2680 	/* Configure TX MAC FIFO */
2681 	SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2682 	SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2683 
2684 	DPRINTFN(6, ("sk_init_yukon: end\n"));
2685 }
2686 
2687 /*
2688  * Note that to properly initialize any part of the GEnesis chip,
2689  * you first have to take it out of reset mode.
2690  */
2691 int
2692 sk_init(struct ifnet *ifp)
2693 {
2694 	struct sk_if_softc	*sc_if = ifp->if_softc;
2695 	struct sk_softc		*sc = sc_if->sk_softc;
2696 	struct mii_data		*mii = &sc_if->sk_mii;
2697 	int			rc = 0, s;
2698 	u_int32_t		imr, imtimer_ticks;
2699 
2700 	DPRINTFN(1, ("sk_init\n"));
2701 
2702 	s = splnet();
2703 
2704 	if (ifp->if_flags & IFF_RUNNING) {
2705 		splx(s);
2706 		return 0;
2707 	}
2708 
2709 	/* Cancel pending I/O and free all RX/TX buffers. */
2710 	sk_stop(ifp,0);
2711 
2712 	if (sc->sk_type == SK_GENESIS) {
2713 		/* Configure LINK_SYNC LED */
2714 		SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
2715 		SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2716 			      SK_LINKLED_LINKSYNC_ON);
2717 
2718 		/* Configure RX LED */
2719 		SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
2720 			      SK_RXLEDCTL_COUNTER_START);
2721 
2722 		/* Configure TX LED */
2723 		SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
2724 			      SK_TXLEDCTL_COUNTER_START);
2725 	}
2726 
2727 	/* Configure I2C registers */
2728 
2729 	/* Configure XMAC(s) */
2730 	switch (sc->sk_type) {
2731 	case SK_GENESIS:
2732 		sk_init_xmac(sc_if);
2733 		break;
2734 	case SK_YUKON:
2735 	case SK_YUKON_LITE:
2736 	case SK_YUKON_LP:
2737 		sk_init_yukon(sc_if);
2738 		break;
2739 	}
2740 	if ((rc = mii_mediachg(mii)) == ENXIO)
2741 		rc = 0;
2742 	else if (rc != 0)
2743 		goto out;
2744 
2745 	if (sc->sk_type == SK_GENESIS) {
2746 		/* Configure MAC FIFOs */
2747 		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
2748 		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
2749 		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
2750 
2751 		SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
2752 		SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
2753 		SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
2754 	}
2755 
2756 	/* Configure transmit arbiter(s) */
2757 	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
2758 	    SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2759 
2760 	/* Configure RAMbuffers */
2761 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2762 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2763 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2764 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2765 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2766 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2767 
2768 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
2769 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
2770 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
2771 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
2772 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
2773 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
2774 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
2775 
2776 	/* Configure BMUs */
2777 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
2778 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
2779 	    SK_RX_RING_ADDR(sc_if, 0));
2780 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
2781 
2782 	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
2783 	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
2784             SK_TX_RING_ADDR(sc_if, 0));
2785 	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
2786 
2787 	/* Init descriptors */
2788 	if (sk_init_rx_ring(sc_if) == ENOBUFS) {
2789 		aprint_error_dev(sc_if->sk_dev, "initialization failed: no "
2790 		    "memory for rx buffers\n");
2791 		sk_stop(ifp,0);
2792 		splx(s);
2793 		return ENOBUFS;
2794 	}
2795 
2796 	if (sk_init_tx_ring(sc_if) == ENOBUFS) {
2797 		aprint_error_dev(sc_if->sk_dev, "initialization failed: no "
2798 		    "memory for tx buffers\n");
2799 		sk_stop(ifp,0);
2800 		splx(s);
2801 		return ENOBUFS;
2802 	}
2803 
2804 	/* Set interrupt moderation if changed via sysctl. */
2805 	switch (sc->sk_type) {
2806 	case SK_GENESIS:
2807 		imtimer_ticks = SK_IMTIMER_TICKS_GENESIS;
2808 		break;
2809 	case SK_YUKON_EC:
2810 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
2811 		break;
2812 	default:
2813 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
2814 	}
2815 	imr = sk_win_read_4(sc, SK_IMTIMERINIT);
2816 	if (imr != SK_IM_USECS(sc->sk_int_mod)) {
2817 		sk_win_write_4(sc, SK_IMTIMERINIT,
2818 		    SK_IM_USECS(sc->sk_int_mod));
2819 		aprint_verbose_dev(sc->sk_dev,
2820 		    "interrupt moderation is %d us\n", sc->sk_int_mod);
2821 	}
2822 
2823 	/* Configure interrupt handling */
2824 	CSR_READ_4(sc, SK_ISSR);
2825 	if (sc_if->sk_port == SK_PORT_A)
2826 		sc->sk_intrmask |= SK_INTRS1;
2827 	else
2828 		sc->sk_intrmask |= SK_INTRS2;
2829 
2830 	sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
2831 
2832 	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2833 
2834 	/* Start BMUs. */
2835 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
2836 
2837 	if (sc->sk_type == SK_GENESIS) {
2838 		/* Enable XMACs TX and RX state machines */
2839 		SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
2840 		SK_XM_SETBIT_2(sc_if, XM_MMUCMD,
2841 			       XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2842 	}
2843 
2844 	if (SK_YUKON_FAMILY(sc->sk_type)) {
2845 		u_int16_t reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
2846 		reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
2847 #if 0
2848 		/* XXX disable 100Mbps and full duplex mode? */
2849 		reg &= ~(YU_GPCR_SPEED | YU_GPCR_DPLX_EN);
2850 #endif
2851 		SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
2852 	}
2853 
2854 
2855 	ifp->if_flags |= IFF_RUNNING;
2856 	ifp->if_flags &= ~IFF_OACTIVE;
2857 
2858 out:
2859 	splx(s);
2860 	return rc;
2861 }
2862 
2863 void
2864 sk_stop(struct ifnet *ifp, int disable)
2865 {
2866         struct sk_if_softc	*sc_if = ifp->if_softc;
2867 	struct sk_softc		*sc = sc_if->sk_softc;
2868 	int			i;
2869 
2870 	DPRINTFN(1, ("sk_stop\n"));
2871 
2872 	callout_stop(&sc_if->sk_tick_ch);
2873 
2874 	if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2875 		u_int32_t		val;
2876 
2877 		/* Put PHY back into reset. */
2878 		val = sk_win_read_4(sc, SK_GPIO);
2879 		if (sc_if->sk_port == SK_PORT_A) {
2880 			val |= SK_GPIO_DIR0;
2881 			val &= ~SK_GPIO_DAT0;
2882 		} else {
2883 			val |= SK_GPIO_DIR2;
2884 			val &= ~SK_GPIO_DAT2;
2885 		}
2886 		sk_win_write_4(sc, SK_GPIO, val);
2887 	}
2888 
2889 	/* Turn off various components of this interface. */
2890 	SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2891 	switch (sc->sk_type) {
2892 	case SK_GENESIS:
2893 		SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL,
2894 			      SK_TXMACCTL_XMAC_RESET);
2895 		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
2896 		break;
2897 	case SK_YUKON:
2898 	case SK_YUKON_LITE:
2899 	case SK_YUKON_LP:
2900 		SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2901 		SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2902 		break;
2903 	}
2904 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2905 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2906 	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
2907 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2908 	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2909 	SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2910 	SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2911 	SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2912 	SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2913 
2914 	/* Disable interrupts */
2915 	if (sc_if->sk_port == SK_PORT_A)
2916 		sc->sk_intrmask &= ~SK_INTRS1;
2917 	else
2918 		sc->sk_intrmask &= ~SK_INTRS2;
2919 	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2920 
2921 	SK_XM_READ_2(sc_if, XM_ISR);
2922 	SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2923 
2924 	/* Free RX and TX mbufs still in the queues. */
2925 	for (i = 0; i < SK_RX_RING_CNT; i++) {
2926 		if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2927 			m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2928 			sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2929 		}
2930 	}
2931 
2932 	for (i = 0; i < SK_TX_RING_CNT; i++) {
2933 		if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2934 			m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2935 			sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2936 		}
2937 	}
2938 
2939 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2940 }
2941 
2942 CFATTACH_DECL_NEW(skc, sizeof(struct sk_softc),
2943     skc_probe, skc_attach, NULL, NULL);
2944 
2945 CFATTACH_DECL_NEW(sk, sizeof(struct sk_if_softc),
2946     sk_probe, sk_attach, NULL, NULL);
2947 
2948 #ifdef SK_DEBUG
2949 void
2950 sk_dump_txdesc(struct sk_tx_desc *desc, int idx)
2951 {
2952 #define DESC_PRINT(X)					\
2953 	if (X)					\
2954 		printf("txdesc[%d]." #X "=%#x\n",	\
2955 		       idx, X);
2956 
2957 	DESC_PRINT(le32toh(desc->sk_ctl));
2958 	DESC_PRINT(le32toh(desc->sk_next));
2959 	DESC_PRINT(le32toh(desc->sk_data_lo));
2960 	DESC_PRINT(le32toh(desc->sk_data_hi));
2961 	DESC_PRINT(le32toh(desc->sk_xmac_txstat));
2962 	DESC_PRINT(le16toh(desc->sk_rsvd0));
2963 	DESC_PRINT(le16toh(desc->sk_csum_startval));
2964 	DESC_PRINT(le16toh(desc->sk_csum_startpos));
2965 	DESC_PRINT(le16toh(desc->sk_csum_writepos));
2966 	DESC_PRINT(le16toh(desc->sk_rsvd1));
2967 #undef PRINT
2968 }
2969 
2970 void
2971 sk_dump_bytes(const char *data, int len)
2972 {
2973 	int c, i, j;
2974 
2975 	for (i = 0; i < len; i += 16) {
2976 		printf("%08x  ", i);
2977 		c = len - i;
2978 		if (c > 16) c = 16;
2979 
2980 		for (j = 0; j < c; j++) {
2981 			printf("%02x ", data[i + j] & 0xff);
2982 			if ((j & 0xf) == 7 && j > 0)
2983 				printf(" ");
2984 		}
2985 
2986 		for (; j < 16; j++)
2987 			printf("   ");
2988 		printf("  ");
2989 
2990 		for (j = 0; j < c; j++) {
2991 			int ch = data[i + j] & 0xff;
2992 			printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
2993 		}
2994 
2995 		printf("\n");
2996 
2997 		if (c < 16)
2998 			break;
2999 	}
3000 }
3001 
3002 void
3003 sk_dump_mbuf(struct mbuf *m)
3004 {
3005 	int count = m->m_pkthdr.len;
3006 
3007 	printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
3008 
3009 	while (count > 0 && m) {
3010 		printf("m=%p, m->m_data=%p, m->m_len=%d\n",
3011 		       m, m->m_data, m->m_len);
3012 		sk_dump_bytes(mtod(m, char *), m->m_len);
3013 
3014 		count -= m->m_len;
3015 		m = m->m_next;
3016 	}
3017 }
3018 #endif
3019 
3020 static int
3021 sk_sysctl_handler(SYSCTLFN_ARGS)
3022 {
3023 	int error, t;
3024 	struct sysctlnode node;
3025 	struct sk_softc *sc;
3026 
3027 	node = *rnode;
3028 	sc = node.sysctl_data;
3029 	t = sc->sk_int_mod;
3030 	node.sysctl_data = &t;
3031 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
3032 	if (error || newp == NULL)
3033 		return error;
3034 
3035 	if (t < SK_IM_MIN || t > SK_IM_MAX)
3036 		return EINVAL;
3037 
3038 	/* update the softc with sysctl-changed value, and mark
3039 	   for hardware update */
3040 	sc->sk_int_mod = t;
3041 	sc->sk_int_mod_pending = 1;
3042 	return 0;
3043 }
3044 
3045 /*
3046  * Set up sysctl(3) MIB, hw.sk.* - Individual controllers will be
3047  * set up in skc_attach()
3048  */
3049 SYSCTL_SETUP(sysctl_sk, "sysctl sk subtree setup")
3050 {
3051 	int rc;
3052 	const struct sysctlnode *node;
3053 
3054 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
3055 	    0, CTLTYPE_NODE, "hw", NULL,
3056 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
3057 		goto err;
3058 	}
3059 
3060 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
3061 	    0, CTLTYPE_NODE, "sk",
3062 	    SYSCTL_DESCR("sk interface controls"),
3063 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
3064 		goto err;
3065 	}
3066 
3067 	sk_root_num = node->sysctl_num;
3068 	return;
3069 
3070 err:
3071 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
3072 }
3073