Home | History | Annotate | Line # | Download | only in sociox
if_scx.c revision 1.2
      1 /*	$NetBSD: if_scx.c,v 1.2 2020/03/23 03:55:49 nisimura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #define NOT_MP_SAFE	0
     33 
     34 /*
     35  * Socionext SC2A11 SynQuacer NetSec GbE driver
     36  *
     37  *   (possibly incorrect notes to be removed eventually)
     38  * - 32 byte descriptor for 64 bit paddr design.
     39  * - multiple rings seems available. There are special descriptor fields
     40  *   to designify ring number from which to arrive or to which go.
     41  * - memory mapped EEPROM to hold MAC address. The rest of the area is
     42  *   occupied by a set of ucode for two DMA engines and one packet engine.
     43  * - The size of frame address filter is unknown. Might be 32
     44  * - The first slot is my own station address. Always enabled to perform
     45  *   to identify oneself.
     46  * - 1~31 are for supplimental MAC addresses. Independently enabled
     47  *   for use. Good to catch multicast. Byte-wise selective match available.
     48  *   Use to catch { 0x01, 0x00, 0x00 } and/or { 0x33, 0x33 }.
     49  * - The size of multicast hash filter store is unknown. Might be 256 bit.
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: if_scx.c,v 1.2 2020/03/23 03:55:49 nisimura Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/bus.h>
     57 #include <sys/intr.h>
     58 #include <sys/device.h>
     59 #include <sys/callout.h>
     60 #include <sys/mbuf.h>
     61 #include <sys/malloc.h>
     62 #include <sys/errno.h>
     63 #include <sys/rndsource.h>
     64 #include <sys/kernel.h>
     65 #include <sys/systm.h>
     66 
     67 #include <net/if.h>
     68 #include <net/if_media.h>
     69 #include <net/if_dl.h>
     70 #include <net/if_ether.h>
     71 #include <dev/mii/mii.h>
     72 #include <dev/mii/miivar.h>
     73 #include <net/bpf.h>
     74 
     75 #include <dev/fdt/fdtvar.h>
     76 #include <dev/acpi/acpireg.h>
     77 #include <dev/acpi/acpivar.h>
     78 #include <dev/acpi/acpi_intr.h>
     79 
     80 #define SWRESET		0x104
     81 #define COMINIT		0x120
     82 #define INTRST		0x200
     83 #define  IRQ_RX		(1U<<1)
     84 #define  IRQ_TX		(1U<<0)
     85 #define INTREN		0x204
     86 #define INTR_SET	0x234
     87 #define INTR_CLR	0x238
     88 #define TXINTST		0x400
     89 #define TXINTEN		0x404
     90 #define TXINT_SET	0x428
     91 #define TXINT_CLR	0x42c
     92 #define  TXI_NTOWNR	(1U<<17)
     93 #define  TXI_TR_ERR	(1U<<16)
     94 #define  TXI_TXDONE	(1U<<15)
     95 #define  TXI_TMREXP	(1U<<14)
     96 #define RXINTST		0x440
     97 #define RXINTEN		0x444
     98 #define RXINT_SET	0x468
     99 #define RXINT_CLR	0x46c
    100 #define  RXI_RC_ERR	(1U<<16)
    101 #define  RXI_PKTCNT	(1U<<15)
    102 #define  RXI_TMREXP	(1U<<14)
    103 #define TXTIMER		0x41c
    104 #define RXTIMER		0x45c
    105 #define TXCOUNT		0x410
    106 #define RXCOUNT		0x454
    107 #define DMACH2M		0x210		/* DMAC host2media ucode port */
    108 #define DMACM2H		0x21c		/* DMAC media2host ucode port */
    109 #define PKTENG		0x0d0		/* packet engine ucode port */
    110 #define HWVER0		0x22c
    111 #define HWVER1		0x230
    112 
    113 #define MACSTAT		0x1024		/* gmac status */
    114 #define MACDATA		0x11c0		/* gmac rd/wr data */
    115 #define MACCMD		0x11c4		/* gmac operation */
    116 #define  CMD_IOWR	(1U<<28)	/* write op */
    117 #define  CMD_BUSY	(1U<<31)	/* busy bit */
    118 #define DESCENG_INIT	0x11fc
    119 #define DESCENG_SRST	0x1204
    120 
    121 #define GMACMCR		0x0000		/* MAC configuration */
    122 #define  MCR_IBN	(1U<<30)	/* */
    123 #define  MCR_CST	(1U<<25)	/* strip CRC */
    124 #define  MCR_TC		(1U<<24)	/* keep RGMII PHY notified */
    125 #define  MCR_JE		(1U<<20)	/* ignore oversized >9018 frame */
    126 #define  MCR_USEMII	(1U<<15)	/* 1: RMII/MII, 0: RGMII */
    127 #define  MCR_SPD100	(1U<<14)	/* force speed 100 */
    128 #define  MCR_USEFDX	(1U<<11)	/* force full duplex */
    129 #define  MCR_IPCKEN	(1U<<10)	/* handle checksum */
    130 #define  MCR_ACS	(1U<<7)		/* auto pad CRC strip */
    131 #define  MCR_TXE	(1U<<3)		/* start Tx DMA engine */
    132 #define  MCR_RXE	(1U<<2)		/* start Rx DMA engine */
    133 #define  _MCR_FDX	0x0000280c	/* XXX TBD */
    134 #define  _MCR_HDX	0x0001a00c	/* XXX TBD */
    135 #define GMACAFR		0x0004		/* frame DA/SA address filter */
    136 #define  AFR_RA		(1U<<31)	/* receive block all on */
    137 #define  AFR_HPF	(1U<<10)	/* activate hash or perfect filter */
    138 #define  AFR_SAF	(1U<<9)		/* source address filter */
    139 #define  AFR_SAIF	(1U<<8)		/* SA inverse filtering */
    140 #define  AFR_PCF	(3U<<6)		/* */
    141 #define  AFR_RB		(1U<<5)		/* reject broadcast frame */
    142 #define  AFR_AM		(1U<<4)		/* accept all multicast frame */
    143 #define  AFR_DAIF	(1U<<3)		/* DA inverse filtering */
    144 #define  AFR_MHTE	(1U<<2)		/* use multicast hash table */
    145 #define  AFR_UHTE	(1U<<1)		/* use additional MAC addresses */
    146 #define  AFR_PM		(1U<<0)		/* run promisc mode */
    147 #define  _AFR		0x80000001	/* XXX TBD */
    148 #define GMACMHTH	0x0008		/* XXX multicast hash table 63:32 */
    149 #define GMACMHTL	0x000c		/* XXX multicast hash table 31:0 */
    150 #define GMACGAR		0x0010		/* MDIO operation */
    151 #define  GAR_PHY	(11)		/* mii phy 15:11 */
    152 #define  GAR_REG	(6)		/* mii reg 10:6 */
    153 #define  GAR_CTL	(2)		/* control 5:2 */
    154 #define  GAR_IOWR	(1U<<1)		/* MDIO write op */
    155 #define  GAR_BUSY	(1U)		/* busy bit */
    156 #define GMACGDR		0x0014		/* MDIO rd/wr data */
    157 #define GMACFCR		0x0018		/* 802.3x flowcontrol */
    158 #define  FCR_RFE	(1U<<2)		/* accept PAUSE to throttle Tx */
    159 #define  FCR_TFE	(1U<<1)		/* generate PAUSE to moderate Rx lvl */
    160 #define GMACIMPL	0x0020		/* (dig this number XXXX.YYYY) */
    161 #define GMACVTAG	0x001c		/* VLAN tag control */
    162 #define GMACMAH0	0x0040		/* MAC address 0 47:32 */
    163 #define GMACMAL0	0x0044		/* MAC address 0 31:0 */
    164 #define GMACMAH(i) 	((i)*8+0x40)	/* supplimental MAC addr 1 - 15 */
    165 #define GMACMAL(i) 	((i)*8+0x44)
    166 #define GMACMHT0	0x0500		/* multicast hash table 0 - 8*/
    167 
    168 #define GMACBMR		0x1000		/* DMA bus mode
    169 					 * 24    4PBL
    170 					 * 22:17 RPBL
    171 					 * 16    fix burst
    172 					 * 15:14 priority between Rx and Tx
    173 					 *  3    rxtx41
    174 					 *  2    rxtx31
    175 					 *  1    rxtx21
    176 					 *  0    rxtx11
    177 					 * 13:8  PBL possible DMA burst len
    178 					 * 0     reset op. self clear
    179 					 */
    180 #define  _BMR		0x00412080	/* XXX TBD */
    181 #define  _BMR0		0x00020181	/* XXX TBD */
    182 #define  BMR_RST	(1U<<0)		/* reset op. self clear when done */
    183 #define GMACRDLAR	0x100c		/* */
    184 #define  _RDLAR		0x18000		/* XXX TBD */
    185 #define GMACTDLAR	0x1010		/* */
    186 #define  _TDLAR		0x1c000		/* XXX TBD */
    187 #define GMACOMR		0x1018		/* DMA operation */
    188 #define  OMR_TXE	(1U<<13)	/* start Tx DMA engine */
    189 #define  OMR_RXE	(1U<<1)		/* start Rx DMA engine */
    190 
    191 const struct {
    192 	uint16_t freq, bit; /* GAR 5:2 MDIO frequency selection */
    193 } garclk[] = {
    194 	{ 35,	2 },	/* 25-35 MHz */
    195 	{ 60,	3 },	/* 35-60 MHz */
    196 	{ 100,	0 },	/* 60-100 MHz */
    197 	{ 150,	1 },	/* 100-150 MHz */
    198 	{ 250,	4 },	/* 150-250 MHz */
    199 	{ 300,	5 },	/* 250-300 MHz */
    200 	{ 0 },
    201 };
    202 static int get_garclk(uint32_t);
    203 
    204 /* descriptor format definition */
    205 struct tdes {
    206 	uint32_t t0, t1, t2, t3;
    207 };
    208 
    209 struct rdes {
    210 	uint32_t r0, r1, r2, r3;
    211 };
    212 
    213 #define T0_OWN		(1U<<31)	/* desc is ready to Tx */
    214 #define T0_EOD		(1U<<30)	/* end of descriptor array */
    215 #define T0_DRID		(24)		/* 29:24 DRID */
    216 #define T0_PT		(1U<<21)	/* 23:21 PT */
    217 #define T0_TRID		(16)		/* 20:16 TRID */
    218 #define T0_FS		(1U<<9)		/* first segment of frame */
    219 #define T0_LS		(1U<<8)		/* last segment of frame */
    220 #define T0_CSUM		(1U<<7)		/* enable check sum offload */
    221 #define T0_SGOL		(1U<<6)		/* enable TCP segment offload */
    222 #define T0_TRS		(1U<<4)		/* 5:4 TRS */
    223 #define T0_IOC		(0)		/* XXX TBD interrupt when completed */
    224 /* T1 segment address 63:32 */
    225 /* T2 segment address 31:0 */
    226 /* T3 31:16 TCP segment length, 15:0 segment length to transmit */
    227 #define R0_OWN		(1U<<31)	/* desc is empty */
    228 #define R0_EOD		(1U<<30)	/* end of descriptor array */
    229 #define R0_SRID		(24)		/* 29:24 SRID */
    230 #define R0_FR		(1U<<23)	/* FR */
    231 #define R0_ER		(1U<<21)	/* Rx error indication */
    232 #define R0_ERR		(3U<<16)	/* 18:16 receive error code */
    233 #define R0_TDRID	(14)		/* 15:14 TDRID */
    234 #define R0_FS		(1U<<9)		/* first segment of frame */
    235 #define R0_LS		(1U<<8)		/* last segment of frame */
    236 #define R0_CSUM		(3U<<6)		/* 7:6 checksum status */
    237 #define R0_CERR		(2U<<6)		/* 0 (undone), 1 (found ok), 2 (bad) */
    238 /* R1 frame address 63:32 */
    239 /* R2 frame address 31:0 */
    240 /* R3 31:16 received frame length, 15:0 buffer length to receive */
    241 
    242 #define SCX_NTXSEGS		16
    243 #define SCX_TXQUEUELEN		16
    244 #define SCX_TXQUEUELEN_MASK	(SCX_TXQUEUELEN - 1)
    245 #define SCX_TXQUEUE_GC		(SCX_TXQUEUELEN / 4)
    246 #define SCX_NTXDESC		(SCX_TXQUEUELEN * SCX_NTXSEGS)
    247 #define SCX_NTXDESC_MASK	(SCX_NTXDESC - 1)
    248 #define SCX_NEXTTX(x)		(((x) + 1) & SCX_NTXDESC_MASK)
    249 #define SCX_NEXTTXS(x)		(((x) + 1) & SCX_TXQUEUELEN_MASK)
    250 
    251 #define SCX_NRXDESC		64
    252 #define SCX_NRXDESC_MASK	(SCX_NRXDESC - 1)
    253 #define SCX_NEXTRX(x)		(((x) + 1) & SCX_NRXDESC_MASK)
    254 
    255 #define SCX_INIT_RXDESC(sc, x)						\
    256 do {									\
    257 	struct scx_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
    258 	struct rdes *__rxd = &(sc)->sc_rxdescs[(x)];			\
    259 	struct mbuf *__m = __rxs->rxs_mbuf;				\
    260 	bus_addr_t __paddr =__rxs->rxs_dmamap->dm_segs[0].ds_addr;	\
    261 	__m->m_data = __m->m_ext.ext_buf;				\
    262 	__rxd->r3 = __rxs->rxs_dmamap->dm_segs[0].ds_len;		\
    263 	__rxd->r2 = htole32(BUS_ADDR_LO32(__paddr));			\
    264 	__rxd->r1 = htole32(BUS_ADDR_HI32(__paddr));			\
    265 	__rxd->r0 = R0_OWN | R0_FS | R0_LS;				\
    266 	if ((x) == SCX_NRXDESC - 1) __rxd->r0 |= R0_EOD;		\
    267 } while (/*CONSTCOND*/0)
    268 
    269 struct control_data {
    270 	struct tdes cd_txdescs[SCX_NTXDESC];
    271 	struct rdes cd_rxdescs[SCX_NRXDESC];
    272 };
    273 #define SCX_CDOFF(x)		offsetof(struct control_data, x)
    274 #define SCX_CDTXOFF(x)		SCX_CDOFF(cd_txdescs[(x)])
    275 #define SCX_CDRXOFF(x)		SCX_CDOFF(cd_rxdescs[(x)])
    276 
    277 struct scx_txsoft {
    278 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    279 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    280 	int txs_firstdesc;		/* first descriptor in packet */
    281 	int txs_lastdesc;		/* last descriptor in packet */
    282 	int txs_ndesc;			/* # of descriptors used */
    283 };
    284 
    285 struct scx_rxsoft {
    286 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    287 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    288 };
    289 
    290 struct scx_softc {
    291 	device_t sc_dev;		/* generic device information */
    292 	bus_space_tag_t sc_st;		/* bus space tag */
    293 	bus_space_handle_t sc_sh;	/* bus space handle */
    294 	bus_size_t sc_sz;		/* csr map size */
    295 	bus_space_handle_t sc_eesh;	/* eeprom section handle */
    296 	bus_size_t sc_eesz;		/* eeprom map size */
    297 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    298 	struct ethercom sc_ethercom;	/* Ethernet common data */
    299 	struct mii_data sc_mii;		/* MII */
    300 	callout_t sc_tick_ch;		/* PHY monitor callout */
    301 	int sc_flowflags;		/* 802.3x PAUSE flow control */
    302 	void *sc_ih;			/* interrupt cookie */
    303 	bus_dma_segment_t sc_seg;	/* descriptor store seg */
    304 	int sc_nseg;			/* descriptor store nseg */
    305 	int sc_phy_id;			/* PHY address */
    306 	uint32_t sc_gar;		/* GAR 5:2 clock selection */
    307 	int sc_phandle;			/* fdt phandle */
    308 	uint32_t sc_t0coso;		/* T0_CSUM | T0_SGOL to run */
    309 
    310 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
    311 #define sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    312 
    313 	struct control_data *sc_control_data;
    314 #define sc_txdescs	sc_control_data->cd_txdescs
    315 #define sc_rxdescs	sc_control_data->cd_rxdescs
    316 
    317 	struct scx_txsoft sc_txsoft[SCX_TXQUEUELEN];
    318 	struct scx_rxsoft sc_rxsoft[SCX_NRXDESC];
    319 	int sc_txfree;			/* number of free Tx descriptors */
    320 	int sc_txnext;			/* next ready Tx descriptor */
    321 	int sc_txsfree;			/* number of free Tx jobs */
    322 	int sc_txsnext;			/* next ready Tx job */
    323 	int sc_txsdirty;		/* dirty Tx jobs */
    324 	int sc_rxptr;			/* next ready Rx descriptor/descsoft */
    325 
    326 	krndsource_t rnd_source;	/* random source */
    327 };
    328 
    329 #define SCX_CDTXADDR(sc, x)	((sc)->sc_cddma + SCX_CDTXOFF((x)))
    330 #define SCX_CDRXADDR(sc, x)	((sc)->sc_cddma + SCX_CDRXOFF((x)))
    331 
    332 #define SCX_CDTXSYNC(sc, x, n, ops)					\
    333 do {									\
    334 	int __x, __n;							\
    335 									\
    336 	__x = (x);							\
    337 	__n = (n);							\
    338 									\
    339 	/* If it will wrap around, sync to the end of the ring. */	\
    340 	if ((__x + __n) > SCX_NTXDESC) {				\
    341 		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
    342 		    SCX_CDTXOFF(__x), sizeof(struct tdes) *		\
    343 		    (SCX_NTXDESC - __x), (ops));			\
    344 		__n -= (SCX_NTXDESC - __x);				\
    345 		__x = 0;						\
    346 	}								\
    347 									\
    348 	/* Now sync whatever is left. */				\
    349 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    350 	    SCX_CDTXOFF(__x), sizeof(struct tdes) * __n, (ops));	\
    351 } while (/*CONSTCOND*/0)
    352 
    353 #define SCX_CDRXSYNC(sc, x, ops)					\
    354 do {									\
    355 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    356 	    SCX_CDRXOFF((x)), sizeof(struct rdes), (ops));		\
    357 } while (/*CONSTCOND*/0)
    358 
    359 static int scx_fdt_match(device_t, cfdata_t, void *);
    360 static void scx_fdt_attach(device_t, device_t, void *);
    361 static int scx_acpi_match(device_t, cfdata_t, void *);
    362 static void scx_acpi_attach(device_t, device_t, void *);
    363 
    364 CFATTACH_DECL_NEW(scx_fdt, sizeof(struct scx_softc),
    365     scx_fdt_match, scx_fdt_attach, NULL, NULL);
    366 
    367 CFATTACH_DECL_NEW(scx_acpi, sizeof(struct scx_softc),
    368     scx_acpi_match, scx_acpi_attach, NULL, NULL);
    369 
    370 static void scx_attach_i(struct scx_softc *);
    371 static void scx_reset(struct scx_softc *);
    372 static int scx_init(struct ifnet *);
    373 static void scx_start(struct ifnet *);
    374 static void scx_stop(struct ifnet *, int);
    375 static void scx_watchdog(struct ifnet *);
    376 static int scx_ioctl(struct ifnet *, u_long, void *);
    377 static void scx_set_rcvfilt(struct scx_softc *);
    378 static int scx_ifmedia_upd(struct ifnet *);
    379 static void scx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    380 static void mii_statchg(struct ifnet *);
    381 static void phy_tick(void *);
    382 static int mii_readreg(device_t, int, int, uint16_t *);
    383 static int mii_writereg(device_t, int, int, uint16_t);
    384 static int scx_intr(void *);
    385 static void txreap(struct scx_softc *);
    386 static void rxintr(struct scx_softc *);
    387 static int add_rxbuf(struct scx_softc *, int);
    388 static int spin_waitfor(struct scx_softc *, int, int);
    389 static int mac_read(struct scx_softc *, int);
    390 static void mac_write(struct scx_softc *, int, int);
    391 static void loaducode(struct scx_softc *);
    392 static void injectucode(struct scx_softc *, int, bus_addr_t, bus_size_t);
    393 
    394 #define CSR_READ(sc,off) \
    395 	    bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (off))
    396 #define CSR_WRITE(sc,off,val) \
    397 	    bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (off), (val))
    398 #define EE_READ(sc,off) \
    399 	    bus_space_read_4((sc)->sc_st, (sc)->sc_eesh, (off))
    400 
    401 static int
    402 scx_fdt_match(device_t parent, cfdata_t cf, void *aux)
    403 {
    404 	static const char * compatible[] = {
    405 		"socionext,synquacer-netsec",
    406 		NULL
    407 	};
    408 	struct fdt_attach_args * const faa = aux;
    409 
    410 	return of_match_compatible(faa->faa_phandle, compatible);
    411 }
    412 
    413 static void
    414 scx_fdt_attach(device_t parent, device_t self, void *aux)
    415 {
    416 	struct scx_softc * const sc = device_private(self);
    417 	struct fdt_attach_args * const faa = aux;
    418 	const int phandle = faa->faa_phandle;
    419 	bus_space_tag_t bst = faa->faa_bst;
    420 	bus_space_handle_t bsh;
    421 	bus_space_handle_t eebsh;
    422 	bus_addr_t addr[2];
    423 	bus_size_t size[2];
    424 	char intrstr[128];
    425 
    426 	if (fdtbus_get_reg(phandle, 0, addr+0, size+0) != 0
    427 	    || bus_space_map(faa->faa_bst, addr[0], size[0], 0, &bsh) != 0) {
    428 		aprint_error(": unable to map device csr\n");
    429 		return;
    430 	}
    431 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    432 		aprint_error(": failed to decode interrupt\n");
    433 		goto fail;
    434 	}
    435 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_NET,
    436 		NOT_MP_SAFE, scx_intr, sc);
    437 	if (sc->sc_ih == NULL) {
    438 		aprint_error_dev(self, "couldn't establish interrupt\n");
    439 		goto fail;
    440 	}
    441 	if (fdtbus_get_reg(phandle, 1, addr+1, size+1) != 0
    442 	    || bus_space_map(faa->faa_bst, addr[0], size[1], 0, &eebsh) != 0) {
    443 		aprint_error(": unable to map device eeprom\n");
    444 		goto fail;
    445 	}
    446 
    447 	aprint_naive("\n");
    448 	aprint_normal(": Gigabit Ethernet Controller\n");
    449 	aprint_normal_dev(self, "interrupt on %s\n", intrstr);
    450 
    451 	sc->sc_dev = self;
    452 	sc->sc_st = bst;
    453 	sc->sc_sh = bsh;
    454 	sc->sc_sz = size[0];
    455 	sc->sc_eesh = eebsh;
    456 	sc->sc_eesz = size[1];
    457 	sc->sc_dmat = faa->faa_dmat;
    458 	sc->sc_phandle = phandle;
    459 
    460 	scx_attach_i(sc);
    461 	return;
    462  fail:
    463 	if (sc->sc_eesz)
    464 		bus_space_unmap(sc->sc_st, sc->sc_eesh, sc->sc_eesz);
    465 	if (sc->sc_sz)
    466 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    467 	return;
    468 }
    469 
    470 static int
    471 scx_acpi_match(device_t parent, cfdata_t cf, void *aux)
    472 {
    473 	static const char * compatible[] = {
    474 		"SCX0001",
    475 		NULL
    476 	};
    477 	struct acpi_attach_args *aa = aux;
    478 
    479 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    480 		return 0;
    481 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
    482 }
    483 
    484 static void
    485 scx_acpi_attach(device_t parent, device_t self, void *aux)
    486 {
    487 	struct scx_softc * const sc = device_private(self);
    488 	struct acpi_attach_args * const aa = aux;
    489 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
    490 	bus_space_tag_t bst = aa->aa_memt;
    491 	bus_space_handle_t bsh, eebsh;
    492 	struct acpi_resources res;
    493 	struct acpi_mem *mem;
    494 	struct acpi_irq *irq;
    495 	ACPI_STATUS rv;
    496 
    497 	rv = acpi_resource_parse(self, handle, "_CRS",
    498 	    &res, &acpi_resource_parse_ops_default);
    499 	if (ACPI_FAILURE(rv))
    500 		return;
    501 	mem = acpi_res_mem(&res, 0);
    502 	irq = acpi_res_irq(&res, 0);
    503 	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
    504 		aprint_error(": incomplete csr resources\n");
    505 		return;
    506 	}
    507 	if (bus_space_map(bst, mem->ar_base, mem->ar_length, 0, &bsh) != 0) {
    508 		aprint_error(": couldn't map registers\n");
    509 		return;
    510 	}
    511 	sc->sc_sz = mem->ar_length;
    512 	sc->sc_ih = acpi_intr_establish(self, (uint64_t)handle, IPL_NET,
    513 	    NOT_MP_SAFE, scx_intr, sc, device_xname(self));
    514 	if (sc->sc_ih == NULL) {
    515 		aprint_error_dev(self, "couldn't establish interrupt\n");
    516 		goto fail;
    517 	}
    518 	mem = acpi_res_mem(&res, 1); /* EEPROM for MAC address and ucode */
    519 	if (mem == NULL || mem->ar_length == 0) {
    520 		aprint_error(": incomplete eeprom resources\n");
    521 		goto fail;
    522 	}
    523 	if (bus_space_map(bst, mem->ar_base, mem->ar_length, 0, &eebsh) != 0) {
    524 		aprint_error(": couldn't map registers\n");
    525 		goto fail;
    526 	}
    527 	sc->sc_eesz = mem->ar_length;
    528 
    529 	aprint_naive("\n");
    530 	aprint_normal(": Gigabit Ethernet Controller\n");
    531 
    532 	sc->sc_dev = self;
    533 	sc->sc_st = bst;
    534 	sc->sc_sh = bsh;
    535 	sc->sc_eesh = eebsh;
    536 	sc->sc_dmat = aa->aa_dmat64;
    537 
    538 	scx_attach_i(sc);
    539 
    540 	acpi_resource_cleanup(&res);
    541 	return;
    542  fail:
    543 	if (sc->sc_eesz > 0)
    544 		bus_space_unmap(sc->sc_st, sc->sc_eesh, sc->sc_eesz);
    545 	if (sc->sc_sz > 0)
    546 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    547 	acpi_resource_cleanup(&res);
    548 	return;
    549 }
    550 
    551 static void
    552 scx_attach_i(struct scx_softc *sc)
    553 {
    554 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
    555 	struct mii_data * const mii = &sc->sc_mii;
    556 	struct ifmedia * const ifm = &mii->mii_media;
    557 	uint32_t hwver, phyfreq;
    558 	uint8_t enaddr[ETHER_ADDR_LEN];
    559 	bus_dma_segment_t seg;
    560 	uint32_t csr;
    561 	int i, nseg, error = 0;
    562 
    563 	hwver = CSR_READ(sc, HWVER1);
    564 	csr = bus_space_read_4(sc->sc_st, sc->sc_eesh, 0);
    565 	enaddr[0] = csr >> 24;
    566 	enaddr[1] = csr >> 16;
    567 	enaddr[2] = csr >> 8;
    568 	enaddr[3] = csr;
    569 	csr = bus_space_read_4(sc->sc_st, sc->sc_eesh, 4);
    570 	enaddr[4] = csr >> 24;
    571 	enaddr[5] = csr >> 16;
    572 
    573 	aprint_normal_dev(sc->sc_dev, "NetSec GbE (%d.%d)\n",
    574 	    hwver >> 16, hwver & 0xffff);
    575 	aprint_normal_dev(sc->sc_dev,
    576 	    "Ethernet address %s\n", ether_sprintf(enaddr));
    577 
    578 	phyfreq = 0;
    579 	sc->sc_phy_id = MII_PHY_ANY;
    580 	sc->sc_gar = get_garclk(phyfreq) << GAR_CTL; /* 5:2 gar control */
    581 
    582 	sc->sc_flowflags = 0;
    583 
    584 	if (0/*CONSTCOND*/)
    585 		loaducode(sc);
    586 
    587 	mii->mii_ifp = ifp;
    588 	mii->mii_readreg = mii_readreg;
    589 	mii->mii_writereg = mii_writereg;
    590 	mii->mii_statchg = mii_statchg;
    591 
    592 	sc->sc_ethercom.ec_mii = mii;
    593 	ifmedia_init(ifm, 0, scx_ifmedia_upd, scx_ifmedia_sts);
    594 	mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id,
    595 	    MII_OFFSET_ANY, MIIF_DOPAUSE);
    596 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    597 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
    598 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
    599 	} else
    600 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
    601 	ifm->ifm_media = ifm->ifm_cur->ifm_media; /* as if user has requested */
    602 
    603 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    604 	ifp->if_softc = sc;
    605 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    606 	ifp->if_ioctl = scx_ioctl;
    607 	ifp->if_start = scx_start;
    608 	ifp->if_watchdog = scx_watchdog;
    609 	ifp->if_init = scx_init;
    610 	ifp->if_stop = scx_stop;
    611 	IFQ_SET_READY(&ifp->if_snd);
    612 
    613 	if_attach(ifp);
    614 	if_deferred_start_init(ifp, NULL);
    615 	ether_ifattach(ifp, enaddr);
    616 
    617 	callout_init(&sc->sc_tick_ch, 0);
    618 	callout_setfunc(&sc->sc_tick_ch, phy_tick, sc);
    619 
    620 	/*
    621 	 * Allocate the control data structures, and create and load the
    622 	 * DMA map for it.
    623 	 */
    624 	error = bus_dmamem_alloc(sc->sc_dmat,
    625 	    sizeof(struct control_data), PAGE_SIZE, 0, &seg, 1, &nseg, 0);
    626 	if (error != 0) {
    627 		aprint_error_dev(sc->sc_dev,
    628 		    "unable to allocate control data, error = %d\n", error);
    629 		goto fail_0;
    630 	}
    631 	error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
    632 	    sizeof(struct control_data), (void **)&sc->sc_control_data,
    633 	    BUS_DMA_COHERENT);
    634 	if (error != 0) {
    635 		aprint_error_dev(sc->sc_dev,
    636 		    "unable to map control data, error = %d\n", error);
    637 		goto fail_1;
    638 	}
    639 	error = bus_dmamap_create(sc->sc_dmat,
    640 	    sizeof(struct control_data), 1,
    641 	    sizeof(struct control_data), 0, 0, &sc->sc_cddmamap);
    642 	if (error != 0) {
    643 		aprint_error_dev(sc->sc_dev,
    644 		    "unable to create control data DMA map, "
    645 		    "error = %d\n", error);
    646 		goto fail_2;
    647 	}
    648 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    649 	    sc->sc_control_data, sizeof(struct control_data), NULL, 0);
    650 	if (error != 0) {
    651 		aprint_error_dev(sc->sc_dev,
    652 		    "unable to load control data DMA map, error = %d\n",
    653 		    error);
    654 		goto fail_3;
    655 	}
    656 	for (i = 0; i < SCX_TXQUEUELEN; i++) {
    657 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    658 		    SCX_NTXSEGS, MCLBYTES, 0, 0,
    659 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    660 			aprint_error_dev(sc->sc_dev,
    661 			    "unable to create tx DMA map %d, error = %d\n",
    662 			    i, error);
    663 			goto fail_4;
    664 		}
    665 	}
    666 	for (i = 0; i < SCX_NRXDESC; i++) {
    667 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    668 		    1, MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    669 			aprint_error_dev(sc->sc_dev,
    670 			    "unable to create rx DMA map %d, error = %d\n",
    671 			    i, error);
    672 			goto fail_5;
    673 		}
    674 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    675 	}
    676 	sc->sc_seg = seg;
    677 	sc->sc_nseg = nseg;
    678 printf("bus_dmaseg ds_addr %08lx, ds_len %08lx, nseg %d\n", seg.ds_addr, seg.ds_len, nseg);
    679 
    680 	if (pmf_device_register(sc->sc_dev, NULL, NULL))
    681 		pmf_class_network_register(sc->sc_dev, ifp);
    682 	else
    683 		aprint_error_dev(sc->sc_dev,
    684 			"couldn't establish power handler\n");
    685 
    686 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    687 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    688 
    689 	return;
    690 
    691   fail_5:
    692 	for (i = 0; i < SCX_NRXDESC; i++) {
    693 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    694 			bus_dmamap_destroy(sc->sc_dmat,
    695 			    sc->sc_rxsoft[i].rxs_dmamap);
    696 	}
    697   fail_4:
    698 	for (i = 0; i < SCX_TXQUEUELEN; i++) {
    699 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    700 			bus_dmamap_destroy(sc->sc_dmat,
    701 			    sc->sc_txsoft[i].txs_dmamap);
    702 	}
    703 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    704   fail_3:
    705 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    706   fail_2:
    707 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    708 	    sizeof(struct control_data));
    709   fail_1:
    710 	bus_dmamem_free(sc->sc_dmat, &seg, nseg);
    711   fail_0:
    712 	if (sc->sc_phandle)
    713 		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
    714 	else
    715 		acpi_intr_disestablish(sc->sc_ih);
    716 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    717 	return;
    718 }
    719 
    720 static void
    721 scx_reset(struct scx_softc *sc)
    722 {
    723 
    724 	mac_write(sc, GMACBMR, BMR_RST); /* may take for a while */
    725 	(void)spin_waitfor(sc, GMACBMR, BMR_RST);
    726 
    727 	CSR_WRITE(sc, DESCENG_SRST, 1);
    728 	CSR_WRITE(sc, DESCENG_INIT, 1);
    729 	mac_write(sc, GMACBMR, _BMR);
    730 	mac_write(sc, GMACRDLAR, _RDLAR);
    731 	mac_write(sc, GMACTDLAR, _TDLAR);
    732 	mac_write(sc, GMACAFR, _AFR);
    733 }
    734 
    735 static int
    736 scx_init(struct ifnet *ifp)
    737 {
    738 	struct scx_softc *sc = ifp->if_softc;
    739 	const uint8_t *ea = CLLADDR(ifp->if_sadl);
    740 	uint32_t csr;
    741 	int i;
    742 
    743 	/* Cancel pending I/O. */
    744 	scx_stop(ifp, 0);
    745 
    746 	/* Reset the chip to a known state. */
    747 	scx_reset(sc);
    748 
    749 	/* build sane Tx and load Rx descriptors with mbuf */
    750 	for (i = 0; i < SCX_NTXDESC; i++)
    751 		sc->sc_txdescs[i].t0 = T0_OWN;
    752 	sc->sc_txdescs[SCX_NTXDESC - 1].t0 |= T0_EOD; /* tie off the ring */
    753 	for (i = 0; i < SCX_NRXDESC; i++)
    754 		(void)add_rxbuf(sc, i);
    755 
    756 	/* set my address in perfect match slot 0 */
    757 	csr = (ea[3] << 24) | (ea[2] << 16) | (ea[1] << 8) |  ea[0];
    758 	CSR_WRITE(sc, GMACMAL0, csr);
    759 	csr = (ea[5] << 8) | ea[4];
    760 	CSR_WRITE(sc, GMACMAH0, csr | 1U<<31); /* always valid? */
    761 
    762 	/* accept multicast frame or run promisc mode */
    763 	scx_set_rcvfilt(sc);
    764 
    765 	(void)scx_ifmedia_upd(ifp);
    766 
    767 	/* kick to start GMAC engine */
    768 	csr = mac_read(sc, GMACOMR);
    769 	CSR_WRITE(sc, RXINT_CLR, ~0);
    770 	CSR_WRITE(sc, TXINT_CLR, ~0);
    771 	mac_write(sc, GMACOMR, csr | OMR_RXE | OMR_TXE);
    772 
    773 	ifp->if_flags |= IFF_RUNNING;
    774 	ifp->if_flags &= ~IFF_OACTIVE;
    775 
    776 	/* start one second timer */
    777 	callout_schedule(&sc->sc_tick_ch, hz);
    778 
    779 	return 0;
    780 }
    781 
    782 static void
    783 scx_stop(struct ifnet *ifp, int disable)
    784 {
    785 	struct scx_softc *sc = ifp->if_softc;
    786 
    787 	/* Stop the one second clock. */
    788 	callout_stop(&sc->sc_tick_ch);
    789 
    790 	/* Down the MII. */
    791 	mii_down(&sc->sc_mii);
    792 
    793 	/* Mark the interface down and cancel the watchdog timer. */
    794 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    795 	ifp->if_timer = 0;
    796 }
    797 
    798 static void
    799 scx_watchdog(struct ifnet *ifp)
    800 {
    801 	struct scx_softc *sc = ifp->if_softc;
    802 
    803 	/*
    804 	 * Since we're not interrupting every packet, sweep
    805 	 * up before we report an error.
    806 	 */
    807 	txreap(sc);
    808 
    809 	if (sc->sc_txfree != SCX_NTXDESC) {
    810 		aprint_error_dev(sc->sc_dev,
    811 		    "device timeout (txfree %d txsfree %d txnext %d)\n",
    812 		    sc->sc_txfree, sc->sc_txsfree, sc->sc_txnext);
    813 		if_statinc(ifp, if_oerrors);
    814 
    815 		/* Reset the interface. */
    816 		scx_init(ifp);
    817 	}
    818 
    819 	scx_start(ifp);
    820 }
    821 
    822 static int
    823 scx_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    824 {
    825 	struct scx_softc *sc = ifp->if_softc;
    826 	struct ifreq *ifr = (struct ifreq *)data;
    827 	struct ifmedia *ifm;
    828 	int s, error;
    829 
    830 	s = splnet();
    831 
    832 	switch (cmd) {
    833 	case SIOCSIFMEDIA:
    834 		/* Flow control requires full-duplex mode. */
    835 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
    836 		    (ifr->ifr_media & IFM_FDX) == 0)
    837 			ifr->ifr_media &= ~IFM_ETH_FMASK;
    838 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
    839 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
    840 				/* We can do both TXPAUSE and RXPAUSE. */
    841 				ifr->ifr_media |=
    842 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
    843 			}
    844 			sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
    845 		}
    846 		ifm = &sc->sc_mii.mii_media;
    847 		error = ifmedia_ioctl(ifp, ifr, ifm, cmd);
    848 		break;
    849 	default:
    850 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
    851 			break;
    852 
    853 		error = 0;
    854 
    855 		if (cmd == SIOCSIFCAP)
    856 			error = (*ifp->if_init)(ifp);
    857 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    858 			;
    859 		else if (ifp->if_flags & IFF_RUNNING) {
    860 			/*
    861 			 * Multicast list has changed; set the hardware filter
    862 			 * accordingly.
    863 			 */
    864 			scx_set_rcvfilt(sc);
    865 		}
    866 		break;
    867 	}
    868 
    869 	splx(s);
    870 	return error;
    871 }
    872 
    873 static void
    874 scx_set_rcvfilt(struct scx_softc *sc)
    875 {
    876 	struct ethercom * const ec = &sc->sc_ethercom;
    877 	struct ifnet * const ifp = &ec->ec_if;
    878 	struct ether_multistep step;
    879 	struct ether_multi *enm;
    880 	uint32_t mchash[8]; 	/* 8x 32 = 256 bit */
    881 	uint32_t csr, crc;
    882 	int i;
    883 
    884 	csr = CSR_READ(sc, GMACAFR);
    885 	csr &= ~(AFR_PM | AFR_AM | AFR_MHTE);
    886 	CSR_WRITE(sc, GMACAFR, csr);
    887 
    888 	ETHER_LOCK(ec);
    889 	if (ifp->if_flags & IFF_PROMISC) {
    890 		ec->ec_flags |= ETHER_F_ALLMULTI;
    891 		ETHER_UNLOCK(ec);
    892 		goto update;
    893 	}
    894 	ec->ec_flags &= ~ETHER_F_ALLMULTI;
    895 
    896 	/* clear 15 entry supplimental perfect match filter */
    897 	for (i = 1; i < 16; i++)
    898 		 CSR_WRITE(sc, GMACMAH(i), 0);
    899 	/* build 256 bit multicast hash filter */
    900 	memset(mchash, 0, sizeof(mchash));
    901 	crc = 0;
    902 
    903 	ETHER_FIRST_MULTI(step, ec, enm);
    904 	i = 1; /* slot 0 is occupied */
    905 	while (enm != NULL) {
    906 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    907 			/*
    908 			 * We must listen to a range of multicast addresses.
    909 			 * For now, just accept all multicasts, rather than
    910 			 * trying to set only those filter bits needed to match
    911 			 * the range.  (At this time, the only use of address
    912 			 * ranges is for IP multicast routing, for which the
    913 			 * range is big enough to require all bits set.)
    914 			 */
    915 			ec->ec_flags |= ETHER_F_ALLMULTI;
    916 			ETHER_UNLOCK(ec);
    917 			goto update;
    918 		}
    919 printf("[%d] %s\n", i, ether_sprintf(enm->enm_addrlo));
    920 		if (i < 16) {
    921 			/* use 31 entry perfect match filter */
    922 			uint32_t addr;
    923 			uint8_t *ep = enm->enm_addrlo;
    924 			addr = (ep[3] << 24) | (ep[2] << 16)
    925 			     | (ep[1] <<  8) |  ep[0];
    926 			CSR_WRITE(sc, GMACMAL(i), addr);
    927 			addr = (ep[5] << 8) | ep[4];
    928 			CSR_WRITE(sc, GMACMAH(i), addr | 1U<<31);
    929 		} else {
    930 			/* use hash table when too many */
    931 			/* bit_reserve_32(~crc) !? */
    932 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    933 			/* 3(31:29) 5(28:24) bit sampling */
    934 			mchash[crc >> 29] |= 1 << ((crc >> 24) & 0x1f);
    935 		}
    936 		ETHER_NEXT_MULTI(step, enm);
    937 		i++;
    938 	}
    939 	ETHER_UNLOCK(ec);
    940 
    941 	if (crc)
    942 		csr |= AFR_MHTE;
    943 	for (i = 0; i < 8; i++)
    944 		CSR_WRITE(sc, GMACMHT0 + i * 4, mchash[i]);
    945 	CSR_WRITE(sc, GMACAFR, csr);
    946 	return;
    947 
    948  update:
    949 	/* With PM or AM, MHTE/MHTL/MHTH are never consulted. really? */
    950 	if (ifp->if_flags & IFF_PROMISC)
    951 		csr |= AFR_PM;	/* run promisc. mode */
    952 	else
    953 		csr |= AFR_AM;	/* accept all multicast */
    954 	CSR_WRITE(sc, GMACAFR, csr);
    955 	return;
    956 }
    957 
    958 static int
    959 scx_ifmedia_upd(struct ifnet *ifp)
    960 {
    961 	struct scx_softc *sc = ifp->if_softc;
    962 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    963 
    964 	if (IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_AUTO) {
    965 		; /* restart AN */
    966 		; /* enable AN */
    967 		; /* advertise flow control pause */
    968 		; /* adv. 100FDX,100HDX,10FDX,10HDX */
    969 	} else {
    970 #if 1
    971 		uint32_t mcr = mac_read(sc, GMACMCR);
    972 		if (IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_1000_T)
    973 			mcr &= ~MCR_USEMII; /* RGMII+SPD1000 */
    974 		else {
    975 			mcr |= MCR_USEMII;  /* RMII/MII */
    976 			if (IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_100_TX)
    977 				mcr |= MCR_SPD100;
    978 		}
    979 		if (ifm->ifm_cur->ifm_media & IFM_FDX)
    980 			mcr |= MCR_USEFDX;
    981 		mcr |= MCR_CST | MCR_JE;
    982 		mcr |= MCR_IBN;
    983 		mac_write(sc, GMACMCR, mcr);
    984 #endif
    985 	}
    986 	return 0;
    987 }
    988 
    989 static void
    990 scx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    991 {
    992 	struct scx_softc *sc = ifp->if_softc;
    993 	struct mii_data *mii = &sc->sc_mii;
    994 
    995 	mii_pollstat(mii);
    996 	ifmr->ifm_status = mii->mii_media_status;
    997 	ifmr->ifm_active = sc->sc_flowflags |
    998 	    (mii->mii_media_active & ~IFM_ETH_FMASK);
    999 }
   1000 
   1001 void
   1002 mii_statchg(struct ifnet *ifp)
   1003 {
   1004 	struct scx_softc *sc = ifp->if_softc;
   1005 	struct mii_data *mii = &sc->sc_mii;
   1006 	uint32_t fcr;
   1007 
   1008 	/* Get flow control negotiation result. */
   1009 	if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
   1010 	    (mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags)
   1011 		sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
   1012 
   1013 	/* Adjust PAUSE flow control. */
   1014 	fcr = mac_read(sc, GMACFCR) & ~(FCR_TFE | FCR_RFE);
   1015 	if (mii->mii_media_active & IFM_FDX) {
   1016 		if (sc->sc_flowflags & IFM_ETH_TXPAUSE)
   1017 			fcr |= FCR_TFE;
   1018 		if (sc->sc_flowflags & IFM_ETH_RXPAUSE)
   1019 			fcr |= FCR_RFE;
   1020 	}
   1021 	mac_write(sc, GMACFCR, fcr);
   1022 
   1023 printf("%ctxfe, %crxfe\n",
   1024      (fcr & FCR_TFE) ? '+' : '-', (fcr & FCR_RFE) ? '+' : '-');
   1025 }
   1026 
   1027 static void
   1028 phy_tick(void *arg)
   1029 {
   1030 	struct scx_softc *sc = arg;
   1031 	struct mii_data *mii = &sc->sc_mii;
   1032 	int s;
   1033 
   1034 	s = splnet();
   1035 	mii_tick(mii);
   1036 	splx(s);
   1037 
   1038 	callout_schedule(&sc->sc_tick_ch, hz);
   1039 }
   1040 
   1041 static int
   1042 mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   1043 {
   1044 	struct scx_softc *sc = device_private(self);
   1045 	uint32_t gar;
   1046 	int error;
   1047 
   1048 	gar = (phy << GAR_PHY) | (reg << GAR_REG) | sc->sc_gar;
   1049 	mac_write(sc, GMACGAR, gar | GAR_BUSY);
   1050 	error = spin_waitfor(sc, GMACGAR, GAR_BUSY);
   1051 	if (error)
   1052 		return error;
   1053 	*val = mac_read(sc, GMACGDR);
   1054 	return 0;
   1055 }
   1056 
   1057 static int
   1058 mii_writereg(device_t self, int phy, int reg, uint16_t val)
   1059 {
   1060 	struct scx_softc *sc = device_private(self);
   1061 	uint32_t gar;
   1062 	uint16_t dummy;
   1063 	int error;
   1064 
   1065 	gar = (phy << GAR_PHY) | (reg << GAR_REG) | sc->sc_gar;
   1066 	mac_write(sc, GMACGDR, val);
   1067 	mac_write(sc, GMACGAR, gar | GAR_IOWR | GAR_BUSY);
   1068 	error = spin_waitfor(sc, GMACGAR, GAR_BUSY);
   1069 	if (error)
   1070 		return error;
   1071 	mii_readreg(self, phy, MII_PHYIDR1, &dummy); /* dummy read cycle */
   1072 	return 0;
   1073 }
   1074 
   1075 static void
   1076 scx_start(struct ifnet *ifp)
   1077 {
   1078 	struct scx_softc *sc = ifp->if_softc;
   1079 	struct mbuf *m0, *m;
   1080 	struct scx_txsoft *txs;
   1081 	bus_dmamap_t dmamap;
   1082 	int error, nexttx, lasttx, ofree, seg;
   1083 	uint32_t tdes0;
   1084 
   1085 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1086 		return;
   1087 
   1088 	/* Remember the previous number of free descriptors. */
   1089 	ofree = sc->sc_txfree;
   1090 
   1091 	/*
   1092 	 * Loop through the send queue, setting up transmit descriptors
   1093 	 * until we drain the queue, or use up all available transmit
   1094 	 * descriptors.
   1095 	 */
   1096 	for (;;) {
   1097 		IFQ_POLL(&ifp->if_snd, m0);
   1098 		if (m0 == NULL)
   1099 			break;
   1100 
   1101 		if (sc->sc_txsfree < SCX_TXQUEUE_GC) {
   1102 			txreap(sc);
   1103 			if (sc->sc_txsfree == 0)
   1104 				break;
   1105 		}
   1106 		txs = &sc->sc_txsoft[sc->sc_txsnext];
   1107 		dmamap = txs->txs_dmamap;
   1108 
   1109 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
   1110 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1111 		if (error) {
   1112 			if (error == EFBIG) {
   1113 				aprint_error_dev(sc->sc_dev,
   1114 				    "Tx packet consumes too many "
   1115 				    "DMA segments, dropping...\n");
   1116 				    IFQ_DEQUEUE(&ifp->if_snd, m0);
   1117 				    m_freem(m0);
   1118 				    continue;
   1119 			}
   1120 			/* Short on resources, just stop for now. */
   1121 			break;
   1122 		}
   1123 
   1124 		if (dmamap->dm_nsegs > sc->sc_txfree) {
   1125 			/*
   1126 			 * Not enough free descriptors to transmit this
   1127 			 * packet.  We haven't committed anything yet,
   1128 			 * so just unload the DMA map, put the packet
   1129 			 * back on the queue, and punt.	 Notify the upper
   1130 			 * layer that there are not more slots left.
   1131 			 */
   1132 			ifp->if_flags |= IFF_OACTIVE;
   1133 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   1134 			break;
   1135 		}
   1136 
   1137 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1138 
   1139 		/*
   1140 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1141 		 */
   1142 
   1143 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1144 		    BUS_DMASYNC_PREWRITE);
   1145 
   1146 		tdes0 = 0; /* to postpone 1st segment T0_OWN write */
   1147 		lasttx = -1;
   1148 		for (nexttx = sc->sc_txnext, seg = 0;
   1149 		     seg < dmamap->dm_nsegs;
   1150 		     seg++, nexttx = SCX_NEXTTX(nexttx)) {
   1151 			struct tdes *tdes = &sc->sc_txdescs[nexttx];
   1152 			bus_addr_t paddr = dmamap->dm_segs[seg].ds_addr;
   1153 			/*
   1154 			 * If this is the first descriptor we're
   1155 			 * enqueueing, don't set the OWN bit just
   1156 			 * yet.	 That could cause a race condition.
   1157 			 * We'll do it below.
   1158 			 */
   1159 			tdes->t3 = dmamap->dm_segs[seg].ds_len;
   1160 			tdes->t2 = htole32(BUS_ADDR_LO32(paddr));
   1161 			tdes->t1 = htole32(BUS_ADDR_HI32(paddr));
   1162 			tdes->t0 = tdes0 | (tdes->t0 & T0_EOD) |
   1163 					(15 << T0_TRID) | T0_PT |
   1164 					sc->sc_t0coso | T0_TRS;
   1165 			tdes0 = T0_OWN; /* 2nd and other segments */
   1166 			lasttx = nexttx;
   1167 		}
   1168 		/*
   1169 		 * Outgoing NFS mbuf must be unloaded when Tx completed.
   1170 		 * Without T1_IC NFS mbuf is left unack'ed for excessive
   1171 		 * time and NFS stops to proceed until scx_watchdog()
   1172 		 * calls txreap() to reclaim the unack'ed mbuf.
   1173 		 * It's painful to traverse every mbuf chain to determine
   1174 		 * whether someone is waiting for Tx completion.
   1175 		 */
   1176 		m = m0;
   1177 		do {
   1178 			if ((m->m_flags & M_EXT) && m->m_ext.ext_free) {
   1179 				sc->sc_txdescs[lasttx].t0 |= T0_IOC; /* !!! */
   1180 				break;
   1181 			}
   1182 		} while ((m = m->m_next) != NULL);
   1183 
   1184 		/* Write deferred 1st segment T0_OWN at the final stage */
   1185 		sc->sc_txdescs[lasttx].t0 |= T0_LS;
   1186 		sc->sc_txdescs[sc->sc_txnext].t0 |= (T0_FS | T0_OWN);
   1187 		SCX_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
   1188 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1189 
   1190 		/* Tell DMA start transmit */
   1191 		/* CSR_WRITE(sc, MDTSC, 1); */
   1192 
   1193 		txs->txs_mbuf = m0;
   1194 		txs->txs_firstdesc = sc->sc_txnext;
   1195 		txs->txs_lastdesc = lasttx;
   1196 		txs->txs_ndesc = dmamap->dm_nsegs;
   1197 
   1198 		sc->sc_txfree -= txs->txs_ndesc;
   1199 		sc->sc_txnext = nexttx;
   1200 		sc->sc_txsfree--;
   1201 		sc->sc_txsnext = SCX_NEXTTXS(sc->sc_txsnext);
   1202 		/*
   1203 		 * Pass the packet to any BPF listeners.
   1204 		 */
   1205 		bpf_mtap(ifp, m0, BPF_D_OUT);
   1206 	}
   1207 
   1208 	if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) {
   1209 		/* No more slots left; notify upper layer. */
   1210 		ifp->if_flags |= IFF_OACTIVE;
   1211 	}
   1212 	if (sc->sc_txfree != ofree) {
   1213 		/* Set a watchdog timer in case the chip flakes out. */
   1214 		ifp->if_timer = 5;
   1215 	}
   1216 }
   1217 
   1218 static int
   1219 scx_intr(void *arg)
   1220 {
   1221 	struct scx_softc *sc = arg;
   1222 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1223 
   1224 	(void)ifp;
   1225 	rxintr(sc);
   1226 	txreap(sc);
   1227 	return 1;
   1228 }
   1229 
   1230 static void
   1231 txreap(struct scx_softc *sc)
   1232 {
   1233 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1234 	struct scx_txsoft *txs;
   1235 	uint32_t txstat;
   1236 	int i;
   1237 
   1238 	ifp->if_flags &= ~IFF_OACTIVE;
   1239 
   1240 	for (i = sc->sc_txsdirty; sc->sc_txsfree != SCX_TXQUEUELEN;
   1241 	     i = SCX_NEXTTXS(i), sc->sc_txsfree++) {
   1242 		txs = &sc->sc_txsoft[i];
   1243 
   1244 		SCX_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndesc,
   1245 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1246 
   1247 		txstat = sc->sc_txdescs[txs->txs_lastdesc].t0;
   1248 		if (txstat & T0_OWN) /* desc is still in use */
   1249 			break;
   1250 
   1251 		/* There is no way to tell transmission status per frame */
   1252 
   1253 		if_statinc(ifp, if_opackets);
   1254 
   1255 		sc->sc_txfree += txs->txs_ndesc;
   1256 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1257 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1258 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1259 		m_freem(txs->txs_mbuf);
   1260 		txs->txs_mbuf = NULL;
   1261 	}
   1262 	sc->sc_txsdirty = i;
   1263 	if (sc->sc_txsfree == SCX_TXQUEUELEN)
   1264 		ifp->if_timer = 0;
   1265 }
   1266 
   1267 static void
   1268 rxintr(struct scx_softc *sc)
   1269 {
   1270 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1271 	struct scx_rxsoft *rxs;
   1272 	struct mbuf *m;
   1273 	uint32_t rxstat;
   1274 	int i, len;
   1275 
   1276 	for (i = sc->sc_rxptr; /*CONSTCOND*/ 1; i = SCX_NEXTRX(i)) {
   1277 		rxs = &sc->sc_rxsoft[i];
   1278 
   1279 		SCX_CDRXSYNC(sc, i,
   1280 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1281 
   1282 		rxstat = sc->sc_rxdescs[i].r0;
   1283 		if (rxstat & R0_OWN) /* desc is left empty */
   1284 			break;
   1285 
   1286 		/* R0_FS | R0_LS must have been marked for this desc */
   1287 
   1288 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1289 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1290 
   1291 		len = sc->sc_rxdescs[i].r3 >> 16; /* 31:16 received */
   1292 		len -= ETHER_CRC_LEN;	/* Trim CRC off */
   1293 		m = rxs->rxs_mbuf;
   1294 
   1295 		if (add_rxbuf(sc, i) != 0) {
   1296 			if_statinc(ifp, if_ierrors);
   1297 			SCX_INIT_RXDESC(sc, i);
   1298 			bus_dmamap_sync(sc->sc_dmat,
   1299 			    rxs->rxs_dmamap, 0,
   1300 			    rxs->rxs_dmamap->dm_mapsize,
   1301 			    BUS_DMASYNC_PREREAD);
   1302 			continue;
   1303 		}
   1304 
   1305 		m_set_rcvif(m, ifp);
   1306 		m->m_pkthdr.len = m->m_len = len;
   1307 
   1308 		if (rxstat & R0_CSUM) {
   1309 			uint32_t csum = M_CSUM_IPv4;
   1310 			if (rxstat & R0_CERR)
   1311 				csum |= M_CSUM_IPv4_BAD;
   1312 			m->m_pkthdr.csum_flags |= csum;
   1313 		}
   1314 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1315 	}
   1316 	sc->sc_rxptr = i;
   1317 }
   1318 
   1319 static int
   1320 add_rxbuf(struct scx_softc *sc, int i)
   1321 {
   1322 	struct scx_rxsoft *rxs = &sc->sc_rxsoft[i];
   1323 	struct mbuf *m;
   1324 	int error;
   1325 
   1326 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1327 	if (m == NULL)
   1328 		return ENOBUFS;
   1329 
   1330 	MCLGET(m, M_DONTWAIT);
   1331 	if ((m->m_flags & M_EXT) == 0) {
   1332 		m_freem(m);
   1333 		return ENOBUFS;
   1334 	}
   1335 
   1336 	if (rxs->rxs_mbuf != NULL)
   1337 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1338 
   1339 	rxs->rxs_mbuf = m;
   1340 
   1341 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1342 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1343 	if (error) {
   1344 		aprint_error_dev(sc->sc_dev,
   1345 		    "can't load rx DMA map %d, error = %d\n", i, error);
   1346 		panic("add_rxbuf");
   1347 	}
   1348 
   1349 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1350 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1351 	SCX_INIT_RXDESC(sc, i);
   1352 
   1353 	return 0;
   1354 }
   1355 
   1356 static int
   1357 spin_waitfor(struct scx_softc *sc, int reg, int exist)
   1358 {
   1359 	int val, loop;
   1360 
   1361 	val = CSR_READ(sc, reg);
   1362 	if ((val & exist) == 0)
   1363 		return 0;
   1364 	loop = 3000;
   1365 	do {
   1366 		DELAY(10);
   1367 		val = CSR_READ(sc, reg);
   1368 	} while (--loop > 0 && (val & exist) != 0);
   1369 	return (loop > 0) ? 0 : ETIMEDOUT;
   1370 }
   1371 
   1372 static int
   1373 mac_read(struct scx_softc *sc, int reg)
   1374 {
   1375 
   1376 	CSR_WRITE(sc, MACCMD, reg);
   1377 	(void)spin_waitfor(sc, MACCMD, CMD_BUSY);
   1378 	return CSR_READ(sc, MACDATA);
   1379 }
   1380 
   1381 static void
   1382 mac_write(struct scx_softc *sc, int reg, int val)
   1383 {
   1384 
   1385 	CSR_WRITE(sc, MACDATA, val);
   1386 	CSR_WRITE(sc, MACCMD, reg | CMD_IOWR);
   1387 	(void)spin_waitfor(sc, MACCMD, CMD_BUSY);
   1388 }
   1389 
   1390 static int
   1391 get_garclk(uint32_t freq)
   1392 {
   1393 	int i;
   1394 
   1395 	for (i = 0; garclk[i].freq != 0; i++) {
   1396 		if (freq < garclk[i].freq)
   1397 			return garclk[i].bit;
   1398 	}
   1399 	return garclk[i - 1].bit;
   1400 }
   1401 
   1402 static void
   1403 loaducode(struct scx_softc *sc)
   1404 {
   1405 	uint32_t up, lo, sz;
   1406 	uint64_t addr;
   1407 
   1408 	up = EE_READ(sc, 0x08); /* H->M ucode addr high */
   1409 	lo = EE_READ(sc, 0x0c); /* H->M ucode addr low */
   1410 	sz = EE_READ(sc, 0x10); /* H->M ucode size */
   1411 	sz *= 4;
   1412 	addr = ((uint64_t)up << 32) | lo;
   1413 	aprint_normal_dev(sc->sc_dev, "H2M ucode %u\n", sz);
   1414 	injectucode(sc, DMACH2M, (bus_addr_t)addr, (bus_size_t)sz);
   1415 
   1416 	up = EE_READ(sc, 0x14); /* M->H ucode addr high */
   1417 	lo = EE_READ(sc, 0x18); /* M->H ucode addr low */
   1418 	sz = EE_READ(sc, 0x1c); /* M->H ucode size */
   1419 	sz *= 4;
   1420 	addr = ((uint64_t)up << 32) | lo;
   1421 	injectucode(sc, DMACM2H, (bus_addr_t)addr, (bus_size_t)sz);
   1422 	aprint_normal_dev(sc->sc_dev, "M2H ucode %u\n", sz);
   1423 
   1424 	lo = EE_READ(sc, 0x20); /* PKT ucode addr */
   1425 	sz = EE_READ(sc, 0x24); /* PKT ucode size */
   1426 	sz *= 4;
   1427 	injectucode(sc, DMACH2M, (bus_addr_t)lo, (bus_size_t)sz);
   1428 	aprint_normal_dev(sc->sc_dev, "PKT ucode %u\n", sz);
   1429 }
   1430 
   1431 static void
   1432 injectucode(struct scx_softc *sc, int port,
   1433 	bus_addr_t addr, bus_size_t size)
   1434 {
   1435 	bus_space_handle_t bsh;
   1436 	bus_size_t off;
   1437 	uint32_t ucode;
   1438 	int i;
   1439 
   1440 	const char *uengine[] = { "H2M", "M2H", "PKT" };
   1441 
   1442 printf("%s ucode %lx\n", uengine[port], addr);
   1443 	if (!bus_space_map(sc->sc_st, addr, size, 0, &bsh) != 0) {
   1444 		aprint_error_dev(sc->sc_dev, "eeprom map %s failure\n",
   1445 		    uengine[port]);
   1446 		return;
   1447 	}
   1448 	off = 0;
   1449 	for (i = 0; i < size; i++) {
   1450 		ucode = bus_space_read_4(sc->sc_st, bsh, off);
   1451 		CSR_WRITE(sc, port, ucode);
   1452 		off += 4;
   1453 	}
   1454 	bus_space_unmap(sc->sc_st, bsh, size);
   1455 }
   1456