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