]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ffec/if_ffec.c
if_ffec bugfixes related to harvesting of hardware-maintained statistics...
[FreeBSD/FreeBSD.git] / sys / dev / ffec / if_ffec.c
1 /*-
2  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Driver for Freescale Fast Ethernet Controller, found on imx-series SoCs among
33  * others.  Also works for the ENET Gigibit controller found on imx6 and imx28,
34  * but the driver doesn't currently use any of the ENET advanced features other
35  * than enabling gigabit.
36  *
37  * The interface name 'fec' is already taken by netgraph's Fast Etherchannel
38  * (netgraph/ng_fec.c), so we use 'ffec'.
39  *
40  * Requires an FDT entry with at least these properties:
41  *   fec: ethernet@02188000 {
42  *      compatible = "fsl,imxNN-fec";
43  *      reg = <0x02188000 0x4000>;
44  *      interrupts = <150 151>;
45  *      phy-mode = "rgmii";
46  *      phy-disable-preamble; // optional
47  *   };
48  * The second interrupt number is for IEEE-1588, and is not currently used; it
49  * need not be present.  phy-mode must be one of: "mii", "rmii", "rgmii".
50  * There is also an optional property, phy-disable-preamble, which if present
51  * will disable the preamble bits, cutting the size of each mdio transaction
52  * (and thus the busy-wait time) in half.
53  */
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/bus.h>
58 #include <sys/endian.h>
59 #include <sys/kernel.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/mbuf.h>
63 #include <sys/module.h>
64 #include <sys/mutex.h>
65 #include <sys/rman.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
68 #include <sys/sysctl.h>
69
70 #include <machine/bus.h>
71
72 #include <net/bpf.h>
73 #include <net/if.h>
74 #include <net/ethernet.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/if_vlan_var.h>
80
81 #include <dev/fdt/fdt_common.h>
82 #include <dev/ffec/if_ffecreg.h>
83 #include <dev/ofw/ofw_bus.h>
84 #include <dev/ofw/ofw_bus_subr.h>
85 #include <dev/mii/mii.h>
86 #include <dev/mii/miivar.h>
87 #include "miibus_if.h"
88
89 /*
90  * There are small differences in the hardware on various SoCs.  Not every SoC
91  * we support has its own FECTYPE; most work as GENERIC and only the ones that
92  * need different handling get their own entry.  In addition to the types in
93  * this list, there are some flags below that can be ORed into the upper bits.
94  */
95 enum {
96         FECTYPE_NONE,
97         FECTYPE_GENERIC,
98         FECTYPE_IMX53,
99         FECTYPE_IMX6,
100         FECTYPE_MVF,
101 };
102
103 /*
104  * Flags that describe general differences between the FEC hardware in various
105  * SoCs.  These are ORed into the FECTYPE enum values.
106  */
107 #define FECTYPE_MASK            0x0000ffff
108 #define FECFLAG_GBE             (0x0001 << 16)
109
110 /*
111  * Table of supported FDT compat strings and their associated FECTYPE values.
112  */
113 static struct ofw_compat_data compat_data[] = {
114         {"fsl,imx51-fec",       FECTYPE_GENERIC},
115         {"fsl,imx53-fec",       FECTYPE_IMX53},
116         {"fsl,imx6q-fec",       FECTYPE_IMX6 | FECFLAG_GBE},
117         {"fsl,imx6ul-fec",      FECTYPE_IMX6},
118         {"fsl,mvf600-fec",      FECTYPE_MVF},
119         {"fsl,mvf-fec",         FECTYPE_MVF},
120         {NULL,                  FECTYPE_NONE},
121 };
122
123 /*
124  * Driver data and defines.
125  */
126 #define RX_DESC_COUNT   64
127 #define RX_DESC_SIZE    (sizeof(struct ffec_hwdesc) * RX_DESC_COUNT)
128 #define TX_DESC_COUNT   64
129 #define TX_DESC_SIZE    (sizeof(struct ffec_hwdesc) * TX_DESC_COUNT)
130
131 #define WATCHDOG_TIMEOUT_SECS   5
132
133 struct ffec_bufmap {
134         struct mbuf     *mbuf;
135         bus_dmamap_t    map;
136 };
137
138 enum {
139         PHY_CONN_UNKNOWN,
140         PHY_CONN_MII,
141         PHY_CONN_RMII,
142         PHY_CONN_RGMII
143 };
144
145 struct ffec_softc {
146         device_t                dev;
147         device_t                miibus;
148         struct mii_data *       mii_softc;
149         struct ifnet            *ifp;
150         int                     if_flags;
151         struct mtx              mtx;
152         struct resource         *irq_res;
153         struct resource         *mem_res;
154         void *                  intr_cookie;
155         struct callout          ffec_callout;
156         uint8_t                 phy_conn_type;
157         uint8_t                 fectype;
158         boolean_t               link_is_up;
159         boolean_t               is_attached;
160         boolean_t               is_detaching;
161         int                     tx_watchdog_count;
162
163         bus_dma_tag_t           rxdesc_tag;
164         bus_dmamap_t            rxdesc_map;
165         struct ffec_hwdesc      *rxdesc_ring;
166         bus_addr_t              rxdesc_ring_paddr;
167         bus_dma_tag_t           rxbuf_tag;
168         struct ffec_bufmap      rxbuf_map[RX_DESC_COUNT];
169         uint32_t                rx_idx;
170
171         bus_dma_tag_t           txdesc_tag;
172         bus_dmamap_t            txdesc_map;
173         struct ffec_hwdesc      *txdesc_ring;
174         bus_addr_t              txdesc_ring_paddr;
175         bus_dma_tag_t           txbuf_tag;
176         struct ffec_bufmap      txbuf_map[TX_DESC_COUNT];
177         uint32_t                tx_idx_head;
178         uint32_t                tx_idx_tail;
179         int                     txcount;
180 };
181
182 #define FFEC_LOCK(sc)                   mtx_lock(&(sc)->mtx)
183 #define FFEC_UNLOCK(sc)                 mtx_unlock(&(sc)->mtx)
184 #define FFEC_LOCK_INIT(sc)              mtx_init(&(sc)->mtx, \
185             device_get_nameunit((sc)->dev), MTX_NETWORK_LOCK, MTX_DEF)
186 #define FFEC_LOCK_DESTROY(sc)           mtx_destroy(&(sc)->mtx);
187 #define FFEC_ASSERT_LOCKED(sc)          mtx_assert(&(sc)->mtx, MA_OWNED);
188 #define FFEC_ASSERT_UNLOCKED(sc)        mtx_assert(&(sc)->mtx, MA_NOTOWNED);
189
190 static void ffec_init_locked(struct ffec_softc *sc);
191 static void ffec_stop_locked(struct ffec_softc *sc);
192 static void ffec_txstart_locked(struct ffec_softc *sc);
193 static void ffec_txfinish_locked(struct ffec_softc *sc);
194
195 static inline uint16_t
196 RD2(struct ffec_softc *sc, bus_size_t off)
197 {
198
199         return (bus_read_2(sc->mem_res, off));
200 }
201
202 static inline void
203 WR2(struct ffec_softc *sc, bus_size_t off, uint16_t val)
204 {
205
206         bus_write_2(sc->mem_res, off, val);
207 }
208
209 static inline uint32_t
210 RD4(struct ffec_softc *sc, bus_size_t off)
211 {
212
213         return (bus_read_4(sc->mem_res, off));
214 }
215
216 static inline void
217 WR4(struct ffec_softc *sc, bus_size_t off, uint32_t val)
218 {
219
220         bus_write_4(sc->mem_res, off, val);
221 }
222
223 static inline uint32_t
224 next_rxidx(struct ffec_softc *sc, uint32_t curidx)
225 {
226
227         return ((curidx == RX_DESC_COUNT - 1) ? 0 : curidx + 1);
228 }
229
230 static inline uint32_t
231 next_txidx(struct ffec_softc *sc, uint32_t curidx)
232 {
233
234         return ((curidx == TX_DESC_COUNT - 1) ? 0 : curidx + 1);
235 }
236
237 static void
238 ffec_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
239 {
240
241         if (error != 0)
242                 return;
243         *(bus_addr_t *)arg = segs[0].ds_addr;
244 }
245
246 static void
247 ffec_miigasket_setup(struct ffec_softc *sc)
248 {
249         uint32_t ifmode;
250
251         /*
252          * We only need the gasket for MII and RMII connections on certain SoCs.
253          */
254
255         switch (sc->fectype & FECTYPE_MASK)
256         {
257         case FECTYPE_IMX53:
258                 break;
259         default:
260                 return;
261         }
262
263         switch (sc->phy_conn_type)
264         {
265         case PHY_CONN_MII:
266                 ifmode = 0;
267                 break;
268         case PHY_CONN_RMII:
269                 ifmode = FEC_MIIGSK_CFGR_IF_MODE_RMII;
270                 break;
271         default:
272                 return;
273         }
274
275         /*
276          * Disable the gasket, configure for either MII or RMII, then enable.
277          */
278
279         WR2(sc, FEC_MIIGSK_ENR, 0);
280         while (RD2(sc, FEC_MIIGSK_ENR) & FEC_MIIGSK_ENR_READY)
281                 continue;
282
283         WR2(sc, FEC_MIIGSK_CFGR, ifmode);
284
285         WR2(sc, FEC_MIIGSK_ENR, FEC_MIIGSK_ENR_EN);
286         while (!(RD2(sc, FEC_MIIGSK_ENR) & FEC_MIIGSK_ENR_READY))
287                 continue;
288 }
289
290 static boolean_t
291 ffec_miibus_iowait(struct ffec_softc *sc)
292 {
293         uint32_t timeout;
294
295         for (timeout = 10000; timeout != 0; --timeout)
296                 if (RD4(sc, FEC_IER_REG) & FEC_IER_MII)
297                         return (true);
298
299         return (false);
300 }
301
302 static int
303 ffec_miibus_readreg(device_t dev, int phy, int reg)
304 {
305         struct ffec_softc *sc;
306         int val;
307
308         sc = device_get_softc(dev);
309
310         WR4(sc, FEC_IER_REG, FEC_IER_MII);
311
312         WR4(sc, FEC_MMFR_REG, FEC_MMFR_OP_READ |
313             FEC_MMFR_ST_VALUE | FEC_MMFR_TA_VALUE |
314             ((phy << FEC_MMFR_PA_SHIFT) & FEC_MMFR_PA_MASK) |
315             ((reg << FEC_MMFR_RA_SHIFT) & FEC_MMFR_RA_MASK));
316
317         if (!ffec_miibus_iowait(sc)) {
318                 device_printf(dev, "timeout waiting for mii read\n");
319                 return (-1); /* All-ones is a symptom of bad mdio. */
320         }
321
322         val = RD4(sc, FEC_MMFR_REG) & FEC_MMFR_DATA_MASK;
323
324         return (val);
325 }
326
327 static int
328 ffec_miibus_writereg(device_t dev, int phy, int reg, int val)
329 {
330         struct ffec_softc *sc;
331
332         sc = device_get_softc(dev);
333
334         WR4(sc, FEC_IER_REG, FEC_IER_MII);
335
336         WR4(sc, FEC_MMFR_REG, FEC_MMFR_OP_WRITE |
337             FEC_MMFR_ST_VALUE | FEC_MMFR_TA_VALUE |
338             ((phy << FEC_MMFR_PA_SHIFT) & FEC_MMFR_PA_MASK) |
339             ((reg << FEC_MMFR_RA_SHIFT) & FEC_MMFR_RA_MASK) |
340             (val & FEC_MMFR_DATA_MASK));
341
342         if (!ffec_miibus_iowait(sc)) {
343                 device_printf(dev, "timeout waiting for mii write\n");
344                 return (-1);
345         }
346
347         return (0);
348 }
349
350 static void
351 ffec_miibus_statchg(device_t dev)
352 {
353         struct ffec_softc *sc;
354         struct mii_data *mii;
355         uint32_t ecr, rcr, tcr;
356
357         /*
358          * Called by the MII bus driver when the PHY establishes link to set the
359          * MAC interface registers.
360          */
361
362         sc = device_get_softc(dev);
363
364         FFEC_ASSERT_LOCKED(sc);
365
366         mii = sc->mii_softc;
367
368         if (mii->mii_media_status & IFM_ACTIVE)
369                 sc->link_is_up = true;
370         else
371                 sc->link_is_up = false;
372
373         ecr = RD4(sc, FEC_ECR_REG) & ~FEC_ECR_SPEED;
374         rcr = RD4(sc, FEC_RCR_REG) & ~(FEC_RCR_RMII_10T | FEC_RCR_RMII_MODE |
375             FEC_RCR_RGMII_EN | FEC_RCR_DRT | FEC_RCR_FCE);
376         tcr = RD4(sc, FEC_TCR_REG) & ~FEC_TCR_FDEN;
377
378         rcr |= FEC_RCR_MII_MODE; /* Must always be on even for R[G]MII. */
379         switch (sc->phy_conn_type) {
380         case PHY_CONN_MII:
381                 break;
382         case PHY_CONN_RMII:
383                 rcr |= FEC_RCR_RMII_MODE;
384                 break;
385         case PHY_CONN_RGMII:
386                 rcr |= FEC_RCR_RGMII_EN;
387                 break;
388         }
389
390         switch (IFM_SUBTYPE(mii->mii_media_active)) {
391         case IFM_1000_T:
392         case IFM_1000_SX:
393                 ecr |= FEC_ECR_SPEED;
394                 break;
395         case IFM_100_TX:
396                 /* Not-FEC_ECR_SPEED + not-FEC_RCR_RMII_10T means 100TX */
397                 break;
398         case IFM_10_T:
399                 rcr |= FEC_RCR_RMII_10T;
400                 break;
401         case IFM_NONE:
402                 sc->link_is_up = false;
403                 return;
404         default:
405                 sc->link_is_up = false;
406                 device_printf(dev, "Unsupported media %u\n",
407                     IFM_SUBTYPE(mii->mii_media_active));
408                 return;
409         }
410
411         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
412                 tcr |= FEC_TCR_FDEN;
413         else
414                 rcr |= FEC_RCR_DRT;
415
416         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FLOW) != 0)
417                 rcr |= FEC_RCR_FCE;
418
419         WR4(sc, FEC_RCR_REG, rcr);
420         WR4(sc, FEC_TCR_REG, tcr);
421         WR4(sc, FEC_ECR_REG, ecr);
422 }
423
424 static void
425 ffec_media_status(struct ifnet * ifp, struct ifmediareq *ifmr)
426 {
427         struct ffec_softc *sc;
428         struct mii_data *mii;
429
430
431         sc = ifp->if_softc;
432         mii = sc->mii_softc;
433         FFEC_LOCK(sc);
434         mii_pollstat(mii);
435         ifmr->ifm_active = mii->mii_media_active;
436         ifmr->ifm_status = mii->mii_media_status;
437         FFEC_UNLOCK(sc);
438 }
439
440 static int
441 ffec_media_change_locked(struct ffec_softc *sc)
442 {
443
444         return (mii_mediachg(sc->mii_softc));
445 }
446
447 static int
448 ffec_media_change(struct ifnet * ifp)
449 {
450         struct ffec_softc *sc;
451         int error;
452
453         sc = ifp->if_softc;
454
455         FFEC_LOCK(sc);
456         error = ffec_media_change_locked(sc);
457         FFEC_UNLOCK(sc);
458         return (error);
459 }
460
461 static void ffec_clear_stats(struct ffec_softc *sc)
462 {
463         uint32_t mibc;
464
465         mibc = RD4(sc, FEC_MIBC_REG);
466
467         /*
468          * On newer hardware the statistic regs are cleared by toggling a bit in
469          * the mib control register.  On older hardware the clear procedure is
470          * to disable statistics collection, zero the regs, then re-enable.
471          */
472         if (sc->fectype == FECTYPE_IMX6 || sc->fectype == FECTYPE_MVF) {
473                 WR4(sc, FEC_MIBC_REG, mibc | FEC_MIBC_CLEAR);
474                 WR4(sc, FEC_MIBC_REG, mibc & ~FEC_MIBC_CLEAR);
475         } else {
476                 WR4(sc, FEC_MIBC_REG, mibc | FEC_MIBC_DIS);
477         
478                 WR4(sc, FEC_IEEE_R_DROP, 0);
479                 WR4(sc, FEC_IEEE_R_MACERR, 0);
480                 WR4(sc, FEC_RMON_R_CRC_ALIGN, 0);
481                 WR4(sc, FEC_RMON_R_FRAG, 0);
482                 WR4(sc, FEC_RMON_R_JAB, 0);
483                 WR4(sc, FEC_RMON_R_MC_PKT, 0);
484                 WR4(sc, FEC_RMON_R_OVERSIZE, 0);
485                 WR4(sc, FEC_RMON_R_PACKETS, 0);
486                 WR4(sc, FEC_RMON_R_UNDERSIZE, 0);
487                 WR4(sc, FEC_RMON_T_COL, 0);
488                 WR4(sc, FEC_RMON_T_CRC_ALIGN, 0);
489                 WR4(sc, FEC_RMON_T_FRAG, 0);
490                 WR4(sc, FEC_RMON_T_JAB, 0);
491                 WR4(sc, FEC_RMON_T_MC_PKT, 0);
492                 WR4(sc, FEC_RMON_T_OVERSIZE , 0);
493                 WR4(sc, FEC_RMON_T_PACKETS, 0);
494                 WR4(sc, FEC_RMON_T_UNDERSIZE, 0);
495
496                 WR4(sc, FEC_MIBC_REG, mibc);
497         }
498 }
499
500 static void
501 ffec_harvest_stats(struct ffec_softc *sc)
502 {
503         struct ifnet *ifp;
504
505         ifp = sc->ifp;
506
507         /*
508          * - FEC_IEEE_R_DROP is "dropped due to invalid start frame delimiter"
509          *   so it's really just another type of input error.
510          * - FEC_IEEE_R_MACERR is "no receive fifo space"; count as input drops.
511          */
512         if_inc_counter(ifp, IFCOUNTER_IPACKETS, RD4(sc, FEC_RMON_R_PACKETS));
513         if_inc_counter(ifp, IFCOUNTER_IMCASTS, RD4(sc, FEC_RMON_R_MC_PKT));
514         if_inc_counter(ifp, IFCOUNTER_IERRORS,
515             RD4(sc, FEC_RMON_R_CRC_ALIGN) + RD4(sc, FEC_RMON_R_UNDERSIZE) +
516             RD4(sc, FEC_RMON_R_OVERSIZE) + RD4(sc, FEC_RMON_R_FRAG) +
517             RD4(sc, FEC_RMON_R_JAB) + RD4(sc, FEC_IEEE_R_DROP));
518
519         if_inc_counter(ifp, IFCOUNTER_IQDROPS, RD4(sc, FEC_IEEE_R_MACERR));
520
521         if_inc_counter(ifp, IFCOUNTER_OPACKETS, RD4(sc, FEC_RMON_T_PACKETS));
522         if_inc_counter(ifp, IFCOUNTER_OMCASTS, RD4(sc, FEC_RMON_T_MC_PKT));
523         if_inc_counter(ifp, IFCOUNTER_OERRORS,
524             RD4(sc, FEC_RMON_T_CRC_ALIGN) + RD4(sc, FEC_RMON_T_UNDERSIZE) +
525             RD4(sc, FEC_RMON_T_OVERSIZE) + RD4(sc, FEC_RMON_T_FRAG) +
526             RD4(sc, FEC_RMON_T_JAB));
527
528         if_inc_counter(ifp, IFCOUNTER_COLLISIONS, RD4(sc, FEC_RMON_T_COL));
529
530         ffec_clear_stats(sc);
531 }
532
533 static void
534 ffec_tick(void *arg)
535 {
536         struct ffec_softc *sc;
537         struct ifnet *ifp;
538         int link_was_up;
539
540         sc = arg;
541
542         FFEC_ASSERT_LOCKED(sc);
543
544         ifp = sc->ifp;
545
546         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
547             return;
548
549         /*
550          * Typical tx watchdog.  If this fires it indicates that we enqueued
551          * packets for output and never got a txdone interrupt for them.  Maybe
552          * it's a missed interrupt somehow, just pretend we got one.
553          */
554         if (sc->tx_watchdog_count > 0) {
555                 if (--sc->tx_watchdog_count == 0) {
556                         ffec_txfinish_locked(sc);
557                 }
558         }
559
560         /* Gather stats from hardware counters. */
561         ffec_harvest_stats(sc);
562
563         /* Check the media status. */
564         link_was_up = sc->link_is_up;
565         mii_tick(sc->mii_softc);
566         if (sc->link_is_up && !link_was_up)
567                 ffec_txstart_locked(sc);
568
569         /* Schedule another check one second from now. */
570         callout_reset(&sc->ffec_callout, hz, ffec_tick, sc);
571 }
572
573 inline static uint32_t
574 ffec_setup_txdesc(struct ffec_softc *sc, int idx, bus_addr_t paddr, 
575     uint32_t len)
576 {
577         uint32_t nidx;
578         uint32_t flags;
579
580         nidx = next_txidx(sc, idx);
581
582         /* Addr/len 0 means we're clearing the descriptor after xmit done. */
583         if (paddr == 0 || len == 0) {
584                 flags = 0;
585                 --sc->txcount;
586         } else {
587                 flags = FEC_TXDESC_READY | FEC_TXDESC_L | FEC_TXDESC_TC;
588                 ++sc->txcount;
589         }
590         if (nidx == 0)
591                 flags |= FEC_TXDESC_WRAP;
592
593         /*
594          * The hardware requires 32-bit physical addresses.  We set up the dma
595          * tag to indicate that, so the cast to uint32_t should never lose
596          * significant bits.
597          */
598         sc->txdesc_ring[idx].buf_paddr = (uint32_t)paddr;
599         sc->txdesc_ring[idx].flags_len = flags | len; /* Must be set last! */
600
601         return (nidx);
602 }
603
604 static int
605 ffec_setup_txbuf(struct ffec_softc *sc, int idx, struct mbuf **mp)
606 {
607         struct mbuf * m;
608         int error, nsegs;
609         struct bus_dma_segment seg;
610
611         if ((m = m_defrag(*mp, M_NOWAIT)) == NULL)
612                 return (ENOMEM);
613         *mp = m;
614
615         error = bus_dmamap_load_mbuf_sg(sc->txbuf_tag, sc->txbuf_map[idx].map,
616             m, &seg, &nsegs, 0);
617         if (error != 0) {
618                 return (ENOMEM);
619         }
620         bus_dmamap_sync(sc->txbuf_tag, sc->txbuf_map[idx].map, 
621             BUS_DMASYNC_PREWRITE);
622
623         sc->txbuf_map[idx].mbuf = m;
624         ffec_setup_txdesc(sc, idx, seg.ds_addr, seg.ds_len);
625
626         return (0);
627
628 }
629
630 static void
631 ffec_txstart_locked(struct ffec_softc *sc)
632 {
633         struct ifnet *ifp;
634         struct mbuf *m;
635         int enqueued;
636
637         FFEC_ASSERT_LOCKED(sc);
638
639         if (!sc->link_is_up)
640                 return;
641
642         ifp = sc->ifp;
643
644         if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
645                 return;
646
647         enqueued = 0;
648
649         for (;;) {
650                 if (sc->txcount == (TX_DESC_COUNT-1)) {
651                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
652                         break;
653                 }
654                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
655                 if (m == NULL)
656                         break;
657                 if (ffec_setup_txbuf(sc, sc->tx_idx_head, &m) != 0) {
658                         IFQ_DRV_PREPEND(&ifp->if_snd, m);
659                         break;
660                 }
661                 BPF_MTAP(ifp, m);
662                 sc->tx_idx_head = next_txidx(sc, sc->tx_idx_head);
663                 ++enqueued;
664         }
665
666         if (enqueued != 0) {
667                 bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_PREWRITE);
668                 WR4(sc, FEC_TDAR_REG, FEC_TDAR_TDAR);
669                 bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_POSTWRITE);
670                 sc->tx_watchdog_count = WATCHDOG_TIMEOUT_SECS;
671         }
672 }
673
674 static void
675 ffec_txstart(struct ifnet *ifp)
676 {
677         struct ffec_softc *sc = ifp->if_softc;
678
679         FFEC_LOCK(sc);
680         ffec_txstart_locked(sc);
681         FFEC_UNLOCK(sc);
682 }
683
684 static void
685 ffec_txfinish_locked(struct ffec_softc *sc)
686 {
687         struct ifnet *ifp;
688         struct ffec_hwdesc *desc;
689         struct ffec_bufmap *bmap;
690         boolean_t retired_buffer;
691
692         FFEC_ASSERT_LOCKED(sc);
693
694         /* XXX Can't set PRE|POST right now, but we need both. */
695         bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_PREREAD);
696         bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_POSTREAD);
697         ifp = sc->ifp;
698         retired_buffer = false;
699         while (sc->tx_idx_tail != sc->tx_idx_head) {
700                 desc = &sc->txdesc_ring[sc->tx_idx_tail];
701                 if (desc->flags_len & FEC_TXDESC_READY)
702                         break;
703                 retired_buffer = true;
704                 bmap = &sc->txbuf_map[sc->tx_idx_tail];
705                 bus_dmamap_sync(sc->txbuf_tag, bmap->map, 
706                     BUS_DMASYNC_POSTWRITE);
707                 bus_dmamap_unload(sc->txbuf_tag, bmap->map);
708                 m_freem(bmap->mbuf);
709                 bmap->mbuf = NULL;
710                 ffec_setup_txdesc(sc, sc->tx_idx_tail, 0, 0);
711                 sc->tx_idx_tail = next_txidx(sc, sc->tx_idx_tail);
712         }
713
714         /*
715          * If we retired any buffers, there will be open tx slots available in
716          * the descriptor ring, go try to start some new output.
717          */
718         if (retired_buffer) {
719                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
720                 ffec_txstart_locked(sc);
721         }
722
723         /* If there are no buffers outstanding, muzzle the watchdog. */
724         if (sc->tx_idx_tail == sc->tx_idx_head) {
725                 sc->tx_watchdog_count = 0;
726         }
727 }
728
729 inline static uint32_t
730 ffec_setup_rxdesc(struct ffec_softc *sc, int idx, bus_addr_t paddr)
731 {
732         uint32_t nidx;
733
734         /*
735          * The hardware requires 32-bit physical addresses.  We set up the dma
736          * tag to indicate that, so the cast to uint32_t should never lose
737          * significant bits.
738          */
739         nidx = next_rxidx(sc, idx);
740         sc->rxdesc_ring[idx].buf_paddr = (uint32_t)paddr;
741         sc->rxdesc_ring[idx].flags_len = FEC_RXDESC_EMPTY | 
742                 ((nidx == 0) ? FEC_RXDESC_WRAP : 0);
743
744         return (nidx);
745 }
746
747 static int
748 ffec_setup_rxbuf(struct ffec_softc *sc, int idx, struct mbuf * m)
749 {
750         int error, nsegs;
751         struct bus_dma_segment seg;
752
753         /*
754          * We need to leave at least ETHER_ALIGN bytes free at the beginning of
755          * the buffer to allow the data to be re-aligned after receiving it (by
756          * copying it backwards ETHER_ALIGN bytes in the same buffer).  We also
757          * have to ensure that the beginning of the buffer is aligned to the
758          * hardware's requirements.
759          */
760         m_adj(m, roundup(ETHER_ALIGN, FEC_RXBUF_ALIGN));
761
762         error = bus_dmamap_load_mbuf_sg(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
763             m, &seg, &nsegs, 0);
764         if (error != 0) {
765                 return (error);
766         }
767
768         bus_dmamap_sync(sc->rxbuf_tag, sc->rxbuf_map[idx].map, 
769             BUS_DMASYNC_PREREAD);
770
771         sc->rxbuf_map[idx].mbuf = m;
772         ffec_setup_rxdesc(sc, idx, seg.ds_addr);
773         
774         return (0);
775 }
776
777 static struct mbuf *
778 ffec_alloc_mbufcl(struct ffec_softc *sc)
779 {
780         struct mbuf *m;
781
782         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
783         m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
784
785         return (m);
786 }
787
788 static void
789 ffec_rxfinish_onebuf(struct ffec_softc *sc, int len)
790 {
791         struct mbuf *m, *newmbuf;
792         struct ffec_bufmap *bmap;
793         uint8_t *dst, *src;
794         int error;
795
796         /*
797          *  First try to get a new mbuf to plug into this slot in the rx ring.
798          *  If that fails, drop the current packet and recycle the current
799          *  mbuf, which is still mapped and loaded.
800          */
801         if ((newmbuf = ffec_alloc_mbufcl(sc)) == NULL) {
802                 if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, 1);
803                 ffec_setup_rxdesc(sc, sc->rx_idx, 
804                     sc->rxdesc_ring[sc->rx_idx].buf_paddr);
805                 return;
806         }
807
808         /*
809          *  Unfortunately, the protocol headers need to be aligned on a 32-bit
810          *  boundary for the upper layers.  The hardware requires receive
811          *  buffers to be 16-byte aligned.  The ethernet header is 14 bytes,
812          *  leaving the protocol header unaligned.  We used m_adj() after
813          *  allocating the buffer to leave empty space at the start of the
814          *  buffer, now we'll use the alignment agnostic bcopy() routine to
815          *  shuffle all the data backwards 2 bytes and adjust m_data.
816          *
817          *  XXX imx6 hardware is able to do this 2-byte alignment by setting the
818          *  SHIFT16 bit in the RACC register.  Older hardware doesn't have that
819          *  feature, but for them could we speed this up by copying just the
820          *  protocol headers into their own small mbuf then chaining the cluster
821          *  to it?  That way we'd only need to copy like 64 bytes or whatever
822          *  the biggest header is, instead of the whole 1530ish-byte frame.
823          */
824
825         FFEC_UNLOCK(sc);
826
827         bmap = &sc->rxbuf_map[sc->rx_idx];
828         len -= ETHER_CRC_LEN;
829         bus_dmamap_sync(sc->rxbuf_tag, bmap->map, BUS_DMASYNC_POSTREAD);
830         bus_dmamap_unload(sc->rxbuf_tag, bmap->map);
831         m = bmap->mbuf;
832         bmap->mbuf = NULL;
833         m->m_len = len;
834         m->m_pkthdr.len = len;
835         m->m_pkthdr.rcvif = sc->ifp;
836
837         src = mtod(m, uint8_t*);
838         dst = src - ETHER_ALIGN;
839         bcopy(src, dst, len);
840         m->m_data = dst;
841         sc->ifp->if_input(sc->ifp, m);
842
843         FFEC_LOCK(sc);
844
845         if ((error = ffec_setup_rxbuf(sc, sc->rx_idx, newmbuf)) != 0) {
846                 device_printf(sc->dev, "ffec_setup_rxbuf error %d\n", error);
847                 /* XXX Now what?  We've got a hole in the rx ring. */
848         }
849
850 }
851
852 static void
853 ffec_rxfinish_locked(struct ffec_softc *sc)
854 {
855         struct ffec_hwdesc *desc;
856         int len;
857         boolean_t produced_empty_buffer;
858
859         FFEC_ASSERT_LOCKED(sc);
860
861         /* XXX Can't set PRE|POST right now, but we need both. */
862         bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_PREREAD);
863         bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_POSTREAD);
864         produced_empty_buffer = false;
865         for (;;) {
866                 desc = &sc->rxdesc_ring[sc->rx_idx];
867                 if (desc->flags_len & FEC_RXDESC_EMPTY)
868                         break;
869                 produced_empty_buffer = true;
870                 len = (desc->flags_len & FEC_RXDESC_LEN_MASK);
871                 if (len < 64) {
872                         /*
873                          * Just recycle the descriptor and continue.           .
874                          */
875                         ffec_setup_rxdesc(sc, sc->rx_idx,
876                             sc->rxdesc_ring[sc->rx_idx].buf_paddr);
877                 } else if ((desc->flags_len & FEC_RXDESC_L) == 0) {
878                         /*
879                          * The entire frame is not in this buffer.  Impossible.
880                          * Recycle the descriptor and continue.
881                          *
882                          * XXX what's the right way to handle this? Probably we
883                          * should stop/init the hardware because this should
884                          * just really never happen when we have buffers bigger
885                          * than the maximum frame size.
886                          */
887                         device_printf(sc->dev, 
888                             "fec_rxfinish: received frame without LAST bit set");
889                         ffec_setup_rxdesc(sc, sc->rx_idx, 
890                             sc->rxdesc_ring[sc->rx_idx].buf_paddr);
891                 } else if (desc->flags_len & FEC_RXDESC_ERROR_BITS) {
892                         /*
893                          *  Something went wrong with receiving the frame, we
894                          *  don't care what (the hardware has counted the error
895                          *  in the stats registers already), we just reuse the
896                          *  same mbuf, which is still dma-mapped, by resetting
897                          *  the rx descriptor.
898                          */
899                         ffec_setup_rxdesc(sc, sc->rx_idx, 
900                             sc->rxdesc_ring[sc->rx_idx].buf_paddr);
901                 } else {
902                         /*
903                          *  Normal case: a good frame all in one buffer.
904                          */
905                         ffec_rxfinish_onebuf(sc, len);
906                 }
907                 sc->rx_idx = next_rxidx(sc, sc->rx_idx);
908         }
909
910         if (produced_empty_buffer) {
911                 bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_PREWRITE);
912                 WR4(sc, FEC_RDAR_REG, FEC_RDAR_RDAR);
913                 bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_POSTWRITE);
914         }
915 }
916
917 static void
918 ffec_get_hwaddr(struct ffec_softc *sc, uint8_t *hwaddr)
919 {
920         uint32_t palr, paur, rnd;
921
922         /*
923          * Try to recover a MAC address from the running hardware. If there's
924          * something non-zero there, assume the bootloader did the right thing
925          * and just use it.
926          *
927          * Otherwise, set the address to a convenient locally assigned address,
928          * 'bsd' + random 24 low-order bits.  'b' is 0x62, which has the locally
929          * assigned bit set, and the broadcast/multicast bit clear.
930          */
931         palr = RD4(sc, FEC_PALR_REG);
932         paur = RD4(sc, FEC_PAUR_REG) & FEC_PAUR_PADDR2_MASK;
933         if ((palr | paur) != 0) {
934                 hwaddr[0] = palr >> 24;
935                 hwaddr[1] = palr >> 16;
936                 hwaddr[2] = palr >>  8;
937                 hwaddr[3] = palr >>  0;
938                 hwaddr[4] = paur >> 24;
939                 hwaddr[5] = paur >> 16;
940         } else {
941                 rnd = arc4random() & 0x00ffffff;
942                 hwaddr[0] = 'b';
943                 hwaddr[1] = 's';
944                 hwaddr[2] = 'd';
945                 hwaddr[3] = rnd >> 16;
946                 hwaddr[4] = rnd >>  8;
947                 hwaddr[5] = rnd >>  0;
948         }
949
950         if (bootverbose) {
951                 device_printf(sc->dev,
952                     "MAC address %02x:%02x:%02x:%02x:%02x:%02x:\n",
953                     hwaddr[0], hwaddr[1], hwaddr[2], 
954                     hwaddr[3], hwaddr[4], hwaddr[5]);
955         }
956 }
957
958 static void
959 ffec_setup_rxfilter(struct ffec_softc *sc)
960 {
961         struct ifnet *ifp;
962         struct ifmultiaddr *ifma;
963         uint8_t *eaddr;
964         uint32_t crc;
965         uint64_t ghash, ihash;
966
967         FFEC_ASSERT_LOCKED(sc);
968
969         ifp = sc->ifp;
970
971         /*
972          * Set the multicast (group) filter hash.
973          */
974         if ((ifp->if_flags & IFF_ALLMULTI))
975                 ghash = 0xffffffffffffffffLLU;
976         else {
977                 ghash = 0;
978                 if_maddr_rlock(ifp);
979                 TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
980                         if (ifma->ifma_addr->sa_family != AF_LINK)
981                                 continue;
982                         /* 6 bits from MSB in LE CRC32 are used for hash. */
983                         crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
984                             ifma->ifma_addr), ETHER_ADDR_LEN);
985                         ghash |= 1LLU << (((uint8_t *)&crc)[3] >> 2);
986                 }
987                 if_maddr_runlock(ifp);
988         }
989         WR4(sc, FEC_GAUR_REG, (uint32_t)(ghash >> 32));
990         WR4(sc, FEC_GALR_REG, (uint32_t)ghash);
991
992         /*
993          * Set the individual address filter hash.
994          *
995          * XXX Is 0 the right value when promiscuous is off?  This hw feature
996          * seems to support the concept of MAC address aliases, does such a
997          * thing even exist?
998          */
999         if ((ifp->if_flags & IFF_PROMISC))
1000                 ihash = 0xffffffffffffffffLLU;
1001         else {
1002                 ihash = 0;
1003         }
1004         WR4(sc, FEC_IAUR_REG, (uint32_t)(ihash >> 32));
1005         WR4(sc, FEC_IALR_REG, (uint32_t)ihash);
1006
1007         /*
1008          * Set the primary address.
1009          */
1010         eaddr = IF_LLADDR(ifp);
1011         WR4(sc, FEC_PALR_REG, (eaddr[0] << 24) | (eaddr[1] << 16) |
1012             (eaddr[2] <<  8) | eaddr[3]);
1013         WR4(sc, FEC_PAUR_REG, (eaddr[4] << 24) | (eaddr[5] << 16));
1014 }
1015
1016 static void
1017 ffec_stop_locked(struct ffec_softc *sc)
1018 {
1019         struct ifnet *ifp;
1020         struct ffec_hwdesc *desc;
1021         struct ffec_bufmap *bmap;
1022         int idx;
1023
1024         FFEC_ASSERT_LOCKED(sc);
1025
1026         ifp = sc->ifp;
1027         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1028         sc->tx_watchdog_count = 0;
1029
1030         /* 
1031          * Stop the hardware, mask all interrupts, and clear all current
1032          * interrupt status bits.
1033          */
1034         WR4(sc, FEC_ECR_REG, RD4(sc, FEC_ECR_REG) & ~FEC_ECR_ETHEREN);
1035         WR4(sc, FEC_IEM_REG, 0x00000000);
1036         WR4(sc, FEC_IER_REG, 0xffffffff);
1037
1038         /*
1039          * Stop the media-check callout.  Do not use callout_drain() because
1040          * we're holding a mutex the callout acquires, and if it's currently
1041          * waiting to acquire it, we'd deadlock.  If it is waiting now, the
1042          * ffec_tick() routine will return without doing anything when it sees
1043          * that IFF_DRV_RUNNING is not set, so avoiding callout_drain() is safe.
1044          */
1045         callout_stop(&sc->ffec_callout);
1046
1047         /*
1048          * Discard all untransmitted buffers.  Each buffer is simply freed;
1049          * it's as if the bits were transmitted and then lost on the wire.
1050          *
1051          * XXX Is this right?  Or should we use IFQ_DRV_PREPEND() to put them
1052          * back on the queue for when we get restarted later?
1053          */
1054         idx = sc->tx_idx_tail;
1055         while (idx != sc->tx_idx_head) {
1056                 desc = &sc->txdesc_ring[idx];
1057                 bmap = &sc->txbuf_map[idx];
1058                 if (desc->buf_paddr != 0) {
1059                         bus_dmamap_unload(sc->txbuf_tag, bmap->map);
1060                         m_freem(bmap->mbuf);
1061                         bmap->mbuf = NULL;
1062                         ffec_setup_txdesc(sc, idx, 0, 0);
1063                 }
1064                 idx = next_txidx(sc, idx);
1065         }
1066
1067         /*
1068          * Discard all unprocessed receive buffers.  This amounts to just
1069          * pretending that nothing ever got received into them.  We reuse the
1070          * mbuf already mapped for each desc, simply turning the EMPTY flags
1071          * back on so they'll get reused when we start up again.
1072          */
1073         for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
1074                 desc = &sc->rxdesc_ring[idx];
1075                 ffec_setup_rxdesc(sc, idx, desc->buf_paddr);
1076         }
1077 }
1078
1079 static void
1080 ffec_init_locked(struct ffec_softc *sc)
1081 {
1082         struct ifnet *ifp = sc->ifp;
1083         uint32_t maxbuf, maxfl, regval;
1084
1085         FFEC_ASSERT_LOCKED(sc);
1086
1087         /*
1088          * The hardware has a limit of 0x7ff as the max frame length (see
1089          * comments for MRBR below), and we use mbuf clusters as receive
1090          * buffers, and we currently are designed to receive an entire frame
1091          * into a single buffer.
1092          *
1093          * We start with a MCLBYTES-sized cluster, but we have to offset into
1094          * the buffer by ETHER_ALIGN to make room for post-receive re-alignment,
1095          * and then that value has to be rounded up to the hardware's DMA
1096          * alignment requirements, so all in all our buffer is that much smaller
1097          * than MCLBYTES.
1098          *
1099          * The resulting value is used as the frame truncation length and the
1100          * max buffer receive buffer size for now.  It'll become more complex
1101          * when we support jumbo frames and receiving fragments of them into
1102          * separate buffers.
1103          */
1104         maxbuf = MCLBYTES - roundup(ETHER_ALIGN, FEC_RXBUF_ALIGN);
1105         maxfl = min(maxbuf, 0x7ff);
1106
1107         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1108                 return;
1109
1110         /* Mask all interrupts and clear all current interrupt status bits. */
1111         WR4(sc, FEC_IEM_REG, 0x00000000);
1112         WR4(sc, FEC_IER_REG, 0xffffffff);
1113
1114         /*
1115          * Go set up palr/puar, galr/gaur, ialr/iaur.
1116          */
1117         ffec_setup_rxfilter(sc);
1118
1119         /*
1120          * TFWR - Transmit FIFO watermark register.
1121          *
1122          * Set the transmit fifo watermark register to "store and forward" mode
1123          * and also set a threshold of 128 bytes in the fifo before transmission
1124          * of a frame begins (to avoid dma underruns).  Recent FEC hardware
1125          * supports STRFWD and when that bit is set, the watermark level in the
1126          * low bits is ignored.  Older hardware doesn't have STRFWD, but writing
1127          * to that bit is innocuous, and the TWFR bits get used instead.
1128          */
1129         WR4(sc, FEC_TFWR_REG, FEC_TFWR_STRFWD | FEC_TFWR_TWFR_128BYTE);
1130
1131         /* RCR - Receive control register.
1132          *
1133          * Set max frame length + clean out anything left from u-boot.
1134          */
1135         WR4(sc, FEC_RCR_REG, (maxfl << FEC_RCR_MAX_FL_SHIFT));
1136
1137         /*
1138          * TCR - Transmit control register.
1139          *
1140          * Clean out anything left from u-boot.  Any necessary values are set in
1141          * ffec_miibus_statchg() based on the media type.
1142          */
1143         WR4(sc, FEC_TCR_REG, 0);
1144         
1145         /*
1146          * OPD - Opcode/pause duration.
1147          *
1148          * XXX These magic numbers come from u-boot.
1149          */
1150         WR4(sc, FEC_OPD_REG, 0x00010020);
1151
1152         /*
1153          * FRSR - Fifo receive start register.
1154          *
1155          * This register does not exist on imx6, it is present on earlier
1156          * hardware. The u-boot code sets this to a non-default value that's 32
1157          * bytes larger than the default, with no clue as to why.  The default
1158          * value should work fine, so there's no code to init it here.
1159          */
1160
1161         /*
1162          *  MRBR - Max RX buffer size.
1163          *
1164          *  Note: For hardware prior to imx6 this value cannot exceed 0x07ff,
1165          *  but the datasheet says no such thing for imx6.  On the imx6, setting
1166          *  this to 2K without setting EN1588 resulted in a crazy runaway
1167          *  receive loop in the hardware, where every rx descriptor in the ring
1168          *  had its EMPTY flag cleared, no completion or error flags set, and a
1169          *  length of zero.  I think maybe you can only exceed it when EN1588 is
1170          *  set, like maybe that's what enables jumbo frames, because in general
1171          *  the EN1588 flag seems to be the "enable new stuff" vs. "be legacy-
1172          *  compatible" flag.
1173          */
1174         WR4(sc, FEC_MRBR_REG, maxfl << FEC_MRBR_R_BUF_SIZE_SHIFT);
1175
1176         /*
1177          * FTRL - Frame truncation length.
1178          *
1179          * Must be greater than or equal to the value set in FEC_RCR_MAXFL.
1180          */
1181         WR4(sc, FEC_FTRL_REG, maxfl);
1182
1183         /*
1184          * RDSR / TDSR descriptor ring pointers.
1185          *
1186          * When we turn on ECR_ETHEREN at the end, the hardware zeroes its
1187          * internal current descriptor index values for both rings, so we zero
1188          * our index values as well.
1189          */
1190         sc->rx_idx = 0;
1191         sc->tx_idx_head = sc->tx_idx_tail = 0;
1192         sc->txcount = 0;
1193         WR4(sc, FEC_RDSR_REG, sc->rxdesc_ring_paddr);
1194         WR4(sc, FEC_TDSR_REG, sc->txdesc_ring_paddr);
1195
1196         /*
1197          * EIM - interrupt mask register.
1198          *
1199          * We always enable the same set of interrupts while running; unlike
1200          * some drivers there's no need to change the mask on the fly depending
1201          * on what operations are in progress.
1202          */
1203         WR4(sc, FEC_IEM_REG, FEC_IER_TXF | FEC_IER_RXF | FEC_IER_EBERR);
1204
1205         /*
1206          * MIBC - MIB control (hardware stats); clear all statistics regs, then
1207          * enable collection of statistics.
1208          */
1209         regval = RD4(sc, FEC_MIBC_REG);
1210         WR4(sc, FEC_MIBC_REG, regval | FEC_MIBC_DIS);
1211         ffec_clear_stats(sc);
1212         WR4(sc, FEC_MIBC_REG, regval & ~FEC_MIBC_DIS);
1213
1214         /*
1215          * ECR - Ethernet control register.
1216          *
1217          * This must happen after all the other config registers are set.  If
1218          * we're running on little-endian hardware, also set the flag for byte-
1219          * swapping descriptor ring entries.  This flag doesn't exist on older
1220          * hardware, but it can be safely set -- the bit position it occupies
1221          * was unused.
1222          */
1223         regval = RD4(sc, FEC_ECR_REG);
1224 #if _BYTE_ORDER == _LITTLE_ENDIAN
1225         regval |= FEC_ECR_DBSWP;
1226 #endif
1227         regval |= FEC_ECR_ETHEREN;
1228         WR4(sc, FEC_ECR_REG, regval);
1229
1230         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1231
1232        /*
1233         * Call mii_mediachg() which will call back into ffec_miibus_statchg() to
1234         * set up the remaining config registers based on the current media.
1235         */
1236         mii_mediachg(sc->mii_softc);
1237         callout_reset(&sc->ffec_callout, hz, ffec_tick, sc);
1238
1239         /*
1240          * Tell the hardware that receive buffers are available.  They were made
1241          * available in ffec_attach() or ffec_stop().
1242          */
1243         WR4(sc, FEC_RDAR_REG, FEC_RDAR_RDAR);
1244 }
1245
1246 static void
1247 ffec_init(void *if_softc)
1248 {
1249         struct ffec_softc *sc = if_softc;
1250
1251         FFEC_LOCK(sc);
1252         ffec_init_locked(sc);
1253         FFEC_UNLOCK(sc);
1254 }
1255
1256 static void
1257 ffec_intr(void *arg)
1258 {
1259         struct ffec_softc *sc;
1260         uint32_t ier;
1261
1262         sc = arg;
1263
1264         FFEC_LOCK(sc);
1265
1266         ier = RD4(sc, FEC_IER_REG);
1267
1268         if (ier & FEC_IER_TXF) {
1269                 WR4(sc, FEC_IER_REG, FEC_IER_TXF);
1270                 ffec_txfinish_locked(sc);
1271         }
1272
1273         if (ier & FEC_IER_RXF) {
1274                 WR4(sc, FEC_IER_REG, FEC_IER_RXF);
1275                 ffec_rxfinish_locked(sc);
1276         }
1277
1278         /*
1279          * We actually don't care about most errors, because the hardware copes
1280          * with them just fine, discarding the incoming bad frame, or forcing a
1281          * bad CRC onto an outgoing bad frame, and counting the errors in the
1282          * stats registers.  The one that really matters is EBERR (DMA bus
1283          * error) because the hardware automatically clears ECR[ETHEREN] and we
1284          * have to restart it here.  It should never happen.
1285          */
1286         if (ier & FEC_IER_EBERR) {
1287                 WR4(sc, FEC_IER_REG, FEC_IER_EBERR);
1288                 device_printf(sc->dev, 
1289                     "Ethernet DMA error, restarting controller.\n");
1290                 ffec_stop_locked(sc);
1291                 ffec_init_locked(sc);
1292         }
1293
1294         FFEC_UNLOCK(sc);
1295
1296 }
1297
1298 static int
1299 ffec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1300 {
1301         struct ffec_softc *sc;
1302         struct mii_data *mii;
1303         struct ifreq *ifr;
1304         int mask, error;
1305
1306         sc = ifp->if_softc;
1307         ifr = (struct ifreq *)data;
1308
1309         error = 0;
1310         switch (cmd) {
1311         case SIOCSIFFLAGS:
1312                 FFEC_LOCK(sc);
1313                 if (ifp->if_flags & IFF_UP) {
1314                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1315                                 if ((ifp->if_flags ^ sc->if_flags) &
1316                                     (IFF_PROMISC | IFF_ALLMULTI))
1317                                         ffec_setup_rxfilter(sc);
1318                         } else {
1319                                 if (!sc->is_detaching)
1320                                         ffec_init_locked(sc);
1321                         }
1322                 } else {
1323                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1324                                 ffec_stop_locked(sc);
1325                 }
1326                 sc->if_flags = ifp->if_flags;
1327                 FFEC_UNLOCK(sc);
1328                 break;
1329
1330         case SIOCADDMULTI:
1331         case SIOCDELMULTI:
1332                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1333                         FFEC_LOCK(sc);
1334                         ffec_setup_rxfilter(sc);
1335                         FFEC_UNLOCK(sc);
1336                 }
1337                 break;
1338
1339         case SIOCSIFMEDIA:
1340         case SIOCGIFMEDIA:
1341                 mii = sc->mii_softc;
1342                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1343                 break;
1344
1345         case SIOCSIFCAP:
1346                 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
1347                 if (mask & IFCAP_VLAN_MTU) {
1348                         /* No work to do except acknowledge the change took. */
1349                         ifp->if_capenable ^= IFCAP_VLAN_MTU;
1350                 }
1351                 break;
1352
1353         default:
1354                 error = ether_ioctl(ifp, cmd, data);
1355                 break;
1356         }       
1357
1358         return (error);
1359 }
1360
1361 static int
1362 ffec_detach(device_t dev)
1363 {
1364         struct ffec_softc *sc;
1365         bus_dmamap_t map;
1366         int idx;
1367
1368         /*
1369          * NB: This function can be called internally to unwind a failure to
1370          * attach. Make sure a resource got allocated/created before destroying.
1371          */
1372
1373         sc = device_get_softc(dev);
1374
1375         if (sc->is_attached) {
1376                 FFEC_LOCK(sc);
1377                 sc->is_detaching = true;
1378                 ffec_stop_locked(sc);
1379                 FFEC_UNLOCK(sc);
1380                 callout_drain(&sc->ffec_callout);
1381                 ether_ifdetach(sc->ifp);
1382         }
1383
1384         /* XXX no miibus detach? */
1385
1386         /* Clean up RX DMA resources and free mbufs. */
1387         for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
1388                 if ((map = sc->rxbuf_map[idx].map) != NULL) {
1389                         bus_dmamap_unload(sc->rxbuf_tag, map);
1390                         bus_dmamap_destroy(sc->rxbuf_tag, map);
1391                         m_freem(sc->rxbuf_map[idx].mbuf);
1392                 }
1393         }
1394         if (sc->rxbuf_tag != NULL)
1395                 bus_dma_tag_destroy(sc->rxbuf_tag);
1396         if (sc->rxdesc_map != NULL) {
1397                 bus_dmamap_unload(sc->rxdesc_tag, sc->rxdesc_map);
1398                 bus_dmamap_destroy(sc->rxdesc_tag, sc->rxdesc_map);
1399         }
1400         if (sc->rxdesc_tag != NULL)
1401         bus_dma_tag_destroy(sc->rxdesc_tag);
1402
1403         /* Clean up TX DMA resources. */
1404         for (idx = 0; idx < TX_DESC_COUNT; ++idx) {
1405                 if ((map = sc->txbuf_map[idx].map) != NULL) {
1406                         /* TX maps are already unloaded. */
1407                         bus_dmamap_destroy(sc->txbuf_tag, map);
1408                 }
1409         }
1410         if (sc->txbuf_tag != NULL)
1411                 bus_dma_tag_destroy(sc->txbuf_tag);
1412         if (sc->txdesc_map != NULL) {
1413                 bus_dmamap_unload(sc->txdesc_tag, sc->txdesc_map);
1414                 bus_dmamap_destroy(sc->txdesc_tag, sc->txdesc_map);
1415         }
1416         if (sc->txdesc_tag != NULL)
1417         bus_dma_tag_destroy(sc->txdesc_tag);
1418
1419         /* Release bus resources. */
1420         if (sc->intr_cookie)
1421                 bus_teardown_intr(dev, sc->irq_res, sc->intr_cookie);
1422
1423         if (sc->irq_res != NULL)
1424                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
1425
1426         if (sc->mem_res != NULL)
1427                 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
1428
1429         FFEC_LOCK_DESTROY(sc);
1430         return (0);
1431 }
1432
1433 static int
1434 ffec_attach(device_t dev)
1435 {
1436         struct ffec_softc *sc;
1437         struct ifnet *ifp = NULL;
1438         struct mbuf *m;
1439         void *dummy;
1440         phandle_t ofw_node;
1441         int error, phynum, rid;
1442         uint8_t eaddr[ETHER_ADDR_LEN];
1443         char phy_conn_name[32];
1444         uint32_t idx, mscr;
1445
1446         sc = device_get_softc(dev);
1447         sc->dev = dev;
1448
1449         FFEC_LOCK_INIT(sc);
1450
1451         /*
1452          * There are differences in the implementation and features of the FEC
1453          * hardware on different SoCs, so figure out what type we are.
1454          */
1455         sc->fectype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1456
1457         /*
1458          * We have to be told what kind of electrical connection exists between
1459          * the MAC and PHY or we can't operate correctly.
1460          */
1461         if ((ofw_node = ofw_bus_get_node(dev)) == -1) {
1462                 device_printf(dev, "Impossible: Can't find ofw bus node\n");
1463                 error = ENXIO;
1464                 goto out;
1465         }
1466         if (OF_searchprop(ofw_node, "phy-mode", 
1467             phy_conn_name, sizeof(phy_conn_name)) != -1) {
1468                 if (strcasecmp(phy_conn_name, "mii") == 0)
1469                         sc->phy_conn_type = PHY_CONN_MII;
1470                 else if (strcasecmp(phy_conn_name, "rmii") == 0)
1471                         sc->phy_conn_type = PHY_CONN_RMII;
1472                 else if (strcasecmp(phy_conn_name, "rgmii") == 0)
1473                         sc->phy_conn_type = PHY_CONN_RGMII;
1474         }
1475         if (sc->phy_conn_type == PHY_CONN_UNKNOWN) {
1476                 device_printf(sc->dev, "No valid 'phy-mode' "
1477                     "property found in FDT data for device.\n");
1478                 error = ENOATTR;
1479                 goto out;
1480         }
1481
1482         callout_init_mtx(&sc->ffec_callout, &sc->mtx, 0);
1483
1484         /* Allocate bus resources for accessing the hardware. */
1485         rid = 0;
1486         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 
1487             RF_ACTIVE);
1488         if (sc->mem_res == NULL) {
1489                 device_printf(dev, "could not allocate memory resources.\n");
1490                 error = ENOMEM;
1491                 goto out;
1492         }
1493         rid = 0;
1494         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1495             RF_ACTIVE);
1496         if (sc->irq_res == NULL) {
1497                 device_printf(dev, "could not allocate interrupt resources.\n");
1498                 error = ENOMEM;
1499                 goto out;
1500         }
1501
1502         /*
1503          * Set up TX descriptor ring, descriptors, and dma maps.
1504          */
1505         error = bus_dma_tag_create(
1506             bus_get_dma_tag(dev),       /* Parent tag. */
1507             FEC_DESC_RING_ALIGN, 0,     /* alignment, boundary */
1508             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1509             BUS_SPACE_MAXADDR,          /* highaddr */
1510             NULL, NULL,                 /* filter, filterarg */
1511             TX_DESC_SIZE, 1,            /* maxsize, nsegments */
1512             TX_DESC_SIZE,               /* maxsegsize */
1513             0,                          /* flags */
1514             NULL, NULL,                 /* lockfunc, lockarg */
1515             &sc->txdesc_tag);
1516         if (error != 0) {
1517                 device_printf(sc->dev,
1518                     "could not create TX ring DMA tag.\n");
1519                 goto out;
1520         }
1521
1522         error = bus_dmamem_alloc(sc->txdesc_tag, (void**)&sc->txdesc_ring,
1523             BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->txdesc_map);
1524         if (error != 0) {
1525                 device_printf(sc->dev,
1526                     "could not allocate TX descriptor ring.\n");
1527                 goto out;
1528         }
1529
1530         error = bus_dmamap_load(sc->txdesc_tag, sc->txdesc_map, sc->txdesc_ring,
1531             TX_DESC_SIZE, ffec_get1paddr, &sc->txdesc_ring_paddr, 0);
1532         if (error != 0) {
1533                 device_printf(sc->dev,
1534                     "could not load TX descriptor ring map.\n");
1535                 goto out;
1536         }
1537
1538         error = bus_dma_tag_create(
1539             bus_get_dma_tag(dev),       /* Parent tag. */
1540             FEC_TXBUF_ALIGN, 0,         /* alignment, boundary */
1541             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1542             BUS_SPACE_MAXADDR,          /* highaddr */
1543             NULL, NULL,                 /* filter, filterarg */
1544             MCLBYTES, 1,                /* maxsize, nsegments */
1545             MCLBYTES,                   /* maxsegsize */
1546             0,                          /* flags */
1547             NULL, NULL,                 /* lockfunc, lockarg */
1548             &sc->txbuf_tag);
1549         if (error != 0) {
1550                 device_printf(sc->dev,
1551                     "could not create TX ring DMA tag.\n");
1552                 goto out;
1553         }
1554
1555         for (idx = 0; idx < TX_DESC_COUNT; ++idx) {
1556                 error = bus_dmamap_create(sc->txbuf_tag, 0, 
1557                     &sc->txbuf_map[idx].map);
1558                 if (error != 0) {
1559                         device_printf(sc->dev,
1560                             "could not create TX buffer DMA map.\n");
1561                         goto out;
1562                 }
1563                 ffec_setup_txdesc(sc, idx, 0, 0);
1564         }
1565
1566         /*
1567          * Set up RX descriptor ring, descriptors, dma maps, and mbufs.
1568          */
1569         error = bus_dma_tag_create(
1570             bus_get_dma_tag(dev),       /* Parent tag. */
1571             FEC_DESC_RING_ALIGN, 0,     /* alignment, boundary */
1572             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1573             BUS_SPACE_MAXADDR,          /* highaddr */
1574             NULL, NULL,                 /* filter, filterarg */
1575             RX_DESC_SIZE, 1,            /* maxsize, nsegments */
1576             RX_DESC_SIZE,               /* maxsegsize */
1577             0,                          /* flags */
1578             NULL, NULL,                 /* lockfunc, lockarg */
1579             &sc->rxdesc_tag);
1580         if (error != 0) {
1581                 device_printf(sc->dev,
1582                     "could not create RX ring DMA tag.\n");
1583                 goto out;
1584         }
1585
1586         error = bus_dmamem_alloc(sc->rxdesc_tag, (void **)&sc->rxdesc_ring, 
1587             BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rxdesc_map);
1588         if (error != 0) {
1589                 device_printf(sc->dev,
1590                     "could not allocate RX descriptor ring.\n");
1591                 goto out;
1592         }
1593
1594         error = bus_dmamap_load(sc->rxdesc_tag, sc->rxdesc_map, sc->rxdesc_ring,
1595             RX_DESC_SIZE, ffec_get1paddr, &sc->rxdesc_ring_paddr, 0);
1596         if (error != 0) {
1597                 device_printf(sc->dev,
1598                     "could not load RX descriptor ring map.\n");
1599                 goto out;
1600         }
1601
1602         error = bus_dma_tag_create(
1603             bus_get_dma_tag(dev),       /* Parent tag. */
1604             1, 0,                       /* alignment, boundary */
1605             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1606             BUS_SPACE_MAXADDR,          /* highaddr */
1607             NULL, NULL,                 /* filter, filterarg */
1608             MCLBYTES, 1,                /* maxsize, nsegments */
1609             MCLBYTES,                   /* maxsegsize */
1610             0,                          /* flags */
1611             NULL, NULL,                 /* lockfunc, lockarg */
1612             &sc->rxbuf_tag);
1613         if (error != 0) {
1614                 device_printf(sc->dev,
1615                     "could not create RX buf DMA tag.\n");
1616                 goto out;
1617         }
1618
1619         for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
1620                 error = bus_dmamap_create(sc->rxbuf_tag, 0, 
1621                     &sc->rxbuf_map[idx].map);
1622                 if (error != 0) {
1623                         device_printf(sc->dev,
1624                             "could not create RX buffer DMA map.\n");
1625                         goto out;
1626                 }
1627                 if ((m = ffec_alloc_mbufcl(sc)) == NULL) {
1628                         device_printf(dev, "Could not alloc mbuf\n");
1629                         error = ENOMEM;
1630                         goto out;
1631                 }
1632                 if ((error = ffec_setup_rxbuf(sc, idx, m)) != 0) {
1633                         device_printf(sc->dev,
1634                             "could not create new RX buffer.\n");
1635                         goto out;
1636                 }
1637         }
1638
1639         /* Try to get the MAC address from the hardware before resetting it. */
1640         ffec_get_hwaddr(sc, eaddr);
1641
1642         /* Reset the hardware.  Disables all interrupts. */
1643         WR4(sc, FEC_ECR_REG, FEC_ECR_RESET);
1644
1645         /* Setup interrupt handler. */
1646         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
1647             NULL, ffec_intr, sc, &sc->intr_cookie);
1648         if (error != 0) {
1649                 device_printf(dev, "could not setup interrupt handler.\n");
1650                 goto out;
1651         }
1652
1653         /*
1654          * Set up the PHY control register.
1655          *
1656          * Speed formula for ENET is md_clock = mac_clock / ((N + 1) * 2).
1657          * Speed formula for FEC is  md_clock = mac_clock / (N * 2)
1658          *
1659          * XXX - Revisit this...
1660          *
1661          * For a Wandboard imx6 (ENET) I was originally using 4, but the uboot
1662          * code uses 10.  Both values seem to work, but I suspect many modern
1663          * PHY parts can do mdio at speeds far above the standard 2.5 MHz.
1664          *
1665          * Different imx manuals use confusingly different terminology (things
1666          * like "system clock" and "internal module clock") with examples that
1667          * use frequencies that have nothing to do with ethernet, giving the
1668          * vague impression that maybe the clock in question is the periphclock
1669          * or something.  In fact, on an imx53 development board (FEC),
1670          * measuring the mdio clock at the pin on the PHY and playing with
1671          * various divisors showed that the root speed was 66 MHz (clk_ipg_root
1672          * aka periphclock) and 13 was the right divisor.
1673          *
1674          * All in all, it seems likely that 13 is a safe divisor for now,
1675          * because if we really do need to base it on the peripheral clock
1676          * speed, then we need a platform-independant get-clock-freq API.
1677          */
1678         mscr = 13 << FEC_MSCR_MII_SPEED_SHIFT;
1679         if (OF_hasprop(ofw_node, "phy-disable-preamble")) {
1680                 mscr |= FEC_MSCR_DIS_PRE;
1681                 if (bootverbose)
1682                         device_printf(dev, "PHY preamble disabled\n");
1683         }
1684         WR4(sc, FEC_MSCR_REG, mscr);
1685
1686         /* Set up the ethernet interface. */
1687         sc->ifp = ifp = if_alloc(IFT_ETHER);
1688
1689         ifp->if_softc = sc;
1690         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1691         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1692         ifp->if_capabilities = IFCAP_VLAN_MTU;
1693         ifp->if_capenable = ifp->if_capabilities;
1694         ifp->if_start = ffec_txstart;
1695         ifp->if_ioctl = ffec_ioctl;
1696         ifp->if_init = ffec_init;
1697         IFQ_SET_MAXLEN(&ifp->if_snd, TX_DESC_COUNT - 1);
1698         ifp->if_snd.ifq_drv_maxlen = TX_DESC_COUNT - 1;
1699         IFQ_SET_READY(&ifp->if_snd);
1700         ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1701
1702 #if 0 /* XXX The hardware keeps stats we could use for these. */
1703         ifp->if_linkmib = &sc->mibdata;
1704         ifp->if_linkmiblen = sizeof(sc->mibdata);
1705 #endif
1706
1707         /* Set up the miigasket hardware (if any). */
1708         ffec_miigasket_setup(sc);
1709
1710         /* Attach the mii driver. */
1711         if (fdt_get_phyaddr(ofw_node, dev, &phynum, &dummy) != 0) {
1712                 phynum = MII_PHY_ANY;
1713         }
1714         error = mii_attach(dev, &sc->miibus, ifp, ffec_media_change,
1715             ffec_media_status, BMSR_DEFCAPMASK, phynum, MII_OFFSET_ANY,
1716             (sc->fectype & FECTYPE_MVF) ? MIIF_FORCEANEG : 0);
1717         if (error != 0) {
1718                 device_printf(dev, "PHY attach failed\n");
1719                 goto out;
1720         }
1721         sc->mii_softc = device_get_softc(sc->miibus);
1722
1723         /* All ready to run, attach the ethernet interface. */
1724         ether_ifattach(ifp, eaddr);
1725         sc->is_attached = true;
1726
1727         error = 0;
1728 out:
1729
1730         if (error != 0)
1731                 ffec_detach(dev);
1732
1733         return (error);
1734 }
1735
1736 static int
1737 ffec_probe(device_t dev)
1738 {
1739         uintptr_t fectype;
1740
1741         if (!ofw_bus_status_okay(dev))
1742                 return (ENXIO);
1743
1744         fectype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1745         if (fectype == FECTYPE_NONE)
1746                 return (ENXIO);
1747
1748         device_set_desc(dev, (fectype & FECFLAG_GBE) ?
1749             "Freescale Gigabit Ethernet Controller" :
1750             "Freescale Fast Ethernet Controller");
1751
1752         return (BUS_PROBE_DEFAULT);
1753 }
1754
1755
1756 static device_method_t ffec_methods[] = {
1757         /* Device interface. */
1758         DEVMETHOD(device_probe,         ffec_probe),
1759         DEVMETHOD(device_attach,        ffec_attach),
1760         DEVMETHOD(device_detach,        ffec_detach),
1761
1762 /*
1763         DEVMETHOD(device_shutdown,      ffec_shutdown),
1764         DEVMETHOD(device_suspend,       ffec_suspend),
1765         DEVMETHOD(device_resume,        ffec_resume),
1766 */
1767
1768         /* MII interface. */
1769         DEVMETHOD(miibus_readreg,       ffec_miibus_readreg),
1770         DEVMETHOD(miibus_writereg,      ffec_miibus_writereg),
1771         DEVMETHOD(miibus_statchg,       ffec_miibus_statchg),
1772
1773         DEVMETHOD_END
1774 };
1775
1776 static driver_t ffec_driver = {
1777         "ffec",
1778         ffec_methods,
1779         sizeof(struct ffec_softc)
1780 };
1781
1782 static devclass_t ffec_devclass;
1783
1784 DRIVER_MODULE(ffec, simplebus, ffec_driver, ffec_devclass, 0, 0);
1785 DRIVER_MODULE(miibus, ffec, miibus_driver, miibus_devclass, 0, 0);
1786
1787 MODULE_DEPEND(ffec, ether, 1, 1, 1);
1788 MODULE_DEPEND(ffec, miibus, 1, 1, 1);