]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/tx/if_tx.c
Merge ACPICA 20140926.
[FreeBSD/FreeBSD.git] / sys / dev / tx / if_tx.c
1 /*-
2  * Copyright (c) 1997 Semen Ustimenko (semenu@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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * EtherPower II 10/100 Fast Ethernet (SMC 9432 serie)
32  *
33  * These cards are based on SMC83c17x (EPIC) chip and one of the various
34  * PHYs (QS6612, AC101 and LXT970 were seen). The media support depends on
35  * card model. All cards support 10baseT/UTP and 100baseTX half- and full-
36  * duplex (SMB9432TX). SMC9432BTX also supports 10baseT/BNC. SMC9432FTX also
37  * supports fibre optics.
38  *
39  * Thanks are going to Steve Bauer and Jason Wright.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sockio.h>
45 #include <sys/mbuf.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/socket.h>
49 #include <sys/queue.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
58
59 #include <net/bpf.h>
60
61 #include <net/if_vlan_var.h>
62
63 #include <machine/bus.h>
64 #include <machine/resource.h>
65 #include <sys/bus.h>
66 #include <sys/rman.h>
67
68 #include <dev/pci/pcireg.h>
69 #include <dev/pci/pcivar.h>
70
71 #include <dev/mii/mii.h>
72 #include <dev/mii/miivar.h>
73 #include "miidevs.h"
74
75 #include <dev/mii/lxtphyreg.h>
76
77 #include "miibus_if.h"
78
79 #include <dev/tx/if_txreg.h>
80 #include <dev/tx/if_txvar.h>
81
82 MODULE_DEPEND(tx, pci, 1, 1, 1);
83 MODULE_DEPEND(tx, ether, 1, 1, 1);
84 MODULE_DEPEND(tx, miibus, 1, 1, 1);
85
86 static int epic_ifioctl(struct ifnet *, u_long, caddr_t);
87 static void epic_intr(void *);
88 static void epic_tx_underrun(epic_softc_t *);
89 static void epic_ifstart(struct ifnet *);
90 static void epic_ifstart_locked(struct ifnet *);
91 static void epic_timer(void *);
92 static void epic_init(void *);
93 static void epic_init_locked(epic_softc_t *);
94 static void epic_stop(epic_softc_t *);
95 static void epic_rx_done(epic_softc_t *);
96 static void epic_tx_done(epic_softc_t *);
97 static int epic_init_rings(epic_softc_t *);
98 static void epic_free_rings(epic_softc_t *);
99 static void epic_stop_activity(epic_softc_t *);
100 static int epic_queue_last_packet(epic_softc_t *);
101 static void epic_start_activity(epic_softc_t *);
102 static void epic_set_rx_mode(epic_softc_t *);
103 static void epic_set_tx_mode(epic_softc_t *);
104 static void epic_set_mc_table(epic_softc_t *);
105 static int epic_read_eeprom(epic_softc_t *,u_int16_t);
106 static void epic_output_eepromw(epic_softc_t *, u_int16_t);
107 static u_int16_t epic_input_eepromw(epic_softc_t *);
108 static u_int8_t epic_eeprom_clock(epic_softc_t *,u_int8_t);
109 static void epic_write_eepromreg(epic_softc_t *,u_int8_t);
110 static u_int8_t epic_read_eepromreg(epic_softc_t *);
111
112 static int epic_read_phy_reg(epic_softc_t *, int, int);
113 static void epic_write_phy_reg(epic_softc_t *, int, int, int);
114
115 static int epic_miibus_readreg(device_t, int, int);
116 static int epic_miibus_writereg(device_t, int, int, int);
117 static void epic_miibus_statchg(device_t);
118 static void epic_miibus_mediainit(device_t);
119
120 static int epic_ifmedia_upd(struct ifnet *);
121 static int epic_ifmedia_upd_locked(struct ifnet *);
122 static void epic_ifmedia_sts(struct ifnet *, struct ifmediareq *);
123
124 static int epic_probe(device_t);
125 static int epic_attach(device_t);
126 static int epic_shutdown(device_t);
127 static int epic_detach(device_t);
128 static void epic_release(epic_softc_t *);
129 static struct epic_type *epic_devtype(device_t);
130
131 static device_method_t epic_methods[] = {
132         /* Device interface */
133         DEVMETHOD(device_probe,         epic_probe),
134         DEVMETHOD(device_attach,        epic_attach),
135         DEVMETHOD(device_detach,        epic_detach),
136         DEVMETHOD(device_shutdown,      epic_shutdown),
137
138         /* MII interface */
139         DEVMETHOD(miibus_readreg,       epic_miibus_readreg),
140         DEVMETHOD(miibus_writereg,      epic_miibus_writereg),
141         DEVMETHOD(miibus_statchg,       epic_miibus_statchg),
142         DEVMETHOD(miibus_mediainit,     epic_miibus_mediainit),
143
144         { 0, 0 }
145 };
146
147 static driver_t epic_driver = {
148         "tx",
149         epic_methods,
150         sizeof(epic_softc_t)
151 };
152
153 static devclass_t epic_devclass;
154
155 DRIVER_MODULE(tx, pci, epic_driver, epic_devclass, 0, 0);
156 DRIVER_MODULE(miibus, tx, miibus_driver, miibus_devclass, 0, 0);
157
158 static struct epic_type epic_devs[] = {
159         { SMC_VENDORID, SMC_DEVICEID_83C170, "SMC EtherPower II 10/100" },
160         { 0, 0, NULL }
161 };
162
163 static int
164 epic_probe(device_t dev)
165 {
166         struct epic_type *t;
167
168         t = epic_devtype(dev);
169
170         if (t != NULL) {
171                 device_set_desc(dev, t->name);
172                 return (BUS_PROBE_DEFAULT);
173         }
174
175         return (ENXIO);
176 }
177
178 static struct epic_type *
179 epic_devtype(device_t dev)
180 {
181         struct epic_type *t;
182
183         t = epic_devs;
184
185         while (t->name != NULL) {
186                 if ((pci_get_vendor(dev) == t->ven_id) &&
187                     (pci_get_device(dev) == t->dev_id)) {
188                         return (t);
189                 }
190                 t++;
191         }
192         return (NULL);
193 }
194
195 #ifdef EPIC_USEIOSPACE
196 #define EPIC_RES        SYS_RES_IOPORT
197 #define EPIC_RID        PCIR_BASEIO
198 #else
199 #define EPIC_RES        SYS_RES_MEMORY
200 #define EPIC_RID        PCIR_BASEMEM
201 #endif
202
203 static void
204 epic_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
205 {
206         u_int32_t *addr;
207
208         if (error)
209                 return;
210
211         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
212         addr = arg;
213         *addr = segs->ds_addr;
214 }
215
216 /*
217  * Attach routine: map registers, allocate softc, rings and descriptors.
218  * Reset to known state.
219  */
220 static int
221 epic_attach(device_t dev)
222 {
223         struct ifnet *ifp;
224         epic_softc_t *sc;
225         int error;
226         int i, rid, tmp;
227         u_char eaddr[6];
228
229         sc = device_get_softc(dev);
230
231         /* Preinitialize softc structure. */
232         sc->dev = dev;
233         mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
234             MTX_DEF);
235
236         /* Fill ifnet structure. */
237         ifp = sc->ifp = if_alloc(IFT_ETHER);
238         if (ifp == NULL) {
239                 device_printf(dev, "can not if_alloc()\n");
240                 error = ENOSPC;
241                 goto fail;
242         }
243         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
244         ifp->if_softc = sc;
245         ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
246         ifp->if_ioctl = epic_ifioctl;
247         ifp->if_start = epic_ifstart;
248         ifp->if_init = epic_init;
249         IFQ_SET_MAXLEN(&ifp->if_snd, TX_RING_SIZE - 1);
250
251         /* Enable busmastering. */
252         pci_enable_busmaster(dev);
253
254         rid = EPIC_RID;
255         sc->res = bus_alloc_resource_any(dev, EPIC_RES, &rid, RF_ACTIVE);
256         if (sc->res == NULL) {
257                 device_printf(dev, "couldn't map ports/memory\n");
258                 error = ENXIO;
259                 goto fail;
260         }
261
262         /* Allocate interrupt. */
263         rid = 0;
264         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
265             RF_SHAREABLE | RF_ACTIVE);
266         if (sc->irq == NULL) {
267                 device_printf(dev, "couldn't map interrupt\n");
268                 error = ENXIO;
269                 goto fail;
270         }
271
272         /* Allocate DMA tags. */
273         error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
274             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
275             MCLBYTES * EPIC_MAX_FRAGS, EPIC_MAX_FRAGS, MCLBYTES, 0, NULL, NULL,
276             &sc->mtag);
277         if (error) {
278                 device_printf(dev, "couldn't allocate dma tag\n");
279                 goto fail;
280         }
281
282         error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
283             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
284             sizeof(struct epic_rx_desc) * RX_RING_SIZE,
285             1, sizeof(struct epic_rx_desc) * RX_RING_SIZE, 0, NULL,
286             NULL, &sc->rtag);
287         if (error) {
288                 device_printf(dev, "couldn't allocate dma tag\n");
289                 goto fail;
290         }
291
292         error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
293             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
294             sizeof(struct epic_tx_desc) * TX_RING_SIZE,
295             1, sizeof(struct epic_tx_desc) * TX_RING_SIZE, 0,
296             NULL, NULL, &sc->ttag);
297         if (error) {
298                 device_printf(dev, "couldn't allocate dma tag\n");
299                 goto fail;
300         }
301
302         error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
303             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
304             sizeof(struct epic_frag_list) * TX_RING_SIZE,
305             1, sizeof(struct epic_frag_list) * TX_RING_SIZE, 0,
306             NULL, NULL, &sc->ftag);
307         if (error) {
308                 device_printf(dev, "couldn't allocate dma tag\n");
309                 goto fail;
310         }
311
312         /* Allocate DMA safe memory and get the DMA addresses. */
313         error = bus_dmamem_alloc(sc->ftag, (void **)&sc->tx_flist,
314             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fmap);
315         if (error) {
316                 device_printf(dev, "couldn't allocate dma memory\n");
317                 goto fail;
318         }
319         error = bus_dmamap_load(sc->ftag, sc->fmap, sc->tx_flist,
320             sizeof(struct epic_frag_list) * TX_RING_SIZE, epic_dma_map_addr,
321             &sc->frag_addr, 0);
322         if (error) {
323                 device_printf(dev, "couldn't map dma memory\n");
324                 goto fail;
325         }
326         error = bus_dmamem_alloc(sc->ttag, (void **)&sc->tx_desc,
327             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tmap);
328         if (error) {
329                 device_printf(dev, "couldn't allocate dma memory\n");
330                 goto fail;
331         }
332         error = bus_dmamap_load(sc->ttag, sc->tmap, sc->tx_desc,
333             sizeof(struct epic_tx_desc) * TX_RING_SIZE, epic_dma_map_addr,
334             &sc->tx_addr, 0);
335         if (error) {
336                 device_printf(dev, "couldn't map dma memory\n");
337                 goto fail;
338         }
339         error = bus_dmamem_alloc(sc->rtag, (void **)&sc->rx_desc,
340             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rmap);
341         if (error) {
342                 device_printf(dev, "couldn't allocate dma memory\n");
343                 goto fail;
344         }
345         error = bus_dmamap_load(sc->rtag, sc->rmap, sc->rx_desc,
346             sizeof(struct epic_rx_desc) * RX_RING_SIZE, epic_dma_map_addr,
347             &sc->rx_addr, 0);
348         if (error) {
349                 device_printf(dev, "couldn't map dma memory\n");
350                 goto fail;
351         }
352
353         /* Bring the chip out of low-power mode. */
354         CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
355         DELAY(500);
356
357         /* Workaround for Application Note 7-15. */
358         for (i = 0; i < 16; i++)
359                 CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
360
361         /* Read MAC address from EEPROM. */
362         for (i = 0; i < ETHER_ADDR_LEN / sizeof(u_int16_t); i++)
363                 ((u_int16_t *)eaddr)[i] = epic_read_eeprom(sc,i);
364
365         /* Set Non-Volatile Control Register from EEPROM. */
366         CSR_WRITE_4(sc, NVCTL, epic_read_eeprom(sc, EEPROM_NVCTL) & 0x1F);
367
368         /* Set defaults. */
369         sc->tx_threshold = TRANSMIT_THRESHOLD;
370         sc->txcon = TXCON_DEFAULT;
371         sc->miicfg = MIICFG_SMI_ENABLE;
372         sc->phyid = EPIC_UNKN_PHY;
373         sc->serinst = -1;
374
375         /* Fetch card id. */
376         sc->cardvend = pci_read_config(dev, PCIR_SUBVEND_0, 2);
377         sc->cardid = pci_read_config(dev, PCIR_SUBDEV_0, 2);
378
379         if (sc->cardvend != SMC_VENDORID)
380                 device_printf(dev, "unknown card vendor %04xh\n", sc->cardvend);
381
382         /* Do ifmedia setup. */
383         error = mii_attach(dev, &sc->miibus, ifp, epic_ifmedia_upd,
384             epic_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
385         if (error != 0) {
386                 device_printf(dev, "attaching PHYs failed\n");
387                 goto fail;
388         }
389
390         /* board type and ... */
391         printf(" type ");
392         for(i = 0x2c; i < 0x32; i++) {
393                 tmp = epic_read_eeprom(sc, i);
394                 if (' ' == (u_int8_t)tmp)
395                         break;
396                 printf("%c", (u_int8_t)tmp);
397                 tmp >>= 8;
398                 if (' ' == (u_int8_t)tmp)
399                         break;
400                 printf("%c", (u_int8_t)tmp);
401         }
402         printf("\n");
403
404         /* Initialize rings. */
405         if (epic_init_rings(sc)) {
406                 device_printf(dev, "failed to init rings\n");
407                 error = ENXIO;
408                 goto fail;
409         }
410
411         ifp->if_hdrlen = sizeof(struct ether_vlan_header);
412         ifp->if_capabilities |= IFCAP_VLAN_MTU;
413         ifp->if_capenable |= IFCAP_VLAN_MTU;
414         callout_init_mtx(&sc->timer, &sc->lock, 0);
415
416         /* Attach to OS's managers. */
417         ether_ifattach(ifp, eaddr);
418
419         /* Activate our interrupt handler. */
420         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
421             NULL, epic_intr, sc, &sc->sc_ih);
422         if (error) {
423                 device_printf(dev, "couldn't set up irq\n");
424                 ether_ifdetach(ifp);
425                 goto fail;
426         }
427
428         return (0);
429 fail:
430         epic_release(sc);
431         return (error);
432 }
433
434 /*
435  * Free any resources allocated by the driver.
436  */
437 static void
438 epic_release(epic_softc_t *sc)
439 {
440         if (sc->ifp != NULL)
441                 if_free(sc->ifp);
442         if (sc->irq)
443                 bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
444         if (sc->res)
445                 bus_release_resource(sc->dev, EPIC_RES, EPIC_RID, sc->res);
446         epic_free_rings(sc);
447         if (sc->tx_flist) {
448                 bus_dmamap_unload(sc->ftag, sc->fmap);
449                 bus_dmamem_free(sc->ftag, sc->tx_flist, sc->fmap);
450         }
451         if (sc->tx_desc) {
452                 bus_dmamap_unload(sc->ttag, sc->tmap);
453                 bus_dmamem_free(sc->ttag, sc->tx_desc, sc->tmap);
454         }
455         if (sc->rx_desc) {
456                 bus_dmamap_unload(sc->rtag, sc->rmap);
457                 bus_dmamem_free(sc->rtag, sc->rx_desc, sc->rmap);
458         }
459         if (sc->mtag)
460                 bus_dma_tag_destroy(sc->mtag);
461         if (sc->ftag)
462                 bus_dma_tag_destroy(sc->ftag);
463         if (sc->ttag)
464                 bus_dma_tag_destroy(sc->ttag);
465         if (sc->rtag)
466                 bus_dma_tag_destroy(sc->rtag);
467         mtx_destroy(&sc->lock);
468 }
469
470 /*
471  * Detach driver and free resources.
472  */
473 static int
474 epic_detach(device_t dev)
475 {
476         struct ifnet *ifp;
477         epic_softc_t *sc;
478
479         sc = device_get_softc(dev);
480         ifp = sc->ifp;
481
482         EPIC_LOCK(sc);
483         epic_stop(sc);
484         EPIC_UNLOCK(sc);
485         callout_drain(&sc->timer);
486         ether_ifdetach(ifp);
487         bus_teardown_intr(dev, sc->irq, sc->sc_ih);
488
489         bus_generic_detach(dev);
490         device_delete_child(dev, sc->miibus);
491
492         epic_release(sc);
493         return (0);
494 }
495
496 #undef  EPIC_RES
497 #undef  EPIC_RID
498
499 /*
500  * Stop all chip I/O so that the kernel's probe routines don't
501  * get confused by errant DMAs when rebooting.
502  */
503 static int
504 epic_shutdown(device_t dev)
505 {
506         epic_softc_t *sc;
507
508         sc = device_get_softc(dev);
509
510         EPIC_LOCK(sc);
511         epic_stop(sc);
512         EPIC_UNLOCK(sc);
513         return (0);
514 }
515
516 /*
517  * This is if_ioctl handler.
518  */
519 static int
520 epic_ifioctl(struct ifnet *ifp, u_long command, caddr_t data)
521 {
522         epic_softc_t *sc = ifp->if_softc;
523         struct mii_data *mii;
524         struct ifreq *ifr = (struct ifreq *) data;
525         int error = 0;
526
527         switch (command) {
528         case SIOCSIFMTU:
529                 if (ifp->if_mtu == ifr->ifr_mtu)
530                         break;
531
532                 /* XXX Though the datasheet doesn't imply any
533                  * limitations on RX and TX sizes beside max 64Kb
534                  * DMA transfer, seems we can't send more then 1600
535                  * data bytes per ethernet packet (transmitter hangs
536                  * up if more data is sent).
537                  */
538                 EPIC_LOCK(sc);
539                 if (ifr->ifr_mtu + ifp->if_hdrlen <= EPIC_MAX_MTU) {
540                         ifp->if_mtu = ifr->ifr_mtu;
541                         epic_stop(sc);
542                         epic_init_locked(sc);
543                 } else
544                         error = EINVAL;
545                 EPIC_UNLOCK(sc);
546                 break;
547
548         case SIOCSIFFLAGS:
549                 /*
550                  * If the interface is marked up and stopped, then start it.
551                  * If it is marked down and running, then stop it.
552                  */
553                 EPIC_LOCK(sc);
554                 if (ifp->if_flags & IFF_UP) {
555                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
556                                 epic_init_locked(sc);
557                                 EPIC_UNLOCK(sc);
558                                 break;
559                         }
560                 } else {
561                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
562                                 epic_stop(sc);
563                                 EPIC_UNLOCK(sc);
564                                 break;
565                         }
566                 }
567
568                 /* Handle IFF_PROMISC and IFF_ALLMULTI flags. */
569                 epic_stop_activity(sc);
570                 epic_set_mc_table(sc);
571                 epic_set_rx_mode(sc);
572                 epic_start_activity(sc);
573                 EPIC_UNLOCK(sc);
574                 break;
575
576         case SIOCADDMULTI:
577         case SIOCDELMULTI:
578                 EPIC_LOCK(sc);
579                 epic_set_mc_table(sc);
580                 EPIC_UNLOCK(sc);
581                 error = 0;
582                 break;
583
584         case SIOCSIFMEDIA:
585         case SIOCGIFMEDIA:
586                 mii = device_get_softc(sc->miibus);
587                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
588                 break;
589
590         default:
591                 error = ether_ioctl(ifp, command, data);
592                 break;
593         }
594         return (error);
595 }
596
597 static void
598 epic_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg,
599     bus_size_t mapsize, int error)
600 {
601         struct epic_frag_list *flist;
602         int i;
603
604         if (error)
605                 return;
606
607         KASSERT(nseg <= EPIC_MAX_FRAGS, ("too many DMA segments"));
608         flist = arg;
609         /* Fill fragments list. */
610         for (i = 0; i < nseg; i++) {
611                 KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large"));
612                 flist->frag[i].fraglen = segs[i].ds_len;
613                 flist->frag[i].fragaddr = segs[i].ds_addr;
614         }
615         flist->numfrags = nseg;
616 }
617
618 static void
619 epic_dma_map_rxbuf(void *arg, bus_dma_segment_t *segs, int nseg,
620     bus_size_t mapsize, int error)
621 {
622         struct epic_rx_desc *desc;
623
624         if (error)
625                 return;
626
627         KASSERT(nseg == 1, ("too many DMA segments"));
628         desc = arg;
629         desc->bufaddr = segs->ds_addr;
630 }
631
632 /*
633  * This is if_start handler. It takes mbufs from if_snd queue
634  * and queue them for transmit, one by one, until TX ring become full
635  * or queue become empty.
636  */
637 static void
638 epic_ifstart(struct ifnet * ifp)
639 {
640         epic_softc_t *sc = ifp->if_softc;
641
642         EPIC_LOCK(sc);
643         epic_ifstart_locked(ifp);
644         EPIC_UNLOCK(sc);
645 }
646
647 static void
648 epic_ifstart_locked(struct ifnet * ifp)
649 {
650         epic_softc_t *sc = ifp->if_softc;
651         struct epic_tx_buffer *buf;
652         struct epic_tx_desc *desc;
653         struct epic_frag_list *flist;
654         struct mbuf *m0, *m;
655         int error;
656
657         while (sc->pending_txs < TX_RING_SIZE) {
658                 buf = sc->tx_buffer + sc->cur_tx;
659                 desc = sc->tx_desc + sc->cur_tx;
660                 flist = sc->tx_flist + sc->cur_tx;
661
662                 /* Get next packet to send. */
663                 IF_DEQUEUE(&ifp->if_snd, m0);
664
665                 /* If nothing to send, return. */
666                 if (m0 == NULL)
667                         return;
668
669                 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
670                     epic_dma_map_txbuf, flist, 0);
671
672                 if (error && error != EFBIG) {
673                         m_freem(m0);
674                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
675                         continue;
676                 }
677
678                 /*
679                  * If packet was more than EPIC_MAX_FRAGS parts,
680                  * recopy packet to a newly allocated mbuf cluster.
681                  */
682                 if (error) {
683                         m = m_defrag(m0, M_NOWAIT);
684                         if (m == NULL) {
685                                 m_freem(m0);
686                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
687                                 continue;
688                         }
689                         m_freem(m0);
690                         m0 = m;
691
692                         error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m,
693                             epic_dma_map_txbuf, flist, 0);
694                         if (error) {
695                                 m_freem(m);
696                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
697                                 continue;
698                         }
699                 }
700                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
701
702                 buf->mbuf = m0;
703                 sc->pending_txs++;
704                 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
705                 desc->control = 0x01;
706                 desc->txlength =
707                     max(m0->m_pkthdr.len, ETHER_MIN_LEN - ETHER_CRC_LEN);
708                 desc->status = 0x8000;
709                 bus_dmamap_sync(sc->ttag, sc->tmap,
710                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
711                 bus_dmamap_sync(sc->ftag, sc->fmap, BUS_DMASYNC_PREWRITE);
712                 CSR_WRITE_4(sc, COMMAND, COMMAND_TXQUEUED);
713
714                 /* Set watchdog timer. */
715                 sc->tx_timeout = 8;
716
717                 BPF_MTAP(ifp, m0);
718         }
719
720         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
721 }
722
723 /*
724  * Synopsis: Finish all received frames.
725  */
726 static void
727 epic_rx_done(epic_softc_t *sc)
728 {
729         struct ifnet *ifp = sc->ifp;
730         u_int16_t len;
731         struct epic_rx_buffer *buf;
732         struct epic_rx_desc *desc;
733         struct mbuf *m;
734         bus_dmamap_t map;
735         int error;
736
737         bus_dmamap_sync(sc->rtag, sc->rmap, BUS_DMASYNC_POSTREAD);
738         while ((sc->rx_desc[sc->cur_rx].status & 0x8000) == 0) {
739                 buf = sc->rx_buffer + sc->cur_rx;
740                 desc = sc->rx_desc + sc->cur_rx;
741
742                 /* Switch to next descriptor. */
743                 sc->cur_rx = (sc->cur_rx + 1) & RX_RING_MASK;
744
745                 /*
746                  * Check for RX errors. This should only happen if
747                  * SAVE_ERRORED_PACKETS is set. RX errors generate
748                  * RXE interrupt usually.
749                  */
750                 if ((desc->status & 1) == 0) {
751                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
752                         desc->status = 0x8000;
753                         continue;
754                 }
755
756                 /* Save packet length and mbuf contained packet. */
757                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTREAD);
758                 len = desc->rxlength - ETHER_CRC_LEN;
759                 m = buf->mbuf;
760
761                 /* Try to get an mbuf cluster. */
762                 buf->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
763                 if (buf->mbuf == NULL) {
764                         buf->mbuf = m;
765                         desc->status = 0x8000;
766                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
767                         continue;
768                 }
769                 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
770                 m_adj(buf->mbuf, ETHER_ALIGN);
771
772                 /* Point to new mbuf, and give descriptor to chip. */
773                 error = bus_dmamap_load_mbuf(sc->mtag, sc->sparemap, buf->mbuf,
774                     epic_dma_map_rxbuf, desc, 0);
775                 if (error) {
776                         buf->mbuf = m;
777                         desc->status = 0x8000;
778                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
779                         continue;
780                 }
781
782                 desc->status = 0x8000;
783                 bus_dmamap_unload(sc->mtag, buf->map);
784                 map = buf->map;
785                 buf->map = sc->sparemap;
786                 sc->sparemap = map;
787                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
788
789                 /* First mbuf in packet holds the ethernet and packet headers */
790                 m->m_pkthdr.rcvif = ifp;
791                 m->m_pkthdr.len = m->m_len = len;
792
793                 /* Give mbuf to OS. */
794                 EPIC_UNLOCK(sc);
795                 (*ifp->if_input)(ifp, m);
796                 EPIC_LOCK(sc);
797
798                 /* Successfuly received frame */
799                 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
800         }
801         bus_dmamap_sync(sc->rtag, sc->rmap,
802             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
803 }
804
805 /*
806  * Synopsis: Do last phase of transmission. I.e. if desc is
807  * transmitted, decrease pending_txs counter, free mbuf contained
808  * packet, switch to next descriptor and repeat until no packets
809  * are pending or descriptor is not transmitted yet.
810  */
811 static void
812 epic_tx_done(epic_softc_t *sc)
813 {
814         struct epic_tx_buffer *buf;
815         struct epic_tx_desc *desc;
816         u_int16_t status;
817
818         bus_dmamap_sync(sc->ttag, sc->tmap, BUS_DMASYNC_POSTREAD);
819         while (sc->pending_txs > 0) {
820                 buf = sc->tx_buffer + sc->dirty_tx;
821                 desc = sc->tx_desc + sc->dirty_tx;
822                 status = desc->status;
823
824                 /*
825                  * If packet is not transmitted, thou followed
826                  * packets are not transmitted too.
827                  */
828                 if (status & 0x8000)
829                         break;
830
831                 /* Packet is transmitted. Switch to next and free mbuf. */
832                 sc->pending_txs--;
833                 sc->dirty_tx = (sc->dirty_tx + 1) & TX_RING_MASK;
834                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_POSTWRITE);
835                 bus_dmamap_unload(sc->mtag, buf->map);
836                 m_freem(buf->mbuf);
837                 buf->mbuf = NULL;
838
839                 /* Check for errors and collisions. */
840                 if (status & 0x0001)
841                         if_inc_counter(sc->ifp, IFCOUNTER_OPACKETS, 1);
842                 else
843                         if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, 1);
844                 if_inc_counter(sc->ifp, IFCOUNTER_COLLISIONS, (status >> 8) & 0x1F);
845 #ifdef EPIC_DIAG
846                 if ((status & 0x1001) == 0x1001)
847                         device_printf(sc->dev,
848                             "Tx ERROR: excessive coll. number\n");
849 #endif
850         }
851
852         if (sc->pending_txs < TX_RING_SIZE)
853                 sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
854         bus_dmamap_sync(sc->ttag, sc->tmap,
855             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
856 }
857
858 /*
859  * Interrupt function
860  */
861 static void
862 epic_intr(void *arg)
863 {
864     epic_softc_t *sc;
865     int status, i;
866
867     sc = arg;
868     i = 4;
869     EPIC_LOCK(sc);
870     while (i-- && ((status = CSR_READ_4(sc, INTSTAT)) & INTSTAT_INT_ACTV)) {
871         CSR_WRITE_4(sc, INTSTAT, status);
872
873         if (status & (INTSTAT_RQE|INTSTAT_RCC|INTSTAT_OVW)) {
874             epic_rx_done(sc);
875             if (status & (INTSTAT_RQE|INTSTAT_OVW)) {
876 #ifdef EPIC_DIAG
877                 if (status & INTSTAT_OVW)
878                     device_printf(sc->dev, "RX buffer overflow\n");
879                 if (status & INTSTAT_RQE)
880                     device_printf(sc->dev, "RX FIFO overflow\n");
881 #endif
882                 if ((CSR_READ_4(sc, COMMAND) & COMMAND_RXQUEUED) == 0)
883                     CSR_WRITE_4(sc, COMMAND, COMMAND_RXQUEUED);
884                 if_inc_counter(sc->ifp, IFCOUNTER_IERRORS, 1);
885             }
886         }
887
888         if (status & (INTSTAT_TXC|INTSTAT_TCC|INTSTAT_TQE)) {
889             epic_tx_done(sc);
890             if (sc->ifp->if_snd.ifq_head != NULL)
891                     epic_ifstart_locked(sc->ifp);
892         }
893
894         /* Check for rare errors */
895         if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
896                       INTSTAT_APE|INTSTAT_DPE|INTSTAT_TXU|INTSTAT_RXE)) {
897             if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
898                           INTSTAT_APE|INTSTAT_DPE)) {
899                 device_printf(sc->dev, "PCI fatal errors occured: %s%s%s%s\n",
900                     (status & INTSTAT_PMA) ? "PMA " : "",
901                     (status & INTSTAT_PTA) ? "PTA " : "",
902                     (status & INTSTAT_APE) ? "APE " : "",
903                     (status & INTSTAT_DPE) ? "DPE" : "");
904
905                 epic_stop(sc);
906                 epic_init_locked(sc);
907                 break;
908             }
909
910             if (status & INTSTAT_RXE) {
911 #ifdef EPIC_DIAG
912                 device_printf(sc->dev, "CRC/Alignment error\n");
913 #endif
914                 if_inc_counter(sc->ifp, IFCOUNTER_IERRORS, 1);
915             }
916
917             if (status & INTSTAT_TXU) {
918                 epic_tx_underrun(sc);
919                 if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, 1);
920             }
921         }
922     }
923
924     /* If no packets are pending, then no timeouts. */
925     if (sc->pending_txs == 0)
926             sc->tx_timeout = 0;
927     EPIC_UNLOCK(sc);
928 }
929
930 /*
931  * Handle the TX underrun error: increase the TX threshold
932  * and restart the transmitter.
933  */
934 static void
935 epic_tx_underrun(epic_softc_t *sc)
936 {
937         if (sc->tx_threshold > TRANSMIT_THRESHOLD_MAX) {
938                 sc->txcon &= ~TXCON_EARLY_TRANSMIT_ENABLE;
939 #ifdef EPIC_DIAG
940                 device_printf(sc->dev, "Tx UNDERRUN: early TX disabled\n");
941 #endif
942         } else {
943                 sc->tx_threshold += 0x40;
944 #ifdef EPIC_DIAG
945                 device_printf(sc->dev,
946                     "Tx UNDERRUN: TX threshold increased to %d\n",
947                     sc->tx_threshold);
948 #endif
949         }
950
951         /* We must set TXUGO to reset the stuck transmitter. */
952         CSR_WRITE_4(sc, COMMAND, COMMAND_TXUGO);
953
954         /* Update the TX threshold */
955         epic_stop_activity(sc);
956         epic_set_tx_mode(sc);
957         epic_start_activity(sc);
958 }
959
960 /*
961  * This function is called once a second when the interface is running
962  * and performs two functions.  First, it provides a timer for the mii
963  * to help with autonegotiation.  Second, it checks for transmit
964  * timeouts.
965  */
966 static void
967 epic_timer(void *arg)
968 {
969         epic_softc_t *sc = arg;
970         struct mii_data *mii;
971         struct ifnet *ifp;
972
973         ifp = sc->ifp;
974         EPIC_ASSERT_LOCKED(sc);
975         if (sc->tx_timeout && --sc->tx_timeout == 0) {
976                 device_printf(sc->dev, "device timeout %d packets\n",
977                     sc->pending_txs);
978
979                 /* Try to finish queued packets. */
980                 epic_tx_done(sc);
981
982                 /* If not successful. */
983                 if (sc->pending_txs > 0) {
984                         if_inc_counter(ifp, IFCOUNTER_OERRORS, sc->pending_txs);
985
986                         /* Reinitialize board. */
987                         device_printf(sc->dev, "reinitialization\n");
988                         epic_stop(sc);
989                         epic_init_locked(sc);
990                 } else
991                         device_printf(sc->dev,
992                             "seems we can continue normaly\n");
993
994                 /* Start output. */
995                 if (ifp->if_snd.ifq_head)
996                         epic_ifstart_locked(ifp);
997         }
998
999         mii = device_get_softc(sc->miibus);
1000         mii_tick(mii);
1001
1002         callout_reset(&sc->timer, hz, epic_timer, sc);
1003 }
1004
1005 /*
1006  * Set media options.
1007  */
1008 static int
1009 epic_ifmedia_upd(struct ifnet *ifp)
1010 {
1011         epic_softc_t *sc;
1012         int error;
1013
1014         sc = ifp->if_softc;
1015         EPIC_LOCK(sc);
1016         error = epic_ifmedia_upd_locked(ifp);
1017         EPIC_UNLOCK(sc);
1018         return (error);
1019 }
1020         
1021 static int
1022 epic_ifmedia_upd_locked(struct ifnet *ifp)
1023 {
1024         epic_softc_t *sc;
1025         struct mii_data *mii;
1026         struct ifmedia *ifm;
1027         struct mii_softc *miisc;
1028         int cfg, media;
1029
1030         sc = ifp->if_softc;
1031         mii = device_get_softc(sc->miibus);
1032         ifm = &mii->mii_media;
1033         media = ifm->ifm_cur->ifm_media;
1034
1035         /* Do not do anything if interface is not up. */
1036         if ((ifp->if_flags & IFF_UP) == 0)
1037                 return (0);
1038
1039         /*
1040          * Lookup current selected PHY.
1041          */
1042         if (IFM_INST(media) == sc->serinst) {
1043                 sc->phyid = EPIC_SERIAL;
1044                 sc->physc = NULL;
1045         } else {
1046                 /* If we're not selecting serial interface, select MII mode. */
1047                 sc->miicfg &= ~MIICFG_SERIAL_ENABLE;
1048                 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1049
1050                 /* Default to unknown PHY. */
1051                 sc->phyid = EPIC_UNKN_PHY;
1052
1053                 /* Lookup selected PHY. */
1054                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1055                         if (IFM_INST(media) == miisc->mii_inst) {
1056                                 sc->physc = miisc;
1057                                 break;
1058                         }
1059                 }
1060
1061                 /* Identify selected PHY. */
1062                 if (sc->physc) {
1063                         int id1, id2, model, oui;
1064
1065                         id1 = PHY_READ(sc->physc, MII_PHYIDR1);
1066                         id2 = PHY_READ(sc->physc, MII_PHYIDR2);
1067
1068                         oui = MII_OUI(id1, id2);
1069                         model = MII_MODEL(id2);
1070                         switch (oui) {
1071                         case MII_OUI_xxQUALSEMI:
1072                                 if (model == MII_MODEL_xxQUALSEMI_QS6612)
1073                                         sc->phyid = EPIC_QS6612_PHY;
1074                                 break;
1075                         case MII_OUI_ALTIMA:
1076                                 if (model == MII_MODEL_ALTIMA_AC101)
1077                                         sc->phyid = EPIC_AC101_PHY;
1078                                 break;
1079                         case MII_OUI_xxLEVEL1:
1080                                 if (model == MII_MODEL_xxLEVEL1_LXT970)
1081                                         sc->phyid = EPIC_LXT970_PHY;
1082                                 break;
1083                         }
1084                 }
1085         }
1086
1087         /*
1088          * Do PHY specific card setup.
1089          */
1090
1091         /*
1092          * Call this, to isolate all not selected PHYs and
1093          * set up selected.
1094          */
1095         mii_mediachg(mii);
1096
1097         /* Do our own setup. */
1098         switch (sc->phyid) {
1099         case EPIC_QS6612_PHY:
1100                 break;
1101         case EPIC_AC101_PHY:
1102                 /* We have to powerup fiber tranceivers. */
1103                 if (IFM_SUBTYPE(media) == IFM_100_FX)
1104                         sc->miicfg |= MIICFG_694_ENABLE;
1105                 else
1106                         sc->miicfg &= ~MIICFG_694_ENABLE;
1107                 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1108
1109                 break;
1110         case EPIC_LXT970_PHY:
1111                 /* We have to powerup fiber tranceivers. */
1112                 cfg = PHY_READ(sc->physc, MII_LXTPHY_CONFIG);
1113                 if (IFM_SUBTYPE(media) == IFM_100_FX)
1114                         cfg |= CONFIG_LEDC1 | CONFIG_LEDC0;
1115                 else
1116                         cfg &= ~(CONFIG_LEDC1 | CONFIG_LEDC0);
1117                 PHY_WRITE(sc->physc, MII_LXTPHY_CONFIG, cfg);
1118
1119                 break;
1120         case EPIC_SERIAL:
1121                 /* Select serial PHY (10base2/BNC usually). */
1122                 sc->miicfg |= MIICFG_694_ENABLE | MIICFG_SERIAL_ENABLE;
1123                 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1124
1125                 /* There is no driver to fill this. */
1126                 mii->mii_media_active = media;
1127                 mii->mii_media_status = 0;
1128
1129                 /*
1130                  * We need to call this manually as it wasn't called
1131                  * in mii_mediachg().
1132                  */
1133                 epic_miibus_statchg(sc->dev);
1134                 break;
1135         default:
1136                 device_printf(sc->dev, "ERROR! Unknown PHY selected\n");
1137                 return (EINVAL);
1138         }
1139
1140         return (0);
1141 }
1142
1143 /*
1144  * Report current media status.
1145  */
1146 static void
1147 epic_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1148 {
1149         epic_softc_t *sc;
1150         struct mii_data *mii;
1151
1152         sc = ifp->if_softc;
1153         mii = device_get_softc(sc->miibus);
1154         EPIC_LOCK(sc);
1155
1156         /* Nothing should be selected if interface is down. */
1157         if ((ifp->if_flags & IFF_UP) == 0) {
1158                 ifmr->ifm_active = IFM_NONE;
1159                 ifmr->ifm_status = 0;
1160                 EPIC_UNLOCK(sc);
1161                 return;
1162         }
1163
1164         /* Call underlying pollstat, if not serial PHY. */
1165         if (sc->phyid != EPIC_SERIAL)
1166                 mii_pollstat(mii);
1167
1168         /* Simply copy media info. */
1169         ifmr->ifm_active = mii->mii_media_active;
1170         ifmr->ifm_status = mii->mii_media_status;
1171         EPIC_UNLOCK(sc);
1172 }
1173
1174 /*
1175  * Callback routine, called on media change.
1176  */
1177 static void
1178 epic_miibus_statchg(device_t dev)
1179 {
1180         epic_softc_t *sc;
1181         struct mii_data *mii;
1182         int media;
1183
1184         sc = device_get_softc(dev);
1185         mii = device_get_softc(sc->miibus);
1186         media = mii->mii_media_active;
1187
1188         sc->txcon &= ~(TXCON_LOOPBACK_MODE | TXCON_FULL_DUPLEX);
1189
1190         /*
1191          * If we are in full-duplex mode or loopback operation,
1192          * we need to decouple receiver and transmitter.
1193          */
1194         if (IFM_OPTIONS(media) & (IFM_FDX | IFM_LOOP))
1195                 sc->txcon |= TXCON_FULL_DUPLEX;
1196
1197         /* On some cards we need manualy set fullduplex led. */
1198         if (sc->cardid == SMC9432FTX ||
1199             sc->cardid == SMC9432FTX_SC) {
1200                 if (IFM_OPTIONS(media) & IFM_FDX)
1201                         sc->miicfg |= MIICFG_694_ENABLE;
1202                 else
1203                         sc->miicfg &= ~MIICFG_694_ENABLE;
1204
1205                 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1206         }
1207
1208         epic_stop_activity(sc);
1209         epic_set_tx_mode(sc);
1210         epic_start_activity(sc);
1211 }
1212
1213 static void
1214 epic_miibus_mediainit(device_t dev)
1215 {
1216         epic_softc_t *sc;
1217         struct mii_data *mii;
1218         struct ifmedia *ifm;
1219         int media;
1220
1221         sc = device_get_softc(dev);
1222         mii = device_get_softc(sc->miibus);
1223         ifm = &mii->mii_media;
1224
1225         /*
1226          * Add Serial Media Interface if present, this applies to
1227          * SMC9432BTX serie.
1228          */
1229         if (CSR_READ_4(sc, MIICFG) & MIICFG_PHY_PRESENT) {
1230                 /* Store its instance. */
1231                 sc->serinst = mii->mii_instance++;
1232
1233                 /* Add as 10base2/BNC media. */
1234                 media = IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->serinst);
1235                 ifmedia_add(ifm, media, 0, NULL);
1236
1237                 /* Report to user. */
1238                 device_printf(sc->dev, "serial PHY detected (10Base2/BNC)\n");
1239         }
1240 }
1241
1242 /*
1243  * Reset chip and update media.
1244  */
1245 static void
1246 epic_init(void *xsc)
1247 {
1248         epic_softc_t *sc = xsc;
1249
1250         EPIC_LOCK(sc);
1251         epic_init_locked(sc);
1252         EPIC_UNLOCK(sc);
1253 }
1254
1255 static void
1256 epic_init_locked(epic_softc_t *sc)
1257 {
1258         struct ifnet *ifp = sc->ifp;
1259         int i;
1260
1261         /* If interface is already running, then we need not do anything. */
1262         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1263                 return;
1264         }
1265
1266         /* Soft reset the chip (we have to power up card before). */
1267         CSR_WRITE_4(sc, GENCTL, 0);
1268         CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1269
1270         /*
1271          * Reset takes 15 pci ticks which depends on PCI bus speed.
1272          * Assuming it >= 33000000 hz, we have wait at least 495e-6 sec.
1273          */
1274         DELAY(500);
1275
1276         /* Wake up */
1277         CSR_WRITE_4(sc, GENCTL, 0);
1278
1279         /* Workaround for Application Note 7-15 */
1280         for (i = 0; i < 16; i++)
1281                 CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
1282
1283         /* Give rings to EPIC */
1284         CSR_WRITE_4(sc, PRCDAR, sc->rx_addr);
1285         CSR_WRITE_4(sc, PTCDAR, sc->tx_addr);
1286
1287         /* Put node address to EPIC. */
1288         CSR_WRITE_4(sc, LAN0, ((u_int16_t *)IF_LLADDR(sc->ifp))[0]);
1289         CSR_WRITE_4(sc, LAN1, ((u_int16_t *)IF_LLADDR(sc->ifp))[1]);
1290         CSR_WRITE_4(sc, LAN2, ((u_int16_t *)IF_LLADDR(sc->ifp))[2]);
1291
1292         /* Set tx mode, includeing transmit threshold. */
1293         epic_set_tx_mode(sc);
1294
1295         /* Compute and set RXCON. */
1296         epic_set_rx_mode(sc);
1297
1298         /* Set multicast table. */
1299         epic_set_mc_table(sc);
1300
1301         /* Enable interrupts by setting the interrupt mask. */
1302         CSR_WRITE_4(sc, INTMASK,
1303                 INTSTAT_RCC  | /* INTSTAT_RQE | INTSTAT_OVW | INTSTAT_RXE | */
1304                 /* INTSTAT_TXC | */ INTSTAT_TCC | INTSTAT_TQE | INTSTAT_TXU |
1305                 INTSTAT_FATAL);
1306
1307         /* Acknowledge all pending interrupts. */
1308         CSR_WRITE_4(sc, INTSTAT, CSR_READ_4(sc, INTSTAT));
1309
1310         /* Enable interrupts,  set for PCI read multiple and etc */
1311         CSR_WRITE_4(sc, GENCTL,
1312                 GENCTL_ENABLE_INTERRUPT | GENCTL_MEMORY_READ_MULTIPLE |
1313                 GENCTL_ONECOPY | GENCTL_RECEIVE_FIFO_THRESHOLD64);
1314
1315         /* Mark interface running ... */
1316         if (ifp->if_flags & IFF_UP)
1317                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1318         else
1319                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1320
1321         /* ... and free */
1322         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1323
1324         /* Start Rx process */
1325         epic_start_activity(sc);
1326
1327         /* Set appropriate media */
1328         epic_ifmedia_upd_locked(ifp);
1329
1330         callout_reset(&sc->timer, hz, epic_timer, sc);
1331 }
1332
1333 /*
1334  * Synopsis: calculate and set Rx mode. Chip must be in idle state to
1335  * access RXCON.
1336  */
1337 static void
1338 epic_set_rx_mode(epic_softc_t *sc)
1339 {
1340         u_int32_t flags;
1341         u_int32_t rxcon;
1342
1343         flags = sc->ifp->if_flags;
1344         rxcon = RXCON_DEFAULT;
1345
1346 #ifdef EPIC_EARLY_RX
1347         rxcon |= RXCON_EARLY_RX;
1348 #endif
1349
1350         rxcon |= (flags & IFF_PROMISC) ? RXCON_PROMISCUOUS_MODE : 0;
1351
1352         CSR_WRITE_4(sc, RXCON, rxcon);
1353 }
1354
1355 /*
1356  * Synopsis: Set transmit control register. Chip must be in idle state to
1357  * access TXCON.
1358  */
1359 static void
1360 epic_set_tx_mode(epic_softc_t *sc)
1361 {
1362
1363         if (sc->txcon & TXCON_EARLY_TRANSMIT_ENABLE)
1364                 CSR_WRITE_4(sc, ETXTHR, sc->tx_threshold);
1365
1366         CSR_WRITE_4(sc, TXCON, sc->txcon);
1367 }
1368
1369 /*
1370  * Synopsis: Program multicast filter honoring IFF_ALLMULTI and IFF_PROMISC
1371  * flags (note that setting PROMISC bit in EPIC's RXCON will only touch
1372  * individual frames, multicast filter must be manually programmed).
1373  *
1374  * Note: EPIC must be in idle state.
1375  */
1376 static void
1377 epic_set_mc_table(epic_softc_t *sc)
1378 {
1379         struct ifnet *ifp;
1380         struct ifmultiaddr *ifma;
1381         u_int16_t filter[4];
1382         u_int8_t h;
1383
1384         ifp = sc->ifp;
1385         if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
1386                 CSR_WRITE_4(sc, MC0, 0xFFFF);
1387                 CSR_WRITE_4(sc, MC1, 0xFFFF);
1388                 CSR_WRITE_4(sc, MC2, 0xFFFF);
1389                 CSR_WRITE_4(sc, MC3, 0xFFFF);
1390                 return;
1391         }
1392
1393         filter[0] = 0;
1394         filter[1] = 0;
1395         filter[2] = 0;
1396         filter[3] = 0;
1397
1398         if_maddr_rlock(ifp);
1399         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1400                 if (ifma->ifma_addr->sa_family != AF_LINK)
1401                         continue;
1402                 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
1403                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
1404                 filter[h >> 4] |= 1 << (h & 0xF);
1405         }
1406         if_maddr_runlock(ifp);
1407
1408         CSR_WRITE_4(sc, MC0, filter[0]);
1409         CSR_WRITE_4(sc, MC1, filter[1]);
1410         CSR_WRITE_4(sc, MC2, filter[2]);
1411         CSR_WRITE_4(sc, MC3, filter[3]);
1412 }
1413
1414
1415 /*
1416  * Synopsis: Start receive process and transmit one, if they need.
1417  */
1418 static void
1419 epic_start_activity(epic_softc_t *sc)
1420 {
1421
1422         /* Start rx process. */
1423         CSR_WRITE_4(sc, COMMAND, COMMAND_RXQUEUED | COMMAND_START_RX |
1424             (sc->pending_txs ? COMMAND_TXQUEUED : 0));
1425 }
1426
1427 /*
1428  * Synopsis: Completely stop Rx and Tx processes. If TQE is set additional
1429  * packet needs to be queued to stop Tx DMA.
1430  */
1431 static void
1432 epic_stop_activity(epic_softc_t *sc)
1433 {
1434         int status, i;
1435
1436         /* Stop Tx and Rx DMA. */
1437         CSR_WRITE_4(sc, COMMAND,
1438             COMMAND_STOP_RX | COMMAND_STOP_RDMA | COMMAND_STOP_TDMA);
1439
1440         /* Wait Rx and Tx DMA to stop (why 1 ms ??? XXX). */
1441         for (i = 0; i < 0x1000; i++) {
1442                 status = CSR_READ_4(sc, INTSTAT) &
1443                     (INTSTAT_TXIDLE | INTSTAT_RXIDLE);
1444                 if (status == (INTSTAT_TXIDLE | INTSTAT_RXIDLE))
1445                         break;
1446                 DELAY(1);
1447         }
1448
1449         /* Catch all finished packets. */
1450         epic_rx_done(sc);
1451         epic_tx_done(sc);
1452
1453         status = CSR_READ_4(sc, INTSTAT);
1454
1455         if ((status & INTSTAT_RXIDLE) == 0)
1456                 device_printf(sc->dev, "ERROR! Can't stop Rx DMA\n");
1457
1458         if ((status & INTSTAT_TXIDLE) == 0)
1459                 device_printf(sc->dev, "ERROR! Can't stop Tx DMA\n");
1460
1461         /*
1462          * May need to queue one more packet if TQE, this is rare
1463          * but existing case.
1464          */
1465         if ((status & INTSTAT_TQE) && !(status & INTSTAT_TXIDLE))
1466                 (void)epic_queue_last_packet(sc);
1467 }
1468
1469 /*
1470  * The EPIC transmitter may stuck in TQE state. It will not go IDLE until
1471  * a packet from current descriptor will be copied to internal RAM. We
1472  * compose a dummy packet here and queue it for transmission.
1473  *
1474  * XXX the packet will then be actually sent over network...
1475  */
1476 static int
1477 epic_queue_last_packet(epic_softc_t *sc)
1478 {
1479         struct epic_tx_desc *desc;
1480         struct epic_frag_list *flist;
1481         struct epic_tx_buffer *buf;
1482         struct mbuf *m0;
1483         int error, i;
1484
1485         device_printf(sc->dev, "queue last packet\n");
1486
1487         desc = sc->tx_desc + sc->cur_tx;
1488         flist = sc->tx_flist + sc->cur_tx;
1489         buf = sc->tx_buffer + sc->cur_tx;
1490
1491         if ((desc->status & 0x8000) || (buf->mbuf != NULL))
1492                 return (EBUSY);
1493
1494         MGETHDR(m0, M_NOWAIT, MT_DATA);
1495         if (m0 == NULL)
1496                 return (ENOBUFS);
1497
1498         /* Prepare mbuf. */
1499         m0->m_len = min(MHLEN, ETHER_MIN_LEN - ETHER_CRC_LEN);
1500         m0->m_pkthdr.len = m0->m_len;
1501         m0->m_pkthdr.rcvif = sc->ifp;
1502         bzero(mtod(m0, caddr_t), m0->m_len);
1503
1504         /* Fill fragments list. */
1505         error = bus_dmamap_load_mbuf(sc->mtag, buf->map, m0,
1506             epic_dma_map_txbuf, flist, 0);
1507         if (error) {
1508                 m_freem(m0);
1509                 return (error);
1510         }
1511         bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREWRITE);
1512
1513         /* Fill in descriptor. */
1514         buf->mbuf = m0;
1515         sc->pending_txs++;
1516         sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
1517         desc->control = 0x01;
1518         desc->txlength = max(m0->m_pkthdr.len, ETHER_MIN_LEN - ETHER_CRC_LEN);
1519         desc->status = 0x8000;
1520         bus_dmamap_sync(sc->ttag, sc->tmap,
1521             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1522         bus_dmamap_sync(sc->ftag, sc->fmap, BUS_DMASYNC_PREWRITE);
1523
1524         /* Launch transmission. */
1525         CSR_WRITE_4(sc, COMMAND, COMMAND_STOP_TDMA | COMMAND_TXQUEUED);
1526
1527         /* Wait Tx DMA to stop (for how long??? XXX) */
1528         for (i = 0; i < 1000; i++) {
1529                 if (CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE)
1530                         break;
1531                 DELAY(1);
1532         }
1533
1534         if ((CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE) == 0)
1535                 device_printf(sc->dev, "ERROR! can't stop Tx DMA (2)\n");
1536         else
1537                 epic_tx_done(sc);
1538
1539         return (0);
1540 }
1541
1542 /*
1543  *  Synopsis: Shut down board and deallocates rings.
1544  */
1545 static void
1546 epic_stop(epic_softc_t *sc)
1547 {
1548
1549         EPIC_ASSERT_LOCKED(sc);
1550
1551         sc->tx_timeout = 0;
1552         callout_stop(&sc->timer);
1553
1554         /* Disable interrupts */
1555         CSR_WRITE_4(sc, INTMASK, 0);
1556         CSR_WRITE_4(sc, GENCTL, 0);
1557
1558         /* Try to stop Rx and TX processes */
1559         epic_stop_activity(sc);
1560
1561         /* Reset chip */
1562         CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1563         DELAY(1000);
1564
1565         /* Make chip go to bed */
1566         CSR_WRITE_4(sc, GENCTL, GENCTL_POWER_DOWN);
1567
1568         /* Mark as stopped */
1569         sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1570 }
1571
1572 /*
1573  * Synopsis: This function should free all memory allocated for rings.
1574  */
1575 static void
1576 epic_free_rings(epic_softc_t *sc)
1577 {
1578         int i;
1579
1580         for (i = 0; i < RX_RING_SIZE; i++) {
1581                 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1582                 struct epic_rx_desc *desc = sc->rx_desc + i;
1583
1584                 desc->status = 0;
1585                 desc->buflength = 0;
1586                 desc->bufaddr = 0;
1587
1588                 if (buf->mbuf) {
1589                         bus_dmamap_unload(sc->mtag, buf->map);
1590                         bus_dmamap_destroy(sc->mtag, buf->map);
1591                         m_freem(buf->mbuf);
1592                 }
1593                 buf->mbuf = NULL;
1594         }
1595
1596         if (sc->sparemap != NULL)
1597                 bus_dmamap_destroy(sc->mtag, sc->sparemap);
1598
1599         for (i = 0; i < TX_RING_SIZE; i++) {
1600                 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1601                 struct epic_tx_desc *desc = sc->tx_desc + i;
1602
1603                 desc->status = 0;
1604                 desc->buflength = 0;
1605                 desc->bufaddr = 0;
1606
1607                 if (buf->mbuf) {
1608                         bus_dmamap_unload(sc->mtag, buf->map);
1609                         bus_dmamap_destroy(sc->mtag, buf->map);
1610                         m_freem(buf->mbuf);
1611                 }
1612                 buf->mbuf = NULL;
1613         }
1614 }
1615
1616 /*
1617  * Synopsis:  Allocates mbufs for Rx ring and point Rx descs to them.
1618  * Point Tx descs to fragment lists. Check that all descs and fraglists
1619  * are bounded and aligned properly.
1620  */
1621 static int
1622 epic_init_rings(epic_softc_t *sc)
1623 {
1624         int error, i;
1625
1626         sc->cur_rx = sc->cur_tx = sc->dirty_tx = sc->pending_txs = 0;
1627
1628         /* Initialize the RX descriptor ring. */
1629         for (i = 0; i < RX_RING_SIZE; i++) {
1630                 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1631                 struct epic_rx_desc *desc = sc->rx_desc + i;
1632
1633                 desc->status = 0;               /* Owned by driver */
1634                 desc->next = sc->rx_addr +
1635                     ((i + 1) & RX_RING_MASK) * sizeof(struct epic_rx_desc);
1636
1637                 if ((desc->next & 3) ||
1638                     ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1639                         epic_free_rings(sc);
1640                         return (EFAULT);
1641                 }
1642
1643                 buf->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1644                 if (buf->mbuf == NULL) {
1645                         epic_free_rings(sc);
1646                         return (ENOBUFS);
1647                 }
1648                 buf->mbuf->m_len = buf->mbuf->m_pkthdr.len = MCLBYTES;
1649                 m_adj(buf->mbuf, ETHER_ALIGN);
1650
1651                 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
1652                 if (error) {
1653                         epic_free_rings(sc);
1654                         return (error);
1655                 }
1656                 error = bus_dmamap_load_mbuf(sc->mtag, buf->map, buf->mbuf,
1657                     epic_dma_map_rxbuf, desc, 0);
1658                 if (error) {
1659                         epic_free_rings(sc);
1660                         return (error);
1661                 }
1662                 bus_dmamap_sync(sc->mtag, buf->map, BUS_DMASYNC_PREREAD);
1663
1664                 desc->buflength = buf->mbuf->m_len; /* Max RX buffer length */
1665                 desc->status = 0x8000;          /* Set owner bit to NIC */
1666         }
1667         bus_dmamap_sync(sc->rtag, sc->rmap,
1668             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1669
1670         /* Create the spare DMA map. */
1671         error = bus_dmamap_create(sc->mtag, 0, &sc->sparemap);
1672         if (error) {
1673                 epic_free_rings(sc);
1674                 return (error);
1675         }
1676
1677         /* Initialize the TX descriptor ring. */
1678         for (i = 0; i < TX_RING_SIZE; i++) {
1679                 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1680                 struct epic_tx_desc *desc = sc->tx_desc + i;
1681
1682                 desc->status = 0;
1683                 desc->next = sc->tx_addr +
1684                     ((i + 1) & TX_RING_MASK) * sizeof(struct epic_tx_desc);
1685
1686                 if ((desc->next & 3) ||
1687                     ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1688                         epic_free_rings(sc);
1689                         return (EFAULT);
1690                 }
1691
1692                 buf->mbuf = NULL;
1693                 desc->bufaddr = sc->frag_addr +
1694                     i * sizeof(struct epic_frag_list);
1695
1696                 if ((desc->bufaddr & 3) ||
1697                     ((desc->bufaddr & PAGE_MASK) +
1698                     sizeof(struct epic_frag_list)) > PAGE_SIZE) {
1699                         epic_free_rings(sc);
1700                         return (EFAULT);
1701                 }
1702
1703                 error = bus_dmamap_create(sc->mtag, 0, &buf->map);
1704                 if (error) {
1705                         epic_free_rings(sc);
1706                         return (error);
1707                 }
1708         }
1709         bus_dmamap_sync(sc->ttag, sc->tmap,
1710             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1711         bus_dmamap_sync(sc->ftag, sc->fmap, BUS_DMASYNC_PREWRITE);
1712
1713         return (0);
1714 }
1715
1716 /*
1717  * EEPROM operation functions
1718  */
1719 static void
1720 epic_write_eepromreg(epic_softc_t *sc, u_int8_t val)
1721 {
1722         u_int16_t i;
1723
1724         CSR_WRITE_1(sc, EECTL, val);
1725
1726         for (i = 0; i < 0xFF; i++) {
1727                 if ((CSR_READ_1(sc, EECTL) & 0x20) == 0)
1728                         break;
1729         }
1730 }
1731
1732 static u_int8_t
1733 epic_read_eepromreg(epic_softc_t *sc)
1734 {
1735
1736         return (CSR_READ_1(sc, EECTL));
1737 }
1738
1739 static u_int8_t
1740 epic_eeprom_clock(epic_softc_t *sc, u_int8_t val)
1741 {
1742
1743         epic_write_eepromreg(sc, val);
1744         epic_write_eepromreg(sc, (val | 0x4));
1745         epic_write_eepromreg(sc, val);
1746
1747         return (epic_read_eepromreg(sc));
1748 }
1749
1750 static void
1751 epic_output_eepromw(epic_softc_t *sc, u_int16_t val)
1752 {
1753         int i;
1754
1755         for (i = 0xF; i >= 0; i--) {
1756                 if (val & (1 << i))
1757                         epic_eeprom_clock(sc, 0x0B);
1758                 else
1759                         epic_eeprom_clock(sc, 0x03);
1760         }
1761 }
1762
1763 static u_int16_t
1764 epic_input_eepromw(epic_softc_t *sc)
1765 {
1766         u_int16_t retval = 0;
1767         int i;
1768
1769         for (i = 0xF; i >= 0; i--) {
1770                 if (epic_eeprom_clock(sc, 0x3) & 0x10)
1771                         retval |= (1 << i);
1772         }
1773
1774         return (retval);
1775 }
1776
1777 static int
1778 epic_read_eeprom(epic_softc_t *sc, u_int16_t loc)
1779 {
1780         u_int16_t dataval;
1781         u_int16_t read_cmd;
1782
1783         epic_write_eepromreg(sc, 3);
1784
1785         if (epic_read_eepromreg(sc) & 0x40)
1786                 read_cmd = (loc & 0x3F) | 0x180;
1787         else
1788                 read_cmd = (loc & 0xFF) | 0x600;
1789
1790         epic_output_eepromw(sc, read_cmd);
1791
1792         dataval = epic_input_eepromw(sc);
1793
1794         epic_write_eepromreg(sc, 1);
1795
1796         return (dataval);
1797 }
1798
1799 /*
1800  * Here goes MII read/write routines.
1801  */
1802 static int
1803 epic_read_phy_reg(epic_softc_t *sc, int phy, int reg)
1804 {
1805         int i;
1806
1807         CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x01));
1808
1809         for (i = 0; i < 0x100; i++) {
1810                 if ((CSR_READ_4(sc, MIICTL) & 0x01) == 0)
1811                         break;
1812                 DELAY(1);
1813         }
1814
1815         return (CSR_READ_4(sc, MIIDATA));
1816 }
1817
1818 static void
1819 epic_write_phy_reg(epic_softc_t *sc, int phy, int reg, int val)
1820 {
1821         int i;
1822
1823         CSR_WRITE_4(sc, MIIDATA, val);
1824         CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x02));
1825
1826         for(i = 0; i < 0x100; i++) {
1827                 if ((CSR_READ_4(sc, MIICTL) & 0x02) == 0)
1828                         break;
1829                 DELAY(1);
1830         }
1831 }
1832
1833 static int
1834 epic_miibus_readreg(device_t dev, int phy, int reg)
1835 {
1836         epic_softc_t *sc;
1837
1838         sc = device_get_softc(dev);
1839
1840         return (PHY_READ_2(sc, phy, reg));
1841 }
1842
1843 static int
1844 epic_miibus_writereg(device_t dev, int phy, int reg, int data)
1845 {
1846         epic_softc_t *sc;
1847
1848         sc = device_get_softc(dev);
1849
1850         PHY_WRITE_2(sc, phy, reg, data);
1851
1852         return (0);
1853 }