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