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