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.33 2005/11/28 17:13:44 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>
50 #include <sys/serialize.h>
51 #include <sys/thread2.h>
54 #include <net/ifq_var.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
62 #include <net/vlan/if_vlan_var.h>
64 #include <vm/vm.h> /* for vtophys */
65 #include <vm/pmap.h> /* for vtophys */
66 #include <machine/bus_memio.h>
67 #include <machine/bus_pio.h>
68 #include <machine/bus.h>
69 #include <machine/resource.h>
73 #include <bus/pci/pcireg.h>
74 #include <bus/pci/pcivar.h>
75 #include <bus/pci/pcidevs.h>
77 #include <dev/netif/mii_layer/mii.h>
78 #include <dev/netif/mii_layer/miivar.h>
79 #include <dev/netif/mii_layer/miidevs.h>
80 #include <dev/netif/mii_layer/lxtphyreg.h>
82 #include "miibus_if.h"
84 #include <dev/netif/tx/if_txreg.h>
85 #include <dev/netif/tx/if_txvar.h>
87 static int epic_ifioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
88 static void epic_intr(void *);
89 static void epic_tx_underrun(epic_softc_t *);
90 static int epic_common_attach(epic_softc_t *);
91 static void epic_ifstart(struct ifnet *);
92 static void epic_ifwatchdog(struct ifnet *);
93 static void epic_stats_update(void *);
94 static int epic_init(epic_softc_t *);
95 static void epic_stop(epic_softc_t *);
96 static void epic_rx_done(epic_softc_t *);
97 static void epic_tx_done(epic_softc_t *);
98 static int epic_init_rings(epic_softc_t *);
99 static void epic_free_rings(epic_softc_t *);
100 static void epic_stop_activity(epic_softc_t *);
101 static int epic_queue_last_packet(epic_softc_t *);
102 static void epic_start_activity(epic_softc_t *);
103 static void epic_set_rx_mode(epic_softc_t *);
104 static void epic_set_tx_mode(epic_softc_t *);
105 static void epic_set_mc_table(epic_softc_t *);
106 static int epic_read_eeprom(epic_softc_t *,u_int16_t);
107 static void epic_output_eepromw(epic_softc_t *, u_int16_t);
108 static u_int16_t epic_input_eepromw(epic_softc_t *);
109 static u_int8_t epic_eeprom_clock(epic_softc_t *,u_int8_t);
110 static void epic_write_eepromreg(epic_softc_t *,u_int8_t);
111 static u_int8_t epic_read_eepromreg(epic_softc_t *);
113 static int epic_read_phy_reg(epic_softc_t *, int, int);
114 static void epic_write_phy_reg(epic_softc_t *, int, int, int);
116 static int epic_miibus_readreg(device_t, int, int);
117 static int epic_miibus_writereg(device_t, int, int, int);
118 static void epic_miibus_statchg(device_t);
119 static void epic_miibus_mediainit(device_t);
121 static int epic_ifmedia_upd(struct ifnet *);
122 static void epic_ifmedia_sts(struct ifnet *, struct ifmediareq *);
124 static int epic_probe(device_t);
125 static int epic_attach(device_t);
126 static void epic_shutdown(device_t);
127 static int epic_detach(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 { PCI_VENDOR_SMC, PCI_PRODUCT_SMC_83C170,
160 "SMC EtherPower II 10/100" },
165 epic_probe(device_t dev)
170 vid = pci_get_vendor(dev);
171 did = pci_get_device(dev);
172 for (t = epic_devs; t->name != NULL; ++t) {
173 if (vid == t->ven_id && did == t->dev_id) {
174 device_set_desc(dev, t->name);
181 #if defined(EPIC_USEIOSPACE)
182 #define EPIC_RES SYS_RES_IOPORT
183 #define EPIC_RID PCIR_BAR(0)
185 #define EPIC_RES SYS_RES_MEMORY
186 #define EPIC_RID PCIR_BAR(1)
190 * Attach routine: map registers, allocate softc, rings and descriptors.
191 * Reset to known state.
194 epic_attach(device_t dev)
201 sc = device_get_softc(dev);
203 /* Preinitialize softc structure */
205 callout_init(&sc->tx_stat_timer);
207 /* Fill ifnet structure */
209 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
211 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
212 ifp->if_ioctl = epic_ifioctl;
213 ifp->if_start = epic_ifstart;
214 ifp->if_watchdog = epic_ifwatchdog;
215 ifp->if_init = (if_init_f_t*)epic_init;
217 ifp->if_baudrate = 10000000;
218 ifq_set_maxlen(&ifp->if_snd, TX_RING_SIZE - 1);
219 ifq_set_ready(&ifp->if_snd);
221 pci_enable_busmaster(dev);
224 sc->res = bus_alloc_resource_any(dev, EPIC_RES, &rid, RF_ACTIVE);
226 if (sc->res == NULL) {
227 device_printf(dev, "couldn't map ports/memory\n");
232 sc->sc_st = rman_get_bustag(sc->res);
233 sc->sc_sh = rman_get_bushandle(sc->res);
235 /* Allocate interrupt */
237 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
238 RF_SHAREABLE | RF_ACTIVE);
240 if (sc->irq == NULL) {
241 device_printf(dev, "couldn't map interrupt\n");
246 /* Do OS independent part, including chip wakeup and reset */
247 error = epic_common_attach(sc);
253 /* Do ifmedia setup */
254 if (mii_phy_probe(dev, &sc->miibus,
255 epic_ifmedia_upd, epic_ifmedia_sts)) {
256 device_printf(dev, "ERROR! MII without any PHY!?\n");
261 /* board type and ... */
263 for(i=0x2c;i<0x32;i++) {
264 tmp = epic_read_eeprom(sc, i);
265 if (' ' == (u_int8_t)tmp) break;
266 printf("%c", (u_int8_t)tmp);
268 if (' ' == (u_int8_t)tmp) break;
269 printf("%c", (u_int8_t)tmp);
273 /* Attach to OS's managers */
274 ether_ifattach(ifp, sc->sc_macaddr, NULL);
275 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
277 error = bus_setup_intr(dev, sc->irq, INTR_NETSAFE,
278 epic_intr, sc, &sc->sc_ih,
282 device_printf(dev, "couldn't set up irq\n");
295 * Detach driver and free resources
298 epic_detach(device_t dev)
303 sc = device_get_softc(dev);
304 ifp = &sc->arpcom.ac_if;
306 lwkt_serialize_enter(ifp->if_serializer);
308 if (device_is_attached(dev)) {
314 device_delete_child(dev, sc->miibus);
315 bus_generic_detach(dev);
318 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
321 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
323 bus_release_resource(dev, EPIC_RES, EPIC_RID, sc->res);
326 free(sc->tx_flist, M_DEVBUF);
328 free(sc->tx_desc, M_DEVBUF);
330 free(sc->rx_desc, M_DEVBUF);
332 lwkt_serialize_exit(ifp->if_serializer);
340 * Stop all chip I/O so that the kernel's probe routines don't
341 * get confused by errant DMAs when rebooting.
344 epic_shutdown(device_t dev)
349 sc = device_get_softc(dev);
350 ifp = &sc->arpcom.ac_if;
351 lwkt_serialize_enter(ifp->if_serializer);
353 lwkt_serialize_exit(ifp->if_serializer);
357 * This is if_ioctl handler.
360 epic_ifioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
362 epic_softc_t *sc = ifp->if_softc;
363 struct mii_data *mii;
364 struct ifreq *ifr = (struct ifreq *) data;
369 if (ifp->if_mtu == ifr->ifr_mtu)
372 /* XXX Though the datasheet doesn't imply any
373 * limitations on RX and TX sizes beside max 64Kb
374 * DMA transfer, seems we can't send more then 1600
375 * data bytes per ethernet packet. (Transmitter hangs
376 * up if more data is sent)
378 if (ifr->ifr_mtu + ifp->if_hdrlen <= EPIC_MAX_MTU) {
379 ifp->if_mtu = ifr->ifr_mtu;
388 * If the interface is marked up and stopped, then start it.
389 * If it is marked down and running, then stop it.
391 if (ifp->if_flags & IFF_UP) {
392 if ((ifp->if_flags & IFF_RUNNING) == 0) {
397 if (ifp->if_flags & IFF_RUNNING) {
403 /* Handle IFF_PROMISC and IFF_ALLMULTI flags */
404 epic_stop_activity(sc);
405 epic_set_mc_table(sc);
406 epic_set_rx_mode(sc);
407 epic_start_activity(sc);
412 epic_set_mc_table(sc);
418 mii = device_get_softc(sc->miibus);
419 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
423 error = ether_ioctl(ifp, command, data);
430 * OS-independed part of attach process. allocate memory for descriptors
431 * and frag lists, wake up chip, read MAC address and PHY identyfier.
432 * Return -1 on failure.
435 epic_common_attach(epic_softc_t *sc)
440 sc->tx_flist = malloc(sizeof(struct epic_frag_list)*TX_RING_SIZE,
441 M_DEVBUF, M_WAITOK | M_ZERO);
442 sc->tx_desc = malloc(sizeof(struct epic_tx_desc)*TX_RING_SIZE,
443 M_DEVBUF, M_WAITOK | M_ZERO);
444 sc->rx_desc = malloc(sizeof(struct epic_rx_desc)*RX_RING_SIZE,
445 M_DEVBUF, M_WAITOK | M_ZERO);
447 /* Bring the chip out of low-power mode. */
448 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
451 /* Workaround for Application Note 7-15 */
452 for (i=0; i<16; i++) CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
454 /* Read mac address from EEPROM */
455 for (i = 0; i < ETHER_ADDR_LEN / sizeof(u_int16_t); i++)
456 ((u_int16_t *)sc->sc_macaddr)[i] = epic_read_eeprom(sc,i);
458 /* Set Non-Volatile Control Register from EEPROM */
459 CSR_WRITE_4(sc, NVCTL, epic_read_eeprom(sc, EEPROM_NVCTL) & 0x1F);
462 sc->tx_threshold = TRANSMIT_THRESHOLD;
463 sc->txcon = TXCON_DEFAULT;
464 sc->miicfg = MIICFG_SMI_ENABLE;
465 sc->phyid = EPIC_UNKN_PHY;
469 sub_vid = pci_get_subvendor(sc->dev);
470 sc->cardid = pci_get_subdevice(sc->dev);
472 if (sub_vid != PCI_VENDOR_SMC)
473 device_printf(sc->dev, "unknown card vendor %04xh\n", sub_vid);
479 * This is if_start handler. It takes mbufs from if_snd queue
480 * and queue them for transmit, one by one, until TX ring become full
481 * or queue become empty.
484 epic_ifstart(struct ifnet *ifp)
486 epic_softc_t *sc = ifp->if_softc;
487 struct epic_tx_buffer *buf;
488 struct epic_tx_desc *desc;
489 struct epic_frag_list *flist;
494 while (sc->pending_txs < TX_RING_SIZE) {
495 buf = sc->tx_buffer + sc->cur_tx;
496 desc = sc->tx_desc + sc->cur_tx;
497 flist = sc->tx_flist + sc->cur_tx;
499 /* Get next packet to send */
500 m0 = ifq_dequeue(&ifp->if_snd, NULL);
502 /* If nothing to send, return */
506 /* Fill fragments list */
508 (NULL != m) && (i < EPIC_MAX_FRAGS);
509 m = m->m_next, i++) {
510 flist->frag[i].fraglen = m->m_len;
511 flist->frag[i].fragaddr = vtophys(mtod(m, caddr_t));
515 /* If packet was more than EPIC_MAX_FRAGS parts, */
516 /* recopy packet to new allocated mbuf cluster */
518 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
525 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
526 flist->frag[0].fraglen =
527 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
528 m->m_pkthdr.rcvif = ifp;
531 flist->frag[0].fragaddr = vtophys(mtod(m, caddr_t));
538 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
539 desc->control = 0x01;
541 max(m0->m_pkthdr.len,ETHER_MIN_LEN-ETHER_CRC_LEN);
542 desc->status = 0x8000;
543 CSR_WRITE_4(sc, COMMAND, COMMAND_TXQUEUED);
545 /* Set watchdog timer */
551 ifp->if_flags |= IFF_OACTIVE;
558 * Synopsis: Finish all received frames.
561 epic_rx_done(epic_softc_t *sc)
564 struct ifnet *ifp = &sc->sc_if;
565 struct epic_rx_buffer *buf;
566 struct epic_rx_desc *desc;
569 while ((sc->rx_desc[sc->cur_rx].status & 0x8000) == 0) {
570 buf = sc->rx_buffer + sc->cur_rx;
571 desc = sc->rx_desc + sc->cur_rx;
573 /* Switch to next descriptor */
574 sc->cur_rx = (sc->cur_rx+1) & RX_RING_MASK;
577 * Check for RX errors. This should only happen if
578 * SAVE_ERRORED_PACKETS is set. RX errors generate
579 * RXE interrupt usually.
581 if ((desc->status & 1) == 0) {
582 sc->sc_if.if_ierrors++;
583 desc->status = 0x8000;
587 /* Save packet length and mbuf contained packet */
588 len = desc->rxlength - ETHER_CRC_LEN;
591 /* Try to get mbuf cluster */
592 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
593 if (NULL == buf->mbuf) {
595 desc->status = 0x8000;
600 /* Point to new mbuf, and give descriptor to chip */
601 desc->bufaddr = vtophys(mtod(buf->mbuf, caddr_t));
602 desc->status = 0x8000;
604 /* First mbuf in packet holds the ethernet and packet headers */
605 m->m_pkthdr.rcvif = ifp;
606 m->m_pkthdr.len = m->m_len = len;
608 /* Give mbuf to OS */
609 ifp->if_input(ifp, m);
611 /* Successfuly received frame */
619 * Synopsis: Do last phase of transmission. I.e. if desc is
620 * transmitted, decrease pending_txs counter, free mbuf contained
621 * packet, switch to next descriptor and repeat until no packets
622 * are pending or descriptor is not transmitted yet.
625 epic_tx_done(epic_softc_t *sc)
627 struct epic_tx_buffer *buf;
628 struct epic_tx_desc *desc;
631 while (sc->pending_txs > 0) {
632 buf = sc->tx_buffer + sc->dirty_tx;
633 desc = sc->tx_desc + sc->dirty_tx;
634 status = desc->status;
636 /* If packet is not transmitted, thou followed */
637 /* packets are not transmitted too */
638 if (status & 0x8000) break;
640 /* Packet is transmitted. Switch to next and */
643 sc->dirty_tx = (sc->dirty_tx + 1) & TX_RING_MASK;
647 /* Check for errors and collisions */
648 if (status & 0x0001) sc->sc_if.if_opackets++;
649 else sc->sc_if.if_oerrors++;
650 sc->sc_if.if_collisions += (status >> 8) & 0x1F;
651 #if defined(EPIC_DIAG)
652 if ((status & 0x1001) == 0x1001) {
653 if_printf(&sc->sc_if,
654 "Tx ERROR: excessive coll. number\n");
659 if (sc->pending_txs < TX_RING_SIZE)
660 sc->sc_if.if_flags &= ~IFF_OACTIVE;
669 epic_softc_t * sc = (epic_softc_t *) arg;
672 while (i-- && ((status = CSR_READ_4(sc, INTSTAT)) & INTSTAT_INT_ACTV)) {
673 CSR_WRITE_4(sc, INTSTAT, status);
675 if (status & (INTSTAT_RQE|INTSTAT_RCC|INTSTAT_OVW)) {
677 if (status & (INTSTAT_RQE|INTSTAT_OVW)) {
678 #if defined(EPIC_DIAG)
679 if (status & INTSTAT_OVW)
680 if_printf(&sc->sc_if, "RX buffer overflow\n");
681 if (status & INTSTAT_RQE)
682 if_printf(&sc->sc_if, "RX FIFO overflow\n");
684 if ((CSR_READ_4(sc, COMMAND) & COMMAND_RXQUEUED) == 0)
685 CSR_WRITE_4(sc, COMMAND, COMMAND_RXQUEUED);
686 sc->sc_if.if_ierrors++;
690 if (status & (INTSTAT_TXC|INTSTAT_TCC|INTSTAT_TQE)) {
692 if (!ifq_is_empty(&sc->sc_if.if_snd))
693 epic_ifstart(&sc->sc_if);
696 /* Check for rare errors */
697 if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
698 INTSTAT_APE|INTSTAT_DPE|INTSTAT_TXU|INTSTAT_RXE)) {
699 if (status & (INTSTAT_FATAL|INTSTAT_PMA|INTSTAT_PTA|
700 INTSTAT_APE|INTSTAT_DPE)) {
701 if_printf(&sc->sc_if, "PCI fatal errors occured: %s%s%s%s\n",
702 (status&INTSTAT_PMA)?"PMA ":"",
703 (status&INTSTAT_PTA)?"PTA ":"",
704 (status&INTSTAT_APE)?"APE ":"",
705 (status&INTSTAT_DPE)?"DPE":""
714 if (status & INTSTAT_RXE) {
715 #if defined(EPIC_DIAG)
716 if_printf(sc->sc_if, "CRC/Alignment error\n");
718 sc->sc_if.if_ierrors++;
721 if (status & INTSTAT_TXU) {
722 epic_tx_underrun(sc);
723 sc->sc_if.if_oerrors++;
728 /* If no packets are pending, then no timeouts */
729 if (sc->pending_txs == 0) sc->sc_if.if_timer = 0;
735 * Handle the TX underrun error: increase the TX threshold
736 * and restart the transmitter.
739 epic_tx_underrun(epic_softc_t *sc)
741 if (sc->tx_threshold > TRANSMIT_THRESHOLD_MAX) {
742 sc->txcon &= ~TXCON_EARLY_TRANSMIT_ENABLE;
743 #if defined(EPIC_DIAG)
744 if_printf(&sc->sc_if, "Tx UNDERRUN: early TX disabled\n");
747 sc->tx_threshold += 0x40;
748 #if defined(EPIC_DIAG)
749 if_printf(&sc->sc_if, "Tx UNDERRUN: "
750 "TX threshold increased to %d\n", sc->tx_threshold);
754 /* We must set TXUGO to reset the stuck transmitter */
755 CSR_WRITE_4(sc, COMMAND, COMMAND_TXUGO);
757 /* Update the TX threshold */
758 epic_stop_activity(sc);
759 epic_set_tx_mode(sc);
760 epic_start_activity(sc);
766 * Synopsis: This one is called if packets wasn't transmitted
767 * during timeout. Try to deallocate transmitted packets, and
768 * if success continue to work.
771 epic_ifwatchdog(struct ifnet *ifp)
773 epic_softc_t *sc = ifp->if_softc;
775 if_printf(ifp, "device timeout %d packets\n", sc->pending_txs);
777 /* Try to finish queued packets */
780 /* If not successful */
781 if (sc->pending_txs > 0) {
783 ifp->if_oerrors+=sc->pending_txs;
785 /* Reinitialize board */
786 if_printf(ifp, "reinitialization\n");
791 if_printf(ifp, "seems we can continue normaly\n");
794 if (!ifq_is_empty(&ifp->if_snd))
799 * Despite the name of this function, it doesn't update statistics, it only
800 * helps in autonegotiation process.
803 epic_stats_update(void *xsc)
805 epic_softc_t *sc = xsc;
806 struct ifnet *ifp = &sc->sc_if;
807 struct mii_data * mii;
809 lwkt_serialize_enter(ifp->if_serializer);
811 mii = device_get_softc(sc->miibus);
814 callout_reset(&sc->tx_stat_timer, hz, epic_stats_update, sc);
816 lwkt_serialize_exit(ifp->if_serializer);
823 epic_ifmedia_upd(struct ifnet *ifp)
826 struct mii_data *mii;
828 struct mii_softc *miisc;
832 mii = device_get_softc(sc->miibus);
833 ifm = &mii->mii_media;
834 media = ifm->ifm_cur->ifm_media;
836 /* Do not do anything if interface is not up */
837 if ((ifp->if_flags & IFF_UP) == 0)
841 * Lookup current selected PHY
843 if (IFM_INST(media) == sc->serinst) {
844 sc->phyid = EPIC_SERIAL;
847 /* If we're not selecting serial interface, select MII mode */
848 sc->miicfg &= ~MIICFG_SERIAL_ENABLE;
849 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
851 /* Default to unknown PHY */
852 sc->phyid = EPIC_UNKN_PHY;
854 /* Lookup selected PHY */
855 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
856 miisc = LIST_NEXT(miisc, mii_list)) {
857 if (IFM_INST(media) == miisc->mii_inst) {
863 /* Identify selected PHY */
865 int id1, id2, model, oui;
867 id1 = PHY_READ(sc->physc, MII_PHYIDR1);
868 id2 = PHY_READ(sc->physc, MII_PHYIDR2);
870 oui = MII_OUI(id1, id2);
871 model = MII_MODEL(id2);
873 case MII_OUI_QUALSEMI:
874 if (model == MII_MODEL_QUALSEMI_QS6612)
875 sc->phyid = EPIC_QS6612_PHY;
877 case MII_OUI_xxALTIMA:
878 if (model == MII_MODEL_xxALTIMA_AC101)
879 sc->phyid = EPIC_AC101_PHY;
881 case MII_OUI_xxLEVEL1:
882 if (model == MII_MODEL_xxLEVEL1_LXT970)
883 sc->phyid = EPIC_LXT970_PHY;
890 * Do PHY specific card setup
893 /* Call this, to isolate all not selected PHYs and
898 /* Do our own setup */
900 case EPIC_QS6612_PHY:
903 /* We have to powerup fiber tranceivers */
904 if (IFM_SUBTYPE(media) == IFM_100_FX)
905 sc->miicfg |= MIICFG_694_ENABLE;
907 sc->miicfg &= ~MIICFG_694_ENABLE;
908 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
911 case EPIC_LXT970_PHY:
912 /* We have to powerup fiber tranceivers */
913 cfg = PHY_READ(sc->physc, MII_LXTPHY_CONFIG);
914 if (IFM_SUBTYPE(media) == IFM_100_FX)
915 cfg |= CONFIG_LEDC1 | CONFIG_LEDC0;
917 cfg &= ~(CONFIG_LEDC1 | CONFIG_LEDC0);
918 PHY_WRITE(sc->physc, MII_LXTPHY_CONFIG, cfg);
922 /* Select serial PHY, (10base2/BNC usually) */
923 sc->miicfg |= MIICFG_694_ENABLE | MIICFG_SERIAL_ENABLE;
924 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
926 /* There is no driver to fill this */
927 mii->mii_media_active = media;
928 mii->mii_media_status = 0;
930 /* We need to call this manualy as i wasn't called
933 epic_miibus_statchg(sc->dev);
937 if_printf(ifp, "ERROR! Unknown PHY selected\n");
945 * Report current media status.
948 epic_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
951 struct mii_data *mii;
955 mii = device_get_softc(sc->miibus);
956 ifm = &mii->mii_media;
958 /* Nothing should be selected if interface is down */
959 if ((ifp->if_flags & IFF_UP) == 0) {
960 ifmr->ifm_active = IFM_NONE;
961 ifmr->ifm_status = 0;
966 /* Call underlying pollstat, if not serial PHY */
967 if (sc->phyid != EPIC_SERIAL)
970 /* Simply copy media info */
971 ifmr->ifm_active = mii->mii_media_active;
972 ifmr->ifm_status = mii->mii_media_status;
978 * Callback routine, called on media change.
981 epic_miibus_statchg(device_t dev)
984 struct mii_data *mii;
987 sc = device_get_softc(dev);
988 mii = device_get_softc(sc->miibus);
989 media = mii->mii_media_active;
991 sc->txcon &= ~(TXCON_LOOPBACK_MODE | TXCON_FULL_DUPLEX);
993 /* If we are in full-duplex mode or loopback operation,
994 * we need to decouple receiver and transmitter.
996 if (IFM_OPTIONS(media) & (IFM_FDX | IFM_LOOP))
997 sc->txcon |= TXCON_FULL_DUPLEX;
999 /* On some cards we need manualy set fullduplex led */
1000 if (sc->cardid == SMC9432FTX ||
1001 sc->cardid == SMC9432FTX_SC) {
1002 if (IFM_OPTIONS(media) & IFM_FDX)
1003 sc->miicfg |= MIICFG_694_ENABLE;
1005 sc->miicfg &= ~MIICFG_694_ENABLE;
1007 CSR_WRITE_4(sc, MIICFG, sc->miicfg);
1010 /* Update baudrate */
1011 if (IFM_SUBTYPE(media) == IFM_100_TX ||
1012 IFM_SUBTYPE(media) == IFM_100_FX)
1013 sc->sc_if.if_baudrate = 100000000;
1015 sc->sc_if.if_baudrate = 10000000;
1017 epic_stop_activity(sc);
1018 epic_set_tx_mode(sc);
1019 epic_start_activity(sc);
1025 epic_miibus_mediainit(device_t dev)
1028 struct mii_data *mii;
1029 struct ifmedia *ifm;
1032 sc = device_get_softc(dev);
1033 mii = device_get_softc(sc->miibus);
1034 ifm = &mii->mii_media;
1036 /* Add Serial Media Interface if present, this applies to
1039 if (CSR_READ_4(sc, MIICFG) & MIICFG_PHY_PRESENT) {
1040 /* Store its instance */
1041 sc->serinst = mii->mii_instance++;
1043 /* Add as 10base2/BNC media */
1044 media = IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->serinst);
1045 ifmedia_add(ifm, media, 0, NULL);
1047 /* Report to user */
1048 if_printf(&sc->sc_if, "serial PHY detected (10Base2/BNC)\n");
1055 * Reset chip, allocate rings, and update media.
1058 epic_init(epic_softc_t *sc)
1060 struct ifnet *ifp = &sc->sc_if;
1063 /* If interface is already running, then we need not do anything */
1064 if (ifp->if_flags & IFF_RUNNING) {
1068 /* Soft reset the chip (we have to power up card before) */
1069 CSR_WRITE_4(sc, GENCTL, 0);
1070 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1073 * Reset takes 15 pci ticks which depends on PCI bus speed.
1074 * Assuming it >= 33000000 hz, we have wait at least 495e-6 sec.
1079 CSR_WRITE_4(sc, GENCTL, 0);
1081 /* Workaround for Application Note 7-15 */
1082 for (i=0; i<16; i++) CSR_WRITE_4(sc, TEST1, TEST1_CLOCK_TEST);
1084 /* Initialize rings */
1085 if (epic_init_rings(sc)) {
1086 if_printf(ifp, "failed to init rings\n");
1090 /* Give rings to EPIC */
1091 CSR_WRITE_4(sc, PRCDAR, vtophys(sc->rx_desc));
1092 CSR_WRITE_4(sc, PTCDAR, vtophys(sc->tx_desc));
1094 /* Put node address to EPIC */
1095 CSR_WRITE_4(sc, LAN0, ((u_int16_t *)sc->sc_macaddr)[0]);
1096 CSR_WRITE_4(sc, LAN1, ((u_int16_t *)sc->sc_macaddr)[1]);
1097 CSR_WRITE_4(sc, LAN2, ((u_int16_t *)sc->sc_macaddr)[2]);
1099 /* Set tx mode, includeing transmit threshold */
1100 epic_set_tx_mode(sc);
1102 /* Compute and set RXCON. */
1103 epic_set_rx_mode(sc);
1105 /* Set multicast table */
1106 epic_set_mc_table(sc);
1108 /* Enable interrupts by setting the interrupt mask. */
1109 CSR_WRITE_4(sc, INTMASK,
1110 INTSTAT_RCC | /* INTSTAT_RQE | INTSTAT_OVW | INTSTAT_RXE | */
1111 /* INTSTAT_TXC | */ INTSTAT_TCC | INTSTAT_TQE | INTSTAT_TXU |
1114 /* Acknowledge all pending interrupts */
1115 CSR_WRITE_4(sc, INTSTAT, CSR_READ_4(sc, INTSTAT));
1117 /* Enable interrupts, set for PCI read multiple and etc */
1118 CSR_WRITE_4(sc, GENCTL,
1119 GENCTL_ENABLE_INTERRUPT | GENCTL_MEMORY_READ_MULTIPLE |
1120 GENCTL_ONECOPY | GENCTL_RECEIVE_FIFO_THRESHOLD64);
1122 /* Mark interface running ... */
1123 if (ifp->if_flags & IFF_UP) ifp->if_flags |= IFF_RUNNING;
1124 else ifp->if_flags &= ~IFF_RUNNING;
1127 ifp->if_flags &= ~IFF_OACTIVE;
1129 /* Start Rx process */
1130 epic_start_activity(sc);
1132 /* Set appropriate media */
1133 epic_ifmedia_upd(ifp);
1135 callout_reset(&sc->tx_stat_timer, hz, epic_stats_update, sc);
1141 * Synopsis: calculate and set Rx mode. Chip must be in idle state to
1145 epic_set_rx_mode(epic_softc_t *sc)
1147 u_int32_t flags = sc->sc_if.if_flags;
1148 u_int32_t rxcon = RXCON_DEFAULT;
1150 #if defined(EPIC_EARLY_RX)
1151 rxcon |= RXCON_EARLY_RX;
1154 rxcon |= (flags & IFF_PROMISC) ? RXCON_PROMISCUOUS_MODE : 0;
1156 CSR_WRITE_4(sc, RXCON, rxcon);
1162 * Synopsis: Set transmit control register. Chip must be in idle state to
1166 epic_set_tx_mode(epic_softc_t *sc)
1168 if (sc->txcon & TXCON_EARLY_TRANSMIT_ENABLE)
1169 CSR_WRITE_4(sc, ETXTHR, sc->tx_threshold);
1171 CSR_WRITE_4(sc, TXCON, sc->txcon);
1175 * Synopsis: Program multicast filter honoring IFF_ALLMULTI and IFF_PROMISC
1176 * flags. (Note, that setting PROMISC bit in EPIC's RXCON will only touch
1177 * individual frames, multicast filter must be manually programmed)
1179 * Note: EPIC must be in idle state.
1182 epic_set_mc_table(epic_softc_t *sc)
1184 struct ifnet *ifp = &sc->sc_if;
1185 struct ifmultiaddr *ifma;
1186 u_int16_t filter[4];
1189 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
1190 CSR_WRITE_4(sc, MC0, 0xFFFF);
1191 CSR_WRITE_4(sc, MC1, 0xFFFF);
1192 CSR_WRITE_4(sc, MC2, 0xFFFF);
1193 CSR_WRITE_4(sc, MC3, 0xFFFF);
1203 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1204 if (ifma->ifma_addr->sa_family != AF_LINK)
1206 h = (ether_crc32_be(
1207 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1208 ETHER_ADDR_LEN) >> 26) & 0x3f;
1209 filter[h >> 4] |= 1 << (h & 0xF);
1212 CSR_WRITE_4(sc, MC0, filter[0]);
1213 CSR_WRITE_4(sc, MC1, filter[1]);
1214 CSR_WRITE_4(sc, MC2, filter[2]);
1215 CSR_WRITE_4(sc, MC3, filter[3]);
1221 * Synopsis: Start receive process and transmit one, if they need.
1224 epic_start_activity(epic_softc_t *sc)
1226 /* Start rx process */
1227 CSR_WRITE_4(sc, COMMAND,
1228 COMMAND_RXQUEUED | COMMAND_START_RX |
1229 (sc->pending_txs?COMMAND_TXQUEUED:0));
1233 * Synopsis: Completely stop Rx and Tx processes. If TQE is set additional
1234 * packet needs to be queued to stop Tx DMA.
1237 epic_stop_activity(epic_softc_t *sc)
1241 /* Stop Tx and Rx DMA */
1242 CSR_WRITE_4(sc, COMMAND,
1243 COMMAND_STOP_RX | COMMAND_STOP_RDMA | COMMAND_STOP_TDMA);
1245 /* Wait Rx and Tx DMA to stop (why 1 ms ??? XXX) */
1246 for (i=0; i<0x1000; i++) {
1247 status = CSR_READ_4(sc, INTSTAT) & (INTSTAT_TXIDLE | INTSTAT_RXIDLE);
1248 if (status == (INTSTAT_TXIDLE | INTSTAT_RXIDLE))
1253 /* Catch all finished packets */
1257 status = CSR_READ_4(sc, INTSTAT);
1259 if ((status & INTSTAT_RXIDLE) == 0)
1260 if_printf(&sc->sc_if, "ERROR! Can't stop Rx DMA\n");
1262 if ((status & INTSTAT_TXIDLE) == 0)
1263 if_printf(&sc->sc_if, "ERROR! Can't stop Tx DMA\n");
1266 * May need to queue one more packet if TQE, this is rare
1267 * but existing case.
1269 if ((status & INTSTAT_TQE) && !(status & INTSTAT_TXIDLE))
1270 (void) epic_queue_last_packet(sc);
1275 * The EPIC transmitter may stuck in TQE state. It will not go IDLE until
1276 * a packet from current descriptor will be copied to internal RAM. We
1277 * compose a dummy packet here and queue it for transmission.
1279 * XXX the packet will then be actually sent over network...
1282 epic_queue_last_packet(epic_softc_t *sc)
1284 struct epic_tx_desc *desc;
1285 struct epic_frag_list *flist;
1286 struct epic_tx_buffer *buf;
1290 if_printf(&sc->sc_if, "queue last packet\n");
1292 desc = sc->tx_desc + sc->cur_tx;
1293 flist = sc->tx_flist + sc->cur_tx;
1294 buf = sc->tx_buffer + sc->cur_tx;
1296 if ((desc->status & 0x8000) || (buf->mbuf != NULL))
1299 MGETHDR(m0, MB_DONTWAIT, MT_DATA);
1304 m0->m_len = min(MHLEN, ETHER_MIN_LEN-ETHER_CRC_LEN);
1305 flist->frag[0].fraglen = m0->m_len;
1306 m0->m_pkthdr.len = m0->m_len;
1307 m0->m_pkthdr.rcvif = &sc->sc_if;
1308 bzero(mtod(m0,caddr_t), m0->m_len);
1310 /* Fill fragments list */
1311 flist->frag[0].fraglen = m0->m_len;
1312 flist->frag[0].fragaddr = vtophys(mtod(m0, caddr_t));
1313 flist->numfrags = 1;
1315 /* Fill in descriptor */
1318 sc->cur_tx = (sc->cur_tx + 1) & TX_RING_MASK;
1319 desc->control = 0x01;
1320 desc->txlength = max(m0->m_pkthdr.len,ETHER_MIN_LEN-ETHER_CRC_LEN);
1321 desc->status = 0x8000;
1323 /* Launch transmition */
1324 CSR_WRITE_4(sc, COMMAND, COMMAND_STOP_TDMA | COMMAND_TXQUEUED);
1326 /* Wait Tx DMA to stop (for how long??? XXX) */
1327 for (i=0; i<1000; i++) {
1328 if (CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE)
1333 if ((CSR_READ_4(sc, INTSTAT) & INTSTAT_TXIDLE) == 0)
1334 if_printf(&sc->sc_if, "ERROR! can't stop Tx DMA (2)\n");
1342 * Synopsis: Shut down board and deallocates rings.
1345 epic_stop(epic_softc_t *sc)
1347 sc->sc_if.if_timer = 0;
1349 callout_stop(&sc->tx_stat_timer);
1351 /* Disable interrupts */
1352 CSR_WRITE_4(sc, INTMASK, 0);
1353 CSR_WRITE_4(sc, GENCTL, 0);
1355 /* Try to stop Rx and TX processes */
1356 epic_stop_activity(sc);
1359 CSR_WRITE_4(sc, GENCTL, GENCTL_SOFT_RESET);
1362 /* Make chip go to bed */
1363 CSR_WRITE_4(sc, GENCTL, GENCTL_POWER_DOWN);
1365 /* Free memory allocated for rings */
1366 epic_free_rings(sc);
1368 /* Mark as stoped */
1369 sc->sc_if.if_flags &= ~IFF_RUNNING;
1373 * Synopsis: This function should free all memory allocated for rings.
1376 epic_free_rings(epic_softc_t *sc)
1380 for (i=0; i<RX_RING_SIZE; i++) {
1381 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1382 struct epic_rx_desc *desc = sc->rx_desc + i;
1385 desc->buflength = 0;
1388 if (buf->mbuf) m_freem(buf->mbuf);
1392 for (i=0; i<TX_RING_SIZE; i++) {
1393 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1394 struct epic_tx_desc *desc = sc->tx_desc + i;
1397 desc->buflength = 0;
1400 if (buf->mbuf) m_freem(buf->mbuf);
1406 * Synopsis: Allocates mbufs for Rx ring and point Rx descs to them.
1407 * Point Tx descs to fragment lists. Check that all descs and fraglists
1408 * are bounded and aligned properly.
1411 epic_init_rings(epic_softc_t *sc)
1415 sc->cur_rx = sc->cur_tx = sc->dirty_tx = sc->pending_txs = 0;
1417 for (i = 0; i < RX_RING_SIZE; i++) {
1418 struct epic_rx_buffer *buf = sc->rx_buffer + i;
1419 struct epic_rx_desc *desc = sc->rx_desc + i;
1421 desc->status = 0; /* Owned by driver */
1422 desc->next = vtophys(sc->rx_desc + ((i+1) & RX_RING_MASK));
1424 if ((desc->next & 3) ||
1425 ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1426 epic_free_rings(sc);
1430 buf->mbuf = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1431 if (NULL == buf->mbuf) {
1432 epic_free_rings(sc);
1435 desc->bufaddr = vtophys(mtod(buf->mbuf, caddr_t));
1437 desc->buflength = MCLBYTES; /* Max RX buffer length */
1438 desc->status = 0x8000; /* Set owner bit to NIC */
1441 for (i = 0; i < TX_RING_SIZE; i++) {
1442 struct epic_tx_buffer *buf = sc->tx_buffer + i;
1443 struct epic_tx_desc *desc = sc->tx_desc + i;
1446 desc->next = vtophys(sc->tx_desc + ((i+1) & TX_RING_MASK));
1448 if ((desc->next & 3) ||
1449 ((desc->next & PAGE_MASK) + sizeof *desc) > PAGE_SIZE) {
1450 epic_free_rings(sc);
1455 desc->bufaddr = vtophys(sc->tx_flist + i);
1457 if ((desc->bufaddr & 3) ||
1458 ((desc->bufaddr & PAGE_MASK) + sizeof(struct epic_frag_list)) > PAGE_SIZE) {
1459 epic_free_rings(sc);
1468 * EEPROM operation functions
1471 epic_write_eepromreg(epic_softc_t *sc, u_int8_t val)
1475 CSR_WRITE_1(sc, EECTL, val);
1477 for (i=0; i<0xFF; i++)
1478 if ((CSR_READ_1(sc, EECTL) & 0x20) == 0) break;
1484 epic_read_eepromreg(epic_softc_t *sc)
1486 return CSR_READ_1(sc, EECTL);
1490 epic_eeprom_clock(epic_softc_t *sc, u_int8_t val)
1492 epic_write_eepromreg(sc, val);
1493 epic_write_eepromreg(sc, (val | 0x4));
1494 epic_write_eepromreg(sc, val);
1496 return epic_read_eepromreg(sc);
1500 epic_output_eepromw(epic_softc_t *sc, u_int16_t val)
1504 for (i = 0xF; i >= 0; i--) {
1506 epic_eeprom_clock(sc, 0x0B);
1508 epic_eeprom_clock(sc, 0x03);
1513 epic_input_eepromw(epic_softc_t *sc)
1515 u_int16_t retval = 0;
1518 for (i = 0xF; i >= 0; i--) {
1519 if (epic_eeprom_clock(sc, 0x3) & 0x10)
1527 epic_read_eeprom(epic_softc_t *sc, u_int16_t loc)
1532 epic_write_eepromreg(sc, 3);
1534 if (epic_read_eepromreg(sc) & 0x40)
1535 read_cmd = (loc & 0x3F) | 0x180;
1537 read_cmd = (loc & 0xFF) | 0x600;
1539 epic_output_eepromw(sc, read_cmd);
1541 dataval = epic_input_eepromw(sc);
1543 epic_write_eepromreg(sc, 1);
1549 * Here goes MII read/write routines
1552 epic_read_phy_reg(epic_softc_t *sc, int phy, int reg)
1556 CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x01));
1558 for (i = 0; i < 0x100; i++) {
1559 if ((CSR_READ_4(sc, MIICTL) & 0x01) == 0) break;
1563 return (CSR_READ_4(sc, MIIDATA));
1567 epic_write_phy_reg(epic_softc_t *sc, int phy, int reg, int val)
1571 CSR_WRITE_4(sc, MIIDATA, val);
1572 CSR_WRITE_4(sc, MIICTL, ((reg << 4) | (phy << 9) | 0x02));
1574 for(i=0;i<0x100;i++) {
1575 if ((CSR_READ_4(sc, MIICTL) & 0x02) == 0) break;
1583 epic_miibus_readreg(device_t dev, int phy, int reg)
1587 sc = device_get_softc(dev);
1589 return (PHY_READ_2(sc, phy, reg));
1593 epic_miibus_writereg(device_t dev, int phy, int reg, int data)
1597 sc = device_get_softc(dev);
1599 PHY_WRITE_2(sc, phy, reg, data);