if_gm.c revision 1.15.6.3 1 1.15.6.3 nathanw /* $NetBSD: if_gm.c,v 1.15.6.3 2002/04/01 07:40:53 nathanw Exp $ */
2 1.15.6.2 nathanw
3 1.15.6.2 nathanw /*-
4 1.15.6.2 nathanw * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 1.15.6.2 nathanw *
6 1.15.6.2 nathanw * Redistribution and use in source and binary forms, with or without
7 1.15.6.2 nathanw * modification, are permitted provided that the following conditions
8 1.15.6.2 nathanw * are met:
9 1.15.6.2 nathanw * 1. Redistributions of source code must retain the above copyright
10 1.15.6.2 nathanw * notice, this list of conditions and the following disclaimer.
11 1.15.6.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
12 1.15.6.2 nathanw * notice, this list of conditions and the following disclaimer in the
13 1.15.6.2 nathanw * documentation and/or other materials provided with the distribution.
14 1.15.6.2 nathanw * 3. The name of the author may not be used to endorse or promote products
15 1.15.6.2 nathanw * derived from this software without specific prior written permission.
16 1.15.6.2 nathanw *
17 1.15.6.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.15.6.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.15.6.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.15.6.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.15.6.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.15.6.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.15.6.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.15.6.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.15.6.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.15.6.2 nathanw * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.15.6.2 nathanw */
28 1.15.6.2 nathanw
29 1.15.6.2 nathanw #include "opt_inet.h"
30 1.15.6.2 nathanw #include "opt_ns.h"
31 1.15.6.2 nathanw #include "rnd.h"
32 1.15.6.2 nathanw #include "bpfilter.h"
33 1.15.6.2 nathanw
34 1.15.6.2 nathanw #include <sys/param.h>
35 1.15.6.2 nathanw #include <sys/device.h>
36 1.15.6.2 nathanw #include <sys/ioctl.h>
37 1.15.6.2 nathanw #include <sys/kernel.h>
38 1.15.6.2 nathanw #include <sys/mbuf.h>
39 1.15.6.2 nathanw #include <sys/socket.h>
40 1.15.6.2 nathanw #include <sys/systm.h>
41 1.15.6.2 nathanw #include <sys/callout.h>
42 1.15.6.2 nathanw
43 1.15.6.2 nathanw #if NRND > 0
44 1.15.6.2 nathanw #include <sys/rnd.h>
45 1.15.6.2 nathanw #endif
46 1.15.6.2 nathanw
47 1.15.6.2 nathanw #include <uvm/uvm_extern.h>
48 1.15.6.2 nathanw
49 1.15.6.2 nathanw #include <net/if.h>
50 1.15.6.2 nathanw #include <net/if_ether.h>
51 1.15.6.2 nathanw #include <net/if_media.h>
52 1.15.6.2 nathanw
53 1.15.6.2 nathanw #if NBPFILTER > 0
54 1.15.6.2 nathanw #include <net/bpf.h>
55 1.15.6.2 nathanw #endif
56 1.15.6.2 nathanw
57 1.15.6.2 nathanw #ifdef INET
58 1.15.6.2 nathanw #include <netinet/in.h>
59 1.15.6.2 nathanw #include <netinet/if_inarp.h>
60 1.15.6.2 nathanw #endif
61 1.15.6.2 nathanw
62 1.15.6.2 nathanw #include <dev/mii/mii.h>
63 1.15.6.2 nathanw #include <dev/mii/miivar.h>
64 1.15.6.2 nathanw
65 1.15.6.2 nathanw #include <dev/pci/pcivar.h>
66 1.15.6.2 nathanw #include <dev/pci/pcireg.h>
67 1.15.6.2 nathanw #include <dev/pci/pcidevs.h>
68 1.15.6.2 nathanw
69 1.15.6.2 nathanw #include <dev/ofw/openfirm.h>
70 1.15.6.2 nathanw #include <macppc/dev/if_gmreg.h>
71 1.15.6.2 nathanw #include <machine/pio.h>
72 1.15.6.2 nathanw
73 1.15.6.2 nathanw #define NTXBUF 4
74 1.15.6.2 nathanw #define NRXBUF 32
75 1.15.6.2 nathanw
76 1.15.6.2 nathanw struct gmac_softc {
77 1.15.6.2 nathanw struct device sc_dev;
78 1.15.6.2 nathanw struct ethercom sc_ethercom;
79 1.15.6.2 nathanw vaddr_t sc_reg;
80 1.15.6.2 nathanw struct gmac_dma *sc_txlist;
81 1.15.6.2 nathanw struct gmac_dma *sc_rxlist;
82 1.15.6.2 nathanw int sc_txnext;
83 1.15.6.2 nathanw int sc_rxlast;
84 1.15.6.2 nathanw caddr_t sc_txbuf[NTXBUF];
85 1.15.6.2 nathanw caddr_t sc_rxbuf[NRXBUF];
86 1.15.6.2 nathanw struct mii_data sc_mii;
87 1.15.6.2 nathanw struct callout sc_tick_ch;
88 1.15.6.2 nathanw char sc_laddr[6];
89 1.15.6.2 nathanw
90 1.15.6.2 nathanw #if NRND > 0
91 1.15.6.2 nathanw rndsource_element_t sc_rnd_source; /* random source */
92 1.15.6.2 nathanw #endif
93 1.15.6.2 nathanw };
94 1.15.6.2 nathanw
95 1.15.6.2 nathanw #define sc_if sc_ethercom.ec_if
96 1.15.6.2 nathanw
97 1.15.6.2 nathanw int gmac_match __P((struct device *, struct cfdata *, void *));
98 1.15.6.2 nathanw void gmac_attach __P((struct device *, struct device *, void *));
99 1.15.6.2 nathanw
100 1.15.6.2 nathanw static __inline u_int gmac_read_reg __P((struct gmac_softc *, int));
101 1.15.6.2 nathanw static __inline void gmac_write_reg __P((struct gmac_softc *, int, u_int));
102 1.15.6.2 nathanw
103 1.15.6.2 nathanw static __inline void gmac_start_txdma __P((struct gmac_softc *));
104 1.15.6.2 nathanw static __inline void gmac_start_rxdma __P((struct gmac_softc *));
105 1.15.6.2 nathanw static __inline void gmac_stop_txdma __P((struct gmac_softc *));
106 1.15.6.2 nathanw static __inline void gmac_stop_rxdma __P((struct gmac_softc *));
107 1.15.6.2 nathanw
108 1.15.6.2 nathanw int gmac_intr __P((void *));
109 1.15.6.2 nathanw void gmac_tint __P((struct gmac_softc *));
110 1.15.6.2 nathanw void gmac_rint __P((struct gmac_softc *));
111 1.15.6.2 nathanw struct mbuf * gmac_get __P((struct gmac_softc *, caddr_t, int));
112 1.15.6.2 nathanw void gmac_start __P((struct ifnet *));
113 1.15.6.2 nathanw int gmac_put __P((struct gmac_softc *, caddr_t, struct mbuf *));
114 1.15.6.2 nathanw
115 1.15.6.2 nathanw void gmac_stop __P((struct gmac_softc *));
116 1.15.6.2 nathanw void gmac_reset __P((struct gmac_softc *));
117 1.15.6.2 nathanw void gmac_init __P((struct gmac_softc *));
118 1.15.6.2 nathanw void gmac_init_mac __P((struct gmac_softc *));
119 1.15.6.2 nathanw void gmac_setladrf __P((struct gmac_softc *));
120 1.15.6.2 nathanw
121 1.15.6.2 nathanw int gmac_ioctl __P((struct ifnet *, u_long, caddr_t));
122 1.15.6.2 nathanw void gmac_watchdog __P((struct ifnet *));
123 1.15.6.2 nathanw
124 1.15.6.2 nathanw int gmac_mediachange __P((struct ifnet *));
125 1.15.6.2 nathanw void gmac_mediastatus __P((struct ifnet *, struct ifmediareq *));
126 1.15.6.2 nathanw int gmac_mii_readreg __P((struct device *, int, int));
127 1.15.6.2 nathanw void gmac_mii_writereg __P((struct device *, int, int, int));
128 1.15.6.2 nathanw void gmac_mii_statchg __P((struct device *));
129 1.15.6.2 nathanw void gmac_mii_tick __P((void *));
130 1.15.6.2 nathanw
131 1.15.6.2 nathanw struct cfattach gm_ca = {
132 1.15.6.2 nathanw sizeof(struct gmac_softc), gmac_match, gmac_attach
133 1.15.6.2 nathanw };
134 1.15.6.2 nathanw
135 1.15.6.2 nathanw int
136 1.15.6.2 nathanw gmac_match(parent, match, aux)
137 1.15.6.2 nathanw struct device *parent;
138 1.15.6.2 nathanw struct cfdata *match;
139 1.15.6.2 nathanw void *aux;
140 1.15.6.2 nathanw {
141 1.15.6.2 nathanw struct pci_attach_args *pa = aux;
142 1.15.6.2 nathanw
143 1.15.6.2 nathanw if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
144 1.15.6.2 nathanw (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
145 1.15.6.2 nathanw PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2))
146 1.15.6.2 nathanw return 1;
147 1.15.6.2 nathanw
148 1.15.6.2 nathanw return 0;
149 1.15.6.2 nathanw }
150 1.15.6.2 nathanw
151 1.15.6.2 nathanw void
152 1.15.6.2 nathanw gmac_attach(parent, self, aux)
153 1.15.6.2 nathanw struct device *parent, *self;
154 1.15.6.2 nathanw void *aux;
155 1.15.6.2 nathanw {
156 1.15.6.2 nathanw struct gmac_softc *sc = (void *)self;
157 1.15.6.2 nathanw struct pci_attach_args *pa = aux;
158 1.15.6.2 nathanw struct ifnet *ifp = &sc->sc_if;
159 1.15.6.2 nathanw struct mii_data *mii = &sc->sc_mii;
160 1.15.6.2 nathanw pci_intr_handle_t ih;
161 1.15.6.2 nathanw const char *intrstr = NULL;
162 1.15.6.2 nathanw int node, i;
163 1.15.6.2 nathanw char *p;
164 1.15.6.2 nathanw struct gmac_dma *dp;
165 1.15.6.2 nathanw u_int32_t reg[10];
166 1.15.6.2 nathanw u_char laddr[6];
167 1.15.6.2 nathanw
168 1.15.6.2 nathanw node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
169 1.15.6.2 nathanw if (node == 0) {
170 1.15.6.2 nathanw printf(": cannot find gmac node\n");
171 1.15.6.2 nathanw return;
172 1.15.6.2 nathanw }
173 1.15.6.2 nathanw
174 1.15.6.2 nathanw OF_getprop(node, "local-mac-address", laddr, sizeof laddr);
175 1.15.6.2 nathanw OF_getprop(node, "assigned-addresses", reg, sizeof reg);
176 1.15.6.2 nathanw
177 1.15.6.2 nathanw memcpy(sc->sc_laddr, laddr, sizeof laddr);
178 1.15.6.2 nathanw sc->sc_reg = reg[2];
179 1.15.6.2 nathanw
180 1.15.6.2 nathanw if (pci_intr_map(pa, &ih)) {
181 1.15.6.2 nathanw printf(": unable to map interrupt\n");
182 1.15.6.2 nathanw return;
183 1.15.6.2 nathanw }
184 1.15.6.2 nathanw intrstr = pci_intr_string(pa->pa_pc, ih);
185 1.15.6.2 nathanw
186 1.15.6.2 nathanw if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, gmac_intr, sc) == NULL) {
187 1.15.6.2 nathanw printf(": unable to establish interrupt");
188 1.15.6.2 nathanw if (intrstr)
189 1.15.6.2 nathanw printf(" at %s", intrstr);
190 1.15.6.2 nathanw printf("\n");
191 1.15.6.2 nathanw return;
192 1.15.6.2 nathanw }
193 1.15.6.2 nathanw
194 1.15.6.2 nathanw /* Setup packet buffers and dma descriptors. */
195 1.15.6.2 nathanw p = malloc((NRXBUF + NTXBUF) * 2048 + 3 * 0x800, M_DEVBUF, M_NOWAIT);
196 1.15.6.2 nathanw if (p == NULL) {
197 1.15.6.2 nathanw printf(": cannot malloc buffers\n");
198 1.15.6.2 nathanw return;
199 1.15.6.2 nathanw }
200 1.15.6.2 nathanw p = (void *)roundup((vaddr_t)p, 0x800);
201 1.15.6.2 nathanw memset(p, 0, 2048 * (NRXBUF + NTXBUF) + 2 * 0x800);
202 1.15.6.2 nathanw
203 1.15.6.2 nathanw sc->sc_rxlist = (void *)p;
204 1.15.6.2 nathanw p += 0x800;
205 1.15.6.2 nathanw sc->sc_txlist = (void *)p;
206 1.15.6.2 nathanw p += 0x800;
207 1.15.6.2 nathanw
208 1.15.6.2 nathanw dp = sc->sc_rxlist;
209 1.15.6.2 nathanw for (i = 0; i < NRXBUF; i++) {
210 1.15.6.2 nathanw sc->sc_rxbuf[i] = p;
211 1.15.6.2 nathanw dp->address = htole32(vtophys((vaddr_t)p));
212 1.15.6.2 nathanw dp->cmd = htole32(GMAC_OWN);
213 1.15.6.2 nathanw dp++;
214 1.15.6.2 nathanw p += 2048;
215 1.15.6.2 nathanw }
216 1.15.6.2 nathanw
217 1.15.6.2 nathanw dp = sc->sc_txlist;
218 1.15.6.2 nathanw for (i = 0; i < NTXBUF; i++) {
219 1.15.6.2 nathanw sc->sc_txbuf[i] = p;
220 1.15.6.2 nathanw dp->address = htole32(vtophys((vaddr_t)p));
221 1.15.6.2 nathanw dp++;
222 1.15.6.2 nathanw p += 2048;
223 1.15.6.2 nathanw }
224 1.15.6.2 nathanw
225 1.15.6.2 nathanw printf(": Ethernet address %s\n", ether_sprintf(laddr));
226 1.15.6.2 nathanw printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
227 1.15.6.2 nathanw
228 1.15.6.2 nathanw callout_init(&sc->sc_tick_ch);
229 1.15.6.2 nathanw
230 1.15.6.2 nathanw gmac_reset(sc);
231 1.15.6.2 nathanw gmac_init_mac(sc);
232 1.15.6.2 nathanw
233 1.15.6.2 nathanw memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
234 1.15.6.2 nathanw ifp->if_softc = sc;
235 1.15.6.2 nathanw ifp->if_ioctl = gmac_ioctl;
236 1.15.6.2 nathanw ifp->if_start = gmac_start;
237 1.15.6.2 nathanw ifp->if_watchdog = gmac_watchdog;
238 1.15.6.2 nathanw ifp->if_flags =
239 1.15.6.2 nathanw IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
240 1.15.6.3 nathanw IFQ_SET_READY(&ifp->if_snd);
241 1.15.6.2 nathanw
242 1.15.6.2 nathanw mii->mii_ifp = ifp;
243 1.15.6.2 nathanw mii->mii_readreg = gmac_mii_readreg;
244 1.15.6.2 nathanw mii->mii_writereg = gmac_mii_writereg;
245 1.15.6.2 nathanw mii->mii_statchg = gmac_mii_statchg;
246 1.15.6.2 nathanw
247 1.15.6.2 nathanw ifmedia_init(&mii->mii_media, 0, gmac_mediachange, gmac_mediastatus);
248 1.15.6.2 nathanw mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
249 1.15.6.2 nathanw
250 1.15.6.2 nathanw /* Choose a default media. */
251 1.15.6.2 nathanw if (LIST_FIRST(&mii->mii_phys) == NULL) {
252 1.15.6.2 nathanw ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
253 1.15.6.2 nathanw ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
254 1.15.6.2 nathanw } else
255 1.15.6.2 nathanw ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
256 1.15.6.2 nathanw
257 1.15.6.2 nathanw if_attach(ifp);
258 1.15.6.2 nathanw ether_ifattach(ifp, laddr);
259 1.15.6.2 nathanw #if NRND > 0
260 1.15.6.2 nathanw rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
261 1.15.6.2 nathanw RND_TYPE_NET, 0);
262 1.15.6.2 nathanw #endif
263 1.15.6.2 nathanw }
264 1.15.6.2 nathanw
265 1.15.6.2 nathanw u_int
266 1.15.6.2 nathanw gmac_read_reg(sc, reg)
267 1.15.6.2 nathanw struct gmac_softc *sc;
268 1.15.6.2 nathanw int reg;
269 1.15.6.2 nathanw {
270 1.15.6.2 nathanw return in32rb(sc->sc_reg + reg);
271 1.15.6.2 nathanw }
272 1.15.6.2 nathanw
273 1.15.6.2 nathanw void
274 1.15.6.2 nathanw gmac_write_reg(sc, reg, val)
275 1.15.6.2 nathanw struct gmac_softc *sc;
276 1.15.6.2 nathanw int reg;
277 1.15.6.2 nathanw u_int val;
278 1.15.6.2 nathanw {
279 1.15.6.2 nathanw out32rb(sc->sc_reg + reg, val);
280 1.15.6.2 nathanw }
281 1.15.6.2 nathanw
282 1.15.6.2 nathanw void
283 1.15.6.2 nathanw gmac_start_txdma(sc)
284 1.15.6.2 nathanw struct gmac_softc *sc;
285 1.15.6.2 nathanw {
286 1.15.6.2 nathanw u_int x;
287 1.15.6.2 nathanw
288 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
289 1.15.6.2 nathanw x |= 1;
290 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
291 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
292 1.15.6.2 nathanw x |= 1;
293 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
294 1.15.6.2 nathanw }
295 1.15.6.2 nathanw
296 1.15.6.2 nathanw void
297 1.15.6.2 nathanw gmac_start_rxdma(sc)
298 1.15.6.2 nathanw struct gmac_softc *sc;
299 1.15.6.2 nathanw {
300 1.15.6.2 nathanw u_int x;
301 1.15.6.2 nathanw
302 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
303 1.15.6.2 nathanw x |= 1;
304 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
305 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
306 1.15.6.2 nathanw x |= 1;
307 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
308 1.15.6.2 nathanw }
309 1.15.6.2 nathanw
310 1.15.6.2 nathanw void
311 1.15.6.2 nathanw gmac_stop_txdma(sc)
312 1.15.6.2 nathanw struct gmac_softc *sc;
313 1.15.6.2 nathanw {
314 1.15.6.2 nathanw u_int x;
315 1.15.6.2 nathanw
316 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
317 1.15.6.2 nathanw x &= ~1;
318 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
319 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
320 1.15.6.2 nathanw x &= ~1;
321 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
322 1.15.6.2 nathanw }
323 1.15.6.2 nathanw
324 1.15.6.2 nathanw void
325 1.15.6.2 nathanw gmac_stop_rxdma(sc)
326 1.15.6.2 nathanw struct gmac_softc *sc;
327 1.15.6.2 nathanw {
328 1.15.6.2 nathanw u_int x;
329 1.15.6.2 nathanw
330 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
331 1.15.6.2 nathanw x &= ~1;
332 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
333 1.15.6.2 nathanw x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
334 1.15.6.2 nathanw x &= ~1;
335 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
336 1.15.6.2 nathanw }
337 1.15.6.2 nathanw
338 1.15.6.2 nathanw int
339 1.15.6.2 nathanw gmac_intr(v)
340 1.15.6.2 nathanw void *v;
341 1.15.6.2 nathanw {
342 1.15.6.2 nathanw struct gmac_softc *sc = v;
343 1.15.6.2 nathanw u_int status;
344 1.15.6.2 nathanw
345 1.15.6.2 nathanw status = gmac_read_reg(sc, GMAC_STATUS) & 0xff;
346 1.15.6.2 nathanw if (status == 0)
347 1.15.6.2 nathanw return 0;
348 1.15.6.2 nathanw
349 1.15.6.2 nathanw if (status & GMAC_INT_RXDONE)
350 1.15.6.2 nathanw gmac_rint(sc);
351 1.15.6.2 nathanw
352 1.15.6.2 nathanw if (status & GMAC_INT_TXEMPTY)
353 1.15.6.2 nathanw gmac_tint(sc);
354 1.15.6.2 nathanw
355 1.15.6.2 nathanw #if NRND > 0
356 1.15.6.2 nathanw rnd_add_uint32(&sc->sc_rnd_source, status);
357 1.15.6.2 nathanw #endif
358 1.15.6.2 nathanw return 1;
359 1.15.6.2 nathanw }
360 1.15.6.2 nathanw
361 1.15.6.2 nathanw void
362 1.15.6.2 nathanw gmac_tint(sc)
363 1.15.6.2 nathanw struct gmac_softc *sc;
364 1.15.6.2 nathanw {
365 1.15.6.2 nathanw struct ifnet *ifp = &sc->sc_if;
366 1.15.6.2 nathanw
367 1.15.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
368 1.15.6.2 nathanw ifp->if_timer = 0;
369 1.15.6.2 nathanw gmac_start(ifp);
370 1.15.6.2 nathanw }
371 1.15.6.2 nathanw
372 1.15.6.2 nathanw void
373 1.15.6.2 nathanw gmac_rint(sc)
374 1.15.6.2 nathanw struct gmac_softc *sc;
375 1.15.6.2 nathanw {
376 1.15.6.2 nathanw struct ifnet *ifp = &sc->sc_if;
377 1.15.6.2 nathanw volatile struct gmac_dma *dp;
378 1.15.6.2 nathanw struct mbuf *m;
379 1.15.6.2 nathanw int i, j, len;
380 1.15.6.2 nathanw u_int cmd;
381 1.15.6.2 nathanw
382 1.15.6.2 nathanw for (i = sc->sc_rxlast;; i++) {
383 1.15.6.2 nathanw if (i == NRXBUF)
384 1.15.6.2 nathanw i = 0;
385 1.15.6.2 nathanw
386 1.15.6.2 nathanw dp = &sc->sc_rxlist[i];
387 1.15.6.2 nathanw cmd = le32toh(dp->cmd);
388 1.15.6.2 nathanw if (cmd & GMAC_OWN)
389 1.15.6.2 nathanw break;
390 1.15.6.2 nathanw len = (cmd >> 16) & GMAC_LEN_MASK;
391 1.15.6.2 nathanw len -= 4; /* CRC */
392 1.15.6.2 nathanw
393 1.15.6.2 nathanw if (le32toh(dp->cmd_hi) & 0x40000000) {
394 1.15.6.2 nathanw ifp->if_ierrors++;
395 1.15.6.2 nathanw goto next;
396 1.15.6.2 nathanw }
397 1.15.6.2 nathanw
398 1.15.6.2 nathanw m = gmac_get(sc, sc->sc_rxbuf[i], len);
399 1.15.6.2 nathanw if (m == NULL) {
400 1.15.6.2 nathanw ifp->if_ierrors++;
401 1.15.6.2 nathanw goto next;
402 1.15.6.2 nathanw }
403 1.15.6.2 nathanw
404 1.15.6.2 nathanw #if NBPFILTER > 0
405 1.15.6.2 nathanw /*
406 1.15.6.2 nathanw * Check if there's a BPF listener on this interface.
407 1.15.6.2 nathanw * If so, hand off the raw packet to BPF.
408 1.15.6.2 nathanw */
409 1.15.6.2 nathanw if (ifp->if_bpf)
410 1.15.6.2 nathanw bpf_mtap(ifp->if_bpf, m);
411 1.15.6.2 nathanw #endif
412 1.15.6.2 nathanw (*ifp->if_input)(ifp, m);
413 1.15.6.2 nathanw ifp->if_ipackets++;
414 1.15.6.2 nathanw
415 1.15.6.2 nathanw next:
416 1.15.6.2 nathanw dp->cmd_hi = 0;
417 1.15.6.2 nathanw __asm __volatile ("sync");
418 1.15.6.2 nathanw dp->cmd = htole32(GMAC_OWN);
419 1.15.6.2 nathanw }
420 1.15.6.2 nathanw sc->sc_rxlast = i;
421 1.15.6.2 nathanw
422 1.15.6.2 nathanw /* XXX Make sure free buffers have GMAC_OWN. */
423 1.15.6.2 nathanw i++;
424 1.15.6.2 nathanw for (j = 1; j < NRXBUF; j++) {
425 1.15.6.2 nathanw if (i == NRXBUF)
426 1.15.6.2 nathanw i = 0;
427 1.15.6.2 nathanw dp = &sc->sc_rxlist[i++];
428 1.15.6.2 nathanw dp->cmd = htole32(GMAC_OWN);
429 1.15.6.2 nathanw }
430 1.15.6.2 nathanw }
431 1.15.6.2 nathanw
432 1.15.6.2 nathanw struct mbuf *
433 1.15.6.2 nathanw gmac_get(sc, pkt, totlen)
434 1.15.6.2 nathanw struct gmac_softc *sc;
435 1.15.6.2 nathanw caddr_t pkt;
436 1.15.6.2 nathanw int totlen;
437 1.15.6.2 nathanw {
438 1.15.6.2 nathanw struct mbuf *m;
439 1.15.6.2 nathanw struct mbuf *top, **mp;
440 1.15.6.2 nathanw int len;
441 1.15.6.2 nathanw
442 1.15.6.2 nathanw MGETHDR(m, M_DONTWAIT, MT_DATA);
443 1.15.6.2 nathanw if (m == 0)
444 1.15.6.2 nathanw return 0;
445 1.15.6.2 nathanw m->m_pkthdr.rcvif = &sc->sc_if;
446 1.15.6.2 nathanw m->m_pkthdr.len = totlen;
447 1.15.6.2 nathanw len = MHLEN;
448 1.15.6.2 nathanw top = 0;
449 1.15.6.2 nathanw mp = ⊤
450 1.15.6.2 nathanw
451 1.15.6.2 nathanw while (totlen > 0) {
452 1.15.6.2 nathanw if (top) {
453 1.15.6.2 nathanw MGET(m, M_DONTWAIT, MT_DATA);
454 1.15.6.2 nathanw if (m == 0) {
455 1.15.6.2 nathanw m_freem(top);
456 1.15.6.2 nathanw return 0;
457 1.15.6.2 nathanw }
458 1.15.6.2 nathanw len = MLEN;
459 1.15.6.2 nathanw }
460 1.15.6.2 nathanw if (totlen >= MINCLSIZE) {
461 1.15.6.2 nathanw MCLGET(m, M_DONTWAIT);
462 1.15.6.2 nathanw if ((m->m_flags & M_EXT) == 0) {
463 1.15.6.2 nathanw m_free(m);
464 1.15.6.2 nathanw m_freem(top);
465 1.15.6.2 nathanw return 0;
466 1.15.6.2 nathanw }
467 1.15.6.2 nathanw len = MCLBYTES;
468 1.15.6.2 nathanw }
469 1.15.6.2 nathanw m->m_len = len = min(totlen, len);
470 1.15.6.2 nathanw memcpy(mtod(m, caddr_t), pkt, len);
471 1.15.6.2 nathanw pkt += len;
472 1.15.6.2 nathanw totlen -= len;
473 1.15.6.2 nathanw *mp = m;
474 1.15.6.2 nathanw mp = &m->m_next;
475 1.15.6.2 nathanw }
476 1.15.6.2 nathanw
477 1.15.6.2 nathanw return top;
478 1.15.6.2 nathanw }
479 1.15.6.2 nathanw
480 1.15.6.2 nathanw void
481 1.15.6.2 nathanw gmac_start(ifp)
482 1.15.6.2 nathanw struct ifnet *ifp;
483 1.15.6.2 nathanw {
484 1.15.6.2 nathanw struct gmac_softc *sc = ifp->if_softc;
485 1.15.6.2 nathanw struct mbuf *m;
486 1.15.6.2 nathanw caddr_t buff;
487 1.15.6.2 nathanw int i, tlen;
488 1.15.6.2 nathanw volatile struct gmac_dma *dp;
489 1.15.6.2 nathanw
490 1.15.6.2 nathanw if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
491 1.15.6.2 nathanw return;
492 1.15.6.2 nathanw
493 1.15.6.2 nathanw for (;;) {
494 1.15.6.2 nathanw if (ifp->if_flags & IFF_OACTIVE)
495 1.15.6.2 nathanw break;
496 1.15.6.2 nathanw
497 1.15.6.3 nathanw IFQ_DEQUEUE(&ifp->if_snd, m);
498 1.15.6.2 nathanw if (m == 0)
499 1.15.6.2 nathanw break;
500 1.15.6.2 nathanw
501 1.15.6.2 nathanw /* 5 seconds to watch for failing to transmit */
502 1.15.6.2 nathanw ifp->if_timer = 5;
503 1.15.6.2 nathanw ifp->if_opackets++; /* # of pkts */
504 1.15.6.2 nathanw
505 1.15.6.2 nathanw i = sc->sc_txnext;
506 1.15.6.2 nathanw buff = sc->sc_txbuf[i];
507 1.15.6.2 nathanw tlen = gmac_put(sc, buff, m);
508 1.15.6.2 nathanw
509 1.15.6.2 nathanw dp = &sc->sc_txlist[i];
510 1.15.6.2 nathanw dp->cmd_hi = 0;
511 1.15.6.2 nathanw dp->address_hi = 0;
512 1.15.6.2 nathanw dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP);
513 1.15.6.2 nathanw
514 1.15.6.2 nathanw i++;
515 1.15.6.2 nathanw if (i == NTXBUF)
516 1.15.6.2 nathanw i = 0;
517 1.15.6.2 nathanw __asm __volatile ("sync");
518 1.15.6.2 nathanw
519 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXDMAKICK, i);
520 1.15.6.2 nathanw sc->sc_txnext = i;
521 1.15.6.2 nathanw
522 1.15.6.2 nathanw #if NBPFILTER > 0
523 1.15.6.2 nathanw /*
524 1.15.6.2 nathanw * If BPF is listening on this interface, let it see the
525 1.15.6.2 nathanw * packet before we commit it to the wire.
526 1.15.6.2 nathanw */
527 1.15.6.2 nathanw if (ifp->if_bpf)
528 1.15.6.2 nathanw bpf_mtap(ifp->if_bpf, m);
529 1.15.6.2 nathanw #endif
530 1.15.6.2 nathanw m_freem(m);
531 1.15.6.2 nathanw
532 1.15.6.2 nathanw i++;
533 1.15.6.2 nathanw if (i == NTXBUF)
534 1.15.6.2 nathanw i = 0;
535 1.15.6.2 nathanw if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) {
536 1.15.6.2 nathanw ifp->if_flags |= IFF_OACTIVE;
537 1.15.6.2 nathanw break;
538 1.15.6.2 nathanw }
539 1.15.6.2 nathanw }
540 1.15.6.2 nathanw }
541 1.15.6.2 nathanw
542 1.15.6.2 nathanw int
543 1.15.6.2 nathanw gmac_put(sc, buff, m)
544 1.15.6.2 nathanw struct gmac_softc *sc;
545 1.15.6.2 nathanw caddr_t buff;
546 1.15.6.2 nathanw struct mbuf *m;
547 1.15.6.2 nathanw {
548 1.15.6.2 nathanw int len, tlen = 0;
549 1.15.6.2 nathanw
550 1.15.6.2 nathanw for (; m; m = m->m_next) {
551 1.15.6.2 nathanw len = m->m_len;
552 1.15.6.2 nathanw if (len == 0)
553 1.15.6.2 nathanw continue;
554 1.15.6.2 nathanw memcpy(buff, mtod(m, caddr_t), len);
555 1.15.6.2 nathanw buff += len;
556 1.15.6.2 nathanw tlen += len;
557 1.15.6.2 nathanw }
558 1.15.6.2 nathanw if (tlen > 2048)
559 1.15.6.2 nathanw panic("%s: gmac_put packet overflow", sc->sc_dev.dv_xname);
560 1.15.6.2 nathanw
561 1.15.6.2 nathanw return tlen;
562 1.15.6.2 nathanw }
563 1.15.6.2 nathanw
564 1.15.6.2 nathanw void
565 1.15.6.2 nathanw gmac_reset(sc)
566 1.15.6.2 nathanw struct gmac_softc *sc;
567 1.15.6.2 nathanw {
568 1.15.6.2 nathanw int i, s;
569 1.15.6.2 nathanw
570 1.15.6.2 nathanw s = splnet();
571 1.15.6.2 nathanw
572 1.15.6.2 nathanw gmac_stop_txdma(sc);
573 1.15.6.2 nathanw gmac_stop_rxdma(sc);
574 1.15.6.2 nathanw
575 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_SOFTWARERESET, 3);
576 1.15.6.2 nathanw for (i = 10; i > 0; i--) {
577 1.15.6.2 nathanw delay(300000); /* XXX long delay */
578 1.15.6.2 nathanw if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0)
579 1.15.6.2 nathanw break;
580 1.15.6.2 nathanw }
581 1.15.6.2 nathanw if (i == 0)
582 1.15.6.2 nathanw printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
583 1.15.6.2 nathanw
584 1.15.6.2 nathanw sc->sc_txnext = 0;
585 1.15.6.2 nathanw sc->sc_rxlast = 0;
586 1.15.6.2 nathanw for (i = 0; i < NRXBUF; i++)
587 1.15.6.2 nathanw sc->sc_rxlist[i].cmd = htole32(GMAC_OWN);
588 1.15.6.2 nathanw __asm __volatile ("sync");
589 1.15.6.2 nathanw
590 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0);
591 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXDMADESCBASELO,
592 1.15.6.2 nathanw vtophys((vaddr_t)sc->sc_txlist));
593 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0);
594 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXDMADESCBASELO,
595 1.15.6.2 nathanw vtophys((vaddr_t)sc->sc_rxlist));
596 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF);
597 1.15.6.2 nathanw
598 1.15.6.2 nathanw splx(s);
599 1.15.6.2 nathanw }
600 1.15.6.2 nathanw
601 1.15.6.2 nathanw void
602 1.15.6.2 nathanw gmac_stop(sc)
603 1.15.6.2 nathanw struct gmac_softc *sc;
604 1.15.6.2 nathanw {
605 1.15.6.2 nathanw struct ifnet *ifp = &sc->sc_if;
606 1.15.6.2 nathanw int s;
607 1.15.6.2 nathanw
608 1.15.6.2 nathanw s = splnet();
609 1.15.6.2 nathanw
610 1.15.6.2 nathanw callout_stop(&sc->sc_tick_ch);
611 1.15.6.2 nathanw mii_down(&sc->sc_mii);
612 1.15.6.2 nathanw
613 1.15.6.2 nathanw gmac_stop_txdma(sc);
614 1.15.6.2 nathanw gmac_stop_rxdma(sc);
615 1.15.6.2 nathanw
616 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff);
617 1.15.6.2 nathanw
618 1.15.6.2 nathanw ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
619 1.15.6.2 nathanw ifp->if_timer = 0;
620 1.15.6.2 nathanw
621 1.15.6.2 nathanw splx(s);
622 1.15.6.2 nathanw }
623 1.15.6.2 nathanw
624 1.15.6.2 nathanw void
625 1.15.6.2 nathanw gmac_init_mac(sc)
626 1.15.6.2 nathanw struct gmac_softc *sc;
627 1.15.6.2 nathanw {
628 1.15.6.2 nathanw int i, tb;
629 1.15.6.2 nathanw char *laddr = sc->sc_laddr;
630 1.15.6.2 nathanw
631 1.15.6.2 nathanw __asm ("mftb %0" : "=r"(tb));
632 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RANDOMSEED, tb);
633 1.15.6.2 nathanw
634 1.15.6.2 nathanw /* init-mii */
635 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_DATAPATHMODE, 4);
636 1.15.6.2 nathanw gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000);
637 1.15.6.2 nathanw
638 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00);
639 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXDMACONFIG, 0);
640 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0);
641 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0);
642 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8);
643 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4);
644 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN);
645 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN);
646 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_PASIZE, 7);
647 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_JAMSIZE, 4);
648 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10);
649 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808);
650 1.15.6.2 nathanw
651 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]);
652 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]);
653 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]);
654 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS3, 0);
655 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS4, 0);
656 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS5, 0);
657 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS6, 1);
658 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200);
659 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180);
660 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRFILT0, 0);
661 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRFILT1, 0);
662 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRFILT2, 0);
663 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0);
664 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0);
665 1.15.6.2 nathanw
666 1.15.6.2 nathanw for (i = 0; i < 0x6c; i += 4)
667 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0);
668 1.15.6.2 nathanw
669 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_SLOTTIME, 0x40);
670 1.15.6.2 nathanw
671 1.15.6.2 nathanw if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
672 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
673 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
674 1.15.6.2 nathanw } else {
675 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
676 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
677 1.15.6.2 nathanw }
678 1.15.6.2 nathanw
679 1.15.6.2 nathanw if (0) /* g-bit? */
680 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
681 1.15.6.2 nathanw else
682 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
683 1.15.6.2 nathanw }
684 1.15.6.2 nathanw
685 1.15.6.2 nathanw void
686 1.15.6.2 nathanw gmac_setladrf(sc)
687 1.15.6.2 nathanw struct gmac_softc *sc;
688 1.15.6.2 nathanw {
689 1.15.6.2 nathanw struct ifnet *ifp = &sc->sc_if;
690 1.15.6.2 nathanw struct ether_multi *enm;
691 1.15.6.2 nathanw struct ether_multistep step;
692 1.15.6.2 nathanw struct ethercom *ec = &sc->sc_ethercom;
693 1.15.6.2 nathanw u_int32_t crc;
694 1.15.6.2 nathanw u_int32_t hash[16];
695 1.15.6.2 nathanw u_int v;
696 1.15.6.2 nathanw int i;
697 1.15.6.2 nathanw
698 1.15.6.2 nathanw /* Clear hash table */
699 1.15.6.2 nathanw for (i = 0; i < 16; i++)
700 1.15.6.2 nathanw hash[i] = 0;
701 1.15.6.2 nathanw
702 1.15.6.2 nathanw /* Get current RX configuration */
703 1.15.6.2 nathanw v = gmac_read_reg(sc, GMAC_RXMACCONFIG);
704 1.15.6.2 nathanw
705 1.15.6.2 nathanw if ((ifp->if_flags & IFF_PROMISC) != 0) {
706 1.15.6.2 nathanw /* Turn on promiscuous mode; turn off the hash filter */
707 1.15.6.2 nathanw v |= GMAC_RXMAC_PR;
708 1.15.6.2 nathanw v &= ~GMAC_RXMAC_HEN;
709 1.15.6.2 nathanw ifp->if_flags |= IFF_ALLMULTI;
710 1.15.6.2 nathanw goto chipit;
711 1.15.6.2 nathanw }
712 1.15.6.2 nathanw
713 1.15.6.2 nathanw /* Turn off promiscuous mode; turn on the hash filter */
714 1.15.6.2 nathanw v &= ~GMAC_RXMAC_PR;
715 1.15.6.2 nathanw v |= GMAC_RXMAC_HEN;
716 1.15.6.2 nathanw
717 1.15.6.2 nathanw /*
718 1.15.6.2 nathanw * Set up multicast address filter by passing all multicast addresses
719 1.15.6.2 nathanw * through a crc generator, and then using the high order 8 bits as an
720 1.15.6.2 nathanw * index into the 256 bit logical address filter. The high order bit
721 1.15.6.2 nathanw * selects the word, while the rest of the bits select the bit within
722 1.15.6.2 nathanw * the word.
723 1.15.6.2 nathanw */
724 1.15.6.2 nathanw
725 1.15.6.2 nathanw ETHER_FIRST_MULTI(step, ec, enm);
726 1.15.6.2 nathanw while (enm != NULL) {
727 1.15.6.2 nathanw if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) {
728 1.15.6.2 nathanw /*
729 1.15.6.2 nathanw * We must listen to a range of multicast addresses.
730 1.15.6.2 nathanw * For now, just accept all multicasts, rather than
731 1.15.6.2 nathanw * trying to set only those filter bits needed to match
732 1.15.6.2 nathanw * the range. (At this time, the only use of address
733 1.15.6.2 nathanw * ranges is for IP multicast routing, for which the
734 1.15.6.2 nathanw * range is big enough to require all bits set.)
735 1.15.6.2 nathanw */
736 1.15.6.2 nathanw for (i = 0; i < 16; i++)
737 1.15.6.2 nathanw hash[i] = 0xffff;
738 1.15.6.2 nathanw ifp->if_flags |= IFF_ALLMULTI;
739 1.15.6.2 nathanw goto chipit;
740 1.15.6.2 nathanw }
741 1.15.6.2 nathanw
742 1.15.6.2 nathanw crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
743 1.15.6.2 nathanw
744 1.15.6.2 nathanw /* Just want the 8 most significant bits. */
745 1.15.6.2 nathanw crc >>= 24;
746 1.15.6.2 nathanw
747 1.15.6.2 nathanw /* Set the corresponding bit in the filter. */
748 1.15.6.2 nathanw hash[crc >> 4] |= 1 << (crc & 0xf);
749 1.15.6.2 nathanw
750 1.15.6.2 nathanw ETHER_NEXT_MULTI(step, enm);
751 1.15.6.2 nathanw }
752 1.15.6.2 nathanw
753 1.15.6.2 nathanw ifp->if_flags &= ~IFF_ALLMULTI;
754 1.15.6.2 nathanw
755 1.15.6.2 nathanw chipit:
756 1.15.6.2 nathanw /* Now load the hash table into the chip */
757 1.15.6.2 nathanw for (i = 0; i < 16; i++)
758 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_HASHTABLE0 + i * 4, hash[i]);
759 1.15.6.2 nathanw
760 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_RXMACCONFIG, v);
761 1.15.6.2 nathanw }
762 1.15.6.2 nathanw
763 1.15.6.2 nathanw void
764 1.15.6.2 nathanw gmac_init(sc)
765 1.15.6.2 nathanw struct gmac_softc *sc;
766 1.15.6.2 nathanw {
767 1.15.6.2 nathanw struct ifnet *ifp = &sc->sc_if;
768 1.15.6.2 nathanw
769 1.15.6.2 nathanw gmac_stop_txdma(sc);
770 1.15.6.2 nathanw gmac_stop_rxdma(sc);
771 1.15.6.2 nathanw
772 1.15.6.2 nathanw gmac_init_mac(sc);
773 1.15.6.2 nathanw gmac_setladrf(sc);
774 1.15.6.2 nathanw
775 1.15.6.2 nathanw gmac_start_txdma(sc);
776 1.15.6.2 nathanw gmac_start_rxdma(sc);
777 1.15.6.2 nathanw
778 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE));
779 1.15.6.2 nathanw
780 1.15.6.2 nathanw ifp->if_flags |= IFF_RUNNING;
781 1.15.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
782 1.15.6.2 nathanw ifp->if_timer = 0;
783 1.15.6.2 nathanw
784 1.15.6.2 nathanw callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc);
785 1.15.6.2 nathanw
786 1.15.6.2 nathanw gmac_start(ifp);
787 1.15.6.2 nathanw }
788 1.15.6.2 nathanw
789 1.15.6.2 nathanw int
790 1.15.6.2 nathanw gmac_ioctl(ifp, cmd, data)
791 1.15.6.2 nathanw struct ifnet *ifp;
792 1.15.6.2 nathanw u_long cmd;
793 1.15.6.2 nathanw caddr_t data;
794 1.15.6.2 nathanw {
795 1.15.6.2 nathanw struct gmac_softc *sc = ifp->if_softc;
796 1.15.6.2 nathanw struct ifaddr *ifa = (struct ifaddr *)data;
797 1.15.6.2 nathanw struct ifreq *ifr = (struct ifreq *)data;
798 1.15.6.2 nathanw int s, error = 0;
799 1.15.6.2 nathanw
800 1.15.6.2 nathanw s = splnet();
801 1.15.6.2 nathanw
802 1.15.6.2 nathanw switch (cmd) {
803 1.15.6.2 nathanw
804 1.15.6.2 nathanw case SIOCSIFADDR:
805 1.15.6.2 nathanw ifp->if_flags |= IFF_UP;
806 1.15.6.2 nathanw
807 1.15.6.2 nathanw switch (ifa->ifa_addr->sa_family) {
808 1.15.6.2 nathanw #ifdef INET
809 1.15.6.2 nathanw case AF_INET:
810 1.15.6.2 nathanw gmac_init(sc);
811 1.15.6.2 nathanw arp_ifinit(ifp, ifa);
812 1.15.6.2 nathanw break;
813 1.15.6.2 nathanw #endif
814 1.15.6.2 nathanw #ifdef NS
815 1.15.6.2 nathanw case AF_NS:
816 1.15.6.2 nathanw {
817 1.15.6.2 nathanw struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
818 1.15.6.2 nathanw
819 1.15.6.2 nathanw if (ns_nullhost(*ina))
820 1.15.6.2 nathanw ina->x_host =
821 1.15.6.2 nathanw *(union ns_host *)LLADDR(ifp->if_sadl);
822 1.15.6.2 nathanw else {
823 1.15.6.2 nathanw memcpy(LLADDR(ifp->if_sadl),
824 1.15.6.2 nathanw ina->x_host.c_host,
825 1.15.6.2 nathanw sizeof(sc->sc_enaddr));
826 1.15.6.2 nathanw }
827 1.15.6.2 nathanw /* Set new address. */
828 1.15.6.2 nathanw gmac_init(sc);
829 1.15.6.2 nathanw break;
830 1.15.6.2 nathanw }
831 1.15.6.2 nathanw #endif
832 1.15.6.2 nathanw default:
833 1.15.6.2 nathanw gmac_init(sc);
834 1.15.6.2 nathanw break;
835 1.15.6.2 nathanw }
836 1.15.6.2 nathanw break;
837 1.15.6.2 nathanw
838 1.15.6.2 nathanw case SIOCSIFFLAGS:
839 1.15.6.2 nathanw if ((ifp->if_flags & IFF_UP) == 0 &&
840 1.15.6.2 nathanw (ifp->if_flags & IFF_RUNNING) != 0) {
841 1.15.6.2 nathanw /*
842 1.15.6.2 nathanw * If interface is marked down and it is running, then
843 1.15.6.2 nathanw * stop it.
844 1.15.6.2 nathanw */
845 1.15.6.2 nathanw gmac_stop(sc);
846 1.15.6.2 nathanw ifp->if_flags &= ~IFF_RUNNING;
847 1.15.6.2 nathanw } else if ((ifp->if_flags & IFF_UP) != 0 &&
848 1.15.6.2 nathanw (ifp->if_flags & IFF_RUNNING) == 0) {
849 1.15.6.2 nathanw /*
850 1.15.6.2 nathanw * If interface is marked up and it is stopped, then
851 1.15.6.2 nathanw * start it.
852 1.15.6.2 nathanw */
853 1.15.6.2 nathanw gmac_init(sc);
854 1.15.6.2 nathanw } else {
855 1.15.6.2 nathanw /*
856 1.15.6.2 nathanw * Reset the interface to pick up changes in any other
857 1.15.6.2 nathanw * flags that affect hardware registers.
858 1.15.6.2 nathanw */
859 1.15.6.2 nathanw gmac_reset(sc);
860 1.15.6.2 nathanw gmac_init(sc);
861 1.15.6.2 nathanw }
862 1.15.6.2 nathanw #ifdef GMAC_DEBUG
863 1.15.6.2 nathanw if (ifp->if_flags & IFF_DEBUG)
864 1.15.6.2 nathanw sc->sc_flags |= GMAC_DEBUGFLAG;
865 1.15.6.2 nathanw #endif
866 1.15.6.2 nathanw break;
867 1.15.6.2 nathanw
868 1.15.6.2 nathanw case SIOCADDMULTI:
869 1.15.6.2 nathanw case SIOCDELMULTI:
870 1.15.6.2 nathanw error = (cmd == SIOCADDMULTI) ?
871 1.15.6.2 nathanw ether_addmulti(ifr, &sc->sc_ethercom) :
872 1.15.6.2 nathanw ether_delmulti(ifr, &sc->sc_ethercom);
873 1.15.6.2 nathanw
874 1.15.6.2 nathanw if (error == ENETRESET) {
875 1.15.6.2 nathanw /*
876 1.15.6.2 nathanw * Multicast list has changed; set the hardware filter
877 1.15.6.2 nathanw * accordingly.
878 1.15.6.2 nathanw */
879 1.15.6.2 nathanw gmac_init(sc);
880 1.15.6.2 nathanw /* gmac_setladrf(sc); */
881 1.15.6.2 nathanw error = 0;
882 1.15.6.2 nathanw }
883 1.15.6.2 nathanw break;
884 1.15.6.2 nathanw
885 1.15.6.2 nathanw case SIOCGIFMEDIA:
886 1.15.6.2 nathanw case SIOCSIFMEDIA:
887 1.15.6.2 nathanw error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
888 1.15.6.2 nathanw break;
889 1.15.6.2 nathanw
890 1.15.6.2 nathanw default:
891 1.15.6.2 nathanw error = EINVAL;
892 1.15.6.2 nathanw }
893 1.15.6.2 nathanw
894 1.15.6.2 nathanw splx(s);
895 1.15.6.2 nathanw return error;
896 1.15.6.2 nathanw }
897 1.15.6.2 nathanw
898 1.15.6.2 nathanw void
899 1.15.6.2 nathanw gmac_watchdog(ifp)
900 1.15.6.2 nathanw struct ifnet *ifp;
901 1.15.6.2 nathanw {
902 1.15.6.2 nathanw struct gmac_softc *sc = ifp->if_softc;
903 1.15.6.2 nathanw
904 1.15.6.2 nathanw printf("%s: device timeout\n", ifp->if_xname);
905 1.15.6.2 nathanw ifp->if_oerrors++;
906 1.15.6.2 nathanw
907 1.15.6.2 nathanw gmac_reset(sc);
908 1.15.6.2 nathanw gmac_init(sc);
909 1.15.6.2 nathanw }
910 1.15.6.2 nathanw
911 1.15.6.2 nathanw int
912 1.15.6.2 nathanw gmac_mediachange(ifp)
913 1.15.6.2 nathanw struct ifnet *ifp;
914 1.15.6.2 nathanw {
915 1.15.6.2 nathanw struct gmac_softc *sc = ifp->if_softc;
916 1.15.6.2 nathanw
917 1.15.6.2 nathanw return mii_mediachg(&sc->sc_mii);
918 1.15.6.2 nathanw }
919 1.15.6.2 nathanw
920 1.15.6.2 nathanw void
921 1.15.6.2 nathanw gmac_mediastatus(ifp, ifmr)
922 1.15.6.2 nathanw struct ifnet *ifp;
923 1.15.6.2 nathanw struct ifmediareq *ifmr;
924 1.15.6.2 nathanw {
925 1.15.6.2 nathanw struct gmac_softc *sc = ifp->if_softc;
926 1.15.6.2 nathanw
927 1.15.6.2 nathanw mii_pollstat(&sc->sc_mii);
928 1.15.6.2 nathanw
929 1.15.6.2 nathanw ifmr->ifm_status = sc->sc_mii.mii_media_status;
930 1.15.6.2 nathanw ifmr->ifm_active = sc->sc_mii.mii_media_active;
931 1.15.6.2 nathanw }
932 1.15.6.2 nathanw
933 1.15.6.2 nathanw int
934 1.15.6.2 nathanw gmac_mii_readreg(dev, phy, reg)
935 1.15.6.2 nathanw struct device *dev;
936 1.15.6.2 nathanw int phy, reg;
937 1.15.6.2 nathanw {
938 1.15.6.2 nathanw struct gmac_softc *sc = (void *)dev;
939 1.15.6.2 nathanw int i;
940 1.15.6.2 nathanw
941 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
942 1.15.6.2 nathanw 0x60020000 | (phy << 23) | (reg << 18));
943 1.15.6.2 nathanw
944 1.15.6.2 nathanw for (i = 1000; i >= 0; i -= 10) {
945 1.15.6.2 nathanw if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
946 1.15.6.2 nathanw break;
947 1.15.6.2 nathanw delay(10);
948 1.15.6.2 nathanw }
949 1.15.6.2 nathanw if (i < 0) {
950 1.15.6.2 nathanw printf("%s: gmac_mii_readreg: timeout\n", sc->sc_dev.dv_xname);
951 1.15.6.2 nathanw return 0;
952 1.15.6.2 nathanw }
953 1.15.6.2 nathanw
954 1.15.6.2 nathanw return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff;
955 1.15.6.2 nathanw }
956 1.15.6.2 nathanw
957 1.15.6.2 nathanw void
958 1.15.6.2 nathanw gmac_mii_writereg(dev, phy, reg, val)
959 1.15.6.2 nathanw struct device *dev;
960 1.15.6.2 nathanw int phy, reg, val;
961 1.15.6.2 nathanw {
962 1.15.6.2 nathanw struct gmac_softc *sc = (void *)dev;
963 1.15.6.2 nathanw int i;
964 1.15.6.2 nathanw
965 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
966 1.15.6.2 nathanw 0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff));
967 1.15.6.2 nathanw
968 1.15.6.2 nathanw for (i = 1000; i >= 0; i -= 10) {
969 1.15.6.2 nathanw if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
970 1.15.6.2 nathanw break;
971 1.15.6.2 nathanw delay(10);
972 1.15.6.2 nathanw }
973 1.15.6.2 nathanw if (i < 0)
974 1.15.6.2 nathanw printf("%s: gmac_mii_writereg: timeout\n", sc->sc_dev.dv_xname);
975 1.15.6.2 nathanw }
976 1.15.6.2 nathanw
977 1.15.6.2 nathanw void
978 1.15.6.2 nathanw gmac_mii_statchg(dev)
979 1.15.6.2 nathanw struct device *dev;
980 1.15.6.2 nathanw {
981 1.15.6.2 nathanw struct gmac_softc *sc = (void *)dev;
982 1.15.6.2 nathanw
983 1.15.6.2 nathanw gmac_stop_txdma(sc);
984 1.15.6.2 nathanw gmac_stop_rxdma(sc);
985 1.15.6.2 nathanw
986 1.15.6.2 nathanw if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
987 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
988 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
989 1.15.6.2 nathanw } else {
990 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
991 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
992 1.15.6.2 nathanw }
993 1.15.6.2 nathanw
994 1.15.6.2 nathanw if (0) /* g-bit? */
995 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
996 1.15.6.2 nathanw else
997 1.15.6.2 nathanw gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
998 1.15.6.2 nathanw
999 1.15.6.2 nathanw gmac_start_txdma(sc);
1000 1.15.6.2 nathanw gmac_start_rxdma(sc);
1001 1.15.6.2 nathanw }
1002 1.15.6.2 nathanw
1003 1.15.6.2 nathanw void
1004 1.15.6.2 nathanw gmac_mii_tick(v)
1005 1.15.6.2 nathanw void *v;
1006 1.15.6.2 nathanw {
1007 1.15.6.2 nathanw struct gmac_softc *sc = v;
1008 1.15.6.2 nathanw int s;
1009 1.15.6.2 nathanw
1010 1.15.6.2 nathanw s = splnet();
1011 1.15.6.2 nathanw mii_tick(&sc->sc_mii);
1012 1.15.6.2 nathanw splx(s);
1013 1.15.6.2 nathanw
1014 1.15.6.2 nathanw callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc);
1015 1.15.6.2 nathanw }
1016