1 /* $OpenBSD: if_sk.c,v 1.33 2003/08/12 05:23:06 nate Exp $ */
4 * Copyright (c) 1997, 1998, 1999, 2000
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 * $FreeBSD: src/sys/pci/if_sk.c,v 1.19.2.9 2003/03/05 18:42:34 njl Exp $
35 * $DragonFly: src/sys/dev/netif/sk/if_sk.c,v 1.8 2003/11/12 22:43:07 dillon Exp $
37 * $FreeBSD: src/sys/pci/if_sk.c,v 1.19.2.9 2003/03/05 18:42:34 njl Exp $
41 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
43 * Permission to use, copy, modify, and distribute this software for any
44 * purpose with or without fee is hereby granted, provided that the above
45 * copyright notice and this permission notice appear in all copies.
47 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
48 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
50 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
51 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
57 * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports
58 * the SK-984x series adapters, both single port and dual port.
60 * The XaQti XMAC II datasheet,
61 * http://www.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
62 * The SysKonnect GEnesis manual, http://www.syskonnect.com
64 * Note: XaQti has been aquired by Vitesse, and Vitesse does not have the
65 * XMAC II datasheet online. I have put my copy at people.freebsd.org as a
66 * convenience to others until Vitesse corrects this problem:
68 * http://people.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
70 * Written by Bill Paul <wpaul@ee.columbia.edu>
71 * Department of Electrical Engineering
72 * Columbia University, New York City
76 * The SysKonnect gigabit ethernet adapters consist of two main
77 * components: the SysKonnect GEnesis controller chip and the XaQti Corp.
78 * XMAC II gigabit ethernet MAC. The XMAC provides all of the MAC
79 * components and a PHY while the GEnesis controller provides a PCI
80 * interface with DMA support. Each card may have between 512K and
81 * 2MB of SRAM on board depending on the configuration.
83 * The SysKonnect GEnesis controller can have either one or two XMAC
84 * chips connected to it, allowing single or dual port NIC configurations.
85 * SysKonnect has the distinction of being the only vendor on the market
86 * with a dual port gigabit ethernet NIC. The GEnesis provides dual FIFOs,
87 * dual DMA queues, packet/MAC/transmit arbiters and direct access to the
88 * XMAC registers. This driver takes advantage of these features to allow
89 * both XMACs to operate as independent interfaces.
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/sockio.h>
96 #include <sys/malloc.h>
97 #include <sys/kernel.h>
98 #include <sys/socket.h>
99 #include <sys/queue.h>
102 #include <net/if_arp.h>
103 #include <net/ethernet.h>
104 #include <net/if_dl.h>
105 #include <net/if_media.h>
109 #include <vm/vm.h> /* for vtophys */
110 #include <vm/pmap.h> /* for vtophys */
111 #include <machine/clock.h> /* for DELAY */
112 #include <machine/bus_pio.h>
113 #include <machine/bus_memio.h>
114 #include <machine/bus.h>
115 #include <machine/resource.h>
117 #include <sys/rman.h>
119 #include "../mii_layer/mii.h"
120 #include "../mii_layer/miivar.h"
121 #include "../mii_layer/brgphyreg.h"
123 #include <bus/pci/pcireg.h>
124 #include <bus/pci/pcivar.h>
127 #define SK_USEIOSPACE
130 #include "if_skreg.h"
131 #include "xmaciireg.h"
132 #include "yukonreg.h"
134 /* "controller miibus0" required. See GENERIC if you get errors here. */
135 #include "miibus_if.h"
137 static struct sk_type sk_devs[] = {
141 "SysKonnect Gigabit Ethernet (V1.0)"
146 "SysKonnect Gigabit Ethernet (V2.0)"
151 "Marvell Gigabit Ethernet"
156 "3Com 3C940 Gigabit Ethernet"
161 static int skc_probe (device_t);
162 static int skc_attach (device_t);
163 static int skc_detach (device_t);
164 static void skc_shutdown (device_t);
165 static int sk_probe (device_t);
166 static int sk_attach (device_t);
167 static int sk_detach (device_t);
168 static void sk_tick (void *);
169 static void sk_intr (void *);
170 static void sk_intr_bcom (struct sk_if_softc *);
171 static void sk_intr_xmac (struct sk_if_softc *);
172 static void sk_intr_yukon (struct sk_if_softc *);
173 static void sk_rxeof (struct sk_if_softc *);
174 static void sk_txeof (struct sk_if_softc *);
175 static int sk_encap (struct sk_if_softc *, struct mbuf *,
177 static void sk_start (struct ifnet *);
178 static int sk_ioctl (struct ifnet *, u_long, caddr_t);
179 static void sk_init (void *);
180 static void sk_init_xmac (struct sk_if_softc *);
181 static void sk_init_yukon (struct sk_if_softc *);
182 static void sk_stop (struct sk_if_softc *);
183 static void sk_watchdog (struct ifnet *);
184 static int sk_ifmedia_upd (struct ifnet *);
185 static void sk_ifmedia_sts (struct ifnet *, struct ifmediareq *);
186 static void sk_reset (struct sk_softc *);
187 static int sk_newbuf (struct sk_if_softc *,
188 struct sk_chain *, struct mbuf *);
189 static int sk_alloc_jumbo_mem (struct sk_if_softc *);
190 static void *sk_jalloc (struct sk_if_softc *);
191 static void sk_jfree (caddr_t, u_int);
192 static void sk_jref (caddr_t, u_int);
193 static int sk_init_rx_ring (struct sk_if_softc *);
194 static void sk_init_tx_ring (struct sk_if_softc *);
195 static u_int32_t sk_win_read_4 (struct sk_softc *, int);
196 static u_int16_t sk_win_read_2 (struct sk_softc *, int);
197 static u_int8_t sk_win_read_1 (struct sk_softc *, int);
198 static void sk_win_write_4 (struct sk_softc *, int, u_int32_t);
199 static void sk_win_write_2 (struct sk_softc *, int, u_int32_t);
200 static void sk_win_write_1 (struct sk_softc *, int, u_int32_t);
201 static u_int8_t sk_vpd_readbyte (struct sk_softc *, int);
202 static void sk_vpd_read_res (struct sk_softc *,
203 struct vpd_res *, int);
204 static void sk_vpd_read (struct sk_softc *);
206 static int sk_miibus_readreg (device_t, int, int);
207 static int sk_miibus_writereg (device_t, int, int, int);
208 static void sk_miibus_statchg (device_t);
210 static int sk_xmac_miibus_readreg (struct sk_if_softc *, int, int);
211 static int sk_xmac_miibus_writereg (struct sk_if_softc *, int, int, int);
212 static void sk_xmac_miibus_statchg (struct sk_if_softc *);
214 static int sk_marv_miibus_readreg (struct sk_if_softc *, int, int);
215 static int sk_marv_miibus_writereg (struct sk_if_softc *, int, int, int);
216 static void sk_marv_miibus_statchg (struct sk_if_softc *);
218 static u_int32_t sk_calchash (caddr_t);
219 static void sk_setfilt (struct sk_if_softc *, caddr_t, int);
220 static void sk_setmulti (struct sk_if_softc *);
221 static void sk_setpromisc (struct sk_if_softc *);
224 #define SK_RES SYS_RES_IOPORT
225 #define SK_RID SK_PCI_LOIO
227 #define SK_RES SYS_RES_MEMORY
228 #define SK_RID SK_PCI_LOMEM
232 * Note that we have newbus methods for both the GEnesis controller
233 * itself and the XMAC(s). The XMACs are children of the GEnesis, and
234 * the miibus code is a child of the XMACs. We need to do it this way
235 * so that the miibus drivers can access the PHY registers on the
236 * right PHY. It's not quite what I had in mind, but it's the only
237 * design that achieves the desired effect.
239 static device_method_t skc_methods[] = {
240 /* Device interface */
241 DEVMETHOD(device_probe, skc_probe),
242 DEVMETHOD(device_attach, skc_attach),
243 DEVMETHOD(device_detach, skc_detach),
244 DEVMETHOD(device_shutdown, skc_shutdown),
247 DEVMETHOD(bus_print_child, bus_generic_print_child),
248 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
253 static driver_t skc_driver = {
256 sizeof(struct sk_softc)
259 static devclass_t skc_devclass;
261 static device_method_t sk_methods[] = {
262 /* Device interface */
263 DEVMETHOD(device_probe, sk_probe),
264 DEVMETHOD(device_attach, sk_attach),
265 DEVMETHOD(device_detach, sk_detach),
266 DEVMETHOD(device_shutdown, bus_generic_shutdown),
269 DEVMETHOD(bus_print_child, bus_generic_print_child),
270 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
273 DEVMETHOD(miibus_readreg, sk_miibus_readreg),
274 DEVMETHOD(miibus_writereg, sk_miibus_writereg),
275 DEVMETHOD(miibus_statchg, sk_miibus_statchg),
280 static driver_t sk_driver = {
283 sizeof(struct sk_if_softc)
286 static devclass_t sk_devclass;
288 DRIVER_MODULE(if_sk, pci, skc_driver, skc_devclass, 0, 0);
289 DRIVER_MODULE(sk, skc, sk_driver, sk_devclass, 0, 0);
290 DRIVER_MODULE(miibus, sk, miibus_driver, miibus_devclass, 0, 0);
292 #define SK_SETBIT(sc, reg, x) \
293 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
295 #define SK_CLRBIT(sc, reg, x) \
296 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
298 #define SK_WIN_SETBIT_4(sc, reg, x) \
299 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) | x)
301 #define SK_WIN_CLRBIT_4(sc, reg, x) \
302 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) & ~x)
304 #define SK_WIN_SETBIT_2(sc, reg, x) \
305 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) | x)
307 #define SK_WIN_CLRBIT_2(sc, reg, x) \
308 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) & ~x)
310 static u_int32_t sk_win_read_4(sc, reg)
315 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
316 return(CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg)));
318 return(CSR_READ_4(sc, reg));
322 static u_int16_t sk_win_read_2(sc, reg)
327 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
328 return(CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg)));
330 return(CSR_READ_2(sc, reg));
334 static u_int8_t sk_win_read_1(sc, reg)
339 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
340 return(CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg)));
342 return(CSR_READ_1(sc, reg));
346 static void sk_win_write_4(sc, reg, val)
352 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
353 CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), val);
355 CSR_WRITE_4(sc, reg, val);
360 static void sk_win_write_2(sc, reg, val)
366 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
367 CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), val);
369 CSR_WRITE_2(sc, reg, val);
374 static void sk_win_write_1(sc, reg, val)
380 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
381 CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), val);
383 CSR_WRITE_1(sc, reg, val);
389 * The VPD EEPROM contains Vital Product Data, as suggested in
390 * the PCI 2.1 specification. The VPD data is separared into areas
391 * denoted by resource IDs. The SysKonnect VPD contains an ID string
392 * resource (the name of the adapter), a read-only area resource
393 * containing various key/data fields and a read/write area which
394 * can be used to store asset management information or log messages.
395 * We read the ID string and read-only into buffers attached to
396 * the controller softc structure for later use. At the moment,
397 * we only use the ID string during sk_attach().
399 static u_int8_t sk_vpd_readbyte(sc, addr)
405 sk_win_write_2(sc, SK_PCI_REG(SK_PCI_VPD_ADDR), addr);
406 for (i = 0; i < SK_TIMEOUT; i++) {
408 if (sk_win_read_2(sc,
409 SK_PCI_REG(SK_PCI_VPD_ADDR)) & SK_VPD_FLAG)
416 return(sk_win_read_1(sc, SK_PCI_REG(SK_PCI_VPD_DATA)));
419 static void sk_vpd_read_res(sc, res, addr)
427 ptr = (u_int8_t *)res;
428 for (i = 0; i < sizeof(struct vpd_res); i++)
429 ptr[i] = sk_vpd_readbyte(sc, i + addr);
434 static void sk_vpd_read(sc)
440 if (sc->sk_vpd_prodname != NULL)
441 free(sc->sk_vpd_prodname, M_DEVBUF);
442 if (sc->sk_vpd_readonly != NULL)
443 free(sc->sk_vpd_readonly, M_DEVBUF);
444 sc->sk_vpd_prodname = NULL;
445 sc->sk_vpd_readonly = NULL;
447 sk_vpd_read_res(sc, &res, pos);
449 if (res.vr_id != VPD_RES_ID) {
450 printf("skc%d: bad VPD resource id: expected %x got %x\n",
451 sc->sk_unit, VPD_RES_ID, res.vr_id);
456 sc->sk_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
457 for (i = 0; i < res.vr_len; i++)
458 sc->sk_vpd_prodname[i] = sk_vpd_readbyte(sc, i + pos);
459 sc->sk_vpd_prodname[i] = '\0';
462 sk_vpd_read_res(sc, &res, pos);
464 if (res.vr_id != VPD_RES_READ) {
465 printf("skc%d: bad VPD resource id: expected %x got %x\n",
466 sc->sk_unit, VPD_RES_READ, res.vr_id);
471 sc->sk_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
472 for (i = 0; i < res.vr_len + 1; i++)
473 sc->sk_vpd_readonly[i] = sk_vpd_readbyte(sc, i + pos);
478 static int sk_miibus_readreg(dev, phy, reg)
482 struct sk_if_softc *sc_if;
484 sc_if = device_get_softc(dev);
486 switch(sc_if->sk_softc->sk_type) {
488 return(sk_xmac_miibus_readreg(sc_if, phy, reg));
490 return(sk_marv_miibus_readreg(sc_if, phy, reg));
496 static int sk_miibus_writereg(dev, phy, reg, val)
500 struct sk_if_softc *sc_if;
502 sc_if = device_get_softc(dev);
504 switch(sc_if->sk_softc->sk_type) {
506 return(sk_xmac_miibus_writereg(sc_if, phy, reg, val));
508 return(sk_marv_miibus_writereg(sc_if, phy, reg, val));
514 static void sk_miibus_statchg(dev)
517 struct sk_if_softc *sc_if;
519 sc_if = device_get_softc(dev);
521 switch(sc_if->sk_softc->sk_type) {
523 sk_xmac_miibus_statchg(sc_if);
526 sk_marv_miibus_statchg(sc_if);
533 static int sk_xmac_miibus_readreg(sc_if, phy, reg)
534 struct sk_if_softc *sc_if;
539 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0)
542 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
543 SK_XM_READ_2(sc_if, XM_PHY_DATA);
544 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
545 for (i = 0; i < SK_TIMEOUT; i++) {
547 if (SK_XM_READ_2(sc_if, XM_MMUCMD) &
548 XM_MMUCMD_PHYDATARDY)
552 if (i == SK_TIMEOUT) {
553 printf("sk%d: phy failed to come ready\n",
559 return(SK_XM_READ_2(sc_if, XM_PHY_DATA));
562 static int sk_xmac_miibus_writereg(sc_if, phy, reg, val)
563 struct sk_if_softc *sc_if;
568 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
569 for (i = 0; i < SK_TIMEOUT; i++) {
570 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
574 if (i == SK_TIMEOUT) {
575 printf("sk%d: phy failed to come ready\n", sc_if->sk_unit);
579 SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val);
580 for (i = 0; i < SK_TIMEOUT; i++) {
582 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
587 printf("sk%d: phy write timed out\n", sc_if->sk_unit);
592 static void sk_xmac_miibus_statchg(sc_if)
593 struct sk_if_softc *sc_if;
595 struct mii_data *mii;
597 mii = device_get_softc(sc_if->sk_miibus);
600 * If this is a GMII PHY, manually set the XMAC's
601 * duplex mode accordingly.
603 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
604 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
605 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
607 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
614 static int sk_marv_miibus_readreg(sc_if, phy, reg)
615 struct sk_if_softc *sc_if;
622 (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER &&
623 sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) {
627 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
628 YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
630 for (i = 0; i < SK_TIMEOUT; i++) {
632 val = SK_YU_READ_2(sc_if, YUKON_SMICR);
633 if (val & YU_SMICR_READ_VALID)
637 if (i == SK_TIMEOUT) {
638 printf("sk%d: phy failed to come ready\n",
643 val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
648 static int sk_marv_miibus_writereg(sc_if, phy, reg, val)
649 struct sk_if_softc *sc_if;
654 SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
655 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
656 YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
658 for (i = 0; i < SK_TIMEOUT; i++) {
660 if (SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY)
667 static void sk_marv_miibus_statchg(sc_if)
668 struct sk_if_softc *sc_if;
673 #define SK_POLY 0xEDB88320
676 static u_int32_t sk_calchash(addr)
679 u_int32_t idx, bit, data, crc;
681 /* Compute CRC for the address value. */
682 crc = 0xFFFFFFFF; /* initial value */
684 for (idx = 0; idx < 6; idx++) {
685 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
686 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? SK_POLY : 0);
689 return (~crc & ((1 << SK_BITS) - 1));
692 static void sk_setfilt(sc_if, addr, slot)
693 struct sk_if_softc *sc_if;
699 base = XM_RXFILT_ENTRY(slot);
701 SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
702 SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
703 SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
708 static void sk_setmulti(sc_if)
709 struct sk_if_softc *sc_if;
711 struct sk_softc *sc = sc_if->sk_softc;
712 struct ifnet *ifp = &sc_if->arpcom.ac_if;
713 u_int32_t hashes[2] = { 0, 0 };
715 struct ifmultiaddr *ifma;
716 u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 };
719 /* First, zot all the existing filters. */
720 switch(sc->sk_type) {
722 for (i = 1; i < XM_RXFILT_MAX; i++)
723 sk_setfilt(sc_if, (caddr_t)&dummy, i);
725 SK_XM_WRITE_4(sc_if, XM_MAR0, 0);
726 SK_XM_WRITE_4(sc_if, XM_MAR2, 0);
729 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
730 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
731 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
732 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
736 /* Now program new ones. */
737 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
738 hashes[0] = 0xFFFFFFFF;
739 hashes[1] = 0xFFFFFFFF;
742 /* First find the tail of the list. */
743 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
744 ifma = ifma->ifma_link.le_next) {
745 if (ifma->ifma_link.le_next == NULL)
748 /* Now traverse the list backwards. */
749 for (; ifma != NULL && ifma != (void *)&ifp->if_multiaddrs;
750 ifma = (struct ifmultiaddr *)ifma->ifma_link.le_prev) {
751 if (ifma->ifma_addr->sa_family != AF_LINK)
754 * Program the first XM_RXFILT_MAX multicast groups
755 * into the perfect filter. For all others,
756 * use the hash table.
758 if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) {
760 LLADDR((struct sockaddr_dl *)ifma->ifma_addr), i);
766 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
768 hashes[0] |= (1 << h);
770 hashes[1] |= (1 << (h - 32));
774 switch(sc->sk_type) {
776 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH|
777 XM_MODE_RX_USE_PERFECT);
778 SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]);
779 SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]);
782 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
783 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
784 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
785 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
792 static void sk_setpromisc(sc_if)
793 struct sk_if_softc *sc_if;
795 struct sk_softc *sc = sc_if->sk_softc;
796 struct ifnet *ifp = &sc_if->arpcom.ac_if;
798 switch(sc->sk_type) {
800 if (ifp->if_flags & IFF_PROMISC) {
801 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
803 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
807 if (ifp->if_flags & IFF_PROMISC) {
808 SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
809 YU_RCR_UFLEN | YU_RCR_MUFLEN);
811 SK_YU_SETBIT_2(sc_if, YUKON_RCR,
812 YU_RCR_UFLEN | YU_RCR_MUFLEN);
820 static int sk_init_rx_ring(sc_if)
821 struct sk_if_softc *sc_if;
823 struct sk_chain_data *cd = &sc_if->sk_cdata;
824 struct sk_ring_data *rd = sc_if->sk_rdata;
827 bzero((char *)rd->sk_rx_ring,
828 sizeof(struct sk_rx_desc) * SK_RX_RING_CNT);
830 for (i = 0; i < SK_RX_RING_CNT; i++) {
831 cd->sk_rx_chain[i].sk_desc = &rd->sk_rx_ring[i];
832 if (sk_newbuf(sc_if, &cd->sk_rx_chain[i], NULL) == ENOBUFS)
834 if (i == (SK_RX_RING_CNT - 1)) {
835 cd->sk_rx_chain[i].sk_next =
837 rd->sk_rx_ring[i].sk_next =
838 vtophys(&rd->sk_rx_ring[0]);
840 cd->sk_rx_chain[i].sk_next =
841 &cd->sk_rx_chain[i + 1];
842 rd->sk_rx_ring[i].sk_next =
843 vtophys(&rd->sk_rx_ring[i + 1]);
847 sc_if->sk_cdata.sk_rx_prod = 0;
848 sc_if->sk_cdata.sk_rx_cons = 0;
853 static void sk_init_tx_ring(sc_if)
854 struct sk_if_softc *sc_if;
856 struct sk_chain_data *cd = &sc_if->sk_cdata;
857 struct sk_ring_data *rd = sc_if->sk_rdata;
860 bzero((char *)sc_if->sk_rdata->sk_tx_ring,
861 sizeof(struct sk_tx_desc) * SK_TX_RING_CNT);
863 for (i = 0; i < SK_TX_RING_CNT; i++) {
864 cd->sk_tx_chain[i].sk_desc = &rd->sk_tx_ring[i];
865 if (i == (SK_TX_RING_CNT - 1)) {
866 cd->sk_tx_chain[i].sk_next =
868 rd->sk_tx_ring[i].sk_next =
869 vtophys(&rd->sk_tx_ring[0]);
871 cd->sk_tx_chain[i].sk_next =
872 &cd->sk_tx_chain[i + 1];
873 rd->sk_tx_ring[i].sk_next =
874 vtophys(&rd->sk_tx_ring[i + 1]);
878 sc_if->sk_cdata.sk_tx_prod = 0;
879 sc_if->sk_cdata.sk_tx_cons = 0;
880 sc_if->sk_cdata.sk_tx_cnt = 0;
885 static int sk_newbuf(sc_if, c, m)
886 struct sk_if_softc *sc_if;
890 struct mbuf *m_new = NULL;
891 struct sk_rx_desc *r;
896 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
900 /* Allocate the jumbo buffer */
901 buf = sk_jalloc(sc_if);
905 printf("sk%d: jumbo allocation failed "
906 "-- packet dropped!\n", sc_if->sk_unit);
911 /* Attach the buffer to the mbuf */
912 m_new->m_data = m_new->m_ext.ext_buf = (void *)buf;
913 m_new->m_flags |= M_EXT;
914 m_new->m_ext.ext_size = m_new->m_pkthdr.len =
915 m_new->m_len = SK_MCLBYTES;
916 m_new->m_ext.ext_free = sk_jfree;
917 m_new->m_ext.ext_ref = sk_jref;
920 * We're re-using a previously allocated mbuf;
921 * be sure to re-init pointers and lengths to
925 m_new->m_len = m_new->m_pkthdr.len = SK_MCLBYTES;
926 m_new->m_data = m_new->m_ext.ext_buf;
930 * Adjust alignment so packet payload begins on a
931 * longword boundary. Mandatory for Alpha, useful on
934 m_adj(m_new, ETHER_ALIGN);
938 r->sk_data_lo = vtophys(mtod(m_new, caddr_t));
939 r->sk_ctl = m_new->m_len | SK_RXSTAT;
945 * Allocate jumbo buffer storage. The SysKonnect adapters support
946 * "jumbograms" (9K frames), although SysKonnect doesn't currently
947 * use them in their drivers. In order for us to use them, we need
948 * large 9K receive buffers, however standard mbuf clusters are only
949 * 2048 bytes in size. Consequently, we need to allocate and manage
950 * our own jumbo buffer pool. Fortunately, this does not require an
951 * excessive amount of additional code.
953 static int sk_alloc_jumbo_mem(sc_if)
954 struct sk_if_softc *sc_if;
958 struct sk_jpool_entry *entry;
960 /* Grab a big chunk o' storage. */
961 sc_if->sk_cdata.sk_jumbo_buf = contigmalloc(SK_JMEM, M_DEVBUF,
962 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
964 if (sc_if->sk_cdata.sk_jumbo_buf == NULL) {
965 printf("sk%d: no memory for jumbo buffers!\n", sc_if->sk_unit);
969 SLIST_INIT(&sc_if->sk_jfree_listhead);
970 SLIST_INIT(&sc_if->sk_jinuse_listhead);
973 * Now divide it up into 9K pieces and save the addresses
974 * in an array. Note that we play an evil trick here by using
975 * the first few bytes in the buffer to hold the the address
976 * of the softc structure for this interface. This is because
977 * sk_jfree() needs it, but it is called by the mbuf management
978 * code which will not pass it to us explicitly.
980 ptr = sc_if->sk_cdata.sk_jumbo_buf;
981 for (i = 0; i < SK_JSLOTS; i++) {
983 aptr = (u_int64_t **)ptr;
984 aptr[0] = (u_int64_t *)sc_if;
985 ptr += sizeof(u_int64_t);
986 sc_if->sk_cdata.sk_jslots[i].sk_buf = ptr;
987 sc_if->sk_cdata.sk_jslots[i].sk_inuse = 0;
989 entry = malloc(sizeof(struct sk_jpool_entry),
992 free(sc_if->sk_cdata.sk_jumbo_buf, M_DEVBUF);
993 sc_if->sk_cdata.sk_jumbo_buf = NULL;
994 printf("sk%d: no memory for jumbo "
995 "buffer queue!\n", sc_if->sk_unit);
999 SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
1000 entry, jpool_entries);
1007 * Allocate a jumbo buffer.
1009 static void *sk_jalloc(sc_if)
1010 struct sk_if_softc *sc_if;
1012 struct sk_jpool_entry *entry;
1014 entry = SLIST_FIRST(&sc_if->sk_jfree_listhead);
1016 if (entry == NULL) {
1018 printf("sk%d: no free jumbo buffers\n", sc_if->sk_unit);
1023 SLIST_REMOVE_HEAD(&sc_if->sk_jfree_listhead, jpool_entries);
1024 SLIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
1025 sc_if->sk_cdata.sk_jslots[entry->slot].sk_inuse = 1;
1026 return(sc_if->sk_cdata.sk_jslots[entry->slot].sk_buf);
1030 * Adjust usage count on a jumbo buffer. In general this doesn't
1031 * get used much because our jumbo buffers don't get passed around
1032 * a lot, but it's implemented for correctness.
1034 static void sk_jref(buf, size)
1038 struct sk_if_softc *sc_if;
1042 /* Extract the softc struct pointer. */
1043 aptr = (u_int64_t **)(buf - sizeof(u_int64_t));
1044 sc_if = (struct sk_if_softc *)(aptr[0]);
1047 panic("sk_jref: can't find softc pointer!");
1049 if (size != SK_MCLBYTES)
1050 panic("sk_jref: adjusting refcount of buf of wrong size!");
1052 /* calculate the slot this buffer belongs to */
1054 i = ((vm_offset_t)aptr
1055 - (vm_offset_t)sc_if->sk_cdata.sk_jumbo_buf) / SK_JLEN;
1057 if ((i < 0) || (i >= SK_JSLOTS))
1058 panic("sk_jref: asked to reference buffer "
1059 "that we don't manage!");
1060 else if (sc_if->sk_cdata.sk_jslots[i].sk_inuse == 0)
1061 panic("sk_jref: buffer already free!");
1063 sc_if->sk_cdata.sk_jslots[i].sk_inuse++;
1069 * Release a jumbo buffer.
1071 static void sk_jfree(buf, size)
1075 struct sk_if_softc *sc_if;
1078 struct sk_jpool_entry *entry;
1080 /* Extract the softc struct pointer. */
1081 aptr = (u_int64_t **)(buf - sizeof(u_int64_t));
1082 sc_if = (struct sk_if_softc *)(aptr[0]);
1085 panic("sk_jfree: can't find softc pointer!");
1087 if (size != SK_MCLBYTES)
1088 panic("sk_jfree: freeing buffer of wrong size!");
1090 /* calculate the slot this buffer belongs to */
1092 i = ((vm_offset_t)aptr
1093 - (vm_offset_t)sc_if->sk_cdata.sk_jumbo_buf) / SK_JLEN;
1095 if ((i < 0) || (i >= SK_JSLOTS))
1096 panic("sk_jfree: asked to free buffer that we don't manage!");
1097 else if (sc_if->sk_cdata.sk_jslots[i].sk_inuse == 0)
1098 panic("sk_jfree: buffer already free!");
1100 sc_if->sk_cdata.sk_jslots[i].sk_inuse--;
1101 if(sc_if->sk_cdata.sk_jslots[i].sk_inuse == 0) {
1102 entry = SLIST_FIRST(&sc_if->sk_jinuse_listhead);
1104 panic("sk_jfree: buffer not in use!");
1106 SLIST_REMOVE_HEAD(&sc_if->sk_jinuse_listhead,
1108 SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
1109 entry, jpool_entries);
1117 * Set media options.
1119 static int sk_ifmedia_upd(ifp)
1122 struct sk_if_softc *sc_if = ifp->if_softc;
1123 struct mii_data *mii;
1125 mii = device_get_softc(sc_if->sk_miibus);
1133 * Report current media status.
1135 static void sk_ifmedia_sts(ifp, ifmr)
1137 struct ifmediareq *ifmr;
1139 struct sk_if_softc *sc_if;
1140 struct mii_data *mii;
1142 sc_if = ifp->if_softc;
1143 mii = device_get_softc(sc_if->sk_miibus);
1146 ifmr->ifm_active = mii->mii_media_active;
1147 ifmr->ifm_status = mii->mii_media_status;
1152 static int sk_ioctl(ifp, command, data)
1157 struct sk_if_softc *sc_if = ifp->if_softc;
1158 struct ifreq *ifr = (struct ifreq *) data;
1160 struct mii_data *mii;
1167 error = ether_ioctl(ifp, command, data);
1170 if (ifr->ifr_mtu > SK_JUMBO_MTU)
1173 ifp->if_mtu = ifr->ifr_mtu;
1178 if (ifp->if_flags & IFF_UP) {
1179 if (ifp->if_flags & IFF_RUNNING) {
1180 if ((ifp->if_flags ^ sc_if->sk_if_flags)
1182 sk_setpromisc(sc_if);
1188 if (ifp->if_flags & IFF_RUNNING)
1191 sc_if->sk_if_flags = ifp->if_flags;
1201 mii = device_get_softc(sc_if->sk_miibus);
1202 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1215 * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device
1216 * IDs against our list and return a device name if we find a match.
1218 static int skc_probe(dev)
1221 struct sk_softc *sc;
1222 struct sk_type *t = sk_devs;
1224 sc = device_get_softc(dev);
1226 while(t->sk_name != NULL) {
1227 if ((pci_get_vendor(dev) == t->sk_vid) &&
1228 (pci_get_device(dev) == t->sk_did)) {
1229 device_set_desc(dev, t->sk_name);
1239 * Force the GEnesis into reset, then bring it out of reset.
1241 static void sk_reset(sc)
1242 struct sk_softc *sc;
1244 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET);
1245 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET);
1246 if (sc->sk_type == SK_YUKON)
1247 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
1250 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET);
1252 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
1253 if (sc->sk_type == SK_YUKON)
1254 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
1256 if (sc->sk_type == SK_GENESIS) {
1257 /* Configure packet arbiter */
1258 sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET);
1259 sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT);
1260 sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT);
1261 sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT);
1262 sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT);
1265 /* Enable RAM interface */
1266 sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
1269 * Configure interrupt moderation. The moderation timer
1270 * defers interrupts specified in the interrupt moderation
1271 * timer mask based on the timeout specified in the interrupt
1272 * moderation timer init register. Each bit in the timer
1273 * register represents 18.825ns, so to specify a timeout in
1274 * microseconds, we have to multiply by 54.
1276 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(200));
1277 sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
1278 SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
1279 sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
1284 static int sk_probe(dev)
1287 struct sk_softc *sc;
1289 sc = device_get_softc(device_get_parent(dev));
1292 * Not much to do here. We always know there will be
1293 * at least one XMAC present, and if there are two,
1294 * skc_attach() will create a second device instance
1297 switch (sc->sk_type) {
1299 device_set_desc(dev, "XaQti Corp. XMAC II");
1302 device_set_desc(dev, "Marvell Semiconductor, Inc. Yukon");
1310 * Each XMAC chip is attached as a separate logical IP interface.
1311 * Single port cards will have only one logical interface of course.
1313 static int sk_attach(dev)
1316 struct sk_softc *sc;
1317 struct sk_if_softc *sc_if;
1324 sc_if = device_get_softc(dev);
1325 sc = device_get_softc(device_get_parent(dev));
1326 port = *(int *)device_get_ivars(dev);
1327 free(device_get_ivars(dev), M_DEVBUF);
1328 device_set_ivars(dev, NULL);
1329 sc_if->sk_dev = dev;
1331 bzero((char *)sc_if, sizeof(struct sk_if_softc));
1333 sc_if->sk_dev = dev;
1334 sc_if->sk_unit = device_get_unit(dev);
1335 sc_if->sk_port = port;
1336 sc_if->sk_softc = sc;
1337 sc->sk_if[port] = sc_if;
1338 if (port == SK_PORT_A)
1339 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0;
1340 if (port == SK_PORT_B)
1341 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1;
1344 * Get station address for this interface. Note that
1345 * dual port cards actually come with three station
1346 * addresses: one for each port, plus an extra. The
1347 * extra one is used by the SysKonnect driver software
1348 * as a 'virtual' station address for when both ports
1349 * are operating in failover mode. Currently we don't
1350 * use this extra address.
1352 for (i = 0; i < ETHER_ADDR_LEN; i++)
1353 sc_if->arpcom.ac_enaddr[i] =
1354 sk_win_read_1(sc, SK_MAC0_0 + (port * 8) + i);
1356 printf("sk%d: Ethernet address: %6D\n",
1357 sc_if->sk_unit, sc_if->arpcom.ac_enaddr, ":");
1360 * Set up RAM buffer addresses. The NIC will have a certain
1361 * amount of SRAM on it, somewhere between 512K and 2MB. We
1362 * need to divide this up a) between the transmitter and
1363 * receiver and b) between the two XMACs, if this is a
1364 * dual port NIC. Our algotithm is to divide up the memory
1365 * evenly so that everyone gets a fair share.
1367 if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) {
1368 u_int32_t chunk, val;
1370 chunk = sc->sk_ramsize / 2;
1371 val = sc->sk_rboff / sizeof(u_int64_t);
1372 sc_if->sk_rx_ramstart = val;
1373 val += (chunk / sizeof(u_int64_t));
1374 sc_if->sk_rx_ramend = val - 1;
1375 sc_if->sk_tx_ramstart = val;
1376 val += (chunk / sizeof(u_int64_t));
1377 sc_if->sk_tx_ramend = val - 1;
1379 u_int32_t chunk, val;
1381 chunk = sc->sk_ramsize / 4;
1382 val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) /
1384 sc_if->sk_rx_ramstart = val;
1385 val += (chunk / sizeof(u_int64_t));
1386 sc_if->sk_rx_ramend = val - 1;
1387 sc_if->sk_tx_ramstart = val;
1388 val += (chunk / sizeof(u_int64_t));
1389 sc_if->sk_tx_ramend = val - 1;
1392 /* Read and save PHY type and set PHY address */
1393 sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF;
1394 switch(sc_if->sk_phytype) {
1395 case SK_PHYTYPE_XMAC:
1396 sc_if->sk_phyaddr = SK_PHYADDR_XMAC;
1398 case SK_PHYTYPE_BCOM:
1399 sc_if->sk_phyaddr = SK_PHYADDR_BCOM;
1401 case SK_PHYTYPE_MARV_COPPER:
1402 sc_if->sk_phyaddr = SK_PHYADDR_MARV;
1405 printf("skc%d: unsupported PHY type: %d\n",
1406 sc->sk_unit, sc_if->sk_phytype);
1410 /* Allocate the descriptor queues. */
1411 sc_if->sk_rdata = contigmalloc(sizeof(struct sk_ring_data), M_DEVBUF,
1412 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1414 if (sc_if->sk_rdata == NULL) {
1415 printf("sk%d: no memory for list buffers!\n", sc_if->sk_unit);
1416 sc->sk_if[port] = NULL;
1420 bzero(sc_if->sk_rdata, sizeof(struct sk_ring_data));
1422 /* Try to allocate memory for jumbo buffers. */
1423 if (sk_alloc_jumbo_mem(sc_if)) {
1424 printf("sk%d: jumbo buffer allocation failed\n",
1426 contigfree(sc_if->sk_rdata,
1427 sizeof(struct sk_ring_data), M_DEVBUF);
1428 sc->sk_if[port] = NULL;
1432 ifp = &sc_if->arpcom.ac_if;
1433 ifp->if_softc = sc_if;
1434 ifp->if_unit = sc_if->sk_unit;
1435 ifp->if_name = "sk";
1436 ifp->if_mtu = ETHERMTU;
1437 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1438 ifp->if_ioctl = sk_ioctl;
1439 ifp->if_output = ether_output;
1440 ifp->if_start = sk_start;
1441 ifp->if_watchdog = sk_watchdog;
1442 ifp->if_init = sk_init;
1443 ifp->if_baudrate = 1000000000;
1444 ifp->if_snd.ifq_maxlen = SK_TX_RING_CNT - 1;
1449 switch (sc->sk_type) {
1451 sk_init_xmac(sc_if);
1454 sk_init_yukon(sc_if);
1458 if (mii_phy_probe(dev, &sc_if->sk_miibus,
1459 sk_ifmedia_upd, sk_ifmedia_sts)) {
1460 printf("skc%d: no PHY found!\n", sc_if->sk_unit);
1461 contigfree(sc_if->sk_cdata.sk_jumbo_buf, SK_JMEM,
1463 contigfree(sc_if->sk_rdata,
1464 sizeof(struct sk_ring_data), M_DEVBUF);
1469 * Call MI attach routine.
1471 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1472 callout_handle_init(&sc_if->sk_tick_ch);
1478 * Attach the interface. Allocate softc structures, do ifmedia
1479 * setup and ethernet/BPF attach.
1481 static int skc_attach(dev)
1486 struct sk_softc *sc;
1487 int unit, error = 0, rid, *port;
1491 sc = device_get_softc(dev);
1492 unit = device_get_unit(dev);
1493 bzero(sc, sizeof(struct sk_softc));
1494 switch (pci_get_device(dev)) {
1495 case DEVICEID_SK_V1:
1496 sc->sk_type = SK_GENESIS;
1498 case DEVICEID_SK_V2:
1499 case DEVICEID_3COM_3C940:
1500 sc->sk_type = SK_YUKON;
1505 * Handle power management nonsense.
1507 command = pci_read_config(dev, SK_PCI_CAPID, 4) & 0x000000FF;
1508 if (command == 0x01) {
1509 command = pci_read_config(dev, SK_PCI_PWRMGMTCTRL, 4);
1510 if (command & SK_PSTATE_MASK) {
1511 u_int32_t iobase, membase, irq;
1513 /* Save important PCI config data. */
1514 iobase = pci_read_config(dev, SK_PCI_LOIO, 4);
1515 membase = pci_read_config(dev, SK_PCI_LOMEM, 4);
1516 irq = pci_read_config(dev, SK_PCI_INTLINE, 4);
1518 /* Reset the power state. */
1519 printf("skc%d: chip is in D%d power mode "
1520 "-- setting to D0\n", unit, command & SK_PSTATE_MASK);
1521 command &= 0xFFFFFFFC;
1522 pci_write_config(dev, SK_PCI_PWRMGMTCTRL, command, 4);
1524 /* Restore PCI config data. */
1525 pci_write_config(dev, SK_PCI_LOIO, iobase, 4);
1526 pci_write_config(dev, SK_PCI_LOMEM, membase, 4);
1527 pci_write_config(dev, SK_PCI_INTLINE, irq, 4);
1532 * Map control/status registers.
1534 command = pci_read_config(dev, PCIR_COMMAND, 4);
1535 command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1536 pci_write_config(dev, PCIR_COMMAND, command, 4);
1537 command = pci_read_config(dev, PCIR_COMMAND, 4);
1539 #ifdef SK_USEIOSPACE
1540 if (!(command & PCIM_CMD_PORTEN)) {
1541 printf("skc%d: failed to enable I/O ports!\n", unit);
1546 if (!(command & PCIM_CMD_MEMEN)) {
1547 printf("skc%d: failed to enable memory mapping!\n", unit);
1554 sc->sk_res = bus_alloc_resource(dev, SK_RES, &rid,
1555 0, ~0, 1, RF_ACTIVE);
1557 if (sc->sk_res == NULL) {
1558 printf("sk%d: couldn't map ports/memory\n", unit);
1563 sc->sk_btag = rman_get_bustag(sc->sk_res);
1564 sc->sk_bhandle = rman_get_bushandle(sc->sk_res);
1566 /* Allocate interrupt */
1568 sc->sk_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1569 RF_SHAREABLE | RF_ACTIVE);
1571 if (sc->sk_irq == NULL) {
1572 printf("skc%d: couldn't map interrupt\n", unit);
1573 bus_release_resource(dev, SK_RES, SK_RID, sc->sk_res);
1578 error = bus_setup_intr(dev, sc->sk_irq, INTR_TYPE_NET,
1579 sk_intr, sc, &sc->sk_intrhand);
1582 printf("skc%d: couldn't set up irq\n", unit);
1583 bus_release_resource(dev, SK_RES, SK_RID, sc->sk_res);
1584 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sk_irq);
1588 /* Reset the adapter. */
1593 /* Read and save vital product data from EEPROM. */
1596 if (sc->sk_type == SK_GENESIS) {
1597 /* Read and save RAM size and RAMbuffer offset */
1598 switch(sk_win_read_1(sc, SK_EPROM0)) {
1599 case SK_RAMSIZE_512K_64:
1600 sc->sk_ramsize = 0x80000;
1601 sc->sk_rboff = SK_RBOFF_0;
1603 case SK_RAMSIZE_1024K_64:
1604 sc->sk_ramsize = 0x100000;
1605 sc->sk_rboff = SK_RBOFF_80000;
1607 case SK_RAMSIZE_1024K_128:
1608 sc->sk_ramsize = 0x100000;
1609 sc->sk_rboff = SK_RBOFF_0;
1611 case SK_RAMSIZE_2048K_128:
1612 sc->sk_ramsize = 0x200000;
1613 sc->sk_rboff = SK_RBOFF_0;
1616 printf("skc%d: unknown ram size: %d\n",
1617 sc->sk_unit, sk_win_read_1(sc, SK_EPROM0));
1618 bus_teardown_intr(dev, sc->sk_irq, sc->sk_intrhand);
1619 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sk_irq);
1620 bus_release_resource(dev, SK_RES, SK_RID, sc->sk_res);
1626 sc->sk_ramsize = 0x20000;
1627 sc->sk_rboff = SK_RBOFF_0;
1630 /* Read and save physical media type */
1631 switch(sk_win_read_1(sc, SK_PMDTYPE)) {
1632 case SK_PMD_1000BASESX:
1633 sc->sk_pmd = IFM_1000_SX;
1635 case SK_PMD_1000BASELX:
1636 sc->sk_pmd = IFM_1000_LX;
1638 case SK_PMD_1000BASECX:
1639 sc->sk_pmd = IFM_1000_CX;
1641 case SK_PMD_1000BASETX:
1642 sc->sk_pmd = IFM_1000_TX;
1645 printf("skc%d: unknown media type: 0x%x\n",
1646 sc->sk_unit, sk_win_read_1(sc, SK_PMDTYPE));
1647 bus_teardown_intr(dev, sc->sk_irq, sc->sk_intrhand);
1648 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sk_irq);
1649 bus_release_resource(dev, SK_RES, SK_RID, sc->sk_res);
1654 /* Announce the product name. */
1655 printf("skc%d: %s\n", sc->sk_unit, sc->sk_vpd_prodname);
1656 sc->sk_devs[SK_PORT_A] = device_add_child(dev, "sk", -1);
1657 port = malloc(sizeof(int), M_DEVBUF, M_NOWAIT);
1659 device_set_ivars(sc->sk_devs[SK_PORT_A], port);
1661 if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
1662 sc->sk_devs[SK_PORT_B] = device_add_child(dev, "sk", -1);
1663 port = malloc(sizeof(int), M_DEVBUF, M_NOWAIT);
1665 device_set_ivars(sc->sk_devs[SK_PORT_B], port);
1668 /* Turn on the 'driver is loaded' LED. */
1669 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
1671 bus_generic_attach(dev);
1678 static int sk_detach(dev)
1681 struct sk_softc *sc;
1682 struct sk_if_softc *sc_if;
1688 sc = device_get_softc(device_get_parent(dev));
1689 sc_if = device_get_softc(dev);
1690 ifp = &sc_if->arpcom.ac_if;
1692 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1693 bus_generic_detach(dev);
1694 if (sc_if->sk_miibus != NULL)
1695 device_delete_child(dev, sc_if->sk_miibus);
1696 contigfree(sc_if->sk_cdata.sk_jumbo_buf, SK_JMEM, M_DEVBUF);
1697 contigfree(sc_if->sk_rdata, sizeof(struct sk_ring_data), M_DEVBUF);
1702 static int skc_detach(dev)
1705 struct sk_softc *sc;
1710 sc = device_get_softc(dev);
1712 bus_generic_detach(dev);
1713 if (sc->sk_devs[SK_PORT_A] != NULL)
1714 device_delete_child(dev, sc->sk_devs[SK_PORT_A]);
1715 if (sc->sk_devs[SK_PORT_B] != NULL)
1716 device_delete_child(dev, sc->sk_devs[SK_PORT_B]);
1718 bus_teardown_intr(dev, sc->sk_irq, sc->sk_intrhand);
1719 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sk_irq);
1720 bus_release_resource(dev, SK_RES, SK_RID, sc->sk_res);
1727 static int sk_encap(sc_if, m_head, txidx)
1728 struct sk_if_softc *sc_if;
1729 struct mbuf *m_head;
1732 struct sk_tx_desc *f = NULL;
1734 u_int32_t frag, cur, cnt = 0;
1737 cur = frag = *txidx;
1740 * Start packing the mbufs in this chain into
1741 * the fragment pointers. Stop when we run out
1742 * of fragments or hit the end of the mbuf chain.
1744 for (m = m_head; m != NULL; m = m->m_next) {
1745 if (m->m_len != 0) {
1746 if ((SK_TX_RING_CNT -
1747 (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2)
1749 f = &sc_if->sk_rdata->sk_tx_ring[frag];
1750 f->sk_data_lo = vtophys(mtod(m, vm_offset_t));
1751 f->sk_ctl = m->m_len | SK_OPCODE_DEFAULT;
1753 f->sk_ctl |= SK_TXCTL_FIRSTFRAG;
1755 f->sk_ctl |= SK_TXCTL_OWN;
1757 SK_INC(frag, SK_TX_RING_CNT);
1765 sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
1766 SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR;
1767 sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
1768 sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |= SK_TXCTL_OWN;
1769 sc_if->sk_cdata.sk_tx_cnt += cnt;
1776 static void sk_start(ifp)
1779 struct sk_softc *sc;
1780 struct sk_if_softc *sc_if;
1781 struct mbuf *m_head = NULL;
1784 sc_if = ifp->if_softc;
1785 sc = sc_if->sk_softc;
1787 idx = sc_if->sk_cdata.sk_tx_prod;
1789 while(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
1790 IF_DEQUEUE(&ifp->if_snd, m_head);
1795 * Pack the data into the transmit ring. If we
1796 * don't have room, set the OACTIVE flag and wait
1797 * for the NIC to drain the ring.
1799 if (sk_encap(sc_if, m_head, &idx)) {
1800 IF_PREPEND(&ifp->if_snd, m_head);
1801 ifp->if_flags |= IFF_OACTIVE;
1806 * If there's a BPF listener, bounce a copy of this frame
1810 bpf_mtap(ifp, m_head);
1814 sc_if->sk_cdata.sk_tx_prod = idx;
1815 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
1817 /* Set a timeout in case the chip goes out to lunch. */
1824 static void sk_watchdog(ifp)
1827 struct sk_if_softc *sc_if;
1829 sc_if = ifp->if_softc;
1831 printf("sk%d: watchdog timeout\n", sc_if->sk_unit);
1837 static void skc_shutdown(dev)
1840 struct sk_softc *sc;
1842 sc = device_get_softc(dev);
1844 /* Turn off the 'driver is loaded' LED. */
1845 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
1848 * Reset the GEnesis controller. Doing this should also
1849 * assert the resets on the attached XMAC(s).
1856 static void sk_rxeof(sc_if)
1857 struct sk_if_softc *sc_if;
1859 struct ether_header *eh;
1862 struct sk_chain *cur_rx;
1867 ifp = &sc_if->arpcom.ac_if;
1868 i = sc_if->sk_cdata.sk_rx_prod;
1869 cur_rx = &sc_if->sk_cdata.sk_rx_chain[i];
1871 while(!(sc_if->sk_rdata->sk_rx_ring[i].sk_ctl & SK_RXCTL_OWN)) {
1873 cur_rx = &sc_if->sk_cdata.sk_rx_chain[i];
1874 rxstat = sc_if->sk_rdata->sk_rx_ring[i].sk_xmac_rxstat;
1875 m = cur_rx->sk_mbuf;
1876 cur_rx->sk_mbuf = NULL;
1877 total_len = SK_RXBYTES(sc_if->sk_rdata->sk_rx_ring[i].sk_ctl);
1878 SK_INC(i, SK_RX_RING_CNT);
1880 if (rxstat & XM_RXSTAT_ERRFRAME) {
1882 sk_newbuf(sc_if, cur_rx, m);
1887 * Try to allocate a new jumbo buffer. If that
1888 * fails, copy the packet to mbufs and put the
1889 * jumbo buffer back in the ring so it can be
1890 * re-used. If allocating mbufs fails, then we
1891 * have to drop the packet.
1893 if (sk_newbuf(sc_if, cur_rx, NULL) == ENOBUFS) {
1895 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1896 total_len + ETHER_ALIGN, 0, ifp, NULL);
1897 sk_newbuf(sc_if, cur_rx, m);
1899 printf("sk%d: no receive buffers "
1900 "available -- packet dropped!\n",
1905 m_adj(m0, ETHER_ALIGN);
1908 m->m_pkthdr.rcvif = ifp;
1909 m->m_pkthdr.len = m->m_len = total_len;
1913 eh = mtod(m, struct ether_header *);
1915 /* Remove header from mbuf and pass it on. */
1916 m_adj(m, sizeof(struct ether_header));
1917 ether_input(ifp, eh, m);
1920 sc_if->sk_cdata.sk_rx_prod = i;
1925 static void sk_txeof(sc_if)
1926 struct sk_if_softc *sc_if;
1928 struct sk_tx_desc *cur_tx = NULL;
1932 ifp = &sc_if->arpcom.ac_if;
1935 * Go through our tx ring and free mbufs for those
1936 * frames that have been sent.
1938 idx = sc_if->sk_cdata.sk_tx_cons;
1939 while(idx != sc_if->sk_cdata.sk_tx_prod) {
1940 cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
1941 if (cur_tx->sk_ctl & SK_TXCTL_OWN)
1943 if (cur_tx->sk_ctl & SK_TXCTL_LASTFRAG)
1945 if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
1946 m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
1947 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
1949 sc_if->sk_cdata.sk_tx_cnt--;
1950 SK_INC(idx, SK_TX_RING_CNT);
1954 sc_if->sk_cdata.sk_tx_cons = idx;
1957 ifp->if_flags &= ~IFF_OACTIVE;
1962 static void sk_tick(xsc_if)
1965 struct sk_if_softc *sc_if;
1966 struct mii_data *mii;
1971 ifp = &sc_if->arpcom.ac_if;
1972 mii = device_get_softc(sc_if->sk_miibus);
1974 if (!(ifp->if_flags & IFF_UP))
1977 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
1978 sk_intr_bcom(sc_if);
1983 * According to SysKonnect, the correct way to verify that
1984 * the link has come back up is to poll bit 0 of the GPIO
1985 * register three times. This pin has the signal from the
1986 * link_sync pin connected to it; if we read the same link
1987 * state 3 times in a row, we know the link is up.
1989 for (i = 0; i < 3; i++) {
1990 if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
1995 sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
1999 /* Turn the GP0 interrupt back on. */
2000 SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2001 SK_XM_READ_2(sc_if, XM_ISR);
2004 untimeout(sk_tick, sc_if, sc_if->sk_tick_ch);
2009 static void sk_intr_bcom(sc_if)
2010 struct sk_if_softc *sc_if;
2012 struct sk_softc *sc;
2013 struct mii_data *mii;
2017 sc = sc_if->sk_softc;
2018 mii = device_get_softc(sc_if->sk_miibus);
2019 ifp = &sc_if->arpcom.ac_if;
2021 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2024 * Read the PHY interrupt register to make sure
2025 * we clear any pending interrupts.
2027 status = sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
2029 if (!(ifp->if_flags & IFF_RUNNING)) {
2030 sk_init_xmac(sc_if);
2034 if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
2036 lstat = sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM,
2039 if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
2041 /* Turn off the link LED. */
2042 SK_IF_WRITE_1(sc_if, 0,
2043 SK_LINKLED1_CTL, SK_LINKLED_OFF);
2045 } else if (status & BRGPHY_ISR_LNK_CHG) {
2046 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2047 BRGPHY_MII_IMR, 0xFF00);
2050 /* Turn on the link LED. */
2051 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2052 SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
2053 SK_LINKLED_BLINK_OFF);
2057 sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2061 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2066 static void sk_intr_xmac(sc_if)
2067 struct sk_if_softc *sc_if;
2069 struct sk_softc *sc;
2071 struct mii_data *mii;
2073 sc = sc_if->sk_softc;
2074 mii = device_get_softc(sc_if->sk_miibus);
2075 status = SK_XM_READ_2(sc_if, XM_ISR);
2078 * Link has gone down. Start MII tick timeout to
2079 * watch for link resync.
2081 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
2082 if (status & XM_ISR_GP0_SET) {
2083 SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
2084 sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2087 if (status & XM_ISR_AUTONEG_DONE) {
2088 sc_if->sk_tick_ch = timeout(sk_tick, sc_if, hz);
2092 if (status & XM_IMR_TX_UNDERRUN)
2093 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
2095 if (status & XM_IMR_RX_OVERRUN)
2096 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
2098 status = SK_XM_READ_2(sc_if, XM_ISR);
2103 static void sk_intr_yukon(sc_if)
2104 struct sk_if_softc *sc_if;
2108 status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2113 static void sk_intr(xsc)
2116 struct sk_softc *sc = xsc;
2117 struct sk_if_softc *sc_if0 = NULL, *sc_if1 = NULL;
2118 struct ifnet *ifp0 = NULL, *ifp1 = NULL;
2121 sc_if0 = sc->sk_if[SK_PORT_A];
2122 sc_if1 = sc->sk_if[SK_PORT_B];
2125 ifp0 = &sc_if0->arpcom.ac_if;
2127 ifp1 = &sc_if1->arpcom.ac_if;
2130 status = CSR_READ_4(sc, SK_ISSR);
2131 if (!(status & sc->sk_intrmask))
2134 /* Handle receive interrupts first. */
2135 if (status & SK_ISR_RX1_EOF) {
2137 CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
2138 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2140 if (status & SK_ISR_RX2_EOF) {
2142 CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
2143 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
2146 /* Then transmit interrupts. */
2147 if (status & SK_ISR_TX1_S_EOF) {
2149 CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
2150 SK_TXBMU_CLR_IRQ_EOF);
2152 if (status & SK_ISR_TX2_S_EOF) {
2154 CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
2155 SK_TXBMU_CLR_IRQ_EOF);
2158 /* Then MAC interrupts. */
2159 if (status & SK_ISR_MAC1 && ifp0->if_flags & IFF_RUNNING) {
2160 if (sc->sk_type == SK_GENESIS)
2161 sk_intr_xmac(sc_if0);
2163 sk_intr_yukon(sc_if0);
2166 if (status & SK_ISR_MAC2 && ifp1->if_flags & IFF_RUNNING) {
2167 if (sc->sk_type == SK_GENESIS)
2168 sk_intr_xmac(sc_if1);
2170 sk_intr_yukon(sc_if0);
2173 if (status & SK_ISR_EXTERNAL_REG) {
2175 sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
2176 sk_intr_bcom(sc_if0);
2178 sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
2179 sk_intr_bcom(sc_if1);
2183 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2185 if (ifp0 != NULL && ifp0->if_snd.ifq_head != NULL)
2187 if (ifp1 != NULL && ifp1->if_snd.ifq_head != NULL)
2193 static void sk_init_xmac(sc_if)
2194 struct sk_if_softc *sc_if;
2196 struct sk_softc *sc;
2198 struct sk_bcom_hack bhack[] = {
2199 { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
2200 { 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
2201 { 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
2204 sc = sc_if->sk_softc;
2205 ifp = &sc_if->arpcom.ac_if;
2207 /* Unreset the XMAC. */
2208 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
2211 /* Reset the XMAC's internal state. */
2212 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2214 /* Save the XMAC II revision */
2215 sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
2218 * Perform additional initialization for external PHYs,
2219 * namely for the 1000baseTX cards that use the XMAC's
2222 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2226 /* Take PHY out of reset. */
2227 val = sk_win_read_4(sc, SK_GPIO);
2228 if (sc_if->sk_port == SK_PORT_A)
2229 val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
2231 val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
2232 sk_win_write_4(sc, SK_GPIO, val);
2234 /* Enable GMII mode on the XMAC. */
2235 SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
2237 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2238 BRGPHY_MII_BMCR, BRGPHY_BMCR_RESET);
2240 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2241 BRGPHY_MII_IMR, 0xFFF0);
2244 * Early versions of the BCM5400 apparently have
2245 * a bug that requires them to have their reserved
2246 * registers initialized to some magic values. I don't
2247 * know what the numbers do, I'm just the messenger.
2249 if (sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, 0x03)
2251 while(bhack[i].reg) {
2252 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM,
2253 bhack[i].reg, bhack[i].val);
2259 /* Set station address */
2260 SK_XM_WRITE_2(sc_if, XM_PAR0,
2261 *(u_int16_t *)(&sc_if->arpcom.ac_enaddr[0]));
2262 SK_XM_WRITE_2(sc_if, XM_PAR1,
2263 *(u_int16_t *)(&sc_if->arpcom.ac_enaddr[2]));
2264 SK_XM_WRITE_2(sc_if, XM_PAR2,
2265 *(u_int16_t *)(&sc_if->arpcom.ac_enaddr[4]));
2266 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
2268 if (ifp->if_flags & IFF_BROADCAST) {
2269 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2271 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
2274 /* We don't need the FCS appended to the packet. */
2275 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
2277 /* We want short frames padded to 60 bytes. */
2278 SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
2281 * Enable the reception of all error frames. This is is
2282 * a necessary evil due to the design of the XMAC. The
2283 * XMAC's receive FIFO is only 8K in size, however jumbo
2284 * frames can be up to 9000 bytes in length. When bad
2285 * frame filtering is enabled, the XMAC's RX FIFO operates
2286 * in 'store and forward' mode. For this to work, the
2287 * entire frame has to fit into the FIFO, but that means
2288 * that jumbo frames larger than 8192 bytes will be
2289 * truncated. Disabling all bad frame filtering causes
2290 * the RX FIFO to operate in streaming mode, in which
2291 * case the XMAC will start transfering frames out of the
2292 * RX FIFO as soon as the FIFO threshold is reached.
2294 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
2295 XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
2296 XM_MODE_RX_INRANGELEN);
2298 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2299 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2301 SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
2304 * Bump up the transmit threshold. This helps hold off transmit
2305 * underruns when we're blasting traffic from both ports at once.
2307 SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
2309 /* Set promiscuous mode */
2310 sk_setpromisc(sc_if);
2312 /* Set multicast filter */
2315 /* Clear and enable interrupts */
2316 SK_XM_READ_2(sc_if, XM_ISR);
2317 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
2318 SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
2320 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2322 /* Configure MAC arbiter */
2323 switch(sc_if->sk_xmac_rev) {
2324 case XM_XMAC_REV_B2:
2325 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
2326 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
2327 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
2328 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
2329 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
2330 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
2331 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
2332 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
2333 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2335 case XM_XMAC_REV_C1:
2336 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
2337 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
2338 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
2339 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
2340 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
2341 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
2342 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
2343 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
2344 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
2349 sk_win_write_2(sc, SK_MACARB_CTL,
2350 SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
2357 static void sk_init_yukon(sc_if)
2358 struct sk_if_softc *sc_if;
2364 /* GMAC and GPHY Reset */
2365 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
2366 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2368 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
2369 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
2372 phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
2373 SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
2375 switch(sc_if->sk_softc->sk_pmd) {
2378 phy |= SK_GPHY_FIBER;
2383 phy |= SK_GPHY_COPPER;
2387 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
2389 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
2390 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
2391 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
2393 /* unused read of the interrupt source register */
2394 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
2396 reg = SK_YU_READ_2(sc_if, YUKON_PAR);
2398 /* MIB Counter Clear Mode set */
2399 reg |= YU_PAR_MIB_CLR;
2400 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2402 /* MIB Counter Clear Mode clear */
2403 reg &= ~YU_PAR_MIB_CLR;
2404 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
2406 /* receive control reg */
2407 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR);
2409 /* transmit parameter register */
2410 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
2411 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
2413 /* serial mode register */
2414 SK_YU_WRITE_2(sc_if, YUKON_SMR, YU_SMR_DATA_BLIND(0x1c) |
2415 YU_SMR_MFL_VLAN | YU_SMR_IPG_DATA(0x1e));
2417 /* Setup Yukon's address */
2418 for (i = 0; i < 3; i++) {
2419 /* Write Source Address 1 (unicast filter) */
2420 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
2421 sc_if->arpcom.ac_enaddr[i * 2] |
2422 sc_if->arpcom.ac_enaddr[i * 2 + 1] << 8);
2425 for (i = 0; i < 3; i++) {
2426 reg = sk_win_read_2(sc_if->sk_softc,
2427 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
2428 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
2431 /* Set promiscuous mode */
2432 sk_setpromisc(sc_if);
2434 /* Set multicast filter */
2437 /* enable interrupt mask for counter overflows */
2438 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
2439 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
2440 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
2442 /* Configure RX MAC FIFO */
2443 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
2444 SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
2446 /* Configure TX MAC FIFO */
2447 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
2448 SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
2452 * Note that to properly initialize any part of the GEnesis chip,
2453 * you first have to take it out of reset mode.
2455 static void sk_init(xsc)
2458 struct sk_if_softc *sc_if = xsc;
2459 struct sk_softc *sc;
2461 struct mii_data *mii;
2467 ifp = &sc_if->arpcom.ac_if;
2468 sc = sc_if->sk_softc;
2469 mii = device_get_softc(sc_if->sk_miibus);
2471 /* Cancel pending I/O and free all RX/TX buffers. */
2474 if (sc->sk_type == SK_GENESIS) {
2475 /* Configure LINK_SYNC LED */
2476 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
2477 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
2478 SK_LINKLED_LINKSYNC_ON);
2480 /* Configure RX LED */
2481 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
2482 SK_RXLEDCTL_COUNTER_START);
2484 /* Configure TX LED */
2485 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
2486 SK_TXLEDCTL_COUNTER_START);
2489 /* Configure I2C registers */
2491 /* Configure XMAC(s) */
2492 switch (sc->sk_type) {
2494 sk_init_xmac(sc_if);
2497 sk_init_yukon(sc_if);
2502 if (sc->sk_type == SK_GENESIS) {
2503 /* Configure MAC FIFOs */
2504 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
2505 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
2506 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
2508 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
2509 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
2510 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
2513 /* Configure transmit arbiter(s) */
2514 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
2515 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
2517 /* Configure RAMbuffers */
2518 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
2519 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
2520 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
2521 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
2522 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
2523 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
2525 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
2526 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
2527 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
2528 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
2529 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
2530 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
2531 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
2533 /* Configure BMUs */
2534 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
2535 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
2536 vtophys(&sc_if->sk_rdata->sk_rx_ring[0]));
2537 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
2539 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
2540 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
2541 vtophys(&sc_if->sk_rdata->sk_tx_ring[0]));
2542 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
2544 /* Init descriptors */
2545 if (sk_init_rx_ring(sc_if) == ENOBUFS) {
2546 printf("sk%d: initialization failed: no "
2547 "memory for rx buffers\n", sc_if->sk_unit);
2552 sk_init_tx_ring(sc_if);
2554 /* Configure interrupt handling */
2555 CSR_READ_4(sc, SK_ISSR);
2556 if (sc_if->sk_port == SK_PORT_A)
2557 sc->sk_intrmask |= SK_INTRS1;
2559 sc->sk_intrmask |= SK_INTRS2;
2561 sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
2563 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2566 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
2568 switch(sc->sk_type) {
2570 /* Enable XMACs TX and RX state machines */
2571 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
2572 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
2575 reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
2576 reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
2577 reg &= ~(YU_GPCR_SPEED_EN | YU_GPCR_DPLX_EN);
2578 SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
2581 ifp->if_flags |= IFF_RUNNING;
2582 ifp->if_flags &= ~IFF_OACTIVE;
2589 static void sk_stop(sc_if)
2590 struct sk_if_softc *sc_if;
2593 struct sk_softc *sc;
2596 sc = sc_if->sk_softc;
2597 ifp = &sc_if->arpcom.ac_if;
2599 untimeout(sk_tick, sc_if, sc_if->sk_tick_ch);
2601 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
2604 /* Put PHY back into reset. */
2605 val = sk_win_read_4(sc, SK_GPIO);
2606 if (sc_if->sk_port == SK_PORT_A) {
2607 val |= SK_GPIO_DIR0;
2608 val &= ~SK_GPIO_DAT0;
2610 val |= SK_GPIO_DIR2;
2611 val &= ~SK_GPIO_DAT2;
2613 sk_win_write_4(sc, SK_GPIO, val);
2616 /* Turn off various components of this interface. */
2617 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
2618 switch (sc->sk_type) {
2620 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_RESET);
2621 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
2624 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
2625 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
2628 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
2629 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2630 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
2631 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
2632 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
2633 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2634 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
2635 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
2636 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
2638 /* Disable interrupts */
2639 if (sc_if->sk_port == SK_PORT_A)
2640 sc->sk_intrmask &= ~SK_INTRS1;
2642 sc->sk_intrmask &= ~SK_INTRS2;
2643 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
2645 SK_XM_READ_2(sc_if, XM_ISR);
2646 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
2648 /* Free RX and TX mbufs still in the queues. */
2649 for (i = 0; i < SK_RX_RING_CNT; i++) {
2650 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
2651 m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
2652 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
2656 for (i = 0; i < SK_TX_RING_CNT; i++) {
2657 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
2658 m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
2659 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
2663 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);