2 * Copyright (c) 1997 Semen Ustimenko (semenu@FreeBSD.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.10 2004/04/07 05:45:30 dillon Exp $
31 * EtherPower II 10/100 Fast Ethernet (SMC 9432 serie)
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.
39 * Thanks are going to Steve Bauer and Jason Wright.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sockio.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/queue.h>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
59 #include <net/vlan/if_vlan_var.h>
61 #include <vm/vm.h> /* for vtophys */
62 #include <vm/pmap.h> /* for vtophys */
63 #include <machine/bus_memio.h>
64 #include <machine/bus_pio.h>
65 #include <machine/bus.h>
66 #include <machine/resource.h>
67 #include <machine/clock.h> /* for DELAY */
71 #include <bus/pci/pcireg.h>
72 #include <bus/pci/pcivar.h>
74 #include "../mii_layer/mii.h"
75 #include "../mii_layer/miivar.h"
76 #include "../mii_layer/miidevs.h"
77 #include "../mii_layer/lxtphyreg.h"
79 #include "miibus_if.h"
84 static int epic_ifioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
85 static void epic_intr(void *);
86 static void epic_tx_underrun(epic_softc_t *);
87 static int epic_common_attach(epic_softc_t *);
88 static void epic_ifstart(struct ifnet *);
89 static void epic_ifwatchdog(struct ifnet *);
90 static void epic_stats_update(epic_softc_t *);
91 static int epic_init(epic_softc_t *);
92 static void epic_stop(epic_softc_t *);
93 static void epic_rx_done(epic_softc_t *);
94 static void epic_tx_done(epic_softc_t *);
95 static int epic_init_rings(epic_softc_t *);
96 static void epic_free_rings(epic_softc_t *);
97 static void epic_stop_activity(epic_softc_t *);
98 static int epic_queue_last_packet(epic_softc_t *);
99 static void epic_start_activity(epic_softc_t *);
100 static void epic_set_rx_mode(epic_softc_t *);
101 static void epic_set_tx_mode(epic_softc_t *);
102 static void epic_set_mc_table(epic_softc_t *);
103 static u_int8_t epic_calchash(caddr_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 *);
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);
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);
119 static int epic_ifmedia_upd(struct ifnet *);
120 static void epic_ifmedia_sts(struct ifnet *, struct ifmediareq *);
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);
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),
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),
144 static driver_t epic_driver = {
150 static devclass_t epic_devclass;
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);
157 static struct epic_type epic_devs[] = {
158 { SMC_VENDORID, SMC_DEVICEID_83C170,
159 "SMC EtherPower II 10/100" },
169 t = epic_devtype(dev);
172 device_set_desc(dev, t->name);
179 static struct epic_type *
187 while(t->name != NULL) {
188 if ((pci_get_vendor(dev) == t->ven_id) &&
189 (pci_get_device(dev) == t->dev_id)) {
197 #if defined(EPIC_USEIOSPACE)
198 #define EPIC_RES SYS_RES_IOPORT
199 #define EPIC_RID PCIR_BASEIO
201 #define EPIC_RES SYS_RES_MEMORY
202 #define EPIC_RID PCIR_BASEMEM
206 * Attach routine: map registers, allocate softc, rings and descriptors.
207 * Reset to known state.
221 sc = device_get_softc(dev);
222 unit = device_get_unit(dev);
224 /* Preinitialize softc structure */
225 bzero(sc, sizeof(epic_softc_t));
229 /* Fill ifnet structure */
231 if_initname(ifp, "tx", unit);
233 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
234 ifp->if_ioctl = epic_ifioctl;
235 ifp->if_output = ether_output;
236 ifp->if_start = epic_ifstart;
237 ifp->if_watchdog = epic_ifwatchdog;
238 ifp->if_init = (if_init_f_t*)epic_init;
240 ifp->if_baudrate = 10000000;
241 ifp->if_snd.ifq_maxlen = TX_RING_SIZE - 1;
243 /* Enable ports, memory and busmastering */
244 command = pci_read_config(dev, PCIR_COMMAND, 4);
245 command |= PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
246 pci_write_config(dev, PCIR_COMMAND, command, 4);
247 command = pci_read_config(dev, PCIR_COMMAND, 4);
249 #if defined(EPIC_USEIOSPACE)
250 if ((command & PCIM_CMD_PORTEN) == 0) {
251 device_printf(dev, "failed to enable I/O mapping!\n");
256 if ((command & PCIM_CMD_MEMEN) == 0) {
257 device_printf(dev, "failed to enable memory mapping!\n");
264 sc->res = bus_alloc_resource(dev, EPIC_RES, &rid, 0, ~0, 1,
267 if (sc->res == NULL) {
268 device_printf(dev, "couldn't map ports/memory\n");
273 sc->sc_st = rman_get_bustag(sc->res);
274 sc->sc_sh = rman_get_bushandle(sc->res);
276 /* Allocate interrupt */
278 sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
279 RF_SHAREABLE | RF_ACTIVE);
281 if (sc->irq == NULL) {
282 device_printf(dev, "couldn't map interrupt\n");
283 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
288 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
289 epic_intr, sc, &sc->sc_ih);
292 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
293 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
294 device_printf(dev, "couldn't set up irq\n");
298 /* Do OS independent part, including chip wakeup and reset */
299 error = epic_common_attach(sc);
301 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
302 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
303 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
308 /* Do ifmedia setup */
309 if (mii_phy_probe(dev, &sc->miibus,
310 epic_ifmedia_upd, epic_ifmedia_sts)) {
311 device_printf(dev, "ERROR! MII without any PHY!?\n");
312 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
313 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
314 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
319 /* Display ethernet address ,... */
320 device_printf(dev, "address %6D,", sc->sc_macaddr, ":");
322 /* board type and ... */
324 for(i=0x2c;i<0x32;i++) {
325 tmp = epic_read_eeprom(sc, i);
326 if (' ' == (u_int8_t)tmp) break;
327 printf("%c", (u_int8_t)tmp);
329 if (' ' == (u_int8_t)tmp) break;
330 printf("%c", (u_int8_t)tmp);
334 /* Attach to OS's managers */
335 ether_ifattach(ifp, sc->sc_macaddr);
336 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
337 callout_handle_init(&sc->stat_ch);
346 * Detach driver and free resources
358 sc = device_get_softc(dev);
359 ifp = &sc->arpcom.ac_if;
365 bus_generic_detach(dev);
366 device_delete_child(dev, sc->miibus);
368 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
369 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
370 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
372 free(sc->tx_flist, M_DEVBUF);
373 free(sc->tx_desc, M_DEVBUF);
374 free(sc->rx_desc, M_DEVBUF);
385 * Stop all chip I/O so that the kernel's probe routines don't
386 * get confused by errant DMAs when rebooting.
394 sc = device_get_softc(dev);
402 * This is if_ioctl handler.
405 epic_ifioctl(ifp, command, data, cr)
411 epic_softc_t *sc = ifp->if_softc;
412 struct mii_data *mii;
413 struct ifreq *ifr = (struct ifreq *) data;
421 error = ether_ioctl(ifp, command, data);
424 if (ifp->if_mtu == ifr->ifr_mtu)
427 /* XXX Though the datasheet doesn't imply any
428 * limitations on RX and TX sizes beside max 64Kb
429 * DMA transfer, seems we can't send more then 1600
430 * data bytes per ethernet packet. (Transmitter hangs
431 * up if more data is sent)
433 if (ifr->ifr_mtu + ifp->if_hdrlen <= EPIC_MAX_MTU) {
434 ifp->if_mtu = ifr->ifr_mtu;
443 * If the interface is marked up and stopped, then start it.
444 * If it is marked down and running, then stop it.
446 if (ifp->if_flags & IFF_UP) {
447 if ((ifp->if_flags & IFF_RUNNING) == 0) {
452 if (ifp->if_flags & IFF_RUNNING) {
458 /* Handle IFF_PROMISC and IFF_ALLMULTI flags */
459 epic_stop_activity(sc);
460 epic_set_mc_table(sc);
461 epic_set_rx_mode(sc);
462 epic_start_activity(sc);
467 epic_set_mc_table(sc);
473 mii = device_get_softc(sc->miibus);
474 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
486 * OS-independed part of attach process. allocate memory for descriptors
487 * and frag lists, wake up chip, read MAC address and PHY identyfier.
488 * Return -1 on failure.
491 epic_common_attach(sc)
496 sc->tx_flist = malloc(sizeof(struct epic_frag_list)*TX_RING_SIZE,
497 M_DEVBUF, M_WAITOK | M_ZERO);
498 sc->tx_desc = malloc(sizeof(struct epic_tx_desc)*TX_RING_SIZE,
499 M_DEVBUF, M_WAITOK | M_ZERO);
500 sc->rx_desc = malloc(sizeof(struct epic_rx_desc)*RX_RING_SIZE,
501 M_DEVBUF, M_WAITOK | M_ZERO);
503 /* Bring the chip out of low-power mode. */
504 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
507 /* Workaround for Application Note 7-15 */
508 for (i=0; i<16; i++) CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
510 /* Read mac address from EEPROM */
511 for (i = 0; i < ETHER_ADDR_LEN / sizeof(u_int16_t); i++)
512 ((u_int16_t *)sc->sc_macaddr)[i] = epic_read_eeprom(sc,i);
514 /* Set Non-Volatile Control Register from EEPROM */
515 CSR_WRITE_4(sc, NVCTL, epic_read_eeprom(sc, EEPROM_NVCTL) & 0x1F);
518 sc->tx_threshold = TRANSMIT_THRESHOLD;
519 sc->txcon = TXCON_DEFAULT;
520 sc->miicfg = MIICFG_SMI_ENABLE;
521 sc->phyid = EPIC_UNKN_PHY;
525 sc->cardvend = pci_read_config(sc->dev, PCIR_SUBVEND_0, 2);
526 sc->cardid = pci_read_config(sc->dev, PCIR_SUBDEV_0, 2);
528 if (sc->cardvend != SMC_VENDORID)
529 device_printf(sc->dev, "unknown card vendor %04xh\n", sc->cardvend);
535 * This is if_start handler. It takes mbufs from if_snd queue
536 * and queue them for transmit, one by one, until TX ring become full
537 * or queue become empty.
543 epic_softc_t *sc = ifp->if_softc;
544 struct epic_tx_buffer *buf;
545 struct epic_tx_desc *desc;
546 struct epic_frag_list *flist;
551 while (sc->pending_txs < TX_RING_SIZE) {
552 buf = sc->tx_buffer + sc->cur_tx;
553 desc = sc->tx_desc + sc->cur_tx;
554 flist = sc->tx_flist + sc->cur_tx;
556 /* Get next packet to send */
557 IF_DEQUEUE(&ifp->if_snd, m0);
559 /* If nothing to send, return */
560 if (NULL == m0) return;
562 /* Fill fragments list */
564 (NULL != m) && (i < EPIC_MAX_FRAGS);
565 m = m->m_next, i++) {
566 flist->frag[i].fraglen = m->m_len;
567 flist->frag[i].fragaddr = vtophys(mtod(m, caddr_t));
571 /* If packet was more than EPIC_MAX_FRAGS parts, */
572 /* recopy packet to new allocated mbuf cluster */
581 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
582 flist->frag[0].fraglen =
583 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
584 m->m_pkthdr.rcvif = ifp;
587 flist->frag[0].fragaddr = vtophys(mtod(m, caddr_t));
594 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
595 desc->control = 0x01;
597 max(m0->m_pkthdr.len,ETHER_MIN_LEN-ETHER_CRC_LEN);
598 desc->status = 0x8000;
599 CSR_WRITE_4(sc, COMMAND, COMMAND_TXQUEUED);
601 /* Set watchdog timer */
608 ifp->if_flags |= IFF_OACTIVE;
615 * Synopsis: Finish all received frames.
622 struct epic_rx_buffer *buf;
623 struct epic_rx_desc *desc;
625 struct ether_header *eh;
627 while ((sc->rx_desc[sc->cur_rx].status & 0x8000) == 0) {
628 buf = sc->rx_buffer + sc->cur_rx;
629 desc = sc->rx_desc + sc->cur_rx;
631 /* Switch to next descriptor */
632 sc->cur_rx = (sc->cur_rx+1) & RX_RING_MASK;
635 * Check for RX errors. This should only happen if
636 * SAVE_ERRORED_PACKETS is set. RX errors generate
637 * RXE interrupt usually.
639 if ((desc->status & 1) == 0) {
640 sc->sc_if.if_ierrors++;
641 desc->status = 0x8000;
645 /* Save packet length and mbuf contained packet */
646 len = desc->rxlength - ETHER_CRC_LEN;
649 /* Try to get mbuf cluster */
650 EPIC_MGETCLUSTER(buf->mbuf);
651 if (NULL == buf->mbuf) {
653 desc->status = 0x8000;
654 sc->sc_if.if_ierrors++;
658 /* Point to new mbuf, and give descriptor to chip */
659 desc->bufaddr = vtophys(mtod(buf->mbuf, caddr_t));
660 desc->status = 0x8000;
662 /* First mbuf in packet holds the ethernet and packet headers */
663 eh = mtod(m, struct ether_header *);
664 m->m_pkthdr.rcvif = &(sc->sc_if);
665 m->m_pkthdr.len = m->m_len = len;
667 /* Second mbuf holds packet ifself */
668 m->m_pkthdr.len = m->m_len = len - sizeof(struct ether_header);
669 m->m_data += sizeof(struct ether_header);
671 /* Give mbuf to OS */
672 ether_input(&sc->sc_if, eh, m);
674 /* Successfuly received frame */
675 sc->sc_if.if_ipackets++;
682 * Synopsis: Do last phase of transmission. I.e. if desc is
683 * transmitted, decrease pending_txs counter, free mbuf contained
684 * packet, switch to next descriptor and repeat until no packets
685 * are pending or descriptor is not transmitted yet.
691 struct epic_tx_buffer *buf;
692 struct epic_tx_desc *desc;
695 while (sc->pending_txs > 0) {
696 buf = sc->tx_buffer + sc->dirty_tx;
697 desc = sc->tx_desc + sc->dirty_tx;
698 status = desc->status;
700 /* If packet is not transmitted, thou followed */
701 /* packets are not transmitted too */
702 if (status & 0x8000) break;
704 /* Packet is transmitted. Switch to next and */
707 sc->dirty_tx = (sc->dirty_tx + 1) & TX_RING_MASK;
711 /* Check for errors and collisions */
712 if (status & 0x0001) sc->sc_if.if_opackets++;
713 else sc->sc_if.if_oerrors++;
714 sc->sc_if.if_collisions += (status >> 8) & 0x1F;
715 #if defined(EPIC_DIAG)
716 if ((status & 0x1001) == 0x1001)
717 device_printf(sc->dev, "Tx ERROR: excessive coll. number\n");
721 if (sc->pending_txs < TX_RING_SIZE)
722 sc->sc_if.if_flags &= ~IFF_OACTIVE;
732 epic_softc_t * sc = (epic_softc_t *) arg;
735 while (i-- && ((status = CSR_READ_4(sc, INTSTAT)) & INTSTAT_INT_ACTV)) {
736 CSR_WRITE_4(sc, INTSTAT, status);
738 if (status & (INTSTAT_RQE|INTSTAT_RCC|INTSTAT_OVW)) {
740 if (status & (INTSTAT_RQE|INTSTAT_OVW)) {
741 #if defined(EPIC_DIAG)
742 if (status & INTSTAT_OVW)
743 device_printf(sc->dev, "RX buffer overflow\n");
744 if (status & INTSTAT_RQE)
745 device_printf(sc->dev, "RX FIFO overflow\n");
747 if ((CSR_READ_4(sc, COMMAND) & COMMAND_RXQUEUED) == 0)
748 CSR_WRITE_4(sc, COMMAND, COMMAND_RXQUEUED);
749 sc->sc_if.if_ierrors++;
753 if (status & (INTSTAT_TXC|INTSTAT_TCC|INTSTAT_TQE)) {
755 if (sc->sc_if.if_snd.ifq_head != NULL)
756 epic_ifstart(&sc->sc_if);
759 /* Check for rare errors */
760 if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
761 INTSTAT_APE|INTSTAT_DPE|INTSTAT_TXU|INTSTAT_RXE)) {
762 if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
763 INTSTAT_APE|INTSTAT_DPE)) {
764 device_printf(sc->dev, "PCI fatal errors occured: %s%s%s%s\n",
765 (status&INTSTAT_PMA)?"PMA ":"",
766 (status&INTSTAT_PTA)?"PTA ":"",
767 (status&INTSTAT_APE)?"APE ":"",
768 (status&INTSTAT_DPE)?"DPE":""
777 if (status & INTSTAT_RXE) {
778 #if defined(EPIC_DIAG)
779 device_printf(sc->dev, "CRC/Alignment error\n");
781 sc->sc_if.if_ierrors++;
784 if (status & INTSTAT_TXU) {
785 epic_tx_underrun(sc);
786 sc->sc_if.if_oerrors++;
791 /* If no packets are pending, then no timeouts */
792 if (sc->pending_txs == 0) sc->sc_if.if_timer = 0;
798 * Handle the TX underrun error: increase the TX threshold
799 * and restart the transmitter.
805 if (sc->tx_threshold > TRANSMIT_THRESHOLD_MAX) {
806 sc->txcon &= ~TXCON_EARLY_TRANSMIT_ENABLE;
807 #if defined(EPIC_DIAG)
808 device_printf(sc->dev, "Tx UNDERRUN: early TX disabled\n");
811 sc->tx_threshold += 0x40;
812 #if defined(EPIC_DIAG)
813 device_printf(sc->dev, "Tx UNDERRUN: TX threshold increased to %d\n",
818 /* We must set TXUGO to reset the stuck transmitter */
819 CSR_WRITE_4(sc, COMMAND, COMMAND_TXUGO);
821 /* Update the TX threshold */
822 epic_stop_activity(sc);
823 epic_set_tx_mode(sc);
824 epic_start_activity(sc);
830 * Synopsis: This one is called if packets wasn't transmitted
831 * during timeout. Try to deallocate transmitted packets, and
832 * if success continue to work.
838 epic_softc_t *sc = ifp->if_softc;
843 device_printf(sc->dev, "device timeout %d packets\n", sc->pending_txs);
845 /* Try to finish queued packets */
848 /* If not successful */
849 if (sc->pending_txs > 0) {
851 ifp->if_oerrors+=sc->pending_txs;
853 /* Reinitialize board */
854 device_printf(sc->dev, "reinitialization\n");
859 device_printf(sc->dev, "seems we can continue normaly\n");
862 if (ifp->if_snd.ifq_head) epic_ifstart(ifp);
868 * Despite the name of this function, it doesn't update statistics, it only
869 * helps in autonegotiation process.
872 epic_stats_update(epic_softc_t * sc)
874 struct mii_data * mii;
879 mii = device_get_softc(sc->miibus);
882 sc->stat_ch = timeout((timeout_t *)epic_stats_update, sc, hz);
891 epic_ifmedia_upd(ifp)
895 struct mii_data *mii;
897 struct mii_softc *miisc;
901 mii = device_get_softc(sc->miibus);
902 ifm = &mii->mii_media;
903 media = ifm->ifm_cur->ifm_media;
905 /* Do not do anything if interface is not up */
906 if ((ifp->if_flags & IFF_UP) == 0)
910 * Lookup current selected PHY
912 if (IFM_INST(media) == sc->serinst) {
913 sc->phyid = EPIC_SERIAL;
916 /* If we're not selecting serial interface, select MII mode */
917 sc->miicfg &= ~MIICFG_SERIAL_ENABLE;
918 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
920 /* Default to unknown PHY */
921 sc->phyid = EPIC_UNKN_PHY;
923 /* Lookup selected PHY */
924 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
925 miisc = LIST_NEXT(miisc, mii_list)) {
926 if (IFM_INST(media) == miisc->mii_inst) {
932 /* Identify selected PHY */
934 int id1, id2, model, oui;
936 id1 = PHY_READ(sc->physc, MII_PHYIDR1);
937 id2 = PHY_READ(sc->physc, MII_PHYIDR2);
939 oui = MII_OUI(id1, id2);
940 model = MII_MODEL(id2);
942 case MII_OUI_QUALSEMI:
943 if (model == MII_MODEL_QUALSEMI_QS6612)
944 sc->phyid = EPIC_QS6612_PHY;
946 case MII_OUI_xxALTIMA:
947 if (model == MII_MODEL_xxALTIMA_AC101)
948 sc->phyid = EPIC_AC101_PHY;
950 case MII_OUI_xxLEVEL1:
951 if (model == MII_MODEL_xxLEVEL1_LXT970)
952 sc->phyid = EPIC_LXT970_PHY;
959 * Do PHY specific card setup
962 /* Call this, to isolate all not selected PHYs and
967 /* Do our own setup */
969 case EPIC_QS6612_PHY:
972 /* We have to powerup fiber tranceivers */
973 if (IFM_SUBTYPE(media) == IFM_100_FX)
974 sc->miicfg |= MIICFG_694_ENABLE;
976 sc->miicfg &= ~MIICFG_694_ENABLE;
977 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
980 case EPIC_LXT970_PHY:
981 /* We have to powerup fiber tranceivers */
982 cfg = PHY_READ(sc->physc, MII_LXTPHY_CONFIG);
983 if (IFM_SUBTYPE(media) == IFM_100_FX)
984 cfg |= CONFIG_LEDC1 | CONFIG_LEDC0;
986 cfg &= ~(CONFIG_LEDC1 | CONFIG_LEDC0);
987 PHY_WRITE(sc->physc, MII_LXTPHY_CONFIG, cfg);
991 /* Select serial PHY, (10base2/BNC usually) */
992 sc->miicfg |= MIICFG_694_ENABLE | MIICFG_SERIAL_ENABLE;
993 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
995 /* There is no driver to fill this */
996 mii->mii_media_active = media;
997 mii->mii_media_status = 0;
999 /* We need to call this manualy as i wasn't called
1002 epic_miibus_statchg(sc->dev);
1006 device_printf(sc->dev, "ERROR! Unknown PHY selected\n");
1014 * Report current media status.
1017 epic_ifmedia_sts(ifp, ifmr)
1019 struct ifmediareq *ifmr;
1022 struct mii_data *mii;
1023 struct ifmedia *ifm;
1026 mii = device_get_softc(sc->miibus);
1027 ifm = &mii->mii_media;
1029 /* Nothing should be selected if interface is down */
1030 if ((ifp->if_flags & IFF_UP) == 0) {
1031 ifmr->ifm_active = IFM_NONE;
1032 ifmr->ifm_status = 0;
1037 /* Call underlying pollstat, if not serial PHY */
1038 if (sc->phyid != EPIC_SERIAL)
1041 /* Simply copy media info */
1042 ifmr->ifm_active = mii->mii_media_active;
1043 ifmr->ifm_status = mii->mii_media_status;
1049 * Callback routine, called on media change.
1052 epic_miibus_statchg(dev)
1056 struct mii_data *mii;
1059 sc = device_get_softc(dev);
1060 mii = device_get_softc(sc->miibus);
1061 media = mii->mii_media_active;
1063 sc->txcon &= ~(TXCON_LOOPBACK_MODE | TXCON_FULL_DUPLEX);
1065 /* If we are in full-duplex mode or loopback operation,
1066 * we need to decouple receiver and transmitter.
1068 if (IFM_OPTIONS(media) & (IFM_FDX | IFM_LOOP))
1069 sc->txcon |= TXCON_FULL_DUPLEX;
1071 /* On some cards we need manualy set fullduplex led */
1072 if (sc->cardid == SMC9432FTX ||
1073 sc->cardid == SMC9432FTX_SC) {
1074 if (IFM_OPTIONS(media) & IFM_FDX)
1075 sc->miicfg |= MIICFG_694_ENABLE;
1077 sc->miicfg &= ~MIICFG_694_ENABLE;
1079 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1082 /* Update baudrate */
1083 if (IFM_SUBTYPE(media) == IFM_100_TX ||
1084 IFM_SUBTYPE(media) == IFM_100_FX)
1085 sc->sc_if.if_baudrate = 100000000;
1087 sc->sc_if.if_baudrate = 10000000;
1089 epic_stop_activity(sc);
1090 epic_set_tx_mode(sc);
1091 epic_start_activity(sc);
1097 epic_miibus_mediainit(dev)
1101 struct mii_data *mii;
1102 struct ifmedia *ifm;
1105 sc = device_get_softc(dev);
1106 mii = device_get_softc(sc->miibus);
1107 ifm = &mii->mii_media;
1109 /* Add Serial Media Interface if present, this applies to
1112 if (CSR_READ_4(sc, MIICFG) & MIICFG_PHY_PRESENT) {
1113 /* Store its instance */
1114 sc->serinst = mii->mii_instance++;
1116 /* Add as 10base2/BNC media */
1117 media = IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->serinst);
1118 ifmedia_add(ifm, media, 0, NULL);
1120 /* Report to user */
1121 device_printf(sc->dev, "serial PHY detected (10Base2/BNC)\n");
1128 * Reset chip, allocate rings, and update media.
1134 struct ifnet *ifp = &sc->sc_if;
1139 /* If interface is already running, then we need not do anything */
1140 if (ifp->if_flags & IFF_RUNNING) {
1145 /* Soft reset the chip (we have to power up card before) */
1146 CSR_WRITE_4(sc, GENCTL, 0);
1147 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1150 * Reset takes 15 pci ticks which depends on PCI bus speed.
1151 * Assuming it >= 33000000 hz, we have wait at least 495e-6 sec.
1156 CSR_WRITE_4(sc, GENCTL, 0);
1158 /* Workaround for Application Note 7-15 */
1159 for (i=0; i<16; i++) CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
1161 /* Initialize rings */
1162 if (epic_init_rings(sc)) {
1163 device_printf(sc->dev, "failed to init rings\n");
1168 /* Give rings to EPIC */
1169 CSR_WRITE_4(sc, PRCDAR, vtophys(sc->rx_desc));
1170 CSR_WRITE_4(sc, PTCDAR, vtophys(sc->tx_desc));
1172 /* Put node address to EPIC */
1173 CSR_WRITE_4(sc, LAN0, ((u_int16_t *)sc->sc_macaddr)[0]);
1174 CSR_WRITE_4(sc, LAN1, ((u_int16_t *)sc->sc_macaddr)[1]);
1175 CSR_WRITE_4(sc, LAN2, ((u_int16_t *)sc->sc_macaddr)[2]);
1177 /* Set tx mode, includeing transmit threshold */
1178 epic_set_tx_mode(sc);
1180 /* Compute and set RXCON. */
1181 epic_set_rx_mode(sc);
1183 /* Set multicast table */
1184 epic_set_mc_table(sc);
1186 /* Enable interrupts by setting the interrupt mask. */
1187 CSR_WRITE_4(sc, INTMASK,
1188 INTSTAT_RCC | /* INTSTAT_RQE | INTSTAT_OVW | INTSTAT_RXE | */
1189 /* INTSTAT_TXC | */ INTSTAT_TCC | INTSTAT_TQE | INTSTAT_TXU |
1192 /* Acknowledge all pending interrupts */
1193 CSR_WRITE_4(sc, INTSTAT, CSR_READ_4(sc, INTSTAT));
1195 /* Enable interrupts, set for PCI read multiple and etc */
1196 CSR_WRITE_4(sc, GENCTL,
1197 GENCTL_ENABLE_INTERRUPT | GENCTL_MEMORY_READ_MULTIPLE |
1198 GENCTL_ONECOPY | GENCTL_RECEIVE_FIFO_THRESHOLD64);
1200 /* Mark interface running ... */
1201 if (ifp->if_flags & IFF_UP) ifp->if_flags |= IFF_RUNNING;
1202 else ifp->if_flags &= ~IFF_RUNNING;
1205 ifp->if_flags &= ~IFF_OACTIVE;
1207 /* Start Rx process */
1208 epic_start_activity(sc);
1210 /* Set appropriate media */
1211 epic_ifmedia_upd(ifp);
1213 sc->stat_ch = timeout((timeout_t *)epic_stats_update, sc, hz);
1221 * Synopsis: calculate and set Rx mode. Chip must be in idle state to
1225 epic_set_rx_mode(sc)
1228 u_int32_t flags = sc->sc_if.if_flags;
1229 u_int32_t rxcon = RXCON_DEFAULT;
1231 #if defined(EPIC_EARLY_RX)
1232 rxcon |= RXCON_EARLY_RX;
1235 rxcon |= (flags & IFF_PROMISC) ? RXCON_PROMISCUOUS_MODE : 0;
1237 CSR_WRITE_4(sc, RXCON, rxcon);
1243 * Synopsis: Set transmit control register. Chip must be in idle state to
1247 epic_set_tx_mode(sc)
1250 if (sc->txcon & TXCON_EARLY_TRANSMIT_ENABLE)
1251 CSR_WRITE_4(sc, ETXTHR, sc->tx_threshold);
1253 CSR_WRITE_4(sc, TXCON, sc->txcon);
1257 * Synopsis: Program multicast filter honoring IFF_ALLMULTI and IFF_PROMISC
1258 * flags. (Note, that setting PROMISC bit in EPIC's RXCON will only touch
1259 * individual frames, multicast filter must be manually programmed)
1261 * Note: EPIC must be in idle state.
1264 epic_set_mc_table(sc)
1267 struct ifnet *ifp = &sc->sc_if;
1268 struct ifmultiaddr *ifma;
1269 u_int16_t filter[4];
1272 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
1273 CSR_WRITE_4(sc, MC0, 0xFFFF);
1274 CSR_WRITE_4(sc, MC1, 0xFFFF);
1275 CSR_WRITE_4(sc, MC2, 0xFFFF);
1276 CSR_WRITE_4(sc, MC3, 0xFFFF);
1286 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1287 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1289 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1291 if (ifma->ifma_addr->sa_family != AF_LINK)
1293 h = epic_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1294 filter[h >> 4] |= 1 << (h & 0xF);
1297 CSR_WRITE_4(sc, MC0, filter[0]);
1298 CSR_WRITE_4(sc, MC1, filter[1]);
1299 CSR_WRITE_4(sc, MC2, filter[2]);
1300 CSR_WRITE_4(sc, MC3, filter[3]);
1306 * Synopsis: calculate EPIC's hash of multicast address.
1312 u_int32_t crc, carry;
1316 /* Compute CRC for the address value. */
1317 crc = 0xFFFFFFFF; /* initial value */
1319 for (i = 0; i < 6; i++) {
1321 for (j = 0; j < 8; j++) {
1322 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
1326 crc = (crc ^ 0x04c11db6) | carry;
1330 return ((crc >> 26) & 0x3F);
1335 * Synopsis: Start receive process and transmit one, if they need.
1338 epic_start_activity(sc)
1341 /* Start rx process */
1342 CSR_WRITE_4(sc, COMMAND,
1343 COMMAND_RXQUEUED | COMMAND_START_RX |
1344 (sc->pending_txs?COMMAND_TXQUEUED:0));
1348 * Synopsis: Completely stop Rx and Tx processes. If TQE is set additional
1349 * packet needs to be queued to stop Tx DMA.
1352 epic_stop_activity(sc)
1357 /* Stop Tx and Rx DMA */
1358 CSR_WRITE_4(sc, COMMAND,
1359 COMMAND_STOP_RX | COMMAND_STOP_RDMA | COMMAND_STOP_TDMA);
1361 /* Wait Rx and Tx DMA to stop (why 1 ms ??? XXX) */
1362 for (i=0; i<0x1000; i++) {
1363 status = CSR_READ_4(sc, INTSTAT) & (INTSTAT_TXIDLE | INTSTAT_RXIDLE);
1364 if (status == (INTSTAT_TXIDLE | INTSTAT_RXIDLE))
1369 /* Catch all finished packets */
1373 status = CSR_READ_4(sc, INTSTAT);
1375 if ((status & INTSTAT_RXIDLE) == 0)
1376 device_printf(sc->dev, "ERROR! Can't stop Rx DMA\n");
1378 if ((status & INTSTAT_TXIDLE) == 0)
1379 device_printf(sc->dev, "ERROR! Can't stop Tx DMA\n");
1382 * May need to queue one more packet if TQE, this is rare
1383 * but existing case.
1385 if ((status & INTSTAT_TQE) && !(status & INTSTAT_TXIDLE))
1386 (void) epic_queue_last_packet(sc);
1391 * The EPIC transmitter may stuck in TQE state. It will not go IDLE until
1392 * a packet from current descriptor will be copied to internal RAM. We
1393 * compose a dummy packet here and queue it for transmission.
1395 * XXX the packet will then be actually sent over network...
1398 epic_queue_last_packet(sc)
1401 struct epic_tx_desc *desc;
1402 struct epic_frag_list *flist;
1403 struct epic_tx_buffer *buf;
1407 device_printf(sc->dev, "queue last packet\n");
1409 desc = sc->tx_desc + sc->cur_tx;
1410 flist = sc->tx_flist + sc->cur_tx;
1411 buf = sc->tx_buffer + sc->cur_tx;
1413 if ((desc->status & 0x8000) || (buf->mbuf != NULL))
1416 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1421 m0->m_len = min(MHLEN, ETHER_MIN_LEN-ETHER_CRC_LEN);
1422 flist->frag[0].fraglen = m0->m_len;
1423 m0->m_pkthdr.len = m0->m_len;
1424 m0->m_pkthdr.rcvif = &sc->sc_if;
1425 bzero(mtod(m0,caddr_t), m0->m_len);
1427 /* Fill fragments list */
1428 flist->frag[0].fraglen = m0->m_len;
1429 flist->frag[0].fragaddr = vtophys(mtod(m0, caddr_t));
1430 flist->numfrags = 1;
1432 /* Fill in descriptor */
1435 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
1436 desc->control = 0x01;
1437 desc->txlength = max(m0->m_pkthdr.len,ETHER_MIN_LEN-ETHER_CRC_LEN);
1438 desc->status = 0x8000;
1440 /* Launch transmition */
1441 CSR_WRITE_4(sc, COMMAND, COMMAND_STOP_TDMA | COMMAND_TXQUEUED);
1443 /* Wait Tx DMA to stop (for how long??? XXX) */
1444 for (i=0; i<1000; i++) {
1445 if (CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE)
1450 if ((CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE) == 0)
1451 device_printf(sc->dev, "ERROR! can't stop Tx DMA (2)\n");
1459 * Synopsis: Shut down board and deallocates rings.
1469 sc->sc_if.if_timer = 0;
1471 untimeout((timeout_t *)epic_stats_update, sc, sc->stat_ch);
1473 /* Disable interrupts */
1474 CSR_WRITE_4(sc, INTMASK, 0);
1475 CSR_WRITE_4(sc, GENCTL, 0);
1477 /* Try to stop Rx and TX processes */
1478 epic_stop_activity(sc);
1481 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1484 /* Make chip go to bed */
1485 CSR_WRITE_4(sc, GENCTL, GENCTL_POWER_DOWN);
1487 /* Free memory allocated for rings */
1488 epic_free_rings(sc);
1490 /* Mark as stoped */
1491 sc->sc_if.if_flags &= ~IFF_RUNNING;
1498 * Synopsis: This function should free all memory allocated for rings.
1506 for (i=0; i<RX_RING_SIZE; i++) {
1507 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1508 struct epic_rx_desc *desc = sc->rx_desc + i;
1511 desc->buflength = 0;
1514 if (buf->mbuf) m_freem(buf->mbuf);
1518 for (i=0; i<TX_RING_SIZE; i++) {
1519 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1520 struct epic_tx_desc *desc = sc->tx_desc + i;
1523 desc->buflength = 0;
1526 if (buf->mbuf) m_freem(buf->mbuf);
1532 * Synopsis: Allocates mbufs for Rx ring and point Rx descs to them.
1533 * Point Tx descs to fragment lists. Check that all descs and fraglists
1534 * are bounded and aligned properly.
1542 sc->cur_rx = sc->cur_tx = sc->dirty_tx = sc->pending_txs = 0;
1544 for (i = 0; i < RX_RING_SIZE; i++) {
1545 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1546 struct epic_rx_desc *desc = sc->rx_desc + i;
1548 desc->status = 0; /* Owned by driver */
1549 desc->next = vtophys(sc->rx_desc + ((i+1) & RX_RING_MASK));
1551 if ((desc->next & 3) ||
1552 ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1553 epic_free_rings(sc);
1557 EPIC_MGETCLUSTER(buf->mbuf);
1558 if (NULL == buf->mbuf) {
1559 epic_free_rings(sc);
1562 desc->bufaddr = vtophys(mtod(buf->mbuf, caddr_t));
1564 desc->buflength = MCLBYTES; /* Max RX buffer length */
1565 desc->status = 0x8000; /* Set owner bit to NIC */
1568 for (i = 0; i < TX_RING_SIZE; i++) {
1569 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1570 struct epic_tx_desc *desc = sc->tx_desc + i;
1573 desc->next = vtophys(sc->tx_desc + ((i+1) & TX_RING_MASK));
1575 if ((desc->next & 3) ||
1576 ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1577 epic_free_rings(sc);
1582 desc->bufaddr = vtophys(sc->tx_flist + i);
1584 if ((desc->bufaddr & 3) ||
1585 ((desc->bufaddr & PAGE_MASK) + sizeof(struct epic_frag_list)) > PAGE_SIZE) {
1586 epic_free_rings(sc);
1595 * EEPROM operation functions
1598 epic_write_eepromreg(sc, val)
1604 CSR_WRITE_1(sc, EECTL, val);
1606 for (i=0; i<0xFF; i++)
1607 if ((CSR_READ_1(sc, EECTL) & 0x20) == 0) break;
1613 epic_read_eepromreg(sc)
1616 return CSR_READ_1(sc, EECTL);
1620 epic_eeprom_clock(sc, val)
1624 epic_write_eepromreg(sc, val);
1625 epic_write_eepromreg(sc, (val | 0x4));
1626 epic_write_eepromreg(sc, val);
1628 return epic_read_eepromreg(sc);
1632 epic_output_eepromw(sc, val)
1638 for (i = 0xF; i >= 0; i--) {
1640 epic_eeprom_clock(sc, 0x0B);
1642 epic_eeprom_clock(sc, 0x03);
1647 epic_input_eepromw(sc)
1650 u_int16_t retval = 0;
1653 for (i = 0xF; i >= 0; i--) {
1654 if (epic_eeprom_clock(sc, 0x3) & 0x10)
1662 epic_read_eeprom(sc, loc)
1669 epic_write_eepromreg(sc, 3);
1671 if (epic_read_eepromreg(sc) & 0x40)
1672 read_cmd = (loc & 0x3F) | 0x180;
1674 read_cmd = (loc & 0xFF) | 0x600;
1676 epic_output_eepromw(sc, read_cmd);
1678 dataval = epic_input_eepromw(sc);
1680 epic_write_eepromreg(sc, 1);
1686 * Here goes MII read/write routines
1689 epic_read_phy_reg(sc, phy, reg)
1695 CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x01));
1697 for (i = 0; i < 0x100; i++) {
1698 if ((CSR_READ_4(sc, MIICTL) & 0x01) == 0) break;
1702 return (CSR_READ_4(sc, MIIDATA));
1706 epic_write_phy_reg(sc, phy, reg, val)
1712 CSR_WRITE_4(sc, MIIDATA, val);
1713 CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x02));
1715 for(i=0;i<0x100;i++) {
1716 if ((CSR_READ_4(sc, MIICTL) & 0x02) == 0) break;
1724 epic_miibus_readreg(dev, phy, reg)
1730 sc = device_get_softc(dev);
1732 return (PHY_READ_2(sc, phy, reg));
1736 epic_miibus_writereg(dev, phy, reg, data)
1742 sc = device_get_softc(dev);
1744 PHY_WRITE_2(sc, phy, reg, data);