xref: /netbsd-src/sys/arch/arm/gemini/if_gmc.c (revision 6312ecd1a0d759f5b8015c3dcff6b760232101e3)
1*6312ecd1Sthorpej /* $NetBSD: if_gmc.c,v 1.15 2022/09/17 19:46:59 thorpej Exp $ */
2145d2756Smatt /*-
3145d2756Smatt  * Copyright (c) 2008 The NetBSD Foundation, Inc.
4145d2756Smatt  * All rights reserved.
5145d2756Smatt  *
6145d2756Smatt  * This code is derived from software contributed to The NetBSD Foundation
7145d2756Smatt  * by Matt Thomas <matt@3am-software.com>
8145d2756Smatt  *
9145d2756Smatt  * Redistribution and use in source and binary forms, with or without
10145d2756Smatt  * modification, are permitted provided that the following conditions
11145d2756Smatt  * are met:
12145d2756Smatt  * 1. Redistributions of source code must retain the above copyright
13145d2756Smatt  *    notice, this list of conditions and the following disclaimer.
14145d2756Smatt  * 2. Redistributions in binary form must reproduce the above copyright
15145d2756Smatt  *    notice, this list of conditions and the following disclaimer in the
16145d2756Smatt  *    documentation and/or other materials provided with the distribution.
17145d2756Smatt  *
18145d2756Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19145d2756Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20145d2756Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21145d2756Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22145d2756Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23145d2756Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24145d2756Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25145d2756Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26145d2756Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27145d2756Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28145d2756Smatt  * POSSIBILITY OF SUCH DAMAGE.
29145d2756Smatt  */
30145d2756Smatt 
31145d2756Smatt #include <sys/param.h>
32145d2756Smatt #include <sys/callout.h>
33145d2756Smatt #include <sys/device.h>
34145d2756Smatt #include <sys/ioctl.h>
35145d2756Smatt #include <sys/kernel.h>
36145d2756Smatt #include <sys/kmem.h>
37145d2756Smatt #include <sys/mbuf.h>
38145d2756Smatt 
39cf10107dSdyoung #include <sys/bus.h>
40145d2756Smatt #include <machine/intr.h>
41145d2756Smatt 
42145d2756Smatt #include <arm/gemini/gemini_reg.h>
43145d2756Smatt #include <arm/gemini/gemini_gmacreg.h>
44145d2756Smatt #include <arm/gemini/gemini_gmacvar.h>
45145d2756Smatt 
46145d2756Smatt #include <net/if.h>
47145d2756Smatt #include <net/if_ether.h>
48145d2756Smatt #include <net/if_dl.h>
49145d2756Smatt 
50*6312ecd1Sthorpej __KERNEL_RCSID(0, "$NetBSD: if_gmc.c,v 1.15 2022/09/17 19:46:59 thorpej Exp $");
51145d2756Smatt 
52145d2756Smatt #define	MAX_TXSEG	32
53145d2756Smatt 
54145d2756Smatt struct gmc_softc {
55145d2756Smatt 	device_t sc_dev;
56145d2756Smatt 	struct gmac_softc *sc_psc;
57145d2756Smatt 	struct gmc_softc *sc_sibling;
58145d2756Smatt 	bus_dma_tag_t sc_dmat;
59145d2756Smatt 	bus_space_tag_t sc_iot;
60145d2756Smatt 	bus_space_handle_t sc_ioh;
61145d2756Smatt 	bus_space_handle_t sc_dma_ioh;
62145d2756Smatt 	bus_space_handle_t sc_gmac_ioh;
63145d2756Smatt 	struct ethercom sc_ec;
64145d2756Smatt 	struct mii_data sc_mii;
65145d2756Smatt 	void *sc_ih;
66145d2756Smatt 	bool sc_port1;
67a97f5310Smatt 	uint8_t sc_phy;
68145d2756Smatt 	gmac_hwqueue_t *sc_rxq;
69145d2756Smatt 	gmac_hwqueue_t *sc_txq[6];
70145d2756Smatt 	callout_t sc_mii_ch;
71145d2756Smatt 
72145d2756Smatt 	uint32_t sc_gmac_status;
73145d2756Smatt 	uint32_t sc_gmac_sta_add[3];
74145d2756Smatt 	uint32_t sc_gmac_mcast_filter[2];
75145d2756Smatt 	uint32_t sc_gmac_rx_filter;
76145d2756Smatt 	uint32_t sc_gmac_config[2];
77145d2756Smatt 	uint32_t sc_dmavr;
78145d2756Smatt 
79145d2756Smatt 	uint32_t sc_int_mask[5];
80145d2756Smatt 	uint32_t sc_int_enabled[5];
81145d2756Smatt };
82145d2756Smatt 
83145d2756Smatt #define	sc_if	sc_ec.ec_if
84145d2756Smatt 
85145d2756Smatt static bool
gmc_txqueue(struct gmc_softc * sc,gmac_hwqueue_t * hwq,struct mbuf * m)86145d2756Smatt gmc_txqueue(struct gmc_softc *sc, gmac_hwqueue_t *hwq, struct mbuf *m)
87145d2756Smatt {
88145d2756Smatt 	bus_dmamap_t map;
89a97f5310Smatt 	uint32_t desc0, desc1, desc3;
90145d2756Smatt 	struct mbuf *last_m, *m0;
91145d2756Smatt 	size_t count, i;
92145d2756Smatt 	int error;
93145d2756Smatt 	gmac_desc_t *d;
94145d2756Smatt 
95313f8b79Smatt 	KASSERT(hwq != NULL);
96313f8b79Smatt 
97145d2756Smatt 	map = gmac_mapcache_get(hwq->hwq_hqm->hqm_mc);
98145d2756Smatt 	if (map == NULL)
99145d2756Smatt 		return false;
100145d2756Smatt 
101145d2756Smatt 	for (last_m = NULL, m0 = m, count = 0;
102145d2756Smatt 	     m0 != NULL;
103145d2756Smatt 	     last_m = m0, m0 = m0->m_next) {
104145d2756Smatt 		vaddr_t addr = (uintptr_t)m0->m_data;
105145d2756Smatt 		if (m0->m_len == 0)
106145d2756Smatt 			continue;
107145d2756Smatt 		if (addr & 1) {
108145d2756Smatt 			if (last_m != NULL && M_TRAILINGSPACE(last_m) > 0) {
109145d2756Smatt 				last_m->m_data[last_m->m_len++] = *m->m_data++;
110145d2756Smatt 				m->m_len--;
111145d2756Smatt 			} else if (M_TRAILINGSPACE(m0) > 0) {
112145d2756Smatt 				memmove(m0->m_data + 1, m0->m_data, m0->m_len);
113145d2756Smatt 				m0->m_data++;
114145d2756Smatt 			} else if (M_LEADINGSPACE(m0) > 0) {
115145d2756Smatt 				memmove(m0->m_data - 1, m0->m_data, m0->m_len);
116145d2756Smatt 				m0->m_data--;
117145d2756Smatt 			} else {
118313f8b79Smatt 				panic("gmc_txqueue: odd addr %p", m0->m_data);
119145d2756Smatt 			}
120145d2756Smatt 		}
121145d2756Smatt 		count += ((addr & PGOFSET) + m->m_len + PGOFSET) >> PGSHIFT;
122145d2756Smatt 	}
123145d2756Smatt 
124145d2756Smatt 	gmac_hwqueue_sync(hwq);
125145d2756Smatt 	if (hwq->hwq_free <= count) {
126145d2756Smatt 		gmac_mapcache_put(hwq->hwq_hqm->hqm_mc, map);
127145d2756Smatt 		return false;
128145d2756Smatt 	}
129145d2756Smatt 
130145d2756Smatt 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
131313f8b79Smatt 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
132145d2756Smatt 	if (error) {
133145d2756Smatt 		aprint_error_dev(sc->sc_dev, "ifstart: load failed: %d\n",
134145d2756Smatt 		    error);
135145d2756Smatt 		gmac_mapcache_put(hwq->hwq_hqm->hqm_mc, map);
136145d2756Smatt 		m_freem(m);
1379b9ae5ecSthorpej 		if_statinc(&sc->sc_if, if_oerrors);
138145d2756Smatt 		return true;
139145d2756Smatt 	}
140145d2756Smatt 	KASSERT(map->dm_nsegs > 0);
141145d2756Smatt 
142145d2756Smatt 	/*
143145d2756Smatt 	 * Sync the mbuf contents to memory/cache.
144145d2756Smatt 	 */
145145d2756Smatt 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
146313f8b79Smatt 		BUS_DMASYNC_PREWRITE);
147145d2756Smatt 
148145d2756Smatt 	/*
149145d2756Smatt 	 * Now we need to load the descriptors...
150145d2756Smatt 	 */
151a97f5310Smatt 	desc0 = map->dm_nsegs << 16;
152145d2756Smatt 	desc1 = m->m_pkthdr.len;
153145d2756Smatt 	desc3 = DESC3_SOF;
154145d2756Smatt 	i = 0;
155313f8b79Smatt 	d = NULL;
156145d2756Smatt 	do {
157a97f5310Smatt #if 0
158313f8b79Smatt 		if (i > 0)
159a97f5310Smatt 			aprint_debug_dev(sc->sc_dev,
160313f8b79Smatt 			    "gmac_txqueue: %zu@%p=%#x/%#x/%#x/%#x\n",
161313f8b79Smatt 			    i-1, d, d->d_desc0, d->d_desc1,
162313f8b79Smatt 			    d->d_bufaddr, d->d_desc3);
163a97f5310Smatt #endif
164145d2756Smatt 		d = gmac_hwqueue_desc(hwq, i);
165145d2756Smatt 		KASSERT(map->dm_segs[i].ds_len > 0);
166145d2756Smatt 		KASSERT((map->dm_segs[i].ds_addr & 1) == 0);
167a97f5310Smatt 		d->d_desc0 = htole32(map->dm_segs[i].ds_len | desc0);
168313f8b79Smatt 		d->d_desc1 = htole32(desc1);
169313f8b79Smatt 		d->d_bufaddr = htole32(map->dm_segs[i].ds_addr);
170313f8b79Smatt 		d->d_desc3 = htole32(desc3);
171313f8b79Smatt 		desc3 = 0;
172145d2756Smatt 	} while (++i < map->dm_nsegs);
173145d2756Smatt 
174313f8b79Smatt 	d->d_desc3 |= htole32(DESC3_EOF | DESC3_EOFIE);
175a97f5310Smatt #if 0
176a97f5310Smatt 	aprint_debug_dev(sc->sc_dev,
177313f8b79Smatt 	    "gmac_txqueue: %zu@%p=%#x/%#x/%#x/%#x\n",
178313f8b79Smatt 	    i-1, d, d->d_desc0, d->d_desc1, d->d_bufaddr, d->d_desc3);
179a97f5310Smatt #endif
180145d2756Smatt 	M_SETCTX(m, map);
181145d2756Smatt 	IF_ENQUEUE(&hwq->hwq_ifq, m);
182145d2756Smatt 	/*
183145d2756Smatt 	 * Last descriptor has been marked.  Give them to the h/w.
184145d2756Smatt 	 * This will sync for us.
185145d2756Smatt 	 */
186145d2756Smatt 	gmac_hwqueue_produce(hwq, map->dm_nsegs);
187a97f5310Smatt #if 0
188a97f5310Smatt 	aprint_debug_dev(sc->sc_dev,
189313f8b79Smatt 	    "gmac_txqueue: *%zu@%p=%#x/%#x/%#x/%#x\n",
190313f8b79Smatt 	    i-1, d, d->d_desc0, d->d_desc1, d->d_bufaddr, d->d_desc3);
191a97f5310Smatt #endif
192145d2756Smatt 	return true;
193145d2756Smatt }
194145d2756Smatt 
195145d2756Smatt static void
gmc_filter_change(struct gmc_softc * sc)196145d2756Smatt gmc_filter_change(struct gmc_softc *sc)
197145d2756Smatt {
19883759283Smsaitoh 	struct ethercom *ec = &sc->sc_ec;
199145d2756Smatt 	struct ether_multi *enm;
200145d2756Smatt 	struct ether_multistep step;
201145d2756Smatt 	uint32_t mhash[2];
202145d2756Smatt 	uint32_t new0, new1, new2;
203145d2756Smatt 	const char * const eaddr = CLLADDR(sc->sc_if.if_sadl);
204145d2756Smatt 
205145d2756Smatt 	new0 = eaddr[0] | ((eaddr[1] | (eaddr[2] | (eaddr[3] << 8)) << 8) << 8);
206145d2756Smatt 	new1 = eaddr[4] | (eaddr[5] << 8);
207145d2756Smatt 	new2 = 0;
208145d2756Smatt 	if (sc->sc_gmac_sta_add[0] != new0
209145d2756Smatt 	    || sc->sc_gmac_sta_add[1] != new1
210145d2756Smatt 	    || sc->sc_gmac_sta_add[2] != new2) {
211145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_STA_ADD0,
212145d2756Smatt 		    new0);
213145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_STA_ADD1,
214145d2756Smatt 		    new1);
215145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_STA_ADD2,
216145d2756Smatt 		    new2);
217145d2756Smatt 		sc->sc_gmac_sta_add[0] = new0;
218145d2756Smatt 		sc->sc_gmac_sta_add[1] = new1;
219145d2756Smatt 		sc->sc_gmac_sta_add[2] = new2;
220145d2756Smatt 	}
221145d2756Smatt 
222145d2756Smatt 	mhash[0] = 0;
223145d2756Smatt 	mhash[1] = 0;
22483759283Smsaitoh 	ETHER_LOCK(ec);
22583759283Smsaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
226145d2756Smatt 	while (enm != NULL) {
227145d2756Smatt 		size_t i;
228145d2756Smatt 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
229145d2756Smatt 			mhash[0] = mhash[1] = 0xffffffff;
230145d2756Smatt 			break;
231145d2756Smatt 		}
232145d2756Smatt 		i = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
233313f8b79Smatt 		mhash[(i >> 5) & 1] |= 1 << (i & 31);
234145d2756Smatt 		ETHER_NEXT_MULTI(step, enm);
235145d2756Smatt 	}
23683759283Smsaitoh 	ETHER_UNLOCK(ec);
237145d2756Smatt 
238145d2756Smatt 	if (sc->sc_gmac_mcast_filter[0] != mhash[0]
239145d2756Smatt 	    || sc->sc_gmac_mcast_filter[1] != mhash[1]) {
240145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh,
241145d2756Smatt 		    GMAC_MCAST_FILTER0, mhash[0]);
242145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh,
243145d2756Smatt 		    GMAC_MCAST_FILTER1, mhash[1]);
244145d2756Smatt 		sc->sc_gmac_mcast_filter[0] = mhash[0];
245145d2756Smatt 		sc->sc_gmac_mcast_filter[1] = mhash[1];
246145d2756Smatt 	}
247145d2756Smatt 
248145d2756Smatt 	new0 = sc->sc_gmac_rx_filter & ~RXFILTER_PROMISC;
249145d2756Smatt 	new0 |= RXFILTER_BROADCAST | RXFILTER_UNICAST | RXFILTER_MULTICAST;
250145d2756Smatt 	if (sc->sc_if.if_flags & IFF_PROMISC)
251145d2756Smatt 		new0 |= RXFILTER_PROMISC;
252145d2756Smatt 
253145d2756Smatt 	if (new0 != sc->sc_gmac_rx_filter) {
254145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_RX_FILTER,
255145d2756Smatt 		    new0);
256145d2756Smatt 		sc->sc_gmac_rx_filter = new0;
257145d2756Smatt 	}
258145d2756Smatt }
259145d2756Smatt 
260145d2756Smatt static void
gmc_mii_tick(void * arg)261145d2756Smatt gmc_mii_tick(void *arg)
262145d2756Smatt {
263145d2756Smatt 	struct gmc_softc * const sc = arg;
264313f8b79Smatt 	struct gmac_softc * const psc = sc->sc_psc;
265145d2756Smatt 	int s = splnet();
266145d2756Smatt 
267313f8b79Smatt 	/*
268313f8b79Smatt 	 * If we had to increase the number of receive mbufs due to fifo
269313f8b79Smatt 	 * overflows, we need a way to decrease them.  So every second we
27039c3181aSmsaitoh 	 * receive less than or equal to MIN_RXMAPS packets, we decrement
271313f8b79Smatt 	 * swfree_min until it returns to MIN_RXMAPS.
272313f8b79Smatt 	 */
273313f8b79Smatt 	if (psc->sc_rxpkts_per_sec <= MIN_RXMAPS
274a97f5310Smatt 	    && psc->sc_swfree_min > MIN_RXMAPS) {
275313f8b79Smatt 		psc->sc_swfree_min--;
276a97f5310Smatt 		gmac_swfree_min_update(psc);
277a97f5310Smatt 	}
278313f8b79Smatt 	/*
279313f8b79Smatt 	 * If only one GMAC is running or this is port0, reset the count.
280313f8b79Smatt 	 */
281313f8b79Smatt 	if (psc->sc_running != 3 || !sc->sc_port1)
282313f8b79Smatt 		psc->sc_rxpkts_per_sec = 0;
283313f8b79Smatt 
284145d2756Smatt 	mii_tick(&sc->sc_mii);
285145d2756Smatt 	if (sc->sc_if.if_flags & IFF_RUNNING)
286145d2756Smatt 		callout_schedule(&sc->sc_mii_ch, hz);
287145d2756Smatt 
288145d2756Smatt 	splx(s);
289145d2756Smatt }
290145d2756Smatt 
291145d2756Smatt static int
gmc_mediachange(struct ifnet * ifp)292145d2756Smatt gmc_mediachange(struct ifnet *ifp)
293145d2756Smatt {
294145d2756Smatt 	struct gmc_softc * const sc = ifp->if_softc;
295145d2756Smatt 
296145d2756Smatt 	if ((ifp->if_flags & IFF_UP) == 0)
297145d2756Smatt 		return 0;
298145d2756Smatt 
299145d2756Smatt 	return mii_mediachg(&sc->sc_mii);
300145d2756Smatt }
301145d2756Smatt 
302145d2756Smatt static void
gmc_mediastatus(struct ifnet * ifp,struct ifmediareq * ifmr)303145d2756Smatt gmc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
304145d2756Smatt {
305145d2756Smatt 	struct gmc_softc * const sc = ifp->if_softc;
306145d2756Smatt 
307145d2756Smatt 	mii_pollstat(&sc->sc_mii);
308145d2756Smatt 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
309145d2756Smatt 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
310145d2756Smatt }
311145d2756Smatt 
312145d2756Smatt static void
gmc_mii_statchg(struct ifnet * ifp)3130bc32000Smatt gmc_mii_statchg(struct ifnet *ifp)
314145d2756Smatt {
3150bc32000Smatt 	struct gmc_softc * const sc = ifp->if_softc;
316145d2756Smatt 	uint32_t gmac_status;
317145d2756Smatt 
318145d2756Smatt 	gmac_status = sc->sc_gmac_status;
319a97f5310Smatt 
320313f8b79Smatt 	gmac_status &= ~STATUS_PHYMODE_MASK;
321a97f5310Smatt 	gmac_status |= STATUS_PHYMODE_RGMII_A;
322a97f5310Smatt 
323145d2756Smatt 	gmac_status &= ~STATUS_SPEED_MASK;
324145d2756Smatt 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_1000_T) {
325145d2756Smatt 		gmac_status |= STATUS_SPEED_1000M;
326145d2756Smatt 	} else if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX) {
327145d2756Smatt 		gmac_status |= STATUS_SPEED_100M;
328145d2756Smatt 	} else {
329145d2756Smatt 		gmac_status |= STATUS_SPEED_10M;
330145d2756Smatt 	}
331145d2756Smatt 
332145d2756Smatt 	if (sc->sc_mii.mii_media_active & IFM_FDX)
333145d2756Smatt 		gmac_status |= STATUS_DUPLEX_FULL;
334145d2756Smatt 	else
335145d2756Smatt 		gmac_status &= ~STATUS_DUPLEX_FULL;
336145d2756Smatt 
337a97f5310Smatt 	if (sc->sc_mii.mii_media_status & IFM_ACTIVE)
338145d2756Smatt 		gmac_status |= STATUS_LINK_ON;
339145d2756Smatt 	else
340145d2756Smatt 		gmac_status &= ~STATUS_LINK_ON;
341145d2756Smatt 
342145d2756Smatt 	if (sc->sc_gmac_status != gmac_status) {
343a97f5310Smatt 		aprint_debug_dev(sc->sc_dev,
344313f8b79Smatt 		    "status change old=%#x new=%#x active=%#x\n",
345313f8b79Smatt 		    sc->sc_gmac_status, gmac_status,
346313f8b79Smatt 		    sc->sc_mii.mii_media_active);
347145d2756Smatt 		sc->sc_gmac_status = gmac_status;
348145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_STATUS,
349145d2756Smatt 		    sc->sc_gmac_status);
350145d2756Smatt 	}
351a97f5310Smatt 
352a97f5310Smatt 	(*sc->sc_mii.mii_writereg)(sc->sc_dev, sc->sc_phy, 0x0018, 0x0041);
353145d2756Smatt }
354145d2756Smatt 
355145d2756Smatt static int
gmc_ifioctl(struct ifnet * ifp,u_long cmd,void * data)356145d2756Smatt gmc_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
357145d2756Smatt {
358145d2756Smatt 	struct gmc_softc * const sc = ifp->if_softc;
359145d2756Smatt 	int s;
360145d2756Smatt 	int error;
361145d2756Smatt 	s = splnet();
362145d2756Smatt 
363145d2756Smatt 	switch (cmd) {
364145d2756Smatt 	default:
365145d2756Smatt 		error = ether_ioctl(ifp, cmd, data);
366145d2756Smatt 		if (error == ENETRESET) {
367145d2756Smatt 			if (ifp->if_flags & IFF_RUNNING) {
368145d2756Smatt 				/*
369145d2756Smatt 				 * If the interface is running, we have to
370145d2756Smatt 				 * update its multicast filter.
371145d2756Smatt 				 */
372145d2756Smatt 				gmc_filter_change(sc);
373145d2756Smatt 			}
374145d2756Smatt 			error = 0;
375145d2756Smatt 		}
376145d2756Smatt 	}
377145d2756Smatt 
378145d2756Smatt 	splx(s);
379145d2756Smatt 	return error;
380145d2756Smatt }
381145d2756Smatt 
382145d2756Smatt static void
gmc_ifstart(struct ifnet * ifp)383145d2756Smatt gmc_ifstart(struct ifnet *ifp)
384145d2756Smatt {
385145d2756Smatt 	struct gmc_softc * const sc = ifp->if_softc;
386145d2756Smatt 
387a97f5310Smatt #if 0
388a97f5310Smatt 	if ((sc->sc_gmac_status & STATUS_LINK_ON) == 0)
389a97f5310Smatt 		return;
390a97f5310Smatt #endif
391a97f5310Smatt 	if ((ifp->if_flags & IFF_RUNNING) == 0)
392313f8b79Smatt 		return;
393313f8b79Smatt 
394145d2756Smatt 	for (;;) {
395145d2756Smatt 		struct mbuf *m;
39602852eeeSthorpej 		IF_POLL(&ifp->if_snd, m);
397145d2756Smatt 		if (m == NULL)
398145d2756Smatt 			break;
399145d2756Smatt 		if (!gmc_txqueue(sc, sc->sc_txq[0], m)) {
400145d2756Smatt 			break;
401145d2756Smatt 		}
40202852eeeSthorpej 		IF_DEQUEUE(&ifp->if_snd, m);
403145d2756Smatt 	}
404145d2756Smatt }
405145d2756Smatt 
406145d2756Smatt static void
gmc_ifstop(struct ifnet * ifp,int disable)407145d2756Smatt gmc_ifstop(struct ifnet *ifp, int disable)
408145d2756Smatt {
409145d2756Smatt 	struct gmc_softc * const sc = ifp->if_softc;
410145d2756Smatt 	struct gmac_softc * const psc = sc->sc_psc;
411145d2756Smatt 
412145d2756Smatt 	psc->sc_running &= ~(sc->sc_port1 ? 2 : 1);
413145d2756Smatt 	psc->sc_int_enabled[0] &= ~sc->sc_int_enabled[0];
414145d2756Smatt 	psc->sc_int_enabled[1] &= ~sc->sc_int_enabled[1];
415145d2756Smatt 	psc->sc_int_enabled[2] &= ~sc->sc_int_enabled[2];
416145d2756Smatt 	psc->sc_int_enabled[3] &= ~sc->sc_int_enabled[3];
417145d2756Smatt 	psc->sc_int_enabled[4] &= ~sc->sc_int_enabled[4] | INT4_SW_FREEQ_EMPTY;
418145d2756Smatt 	if (psc->sc_running == 0) {
419145d2756Smatt 		psc->sc_int_enabled[4] &= ~INT4_SW_FREEQ_EMPTY;
420145d2756Smatt 		KASSERT(psc->sc_int_enabled[0] == 0);
421145d2756Smatt 		KASSERT(psc->sc_int_enabled[1] == 0);
422145d2756Smatt 		KASSERT(psc->sc_int_enabled[2] == 0);
423145d2756Smatt 		KASSERT(psc->sc_int_enabled[3] == 0);
424145d2756Smatt 		KASSERT(psc->sc_int_enabled[4] == 0);
425145d2756Smatt 	} else if (((psc->sc_int_select[4] & INT4_SW_FREEQ_EMPTY) != 0)
426145d2756Smatt 			== sc->sc_port1) {
427145d2756Smatt 		psc->sc_int_select[4] &= ~INT4_SW_FREEQ_EMPTY;
428145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT4_MASK,
429145d2756Smatt 		    psc->sc_int_select[4]);
430145d2756Smatt 	}
431145d2756Smatt 	gmac_intr_update(psc);
432145d2756Smatt 	if (disable) {
433145d2756Smatt #if 0
434145d2756Smatt 		if (psc->sc_running == 0) {
435145d2756Smatt 			gmac_mapcache_destroy(&psc->sc_txmaps);
436145d2756Smatt 			gmac_mapcache_destroy(&psc->sc_rxmaps);
437145d2756Smatt 		}
438145d2756Smatt #endif
439145d2756Smatt 	}
440145d2756Smatt }
441145d2756Smatt 
442145d2756Smatt static int
gmc_ifinit(struct ifnet * ifp)443145d2756Smatt gmc_ifinit(struct ifnet *ifp)
444145d2756Smatt {
445145d2756Smatt 	struct gmc_softc * const sc = ifp->if_softc;
446145d2756Smatt 	struct gmac_softc * const psc = sc->sc_psc;
447145d2756Smatt 	uint32_t new, mask;
448145d2756Smatt 
449145d2756Smatt 	gmac_mapcache_fill(psc->sc_rxmaps, MIN_RXMAPS);
450145d2756Smatt 	gmac_mapcache_fill(psc->sc_txmaps, MIN_TXMAPS);
451145d2756Smatt 
452145d2756Smatt 	if (sc->sc_rxq == NULL) {
453145d2756Smatt 		gmac_hwqmem_t *hqm;
454a97f5310Smatt 		hqm = gmac_hwqmem_create(psc->sc_rxmaps, 16, /*RXQ_NDESCS,*/ 1,
455145d2756Smatt 		   HQM_CONSUMER | HQM_RX);
456145d2756Smatt 		sc->sc_rxq = gmac_hwqueue_create(hqm, sc->sc_iot,
457313f8b79Smatt 		    sc->sc_ioh, GMAC_DEF_RXQn_RWPTR(sc->sc_port1),
458313f8b79Smatt 		    GMAC_DEF_RXQn_BASE(sc->sc_port1), 0);
459145d2756Smatt 		if (sc->sc_rxq == NULL) {
460145d2756Smatt 			gmac_hwqmem_destroy(hqm);
461145d2756Smatt 			goto failed;
462145d2756Smatt 		}
463145d2756Smatt 		sc->sc_rxq->hwq_ifp = ifp;
464145d2756Smatt 		sc->sc_rxq->hwq_producer = psc->sc_swfreeq;
465145d2756Smatt 	}
466145d2756Smatt 
467313f8b79Smatt 	if (sc->sc_txq[0] == NULL) {
468145d2756Smatt 		gmac_hwqueue_t *hwq, *last_hwq;
469145d2756Smatt 		gmac_hwqmem_t *hqm;
470145d2756Smatt 		size_t i;
471145d2756Smatt 
472145d2756Smatt 		hqm = gmac_hwqmem_create(psc->sc_txmaps, TXQ_NDESCS, 6,
473145d2756Smatt 		   HQM_PRODUCER | HQM_TX);
474313f8b79Smatt 		KASSERT(hqm != NULL);
475145d2756Smatt 		for (i = 0; i < __arraycount(sc->sc_txq); i++) {
476145d2756Smatt 			sc->sc_txq[i] = gmac_hwqueue_create(hqm, sc->sc_iot,
477145d2756Smatt 			    sc->sc_dma_ioh, GMAC_SW_TX_Qn_RWPTR(i),
478145d2756Smatt 			    GMAC_SW_TX_Q_BASE, i);
479145d2756Smatt 			if (sc->sc_txq[i] == NULL) {
480145d2756Smatt 				if (i == 0)
481145d2756Smatt 					gmac_hwqmem_destroy(hqm);
482145d2756Smatt 				goto failed;
483145d2756Smatt 			}
484145d2756Smatt 			sc->sc_txq[i]->hwq_ifp = ifp;
485145d2756Smatt 
486145d2756Smatt 			last_hwq = NULL;
487145d2756Smatt 			SLIST_FOREACH(hwq, &psc->sc_hwfreeq->hwq_producers,
488145d2756Smatt 			    hwq_link) {
489145d2756Smatt 				if (sc->sc_txq[i]->hwq_qoff < hwq->hwq_qoff)
490145d2756Smatt 					break;
491145d2756Smatt 				last_hwq = hwq;
492145d2756Smatt 			}
493145d2756Smatt 			if (last_hwq == NULL)
494145d2756Smatt 				SLIST_INSERT_HEAD(
495145d2756Smatt 				    &psc->sc_hwfreeq->hwq_producers,
496145d2756Smatt 				    sc->sc_txq[i], hwq_link);
497145d2756Smatt 			else
498145d2756Smatt 				SLIST_INSERT_AFTER(last_hwq, sc->sc_txq[i],
499145d2756Smatt 				    hwq_link);
500145d2756Smatt 		}
501145d2756Smatt 	}
502145d2756Smatt 
503145d2756Smatt 	gmc_filter_change(sc);
504145d2756Smatt 
505145d2756Smatt 	mask = DMAVR_LOOPBACK | DMAVR_DROP_SMALL_ACK | DMAVR_EXTRABYTES_MASK
506145d2756Smatt 	    | DMAVR_RXBURSTSIZE_MASK | DMAVR_RXBUSWIDTH_MASK
507145d2756Smatt 	    | DMAVR_TXBURSTSIZE_MASK | DMAVR_TXBUSWIDTH_MASK;
508a97f5310Smatt 	new = DMAVR_RXDMA_ENABLE | DMAVR_TXDMA_ENABLE
509145d2756Smatt 	    | DMAVR_EXTRABYTES(2)
510145d2756Smatt 	    | DMAVR_RXBURSTSIZE(DMAVR_BURSTSIZE_32W)
511145d2756Smatt 	    | DMAVR_RXBUSWIDTH(DMAVR_BUSWIDTH_32BITS)
512145d2756Smatt 	    | DMAVR_TXBURSTSIZE(DMAVR_BURSTSIZE_32W)
513145d2756Smatt 	    | DMAVR_TXBUSWIDTH(DMAVR_BUSWIDTH_32BITS);
514145d2756Smatt 	new |= sc->sc_dmavr & ~mask;
515145d2756Smatt 	if (sc->sc_dmavr != new) {
516145d2756Smatt 		sc->sc_dmavr = new;
517145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_dma_ioh, GMAC_DMAVR,
518145d2756Smatt 		    sc->sc_dmavr);
519a97f5310Smatt 		aprint_debug_dev(sc->sc_dev, "gmc_ifinit: dmavr=%#x/%#x\n",
520313f8b79Smatt 		    sc->sc_dmavr,
521313f8b79Smatt 		    bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh, GMAC_DMAVR));
522145d2756Smatt 	}
523145d2756Smatt 
524a97f5310Smatt 	mask = CONFIG0_MAXLEN_MASK | CONFIG0_TX_DISABLE | CONFIG0_RX_DISABLE
525a97f5310Smatt 	    | CONFIG0_LOOPBACK |/*CONFIG0_SIM_TEST|*/CONFIG0_INVERSE_RXC_RGMII
526a97f5310Smatt 	    | CONFIG0_RGMII_INBAND_STATUS_ENABLE;
527a97f5310Smatt 	new = CONFIG0_MAXLEN(CONFIG0_MAXLEN_1536) | CONFIG0_R_LATCHED_MMII;
528145d2756Smatt 	new |= (sc->sc_gmac_config[0] & ~mask);
529145d2756Smatt 	if (sc->sc_gmac_config[0] != new) {
530145d2756Smatt 		sc->sc_gmac_config[0] = new;
531313f8b79Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_CONFIG0,
532145d2756Smatt 		    sc->sc_gmac_config[0]);
533a97f5310Smatt 		aprint_debug_dev(sc->sc_dev, "gmc_ifinit: config0=%#x/%#x\n",
534313f8b79Smatt 		    sc->sc_gmac_config[0],
535313f8b79Smatt 		    bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh, GMAC_CONFIG0));
536145d2756Smatt 	}
537145d2756Smatt 
538a97f5310Smatt 	psc->sc_rxpkts_per_sec +=
539a97f5310Smatt 	    gmac_rxproduce(psc->sc_swfreeq, psc->sc_swfree_min);
540145d2756Smatt 
541145d2756Smatt 	/*
542145d2756Smatt 	 * If we will be the only active interface, make sure the sw freeq
543145d2756Smatt 	 * interrupt gets routed to use.
544145d2756Smatt 	 */
545145d2756Smatt 	if (psc->sc_running == 0
546145d2756Smatt 	    && (((psc->sc_int_select[4] & INT4_SW_FREEQ_EMPTY) != 0) != sc->sc_port1)) {
547145d2756Smatt 		psc->sc_int_select[4] ^= INT4_SW_FREEQ_EMPTY;
548145d2756Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT4_MASK,
549145d2756Smatt 		    psc->sc_int_select[4]);
550145d2756Smatt 	}
551145d2756Smatt 	sc->sc_int_enabled[0] = sc->sc_int_mask[0]
552145d2756Smatt 	    & (INT0_TXDERR|INT0_TXPERR|INT0_RXDERR|INT0_RXPERR|INT0_SWTXQ_EOF);
553145d2756Smatt 	sc->sc_int_enabled[1] = sc->sc_int_mask[1] & INT1_DEF_RXQ_EOF;
554145d2756Smatt 	sc->sc_int_enabled[4] = INT4_SW_FREEQ_EMPTY | (sc->sc_int_mask[4]
555145d2756Smatt 	    & (INT4_TX_FAIL | INT4_MIB_HEMIWRAP | INT4_RX_FIFO_OVRN
556145d2756Smatt 	       | INT4_RGMII_STSCHG));
557145d2756Smatt 
558145d2756Smatt 	psc->sc_int_enabled[0] |= sc->sc_int_enabled[0];
559145d2756Smatt 	psc->sc_int_enabled[1] |= sc->sc_int_enabled[1];
560145d2756Smatt 	psc->sc_int_enabled[4] |= sc->sc_int_enabled[4];
561145d2756Smatt 
562145d2756Smatt 	gmac_intr_update(psc);
563145d2756Smatt 
564145d2756Smatt 	if ((ifp->if_flags & IFF_RUNNING) == 0)
565145d2756Smatt 		mii_tick(&sc->sc_mii);
566145d2756Smatt 
567145d2756Smatt 	ifp->if_flags |= IFF_RUNNING;
568145d2756Smatt 	psc->sc_running |= (sc->sc_port1 ? 2 : 1);
569145d2756Smatt 
570145d2756Smatt 	callout_schedule(&sc->sc_mii_ch, hz);
571145d2756Smatt 
572145d2756Smatt 	return 0;
573145d2756Smatt 
574145d2756Smatt failed:
575145d2756Smatt 	gmc_ifstop(ifp, true);
576145d2756Smatt 	return ENOMEM;
577145d2756Smatt }
578145d2756Smatt 
579145d2756Smatt static int
gmc_intr(void * arg)580145d2756Smatt gmc_intr(void *arg)
581145d2756Smatt {
582145d2756Smatt 	struct gmc_softc * const sc = arg;
583145d2756Smatt 	uint32_t int0_status, int1_status, int4_status;
584145d2756Smatt 	uint32_t status;
585145d2756Smatt 	bool do_ifstart = false;
586145d2756Smatt 	int rv = 0;
587145d2756Smatt 
588a97f5310Smatt 	aprint_debug_dev(sc->sc_dev, "gmac_intr: entry\n");
589a97f5310Smatt 
590145d2756Smatt 	int0_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
591145d2756Smatt 	    GMAC_INT0_STATUS);
592145d2756Smatt 	int1_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
593145d2756Smatt 	    GMAC_INT1_STATUS);
594145d2756Smatt 	int4_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
595145d2756Smatt 	    GMAC_INT4_STATUS);
596145d2756Smatt 
597a97f5310Smatt 	aprint_debug_dev(sc->sc_dev, "gmac_intr: sts=%#x/%#x/%#x/%#x/%#x\n",
598313f8b79Smatt 	    int0_status, int1_status,
599313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT2_STATUS),
600313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT3_STATUS),
601313f8b79Smatt 	    int4_status);
602313f8b79Smatt 
603a97f5310Smatt #if 0
604a97f5310Smatt 	aprint_debug_dev(sc->sc_dev, "gmac_intr: mask=%#x/%#x/%#x/%#x/%#x\n",
605313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT0_MASK),
606313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT1_MASK),
607313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT2_MASK),
608313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT3_MASK),
609313f8b79Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT4_MASK));
610a97f5310Smatt #endif
611313f8b79Smatt 
612145d2756Smatt 	status = int0_status & sc->sc_int_mask[0];
613145d2756Smatt 	if (status & (INT0_TXDERR | INT0_TXPERR)) {
614145d2756Smatt 		aprint_error_dev(sc->sc_dev,
615313f8b79Smatt 		    "transmit%s%s error: %#x %08x bufaddr %#x\n",
616145d2756Smatt 		    status & INT0_TXDERR ? " data" : "",
617145d2756Smatt 		    status & INT0_TXPERR ? " protocol" : "",
618145d2756Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
619313f8b79Smatt 		    GMAC_DMA_TX_CUR_DESC),
620313f8b79Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
621313f8b79Smatt 		    GMAC_SW_TX_Q0_RWPTR),
622313f8b79Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
623145d2756Smatt 		    GMAC_DMA_TX_DESC2));
624a97f5310Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT0_STATUS,
625a97f5310Smatt 		    status & (INT0_TXDERR | INT0_TXPERR));
626313f8b79Smatt 		Debugger();
627145d2756Smatt 	}
628145d2756Smatt 	if (status & (INT0_RXDERR | INT0_RXPERR)) {
629145d2756Smatt 		aprint_error_dev(sc->sc_dev,
630a97f5310Smatt 		    "receive%s%s error: %#x %#x=%#x/%#x/%#x/%#x\n",
631a97f5310Smatt 		    status & INT0_RXDERR ? " data" : "",
632a97f5310Smatt 		    status & INT0_RXPERR ? " protocol" : "",
633145d2756Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
634313f8b79Smatt 		    GMAC_DMA_RX_CUR_DESC),
635313f8b79Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_ioh,
636a97f5310Smatt 		    GMAC_SWFREEQ_RWPTR),
637313f8b79Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
638a97f5310Smatt 		    GMAC_DMA_RX_DESC0),
639a97f5310Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
640a97f5310Smatt 		    GMAC_DMA_RX_DESC1),
641a97f5310Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
642a97f5310Smatt 		    GMAC_DMA_RX_DESC2),
643a97f5310Smatt 		bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh,
644a97f5310Smatt 		    GMAC_DMA_RX_DESC3));
645a97f5310Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT0_STATUS,
646a97f5310Smatt 		    status & (INT0_RXDERR | INT0_RXPERR));
647313f8b79Smatt 		    Debugger();
648145d2756Smatt 	}
649145d2756Smatt 	if (status & INT0_SWTXQ_EOF) {
650145d2756Smatt 		status &= INT0_SWTXQ_EOF;
651145d2756Smatt 		for (int i = 0; status && i < __arraycount(sc->sc_txq); i++) {
652145d2756Smatt 			if (status & INT0_SWTXQn_EOF(i)) {
653145d2756Smatt 				gmac_hwqueue_sync(sc->sc_txq[i]);
654a97f5310Smatt 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
655a97f5310Smatt 				    GMAC_INT0_STATUS,
656811add33Smsaitoh 				    sc->sc_int_mask[0] & (INT0_SWTXQn_EOF(i)
657811add33Smsaitoh 					| INT0_SWTXQn_FIN(i)));
658145d2756Smatt 				status &= ~INT0_SWTXQn_EOF(i);
659145d2756Smatt 			}
660145d2756Smatt 		}
661145d2756Smatt 		do_ifstart = true;
662145d2756Smatt 		rv = 1;
663145d2756Smatt 	}
664145d2756Smatt 
665a97f5310Smatt 	if (int4_status & INT4_SW_FREEQ_EMPTY) {
666a97f5310Smatt 		struct gmac_softc * const psc = sc->sc_psc;
667a97f5310Smatt 		psc->sc_rxpkts_per_sec +=
668a97f5310Smatt 		    gmac_rxproduce(psc->sc_swfreeq, psc->sc_swfree_min);
669a97f5310Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT4_STATUS,
670a97f5310Smatt 		    status & INT4_SW_FREEQ_EMPTY);
671a97f5310Smatt 		rv = 1;
672a97f5310Smatt 	}
673a97f5310Smatt 
674145d2756Smatt 	status = int1_status & sc->sc_int_mask[1];
675145d2756Smatt 	if (status & INT1_DEF_RXQ_EOF) {
676a97f5310Smatt 		struct gmac_softc * const psc = sc->sc_psc;
677a97f5310Smatt 		psc->sc_rxpkts_per_sec +=
678a97f5310Smatt 		    gmac_hwqueue_consume(sc->sc_rxq, psc->sc_swfree_min);
679a97f5310Smatt 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT1_STATUS,
680a97f5310Smatt 		    status & INT1_DEF_RXQ_EOF);
681145d2756Smatt 		rv = 1;
682145d2756Smatt 	}
683313f8b79Smatt 
684145d2756Smatt 	status = int4_status & sc->sc_int_enabled[4];
685145d2756Smatt 	if (status & INT4_TX_FAIL) {
686145d2756Smatt 	}
687145d2756Smatt 	if (status & INT4_MIB_HEMIWRAP) {
688145d2756Smatt 	}
689145d2756Smatt 	if (status & INT4_RX_XON) {
690145d2756Smatt 	}
691145d2756Smatt 	if (status & INT4_RX_XOFF) {
692145d2756Smatt 	}
693145d2756Smatt 	if (status & INT4_TX_XON) {
694145d2756Smatt 	}
695145d2756Smatt 	if (status & INT4_TX_XOFF) {
696145d2756Smatt 	}
697145d2756Smatt 	if (status & INT4_RX_FIFO_OVRN) {
698a97f5310Smatt #if 0
699a97f5310Smatt 		if (sc->sc_psc->sc_swfree_min < MAX_RXMAPS) {
700313f8b79Smatt 			sc->sc_psc->sc_swfree_min++;
701a97f5310Smatt 			gmac_swfree_min_update(psc);
702a97f5310Smatt 		}
703a97f5310Smatt #endif
7049b9ae5ecSthorpej 		if_statinc(&sc->sc_if, if_ierrors);
705145d2756Smatt 	}
706145d2756Smatt 	if (status & INT4_RGMII_STSCHG) {
7070fe9644dSmsaitoh 		mii_pollstat(&sc->sc_mii);
708145d2756Smatt 	}
709a97f5310Smatt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GMAC_INT4_STATUS, status);
710145d2756Smatt 
711145d2756Smatt 	if (do_ifstart)
712fa599b2eSozaki-r 		if_schedule_deferred_start(&sc->sc_if);
713145d2756Smatt 
714a97f5310Smatt 	aprint_debug_dev(sc->sc_dev, "gmac_intr: sts=%#x/%#x/%#x/%#x/%#x\n",
715a97f5310Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT0_STATUS),
716a97f5310Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT1_STATUS),
717a97f5310Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT2_STATUS),
718a97f5310Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT3_STATUS),
719a97f5310Smatt 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, GMAC_INT4_STATUS));
720a97f5310Smatt 	aprint_debug_dev(sc->sc_dev, "gmac_intr: exit rv=%d\n", rv);
721145d2756Smatt 	return rv;
722145d2756Smatt }
723145d2756Smatt 
724145d2756Smatt static int
gmc_match(device_t parent,cfdata_t cf,void * aux)725145d2756Smatt gmc_match(device_t parent, cfdata_t cf, void *aux)
726145d2756Smatt {
727145d2756Smatt 	struct gmac_softc *psc = device_private(parent);
728145d2756Smatt 	struct gmac_attach_args *gma = aux;
729145d2756Smatt 
730145d2756Smatt 	if ((unsigned int)gma->gma_phy > 31)
731145d2756Smatt 		return 0;
732145d2756Smatt 	if ((unsigned int)gma->gma_port > 1)
733145d2756Smatt 		return 0;
734145d2756Smatt 	if (gma->gma_intr < 1 || gma->gma_intr > 2)
735145d2756Smatt 		return 0;
736145d2756Smatt 
737145d2756Smatt 	if (psc->sc_ports & (1 << gma->gma_port))
738145d2756Smatt 		return 0;
739145d2756Smatt 
740145d2756Smatt 	return 1;
741145d2756Smatt }
742145d2756Smatt 
743145d2756Smatt static void
gmc_attach(device_t parent,device_t self,void * aux)744145d2756Smatt gmc_attach(device_t parent, device_t self, void *aux)
745145d2756Smatt {
746145d2756Smatt 	struct gmac_softc * const psc = device_private(parent);
747145d2756Smatt 	struct gmc_softc * const sc = device_private(self);
748145d2756Smatt 	struct gmac_attach_args *gma = aux;
749145d2756Smatt 	struct ifnet * const ifp = &sc->sc_if;
750811add33Smsaitoh 	struct mii_data * const mii = &sc->sc_mii;
751145d2756Smatt 	static const char eaddrs[2][6] = {
752145d2756Smatt 		"\x0\x52\xc3\x11\x22\x33",
753145d2756Smatt 		"\x0\x52\xc3\x44\x55\x66",
754145d2756Smatt 	};
755145d2756Smatt 
756145d2756Smatt 	psc->sc_ports |= 1 << gma->gma_port;
757145d2756Smatt 	sc->sc_port1 = (gma->gma_port == 1);
758a97f5310Smatt 	sc->sc_phy = gma->gma_phy;
759145d2756Smatt 
760145d2756Smatt 	sc->sc_dev = self;
761145d2756Smatt 	sc->sc_psc = psc;
762145d2756Smatt 	sc->sc_iot = psc->sc_iot;
763145d2756Smatt 	sc->sc_ioh = psc->sc_ioh;
764145d2756Smatt 	sc->sc_dmat = psc->sc_dmat;
765145d2756Smatt 
766145d2756Smatt 	bus_space_subregion(sc->sc_iot, sc->sc_ioh,
767145d2756Smatt 	    GMAC_PORTn_DMA_OFFSET(gma->gma_port), GMAC_PORTn_DMA_SIZE,
768145d2756Smatt 	    &sc->sc_dma_ioh);
769145d2756Smatt 	bus_space_subregion(sc->sc_iot, sc->sc_ioh,
770145d2756Smatt 	    GMAC_PORTn_GMAC_OFFSET(gma->gma_port), GMAC_PORTn_GMAC_SIZE,
771145d2756Smatt 	    &sc->sc_gmac_ioh);
772145d2756Smatt 	aprint_normal("\n");
773145d2756Smatt 	aprint_naive("\n");
774145d2756Smatt 
775145d2756Smatt 	strlcpy(ifp->if_xname, device_xname(self), sizeof(ifp->if_xname));
776145d2756Smatt 	ifp->if_flags = IFF_SIMPLEX | IFF_MULTICAST | IFF_BROADCAST;
777145d2756Smatt 	ifp->if_softc = sc;
778145d2756Smatt 	ifp->if_ioctl = gmc_ifioctl;
779145d2756Smatt 	ifp->if_stop  = gmc_ifstop;
780145d2756Smatt 	ifp->if_start = gmc_ifstart;
781145d2756Smatt 	ifp->if_init  = gmc_ifinit;
782145d2756Smatt 
783145d2756Smatt 	IFQ_SET_READY(&ifp->if_snd);
784145d2756Smatt 
785145d2756Smatt 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
786811add33Smsaitoh 	sc->sc_ec.ec_mii = mii;
787145d2756Smatt 
788811add33Smsaitoh 	mii->mii_ifp = ifp;
789811add33Smsaitoh 	mii->mii_statchg = gmc_mii_statchg;
790811add33Smsaitoh 	mii->mii_readreg = gma->gma_mii_readreg;
791811add33Smsaitoh 	mii->mii_writereg = gma->gma_mii_writereg;
792145d2756Smatt 
793811add33Smsaitoh 	ifmedia_init(&mii->mii_media, 0, gmc_mediachange, gmc_mediastatus);
794145d2756Smatt 
795145d2756Smatt 	if_attach(ifp);
796fa599b2eSozaki-r 	if_deferred_start_init(ifp, NULL);
797145d2756Smatt 	ether_ifattach(ifp, eaddrs[gma->gma_port]);
798811add33Smsaitoh 	mii_attach(sc->sc_dev, mii, 0xffffffff,
799145d2756Smatt 	    gma->gma_phy, MII_OFFSET_ANY, 0);
800145d2756Smatt 
801811add33Smsaitoh 	if (LIST_EMPTY(&mii->mii_phys)) {
802811add33Smsaitoh 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
803811add33Smsaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
804145d2756Smatt 	} else {
805811add33Smsaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
806811add33Smsaitoh //		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_1000_T | IFM_FDX);
807145d2756Smatt 	}
808145d2756Smatt 
809145d2756Smatt 	sc->sc_gmac_status = bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh,
810145d2756Smatt 	    GMAC_STATUS);
811145d2756Smatt 	sc->sc_gmac_sta_add[0] = bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh,
812145d2756Smatt 	    GMAC_STA_ADD0);
813145d2756Smatt 	sc->sc_gmac_sta_add[1] = bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh,
814145d2756Smatt 	    GMAC_STA_ADD1);
815145d2756Smatt 	sc->sc_gmac_sta_add[2] = bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh,
816145d2756Smatt 	    GMAC_STA_ADD2);
817145d2756Smatt 	sc->sc_gmac_mcast_filter[0] = bus_space_read_4(sc->sc_iot,
818145d2756Smatt 	    sc->sc_gmac_ioh, GMAC_MCAST_FILTER0);
819145d2756Smatt 	sc->sc_gmac_mcast_filter[1] = bus_space_read_4(sc->sc_iot,
820145d2756Smatt 	    sc->sc_gmac_ioh, GMAC_MCAST_FILTER1);
821145d2756Smatt 	sc->sc_gmac_rx_filter = bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh,
822145d2756Smatt 	    GMAC_RX_FILTER);
823145d2756Smatt 	sc->sc_gmac_config[0] = bus_space_read_4(sc->sc_iot, sc->sc_gmac_ioh,
824145d2756Smatt 	    GMAC_CONFIG0);
825145d2756Smatt 	sc->sc_dmavr = bus_space_read_4(sc->sc_iot, sc->sc_dma_ioh, GMAC_DMAVR);
826145d2756Smatt 
827145d2756Smatt 	/* sc->sc_int_enabled is already zeroed */
828145d2756Smatt 	sc->sc_int_mask[0] = (sc->sc_port1 ? INT0_GMAC1 : INT0_GMAC0);
829145d2756Smatt 	sc->sc_int_mask[1] = (sc->sc_port1 ? INT1_GMAC1 : INT1_GMAC0);
830145d2756Smatt 	sc->sc_int_mask[2] = (sc->sc_port1 ? INT2_GMAC1 : INT2_GMAC0);
831145d2756Smatt 	sc->sc_int_mask[3] = (sc->sc_port1 ? INT3_GMAC1 : INT3_GMAC0);
832145d2756Smatt 	sc->sc_int_mask[4] = (sc->sc_port1 ? INT4_GMAC1 : INT4_GMAC0);
833145d2756Smatt 
834313f8b79Smatt 	if (!sc->sc_port1) {
835145d2756Smatt 	sc->sc_ih = intr_establish(gma->gma_intr, IPL_NET, IST_LEVEL_HIGH,
836145d2756Smatt 	    gmc_intr, sc);
837145d2756Smatt 	KASSERT(sc->sc_ih != NULL);
838313f8b79Smatt 	}
839145d2756Smatt 
840145d2756Smatt 	callout_init(&sc->sc_mii_ch, 0);
841145d2756Smatt 	callout_setfunc(&sc->sc_mii_ch, gmc_mii_tick, sc);
842145d2756Smatt 
843145d2756Smatt 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
844145d2756Smatt 	     ether_sprintf(CLLADDR(sc->sc_if.if_sadl)));
845145d2756Smatt }
846145d2756Smatt 
847145d2756Smatt CFATTACH_DECL_NEW(gmc, sizeof(struct gmc_softc),
848145d2756Smatt     gmc_match, gmc_attach, NULL, NULL);
849