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