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