1/* 2 * Copyright (c) 1995, David Greenman 3 * All rights reserved. 4 * 5 * Modifications to support NetBSD and media selection: 6 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 13 unchanged lines hidden (view full) --- 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * |
30 * $Id: if_fxp.c,v 1.40 1997/09/21 22:02:08 gibbs Exp $ |
31 */ 32 33/* 34 * Intel EtherExpress Pro/100B PCI Fast Ethernet driver 35 */ 36 37#include "bpfilter.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/mbuf.h> 42#include <sys/malloc.h> 43#include <sys/kernel.h> 44#include <sys/socket.h> 45#include <sys/syslog.h> 46 47#include <net/if.h> |
48#include <net/if_dl.h> |
49#include <net/if_media.h> 50 51#ifdef INET 52#include <netinet/in.h> 53#endif 54 55#ifdef NS 56#include <netns/ns.h> --- 101 unchanged lines hidden (view full) --- 158 0x0, /* 13 */ 159 0xf2, /* 14 */ 160 0x48, /* 15 */ 161 0x0, /* 16 */ 162 0x40, /* 17 */ 163 0xf3, /* 18 */ 164 0x0, /* 19 */ 165 0x3f, /* 20 */ |
166 0x5 /* 21 */ |
167}; 168 169/* Supported media types. */ 170struct fxp_supported_media { 171 const int fsm_phy; /* PHY type */ 172 const int *fsm_media; /* the media array */ 173 const int fsm_nmedia; /* the number of supported media */ 174 const int fsm_defmedia; /* default media for this PHY */ --- 26 unchanged lines hidden (view full) --- 201 { FXP_PHY_80C24, fxp_media_default, 202 sizeof(fxp_media_default) / sizeof(fxp_media_default[0]), 203 FXP_MEDIA_DEFAULT_DEFMEDIA }, 204}; 205#define NFXPMEDIA (sizeof(fxp_media) / sizeof(fxp_media[0])) 206 207static int fxp_mediachange __P((struct ifnet *)); 208static void fxp_mediastatus __P((struct ifnet *, struct ifmediareq *)); |
209void fxp_set_media __P((struct fxp_softc *, int)); |
210static inline void fxp_scb_wait __P((struct fxp_softc *)); 211static FXP_INTR_TYPE fxp_intr __P((void *)); 212static void fxp_start __P((struct ifnet *)); 213static int fxp_ioctl __P((struct ifnet *, 214 FXP_IOCTLCMD_TYPE, caddr_t)); 215static void fxp_init __P((void *)); 216static void fxp_stop __P((struct fxp_softc *)); 217static void fxp_watchdog __P((struct ifnet *)); 218static int fxp_add_rfabuf __P((struct fxp_softc *, struct mbuf *)); 219static int fxp_mdi_read __P((struct fxp_softc *, int, int)); 220static void fxp_mdi_write __P((struct fxp_softc *, int, int, int)); 221static void fxp_read_eeprom __P((struct fxp_softc *, u_int16_t *, 222 int, int)); 223static int fxp_attach_common __P((struct fxp_softc *, u_int8_t *)); |
224void fxp_stats_update __P((void *)); |
225static void fxp_mc_setup __P((struct fxp_softc *)); |
226 227/* 228 * Set initial transmit threshold at 64 (512 bytes). This is 229 * increased by 64 (512 bytes) at a time, to maximum of 192 230 * (1536 bytes), if an underrun occurs. 231 */ 232static int tx_threshold = 64; 233 --- 5 unchanged lines hidden (view full) --- 239#define FXP_NTXCB 128 240 241/* 242 * TxCB list index mask. This is used to do list wrap-around. 243 */ 244#define FXP_TXCB_MASK (FXP_NTXCB - 1) 245 246/* |
247 * Number of receive frame area buffers. These are large so chose 248 * wisely. 249 */ 250#define FXP_NRFABUFS 32 251 252/* |
253 * Maximum number of seconds that the receiver can be idle before we 254 * assume it's dead and attempt to reset it by reprogramming the 255 * multicast filter. This is part of a work-around for a bug in the 256 * NIC. See fxp_stats_update(). 257 */ 258#define FXP_MAX_RX_IDLE 15 259 260/* |
261 * Wait for the previous command to be accepted (but not necessarily 262 * completed). 263 */ 264static inline void 265fxp_scb_wait(sc) 266 struct fxp_softc *sc; 267{ 268 int i = 10000; 269 |
270 while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i); |
271} 272 273/************************************************************* 274 * Operating system-specific autoconfiguration glue 275 *************************************************************/ 276 277#if defined(__NetBSD__) 278 --- 339 unchanged lines hidden (view full) --- 618 if (sc->cbl_base == NULL) 619 goto fail; 620 621 sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT); 622 if (sc->fxp_stats == NULL) 623 goto fail; 624 bzero(sc->fxp_stats, sizeof(struct fxp_stats)); 625 |
626 sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT); 627 if (sc->mcsp == NULL) 628 goto fail; 629 |
630 /* 631 * Pre-allocate our receive buffers. 632 */ 633 for (i = 0; i < FXP_NRFABUFS; i++) { 634 if (fxp_add_rfabuf(sc, NULL) != 0) { 635 goto fail; 636 } 637 } --- 38 unchanged lines hidden (view full) --- 676 return (0); 677 678 fail: 679 printf(FXP_FORMAT ": Failed to malloc memory\n", FXP_ARGS(sc)); 680 if (sc->cbl_base) 681 free(sc->cbl_base, M_DEVBUF); 682 if (sc->fxp_stats) 683 free(sc->fxp_stats, M_DEVBUF); |
684 if (sc->mcsp) 685 free(sc->mcsp, M_DEVBUF); |
686 /* frees entire chain */ 687 if (sc->rfa_headm) 688 m_freem(sc->rfa_headm); 689 690 return (ENOMEM); 691} 692 693/* --- 76 unchanged lines hidden (view full) --- 770{ 771 struct fxp_softc *sc = ifp->if_softc; 772 struct fxp_cb_tx *txp; 773 struct mbuf *m, *mb_head; 774 int segment, first = 1; 775 776txloop: 777 /* |
778 * See if we're all filled up with buffers to transmit, or 779 * if we need to suspend xmit until the multicast filter 780 * has been reprogrammed (which can only be done at the 781 * head of the command chain). |
782 */ |
783 if (sc->tx_queued >= FXP_NTXCB || sc->need_mcsetup) |
784 return; 785 786 /* 787 * Grab a packet to transmit. 788 */ 789 IF_DEQUEUE(&ifp->if_snd, mb_head); 790 if (mb_head == NULL) { 791 /* --- 131 unchanged lines hidden (view full) --- 923 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack); 924 925 /* 926 * Free any finished transmit mbuf chains. 927 */ 928 if (statack & FXP_SCB_STATACK_CNA) { 929 struct fxp_cb_tx *txp; 930 |
931 for (txp = sc->cbl_first; sc->tx_queued && |
932 (txp->cb_status & FXP_CB_STATUS_C) != 0; 933 txp = txp->next) { 934 if (txp->mb_head != NULL) { 935 m_freem(txp->mb_head); 936 txp->mb_head = NULL; |
937 } |
938 sc->tx_queued--; |
939 } 940 sc->cbl_first = txp; |
941 if (sc->tx_queued == 0) { 942 ifp->if_timer = 0; 943 if (sc->need_mcsetup) 944 fxp_mc_setup(sc); 945 } |
946 /* 947 * Try to start more packets transmitting. 948 */ 949 if (ifp->if_snd.ifq_head != NULL) 950 fxp_start(ifp); 951 } 952 /* 953 * Process receiver interrupts. If a no-resource (RNR) --- 90 unchanged lines hidden (view full) --- 1044 */ 1045void 1046fxp_stats_update(arg) 1047 void *arg; 1048{ 1049 struct fxp_softc *sc = arg; 1050 struct ifnet *ifp = &sc->sc_if; 1051 struct fxp_stats *sp = sc->fxp_stats; |
1052 int s; |
1053 1054 ifp->if_opackets += sp->tx_good; 1055 ifp->if_collisions += sp->tx_total_collisions; 1056 ifp->if_ipackets += sp->rx_good; |
1057 if (sp->rx_good) { 1058 ifp->if_ipackets += sp->rx_good; 1059 sc->rx_idle_secs = 0; 1060 } else { 1061 sc->rx_idle_secs++; 1062 } |
1063 ifp->if_ierrors += 1064 sp->rx_crc_errors + 1065 sp->rx_alignment_errors + 1066 sp->rx_rnr_errors + 1067 sp->rx_overrun_errors; 1068 /* 1069 * If any transmit underruns occured, bump up the transmit 1070 * threshold by another 512 bytes (64 * 8). 1071 */ 1072 if (sp->tx_underruns) { 1073 ifp->if_oerrors += sp->tx_underruns; 1074 if (tx_threshold < 192) 1075 tx_threshold += 64; 1076 } |
1077 s = splimp(); |
1078 /* |
1079 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds, 1080 * then assume the receiver has locked up and attempt to clear 1081 * the condition by reprogramming the multicast filter. This is 1082 * a work-around for a bug in the 82557 where the receiver locks 1083 * up if it gets certain types of garbage in the syncronization 1084 * bits prior to the packet header. This bug is supposed to only 1085 * occur in 10Mbps mode, but has been seen to occur in 100Mbps 1086 * mode as well (perhaps due to a 10/100 speed transition). 1087 */ 1088 if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) { 1089 sc->rx_idle_secs = 0; 1090 fxp_mc_setup(sc); 1091 } 1092 /* |
1093 * If there is no pending command, start another stats 1094 * dump. Otherwise punt for now. 1095 */ |
1096 if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) { |
1097 /* |
1098 * Start another stats dump. |
1099 */ 1100 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, 1101 FXP_SCB_COMMAND_CU_DUMPRESET); |
1102 } else { 1103 /* 1104 * A previous command is still waiting to be accepted. 1105 * Just zero our copy of the stats and wait for the 1106 * next timer event to update them. 1107 */ 1108 sp->tx_good = 0; 1109 sp->tx_underruns = 0; 1110 sp->tx_total_collisions = 0; 1111 1112 sp->rx_good = 0; 1113 sp->rx_crc_errors = 0; 1114 sp->rx_alignment_errors = 0; 1115 sp->rx_rnr_errors = 0; 1116 sp->rx_overrun_errors = 0; 1117 } |
1118 splx(s); |
1119 /* 1120 * Schedule another timeout one second from now. 1121 */ |
1122 sc->stat_ch = timeout(fxp_stats_update, sc, hz); |
1123} 1124 1125/* 1126 * Stop the interface. Cancels the statistics updater and resets 1127 * the interface. 1128 */ 1129static void 1130fxp_stop(sc) --- 53 unchanged lines hidden (view full) --- 1184 * card has wedged for some reason. 1185 */ 1186static void 1187fxp_watchdog(ifp) 1188 struct ifnet *ifp; 1189{ 1190 struct fxp_softc *sc = ifp->if_softc; 1191 |
1192 printf(FXP_FORMAT ": device timeout\n", FXP_ARGS(sc)); |
1193 ifp->if_oerrors++; 1194 1195 fxp_init(sc); 1196} 1197 1198static void 1199fxp_init(xsc) 1200 void *xsc; 1201{ 1202 struct fxp_softc *sc = xsc; 1203 struct ifnet *ifp = &sc->sc_if; 1204 struct fxp_cb_config *cbp; 1205 struct fxp_cb_ias *cb_ias; 1206 struct fxp_cb_tx *txp; |
1207 int i, s, prm; |
1208 1209 s = splimp(); 1210 /* 1211 * Cancel any pending I/O 1212 */ 1213 fxp_stop(sc); 1214 1215 prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0; 1216 sc->promisc_mode = prm; |
1217 1218 /* 1219 * Initialize base of CBL and RFA memory. Loading with zero 1220 * sets it up for regular linear addressing. 1221 */ 1222 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0); 1223 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE); 1224 --- 14 unchanged lines hidden (view full) --- 1239 */ 1240 cbp = (struct fxp_cb_config *) sc->cbl_base; 1241 1242 /* 1243 * This bcopy is kind of disgusting, but there are a bunch of must be 1244 * zero and must be one bits in this structure and this is the easiest 1245 * way to initialize them all to proper values. 1246 */ |
1247 bcopy(fxp_cb_config_template, (void *)&cbp->cb_status, 1248 sizeof(fxp_cb_config_template)); |
1249 1250 cbp->cb_status = 0; 1251 cbp->cb_command = FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL; 1252 cbp->link_addr = -1; /* (no) next command */ 1253 cbp->byte_count = 22; /* (22) bytes to config */ 1254 cbp->rx_fifo_limit = 8; /* rx fifo threshold (32 bytes) */ 1255 cbp->tx_fifo_limit = 0; /* tx fifo threshold (0 bytes) */ 1256 cbp->adaptive_ifs = 0; /* (no) adaptive interframe spacing */ --- 17 unchanged lines hidden (view full) --- 1274 cbp->bcast_disable = 0; /* (don't) disable broadcasts */ 1275 cbp->crscdt = 0; /* (CRS only) */ 1276 cbp->stripping = !prm; /* truncate rx packet to byte count */ 1277 cbp->padding = 1; /* (do) pad short tx packets */ 1278 cbp->rcv_crc_xfer = 0; /* (don't) xfer CRC to host */ 1279 cbp->force_fdx = 0; /* (don't) force full duplex */ 1280 cbp->fdx_pin_en = 1; /* (enable) FDX# pin */ 1281 cbp->multi_ia = 0; /* (don't) accept multiple IAs */ |
1282 cbp->mc_all = sc->all_mcasts;/* accept all multicasts */ |
1283 1284 /* 1285 * Start the config command/DMA. 1286 */ 1287 fxp_scb_wait(sc); |
1288 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status)); |
1289 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START); 1290 /* ...and wait for it to complete. */ 1291 while (!(cbp->cb_status & FXP_CB_STATUS_C)); 1292 1293 /* 1294 * Now initialize the station address. Temporarily use the TxCB 1295 * memory area like we did above for the config CB. 1296 */ --- 20 unchanged lines hidden (view full) --- 1317 * Initialize transmit control block (TxCB) list. 1318 */ 1319 1320 txp = sc->cbl_base; 1321 bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB); 1322 for (i = 0; i < FXP_NTXCB; i++) { 1323 txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK; 1324 txp[i].cb_command = FXP_CB_COMMAND_NOP; |
1325 txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status); |
1326 txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]); 1327 txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK]; 1328 } 1329 /* |
1330 * Set the suspend flag on the first TxCB and start the control |
1331 * unit. It will execute the NOP and then suspend. 1332 */ 1333 txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S; 1334 sc->cbl_first = sc->cbl_last = txp; |
1335 sc->tx_queued = 1; |
1336 1337 fxp_scb_wait(sc); 1338 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START); 1339 1340 /* 1341 * Initialize receiver buffer area - RFA. 1342 */ 1343 fxp_scb_wait(sc); --- 262 unchanged lines hidden (view full) --- 1606#if !defined(__NetBSD__) 1607 case SIOCGIFADDR: 1608 case SIOCSIFMTU: 1609#endif 1610 error = ether_ioctl(ifp, command, data); 1611 break; 1612 1613 case SIOCSIFFLAGS: |
1614 sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0; |
1615 1616 /* 1617 * If interface is marked up and not running, then start it. 1618 * If it is marked down and running, stop it. 1619 * XXX If it's up then re-initialize it. This is so flags 1620 * such as IFF_PROMISC are handled. 1621 */ 1622 if (ifp->if_flags & IFF_UP) { 1623 fxp_init(sc); 1624 } else { 1625 if (ifp->if_flags & IFF_RUNNING) 1626 fxp_stop(sc); 1627 } 1628 break; 1629 1630 case SIOCADDMULTI: 1631 case SIOCDELMULTI: |
1632 sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0; |
1633#if defined(__NetBSD__) 1634 { 1635 struct ifreq *ifr = (struct ifreq *) data; 1636 1637 error = (command == SIOCADDMULTI) ? 1638 ether_addmulti(ifr, &sc->sc_ethercom) : 1639 ether_delmulti(ifr, &sc->sc_ethercom); 1640 1641 if (error == ENETRESET) { 1642 /* 1643 * Multicast list has changed; set the hardware 1644 * filter accordingly. 1645 */ |
1646 if (!sc->all_mcasts) 1647 fxp_mc_setup(sc); 1648 /* 1649 * fxp_mc_setup() can turn on all_mcasts if we run 1650 * out of space, so check it again rather than else {}. 1651 */ 1652 if (sc->all_mcasts) 1653 fxp_init(sc); |
1654 error = 0; 1655 } 1656 } 1657#else /* __FreeBSD__ */ 1658 /* 1659 * Multicast list has changed; set the hardware filter 1660 * accordingly. 1661 */ |
1662 if (!sc->all_mcasts) 1663 fxp_mc_setup(sc); 1664 /* 1665 * fxp_mc_setup() can turn on sc->all_mcasts, so check it 1666 * again rather than else {}. 1667 */ 1668 if (sc->all_mcasts) 1669 fxp_init(sc); |
1670 error = 0; 1671#endif /* __NetBSD__ */ 1672 break; 1673 1674 case SIOCSIFMEDIA: 1675 case SIOCGIFMEDIA: 1676 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command); 1677 break; 1678 1679 default: 1680 error = EINVAL; 1681 } 1682 (void) splx(s); 1683 return (error); 1684} |
1685 1686/* 1687 * Program the multicast filter. 1688 * 1689 * We have an artificial restriction that the multicast setup command 1690 * must be the first command in the chain, so we take steps to ensure 1691 * that. By requiring this, it allows us to keep the performance of 1692 * the pre-initialized command ring (esp. link pointers) by not actually 1693 * inserting the mcsetup command in the ring - i.e. it's link pointer 1694 * points to the TxCB ring, but the mcsetup descriptor itself is not part 1695 * of it. We then can do 'CU_START' on the mcsetup descriptor and have it 1696 * lead into the regular TxCB ring when it completes. 1697 * 1698 * This function must be called at splimp. 1699 */ 1700static void 1701fxp_mc_setup(sc) 1702 struct fxp_softc *sc; 1703{ 1704 struct fxp_cb_mcs *mcsp = sc->mcsp; 1705 struct ifnet *ifp = &sc->sc_if; 1706 struct ifmultiaddr *ifma; 1707 int nmcasts; 1708 1709 if (sc->tx_queued) { 1710 sc->need_mcsetup = 1; 1711 return; 1712 } 1713 sc->need_mcsetup = 0; 1714 1715 /* 1716 * Initialize multicast setup descriptor. 1717 */ 1718 mcsp->next = sc->cbl_base; 1719 mcsp->mb_head = NULL; 1720 mcsp->cb_status = 0; 1721 mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_S; 1722 mcsp->link_addr = vtophys(&sc->cbl_base->cb_status); 1723 1724 nmcasts = 0; 1725 if (!sc->all_mcasts) { 1726 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; 1727 ifma = ifma->ifma_link.le_next) { 1728 if (ifma->ifma_addr->sa_family != AF_LINK) 1729 continue; 1730 if (nmcasts >= MAXMCADDR) { 1731 sc->all_mcasts = 1; 1732 nmcasts = 0; 1733 break; 1734 } 1735 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 1736 (void *) &sc->mcsp->mc_addr[nmcasts][0], 6); 1737 nmcasts++; 1738 } 1739 } 1740 mcsp->mc_cnt = nmcasts * 6; 1741 sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp; 1742 sc->tx_queued = 1; 1743 1744 /* 1745 * Wait until command unit is not active. This should never 1746 * be the case when nothing is queued, but make sure anyway. 1747 */ 1748 while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) == 1749 FXP_SCB_CUS_ACTIVE) ; 1750 1751 /* 1752 * Start the multicast setup command. 1753 */ 1754 fxp_scb_wait(sc); 1755 CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status)); 1756 CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START); 1757 1758 ifp->if_timer = 5; 1759 return; 1760} |