if_sk.c revision 143751
1/*	$OpenBSD: if_sk.c,v 2.33 2003/08/12 05:23:06 nate Exp $	*/
2
3/*-
4 * Copyright (c) 1997, 1998, 1999, 2000
5 *	Bill Paul <wpaul@ctr.columbia.edu>.  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 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34/*-
35 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
40 *
41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 */
49
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD: head/sys/dev/sk/if_sk.c 143751 2005-03-17 14:18:58Z bz $");
52
53/*
54 * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports
55 * the SK-984x series adapters, both single port and dual port.
56 * References:
57 * 	The XaQti XMAC II datasheet,
58 *  http://www.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
59 *	The SysKonnect GEnesis manual, http://www.syskonnect.com
60 *
61 * Note: XaQti has been aquired by Vitesse, and Vitesse does not have the
62 * XMAC II datasheet online. I have put my copy at people.freebsd.org as a
63 * convenience to others until Vitesse corrects this problem:
64 *
65 * http://people.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
66 *
67 * Written by Bill Paul <wpaul@ee.columbia.edu>
68 * Department of Electrical Engineering
69 * Columbia University, New York City
70 */
71/*
72 * The SysKonnect gigabit ethernet adapters consist of two main
73 * components: the SysKonnect GEnesis controller chip and the XaQti Corp.
74 * XMAC II gigabit ethernet MAC. The XMAC provides all of the MAC
75 * components and a PHY while the GEnesis controller provides a PCI
76 * interface with DMA support. Each card may have between 512K and
77 * 2MB of SRAM on board depending on the configuration.
78 *
79 * The SysKonnect GEnesis controller can have either one or two XMAC
80 * chips connected to it, allowing single or dual port NIC configurations.
81 * SysKonnect has the distinction of being the only vendor on the market
82 * with a dual port gigabit ethernet NIC. The GEnesis provides dual FIFOs,
83 * dual DMA queues, packet/MAC/transmit arbiters and direct access to the
84 * XMAC registers. This driver takes advantage of these features to allow
85 * both XMACs to operate as independent interfaces.
86 */
87
88#include <sys/param.h>
89#include <sys/systm.h>
90#include <sys/sockio.h>
91#include <sys/mbuf.h>
92#include <sys/malloc.h>
93#include <sys/kernel.h>
94#include <sys/module.h>
95#include <sys/socket.h>
96#include <sys/queue.h>
97
98#include <net/if.h>
99#include <net/if_arp.h>
100#include <net/ethernet.h>
101#include <net/if_dl.h>
102#include <net/if_media.h>
103
104#include <net/bpf.h>
105
106#include <vm/vm.h>              /* for vtophys */
107#include <vm/pmap.h>            /* for vtophys */
108#include <machine/bus_pio.h>
109#include <machine/bus_memio.h>
110#include <machine/bus.h>
111#include <machine/resource.h>
112#include <sys/bus.h>
113#include <sys/rman.h>
114
115#include <dev/mii/mii.h>
116#include <dev/mii/miivar.h>
117#include <dev/mii/brgphyreg.h>
118
119#include <dev/pci/pcireg.h>
120#include <dev/pci/pcivar.h>
121
122#if 0
123#define SK_USEIOSPACE
124#endif
125
126#include <pci/if_skreg.h>
127#include <pci/xmaciireg.h>
128#include <pci/yukonreg.h>
129
130MODULE_DEPEND(sk, pci, 1, 1, 1);
131MODULE_DEPEND(sk, ether, 1, 1, 1);
132MODULE_DEPEND(sk, miibus, 1, 1, 1);
133
134/* "controller miibus0" required.  See GENERIC if you get errors here. */
135#include "miibus_if.h"
136
137#ifndef lint
138static const char rcsid[] =
139  "$FreeBSD: head/sys/dev/sk/if_sk.c 143751 2005-03-17 14:18:58Z bz $";
140#endif
141
142static struct sk_type sk_devs[] = {
143	{
144		VENDORID_SK,
145		DEVICEID_SK_V1,
146		"SysKonnect Gigabit Ethernet (V1.0)"
147	},
148	{
149		VENDORID_SK,
150		DEVICEID_SK_V2,
151		"SysKonnect Gigabit Ethernet (V2.0)"
152	},
153	{
154		VENDORID_MARVELL,
155		DEVICEID_SK_V2,
156		"Marvell Gigabit Ethernet"
157	},
158	{
159		VENDORID_MARVELL,
160		DEVICEID_BELKIN_5005,
161		"Belkin F5D5005 Gigabit Ethernet"
162	},
163	{
164		VENDORID_3COM,
165		DEVICEID_3COM_3C940,
166		"3Com 3C940 Gigabit Ethernet"
167	},
168	{
169		VENDORID_LINKSYS,
170		DEVICEID_LINKSYS_EG1032,
171		"Linksys EG1032 Gigabit Ethernet"
172	},
173	{
174		VENDORID_DLINK,
175		DEVICEID_DLINK_DGE530T,
176		"D-Link DGE-530T Gigabit Ethernet"
177	},
178	{ 0, 0, NULL }
179};
180
181static int skc_probe(device_t);
182static int skc_attach(device_t);
183static int skc_detach(device_t);
184static void skc_shutdown(device_t);
185static int sk_detach(device_t);
186static int sk_probe(device_t);
187static int sk_attach(device_t);
188static void sk_tick(void *);
189static void sk_intr(void *);
190static void sk_intr_xmac(struct sk_if_softc *);
191static void sk_intr_bcom(struct sk_if_softc *);
192static void sk_intr_yukon(struct sk_if_softc *);
193static void sk_rxeof(struct sk_if_softc *);
194static void sk_txeof(struct sk_if_softc *);
195static int sk_encap(struct sk_if_softc *, struct mbuf *,
196					u_int32_t *);
197static void sk_start(struct ifnet *);
198static int sk_ioctl(struct ifnet *, u_long, caddr_t);
199static void sk_init(void *);
200static void sk_init_xmac(struct sk_if_softc *);
201static void sk_init_yukon(struct sk_if_softc *);
202static void sk_stop(struct sk_if_softc *);
203static void sk_watchdog(struct ifnet *);
204static int sk_ifmedia_upd(struct ifnet *);
205static void sk_ifmedia_sts(struct ifnet *, struct ifmediareq *);
206static void sk_reset(struct sk_softc *);
207static int sk_newbuf(struct sk_if_softc *,
208					struct sk_chain *, struct mbuf *);
209static int sk_alloc_jumbo_mem(struct sk_if_softc *);
210static void *sk_jalloc(struct sk_if_softc *);
211static void sk_jfree(void *, void *);
212static int sk_init_rx_ring(struct sk_if_softc *);
213static void sk_init_tx_ring(struct sk_if_softc *);
214static u_int32_t sk_win_read_4(struct sk_softc *, int);
215static u_int16_t sk_win_read_2(struct sk_softc *, int);
216static u_int8_t sk_win_read_1(struct sk_softc *, int);
217static void sk_win_write_4(struct sk_softc *, int, u_int32_t);
218static void sk_win_write_2(struct sk_softc *, int, u_int32_t);
219static void sk_win_write_1(struct sk_softc *, int, u_int32_t);
220static u_int8_t sk_vpd_readbyte(struct sk_softc *, int);
221static void sk_vpd_read_res(struct sk_softc *, struct vpd_res *, int);
222static void sk_vpd_read(struct sk_softc *);
223
224static int sk_miibus_readreg(device_t, int, int);
225static int sk_miibus_writereg(device_t, int, int, int);
226static void sk_miibus_statchg(device_t);
227
228static int sk_xmac_miibus_readreg(struct sk_if_softc *, int, int);
229static int sk_xmac_miibus_writereg(struct sk_if_softc *, int, int,
230						int);
231static void sk_xmac_miibus_statchg(struct sk_if_softc *);
232
233static int sk_marv_miibus_readreg(struct sk_if_softc *, int, int);
234static int sk_marv_miibus_writereg(struct sk_if_softc *, int, int,
235						int);
236static void sk_marv_miibus_statchg(struct sk_if_softc *);
237
238static uint32_t sk_xmchash(const uint8_t *);
239static uint32_t sk_gmchash(const uint8_t *);
240static void sk_setfilt(struct sk_if_softc *, caddr_t, int);
241static void sk_setmulti(struct sk_if_softc *);
242static void sk_setpromisc(struct sk_if_softc *);
243
244#ifdef SK_USEIOSPACE
245#define SK_RES		SYS_RES_IOPORT
246#define SK_RID		SK_PCI_LOIO
247#else
248#define SK_RES		SYS_RES_MEMORY
249#define SK_RID		SK_PCI_LOMEM
250#endif
251
252/*
253 * Note that we have newbus methods for both the GEnesis controller
254 * itself and the XMAC(s). The XMACs are children of the GEnesis, and
255 * the miibus code is a child of the XMACs. We need to do it this way
256 * so that the miibus drivers can access the PHY registers on the
257 * right PHY. It's not quite what I had in mind, but it's the only
258 * design that achieves the desired effect.
259 */
260static device_method_t skc_methods[] = {
261	/* Device interface */
262	DEVMETHOD(device_probe,		skc_probe),
263	DEVMETHOD(device_attach,	skc_attach),
264	DEVMETHOD(device_detach,	skc_detach),
265	DEVMETHOD(device_shutdown,	skc_shutdown),
266
267	/* bus interface */
268	DEVMETHOD(bus_print_child,	bus_generic_print_child),
269	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
270
271	{ 0, 0 }
272};
273
274static driver_t skc_driver = {
275	"skc",
276	skc_methods,
277	sizeof(struct sk_softc)
278};
279
280static devclass_t skc_devclass;
281
282static device_method_t sk_methods[] = {
283	/* Device interface */
284	DEVMETHOD(device_probe,		sk_probe),
285	DEVMETHOD(device_attach,	sk_attach),
286	DEVMETHOD(device_detach,	sk_detach),
287	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
288
289	/* bus interface */
290	DEVMETHOD(bus_print_child,	bus_generic_print_child),
291	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
292
293	/* MII interface */
294	DEVMETHOD(miibus_readreg,	sk_miibus_readreg),
295	DEVMETHOD(miibus_writereg,	sk_miibus_writereg),
296	DEVMETHOD(miibus_statchg,	sk_miibus_statchg),
297
298	{ 0, 0 }
299};
300
301static driver_t sk_driver = {
302	"sk",
303	sk_methods,
304	sizeof(struct sk_if_softc)
305};
306
307static devclass_t sk_devclass;
308
309DRIVER_MODULE(sk, pci, skc_driver, skc_devclass, 0, 0);
310DRIVER_MODULE(sk, skc, sk_driver, sk_devclass, 0, 0);
311DRIVER_MODULE(miibus, sk, miibus_driver, miibus_devclass, 0, 0);
312
313#define SK_SETBIT(sc, reg, x)		\
314	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
315
316#define SK_CLRBIT(sc, reg, x)		\
317	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
318
319#define SK_WIN_SETBIT_4(sc, reg, x)	\
320	sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) | x)
321
322#define SK_WIN_CLRBIT_4(sc, reg, x)	\
323	sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) & ~x)
324
325#define SK_WIN_SETBIT_2(sc, reg, x)	\
326	sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) | x)
327
328#define SK_WIN_CLRBIT_2(sc, reg, x)	\
329	sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) & ~x)
330
331static u_int32_t
332sk_win_read_4(sc, reg)
333	struct sk_softc		*sc;
334	int			reg;
335{
336#ifdef SK_USEIOSPACE
337	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
338	return(CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg)));
339#else
340	return(CSR_READ_4(sc, reg));
341#endif
342}
343
344static u_int16_t
345sk_win_read_2(sc, reg)
346	struct sk_softc		*sc;
347	int			reg;
348{
349#ifdef SK_USEIOSPACE
350	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
351	return(CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg)));
352#else
353	return(CSR_READ_2(sc, reg));
354#endif
355}
356
357static u_int8_t
358sk_win_read_1(sc, reg)
359	struct sk_softc		*sc;
360	int			reg;
361{
362#ifdef SK_USEIOSPACE
363	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
364	return(CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg)));
365#else
366	return(CSR_READ_1(sc, reg));
367#endif
368}
369
370static void
371sk_win_write_4(sc, reg, val)
372	struct sk_softc		*sc;
373	int			reg;
374	u_int32_t		val;
375{
376#ifdef SK_USEIOSPACE
377	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
378	CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), val);
379#else
380	CSR_WRITE_4(sc, reg, val);
381#endif
382	return;
383}
384
385static void
386sk_win_write_2(sc, reg, val)
387	struct sk_softc		*sc;
388	int			reg;
389	u_int32_t		val;
390{
391#ifdef SK_USEIOSPACE
392	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
393	CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), val);
394#else
395	CSR_WRITE_2(sc, reg, val);
396#endif
397	return;
398}
399
400static void
401sk_win_write_1(sc, reg, val)
402	struct sk_softc		*sc;
403	int			reg;
404	u_int32_t		val;
405{
406#ifdef SK_USEIOSPACE
407	CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
408	CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), val);
409#else
410	CSR_WRITE_1(sc, reg, val);
411#endif
412	return;
413}
414
415/*
416 * The VPD EEPROM contains Vital Product Data, as suggested in
417 * the PCI 2.1 specification. The VPD data is separared into areas
418 * denoted by resource IDs. The SysKonnect VPD contains an ID string
419 * resource (the name of the adapter), a read-only area resource
420 * containing various key/data fields and a read/write area which
421 * can be used to store asset management information or log messages.
422 * We read the ID string and read-only into buffers attached to
423 * the controller softc structure for later use. At the moment,
424 * we only use the ID string during skc_attach().
425 */
426static u_int8_t
427sk_vpd_readbyte(sc, addr)
428	struct sk_softc		*sc;
429	int			addr;
430{
431	int			i;
432
433	sk_win_write_2(sc, SK_PCI_REG(SK_PCI_VPD_ADDR), addr);
434	for (i = 0; i < SK_TIMEOUT; i++) {
435		DELAY(1);
436		if (sk_win_read_2(sc,
437		    SK_PCI_REG(SK_PCI_VPD_ADDR)) & SK_VPD_FLAG)
438			break;
439	}
440
441	if (i == SK_TIMEOUT)
442		return(0);
443
444	return(sk_win_read_1(sc, SK_PCI_REG(SK_PCI_VPD_DATA)));
445}
446
447static void
448sk_vpd_read_res(sc, res, addr)
449	struct sk_softc		*sc;
450	struct vpd_res		*res;
451	int			addr;
452{
453	int			i;
454	u_int8_t		*ptr;
455
456	ptr = (u_int8_t *)res;
457	for (i = 0; i < sizeof(struct vpd_res); i++)
458		ptr[i] = sk_vpd_readbyte(sc, i + addr);
459
460	return;
461}
462
463static void
464sk_vpd_read(sc)
465	struct sk_softc		*sc;
466{
467	int			pos = 0, i;
468	struct vpd_res		res;
469
470	if (sc->sk_vpd_prodname != NULL)
471		free(sc->sk_vpd_prodname, M_DEVBUF);
472	if (sc->sk_vpd_readonly != NULL)
473		free(sc->sk_vpd_readonly, M_DEVBUF);
474	sc->sk_vpd_prodname = NULL;
475	sc->sk_vpd_readonly = NULL;
476	sc->sk_vpd_readonly_len = 0;
477
478	sk_vpd_read_res(sc, &res, pos);
479
480	/*
481	 * Bail out quietly if the eeprom appears to be missing or empty.
482	 */
483	if (res.vr_id == 0xff && res.vr_len == 0xff && res.vr_pad == 0xff)
484		return;
485
486	if (res.vr_id != VPD_RES_ID) {
487		printf("skc%d: bad VPD resource id: expected %x got %x\n",
488		    sc->sk_unit, VPD_RES_ID, res.vr_id);
489		return;
490	}
491
492	pos += sizeof(res);
493	sc->sk_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
494	for (i = 0; i < res.vr_len; i++)
495		sc->sk_vpd_prodname[i] = sk_vpd_readbyte(sc, i + pos);
496	sc->sk_vpd_prodname[i] = '\0';
497	pos += i;
498
499	sk_vpd_read_res(sc, &res, pos);
500
501	if (res.vr_id != VPD_RES_READ) {
502		printf("skc%d: bad VPD resource id: expected %x got %x\n",
503		    sc->sk_unit, VPD_RES_READ, res.vr_id);
504		return;
505	}
506
507	pos += sizeof(res);
508	sc->sk_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
509	for (i = 0; i < res.vr_len; i++)
510		sc->sk_vpd_readonly[i] = sk_vpd_readbyte(sc, i + pos);
511	sc->sk_vpd_readonly_len = res.vr_len;
512
513	return;
514}
515
516static int
517sk_miibus_readreg(dev, phy, reg)
518	device_t		dev;
519	int			phy, reg;
520{
521	struct sk_if_softc	*sc_if;
522
523	sc_if = device_get_softc(dev);
524
525	switch(sc_if->sk_softc->sk_type) {
526	case SK_GENESIS:
527		return(sk_xmac_miibus_readreg(sc_if, phy, reg));
528	case SK_YUKON:
529		return(sk_marv_miibus_readreg(sc_if, phy, reg));
530	}
531
532	return(0);
533}
534
535static int
536sk_miibus_writereg(dev, phy, reg, val)
537	device_t		dev;
538	int			phy, reg, val;
539{
540	struct sk_if_softc	*sc_if;
541
542	sc_if = device_get_softc(dev);
543
544	switch(sc_if->sk_softc->sk_type) {
545	case SK_GENESIS:
546		return(sk_xmac_miibus_writereg(sc_if, phy, reg, val));
547	case SK_YUKON:
548		return(sk_marv_miibus_writereg(sc_if, phy, reg, val));
549	}
550
551	return(0);
552}
553
554static void
555sk_miibus_statchg(dev)
556	device_t		dev;
557{
558	struct sk_if_softc	*sc_if;
559
560	sc_if = device_get_softc(dev);
561
562	switch(sc_if->sk_softc->sk_type) {
563	case SK_GENESIS:
564		sk_xmac_miibus_statchg(sc_if);
565		break;
566	case SK_YUKON:
567		sk_marv_miibus_statchg(sc_if);
568		break;
569	}
570
571	return;
572}
573
574static int
575sk_xmac_miibus_readreg(sc_if, phy, reg)
576	struct sk_if_softc	*sc_if;
577	int			phy, reg;
578{
579	int			i;
580
581	if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0)
582		return(0);
583
584	SK_IF_LOCK(sc_if);
585	SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
586	SK_XM_READ_2(sc_if, XM_PHY_DATA);
587	if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
588		for (i = 0; i < SK_TIMEOUT; i++) {
589			DELAY(1);
590			if (SK_XM_READ_2(sc_if, XM_MMUCMD) &
591			    XM_MMUCMD_PHYDATARDY)
592				break;
593		}
594
595		if (i == SK_TIMEOUT) {
596			printf("sk%d: phy failed to come ready\n",
597			    sc_if->sk_unit);
598			SK_IF_UNLOCK(sc_if);
599			return(0);
600		}
601	}
602	DELAY(1);
603	i = SK_XM_READ_2(sc_if, XM_PHY_DATA);
604	SK_IF_UNLOCK(sc_if);
605	return(i);
606}
607
608static int
609sk_xmac_miibus_writereg(sc_if, phy, reg, val)
610	struct sk_if_softc	*sc_if;
611	int			phy, reg, val;
612{
613	int			i;
614
615	SK_IF_LOCK(sc_if);
616	SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
617	for (i = 0; i < SK_TIMEOUT; i++) {
618		if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
619			break;
620	}
621
622	if (i == SK_TIMEOUT) {
623		printf("sk%d: phy failed to come ready\n", sc_if->sk_unit);
624		SK_IF_UNLOCK(sc_if);
625		return(ETIMEDOUT);
626	}
627
628	SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val);
629	for (i = 0; i < SK_TIMEOUT; i++) {
630		DELAY(1);
631		if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
632			break;
633	}
634	SK_IF_UNLOCK(sc_if);
635	if (i == SK_TIMEOUT)
636		printf("sk%d: phy write timed out\n", sc_if->sk_unit);
637
638	return(0);
639}
640
641static void
642sk_xmac_miibus_statchg(sc_if)
643	struct sk_if_softc	*sc_if;
644{
645	struct mii_data		*mii;
646
647	mii = device_get_softc(sc_if->sk_miibus);
648
649	SK_IF_LOCK(sc_if);
650	/*
651	 * If this is a GMII PHY, manually set the XMAC's
652	 * duplex mode accordingly.
653	 */
654	if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
655		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
656			SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
657		} else {
658			SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
659		}
660	}
661	SK_IF_UNLOCK(sc_if);
662
663	return;
664}
665
666static int
667sk_marv_miibus_readreg(sc_if, phy, reg)
668	struct sk_if_softc	*sc_if;
669	int			phy, reg;
670{
671	u_int16_t		val;
672	int			i;
673
674	if (phy != 0 ||
675	    (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER &&
676	     sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) {
677		return(0);
678	}
679
680	SK_IF_LOCK(sc_if);
681        SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
682		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
683
684	for (i = 0; i < SK_TIMEOUT; i++) {
685		DELAY(1);
686		val = SK_YU_READ_2(sc_if, YUKON_SMICR);
687		if (val & YU_SMICR_READ_VALID)
688			break;
689	}
690
691	if (i == SK_TIMEOUT) {
692		printf("sk%d: phy failed to come ready\n",
693		    sc_if->sk_unit);
694		SK_IF_UNLOCK(sc_if);
695		return(0);
696	}
697
698	val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
699	SK_IF_UNLOCK(sc_if);
700
701	return(val);
702}
703
704static int
705sk_marv_miibus_writereg(sc_if, phy, reg, val)
706	struct sk_if_softc	*sc_if;
707	int			phy, reg, val;
708{
709	int			i;
710
711	SK_IF_LOCK(sc_if);
712	SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
713	SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
714		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
715
716	for (i = 0; i < SK_TIMEOUT; i++) {
717		DELAY(1);
718		if (SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY)
719			break;
720	}
721	SK_IF_UNLOCK(sc_if);
722
723	return(0);
724}
725
726static void
727sk_marv_miibus_statchg(sc_if)
728	struct sk_if_softc	*sc_if;
729{
730	return;
731}
732
733#define HASH_BITS		6
734
735static u_int32_t
736sk_xmchash(addr)
737	const uint8_t *addr;
738{
739	uint32_t crc;
740
741	/* Compute CRC for the address value. */
742	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
743
744	return (~crc & ((1 << HASH_BITS) - 1));
745}
746
747/* gmchash is just a big endian crc */
748static u_int32_t
749sk_gmchash(addr)
750	const uint8_t *addr;
751{
752	uint32_t crc;
753
754	/* Compute CRC for the address value. */
755	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
756
757	return (crc & ((1 << HASH_BITS) - 1));
758}
759
760static void
761sk_setfilt(sc_if, addr, slot)
762	struct sk_if_softc	*sc_if;
763	caddr_t			addr;
764	int			slot;
765{
766	int			base;
767
768	base = XM_RXFILT_ENTRY(slot);
769
770	SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
771	SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
772	SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
773
774	return;
775}
776
777static void
778sk_setmulti(sc_if)
779	struct sk_if_softc	*sc_if;
780{
781	struct sk_softc		*sc = sc_if->sk_softc;
782	struct ifnet		*ifp = &sc_if->arpcom.ac_if;
783	u_int32_t		hashes[2] = { 0, 0 };
784	int			h = 0, i;
785	struct ifmultiaddr	*ifma;
786	u_int8_t		dummy[] = { 0, 0, 0, 0, 0 ,0 };
787
788
789	/* First, zot all the existing filters. */
790	switch(sc->sk_type) {
791	case SK_GENESIS:
792		for (i = 1; i < XM_RXFILT_MAX; i++)
793			sk_setfilt(sc_if, (caddr_t)&dummy, i);
794
795		SK_XM_WRITE_4(sc_if, XM_MAR0, 0);
796		SK_XM_WRITE_4(sc_if, XM_MAR2, 0);
797		break;
798	case SK_YUKON:
799		SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
800		SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
801		SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
802		SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
803		break;
804	}
805
806	/* Now program new ones. */
807	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
808		hashes[0] = 0xFFFFFFFF;
809		hashes[1] = 0xFFFFFFFF;
810	} else {
811		i = 1;
812		TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) {
813			if (ifma->ifma_addr->sa_family != AF_LINK)
814				continue;
815			/*
816			 * Program the first XM_RXFILT_MAX multicast groups
817			 * into the perfect filter. For all others,
818			 * use the hash table.
819			 */
820			if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) {
821				sk_setfilt(sc_if,
822			LLADDR((struct sockaddr_dl *)ifma->ifma_addr), i);
823				i++;
824				continue;
825			}
826
827			switch(sc->sk_type) {
828			case SK_GENESIS:
829				h = sk_xmchash(
830					LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
831				break;
832			case SK_YUKON:
833				h = sk_gmchash(
834					LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
835				break;
836			}
837			if (h < 32)
838				hashes[0] |= (1 << h);
839			else
840				hashes[1] |= (1 << (h - 32));
841		}
842	}
843
844	switch(sc->sk_type) {
845	case SK_GENESIS:
846		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH|
847			       XM_MODE_RX_USE_PERFECT);
848		SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]);
849		SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]);
850		break;
851	case SK_YUKON:
852		SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
853		SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
854		SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
855		SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
856		break;
857	}
858
859	return;
860}
861
862static void
863sk_setpromisc(sc_if)
864	struct sk_if_softc	*sc_if;
865{
866	struct sk_softc		*sc = sc_if->sk_softc;
867	struct ifnet		*ifp = &sc_if->arpcom.ac_if;
868
869	switch(sc->sk_type) {
870	case SK_GENESIS:
871		if (ifp->if_flags & IFF_PROMISC) {
872			SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
873		} else {
874			SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
875		}
876		break;
877	case SK_YUKON:
878		if (ifp->if_flags & IFF_PROMISC) {
879			SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
880			    YU_RCR_UFLEN | YU_RCR_MUFLEN);
881		} else {
882			SK_YU_SETBIT_2(sc_if, YUKON_RCR,
883			    YU_RCR_UFLEN | YU_RCR_MUFLEN);
884		}
885		break;
886	}
887
888	return;
889}
890
891static int
892sk_init_rx_ring(sc_if)
893	struct sk_if_softc	*sc_if;
894{
895	struct sk_chain_data	*cd = &sc_if->sk_cdata;
896	struct sk_ring_data	*rd = sc_if->sk_rdata;
897	int			i;
898
899	bzero((char *)rd->sk_rx_ring,
900	    sizeof(struct sk_rx_desc) * SK_RX_RING_CNT);
901
902	for (i = 0; i < SK_RX_RING_CNT; i++) {
903		cd->sk_rx_chain[i].sk_desc = &rd->sk_rx_ring[i];
904		if (sk_newbuf(sc_if, &cd->sk_rx_chain[i], NULL) == ENOBUFS)
905			return(ENOBUFS);
906		if (i == (SK_RX_RING_CNT - 1)) {
907			cd->sk_rx_chain[i].sk_next =
908			    &cd->sk_rx_chain[0];
909			rd->sk_rx_ring[i].sk_next =
910			    vtophys(&rd->sk_rx_ring[0]);
911		} else {
912			cd->sk_rx_chain[i].sk_next =
913			    &cd->sk_rx_chain[i + 1];
914			rd->sk_rx_ring[i].sk_next =
915			    vtophys(&rd->sk_rx_ring[i + 1]);
916		}
917	}
918
919	sc_if->sk_cdata.sk_rx_prod = 0;
920	sc_if->sk_cdata.sk_rx_cons = 0;
921
922	return(0);
923}
924
925static void
926sk_init_tx_ring(sc_if)
927	struct sk_if_softc	*sc_if;
928{
929	struct sk_chain_data	*cd = &sc_if->sk_cdata;
930	struct sk_ring_data	*rd = sc_if->sk_rdata;
931	int			i;
932
933	bzero((char *)sc_if->sk_rdata->sk_tx_ring,
934	    sizeof(struct sk_tx_desc) * SK_TX_RING_CNT);
935
936	for (i = 0; i < SK_TX_RING_CNT; i++) {
937		cd->sk_tx_chain[i].sk_desc = &rd->sk_tx_ring[i];
938		if (i == (SK_TX_RING_CNT - 1)) {
939			cd->sk_tx_chain[i].sk_next =
940			    &cd->sk_tx_chain[0];
941			rd->sk_tx_ring[i].sk_next =
942			    vtophys(&rd->sk_tx_ring[0]);
943		} else {
944			cd->sk_tx_chain[i].sk_next =
945			    &cd->sk_tx_chain[i + 1];
946			rd->sk_tx_ring[i].sk_next =
947			    vtophys(&rd->sk_tx_ring[i + 1]);
948		}
949	}
950
951	sc_if->sk_cdata.sk_tx_prod = 0;
952	sc_if->sk_cdata.sk_tx_cons = 0;
953	sc_if->sk_cdata.sk_tx_cnt = 0;
954
955	return;
956}
957
958static int
959sk_newbuf(sc_if, c, m)
960	struct sk_if_softc	*sc_if;
961	struct sk_chain		*c;
962	struct mbuf		*m;
963{
964	struct mbuf		*m_new = NULL;
965	struct sk_rx_desc	*r;
966
967	if (m == NULL) {
968		caddr_t			*buf = NULL;
969
970		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
971		if (m_new == NULL)
972			return(ENOBUFS);
973
974		/* Allocate the jumbo buffer */
975		buf = sk_jalloc(sc_if);
976		if (buf == NULL) {
977			m_freem(m_new);
978#ifdef SK_VERBOSE
979			printf("sk%d: jumbo allocation failed "
980			    "-- packet dropped!\n", sc_if->sk_unit);
981#endif
982			return(ENOBUFS);
983		}
984
985		/* Attach the buffer to the mbuf */
986		MEXTADD(m_new, buf, SK_JLEN, sk_jfree,
987		    (struct sk_if_softc *)sc_if, 0, EXT_NET_DRV);
988		m_new->m_data = (void *)buf;
989		m_new->m_pkthdr.len = m_new->m_len = SK_JLEN;
990	} else {
991		/*
992	 	 * We're re-using a previously allocated mbuf;
993		 * be sure to re-init pointers and lengths to
994		 * default values.
995		 */
996		m_new = m;
997		m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
998		m_new->m_data = m_new->m_ext.ext_buf;
999	}
1000
1001	/*
1002	 * Adjust alignment so packet payload begins on a
1003	 * longword boundary. Mandatory for Alpha, useful on
1004	 * x86 too.
1005	 */
1006	m_adj(m_new, ETHER_ALIGN);
1007
1008	r = c->sk_desc;
1009	c->sk_mbuf = m_new;
1010	r->sk_data_lo = vtophys(mtod(m_new, caddr_t));
1011	r->sk_ctl = m_new->m_len | SK_RXSTAT;
1012
1013	return(0);
1014}
1015
1016/*
1017 * Allocate jumbo buffer storage. The SysKonnect adapters support
1018 * "jumbograms" (9K frames), although SysKonnect doesn't currently
1019 * use them in their drivers. In order for us to use them, we need
1020 * large 9K receive buffers, however standard mbuf clusters are only
1021 * 2048 bytes in size. Consequently, we need to allocate and manage
1022 * our own jumbo buffer pool. Fortunately, this does not require an
1023 * excessive amount of additional code.
1024 */
1025static int
1026sk_alloc_jumbo_mem(sc_if)
1027	struct sk_if_softc	*sc_if;
1028{
1029	caddr_t			ptr;
1030	register int		i;
1031	struct sk_jpool_entry   *entry;
1032
1033	/* Grab a big chunk o' storage. */
1034	sc_if->sk_cdata.sk_jumbo_buf = contigmalloc(SK_JMEM, M_DEVBUF,
1035	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1036
1037	if (sc_if->sk_cdata.sk_jumbo_buf == NULL) {
1038		printf("sk%d: no memory for jumbo buffers!\n", sc_if->sk_unit);
1039		return(ENOBUFS);
1040	}
1041
1042	SLIST_INIT(&sc_if->sk_jfree_listhead);
1043	SLIST_INIT(&sc_if->sk_jinuse_listhead);
1044
1045	/*
1046	 * Now divide it up into 9K pieces and save the addresses
1047	 * in an array.
1048	 */
1049	ptr = sc_if->sk_cdata.sk_jumbo_buf;
1050	for (i = 0; i < SK_JSLOTS; i++) {
1051		sc_if->sk_cdata.sk_jslots[i] = ptr;
1052		ptr += SK_JLEN;
1053		entry = malloc(sizeof(struct sk_jpool_entry),
1054		    M_DEVBUF, M_NOWAIT);
1055		if (entry == NULL) {
1056			free(sc_if->sk_cdata.sk_jumbo_buf, M_DEVBUF);
1057			sc_if->sk_cdata.sk_jumbo_buf = NULL;
1058			printf("sk%d: no memory for jumbo "
1059			    "buffer queue!\n", sc_if->sk_unit);
1060			return(ENOBUFS);
1061		}
1062		entry->slot = i;
1063		SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
1064		    entry, jpool_entries);
1065	}
1066
1067	return(0);
1068}
1069
1070/*
1071 * Allocate a jumbo buffer.
1072 */
1073static void *
1074sk_jalloc(sc_if)
1075	struct sk_if_softc	*sc_if;
1076{
1077	struct sk_jpool_entry   *entry;
1078
1079	SK_IF_LOCK_ASSERT(sc_if);
1080
1081	entry = SLIST_FIRST(&sc_if->sk_jfree_listhead);
1082
1083	if (entry == NULL) {
1084#ifdef SK_VERBOSE
1085		printf("sk%d: no free jumbo buffers\n", sc_if->sk_unit);
1086#endif
1087		return(NULL);
1088	}
1089
1090	SLIST_REMOVE_HEAD(&sc_if->sk_jfree_listhead, jpool_entries);
1091	SLIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
1092	return(sc_if->sk_cdata.sk_jslots[entry->slot]);
1093}
1094
1095/*
1096 * Release a jumbo buffer.
1097 */
1098static void
1099sk_jfree(buf, args)
1100	void			*buf;
1101	void			*args;
1102{
1103	struct sk_if_softc	*sc_if;
1104	int		        i;
1105	struct sk_jpool_entry   *entry;
1106
1107	/* Extract the softc struct pointer. */
1108	sc_if = (struct sk_if_softc *)args;
1109	if (sc_if == NULL)
1110		panic("sk_jfree: didn't get softc pointer!");
1111
1112	SK_IF_LOCK(sc_if);
1113
1114	/* calculate the slot this buffer belongs to */
1115	i = ((vm_offset_t)buf
1116	     - (vm_offset_t)sc_if->sk_cdata.sk_jumbo_buf) / SK_JLEN;
1117
1118	if ((i < 0) || (i >= SK_JSLOTS))
1119		panic("sk_jfree: asked to free buffer that we don't manage!");
1120
1121	entry = SLIST_FIRST(&sc_if->sk_jinuse_listhead);
1122	if (entry == NULL)
1123		panic("sk_jfree: buffer not in use!");
1124	entry->slot = i;
1125	SLIST_REMOVE_HEAD(&sc_if->sk_jinuse_listhead, jpool_entries);
1126	SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead, entry, jpool_entries);
1127
1128	SK_IF_UNLOCK(sc_if);
1129	return;
1130}
1131
1132/*
1133 * Set media options.
1134 */
1135static int
1136sk_ifmedia_upd(ifp)
1137	struct ifnet		*ifp;
1138{
1139	struct sk_if_softc	*sc_if = ifp->if_softc;
1140	struct mii_data		*mii;
1141
1142	mii = device_get_softc(sc_if->sk_miibus);
1143	sk_init(sc_if);
1144	mii_mediachg(mii);
1145
1146	return(0);
1147}
1148
1149/*
1150 * Report current media status.
1151 */
1152static void
1153sk_ifmedia_sts(ifp, ifmr)
1154	struct ifnet		*ifp;
1155	struct ifmediareq	*ifmr;
1156{
1157	struct sk_if_softc	*sc_if;
1158	struct mii_data		*mii;
1159
1160	sc_if = ifp->if_softc;
1161	mii = device_get_softc(sc_if->sk_miibus);
1162
1163	mii_pollstat(mii);
1164	ifmr->ifm_active = mii->mii_media_active;
1165	ifmr->ifm_status = mii->mii_media_status;
1166
1167	return;
1168}
1169
1170static int
1171sk_ioctl(ifp, command, data)
1172	struct ifnet		*ifp;
1173	u_long			command;
1174	caddr_t			data;
1175{
1176	struct sk_if_softc	*sc_if = ifp->if_softc;
1177	struct ifreq		*ifr = (struct ifreq *) data;
1178	int			error = 0;
1179	struct mii_data		*mii;
1180
1181	switch(command) {
1182	case SIOCSIFMTU:
1183		if (ifr->ifr_mtu > SK_JUMBO_MTU)
1184			error = EINVAL;
1185		else {
1186			ifp->if_mtu = ifr->ifr_mtu;
1187			ifp->if_flags &= ~IFF_RUNNING;
1188			sk_init(sc_if);
1189		}
1190		break;
1191	case SIOCSIFFLAGS:
1192		SK_IF_LOCK(sc_if);
1193		if (ifp->if_flags & IFF_UP) {
1194			if (ifp->if_flags & IFF_RUNNING) {
1195				if ((ifp->if_flags ^ sc_if->sk_if_flags)
1196				    & IFF_PROMISC) {
1197					sk_setpromisc(sc_if);
1198					sk_setmulti(sc_if);
1199				}
1200			} else
1201				sk_init(sc_if);
1202		} else {
1203			if (ifp->if_flags & IFF_RUNNING)
1204				sk_stop(sc_if);
1205		}
1206		sc_if->sk_if_flags = ifp->if_flags;
1207		SK_IF_UNLOCK(sc_if);
1208		error = 0;
1209		break;
1210	case SIOCADDMULTI:
1211	case SIOCDELMULTI:
1212		if (ifp->if_flags & IFF_RUNNING) {
1213			SK_IF_LOCK(sc_if);
1214			sk_setmulti(sc_if);
1215			SK_IF_UNLOCK(sc_if);
1216			error = 0;
1217		}
1218		break;
1219	case SIOCGIFMEDIA:
1220	case SIOCSIFMEDIA:
1221		mii = device_get_softc(sc_if->sk_miibus);
1222		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1223		break;
1224	default:
1225		error = ether_ioctl(ifp, command, data);
1226		break;
1227	}
1228
1229	return(error);
1230}
1231
1232/*
1233 * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device
1234 * IDs against our list and return a device name if we find a match.
1235 */
1236static int
1237skc_probe(dev)
1238	device_t		dev;
1239{
1240	struct sk_softc		*sc;
1241	struct sk_type		*t = sk_devs;
1242
1243	sc = device_get_softc(dev);
1244
1245	while(t->sk_name != NULL) {
1246		if ((pci_get_vendor(dev) == t->sk_vid) &&
1247		    (pci_get_device(dev) == t->sk_did)) {
1248			device_set_desc(dev, t->sk_name);
1249			return (BUS_PROBE_DEFAULT);
1250		}
1251		t++;
1252	}
1253
1254	return(ENXIO);
1255}
1256
1257/*
1258 * Force the GEnesis into reset, then bring it out of reset.
1259 */
1260static void
1261sk_reset(sc)
1262	struct sk_softc		*sc;
1263{
1264	CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET);
1265	CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET);
1266	if (sc->sk_type == SK_YUKON)
1267		CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
1268
1269	DELAY(1000);
1270	CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET);
1271	DELAY(2);
1272	CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
1273	if (sc->sk_type == SK_YUKON)
1274		CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
1275
1276	if (sc->sk_type == SK_GENESIS) {
1277		/* Configure packet arbiter */
1278		sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET);
1279		sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT);
1280		sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT);
1281		sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT);
1282		sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT);
1283	}
1284
1285	/* Enable RAM interface */
1286	sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
1287
1288	/*
1289         * Configure interrupt moderation. The moderation timer
1290	 * defers interrupts specified in the interrupt moderation
1291	 * timer mask based on the timeout specified in the interrupt
1292	 * moderation timer init register. Each bit in the timer
1293	 * register represents 18.825ns, so to specify a timeout in
1294	 * microseconds, we have to multiply by 54.
1295	 */
1296	sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(200));
1297	sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
1298	    SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
1299	sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
1300
1301	return;
1302}
1303
1304static int
1305sk_probe(dev)
1306	device_t		dev;
1307{
1308	struct sk_softc		*sc;
1309
1310	sc = device_get_softc(device_get_parent(dev));
1311
1312	/*
1313	 * Not much to do here. We always know there will be
1314	 * at least one XMAC present, and if there are two,
1315	 * skc_attach() will create a second device instance
1316	 * for us.
1317	 */
1318	switch (sc->sk_type) {
1319	case SK_GENESIS:
1320		device_set_desc(dev, "XaQti Corp. XMAC II");
1321		break;
1322	case SK_YUKON:
1323		device_set_desc(dev, "Marvell Semiconductor, Inc. Yukon");
1324		break;
1325	}
1326
1327	return (BUS_PROBE_DEFAULT);
1328}
1329
1330/*
1331 * Each XMAC chip is attached as a separate logical IP interface.
1332 * Single port cards will have only one logical interface of course.
1333 */
1334static int
1335sk_attach(dev)
1336	device_t		dev;
1337{
1338	struct sk_softc		*sc;
1339	struct sk_if_softc	*sc_if;
1340	struct ifnet		*ifp;
1341	int			i, port, error;
1342
1343	if (dev == NULL)
1344		return(EINVAL);
1345
1346	error = 0;
1347	sc_if = device_get_softc(dev);
1348	sc = device_get_softc(device_get_parent(dev));
1349	port = *(int *)device_get_ivars(dev);
1350	free(device_get_ivars(dev), M_DEVBUF);
1351	device_set_ivars(dev, NULL);
1352
1353	sc_if->sk_dev = dev;
1354	sc_if->sk_unit = device_get_unit(dev);
1355	sc_if->sk_port = port;
1356	sc_if->sk_softc = sc;
1357	sc->sk_if[port] = sc_if;
1358	if (port == SK_PORT_A)
1359		sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0;
1360	if (port == SK_PORT_B)
1361		sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1;
1362
1363	/* Allocate the descriptor queues. */
1364	sc_if->sk_rdata = contigmalloc(sizeof(struct sk_ring_data), M_DEVBUF,
1365	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1366
1367	if (sc_if->sk_rdata == NULL) {
1368		printf("sk%d: no memory for list buffers!\n", sc_if->sk_unit);
1369		error = ENOMEM;
1370		goto fail;
1371	}
1372
1373	bzero(sc_if->sk_rdata, sizeof(struct sk_ring_data));
1374
1375	/* Try to allocate memory for jumbo buffers. */
1376	if (sk_alloc_jumbo_mem(sc_if)) {
1377		printf("sk%d: jumbo buffer allocation failed\n",
1378		    sc_if->sk_unit);
1379		error = ENOMEM;
1380		goto fail;
1381	}
1382
1383	ifp = &sc_if->arpcom.ac_if;
1384	ifp->if_softc = sc_if;
1385	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1386	ifp->if_mtu = ETHERMTU;
1387	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1388	ifp->if_ioctl = sk_ioctl;
1389	ifp->if_start = sk_start;
1390	ifp->if_watchdog = sk_watchdog;
1391	ifp->if_init = sk_init;
1392	ifp->if_baudrate = 1000000000;
1393	IFQ_SET_MAXLEN(&ifp->if_snd, SK_TX_RING_CNT - 1);
1394	ifp->if_snd.ifq_drv_maxlen = SK_TX_RING_CNT - 1;
1395	IFQ_SET_READY(&ifp->if_snd);
1396
1397	callout_handle_init(&sc_if->sk_tick_ch);
1398
1399	/*
1400	 * Get station address for this interface. Note that
1401	 * dual port cards actually come with three station
1402	 * addresses: one for each port, plus an extra. The
1403	 * extra one is used by the SysKonnect driver software
1404	 * as a 'virtual' station address for when both ports
1405	 * are operating in failover mode. Currently we don't
1406	 * use this extra address.
1407	 */
1408	SK_LOCK(sc);
1409	for (i = 0; i < ETHER_ADDR_LEN; i++)
1410		sc_if->arpcom.ac_enaddr[i] =
1411		    sk_win_read_1(sc, SK_MAC0_0 + (port * 8) + i);
1412
1413	/*
1414	 * Set up RAM buffer addresses. The NIC will have a certain
1415	 * amount of SRAM on it, somewhere between 512K and 2MB. We
1416	 * need to divide this up a) between the transmitter and
1417 	 * receiver and b) between the two XMACs, if this is a
1418	 * dual port NIC. Our algotithm is to divide up the memory
1419	 * evenly so that everyone gets a fair share.
1420	 */
1421	if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) {
1422		u_int32_t		chunk, val;
1423
1424		chunk = sc->sk_ramsize / 2;
1425		val = sc->sk_rboff / sizeof(u_int64_t);
1426		sc_if->sk_rx_ramstart = val;
1427		val += (chunk / sizeof(u_int64_t));
1428		sc_if->sk_rx_ramend = val - 1;
1429		sc_if->sk_tx_ramstart = val;
1430		val += (chunk / sizeof(u_int64_t));
1431		sc_if->sk_tx_ramend = val - 1;
1432	} else {
1433		u_int32_t		chunk, val;
1434
1435		chunk = sc->sk_ramsize / 4;
1436		val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) /
1437		    sizeof(u_int64_t);
1438		sc_if->sk_rx_ramstart = val;
1439		val += (chunk / sizeof(u_int64_t));
1440		sc_if->sk_rx_ramend = val - 1;
1441		sc_if->sk_tx_ramstart = val;
1442		val += (chunk / sizeof(u_int64_t));
1443		sc_if->sk_tx_ramend = val - 1;
1444	}
1445
1446	/* Read and save PHY type and set PHY address */
1447	sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF;
1448	switch(sc_if->sk_phytype) {
1449	case SK_PHYTYPE_XMAC:
1450		sc_if->sk_phyaddr = SK_PHYADDR_XMAC;
1451		break;
1452	case SK_PHYTYPE_BCOM:
1453		sc_if->sk_phyaddr = SK_PHYADDR_BCOM;
1454		break;
1455	case SK_PHYTYPE_MARV_COPPER:
1456		sc_if->sk_phyaddr = SK_PHYADDR_MARV;
1457		break;
1458	default:
1459		printf("skc%d: unsupported PHY type: %d\n",
1460		    sc->sk_unit, sc_if->sk_phytype);
1461		error = ENODEV;
1462		SK_UNLOCK(sc);
1463		goto fail;
1464	}
1465
1466
1467	/*
1468	 * Call MI attach routine.  Can't hold locks when calling into ether_*.
1469	 */
1470	SK_UNLOCK(sc);
1471	ether_ifattach(ifp, sc_if->arpcom.ac_enaddr);
1472	SK_LOCK(sc);
1473
1474	/*
1475	 * Do miibus setup.
1476	 */
1477	switch (sc->sk_type) {
1478	case SK_GENESIS:
1479		sk_init_xmac(sc_if);
1480		break;
1481	case SK_YUKON:
1482		sk_init_yukon(sc_if);
1483		break;
1484	}
1485
1486	SK_UNLOCK(sc);
1487	if (mii_phy_probe(dev, &sc_if->sk_miibus,
1488	    sk_ifmedia_upd, sk_ifmedia_sts)) {
1489		printf("skc%d: no PHY found!\n", sc_if->sk_unit);
1490		ether_ifdetach(ifp);
1491		error = ENXIO;
1492		goto fail;
1493	}
1494
1495fail:
1496	if (error) {
1497		/* Access should be ok even though lock has been dropped */
1498		sc->sk_if[port] = NULL;
1499		sk_detach(dev);
1500	}
1501
1502	return(error);
1503}
1504
1505/*
1506 * Attach the interface. Allocate softc structures, do ifmedia
1507 * setup and ethernet/BPF attach.
1508 */
1509static int
1510skc_attach(dev)
1511	device_t		dev;
1512{
1513	struct sk_softc		*sc;
1514	int			unit, error = 0, rid, *port;
1515	uint8_t			skrs;
1516
1517	sc = device_get_softc(dev);
1518	unit = device_get_unit(dev);
1519
1520	mtx_init(&sc->sk_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1521	    MTX_DEF | MTX_RECURSE);
1522	/*
1523	 * Map control/status registers.
1524	 */
1525	pci_enable_busmaster(dev);
1526
1527	rid = SK_RID;
1528	sc->sk_res = bus_alloc_resource_any(dev, SK_RES, &rid, RF_ACTIVE);
1529
1530	if (sc->sk_res == NULL) {
1531		printf("sk%d: couldn't map ports/memory\n", unit);
1532		error = ENXIO;
1533		goto fail;
1534	}
1535
1536	sc->sk_btag = rman_get_bustag(sc->sk_res);
1537	sc->sk_bhandle = rman_get_bushandle(sc->sk_res);
1538
1539	/* Allocate interrupt */
1540	rid = 0;
1541	sc->sk_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1542	    RF_SHAREABLE | RF_ACTIVE);
1543
1544	if (sc->sk_irq == NULL) {
1545		printf("skc%d: couldn't map interrupt\n", unit);
1546		error = ENXIO;
1547		goto fail;
1548	}
1549
1550	/* Set adapter type */
1551	switch (pci_get_device(dev)) {
1552	case DEVICEID_SK_V1:
1553		sc->sk_type = SK_GENESIS;
1554		break;
1555	case DEVICEID_SK_V2:
1556	case DEVICEID_BELKIN_5005:
1557	case DEVICEID_3COM_3C940:
1558	case DEVICEID_LINKSYS_EG1032:
1559	case DEVICEID_DLINK_DGE530T:
1560		sc->sk_type = SK_YUKON;
1561		break;
1562	default:
1563		printf("skc%d: unknown device!\n", unit);
1564		error = ENXIO;
1565		goto fail;
1566	}
1567
1568	/* Reset the adapter. */
1569	sk_reset(sc);
1570
1571	sc->sk_unit = unit;
1572
1573	/* Read and save vital product data from EEPROM. */
1574	sk_vpd_read(sc);
1575
1576	skrs = sk_win_read_1(sc, SK_EPROM0);
1577	if (sc->sk_type == SK_GENESIS) {
1578		/* Read and save RAM size and RAMbuffer offset */
1579		switch(skrs) {
1580		case SK_RAMSIZE_512K_64:
1581			sc->sk_ramsize = 0x80000;
1582			sc->sk_rboff = SK_RBOFF_0;
1583			break;
1584		case SK_RAMSIZE_1024K_64:
1585			sc->sk_ramsize = 0x100000;
1586			sc->sk_rboff = SK_RBOFF_80000;
1587			break;
1588		case SK_RAMSIZE_1024K_128:
1589			sc->sk_ramsize = 0x100000;
1590			sc->sk_rboff = SK_RBOFF_0;
1591			break;
1592		case SK_RAMSIZE_2048K_128:
1593			sc->sk_ramsize = 0x200000;
1594			sc->sk_rboff = SK_RBOFF_0;
1595			break;
1596		default:
1597			printf("skc%d: unknown ram size: %d\n",
1598			    sc->sk_unit, sk_win_read_1(sc, SK_EPROM0));
1599			error = ENXIO;
1600			goto fail;
1601		}
1602	} else { /* SK_YUKON */
1603		if (skrs == 0x00)
1604			sc->sk_ramsize = 0x20000;
1605		else
1606			sc->sk_ramsize = skrs * (1<<12);
1607		sc->sk_rboff = SK_RBOFF_0;
1608	}
1609
1610	/* Read and save physical media type */
1611	switch(sk_win_read_1(sc, SK_PMDTYPE)) {
1612	case SK_PMD_1000BASESX:
1613		sc->sk_pmd = IFM_1000_SX;
1614		break;
1615	case SK_PMD_1000BASELX:
1616		sc->sk_pmd = IFM_1000_LX;
1617		break;
1618	case SK_PMD_1000BASECX:
1619		sc->sk_pmd = IFM_1000_CX;
1620		break;
1621	case SK_PMD_1000BASETX:
1622		sc->sk_pmd = IFM_1000_T;
1623		break;
1624	default:
1625		printf("skc%d: unknown media type: 0x%x\n",
1626		    sc->sk_unit, sk_win_read_1(sc, SK_PMDTYPE));
1627		error = ENXIO;
1628		goto fail;
1629	}
1630
1631	/* Announce the product name and more VPD data if there. */
1632	if (sc->sk_vpd_prodname != NULL)
1633		printf("skc%d: %s\n", sc->sk_unit, sc->sk_vpd_prodname);
1634
1635	if (bootverbose) {
1636		if (sc->sk_vpd_readonly != NULL &&
1637		    sc->sk_vpd_readonly_len != 0) {
1638			char buf[256];
1639			char *dp = sc->sk_vpd_readonly;
1640			uint16_t l, len = sc->sk_vpd_readonly_len;
1641
1642			while (len >= 3) {
1643				if ((*dp == 'P' && *(dp+1) == 'N') ||
1644				    (*dp == 'E' && *(dp+1) == 'C') ||
1645				    (*dp == 'M' && *(dp+1) == 'N') ||
1646				    (*dp == 'S' && *(dp+1) == 'N')) {
1647					l = 0;
1648					while (l < *(dp+2)) {
1649						buf[l] = *(dp+3+l);
1650						++l;
1651					}
1652					buf[l] = '\0';
1653					device_printf(dev, "%c%c: %s\n",
1654					    *dp, *(dp+1), buf);
1655					len -= (3 + l);
1656					dp += (3 + l);
1657				} else {
1658					len -= (3 + *(dp+2));
1659					dp += (3 + *(dp+2));
1660				}
1661			}
1662		}
1663		device_printf(dev, "type = %s\n",
1664		    (sc->sk_type == SK_GENESIS) ? "GENESIS" : "YUKON");
1665		device_printf(dev, "SK_EPROM0 = 0x%02x\n", skrs);
1666		device_printf(dev, "SRAM size = 0x%06x\n", sc->sk_ramsize);
1667		device_printf(dev, "chip ver  0x%02x\n",
1668		    sk_win_read_1(sc, SK_CHIPVER));
1669		device_printf(dev, "chip conf 0x%02x\n",
1670		    sk_win_read_1(sc, SK_CONFIG));
1671	}
1672
1673	sc->sk_devs[SK_PORT_A] = device_add_child(dev, "sk", -1);
1674	port = malloc(sizeof(int), M_DEVBUF, M_NOWAIT);
1675	*port = SK_PORT_A;
1676	device_set_ivars(sc->sk_devs[SK_PORT_A], port);
1677
1678	if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
1679		sc->sk_devs[SK_PORT_B] = device_add_child(dev, "sk", -1);
1680		port = malloc(sizeof(int), M_DEVBUF, M_NOWAIT);
1681		*port = SK_PORT_B;
1682		device_set_ivars(sc->sk_devs[SK_PORT_B], port);
1683	}
1684
1685	/* Turn on the 'driver is loaded' LED. */
1686	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1687
1688	bus_generic_attach(dev);
1689
1690	/* Hook interrupt last to avoid having to lock softc */
1691	error = bus_setup_intr(dev, sc->sk_irq, INTR_TYPE_NET|INTR_MPSAFE,
1692	    sk_intr, sc, &sc->sk_intrhand);
1693
1694	if (error) {
1695		printf("skc%d: couldn't set up irq\n", unit);
1696		goto fail;
1697	}
1698
1699fail:
1700	if (error)
1701		skc_detach(dev);
1702
1703	return(error);
1704}
1705
1706/*
1707 * Shutdown hardware and free up resources. This can be called any
1708 * time after the mutex has been initialized. It is called in both
1709 * the error case in attach and the normal detach case so it needs
1710 * to be careful about only freeing resources that have actually been
1711 * allocated.
1712 */
1713static int
1714sk_detach(dev)
1715	device_t		dev;
1716{
1717	struct sk_if_softc	*sc_if;
1718	struct ifnet		*ifp;
1719
1720	sc_if = device_get_softc(dev);
1721	KASSERT(mtx_initialized(&sc_if->sk_softc->sk_mtx),
1722	    ("sk mutex not initialized in sk_detach"));
1723	SK_IF_LOCK(sc_if);
1724
1725	ifp = &sc_if->arpcom.ac_if;
1726	/* These should only be active if attach_xmac succeeded */
1727	if (device_is_attached(dev)) {
1728		sk_stop(sc_if);
1729		/* Can't hold locks while calling detach */
1730		SK_IF_UNLOCK(sc_if);
1731		ether_ifdetach(ifp);
1732		SK_IF_LOCK(sc_if);
1733	}
1734	/*
1735	 * We're generally called from skc_detach() which is using
1736	 * device_delete_child() to get to here. It's already trashed
1737	 * miibus for us, so don't do it here or we'll panic.
1738	 */
1739	/*
1740	if (sc_if->sk_miibus != NULL)
1741		device_delete_child(dev, sc_if->sk_miibus);
1742	*/
1743	bus_generic_detach(dev);
1744	if (sc_if->sk_cdata.sk_jumbo_buf != NULL)
1745		contigfree(sc_if->sk_cdata.sk_jumbo_buf, SK_JMEM, M_DEVBUF);
1746	if (sc_if->sk_rdata != NULL) {
1747		contigfree(sc_if->sk_rdata, sizeof(struct sk_ring_data),
1748		    M_DEVBUF);
1749	}
1750	SK_IF_UNLOCK(sc_if);
1751
1752	return(0);
1753}
1754
1755static int
1756skc_detach(dev)
1757	device_t		dev;
1758{
1759	struct sk_softc		*sc;
1760
1761	sc = device_get_softc(dev);
1762	KASSERT(mtx_initialized(&sc->sk_mtx), ("sk mutex not initialized"));
1763
1764	if (device_is_alive(dev)) {
1765		if (sc->sk_devs[SK_PORT_A] != NULL)
1766			device_delete_child(dev, sc->sk_devs[SK_PORT_A]);
1767		if (sc->sk_devs[SK_PORT_B] != NULL)
1768			device_delete_child(dev, sc->sk_devs[SK_PORT_B]);
1769		bus_generic_detach(dev);
1770	}
1771
1772	if (sc->sk_intrhand)
1773		bus_teardown_intr(dev, sc->sk_irq, sc->sk_intrhand);
1774	if (sc->sk_irq)
1775		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sk_irq);
1776	if (sc->sk_res)
1777		bus_release_resource(dev, SK_RES, SK_RID, sc->sk_res);
1778
1779	mtx_destroy(&sc->sk_mtx);
1780
1781	return(0);
1782}
1783
1784static int
1785sk_encap(sc_if, m_head, txidx)
1786        struct sk_if_softc	*sc_if;
1787        struct mbuf		*m_head;
1788        u_int32_t		*txidx;
1789{
1790	struct sk_tx_desc	*f = NULL;
1791	struct mbuf		*m;
1792	u_int32_t		frag, cur, cnt = 0;
1793
1794	SK_IF_LOCK_ASSERT(sc_if);
1795
1796	m = m_head;
1797	cur = frag = *txidx;
1798
1799	/*
1800	 * Start packing the mbufs in this chain into
1801	 * the fragment pointers. Stop when we run out
1802	 * of fragments or hit the end of the mbuf chain.
1803	 */
1804	for (m = m_head; m != NULL; m = m->m_next) {
1805		if (m->m_len != 0) {
1806			if ((SK_TX_RING_CNT -
1807			    (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2)
1808				return(ENOBUFS);
1809			f = &sc_if->sk_rdata->sk_tx_ring[frag];
1810			f->sk_data_lo = vtophys(mtod(m, vm_offset_t));
1811			f->sk_ctl = m->m_len | SK_OPCODE_DEFAULT;
1812			if (cnt == 0)
1813				f->sk_ctl |= SK_TXCTL_FIRSTFRAG;
1814			else
1815				f->sk_ctl |= SK_TXCTL_OWN;
1816			cur = frag;
1817			SK_INC(frag, SK_TX_RING_CNT);
1818			cnt++;
1819		}
1820	}
1821
1822	if (m != NULL)
1823		return(ENOBUFS);
1824
1825	sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
1826		SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR;
1827	sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1828	sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |= SK_TXCTL_OWN;
1829	sc_if->sk_cdata.sk_tx_cnt += cnt;
1830
1831	*txidx = frag;
1832
1833	return(0);
1834}
1835
1836static void
1837sk_start(ifp)
1838	struct ifnet		*ifp;
1839{
1840        struct sk_softc		*sc;
1841        struct sk_if_softc	*sc_if;
1842        struct mbuf		*m_head = NULL;
1843        u_int32_t		idx;
1844
1845	sc_if = ifp->if_softc;
1846	sc = sc_if->sk_softc;
1847
1848	SK_IF_LOCK(sc_if);
1849
1850	idx = sc_if->sk_cdata.sk_tx_prod;
1851
1852	while(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1853		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1854		if (m_head == NULL)
1855			break;
1856
1857		/*
1858		 * Pack the data into the transmit ring. If we
1859		 * don't have room, set the OACTIVE flag and wait
1860		 * for the NIC to drain the ring.
1861		 */
1862		if (sk_encap(sc_if, m_head, &idx)) {
1863			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1864			ifp->if_flags |= IFF_OACTIVE;
1865			break;
1866		}
1867
1868		/*
1869		 * If there's a BPF listener, bounce a copy of this frame
1870		 * to him.
1871		 */
1872		BPF_MTAP(ifp, m_head);
1873	}
1874
1875	/* Transmit */
1876	if (idx != sc_if->sk_cdata.sk_tx_prod) {
1877		sc_if->sk_cdata.sk_tx_prod = idx;
1878		CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
1879
1880		/* Set a timeout in case the chip goes out to lunch. */
1881		ifp->if_timer = 5;
1882	}
1883	SK_IF_UNLOCK(sc_if);
1884
1885	return;
1886}
1887
1888
1889static void
1890sk_watchdog(ifp)
1891	struct ifnet		*ifp;
1892{
1893	struct sk_if_softc	*sc_if;
1894
1895	sc_if = ifp->if_softc;
1896
1897	printf("sk%d: watchdog timeout\n", sc_if->sk_unit);
1898	ifp->if_flags &= ~IFF_RUNNING;
1899	sk_init(sc_if);
1900
1901	return;
1902}
1903
1904static void
1905skc_shutdown(dev)
1906	device_t		dev;
1907{
1908	struct sk_softc		*sc;
1909
1910	sc = device_get_softc(dev);
1911	SK_LOCK(sc);
1912
1913	/* Turn off the 'driver is loaded' LED. */
1914	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
1915
1916	/*
1917	 * Reset the GEnesis controller. Doing this should also
1918	 * assert the resets on the attached XMAC(s).
1919	 */
1920	sk_reset(sc);
1921	SK_UNLOCK(sc);
1922
1923	return;
1924}
1925
1926static void
1927sk_rxeof(sc_if)
1928	struct sk_if_softc	*sc_if;
1929{
1930	struct sk_softc		*sc;
1931	struct mbuf		*m;
1932	struct ifnet		*ifp;
1933	struct sk_chain		*cur_rx;
1934	int			total_len = 0;
1935	int			i;
1936	u_int32_t		rxstat;
1937
1938	sc = sc_if->sk_softc;
1939	ifp = &sc_if->arpcom.ac_if;
1940	i = sc_if->sk_cdata.sk_rx_prod;
1941	cur_rx = &sc_if->sk_cdata.sk_rx_chain[i];
1942
1943	SK_LOCK_ASSERT(sc);
1944
1945	while(!(sc_if->sk_rdata->sk_rx_ring[i].sk_ctl & SK_RXCTL_OWN)) {
1946
1947		cur_rx = &sc_if->sk_cdata.sk_rx_chain[i];
1948		rxstat = sc_if->sk_rdata->sk_rx_ring[i].sk_xmac_rxstat;
1949		m = cur_rx->sk_mbuf;
1950		cur_rx->sk_mbuf = NULL;
1951		total_len = SK_RXBYTES(sc_if->sk_rdata->sk_rx_ring[i].sk_ctl);
1952		SK_INC(i, SK_RX_RING_CNT);
1953
1954		if (rxstat & XM_RXSTAT_ERRFRAME) {
1955			ifp->if_ierrors++;
1956			sk_newbuf(sc_if, cur_rx, m);
1957			continue;
1958		}
1959
1960		/*
1961		 * Try to allocate a new jumbo buffer. If that
1962		 * fails, copy the packet to mbufs and put the
1963		 * jumbo buffer back in the ring so it can be
1964		 * re-used. If allocating mbufs fails, then we
1965		 * have to drop the packet.
1966		 */
1967		if (sk_newbuf(sc_if, cur_rx, NULL) == ENOBUFS) {
1968			struct mbuf		*m0;
1969			m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN,
1970			    ifp, NULL);
1971			sk_newbuf(sc_if, cur_rx, m);
1972			if (m0 == NULL) {
1973				printf("sk%d: no receive buffers "
1974				    "available -- packet dropped!\n",
1975				    sc_if->sk_unit);
1976				ifp->if_ierrors++;
1977				continue;
1978			}
1979			m = m0;
1980		} else {
1981			m->m_pkthdr.rcvif = ifp;
1982			m->m_pkthdr.len = m->m_len = total_len;
1983		}
1984
1985		ifp->if_ipackets++;
1986		SK_UNLOCK(sc);
1987		(*ifp->if_input)(ifp, m);
1988		SK_LOCK(sc);
1989	}
1990
1991	sc_if->sk_cdata.sk_rx_prod = i;
1992
1993	return;
1994}
1995
1996static void
1997sk_txeof(sc_if)
1998	struct sk_if_softc	*sc_if;
1999{
2000	struct sk_softc		*sc;
2001	struct sk_tx_desc	*cur_tx;
2002	struct ifnet		*ifp;
2003	u_int32_t		idx;
2004
2005	sc = sc_if->sk_softc;
2006	ifp = &sc_if->arpcom.ac_if;
2007
2008	/*
2009	 * Go through our tx ring and free mbufs for those
2010	 * frames that have been sent.
2011	 */
2012	idx = sc_if->sk_cdata.sk_tx_cons;
2013	while(idx != sc_if->sk_cdata.sk_tx_prod) {
2014		cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
2015		if (cur_tx->sk_ctl & SK_TXCTL_OWN)
2016			break;
2017		if (cur_tx->sk_ctl & SK_TXCTL_LASTFRAG)
2018			ifp->if_opackets++;
2019		if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
2020			m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
2021			sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
2022		}
2023		sc_if->sk_cdata.sk_tx_cnt--;
2024		SK_INC(idx, SK_TX_RING_CNT);
2025	}
2026
2027	if (sc_if->sk_cdata.sk_tx_cnt == 0) {
2028		ifp->if_timer = 0;
2029	} else /* nudge chip to keep tx ring moving */
2030		CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
2031
2032	if (sc_if->sk_cdata.sk_tx_cnt < SK_TX_RING_CNT - 2)
2033		ifp->if_flags &= ~IFF_OACTIVE;
2034
2035	sc_if->sk_cdata.sk_tx_cons = idx;
2036}
2037
2038static void
2039sk_tick(xsc_if)
2040	void			*xsc_if;
2041{
2042	struct sk_if_softc	*sc_if;
2043	struct mii_data		*mii;
2044	struct ifnet		*ifp;
2045	int			i;
2046
2047	sc_if = xsc_if;
2048	SK_IF_LOCK(sc_if);
2049	ifp = &sc_if->arpcom.ac_if;
2050	mii = device_get_softc(sc_if->sk_miibus);
2051
2052	if (!(ifp->if_flags & IFF_UP)) {
2053		SK_IF_UNLOCK(sc_if);
2054		return;
2055	}
2056
2057	if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2058		sk_intr_bcom(sc_if);
2059		SK_IF_UNLOCK(sc_if);
2060		return;
2061	}
2062
2063	/*
2064	 * According to SysKonnect, the correct way to verify that
2065	 * the link has come back up is to poll bit 0 of the GPIO
2066	 * register three times. This pin has the signal from the
2067	 * link_sync pin connected to it; if we read the same link
2068	 * state 3 times in a row, we know the link is up.
2069	 */
2070	for (i = 0; i < 3; i++) {
2071		if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
2072			break;
2073	}
2074
2075	if (i != 3) {
2076		sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2077		SK_IF_UNLOCK(sc_if);
2078		return;
2079	}
2080
2081	/* Turn the GP0 interrupt back on. */
2082	SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2083	SK_XM_READ_2(sc_if, XM_ISR);
2084	mii_tick(mii);
2085	untimeout(sk_tick, sc_if, sc_if->sk_tick_ch);
2086
2087	SK_IF_UNLOCK(sc_if);
2088	return;
2089}
2090
2091static void
2092sk_intr_bcom(sc_if)
2093	struct sk_if_softc	*sc_if;
2094{
2095	struct mii_data		*mii;
2096	struct ifnet		*ifp;
2097	int			status;
2098	mii = device_get_softc(sc_if->sk_miibus);
2099	ifp = &sc_if->arpcom.ac_if;
2100
2101	SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2102
2103	/*
2104	 * Read the PHY interrupt register to make sure
2105	 * we clear any pending interrupts.
2106	 */
2107	status = sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
2108
2109	if (!(ifp->if_flags & IFF_RUNNING)) {
2110		sk_init_xmac(sc_if);
2111		return;
2112	}
2113
2114	if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
2115		int			lstat;
2116		lstat = sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM,
2117		    BRGPHY_MII_AUXSTS);
2118
2119		if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
2120			mii_mediachg(mii);
2121			/* Turn off the link LED. */
2122			SK_IF_WRITE_1(sc_if, 0,
2123			    SK_LINKLED1_CTL, SK_LINKLED_OFF);
2124			sc_if->sk_link = 0;
2125		} else if (status & BRGPHY_ISR_LNK_CHG) {
2126			sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2127	    		    BRGPHY_MII_IMR, 0xFF00);
2128			mii_tick(mii);
2129			sc_if->sk_link = 1;
2130			/* Turn on the link LED. */
2131			SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2132			    SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
2133			    SK_LINKLED_BLINK_OFF);
2134		} else {
2135			mii_tick(mii);
2136			sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2137		}
2138	}
2139
2140	SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2141
2142	return;
2143}
2144
2145static void
2146sk_intr_xmac(sc_if)
2147	struct sk_if_softc	*sc_if;
2148{
2149	struct sk_softc		*sc;
2150	u_int16_t		status;
2151
2152	sc = sc_if->sk_softc;
2153	status = SK_XM_READ_2(sc_if, XM_ISR);
2154
2155	/*
2156	 * Link has gone down. Start MII tick timeout to
2157	 * watch for link resync.
2158	 */
2159	if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
2160		if (status & XM_ISR_GP0_SET) {
2161			SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2162			sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2163		}
2164
2165		if (status & XM_ISR_AUTONEG_DONE) {
2166			sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2167		}
2168	}
2169
2170	if (status & XM_IMR_TX_UNDERRUN)
2171		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
2172
2173	if (status & XM_IMR_RX_OVERRUN)
2174		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
2175
2176	status = SK_XM_READ_2(sc_if, XM_ISR);
2177
2178	return;
2179}
2180
2181static void
2182sk_intr_yukon(sc_if)
2183	struct sk_if_softc	*sc_if;
2184{
2185	int status;
2186
2187	status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2188
2189	return;
2190}
2191
2192static void
2193sk_intr(xsc)
2194	void			*xsc;
2195{
2196	struct sk_softc		*sc = xsc;
2197	struct sk_if_softc	*sc_if0 = NULL, *sc_if1 = NULL;
2198	struct ifnet		*ifp0 = NULL, *ifp1 = NULL;
2199	u_int32_t		status;
2200
2201	SK_LOCK(sc);
2202
2203	sc_if0 = sc->sk_if[SK_PORT_A];
2204	sc_if1 = sc->sk_if[SK_PORT_B];
2205
2206	if (sc_if0 != NULL)
2207		ifp0 = &sc_if0->arpcom.ac_if;
2208	if (sc_if1 != NULL)
2209		ifp1 = &sc_if1->arpcom.ac_if;
2210
2211	for (;;) {
2212		status = CSR_READ_4(sc, SK_ISSR);
2213		if (!(status & sc->sk_intrmask))
2214			break;
2215
2216		/* Handle receive interrupts first. */
2217		if (status & SK_ISR_RX1_EOF) {
2218			sk_rxeof(sc_if0);
2219			CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
2220			    SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2221		}
2222		if (status & SK_ISR_RX2_EOF) {
2223			sk_rxeof(sc_if1);
2224			CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
2225			    SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2226		}
2227
2228		/* Then transmit interrupts. */
2229		if (status & SK_ISR_TX1_S_EOF) {
2230			sk_txeof(sc_if0);
2231			CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
2232			    SK_TXBMU_CLR_IRQ_EOF);
2233		}
2234		if (status & SK_ISR_TX2_S_EOF) {
2235			sk_txeof(sc_if1);
2236			CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
2237			    SK_TXBMU_CLR_IRQ_EOF);
2238		}
2239
2240		/* Then MAC interrupts. */
2241		if (status & SK_ISR_MAC1 && ifp0->if_flags & IFF_RUNNING) {
2242			if (sc->sk_type == SK_GENESIS)
2243				sk_intr_xmac(sc_if0);
2244			else
2245				sk_intr_yukon(sc_if0);
2246		}
2247
2248		if (status & SK_ISR_MAC2 && ifp1->if_flags & IFF_RUNNING) {
2249			if (sc->sk_type == SK_GENESIS)
2250				sk_intr_xmac(sc_if1);
2251			else
2252				sk_intr_yukon(sc_if1);
2253		}
2254
2255		if (status & SK_ISR_EXTERNAL_REG) {
2256			if (ifp0 != NULL &&
2257			    sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
2258				sk_intr_bcom(sc_if0);
2259			if (ifp1 != NULL &&
2260			    sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
2261				sk_intr_bcom(sc_if1);
2262		}
2263	}
2264
2265	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2266
2267	if (ifp0 != NULL && !IFQ_DRV_IS_EMPTY(&ifp0->if_snd))
2268		sk_start(ifp0);
2269	if (ifp1 != NULL && !IFQ_DRV_IS_EMPTY(&ifp1->if_snd))
2270		sk_start(ifp1);
2271
2272	SK_UNLOCK(sc);
2273
2274	return;
2275}
2276
2277static void
2278sk_init_xmac(sc_if)
2279	struct sk_if_softc	*sc_if;
2280{
2281	struct sk_softc		*sc;
2282	struct ifnet		*ifp;
2283	struct sk_bcom_hack	bhack[] = {
2284	{ 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
2285	{ 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
2286	{ 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
2287	{ 0, 0 } };
2288
2289	sc = sc_if->sk_softc;
2290	ifp = &sc_if->arpcom.ac_if;
2291
2292	/* Unreset the XMAC. */
2293	SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
2294	DELAY(1000);
2295
2296	/* Reset the XMAC's internal state. */
2297	SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2298
2299	/* Save the XMAC II revision */
2300	sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
2301
2302	/*
2303	 * Perform additional initialization for external PHYs,
2304	 * namely for the 1000baseTX cards that use the XMAC's
2305	 * GMII mode.
2306	 */
2307	if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2308		int			i = 0;
2309		u_int32_t		val;
2310
2311		/* Take PHY out of reset. */
2312		val = sk_win_read_4(sc, SK_GPIO);
2313		if (sc_if->sk_port == SK_PORT_A)
2314			val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
2315		else
2316			val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
2317		sk_win_write_4(sc, SK_GPIO, val);
2318
2319		/* Enable GMII mode on the XMAC. */
2320		SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
2321
2322		sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2323		    BRGPHY_MII_BMCR, BRGPHY_BMCR_RESET);
2324		DELAY(10000);
2325		sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2326		    BRGPHY_MII_IMR, 0xFFF0);
2327
2328		/*
2329		 * Early versions of the BCM5400 apparently have
2330		 * a bug that requires them to have their reserved
2331		 * registers initialized to some magic values. I don't
2332		 * know what the numbers do, I'm just the messenger.
2333		 */
2334		if (sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, 0x03)
2335		    == 0x6041) {
2336			while(bhack[i].reg) {
2337				sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2338				    bhack[i].reg, bhack[i].val);
2339				i++;
2340			}
2341		}
2342	}
2343
2344	/* Set station address */
2345	SK_XM_WRITE_2(sc_if, XM_PAR0,
2346	    *(u_int16_t *)(&sc_if->arpcom.ac_enaddr[0]));
2347	SK_XM_WRITE_2(sc_if, XM_PAR1,
2348	    *(u_int16_t *)(&sc_if->arpcom.ac_enaddr[2]));
2349	SK_XM_WRITE_2(sc_if, XM_PAR2,
2350	    *(u_int16_t *)(&sc_if->arpcom.ac_enaddr[4]));
2351	SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
2352
2353	if (ifp->if_flags & IFF_BROADCAST) {
2354		SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2355	} else {
2356		SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2357	}
2358
2359	/* We don't need the FCS appended to the packet. */
2360	SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
2361
2362	/* We want short frames padded to 60 bytes. */
2363	SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
2364
2365	/*
2366	 * Enable the reception of all error frames. This is is
2367	 * a necessary evil due to the design of the XMAC. The
2368	 * XMAC's receive FIFO is only 8K in size, however jumbo
2369	 * frames can be up to 9000 bytes in length. When bad
2370	 * frame filtering is enabled, the XMAC's RX FIFO operates
2371	 * in 'store and forward' mode. For this to work, the
2372	 * entire frame has to fit into the FIFO, but that means
2373	 * that jumbo frames larger than 8192 bytes will be
2374	 * truncated. Disabling all bad frame filtering causes
2375	 * the RX FIFO to operate in streaming mode, in which
2376	 * case the XMAC will start transfering frames out of the
2377	 * RX FIFO as soon as the FIFO threshold is reached.
2378	 */
2379	SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
2380	    XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
2381	    XM_MODE_RX_INRANGELEN);
2382
2383	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2384		SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2385	else
2386		SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2387
2388	/*
2389	 * Bump up the transmit threshold. This helps hold off transmit
2390	 * underruns when we're blasting traffic from both ports at once.
2391	 */
2392	SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
2393
2394	/* Set promiscuous mode */
2395	sk_setpromisc(sc_if);
2396
2397	/* Set multicast filter */
2398	sk_setmulti(sc_if);
2399
2400	/* Clear and enable interrupts */
2401	SK_XM_READ_2(sc_if, XM_ISR);
2402	if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
2403		SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
2404	else
2405		SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2406
2407	/* Configure MAC arbiter */
2408	switch(sc_if->sk_xmac_rev) {
2409	case XM_XMAC_REV_B2:
2410		sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
2411		sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
2412		sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
2413		sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
2414		sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
2415		sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
2416		sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
2417		sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
2418		sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2419		break;
2420	case XM_XMAC_REV_C1:
2421		sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
2422		sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
2423		sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
2424		sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
2425		sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
2426		sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
2427		sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
2428		sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
2429		sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2430		break;
2431	default:
2432		break;
2433	}
2434	sk_win_write_2(sc, SK_MACARB_CTL,
2435	    SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
2436
2437	sc_if->sk_link = 1;
2438
2439	return;
2440}
2441
2442static void
2443sk_init_yukon(sc_if)
2444	struct sk_if_softc	*sc_if;
2445{
2446	u_int32_t		phy;
2447	u_int16_t		reg;
2448	struct sk_softc		*sc;
2449	struct ifnet		*ifp;
2450	int			i;
2451
2452	sc = sc_if->sk_softc;
2453	ifp = &sc_if->arpcom.ac_if;
2454
2455	/* GMAC and GPHY Reset */
2456	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
2457	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2458	DELAY(1000);
2459	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
2460	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2461	DELAY(1000);
2462
2463	phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
2464		SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
2465
2466	switch(sc_if->sk_softc->sk_pmd) {
2467	case IFM_1000_SX:
2468	case IFM_1000_LX:
2469		phy |= SK_GPHY_FIBER;
2470		break;
2471
2472	case IFM_1000_CX:
2473	case IFM_1000_T:
2474		phy |= SK_GPHY_COPPER;
2475		break;
2476	}
2477
2478	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
2479	DELAY(1000);
2480	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
2481	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
2482		      SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
2483
2484	/* unused read of the interrupt source register */
2485	SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2486
2487	reg = SK_YU_READ_2(sc_if, YUKON_PAR);
2488
2489	/* MIB Counter Clear Mode set */
2490	reg |= YU_PAR_MIB_CLR;
2491	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2492
2493	/* MIB Counter Clear Mode clear */
2494	reg &= ~YU_PAR_MIB_CLR;
2495	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2496
2497	/* receive control reg */
2498	SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR);
2499
2500	/* transmit parameter register */
2501	SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
2502		      YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
2503
2504	/* serial mode register */
2505	reg = YU_SMR_DATA_BLIND(0x1c) | YU_SMR_MFL_VLAN | YU_SMR_IPG_DATA(0x1e);
2506	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2507		reg |= YU_SMR_MFL_JUMBO;
2508	SK_YU_WRITE_2(sc_if, YUKON_SMR, reg);
2509
2510	/* Setup Yukon's address */
2511	for (i = 0; i < 3; i++) {
2512		/* Write Source Address 1 (unicast filter) */
2513		SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2514			      sc_if->arpcom.ac_enaddr[i * 2] |
2515			      sc_if->arpcom.ac_enaddr[i * 2 + 1] << 8);
2516	}
2517
2518	for (i = 0; i < 3; i++) {
2519		reg = sk_win_read_2(sc_if->sk_softc,
2520				    SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2521		SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2522	}
2523
2524	/* Set promiscuous mode */
2525	sk_setpromisc(sc_if);
2526
2527	/* Set multicast filter */
2528	sk_setmulti(sc_if);
2529
2530	/* enable interrupt mask for counter overflows */
2531	SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2532	SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2533	SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2534
2535	/* Configure RX MAC FIFO */
2536	SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2537	SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
2538
2539	/* Configure TX MAC FIFO */
2540	SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2541	SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2542}
2543
2544/*
2545 * Note that to properly initialize any part of the GEnesis chip,
2546 * you first have to take it out of reset mode.
2547 */
2548static void
2549sk_init(xsc)
2550	void			*xsc;
2551{
2552	struct sk_if_softc	*sc_if = xsc;
2553	struct sk_softc		*sc;
2554	struct ifnet		*ifp;
2555	struct mii_data		*mii;
2556	u_int16_t		reg;
2557
2558	SK_IF_LOCK(sc_if);
2559
2560	ifp = &sc_if->arpcom.ac_if;
2561	sc = sc_if->sk_softc;
2562	mii = device_get_softc(sc_if->sk_miibus);
2563
2564	if (ifp->if_flags & IFF_RUNNING) {
2565		SK_IF_UNLOCK(sc_if);
2566		return;
2567	}
2568
2569	/* Cancel pending I/O and free all RX/TX buffers. */
2570	sk_stop(sc_if);
2571
2572	if (sc->sk_type == SK_GENESIS) {
2573		/* Configure LINK_SYNC LED */
2574		SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
2575		SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2576			SK_LINKLED_LINKSYNC_ON);
2577
2578		/* Configure RX LED */
2579		SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
2580			SK_RXLEDCTL_COUNTER_START);
2581
2582		/* Configure TX LED */
2583		SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
2584			SK_TXLEDCTL_COUNTER_START);
2585	}
2586
2587	/* Configure I2C registers */
2588
2589	/* Configure XMAC(s) */
2590	switch (sc->sk_type) {
2591	case SK_GENESIS:
2592		sk_init_xmac(sc_if);
2593		break;
2594	case SK_YUKON:
2595		sk_init_yukon(sc_if);
2596		break;
2597	}
2598	mii_mediachg(mii);
2599
2600	if (sc->sk_type == SK_GENESIS) {
2601		/* Configure MAC FIFOs */
2602		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
2603		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
2604		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
2605
2606		SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
2607		SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
2608		SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
2609	}
2610
2611	/* Configure transmit arbiter(s) */
2612	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
2613	    SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2614
2615	/* Configure RAMbuffers */
2616	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2617	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2618	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2619	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2620	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2621	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2622
2623	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
2624	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
2625	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
2626	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
2627	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
2628	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
2629	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
2630
2631	/* Configure BMUs */
2632	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
2633	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
2634	    vtophys(&sc_if->sk_rdata->sk_rx_ring[0]));
2635	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
2636
2637	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
2638	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
2639	    vtophys(&sc_if->sk_rdata->sk_tx_ring[0]));
2640	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
2641
2642	/* Init descriptors */
2643	if (sk_init_rx_ring(sc_if) == ENOBUFS) {
2644		printf("sk%d: initialization failed: no "
2645		    "memory for rx buffers\n", sc_if->sk_unit);
2646		sk_stop(sc_if);
2647		SK_IF_UNLOCK(sc_if);
2648		return;
2649	}
2650	sk_init_tx_ring(sc_if);
2651
2652	/* Configure interrupt handling */
2653	CSR_READ_4(sc, SK_ISSR);
2654	if (sc_if->sk_port == SK_PORT_A)
2655		sc->sk_intrmask |= SK_INTRS1;
2656	else
2657		sc->sk_intrmask |= SK_INTRS2;
2658
2659	sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
2660
2661	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2662
2663	/* Start BMUs. */
2664	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
2665
2666	switch(sc->sk_type) {
2667	case SK_GENESIS:
2668		/* Enable XMACs TX and RX state machines */
2669		SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
2670		SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2671		break;
2672	case SK_YUKON:
2673		reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
2674		reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
2675		reg &= ~(YU_GPCR_SPEED_EN | YU_GPCR_DPLX_EN);
2676		SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
2677	}
2678
2679	ifp->if_flags |= IFF_RUNNING;
2680	ifp->if_flags &= ~IFF_OACTIVE;
2681
2682	SK_IF_UNLOCK(sc_if);
2683
2684	return;
2685}
2686
2687static void
2688sk_stop(sc_if)
2689	struct sk_if_softc	*sc_if;
2690{
2691	int			i;
2692	struct sk_softc		*sc;
2693	struct ifnet		*ifp;
2694
2695	SK_IF_LOCK(sc_if);
2696	sc = sc_if->sk_softc;
2697	ifp = &sc_if->arpcom.ac_if;
2698
2699	untimeout(sk_tick, sc_if, sc_if->sk_tick_ch);
2700
2701	if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2702		u_int32_t		val;
2703
2704		/* Put PHY back into reset. */
2705		val = sk_win_read_4(sc, SK_GPIO);
2706		if (sc_if->sk_port == SK_PORT_A) {
2707			val |= SK_GPIO_DIR0;
2708			val &= ~SK_GPIO_DAT0;
2709		} else {
2710			val |= SK_GPIO_DIR2;
2711			val &= ~SK_GPIO_DAT2;
2712		}
2713		sk_win_write_4(sc, SK_GPIO, val);
2714	}
2715
2716	/* Turn off various components of this interface. */
2717	SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2718	switch (sc->sk_type) {
2719	case SK_GENESIS:
2720		SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_RESET);
2721		SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
2722		break;
2723	case SK_YUKON:
2724		SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2725		SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2726		break;
2727	}
2728	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2729	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2730	SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
2731	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2732	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2733	SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2734	SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2735	SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2736	SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2737
2738	/* Disable interrupts */
2739	if (sc_if->sk_port == SK_PORT_A)
2740		sc->sk_intrmask &= ~SK_INTRS1;
2741	else
2742		sc->sk_intrmask &= ~SK_INTRS2;
2743	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2744
2745	SK_XM_READ_2(sc_if, XM_ISR);
2746	SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2747
2748	/* Free RX and TX mbufs still in the queues. */
2749	for (i = 0; i < SK_RX_RING_CNT; i++) {
2750		if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2751			m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2752			sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2753		}
2754	}
2755
2756	for (i = 0; i < SK_TX_RING_CNT; i++) {
2757		if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2758			m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2759			sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2760		}
2761	}
2762
2763	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2764	SK_IF_UNLOCK(sc_if);
2765	return;
2766}
2767