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