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.19 2005/05/27 15:36:10 joerg 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/ifq_var.h>
53 #include <net/if_arp.h>
54 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
60 #include <net/vlan/if_vlan_var.h>
62 #include <vm/vm.h> /* for vtophys */
63 #include <vm/pmap.h> /* for vtophys */
64 #include <machine/bus_memio.h>
65 #include <machine/bus_pio.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
68 #include <machine/clock.h> /* for DELAY */
72 #include <bus/pci/pcireg.h>
73 #include <bus/pci/pcivar.h>
75 #include "../mii_layer/mii.h"
76 #include "../mii_layer/miivar.h"
77 #include "../mii_layer/miidevs.h"
78 #include "../mii_layer/lxtphyreg.h"
80 #include "miibus_if.h"
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 u_int8_t epic_calchash(caddr_t);
105 static int epic_read_eeprom(epic_softc_t *,u_int16_t);
106 static void epic_output_eepromw(epic_softc_t *, u_int16_t);
107 static u_int16_t epic_input_eepromw(epic_softc_t *);
108 static u_int8_t epic_eeprom_clock(epic_softc_t *,u_int8_t);
109 static void epic_write_eepromreg(epic_softc_t *,u_int8_t);
110 static u_int8_t epic_read_eepromreg(epic_softc_t *);
112 static int epic_read_phy_reg(epic_softc_t *, int, int);
113 static void epic_write_phy_reg(epic_softc_t *, int, int, int);
115 static int epic_miibus_readreg(device_t, int, int);
116 static int epic_miibus_writereg(device_t, int, int, int);
117 static void epic_miibus_statchg(device_t);
118 static void epic_miibus_mediainit(device_t);
120 static int epic_ifmedia_upd(struct ifnet *);
121 static void epic_ifmedia_sts(struct ifnet *, struct ifmediareq *);
123 static int epic_probe(device_t);
124 static int epic_attach(device_t);
125 static void epic_shutdown(device_t);
126 static int epic_detach(device_t);
127 static struct epic_type *epic_devtype(device_t);
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),
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),
145 static driver_t epic_driver = {
151 static devclass_t epic_devclass;
153 DECLARE_DUMMY_MODULE(if_tx);
154 MODULE_DEPEND(if_tx, miibus, 1, 1, 1);
155 DRIVER_MODULE(if_tx, pci, epic_driver, epic_devclass, 0, 0);
156 DRIVER_MODULE(miibus, tx, miibus_driver, miibus_devclass, 0, 0);
158 static struct epic_type epic_devs[] = {
159 { SMC_VENDORID, SMC_DEVICEID_83C170,
160 "SMC EtherPower II 10/100" },
170 t = epic_devtype(dev);
173 device_set_desc(dev, t->name);
180 static struct epic_type *
188 while(t->name != NULL) {
189 if ((pci_get_vendor(dev) == t->ven_id) &&
190 (pci_get_device(dev) == t->dev_id)) {
198 #if defined(EPIC_USEIOSPACE)
199 #define EPIC_RES SYS_RES_IOPORT
200 #define EPIC_RID PCIR_BASEIO
202 #define EPIC_RES SYS_RES_MEMORY
203 #define EPIC_RID PCIR_BASEMEM
207 * Attach routine: map registers, allocate softc, rings and descriptors.
208 * Reset to known state.
222 sc = device_get_softc(dev);
223 unit = device_get_unit(dev);
225 /* Preinitialize softc structure */
226 bzero(sc, sizeof(epic_softc_t));
229 callout_init(&sc->tx_stat_timer);
231 /* Fill ifnet structure */
233 if_initname(ifp, "tx", unit);
235 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
236 ifp->if_ioctl = epic_ifioctl;
237 ifp->if_start = epic_ifstart;
238 ifp->if_watchdog = epic_ifwatchdog;
239 ifp->if_init = (if_init_f_t*)epic_init;
241 ifp->if_baudrate = 10000000;
242 ifq_set_maxlen(&ifp->if_snd, TX_RING_SIZE - 1);
243 ifq_set_ready(&ifp->if_snd);
245 /* Enable ports, memory and busmastering */
246 command = pci_read_config(dev, PCIR_COMMAND, 4);
247 command |= PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
248 pci_write_config(dev, PCIR_COMMAND, command, 4);
249 command = pci_read_config(dev, PCIR_COMMAND, 4);
251 #if defined(EPIC_USEIOSPACE)
252 if ((command & PCIM_CMD_PORTEN) == 0) {
253 device_printf(dev, "failed to enable I/O mapping!\n");
258 if ((command & PCIM_CMD_MEMEN) == 0) {
259 device_printf(dev, "failed to enable memory mapping!\n");
266 sc->res = bus_alloc_resource_any(dev, EPIC_RES, &rid, RF_ACTIVE);
268 if (sc->res == NULL) {
269 device_printf(dev, "couldn't map ports/memory\n");
274 sc->sc_st = rman_get_bustag(sc->res);
275 sc->sc_sh = rman_get_bushandle(sc->res);
277 /* Allocate interrupt */
279 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
280 RF_SHAREABLE | RF_ACTIVE);
282 if (sc->irq == NULL) {
283 device_printf(dev, "couldn't map interrupt\n");
284 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
289 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
290 epic_intr, sc, &sc->sc_ih, NULL);
293 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
294 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
295 device_printf(dev, "couldn't set up irq\n");
299 /* Do OS independent part, including chip wakeup and reset */
300 error = epic_common_attach(sc);
302 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
303 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
304 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
309 /* Do ifmedia setup */
310 if (mii_phy_probe(dev, &sc->miibus,
311 epic_ifmedia_upd, epic_ifmedia_sts)) {
312 device_printf(dev, "ERROR! MII without any PHY!?\n");
313 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
314 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
315 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
320 /* board type and ... */
322 for(i=0x2c;i<0x32;i++) {
323 tmp = epic_read_eeprom(sc, i);
324 if (' ' == (u_int8_t)tmp) break;
325 printf("%c", (u_int8_t)tmp);
327 if (' ' == (u_int8_t)tmp) break;
328 printf("%c", (u_int8_t)tmp);
332 /* Attach to OS's managers */
333 ether_ifattach(ifp, sc->sc_macaddr);
334 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
343 * Detach driver and free resources
355 sc = device_get_softc(dev);
356 ifp = &sc->arpcom.ac_if;
362 bus_generic_detach(dev);
363 device_delete_child(dev, sc->miibus);
365 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
366 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
367 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
369 free(sc->tx_flist, M_DEVBUF);
370 free(sc->tx_desc, M_DEVBUF);
371 free(sc->rx_desc, M_DEVBUF);
382 * Stop all chip I/O so that the kernel's probe routines don't
383 * get confused by errant DMAs when rebooting.
391 sc = device_get_softc(dev);
399 * This is if_ioctl handler.
402 epic_ifioctl(ifp, command, data, cr)
408 epic_softc_t *sc = ifp->if_softc;
409 struct mii_data *mii;
410 struct ifreq *ifr = (struct ifreq *) data;
417 if (ifp->if_mtu == ifr->ifr_mtu)
420 /* XXX Though the datasheet doesn't imply any
421 * limitations on RX and TX sizes beside max 64Kb
422 * DMA transfer, seems we can't send more then 1600
423 * data bytes per ethernet packet. (Transmitter hangs
424 * up if more data is sent)
426 if (ifr->ifr_mtu + ifp->if_hdrlen <= EPIC_MAX_MTU) {
427 ifp->if_mtu = ifr->ifr_mtu;
436 * If the interface is marked up and stopped, then start it.
437 * If it is marked down and running, then stop it.
439 if (ifp->if_flags & IFF_UP) {
440 if ((ifp->if_flags & IFF_RUNNING) == 0) {
445 if (ifp->if_flags & IFF_RUNNING) {
451 /* Handle IFF_PROMISC and IFF_ALLMULTI flags */
452 epic_stop_activity(sc);
453 epic_set_mc_table(sc);
454 epic_set_rx_mode(sc);
455 epic_start_activity(sc);
460 epic_set_mc_table(sc);
466 mii = device_get_softc(sc->miibus);
467 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
471 error = ether_ioctl(ifp, command, data);
480 * OS-independed part of attach process. allocate memory for descriptors
481 * and frag lists, wake up chip, read MAC address and PHY identyfier.
482 * Return -1 on failure.
485 epic_common_attach(sc)
490 sc->tx_flist = malloc(sizeof(struct epic_frag_list)*TX_RING_SIZE,
491 M_DEVBUF, M_WAITOK | M_ZERO);
492 sc->tx_desc = malloc(sizeof(struct epic_tx_desc)*TX_RING_SIZE,
493 M_DEVBUF, M_WAITOK | M_ZERO);
494 sc->rx_desc = malloc(sizeof(struct epic_rx_desc)*RX_RING_SIZE,
495 M_DEVBUF, M_WAITOK | M_ZERO);
497 /* Bring the chip out of low-power mode. */
498 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
501 /* Workaround for Application Note 7-15 */
502 for (i=0; i<16; i++) CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
504 /* Read mac address from EEPROM */
505 for (i = 0; i < ETHER_ADDR_LEN / sizeof(u_int16_t); i++)
506 ((u_int16_t *)sc->sc_macaddr)[i] = epic_read_eeprom(sc,i);
508 /* Set Non-Volatile Control Register from EEPROM */
509 CSR_WRITE_4(sc, NVCTL, epic_read_eeprom(sc, EEPROM_NVCTL) & 0x1F);
512 sc->tx_threshold = TRANSMIT_THRESHOLD;
513 sc->txcon = TXCON_DEFAULT;
514 sc->miicfg = MIICFG_SMI_ENABLE;
515 sc->phyid = EPIC_UNKN_PHY;
519 sc->cardvend = pci_read_config(sc->dev, PCIR_SUBVEND_0, 2);
520 sc->cardid = pci_read_config(sc->dev, PCIR_SUBDEV_0, 2);
522 if (sc->cardvend != SMC_VENDORID)
523 device_printf(sc->dev, "unknown card vendor %04xh\n", sc->cardvend);
529 * This is if_start handler. It takes mbufs from if_snd queue
530 * and queue them for transmit, one by one, until TX ring become full
531 * or queue become empty.
537 epic_softc_t *sc = ifp->if_softc;
538 struct epic_tx_buffer *buf;
539 struct epic_tx_desc *desc;
540 struct epic_frag_list *flist;
545 while (sc->pending_txs < TX_RING_SIZE) {
546 buf = sc->tx_buffer + sc->cur_tx;
547 desc = sc->tx_desc + sc->cur_tx;
548 flist = sc->tx_flist + sc->cur_tx;
550 /* Get next packet to send */
551 m0 = ifq_dequeue(&ifp->if_snd);
553 /* If nothing to send, return */
557 /* Fill fragments list */
559 (NULL != m) && (i < EPIC_MAX_FRAGS);
560 m = m->m_next, i++) {
561 flist->frag[i].fraglen = m->m_len;
562 flist->frag[i].fragaddr = vtophys(mtod(m, caddr_t));
566 /* If packet was more than EPIC_MAX_FRAGS parts, */
567 /* recopy packet to new allocated mbuf cluster */
576 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
577 flist->frag[0].fraglen =
578 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
579 m->m_pkthdr.rcvif = ifp;
582 flist->frag[0].fragaddr = vtophys(mtod(m, caddr_t));
589 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
590 desc->control = 0x01;
592 max(m0->m_pkthdr.len,ETHER_MIN_LEN-ETHER_CRC_LEN);
593 desc->status = 0x8000;
594 CSR_WRITE_4(sc, COMMAND, COMMAND_TXQUEUED);
596 /* Set watchdog timer */
602 ifp->if_flags |= IFF_OACTIVE;
609 * Synopsis: Finish all received frames.
616 struct ifnet *ifp = &sc->sc_if;
617 struct epic_rx_buffer *buf;
618 struct epic_rx_desc *desc;
621 while ((sc->rx_desc[sc->cur_rx].status & 0x8000) == 0) {
622 buf = sc->rx_buffer + sc->cur_rx;
623 desc = sc->rx_desc + sc->cur_rx;
625 /* Switch to next descriptor */
626 sc->cur_rx = (sc->cur_rx+1) & RX_RING_MASK;
629 * Check for RX errors. This should only happen if
630 * SAVE_ERRORED_PACKETS is set. RX errors generate
631 * RXE interrupt usually.
633 if ((desc->status & 1) == 0) {
634 sc->sc_if.if_ierrors++;
635 desc->status = 0x8000;
639 /* Save packet length and mbuf contained packet */
640 len = desc->rxlength - ETHER_CRC_LEN;
643 /* Try to get mbuf cluster */
644 EPIC_MGETCLUSTER(buf->mbuf);
645 if (NULL == buf->mbuf) {
647 desc->status = 0x8000;
652 /* Point to new mbuf, and give descriptor to chip */
653 desc->bufaddr = vtophys(mtod(buf->mbuf, caddr_t));
654 desc->status = 0x8000;
656 /* First mbuf in packet holds the ethernet and packet headers */
657 m->m_pkthdr.rcvif = ifp;
658 m->m_pkthdr.len = m->m_len = len;
660 /* Give mbuf to OS */
661 (*ifp->if_input)(ifp, m);
663 /* Successfuly received frame */
671 * Synopsis: Do last phase of transmission. I.e. if desc is
672 * transmitted, decrease pending_txs counter, free mbuf contained
673 * packet, switch to next descriptor and repeat until no packets
674 * are pending or descriptor is not transmitted yet.
680 struct epic_tx_buffer *buf;
681 struct epic_tx_desc *desc;
684 while (sc->pending_txs > 0) {
685 buf = sc->tx_buffer + sc->dirty_tx;
686 desc = sc->tx_desc + sc->dirty_tx;
687 status = desc->status;
689 /* If packet is not transmitted, thou followed */
690 /* packets are not transmitted too */
691 if (status & 0x8000) break;
693 /* Packet is transmitted. Switch to next and */
696 sc->dirty_tx = (sc->dirty_tx + 1) & TX_RING_MASK;
700 /* Check for errors and collisions */
701 if (status & 0x0001) sc->sc_if.if_opackets++;
702 else sc->sc_if.if_oerrors++;
703 sc->sc_if.if_collisions += (status >> 8) & 0x1F;
704 #if defined(EPIC_DIAG)
705 if ((status & 0x1001) == 0x1001)
706 device_printf(sc->dev, "Tx ERROR: excessive coll. number\n");
710 if (sc->pending_txs < TX_RING_SIZE)
711 sc->sc_if.if_flags &= ~IFF_OACTIVE;
721 epic_softc_t * sc = (epic_softc_t *) arg;
724 while (i-- && ((status = CSR_READ_4(sc, INTSTAT)) & INTSTAT_INT_ACTV)) {
725 CSR_WRITE_4(sc, INTSTAT, status);
727 if (status & (INTSTAT_RQE|INTSTAT_RCC|INTSTAT_OVW)) {
729 if (status & (INTSTAT_RQE|INTSTAT_OVW)) {
730 #if defined(EPIC_DIAG)
731 if (status & INTSTAT_OVW)
732 device_printf(sc->dev, "RX buffer overflow\n");
733 if (status & INTSTAT_RQE)
734 device_printf(sc->dev, "RX FIFO overflow\n");
736 if ((CSR_READ_4(sc, COMMAND) & COMMAND_RXQUEUED) == 0)
737 CSR_WRITE_4(sc, COMMAND, COMMAND_RXQUEUED);
738 sc->sc_if.if_ierrors++;
742 if (status & (INTSTAT_TXC|INTSTAT_TCC|INTSTAT_TQE)) {
744 if (!ifq_is_empty(&sc->sc_if.if_snd))
745 epic_ifstart(&sc->sc_if);
748 /* Check for rare errors */
749 if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
750 INTSTAT_APE|INTSTAT_DPE|INTSTAT_TXU|INTSTAT_RXE)) {
751 if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
752 INTSTAT_APE|INTSTAT_DPE)) {
753 device_printf(sc->dev, "PCI fatal errors occured: %s%s%s%s\n",
754 (status&INTSTAT_PMA)?"PMA ":"",
755 (status&INTSTAT_PTA)?"PTA ":"",
756 (status&INTSTAT_APE)?"APE ":"",
757 (status&INTSTAT_DPE)?"DPE":""
766 if (status & INTSTAT_RXE) {
767 #if defined(EPIC_DIAG)
768 device_printf(sc->dev, "CRC/Alignment error\n");
770 sc->sc_if.if_ierrors++;
773 if (status & INTSTAT_TXU) {
774 epic_tx_underrun(sc);
775 sc->sc_if.if_oerrors++;
780 /* If no packets are pending, then no timeouts */
781 if (sc->pending_txs == 0) sc->sc_if.if_timer = 0;
787 * Handle the TX underrun error: increase the TX threshold
788 * and restart the transmitter.
794 if (sc->tx_threshold > TRANSMIT_THRESHOLD_MAX) {
795 sc->txcon &= ~TXCON_EARLY_TRANSMIT_ENABLE;
796 #if defined(EPIC_DIAG)
797 device_printf(sc->dev, "Tx UNDERRUN: early TX disabled\n");
800 sc->tx_threshold += 0x40;
801 #if defined(EPIC_DIAG)
802 device_printf(sc->dev, "Tx UNDERRUN: TX threshold increased to %d\n",
807 /* We must set TXUGO to reset the stuck transmitter */
808 CSR_WRITE_4(sc, COMMAND, COMMAND_TXUGO);
810 /* Update the TX threshold */
811 epic_stop_activity(sc);
812 epic_set_tx_mode(sc);
813 epic_start_activity(sc);
819 * Synopsis: This one is called if packets wasn't transmitted
820 * during timeout. Try to deallocate transmitted packets, and
821 * if success continue to work.
827 epic_softc_t *sc = ifp->if_softc;
832 device_printf(sc->dev, "device timeout %d packets\n", sc->pending_txs);
834 /* Try to finish queued packets */
837 /* If not successful */
838 if (sc->pending_txs > 0) {
840 ifp->if_oerrors+=sc->pending_txs;
842 /* Reinitialize board */
843 device_printf(sc->dev, "reinitialization\n");
848 device_printf(sc->dev, "seems we can continue normaly\n");
851 if (!ifq_is_empty(&ifp->if_snd))
858 * Despite the name of this function, it doesn't update statistics, it only
859 * helps in autonegotiation process.
862 epic_stats_update(void *xsc)
864 epic_softc_t *sc = xsc;
865 struct mii_data * mii;
870 mii = device_get_softc(sc->miibus);
873 callout_reset(&sc->tx_stat_timer, hz, epic_stats_update, sc);
882 epic_ifmedia_upd(ifp)
886 struct mii_data *mii;
888 struct mii_softc *miisc;
892 mii = device_get_softc(sc->miibus);
893 ifm = &mii->mii_media;
894 media = ifm->ifm_cur->ifm_media;
896 /* Do not do anything if interface is not up */
897 if ((ifp->if_flags & IFF_UP) == 0)
901 * Lookup current selected PHY
903 if (IFM_INST(media) == sc->serinst) {
904 sc->phyid = EPIC_SERIAL;
907 /* If we're not selecting serial interface, select MII mode */
908 sc->miicfg &= ~MIICFG_SERIAL_ENABLE;
909 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
911 /* Default to unknown PHY */
912 sc->phyid = EPIC_UNKN_PHY;
914 /* Lookup selected PHY */
915 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
916 miisc = LIST_NEXT(miisc, mii_list)) {
917 if (IFM_INST(media) == miisc->mii_inst) {
923 /* Identify selected PHY */
925 int id1, id2, model, oui;
927 id1 = PHY_READ(sc->physc, MII_PHYIDR1);
928 id2 = PHY_READ(sc->physc, MII_PHYIDR2);
930 oui = MII_OUI(id1, id2);
931 model = MII_MODEL(id2);
933 case MII_OUI_QUALSEMI:
934 if (model == MII_MODEL_QUALSEMI_QS6612)
935 sc->phyid = EPIC_QS6612_PHY;
937 case MII_OUI_xxALTIMA:
938 if (model == MII_MODEL_xxALTIMA_AC101)
939 sc->phyid = EPIC_AC101_PHY;
941 case MII_OUI_xxLEVEL1:
942 if (model == MII_MODEL_xxLEVEL1_LXT970)
943 sc->phyid = EPIC_LXT970_PHY;
950 * Do PHY specific card setup
953 /* Call this, to isolate all not selected PHYs and
958 /* Do our own setup */
960 case EPIC_QS6612_PHY:
963 /* We have to powerup fiber tranceivers */
964 if (IFM_SUBTYPE(media) == IFM_100_FX)
965 sc->miicfg |= MIICFG_694_ENABLE;
967 sc->miicfg &= ~MIICFG_694_ENABLE;
968 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
971 case EPIC_LXT970_PHY:
972 /* We have to powerup fiber tranceivers */
973 cfg = PHY_READ(sc->physc, MII_LXTPHY_CONFIG);
974 if (IFM_SUBTYPE(media) == IFM_100_FX)
975 cfg |= CONFIG_LEDC1 | CONFIG_LEDC0;
977 cfg &= ~(CONFIG_LEDC1 | CONFIG_LEDC0);
978 PHY_WRITE(sc->physc, MII_LXTPHY_CONFIG, cfg);
982 /* Select serial PHY, (10base2/BNC usually) */
983 sc->miicfg |= MIICFG_694_ENABLE | MIICFG_SERIAL_ENABLE;
984 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
986 /* There is no driver to fill this */
987 mii->mii_media_active = media;
988 mii->mii_media_status = 0;
990 /* We need to call this manualy as i wasn't called
993 epic_miibus_statchg(sc->dev);
997 device_printf(sc->dev, "ERROR! Unknown PHY selected\n");
1005 * Report current media status.
1008 epic_ifmedia_sts(ifp, ifmr)
1010 struct ifmediareq *ifmr;
1013 struct mii_data *mii;
1014 struct ifmedia *ifm;
1017 mii = device_get_softc(sc->miibus);
1018 ifm = &mii->mii_media;
1020 /* Nothing should be selected if interface is down */
1021 if ((ifp->if_flags & IFF_UP) == 0) {
1022 ifmr->ifm_active = IFM_NONE;
1023 ifmr->ifm_status = 0;
1028 /* Call underlying pollstat, if not serial PHY */
1029 if (sc->phyid != EPIC_SERIAL)
1032 /* Simply copy media info */
1033 ifmr->ifm_active = mii->mii_media_active;
1034 ifmr->ifm_status = mii->mii_media_status;
1040 * Callback routine, called on media change.
1043 epic_miibus_statchg(dev)
1047 struct mii_data *mii;
1050 sc = device_get_softc(dev);
1051 mii = device_get_softc(sc->miibus);
1052 media = mii->mii_media_active;
1054 sc->txcon &= ~(TXCON_LOOPBACK_MODE | TXCON_FULL_DUPLEX);
1056 /* If we are in full-duplex mode or loopback operation,
1057 * we need to decouple receiver and transmitter.
1059 if (IFM_OPTIONS(media) & (IFM_FDX | IFM_LOOP))
1060 sc->txcon |= TXCON_FULL_DUPLEX;
1062 /* On some cards we need manualy set fullduplex led */
1063 if (sc->cardid == SMC9432FTX ||
1064 sc->cardid == SMC9432FTX_SC) {
1065 if (IFM_OPTIONS(media) & IFM_FDX)
1066 sc->miicfg |= MIICFG_694_ENABLE;
1068 sc->miicfg &= ~MIICFG_694_ENABLE;
1070 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1073 /* Update baudrate */
1074 if (IFM_SUBTYPE(media) == IFM_100_TX ||
1075 IFM_SUBTYPE(media) == IFM_100_FX)
1076 sc->sc_if.if_baudrate = 100000000;
1078 sc->sc_if.if_baudrate = 10000000;
1080 epic_stop_activity(sc);
1081 epic_set_tx_mode(sc);
1082 epic_start_activity(sc);
1088 epic_miibus_mediainit(dev)
1092 struct mii_data *mii;
1093 struct ifmedia *ifm;
1096 sc = device_get_softc(dev);
1097 mii = device_get_softc(sc->miibus);
1098 ifm = &mii->mii_media;
1100 /* Add Serial Media Interface if present, this applies to
1103 if (CSR_READ_4(sc, MIICFG) & MIICFG_PHY_PRESENT) {
1104 /* Store its instance */
1105 sc->serinst = mii->mii_instance++;
1107 /* Add as 10base2/BNC media */
1108 media = IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->serinst);
1109 ifmedia_add(ifm, media, 0, NULL);
1111 /* Report to user */
1112 device_printf(sc->dev, "serial PHY detected (10Base2/BNC)\n");
1119 * Reset chip, allocate rings, and update media.
1125 struct ifnet *ifp = &sc->sc_if;
1130 /* If interface is already running, then we need not do anything */
1131 if (ifp->if_flags & IFF_RUNNING) {
1136 /* Soft reset the chip (we have to power up card before) */
1137 CSR_WRITE_4(sc, GENCTL, 0);
1138 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1141 * Reset takes 15 pci ticks which depends on PCI bus speed.
1142 * Assuming it >= 33000000 hz, we have wait at least 495e-6 sec.
1147 CSR_WRITE_4(sc, GENCTL, 0);
1149 /* Workaround for Application Note 7-15 */
1150 for (i=0; i<16; i++) CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
1152 /* Initialize rings */
1153 if (epic_init_rings(sc)) {
1154 device_printf(sc->dev, "failed to init rings\n");
1159 /* Give rings to EPIC */
1160 CSR_WRITE_4(sc, PRCDAR, vtophys(sc->rx_desc));
1161 CSR_WRITE_4(sc, PTCDAR, vtophys(sc->tx_desc));
1163 /* Put node address to EPIC */
1164 CSR_WRITE_4(sc, LAN0, ((u_int16_t *)sc->sc_macaddr)[0]);
1165 CSR_WRITE_4(sc, LAN1, ((u_int16_t *)sc->sc_macaddr)[1]);
1166 CSR_WRITE_4(sc, LAN2, ((u_int16_t *)sc->sc_macaddr)[2]);
1168 /* Set tx mode, includeing transmit threshold */
1169 epic_set_tx_mode(sc);
1171 /* Compute and set RXCON. */
1172 epic_set_rx_mode(sc);
1174 /* Set multicast table */
1175 epic_set_mc_table(sc);
1177 /* Enable interrupts by setting the interrupt mask. */
1178 CSR_WRITE_4(sc, INTMASK,
1179 INTSTAT_RCC | /* INTSTAT_RQE | INTSTAT_OVW | INTSTAT_RXE | */
1180 /* INTSTAT_TXC | */ INTSTAT_TCC | INTSTAT_TQE | INTSTAT_TXU |
1183 /* Acknowledge all pending interrupts */
1184 CSR_WRITE_4(sc, INTSTAT, CSR_READ_4(sc, INTSTAT));
1186 /* Enable interrupts, set for PCI read multiple and etc */
1187 CSR_WRITE_4(sc, GENCTL,
1188 GENCTL_ENABLE_INTERRUPT | GENCTL_MEMORY_READ_MULTIPLE |
1189 GENCTL_ONECOPY | GENCTL_RECEIVE_FIFO_THRESHOLD64);
1191 /* Mark interface running ... */
1192 if (ifp->if_flags & IFF_UP) ifp->if_flags |= IFF_RUNNING;
1193 else ifp->if_flags &= ~IFF_RUNNING;
1196 ifp->if_flags &= ~IFF_OACTIVE;
1198 /* Start Rx process */
1199 epic_start_activity(sc);
1201 /* Set appropriate media */
1202 epic_ifmedia_upd(ifp);
1204 callout_reset(&sc->tx_stat_timer, hz, epic_stats_update, sc);
1212 * Synopsis: calculate and set Rx mode. Chip must be in idle state to
1216 epic_set_rx_mode(sc)
1219 u_int32_t flags = sc->sc_if.if_flags;
1220 u_int32_t rxcon = RXCON_DEFAULT;
1222 #if defined(EPIC_EARLY_RX)
1223 rxcon |= RXCON_EARLY_RX;
1226 rxcon |= (flags & IFF_PROMISC) ? RXCON_PROMISCUOUS_MODE : 0;
1228 CSR_WRITE_4(sc, RXCON, rxcon);
1234 * Synopsis: Set transmit control register. Chip must be in idle state to
1238 epic_set_tx_mode(sc)
1241 if (sc->txcon & TXCON_EARLY_TRANSMIT_ENABLE)
1242 CSR_WRITE_4(sc, ETXTHR, sc->tx_threshold);
1244 CSR_WRITE_4(sc, TXCON, sc->txcon);
1248 * Synopsis: Program multicast filter honoring IFF_ALLMULTI and IFF_PROMISC
1249 * flags. (Note, that setting PROMISC bit in EPIC's RXCON will only touch
1250 * individual frames, multicast filter must be manually programmed)
1252 * Note: EPIC must be in idle state.
1255 epic_set_mc_table(sc)
1258 struct ifnet *ifp = &sc->sc_if;
1259 struct ifmultiaddr *ifma;
1260 u_int16_t filter[4];
1263 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
1264 CSR_WRITE_4(sc, MC0, 0xFFFF);
1265 CSR_WRITE_4(sc, MC1, 0xFFFF);
1266 CSR_WRITE_4(sc, MC2, 0xFFFF);
1267 CSR_WRITE_4(sc, MC3, 0xFFFF);
1277 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1278 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1280 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1282 if (ifma->ifma_addr->sa_family != AF_LINK)
1284 h = epic_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1285 filter[h >> 4] |= 1 << (h & 0xF);
1288 CSR_WRITE_4(sc, MC0, filter[0]);
1289 CSR_WRITE_4(sc, MC1, filter[1]);
1290 CSR_WRITE_4(sc, MC2, filter[2]);
1291 CSR_WRITE_4(sc, MC3, filter[3]);
1297 * Synopsis: calculate EPIC's hash of multicast address.
1303 u_int32_t crc, carry;
1307 /* Compute CRC for the address value. */
1308 crc = 0xFFFFFFFF; /* initial value */
1310 for (i = 0; i < 6; i++) {
1312 for (j = 0; j < 8; j++) {
1313 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
1317 crc = (crc ^ 0x04c11db6) | carry;
1321 return ((crc >> 26) & 0x3F);
1326 * Synopsis: Start receive process and transmit one, if they need.
1329 epic_start_activity(sc)
1332 /* Start rx process */
1333 CSR_WRITE_4(sc, COMMAND,
1334 COMMAND_RXQUEUED | COMMAND_START_RX |
1335 (sc->pending_txs?COMMAND_TXQUEUED:0));
1339 * Synopsis: Completely stop Rx and Tx processes. If TQE is set additional
1340 * packet needs to be queued to stop Tx DMA.
1343 epic_stop_activity(sc)
1348 /* Stop Tx and Rx DMA */
1349 CSR_WRITE_4(sc, COMMAND,
1350 COMMAND_STOP_RX | COMMAND_STOP_RDMA | COMMAND_STOP_TDMA);
1352 /* Wait Rx and Tx DMA to stop (why 1 ms ??? XXX) */
1353 for (i=0; i<0x1000; i++) {
1354 status = CSR_READ_4(sc, INTSTAT) & (INTSTAT_TXIDLE | INTSTAT_RXIDLE);
1355 if (status == (INTSTAT_TXIDLE | INTSTAT_RXIDLE))
1360 /* Catch all finished packets */
1364 status = CSR_READ_4(sc, INTSTAT);
1366 if ((status & INTSTAT_RXIDLE) == 0)
1367 device_printf(sc->dev, "ERROR! Can't stop Rx DMA\n");
1369 if ((status & INTSTAT_TXIDLE) == 0)
1370 device_printf(sc->dev, "ERROR! Can't stop Tx DMA\n");
1373 * May need to queue one more packet if TQE, this is rare
1374 * but existing case.
1376 if ((status & INTSTAT_TQE) && !(status & INTSTAT_TXIDLE))
1377 (void) epic_queue_last_packet(sc);
1382 * The EPIC transmitter may stuck in TQE state. It will not go IDLE until
1383 * a packet from current descriptor will be copied to internal RAM. We
1384 * compose a dummy packet here and queue it for transmission.
1386 * XXX the packet will then be actually sent over network...
1389 epic_queue_last_packet(sc)
1392 struct epic_tx_desc *desc;
1393 struct epic_frag_list *flist;
1394 struct epic_tx_buffer *buf;
1398 device_printf(sc->dev, "queue last packet\n");
1400 desc = sc->tx_desc + sc->cur_tx;
1401 flist = sc->tx_flist + sc->cur_tx;
1402 buf = sc->tx_buffer + sc->cur_tx;
1404 if ((desc->status & 0x8000) || (buf->mbuf != NULL))
1407 MGETHDR(m0, MB_DONTWAIT, MT_DATA);
1412 m0->m_len = min(MHLEN, ETHER_MIN_LEN-ETHER_CRC_LEN);
1413 flist->frag[0].fraglen = m0->m_len;
1414 m0->m_pkthdr.len = m0->m_len;
1415 m0->m_pkthdr.rcvif = &sc->sc_if;
1416 bzero(mtod(m0,caddr_t), m0->m_len);
1418 /* Fill fragments list */
1419 flist->frag[0].fraglen = m0->m_len;
1420 flist->frag[0].fragaddr = vtophys(mtod(m0, caddr_t));
1421 flist->numfrags = 1;
1423 /* Fill in descriptor */
1426 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
1427 desc->control = 0x01;
1428 desc->txlength = max(m0->m_pkthdr.len,ETHER_MIN_LEN-ETHER_CRC_LEN);
1429 desc->status = 0x8000;
1431 /* Launch transmition */
1432 CSR_WRITE_4(sc, COMMAND, COMMAND_STOP_TDMA | COMMAND_TXQUEUED);
1434 /* Wait Tx DMA to stop (for how long??? XXX) */
1435 for (i=0; i<1000; i++) {
1436 if (CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE)
1441 if ((CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE) == 0)
1442 device_printf(sc->dev, "ERROR! can't stop Tx DMA (2)\n");
1450 * Synopsis: Shut down board and deallocates rings.
1460 sc->sc_if.if_timer = 0;
1462 callout_stop(&sc->tx_stat_timer);
1464 /* Disable interrupts */
1465 CSR_WRITE_4(sc, INTMASK, 0);
1466 CSR_WRITE_4(sc, GENCTL, 0);
1468 /* Try to stop Rx and TX processes */
1469 epic_stop_activity(sc);
1472 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1475 /* Make chip go to bed */
1476 CSR_WRITE_4(sc, GENCTL, GENCTL_POWER_DOWN);
1478 /* Free memory allocated for rings */
1479 epic_free_rings(sc);
1481 /* Mark as stoped */
1482 sc->sc_if.if_flags &= ~IFF_RUNNING;
1489 * Synopsis: This function should free all memory allocated for rings.
1497 for (i=0; i<RX_RING_SIZE; i++) {
1498 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1499 struct epic_rx_desc *desc = sc->rx_desc + i;
1502 desc->buflength = 0;
1505 if (buf->mbuf) m_freem(buf->mbuf);
1509 for (i=0; i<TX_RING_SIZE; i++) {
1510 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1511 struct epic_tx_desc *desc = sc->tx_desc + i;
1514 desc->buflength = 0;
1517 if (buf->mbuf) m_freem(buf->mbuf);
1523 * Synopsis: Allocates mbufs for Rx ring and point Rx descs to them.
1524 * Point Tx descs to fragment lists. Check that all descs and fraglists
1525 * are bounded and aligned properly.
1533 sc->cur_rx = sc->cur_tx = sc->dirty_tx = sc->pending_txs = 0;
1535 for (i = 0; i < RX_RING_SIZE; i++) {
1536 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1537 struct epic_rx_desc *desc = sc->rx_desc + i;
1539 desc->status = 0; /* Owned by driver */
1540 desc->next = vtophys(sc->rx_desc + ((i+1) & RX_RING_MASK));
1542 if ((desc->next & 3) ||
1543 ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1544 epic_free_rings(sc);
1548 EPIC_MGETCLUSTER(buf->mbuf);
1549 if (NULL == buf->mbuf) {
1550 epic_free_rings(sc);
1553 desc->bufaddr = vtophys(mtod(buf->mbuf, caddr_t));
1555 desc->buflength = MCLBYTES; /* Max RX buffer length */
1556 desc->status = 0x8000; /* Set owner bit to NIC */
1559 for (i = 0; i < TX_RING_SIZE; i++) {
1560 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1561 struct epic_tx_desc *desc = sc->tx_desc + i;
1564 desc->next = vtophys(sc->tx_desc + ((i+1) & TX_RING_MASK));
1566 if ((desc->next & 3) ||
1567 ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1568 epic_free_rings(sc);
1573 desc->bufaddr = vtophys(sc->tx_flist + i);
1575 if ((desc->bufaddr & 3) ||
1576 ((desc->bufaddr & PAGE_MASK) + sizeof(struct epic_frag_list)) > PAGE_SIZE) {
1577 epic_free_rings(sc);
1586 * EEPROM operation functions
1589 epic_write_eepromreg(sc, val)
1595 CSR_WRITE_1(sc, EECTL, val);
1597 for (i=0; i<0xFF; i++)
1598 if ((CSR_READ_1(sc, EECTL) & 0x20) == 0) break;
1604 epic_read_eepromreg(sc)
1607 return CSR_READ_1(sc, EECTL);
1611 epic_eeprom_clock(sc, val)
1615 epic_write_eepromreg(sc, val);
1616 epic_write_eepromreg(sc, (val | 0x4));
1617 epic_write_eepromreg(sc, val);
1619 return epic_read_eepromreg(sc);
1623 epic_output_eepromw(sc, val)
1629 for (i = 0xF; i >= 0; i--) {
1631 epic_eeprom_clock(sc, 0x0B);
1633 epic_eeprom_clock(sc, 0x03);
1638 epic_input_eepromw(sc)
1641 u_int16_t retval = 0;
1644 for (i = 0xF; i >= 0; i--) {
1645 if (epic_eeprom_clock(sc, 0x3) & 0x10)
1653 epic_read_eeprom(sc, loc)
1660 epic_write_eepromreg(sc, 3);
1662 if (epic_read_eepromreg(sc) & 0x40)
1663 read_cmd = (loc & 0x3F) | 0x180;
1665 read_cmd = (loc & 0xFF) | 0x600;
1667 epic_output_eepromw(sc, read_cmd);
1669 dataval = epic_input_eepromw(sc);
1671 epic_write_eepromreg(sc, 1);
1677 * Here goes MII read/write routines
1680 epic_read_phy_reg(sc, phy, reg)
1686 CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x01));
1688 for (i = 0; i < 0x100; i++) {
1689 if ((CSR_READ_4(sc, MIICTL) & 0x01) == 0) break;
1693 return (CSR_READ_4(sc, MIIDATA));
1697 epic_write_phy_reg(sc, phy, reg, val)
1703 CSR_WRITE_4(sc, MIIDATA, val);
1704 CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x02));
1706 for(i=0;i<0x100;i++) {
1707 if ((CSR_READ_4(sc, MIICTL) & 0x02) == 0) break;
1715 epic_miibus_readreg(dev, phy, reg)
1721 sc = device_get_softc(dev);
1723 return (PHY_READ_2(sc, phy, reg));
1727 epic_miibus_writereg(dev, phy, reg, data)
1733 sc = device_get_softc(dev);
1735 PHY_WRITE_2(sc, phy, reg, data);