gem.c revision 1.49 1 1.49 martin /* $NetBSD: gem.c,v 1.49 2006/11/23 18:41:25 martin Exp $ */
2 1.1 eeh
3 1.1 eeh /*
4 1.31 heas *
5 1.1 eeh * Copyright (C) 2001 Eduardo Horvath.
6 1.1 eeh * All rights reserved.
7 1.1 eeh *
8 1.1 eeh *
9 1.1 eeh * Redistribution and use in source and binary forms, with or without
10 1.1 eeh * modification, are permitted provided that the following conditions
11 1.1 eeh * are met:
12 1.1 eeh * 1. Redistributions of source code must retain the above copyright
13 1.1 eeh * notice, this list of conditions and the following disclaimer.
14 1.1 eeh * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 eeh * notice, this list of conditions and the following disclaimer in the
16 1.1 eeh * documentation and/or other materials provided with the distribution.
17 1.31 heas *
18 1.1 eeh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
19 1.1 eeh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 eeh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 eeh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
22 1.1 eeh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 eeh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 eeh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 eeh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 eeh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 eeh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 eeh * SUCH DAMAGE.
29 1.1 eeh *
30 1.1 eeh */
31 1.1 eeh
32 1.1 eeh /*
33 1.1 eeh * Driver for Sun GEM ethernet controllers.
34 1.1 eeh */
35 1.10 lukem
36 1.10 lukem #include <sys/cdefs.h>
37 1.49 martin __KERNEL_RCSID(0, "$NetBSD: gem.c,v 1.49 2006/11/23 18:41:25 martin Exp $");
38 1.1 eeh
39 1.35 heas #include "opt_inet.h"
40 1.1 eeh #include "bpfilter.h"
41 1.1 eeh
42 1.1 eeh #include <sys/param.h>
43 1.31 heas #include <sys/systm.h>
44 1.1 eeh #include <sys/callout.h>
45 1.31 heas #include <sys/mbuf.h>
46 1.1 eeh #include <sys/syslog.h>
47 1.1 eeh #include <sys/malloc.h>
48 1.1 eeh #include <sys/kernel.h>
49 1.1 eeh #include <sys/socket.h>
50 1.1 eeh #include <sys/ioctl.h>
51 1.1 eeh #include <sys/errno.h>
52 1.1 eeh #include <sys/device.h>
53 1.1 eeh
54 1.1 eeh #include <machine/endian.h>
55 1.1 eeh
56 1.1 eeh #include <uvm/uvm_extern.h>
57 1.31 heas
58 1.1 eeh #include <net/if.h>
59 1.1 eeh #include <net/if_dl.h>
60 1.1 eeh #include <net/if_media.h>
61 1.1 eeh #include <net/if_ether.h>
62 1.1 eeh
63 1.35 heas #ifdef INET
64 1.35 heas #include <netinet/in.h>
65 1.35 heas #include <netinet/in_systm.h>
66 1.35 heas #include <netinet/in_var.h>
67 1.35 heas #include <netinet/ip.h>
68 1.35 heas #include <netinet/tcp.h>
69 1.35 heas #include <netinet/udp.h>
70 1.35 heas #endif
71 1.35 heas
72 1.31 heas #if NBPFILTER > 0
73 1.1 eeh #include <net/bpf.h>
74 1.31 heas #endif
75 1.1 eeh
76 1.1 eeh #include <machine/bus.h>
77 1.1 eeh #include <machine/intr.h>
78 1.1 eeh
79 1.1 eeh #include <dev/mii/mii.h>
80 1.1 eeh #include <dev/mii/miivar.h>
81 1.1 eeh #include <dev/mii/mii_bitbang.h>
82 1.1 eeh
83 1.1 eeh #include <dev/ic/gemreg.h>
84 1.1 eeh #include <dev/ic/gemvar.h>
85 1.1 eeh
86 1.1 eeh #define TRIES 10000
87 1.1 eeh
88 1.41 christos static void gem_start(struct ifnet *);
89 1.41 christos static void gem_stop(struct ifnet *, int);
90 1.34 perry int gem_ioctl(struct ifnet *, u_long, caddr_t);
91 1.34 perry void gem_tick(void *);
92 1.34 perry void gem_watchdog(struct ifnet *);
93 1.34 perry void gem_shutdown(void *);
94 1.34 perry int gem_init(struct ifnet *);
95 1.1 eeh void gem_init_regs(struct gem_softc *sc);
96 1.1 eeh static int gem_ringsize(int sz);
97 1.41 christos static int gem_meminit(struct gem_softc *);
98 1.34 perry void gem_mifinit(struct gem_softc *);
99 1.41 christos static int gem_bitwait(struct gem_softc *sc, int, u_int32_t, u_int32_t);
100 1.34 perry void gem_reset(struct gem_softc *);
101 1.1 eeh int gem_reset_rx(struct gem_softc *sc);
102 1.1 eeh int gem_reset_tx(struct gem_softc *sc);
103 1.1 eeh int gem_disable_rx(struct gem_softc *sc);
104 1.1 eeh int gem_disable_tx(struct gem_softc *sc);
105 1.41 christos static void gem_rxdrain(struct gem_softc *sc);
106 1.1 eeh int gem_add_rxbuf(struct gem_softc *sc, int idx);
107 1.34 perry void gem_setladrf(struct gem_softc *);
108 1.1 eeh
109 1.1 eeh /* MII methods & callbacks */
110 1.34 perry static int gem_mii_readreg(struct device *, int, int);
111 1.34 perry static void gem_mii_writereg(struct device *, int, int, int);
112 1.34 perry static void gem_mii_statchg(struct device *);
113 1.34 perry
114 1.34 perry int gem_mediachange(struct ifnet *);
115 1.34 perry void gem_mediastatus(struct ifnet *, struct ifmediareq *);
116 1.34 perry
117 1.34 perry struct mbuf *gem_get(struct gem_softc *, int, int);
118 1.34 perry int gem_put(struct gem_softc *, int, struct mbuf *);
119 1.34 perry void gem_read(struct gem_softc *, int, int);
120 1.34 perry int gem_eint(struct gem_softc *, u_int);
121 1.34 perry int gem_rint(struct gem_softc *);
122 1.34 perry int gem_tint(struct gem_softc *);
123 1.34 perry void gem_power(int, void *);
124 1.1 eeh
125 1.1 eeh #ifdef GEM_DEBUG
126 1.1 eeh #define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
127 1.1 eeh printf x
128 1.1 eeh #else
129 1.1 eeh #define DPRINTF(sc, x) /* nothing */
130 1.1 eeh #endif
131 1.1 eeh
132 1.40 bouyer #define ETHER_MIN_TX (ETHERMIN + sizeof(struct ether_header))
133 1.40 bouyer
134 1.1 eeh
135 1.1 eeh /*
136 1.6 thorpej * gem_attach:
137 1.1 eeh *
138 1.1 eeh * Attach a Gem interface to the system.
139 1.1 eeh */
140 1.1 eeh void
141 1.6 thorpej gem_attach(sc, enaddr)
142 1.1 eeh struct gem_softc *sc;
143 1.6 thorpej const uint8_t *enaddr;
144 1.1 eeh {
145 1.1 eeh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
146 1.1 eeh struct mii_data *mii = &sc->sc_mii;
147 1.1 eeh struct mii_softc *child;
148 1.15 matt struct ifmedia_entry *ifm;
149 1.1 eeh int i, error;
150 1.15 matt u_int32_t v;
151 1.40 bouyer char *nullbuf;
152 1.1 eeh
153 1.1 eeh /* Make sure the chip is stopped. */
154 1.1 eeh ifp->if_softc = sc;
155 1.1 eeh gem_reset(sc);
156 1.1 eeh
157 1.1 eeh /*
158 1.1 eeh * Allocate the control data structures, and create and load the
159 1.40 bouyer * DMA map for it. gem_control_data is 9216 bytes, we have space for
160 1.40 bouyer * the padding buffer in the bus_dmamem_alloc()'d memory.
161 1.1 eeh */
162 1.1 eeh if ((error = bus_dmamem_alloc(sc->sc_dmatag,
163 1.40 bouyer sizeof(struct gem_control_data) + ETHER_MIN_TX, PAGE_SIZE,
164 1.40 bouyer 0, &sc->sc_cdseg, 1, &sc->sc_cdnseg, 0)) != 0) {
165 1.24 thorpej aprint_error(
166 1.24 thorpej "%s: unable to allocate control data, error = %d\n",
167 1.1 eeh sc->sc_dev.dv_xname, error);
168 1.1 eeh goto fail_0;
169 1.1 eeh }
170 1.1 eeh
171 1.1 eeh /* XXX should map this in with correct endianness */
172 1.1 eeh if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
173 1.1 eeh sizeof(struct gem_control_data), (caddr_t *)&sc->sc_control_data,
174 1.1 eeh BUS_DMA_COHERENT)) != 0) {
175 1.24 thorpej aprint_error("%s: unable to map control data, error = %d\n",
176 1.1 eeh sc->sc_dev.dv_xname, error);
177 1.1 eeh goto fail_1;
178 1.1 eeh }
179 1.1 eeh
180 1.40 bouyer nullbuf =
181 1.40 bouyer (caddr_t)sc->sc_control_data + sizeof(struct gem_control_data);
182 1.40 bouyer
183 1.1 eeh if ((error = bus_dmamap_create(sc->sc_dmatag,
184 1.1 eeh sizeof(struct gem_control_data), 1,
185 1.1 eeh sizeof(struct gem_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
186 1.24 thorpej aprint_error("%s: unable to create control data DMA map, "
187 1.1 eeh "error = %d\n", sc->sc_dev.dv_xname, error);
188 1.1 eeh goto fail_2;
189 1.1 eeh }
190 1.1 eeh
191 1.1 eeh if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
192 1.1 eeh sc->sc_control_data, sizeof(struct gem_control_data), NULL,
193 1.1 eeh 0)) != 0) {
194 1.24 thorpej aprint_error(
195 1.24 thorpej "%s: unable to load control data DMA map, error = %d\n",
196 1.1 eeh sc->sc_dev.dv_xname, error);
197 1.1 eeh goto fail_3;
198 1.1 eeh }
199 1.1 eeh
200 1.40 bouyer memset(nullbuf, 0, ETHER_MIN_TX);
201 1.40 bouyer if ((error = bus_dmamap_create(sc->sc_dmatag,
202 1.40 bouyer ETHER_MIN_TX, 1, ETHER_MIN_TX, 0, 0, &sc->sc_nulldmamap)) != 0) {
203 1.40 bouyer aprint_error("%s: unable to create padding DMA map, "
204 1.40 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
205 1.40 bouyer goto fail_4;
206 1.40 bouyer }
207 1.40 bouyer
208 1.40 bouyer if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_nulldmamap,
209 1.40 bouyer nullbuf, ETHER_MIN_TX, NULL, 0)) != 0) {
210 1.40 bouyer aprint_error(
211 1.40 bouyer "%s: unable to load padding DMA map, error = %d\n",
212 1.40 bouyer sc->sc_dev.dv_xname, error);
213 1.40 bouyer goto fail_5;
214 1.40 bouyer }
215 1.40 bouyer
216 1.40 bouyer bus_dmamap_sync(sc->sc_dmatag, sc->sc_nulldmamap, 0, ETHER_MIN_TX,
217 1.40 bouyer BUS_DMASYNC_PREWRITE);
218 1.40 bouyer
219 1.1 eeh /*
220 1.1 eeh * Initialize the transmit job descriptors.
221 1.1 eeh */
222 1.1 eeh SIMPLEQ_INIT(&sc->sc_txfreeq);
223 1.1 eeh SIMPLEQ_INIT(&sc->sc_txdirtyq);
224 1.1 eeh
225 1.1 eeh /*
226 1.1 eeh * Create the transmit buffer DMA maps.
227 1.1 eeh */
228 1.1 eeh for (i = 0; i < GEM_TXQUEUELEN; i++) {
229 1.1 eeh struct gem_txsoft *txs;
230 1.1 eeh
231 1.1 eeh txs = &sc->sc_txsoft[i];
232 1.1 eeh txs->txs_mbuf = NULL;
233 1.15 matt if ((error = bus_dmamap_create(sc->sc_dmatag,
234 1.15 matt ETHER_MAX_LEN_JUMBO, GEM_NTXSEGS,
235 1.15 matt ETHER_MAX_LEN_JUMBO, 0, 0,
236 1.1 eeh &txs->txs_dmamap)) != 0) {
237 1.24 thorpej aprint_error("%s: unable to create tx DMA map %d, "
238 1.1 eeh "error = %d\n", sc->sc_dev.dv_xname, i, error);
239 1.40 bouyer goto fail_6;
240 1.1 eeh }
241 1.1 eeh SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
242 1.1 eeh }
243 1.1 eeh
244 1.1 eeh /*
245 1.1 eeh * Create the receive buffer DMA maps.
246 1.1 eeh */
247 1.1 eeh for (i = 0; i < GEM_NRXDESC; i++) {
248 1.1 eeh if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
249 1.1 eeh MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
250 1.24 thorpej aprint_error("%s: unable to create rx DMA map %d, "
251 1.1 eeh "error = %d\n", sc->sc_dev.dv_xname, i, error);
252 1.40 bouyer goto fail_7;
253 1.1 eeh }
254 1.1 eeh sc->sc_rxsoft[i].rxs_mbuf = NULL;
255 1.1 eeh }
256 1.1 eeh
257 1.1 eeh /*
258 1.1 eeh * From this point forward, the attachment cannot fail. A failure
259 1.1 eeh * before this point releases all resources that may have been
260 1.1 eeh * allocated.
261 1.1 eeh */
262 1.1 eeh
263 1.1 eeh /* Announce ourselves. */
264 1.24 thorpej aprint_normal("%s: Ethernet address %s", sc->sc_dev.dv_xname,
265 1.6 thorpej ether_sprintf(enaddr));
266 1.1 eeh
267 1.15 matt /* Get RX FIFO size */
268 1.15 matt sc->sc_rxfifosize = 64 *
269 1.31 heas bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_FIFO_SIZE);
270 1.24 thorpej aprint_normal(", %uKB RX fifo", sc->sc_rxfifosize / 1024);
271 1.15 matt
272 1.15 matt /* Get TX FIFO size */
273 1.31 heas v = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_FIFO_SIZE);
274 1.24 thorpej aprint_normal(", %uKB TX fifo\n", v / 16);
275 1.15 matt
276 1.1 eeh /* Initialize ifnet structure. */
277 1.1 eeh strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
278 1.1 eeh ifp->if_softc = sc;
279 1.1 eeh ifp->if_flags =
280 1.1 eeh IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
281 1.41 christos sc->sc_if_flags = ifp->if_flags;
282 1.39 yamt ifp->if_capabilities |=
283 1.39 yamt IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
284 1.39 yamt IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
285 1.1 eeh ifp->if_start = gem_start;
286 1.1 eeh ifp->if_ioctl = gem_ioctl;
287 1.1 eeh ifp->if_watchdog = gem_watchdog;
288 1.1 eeh ifp->if_stop = gem_stop;
289 1.1 eeh ifp->if_init = gem_init;
290 1.1 eeh IFQ_SET_READY(&ifp->if_snd);
291 1.1 eeh
292 1.1 eeh /* Initialize ifmedia structures and MII info */
293 1.1 eeh mii->mii_ifp = ifp;
294 1.31 heas mii->mii_readreg = gem_mii_readreg;
295 1.1 eeh mii->mii_writereg = gem_mii_writereg;
296 1.1 eeh mii->mii_statchg = gem_mii_statchg;
297 1.1 eeh
298 1.23 fair ifmedia_init(&mii->mii_media, IFM_IMASK, gem_mediachange, gem_mediastatus);
299 1.1 eeh
300 1.1 eeh gem_mifinit(sc);
301 1.1 eeh
302 1.47 sanjayl #if defined (PMAC_G5)
303 1.47 sanjayl mii_attach(&sc->sc_dev, mii, 0xffffffff,
304 1.47 sanjayl 1, MII_OFFSET_ANY, MIIF_FORCEANEG);
305 1.47 sanjayl #else
306 1.1 eeh mii_attach(&sc->sc_dev, mii, 0xffffffff,
307 1.25 matt MII_PHY_ANY, MII_OFFSET_ANY, MIIF_FORCEANEG);
308 1.47 sanjayl #endif
309 1.1 eeh
310 1.1 eeh child = LIST_FIRST(&mii->mii_phys);
311 1.1 eeh if (child == NULL) {
312 1.1 eeh /* No PHY attached */
313 1.1 eeh ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
314 1.1 eeh ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
315 1.1 eeh } else {
316 1.1 eeh /*
317 1.1 eeh * Walk along the list of attached MII devices and
318 1.1 eeh * establish an `MII instance' to `phy number'
319 1.1 eeh * mapping. We'll use this mapping in media change
320 1.1 eeh * requests to determine which phy to use to program
321 1.1 eeh * the MIF configuration register.
322 1.1 eeh */
323 1.1 eeh for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
324 1.1 eeh /*
325 1.1 eeh * Note: we support just two PHYs: the built-in
326 1.1 eeh * internal device and an external on the MII
327 1.1 eeh * connector.
328 1.1 eeh */
329 1.1 eeh if (child->mii_phy > 1 || child->mii_inst > 1) {
330 1.24 thorpej aprint_error(
331 1.24 thorpej "%s: cannot accomodate MII device %s"
332 1.1 eeh " at phy %d, instance %d\n",
333 1.1 eeh sc->sc_dev.dv_xname,
334 1.1 eeh child->mii_dev.dv_xname,
335 1.1 eeh child->mii_phy, child->mii_inst);
336 1.1 eeh continue;
337 1.1 eeh }
338 1.1 eeh
339 1.1 eeh sc->sc_phys[child->mii_inst] = child->mii_phy;
340 1.1 eeh }
341 1.1 eeh
342 1.1 eeh /*
343 1.1 eeh * Now select and activate the PHY we will use.
344 1.1 eeh *
345 1.1 eeh * The order of preference is External (MDI1),
346 1.1 eeh * Internal (MDI0), Serial Link (no MII).
347 1.1 eeh */
348 1.1 eeh if (sc->sc_phys[1]) {
349 1.41 christos #ifdef GEM_DEBUG
350 1.24 thorpej aprint_debug("using external phy\n");
351 1.1 eeh #endif
352 1.1 eeh sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
353 1.1 eeh } else {
354 1.41 christos #ifdef GEM_DEBUG
355 1.24 thorpej aprint_debug("using internal phy\n");
356 1.1 eeh #endif
357 1.1 eeh sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
358 1.1 eeh }
359 1.31 heas bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG,
360 1.1 eeh sc->sc_mif_config);
361 1.1 eeh
362 1.1 eeh /*
363 1.1 eeh * XXX - we can really do the following ONLY if the
364 1.1 eeh * phy indeed has the auto negotiation capability!!
365 1.1 eeh */
366 1.1 eeh ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
367 1.1 eeh }
368 1.1 eeh
369 1.15 matt /*
370 1.15 matt * If we support GigE media, we support jumbo frames too.
371 1.15 matt * Unless we are Apple.
372 1.15 matt */
373 1.15 matt TAILQ_FOREACH(ifm, &sc->sc_media.ifm_list, ifm_list) {
374 1.15 matt if (IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_T ||
375 1.15 matt IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_SX ||
376 1.15 matt IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_LX ||
377 1.15 matt IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_CX) {
378 1.15 matt if (sc->sc_variant != GEM_APPLE_GMAC)
379 1.15 matt sc->sc_ethercom.ec_capabilities
380 1.15 matt |= ETHERCAP_JUMBO_MTU;
381 1.15 matt
382 1.15 matt sc->sc_flags |= GEM_GIGABIT;
383 1.15 matt break;
384 1.15 matt }
385 1.15 matt }
386 1.15 matt
387 1.1 eeh /* claim 802.1q capability */
388 1.1 eeh sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
389 1.1 eeh
390 1.1 eeh /* Attach the interface. */
391 1.1 eeh if_attach(ifp);
392 1.6 thorpej ether_ifattach(ifp, enaddr);
393 1.1 eeh
394 1.1 eeh sc->sc_sh = shutdownhook_establish(gem_shutdown, sc);
395 1.1 eeh if (sc->sc_sh == NULL)
396 1.1 eeh panic("gem_config: can't establish shutdownhook");
397 1.1 eeh
398 1.1 eeh #if NRND > 0
399 1.1 eeh rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
400 1.1 eeh RND_TYPE_NET, 0);
401 1.1 eeh #endif
402 1.1 eeh
403 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
404 1.18 matt NULL, sc->sc_dev.dv_xname, "interrupts");
405 1.19 matt #ifdef GEM_COUNTERS
406 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_txint, EVCNT_TYPE_INTR,
407 1.18 matt &sc->sc_ev_intr, sc->sc_dev.dv_xname, "tx interrupts");
408 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxint, EVCNT_TYPE_INTR,
409 1.18 matt &sc->sc_ev_intr, sc->sc_dev.dv_xname, "rx interrupts");
410 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxfull, EVCNT_TYPE_INTR,
411 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx ring full");
412 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxnobuf, EVCNT_TYPE_INTR,
413 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx malloc failure");
414 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[0], EVCNT_TYPE_INTR,
415 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 0desc");
416 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[1], EVCNT_TYPE_INTR,
417 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 1desc");
418 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[2], EVCNT_TYPE_INTR,
419 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 2desc");
420 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[3], EVCNT_TYPE_INTR,
421 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 3desc");
422 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[4], EVCNT_TYPE_INTR,
423 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >3desc");
424 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[5], EVCNT_TYPE_INTR,
425 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >7desc");
426 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[6], EVCNT_TYPE_INTR,
427 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >15desc");
428 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[7], EVCNT_TYPE_INTR,
429 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >31desc");
430 1.18 matt evcnt_attach_dynamic(&sc->sc_ev_rxhist[8], EVCNT_TYPE_INTR,
431 1.18 matt &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >63desc");
432 1.19 matt #endif
433 1.1 eeh
434 1.1 eeh #if notyet
435 1.1 eeh /*
436 1.1 eeh * Add a suspend hook to make sure we come back up after a
437 1.1 eeh * resume.
438 1.1 eeh */
439 1.48 jmcneill sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
440 1.48 jmcneill gem_power, sc);
441 1.1 eeh if (sc->sc_powerhook == NULL)
442 1.24 thorpej aprint_error("%s: WARNING: unable to establish power hook\n",
443 1.1 eeh sc->sc_dev.dv_xname);
444 1.1 eeh #endif
445 1.1 eeh
446 1.1 eeh callout_init(&sc->sc_tick_ch);
447 1.1 eeh return;
448 1.1 eeh
449 1.1 eeh /*
450 1.1 eeh * Free any resources we've allocated during the failed attach
451 1.1 eeh * attempt. Do this in reverse order and fall through.
452 1.1 eeh */
453 1.40 bouyer fail_7:
454 1.1 eeh for (i = 0; i < GEM_NRXDESC; i++) {
455 1.1 eeh if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
456 1.1 eeh bus_dmamap_destroy(sc->sc_dmatag,
457 1.1 eeh sc->sc_rxsoft[i].rxs_dmamap);
458 1.1 eeh }
459 1.40 bouyer fail_6:
460 1.1 eeh for (i = 0; i < GEM_TXQUEUELEN; i++) {
461 1.1 eeh if (sc->sc_txsoft[i].txs_dmamap != NULL)
462 1.1 eeh bus_dmamap_destroy(sc->sc_dmatag,
463 1.1 eeh sc->sc_txsoft[i].txs_dmamap);
464 1.1 eeh }
465 1.1 eeh bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
466 1.40 bouyer fail_5:
467 1.40 bouyer bus_dmamap_destroy(sc->sc_dmatag, sc->sc_nulldmamap);
468 1.40 bouyer fail_4:
469 1.40 bouyer bus_dmamem_unmap(sc->sc_dmatag, (caddr_t)nullbuf, ETHER_MIN_TX);
470 1.1 eeh fail_3:
471 1.1 eeh bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
472 1.1 eeh fail_2:
473 1.1 eeh bus_dmamem_unmap(sc->sc_dmatag, (caddr_t)sc->sc_control_data,
474 1.1 eeh sizeof(struct gem_control_data));
475 1.1 eeh fail_1:
476 1.1 eeh bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
477 1.1 eeh fail_0:
478 1.1 eeh return;
479 1.1 eeh }
480 1.1 eeh
481 1.1 eeh
482 1.1 eeh void
483 1.1 eeh gem_tick(arg)
484 1.1 eeh void *arg;
485 1.1 eeh {
486 1.1 eeh struct gem_softc *sc = arg;
487 1.1 eeh int s;
488 1.1 eeh
489 1.1 eeh s = splnet();
490 1.1 eeh mii_tick(&sc->sc_mii);
491 1.1 eeh splx(s);
492 1.1 eeh
493 1.1 eeh callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
494 1.31 heas
495 1.1 eeh }
496 1.1 eeh
497 1.41 christos static int
498 1.41 christos gem_bitwait(sc, r, clr, set)
499 1.41 christos struct gem_softc *sc;
500 1.41 christos int r;
501 1.41 christos u_int32_t clr;
502 1.41 christos u_int32_t set;
503 1.41 christos {
504 1.41 christos int i;
505 1.41 christos u_int32_t reg;
506 1.46 blymn
507 1.41 christos for (i = TRIES; i--; DELAY(100)) {
508 1.41 christos reg = bus_space_read_4(sc->sc_bustag, sc->sc_h, r);
509 1.41 christos if ((r & clr) == 0 && (r & set) == set)
510 1.41 christos return (1);
511 1.41 christos }
512 1.41 christos return (0);
513 1.41 christos }
514 1.41 christos
515 1.1 eeh void
516 1.1 eeh gem_reset(sc)
517 1.1 eeh struct gem_softc *sc;
518 1.1 eeh {
519 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
520 1.1 eeh bus_space_handle_t h = sc->sc_h;
521 1.1 eeh int s;
522 1.1 eeh
523 1.1 eeh s = splnet();
524 1.1 eeh DPRINTF(sc, ("%s: gem_reset\n", sc->sc_dev.dv_xname));
525 1.1 eeh gem_reset_rx(sc);
526 1.1 eeh gem_reset_tx(sc);
527 1.1 eeh
528 1.1 eeh /* Do a full reset */
529 1.1 eeh bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX|GEM_RESET_TX);
530 1.41 christos if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
531 1.41 christos printf("%s: cannot reset device\n", sc->sc_dev.dv_xname);
532 1.1 eeh splx(s);
533 1.1 eeh }
534 1.1 eeh
535 1.1 eeh
536 1.1 eeh /*
537 1.1 eeh * gem_rxdrain:
538 1.1 eeh *
539 1.1 eeh * Drain the receive queue.
540 1.1 eeh */
541 1.41 christos static void
542 1.1 eeh gem_rxdrain(struct gem_softc *sc)
543 1.1 eeh {
544 1.1 eeh struct gem_rxsoft *rxs;
545 1.1 eeh int i;
546 1.1 eeh
547 1.1 eeh for (i = 0; i < GEM_NRXDESC; i++) {
548 1.1 eeh rxs = &sc->sc_rxsoft[i];
549 1.1 eeh if (rxs->rxs_mbuf != NULL) {
550 1.41 christos bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
551 1.41 christos rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
552 1.1 eeh bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
553 1.1 eeh m_freem(rxs->rxs_mbuf);
554 1.1 eeh rxs->rxs_mbuf = NULL;
555 1.1 eeh }
556 1.1 eeh }
557 1.1 eeh }
558 1.1 eeh
559 1.31 heas /*
560 1.1 eeh * Reset the whole thing.
561 1.1 eeh */
562 1.41 christos static void
563 1.1 eeh gem_stop(struct ifnet *ifp, int disable)
564 1.1 eeh {
565 1.1 eeh struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
566 1.1 eeh struct gem_txsoft *txs;
567 1.1 eeh
568 1.1 eeh DPRINTF(sc, ("%s: gem_stop\n", sc->sc_dev.dv_xname));
569 1.1 eeh
570 1.1 eeh callout_stop(&sc->sc_tick_ch);
571 1.1 eeh mii_down(&sc->sc_mii);
572 1.1 eeh
573 1.1 eeh /* XXX - Should we reset these instead? */
574 1.1 eeh gem_disable_rx(sc);
575 1.22 matt gem_disable_tx(sc);
576 1.1 eeh
577 1.1 eeh /*
578 1.1 eeh * Release any queued transmit buffers.
579 1.1 eeh */
580 1.1 eeh while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
581 1.21 lukem SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
582 1.1 eeh if (txs->txs_mbuf != NULL) {
583 1.41 christos bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap, 0,
584 1.41 christos txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
585 1.1 eeh bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
586 1.1 eeh m_freem(txs->txs_mbuf);
587 1.1 eeh txs->txs_mbuf = NULL;
588 1.1 eeh }
589 1.1 eeh SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
590 1.1 eeh }
591 1.1 eeh
592 1.1 eeh if (disable) {
593 1.1 eeh gem_rxdrain(sc);
594 1.1 eeh }
595 1.1 eeh
596 1.1 eeh /*
597 1.1 eeh * Mark the interface down and cancel the watchdog timer.
598 1.1 eeh */
599 1.1 eeh ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
600 1.41 christos sc->sc_if_flags = ifp->if_flags;
601 1.1 eeh ifp->if_timer = 0;
602 1.1 eeh }
603 1.1 eeh
604 1.1 eeh
605 1.1 eeh /*
606 1.1 eeh * Reset the receiver
607 1.1 eeh */
608 1.1 eeh int
609 1.1 eeh gem_reset_rx(struct gem_softc *sc)
610 1.1 eeh {
611 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
612 1.1 eeh bus_space_handle_t h = sc->sc_h;
613 1.1 eeh
614 1.1 eeh /*
615 1.1 eeh * Resetting while DMA is in progress can cause a bus hang, so we
616 1.1 eeh * disable DMA first.
617 1.1 eeh */
618 1.1 eeh gem_disable_rx(sc);
619 1.1 eeh bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
620 1.1 eeh /* Wait till it finishes */
621 1.41 christos if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0))
622 1.41 christos printf("%s: cannot disable read dma\n", sc->sc_dev.dv_xname);
623 1.1 eeh /* Wait 5ms extra. */
624 1.1 eeh delay(5000);
625 1.1 eeh
626 1.1 eeh /* Finally, reset the ERX */
627 1.1 eeh bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX);
628 1.1 eeh /* Wait till it finishes */
629 1.49 martin if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX, 0)) {
630 1.41 christos printf("%s: cannot reset receiver\n", sc->sc_dev.dv_xname);
631 1.1 eeh return (1);
632 1.1 eeh }
633 1.1 eeh return (0);
634 1.1 eeh }
635 1.1 eeh
636 1.1 eeh
637 1.1 eeh /*
638 1.1 eeh * Reset the transmitter
639 1.1 eeh */
640 1.1 eeh int
641 1.1 eeh gem_reset_tx(struct gem_softc *sc)
642 1.1 eeh {
643 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
644 1.1 eeh bus_space_handle_t h = sc->sc_h;
645 1.1 eeh
646 1.1 eeh /*
647 1.1 eeh * Resetting while DMA is in progress can cause a bus hang, so we
648 1.1 eeh * disable DMA first.
649 1.1 eeh */
650 1.1 eeh gem_disable_tx(sc);
651 1.1 eeh bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
652 1.1 eeh /* Wait till it finishes */
653 1.41 christos if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0))
654 1.41 christos printf("%s: cannot disable read dma\n", sc->sc_dev.dv_xname);
655 1.1 eeh /* Wait 5ms extra. */
656 1.1 eeh delay(5000);
657 1.1 eeh
658 1.1 eeh /* Finally, reset the ETX */
659 1.1 eeh bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX);
660 1.1 eeh /* Wait till it finishes */
661 1.41 christos if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
662 1.1 eeh printf("%s: cannot reset receiver\n",
663 1.1 eeh sc->sc_dev.dv_xname);
664 1.1 eeh return (1);
665 1.1 eeh }
666 1.1 eeh return (0);
667 1.1 eeh }
668 1.1 eeh
669 1.1 eeh /*
670 1.1 eeh * disable receiver.
671 1.1 eeh */
672 1.1 eeh int
673 1.1 eeh gem_disable_rx(struct gem_softc *sc)
674 1.1 eeh {
675 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
676 1.1 eeh bus_space_handle_t h = sc->sc_h;
677 1.1 eeh u_int32_t cfg;
678 1.1 eeh
679 1.1 eeh /* Flip the enable bit */
680 1.1 eeh cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
681 1.1 eeh cfg &= ~GEM_MAC_RX_ENABLE;
682 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
683 1.1 eeh
684 1.1 eeh /* Wait for it to finish */
685 1.41 christos return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
686 1.1 eeh }
687 1.1 eeh
688 1.1 eeh /*
689 1.1 eeh * disable transmitter.
690 1.1 eeh */
691 1.1 eeh int
692 1.1 eeh gem_disable_tx(struct gem_softc *sc)
693 1.1 eeh {
694 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
695 1.1 eeh bus_space_handle_t h = sc->sc_h;
696 1.1 eeh u_int32_t cfg;
697 1.1 eeh
698 1.1 eeh /* Flip the enable bit */
699 1.1 eeh cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
700 1.1 eeh cfg &= ~GEM_MAC_TX_ENABLE;
701 1.1 eeh bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
702 1.1 eeh
703 1.1 eeh /* Wait for it to finish */
704 1.41 christos return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
705 1.1 eeh }
706 1.1 eeh
707 1.1 eeh /*
708 1.1 eeh * Initialize interface.
709 1.1 eeh */
710 1.1 eeh int
711 1.1 eeh gem_meminit(struct gem_softc *sc)
712 1.1 eeh {
713 1.1 eeh struct gem_rxsoft *rxs;
714 1.1 eeh int i, error;
715 1.1 eeh
716 1.1 eeh /*
717 1.1 eeh * Initialize the transmit descriptor ring.
718 1.1 eeh */
719 1.1 eeh memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
720 1.1 eeh for (i = 0; i < GEM_NTXDESC; i++) {
721 1.1 eeh sc->sc_txdescs[i].gd_flags = 0;
722 1.1 eeh sc->sc_txdescs[i].gd_addr = 0;
723 1.1 eeh }
724 1.1 eeh GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
725 1.1 eeh BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
726 1.14 matt sc->sc_txfree = GEM_NTXDESC-1;
727 1.1 eeh sc->sc_txnext = 0;
728 1.14 matt sc->sc_txwin = 0;
729 1.1 eeh
730 1.1 eeh /*
731 1.1 eeh * Initialize the receive descriptor and receive job
732 1.1 eeh * descriptor rings.
733 1.1 eeh */
734 1.1 eeh for (i = 0; i < GEM_NRXDESC; i++) {
735 1.1 eeh rxs = &sc->sc_rxsoft[i];
736 1.1 eeh if (rxs->rxs_mbuf == NULL) {
737 1.1 eeh if ((error = gem_add_rxbuf(sc, i)) != 0) {
738 1.1 eeh printf("%s: unable to allocate or map rx "
739 1.1 eeh "buffer %d, error = %d\n",
740 1.1 eeh sc->sc_dev.dv_xname, i, error);
741 1.1 eeh /*
742 1.1 eeh * XXX Should attempt to run with fewer receive
743 1.1 eeh * XXX buffers instead of just failing.
744 1.1 eeh */
745 1.1 eeh gem_rxdrain(sc);
746 1.1 eeh return (1);
747 1.1 eeh }
748 1.1 eeh } else
749 1.1 eeh GEM_INIT_RXDESC(sc, i);
750 1.1 eeh }
751 1.1 eeh sc->sc_rxptr = 0;
752 1.1 eeh
753 1.1 eeh return (0);
754 1.1 eeh }
755 1.1 eeh
756 1.1 eeh static int
757 1.1 eeh gem_ringsize(int sz)
758 1.1 eeh {
759 1.1 eeh switch (sz) {
760 1.1 eeh case 32:
761 1.29 christos return GEM_RING_SZ_32;
762 1.1 eeh case 64:
763 1.29 christos return GEM_RING_SZ_64;
764 1.1 eeh case 128:
765 1.29 christos return GEM_RING_SZ_128;
766 1.1 eeh case 256:
767 1.29 christos return GEM_RING_SZ_256;
768 1.1 eeh case 512:
769 1.29 christos return GEM_RING_SZ_512;
770 1.1 eeh case 1024:
771 1.29 christos return GEM_RING_SZ_1024;
772 1.1 eeh case 2048:
773 1.29 christos return GEM_RING_SZ_2048;
774 1.1 eeh case 4096:
775 1.29 christos return GEM_RING_SZ_4096;
776 1.1 eeh case 8192:
777 1.29 christos return GEM_RING_SZ_8192;
778 1.1 eeh default:
779 1.29 christos printf("gem: invalid Receive Descriptor ring size %d\n", sz);
780 1.29 christos return GEM_RING_SZ_32;
781 1.1 eeh }
782 1.1 eeh }
783 1.1 eeh
784 1.1 eeh /*
785 1.1 eeh * Initialization of interface; set up initialization block
786 1.1 eeh * and transmit/receive descriptor rings.
787 1.1 eeh */
788 1.1 eeh int
789 1.1 eeh gem_init(struct ifnet *ifp)
790 1.1 eeh {
791 1.1 eeh struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
792 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
793 1.1 eeh bus_space_handle_t h = sc->sc_h;
794 1.1 eeh int s;
795 1.15 matt u_int max_frame_size;
796 1.1 eeh u_int32_t v;
797 1.1 eeh
798 1.1 eeh s = splnet();
799 1.1 eeh
800 1.1 eeh DPRINTF(sc, ("%s: gem_init: calling stop\n", sc->sc_dev.dv_xname));
801 1.1 eeh /*
802 1.1 eeh * Initialization sequence. The numbered steps below correspond
803 1.1 eeh * to the sequence outlined in section 6.3.5.1 in the Ethernet
804 1.1 eeh * Channel Engine manual (part of the PCIO manual).
805 1.1 eeh * See also the STP2002-STQ document from Sun Microsystems.
806 1.1 eeh */
807 1.1 eeh
808 1.1 eeh /* step 1 & 2. Reset the Ethernet Channel */
809 1.1 eeh gem_stop(ifp, 0);
810 1.1 eeh gem_reset(sc);
811 1.1 eeh DPRINTF(sc, ("%s: gem_init: restarting\n", sc->sc_dev.dv_xname));
812 1.1 eeh
813 1.1 eeh /* Re-initialize the MIF */
814 1.1 eeh gem_mifinit(sc);
815 1.1 eeh
816 1.1 eeh /* Call MI reset function if any */
817 1.1 eeh if (sc->sc_hwreset)
818 1.1 eeh (*sc->sc_hwreset)(sc);
819 1.1 eeh
820 1.1 eeh /* step 3. Setup data structures in host memory */
821 1.1 eeh gem_meminit(sc);
822 1.1 eeh
823 1.1 eeh /* step 4. TX MAC registers & counters */
824 1.1 eeh gem_init_regs(sc);
825 1.15 matt max_frame_size = max(sc->sc_ethercom.ec_if.if_mtu, ETHERMTU);
826 1.15 matt max_frame_size += ETHER_HDR_LEN + ETHER_CRC_LEN;
827 1.15 matt if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
828 1.15 matt max_frame_size += ETHER_VLAN_ENCAP_LEN;
829 1.1 eeh bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
830 1.15 matt max_frame_size|/* burst size */(0x2000<<16));
831 1.1 eeh
832 1.1 eeh /* step 5. RX MAC registers & counters */
833 1.1 eeh gem_setladrf(sc);
834 1.1 eeh
835 1.1 eeh /* step 6 & 7. Program Descriptor Ring Base Addresses */
836 1.4 thorpej /* NOTE: we use only 32-bit DMA addresses here. */
837 1.4 thorpej bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0);
838 1.4 thorpej bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
839 1.4 thorpej
840 1.4 thorpej bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
841 1.4 thorpej bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
842 1.1 eeh
843 1.1 eeh /* step 8. Global Configuration & Interrupt Mask */
844 1.1 eeh bus_space_write_4(t, h, GEM_INTMASK,
845 1.1 eeh ~(GEM_INTR_TX_INTME|
846 1.1 eeh GEM_INTR_TX_EMPTY|
847 1.1 eeh GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF|
848 1.1 eeh GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS|
849 1.1 eeh GEM_INTR_MAC_CONTROL|GEM_INTR_MIF|
850 1.1 eeh GEM_INTR_BERR));
851 1.16 matt bus_space_write_4(t, h, GEM_MAC_RX_MASK,
852 1.17 matt GEM_MAC_RX_DONE|GEM_MAC_RX_FRAME_CNT);
853 1.1 eeh bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */
854 1.1 eeh bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */
855 1.5 thorpej
856 1.1 eeh /* step 9. ETX Configuration: use mostly default values */
857 1.1 eeh
858 1.1 eeh /* Enable DMA */
859 1.1 eeh v = gem_ringsize(GEM_NTXDESC /*XXX*/);
860 1.31 heas bus_space_write_4(t, h, GEM_TX_CONFIG,
861 1.1 eeh v|GEM_TX_CONFIG_TXDMA_EN|
862 1.1 eeh ((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH));
863 1.1 eeh bus_space_write_4(t, h, GEM_TX_KICK, sc->sc_txnext);
864 1.1 eeh
865 1.1 eeh /* step 10. ERX Configuration */
866 1.1 eeh
867 1.1 eeh /* Encode Receive Descriptor ring size: four possible values */
868 1.1 eeh v = gem_ringsize(GEM_NRXDESC /*XXX*/);
869 1.1 eeh
870 1.35 heas /* Set receive h/w checksum offset */
871 1.35 heas #ifdef INET
872 1.35 heas v |= (ETHER_HDR_LEN + sizeof(struct ip) +
873 1.35 heas ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
874 1.35 heas ETHER_VLAN_ENCAP_LEN : 0)) << GEM_RX_CONFIG_CXM_START_SHFT;
875 1.35 heas #endif
876 1.35 heas
877 1.1 eeh /* Enable DMA */
878 1.31 heas bus_space_write_4(t, h, GEM_RX_CONFIG,
879 1.1 eeh v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)|
880 1.35 heas (2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN);
881 1.35 heas
882 1.1 eeh /*
883 1.15 matt * The following value is for an OFF Threshold of about 3/4 full
884 1.15 matt * and an ON Threshold of 1/4 full.
885 1.1 eeh */
886 1.15 matt bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH,
887 1.15 matt (3 * sc->sc_rxfifosize / 256) |
888 1.15 matt ( (sc->sc_rxfifosize / 256) << 12));
889 1.15 matt bus_space_write_4(t, h, GEM_RX_BLANKING, (6<<12)|6);
890 1.1 eeh
891 1.1 eeh /* step 11. Configure Media */
892 1.15 matt mii_mediachg(&sc->sc_mii);
893 1.11 thorpej
894 1.11 thorpej /* XXXX Serial link needs a whole different setup. */
895 1.11 thorpej
896 1.1 eeh
897 1.1 eeh /* step 12. RX_MAC Configuration Register */
898 1.1 eeh v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
899 1.35 heas v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC;
900 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
901 1.1 eeh
902 1.1 eeh /* step 14. Issue Transmit Pending command */
903 1.1 eeh
904 1.1 eeh /* Call MI initialization function if any */
905 1.1 eeh if (sc->sc_hwinit)
906 1.1 eeh (*sc->sc_hwinit)(sc);
907 1.1 eeh
908 1.1 eeh
909 1.1 eeh /* step 15. Give the reciever a swift kick */
910 1.1 eeh bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
911 1.1 eeh
912 1.1 eeh /* Start the one second timer. */
913 1.1 eeh callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
914 1.1 eeh
915 1.1 eeh ifp->if_flags |= IFF_RUNNING;
916 1.1 eeh ifp->if_flags &= ~IFF_OACTIVE;
917 1.1 eeh ifp->if_timer = 0;
918 1.41 christos sc->sc_if_flags = ifp->if_flags;
919 1.1 eeh splx(s);
920 1.1 eeh
921 1.1 eeh return (0);
922 1.1 eeh }
923 1.1 eeh
924 1.1 eeh void
925 1.1 eeh gem_init_regs(struct gem_softc *sc)
926 1.1 eeh {
927 1.1 eeh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
928 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
929 1.1 eeh bus_space_handle_t h = sc->sc_h;
930 1.13 matt const u_char *laddr = LLADDR(ifp->if_sadl);
931 1.15 matt u_int32_t v;
932 1.1 eeh
933 1.1 eeh /* These regs are not cleared on reset */
934 1.1 eeh if (!sc->sc_inited) {
935 1.1 eeh
936 1.1 eeh /* Wooo. Magic values. */
937 1.1 eeh bus_space_write_4(t, h, GEM_MAC_IPG0, 0);
938 1.1 eeh bus_space_write_4(t, h, GEM_MAC_IPG1, 8);
939 1.1 eeh bus_space_write_4(t, h, GEM_MAC_IPG2, 4);
940 1.1 eeh
941 1.1 eeh bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
942 1.1 eeh /* Max frame and max burst size */
943 1.1 eeh bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
944 1.15 matt ETHER_MAX_LEN | (0x2000<<16));
945 1.15 matt
946 1.1 eeh bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7);
947 1.1 eeh bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4);
948 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
949 1.1 eeh /* Dunno.... */
950 1.1 eeh bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
951 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
952 1.15 matt ((laddr[5]<<8)|laddr[4])&0x3ff);
953 1.13 matt
954 1.1 eeh /* Secondary MAC addr set to 0:0:0:0:0:0 */
955 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
956 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
957 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
958 1.13 matt
959 1.13 matt /* MAC control addr set to 01:80:c2:00:00:01 */
960 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
961 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
962 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
963 1.1 eeh
964 1.1 eeh /* MAC filter addr set to 0:0:0:0:0:0 */
965 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
966 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
967 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
968 1.1 eeh
969 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
970 1.1 eeh bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
971 1.1 eeh
972 1.1 eeh sc->sc_inited = 1;
973 1.1 eeh }
974 1.1 eeh
975 1.1 eeh /* Counters need to be zeroed */
976 1.1 eeh bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
977 1.1 eeh bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
978 1.1 eeh bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
979 1.1 eeh bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
980 1.1 eeh bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
981 1.1 eeh bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
982 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
983 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
984 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
985 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
986 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
987 1.1 eeh
988 1.1 eeh /* Un-pause stuff */
989 1.1 eeh #if 0
990 1.1 eeh bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
991 1.1 eeh #else
992 1.1 eeh bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0);
993 1.1 eeh #endif
994 1.1 eeh
995 1.1 eeh /*
996 1.1 eeh * Set the station address.
997 1.1 eeh */
998 1.13 matt bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]);
999 1.13 matt bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]);
1000 1.13 matt bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]);
1001 1.1 eeh
1002 1.15 matt #if 0
1003 1.15 matt if (sc->sc_variant != APPLE_GMAC)
1004 1.15 matt return;
1005 1.15 matt #endif
1006 1.15 matt
1007 1.15 matt /*
1008 1.15 matt * Enable MII outputs. Enable GMII if there is a gigabit PHY.
1009 1.15 matt */
1010 1.15 matt sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG);
1011 1.15 matt v = GEM_MAC_XIF_TX_MII_ENA;
1012 1.15 matt if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) {
1013 1.15 matt v |= GEM_MAC_XIF_FDPLX_LED;
1014 1.15 matt if (sc->sc_flags & GEM_GIGABIT)
1015 1.15 matt v |= GEM_MAC_XIF_GMII_MODE;
1016 1.15 matt }
1017 1.15 matt bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v);
1018 1.1 eeh }
1019 1.1 eeh
1020 1.41 christos static void
1021 1.1 eeh gem_start(ifp)
1022 1.1 eeh struct ifnet *ifp;
1023 1.1 eeh {
1024 1.1 eeh struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
1025 1.1 eeh struct mbuf *m0, *m;
1026 1.1 eeh struct gem_txsoft *txs, *last_txs;
1027 1.1 eeh bus_dmamap_t dmamap;
1028 1.49 martin int error, firsttx, nexttx = -1, lasttx = -1, ofree, seg;
1029 1.40 bouyer uint64_t flags = 0;
1030 1.1 eeh
1031 1.1 eeh if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1032 1.1 eeh return;
1033 1.1 eeh
1034 1.1 eeh /*
1035 1.1 eeh * Remember the previous number of free descriptors and
1036 1.1 eeh * the first descriptor we'll use.
1037 1.1 eeh */
1038 1.1 eeh ofree = sc->sc_txfree;
1039 1.1 eeh firsttx = sc->sc_txnext;
1040 1.1 eeh
1041 1.1 eeh DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
1042 1.1 eeh sc->sc_dev.dv_xname, ofree, firsttx));
1043 1.1 eeh
1044 1.1 eeh /*
1045 1.1 eeh * Loop through the send queue, setting up transmit descriptors
1046 1.1 eeh * until we drain the queue, or use up all available transmit
1047 1.1 eeh * descriptors.
1048 1.1 eeh */
1049 1.11 thorpej while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
1050 1.11 thorpej sc->sc_txfree != 0) {
1051 1.1 eeh /*
1052 1.1 eeh * Grab a packet off the queue.
1053 1.1 eeh */
1054 1.1 eeh IFQ_POLL(&ifp->if_snd, m0);
1055 1.1 eeh if (m0 == NULL)
1056 1.1 eeh break;
1057 1.1 eeh m = NULL;
1058 1.1 eeh
1059 1.1 eeh dmamap = txs->txs_dmamap;
1060 1.1 eeh
1061 1.1 eeh /*
1062 1.1 eeh * Load the DMA map. If this fails, the packet either
1063 1.1 eeh * didn't fit in the alloted number of segments, or we were
1064 1.1 eeh * short on resources. In this case, we'll copy and try
1065 1.1 eeh * again.
1066 1.1 eeh */
1067 1.1 eeh if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m0,
1068 1.40 bouyer BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0 ||
1069 1.40 bouyer (m0->m_pkthdr.len < ETHER_MIN_TX &&
1070 1.40 bouyer dmamap->dm_nsegs == GEM_NTXSEGS)) {
1071 1.15 matt if (m0->m_pkthdr.len > MCLBYTES) {
1072 1.15 matt printf("%s: unable to allocate jumbo Tx "
1073 1.15 matt "cluster\n", sc->sc_dev.dv_xname);
1074 1.15 matt IFQ_DEQUEUE(&ifp->if_snd, m0);
1075 1.15 matt m_freem(m0);
1076 1.15 matt continue;
1077 1.15 matt }
1078 1.1 eeh MGETHDR(m, M_DONTWAIT, MT_DATA);
1079 1.1 eeh if (m == NULL) {
1080 1.1 eeh printf("%s: unable to allocate Tx mbuf\n",
1081 1.1 eeh sc->sc_dev.dv_xname);
1082 1.1 eeh break;
1083 1.1 eeh }
1084 1.26 matt MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
1085 1.1 eeh if (m0->m_pkthdr.len > MHLEN) {
1086 1.1 eeh MCLGET(m, M_DONTWAIT);
1087 1.1 eeh if ((m->m_flags & M_EXT) == 0) {
1088 1.1 eeh printf("%s: unable to allocate Tx "
1089 1.1 eeh "cluster\n", sc->sc_dev.dv_xname);
1090 1.1 eeh m_freem(m);
1091 1.1 eeh break;
1092 1.1 eeh }
1093 1.1 eeh }
1094 1.1 eeh m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1095 1.1 eeh m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1096 1.1 eeh error = bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap,
1097 1.1 eeh m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1098 1.1 eeh if (error) {
1099 1.1 eeh printf("%s: unable to load Tx buffer, "
1100 1.1 eeh "error = %d\n", sc->sc_dev.dv_xname, error);
1101 1.1 eeh break;
1102 1.1 eeh }
1103 1.1 eeh }
1104 1.1 eeh
1105 1.1 eeh /*
1106 1.1 eeh * Ensure we have enough descriptors free to describe
1107 1.11 thorpej * the packet.
1108 1.1 eeh */
1109 1.40 bouyer if (dmamap->dm_nsegs > ((m0->m_pkthdr.len < ETHER_MIN_TX) ?
1110 1.40 bouyer (sc->sc_txfree - 1) : sc->sc_txfree)) {
1111 1.1 eeh /*
1112 1.1 eeh * Not enough free descriptors to transmit this
1113 1.1 eeh * packet. We haven't committed to anything yet,
1114 1.1 eeh * so just unload the DMA map, put the packet
1115 1.1 eeh * back on the queue, and punt. Notify the upper
1116 1.1 eeh * layer that there are no more slots left.
1117 1.1 eeh *
1118 1.1 eeh * XXX We could allocate an mbuf and copy, but
1119 1.1 eeh * XXX it is worth it?
1120 1.1 eeh */
1121 1.1 eeh ifp->if_flags |= IFF_OACTIVE;
1122 1.41 christos sc->sc_if_flags = ifp->if_flags;
1123 1.1 eeh bus_dmamap_unload(sc->sc_dmatag, dmamap);
1124 1.1 eeh if (m != NULL)
1125 1.1 eeh m_freem(m);
1126 1.1 eeh break;
1127 1.1 eeh }
1128 1.1 eeh
1129 1.1 eeh IFQ_DEQUEUE(&ifp->if_snd, m0);
1130 1.1 eeh if (m != NULL) {
1131 1.1 eeh m_freem(m0);
1132 1.1 eeh m0 = m;
1133 1.1 eeh }
1134 1.1 eeh
1135 1.1 eeh /*
1136 1.1 eeh * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1137 1.1 eeh */
1138 1.1 eeh
1139 1.1 eeh /* Sync the DMA map. */
1140 1.1 eeh bus_dmamap_sync(sc->sc_dmatag, dmamap, 0, dmamap->dm_mapsize,
1141 1.1 eeh BUS_DMASYNC_PREWRITE);
1142 1.1 eeh
1143 1.1 eeh /*
1144 1.1 eeh * Initialize the transmit descriptors.
1145 1.1 eeh */
1146 1.1 eeh for (nexttx = sc->sc_txnext, seg = 0;
1147 1.1 eeh seg < dmamap->dm_nsegs;
1148 1.1 eeh seg++, nexttx = GEM_NEXTTX(nexttx)) {
1149 1.1 eeh
1150 1.1 eeh /*
1151 1.1 eeh * If this is the first descriptor we're
1152 1.1 eeh * enqueueing, set the start of packet flag,
1153 1.1 eeh * and the checksum stuff if we want the hardware
1154 1.1 eeh * to do it.
1155 1.1 eeh */
1156 1.1 eeh sc->sc_txdescs[nexttx].gd_addr =
1157 1.2 eeh GEM_DMA_WRITE(sc, dmamap->dm_segs[seg].ds_addr);
1158 1.1 eeh flags = dmamap->dm_segs[seg].ds_len & GEM_TD_BUFSIZE;
1159 1.1 eeh if (nexttx == firsttx) {
1160 1.1 eeh flags |= GEM_TD_START_OF_PACKET;
1161 1.14 matt if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
1162 1.14 matt sc->sc_txwin = 0;
1163 1.14 matt flags |= GEM_TD_INTERRUPT_ME;
1164 1.14 matt }
1165 1.35 heas
1166 1.35 heas #ifdef INET
1167 1.35 heas /* h/w checksum */
1168 1.35 heas if (ifp->if_csum_flags_tx & (M_CSUM_TCPv4 |
1169 1.35 heas M_CSUM_UDPv4) && m0->m_pkthdr.csum_flags &
1170 1.35 heas (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1171 1.35 heas struct ether_header *eh;
1172 1.35 heas uint16_t offset, start;
1173 1.35 heas
1174 1.35 heas eh = mtod(m0, struct ether_header *);
1175 1.35 heas switch (ntohs(eh->ether_type)) {
1176 1.35 heas case ETHERTYPE_IP:
1177 1.35 heas start = ETHER_HDR_LEN;
1178 1.35 heas break;
1179 1.35 heas case ETHERTYPE_VLAN:
1180 1.35 heas start = ETHER_HDR_LEN +
1181 1.35 heas ETHER_VLAN_ENCAP_LEN;
1182 1.37 perry break;
1183 1.35 heas default:
1184 1.37 perry /* unsupported, drop it */
1185 1.35 heas m_free(m0);
1186 1.35 heas continue;
1187 1.35 heas }
1188 1.36 thorpej start += M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
1189 1.36 thorpej offset = M_CSUM_DATA_IPv4_OFFSET(m0->m_pkthdr.csum_data) + start;
1190 1.35 heas flags |= (start <<
1191 1.35 heas GEM_TD_CXSUM_STARTSHFT) |
1192 1.35 heas (offset <<
1193 1.35 heas GEM_TD_CXSUM_STUFFSHFT) |
1194 1.35 heas GEM_TD_CXSUM_ENABLE;
1195 1.35 heas }
1196 1.35 heas #endif
1197 1.1 eeh }
1198 1.1 eeh if (seg == dmamap->dm_nsegs - 1) {
1199 1.1 eeh flags |= GEM_TD_END_OF_PACKET;
1200 1.40 bouyer } else {
1201 1.40 bouyer /* last flag set outside of loop */
1202 1.40 bouyer sc->sc_txdescs[nexttx].gd_flags =
1203 1.40 bouyer GEM_DMA_WRITE(sc, flags);
1204 1.1 eeh }
1205 1.1 eeh lasttx = nexttx;
1206 1.1 eeh }
1207 1.40 bouyer if (m0->m_pkthdr.len < ETHER_MIN_TX) {
1208 1.40 bouyer /* add padding buffer at end of chain */
1209 1.40 bouyer flags &= ~GEM_TD_END_OF_PACKET;
1210 1.40 bouyer sc->sc_txdescs[lasttx].gd_flags =
1211 1.40 bouyer GEM_DMA_WRITE(sc, flags);
1212 1.40 bouyer
1213 1.40 bouyer sc->sc_txdescs[nexttx].gd_addr =
1214 1.40 bouyer GEM_DMA_WRITE(sc,
1215 1.40 bouyer sc->sc_nulldmamap->dm_segs[0].ds_addr);
1216 1.40 bouyer flags = ((ETHER_MIN_TX - m0->m_pkthdr.len) &
1217 1.40 bouyer GEM_TD_BUFSIZE) | GEM_TD_END_OF_PACKET;
1218 1.40 bouyer lasttx = nexttx;
1219 1.40 bouyer nexttx = GEM_NEXTTX(nexttx);
1220 1.40 bouyer seg++;
1221 1.40 bouyer }
1222 1.40 bouyer sc->sc_txdescs[lasttx].gd_flags = GEM_DMA_WRITE(sc, flags);
1223 1.30 christos
1224 1.30 christos KASSERT(lasttx != -1);
1225 1.1 eeh
1226 1.40 bouyer /*
1227 1.40 bouyer * Store a pointer to the packet so we can free it later,
1228 1.40 bouyer * and remember what txdirty will be once the packet is
1229 1.40 bouyer * done.
1230 1.40 bouyer */
1231 1.40 bouyer txs->txs_mbuf = m0;
1232 1.40 bouyer txs->txs_firstdesc = sc->sc_txnext;
1233 1.40 bouyer txs->txs_lastdesc = lasttx;
1234 1.40 bouyer txs->txs_ndescs = seg;
1235 1.40 bouyer
1236 1.1 eeh #ifdef GEM_DEBUG
1237 1.1 eeh if (ifp->if_flags & IFF_DEBUG) {
1238 1.1 eeh printf(" gem_start %p transmit chain:\n", txs);
1239 1.1 eeh for (seg = sc->sc_txnext;; seg = GEM_NEXTTX(seg)) {
1240 1.1 eeh printf("descriptor %d:\t", seg);
1241 1.1 eeh printf("gd_flags: 0x%016llx\t", (long long)
1242 1.2 eeh GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_flags));
1243 1.1 eeh printf("gd_addr: 0x%016llx\n", (long long)
1244 1.2 eeh GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_addr));
1245 1.1 eeh if (seg == lasttx)
1246 1.1 eeh break;
1247 1.1 eeh }
1248 1.1 eeh }
1249 1.1 eeh #endif
1250 1.1 eeh
1251 1.1 eeh /* Sync the descriptors we're using. */
1252 1.1 eeh GEM_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1253 1.1 eeh BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1254 1.1 eeh
1255 1.1 eeh /* Advance the tx pointer. */
1256 1.40 bouyer sc->sc_txfree -= txs->txs_ndescs;
1257 1.1 eeh sc->sc_txnext = nexttx;
1258 1.1 eeh
1259 1.21 lukem SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
1260 1.1 eeh SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1261 1.1 eeh
1262 1.1 eeh last_txs = txs;
1263 1.1 eeh
1264 1.1 eeh #if NBPFILTER > 0
1265 1.1 eeh /*
1266 1.1 eeh * Pass the packet to any BPF listeners.
1267 1.1 eeh */
1268 1.1 eeh if (ifp->if_bpf)
1269 1.1 eeh bpf_mtap(ifp->if_bpf, m0);
1270 1.1 eeh #endif /* NBPFILTER > 0 */
1271 1.1 eeh }
1272 1.1 eeh
1273 1.1 eeh if (txs == NULL || sc->sc_txfree == 0) {
1274 1.1 eeh /* No more slots left; notify upper layer. */
1275 1.1 eeh ifp->if_flags |= IFF_OACTIVE;
1276 1.41 christos sc->sc_if_flags = ifp->if_flags;
1277 1.1 eeh }
1278 1.1 eeh
1279 1.1 eeh if (sc->sc_txfree != ofree) {
1280 1.1 eeh DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
1281 1.1 eeh sc->sc_dev.dv_xname, lasttx, firsttx));
1282 1.1 eeh /*
1283 1.31 heas * The entire packet chain is set up.
1284 1.1 eeh * Kick the transmitter.
1285 1.1 eeh */
1286 1.1 eeh DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
1287 1.1 eeh sc->sc_dev.dv_xname, nexttx));
1288 1.1 eeh bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK,
1289 1.1 eeh sc->sc_txnext);
1290 1.1 eeh
1291 1.1 eeh /* Set a watchdog timer in case the chip flakes out. */
1292 1.1 eeh ifp->if_timer = 5;
1293 1.1 eeh DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
1294 1.1 eeh sc->sc_dev.dv_xname, ifp->if_timer));
1295 1.1 eeh }
1296 1.1 eeh }
1297 1.1 eeh
1298 1.1 eeh /*
1299 1.1 eeh * Transmit interrupt.
1300 1.1 eeh */
1301 1.1 eeh int
1302 1.1 eeh gem_tint(sc)
1303 1.1 eeh struct gem_softc *sc;
1304 1.1 eeh {
1305 1.1 eeh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1306 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1307 1.1 eeh bus_space_handle_t mac = sc->sc_h;
1308 1.1 eeh struct gem_txsoft *txs;
1309 1.1 eeh int txlast;
1310 1.14 matt int progress = 0;
1311 1.1 eeh
1312 1.1 eeh
1313 1.2 eeh DPRINTF(sc, ("%s: gem_tint\n", sc->sc_dev.dv_xname));
1314 1.1 eeh
1315 1.1 eeh /*
1316 1.1 eeh * Unload collision counters
1317 1.1 eeh */
1318 1.1 eeh ifp->if_collisions +=
1319 1.1 eeh bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
1320 1.1 eeh bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
1321 1.1 eeh bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
1322 1.1 eeh bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
1323 1.1 eeh
1324 1.1 eeh /*
1325 1.1 eeh * then clear the hardware counters.
1326 1.1 eeh */
1327 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
1328 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
1329 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
1330 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
1331 1.1 eeh
1332 1.1 eeh /*
1333 1.1 eeh * Go through our Tx list and free mbufs for those
1334 1.1 eeh * frames that have been transmitted.
1335 1.1 eeh */
1336 1.1 eeh while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1337 1.1 eeh GEM_CDTXSYNC(sc, txs->txs_lastdesc,
1338 1.1 eeh txs->txs_ndescs,
1339 1.1 eeh BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1340 1.1 eeh
1341 1.1 eeh #ifdef GEM_DEBUG
1342 1.1 eeh if (ifp->if_flags & IFF_DEBUG) {
1343 1.1 eeh int i;
1344 1.1 eeh printf(" txsoft %p transmit chain:\n", txs);
1345 1.1 eeh for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
1346 1.1 eeh printf("descriptor %d: ", i);
1347 1.1 eeh printf("gd_flags: 0x%016llx\t", (long long)
1348 1.2 eeh GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
1349 1.1 eeh printf("gd_addr: 0x%016llx\n", (long long)
1350 1.2 eeh GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
1351 1.1 eeh if (i == txs->txs_lastdesc)
1352 1.1 eeh break;
1353 1.1 eeh }
1354 1.1 eeh }
1355 1.1 eeh #endif
1356 1.1 eeh
1357 1.1 eeh /*
1358 1.1 eeh * In theory, we could harveast some descriptors before
1359 1.1 eeh * the ring is empty, but that's a bit complicated.
1360 1.1 eeh *
1361 1.1 eeh * GEM_TX_COMPLETION points to the last descriptor
1362 1.1 eeh * processed +1.
1363 1.1 eeh */
1364 1.1 eeh txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
1365 1.1 eeh DPRINTF(sc,
1366 1.1 eeh ("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
1367 1.1 eeh txs->txs_lastdesc, txlast));
1368 1.1 eeh if (txs->txs_firstdesc <= txs->txs_lastdesc) {
1369 1.1 eeh if ((txlast >= txs->txs_firstdesc) &&
1370 1.1 eeh (txlast <= txs->txs_lastdesc))
1371 1.1 eeh break;
1372 1.1 eeh } else {
1373 1.1 eeh /* Ick -- this command wraps */
1374 1.1 eeh if ((txlast >= txs->txs_firstdesc) ||
1375 1.1 eeh (txlast <= txs->txs_lastdesc))
1376 1.1 eeh break;
1377 1.1 eeh }
1378 1.1 eeh
1379 1.1 eeh DPRINTF(sc, ("gem_tint: releasing a desc\n"));
1380 1.21 lukem SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1381 1.1 eeh
1382 1.1 eeh sc->sc_txfree += txs->txs_ndescs;
1383 1.1 eeh
1384 1.1 eeh if (txs->txs_mbuf == NULL) {
1385 1.1 eeh #ifdef DIAGNOSTIC
1386 1.1 eeh panic("gem_txintr: null mbuf");
1387 1.1 eeh #endif
1388 1.1 eeh }
1389 1.1 eeh
1390 1.1 eeh bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
1391 1.1 eeh 0, txs->txs_dmamap->dm_mapsize,
1392 1.1 eeh BUS_DMASYNC_POSTWRITE);
1393 1.1 eeh bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
1394 1.1 eeh m_freem(txs->txs_mbuf);
1395 1.1 eeh txs->txs_mbuf = NULL;
1396 1.1 eeh
1397 1.1 eeh SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1398 1.1 eeh
1399 1.1 eeh ifp->if_opackets++;
1400 1.14 matt progress = 1;
1401 1.1 eeh }
1402 1.1 eeh
1403 1.28 chs #if 0
1404 1.1 eeh DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
1405 1.1 eeh "GEM_TX_DATA_PTR %llx "
1406 1.1 eeh "GEM_TX_COMPLETION %x\n",
1407 1.1 eeh bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
1408 1.4 thorpej ((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h,
1409 1.4 thorpej GEM_TX_DATA_PTR_HI) << 32) |
1410 1.4 thorpej bus_space_read_4(sc->sc_bustag, sc->sc_h,
1411 1.4 thorpej GEM_TX_DATA_PTR_LO),
1412 1.1 eeh bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)));
1413 1.28 chs #endif
1414 1.1 eeh
1415 1.14 matt if (progress) {
1416 1.14 matt if (sc->sc_txfree == GEM_NTXDESC - 1)
1417 1.14 matt sc->sc_txwin = 0;
1418 1.14 matt
1419 1.14 matt ifp->if_flags &= ~IFF_OACTIVE;
1420 1.41 christos sc->sc_if_flags = ifp->if_flags;
1421 1.14 matt gem_start(ifp);
1422 1.1 eeh
1423 1.21 lukem if (SIMPLEQ_EMPTY(&sc->sc_txdirtyq))
1424 1.14 matt ifp->if_timer = 0;
1425 1.14 matt }
1426 1.1 eeh DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
1427 1.1 eeh sc->sc_dev.dv_xname, ifp->if_timer));
1428 1.1 eeh
1429 1.1 eeh return (1);
1430 1.1 eeh }
1431 1.1 eeh
1432 1.1 eeh /*
1433 1.1 eeh * Receive interrupt.
1434 1.1 eeh */
1435 1.1 eeh int
1436 1.1 eeh gem_rint(sc)
1437 1.1 eeh struct gem_softc *sc;
1438 1.1 eeh {
1439 1.1 eeh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1440 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1441 1.1 eeh bus_space_handle_t h = sc->sc_h;
1442 1.1 eeh struct gem_rxsoft *rxs;
1443 1.1 eeh struct mbuf *m;
1444 1.1 eeh u_int64_t rxstat;
1445 1.18 matt u_int32_t rxcomp;
1446 1.18 matt int i, len, progress = 0;
1447 1.1 eeh
1448 1.2 eeh DPRINTF(sc, ("%s: gem_rint\n", sc->sc_dev.dv_xname));
1449 1.18 matt
1450 1.18 matt /*
1451 1.18 matt * Read the completion register once. This limits
1452 1.18 matt * how long the following loop can execute.
1453 1.18 matt */
1454 1.18 matt rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION);
1455 1.18 matt
1456 1.1 eeh /*
1457 1.1 eeh * XXXX Read the lastrx only once at the top for speed.
1458 1.1 eeh */
1459 1.1 eeh DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
1460 1.18 matt sc->sc_rxptr, rxcomp));
1461 1.18 matt
1462 1.18 matt /*
1463 1.18 matt * Go into the loop at least once.
1464 1.18 matt */
1465 1.18 matt for (i = sc->sc_rxptr; i == sc->sc_rxptr || i != rxcomp;
1466 1.1 eeh i = GEM_NEXTRX(i)) {
1467 1.1 eeh rxs = &sc->sc_rxsoft[i];
1468 1.1 eeh
1469 1.1 eeh GEM_CDRXSYNC(sc, i,
1470 1.1 eeh BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1471 1.1 eeh
1472 1.2 eeh rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
1473 1.1 eeh
1474 1.1 eeh if (rxstat & GEM_RD_OWN) {
1475 1.1 eeh /*
1476 1.1 eeh * We have processed all of the receive buffers.
1477 1.1 eeh */
1478 1.1 eeh break;
1479 1.1 eeh }
1480 1.1 eeh
1481 1.18 matt progress++;
1482 1.18 matt ifp->if_ipackets++;
1483 1.18 matt
1484 1.1 eeh if (rxstat & GEM_RD_BAD_CRC) {
1485 1.18 matt ifp->if_ierrors++;
1486 1.1 eeh printf("%s: receive error: CRC error\n",
1487 1.1 eeh sc->sc_dev.dv_xname);
1488 1.1 eeh GEM_INIT_RXDESC(sc, i);
1489 1.1 eeh continue;
1490 1.1 eeh }
1491 1.1 eeh
1492 1.1 eeh bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1493 1.1 eeh rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1494 1.1 eeh #ifdef GEM_DEBUG
1495 1.1 eeh if (ifp->if_flags & IFF_DEBUG) {
1496 1.1 eeh printf(" rxsoft %p descriptor %d: ", rxs, i);
1497 1.1 eeh printf("gd_flags: 0x%016llx\t", (long long)
1498 1.2 eeh GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
1499 1.1 eeh printf("gd_addr: 0x%016llx\n", (long long)
1500 1.2 eeh GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
1501 1.1 eeh }
1502 1.1 eeh #endif
1503 1.1 eeh
1504 1.35 heas /* No errors; receive the packet. */
1505 1.35 heas len = GEM_RD_BUFLEN(rxstat);
1506 1.1 eeh
1507 1.1 eeh /*
1508 1.1 eeh * Allocate a new mbuf cluster. If that fails, we are
1509 1.1 eeh * out of memory, and must drop the packet and recycle
1510 1.1 eeh * the buffer that's already attached to this descriptor.
1511 1.1 eeh */
1512 1.1 eeh m = rxs->rxs_mbuf;
1513 1.1 eeh if (gem_add_rxbuf(sc, i) != 0) {
1514 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxnobuf);
1515 1.1 eeh ifp->if_ierrors++;
1516 1.1 eeh GEM_INIT_RXDESC(sc, i);
1517 1.1 eeh bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1518 1.1 eeh rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1519 1.1 eeh continue;
1520 1.1 eeh }
1521 1.1 eeh m->m_data += 2; /* We're already off by two */
1522 1.1 eeh
1523 1.1 eeh m->m_pkthdr.rcvif = ifp;
1524 1.1 eeh m->m_pkthdr.len = m->m_len = len;
1525 1.1 eeh
1526 1.1 eeh #if NBPFILTER > 0
1527 1.1 eeh /*
1528 1.1 eeh * Pass this up to any BPF listeners, but only
1529 1.1 eeh * pass it up the stack if its for us.
1530 1.1 eeh */
1531 1.1 eeh if (ifp->if_bpf)
1532 1.1 eeh bpf_mtap(ifp->if_bpf, m);
1533 1.1 eeh #endif /* NPBFILTER > 0 */
1534 1.1 eeh
1535 1.35 heas #ifdef INET
1536 1.35 heas /* hardware checksum */
1537 1.35 heas if (ifp->if_csum_flags_rx & (M_CSUM_UDPv4 | M_CSUM_TCPv4)) {
1538 1.35 heas struct ether_header *eh;
1539 1.35 heas struct ip *ip;
1540 1.35 heas struct udphdr *uh;
1541 1.35 heas int32_t hlen, pktlen;
1542 1.35 heas
1543 1.35 heas if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
1544 1.35 heas pktlen = m->m_pkthdr.len - ETHER_HDR_LEN -
1545 1.35 heas ETHER_VLAN_ENCAP_LEN;
1546 1.35 heas eh = (struct ether_header *) mtod(m, caddr_t) +
1547 1.35 heas ETHER_VLAN_ENCAP_LEN;
1548 1.35 heas } else {
1549 1.35 heas pktlen = m->m_pkthdr.len - ETHER_HDR_LEN;
1550 1.35 heas eh = mtod(m, struct ether_header *);
1551 1.35 heas }
1552 1.35 heas if (ntohs(eh->ether_type) != ETHERTYPE_IP)
1553 1.35 heas goto swcsum;
1554 1.35 heas ip = (struct ip *) ((caddr_t)eh + ETHER_HDR_LEN);
1555 1.35 heas
1556 1.35 heas /* IPv4 only */
1557 1.35 heas if (ip->ip_v != IPVERSION)
1558 1.35 heas goto swcsum;
1559 1.35 heas
1560 1.35 heas hlen = ip->ip_hl << 2;
1561 1.35 heas if (hlen < sizeof(struct ip))
1562 1.35 heas goto swcsum;
1563 1.35 heas
1564 1.38 heas /*
1565 1.38 heas * bail if too short, has random trailing garbage,
1566 1.38 heas * truncated, fragment, or has ethernet pad.
1567 1.38 heas */
1568 1.35 heas if ((ntohs(ip->ip_len) < hlen) ||
1569 1.38 heas (ntohs(ip->ip_len) != pktlen) ||
1570 1.35 heas (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)))
1571 1.35 heas goto swcsum;
1572 1.35 heas
1573 1.35 heas switch (ip->ip_p) {
1574 1.35 heas case IPPROTO_TCP:
1575 1.35 heas if (! (ifp->if_csum_flags_rx & M_CSUM_TCPv4))
1576 1.35 heas goto swcsum;
1577 1.35 heas if (pktlen < (hlen + sizeof(struct tcphdr)))
1578 1.35 heas goto swcsum;
1579 1.35 heas m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1580 1.35 heas break;
1581 1.35 heas case IPPROTO_UDP:
1582 1.35 heas if (! (ifp->if_csum_flags_rx & M_CSUM_UDPv4))
1583 1.35 heas goto swcsum;
1584 1.35 heas if (pktlen < (hlen + sizeof(struct udphdr)))
1585 1.35 heas goto swcsum;
1586 1.35 heas uh = (struct udphdr *)((caddr_t)ip + hlen);
1587 1.35 heas /* no checksum */
1588 1.35 heas if (uh->uh_sum == 0)
1589 1.35 heas goto swcsum;
1590 1.35 heas m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1591 1.35 heas break;
1592 1.35 heas default:
1593 1.35 heas goto swcsum;
1594 1.35 heas }
1595 1.35 heas
1596 1.35 heas /* the uncomplemented sum is expected */
1597 1.35 heas m->m_pkthdr.csum_data = (~rxstat) & GEM_RD_CHECKSUM;
1598 1.35 heas
1599 1.35 heas /* if the pkt had ip options, we have to deduct them */
1600 1.35 heas if (hlen > sizeof(struct ip)) {
1601 1.35 heas uint16_t *opts;
1602 1.35 heas uint32_t optsum, temp;
1603 1.35 heas
1604 1.35 heas optsum = 0;
1605 1.35 heas temp = hlen - sizeof(struct ip);
1606 1.35 heas opts = (uint16_t *) ((caddr_t) ip +
1607 1.35 heas sizeof(struct ip));
1608 1.35 heas
1609 1.35 heas while (temp > 1) {
1610 1.35 heas optsum += ntohs(*opts++);
1611 1.35 heas temp -= 2;
1612 1.35 heas }
1613 1.35 heas while (optsum >> 16)
1614 1.35 heas optsum = (optsum >> 16) +
1615 1.35 heas (optsum & 0xffff);
1616 1.35 heas
1617 1.35 heas /* Deduct ip opts sum from hwsum (rfc 1624). */
1618 1.35 heas m->m_pkthdr.csum_data =
1619 1.35 heas ~((~m->m_pkthdr.csum_data) - ~optsum);
1620 1.35 heas
1621 1.35 heas while (m->m_pkthdr.csum_data >> 16)
1622 1.35 heas m->m_pkthdr.csum_data =
1623 1.35 heas (m->m_pkthdr.csum_data >> 16) +
1624 1.35 heas (m->m_pkthdr.csum_data &
1625 1.35 heas 0xffff);
1626 1.35 heas }
1627 1.35 heas
1628 1.35 heas m->m_pkthdr.csum_flags |= M_CSUM_DATA |
1629 1.35 heas M_CSUM_NO_PSEUDOHDR;
1630 1.35 heas } else
1631 1.35 heas swcsum:
1632 1.35 heas m->m_pkthdr.csum_flags = 0;
1633 1.35 heas #endif
1634 1.1 eeh /* Pass it on. */
1635 1.1 eeh (*ifp->if_input)(ifp, m);
1636 1.1 eeh }
1637 1.1 eeh
1638 1.18 matt if (progress) {
1639 1.18 matt /* Update the receive pointer. */
1640 1.18 matt if (i == sc->sc_rxptr) {
1641 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxfull);
1642 1.19 matt #ifdef GEM_DEBUG
1643 1.28 chs if (ifp->if_flags & IFF_DEBUG)
1644 1.19 matt printf("%s: rint: ring wrap\n",
1645 1.19 matt sc->sc_dev.dv_xname);
1646 1.19 matt #endif
1647 1.18 matt }
1648 1.18 matt sc->sc_rxptr = i;
1649 1.18 matt bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i));
1650 1.18 matt }
1651 1.19 matt #ifdef GEM_COUNTERS
1652 1.18 matt if (progress <= 4) {
1653 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxhist[progress]);
1654 1.28 chs } else if (progress < 32) {
1655 1.18 matt if (progress < 16)
1656 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxhist[5]);
1657 1.18 matt else
1658 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxhist[6]);
1659 1.31 heas
1660 1.18 matt } else {
1661 1.18 matt if (progress < 64)
1662 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxhist[7]);
1663 1.18 matt else
1664 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxhist[8]);
1665 1.18 matt }
1666 1.19 matt #endif
1667 1.1 eeh
1668 1.1 eeh DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
1669 1.1 eeh sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
1670 1.1 eeh
1671 1.1 eeh return (1);
1672 1.1 eeh }
1673 1.1 eeh
1674 1.1 eeh
1675 1.1 eeh /*
1676 1.1 eeh * gem_add_rxbuf:
1677 1.1 eeh *
1678 1.1 eeh * Add a receive buffer to the indicated descriptor.
1679 1.1 eeh */
1680 1.1 eeh int
1681 1.1 eeh gem_add_rxbuf(struct gem_softc *sc, int idx)
1682 1.1 eeh {
1683 1.1 eeh struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
1684 1.1 eeh struct mbuf *m;
1685 1.1 eeh int error;
1686 1.1 eeh
1687 1.1 eeh MGETHDR(m, M_DONTWAIT, MT_DATA);
1688 1.1 eeh if (m == NULL)
1689 1.1 eeh return (ENOBUFS);
1690 1.1 eeh
1691 1.26 matt MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1692 1.1 eeh MCLGET(m, M_DONTWAIT);
1693 1.1 eeh if ((m->m_flags & M_EXT) == 0) {
1694 1.1 eeh m_freem(m);
1695 1.1 eeh return (ENOBUFS);
1696 1.1 eeh }
1697 1.1 eeh
1698 1.1 eeh #ifdef GEM_DEBUG
1699 1.27 wiz /* bzero the packet to check DMA */
1700 1.1 eeh memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
1701 1.1 eeh #endif
1702 1.1 eeh
1703 1.1 eeh if (rxs->rxs_mbuf != NULL)
1704 1.1 eeh bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
1705 1.1 eeh
1706 1.1 eeh rxs->rxs_mbuf = m;
1707 1.1 eeh
1708 1.1 eeh error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
1709 1.1 eeh m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1710 1.1 eeh BUS_DMA_READ|BUS_DMA_NOWAIT);
1711 1.1 eeh if (error) {
1712 1.1 eeh printf("%s: can't load rx DMA map %d, error = %d\n",
1713 1.1 eeh sc->sc_dev.dv_xname, idx, error);
1714 1.1 eeh panic("gem_add_rxbuf"); /* XXX */
1715 1.1 eeh }
1716 1.1 eeh
1717 1.1 eeh bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1718 1.1 eeh rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1719 1.1 eeh
1720 1.1 eeh GEM_INIT_RXDESC(sc, idx);
1721 1.1 eeh
1722 1.1 eeh return (0);
1723 1.1 eeh }
1724 1.1 eeh
1725 1.1 eeh
1726 1.1 eeh int
1727 1.1 eeh gem_eint(sc, status)
1728 1.1 eeh struct gem_softc *sc;
1729 1.1 eeh u_int status;
1730 1.1 eeh {
1731 1.1 eeh char bits[128];
1732 1.1 eeh
1733 1.1 eeh if ((status & GEM_INTR_MIF) != 0) {
1734 1.1 eeh printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
1735 1.1 eeh return (1);
1736 1.1 eeh }
1737 1.1 eeh
1738 1.1 eeh printf("%s: status=%s\n", sc->sc_dev.dv_xname,
1739 1.1 eeh bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
1740 1.1 eeh return (1);
1741 1.1 eeh }
1742 1.1 eeh
1743 1.1 eeh
1744 1.1 eeh int
1745 1.1 eeh gem_intr(v)
1746 1.1 eeh void *v;
1747 1.1 eeh {
1748 1.1 eeh struct gem_softc *sc = (struct gem_softc *)v;
1749 1.41 christos struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1750 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1751 1.1 eeh bus_space_handle_t seb = sc->sc_h;
1752 1.1 eeh u_int32_t status;
1753 1.1 eeh int r = 0;
1754 1.3 eeh #ifdef GEM_DEBUG
1755 1.1 eeh char bits[128];
1756 1.3 eeh #endif
1757 1.1 eeh
1758 1.19 matt sc->sc_ev_intr.ev_count++;
1759 1.19 matt
1760 1.1 eeh status = bus_space_read_4(t, seb, GEM_STATUS);
1761 1.28 chs DPRINTF(sc, ("%s: gem_intr: cplt 0x%x status %s\n",
1762 1.28 chs sc->sc_dev.dv_xname, (status >> 19),
1763 1.1 eeh bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits))));
1764 1.1 eeh
1765 1.1 eeh if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
1766 1.1 eeh r |= gem_eint(sc, status);
1767 1.1 eeh
1768 1.18 matt if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) {
1769 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_txint);
1770 1.1 eeh r |= gem_tint(sc);
1771 1.18 matt }
1772 1.1 eeh
1773 1.18 matt if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) {
1774 1.19 matt GEM_COUNTER_INCR(sc, sc_ev_rxint);
1775 1.1 eeh r |= gem_rint(sc);
1776 1.18 matt }
1777 1.1 eeh
1778 1.1 eeh /* We should eventually do more than just print out error stats. */
1779 1.1 eeh if (status & GEM_INTR_TX_MAC) {
1780 1.1 eeh int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS);
1781 1.1 eeh if (txstat & ~GEM_MAC_TX_XMIT_DONE)
1782 1.14 matt printf("%s: MAC tx fault, status %x\n",
1783 1.14 matt sc->sc_dev.dv_xname, txstat);
1784 1.41 christos if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG))
1785 1.41 christos gem_init(ifp);
1786 1.1 eeh }
1787 1.1 eeh if (status & GEM_INTR_RX_MAC) {
1788 1.1 eeh int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS);
1789 1.1 eeh if (rxstat & ~GEM_MAC_RX_DONE)
1790 1.14 matt printf("%s: MAC rx fault, status %x\n",
1791 1.14 matt sc->sc_dev.dv_xname, rxstat);
1792 1.41 christos /*
1793 1.41 christos * On some chip revisions GEM_MAC_RX_OVERFLOW happen often
1794 1.41 christos * due to a silicon bug so handle them silently.
1795 1.41 christos */
1796 1.41 christos if (rxstat & GEM_MAC_RX_OVERFLOW)
1797 1.41 christos gem_init(ifp);
1798 1.41 christos else if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
1799 1.41 christos printf("%s: MAC rx fault, status %x\n",
1800 1.41 christos sc->sc_dev.dv_xname, rxstat);
1801 1.1 eeh }
1802 1.45 heas #if NRND > 0
1803 1.45 heas rnd_add_uint32(&sc->rnd_source, status);
1804 1.45 heas #endif
1805 1.1 eeh return (r);
1806 1.1 eeh }
1807 1.1 eeh
1808 1.1 eeh
1809 1.1 eeh void
1810 1.1 eeh gem_watchdog(ifp)
1811 1.1 eeh struct ifnet *ifp;
1812 1.1 eeh {
1813 1.1 eeh struct gem_softc *sc = ifp->if_softc;
1814 1.1 eeh
1815 1.1 eeh DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
1816 1.1 eeh "GEM_MAC_RX_CONFIG %x\n",
1817 1.1 eeh bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
1818 1.1 eeh bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
1819 1.1 eeh bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)));
1820 1.1 eeh
1821 1.1 eeh log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1822 1.1 eeh ++ifp->if_oerrors;
1823 1.1 eeh
1824 1.1 eeh /* Try to get more packets going. */
1825 1.1 eeh gem_start(ifp);
1826 1.1 eeh }
1827 1.1 eeh
1828 1.1 eeh /*
1829 1.1 eeh * Initialize the MII Management Interface
1830 1.1 eeh */
1831 1.1 eeh void
1832 1.1 eeh gem_mifinit(sc)
1833 1.1 eeh struct gem_softc *sc;
1834 1.1 eeh {
1835 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1836 1.1 eeh bus_space_handle_t mif = sc->sc_h;
1837 1.1 eeh
1838 1.1 eeh /* Configure the MIF in frame mode */
1839 1.1 eeh sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1840 1.1 eeh sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
1841 1.1 eeh bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
1842 1.1 eeh }
1843 1.1 eeh
1844 1.1 eeh /*
1845 1.1 eeh * MII interface
1846 1.1 eeh *
1847 1.1 eeh * The GEM MII interface supports at least three different operating modes:
1848 1.1 eeh *
1849 1.1 eeh * Bitbang mode is implemented using data, clock and output enable registers.
1850 1.1 eeh *
1851 1.1 eeh * Frame mode is implemented by loading a complete frame into the frame
1852 1.1 eeh * register and polling the valid bit for completion.
1853 1.1 eeh *
1854 1.1 eeh * Polling mode uses the frame register but completion is indicated by
1855 1.1 eeh * an interrupt.
1856 1.1 eeh *
1857 1.1 eeh */
1858 1.1 eeh static int
1859 1.1 eeh gem_mii_readreg(self, phy, reg)
1860 1.1 eeh struct device *self;
1861 1.1 eeh int phy, reg;
1862 1.1 eeh {
1863 1.1 eeh struct gem_softc *sc = (void *)self;
1864 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1865 1.1 eeh bus_space_handle_t mif = sc->sc_h;
1866 1.1 eeh int n;
1867 1.1 eeh u_int32_t v;
1868 1.1 eeh
1869 1.1 eeh #ifdef GEM_DEBUG1
1870 1.1 eeh if (sc->sc_debug)
1871 1.1 eeh printf("gem_mii_readreg: phy %d reg %d\n", phy, reg);
1872 1.1 eeh #endif
1873 1.1 eeh
1874 1.1 eeh #if 0
1875 1.1 eeh /* Select the desired PHY in the MIF configuration register */
1876 1.1 eeh v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1877 1.1 eeh /* Clear PHY select bit */
1878 1.1 eeh v &= ~GEM_MIF_CONFIG_PHY_SEL;
1879 1.1 eeh if (phy == GEM_PHYAD_EXTERNAL)
1880 1.1 eeh /* Set PHY select bit to get at external device */
1881 1.1 eeh v |= GEM_MIF_CONFIG_PHY_SEL;
1882 1.1 eeh bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
1883 1.1 eeh #endif
1884 1.1 eeh
1885 1.1 eeh /* Construct the frame command */
1886 1.1 eeh v = (reg << GEM_MIF_REG_SHIFT) | (phy << GEM_MIF_PHY_SHIFT) |
1887 1.1 eeh GEM_MIF_FRAME_READ;
1888 1.1 eeh
1889 1.1 eeh bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
1890 1.1 eeh for (n = 0; n < 100; n++) {
1891 1.1 eeh DELAY(1);
1892 1.1 eeh v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
1893 1.1 eeh if (v & GEM_MIF_FRAME_TA0)
1894 1.1 eeh return (v & GEM_MIF_FRAME_DATA);
1895 1.1 eeh }
1896 1.1 eeh
1897 1.1 eeh printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
1898 1.1 eeh return (0);
1899 1.1 eeh }
1900 1.1 eeh
1901 1.1 eeh static void
1902 1.1 eeh gem_mii_writereg(self, phy, reg, val)
1903 1.1 eeh struct device *self;
1904 1.1 eeh int phy, reg, val;
1905 1.1 eeh {
1906 1.1 eeh struct gem_softc *sc = (void *)self;
1907 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1908 1.1 eeh bus_space_handle_t mif = sc->sc_h;
1909 1.1 eeh int n;
1910 1.1 eeh u_int32_t v;
1911 1.1 eeh
1912 1.1 eeh #ifdef GEM_DEBUG1
1913 1.1 eeh if (sc->sc_debug)
1914 1.31 heas printf("gem_mii_writereg: phy %d reg %d val %x\n",
1915 1.1 eeh phy, reg, val);
1916 1.1 eeh #endif
1917 1.1 eeh
1918 1.1 eeh #if 0
1919 1.1 eeh /* Select the desired PHY in the MIF configuration register */
1920 1.1 eeh v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1921 1.1 eeh /* Clear PHY select bit */
1922 1.1 eeh v &= ~GEM_MIF_CONFIG_PHY_SEL;
1923 1.1 eeh if (phy == GEM_PHYAD_EXTERNAL)
1924 1.1 eeh /* Set PHY select bit to get at external device */
1925 1.1 eeh v |= GEM_MIF_CONFIG_PHY_SEL;
1926 1.1 eeh bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
1927 1.1 eeh #endif
1928 1.1 eeh /* Construct the frame command */
1929 1.1 eeh v = GEM_MIF_FRAME_WRITE |
1930 1.1 eeh (phy << GEM_MIF_PHY_SHIFT) |
1931 1.1 eeh (reg << GEM_MIF_REG_SHIFT) |
1932 1.1 eeh (val & GEM_MIF_FRAME_DATA);
1933 1.1 eeh
1934 1.1 eeh bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
1935 1.1 eeh for (n = 0; n < 100; n++) {
1936 1.1 eeh DELAY(1);
1937 1.1 eeh v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
1938 1.1 eeh if (v & GEM_MIF_FRAME_TA0)
1939 1.1 eeh return;
1940 1.1 eeh }
1941 1.1 eeh
1942 1.1 eeh printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
1943 1.1 eeh }
1944 1.1 eeh
1945 1.1 eeh static void
1946 1.1 eeh gem_mii_statchg(dev)
1947 1.1 eeh struct device *dev;
1948 1.1 eeh {
1949 1.1 eeh struct gem_softc *sc = (void *)dev;
1950 1.3 eeh #ifdef GEM_DEBUG
1951 1.1 eeh int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1952 1.3 eeh #endif
1953 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
1954 1.1 eeh bus_space_handle_t mac = sc->sc_h;
1955 1.1 eeh u_int32_t v;
1956 1.1 eeh
1957 1.1 eeh #ifdef GEM_DEBUG
1958 1.1 eeh if (sc->sc_debug)
1959 1.31 heas printf("gem_mii_statchg: status change: phy = %d\n",
1960 1.28 chs sc->sc_phys[instance]);
1961 1.1 eeh #endif
1962 1.1 eeh
1963 1.1 eeh
1964 1.1 eeh /* Set tx full duplex options */
1965 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
1966 1.1 eeh delay(10000); /* reg must be cleared and delay before changing. */
1967 1.1 eeh v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT|
1968 1.1 eeh GEM_MAC_TX_ENABLE;
1969 1.1 eeh if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
1970 1.1 eeh v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS;
1971 1.1 eeh }
1972 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v);
1973 1.1 eeh
1974 1.1 eeh /* XIF Configuration */
1975 1.1 eeh /* We should really calculate all this rather than rely on defaults */
1976 1.1 eeh v = bus_space_read_4(t, mac, GEM_MAC_XIF_CONFIG);
1977 1.1 eeh v = GEM_MAC_XIF_LINK_LED;
1978 1.1 eeh v |= GEM_MAC_XIF_TX_MII_ENA;
1979 1.15 matt
1980 1.1 eeh /* If an external transceiver is connected, enable its MII drivers */
1981 1.1 eeh sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
1982 1.1 eeh if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
1983 1.1 eeh /* External MII needs echo disable if half duplex. */
1984 1.1 eeh if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1985 1.1 eeh /* turn on full duplex LED */
1986 1.1 eeh v |= GEM_MAC_XIF_FDPLX_LED;
1987 1.15 matt else
1988 1.15 matt /* half duplex -- disable echo */
1989 1.15 matt v |= GEM_MAC_XIF_ECHO_DISABL;
1990 1.15 matt
1991 1.14 matt if (sc->sc_ethercom.ec_if.if_baudrate == IF_Mbps(1000))
1992 1.14 matt v |= GEM_MAC_XIF_GMII_MODE;
1993 1.14 matt else
1994 1.14 matt v &= ~GEM_MAC_XIF_GMII_MODE;
1995 1.31 heas } else
1996 1.1 eeh /* Internal MII needs buf enable */
1997 1.1 eeh v |= GEM_MAC_XIF_MII_BUF_ENA;
1998 1.1 eeh bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
1999 1.1 eeh }
2000 1.1 eeh
2001 1.1 eeh int
2002 1.1 eeh gem_mediachange(ifp)
2003 1.1 eeh struct ifnet *ifp;
2004 1.1 eeh {
2005 1.1 eeh struct gem_softc *sc = ifp->if_softc;
2006 1.1 eeh
2007 1.11 thorpej if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
2008 1.11 thorpej return (EINVAL);
2009 1.1 eeh
2010 1.1 eeh return (mii_mediachg(&sc->sc_mii));
2011 1.1 eeh }
2012 1.1 eeh
2013 1.1 eeh void
2014 1.1 eeh gem_mediastatus(ifp, ifmr)
2015 1.1 eeh struct ifnet *ifp;
2016 1.1 eeh struct ifmediareq *ifmr;
2017 1.1 eeh {
2018 1.1 eeh struct gem_softc *sc = ifp->if_softc;
2019 1.1 eeh
2020 1.1 eeh if ((ifp->if_flags & IFF_UP) == 0)
2021 1.1 eeh return;
2022 1.1 eeh
2023 1.1 eeh mii_pollstat(&sc->sc_mii);
2024 1.1 eeh ifmr->ifm_active = sc->sc_mii.mii_media_active;
2025 1.1 eeh ifmr->ifm_status = sc->sc_mii.mii_media_status;
2026 1.1 eeh }
2027 1.1 eeh
2028 1.1 eeh /*
2029 1.1 eeh * Process an ioctl request.
2030 1.1 eeh */
2031 1.1 eeh int
2032 1.1 eeh gem_ioctl(ifp, cmd, data)
2033 1.1 eeh struct ifnet *ifp;
2034 1.1 eeh u_long cmd;
2035 1.1 eeh caddr_t data;
2036 1.1 eeh {
2037 1.1 eeh struct gem_softc *sc = ifp->if_softc;
2038 1.1 eeh struct ifreq *ifr = (struct ifreq *)data;
2039 1.1 eeh int s, error = 0;
2040 1.1 eeh
2041 1.20 matt s = splnet();
2042 1.1 eeh
2043 1.1 eeh switch (cmd) {
2044 1.1 eeh case SIOCGIFMEDIA:
2045 1.1 eeh case SIOCSIFMEDIA:
2046 1.1 eeh error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
2047 1.1 eeh break;
2048 1.41 christos case SIOCSIFFLAGS:
2049 1.41 christos #define RESETIGN (IFF_CANTCHANGE|IFF_DEBUG)
2050 1.41 christos if (((ifp->if_flags & (IFF_UP|IFF_RUNNING))
2051 1.41 christos == (IFF_UP|IFF_RUNNING))
2052 1.41 christos && ((ifp->if_flags & (~RESETIGN))
2053 1.41 christos == (sc->sc_if_flags & (~RESETIGN)))) {
2054 1.41 christos gem_setladrf(sc);
2055 1.41 christos break;
2056 1.41 christos }
2057 1.41 christos #undef RESETIGN
2058 1.41 christos /*FALLTHROUGH*/
2059 1.1 eeh default:
2060 1.1 eeh error = ether_ioctl(ifp, cmd, data);
2061 1.31 heas if (error == ENETRESET) {
2062 1.1 eeh /*
2063 1.1 eeh * Multicast list has changed; set the hardware filter
2064 1.1 eeh * accordingly.
2065 1.1 eeh */
2066 1.43 christos if (ifp->if_flags & IFF_RUNNING)
2067 1.43 christos gem_setladrf(sc);
2068 1.1 eeh error = 0;
2069 1.1 eeh }
2070 1.1 eeh break;
2071 1.1 eeh }
2072 1.1 eeh
2073 1.1 eeh /* Try to get things going again */
2074 1.43 christos if (ifp->if_flags & IFF_UP)
2075 1.1 eeh gem_start(ifp);
2076 1.1 eeh splx(s);
2077 1.1 eeh return (error);
2078 1.1 eeh }
2079 1.1 eeh
2080 1.1 eeh
2081 1.1 eeh void
2082 1.1 eeh gem_shutdown(arg)
2083 1.1 eeh void *arg;
2084 1.1 eeh {
2085 1.1 eeh struct gem_softc *sc = (struct gem_softc *)arg;
2086 1.1 eeh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2087 1.1 eeh
2088 1.1 eeh gem_stop(ifp, 1);
2089 1.1 eeh }
2090 1.1 eeh
2091 1.1 eeh /*
2092 1.1 eeh * Set up the logical address filter.
2093 1.1 eeh */
2094 1.1 eeh void
2095 1.1 eeh gem_setladrf(sc)
2096 1.1 eeh struct gem_softc *sc;
2097 1.1 eeh {
2098 1.15 matt struct ethercom *ec = &sc->sc_ethercom;
2099 1.15 matt struct ifnet *ifp = &ec->ec_if;
2100 1.1 eeh struct ether_multi *enm;
2101 1.1 eeh struct ether_multistep step;
2102 1.1 eeh bus_space_tag_t t = sc->sc_bustag;
2103 1.1 eeh bus_space_handle_t h = sc->sc_h;
2104 1.1 eeh u_int32_t crc;
2105 1.1 eeh u_int32_t hash[16];
2106 1.1 eeh u_int32_t v;
2107 1.15 matt int i;
2108 1.1 eeh
2109 1.1 eeh /* Get current RX configuration */
2110 1.1 eeh v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
2111 1.1 eeh
2112 1.15 matt /*
2113 1.15 matt * Turn off promiscuous mode, promiscuous group mode (all multicast),
2114 1.15 matt * and hash filter. Depending on the case, the right bit will be
2115 1.15 matt * enabled.
2116 1.15 matt */
2117 1.15 matt v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER|
2118 1.15 matt GEM_MAC_RX_PROMISC_GRP);
2119 1.15 matt
2120 1.1 eeh if ((ifp->if_flags & IFF_PROMISC) != 0) {
2121 1.15 matt /* Turn on promiscuous mode */
2122 1.1 eeh v |= GEM_MAC_RX_PROMISCUOUS;
2123 1.1 eeh ifp->if_flags |= IFF_ALLMULTI;
2124 1.1 eeh goto chipit;
2125 1.1 eeh }
2126 1.1 eeh
2127 1.1 eeh /*
2128 1.1 eeh * Set up multicast address filter by passing all multicast addresses
2129 1.15 matt * through a crc generator, and then using the high order 8 bits as an
2130 1.15 matt * index into the 256 bit logical address filter. The high order 4
2131 1.41 christos * bits selects the word, while the other 4 bits select the bit within
2132 1.15 matt * the word (where bit 0 is the MSB).
2133 1.1 eeh */
2134 1.1 eeh
2135 1.15 matt /* Clear hash table */
2136 1.15 matt memset(hash, 0, sizeof(hash));
2137 1.15 matt
2138 1.1 eeh ETHER_FIRST_MULTI(step, ec, enm);
2139 1.1 eeh while (enm != NULL) {
2140 1.6 thorpej if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2141 1.1 eeh /*
2142 1.1 eeh * We must listen to a range of multicast addresses.
2143 1.1 eeh * For now, just accept all multicasts, rather than
2144 1.1 eeh * trying to set only those filter bits needed to match
2145 1.1 eeh * the range. (At this time, the only use of address
2146 1.1 eeh * ranges is for IP multicast routing, for which the
2147 1.1 eeh * range is big enough to require all bits set.)
2148 1.15 matt * XXX use the addr filter for this
2149 1.1 eeh */
2150 1.1 eeh ifp->if_flags |= IFF_ALLMULTI;
2151 1.15 matt v |= GEM_MAC_RX_PROMISC_GRP;
2152 1.1 eeh goto chipit;
2153 1.1 eeh }
2154 1.1 eeh
2155 1.15 matt /* Get the LE CRC32 of the address */
2156 1.15 matt crc = ether_crc32_le(enm->enm_addrlo, sizeof(enm->enm_addrlo));
2157 1.1 eeh
2158 1.1 eeh /* Just want the 8 most significant bits. */
2159 1.1 eeh crc >>= 24;
2160 1.1 eeh
2161 1.1 eeh /* Set the corresponding bit in the filter. */
2162 1.15 matt hash[crc >> 4] |= 1 << (15 - (crc & 15));
2163 1.1 eeh
2164 1.1 eeh ETHER_NEXT_MULTI(step, enm);
2165 1.1 eeh }
2166 1.1 eeh
2167 1.15 matt v |= GEM_MAC_RX_HASH_FILTER;
2168 1.1 eeh ifp->if_flags &= ~IFF_ALLMULTI;
2169 1.1 eeh
2170 1.15 matt /* Now load the hash table into the chip (if we are using it) */
2171 1.15 matt for (i = 0; i < 16; i++) {
2172 1.15 matt bus_space_write_4(t, h,
2173 1.15 matt GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0),
2174 1.15 matt hash[i]);
2175 1.15 matt }
2176 1.15 matt
2177 1.1 eeh chipit:
2178 1.41 christos sc->sc_if_flags = ifp->if_flags;
2179 1.1 eeh bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
2180 1.1 eeh }
2181 1.1 eeh
2182 1.1 eeh #if notyet
2183 1.1 eeh
2184 1.1 eeh /*
2185 1.1 eeh * gem_power:
2186 1.1 eeh *
2187 1.1 eeh * Power management (suspend/resume) hook.
2188 1.1 eeh */
2189 1.1 eeh void
2190 1.1 eeh gem_power(why, arg)
2191 1.1 eeh int why;
2192 1.1 eeh void *arg;
2193 1.1 eeh {
2194 1.1 eeh struct gem_softc *sc = arg;
2195 1.1 eeh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2196 1.1 eeh int s;
2197 1.1 eeh
2198 1.1 eeh s = splnet();
2199 1.1 eeh switch (why) {
2200 1.1 eeh case PWR_SUSPEND:
2201 1.1 eeh case PWR_STANDBY:
2202 1.1 eeh gem_stop(ifp, 1);
2203 1.1 eeh if (sc->sc_power != NULL)
2204 1.1 eeh (*sc->sc_power)(sc, why);
2205 1.1 eeh break;
2206 1.1 eeh case PWR_RESUME:
2207 1.1 eeh if (ifp->if_flags & IFF_UP) {
2208 1.1 eeh if (sc->sc_power != NULL)
2209 1.1 eeh (*sc->sc_power)(sc, why);
2210 1.1 eeh gem_init(ifp);
2211 1.1 eeh }
2212 1.1 eeh break;
2213 1.1 eeh case PWR_SOFTSUSPEND:
2214 1.1 eeh case PWR_SOFTSTANDBY:
2215 1.1 eeh case PWR_SOFTRESUME:
2216 1.1 eeh break;
2217 1.1 eeh }
2218 1.1 eeh splx(s);
2219 1.1 eeh }
2220 1.1 eeh #endif
2221