1/* 2 * Copyright (c) 1997, 1998 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 15 unchanged lines hidden (view full) --- 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 * |
32 * $FreeBSD: head/sys/pci/if_tl.c 102336 2002-08-24 00:02:03Z alfred $ |
33 */ 34 35/* 36 * Texas Instruments ThunderLAN driver for FreeBSD 2.2.6 and 3.x. 37 * Supports many Compaq PCI NICs based on the ThunderLAN ethernet controller, 38 * the National Semiconductor DP83840A physical interface and the 39 * Microchip Technology 24Cxx series serial EEPROM. 40 * --- 179 unchanged lines hidden (view full) --- 220 221MODULE_DEPEND(tl, miibus, 1, 1, 1); 222 223/* "controller miibus0" required. See GENERIC if you get errors here. */ 224#include "miibus_if.h" 225 226#if !defined(lint) 227static const char rcsid[] = |
228 "$FreeBSD: head/sys/pci/if_tl.c 102336 2002-08-24 00:02:03Z alfred $"; |
229#endif 230 231/* 232 * Various supported device vendors/types and their names. 233 */ 234 235static struct tl_type tl_devs[] = { 236 { TI_VENDORID, TI_DEVICEID_THUNDERLAN, --- 171 unchanged lines hidden (view full) --- 408 int reg; 409 int val; 410{ 411 CSR_WRITE_2(sc, TL_DIO_ADDR, reg); 412 CSR_WRITE_4(sc, TL_DIO_DATA + (reg & 3), val); 413 return; 414} 415 |
416static void 417tl_dio_setbit(sc, reg, bit) |
418 struct tl_softc *sc; 419 int reg; 420 int bit; 421{ 422 u_int8_t f; 423 424 CSR_WRITE_2(sc, TL_DIO_ADDR, reg); 425 f = CSR_READ_1(sc, TL_DIO_DATA + (reg & 3)); 426 f |= bit; 427 CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), f); 428 429 return; 430} 431 |
432static void 433tl_dio_clrbit(sc, reg, bit) |
434 struct tl_softc *sc; 435 int reg; 436 int bit; 437{ 438 u_int8_t f; 439 440 CSR_WRITE_2(sc, TL_DIO_ADDR, reg); 441 f = CSR_READ_1(sc, TL_DIO_DATA + (reg & 3)); --- 143 unchanged lines hidden (view full) --- 585 *dest = byte; 586 587 return(0); 588} 589 590/* 591 * Read a sequence of bytes from the EEPROM. 592 */ |
593static int 594tl_read_eeprom(sc, dest, off, cnt) |
595 struct tl_softc *sc; 596 caddr_t dest; 597 int off; 598 int cnt; 599{ 600 int err = 0, i; 601 u_int8_t byte = 0; 602 603 for (i = 0; i < cnt; i++) { 604 err = tl_eeprom_getbyte(sc, off + i, &byte); 605 if (err) 606 break; 607 *(dest + i) = byte; 608 } 609 610 return(err ? 1 : 0); 611} 612 |
613static void 614tl_mii_sync(sc) |
615 struct tl_softc *sc; 616{ 617 register int i; 618 619 tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN); 620 621 for (i = 0; i < 32; i++) { 622 tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK); 623 tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK); 624 } 625 626 return; 627} 628 |
629static void 630tl_mii_send(sc, bits, cnt) |
631 struct tl_softc *sc; 632 u_int32_t bits; 633 int cnt; 634{ 635 int i; 636 637 for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 638 tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK); 639 if (bits & i) { 640 tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MDATA); 641 } else { 642 tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MDATA); 643 } 644 tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK); 645 } 646} 647 |
648static int 649tl_mii_readreg(sc, frame) |
650 struct tl_softc *sc; 651 struct tl_mii_frame *frame; 652 653{ 654 int i, ack; 655 int minten = 0; 656 657 TL_LOCK(sc); --- 78 unchanged lines hidden (view full) --- 736 737 TL_UNLOCK(sc); 738 739 if (ack) 740 return(1); 741 return(0); 742} 743 |
744static int 745tl_mii_writereg(sc, frame) |
746 struct tl_softc *sc; 747 struct tl_mii_frame *frame; 748 749{ 750 int minten; 751 752 TL_LOCK(sc); 753 --- 39 unchanged lines hidden (view full) --- 793 if (minten) 794 tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MINTEN); 795 796 TL_UNLOCK(sc); 797 798 return(0); 799} 800 |
801static int 802tl_miibus_readreg(dev, phy, reg) |
803 device_t dev; 804 int phy, reg; 805{ 806 struct tl_softc *sc; 807 struct tl_mii_frame frame; 808 809 sc = device_get_softc(dev); 810 bzero((char *)&frame, sizeof(frame)); 811 812 frame.mii_phyaddr = phy; 813 frame.mii_regaddr = reg; 814 tl_mii_readreg(sc, &frame); 815 816 return(frame.mii_data); 817} 818 |
819static int 820tl_miibus_writereg(dev, phy, reg, data) |
821 device_t dev; 822 int phy, reg, data; 823{ 824 struct tl_softc *sc; 825 struct tl_mii_frame frame; 826 827 sc = device_get_softc(dev); 828 bzero((char *)&frame, sizeof(frame)); 829 830 frame.mii_phyaddr = phy; 831 frame.mii_regaddr = reg; 832 frame.mii_data = data; 833 834 tl_mii_writereg(sc, &frame); 835 836 return(0); 837} 838 |
839static void 840tl_miibus_statchg(dev) |
841 device_t dev; 842{ 843 struct tl_softc *sc; 844 struct mii_data *mii; 845 846 sc = device_get_softc(dev); 847 TL_LOCK(sc); 848 mii = device_get_softc(sc->tl_miibus); --- 6 unchanged lines hidden (view full) --- 855 TL_UNLOCK(sc); 856 857 return; 858} 859 860/* 861 * Set modes for bitrate devices. 862 */ |
863static void 864tl_setmode(sc, media) |
865 struct tl_softc *sc; 866 int media; 867{ 868 if (IFM_SUBTYPE(media) == IFM_10_5) 869 tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_MTXD1); 870 if (IFM_SUBTYPE(media) == IFM_10_T) { 871 tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_MTXD1); 872 if ((media & IFM_GMASK) == IFM_FDX) { --- 12 unchanged lines hidden (view full) --- 885 * Calculate the hash of a MAC address for programming the multicast hash 886 * table. This hash is simply the address split into 6-bit chunks 887 * XOR'd, e.g. 888 * byte: 000000|00 1111|1111 22|222222|333333|33 4444|4444 55|555555 889 * bit: 765432|10 7654|3210 76|543210|765432|10 7654|3210 76|543210 890 * Bytes 0-2 and 3-5 are symmetrical, so are folded together. Then 891 * the folded 24-bit value is split into 6-bit portions and XOR'd. 892 */ |
893static int 894tl_calchash(addr) |
895 caddr_t addr; 896{ 897 int t; 898 899 t = (addr[0] ^ addr[3]) << 16 | (addr[1] ^ addr[4]) << 8 | 900 (addr[2] ^ addr[5]); 901 return ((t >> 18) ^ (t >> 12) ^ (t >> 6) ^ t) & 0x3f; 902} 903 904/* 905 * The ThunderLAN has a perfect MAC address filter in addition to 906 * the multicast hash filter. The perfect filter can be programmed 907 * with up to four MAC addresses. The first one is always used to 908 * hold the station address, which leaves us free to use the other 909 * three for multicast addresses. 910 */ |
911static void 912tl_setfilt(sc, addr, slot) |
913 struct tl_softc *sc; 914 caddr_t addr; 915 int slot; 916{ 917 int i; 918 u_int16_t regaddr; 919 920 regaddr = TL_AREG0_B5 + (slot * ETHER_ADDR_LEN); --- 15 unchanged lines hidden (view full) --- 936 * and into the hash table. 937 * 938 * Because the multicast list is a doubly-linked list as opposed to a 939 * circular queue, we don't have the ability to just grab the tail of 940 * the list and traverse it backwards. Instead, we have to traverse 941 * the list once to find the tail, then traverse it again backwards to 942 * update the multicast filter. 943 */ |
944static void 945tl_setmulti(sc) |
946 struct tl_softc *sc; 947{ 948 struct ifnet *ifp; 949 u_int32_t hashes[2] = { 0, 0 }; 950 int h, i; 951 struct ifmultiaddr *ifma; 952 u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 }; 953 ifp = &sc->arpcom.ac_if; --- 41 unchanged lines hidden (view full) --- 995} 996 997/* 998 * This routine is recommended by the ThunderLAN manual to insure that 999 * the internal PHY is powered up correctly. It also recommends a one 1000 * second pause at the end to 'wait for the clocks to start' but in my 1001 * experience this isn't necessary. 1002 */ |
1003static void 1004tl_hardreset(dev) |
1005 device_t dev; 1006{ 1007 struct tl_softc *sc; 1008 int i; 1009 u_int16_t flags; 1010 1011 sc = device_get_softc(dev); 1012 --- 9 unchanged lines hidden (view full) --- 1022 tl_miibus_writereg(dev, 31, MII_BMCR, BMCR_LOOP|BMCR_ISO); 1023 tl_mii_sync(sc); 1024 while(tl_miibus_readreg(dev, 31, MII_BMCR) & BMCR_RESET); 1025 1026 DELAY(50000); 1027 return; 1028} 1029 |
1030static void 1031tl_softreset(sc, internal) |
1032 struct tl_softc *sc; 1033 int internal; 1034{ 1035 u_int32_t cmd, dummy, i; 1036 1037 /* Assert the adapter reset bit. */ 1038 CMD_SET(sc, TL_CMD_ADRST); 1039 --- 45 unchanged lines hidden (view full) --- 1085 1086 return; 1087} 1088 1089/* 1090 * Probe for a ThunderLAN chip. Check the PCI vendor and device IDs 1091 * against our list and return its name if we find a match. 1092 */ |
1093static int 1094tl_probe(dev) |
1095 device_t dev; 1096{ 1097 struct tl_type *t; 1098 1099 t = tl_devs; 1100 1101 while(t->tl_name != NULL) { 1102 if ((pci_get_vendor(dev) == t->tl_vid) && 1103 (pci_get_device(dev) == t->tl_did)) { 1104 device_set_desc(dev, t->tl_name); 1105 return(0); 1106 } 1107 t++; 1108 } 1109 1110 return(ENXIO); 1111} 1112 |
1113static int 1114tl_attach(dev) |
1115 device_t dev; 1116{ 1117 int i; 1118 u_int32_t command; 1119 u_int16_t did, vid; 1120 struct tl_type *t; 1121 struct ifnet *ifp; 1122 struct tl_softc *sc; --- 230 unchanged lines hidden (view full) --- 1353 return(0); 1354 1355fail: 1356 TL_UNLOCK(sc); 1357 mtx_destroy(&sc->tl_mtx); 1358 return(error); 1359} 1360 |
1361static int 1362tl_detach(dev) |
1363 device_t dev; 1364{ 1365 struct tl_softc *sc; 1366 struct ifnet *ifp; 1367 1368 sc = device_get_softc(dev); 1369 TL_LOCK(sc); 1370 ifp = &sc->arpcom.ac_if; --- 16 unchanged lines hidden (view full) --- 1387 mtx_destroy(&sc->tl_mtx); 1388 1389 return(0); 1390} 1391 1392/* 1393 * Initialize the transmit lists. 1394 */ |
1395static int 1396tl_list_tx_init(sc) |
1397 struct tl_softc *sc; 1398{ 1399 struct tl_chain_data *cd; 1400 struct tl_list_data *ld; 1401 int i; 1402 1403 cd = &sc->tl_cdata; 1404 ld = sc->tl_ldata; --- 10 unchanged lines hidden (view full) --- 1415 sc->tl_txeoc = 1; 1416 1417 return(0); 1418} 1419 1420/* 1421 * Initialize the RX lists and allocate mbufs for them. 1422 */ |
1423static int 1424tl_list_rx_init(sc) |
1425 struct tl_softc *sc; 1426{ 1427 struct tl_chain_data *cd; 1428 struct tl_list_data *ld; 1429 int i; 1430 1431 cd = &sc->tl_cdata; 1432 ld = sc->tl_ldata; --- 14 unchanged lines hidden (view full) --- 1447 } 1448 1449 cd->tl_rx_head = &cd->tl_rx_chain[0]; 1450 cd->tl_rx_tail = &cd->tl_rx_chain[TL_RX_LIST_CNT - 1]; 1451 1452 return(0); 1453} 1454 |
1455static int 1456tl_newbuf(sc, c) |
1457 struct tl_softc *sc; 1458 struct tl_chain_onefrag *c; 1459{ 1460 struct mbuf *m_new = NULL; 1461 1462 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1463 if (m_new == NULL) 1464 return(ENOBUFS); --- 36 unchanged lines hidden (view full) --- 1501 * been sent on its way, the 'list' structure is assigned a new buffer 1502 * and moved to the end of the RX chain. As long we we stay ahead of 1503 * the chip, it will always think it has an endless receive channel. 1504 * 1505 * If we happen to fall behind and the chip manages to fill up all of 1506 * the buffers, it will generate an end of channel interrupt and wait 1507 * for us to empty the chain and restart the receiver. 1508 */ |
1509static int 1510tl_intvec_rxeof(xsc, type) |
1511 void *xsc; 1512 u_int32_t type; 1513{ 1514 struct tl_softc *sc; 1515 int r = 0, total_len = 0; 1516 struct ether_header *eh; 1517 struct mbuf *m; 1518 struct ifnet *ifp; --- 52 unchanged lines hidden (view full) --- 1571 1572/* 1573 * The RX-EOC condition hits when the ch_parm address hasn't been 1574 * initialized or the adapter reached a list with a forward pointer 1575 * of 0 (which indicates the end of the chain). In our case, this means 1576 * the card has hit the end of the receive buffer chain and we need to 1577 * empty out the buffers and shift the pointer back to the beginning again. 1578 */ |
1579static int 1580tl_intvec_rxeoc(xsc, type) |
1581 void *xsc; 1582 u_int32_t type; 1583{ 1584 struct tl_softc *sc; 1585 int r; 1586 struct tl_chain_data *cd; 1587 1588 --- 6 unchanged lines hidden (view full) --- 1595 r = 1; 1596 cd->tl_rx_head = &cd->tl_rx_chain[0]; 1597 cd->tl_rx_tail = &cd->tl_rx_chain[TL_RX_LIST_CNT - 1]; 1598 CSR_WRITE_4(sc, TL_CH_PARM, vtophys(sc->tl_cdata.tl_rx_head->tl_ptr)); 1599 r |= (TL_CMD_GO|TL_CMD_RT); 1600 return(r); 1601} 1602 |
1603static int 1604tl_intvec_txeof(xsc, type) |
1605 void *xsc; 1606 u_int32_t type; 1607{ 1608 struct tl_softc *sc; 1609 int r = 0; 1610 struct tl_chain *cur_tx; 1611 1612 sc = xsc; --- 35 unchanged lines hidden (view full) --- 1648 * are queued for transmission. If tl_statrt() is called after TXEOF 1649 * resets the tx_head pointer but _before_ the TXEOC interrupt arrives, 1650 * it could attempt to issue a transmit command prematurely. 1651 * 1652 * To guard against this, tl_start() will only issue transmit commands 1653 * if the tl_txeoc flag is set, and only the TXEOC interrupt handler 1654 * can set this flag once tl_start() has cleared it. 1655 */ |
1656static int 1657tl_intvec_txeoc(xsc, type) |
1658 void *xsc; 1659 u_int32_t type; 1660{ 1661 struct tl_softc *sc; 1662 struct ifnet *ifp; 1663 u_int32_t cmd; 1664 1665 sc = xsc; --- 19 unchanged lines hidden (view full) --- 1685 cmd |= TL_CMD_GO|TL_CMD_INTSON; 1686 CMD_PUT(sc, cmd); 1687 return(0); 1688 } 1689 1690 return(1); 1691} 1692 |
1693static int 1694tl_intvec_adchk(xsc, type) |
1695 void *xsc; 1696 u_int32_t type; 1697{ 1698 struct tl_softc *sc; 1699 1700 sc = xsc; 1701 1702 if (type) 1703 printf("tl%d: adapter check: %x\n", sc->tl_unit, 1704 (unsigned int)CSR_READ_4(sc, TL_CH_PARM)); 1705 1706 tl_softreset(sc, 1); 1707 tl_stop(sc); 1708 tl_init(sc); 1709 CMD_SET(sc, TL_CMD_INTSON); 1710 1711 return(0); 1712} 1713 |
1714static int 1715tl_intvec_netsts(xsc, type) |
1716 void *xsc; 1717 u_int32_t type; 1718{ 1719 struct tl_softc *sc; 1720 u_int16_t netsts; 1721 1722 sc = xsc; 1723 1724 netsts = tl_dio_read16(sc, TL_NETSTS); 1725 tl_dio_write16(sc, TL_NETSTS, netsts); 1726 1727 printf("tl%d: network status: %x\n", sc->tl_unit, netsts); 1728 1729 return(1); 1730} 1731 |
1732static void 1733tl_intr(xsc) |
1734 void *xsc; 1735{ 1736 struct tl_softc *sc; 1737 struct ifnet *ifp; 1738 int r = 0; 1739 u_int32_t type = 0; 1740 u_int16_t ints = 0; 1741 u_int8_t ivec = 0; --- 58 unchanged lines hidden (view full) --- 1800 if (ifp->if_snd.ifq_head != NULL) 1801 tl_start(ifp); 1802 1803 TL_UNLOCK(sc); 1804 1805 return; 1806} 1807 |
1808static void 1809tl_stats_update(xsc) |
1810 void *xsc; 1811{ 1812 struct tl_softc *sc; 1813 struct ifnet *ifp; 1814 struct tl_stats tl_stats; 1815 struct mii_data *mii; 1816 u_int32_t *p; 1817 --- 45 unchanged lines hidden (view full) --- 1863 1864 return; 1865} 1866 1867/* 1868 * Encapsulate an mbuf chain in a list by coupling the mbuf data 1869 * pointers to the fragment pointers. 1870 */ |
1871static int 1872tl_encap(sc, c, m_head) |
1873 struct tl_softc *sc; 1874 struct tl_chain *c; 1875 struct mbuf *m_head; 1876{ 1877 int frag = 0; 1878 struct tl_frag *f = NULL; 1879 int total_len; 1880 struct mbuf *m; --- 80 unchanged lines hidden (view full) --- 1961} 1962 1963/* 1964 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1965 * to the mbuf data regions directly in the transmit lists. We also save a 1966 * copy of the pointers since the transmit list fragment pointers are 1967 * physical addresses. 1968 */ |
1969static void 1970tl_start(ifp) |
1971 struct ifnet *ifp; 1972{ 1973 struct tl_softc *sc; 1974 struct mbuf *m_head = NULL; 1975 u_int32_t cmd; 1976 struct tl_chain *prev = NULL, *cur_tx = NULL, *start_tx; 1977 1978 sc = ifp->if_softc; --- 76 unchanged lines hidden (view full) --- 2055 * Set a timeout in case the chip goes out to lunch. 2056 */ 2057 ifp->if_timer = 5; 2058 TL_UNLOCK(sc); 2059 2060 return; 2061} 2062 |
2063static void 2064tl_init(xsc) |
2065 void *xsc; 2066{ 2067 struct tl_softc *sc = xsc; 2068 struct ifnet *ifp = &sc->arpcom.ac_if; 2069 struct mii_data *mii; 2070 2071 TL_LOCK(sc); 2072 --- 72 unchanged lines hidden (view full) --- 2145 TL_UNLOCK(sc); 2146 2147 return; 2148} 2149 2150/* 2151 * Set media options. 2152 */ |
2153static int 2154tl_ifmedia_upd(ifp) |
2155 struct ifnet *ifp; 2156{ 2157 struct tl_softc *sc; 2158 struct mii_data *mii = NULL; 2159 2160 sc = ifp->if_softc; 2161 2162 if (sc->tl_bitrate) --- 4 unchanged lines hidden (view full) --- 2167 } 2168 2169 return(0); 2170} 2171 2172/* 2173 * Report current media status. 2174 */ |
2175static void 2176tl_ifmedia_sts(ifp, ifmr) |
2177 struct ifnet *ifp; 2178 struct ifmediareq *ifmr; 2179{ 2180 struct tl_softc *sc; 2181 struct mii_data *mii; 2182 2183 sc = ifp->if_softc; 2184 --- 14 unchanged lines hidden (view full) --- 2199 mii_pollstat(mii); 2200 ifmr->ifm_active = mii->mii_media_active; 2201 ifmr->ifm_status = mii->mii_media_status; 2202 } 2203 2204 return; 2205} 2206 |
2207static int 2208tl_ioctl(ifp, command, data) |
2209 struct ifnet *ifp; 2210 u_long command; 2211 caddr_t data; 2212{ 2213 struct tl_softc *sc = ifp->if_softc; 2214 struct ifreq *ifr = (struct ifreq *) data; 2215 int s, error = 0; 2216 --- 48 unchanged lines hidden (view full) --- 2265 break; 2266 } 2267 2268 (void)splx(s); 2269 2270 return(error); 2271} 2272 |
2273static void 2274tl_watchdog(ifp) |
2275 struct ifnet *ifp; 2276{ 2277 struct tl_softc *sc; 2278 2279 sc = ifp->if_softc; 2280 2281 printf("tl%d: device timeout\n", sc->tl_unit); 2282 --- 4 unchanged lines hidden (view full) --- 2287 2288 return; 2289} 2290 2291/* 2292 * Stop the adapter and free any mbufs allocated to the 2293 * RX and TX lists. 2294 */ |
2295static void 2296tl_stop(sc) |
2297 struct tl_softc *sc; 2298{ 2299 register int i; 2300 struct ifnet *ifp; 2301 2302 TL_LOCK(sc); 2303 2304 ifp = &sc->arpcom.ac_if; --- 50 unchanged lines hidden (view full) --- 2355 2356 return; 2357} 2358 2359/* 2360 * Stop all chip I/O so that the kernel's probe routines don't 2361 * get confused by errant DMAs when rebooting. 2362 */ |
2363static void 2364tl_shutdown(dev) |
2365 device_t dev; 2366{ 2367 struct tl_softc *sc; 2368 2369 sc = device_get_softc(dev); 2370 2371 tl_stop(sc); 2372 2373 return; 2374} |