]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/ralink/if_fv.c
Upgrade Unbound to 1.6.1. More to follow.
[FreeBSD/FreeBSD.git] / sys / arm / ralink / if_fv.c
1 /*-
2  * Copyright (c) 2016 Hiroki Mori. All rights reserved.
3  * Copyright (C) 2007 
4  *      Oleksandr Tymoshenko <gonzo@freebsd.org>. 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 SOFTWFV 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 FV DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWFV, EVEN IF ADVISED OF
25  * THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: $
28  * 
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35  * FV Ethernet interface driver
36  * copy from mips/idt/if_kr.c and netbsd code
37  */
38 #include <sys/param.h>
39 #include <sys/endian.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/socket.h>
49 #include <sys/taskqueue.h>
50
51 #include <net/if.h>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/if_types.h>
57 #include <net/if_var.h>
58
59 #include <net/bpf.h>
60
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>                                              
63
64 #include <machine/bus.h>
65 #include <machine/resource.h>
66 #include <sys/bus.h>
67 #include <sys/rman.h>
68
69 #include <dev/mii/mii.h>
70 #include <dev/mii/miivar.h>
71
72 /* Todo: move to options.arm */
73 /*#define FV_MDIO*/
74
75 #ifdef FV_MDIO
76 #include <dev/mdio/mdio.h>
77 #include <dev/etherswitch/miiproxy.h>
78 #include "mdio_if.h"
79 #endif
80
81 MODULE_DEPEND(are, ether, 1, 1, 1);
82 MODULE_DEPEND(are, miibus, 1, 1, 1);
83 #ifdef FV_MDIO
84 MODULE_DEPEND(are, mdio, 1, 1, 1);
85 #endif
86
87 #include "miibus_if.h"
88
89 #include <arm/ralink/if_fvreg.h>
90
91 #ifdef FV_DEBUG
92 void dump_txdesc(struct fv_softc *, int);
93 void dump_status_reg(struct fv_softc *);
94 #endif
95
96 static int fv_attach(device_t);
97 static int fv_detach(device_t);
98 static int fv_ifmedia_upd(struct ifnet *);
99 static void fv_ifmedia_sts(struct ifnet *, struct ifmediareq *);
100 static int fv_ioctl(struct ifnet *, u_long, caddr_t);
101 static void fv_init(void *);
102 static void fv_init_locked(struct fv_softc *);
103 static void fv_link_task(void *, int);
104 static int fv_miibus_readreg(device_t, int, int);
105 static void fv_miibus_statchg(device_t);
106 static int fv_miibus_writereg(device_t, int, int, int);
107 static int fv_probe(device_t);
108 static void fv_reset(struct fv_softc *);
109 static int fv_resume(device_t);
110 static int fv_rx_ring_init(struct fv_softc *);
111 static int fv_tx_ring_init(struct fv_softc *);
112 static int fv_shutdown(device_t);
113 static void fv_start(struct ifnet *);
114 static void fv_start_locked(struct ifnet *);
115 static void fv_stop(struct fv_softc *);
116 static int fv_suspend(device_t);
117
118 static void fv_rx(struct fv_softc *);
119 static void fv_tx(struct fv_softc *);
120 static void fv_intr(void *);
121 static void fv_tick(void *);
122
123 static void fv_dmamap_cb(void *, bus_dma_segment_t *, int, int);
124 static int fv_dma_alloc(struct fv_softc *);
125 static void fv_dma_free(struct fv_softc *);
126 static int fv_newbuf(struct fv_softc *, int);
127 static __inline void fv_fixup_rx(struct mbuf *);
128
129 static void fv_hinted_child(device_t bus, const char *dname, int dunit);
130
131 static void fv_setfilt(struct fv_softc *sc);
132
133 static device_method_t fv_methods[] = {
134         /* Device interface */
135         DEVMETHOD(device_probe,         fv_probe),
136         DEVMETHOD(device_attach,        fv_attach),
137         DEVMETHOD(device_detach,        fv_detach),
138         DEVMETHOD(device_suspend,       fv_suspend),
139         DEVMETHOD(device_resume,        fv_resume),
140         DEVMETHOD(device_shutdown,      fv_shutdown),
141
142         /* MII interface */
143         DEVMETHOD(miibus_readreg,       fv_miibus_readreg),
144         DEVMETHOD(miibus_writereg,      fv_miibus_writereg),
145 #if !defined(FV_MDIO)
146         DEVMETHOD(miibus_statchg,       fv_miibus_statchg),
147 #endif
148
149         /* bus interface */
150         DEVMETHOD(bus_add_child,        device_add_child_ordered),
151         DEVMETHOD(bus_hinted_child,     fv_hinted_child),
152
153         DEVMETHOD_END
154 };
155
156 static driver_t fv_driver = {
157         "fv",
158         fv_methods,
159         sizeof(struct fv_softc)
160 };
161
162 static devclass_t fv_devclass;
163
164 DRIVER_MODULE(fv, simplebus, fv_driver, fv_devclass, 0, 0);
165 #ifdef MII
166 DRIVER_MODULE(miibus, fv, miibus_driver, miibus_devclass, 0, 0);
167 #endif
168
169 static struct mtx miibus_mtx;
170 MTX_SYSINIT(miibus_mtx, &miibus_mtx, "are mii lock", MTX_DEF);
171
172 #ifdef FV_MDIO
173 static int fvmdio_probe(device_t);
174 static int fvmdio_attach(device_t);
175 static int fvmdio_detach(device_t);
176
177 /*
178  * Declare an additional, separate driver for accessing the MDIO bus.
179  */
180 static device_method_t fvmdio_methods[] = {
181         /* Device interface */
182         DEVMETHOD(device_probe,         fvmdio_probe),
183         DEVMETHOD(device_attach,        fvmdio_attach),
184         DEVMETHOD(device_detach,        fvmdio_detach),
185
186         /* bus interface */
187         DEVMETHOD(bus_add_child,        device_add_child_ordered),
188         
189         /* MDIO access */
190         DEVMETHOD(mdio_readreg,         fv_miibus_readreg),
191         DEVMETHOD(mdio_writereg,        fv_miibus_writereg),
192 };
193
194 DEFINE_CLASS_0(fvmdio, fvmdio_driver, fvmdio_methods,
195     sizeof(struct fv_softc));
196 static devclass_t fvmdio_devclass;
197
198 DRIVER_MODULE(miiproxy, fv, miiproxy_driver, miiproxy_devclass, 0, 0);
199 DRIVER_MODULE(fvmdio, simplebus, fvmdio_driver, fvmdio_devclass, 0, 0);
200 DRIVER_MODULE(mdio, fvmdio, mdio_driver, mdio_devclass, 0, 0);
201 #endif
202
203 /* setup frame code refer dc code */
204
205 static void
206 fv_setfilt(struct fv_softc *sc)
207 {
208         uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
209         struct fv_desc *sframe;
210         int i;
211         struct ifnet *ifp;
212         struct ifmultiaddr *ifma;
213         uint16_t *sp;
214         uint8_t *ma;
215
216         ifp = sc->fv_ifp;
217
218         i = sc->fv_cdata.fv_tx_prod;
219         FV_INC(sc->fv_cdata.fv_tx_prod, FV_TX_RING_CNT);
220         sc->fv_cdata.fv_tx_cnt++;
221         sframe = &sc->fv_rdata.fv_tx_ring[i];
222         sp = (uint16_t *)sc->fv_cdata.fv_sf_buff;
223         memset(sp, 0xff, FV_SFRAME_LEN);
224         
225         sframe->fv_addr = sc->fv_rdata.fv_sf_paddr;
226         sframe->fv_devcs = ADCTL_Tx_SETUP | FV_DMASIZE(FV_SFRAME_LEN);
227
228         i = 0;
229         if_maddr_rlock(ifp);
230         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
231                 if (ifma->ifma_addr->sa_family != AF_LINK)
232                         continue;
233                 ma = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
234                 sp[i] = sp[i+1] = (ma[1] << 8 | ma[0]);
235                 i += 2;
236                 sp[i] = sp[i+1] = (ma[3] << 8 | ma[2]);
237                 i += 2;
238                 sp[i] = sp[i+1] = (ma[5] << 8 | ma[4]);
239                 i += 2;
240         }
241         if_maddr_runlock(ifp);
242
243         bcopy(IF_LLADDR(sc->fv_ifp), eaddr, ETHER_ADDR_LEN);
244         sp[90] = sp[91] = eaddr[0];
245         sp[92] = sp[93] = eaddr[1];
246         sp[94] = sp[95] = eaddr[2];
247
248         sframe->fv_stat = ADSTAT_OWN;
249         bus_dmamap_sync(sc->fv_cdata.fv_tx_ring_tag,
250             sc->fv_cdata.fv_tx_ring_map, BUS_DMASYNC_PREWRITE);
251         bus_dmamap_sync(sc->fv_cdata.fv_sf_tag,
252             sc->fv_cdata.fv_sf_buff_map, BUS_DMASYNC_PREWRITE);
253         CSR_WRITE_4(sc, CSR_TXPOLL, 0xFFFFFFFF);
254         DELAY(10000);
255 }
256
257 static int 
258 fv_probe(device_t dev)
259 {
260
261         if (!ofw_bus_status_okay(dev))
262                 return (ENXIO);
263
264         if (!ofw_bus_is_compatible(dev, "fv,ethernet"))
265                 return (ENXIO);
266
267         device_set_desc(dev, "FV Ethernet interface");
268         return (BUS_PROBE_DEFAULT);
269 }
270
271 static int
272 fv_attach(device_t dev)
273 {
274         struct ifnet            *ifp;
275         struct fv_softc         *sc;
276         int                     error = 0, rid;
277         int                     unit;
278         int                     i;
279
280         sc = device_get_softc(dev);
281         unit = device_get_unit(dev);
282         sc->fv_dev = dev;
283         sc->fv_ofw = ofw_bus_get_node(dev); 
284
285         i = OF_getprop(sc->fv_ofw, "local-mac-address", (void *)&sc->fv_eaddr, 6);
286         if (i != 6) {
287                 /* hardcode macaddress */
288                 sc->fv_eaddr[0] = 0x00;
289                 sc->fv_eaddr[1] = 0x0C;
290                 sc->fv_eaddr[2] = 0x42;
291                 sc->fv_eaddr[3] = 0x09;
292                 sc->fv_eaddr[4] = 0x5E;
293                 sc->fv_eaddr[5] = 0x6B;
294         }
295
296         mtx_init(&sc->fv_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
297             MTX_DEF);
298         callout_init_mtx(&sc->fv_stat_callout, &sc->fv_mtx, 0);
299         TASK_INIT(&sc->fv_link_task, 0, fv_link_task, sc);
300
301         /* Map control/status registers. */
302         sc->fv_rid = 0;
303         sc->fv_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->fv_rid, 
304             RF_ACTIVE | RF_SHAREABLE);
305
306         if (sc->fv_res == NULL) {
307                 device_printf(dev, "couldn't map memory\n");
308                 error = ENXIO;
309                 goto fail;
310         }
311
312         sc->fv_btag = rman_get_bustag(sc->fv_res);
313         sc->fv_bhandle = rman_get_bushandle(sc->fv_res);
314
315         /* Allocate interrupts */
316         rid = 0;
317         sc->fv_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 
318             RF_SHAREABLE | RF_ACTIVE);
319
320         if (sc->fv_irq == NULL) {
321                 device_printf(dev, "couldn't map interrupt\n");
322                 error = ENXIO;
323                 goto fail;
324         }
325
326         /* Allocate ifnet structure. */
327         ifp = sc->fv_ifp = if_alloc(IFT_ETHER);
328
329         if (ifp == NULL) {
330                 device_printf(dev, "couldn't allocate ifnet structure\n");
331                 error = ENOSPC;
332                 goto fail;
333         }
334         ifp->if_softc = sc;
335         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
336         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
337         ifp->if_ioctl = fv_ioctl;
338         ifp->if_start = fv_start;
339         ifp->if_init = fv_init;
340
341         /* ifqmaxlen is sysctl value in net/if.c */
342         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
343         ifp->if_snd.ifq_maxlen = ifqmaxlen;
344         IFQ_SET_READY(&ifp->if_snd);
345
346         ifp->if_capenable = ifp->if_capabilities;
347
348         if (fv_dma_alloc(sc) != 0) {
349                 error = ENXIO;
350                 goto fail;
351         }
352
353         /* TODO: calculate prescale */
354 /*
355         CSR_WRITE_4(sc, FV_ETHMCP, (165000000 / (1250000 + 1)) & ~1);
356
357         CSR_WRITE_4(sc, FV_MIIMCFG, FV_MIIMCFG_R);
358         DELAY(1000);
359         CSR_WRITE_4(sc, FV_MIIMCFG, 0);
360 */
361         CSR_WRITE_4(sc, CSR_BUSMODE, BUSMODE_SWR);
362         DELAY(1000);
363
364 #ifdef FV_MDIO
365         sc->fv_miiproxy = mii_attach_proxy(sc->fv_dev);
366 #endif
367
368 #ifdef MII
369         /* Do MII setup. */
370         error = mii_attach(dev, &sc->fv_miibus, ifp, fv_ifmedia_upd,
371             fv_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
372         if (error != 0) {
373                 device_printf(dev, "attaching PHYs failed\n");
374                 goto fail;
375         }
376 #else
377         ifmedia_init(&sc->fv_ifmedia, 0, fv_ifmedia_upd, fv_ifmedia_sts);
378
379         ifmedia_add(&sc->fv_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
380         ifmedia_set(&sc->fv_ifmedia, IFM_ETHER | IFM_AUTO);
381 #endif
382
383         /* Call MI attach routine. */
384         ether_ifattach(ifp, sc->fv_eaddr);
385
386         /* Hook interrupt last to avoid having to lock softc */
387         error = bus_setup_intr(dev, sc->fv_irq, INTR_TYPE_NET | INTR_MPSAFE,
388             NULL, fv_intr, sc, &sc->fv_intrhand);
389
390         if (error) {
391                 device_printf(dev, "couldn't set up irq\n");
392                 ether_ifdetach(ifp);
393                 goto fail;
394         }
395
396 fail:
397         if (error) 
398                 fv_detach(dev);
399
400         return (error);
401 }
402
403 static int
404 fv_detach(device_t dev)
405 {
406         struct fv_softc         *sc = device_get_softc(dev);
407         struct ifnet            *ifp = sc->fv_ifp;
408
409         KASSERT(mtx_initialized(&sc->fv_mtx), ("vr mutex not initialized"));
410
411         /* These should only be active if attach succeeded */
412         if (device_is_attached(dev)) {
413                 FV_LOCK(sc);
414                 sc->fv_detach = 1;
415                 fv_stop(sc);
416                 FV_UNLOCK(sc);
417                 taskqueue_drain(taskqueue_swi, &sc->fv_link_task);
418                 ether_ifdetach(ifp);
419         }
420 #ifdef MII
421         if (sc->fv_miibus)
422                 device_delete_child(dev, sc->fv_miibus);
423 #endif
424         bus_generic_detach(dev);
425
426         if (sc->fv_intrhand)
427                 bus_teardown_intr(dev, sc->fv_irq, sc->fv_intrhand);
428         if (sc->fv_irq)
429                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->fv_irq);
430
431         if (sc->fv_res)
432                 bus_release_resource(dev, SYS_RES_MEMORY, sc->fv_rid, 
433                     sc->fv_res);
434
435         if (ifp)
436                 if_free(ifp);
437
438         fv_dma_free(sc);
439
440         mtx_destroy(&sc->fv_mtx);
441
442         return (0);
443
444 }
445
446 static int
447 fv_suspend(device_t dev)
448 {
449
450         panic("%s", __func__);
451         return 0;
452 }
453
454 static int
455 fv_resume(device_t dev)
456 {
457
458         panic("%s", __func__);
459         return 0;
460 }
461
462 static int
463 fv_shutdown(device_t dev)
464 {
465         struct fv_softc *sc;
466
467         sc = device_get_softc(dev);
468
469         FV_LOCK(sc);
470         fv_stop(sc);
471         FV_UNLOCK(sc);
472
473         return (0);
474 }
475
476 static int
477 fv_miibus_readbits(struct fv_softc *sc, int count)
478 {
479         int result;
480
481         result = 0;
482         while(count--) {
483                 result <<= 1;
484                 CSR_WRITE_4(sc, CSR_MIIMNG, MII_RD);
485                 DELAY(10);
486                 CSR_WRITE_4(sc, CSR_MIIMNG, MII_RD | MII_CLK);
487                 DELAY(10);
488                 if (CSR_READ_4(sc, CSR_MIIMNG) & MII_DIN)
489                         result |= 1;
490         }
491
492         return (result);
493 }
494
495 static int
496 fv_miibus_writebits(struct fv_softc *sc, int data, int count)
497 {
498         int bit;
499
500         while(count--) {
501                 bit = ((data) >> count) & 0x1 ? MII_DOUT : 0;
502                 CSR_WRITE_4(sc, CSR_MIIMNG, bit | MII_WR);
503                 DELAY(10);
504                 CSR_WRITE_4(sc, CSR_MIIMNG, bit | MII_WR | MII_CLK);
505                 DELAY(10);
506         }
507
508         return (0);
509 }
510
511 static void
512 fv_miibus_turnaround(struct fv_softc *sc, int cmd)
513 {
514         if (cmd == MII_WRCMD) {
515                 fv_miibus_writebits(sc, 0x02, 2);
516         } else {
517                 fv_miibus_readbits(sc, 1);
518         }
519 }
520
521 static int
522 fv_miibus_readreg(device_t dev, int phy, int reg)
523 {
524         struct fv_softc * sc = device_get_softc(dev);
525         int             result;
526
527         mtx_lock(&miibus_mtx);
528         fv_miibus_writebits(sc, MII_PREAMBLE, 32);
529         fv_miibus_writebits(sc, MII_RDCMD, 4);
530         fv_miibus_writebits(sc, phy, 5);
531         fv_miibus_writebits(sc, reg, 5);
532         fv_miibus_turnaround(sc, MII_RDCMD);
533         result = fv_miibus_readbits(sc, 16);
534         fv_miibus_turnaround(sc, MII_RDCMD);
535         mtx_unlock(&miibus_mtx);
536
537         return (result);
538 }
539
540 static int
541 fv_miibus_writereg(device_t dev, int phy, int reg, int data)
542 {
543         struct fv_softc * sc = device_get_softc(dev);
544
545         mtx_lock(&miibus_mtx);
546         fv_miibus_writebits(sc, MII_PREAMBLE, 32);
547         fv_miibus_writebits(sc, MII_WRCMD, 4);
548         fv_miibus_writebits(sc, phy, 5);
549         fv_miibus_writebits(sc, reg, 5);
550         fv_miibus_turnaround(sc, MII_WRCMD);
551         fv_miibus_writebits(sc, data, 16);
552         mtx_unlock(&miibus_mtx);
553
554         return (0);
555 }
556
557 #if !defined(FV_MDIO)
558 static void
559 fv_miibus_statchg(device_t dev)
560 {
561         struct fv_softc         *sc;
562
563         sc = device_get_softc(dev);
564         taskqueue_enqueue(taskqueue_swi, &sc->fv_link_task);
565 }
566 #endif
567
568 static void
569 fv_link_task(void *arg, int pending)
570 {
571 #ifdef MII
572         struct fv_softc         *sc;
573         struct mii_data         *mii;
574         struct ifnet            *ifp;
575         /* int                  lfdx, mfdx; */
576
577         sc = (struct fv_softc *)arg;
578
579         FV_LOCK(sc);
580         mii = device_get_softc(sc->fv_miibus);
581         ifp = sc->fv_ifp;
582         if (mii == NULL || ifp == NULL ||
583             (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
584                 FV_UNLOCK(sc);
585                 return;
586         }
587
588         if (mii->mii_media_status & IFM_ACTIVE) {
589                 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
590                         sc->fv_link_status = 1;
591         } else
592                 sc->fv_link_status = 0;
593
594         FV_UNLOCK(sc);
595 #endif
596 }
597
598 static void
599 fv_reset(struct fv_softc *sc)
600 {
601         int             i;
602
603         CSR_WRITE_4(sc, CSR_BUSMODE, BUSMODE_SWR);
604
605         /*
606          * The chip doesn't take itself out of reset automatically.
607          * We need to do so after 2us.
608          */
609         DELAY(1000);
610         CSR_WRITE_4(sc, CSR_BUSMODE, 0);
611
612         for (i = 0; i < 1000; i++) {
613                 /*
614                  * Wait a bit for the reset to complete before peeking
615                  * at the chip again.
616                  */
617                 DELAY(1000);
618                 if ((CSR_READ_4(sc, CSR_BUSMODE) & BUSMODE_SWR) == 0)
619                         break;
620         }
621
622         if (CSR_READ_4(sc, CSR_BUSMODE) & BUSMODE_SWR)
623                 device_printf(sc->fv_dev, "reset time out\n");
624
625         DELAY(1000);
626 }
627
628 static void
629 fv_init(void *xsc)
630 {
631         struct fv_softc  *sc = xsc;
632
633         FV_LOCK(sc);
634         fv_init_locked(sc);
635         FV_UNLOCK(sc);
636 }
637
638 static void
639 fv_init_locked(struct fv_softc *sc)
640 {
641         struct ifnet            *ifp = sc->fv_ifp;
642 #ifdef MII
643         struct mii_data         *mii;
644 #endif
645
646         FV_LOCK_ASSERT(sc);
647
648 #ifdef MII
649         mii = device_get_softc(sc->fv_miibus);
650 #endif
651
652         fv_stop(sc);
653         fv_reset(sc);
654
655         /* Init circular RX list. */
656         if (fv_rx_ring_init(sc) != 0) {
657                 device_printf(sc->fv_dev,
658                     "initialization failed: no memory for rx buffers\n");
659                 fv_stop(sc);
660                 return;
661         }
662
663         /* Init tx descriptors. */
664         fv_tx_ring_init(sc);
665
666         /*
667          * Initialize the BUSMODE register.
668          */
669         CSR_WRITE_4(sc, CSR_BUSMODE,
670             /* XXX: not sure if this is a good thing or not... */
671             BUSMODE_BAR | BUSMODE_PBL_32LW);
672
673         /*
674          * Initialize the interrupt mask and enable interrupts.
675          */
676         /* normal interrupts */
677         sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
678
679         /* abnormal interrupts */
680         sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
681             STATUS_RU | STATUS_RPS | STATUS_SE | STATUS_AIS;
682
683         sc->sc_rxint_mask = STATUS_RI|STATUS_RU;
684         sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
685
686         sc->sc_rxint_mask &= sc->sc_inten;
687         sc->sc_txint_mask &= sc->sc_inten;
688
689         CSR_WRITE_4(sc, CSR_INTEN, sc->sc_inten);
690         CSR_WRITE_4(sc, CSR_STATUS, 0xffffffff);
691
692         /*
693          * Give the transmit and receive rings to the chip.
694          */
695         CSR_WRITE_4(sc, CSR_TXLIST, FV_TX_RING_ADDR(sc, 0));
696         CSR_WRITE_4(sc, CSR_RXLIST, FV_RX_RING_ADDR(sc, 0));
697
698         /*
699          * Set the station address.
700          */
701         fv_setfilt(sc);
702
703
704         /*
705          * Write out the opmode.
706          */
707         CSR_WRITE_4(sc, CSR_OPMODE, OPMODE_SR | OPMODE_ST |
708             OPMODE_TR_128 | OPMODE_FDX | OPMODE_SPEED);
709         /*
710          * Start the receive process.
711          */
712         CSR_WRITE_4(sc, CSR_RXPOLL, RXPOLL_RPD);
713
714         sc->fv_link_status = 1;
715 #ifdef MII
716         mii_mediachg(mii);
717 #endif
718
719         ifp->if_drv_flags |= IFF_DRV_RUNNING;
720         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
721
722         callout_reset(&sc->fv_stat_callout, hz, fv_tick, sc);
723 }
724
725 static void
726 fv_start(struct ifnet *ifp)
727 {
728         struct fv_softc  *sc;
729
730         sc = ifp->if_softc;
731
732         FV_LOCK(sc);
733         fv_start_locked(ifp);
734         FV_UNLOCK(sc);
735 }
736
737 /*
738  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
739  * pointers to the fragment pointers.
740  * Use Implicit Chain implementation.
741  */
742 static int
743 fv_encap(struct fv_softc *sc, struct mbuf **m_head)
744 {
745         struct fv_txdesc        *txd;
746         struct fv_desc          *desc;
747         struct mbuf             *m;
748         bus_dma_segment_t       txsegs[FV_MAXFRAGS];
749         int                     error, i, nsegs, prod, si;
750         int                     padlen;
751         int                     txstat;
752
753         FV_LOCK_ASSERT(sc);
754
755         /*
756          * Some VIA Rhine wants packet buffers to be longword
757          * aligned, but very often our mbufs aren't. Rather than
758          * waste time trying to decide when to copy and when not
759          * to copy, just do it all the time.
760          */
761         m = m_defrag(*m_head, M_NOWAIT);
762         if (m == NULL) {
763                 device_printf(sc->fv_dev, "fv_encap m_defrag error\n");
764                 m_freem(*m_head);
765                 *m_head = NULL;
766                 return (ENOBUFS);
767         }
768         *m_head = m;
769
770         /*
771          * The Rhine chip doesn't auto-pad, so we have to make
772          * sure to pad short frames out to the minimum frame length
773          * ourselves.
774          */
775         if ((*m_head)->m_pkthdr.len < FV_MIN_FRAMELEN) {
776                 m = *m_head;
777                 padlen = FV_MIN_FRAMELEN - m->m_pkthdr.len;
778                 if (M_WRITABLE(m) == 0) {
779                         /* Get a writable copy. */
780                         m = m_dup(*m_head, M_NOWAIT);
781                         m_freem(*m_head);
782                         if (m == NULL) {
783                                 device_printf(sc->fv_dev, "fv_encap m_dup error\n");
784                                 *m_head = NULL;
785                                 return (ENOBUFS);
786                         }
787                         *m_head = m;
788                 }
789                 if (m->m_next != NULL || M_TRAILINGSPACE(m) < padlen) {
790                         m = m_defrag(m, M_NOWAIT);
791                         if (m == NULL) {
792                                 device_printf(sc->fv_dev, "fv_encap m_defrag error\n");
793                                 m_freem(*m_head);
794                                 *m_head = NULL;
795                                 return (ENOBUFS);
796                         }
797                 }
798                 /*
799                  * Manually pad short frames, and zero the pad space
800                  * to avoid leaking data.
801                  */
802                 bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
803                 m->m_pkthdr.len += padlen;
804                 m->m_len = m->m_pkthdr.len;
805                 *m_head = m;
806         }
807
808         prod = sc->fv_cdata.fv_tx_prod;
809         txd = &sc->fv_cdata.fv_txdesc[prod];
810         error = bus_dmamap_load_mbuf_sg(sc->fv_cdata.fv_tx_tag, txd->tx_dmamap,
811             *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
812         if (error == EFBIG) {
813                 device_printf(sc->fv_dev, "fv_encap EFBIG error\n");
814                 m = m_defrag(*m_head, M_NOWAIT);
815                 if (m == NULL) {
816                         m_freem(*m_head);
817                         *m_head = NULL;
818                         return (ENOBUFS);
819                 }
820                 *m_head = m;
821                 error = bus_dmamap_load_mbuf_sg(sc->fv_cdata.fv_tx_tag,
822                     txd->tx_dmamap, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
823                 if (error != 0) {
824                         m_freem(*m_head);
825                         *m_head = NULL;
826                         return (error);
827                 }
828
829         } else if (error != 0)
830                 return (error);
831         if (nsegs == 0) {
832                 m_freem(*m_head);
833                 *m_head = NULL;
834                 return (EIO);
835         }
836
837         /* Check number of available descriptors. */
838         if (sc->fv_cdata.fv_tx_cnt + nsegs >= (FV_TX_RING_CNT - 1)) {
839                 bus_dmamap_unload(sc->fv_cdata.fv_tx_tag, txd->tx_dmamap);
840                 return (ENOBUFS);
841         }
842
843         txd->tx_m = *m_head;
844         bus_dmamap_sync(sc->fv_cdata.fv_tx_tag, txd->tx_dmamap,
845             BUS_DMASYNC_PREWRITE);
846
847         si = prod;
848
849         /* 
850          * Make a list of descriptors for this packet. 
851          */
852         desc = NULL;
853         for (i = 0; i < nsegs; i++) {
854                 desc = &sc->fv_rdata.fv_tx_ring[prod];
855                 desc->fv_stat = ADSTAT_OWN;
856                 desc->fv_devcs = txsegs[i].ds_len;
857                 /* end of descriptor */
858                 if (prod == FV_TX_RING_CNT - 1)
859                         desc->fv_devcs |= ADCTL_ER;
860                 desc->fv_addr = txsegs[i].ds_addr;
861
862                 ++sc->fv_cdata.fv_tx_cnt;
863                 FV_INC(prod, FV_TX_RING_CNT);
864         }
865
866         /* 
867          * Set mark last fragment with Last/Intr flag
868          */
869         if (desc) {
870                 desc->fv_devcs |= ADCTL_Tx_IC;
871                 desc->fv_devcs |= ADCTL_Tx_LS;
872         }
873
874         /* Update producer index. */
875         sc->fv_cdata.fv_tx_prod = prod;
876
877         /* Sync descriptors. */
878         bus_dmamap_sync(sc->fv_cdata.fv_tx_ring_tag,
879             sc->fv_cdata.fv_tx_ring_map,
880             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
881
882         txstat = (CSR_READ_4(sc, CSR_STATUS) >> 20) & 7;
883         if (txstat == 0 || txstat == 6) {
884                 /* Transmit Process Stat is stop or suspended */
885                 desc = &sc->fv_rdata.fv_tx_ring[si];
886                 desc->fv_devcs |= ADCTL_Tx_FS;
887         }
888         else {
889                 /* Get previous descriptor */
890                 si = (si + FV_TX_RING_CNT - 1) % FV_TX_RING_CNT;
891                 desc = &sc->fv_rdata.fv_tx_ring[si];
892                 /* join remain data and flugs */
893                 desc->fv_devcs &= ~ADCTL_Tx_IC;
894                 desc->fv_devcs &= ~ADCTL_Tx_LS;
895         }
896
897
898         return (0);
899 }
900
901 static void
902 fv_start_locked(struct ifnet *ifp)
903 {
904         struct fv_softc         *sc;
905         struct mbuf             *m_head;
906         int                     enq;
907         int                     txstat;
908
909         sc = ifp->if_softc;
910
911         FV_LOCK_ASSERT(sc);
912
913         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
914             IFF_DRV_RUNNING || sc->fv_link_status == 0 )
915                 return;
916
917         for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
918             sc->fv_cdata.fv_tx_cnt < FV_TX_RING_CNT - 2; ) {
919                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
920                 if (m_head == NULL)
921                         break;
922                 /*
923                  * Pack the data into the transmit ring. If we
924                  * don't have room, set the OACTIVE flag and wait
925                  * for the NIC to drain the ring.
926                  */
927                 if (fv_encap(sc, &m_head)) {
928                         if (m_head == NULL)
929                                 break;
930                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
931                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
932                         break;
933                 }
934
935                 enq++;
936                 /*
937                  * If there's a BPF listener, bounce a copy of this frame
938                  * to him.
939                  */
940                 ETHER_BPF_MTAP(ifp, m_head);
941         }
942
943         if (enq > 0) {
944                 txstat = (CSR_READ_4(sc, CSR_STATUS) >> 20) & 7;
945                 if (txstat == 0 || txstat == 6)
946                         CSR_WRITE_4(sc, CSR_TXPOLL, TXPOLL_TPD);
947         }
948 }
949
950 static void
951 fv_stop(struct fv_softc *sc)
952 {
953         struct ifnet        *ifp;
954
955         FV_LOCK_ASSERT(sc);
956
957         ifp = sc->fv_ifp;
958         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
959         callout_stop(&sc->fv_stat_callout);
960
961         /* Disable interrupts. */
962         CSR_WRITE_4(sc, CSR_INTEN, 0);
963
964         /* Stop the transmit and receive processes. */
965         CSR_WRITE_4(sc, CSR_OPMODE, 0);
966         CSR_WRITE_4(sc, CSR_RXLIST, 0);
967         CSR_WRITE_4(sc, CSR_TXLIST, 0);
968
969 }
970
971
972 static int
973 fv_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
974 {
975         struct fv_softc         *sc = ifp->if_softc;
976         struct ifreq            *ifr = (struct ifreq *) data;
977 #ifdef MII
978         struct mii_data         *mii;
979 #endif
980         int                     error;
981         int                     csr;
982
983         switch (command) {
984         case SIOCSIFFLAGS:
985                 FV_LOCK(sc);
986                 if (ifp->if_flags & IFF_UP) {
987                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
988                                 if ((ifp->if_flags ^ sc->fv_if_flags) &
989                                     IFF_PROMISC) {
990                                         csr = CSR_READ_4(sc, CSR_OPMODE);
991                                         CSR_WRITE_4(sc, CSR_OPMODE, csr |
992                                             OPMODE_PM | OPMODE_PR);
993                                 }
994                                 if ((ifp->if_flags ^ sc->fv_if_flags) &
995                                     IFF_ALLMULTI) {
996                                         csr = CSR_READ_4(sc, CSR_OPMODE);
997                                         CSR_WRITE_4(sc, CSR_OPMODE, csr |
998                                             OPMODE_PM);
999                                 }
1000                         } else {
1001                                 if (sc->fv_detach == 0)
1002                                         fv_init_locked(sc);
1003                         }
1004                 } else {
1005                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1006                                 fv_stop(sc);
1007                 }
1008                 sc->fv_if_flags = ifp->if_flags;
1009                 FV_UNLOCK(sc);
1010                 error = 0;
1011                 break;
1012         case SIOCADDMULTI:
1013         case SIOCDELMULTI:
1014 #if 0
1015                 FV_LOCK(sc);
1016                 fv_set_filter(sc);
1017                 FV_UNLOCK(sc);
1018 #endif
1019                 error = 0;
1020                 break;
1021         case SIOCGIFMEDIA:
1022         case SIOCSIFMEDIA:
1023 #ifdef MII
1024                 mii = device_get_softc(sc->fv_miibus);
1025                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1026 #else
1027                 error = ifmedia_ioctl(ifp, ifr, &sc->fv_ifmedia, command);
1028 #endif
1029                 break;
1030         case SIOCSIFCAP:
1031                 error = 0;
1032 #if 0
1033                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1034                 if ((mask & IFCAP_HWCSUM) != 0) {
1035                         ifp->if_capenable ^= IFCAP_HWCSUM;
1036                         if ((IFCAP_HWCSUM & ifp->if_capenable) &&
1037                             (IFCAP_HWCSUM & ifp->if_capabilities))
1038                                 ifp->if_hwassist = FV_CSUM_FEATURES;
1039                         else
1040                                 ifp->if_hwassist = 0;
1041                 }
1042                 if ((mask & IFCAP_VLAN_HWTAGGING) != 0) {
1043                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1044                         if (IFCAP_VLAN_HWTAGGING & ifp->if_capenable &&
1045                             IFCAP_VLAN_HWTAGGING & ifp->if_capabilities &&
1046                             ifp->if_drv_flags & IFF_DRV_RUNNING) {
1047                                 FV_LOCK(sc);
1048                                 fv_vlan_setup(sc);
1049                                 FV_UNLOCK(sc);
1050                         }
1051                 }
1052                 VLAN_CAPABILITIES(ifp);
1053 #endif
1054                 break;
1055         default:
1056                 error = ether_ioctl(ifp, command, data);
1057                 break;
1058         }
1059
1060         return (error);
1061 }
1062
1063 /*
1064  * Set media options.
1065  */
1066 static int
1067 fv_ifmedia_upd(struct ifnet *ifp)
1068 {
1069 #ifdef MII
1070         struct fv_softc         *sc;
1071         struct mii_data         *mii;
1072         struct mii_softc        *miisc;
1073         int                     error;
1074
1075         sc = ifp->if_softc;
1076         FV_LOCK(sc);
1077         mii = device_get_softc(sc->fv_miibus);
1078         LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1079                 PHY_RESET(miisc);
1080         error = mii_mediachg(mii);
1081         FV_UNLOCK(sc);
1082
1083         return (error);
1084 #else
1085         return (0);
1086 #endif
1087 }
1088
1089 /*
1090  * Report current media status.
1091  */
1092 static void
1093 fv_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1094 {
1095 #ifdef MII
1096         struct fv_softc         *sc = ifp->if_softc;
1097         struct mii_data         *mii;
1098
1099         mii = device_get_softc(sc->fv_miibus);
1100         FV_LOCK(sc);
1101         mii_pollstat(mii);
1102         ifmr->ifm_active = mii->mii_media_active;
1103         ifmr->ifm_status = mii->mii_media_status;
1104         FV_UNLOCK(sc);
1105 #else
1106         ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
1107 #endif
1108 }
1109
1110 struct fv_dmamap_arg {
1111         bus_addr_t      fv_busaddr;
1112 };
1113
1114 static void
1115 fv_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1116 {
1117         struct fv_dmamap_arg    *ctx;
1118
1119         if (error != 0)
1120                 return;
1121         ctx = arg;
1122         ctx->fv_busaddr = segs[0].ds_addr;
1123 }
1124
1125 static int
1126 fv_dma_alloc(struct fv_softc *sc)
1127 {
1128         struct fv_dmamap_arg    ctx;
1129         struct fv_txdesc        *txd;
1130         struct fv_rxdesc        *rxd;
1131         int                     error, i;
1132
1133         /* Create parent DMA tag. */
1134         error = bus_dma_tag_create(
1135             bus_get_dma_tag(sc->fv_dev),        /* parent */
1136             1, 0,                       /* alignment, boundary */
1137             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1138             BUS_SPACE_MAXADDR,          /* highaddr */
1139             NULL, NULL,                 /* filter, filterarg */
1140             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
1141             0,                          /* nsegments */
1142             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1143             0,                          /* flags */
1144             NULL, NULL,                 /* lockfunc, lockarg */
1145             &sc->fv_cdata.fv_parent_tag);
1146         if (error != 0) {
1147                 device_printf(sc->fv_dev, "failed to create parent DMA tag\n");
1148                 goto fail;
1149         }
1150         /* Create tag for Tx ring. */
1151         error = bus_dma_tag_create(
1152             sc->fv_cdata.fv_parent_tag, /* parent */
1153             FV_RING_ALIGN, 0,           /* alignment, boundary */
1154             BUS_SPACE_MAXADDR,          /* lowaddr */
1155             BUS_SPACE_MAXADDR,          /* highaddr */
1156             NULL, NULL,                 /* filter, filterarg */
1157             FV_TX_RING_SIZE,            /* maxsize */
1158             1,                          /* nsegments */
1159             FV_TX_RING_SIZE,            /* maxsegsize */
1160             0,                          /* flags */
1161             NULL, NULL,                 /* lockfunc, lockarg */
1162             &sc->fv_cdata.fv_tx_ring_tag);
1163         if (error != 0) {
1164                 device_printf(sc->fv_dev, "failed to create Tx ring DMA tag\n");
1165                 goto fail;
1166         }
1167
1168         /* Create tag for Rx ring. */
1169         error = bus_dma_tag_create(
1170             sc->fv_cdata.fv_parent_tag, /* parent */
1171             FV_RING_ALIGN, 0,           /* alignment, boundary */
1172             BUS_SPACE_MAXADDR,          /* lowaddr */
1173             BUS_SPACE_MAXADDR,          /* highaddr */
1174             NULL, NULL,                 /* filter, filterarg */
1175             FV_RX_RING_SIZE,            /* maxsize */
1176             1,                          /* nsegments */
1177             FV_RX_RING_SIZE,            /* maxsegsize */
1178             0,                          /* flags */
1179             NULL, NULL,                 /* lockfunc, lockarg */
1180             &sc->fv_cdata.fv_rx_ring_tag);
1181         if (error != 0) {
1182                 device_printf(sc->fv_dev, "failed to create Rx ring DMA tag\n");
1183                 goto fail;
1184         }
1185
1186         /* Create tag for Tx buffers. */
1187         error = bus_dma_tag_create(
1188             sc->fv_cdata.fv_parent_tag, /* parent */
1189             1, 0,               /* alignment, boundary */
1190             BUS_SPACE_MAXADDR,          /* lowaddr */
1191             BUS_SPACE_MAXADDR,          /* highaddr */
1192             NULL, NULL,                 /* filter, filterarg */
1193             MCLBYTES * FV_MAXFRAGS,     /* maxsize */
1194             FV_MAXFRAGS,                /* nsegments */
1195             MCLBYTES,                   /* maxsegsize */
1196             0,                          /* flags */
1197             NULL, NULL,                 /* lockfunc, lockarg */
1198             &sc->fv_cdata.fv_tx_tag);
1199         if (error != 0) {
1200                 device_printf(sc->fv_dev, "failed to create Tx DMA tag\n");
1201                 goto fail;
1202         }
1203
1204         /* Create tag for Rx buffers. */
1205         error = bus_dma_tag_create(
1206             sc->fv_cdata.fv_parent_tag, /* parent */
1207             FV_RX_ALIGN, 0,             /* alignment, boundary */
1208             BUS_SPACE_MAXADDR,          /* lowaddr */
1209             BUS_SPACE_MAXADDR,          /* highaddr */
1210             NULL, NULL,                 /* filter, filterarg */
1211             MCLBYTES,                   /* maxsize */
1212             1,                          /* nsegments */
1213             MCLBYTES,                   /* maxsegsize */
1214             0,                          /* flags */
1215             NULL, NULL,                 /* lockfunc, lockarg */
1216             &sc->fv_cdata.fv_rx_tag);
1217         if (error != 0) {
1218                 device_printf(sc->fv_dev, "failed to create Rx DMA tag\n");
1219                 goto fail;
1220         }
1221
1222         /* Create tag for setup frame buffers. */
1223         error = bus_dma_tag_create(
1224             sc->fv_cdata.fv_parent_tag, /* parent */
1225             sizeof(uint32_t), 0,        /* alignment, boundary */
1226             BUS_SPACE_MAXADDR,          /* lowaddr */
1227             BUS_SPACE_MAXADDR,          /* highaddr */
1228             NULL, NULL,                 /* filter, filterarg */
1229             FV_SFRAME_LEN + FV_MIN_FRAMELEN,                    /* maxsize */
1230             1,                          /* nsegments */
1231             FV_SFRAME_LEN + FV_MIN_FRAMELEN,                    /* maxsegsize */
1232             0,                          /* flags */
1233             NULL, NULL,                 /* lockfunc, lockarg */
1234             &sc->fv_cdata.fv_sf_tag);
1235         if (error != 0) {
1236                 device_printf(sc->fv_dev, "failed to create setup frame DMA tag\n");
1237                 goto fail;
1238         }
1239
1240         /* Allocate DMA'able memory and load the DMA map for Tx ring. */
1241         error = bus_dmamem_alloc(sc->fv_cdata.fv_tx_ring_tag,
1242             (void **)&sc->fv_rdata.fv_tx_ring, BUS_DMA_WAITOK |
1243             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->fv_cdata.fv_tx_ring_map);
1244         if (error != 0) {
1245                 device_printf(sc->fv_dev,
1246                     "failed to allocate DMA'able memory for Tx ring\n");
1247                 goto fail;
1248         }
1249
1250         ctx.fv_busaddr = 0;
1251         error = bus_dmamap_load(sc->fv_cdata.fv_tx_ring_tag,
1252             sc->fv_cdata.fv_tx_ring_map, sc->fv_rdata.fv_tx_ring,
1253             FV_TX_RING_SIZE, fv_dmamap_cb, &ctx, 0);
1254         if (error != 0 || ctx.fv_busaddr == 0) {
1255                 device_printf(sc->fv_dev,
1256                     "failed to load DMA'able memory for Tx ring\n");
1257                 goto fail;
1258         }
1259         sc->fv_rdata.fv_tx_ring_paddr = ctx.fv_busaddr;
1260
1261         /* Allocate DMA'able memory and load the DMA map for Rx ring. */
1262         error = bus_dmamem_alloc(sc->fv_cdata.fv_rx_ring_tag,
1263             (void **)&sc->fv_rdata.fv_rx_ring, BUS_DMA_WAITOK |
1264             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->fv_cdata.fv_rx_ring_map);
1265         if (error != 0) {
1266                 device_printf(sc->fv_dev,
1267                     "failed to allocate DMA'able memory for Rx ring\n");
1268                 goto fail;
1269         }
1270
1271         ctx.fv_busaddr = 0;
1272         error = bus_dmamap_load(sc->fv_cdata.fv_rx_ring_tag,
1273             sc->fv_cdata.fv_rx_ring_map, sc->fv_rdata.fv_rx_ring,
1274             FV_RX_RING_SIZE, fv_dmamap_cb, &ctx, 0);
1275         if (error != 0 || ctx.fv_busaddr == 0) {
1276                 device_printf(sc->fv_dev,
1277                     "failed to load DMA'able memory for Rx ring\n");
1278                 goto fail;
1279         }
1280         sc->fv_rdata.fv_rx_ring_paddr = ctx.fv_busaddr;
1281
1282         /* Allocate DMA'able memory and load the DMA map for setup frame. */
1283         error = bus_dmamem_alloc(sc->fv_cdata.fv_sf_tag,
1284             (void **)&sc->fv_cdata.fv_sf_buff, BUS_DMA_WAITOK |
1285             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->fv_cdata.fv_sf_buff_map);
1286         if (error != 0) {
1287                 device_printf(sc->fv_dev,
1288                     "failed to allocate DMA'able memory for setup frame\n");
1289                 goto fail;
1290         }
1291
1292         ctx.fv_busaddr = 0;
1293         error = bus_dmamap_load(sc->fv_cdata.fv_sf_tag,
1294             sc->fv_cdata.fv_sf_buff_map, sc->fv_cdata.fv_sf_buff,
1295             FV_SFRAME_LEN, fv_dmamap_cb, &ctx, 0);
1296         if (error != 0 || ctx.fv_busaddr == 0) {
1297                 device_printf(sc->fv_dev,
1298                     "failed to load DMA'able memory for setup frame\n");
1299                 goto fail;
1300         }
1301         sc->fv_rdata.fv_sf_paddr = ctx.fv_busaddr;
1302
1303         /* Create DMA maps for Tx buffers. */
1304         for (i = 0; i < FV_TX_RING_CNT; i++) {
1305                 txd = &sc->fv_cdata.fv_txdesc[i];
1306                 txd->tx_m = NULL;
1307                 txd->tx_dmamap = NULL;
1308                 error = bus_dmamap_create(sc->fv_cdata.fv_tx_tag, 0,
1309                     &txd->tx_dmamap);
1310                 if (error != 0) {
1311                         device_printf(sc->fv_dev,
1312                             "failed to create Tx dmamap\n");
1313                         goto fail;
1314                 }
1315         }
1316         /* Create DMA maps for Rx buffers. */
1317         if ((error = bus_dmamap_create(sc->fv_cdata.fv_rx_tag, 0,
1318             &sc->fv_cdata.fv_rx_sparemap)) != 0) {
1319                 device_printf(sc->fv_dev,
1320                     "failed to create spare Rx dmamap\n");
1321                 goto fail;
1322         }
1323         for (i = 0; i < FV_RX_RING_CNT; i++) {
1324                 rxd = &sc->fv_cdata.fv_rxdesc[i];
1325                 rxd->rx_m = NULL;
1326                 rxd->rx_dmamap = NULL;
1327                 error = bus_dmamap_create(sc->fv_cdata.fv_rx_tag, 0,
1328                     &rxd->rx_dmamap);
1329                 if (error != 0) {
1330                         device_printf(sc->fv_dev,
1331                             "failed to create Rx dmamap\n");
1332                         goto fail;
1333                 }
1334         }
1335
1336 fail:
1337         return (error);
1338 }
1339
1340 static void
1341 fv_dma_free(struct fv_softc *sc)
1342 {
1343         struct fv_txdesc        *txd;
1344         struct fv_rxdesc        *rxd;
1345         int                     i;
1346
1347         /* Tx ring. */
1348         if (sc->fv_cdata.fv_tx_ring_tag) {
1349                 if (sc->fv_rdata.fv_tx_ring_paddr)
1350                         bus_dmamap_unload(sc->fv_cdata.fv_tx_ring_tag,
1351                             sc->fv_cdata.fv_tx_ring_map);
1352                 if (sc->fv_rdata.fv_tx_ring)
1353                         bus_dmamem_free(sc->fv_cdata.fv_tx_ring_tag,
1354                             sc->fv_rdata.fv_tx_ring,
1355                             sc->fv_cdata.fv_tx_ring_map);
1356                 sc->fv_rdata.fv_tx_ring = NULL;
1357                 sc->fv_rdata.fv_tx_ring_paddr = 0;
1358                 bus_dma_tag_destroy(sc->fv_cdata.fv_tx_ring_tag);
1359                 sc->fv_cdata.fv_tx_ring_tag = NULL;
1360         }
1361         /* Rx ring. */
1362         if (sc->fv_cdata.fv_rx_ring_tag) {
1363                 if (sc->fv_rdata.fv_rx_ring_paddr)
1364                         bus_dmamap_unload(sc->fv_cdata.fv_rx_ring_tag,
1365                             sc->fv_cdata.fv_rx_ring_map);
1366                 if (sc->fv_rdata.fv_rx_ring)
1367                         bus_dmamem_free(sc->fv_cdata.fv_rx_ring_tag,
1368                             sc->fv_rdata.fv_rx_ring,
1369                             sc->fv_cdata.fv_rx_ring_map);
1370                 sc->fv_rdata.fv_rx_ring = NULL;
1371                 sc->fv_rdata.fv_rx_ring_paddr = 0;
1372                 bus_dma_tag_destroy(sc->fv_cdata.fv_rx_ring_tag);
1373                 sc->fv_cdata.fv_rx_ring_tag = NULL;
1374         }
1375         /* Tx buffers. */
1376         if (sc->fv_cdata.fv_tx_tag) {
1377                 for (i = 0; i < FV_TX_RING_CNT; i++) {
1378                         txd = &sc->fv_cdata.fv_txdesc[i];
1379                         if (txd->tx_dmamap) {
1380                                 bus_dmamap_destroy(sc->fv_cdata.fv_tx_tag,
1381                                     txd->tx_dmamap);
1382                                 txd->tx_dmamap = NULL;
1383                         }
1384                 }
1385                 bus_dma_tag_destroy(sc->fv_cdata.fv_tx_tag);
1386                 sc->fv_cdata.fv_tx_tag = NULL;
1387         }
1388         /* Rx buffers. */
1389         if (sc->fv_cdata.fv_rx_tag) {
1390                 for (i = 0; i < FV_RX_RING_CNT; i++) {
1391                         rxd = &sc->fv_cdata.fv_rxdesc[i];
1392                         if (rxd->rx_dmamap) {
1393                                 bus_dmamap_destroy(sc->fv_cdata.fv_rx_tag,
1394                                     rxd->rx_dmamap);
1395                                 rxd->rx_dmamap = NULL;
1396                         }
1397                 }
1398                 if (sc->fv_cdata.fv_rx_sparemap) {
1399                         bus_dmamap_destroy(sc->fv_cdata.fv_rx_tag,
1400                             sc->fv_cdata.fv_rx_sparemap);
1401                         sc->fv_cdata.fv_rx_sparemap = 0;
1402                 }
1403                 bus_dma_tag_destroy(sc->fv_cdata.fv_rx_tag);
1404                 sc->fv_cdata.fv_rx_tag = NULL;
1405         }
1406
1407         if (sc->fv_cdata.fv_parent_tag) {
1408                 bus_dma_tag_destroy(sc->fv_cdata.fv_parent_tag);
1409                 sc->fv_cdata.fv_parent_tag = NULL;
1410         }
1411 }
1412
1413 /*
1414  * Initialize the transmit descriptors.
1415  */
1416 static int
1417 fv_tx_ring_init(struct fv_softc *sc)
1418 {
1419         struct fv_ring_data     *rd;
1420         struct fv_txdesc        *txd;
1421         bus_addr_t              addr;
1422         int                     i;
1423
1424         sc->fv_cdata.fv_tx_prod = 0;
1425         sc->fv_cdata.fv_tx_cons = 0;
1426         sc->fv_cdata.fv_tx_cnt = 0;
1427         sc->fv_cdata.fv_tx_pkts = 0;
1428
1429         rd = &sc->fv_rdata;
1430         bzero(rd->fv_tx_ring, FV_TX_RING_SIZE);
1431         for (i = 0; i < FV_TX_RING_CNT; i++) {
1432                 if (i == FV_TX_RING_CNT - 1)
1433                         addr = FV_TX_RING_ADDR(sc, 0);
1434                 else
1435                         addr = FV_TX_RING_ADDR(sc, i + 1);
1436                 rd->fv_tx_ring[i].fv_stat = 0;
1437                 rd->fv_tx_ring[i].fv_devcs = 0;
1438                 rd->fv_tx_ring[i].fv_addr = 0;
1439                 rd->fv_tx_ring[i].fv_link = addr;
1440                 txd = &sc->fv_cdata.fv_txdesc[i];
1441                 txd->tx_m = NULL;
1442         }
1443
1444         bus_dmamap_sync(sc->fv_cdata.fv_tx_ring_tag,
1445             sc->fv_cdata.fv_tx_ring_map,
1446             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1447
1448         return (0);
1449 }
1450
1451 /*
1452  * Initialize the RX descriptors and allocate mbufs for them. Note that
1453  * we arrange the descriptors in a closed ring, so that the last descriptor
1454  * points back to the first.
1455  */
1456 static int
1457 fv_rx_ring_init(struct fv_softc *sc)
1458 {
1459         struct fv_ring_data     *rd;
1460         struct fv_rxdesc        *rxd;
1461         int                     i;
1462
1463         sc->fv_cdata.fv_rx_cons = 0;
1464
1465         rd = &sc->fv_rdata;
1466         bzero(rd->fv_rx_ring, FV_RX_RING_SIZE);
1467         for (i = 0; i < FV_RX_RING_CNT; i++) {
1468                 rxd = &sc->fv_cdata.fv_rxdesc[i];
1469                 rxd->rx_m = NULL;
1470                 rxd->desc = &rd->fv_rx_ring[i];
1471                 rd->fv_rx_ring[i].fv_stat = ADSTAT_OWN;
1472                 rd->fv_rx_ring[i].fv_devcs = 0;
1473                 if (i == FV_RX_RING_CNT - 1)
1474                         rd->fv_rx_ring[i].fv_devcs |= ADCTL_ER;
1475                 rd->fv_rx_ring[i].fv_addr = 0;
1476                 if (fv_newbuf(sc, i) != 0)
1477                         return (ENOBUFS);
1478         }
1479
1480         bus_dmamap_sync(sc->fv_cdata.fv_rx_ring_tag,
1481             sc->fv_cdata.fv_rx_ring_map,
1482             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1483
1484         return (0);
1485 }
1486
1487 /*
1488  * Initialize an RX descriptor and attach an MBUF cluster.
1489  */
1490 static int
1491 fv_newbuf(struct fv_softc *sc, int idx)
1492 {
1493         struct fv_desc          *desc;
1494         struct fv_rxdesc        *rxd;
1495         struct mbuf             *m;
1496         bus_dma_segment_t       segs[1];
1497         bus_dmamap_t            map;
1498         int                     nsegs;
1499
1500         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1501         if (m == NULL)
1502                 return (ENOBUFS);
1503         m->m_len = m->m_pkthdr.len = MCLBYTES;
1504
1505         /* tcp header boundary alignment margin */
1506         m_adj(m, 4);
1507
1508         if (bus_dmamap_load_mbuf_sg(sc->fv_cdata.fv_rx_tag,
1509             sc->fv_cdata.fv_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1510                 m_freem(m);
1511                 return (ENOBUFS);
1512         }
1513         KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1514
1515         rxd = &sc->fv_cdata.fv_rxdesc[idx];
1516         if (rxd->rx_m != NULL) {
1517 /* This code make bug. Make scranble on buffer data. 
1518                 bus_dmamap_sync(sc->fv_cdata.fv_rx_tag, rxd->rx_dmamap,
1519                     BUS_DMASYNC_POSTREAD);
1520 */
1521                 bus_dmamap_unload(sc->fv_cdata.fv_rx_tag, rxd->rx_dmamap);
1522         }
1523         map = rxd->rx_dmamap;
1524         rxd->rx_dmamap = sc->fv_cdata.fv_rx_sparemap;
1525         sc->fv_cdata.fv_rx_sparemap = map;
1526         bus_dmamap_sync(sc->fv_cdata.fv_rx_tag, rxd->rx_dmamap,
1527             BUS_DMASYNC_PREREAD);
1528         rxd->rx_m = m;
1529         desc = rxd->desc;
1530         desc->fv_addr = segs[0].ds_addr;
1531         desc->fv_devcs |= FV_DMASIZE(segs[0].ds_len);
1532         rxd->saved_ca = desc->fv_addr ;
1533         rxd->saved_ctl = desc->fv_stat ;
1534
1535         return (0);
1536 }
1537
1538 static __inline void
1539 fv_fixup_rx(struct mbuf *m)
1540 {
1541         int             i;
1542         uint16_t        *src, *dst;
1543
1544         src = mtod(m, uint16_t *);
1545         dst = src - 1;
1546
1547         for (i = 0; i < m->m_len / sizeof(uint16_t); i++) {
1548                 *dst++ = *src++;
1549         }
1550
1551         if (m->m_len % sizeof(uint16_t))
1552                 *(uint8_t *)dst = *(uint8_t *)src;
1553
1554         m->m_data -= ETHER_ALIGN;
1555 }
1556
1557
1558 static void
1559 fv_tx(struct fv_softc *sc)
1560 {
1561         struct fv_txdesc        *txd;
1562         struct fv_desc          *cur_tx;
1563         struct ifnet            *ifp;
1564         uint32_t                ctl, devcs;
1565         int                     cons, prod, prev_cons;
1566
1567         FV_LOCK_ASSERT(sc);
1568
1569         cons = sc->fv_cdata.fv_tx_cons;
1570         prod = sc->fv_cdata.fv_tx_prod;
1571         if (cons == prod)
1572                 return;
1573
1574         bus_dmamap_sync(sc->fv_cdata.fv_tx_ring_tag,
1575             sc->fv_cdata.fv_tx_ring_map,
1576             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1577
1578         ifp = sc->fv_ifp;
1579         /*
1580          * Go through our tx list and free mbufs for those
1581          * frames that have been transmitted.
1582          */
1583         prev_cons = cons;
1584         for (; cons != prod; FV_INC(cons, FV_TX_RING_CNT)) {
1585                 cur_tx = &sc->fv_rdata.fv_tx_ring[cons];
1586                 ctl = cur_tx->fv_stat;
1587                 devcs = cur_tx->fv_devcs;
1588                 /* Check if descriptor has "finished" flag */
1589                 if (FV_DMASIZE(devcs) == 0)
1590                         break;
1591
1592                 sc->fv_cdata.fv_tx_cnt--;
1593                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1594
1595                 txd = &sc->fv_cdata.fv_txdesc[cons];
1596
1597                 if ((ctl & ADSTAT_Tx_ES) == 0)
1598                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1599                 else if (ctl & ADSTAT_Tx_UF) {   /* only underflow not check collision */
1600                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1601                 }
1602
1603                 bus_dmamap_sync(sc->fv_cdata.fv_tx_tag, txd->tx_dmamap,
1604                     BUS_DMASYNC_POSTWRITE);
1605                 bus_dmamap_unload(sc->fv_cdata.fv_tx_tag, txd->tx_dmamap);
1606
1607                 /* Free only if it's first descriptor in list */
1608                 if (txd->tx_m)
1609                         m_freem(txd->tx_m);
1610                 txd->tx_m = NULL;
1611
1612                 /* reset descriptor */
1613                 cur_tx->fv_stat = 0;
1614                 cur_tx->fv_devcs = 0;
1615                 cur_tx->fv_addr = 0;
1616         }
1617
1618         sc->fv_cdata.fv_tx_cons = cons;
1619
1620         bus_dmamap_sync(sc->fv_cdata.fv_tx_ring_tag,
1621             sc->fv_cdata.fv_tx_ring_map, BUS_DMASYNC_PREWRITE);
1622 }
1623
1624
1625 static void
1626 fv_rx(struct fv_softc *sc)
1627 {
1628         struct fv_rxdesc        *rxd;
1629         struct ifnet            *ifp = sc->fv_ifp;
1630         int                     cons, prog, packet_len, error;
1631         struct fv_desc          *cur_rx;
1632         struct mbuf             *m;
1633
1634         FV_LOCK_ASSERT(sc);
1635
1636         cons = sc->fv_cdata.fv_rx_cons;
1637
1638         bus_dmamap_sync(sc->fv_cdata.fv_rx_ring_tag,
1639             sc->fv_cdata.fv_rx_ring_map,
1640             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1641
1642         for (prog = 0; prog < FV_RX_RING_CNT; FV_INC(cons, FV_RX_RING_CNT)) {
1643                 cur_rx = &sc->fv_rdata.fv_rx_ring[cons];
1644                 rxd = &sc->fv_cdata.fv_rxdesc[cons];
1645                 m = rxd->rx_m;
1646
1647                 if ((cur_rx->fv_stat & ADSTAT_OWN) == ADSTAT_OWN)
1648                        break;   
1649                 
1650                 prog++;
1651
1652                 if (cur_rx->fv_stat & (ADSTAT_ES | ADSTAT_Rx_TL)) {
1653                         device_printf(sc->fv_dev, 
1654                             "Receive Descriptor error %x\n", cur_rx->fv_stat);
1655                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1656                         packet_len = 0;
1657                 } else {
1658                         packet_len = ADSTAT_Rx_LENGTH(cur_rx->fv_stat);
1659                 }
1660         
1661                 /* Assume it's error */
1662                 error = 1;
1663
1664                 if (packet_len < 64)
1665                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1666                 else if ((cur_rx->fv_stat & ADSTAT_Rx_DE) == 0) {
1667                         error = 0;
1668                         bus_dmamap_sync(sc->fv_cdata.fv_rx_tag, rxd->rx_dmamap,
1669                             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1670                         m = rxd->rx_m;
1671                         /* Skip 4 bytes of CRC */
1672                         m->m_pkthdr.len = m->m_len = packet_len - ETHER_CRC_LEN;
1673
1674                         fv_fixup_rx(m);
1675                         m->m_pkthdr.rcvif = ifp;
1676                         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1677
1678                         FV_UNLOCK(sc);
1679                         (*ifp->if_input)(ifp, m);
1680                         FV_LOCK(sc);
1681                 }
1682
1683                 if (error) {
1684                         /* Restore CONTROL and CA values, reset DEVCS */
1685                         cur_rx->fv_stat = rxd->saved_ctl;
1686                         cur_rx->fv_addr = rxd->saved_ca;
1687                         cur_rx->fv_devcs = 0;
1688                 }
1689                 else {
1690                         /* Reinit descriptor */
1691                         cur_rx->fv_stat = ADSTAT_OWN;
1692                         cur_rx->fv_devcs = 0;
1693                         if (cons == FV_RX_RING_CNT - 1)
1694                                 cur_rx->fv_devcs |= ADCTL_ER;
1695                         cur_rx->fv_addr = 0;
1696                         if (fv_newbuf(sc, cons) != 0) {
1697                                 device_printf(sc->fv_dev, 
1698                                     "Failed to allocate buffer\n");
1699                                 break;
1700                         }
1701                 }
1702
1703                 bus_dmamap_sync(sc->fv_cdata.fv_rx_ring_tag,
1704                     sc->fv_cdata.fv_rx_ring_map,
1705                     BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1706
1707         }
1708
1709         if (prog > 0) {
1710                 sc->fv_cdata.fv_rx_cons = cons;
1711
1712                 bus_dmamap_sync(sc->fv_cdata.fv_rx_ring_tag,
1713                     sc->fv_cdata.fv_rx_ring_map,
1714                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1715         }
1716 }
1717
1718 static void
1719 fv_intr(void *arg)
1720 {
1721         struct fv_softc         *sc = arg;
1722         uint32_t                status;
1723         struct ifnet            *ifp = sc->fv_ifp;
1724
1725         FV_LOCK(sc);
1726
1727         status = CSR_READ_4(sc, CSR_STATUS);
1728         /* mask out interrupts */
1729         while((status & sc->sc_inten) != 0) {
1730                 if (status) {
1731                         CSR_WRITE_4(sc, CSR_STATUS, status);
1732                 }
1733                 if (status & STATUS_UNF) {
1734                         device_printf(sc->fv_dev, "Transmit Underflow\n");
1735                 }
1736                 if (status & sc->sc_rxint_mask) {
1737                         fv_rx(sc);
1738                 }
1739                 if (status & sc->sc_txint_mask) {
1740                         fv_tx(sc);
1741                 }
1742                 if (status & STATUS_AIS) {
1743                         device_printf(sc->fv_dev, "Abnormal Interrupt %x\n",
1744                             status);
1745                 }
1746                 CSR_WRITE_4(sc, CSR_FULLDUP, FULLDUP_CS | 
1747                     (1 << FULLDUP_TT_SHIFT) | (3 << FULLDUP_NTP_SHIFT) | 
1748                     (2 << FULLDUP_RT_SHIFT) | (2 << FULLDUP_NRP_SHIFT));
1749
1750
1751                 status = CSR_READ_4(sc, CSR_STATUS);
1752         }
1753
1754         /* Try to get more packets going. */
1755         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1756                 fv_start_locked(ifp);
1757
1758         FV_UNLOCK(sc);
1759 }
1760
1761 static void
1762 fv_tick(void *xsc)
1763 {
1764         struct fv_softc         *sc = xsc;
1765 #ifdef MII
1766         struct mii_data         *mii;
1767
1768         FV_LOCK_ASSERT(sc);
1769
1770         mii = device_get_softc(sc->fv_miibus);
1771         mii_tick(mii);
1772 #endif
1773         callout_reset(&sc->fv_stat_callout, hz, fv_tick, sc);
1774 }
1775
1776 static void
1777 fv_hinted_child(device_t bus, const char *dname, int dunit)
1778 {
1779         BUS_ADD_CHILD(bus, 0, dname, dunit);
1780         device_printf(bus, "hinted child %s%d\n", dname, dunit);
1781 }
1782
1783 #ifdef FV_MDIO
1784 static int
1785 fvmdio_probe(device_t dev)
1786 {
1787         if (!ofw_bus_status_okay(dev))
1788                 return (ENXIO);
1789
1790         if (!ofw_bus_is_compatible(dev, "fv,mdio"))
1791                 return (ENXIO);
1792
1793         device_set_desc(dev, "FV built-in ethernet interface, MDIO controller");
1794         return(0);
1795 }
1796
1797 static int
1798 fvmdio_attach(device_t dev)
1799 {
1800         struct fv_softc *sc;
1801         int     error;
1802
1803         sc = device_get_softc(dev);
1804         sc->fv_dev = dev;
1805         sc->fv_rid = 0;
1806         sc->fv_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1807             &sc->fv_rid, RF_ACTIVE | RF_SHAREABLE);
1808         if (sc->fv_res == NULL) {
1809                 device_printf(dev, "couldn't map memory\n");
1810                 error = ENXIO;
1811                 goto fail;
1812         }
1813
1814         sc->fv_btag = rman_get_bustag(sc->fv_res);
1815         sc->fv_bhandle = rman_get_bushandle(sc->fv_res);
1816
1817         bus_generic_probe(dev);
1818         bus_enumerate_hinted_children(dev);
1819         error = bus_generic_attach(dev);
1820 fail:
1821         return(error);
1822 }
1823
1824 static int
1825 fvmdio_detach(device_t dev)
1826 {
1827         return(0);
1828 }
1829 #endif
1830
1831 #ifdef FV_DEBUG
1832 void
1833 dump_txdesc(struct fv_softc *sc, int pos)
1834 {
1835         struct fv_desc          *desc;
1836
1837         desc = &sc->fv_rdata.fv_tx_ring[pos];
1838         device_printf(sc->fv_dev, "CSR_TXLIST %08x\n", CSR_READ_4(sc, CSR_TXLIST));
1839         device_printf(sc->fv_dev, "%d TDES0:%08x TDES1:%08x TDES2:%08x TDES3:%08x\n",
1840             pos, desc->fv_stat, desc->fv_devcs, desc->fv_addr, desc->fv_link);
1841 }
1842
1843 void 
1844 dump_status_reg(struct fv_softc *sc)
1845 {
1846         uint32_t                status;
1847
1848         /* mask out interrupts */
1849
1850         status = CSR_READ_4(sc, CSR_STATUS);
1851         device_printf(sc->fv_dev, "CSR5 Status Register EB:%d TS:%d RS:%d NIS:%d AIS:%d ER:%d SE:%d LNF:%d TM:%d RWT:%d RPS:%d RU:%d RI:%d UNF:%d LNP/ANC:%d TJT:%d TU:%d TPS:%d TI:%d\n", 
1852             (status >> 23 ) & 7,
1853             (status >> 20 ) & 7,
1854             (status >> 17 ) & 7,
1855             (status >> 16 ) & 1,
1856             (status >> 15 ) & 1,
1857             (status >> 14 ) & 1,
1858             (status >> 13 ) & 1,
1859             (status >> 12 ) & 1,
1860             (status >> 11 ) & 1,
1861             (status >> 9 ) & 1,
1862             (status >> 8 ) & 1,
1863             (status >> 7 ) & 1,
1864             (status >> 6 ) & 1,
1865             (status >> 5 ) & 1,
1866             (status >> 4 ) & 1,
1867             (status >> 3 ) & 1,
1868             (status >> 2 ) & 1,
1869             (status >> 1 ) & 1,
1870             (status >> 0 ) & 1);
1871
1872 }
1873 #endif