if_mec.c revision 1.18 1 /* $NetBSD: if_mec.c,v 1.18 2008/02/25 22:49:20 martin Exp $ */
2
3 /*
4 * Copyright (c) 2004 Izumi Tsutsui.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Copyright (c) 2003 Christopher SEKIYA
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed for the
45 * NetBSD Project. See http://www.NetBSD.org/ for
46 * information about NetBSD.
47 * 4. The name of the author may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * MACE MAC-110 Ethernet driver
64 */
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: if_mec.c,v 1.18 2008/02/25 22:49:20 martin Exp $");
68
69 #include "opt_ddb.h"
70 #include "bpfilter.h"
71 #include "rnd.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/device.h>
76 #include <sys/callout.h>
77 #include <sys/mbuf.h>
78 #include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #include <sys/socket.h>
81 #include <sys/ioctl.h>
82 #include <sys/errno.h>
83
84 #if NRND > 0
85 #include <sys/rnd.h>
86 #endif
87
88 #include <net/if.h>
89 #include <net/if_dl.h>
90 #include <net/if_media.h>
91 #include <net/if_ether.h>
92
93 #if NBPFILTER > 0
94 #include <net/bpf.h>
95 #endif
96
97 #include <machine/bus.h>
98 #include <machine/intr.h>
99 #include <machine/machtype.h>
100
101 #include <dev/mii/mii.h>
102 #include <dev/mii/miivar.h>
103
104 #include <sgimips/mace/macevar.h>
105 #include <sgimips/mace/if_mecreg.h>
106
107 #include <dev/arcbios/arcbios.h>
108 #include <dev/arcbios/arcbiosvar.h>
109
110 /* #define MEC_DEBUG */
111
112 #ifdef MEC_DEBUG
113 #define MEC_DEBUG_RESET 0x01
114 #define MEC_DEBUG_START 0x02
115 #define MEC_DEBUG_STOP 0x04
116 #define MEC_DEBUG_INTR 0x08
117 #define MEC_DEBUG_RXINTR 0x10
118 #define MEC_DEBUG_TXINTR 0x20
119 uint32_t mec_debug = 0;
120 #define DPRINTF(x, y) if (mec_debug & (x)) printf y
121 #else
122 #define DPRINTF(x, y) /* nothing */
123 #endif
124
125 /*
126 * Transmit descriptor list size
127 */
128 #define MEC_NTXDESC 64
129 #define MEC_NTXDESC_MASK (MEC_NTXDESC - 1)
130 #define MEC_NEXTTX(x) (((x) + 1) & MEC_NTXDESC_MASK)
131
132 /*
133 * software state for TX
134 */
135 struct mec_txsoft {
136 struct mbuf *txs_mbuf; /* head of our mbuf chain */
137 bus_dmamap_t txs_dmamap; /* our DMA map */
138 uint32_t txs_flags;
139 #define MEC_TXS_BUFLEN_MASK 0x0000007f /* data len in txd_buf */
140 #define MEC_TXS_TXDBUF 0x00000080 /* txd_buf is used */
141 #define MEC_TXS_TXDPTR1 0x00000100 /* txd_ptr[0] is used */
142 };
143
144 /*
145 * Transmit buffer descriptor
146 */
147 #define MEC_TXDESCSIZE 128
148 #define MEC_NTXPTR 3
149 #define MEC_TXD_BUFOFFSET \
150 (sizeof(uint64_t) + MEC_NTXPTR * sizeof(uint64_t))
151 #define MEC_TXD_BUFSIZE (MEC_TXDESCSIZE - MEC_TXD_BUFOFFSET)
152 #define MEC_TXD_BUFSTART(len) (MEC_TXD_BUFSIZE - (len))
153 #define MEC_TXD_ALIGN 8
154 #define MEC_TXD_ROUNDUP(addr) \
155 (((addr) + (MEC_TXD_ALIGN - 1)) & ~((uint64_t)MEC_TXD_ALIGN - 1))
156
157 struct mec_txdesc {
158 volatile uint64_t txd_cmd;
159 #define MEC_TXCMD_DATALEN 0x000000000000ffff /* data length */
160 #define MEC_TXCMD_BUFSTART 0x00000000007f0000 /* start byte offset */
161 #define TXCMD_BUFSTART(x) ((x) << 16)
162 #define MEC_TXCMD_TERMDMA 0x0000000000800000 /* stop DMA on abort */
163 #define MEC_TXCMD_TXINT 0x0000000001000000 /* INT after TX done */
164 #define MEC_TXCMD_PTR1 0x0000000002000000 /* valid 1st txd_ptr */
165 #define MEC_TXCMD_PTR2 0x0000000004000000 /* valid 2nd txd_ptr */
166 #define MEC_TXCMD_PTR3 0x0000000008000000 /* valid 3rd txd_ptr */
167 #define MEC_TXCMD_UNUSED 0xfffffffff0000000ULL /* should be zero */
168
169 #define txd_stat txd_cmd
170 #define MEC_TXSTAT_LEN 0x000000000000ffff /* TX length */
171 #define MEC_TXSTAT_COLCNT 0x00000000000f0000 /* collision count */
172 #define MEC_TXSTAT_COLCNT_SHIFT 16
173 #define MEC_TXSTAT_LATE_COL 0x0000000000100000 /* late collision */
174 #define MEC_TXSTAT_CRCERROR 0x0000000000200000 /* */
175 #define MEC_TXSTAT_DEFERRED 0x0000000000400000 /* */
176 #define MEC_TXSTAT_SUCCESS 0x0000000000800000 /* TX complete */
177 #define MEC_TXSTAT_TOOBIG 0x0000000001000000 /* */
178 #define MEC_TXSTAT_UNDERRUN 0x0000000002000000 /* */
179 #define MEC_TXSTAT_COLLISIONS 0x0000000004000000 /* */
180 #define MEC_TXSTAT_EXDEFERRAL 0x0000000008000000 /* */
181 #define MEC_TXSTAT_COLLIDED 0x0000000010000000 /* */
182 #define MEC_TXSTAT_UNUSED 0x7fffffffe0000000ULL /* should be zero */
183 #define MEC_TXSTAT_SENT 0x8000000000000000ULL /* packet sent */
184
185 uint64_t txd_ptr[MEC_NTXPTR];
186 #define MEC_TXPTR_UNUSED2 0x0000000000000007 /* should be zero */
187 #define MEC_TXPTR_DMAADDR 0x00000000fffffff8 /* TX DMA address */
188 #define MEC_TXPTR_LEN 0x0000ffff00000000ULL /* buffer length */
189 #define TXPTR_LEN(x) ((uint64_t)(x) << 32)
190 #define MEC_TXPTR_UNUSED1 0xffff000000000000ULL /* should be zero */
191
192 uint8_t txd_buf[MEC_TXD_BUFSIZE];
193 };
194
195 /*
196 * Receive buffer size
197 */
198 #define MEC_NRXDESC 16
199 #define MEC_NRXDESC_MASK (MEC_NRXDESC - 1)
200 #define MEC_NEXTRX(x) (((x) + 1) & MEC_NRXDESC_MASK)
201
202 /*
203 * Receive buffer description
204 */
205 #define MEC_RXDESCSIZE 4096 /* umm, should be 4kbyte aligned */
206 #define MEC_RXD_NRXPAD 3
207 #define MEC_RXD_DMAOFFSET (1 + MEC_RXD_NRXPAD)
208 #define MEC_RXD_BUFOFFSET (MEC_RXD_DMAOFFSET * sizeof(uint64_t))
209 #define MEC_RXD_BUFSIZE (MEC_RXDESCSIZE - MEC_RXD_BUFOFFSET)
210
211 struct mec_rxdesc {
212 volatile uint64_t rxd_stat;
213 #define MEC_RXSTAT_LEN 0x000000000000ffff /* data length */
214 #define MEC_RXSTAT_VIOLATION 0x0000000000010000 /* code violation (?) */
215 #define MEC_RXSTAT_UNUSED2 0x0000000000020000 /* unknown (?) */
216 #define MEC_RXSTAT_CRCERROR 0x0000000000040000 /* CRC error */
217 #define MEC_RXSTAT_MULTICAST 0x0000000000080000 /* multicast packet */
218 #define MEC_RXSTAT_BROADCAST 0x0000000000100000 /* broadcast packet */
219 #define MEC_RXSTAT_INVALID 0x0000000000200000 /* invalid preamble */
220 #define MEC_RXSTAT_LONGEVENT 0x0000000000400000 /* long packet */
221 #define MEC_RXSTAT_BADPACKET 0x0000000000800000 /* bad packet */
222 #define MEC_RXSTAT_CAREVENT 0x0000000001000000 /* carrier event */
223 #define MEC_RXSTAT_MATCHMCAST 0x0000000002000000 /* match multicast */
224 #define MEC_RXSTAT_MATCHMAC 0x0000000004000000 /* match MAC */
225 #define MEC_RXSTAT_SEQNUM 0x00000000f8000000 /* sequence number */
226 #define MEC_RXSTAT_CKSUM 0x0000ffff00000000ULL /* IP checksum */
227 #define MEC_RXSTAT_UNUSED1 0x7fff000000000000ULL /* should be zero */
228 #define MEC_RXSTAT_RECEIVED 0x8000000000000000ULL /* set to 1 on RX */
229 uint64_t rxd_pad1[MEC_RXD_NRXPAD];
230 uint8_t rxd_buf[MEC_RXD_BUFSIZE];
231 };
232
233 /*
234 * control structures for DMA ops
235 */
236 struct mec_control_data {
237 /*
238 * TX descriptors and buffers
239 */
240 struct mec_txdesc mcd_txdesc[MEC_NTXDESC];
241
242 /*
243 * RX descriptors and buffers
244 */
245 struct mec_rxdesc mcd_rxdesc[MEC_NRXDESC];
246 };
247
248 /*
249 * It _seems_ there are some restrictions on descriptor address:
250 *
251 * - Base address of txdescs should be 8kbyte aligned
252 * - Each txdesc should be 128byte aligned
253 * - Each rxdesc should be 4kbyte aligned
254 *
255 * So we should specify 8k align to allocalte txdescs.
256 * In this case, sizeof(struct mec_txdesc) * MEC_NTXDESC is 8192
257 * so rxdescs are also allocated at 4kbyte aligned.
258 */
259 #define MEC_CONTROL_DATA_ALIGN (8 * 1024)
260
261 #define MEC_CDOFF(x) offsetof(struct mec_control_data, x)
262 #define MEC_CDTXOFF(x) MEC_CDOFF(mcd_txdesc[(x)])
263 #define MEC_CDRXOFF(x) MEC_CDOFF(mcd_rxdesc[(x)])
264
265 /*
266 * software state per device
267 */
268 struct mec_softc {
269 struct device sc_dev; /* generic device structures */
270
271 bus_space_tag_t sc_st; /* bus_space tag */
272 bus_space_handle_t sc_sh; /* bus_space handle */
273 bus_dma_tag_t sc_dmat; /* bus_dma tag */
274 void *sc_sdhook; /* shutdown hook */
275
276 struct ethercom sc_ethercom; /* Ethernet common part */
277
278 struct mii_data sc_mii; /* MII/media information */
279 int sc_phyaddr; /* MII address */
280 struct callout sc_tick_ch; /* tick callout */
281
282 uint8_t sc_enaddr[ETHER_ADDR_LEN]; /* MAC address */
283
284 bus_dmamap_t sc_cddmamap; /* bus_dma map for control data */
285 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
286
287 /* pointer to allocated control data */
288 struct mec_control_data *sc_control_data;
289 #define sc_txdesc sc_control_data->mcd_txdesc
290 #define sc_rxdesc sc_control_data->mcd_rxdesc
291
292 /* software state for TX descs */
293 struct mec_txsoft sc_txsoft[MEC_NTXDESC];
294
295 int sc_txpending; /* number of TX requests pending */
296 int sc_txdirty; /* first dirty TX descriptor */
297 int sc_txlast; /* last used TX descriptor */
298
299 int sc_rxptr; /* next ready RX buffer */
300
301 #if NRND > 0
302 rndsource_element_t sc_rnd_source; /* random source */
303 #endif
304 };
305
306 #define MEC_CDTXADDR(sc, x) ((sc)->sc_cddma + MEC_CDTXOFF(x))
307 #define MEC_CDRXADDR(sc, x) ((sc)->sc_cddma + MEC_CDRXOFF(x))
308
309 #define MEC_TXDESCSYNC(sc, x, ops) \
310 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
311 MEC_CDTXOFF(x), MEC_TXDESCSIZE, (ops))
312 #define MEC_TXCMDSYNC(sc, x, ops) \
313 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
314 MEC_CDTXOFF(x), sizeof(uint64_t), (ops))
315
316 #define MEC_RXSTATSYNC(sc, x, ops) \
317 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
318 MEC_CDRXOFF(x), sizeof(uint64_t), (ops))
319 #define MEC_RXBUFSYNC(sc, x, len, ops) \
320 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
321 MEC_CDRXOFF(x) + MEC_RXD_BUFOFFSET, \
322 MEC_ETHER_ALIGN + (len), (ops))
323
324 /* XXX these values should be moved to <net/if_ether.h> ? */
325 #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
326 #define MEC_ETHER_ALIGN 2
327
328 #ifdef DDB
329 #define STATIC
330 #else
331 #define STATIC static
332 #endif
333
334 STATIC int mec_match(struct device *, struct cfdata *, void *);
335 STATIC void mec_attach(struct device *, struct device *, void *);
336
337 STATIC int mec_mii_readreg(struct device *, int, int);
338 STATIC void mec_mii_writereg(struct device *, int, int, int);
339 STATIC int mec_mii_wait(struct mec_softc *);
340 STATIC void mec_statchg(struct device *);
341
342 static void enaddr_aton(const char *, uint8_t *);
343
344 STATIC int mec_init(struct ifnet * ifp);
345 STATIC void mec_start(struct ifnet *);
346 STATIC void mec_watchdog(struct ifnet *);
347 STATIC void mec_tick(void *);
348 STATIC int mec_ioctl(struct ifnet *, u_long, void *);
349 STATIC void mec_reset(struct mec_softc *);
350 STATIC void mec_setfilter(struct mec_softc *);
351 STATIC int mec_intr(void *arg);
352 STATIC void mec_stop(struct ifnet *, int);
353 STATIC void mec_rxintr(struct mec_softc *);
354 STATIC void mec_txintr(struct mec_softc *);
355 STATIC void mec_shutdown(void *);
356
357 CFATTACH_DECL(mec, sizeof(struct mec_softc),
358 mec_match, mec_attach, NULL, NULL);
359
360 static int mec_matched = 0;
361
362 STATIC int
363 mec_match(struct device *parent, struct cfdata *match, void *aux)
364 {
365
366 /* allow only one device */
367 if (mec_matched)
368 return 0;
369
370 mec_matched = 1;
371 return 1;
372 }
373
374 STATIC void
375 mec_attach(struct device *parent, struct device *self, void *aux)
376 {
377 struct mec_softc *sc = (void *)self;
378 struct mace_attach_args *maa = aux;
379 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
380 uint64_t address, command;
381 const char *macaddr;
382 struct mii_softc *child;
383 bus_dma_segment_t seg;
384 int i, err, rseg;
385 bool mac_is_fake;
386
387 sc->sc_st = maa->maa_st;
388 if (bus_space_subregion(sc->sc_st, maa->maa_sh,
389 maa->maa_offset, 0, &sc->sc_sh) != 0) {
390 printf(": can't map i/o space\n");
391 return;
392 }
393
394 /* set up DMA structures */
395 sc->sc_dmat = maa->maa_dmat;
396
397 /*
398 * Allocate the control data structures, and create and load the
399 * DMA map for it.
400 */
401 if ((err = bus_dmamem_alloc(sc->sc_dmat,
402 sizeof(struct mec_control_data), MEC_CONTROL_DATA_ALIGN, 0,
403 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
404 printf(": unable to allocate control data, error = %d\n", err);
405 goto fail_0;
406 }
407 /*
408 * XXX needs re-think...
409 * control data structures contain whole RX data buffer, so
410 * BUS_DMA_COHERENT (which disables cache) may cause some performance
411 * issue on copying data from the RX buffer to mbuf on normal memory,
412 * though we have to make sure all bus_dmamap_sync(9) ops are called
413 * properly in that case.
414 */
415 if ((err = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
416 sizeof(struct mec_control_data),
417 (void **)&sc->sc_control_data, /*BUS_DMA_COHERENT*/ 0)) != 0) {
418 printf(": unable to map control data, error = %d\n", err);
419 goto fail_1;
420 }
421 memset(sc->sc_control_data, 0, sizeof(struct mec_control_data));
422
423 if ((err = bus_dmamap_create(sc->sc_dmat,
424 sizeof(struct mec_control_data), 1,
425 sizeof(struct mec_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
426 printf(": unable to create control data DMA map, error = %d\n",
427 err);
428 goto fail_2;
429 }
430 if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
431 sc->sc_control_data, sizeof(struct mec_control_data), NULL,
432 BUS_DMA_NOWAIT)) != 0) {
433 printf(": unable to load control data DMA map, error = %d\n",
434 err);
435 goto fail_3;
436 }
437
438 /* create TX buffer DMA maps */
439 for (i = 0; i < MEC_NTXDESC; i++) {
440 if ((err = bus_dmamap_create(sc->sc_dmat,
441 MCLBYTES, 1, MCLBYTES, 0, 0,
442 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
443 printf(": unable to create tx DMA map %d, error = %d\n",
444 i, err);
445 goto fail_4;
446 }
447 }
448
449 callout_init(&sc->sc_tick_ch, 0);
450
451 /* get Ethernet address from ARCBIOS */
452 if ((macaddr = ARCBIOS->GetEnvironmentVariable("eaddr")) == NULL) {
453 printf(": unable to get MAC address!\n");
454 goto fail_4;
455 }
456 /*
457 * On some machines the DS2502 chip storing the serial number/
458 * mac address is on the pci riser board - if this board is
459 * missing, ARCBIOS will not know a good ethernet address (but
460 * otherwise the machine will work fine).
461 */
462 mac_is_fake = false;
463 if (strcmp(macaddr, "ff:ff:ff:ff:ff:ff") == 0) {
464 uint32_t ui = 0;
465 const char * netaddr =
466 ARCBIOS->GetEnvironmentVariable("netaddr");
467
468 /*
469 * Create a MAC address by abusing the "netaddr" env var
470 */
471 sc->sc_enaddr[0] = 0xf2;
472 sc->sc_enaddr[1] = 0x0b;
473 sc->sc_enaddr[2] = 0xa4;
474 if (netaddr) {
475 mac_is_fake = true;
476 while (*netaddr) {
477 int v = 0;
478 while (*netaddr && *netaddr != '.') {
479 if (*netaddr >= '0' && *netaddr <= '9')
480 v = v*10 + (*netaddr - '0');
481 netaddr++;
482 }
483 ui <<= 8;
484 ui |= v;
485 if (*netaddr == '.')
486 netaddr++;
487 }
488 }
489 memcpy(sc->sc_enaddr+3, ((uint8_t *)&ui)+1, 3);
490 }
491 if (!mac_is_fake)
492 enaddr_aton(macaddr, sc->sc_enaddr);
493
494 /* set the Ethernet address */
495 address = 0;
496 for (i = 0; i < ETHER_ADDR_LEN; i++) {
497 address = address << 8;
498 address |= sc->sc_enaddr[i];
499 }
500 bus_space_write_8(sc->sc_st, sc->sc_sh, MEC_STATION, address);
501
502 /* reset device */
503 mec_reset(sc);
504
505 command = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_MAC_CONTROL);
506
507 printf(": MAC-110 Ethernet, rev %u\n",
508 (u_int)((command & MEC_MAC_REVISION) >> MEC_MAC_REVISION_SHIFT));
509
510 if (mac_is_fake)
511 printf("%s: could not get ethernet address from firmware"
512 " - generated one from the \"netaddr\" environment"
513 " variable\n", sc->sc_dev.dv_xname);
514 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
515 ether_sprintf(sc->sc_enaddr));
516
517 /* Done, now attach everything */
518
519 sc->sc_mii.mii_ifp = ifp;
520 sc->sc_mii.mii_readreg = mec_mii_readreg;
521 sc->sc_mii.mii_writereg = mec_mii_writereg;
522 sc->sc_mii.mii_statchg = mec_statchg;
523
524 /* Set up PHY properties */
525 sc->sc_ethercom.ec_mii = &sc->sc_mii;
526 ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
527 ether_mediastatus);
528 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
529 MII_OFFSET_ANY, 0);
530
531 child = LIST_FIRST(&sc->sc_mii.mii_phys);
532 if (child == NULL) {
533 /* No PHY attached */
534 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL,
535 0, NULL);
536 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
537 } else {
538 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
539 sc->sc_phyaddr = child->mii_phy;
540 }
541
542 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
543 ifp->if_softc = sc;
544 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
545 ifp->if_ioctl = mec_ioctl;
546 ifp->if_start = mec_start;
547 ifp->if_watchdog = mec_watchdog;
548 ifp->if_init = mec_init;
549 ifp->if_stop = mec_stop;
550 ifp->if_mtu = ETHERMTU;
551 IFQ_SET_READY(&ifp->if_snd);
552
553 if_attach(ifp);
554 ether_ifattach(ifp, sc->sc_enaddr);
555
556 /* establish interrupt */
557 cpu_intr_establish(maa->maa_intr, maa->maa_intrmask, mec_intr, sc);
558
559 #if NRND > 0
560 rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
561 RND_TYPE_NET, 0);
562 #endif
563
564 /* set shutdown hook to reset interface on powerdown */
565 sc->sc_sdhook = shutdownhook_establish(mec_shutdown, sc);
566
567 return;
568
569 /*
570 * Free any resources we've allocated during the failed attach
571 * attempt. Do this in reverse order and fall though.
572 */
573 fail_4:
574 for (i = 0; i < MEC_NTXDESC; i++) {
575 if (sc->sc_txsoft[i].txs_dmamap != NULL)
576 bus_dmamap_destroy(sc->sc_dmat,
577 sc->sc_txsoft[i].txs_dmamap);
578 }
579 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
580 fail_3:
581 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
582 fail_2:
583 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
584 sizeof(struct mec_control_data));
585 fail_1:
586 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
587 fail_0:
588 return;
589 }
590
591 STATIC int
592 mec_mii_readreg(struct device *self, int phy, int reg)
593 {
594 struct mec_softc *sc = (void *)self;
595 bus_space_tag_t st = sc->sc_st;
596 bus_space_handle_t sh = sc->sc_sh;
597 uint64_t val;
598 int i;
599
600 if (mec_mii_wait(sc) != 0)
601 return 0;
602
603 bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
604 (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
605 delay(25);
606 bus_space_write_8(st, sh, MEC_PHY_READ_INITIATE, 1);
607 delay(25);
608 mec_mii_wait(sc);
609
610 for (i = 0; i < 20; i++) {
611 delay(30);
612
613 val = bus_space_read_8(st, sh, MEC_PHY_DATA);
614
615 if ((val & MEC_PHY_DATA_BUSY) == 0)
616 return val & MEC_PHY_DATA_VALUE;
617 }
618 return 0;
619 }
620
621 STATIC void
622 mec_mii_writereg(struct device *self, int phy, int reg, int val)
623 {
624 struct mec_softc *sc = (void *)self;
625 bus_space_tag_t st = sc->sc_st;
626 bus_space_handle_t sh = sc->sc_sh;
627
628 if (mec_mii_wait(sc) != 0) {
629 printf("timed out writing %x: %x\n", reg, val);
630 return;
631 }
632
633 bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
634 (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
635
636 delay(60);
637
638 bus_space_write_8(st, sh, MEC_PHY_DATA, val & MEC_PHY_DATA_VALUE);
639
640 delay(60);
641
642 mec_mii_wait(sc);
643 }
644
645 STATIC int
646 mec_mii_wait(struct mec_softc *sc)
647 {
648 uint32_t busy;
649 int i, s;
650
651 for (i = 0; i < 100; i++) {
652 delay(30);
653
654 s = splhigh();
655 busy = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_PHY_DATA);
656 splx(s);
657
658 if ((busy & MEC_PHY_DATA_BUSY) == 0)
659 return 0;
660 #if 0
661 if (busy == 0xffff) /* XXX ? */
662 return 0;
663 #endif
664 }
665
666 printf("%s: MII timed out\n", sc->sc_dev.dv_xname);
667 return 1;
668 }
669
670 STATIC void
671 mec_statchg(struct device *self)
672 {
673 struct mec_softc *sc = (void *)self;
674 bus_space_tag_t st = sc->sc_st;
675 bus_space_handle_t sh = sc->sc_sh;
676 uint32_t control;
677
678 control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
679 control &= ~(MEC_MAC_IPGT | MEC_MAC_IPGR1 | MEC_MAC_IPGR2 |
680 MEC_MAC_FULL_DUPLEX | MEC_MAC_SPEED_SELECT);
681
682 /* must also set IPG here for duplex stuff ... */
683 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0) {
684 control |= MEC_MAC_FULL_DUPLEX;
685 } else {
686 /* set IPG */
687 control |= MEC_MAC_IPG_DEFAULT;
688 }
689
690 bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
691 }
692
693 /*
694 * XXX
695 * maybe this function should be moved to common part
696 * (sgimips/machdep.c or elsewhere) for all on-board network devices.
697 */
698 static void
699 enaddr_aton(const char *str, uint8_t *eaddr)
700 {
701 int i;
702 char c;
703
704 for (i = 0; i < ETHER_ADDR_LEN; i++) {
705 if (*str == ':')
706 str++;
707
708 c = *str++;
709 if (isdigit(c)) {
710 eaddr[i] = (c - '0');
711 } else if (isxdigit(c)) {
712 eaddr[i] = (toupper(c) + 10 - 'A');
713 }
714 c = *str++;
715 if (isdigit(c)) {
716 eaddr[i] = (eaddr[i] << 4) | (c - '0');
717 } else if (isxdigit(c)) {
718 eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
719 }
720 }
721 }
722
723 STATIC int
724 mec_init(struct ifnet *ifp)
725 {
726 struct mec_softc *sc = ifp->if_softc;
727 bus_space_tag_t st = sc->sc_st;
728 bus_space_handle_t sh = sc->sc_sh;
729 struct mec_rxdesc *rxd;
730 int i, rc;
731
732 /* cancel any pending I/O */
733 mec_stop(ifp, 0);
734
735 /* reset device */
736 mec_reset(sc);
737
738 /* setup filter for multicast or promisc mode */
739 mec_setfilter(sc);
740
741 /* set the TX ring pointer to the base address */
742 bus_space_write_8(st, sh, MEC_TX_RING_BASE, MEC_CDTXADDR(sc, 0));
743
744 sc->sc_txpending = 0;
745 sc->sc_txdirty = 0;
746 sc->sc_txlast = MEC_NTXDESC - 1;
747
748 /* put RX buffers into FIFO */
749 for (i = 0; i < MEC_NRXDESC; i++) {
750 rxd = &sc->sc_rxdesc[i];
751 rxd->rxd_stat = 0;
752 MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
753 MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
754 bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
755 }
756 sc->sc_rxptr = 0;
757
758 #if 0 /* XXX no info */
759 bus_space_write_8(st, sh, MEC_TIMER, 0);
760 #endif
761
762 /*
763 * MEC_DMA_TX_INT_ENABLE will be set later otherwise it causes
764 * spurious interrupts when TX buffers are empty
765 */
766 bus_space_write_8(st, sh, MEC_DMA_CONTROL,
767 (MEC_RXD_DMAOFFSET << MEC_DMA_RX_DMA_OFFSET_SHIFT) |
768 (MEC_NRXDESC << MEC_DMA_RX_INT_THRESH_SHIFT) |
769 MEC_DMA_TX_DMA_ENABLE | /* MEC_DMA_TX_INT_ENABLE | */
770 MEC_DMA_RX_DMA_ENABLE | MEC_DMA_RX_INT_ENABLE);
771
772 callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
773
774 if ((rc = ether_mediachange(ifp)) != 0)
775 return rc;
776
777 ifp->if_flags |= IFF_RUNNING;
778 ifp->if_flags &= ~IFF_OACTIVE;
779 mec_start(ifp);
780
781 return 0;
782 }
783
784 STATIC void
785 mec_reset(struct mec_softc *sc)
786 {
787 bus_space_tag_t st = sc->sc_st;
788 bus_space_handle_t sh = sc->sc_sh;
789 uint64_t control;
790
791 /* stop DMA first */
792 bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
793
794 /* reset chip */
795 bus_space_write_8(st, sh, MEC_MAC_CONTROL, MEC_MAC_CORE_RESET);
796 delay(1000);
797 bus_space_write_8(st, sh, MEC_MAC_CONTROL, 0);
798 delay(1000);
799
800 /* Default to 100/half and let auto-negotiation work its magic */
801 control = MEC_MAC_SPEED_SELECT | MEC_MAC_FILTER_MATCHMULTI |
802 MEC_MAC_IPG_DEFAULT;
803
804 bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
805 /* stop DMA again for sanity */
806 bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
807
808 DPRINTF(MEC_DEBUG_RESET, ("mec: control now %llx\n",
809 bus_space_read_8(st, sh, MEC_MAC_CONTROL)));
810 }
811
812 STATIC void
813 mec_start(struct ifnet *ifp)
814 {
815 struct mec_softc *sc = ifp->if_softc;
816 struct mbuf *m0, *m;
817 struct mec_txdesc *txd;
818 struct mec_txsoft *txs;
819 bus_dmamap_t dmamap;
820 bus_space_tag_t st = sc->sc_st;
821 bus_space_handle_t sh = sc->sc_sh;
822 uint64_t txdaddr;
823 int error, firsttx, nexttx, opending;
824 int len, bufoff, buflen, unaligned, txdlen;
825
826 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
827 return;
828
829 /*
830 * Remember the previous txpending and the first transmit descriptor.
831 */
832 opending = sc->sc_txpending;
833 firsttx = MEC_NEXTTX(sc->sc_txlast);
834
835 DPRINTF(MEC_DEBUG_START,
836 ("mec_start: opending = %d, firsttx = %d\n", opending, firsttx));
837
838 for (;;) {
839 /* Grab a packet off the queue. */
840 IFQ_POLL(&ifp->if_snd, m0);
841 if (m0 == NULL)
842 break;
843 m = NULL;
844
845 if (sc->sc_txpending == MEC_NTXDESC) {
846 break;
847 }
848
849 /*
850 * Get the next available transmit descriptor.
851 */
852 nexttx = MEC_NEXTTX(sc->sc_txlast);
853 txd = &sc->sc_txdesc[nexttx];
854 txs = &sc->sc_txsoft[nexttx];
855
856 buflen = 0;
857 bufoff = 0;
858 txdaddr = 0; /* XXX gcc */
859 txdlen = 0; /* XXX gcc */
860
861 len = m0->m_pkthdr.len;
862
863 DPRINTF(MEC_DEBUG_START,
864 ("mec_start: len = %d, nexttx = %d\n", len, nexttx));
865
866 if (len < ETHER_PAD_LEN) {
867 /*
868 * I don't know if MEC chip does auto padding,
869 * so if the packet is small enough,
870 * just copy it to the buffer in txdesc.
871 * Maybe this is the simple way.
872 */
873 DPRINTF(MEC_DEBUG_START, ("mec_start: short packet\n"));
874
875 IFQ_DEQUEUE(&ifp->if_snd, m0);
876 bufoff = MEC_TXD_BUFSTART(ETHER_PAD_LEN);
877 m_copydata(m0, 0, m0->m_pkthdr.len,
878 txd->txd_buf + bufoff);
879 memset(txd->txd_buf + bufoff + len, 0,
880 ETHER_PAD_LEN - len);
881 len = buflen = ETHER_PAD_LEN;
882
883 txs->txs_flags = MEC_TXS_TXDBUF | buflen;
884 } else {
885 /*
886 * If the packet won't fit the buffer in txdesc,
887 * we have to use concatenate pointer to handle it.
888 * While MEC can handle up to three segments to
889 * concatenate, MEC requires that both the second and
890 * third segments have to be 8 byte aligned.
891 * Since it's unlikely for mbuf clusters, we use
892 * only the first concatenate pointer. If the packet
893 * doesn't fit in one DMA segment, allocate new mbuf
894 * and copy the packet to it.
895 *
896 * Besides, if the start address of the first segments
897 * is not 8 byte aligned, such part have to be copied
898 * to the txdesc buffer. (XXX see below comments)
899 */
900 DPRINTF(MEC_DEBUG_START, ("mec_start: long packet\n"));
901
902 dmamap = txs->txs_dmamap;
903 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
904 BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
905 DPRINTF(MEC_DEBUG_START,
906 ("mec_start: re-allocating mbuf\n"));
907 MGETHDR(m, M_DONTWAIT, MT_DATA);
908 if (m == NULL) {
909 printf("%s: unable to allocate "
910 "TX mbuf\n", sc->sc_dev.dv_xname);
911 break;
912 }
913 if (len > (MHLEN - MEC_ETHER_ALIGN)) {
914 MCLGET(m, M_DONTWAIT);
915 if ((m->m_flags & M_EXT) == 0) {
916 printf("%s: unable to allocate "
917 "TX cluster\n",
918 sc->sc_dev.dv_xname);
919 m_freem(m);
920 break;
921 }
922 }
923 /*
924 * Each packet has the Ethernet header, so
925 * in many case the header isn't 4-byte aligned
926 * and data after the header is 4-byte aligned.
927 * Thus adding 2-byte offset before copying to
928 * new mbuf avoids unaligned copy and this may
929 * improve some performance.
930 * As noted above, unaligned part has to be
931 * copied to txdesc buffer so this may cause
932 * extra copy ops, but for now MEC always
933 * requires some data in txdesc buffer,
934 * so we always have to copy some data anyway.
935 */
936 m->m_data += MEC_ETHER_ALIGN;
937 m_copydata(m0, 0, len, mtod(m, void *));
938 m->m_pkthdr.len = m->m_len = len;
939 error = bus_dmamap_load_mbuf(sc->sc_dmat,
940 dmamap, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
941 if (error) {
942 printf("%s: unable to load TX buffer, "
943 "error = %d\n",
944 sc->sc_dev.dv_xname, error);
945 break;
946 }
947 }
948 IFQ_DEQUEUE(&ifp->if_snd, m0);
949 if (m != NULL) {
950 m_freem(m0);
951 m0 = m;
952 }
953
954 /* handle unaligned part */
955 txdaddr = MEC_TXD_ROUNDUP(dmamap->dm_segs[0].ds_addr);
956 txs->txs_flags = MEC_TXS_TXDPTR1;
957 unaligned =
958 dmamap->dm_segs[0].ds_addr & (MEC_TXD_ALIGN - 1);
959 DPRINTF(MEC_DEBUG_START,
960 ("mec_start: ds_addr = 0x%08x, unaligned = %d\n",
961 (u_int)dmamap->dm_segs[0].ds_addr, unaligned));
962 if (unaligned != 0) {
963 buflen = MEC_TXD_ALIGN - unaligned;
964 bufoff = MEC_TXD_BUFSTART(buflen);
965 DPRINTF(MEC_DEBUG_START,
966 ("mec_start: unaligned, "
967 "buflen = %d, bufoff = %d\n",
968 buflen, bufoff));
969 memcpy(txd->txd_buf + bufoff,
970 mtod(m0, void *), buflen);
971 txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
972 }
973 #if 1
974 else {
975 /*
976 * XXX needs hardware info XXX
977 * It seems MEC always requires some data
978 * in txd_buf[] even if buffer is
979 * 8-byte aligned otherwise DMA abort error
980 * occurs later...
981 */
982 buflen = MEC_TXD_ALIGN;
983 bufoff = MEC_TXD_BUFSTART(buflen);
984 memcpy(txd->txd_buf + bufoff,
985 mtod(m0, void *), buflen);
986 DPRINTF(MEC_DEBUG_START,
987 ("mec_start: aligned, "
988 "buflen = %d, bufoff = %d\n",
989 buflen, bufoff));
990 txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
991 txdaddr += MEC_TXD_ALIGN;
992 }
993 #endif
994 txdlen = len - buflen;
995 DPRINTF(MEC_DEBUG_START,
996 ("mec_start: txdaddr = 0x%08llx, txdlen = %d\n",
997 txdaddr, txdlen));
998
999 /*
1000 * sync the DMA map for TX mbuf
1001 *
1002 * XXX unaligned part doesn't have to be sync'ed,
1003 * but it's harmless...
1004 */
1005 bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
1006 dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1007 }
1008
1009 #if NBPFILTER > 0
1010 /*
1011 * Pass packet to bpf if there is a listener.
1012 */
1013 if (ifp->if_bpf)
1014 bpf_mtap(ifp->if_bpf, m0);
1015 #endif
1016
1017 /*
1018 * setup the transmit descriptor.
1019 */
1020
1021 /* TXINT bit will be set later on the last packet */
1022 txd->txd_cmd = (len - 1);
1023 /* but also set TXINT bit on a half of TXDESC */
1024 if (sc->sc_txpending == (MEC_NTXDESC / 2))
1025 txd->txd_cmd |= MEC_TXCMD_TXINT;
1026
1027 if (txs->txs_flags & MEC_TXS_TXDBUF)
1028 txd->txd_cmd |= TXCMD_BUFSTART(MEC_TXDESCSIZE - buflen);
1029 if (txs->txs_flags & MEC_TXS_TXDPTR1) {
1030 txd->txd_cmd |= MEC_TXCMD_PTR1;
1031 txd->txd_ptr[0] = TXPTR_LEN(txdlen - 1) | txdaddr;
1032 /*
1033 * Store a pointer to the packet so we can
1034 * free it later.
1035 */
1036 txs->txs_mbuf = m0;
1037 } else {
1038 txd->txd_ptr[0] = 0;
1039 /*
1040 * In this case all data are copied to buffer in txdesc,
1041 * we can free TX mbuf here.
1042 */
1043 m_freem(m0);
1044 }
1045
1046 DPRINTF(MEC_DEBUG_START,
1047 ("mec_start: txd_cmd = 0x%016llx, txd_ptr = 0x%016llx\n",
1048 txd->txd_cmd, txd->txd_ptr[0]));
1049 DPRINTF(MEC_DEBUG_START,
1050 ("mec_start: len = %d (0x%04x), buflen = %d (0x%02x)\n",
1051 len, len, buflen, buflen));
1052
1053 /* sync TX descriptor */
1054 MEC_TXDESCSYNC(sc, nexttx,
1055 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1056
1057 /* advance the TX pointer. */
1058 sc->sc_txpending++;
1059 sc->sc_txlast = nexttx;
1060 }
1061
1062 if (sc->sc_txpending == MEC_NTXDESC) {
1063 /* No more slots; notify upper layer. */
1064 ifp->if_flags |= IFF_OACTIVE;
1065 }
1066
1067 if (sc->sc_txpending != opending) {
1068 /*
1069 * Cause a TX interrupt to happen on the last packet
1070 * we enqueued.
1071 */
1072 sc->sc_txdesc[sc->sc_txlast].txd_cmd |= MEC_TXCMD_TXINT;
1073 MEC_TXCMDSYNC(sc, sc->sc_txlast,
1074 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1075
1076 /* start TX */
1077 bus_space_write_8(st, sh, MEC_TX_RING_PTR,
1078 MEC_NEXTTX(sc->sc_txlast));
1079
1080 /*
1081 * If the transmitter was idle,
1082 * reset the txdirty pointer and re-enable TX interrupt.
1083 */
1084 if (opending == 0) {
1085 sc->sc_txdirty = firsttx;
1086 bus_space_write_8(st, sh, MEC_TX_ALIAS,
1087 MEC_TX_ALIAS_INT_ENABLE);
1088 }
1089
1090 /* Set a watchdog timer in case the chip flakes out. */
1091 ifp->if_timer = 5;
1092 }
1093 }
1094
1095 STATIC void
1096 mec_stop(struct ifnet *ifp, int disable)
1097 {
1098 struct mec_softc *sc = ifp->if_softc;
1099 struct mec_txsoft *txs;
1100 int i;
1101
1102 DPRINTF(MEC_DEBUG_STOP, ("mec_stop\n"));
1103
1104 ifp->if_timer = 0;
1105 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1106
1107 callout_stop(&sc->sc_tick_ch);
1108 mii_down(&sc->sc_mii);
1109
1110 /* release any TX buffers */
1111 for (i = 0; i < MEC_NTXDESC; i++) {
1112 txs = &sc->sc_txsoft[i];
1113 if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
1114 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1115 m_freem(txs->txs_mbuf);
1116 txs->txs_mbuf = NULL;
1117 }
1118 }
1119 }
1120
1121 STATIC int
1122 mec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1123 {
1124 int s, error;
1125
1126 s = splnet();
1127
1128 error = ether_ioctl(ifp, cmd, data);
1129 if (error == ENETRESET) {
1130 /*
1131 * Multicast list has changed; set the hardware filter
1132 * accordingly.
1133 */
1134 if (ifp->if_flags & IFF_RUNNING)
1135 error = mec_init(ifp);
1136 else
1137 error = 0;
1138 }
1139
1140 /* Try to get more packets going. */
1141 mec_start(ifp);
1142
1143 splx(s);
1144 return error;
1145 }
1146
1147 STATIC void
1148 mec_watchdog(struct ifnet *ifp)
1149 {
1150 struct mec_softc *sc = ifp->if_softc;
1151
1152 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1153 ifp->if_oerrors++;
1154
1155 mec_init(ifp);
1156 }
1157
1158 STATIC void
1159 mec_tick(void *arg)
1160 {
1161 struct mec_softc *sc = arg;
1162 int s;
1163
1164 s = splnet();
1165 mii_tick(&sc->sc_mii);
1166 splx(s);
1167
1168 callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
1169 }
1170
1171 STATIC void
1172 mec_setfilter(struct mec_softc *sc)
1173 {
1174 struct ethercom *ec = &sc->sc_ethercom;
1175 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1176 struct ether_multi *enm;
1177 struct ether_multistep step;
1178 bus_space_tag_t st = sc->sc_st;
1179 bus_space_handle_t sh = sc->sc_sh;
1180 uint64_t mchash;
1181 uint32_t control, hash;
1182 int mcnt;
1183
1184 control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
1185 control &= ~MEC_MAC_FILTER_MASK;
1186
1187 if (ifp->if_flags & IFF_PROMISC) {
1188 control |= MEC_MAC_FILTER_PROMISC;
1189 bus_space_write_8(st, sh, MEC_MULTICAST, 0xffffffffffffffffULL);
1190 bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
1191 return;
1192 }
1193
1194 mcnt = 0;
1195 mchash = 0;
1196 ETHER_FIRST_MULTI(step, ec, enm);
1197 while (enm != NULL) {
1198 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1199 /* set allmulti for a range of multicast addresses */
1200 control |= MEC_MAC_FILTER_ALLMULTI;
1201 bus_space_write_8(st, sh, MEC_MULTICAST,
1202 0xffffffffffffffffULL);
1203 bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
1204 return;
1205 }
1206
1207 #define mec_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
1208
1209 hash = mec_calchash(enm->enm_addrlo);
1210 mchash |= 1 << hash;
1211 mcnt++;
1212 ETHER_NEXT_MULTI(step, enm);
1213 }
1214
1215 ifp->if_flags &= ~IFF_ALLMULTI;
1216
1217 if (mcnt > 0)
1218 control |= MEC_MAC_FILTER_MATCHMULTI;
1219
1220 bus_space_write_8(st, sh, MEC_MULTICAST, mchash);
1221 bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
1222 }
1223
1224 STATIC int
1225 mec_intr(void *arg)
1226 {
1227 struct mec_softc *sc = arg;
1228 bus_space_tag_t st = sc->sc_st;
1229 bus_space_handle_t sh = sc->sc_sh;
1230 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1231 uint32_t statreg, statack, dmac;
1232 int handled, sent;
1233
1234 DPRINTF(MEC_DEBUG_INTR, ("mec_intr: called\n"));
1235
1236 handled = sent = 0;
1237
1238 for (;;) {
1239 statreg = bus_space_read_8(st, sh, MEC_INT_STATUS);
1240
1241 DPRINTF(MEC_DEBUG_INTR,
1242 ("mec_intr: INT_STAT = 0x%08x\n", statreg));
1243
1244 statack = statreg & MEC_INT_STATUS_MASK;
1245 if (statack == 0)
1246 break;
1247 bus_space_write_8(st, sh, MEC_INT_STATUS, statack);
1248
1249 handled = 1;
1250
1251 if (statack &
1252 (MEC_INT_RX_THRESHOLD |
1253 MEC_INT_RX_FIFO_UNDERFLOW)) {
1254 mec_rxintr(sc);
1255 }
1256
1257 dmac = bus_space_read_8(st, sh, MEC_DMA_CONTROL);
1258 DPRINTF(MEC_DEBUG_INTR,
1259 ("mec_intr: DMA_CONT = 0x%08x\n", dmac));
1260
1261 if (statack &
1262 (MEC_INT_TX_EMPTY |
1263 MEC_INT_TX_PACKET_SENT |
1264 MEC_INT_TX_ABORT)) {
1265 mec_txintr(sc);
1266 sent = 1;
1267 if ((statack & MEC_INT_TX_EMPTY) != 0 &&
1268 (dmac & MEC_DMA_TX_INT_ENABLE) != 0) {
1269 /*
1270 * disable TX interrupt to stop
1271 * TX empty interrupt
1272 */
1273 bus_space_write_8(st, sh, MEC_TX_ALIAS, 0);
1274 DPRINTF(MEC_DEBUG_INTR,
1275 ("mec_intr: disable TX_INT\n"));
1276 }
1277 }
1278
1279 if (statack &
1280 (MEC_INT_TX_LINK_FAIL |
1281 MEC_INT_TX_MEM_ERROR |
1282 MEC_INT_TX_ABORT |
1283 MEC_INT_RX_FIFO_UNDERFLOW |
1284 MEC_INT_RX_DMA_UNDERFLOW)) {
1285 printf("%s: mec_intr: interrupt status = 0x%08x\n",
1286 sc->sc_dev.dv_xname, statreg);
1287 }
1288 }
1289
1290 if (sent) {
1291 /* try to get more packets going */
1292 mec_start(ifp);
1293 }
1294
1295 #if NRND > 0
1296 if (handled)
1297 rnd_add_uint32(&sc->sc_rnd_source, statreg);
1298 #endif
1299
1300 return handled;
1301 }
1302
1303 STATIC void
1304 mec_rxintr(struct mec_softc *sc)
1305 {
1306 bus_space_tag_t st = sc->sc_st;
1307 bus_space_handle_t sh = sc->sc_sh;
1308 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1309 struct mbuf *m;
1310 struct mec_rxdesc *rxd;
1311 uint64_t rxstat;
1312 u_int len;
1313 int i;
1314
1315 DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: called\n"));
1316
1317 for (i = sc->sc_rxptr;; i = MEC_NEXTRX(i)) {
1318 rxd = &sc->sc_rxdesc[i];
1319
1320 MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_POSTREAD);
1321 rxstat = rxd->rxd_stat;
1322
1323 DPRINTF(MEC_DEBUG_RXINTR,
1324 ("mec_rxintr: rxstat = 0x%016llx, rxptr = %d\n",
1325 rxstat, i));
1326 DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: rxfifo = 0x%08x\n",
1327 (u_int)bus_space_read_8(st, sh, MEC_RX_FIFO)));
1328
1329 if ((rxstat & MEC_RXSTAT_RECEIVED) == 0) {
1330 MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
1331 break;
1332 }
1333
1334 len = rxstat & MEC_RXSTAT_LEN;
1335
1336 if (len < ETHER_MIN_LEN ||
1337 len > (MCLBYTES - MEC_ETHER_ALIGN)) {
1338 /* invalid length packet; drop it. */
1339 DPRINTF(MEC_DEBUG_RXINTR,
1340 ("mec_rxintr: wrong packet\n"));
1341 dropit:
1342 ifp->if_ierrors++;
1343 rxd->rxd_stat = 0;
1344 MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
1345 bus_space_write_8(st, sh, MEC_MCL_RX_FIFO,
1346 MEC_CDRXADDR(sc, i));
1347 continue;
1348 }
1349
1350 if (rxstat &
1351 (MEC_RXSTAT_BADPACKET |
1352 MEC_RXSTAT_LONGEVENT |
1353 MEC_RXSTAT_INVALID |
1354 MEC_RXSTAT_CRCERROR |
1355 MEC_RXSTAT_VIOLATION)) {
1356 printf("%s: mec_rxintr: status = 0x%016llx\n",
1357 sc->sc_dev.dv_xname, rxstat);
1358 goto dropit;
1359 }
1360
1361 /*
1362 * The MEC includes the CRC with every packet. Trim
1363 * it off here.
1364 */
1365 len -= ETHER_CRC_LEN;
1366
1367 /*
1368 * now allocate an mbuf (and possibly a cluster) to hold
1369 * the received packet.
1370 */
1371 MGETHDR(m, M_DONTWAIT, MT_DATA);
1372 if (m == NULL) {
1373 printf("%s: unable to allocate RX mbuf\n",
1374 sc->sc_dev.dv_xname);
1375 goto dropit;
1376 }
1377 if (len > (MHLEN - MEC_ETHER_ALIGN)) {
1378 MCLGET(m, M_DONTWAIT);
1379 if ((m->m_flags & M_EXT) == 0) {
1380 printf("%s: unable to allocate RX cluster\n",
1381 sc->sc_dev.dv_xname);
1382 m_freem(m);
1383 m = NULL;
1384 goto dropit;
1385 }
1386 }
1387
1388 /*
1389 * Note MEC chip seems to insert 2 byte padding at the top of
1390 * RX buffer, but we copy whole buffer to avoid unaligned copy.
1391 */
1392 MEC_RXBUFSYNC(sc, i, len, BUS_DMASYNC_POSTREAD);
1393 memcpy(mtod(m, void *), rxd->rxd_buf, MEC_ETHER_ALIGN + len);
1394 MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
1395 m->m_data += MEC_ETHER_ALIGN;
1396
1397 /* put RX buffer into FIFO again */
1398 rxd->rxd_stat = 0;
1399 MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
1400 bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
1401
1402 m->m_pkthdr.rcvif = ifp;
1403 m->m_pkthdr.len = m->m_len = len;
1404
1405 ifp->if_ipackets++;
1406
1407 #if NBPFILTER > 0
1408 /*
1409 * Pass this up to any BPF listeners, but only
1410 * pass it up the stack if it's for us.
1411 */
1412 if (ifp->if_bpf)
1413 bpf_mtap(ifp->if_bpf, m);
1414 #endif
1415
1416 /* Pass it on. */
1417 (*ifp->if_input)(ifp, m);
1418 }
1419
1420 /* update RX pointer */
1421 sc->sc_rxptr = i;
1422 }
1423
1424 STATIC void
1425 mec_txintr(struct mec_softc *sc)
1426 {
1427 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1428 struct mec_txdesc *txd;
1429 struct mec_txsoft *txs;
1430 bus_dmamap_t dmamap;
1431 uint64_t txstat;
1432 int i;
1433 u_int col;
1434
1435 ifp->if_flags &= ~IFF_OACTIVE;
1436
1437 DPRINTF(MEC_DEBUG_TXINTR, ("mec_txintr: called\n"));
1438
1439 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1440 i = MEC_NEXTTX(i), sc->sc_txpending--) {
1441 txd = &sc->sc_txdesc[i];
1442
1443 MEC_TXDESCSYNC(sc, i,
1444 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1445
1446 txstat = txd->txd_stat;
1447 DPRINTF(MEC_DEBUG_TXINTR,
1448 ("mec_txintr: dirty = %d, txstat = 0x%016llx\n",
1449 i, txstat));
1450 if ((txstat & MEC_TXSTAT_SENT) == 0) {
1451 MEC_TXCMDSYNC(sc, i, BUS_DMASYNC_PREREAD);
1452 break;
1453 }
1454
1455 if ((txstat & MEC_TXSTAT_SUCCESS) == 0) {
1456 printf("%s: TX error: txstat = 0x%016llx\n",
1457 sc->sc_dev.dv_xname, txstat);
1458 ifp->if_oerrors++;
1459 continue;
1460 }
1461
1462 txs = &sc->sc_txsoft[i];
1463 if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
1464 dmamap = txs->txs_dmamap;
1465 bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
1466 dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1467 bus_dmamap_unload(sc->sc_dmat, dmamap);
1468 m_freem(txs->txs_mbuf);
1469 txs->txs_mbuf = NULL;
1470 }
1471
1472 col = (txstat & MEC_TXSTAT_COLCNT) >> MEC_TXSTAT_COLCNT_SHIFT;
1473 ifp->if_collisions += col;
1474 ifp->if_opackets++;
1475 }
1476
1477 /* update the dirty TX buffer pointer */
1478 sc->sc_txdirty = i;
1479 DPRINTF(MEC_DEBUG_INTR,
1480 ("mec_txintr: sc_txdirty = %2d, sc_txpending = %2d\n",
1481 sc->sc_txdirty, sc->sc_txpending));
1482
1483 /* cancel the watchdog timer if there are no pending TX packets */
1484 if (sc->sc_txpending == 0)
1485 ifp->if_timer = 0;
1486 }
1487
1488 STATIC void
1489 mec_shutdown(void *arg)
1490 {
1491 struct mec_softc *sc = arg;
1492
1493 mec_stop(&sc->sc_ethercom.ec_if, 1);
1494 /* make sure to stop DMA etc. */
1495 mec_reset(sc);
1496 }
1497