linux-bk/drivers/net/e1000/e1000_hw.c
<<
>>
Prefs
   1/*******************************************************************************
   2
   3  
   4  Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
   5  
   6  This program is free software; you can redistribute it and/or modify it 
   7  under the terms of the GNU General Public License as published by the Free 
   8  Software Foundation; either version 2 of the License, or (at your option) 
   9  any later version.
  10  
  11  This program is distributed in the hope that it will be useful, but WITHOUT 
  12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
  14  more details.
  15  
  16  You should have received a copy of the GNU General Public License along with
  17  this program; if not, write to the Free Software Foundation, Inc., 59 
  18  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19  
  20  The full GNU General Public License is included in this distribution in the
  21  file called LICENSE.
  22  
  23  Contact Information:
  24  Linux NICS <linux.nics@intel.com>
  25  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  26
  27*******************************************************************************/
  28
  29/* e1000_hw.c
  30 * Shared functions for accessing and configuring the MAC
  31 */
  32
  33#include "e1000_hw.h"
  34
  35static int32_t e1000_setup_fiber_link(struct e1000_hw *hw);
  36static int32_t e1000_setup_copper_link(struct e1000_hw *hw);
  37static int32_t e1000_phy_force_speed_duplex(struct e1000_hw *hw);
  38static int32_t e1000_config_mac_to_phy(struct e1000_hw *hw);
  39static int32_t e1000_force_mac_fc(struct e1000_hw *hw);
  40static void e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
  41static void e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
  42static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count);
  43static uint16_t e1000_shift_in_mdi_bits(struct e1000_hw *hw);
  44static int32_t e1000_phy_reset_dsp(struct e1000_hw *hw);
  45static void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
  46static void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
  47static void e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count);
  48static uint16_t e1000_shift_in_ee_bits(struct e1000_hw *hw);
  49static void e1000_setup_eeprom(struct e1000_hw *hw);
  50static void e1000_standby_eeprom(struct e1000_hw *hw);
  51static void e1000_clock_eeprom(struct e1000_hw *hw);
  52static void e1000_cleanup_eeprom(struct e1000_hw *hw);
  53static int32_t e1000_id_led_init(struct e1000_hw * hw);
  54
  55/******************************************************************************
  56 * Set the mac type member in the hw struct.
  57 * 
  58 * hw - Struct containing variables accessed by shared code
  59 *****************************************************************************/
  60int32_t
  61e1000_set_mac_type(struct e1000_hw *hw)
  62{
  63    DEBUGFUNC("e1000_set_mac_type");
  64
  65    switch (hw->device_id) {
  66    case E1000_DEV_ID_82542:
  67        switch (hw->revision_id) {
  68        case E1000_82542_2_0_REV_ID:
  69            hw->mac_type = e1000_82542_rev2_0;
  70            break;
  71        case E1000_82542_2_1_REV_ID:
  72            hw->mac_type = e1000_82542_rev2_1;
  73            break;
  74        default:
  75            /* Invalid 82542 revision ID */
  76            return -E1000_ERR_MAC_TYPE;
  77        }
  78        break;
  79    case E1000_DEV_ID_82543GC_FIBER:
  80    case E1000_DEV_ID_82543GC_COPPER:
  81        hw->mac_type = e1000_82543;
  82        break;
  83    case E1000_DEV_ID_82544EI_COPPER:
  84    case E1000_DEV_ID_82544EI_FIBER:
  85    case E1000_DEV_ID_82544GC_COPPER:
  86    case E1000_DEV_ID_82544GC_LOM:
  87        hw->mac_type = e1000_82544;
  88        break;
  89    case E1000_DEV_ID_82540EM:
  90    case E1000_DEV_ID_82540EM_LOM:
  91        hw->mac_type = e1000_82540;
  92        break;
  93    case E1000_DEV_ID_82545EM_COPPER:
  94    case E1000_DEV_ID_82545EM_FIBER:
  95        hw->mac_type = e1000_82545;
  96        break;
  97    case E1000_DEV_ID_82546EB_COPPER:
  98    case E1000_DEV_ID_82546EB_FIBER:
  99        hw->mac_type = e1000_82546;
 100        break;
 101    default:
 102        /* Should never have loaded on this device */
 103        return -E1000_ERR_MAC_TYPE;
 104    }
 105    return E1000_SUCCESS;
 106}
 107/******************************************************************************
 108 * Reset the transmit and receive units; mask and clear all interrupts.
 109 *
 110 * hw - Struct containing variables accessed by shared code
 111 *****************************************************************************/
 112void
 113e1000_reset_hw(struct e1000_hw *hw)
 114{
 115    uint32_t ctrl;
 116    uint32_t ctrl_ext;
 117    uint32_t icr;
 118    uint32_t manc;
 119
 120    DEBUGFUNC("e1000_reset_hw");
 121    
 122    /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
 123    if(hw->mac_type == e1000_82542_rev2_0) {
 124        DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
 125        e1000_pci_clear_mwi(hw);
 126    }
 127
 128    /* Clear interrupt mask to stop board from generating interrupts */
 129    DEBUGOUT("Masking off all interrupts\n");
 130    E1000_WRITE_REG(hw, IMC, 0xffffffff);
 131
 132    /* Disable the Transmit and Receive units.  Then delay to allow
 133     * any pending transactions to complete before we hit the MAC with
 134     * the global reset.
 135     */
 136    E1000_WRITE_REG(hw, RCTL, 0);
 137    E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
 138    E1000_WRITE_FLUSH(hw);
 139
 140    /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
 141    hw->tbi_compatibility_on = FALSE;
 142
 143    /* Delay to allow any outstanding PCI transactions to complete before
 144     * resetting the device
 145     */ 
 146    msec_delay(10);
 147
 148    /* Issue a global reset to the MAC.  This will reset the chip's
 149     * transmit, receive, DMA, and link units.  It will not effect
 150     * the current PCI configuration.  The global reset bit is self-
 151     * clearing, and should clear within a microsecond.
 152     */
 153    DEBUGOUT("Issuing a global reset to MAC\n");
 154    ctrl = E1000_READ_REG(hw, CTRL);
 155
 156    if(hw->mac_type > e1000_82543)
 157        E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
 158    else
 159        E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
 160
 161    /* Force a reload from the EEPROM if necessary */
 162    if(hw->mac_type < e1000_82540) {
 163        /* Wait for reset to complete */
 164        udelay(10);
 165        ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
 166        ctrl_ext |= E1000_CTRL_EXT_EE_RST;
 167        E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
 168        E1000_WRITE_FLUSH(hw);
 169        /* Wait for EEPROM reload */
 170        msec_delay(2);
 171    } else {
 172        /* Wait for EEPROM reload (it happens automatically) */
 173        msec_delay(4);
 174        /* Dissable HW ARPs on ASF enabled adapters */
 175        manc = E1000_READ_REG(hw, MANC);
 176        manc &= ~(E1000_MANC_ARP_EN);
 177        E1000_WRITE_REG(hw, MANC, manc);
 178    }
 179    
 180    /* Clear interrupt mask to stop board from generating interrupts */
 181    DEBUGOUT("Masking off all interrupts\n");
 182    E1000_WRITE_REG(hw, IMC, 0xffffffff);
 183
 184    /* Clear any pending interrupt events. */
 185    icr = E1000_READ_REG(hw, ICR);
 186
 187    /* If MWI was previously enabled, reenable it. */
 188    if(hw->mac_type == e1000_82542_rev2_0) {
 189        if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
 190            e1000_pci_set_mwi(hw);
 191    }
 192}
 193
 194/******************************************************************************
 195 * Performs basic configuration of the adapter.
 196 *
 197 * hw - Struct containing variables accessed by shared code
 198 * 
 199 * Assumes that the controller has previously been reset and is in a 
 200 * post-reset uninitialized state. Initializes the receive address registers,
 201 * multicast table, and VLAN filter table. Calls routines to setup link
 202 * configuration and flow control settings. Clears all on-chip counters. Leaves
 203 * the transmit and receive units disabled and uninitialized.
 204 *****************************************************************************/
 205int32_t
 206e1000_init_hw(struct e1000_hw *hw)
 207{
 208    uint32_t ctrl, status;
 209    uint32_t i;
 210    int32_t ret_val;
 211    uint16_t pcix_cmd_word;
 212    uint16_t pcix_stat_hi_word;
 213    uint16_t cmd_mmrbc;
 214    uint16_t stat_mmrbc;
 215
 216    DEBUGFUNC("e1000_init_hw");
 217
 218    /* Initialize Identification LED */
 219    ret_val = e1000_id_led_init(hw);
 220    if(ret_val < 0) {
 221        DEBUGOUT("Error Initializing Identification LED\n");
 222        return ret_val;
 223    }
 224    
 225    /* Set the Media Type and exit with error if it is not valid. */
 226    if(hw->mac_type != e1000_82543) {
 227        /* tbi_compatibility is only valid on 82543 */
 228        hw->tbi_compatibility_en = FALSE;
 229    }
 230
 231    if(hw->mac_type >= e1000_82543) {
 232        status = E1000_READ_REG(hw, STATUS);
 233        if(status & E1000_STATUS_TBIMODE) {
 234            hw->media_type = e1000_media_type_fiber;
 235            /* tbi_compatibility not valid on fiber */
 236            hw->tbi_compatibility_en = FALSE;
 237        } else {
 238            hw->media_type = e1000_media_type_copper;
 239        }
 240    } else {
 241        /* This is an 82542 (fiber only) */
 242        hw->media_type = e1000_media_type_fiber;
 243    }
 244
 245    /* Disabling VLAN filtering. */
 246    DEBUGOUT("Initializing the IEEE VLAN\n");
 247    E1000_WRITE_REG(hw, VET, 0);
 248
 249    e1000_clear_vfta(hw);
 250
 251    /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
 252    if(hw->mac_type == e1000_82542_rev2_0) {
 253        DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
 254        e1000_pci_clear_mwi(hw);
 255        E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
 256        E1000_WRITE_FLUSH(hw);
 257        msec_delay(5);
 258    }
 259
 260    /* Setup the receive address. This involves initializing all of the Receive
 261     * Address Registers (RARs 0 - 15).
 262     */
 263    e1000_init_rx_addrs(hw);
 264
 265    /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
 266    if(hw->mac_type == e1000_82542_rev2_0) {
 267        E1000_WRITE_REG(hw, RCTL, 0);
 268        E1000_WRITE_FLUSH(hw);
 269        msec_delay(1);
 270        if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
 271            e1000_pci_set_mwi(hw);
 272    }
 273
 274    /* Zero out the Multicast HASH table */
 275    DEBUGOUT("Zeroing the MTA\n");
 276    for(i = 0; i < E1000_MC_TBL_SIZE; i++)
 277        E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
 278
 279    /* Set the PCI priority bit correctly in the CTRL register.  This
 280     * determines if the adapter gives priority to receives, or if it
 281     * gives equal priority to transmits and receives.
 282     */
 283    if(hw->dma_fairness) {
 284        ctrl = E1000_READ_REG(hw, CTRL);
 285        E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
 286    }
 287
 288    /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
 289    if(hw->bus_type == e1000_bus_type_pcix) {
 290        e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
 291        e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI, &pcix_stat_hi_word);
 292        cmd_mmrbc = (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
 293            PCIX_COMMAND_MMRBC_SHIFT;
 294        stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
 295            PCIX_STATUS_HI_MMRBC_SHIFT;
 296        if(stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
 297            stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
 298        if(cmd_mmrbc > stat_mmrbc) {
 299            pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
 300            pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
 301            e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
 302        }
 303    }
 304
 305    /* Call a subroutine to configure the link and setup flow control. */
 306    ret_val = e1000_setup_link(hw);
 307
 308    /* Set the transmit descriptor write-back policy */
 309    if(hw->mac_type > e1000_82544) {
 310        ctrl = E1000_READ_REG(hw, TXDCTL);
 311        ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
 312        E1000_WRITE_REG(hw, TXDCTL, ctrl);
 313    }
 314
 315    /* Clear all of the statistics registers (clear on read).  It is
 316     * important that we do this after we have tried to establish link
 317     * because the symbol error count will increment wildly if there
 318     * is no link.
 319     */
 320    e1000_clear_hw_cntrs(hw);
 321
 322    return ret_val;
 323}
 324
 325/******************************************************************************
 326 * Configures flow control and link settings.
 327 * 
 328 * hw - Struct containing variables accessed by shared code
 329 * 
 330 * Determines which flow control settings to use. Calls the apropriate media-
 331 * specific link configuration function. Configures the flow control settings.
 332 * Assuming the adapter has a valid link partner, a valid link should be
 333 * established. Assumes the hardware has previously been reset and the 
 334 * transmitter and receiver are not enabled.
 335 *****************************************************************************/
 336int32_t
 337e1000_setup_link(struct e1000_hw *hw)
 338{
 339    uint32_t ctrl_ext;
 340    int32_t ret_val;
 341    uint16_t eeprom_data;
 342
 343    DEBUGFUNC("e1000_setup_link");
 344
 345    /* Read and store word 0x0F of the EEPROM. This word contains bits
 346     * that determine the hardware's default PAUSE (flow control) mode,
 347     * a bit that determines whether the HW defaults to enabling or
 348     * disabling auto-negotiation, and the direction of the
 349     * SW defined pins. If there is no SW over-ride of the flow
 350     * control setting, then the variable hw->fc will
 351     * be initialized based on a value in the EEPROM.
 352     */
 353    if(e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, &eeprom_data) < 0) {
 354        DEBUGOUT("EEPROM Read Error\n");
 355        return -E1000_ERR_EEPROM;
 356    }
 357
 358    if(hw->fc == e1000_fc_default) {
 359        if((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
 360            hw->fc = e1000_fc_none;
 361        else if((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 
 362                EEPROM_WORD0F_ASM_DIR)
 363            hw->fc = e1000_fc_tx_pause;
 364        else
 365            hw->fc = e1000_fc_full;
 366    }
 367
 368    /* We want to save off the original Flow Control configuration just
 369     * in case we get disconnected and then reconnected into a different
 370     * hub or switch with different Flow Control capabilities.
 371     */
 372    if(hw->mac_type == e1000_82542_rev2_0)
 373        hw->fc &= (~e1000_fc_tx_pause);
 374
 375    if((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
 376        hw->fc &= (~e1000_fc_rx_pause);
 377
 378    hw->original_fc = hw->fc;
 379
 380    DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
 381
 382    /* Take the 4 bits from EEPROM word 0x0F that determine the initial
 383     * polarity value for the SW controlled pins, and setup the
 384     * Extended Device Control reg with that info.
 385     * This is needed because one of the SW controlled pins is used for
 386     * signal detection.  So this should be done before e1000_setup_pcs_link()
 387     * or e1000_phy_setup() is called.
 388     */
 389    if(hw->mac_type == e1000_82543) {
 390        ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) << 
 391                    SWDPIO__EXT_SHIFT);
 392        E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
 393    }
 394
 395    /* Call the necessary subroutine to configure the link. */
 396    ret_val = (hw->media_type == e1000_media_type_fiber) ?
 397              e1000_setup_fiber_link(hw) :
 398              e1000_setup_copper_link(hw);
 399
 400    /* Initialize the flow control address, type, and PAUSE timer
 401     * registers to their default values.  This is done even if flow
 402     * control is disabled, because it does not hurt anything to
 403     * initialize these registers.
 404     */
 405    DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
 406
 407    E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
 408    E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
 409    E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
 410    E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
 411
 412    /* Set the flow control receive threshold registers.  Normally,
 413     * these registers will be set to a default threshold that may be
 414     * adjusted later by the driver's runtime code.  However, if the
 415     * ability to transmit pause frames in not enabled, then these
 416     * registers will be set to 0. 
 417     */
 418    if(!(hw->fc & e1000_fc_tx_pause)) {
 419        E1000_WRITE_REG(hw, FCRTL, 0);
 420        E1000_WRITE_REG(hw, FCRTH, 0);
 421    } else {
 422        /* We need to set up the Receive Threshold high and low water marks
 423         * as well as (optionally) enabling the transmission of XON frames.
 424         */
 425        if(hw->fc_send_xon) {
 426            E1000_WRITE_REG(hw, FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
 427            E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
 428        } else {
 429            E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
 430            E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
 431        }
 432    }
 433    return ret_val;
 434}
 435
 436/******************************************************************************
 437 * Sets up link for a fiber based adapter
 438 *
 439 * hw - Struct containing variables accessed by shared code
 440 *
 441 * Manipulates Physical Coding Sublayer functions in order to configure
 442 * link. Assumes the hardware has been previously reset and the transmitter
 443 * and receiver are not enabled.
 444 *****************************************************************************/
 445static int32_t 
 446e1000_setup_fiber_link(struct e1000_hw *hw)
 447{
 448    uint32_t ctrl;
 449    uint32_t status;
 450    uint32_t txcw = 0;
 451    uint32_t i;
 452    uint32_t signal;
 453    int32_t ret_val;
 454
 455    DEBUGFUNC("e1000_setup_fiber_link");
 456
 457    /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be 
 458     * set when the optics detect a signal. On older adapters, it will be 
 459     * cleared when there is a signal
 460     */
 461    ctrl = E1000_READ_REG(hw, CTRL);
 462    if(hw->mac_type > e1000_82544) signal = E1000_CTRL_SWDPIN1;
 463    else signal = 0;
 464   
 465    /* Take the link out of reset */
 466    ctrl &= ~(E1000_CTRL_LRST);
 467    
 468    e1000_config_collision_dist(hw);
 469
 470    /* Check for a software override of the flow control settings, and setup
 471     * the device accordingly.  If auto-negotiation is enabled, then software
 472     * will have to set the "PAUSE" bits to the correct value in the Tranmsit
 473     * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
 474     * auto-negotiation is disabled, then software will have to manually 
 475     * configure the two flow control enable bits in the CTRL register.
 476     *
 477     * The possible values of the "fc" parameter are:
 478     *      0:  Flow control is completely disabled
 479     *      1:  Rx flow control is enabled (we can receive pause frames, but 
 480     *          not send pause frames).
 481     *      2:  Tx flow control is enabled (we can send pause frames but we do
 482     *          not support receiving pause frames).
 483     *      3:  Both Rx and TX flow control (symmetric) are enabled.
 484     */
 485    switch (hw->fc) {
 486    case e1000_fc_none:
 487        /* Flow control is completely disabled by a software over-ride. */
 488        txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
 489        break;
 490    case e1000_fc_rx_pause:
 491        /* RX Flow control is enabled and TX Flow control is disabled by a 
 492         * software over-ride. Since there really isn't a way to advertise 
 493         * that we are capable of RX Pause ONLY, we will advertise that we
 494         * support both symmetric and asymmetric RX PAUSE. Later, we will
 495         *  disable the adapter's ability to send PAUSE frames.
 496         */
 497        txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
 498        break;
 499    case e1000_fc_tx_pause:
 500        /* TX Flow control is enabled, and RX Flow control is disabled, by a 
 501         * software over-ride.
 502         */
 503        txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
 504        break;
 505    case e1000_fc_full:
 506        /* Flow control (both RX and TX) is enabled by a software over-ride. */
 507        txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
 508        break;
 509    default:
 510        DEBUGOUT("Flow control param set incorrectly\n");
 511        return -E1000_ERR_CONFIG;
 512        break;
 513    }
 514
 515    /* Since auto-negotiation is enabled, take the link out of reset (the link
 516     * will be in reset, because we previously reset the chip). This will
 517     * restart auto-negotiation.  If auto-neogtiation is successful then the
 518     * link-up status bit will be set and the flow control enable bits (RFCE
 519     * and TFCE) will be set according to their negotiated value.
 520     */
 521    DEBUGOUT("Auto-negotiation enabled\n");
 522
 523    E1000_WRITE_REG(hw, TXCW, txcw);
 524    E1000_WRITE_REG(hw, CTRL, ctrl);
 525    E1000_WRITE_FLUSH(hw);
 526
 527    hw->txcw = txcw;
 528    msec_delay(1);
 529
 530    /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
 531     * indication in the Device Status Register.  Time-out if a link isn't 
 532     * seen in 500 milliseconds seconds (Auto-negotiation should complete in 
 533     * less than 500 milliseconds even if the other end is doing it in SW).
 534     */
 535    if((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
 536        DEBUGOUT("Looking for Link\n");
 537        for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
 538            msec_delay(10);
 539            status = E1000_READ_REG(hw, STATUS);
 540            if(status & E1000_STATUS_LU) break;
 541        }
 542        if(i == (LINK_UP_TIMEOUT / 10)) {
 543            /* AutoNeg failed to achieve a link, so we'll call 
 544             * e1000_check_for_link. This routine will force the link up if we
 545             * detect a signal. This will allow us to communicate with
 546             * non-autonegotiating link partners.
 547             */
 548            DEBUGOUT("Never got a valid link from auto-neg!!!\n");
 549            hw->autoneg_failed = 1;
 550            ret_val = e1000_check_for_link(hw);
 551            if(ret_val < 0) {
 552                DEBUGOUT("Error while checking for link\n");
 553                return ret_val;
 554            }
 555            hw->autoneg_failed = 0;
 556        } else {
 557            hw->autoneg_failed = 0;
 558            DEBUGOUT("Valid Link Found\n");
 559        }
 560    } else {
 561        DEBUGOUT("No Signal Detected\n");
 562    }
 563    return 0;
 564}
 565
 566/******************************************************************************
 567* Detects which PHY is present and the speed and duplex
 568*
 569* hw - Struct containing variables accessed by shared code
 570******************************************************************************/
 571static int32_t 
 572e1000_setup_copper_link(struct e1000_hw *hw)
 573{
 574    uint32_t ctrl;
 575    int32_t ret_val;
 576    uint16_t i;
 577    uint16_t phy_data;
 578
 579    DEBUGFUNC("e1000_setup_copper_link");
 580
 581    ctrl = E1000_READ_REG(hw, CTRL);
 582    /* With 82543, we need to force speed and duplex on the MAC equal to what
 583     * the PHY speed and duplex configuration is. In addition, we need to
 584     * perform a hardware reset on the PHY to take it out of reset.
 585     */
 586    if(hw->mac_type > e1000_82543) {
 587        ctrl |= E1000_CTRL_SLU;
 588        ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
 589        E1000_WRITE_REG(hw, CTRL, ctrl);
 590    } else {
 591        ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
 592        E1000_WRITE_REG(hw, CTRL, ctrl);
 593        e1000_phy_hw_reset(hw);
 594    }
 595
 596    /* Make sure we have a valid PHY */
 597    ret_val = e1000_detect_gig_phy(hw);
 598    if(ret_val < 0) {
 599        DEBUGOUT("Error, did not detect valid phy.\n");
 600        return ret_val;
 601    }
 602    DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
 603
 604    /* Enable CRS on TX. This must be set for half-duplex operation. */
 605    if(e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data) < 0) {
 606        DEBUGOUT("PHY Read Error\n");
 607        return -E1000_ERR_PHY;
 608    }
 609    phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
 610
 611    /* Options:
 612     *   MDI/MDI-X = 0 (default)
 613     *   0 - Auto for all speeds
 614     *   1 - MDI mode
 615     *   2 - MDI-X mode
 616     *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
 617     */
 618    phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
 619
 620    switch (hw->mdix) {
 621    case 1:
 622        phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
 623        break;
 624    case 2:
 625        phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
 626        break;
 627    case 3:
 628        phy_data |= M88E1000_PSCR_AUTO_X_1000T;
 629        break;
 630    case 0:
 631    default:
 632        phy_data |= M88E1000_PSCR_AUTO_X_MODE;
 633        break;
 634    }
 635
 636    /* Options:
 637     *   disable_polarity_correction = 0 (default)
 638     *       Automatic Correction for Reversed Cable Polarity
 639     *   0 - Disabled
 640     *   1 - Enabled
 641     */
 642    phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
 643    if(hw->disable_polarity_correction == 1)
 644        phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
 645    if(e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data) < 0) {
 646        DEBUGOUT("PHY Write Error\n");
 647        return -E1000_ERR_PHY;
 648    }
 649
 650    /* Force TX_CLK in the Extended PHY Specific Control Register
 651     * to 25MHz clock.
 652     */
 653    if(e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data) < 0) {
 654        DEBUGOUT("PHY Read Error\n");
 655        return -E1000_ERR_PHY;
 656    }
 657    phy_data |= M88E1000_EPSCR_TX_CLK_25;
 658    /* Configure Master and Slave downshift values */
 659    phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
 660                  M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
 661    phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
 662                 M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
 663    if(e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data) < 0) {
 664        DEBUGOUT("PHY Write Error\n");
 665        return -E1000_ERR_PHY;
 666    }
 667
 668    /* SW Reset the PHY so all changes take effect */
 669    ret_val = e1000_phy_reset(hw);
 670    if(ret_val < 0) {
 671        DEBUGOUT("Error Resetting the PHY\n");
 672        return ret_val;
 673    }
 674    
 675    /* Options:
 676     *   autoneg = 1 (default)
 677     *      PHY will advertise value(s) parsed from
 678     *      autoneg_advertised and fc
 679     *   autoneg = 0
 680     *      PHY will be set to 10H, 10F, 100H, or 100F
 681     *      depending on value parsed from forced_speed_duplex.
 682     */
 683
 684    /* Is autoneg enabled?  This is enabled by default or by software override.
 685     * If so, call e1000_phy_setup_autoneg routine to parse the
 686     * autoneg_advertised and fc options. If autoneg is NOT enabled, then the
 687     * user should have provided a speed/duplex override.  If so, then call
 688     * e1000_phy_force_speed_duplex to parse and set this up.
 689     */
 690    if(hw->autoneg) {
 691        /* Perform some bounds checking on the hw->autoneg_advertised
 692         * parameter.  If this variable is zero, then set it to the default.
 693         */
 694        hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
 695
 696        /* If autoneg_advertised is zero, we assume it was not defaulted
 697         * by the calling code so we set to advertise full capability.
 698         */
 699        if(hw->autoneg_advertised == 0)
 700            hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
 701
 702        DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
 703        ret_val = e1000_phy_setup_autoneg(hw);
 704        if(ret_val < 0) {
 705            DEBUGOUT("Error Setting up Auto-Negotiation\n");
 706            return ret_val;
 707        }
 708        DEBUGOUT("Restarting Auto-Neg\n");
 709
 710        /* Restart auto-negotiation by setting the Auto Neg Enable bit and
 711         * the Auto Neg Restart bit in the PHY control register.
 712         */
 713        if(e1000_read_phy_reg(hw, PHY_CTRL, &phy_data) < 0) {
 714            DEBUGOUT("PHY Read Error\n");
 715            return -E1000_ERR_PHY;
 716        }
 717        phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
 718        if(e1000_write_phy_reg(hw, PHY_CTRL, phy_data) < 0) {
 719            DEBUGOUT("PHY Write Error\n");
 720            return -E1000_ERR_PHY;
 721        }
 722
 723        /* Does the user want to wait for Auto-Neg to complete here, or
 724         * check at a later time (for example, callback routine).
 725         */
 726        if(hw->wait_autoneg_complete) {
 727            ret_val = e1000_wait_autoneg(hw);
 728            if(ret_val < 0) {
 729                DEBUGOUT("Error while waiting for autoneg to complete\n");
 730                return ret_val;
 731            }
 732        }
 733    } else {
 734        DEBUGOUT("Forcing speed and duplex\n");
 735        ret_val = e1000_phy_force_speed_duplex(hw);
 736        if(ret_val < 0) {
 737            DEBUGOUT("Error Forcing Speed and Duplex\n");
 738            return ret_val;
 739        }
 740    }
 741
 742    /* Check link status. Wait up to 100 microseconds for link to become
 743     * valid.
 744     */
 745    for(i = 0; i < 10; i++) {
 746        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
 747            DEBUGOUT("PHY Read Error\n");
 748            return -E1000_ERR_PHY;
 749        }
 750        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
 751            DEBUGOUT("PHY Read Error\n");
 752            return -E1000_ERR_PHY;
 753        }
 754        if(phy_data & MII_SR_LINK_STATUS) {
 755            /* We have link, so we need to finish the config process:
 756             *   1) Set up the MAC to the current PHY speed/duplex
 757             *      if we are on 82543.  If we
 758             *      are on newer silicon, we only need to configure
 759             *      collision distance in the Transmit Control Register.
 760             *   2) Set up flow control on the MAC to that established with
 761             *      the link partner.
 762             */
 763            if(hw->mac_type >= e1000_82544) {
 764                e1000_config_collision_dist(hw);
 765            } else {
 766                ret_val = e1000_config_mac_to_phy(hw);
 767                if(ret_val < 0) {
 768                    DEBUGOUT("Error configuring MAC to PHY settings\n");
 769                    return ret_val;
 770                  }
 771            }
 772            ret_val = e1000_config_fc_after_link_up(hw);
 773            if(ret_val < 0) {
 774                DEBUGOUT("Error Configuring Flow Control\n");
 775                return ret_val;
 776            }
 777            DEBUGOUT("Valid link established!!!\n");
 778            return 0;
 779        }
 780        udelay(10);
 781    }
 782
 783    DEBUGOUT("Unable to establish link!!!\n");
 784    return 0;
 785}
 786
 787/******************************************************************************
 788* Configures PHY autoneg and flow control advertisement settings
 789*
 790* hw - Struct containing variables accessed by shared code
 791******************************************************************************/
 792int32_t
 793e1000_phy_setup_autoneg(struct e1000_hw *hw)
 794{
 795    uint16_t mii_autoneg_adv_reg;
 796    uint16_t mii_1000t_ctrl_reg;
 797
 798    DEBUGFUNC("e1000_phy_setup_autoneg");
 799
 800    /* Read the MII Auto-Neg Advertisement Register (Address 4). */
 801    if(e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg) < 0) {
 802        DEBUGOUT("PHY Read Error\n");
 803        return -E1000_ERR_PHY;
 804    }
 805
 806    /* Read the MII 1000Base-T Control Register (Address 9). */
 807    if(e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg) < 0) {
 808        DEBUGOUT("PHY Read Error\n");
 809        return -E1000_ERR_PHY;
 810    }
 811
 812    /* Need to parse both autoneg_advertised and fc and set up
 813     * the appropriate PHY registers.  First we will parse for
 814     * autoneg_advertised software override.  Since we can advertise
 815     * a plethora of combinations, we need to check each bit
 816     * individually.
 817     */
 818
 819    /* First we clear all the 10/100 mb speed bits in the Auto-Neg
 820     * Advertisement Register (Address 4) and the 1000 mb speed bits in
 821     * the  1000Base-T Control Register (Address 9).
 822     */
 823    mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
 824    mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
 825
 826    DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
 827
 828    /* Do we want to advertise 10 Mb Half Duplex? */
 829    if(hw->autoneg_advertised & ADVERTISE_10_HALF) {
 830        DEBUGOUT("Advertise 10mb Half duplex\n");
 831        mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
 832    }
 833
 834    /* Do we want to advertise 10 Mb Full Duplex? */
 835    if(hw->autoneg_advertised & ADVERTISE_10_FULL) {
 836        DEBUGOUT("Advertise 10mb Full duplex\n");
 837        mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
 838    }
 839
 840    /* Do we want to advertise 100 Mb Half Duplex? */
 841    if(hw->autoneg_advertised & ADVERTISE_100_HALF) {
 842        DEBUGOUT("Advertise 100mb Half duplex\n");
 843        mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
 844    }
 845
 846    /* Do we want to advertise 100 Mb Full Duplex? */
 847    if(hw->autoneg_advertised & ADVERTISE_100_FULL) {
 848        DEBUGOUT("Advertise 100mb Full duplex\n");
 849        mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
 850    }
 851
 852    /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
 853    if(hw->autoneg_advertised & ADVERTISE_1000_HALF) {
 854        DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
 855    }
 856
 857    /* Do we want to advertise 1000 Mb Full Duplex? */
 858    if(hw->autoneg_advertised & ADVERTISE_1000_FULL) {
 859        DEBUGOUT("Advertise 1000mb Full duplex\n");
 860        mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
 861    }
 862
 863    /* Check for a software override of the flow control settings, and
 864     * setup the PHY advertisement registers accordingly.  If
 865     * auto-negotiation is enabled, then software will have to set the
 866     * "PAUSE" bits to the correct value in the Auto-Negotiation
 867     * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
 868     *
 869     * The possible values of the "fc" parameter are:
 870     *      0:  Flow control is completely disabled
 871     *      1:  Rx flow control is enabled (we can receive pause frames
 872     *          but not send pause frames).
 873     *      2:  Tx flow control is enabled (we can send pause frames
 874     *          but we do not support receiving pause frames).
 875     *      3:  Both Rx and TX flow control (symmetric) are enabled.
 876     *  other:  No software override.  The flow control configuration
 877     *          in the EEPROM is used.
 878     */
 879    switch (hw->fc) {
 880    case e1000_fc_none: /* 0 */
 881        /* Flow control (RX & TX) is completely disabled by a
 882         * software over-ride.
 883         */
 884        mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
 885        break;
 886    case e1000_fc_rx_pause: /* 1 */
 887        /* RX Flow control is enabled, and TX Flow control is
 888         * disabled, by a software over-ride.
 889         */
 890        /* Since there really isn't a way to advertise that we are
 891         * capable of RX Pause ONLY, we will advertise that we
 892         * support both symmetric and asymmetric RX PAUSE.  Later
 893         * (in e1000_config_fc_after_link_up) we will disable the
 894         *hw's ability to send PAUSE frames.
 895         */
 896        mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
 897        break;
 898    case e1000_fc_tx_pause: /* 2 */
 899        /* TX Flow control is enabled, and RX Flow control is
 900         * disabled, by a software over-ride.
 901         */
 902        mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
 903        mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
 904        break;
 905    case e1000_fc_full: /* 3 */
 906        /* Flow control (both RX and TX) is enabled by a software
 907         * over-ride.
 908         */
 909        mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
 910        break;
 911    default:
 912        DEBUGOUT("Flow control param set incorrectly\n");
 913        return -E1000_ERR_CONFIG;
 914    }
 915
 916    if(e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg) < 0) {
 917        DEBUGOUT("PHY Write Error\n");
 918        return -E1000_ERR_PHY;
 919    }
 920
 921    DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
 922
 923    if(e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg) < 0) {
 924        DEBUGOUT("PHY Write Error\n");
 925        return -E1000_ERR_PHY;
 926    }
 927    return 0;
 928}
 929
 930/******************************************************************************
 931* Force PHY speed and duplex settings to hw->forced_speed_duplex
 932*
 933* hw - Struct containing variables accessed by shared code
 934******************************************************************************/
 935static int32_t
 936e1000_phy_force_speed_duplex(struct e1000_hw *hw)
 937{
 938    uint32_t ctrl;
 939    int32_t ret_val;
 940    uint16_t mii_ctrl_reg;
 941    uint16_t mii_status_reg;
 942    uint16_t phy_data;
 943    uint16_t i;
 944
 945    DEBUGFUNC("e1000_phy_force_speed_duplex");
 946
 947    /* Turn off Flow control if we are forcing speed and duplex. */
 948    hw->fc = e1000_fc_none;
 949
 950    DEBUGOUT1("hw->fc = %d\n", hw->fc);
 951
 952    /* Read the Device Control Register. */
 953    ctrl = E1000_READ_REG(hw, CTRL);
 954
 955    /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
 956    ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
 957    ctrl &= ~(DEVICE_SPEED_MASK);
 958
 959    /* Clear the Auto Speed Detect Enable bit. */
 960    ctrl &= ~E1000_CTRL_ASDE;
 961
 962    /* Read the MII Control Register. */
 963    if(e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg) < 0) {
 964        DEBUGOUT("PHY Read Error\n");
 965        return -E1000_ERR_PHY;
 966    }
 967
 968    /* We need to disable autoneg in order to force link and duplex. */
 969
 970    mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
 971
 972    /* Are we forcing Full or Half Duplex? */
 973    if(hw->forced_speed_duplex == e1000_100_full ||
 974       hw->forced_speed_duplex == e1000_10_full) {
 975        /* We want to force full duplex so we SET the full duplex bits in the
 976         * Device and MII Control Registers.
 977         */
 978        ctrl |= E1000_CTRL_FD;
 979        mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
 980        DEBUGOUT("Full Duplex\n");
 981    } else {
 982        /* We want to force half duplex so we CLEAR the full duplex bits in
 983         * the Device and MII Control Registers.
 984         */
 985        ctrl &= ~E1000_CTRL_FD;
 986        mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
 987        DEBUGOUT("Half Duplex\n");
 988    }
 989
 990    /* Are we forcing 100Mbps??? */
 991    if(hw->forced_speed_duplex == e1000_100_full ||
 992       hw->forced_speed_duplex == e1000_100_half) {
 993        /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
 994        ctrl |= E1000_CTRL_SPD_100;
 995        mii_ctrl_reg |= MII_CR_SPEED_100;
 996        mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
 997        DEBUGOUT("Forcing 100mb ");
 998    } else {
 999        /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
1000        ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1001        mii_ctrl_reg |= MII_CR_SPEED_10;
1002        mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1003        DEBUGOUT("Forcing 10mb ");
1004    }
1005
1006    e1000_config_collision_dist(hw);
1007
1008    /* Write the configured values back to the Device Control Reg. */
1009    E1000_WRITE_REG(hw, CTRL, ctrl);
1010
1011    /* Write the MII Control Register with the new PHY configuration. */
1012    if(e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data) < 0) {
1013        DEBUGOUT("PHY Read Error\n");
1014        return -E1000_ERR_PHY;
1015    }
1016
1017    /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
1018     * forced whenever speed are duplex are forced.
1019     */
1020    phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1021    if(e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data) < 0) {
1022        DEBUGOUT("PHY Write Error\n");
1023        return -E1000_ERR_PHY;
1024    }
1025    DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
1026    
1027    /* Need to reset the PHY or these changes will be ignored */
1028    mii_ctrl_reg |= MII_CR_RESET;
1029    if(e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg) < 0) {
1030        DEBUGOUT("PHY Write Error\n");
1031        return -E1000_ERR_PHY;
1032    }
1033    udelay(1);
1034
1035    /* The wait_autoneg_complete flag may be a little misleading here.
1036     * Since we are forcing speed and duplex, Auto-Neg is not enabled.
1037     * But we do want to delay for a period while forcing only so we
1038     * don't generate false No Link messages.  So we will wait here
1039     * only if the user has set wait_autoneg_complete to 1, which is
1040     * the default.
1041     */
1042    if(hw->wait_autoneg_complete) {
1043        /* We will wait for autoneg to complete. */
1044        DEBUGOUT("Waiting for forced speed/duplex link.\n");
1045        mii_status_reg = 0;
1046
1047        /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1048        for(i = PHY_FORCE_TIME; i > 0; i--) {
1049            /* Read the MII Status Register and wait for Auto-Neg Complete bit
1050             * to be set.
1051             */
1052            if(e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
1053                DEBUGOUT("PHY Read Error\n");
1054                return -E1000_ERR_PHY;
1055            }
1056            if(e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
1057                DEBUGOUT("PHY Read Error\n");
1058                return -E1000_ERR_PHY;
1059            }
1060            if(mii_status_reg & MII_SR_LINK_STATUS) break;
1061            msec_delay(100);
1062        }
1063        if(i == 0) { /* We didn't get link */
1064            /* Reset the DSP and wait again for link. */
1065            
1066            ret_val = e1000_phy_reset_dsp(hw);
1067            if(ret_val < 0) {
1068                DEBUGOUT("Error Resetting PHY DSP\n");
1069                return ret_val;
1070            }
1071        }
1072        /* This loop will early-out if the link condition has been met.  */
1073        for(i = PHY_FORCE_TIME; i > 0; i--) {
1074            if(mii_status_reg & MII_SR_LINK_STATUS) break;
1075            msec_delay(100);
1076            /* Read the MII Status Register and wait for Auto-Neg Complete bit
1077             * to be set.
1078             */
1079            if(e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
1080                DEBUGOUT("PHY Read Error\n");
1081                return -E1000_ERR_PHY;
1082            }
1083            if(e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
1084                DEBUGOUT("PHY Read Error\n");
1085                return -E1000_ERR_PHY;
1086            }
1087        }
1088    }
1089    
1090    /* Because we reset the PHY above, we need to re-force TX_CLK in the
1091     * Extended PHY Specific Control Register to 25MHz clock.  This value
1092     * defaults back to a 2.5MHz clock when the PHY is reset.
1093     */
1094    if(e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data) < 0) {
1095        DEBUGOUT("PHY Read Error\n");
1096        return -E1000_ERR_PHY;
1097    }
1098    phy_data |= M88E1000_EPSCR_TX_CLK_25;
1099    if(e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data) < 0) {
1100        DEBUGOUT("PHY Write Error\n");
1101        return -E1000_ERR_PHY;
1102    }
1103
1104    /* In addition, because of the s/w reset above, we need to enable CRS on
1105     * TX.  This must be set for both full and half duplex operation.
1106     */
1107    if(e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data) < 0) {
1108        DEBUGOUT("PHY Read Error\n");
1109        return -E1000_ERR_PHY;
1110    }
1111    phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1112    if(e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data) < 0) {
1113        DEBUGOUT("PHY Write Error\n");
1114        return -E1000_ERR_PHY;
1115    }
1116    return 0;
1117}
1118
1119/******************************************************************************
1120* Sets the collision distance in the Transmit Control register
1121*
1122* hw - Struct containing variables accessed by shared code
1123*
1124* Link should have been established previously. Reads the speed and duplex
1125* information from the Device Status register.
1126******************************************************************************/
1127void
1128e1000_config_collision_dist(struct e1000_hw *hw)
1129{
1130    uint32_t tctl;
1131
1132    tctl = E1000_READ_REG(hw, TCTL);
1133
1134    tctl &= ~E1000_TCTL_COLD;
1135    tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1136
1137    E1000_WRITE_REG(hw, TCTL, tctl);
1138    E1000_WRITE_FLUSH(hw);
1139}
1140
1141/******************************************************************************
1142* Sets MAC speed and duplex settings to reflect the those in the PHY
1143*
1144* hw - Struct containing variables accessed by shared code
1145* mii_reg - data to write to the MII control register
1146*
1147* The contents of the PHY register containing the needed information need to
1148* be passed in.
1149******************************************************************************/
1150static int32_t
1151e1000_config_mac_to_phy(struct e1000_hw *hw)
1152{
1153    uint32_t ctrl;
1154    uint16_t phy_data;
1155
1156    DEBUGFUNC("e1000_config_mac_to_phy");
1157
1158    /* Read the Device Control Register and set the bits to Force Speed
1159     * and Duplex.
1160     */
1161    ctrl = E1000_READ_REG(hw, CTRL);
1162    ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1163    ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
1164
1165    /* Set up duplex in the Device Control and Transmit Control
1166     * registers depending on negotiated values.
1167     */
1168    if(e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
1169        DEBUGOUT("PHY Read Error\n");
1170        return -E1000_ERR_PHY;
1171    }
1172    if(phy_data & M88E1000_PSSR_DPLX) ctrl |= E1000_CTRL_FD;
1173    else ctrl &= ~E1000_CTRL_FD;
1174
1175    e1000_config_collision_dist(hw);
1176
1177    /* Set up speed in the Device Control register depending on
1178     * negotiated values.
1179     */
1180    if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
1181        ctrl |= E1000_CTRL_SPD_1000;
1182    else if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
1183        ctrl |= E1000_CTRL_SPD_100;
1184    /* Write the configured values back to the Device Control Reg. */
1185    E1000_WRITE_REG(hw, CTRL, ctrl);
1186    return 0;
1187}
1188
1189/******************************************************************************
1190 * Forces the MAC's flow control settings.
1191 * 
1192 * hw - Struct containing variables accessed by shared code
1193 *
1194 * Sets the TFCE and RFCE bits in the device control register to reflect
1195 * the adapter settings. TFCE and RFCE need to be explicitly set by
1196 * software when a Copper PHY is used because autonegotiation is managed
1197 * by the PHY rather than the MAC. Software must also configure these
1198 * bits when link is forced on a fiber connection.
1199 *****************************************************************************/
1200static int32_t
1201e1000_force_mac_fc(struct e1000_hw *hw)
1202{
1203    uint32_t ctrl;
1204
1205    DEBUGFUNC("e1000_force_mac_fc");
1206
1207    /* Get the current configuration of the Device Control Register */
1208    ctrl = E1000_READ_REG(hw, CTRL);
1209
1210    /* Because we didn't get link via the internal auto-negotiation
1211     * mechanism (we either forced link or we got link via PHY
1212     * auto-neg), we have to manually enable/disable transmit an
1213     * receive flow control.
1214     *
1215     * The "Case" statement below enables/disable flow control
1216     * according to the "hw->fc" parameter.
1217     *
1218     * The possible values of the "fc" parameter are:
1219     *      0:  Flow control is completely disabled
1220     *      1:  Rx flow control is enabled (we can receive pause
1221     *          frames but not send pause frames).
1222     *      2:  Tx flow control is enabled (we can send pause frames
1223     *          frames but we do not receive pause frames).
1224     *      3:  Both Rx and TX flow control (symmetric) is enabled.
1225     *  other:  No other values should be possible at this point.
1226     */
1227
1228    switch (hw->fc) {
1229    case e1000_fc_none:
1230        ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1231        break;
1232    case e1000_fc_rx_pause:
1233        ctrl &= (~E1000_CTRL_TFCE);
1234        ctrl |= E1000_CTRL_RFCE;
1235        break;
1236    case e1000_fc_tx_pause:
1237        ctrl &= (~E1000_CTRL_RFCE);
1238        ctrl |= E1000_CTRL_TFCE;
1239        break;
1240    case e1000_fc_full:
1241        ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1242        break;
1243    default:
1244        DEBUGOUT("Flow control param set incorrectly\n");
1245        return -E1000_ERR_CONFIG;
1246    }
1247
1248    /* Disable TX Flow Control for 82542 (rev 2.0) */
1249    if(hw->mac_type == e1000_82542_rev2_0)
1250        ctrl &= (~E1000_CTRL_TFCE);
1251
1252    E1000_WRITE_REG(hw, CTRL, ctrl);
1253    return 0;
1254}
1255
1256/******************************************************************************
1257 * Configures flow control settings after link is established
1258 * 
1259 * hw - Struct containing variables accessed by shared code
1260 *
1261 * Should be called immediately after a valid link has been established.
1262 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
1263 * and autonegotiation is enabled, the MAC flow control settings will be set
1264 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
1265 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
1266 *****************************************************************************/
1267int32_t
1268e1000_config_fc_after_link_up(struct e1000_hw *hw)
1269{
1270    int32_t ret_val;
1271    uint16_t mii_status_reg;
1272    uint16_t mii_nway_adv_reg;
1273    uint16_t mii_nway_lp_ability_reg;
1274    uint16_t speed;
1275    uint16_t duplex;
1276
1277    DEBUGFUNC("e1000_config_fc_after_link_up");
1278
1279    /* Check for the case where we have fiber media and auto-neg failed
1280     * so we had to force link.  In this case, we need to force the
1281     * configuration of the MAC to match the "fc" parameter.
1282     */
1283    if(((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
1284       ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
1285        ret_val = e1000_force_mac_fc(hw);
1286        if(ret_val < 0) {
1287            DEBUGOUT("Error forcing flow control settings\n");
1288            return ret_val;
1289        }
1290    }
1291
1292    /* Check for the case where we have copper media and auto-neg is
1293     * enabled.  In this case, we need to check and see if Auto-Neg
1294     * has completed, and if so, how the PHY and link partner has
1295     * flow control configured.
1296     */
1297    if((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
1298        /* Read the MII Status Register and check to see if AutoNeg
1299         * has completed.  We read this twice because this reg has
1300         * some "sticky" (latched) bits.
1301         */
1302        if(e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
1303            DEBUGOUT("PHY Read Error \n");
1304            return -E1000_ERR_PHY;
1305        }
1306        if(e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
1307            DEBUGOUT("PHY Read Error \n");
1308            return -E1000_ERR_PHY;
1309        }
1310
1311        if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
1312            /* The AutoNeg process has completed, so we now need to
1313             * read both the Auto Negotiation Advertisement Register
1314             * (Address 4) and the Auto_Negotiation Base Page Ability
1315             * Register (Address 5) to determine how flow control was
1316             * negotiated.
1317             */
1318            if(e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
1319                DEBUGOUT("PHY Read Error\n");
1320                return -E1000_ERR_PHY;
1321            }
1322            if(e1000_read_phy_reg(hw, PHY_LP_ABILITY, &mii_nway_lp_ability_reg) < 0) {
1323                DEBUGOUT("PHY Read Error\n");
1324                return -E1000_ERR_PHY;
1325            }
1326
1327            /* Two bits in the Auto Negotiation Advertisement Register
1328             * (Address 4) and two bits in the Auto Negotiation Base
1329             * Page Ability Register (Address 5) determine flow control
1330             * for both the PHY and the link partner.  The following
1331             * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1332             * 1999, describes these PAUSE resolution bits and how flow
1333             * control is determined based upon these settings.
1334             * NOTE:  DC = Don't Care
1335             *
1336             *   LOCAL DEVICE  |   LINK PARTNER
1337             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1338             *-------|---------|-------|---------|--------------------
1339             *   0   |    0    |  DC   |   DC    | e1000_fc_none
1340             *   0   |    1    |   0   |   DC    | e1000_fc_none
1341             *   0   |    1    |   1   |    0    | e1000_fc_none
1342             *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1343             *   1   |    0    |   0   |   DC    | e1000_fc_none
1344             *   1   |   DC    |   1   |   DC    | e1000_fc_full
1345             *   1   |    1    |   0   |    0    | e1000_fc_none
1346             *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1347             *
1348             */
1349            /* Are both PAUSE bits set to 1?  If so, this implies
1350             * Symmetric Flow Control is enabled at both ends.  The
1351             * ASM_DIR bits are irrelevant per the spec.
1352             *
1353             * For Symmetric Flow Control:
1354             *
1355             *   LOCAL DEVICE  |   LINK PARTNER
1356             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1357             *-------|---------|-------|---------|--------------------
1358             *   1   |   DC    |   1   |   DC    | e1000_fc_full
1359             *
1360             */
1361            if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1362               (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
1363                /* Now we need to check if the user selected RX ONLY
1364                 * of pause frames.  In this case, we had to advertise
1365                 * FULL flow control because we could not advertise RX
1366                 * ONLY. Hence, we must now check to see if we need to
1367                 * turn OFF  the TRANSMISSION of PAUSE frames.
1368                 */
1369                if(hw->original_fc == e1000_fc_full) {
1370                    hw->fc = e1000_fc_full;
1371                    DEBUGOUT("Flow Control = FULL.\r\n");
1372                } else {
1373                    hw->fc = e1000_fc_rx_pause;
1374                    DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1375                }
1376            }
1377            /* For receiving PAUSE frames ONLY.
1378             *
1379             *   LOCAL DEVICE  |   LINK PARTNER
1380             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1381             *-------|---------|-------|---------|--------------------
1382             *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1383             *
1384             */
1385            else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1386                    (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1387                    (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1388                    (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1389                hw->fc = e1000_fc_tx_pause;
1390                DEBUGOUT("Flow Control = TX PAUSE frames only.\r\n");
1391            }
1392            /* For transmitting PAUSE frames ONLY.
1393             *
1394             *   LOCAL DEVICE  |   LINK PARTNER
1395             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1396             *-------|---------|-------|---------|--------------------
1397             *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1398             *
1399             */
1400            else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1401                    (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1402                    !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1403                    (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1404                hw->fc = e1000_fc_rx_pause;
1405                DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1406            }
1407            /* Per the IEEE spec, at this point flow control should be
1408             * disabled.  However, we want to consider that we could
1409             * be connected to a legacy switch that doesn't advertise
1410             * desired flow control, but can be forced on the link
1411             * partner.  So if we advertised no flow control, that is
1412             * what we will resolve to.  If we advertised some kind of
1413             * receive capability (Rx Pause Only or Full Flow Control)
1414             * and the link partner advertised none, we will configure
1415             * ourselves to enable Rx Flow Control only.  We can do
1416             * this safely for two reasons:  If the link partner really
1417             * didn't want flow control enabled, and we enable Rx, no
1418             * harm done since we won't be receiving any PAUSE frames
1419             * anyway.  If the intent on the link partner was to have
1420             * flow control enabled, then by us enabling RX only, we
1421             * can at least receive pause frames and process them.
1422             * This is a good idea because in most cases, since we are
1423             * predominantly a server NIC, more times than not we will
1424             * be asked to delay transmission of packets than asking
1425             * our link partner to pause transmission of frames.
1426             */
1427            else if(hw->original_fc == e1000_fc_none ||
1428                    hw->original_fc == e1000_fc_tx_pause) {
1429                hw->fc = e1000_fc_none;
1430                DEBUGOUT("Flow Control = NONE.\r\n");
1431            } else {
1432                hw->fc = e1000_fc_rx_pause;
1433                DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1434            }
1435
1436            /* Now we need to do one last check...  If we auto-
1437             * negotiated to HALF DUPLEX, flow control should not be
1438             * enabled per IEEE 802.3 spec.
1439             */
1440            e1000_get_speed_and_duplex(hw, &speed, &duplex);
1441
1442            if(duplex == HALF_DUPLEX)
1443                hw->fc = e1000_fc_none;
1444
1445            /* Now we call a subroutine to actually force the MAC
1446             * controller to use the correct flow control settings.
1447             */
1448            ret_val = e1000_force_mac_fc(hw);
1449            if(ret_val < 0) {
1450                DEBUGOUT("Error forcing flow control settings\n");
1451                return ret_val;
1452             }
1453        } else {
1454            DEBUGOUT("Copper PHY and Auto Neg has not completed.\r\n");
1455        }
1456    }
1457    return 0;
1458}
1459
1460/******************************************************************************
1461 * Checks to see if the link status of the hardware has changed.
1462 *
1463 * hw - Struct containing variables accessed by shared code
1464 *
1465 * Called by any function that needs to check the link status of the adapter.
1466 *****************************************************************************/
1467int32_t
1468e1000_check_for_link(struct e1000_hw *hw)
1469{
1470    uint32_t rxcw;
1471    uint32_t ctrl;
1472    uint32_t status;
1473    uint32_t rctl;
1474    uint32_t signal;
1475    int32_t ret_val;
1476    uint16_t phy_data;
1477    uint16_t lp_capability;
1478
1479    DEBUGFUNC("e1000_check_for_link");
1480    
1481    /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be 
1482     * set when the optics detect a signal. On older adapters, it will be 
1483     * cleared when there is a signal
1484     */
1485    if(hw->mac_type > e1000_82544) signal = E1000_CTRL_SWDPIN1;
1486    else signal = 0;
1487
1488    ctrl = E1000_READ_REG(hw, CTRL);
1489    status = E1000_READ_REG(hw, STATUS);
1490    rxcw = E1000_READ_REG(hw, RXCW);
1491
1492    /* If we have a copper PHY then we only want to go out to the PHY
1493     * registers to see if Auto-Neg has completed and/or if our link
1494     * status has changed.  The get_link_status flag will be set if we
1495     * receive a Link Status Change interrupt or we have Rx Sequence
1496     * Errors.
1497     */
1498    if((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
1499        /* First we want to see if the MII Status Register reports
1500         * link.  If so, then we want to get the current speed/duplex
1501         * of the PHY.
1502         * Read the register twice since the link bit is sticky.
1503         */
1504        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
1505            DEBUGOUT("PHY Read Error\n");
1506            return -E1000_ERR_PHY;
1507        }
1508        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
1509            DEBUGOUT("PHY Read Error\n");
1510            return -E1000_ERR_PHY;
1511        }
1512
1513        if(phy_data & MII_SR_LINK_STATUS) {
1514            hw->get_link_status = FALSE;
1515        } else {
1516            /* No link detected */
1517            return 0;
1518        }
1519
1520        /* If we are forcing speed/duplex, then we simply return since
1521         * we have already determined whether we have link or not.
1522         */
1523        if(!hw->autoneg) return -E1000_ERR_CONFIG;
1524
1525        /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
1526         * have Si on board that is 82544 or newer, Auto
1527         * Speed Detection takes care of MAC speed/duplex
1528         * configuration.  So we only need to configure Collision
1529         * Distance in the MAC.  Otherwise, we need to force
1530         * speed/duplex on the MAC to the current PHY speed/duplex
1531         * settings.
1532         */
1533        if(hw->mac_type >= e1000_82544)
1534            e1000_config_collision_dist(hw);
1535        else {
1536            ret_val = e1000_config_mac_to_phy(hw);
1537            if(ret_val < 0) {
1538                DEBUGOUT("Error configuring MAC to PHY settings\n");
1539                return ret_val;
1540            }
1541        }
1542
1543        /* Configure Flow Control now that Auto-Neg has completed. First, we 
1544         * need to restore the desired flow control settings because we may
1545         * have had to re-autoneg with a different link partner.
1546         */
1547        ret_val = e1000_config_fc_after_link_up(hw);
1548        if(ret_val < 0) {
1549            DEBUGOUT("Error configuring flow control\n");
1550            return ret_val;
1551        }
1552
1553        /* At this point we know that we are on copper and we have
1554         * auto-negotiated link.  These are conditions for checking the link
1555         * parter capability register.  We use the link partner capability to
1556         * determine if TBI Compatibility needs to be turned on or off.  If
1557         * the link partner advertises any speed in addition to Gigabit, then
1558         * we assume that they are GMII-based, and TBI compatibility is not
1559         * needed. If no other speeds are advertised, we assume the link
1560         * partner is TBI-based, and we turn on TBI Compatibility.
1561         */
1562        if(hw->tbi_compatibility_en) {
1563            if(e1000_read_phy_reg(hw, PHY_LP_ABILITY, &lp_capability) < 0) {
1564                DEBUGOUT("PHY Read Error\n");
1565                return -E1000_ERR_PHY;
1566            }
1567            if(lp_capability & (NWAY_LPAR_10T_HD_CAPS |
1568                                NWAY_LPAR_10T_FD_CAPS |
1569                                NWAY_LPAR_100TX_HD_CAPS |
1570                                NWAY_LPAR_100TX_FD_CAPS |
1571                                NWAY_LPAR_100T4_CAPS)) {
1572                /* If our link partner advertises anything in addition to 
1573                 * gigabit, we do not need to enable TBI compatibility.
1574                 */
1575                if(hw->tbi_compatibility_on) {
1576                    /* If we previously were in the mode, turn it off. */
1577                    rctl = E1000_READ_REG(hw, RCTL);
1578                    rctl &= ~E1000_RCTL_SBP;
1579                    E1000_WRITE_REG(hw, RCTL, rctl);
1580                    hw->tbi_compatibility_on = FALSE;
1581                }
1582            } else {
1583                /* If TBI compatibility is was previously off, turn it on. For
1584                 * compatibility with a TBI link partner, we will store bad
1585                 * packets. Some frames have an additional byte on the end and
1586                 * will look like CRC errors to to the hardware.
1587                 */
1588                if(!hw->tbi_compatibility_on) {
1589                    hw->tbi_compatibility_on = TRUE;
1590                    rctl = E1000_READ_REG(hw, RCTL);
1591                    rctl |= E1000_RCTL_SBP;
1592                    E1000_WRITE_REG(hw, RCTL, rctl);
1593                }
1594            }
1595        }
1596    }
1597    /* If we don't have link (auto-negotiation failed or link partner cannot
1598     * auto-negotiate), the cable is plugged in (we have signal), and our
1599     * link partner is not trying to auto-negotiate with us (we are receiving
1600     * idles or data), we need to force link up. We also need to give
1601     * auto-negotiation time to complete, in case the cable was just plugged
1602     * in. The autoneg_failed flag does this.
1603     */
1604    else if((hw->media_type == e1000_media_type_fiber) &&
1605            (!(status & E1000_STATUS_LU)) &&
1606            ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
1607            (!(rxcw & E1000_RXCW_C))) {
1608        if(hw->autoneg_failed == 0) {
1609            hw->autoneg_failed = 1;
1610            return 0;
1611        }
1612        DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
1613
1614        /* Disable auto-negotiation in the TXCW register */
1615        E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
1616
1617        /* Force link-up and also force full-duplex. */
1618        ctrl = E1000_READ_REG(hw, CTRL);
1619        ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
1620        E1000_WRITE_REG(hw, CTRL, ctrl);
1621
1622        /* Configure Flow Control after forcing link up. */
1623        ret_val = e1000_config_fc_after_link_up(hw);
1624        if(ret_val < 0) {
1625            DEBUGOUT("Error configuring flow control\n");
1626            return ret_val;
1627        }
1628    }
1629    /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
1630     * auto-negotiation in the TXCW register and disable forced link in the
1631     * Device Control register in an attempt to auto-negotiate with our link
1632     * partner.
1633     */
1634    else if((hw->media_type == e1000_media_type_fiber) &&
1635              (ctrl & E1000_CTRL_SLU) &&
1636              (rxcw & E1000_RXCW_C)) {
1637        DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
1638        E1000_WRITE_REG(hw, TXCW, hw->txcw);
1639        E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
1640    }
1641    return 0;
1642}
1643
1644/******************************************************************************
1645 * Detects the current speed and duplex settings of the hardware.
1646 *
1647 * hw - Struct containing variables accessed by shared code
1648 * speed - Speed of the connection
1649 * duplex - Duplex setting of the connection
1650 *****************************************************************************/
1651void
1652e1000_get_speed_and_duplex(struct e1000_hw *hw,
1653                           uint16_t *speed,
1654                           uint16_t *duplex)
1655{
1656    uint32_t status;
1657
1658    DEBUGFUNC("e1000_get_speed_and_duplex");
1659
1660    if(hw->mac_type >= e1000_82543) {
1661        status = E1000_READ_REG(hw, STATUS);
1662        if(status & E1000_STATUS_SPEED_1000) {
1663            *speed = SPEED_1000;
1664            DEBUGOUT("1000 Mbs, ");
1665        } else if(status & E1000_STATUS_SPEED_100) {
1666            *speed = SPEED_100;
1667            DEBUGOUT("100 Mbs, ");
1668        } else {
1669            *speed = SPEED_10;
1670            DEBUGOUT("10 Mbs, ");
1671        }
1672
1673        if(status & E1000_STATUS_FD) {
1674            *duplex = FULL_DUPLEX;
1675            DEBUGOUT("Full Duplex\r\n");
1676        } else {
1677            *duplex = HALF_DUPLEX;
1678            DEBUGOUT(" Half Duplex\r\n");
1679        }
1680    } else {
1681        DEBUGOUT("1000 Mbs, Full Duplex\r\n");
1682        *speed = SPEED_1000;
1683        *duplex = FULL_DUPLEX;
1684    }
1685}
1686
1687/******************************************************************************
1688* Blocks until autoneg completes or times out (~4.5 seconds)
1689*
1690* hw - Struct containing variables accessed by shared code
1691******************************************************************************/
1692int32_t
1693e1000_wait_autoneg(struct e1000_hw *hw)
1694{
1695    uint16_t i;
1696    uint16_t phy_data;
1697
1698    DEBUGFUNC("e1000_wait_autoneg");
1699    DEBUGOUT("Waiting for Auto-Neg to complete.\n");
1700
1701    /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1702    for(i = PHY_AUTO_NEG_TIME; i > 0; i--) {
1703        /* Read the MII Status Register and wait for Auto-Neg
1704         * Complete bit to be set.
1705         */
1706        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
1707            DEBUGOUT("PHY Read Error\n");
1708            return -E1000_ERR_PHY;
1709        }
1710        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
1711            DEBUGOUT("PHY Read Error\n");
1712            return -E1000_ERR_PHY;
1713        }
1714        if(phy_data & MII_SR_AUTONEG_COMPLETE) {
1715            return 0;
1716        }
1717        msec_delay(100);
1718    }
1719    return 0;
1720}
1721
1722/******************************************************************************
1723* Raises the Management Data Clock
1724*
1725* hw - Struct containing variables accessed by shared code
1726* ctrl - Device control register's current value
1727******************************************************************************/
1728static void
1729e1000_raise_mdi_clk(struct e1000_hw *hw,
1730                    uint32_t *ctrl)
1731{
1732    /* Raise the clock input to the Management Data Clock (by setting the MDC
1733     * bit), and then delay 2 microseconds.
1734     */
1735    E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
1736    E1000_WRITE_FLUSH(hw);
1737    udelay(2);
1738}
1739
1740/******************************************************************************
1741* Lowers the Management Data Clock
1742*
1743* hw - Struct containing variables accessed by shared code
1744* ctrl - Device control register's current value
1745******************************************************************************/
1746static void
1747e1000_lower_mdi_clk(struct e1000_hw *hw,
1748                    uint32_t *ctrl)
1749{
1750    /* Lower the clock input to the Management Data Clock (by clearing the MDC
1751     * bit), and then delay 2 microseconds.
1752     */
1753    E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
1754    E1000_WRITE_FLUSH(hw);
1755    udelay(2);
1756}
1757
1758/******************************************************************************
1759* Shifts data bits out to the PHY
1760*
1761* hw - Struct containing variables accessed by shared code
1762* data - Data to send out to the PHY
1763* count - Number of bits to shift out
1764*
1765* Bits are shifted out in MSB to LSB order.
1766******************************************************************************/
1767static void
1768e1000_shift_out_mdi_bits(struct e1000_hw *hw,
1769                         uint32_t data,
1770                         uint16_t count)
1771{
1772    uint32_t ctrl;
1773    uint32_t mask;
1774
1775    /* We need to shift "count" number of bits out to the PHY. So, the value
1776     * in the "data" parameter will be shifted out to the PHY one bit at a 
1777     * time. In order to do this, "data" must be broken down into bits.
1778     */
1779    mask = 0x01;
1780    mask <<= (count - 1);
1781
1782    ctrl = E1000_READ_REG(hw, CTRL);
1783
1784    /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
1785    ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
1786
1787    while(mask) {
1788        /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
1789         * then raising and lowering the Management Data Clock. A "0" is
1790         * shifted out to the PHY by setting the MDIO bit to "0" and then
1791         * raising and lowering the clock.
1792         */
1793        if(data & mask) ctrl |= E1000_CTRL_MDIO;
1794        else ctrl &= ~E1000_CTRL_MDIO;
1795
1796        E1000_WRITE_REG(hw, CTRL, ctrl);
1797        E1000_WRITE_FLUSH(hw);
1798
1799        udelay(2);
1800
1801        e1000_raise_mdi_clk(hw, &ctrl);
1802        e1000_lower_mdi_clk(hw, &ctrl);
1803
1804        mask = mask >> 1;
1805    }
1806}
1807
1808/******************************************************************************
1809* Shifts data bits in from the PHY
1810*
1811* hw - Struct containing variables accessed by shared code
1812*
1813* Bits are shifted in in MSB to LSB order. 
1814******************************************************************************/
1815static uint16_t
1816e1000_shift_in_mdi_bits(struct e1000_hw *hw)
1817{
1818    uint32_t ctrl;
1819    uint16_t data = 0;
1820    uint8_t i;
1821
1822    /* In order to read a register from the PHY, we need to shift in a total
1823     * of 18 bits from the PHY. The first two bit (turnaround) times are used
1824     * to avoid contention on the MDIO pin when a read operation is performed.
1825     * These two bits are ignored by us and thrown away. Bits are "shifted in"
1826     * by raising the input to the Management Data Clock (setting the MDC bit),
1827     * and then reading the value of the MDIO bit.
1828     */ 
1829    ctrl = E1000_READ_REG(hw, CTRL);
1830
1831    /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
1832    ctrl &= ~E1000_CTRL_MDIO_DIR;
1833    ctrl &= ~E1000_CTRL_MDIO;
1834
1835    E1000_WRITE_REG(hw, CTRL, ctrl);
1836    E1000_WRITE_FLUSH(hw);
1837
1838    /* Raise and Lower the clock before reading in the data. This accounts for
1839     * the turnaround bits. The first clock occurred when we clocked out the
1840     * last bit of the Register Address.
1841     */
1842    e1000_raise_mdi_clk(hw, &ctrl);
1843    e1000_lower_mdi_clk(hw, &ctrl);
1844
1845    for(data = 0, i = 0; i < 16; i++) {
1846        data = data << 1;
1847        e1000_raise_mdi_clk(hw, &ctrl);
1848        ctrl = E1000_READ_REG(hw, CTRL);
1849        /* Check to see if we shifted in a "1". */
1850        if(ctrl & E1000_CTRL_MDIO) data |= 1;
1851        e1000_lower_mdi_clk(hw, &ctrl);
1852    }
1853
1854    e1000_raise_mdi_clk(hw, &ctrl);
1855    e1000_lower_mdi_clk(hw, &ctrl);
1856
1857    return data;
1858}
1859
1860/*****************************************************************************
1861* Reads the value from a PHY register
1862*
1863* hw - Struct containing variables accessed by shared code
1864* reg_addr - address of the PHY register to read
1865******************************************************************************/
1866int32_t
1867e1000_read_phy_reg(struct e1000_hw *hw,
1868                   uint32_t reg_addr,
1869                   uint16_t *phy_data)
1870{
1871    uint32_t i;
1872    uint32_t mdic = 0;
1873    const uint32_t phy_addr = 1;
1874
1875    DEBUGFUNC("e1000_read_phy_reg");
1876
1877    if(reg_addr > MAX_PHY_REG_ADDRESS) {
1878        DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
1879        return -E1000_ERR_PARAM;
1880    }
1881
1882    if(hw->mac_type > e1000_82543) {
1883        /* Set up Op-code, Phy Address, and register address in the MDI
1884         * Control register.  The MAC will take care of interfacing with the
1885         * PHY to retrieve the desired data.
1886         */
1887        mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
1888                (phy_addr << E1000_MDIC_PHY_SHIFT) | 
1889                (E1000_MDIC_OP_READ));
1890
1891        E1000_WRITE_REG(hw, MDIC, mdic);
1892
1893        /* Poll the ready bit to see if the MDI read completed */
1894        for(i = 0; i < 64; i++) {
1895            udelay(10);
1896            mdic = E1000_READ_REG(hw, MDIC);
1897            if(mdic & E1000_MDIC_READY) break;
1898        }
1899        if(!(mdic & E1000_MDIC_READY)) {
1900            DEBUGOUT("MDI Read did not complete\n");
1901            return -E1000_ERR_PHY;
1902        }
1903        if(mdic & E1000_MDIC_ERROR) {
1904            DEBUGOUT("MDI Error\n");
1905            return -E1000_ERR_PHY;
1906        }
1907        *phy_data = (uint16_t) mdic;
1908    } else {
1909        /* We must first send a preamble through the MDIO pin to signal the
1910         * beginning of an MII instruction.  This is done by sending 32
1911         * consecutive "1" bits.
1912         */
1913        e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
1914
1915        /* Now combine the next few fields that are required for a read
1916         * operation.  We use this method instead of calling the
1917         * e1000_shift_out_mdi_bits routine five different times. The format of
1918         * a MII read instruction consists of a shift out of 14 bits and is
1919         * defined as follows:
1920         *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
1921         * followed by a shift in of 18 bits.  This first two bits shifted in
1922         * are TurnAround bits used to avoid contention on the MDIO pin when a
1923         * READ operation is performed.  These two bits are thrown away
1924         * followed by a shift in of 16 bits which contains the desired data.
1925         */
1926        mdic = ((reg_addr) | (phy_addr << 5) | 
1927                (PHY_OP_READ << 10) | (PHY_SOF << 12));
1928
1929        e1000_shift_out_mdi_bits(hw, mdic, 14);
1930
1931        /* Now that we've shifted out the read command to the MII, we need to
1932         * "shift in" the 16-bit value (18 total bits) of the requested PHY
1933         * register address.
1934         */
1935        *phy_data = e1000_shift_in_mdi_bits(hw);
1936    }
1937    return 0;
1938}
1939
1940/******************************************************************************
1941* Writes a value to a PHY register
1942*
1943* hw - Struct containing variables accessed by shared code
1944* reg_addr - address of the PHY register to write
1945* data - data to write to the PHY
1946******************************************************************************/
1947int32_t
1948e1000_write_phy_reg(struct e1000_hw *hw,
1949                    uint32_t reg_addr,
1950                    uint16_t phy_data)
1951{
1952    uint32_t i;
1953    uint32_t mdic = 0;
1954    const uint32_t phy_addr = 1;
1955
1956    DEBUGFUNC("e1000_write_phy_reg");
1957
1958    if(reg_addr > MAX_PHY_REG_ADDRESS) {
1959        DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
1960        return -E1000_ERR_PARAM;
1961    }
1962
1963    if(hw->mac_type > e1000_82543) {
1964        /* Set up Op-code, Phy Address, register address, and data intended
1965         * for the PHY register in the MDI Control register.  The MAC will take
1966         * care of interfacing with the PHY to send the desired data.
1967         */
1968        mdic = (((uint32_t) phy_data) |
1969                (reg_addr << E1000_MDIC_REG_SHIFT) |
1970                (phy_addr << E1000_MDIC_PHY_SHIFT) | 
1971                (E1000_MDIC_OP_WRITE));
1972
1973        E1000_WRITE_REG(hw, MDIC, mdic);
1974
1975        /* Poll the ready bit to see if the MDI read completed */
1976        for(i = 0; i < 64; i++) {
1977            udelay(10);
1978            mdic = E1000_READ_REG(hw, MDIC);
1979            if(mdic & E1000_MDIC_READY) break;
1980        }
1981        if(!(mdic & E1000_MDIC_READY)) {
1982            DEBUGOUT("MDI Write did not complete\n");
1983            return -E1000_ERR_PHY;
1984        }
1985    } else {
1986        /* We'll need to use the SW defined pins to shift the write command
1987         * out to the PHY. We first send a preamble to the PHY to signal the
1988         * beginning of the MII instruction.  This is done by sending 32 
1989         * consecutive "1" bits.
1990         */
1991        e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
1992
1993        /* Now combine the remaining required fields that will indicate a 
1994         * write operation. We use this method instead of calling the
1995         * e1000_shift_out_mdi_bits routine for each field in the command. The
1996         * format of a MII write instruction is as follows:
1997         * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
1998         */
1999        mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
2000                (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
2001        mdic <<= 16;
2002        mdic |= (uint32_t) phy_data;
2003
2004        e1000_shift_out_mdi_bits(hw, mdic, 32);
2005    }
2006    return 0;
2007}
2008
2009/******************************************************************************
2010* Returns the PHY to the power-on reset state
2011*
2012* hw - Struct containing variables accessed by shared code
2013******************************************************************************/
2014void
2015e1000_phy_hw_reset(struct e1000_hw *hw)
2016{
2017    uint32_t ctrl;
2018    uint32_t ctrl_ext;
2019
2020    DEBUGFUNC("e1000_phy_hw_reset");
2021
2022    DEBUGOUT("Resetting Phy...\n");
2023
2024    if(hw->mac_type > e1000_82543) {
2025        /* Read the device control register and assert the E1000_CTRL_PHY_RST
2026         * bit. Then, take it out of reset.
2027         */
2028        ctrl = E1000_READ_REG(hw, CTRL);
2029        E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
2030        E1000_WRITE_FLUSH(hw);
2031        msec_delay(10);
2032        E1000_WRITE_REG(hw, CTRL, ctrl);
2033        E1000_WRITE_FLUSH(hw);
2034    } else {
2035        /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
2036         * bit to put the PHY into reset. Then, take it out of reset.
2037         */
2038        ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2039        ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
2040        ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
2041        E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2042        E1000_WRITE_FLUSH(hw);
2043        msec_delay(10);
2044        ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
2045        E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2046        E1000_WRITE_FLUSH(hw);
2047    }
2048    udelay(150);
2049}
2050
2051/******************************************************************************
2052* Resets the PHY
2053*
2054* hw - Struct containing variables accessed by shared code
2055*
2056* Sets bit 15 of the MII Control regiser
2057******************************************************************************/
2058int32_t
2059e1000_phy_reset(struct e1000_hw *hw)
2060{
2061    uint16_t phy_data;
2062
2063    DEBUGFUNC("e1000_phy_reset");
2064
2065    if(e1000_read_phy_reg(hw, PHY_CTRL, &phy_data) < 0) {
2066        DEBUGOUT("PHY Read Error\n");
2067        return -E1000_ERR_PHY;
2068    }
2069    phy_data |= MII_CR_RESET;
2070    if(e1000_write_phy_reg(hw, PHY_CTRL, phy_data) < 0) {
2071        DEBUGOUT("PHY Write Error\n");
2072        return -E1000_ERR_PHY;
2073    }
2074    udelay(1);
2075    return 0;
2076}
2077
2078/******************************************************************************
2079* Probes the expected PHY address for known PHY IDs
2080*
2081* hw - Struct containing variables accessed by shared code
2082******************************************************************************/
2083int32_t
2084e1000_detect_gig_phy(struct e1000_hw *hw)
2085{
2086    uint16_t phy_id_high, phy_id_low;
2087    boolean_t match = FALSE;
2088
2089    DEBUGFUNC("e1000_detect_gig_phy");
2090
2091    /* Read the PHY ID Registers to identify which PHY is onboard. */
2092    if(e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high) < 0) {
2093        DEBUGOUT("PHY Read Error\n");
2094        return -E1000_ERR_PHY;
2095    }
2096    hw->phy_id = (uint32_t) (phy_id_high << 16);
2097    udelay(2);
2098    if(e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low) < 0) {
2099        DEBUGOUT("PHY Read Error\n");
2100        return -E1000_ERR_PHY;
2101    }
2102    hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
2103    
2104    switch(hw->mac_type) {
2105    case e1000_82543:
2106        if(hw->phy_id == M88E1000_E_PHY_ID) match = TRUE;
2107        break;
2108    case e1000_82544:
2109        if(hw->phy_id == M88E1000_I_PHY_ID) match = TRUE;
2110        break;
2111    case e1000_82540:
2112    case e1000_82545:
2113    case e1000_82546:
2114        if(hw->phy_id == M88E1011_I_PHY_ID) match = TRUE;
2115        break;
2116    default:
2117        DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
2118        return -E1000_ERR_CONFIG;
2119    }
2120    if(match) {
2121        DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
2122        return 0;
2123    }
2124    DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
2125    return -E1000_ERR_PHY;
2126}
2127
2128/******************************************************************************
2129* Resets the PHY's DSP
2130*
2131* hw - Struct containing variables accessed by shared code
2132******************************************************************************/
2133static int32_t
2134e1000_phy_reset_dsp(struct e1000_hw *hw)
2135{
2136    int32_t ret_val = -E1000_ERR_PHY;
2137    DEBUGFUNC("e1000_phy_reset_dsp");
2138    
2139    do {
2140        if(e1000_write_phy_reg(hw, 29, 0x001d) < 0) break;
2141        if(e1000_write_phy_reg(hw, 30, 0x00c1) < 0) break;
2142        if(e1000_write_phy_reg(hw, 30, 0x0000) < 0) break;
2143        ret_val = 0;
2144    } while(0);
2145
2146    if(ret_val < 0) DEBUGOUT("PHY Write Error\n");
2147    return ret_val;
2148}
2149
2150/******************************************************************************
2151* Get PHY information from various PHY registers
2152*
2153* hw - Struct containing variables accessed by shared code
2154* phy_info - PHY information structure
2155******************************************************************************/
2156int32_t
2157e1000_phy_get_info(struct e1000_hw *hw,
2158                   struct e1000_phy_info *phy_info)
2159{
2160    int32_t ret_val = -E1000_ERR_PHY;
2161    uint16_t phy_data;
2162
2163    DEBUGFUNC("e1000_phy_get_info");
2164
2165    phy_info->cable_length = e1000_cable_length_undefined;
2166    phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
2167    phy_info->cable_polarity = e1000_rev_polarity_undefined;
2168    phy_info->polarity_correction = e1000_polarity_reversal_undefined;
2169    phy_info->mdix_mode = e1000_auto_x_mode_undefined;
2170    phy_info->local_rx = e1000_1000t_rx_status_undefined;
2171    phy_info->remote_rx = e1000_1000t_rx_status_undefined;
2172
2173    if(hw->media_type != e1000_media_type_copper) {
2174        DEBUGOUT("PHY info is only valid for copper media\n");
2175        return -E1000_ERR_CONFIG;
2176    }
2177
2178    do {
2179        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) break;
2180        if(e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) break;
2181        if((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
2182            DEBUGOUT("PHY info is only valid if link is up\n");
2183            return -E1000_ERR_CONFIG;
2184        }
2185
2186        if(e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data) < 0)
2187            break;
2188        phy_info->extended_10bt_distance =
2189            (phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
2190            M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT;
2191        phy_info->polarity_correction =
2192            (phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
2193            M88E1000_PSCR_POLARITY_REVERSAL_SHIFT;
2194
2195        if(e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0)
2196            break;
2197        phy_info->cable_polarity = (phy_data & M88E1000_PSSR_REV_POLARITY) >>
2198            M88E1000_PSSR_REV_POLARITY_SHIFT;
2199        phy_info->mdix_mode = (phy_data & M88E1000_PSSR_MDIX) >>
2200            M88E1000_PSSR_MDIX_SHIFT;
2201        if(phy_data & M88E1000_PSSR_1000MBS) {
2202            /* Cable Length Estimation and Local/Remote Receiver Informatoion
2203             * are only valid at 1000 Mbps
2204             */
2205            phy_info->cable_length = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
2206                                      M88E1000_PSSR_CABLE_LENGTH_SHIFT);
2207            if(e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data) < 0) 
2208                break;
2209            phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
2210                SR_1000T_LOCAL_RX_STATUS_SHIFT;
2211            phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
2212                SR_1000T_REMOTE_RX_STATUS_SHIFT;
2213        }
2214        ret_val = 0;
2215    } while(0);
2216
2217    if(ret_val < 0) DEBUGOUT("PHY Read Error\n");
2218    return ret_val;
2219}
2220
2221int32_t
2222e1000_validate_mdi_setting(struct e1000_hw *hw)
2223{
2224    DEBUGFUNC("e1000_validate_mdi_settings");
2225
2226    if(!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
2227        DEBUGOUT("Invalid MDI setting detected\n");
2228        hw->mdix = 1;
2229        return -E1000_ERR_CONFIG;
2230    }
2231    return 0;
2232}
2233
2234/******************************************************************************
2235 * Raises the EEPROM's clock input.
2236 *
2237 * hw - Struct containing variables accessed by shared code
2238 * eecd - EECD's current value
2239 *****************************************************************************/
2240static void
2241e1000_raise_ee_clk(struct e1000_hw *hw,
2242                   uint32_t *eecd)
2243{
2244    /* Raise the clock input to the EEPROM (by setting the SK bit), and then
2245     * wait 50 microseconds.
2246     */
2247    *eecd = *eecd | E1000_EECD_SK;
2248    E1000_WRITE_REG(hw, EECD, *eecd);
2249    E1000_WRITE_FLUSH(hw);
2250    udelay(50);
2251}
2252
2253/******************************************************************************
2254 * Lowers the EEPROM's clock input.
2255 *
2256 * hw - Struct containing variables accessed by shared code 
2257 * eecd - EECD's current value
2258 *****************************************************************************/
2259static void
2260e1000_lower_ee_clk(struct e1000_hw *hw,
2261                   uint32_t *eecd)
2262{
2263    /* Lower the clock input to the EEPROM (by clearing the SK bit), and then 
2264     * wait 50 microseconds. 
2265     */
2266    *eecd = *eecd & ~E1000_EECD_SK;
2267    E1000_WRITE_REG(hw, EECD, *eecd);
2268    E1000_WRITE_FLUSH(hw);
2269    udelay(50);
2270}
2271
2272/******************************************************************************
2273 * Shift data bits out to the EEPROM.
2274 *
2275 * hw - Struct containing variables accessed by shared code
2276 * data - data to send to the EEPROM
2277 * count - number of bits to shift out
2278 *****************************************************************************/
2279static void
2280e1000_shift_out_ee_bits(struct e1000_hw *hw,
2281                        uint16_t data,
2282                        uint16_t count)
2283{
2284    uint32_t eecd;
2285    uint32_t mask;
2286
2287    /* We need to shift "count" bits out to the EEPROM. So, value in the
2288     * "data" parameter will be shifted out to the EEPROM one bit at a time.
2289     * In order to do this, "data" must be broken down into bits. 
2290     */
2291    mask = 0x01 << (count - 1);
2292    eecd = E1000_READ_REG(hw, EECD);
2293    eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
2294    do {
2295        /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
2296         * and then raising and then lowering the clock (the SK bit controls
2297         * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
2298         * by setting "DI" to "0" and then raising and then lowering the clock.
2299         */
2300        eecd &= ~E1000_EECD_DI;
2301
2302        if(data & mask)
2303            eecd |= E1000_EECD_DI;
2304
2305        E1000_WRITE_REG(hw, EECD, eecd);
2306        E1000_WRITE_FLUSH(hw);
2307
2308        udelay(50);
2309
2310        e1000_raise_ee_clk(hw, &eecd);
2311        e1000_lower_ee_clk(hw, &eecd);
2312
2313        mask = mask >> 1;
2314
2315    } while(mask);
2316
2317    /* We leave the "DI" bit set to "0" when we leave this routine. */
2318    eecd &= ~E1000_EECD_DI;
2319    E1000_WRITE_REG(hw, EECD, eecd);
2320}
2321
2322/******************************************************************************
2323 * Shift data bits in from the EEPROM
2324 *
2325 * hw - Struct containing variables accessed by shared code
2326 *****************************************************************************/
2327static uint16_t
2328e1000_shift_in_ee_bits(struct e1000_hw *hw)
2329{
2330    uint32_t eecd;
2331    uint32_t i;
2332    uint16_t data;
2333
2334    /* In order to read a register from the EEPROM, we need to shift 16 bits 
2335     * in from the EEPROM. Bits are "shifted in" by raising the clock input to
2336     * the EEPROM (setting the SK bit), and then reading the value of the "DO"
2337     * bit.  During this "shifting in" process the "DI" bit should always be 
2338     * clear..
2339     */
2340
2341    eecd = E1000_READ_REG(hw, EECD);
2342
2343    eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
2344    data = 0;
2345
2346    for(i = 0; i < 16; i++) {
2347        data = data << 1;
2348        e1000_raise_ee_clk(hw, &eecd);
2349
2350        eecd = E1000_READ_REG(hw, EECD);
2351
2352        eecd &= ~(E1000_EECD_DI);
2353        if(eecd & E1000_EECD_DO)
2354            data |= 1;
2355
2356        e1000_lower_ee_clk(hw, &eecd);
2357    }
2358
2359    return data;
2360}
2361
2362/******************************************************************************
2363 * Prepares EEPROM for access
2364 *
2365 * hw - Struct containing variables accessed by shared code
2366 *
2367 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This 
2368 * function should be called before issuing a command to the EEPROM.
2369 *****************************************************************************/
2370static void
2371e1000_setup_eeprom(struct e1000_hw *hw)
2372{
2373    uint32_t eecd;
2374
2375    eecd = E1000_READ_REG(hw, EECD);
2376
2377    /* Clear SK and DI */
2378    eecd &= ~(E1000_EECD_SK | E1000_EECD_DI);
2379    E1000_WRITE_REG(hw, EECD, eecd);
2380
2381    /* Set CS */
2382    eecd |= E1000_EECD_CS;
2383    E1000_WRITE_REG(hw, EECD, eecd);
2384}
2385
2386/******************************************************************************
2387 * Returns EEPROM to a "standby" state
2388 * 
2389 * hw - Struct containing variables accessed by shared code
2390 *****************************************************************************/
2391static void
2392e1000_standby_eeprom(struct e1000_hw *hw)
2393{
2394    uint32_t eecd;
2395
2396    eecd = E1000_READ_REG(hw, EECD);
2397
2398    /* Deselct EEPROM */
2399    eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
2400    E1000_WRITE_REG(hw, EECD, eecd);
2401    E1000_WRITE_FLUSH(hw);
2402    udelay(50);
2403
2404    /* Clock high */
2405    eecd |= E1000_EECD_SK;
2406    E1000_WRITE_REG(hw, EECD, eecd);
2407    E1000_WRITE_FLUSH(hw);
2408    udelay(50);
2409
2410    /* Select EEPROM */
2411    eecd |= E1000_EECD_CS;
2412    E1000_WRITE_REG(hw, EECD, eecd);
2413    E1000_WRITE_FLUSH(hw);
2414    udelay(50);
2415
2416    /* Clock low */
2417    eecd &= ~E1000_EECD_SK;
2418    E1000_WRITE_REG(hw, EECD, eecd);
2419    E1000_WRITE_FLUSH(hw);
2420    udelay(50);
2421}
2422
2423/******************************************************************************
2424 * Raises then lowers the EEPROM's clock pin
2425 *
2426 * hw - Struct containing variables accessed by shared code
2427 *****************************************************************************/
2428static void
2429e1000_clock_eeprom(struct e1000_hw *hw)
2430{
2431    uint32_t eecd;
2432
2433    eecd = E1000_READ_REG(hw, EECD);
2434
2435    /* Rising edge of clock */
2436    eecd |= E1000_EECD_SK;
2437    E1000_WRITE_REG(hw, EECD, eecd);
2438    E1000_WRITE_FLUSH(hw);
2439    udelay(50);
2440
2441    /* Falling edge of clock */
2442    eecd &= ~E1000_EECD_SK;
2443    E1000_WRITE_REG(hw, EECD, eecd);
2444    E1000_WRITE_FLUSH(hw);
2445    udelay(50);
2446}
2447
2448/******************************************************************************
2449 * Terminates a command by lowering the EEPROM's chip select pin
2450 *
2451 * hw - Struct containing variables accessed by shared code
2452 *****************************************************************************/
2453static void
2454e1000_cleanup_eeprom(struct e1000_hw *hw)
2455{
2456    uint32_t eecd;
2457
2458    eecd = E1000_READ_REG(hw, EECD);
2459
2460    eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
2461
2462    E1000_WRITE_REG(hw, EECD, eecd);
2463
2464    e1000_clock_eeprom(hw);
2465}
2466
2467/******************************************************************************
2468 * Reads a 16 bit word from the EEPROM.
2469 *
2470 * hw - Struct containing variables accessed by shared code
2471 * offset - offset of  word in the EEPROM to read
2472 * data - word read from the EEPROM 
2473 *****************************************************************************/
2474int32_t
2475e1000_read_eeprom(struct e1000_hw *hw,
2476                  uint16_t offset,
2477                  uint16_t *data)
2478{
2479    uint32_t eecd;
2480    uint32_t i = 0;
2481    boolean_t large_eeprom = FALSE;
2482
2483    DEBUGFUNC("e1000_read_eeprom");
2484
2485    /* Request EEPROM Access */
2486    if(hw->mac_type > e1000_82544) {
2487        eecd = E1000_READ_REG(hw, EECD);
2488        if(eecd & E1000_EECD_SIZE) large_eeprom = TRUE;
2489        eecd |= E1000_EECD_REQ;
2490        E1000_WRITE_REG(hw, EECD, eecd);
2491        eecd = E1000_READ_REG(hw, EECD);
2492        while((!(eecd & E1000_EECD_GNT)) && (i < 100)) {
2493            i++;
2494            udelay(5);
2495            eecd = E1000_READ_REG(hw, EECD);
2496        }
2497        if(!(eecd & E1000_EECD_GNT)) {
2498            eecd &= ~E1000_EECD_REQ;
2499            E1000_WRITE_REG(hw, EECD, eecd);
2500            DEBUGOUT("Could not acquire EEPROM grant\n");
2501            return -E1000_ERR_EEPROM;
2502        }
2503    }
2504
2505    /*  Prepare the EEPROM for reading  */
2506    e1000_setup_eeprom(hw);
2507
2508    /*  Send the READ command (opcode + addr)  */
2509    e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE, 3);
2510    if(large_eeprom) {
2511        /* If we have a 256 word EEPROM, there are 8 address bits */
2512        e1000_shift_out_ee_bits(hw, offset, 8);
2513    } else {
2514        /* If we have a 64 word EEPROM, there are 6 address bits */
2515        e1000_shift_out_ee_bits(hw, offset, 6);
2516    }
2517
2518    /* Read the data */
2519    *data = e1000_shift_in_ee_bits(hw);
2520
2521    /* End this read operation */
2522    e1000_standby_eeprom(hw);
2523
2524    /* Stop requesting EEPROM access */
2525    if(hw->mac_type > e1000_82544) {
2526        eecd = E1000_READ_REG(hw, EECD);
2527        eecd &= ~E1000_EECD_REQ;
2528        E1000_WRITE_REG(hw, EECD, eecd);
2529    }
2530
2531    return 0;
2532}
2533
2534/******************************************************************************
2535 * Verifies that the EEPROM has a valid checksum
2536 * 
2537 * hw - Struct containing variables accessed by shared code
2538 *
2539 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
2540 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
2541 * valid.
2542 *****************************************************************************/
2543int32_t
2544e1000_validate_eeprom_checksum(struct e1000_hw *hw)
2545{
2546    uint16_t checksum = 0;
2547    uint16_t i, eeprom_data;
2548
2549    DEBUGFUNC("e1000_validate_eeprom_checksum");
2550
2551    for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
2552        if(e1000_read_eeprom(hw, i, &eeprom_data) < 0) {
2553            DEBUGOUT("EEPROM Read Error\n");
2554            return -E1000_ERR_EEPROM;
2555        }
2556        checksum += eeprom_data;
2557    }
2558
2559    if(checksum == (uint16_t) EEPROM_SUM) {
2560        return 0;
2561    } else {
2562        DEBUGOUT("EEPROM Checksum Invalid\n");    
2563        return -E1000_ERR_EEPROM;
2564    }
2565}
2566
2567/******************************************************************************
2568 * Calculates the EEPROM checksum and writes it to the EEPROM
2569 *
2570 * hw - Struct containing variables accessed by shared code
2571 *
2572 * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
2573 * Writes the difference to word offset 63 of the EEPROM.
2574 *****************************************************************************/
2575int32_t
2576e1000_update_eeprom_checksum(struct e1000_hw *hw)
2577{
2578    uint16_t checksum = 0;
2579    uint16_t i, eeprom_data;
2580
2581    DEBUGFUNC("e1000_update_eeprom_checksum");
2582
2583    for(i = 0; i < EEPROM_CHECKSUM_REG; i++) {
2584        if(e1000_read_eeprom(hw, i, &eeprom_data) < 0) {
2585            DEBUGOUT("EEPROM Read Error\n");
2586            return -E1000_ERR_EEPROM;
2587        }
2588        checksum += eeprom_data;
2589    }
2590    checksum = (uint16_t) EEPROM_SUM - checksum;
2591    if(e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, checksum) < 0) {
2592        DEBUGOUT("EEPROM Write Error\n");
2593        return -E1000_ERR_EEPROM;
2594    }
2595    return 0;
2596}
2597
2598/******************************************************************************
2599 * Writes a 16 bit word to a given offset in the EEPROM.
2600 *
2601 * hw - Struct containing variables accessed by shared code
2602 * offset - offset within the EEPROM to be written to
2603 * data - 16 bit word to be writen to the EEPROM
2604 *
2605 * If e1000_update_eeprom_checksum is not called after this function, the 
2606 * EEPROM will most likely contain an invalid checksum.
2607 *****************************************************************************/
2608int32_t
2609e1000_write_eeprom(struct e1000_hw *hw,
2610                   uint16_t offset,
2611                   uint16_t data)
2612{
2613    uint32_t eecd;
2614    uint32_t i = 0;
2615    int32_t status = 0;
2616    boolean_t large_eeprom = FALSE;
2617
2618    DEBUGFUNC("e1000_write_eeprom");
2619
2620    /* Request EEPROM Access */
2621    if(hw->mac_type > e1000_82544) {
2622        eecd = E1000_READ_REG(hw, EECD);
2623        if(eecd & E1000_EECD_SIZE) large_eeprom = TRUE;
2624        eecd |= E1000_EECD_REQ;
2625        E1000_WRITE_REG(hw, EECD, eecd);
2626        eecd = E1000_READ_REG(hw, EECD);
2627        while((!(eecd & E1000_EECD_GNT)) && (i < 100)) {
2628            i++;
2629            udelay(5);
2630            eecd = E1000_READ_REG(hw, EECD);
2631        }
2632        if(!(eecd & E1000_EECD_GNT)) {
2633            eecd &= ~E1000_EECD_REQ;
2634            E1000_WRITE_REG(hw, EECD, eecd);
2635            DEBUGOUT("Could not acquire EEPROM grant\n");
2636            return -E1000_ERR_EEPROM;
2637        }
2638    }
2639
2640    /* Prepare the EEPROM for writing  */
2641    e1000_setup_eeprom(hw);
2642
2643    /* Send the 9-bit (or 11-bit on large EEPROM) EWEN (write enable) command
2644     * to the EEPROM (5-bit opcode plus 4/6-bit dummy). This puts the EEPROM
2645     * into write/erase mode. 
2646     */
2647    e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE, 5);
2648    if(large_eeprom) 
2649        e1000_shift_out_ee_bits(hw, 0, 6);
2650    else
2651        e1000_shift_out_ee_bits(hw, 0, 4);
2652
2653    /* Prepare the EEPROM */
2654    e1000_standby_eeprom(hw);
2655
2656    /* Send the Write command (3-bit opcode + addr) */
2657    e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE, 3);
2658    if(large_eeprom) 
2659        /* If we have a 256 word EEPROM, there are 8 address bits */
2660        e1000_shift_out_ee_bits(hw, offset, 8);
2661    else
2662        /* If we have a 64 word EEPROM, there are 6 address bits */
2663        e1000_shift_out_ee_bits(hw, offset, 6);
2664
2665    /* Send the data */
2666    e1000_shift_out_ee_bits(hw, data, 16);
2667
2668    /* Toggle the CS line.  This in effect tells to EEPROM to actually execute 
2669     * the command in question.
2670     */
2671    e1000_standby_eeprom(hw);
2672
2673    /* Now read DO repeatedly until is high (equal to '1').  The EEEPROM will
2674     * signal that the command has been completed by raising the DO signal.
2675     * If DO does not go high in 10 milliseconds, then error out.
2676     */
2677    for(i = 0; i < 200; i++) {
2678        eecd = E1000_READ_REG(hw, EECD);
2679        if(eecd & E1000_EECD_DO) break;
2680        udelay(50);
2681    }
2682    if(i == 200) {
2683        DEBUGOUT("EEPROM Write did not complete\n");
2684        status = -E1000_ERR_EEPROM;
2685    }
2686
2687    /* Recover from write */
2688    e1000_standby_eeprom(hw);
2689
2690    /* Send the 9-bit (or 11-bit on large EEPROM) EWDS (write disable) command
2691     * to the EEPROM (5-bit opcode plus 4/6-bit dummy). This takes the EEPROM
2692     * out of write/erase mode.
2693     */
2694    e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE, 5);
2695    if(large_eeprom) 
2696        e1000_shift_out_ee_bits(hw, 0, 6);
2697    else
2698        e1000_shift_out_ee_bits(hw, 0, 4);
2699
2700    /* Done with writing */
2701    e1000_cleanup_eeprom(hw);
2702
2703    /* Stop requesting EEPROM access */
2704    if(hw->mac_type > e1000_82544) {
2705        eecd = E1000_READ_REG(hw, EECD);
2706        eecd &= ~E1000_EECD_REQ;
2707        E1000_WRITE_REG(hw, EECD, eecd);
2708    }
2709
2710    return status;
2711}
2712
2713/******************************************************************************
2714 * Reads the adapter's part number from the EEPROM
2715 *
2716 * hw - Struct containing variables accessed by shared code
2717 * part_num - Adapter's part number
2718 *****************************************************************************/
2719int32_t
2720e1000_read_part_num(struct e1000_hw *hw,
2721                    uint32_t *part_num)
2722{
2723    uint16_t offset = EEPROM_PBA_BYTE_1;
2724    uint16_t eeprom_data;
2725
2726    DEBUGFUNC("e1000_read_part_num");
2727
2728    /* Get word 0 from EEPROM */
2729    if(e1000_read_eeprom(hw, offset, &eeprom_data) < 0) {
2730        DEBUGOUT("EEPROM Read Error\n");
2731        return -E1000_ERR_EEPROM;
2732    }
2733    /* Save word 0 in upper half of part_num */
2734    *part_num = (uint32_t) (eeprom_data << 16);
2735
2736    /* Get word 1 from EEPROM */
2737    if(e1000_read_eeprom(hw, ++offset, &eeprom_data) < 0) {
2738        DEBUGOUT("EEPROM Read Error\n");
2739        return -E1000_ERR_EEPROM;
2740    }
2741    /* Save word 1 in lower half of part_num */
2742    *part_num |= eeprom_data;
2743
2744    return 0;
2745}
2746
2747/******************************************************************************
2748 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
2749 * second function of dual function devices
2750 *
2751 * hw - Struct containing variables accessed by shared code
2752 *****************************************************************************/
2753int32_t
2754e1000_read_mac_addr(struct e1000_hw * hw)
2755{
2756    uint16_t offset;
2757    uint16_t eeprom_data, i;
2758
2759    DEBUGFUNC("e1000_read_mac_addr");
2760
2761    for(i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
2762        offset = i >> 1;
2763        if(e1000_read_eeprom(hw, offset, &eeprom_data) < 0) {
2764            DEBUGOUT("EEPROM Read Error\n");
2765            return -E1000_ERR_EEPROM;
2766        }
2767        hw->perm_mac_addr[i] = (uint8_t) (eeprom_data & 0x00FF);
2768        hw->perm_mac_addr[i+1] = (uint8_t) (eeprom_data >> 8);
2769    }
2770    if((hw->mac_type == e1000_82546) &&
2771       (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
2772        if(hw->perm_mac_addr[5] & 0x01)
2773            hw->perm_mac_addr[5] &= ~(0x01);
2774        else
2775            hw->perm_mac_addr[5] |= 0x01;
2776    }
2777    for(i = 0; i < NODE_ADDRESS_SIZE; i++)
2778        hw->mac_addr[i] = hw->perm_mac_addr[i];
2779    return 0;
2780}
2781
2782/******************************************************************************
2783 * Initializes receive address filters.
2784 *
2785 * hw - Struct containing variables accessed by shared code 
2786 *
2787 * Places the MAC address in receive address register 0 and clears the rest
2788 * of the receive addresss registers. Clears the multicast table. Assumes
2789 * the receiver is in reset when the routine is called.
2790 *****************************************************************************/
2791void
2792e1000_init_rx_addrs(struct e1000_hw *hw)
2793{
2794    uint32_t i;
2795    uint32_t addr_low;
2796    uint32_t addr_high;
2797
2798    DEBUGFUNC("e1000_init_rx_addrs");
2799
2800    /* Setup the receive address. */
2801    DEBUGOUT("Programming MAC Address into RAR[0]\n");
2802    addr_low = (hw->mac_addr[0] |
2803                (hw->mac_addr[1] << 8) |
2804                (hw->mac_addr[2] << 16) | (hw->mac_addr[3] << 24));
2805
2806    addr_high = (hw->mac_addr[4] |
2807                 (hw->mac_addr[5] << 8) | E1000_RAH_AV);
2808
2809    E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
2810    E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
2811
2812    /* Zero out the other 15 receive addresses. */
2813    DEBUGOUT("Clearing RAR[1-15]\n");
2814    for(i = 1; i < E1000_RAR_ENTRIES; i++) {
2815        E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
2816        E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
2817    }
2818}
2819
2820/******************************************************************************
2821 * Updates the MAC's list of multicast addresses.
2822 *
2823 * hw - Struct containing variables accessed by shared code
2824 * mc_addr_list - the list of new multicast addresses
2825 * mc_addr_count - number of addresses
2826 * pad - number of bytes between addresses in the list
2827 *
2828 * The given list replaces any existing list. Clears the last 15 receive
2829 * address registers and the multicast table. Uses receive address registers
2830 * for the first 15 multicast addresses, and hashes the rest into the 
2831 * multicast table.
2832 *****************************************************************************/
2833void
2834e1000_mc_addr_list_update(struct e1000_hw *hw,
2835                          uint8_t *mc_addr_list,
2836                          uint32_t mc_addr_count,
2837                          uint32_t pad)
2838{
2839    uint32_t hash_value;
2840    uint32_t i;
2841    uint32_t rar_used_count = 1; /* RAR[0] is used for our MAC address */
2842
2843    DEBUGFUNC("e1000_mc_addr_list_update");
2844
2845    /* Set the new number of MC addresses that we are being requested to use. */
2846    hw->num_mc_addrs = mc_addr_count;
2847
2848    /* Clear RAR[1-15] */
2849    DEBUGOUT(" Clearing RAR[1-15]\n");
2850    for(i = rar_used_count; i < E1000_RAR_ENTRIES; i++) {
2851        E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
2852        E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
2853    }
2854
2855    /* Clear the MTA */
2856    DEBUGOUT(" Clearing MTA\n");
2857    for(i = 0; i < E1000_NUM_MTA_REGISTERS; i++) {
2858        E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
2859    }
2860
2861    /* Add the new addresses */
2862    for(i = 0; i < mc_addr_count; i++) {
2863        DEBUGOUT(" Adding the multicast addresses:\n");
2864        DEBUGOUT7(" MC Addr #%d =%.2X %.2X %.2X %.2X %.2X %.2X\n", i,
2865                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad)],
2866                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 1],
2867                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 2],
2868                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 3],
2869                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 4],
2870                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 5]);
2871
2872        hash_value = e1000_hash_mc_addr(hw,
2873                                        mc_addr_list +
2874                                        (i * (ETH_LENGTH_OF_ADDRESS + pad)));
2875
2876        DEBUGOUT1(" Hash value = 0x%03X\n", hash_value);
2877
2878        /* Place this multicast address in the RAR if there is room, *
2879         * else put it in the MTA            
2880         */
2881        if(rar_used_count < E1000_RAR_ENTRIES) {
2882            e1000_rar_set(hw,
2883                          mc_addr_list + (i * (ETH_LENGTH_OF_ADDRESS + pad)),
2884                          rar_used_count);
2885            rar_used_count++;
2886        } else {
2887            e1000_mta_set(hw, hash_value);
2888        }
2889    }
2890    DEBUGOUT("MC Update Complete\n");
2891}
2892
2893/******************************************************************************
2894 * Hashes an address to determine its location in the multicast table
2895 *
2896 * hw - Struct containing variables accessed by shared code
2897 * mc_addr - the multicast address to hash 
2898 *****************************************************************************/
2899uint32_t
2900e1000_hash_mc_addr(struct e1000_hw *hw,
2901                   uint8_t *mc_addr)
2902{
2903    uint32_t hash_value = 0;
2904
2905    /* The portion of the address that is used for the hash table is
2906     * determined by the mc_filter_type setting.  
2907     */
2908    switch (hw->mc_filter_type) {
2909    /* [0] [1] [2] [3] [4] [5]
2910     * 01  AA  00  12  34  56
2911     * LSB                 MSB
2912     */
2913    case 0:
2914        /* [47:36] i.e. 0x563 for above example address */
2915        hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
2916        break;
2917    case 1:
2918        /* [46:35] i.e. 0xAC6 for above example address */
2919        hash_value = ((mc_addr[4] >> 3) | (((uint16_t) mc_addr[5]) << 5));
2920        break;
2921    case 2:
2922        /* [45:34] i.e. 0x5D8 for above example address */
2923        hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
2924        break;
2925    case 3:
2926        /* [43:32] i.e. 0x634 for above example address */
2927        hash_value = ((mc_addr[4]) | (((uint16_t) mc_addr[5]) << 8));
2928        break;
2929    }
2930
2931    hash_value &= 0xFFF;
2932    return hash_value;
2933}
2934
2935/******************************************************************************
2936 * Sets the bit in the multicast table corresponding to the hash value.
2937 *
2938 * hw - Struct containing variables accessed by shared code
2939 * hash_value - Multicast address hash value
2940 *****************************************************************************/
2941void
2942e1000_mta_set(struct e1000_hw *hw,
2943              uint32_t hash_value)
2944{
2945    uint32_t hash_bit, hash_reg;
2946    uint32_t mta;
2947    uint32_t temp;
2948
2949    /* The MTA is a register array of 128 32-bit registers.  
2950     * It is treated like an array of 4096 bits.  We want to set 
2951     * bit BitArray[hash_value]. So we figure out what register
2952     * the bit is in, read it, OR in the new bit, then write
2953     * back the new value.  The register is determined by the 
2954     * upper 7 bits of the hash value and the bit within that 
2955     * register are determined by the lower 5 bits of the value.
2956     */
2957    hash_reg = (hash_value >> 5) & 0x7F;
2958    hash_bit = hash_value & 0x1F;
2959
2960    mta = E1000_READ_REG_ARRAY(hw, MTA, hash_reg);
2961
2962    mta |= (1 << hash_bit);
2963
2964    /* If we are on an 82544 and we are trying to write an odd offset
2965     * in the MTA, save off the previous entry before writing and
2966     * restore the old value after writing.
2967     */
2968    if((hw->mac_type == e1000_82544) && ((hash_reg & 0x1) == 1)) {
2969        temp = E1000_READ_REG_ARRAY(hw, MTA, (hash_reg - 1));
2970        E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
2971        E1000_WRITE_REG_ARRAY(hw, MTA, (hash_reg - 1), temp);
2972    } else {
2973        E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
2974    }
2975}
2976
2977/******************************************************************************
2978 * Puts an ethernet address into a receive address register.
2979 *
2980 * hw - Struct containing variables accessed by shared code
2981 * addr - Address to put into receive address register
2982 * index - Receive address register to write
2983 *****************************************************************************/
2984void
2985e1000_rar_set(struct e1000_hw *hw,
2986              uint8_t *addr,
2987              uint32_t index)
2988{
2989    uint32_t rar_low, rar_high;
2990
2991    /* HW expects these in little endian so we reverse the byte order
2992     * from network order (big endian) to little endian              
2993     */
2994    rar_low = ((uint32_t) addr[0] |
2995               ((uint32_t) addr[1] << 8) |
2996               ((uint32_t) addr[2] << 16) | ((uint32_t) addr[3] << 24));
2997
2998    rar_high = ((uint32_t) addr[4] | ((uint32_t) addr[5] << 8) | E1000_RAH_AV);
2999
3000    E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
3001    E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
3002}
3003
3004/******************************************************************************
3005 * Writes a value to the specified offset in the VLAN filter table.
3006 *
3007 * hw - Struct containing variables accessed by shared code
3008 * offset - Offset in VLAN filer table to write
3009 * value - Value to write into VLAN filter table
3010 *****************************************************************************/
3011void
3012e1000_write_vfta(struct e1000_hw *hw,
3013                 uint32_t offset,
3014                 uint32_t value)
3015{
3016    uint32_t temp;
3017
3018    if((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
3019        temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
3020        E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
3021        E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
3022    } else {
3023        E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
3024    }
3025}
3026
3027/******************************************************************************
3028 * Clears the VLAN filer table
3029 *
3030 * hw - Struct containing variables accessed by shared code
3031 *****************************************************************************/
3032void
3033e1000_clear_vfta(struct e1000_hw *hw)
3034{
3035    uint32_t offset;
3036
3037    for(offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
3038        E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
3039}
3040
3041static int32_t
3042e1000_id_led_init(struct e1000_hw * hw)
3043{
3044    uint32_t ledctl;
3045    const uint32_t ledctl_mask = 0x000000FF;
3046    const uint32_t ledctl_on = E1000_LEDCTL_MODE_LED_ON;
3047    const uint32_t ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
3048    uint16_t eeprom_data, i, temp;
3049    const uint16_t led_mask = 0x0F;
3050        
3051    DEBUGFUNC("e1000_id_led_init");
3052    
3053    if(hw->mac_type < e1000_82540) {
3054        /* Nothing to do */
3055        return 0;
3056    }
3057    
3058    ledctl = E1000_READ_REG(hw, LEDCTL);
3059    hw->ledctl_default = ledctl;
3060    hw->ledctl_mode1 = hw->ledctl_default;
3061    hw->ledctl_mode2 = hw->ledctl_default;
3062        
3063    if(e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, &eeprom_data) < 0) {
3064        DEBUGOUT("EEPROM Read Error\n");
3065        return -E1000_ERR_EEPROM;
3066    }
3067    if((eeprom_data== ID_LED_RESERVED_0000) || 
3068       (eeprom_data == ID_LED_RESERVED_FFFF)) eeprom_data = ID_LED_DEFAULT;
3069    for(i = 0; i < 4; i++) {
3070        temp = (eeprom_data >> (i << 2)) & led_mask;
3071        switch(temp) {
3072        case ID_LED_ON1_DEF2:
3073        case ID_LED_ON1_ON2:
3074        case ID_LED_ON1_OFF2:
3075            hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
3076            hw->ledctl_mode1 |= ledctl_on << (i << 3);
3077            break;
3078        case ID_LED_OFF1_DEF2:
3079        case ID_LED_OFF1_ON2:
3080        case ID_LED_OFF1_OFF2:
3081            hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
3082            hw->ledctl_mode1 |= ledctl_off << (i << 3);
3083            break;
3084        default:
3085            /* Do nothing */
3086            break;
3087        }
3088        switch(temp) {
3089        case ID_LED_DEF1_ON2:
3090        case ID_LED_ON1_ON2:
3091        case ID_LED_OFF1_ON2:
3092            hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
3093            hw->ledctl_mode2 |= ledctl_on << (i << 3);
3094            break;
3095        case ID_LED_DEF1_OFF2:
3096        case ID_LED_ON1_OFF2:
3097        case ID_LED_OFF1_OFF2:
3098            hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
3099            hw->ledctl_mode2 |= ledctl_off << (i << 3);
3100            break;
3101        default:
3102            /* Do nothing */
3103            break;
3104        }
3105    }
3106    return 0;
3107}
3108
3109/******************************************************************************
3110 * Prepares SW controlable LED for use and saves the current state of the LED.
3111 *
3112 * hw - Struct containing variables accessed by shared code
3113 *****************************************************************************/
3114int32_t
3115e1000_setup_led(struct e1000_hw *hw)
3116{
3117    uint32_t ledctl;
3118 
3119    DEBUGFUNC("e1000_setup_led");
3120   
3121    switch(hw->device_id) {
3122    case E1000_DEV_ID_82542:
3123    case E1000_DEV_ID_82543GC_FIBER:
3124    case E1000_DEV_ID_82543GC_COPPER:
3125    case E1000_DEV_ID_82544EI_COPPER:
3126    case E1000_DEV_ID_82544EI_FIBER:
3127    case E1000_DEV_ID_82544GC_COPPER:
3128    case E1000_DEV_ID_82544GC_LOM:
3129        /* No setup necessary */
3130        break;
3131    case E1000_DEV_ID_82545EM_FIBER:
3132    case E1000_DEV_ID_82546EB_FIBER:
3133        ledctl = E1000_READ_REG(hw, LEDCTL);
3134        /* Save current LEDCTL settings */
3135        hw->ledctl_default = ledctl;
3136        /* Turn off LED0 */
3137        ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
3138                    E1000_LEDCTL_LED0_BLINK | 
3139                    E1000_LEDCTL_LED0_MODE_MASK);
3140        ledctl |= (E1000_LEDCTL_MODE_LED_OFF << E1000_LEDCTL_LED0_MODE_SHIFT);
3141        E1000_WRITE_REG(hw, LEDCTL, ledctl);
3142        break;
3143    case E1000_DEV_ID_82540EM:
3144    case E1000_DEV_ID_82540EM_LOM:
3145    case E1000_DEV_ID_82545EM_COPPER:
3146    case E1000_DEV_ID_82546EB_COPPER:
3147        E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
3148        break;
3149    default:
3150        DEBUGOUT("Invalid device ID\n");
3151        return -E1000_ERR_CONFIG;
3152    }
3153    return 0;
3154}
3155
3156/******************************************************************************
3157 * Restores the saved state of the SW controlable LED.
3158 *
3159 * hw - Struct containing variables accessed by shared code
3160 *****************************************************************************/
3161int32_t
3162e1000_cleanup_led(struct e1000_hw *hw)
3163{
3164    DEBUGFUNC("e1000_cleanup_led");
3165
3166    switch(hw->device_id) {
3167    case E1000_DEV_ID_82542:
3168    case E1000_DEV_ID_82543GC_FIBER:
3169    case E1000_DEV_ID_82543GC_COPPER:
3170    case E1000_DEV_ID_82544EI_COPPER:
3171    case E1000_DEV_ID_82544EI_FIBER:
3172    case E1000_DEV_ID_82544GC_COPPER:
3173    case E1000_DEV_ID_82544GC_LOM:
3174        /* No cleanup necessary */
3175        break;
3176    case E1000_DEV_ID_82540EM:
3177    case E1000_DEV_ID_82540EM_LOM:
3178    case E1000_DEV_ID_82545EM_COPPER:
3179    case E1000_DEV_ID_82545EM_FIBER:
3180    case E1000_DEV_ID_82546EB_COPPER:
3181    case E1000_DEV_ID_82546EB_FIBER:
3182        /* Restore LEDCTL settings */
3183        E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_default);
3184        break;
3185    default:
3186        DEBUGOUT("Invalid device ID\n");
3187        return -E1000_ERR_CONFIG;
3188    }
3189    return 0;
3190}
3191    
3192/******************************************************************************
3193 * Turns on the software controllable LED
3194 *
3195 * hw - Struct containing variables accessed by shared code
3196 *****************************************************************************/
3197int32_t
3198e1000_led_on(struct e1000_hw *hw)
3199{
3200    uint32_t ctrl;
3201
3202    DEBUGFUNC("e1000_led_on");
3203
3204    switch(hw->device_id) {
3205    case E1000_DEV_ID_82542:
3206    case E1000_DEV_ID_82543GC_FIBER:
3207    case E1000_DEV_ID_82543GC_COPPER:
3208    case E1000_DEV_ID_82544EI_FIBER:
3209        ctrl = E1000_READ_REG(hw, CTRL);
3210        /* Set SW Defineable Pin 0 to turn on the LED */
3211        ctrl |= E1000_CTRL_SWDPIN0;
3212        ctrl |= E1000_CTRL_SWDPIO0;
3213        E1000_WRITE_REG(hw, CTRL, ctrl);
3214        break;
3215    case E1000_DEV_ID_82544EI_COPPER:
3216    case E1000_DEV_ID_82544GC_COPPER:
3217    case E1000_DEV_ID_82544GC_LOM:
3218    case E1000_DEV_ID_82545EM_FIBER:
3219    case E1000_DEV_ID_82546EB_FIBER:
3220        ctrl = E1000_READ_REG(hw, CTRL);
3221        /* Clear SW Defineable Pin 0 to turn on the LED */
3222        ctrl &= ~E1000_CTRL_SWDPIN0;
3223        ctrl |= E1000_CTRL_SWDPIO0;
3224        E1000_WRITE_REG(hw, CTRL, ctrl);
3225        break;
3226    case E1000_DEV_ID_82540EM:
3227    case E1000_DEV_ID_82540EM_LOM:
3228    case E1000_DEV_ID_82545EM_COPPER:
3229    case E1000_DEV_ID_82546EB_COPPER:
3230        E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode2);
3231        break;
3232    default:
3233        DEBUGOUT("Invalid device ID\n");
3234        return -E1000_ERR_CONFIG;
3235    }
3236    return 0;
3237}
3238
3239/******************************************************************************
3240 * Turns off the software controllable LED
3241 *
3242 * hw - Struct containing variables accessed by shared code
3243 *****************************************************************************/
3244int32_t
3245e1000_led_off(struct e1000_hw *hw)
3246{
3247    uint32_t ctrl;
3248
3249    DEBUGFUNC("e1000_led_off");
3250
3251    switch(hw->device_id) {
3252    case E1000_DEV_ID_82542:
3253    case E1000_DEV_ID_82543GC_FIBER:
3254    case E1000_DEV_ID_82543GC_COPPER:
3255    case E1000_DEV_ID_82544EI_FIBER:
3256        ctrl = E1000_READ_REG(hw, CTRL);
3257        /* Clear SW Defineable Pin 0 to turn off the LED */
3258        ctrl &= ~E1000_CTRL_SWDPIN0;
3259        ctrl |= E1000_CTRL_SWDPIO0;
3260        E1000_WRITE_REG(hw, CTRL, ctrl);
3261        break;
3262    case E1000_DEV_ID_82544EI_COPPER:
3263    case E1000_DEV_ID_82544GC_COPPER:
3264    case E1000_DEV_ID_82544GC_LOM:
3265    case E1000_DEV_ID_82545EM_FIBER:
3266    case E1000_DEV_ID_82546EB_FIBER:
3267        ctrl = E1000_READ_REG(hw, CTRL);
3268        /* Set SW Defineable Pin 0 to turn off the LED */
3269        ctrl |= E1000_CTRL_SWDPIN0;
3270        ctrl |= E1000_CTRL_SWDPIO0;
3271        E1000_WRITE_REG(hw, CTRL, ctrl);
3272        break;
3273    case E1000_DEV_ID_82540EM:
3274    case E1000_DEV_ID_82540EM_LOM:
3275    case E1000_DEV_ID_82545EM_COPPER:
3276    case E1000_DEV_ID_82546EB_COPPER:
3277        E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
3278        break;
3279    default:
3280        DEBUGOUT("Invalid device ID\n");
3281        return -E1000_ERR_CONFIG;
3282    }
3283    return 0;
3284}
3285
3286/******************************************************************************
3287 * Clears all hardware statistics counters. 
3288 *
3289 * hw - Struct containing variables accessed by shared code
3290 *****************************************************************************/
3291void
3292e1000_clear_hw_cntrs(struct e1000_hw *hw)
3293{
3294    volatile uint32_t temp;
3295
3296    temp = E1000_READ_REG(hw, CRCERRS);
3297    temp = E1000_READ_REG(hw, SYMERRS);
3298    temp = E1000_READ_REG(hw, MPC);
3299    temp = E1000_READ_REG(hw, SCC);
3300    temp = E1000_READ_REG(hw, ECOL);
3301    temp = E1000_READ_REG(hw, MCC);
3302    temp = E1000_READ_REG(hw, LATECOL);
3303    temp = E1000_READ_REG(hw, COLC);
3304    temp = E1000_READ_REG(hw, DC);
3305    temp = E1000_READ_REG(hw, SEC);
3306    temp = E1000_READ_REG(hw, RLEC);
3307    temp = E1000_READ_REG(hw, XONRXC);
3308    temp = E1000_READ_REG(hw, XONTXC);
3309    temp = E1000_READ_REG(hw, XOFFRXC);
3310    temp = E1000_READ_REG(hw, XOFFTXC);
3311    temp = E1000_READ_REG(hw, FCRUC);
3312    temp = E1000_READ_REG(hw, PRC64);
3313    temp = E1000_READ_REG(hw, PRC127);
3314    temp = E1000_READ_REG(hw, PRC255);
3315    temp = E1000_READ_REG(hw, PRC511);
3316    temp = E1000_READ_REG(hw, PRC1023);
3317    temp = E1000_READ_REG(hw, PRC1522);
3318    temp = E1000_READ_REG(hw, GPRC);
3319    temp = E1000_READ_REG(hw, BPRC);
3320    temp = E1000_READ_REG(hw, MPRC);
3321    temp = E1000_READ_REG(hw, GPTC);
3322    temp = E1000_READ_REG(hw, GORCL);
3323    temp = E1000_READ_REG(hw, GORCH);
3324    temp = E1000_READ_REG(hw, GOTCL);
3325    temp = E1000_READ_REG(hw, GOTCH);
3326    temp = E1000_READ_REG(hw, RNBC);
3327    temp = E1000_READ_REG(hw, RUC);
3328    temp = E1000_READ_REG(hw, RFC);
3329    temp = E1000_READ_REG(hw, ROC);
3330    temp = E1000_READ_REG(hw, RJC);
3331    temp = E1000_READ_REG(hw, TORL);
3332    temp = E1000_READ_REG(hw, TORH);
3333    temp = E1000_READ_REG(hw, TOTL);
3334    temp = E1000_READ_REG(hw, TOTH);
3335    temp = E1000_READ_REG(hw, TPR);
3336    temp = E1000_READ_REG(hw, TPT);
3337    temp = E1000_READ_REG(hw, PTC64);
3338    temp = E1000_READ_REG(hw, PTC127);
3339    temp = E1000_READ_REG(hw, PTC255);
3340    temp = E1000_READ_REG(hw, PTC511);
3341    temp = E1000_READ_REG(hw, PTC1023);
3342    temp = E1000_READ_REG(hw, PTC1522);
3343    temp = E1000_READ_REG(hw, MPTC);
3344    temp = E1000_READ_REG(hw, BPTC);
3345
3346    if(hw->mac_type < e1000_82543) return;
3347
3348    temp = E1000_READ_REG(hw, ALGNERRC);
3349    temp = E1000_READ_REG(hw, RXERRC);
3350    temp = E1000_READ_REG(hw, TNCRS);
3351    temp = E1000_READ_REG(hw, CEXTERR);
3352    temp = E1000_READ_REG(hw, TSCTC);
3353    temp = E1000_READ_REG(hw, TSCTFC);
3354
3355    if(hw->mac_type <= e1000_82544) return;
3356
3357    temp = E1000_READ_REG(hw, MGTPRC);
3358    temp = E1000_READ_REG(hw, MGTPDC);
3359    temp = E1000_READ_REG(hw, MGTPTC);
3360}
3361
3362/******************************************************************************
3363 * Resets Adaptive IFS to its default state.
3364 *
3365 * hw - Struct containing variables accessed by shared code
3366 *
3367 * Call this after e1000_init_hw. You may override the IFS defaults by setting
3368 * hw->ifs_params_forced to TRUE. However, you must initialize hw->
3369 * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
3370 * before calling this function.
3371 *****************************************************************************/
3372void
3373e1000_reset_adaptive(struct e1000_hw *hw)
3374{
3375    DEBUGFUNC("e1000_reset_adaptive");
3376
3377    if(hw->adaptive_ifs) {
3378        if(!hw->ifs_params_forced) {
3379            hw->current_ifs_val = 0;
3380            hw->ifs_min_val = IFS_MIN;
3381            hw->ifs_max_val = IFS_MAX;
3382            hw->ifs_step_size = IFS_STEP;
3383            hw->ifs_ratio = IFS_RATIO;
3384        }
3385        hw->in_ifs_mode = FALSE;
3386        E1000_WRITE_REG(hw, AIT, 0);
3387    } else {
3388        DEBUGOUT("Not in Adaptive IFS mode!\n");
3389    }
3390}
3391
3392/******************************************************************************
3393 * Called during the callback/watchdog routine to update IFS value based on
3394 * the ratio of transmits to collisions.
3395 *
3396 * hw - Struct containing variables accessed by shared code
3397 * tx_packets - Number of transmits since last callback
3398 * total_collisions - Number of collisions since last callback
3399 *****************************************************************************/
3400void
3401e1000_update_adaptive(struct e1000_hw *hw)
3402{
3403    DEBUGFUNC("e1000_update_adaptive");
3404
3405    if(hw->adaptive_ifs) {
3406        if((hw->collision_delta * hw->ifs_ratio) > 
3407           hw->tx_packet_delta) {
3408            if(hw->tx_packet_delta > MIN_NUM_XMITS) {
3409                hw->in_ifs_mode = TRUE;
3410                if(hw->current_ifs_val < hw->ifs_max_val) {
3411                    if(hw->current_ifs_val == 0)
3412                        hw->current_ifs_val = hw->ifs_min_val;
3413                    else
3414                        hw->current_ifs_val += hw->ifs_step_size;
3415                    E1000_WRITE_REG(hw, AIT, hw->current_ifs_val);
3416                }
3417            }
3418        } else {
3419            if((hw->in_ifs_mode == TRUE) && 
3420               (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
3421                hw->current_ifs_val = 0;
3422                hw->in_ifs_mode = FALSE;
3423                E1000_WRITE_REG(hw, AIT, 0);
3424            }
3425        }
3426    } else {
3427        DEBUGOUT("Not in Adaptive IFS mode!\n");
3428    }
3429}
3430
3431/******************************************************************************
3432 * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
3433 * 
3434 * hw - Struct containing variables accessed by shared code
3435 * frame_len - The length of the frame in question
3436 * mac_addr - The Ethernet destination address of the frame in question
3437 *****************************************************************************/
3438void
3439e1000_tbi_adjust_stats(struct e1000_hw *hw,
3440                       struct e1000_hw_stats *stats,
3441                       uint32_t frame_len,
3442                       uint8_t *mac_addr)
3443{
3444    uint64_t carry_bit;
3445
3446    /* First adjust the frame length. */
3447    frame_len--;
3448    /* We need to adjust the statistics counters, since the hardware
3449     * counters overcount this packet as a CRC error and undercount
3450     * the packet as a good packet
3451     */
3452    /* This packet should not be counted as a CRC error.    */
3453    stats->crcerrs--;
3454    /* This packet does count as a Good Packet Received.    */
3455    stats->gprc++;
3456
3457    /* Adjust the Good Octets received counters             */
3458    carry_bit = 0x80000000 & stats->gorcl;
3459    stats->gorcl += frame_len;
3460    /* If the high bit of Gorcl (the low 32 bits of the Good Octets
3461     * Received Count) was one before the addition, 
3462     * AND it is zero after, then we lost the carry out, 
3463     * need to add one to Gorch (Good Octets Received Count High).
3464     * This could be simplified if all environments supported 
3465     * 64-bit integers.
3466     */
3467    if(carry_bit && ((stats->gorcl & 0x80000000) == 0))
3468        stats->gorch++;
3469    /* Is this a broadcast or multicast?  Check broadcast first,
3470     * since the test for a multicast frame will test positive on 
3471     * a broadcast frame.
3472     */
3473    if((mac_addr[0] == (uint8_t) 0xff) && (mac_addr[1] == (uint8_t) 0xff))
3474        /* Broadcast packet */
3475        stats->bprc++;
3476    else if(*mac_addr & 0x01)
3477        /* Multicast packet */
3478        stats->mprc++;
3479
3480    if(frame_len == hw->max_frame_size) {
3481        /* In this case, the hardware has overcounted the number of
3482         * oversize frames.
3483         */
3484        if(stats->roc > 0)
3485            stats->roc--;
3486    }
3487
3488    /* Adjust the bin counters when the extra byte put the frame in the
3489     * wrong bin. Remember that the frame_len was adjusted above.
3490     */
3491    if(frame_len == 64) {
3492        stats->prc64++;
3493        stats->prc127--;
3494    } else if(frame_len == 127) {
3495        stats->prc127++;
3496        stats->prc255--;
3497    } else if(frame_len == 255) {
3498        stats->prc255++;
3499        stats->prc511--;
3500    } else if(frame_len == 511) {
3501        stats->prc511++;
3502        stats->prc1023--;
3503    } else if(frame_len == 1023) {
3504        stats->prc1023++;
3505        stats->prc1522--;
3506    } else if(frame_len == 1522) {
3507        stats->prc1522++;
3508    }
3509}
3510
3511/******************************************************************************
3512 * Gets the current PCI bus type, speed, and width of the hardware
3513 *
3514 * hw - Struct containing variables accessed by shared code
3515 *****************************************************************************/
3516void
3517e1000_get_bus_info(struct e1000_hw *hw)
3518{
3519    uint32_t status;
3520
3521    if(hw->mac_type < e1000_82543) {
3522        hw->bus_type = e1000_bus_type_unknown;
3523        hw->bus_speed = e1000_bus_speed_unknown;
3524        hw->bus_width = e1000_bus_width_unknown;
3525        return;
3526    }
3527
3528    status = E1000_READ_REG(hw, STATUS);
3529    hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
3530                   e1000_bus_type_pcix : e1000_bus_type_pci;
3531    if(hw->bus_type == e1000_bus_type_pci) {
3532        hw->bus_speed = (status & E1000_STATUS_PCI66) ?
3533                        e1000_bus_speed_66 : e1000_bus_speed_33;
3534    } else {
3535        switch (status & E1000_STATUS_PCIX_SPEED) {
3536        case E1000_STATUS_PCIX_SPEED_66:
3537            hw->bus_speed = e1000_bus_speed_66;
3538            break;
3539        case E1000_STATUS_PCIX_SPEED_100:
3540            hw->bus_speed = e1000_bus_speed_100;
3541            break;
3542        case E1000_STATUS_PCIX_SPEED_133:
3543            hw->bus_speed = e1000_bus_speed_133;
3544            break;
3545        default:
3546            hw->bus_speed = e1000_bus_speed_reserved;
3547            break;
3548        }
3549    }
3550    hw->bus_width = (status & E1000_STATUS_BUS64) ?
3551                    e1000_bus_width_64 : e1000_bus_width_32;
3552}
3553/******************************************************************************
3554 * Reads a value from one of the devices registers using port I/O (as opposed
3555 * memory mapped I/O). Only 82544 and newer devices support port I/O.
3556 *
3557 * hw - Struct containing variables accessed by shared code
3558 * offset - offset to read from
3559 *****************************************************************************/
3560uint32_t
3561e1000_read_reg_io(struct e1000_hw *hw,
3562                  uint32_t offset)
3563{
3564    uint32_t io_addr = hw->io_base;
3565    uint32_t io_data = hw->io_base + 4;
3566
3567    e1000_io_write(hw, io_addr, offset);
3568    return e1000_io_read(hw, io_data);
3569}
3570
3571/******************************************************************************
3572 * Writes a value to one of the devices registers using port I/O (as opposed to
3573 * memory mapped I/O). Only 82544 and newer devices support port I/O.
3574 *
3575 * hw - Struct containing variables accessed by shared code
3576 * offset - offset to write to
3577 * value - value to write
3578 *****************************************************************************/
3579void
3580e1000_write_reg_io(struct e1000_hw *hw,
3581                   uint32_t offset,
3582                   uint32_t value)
3583{
3584    uint32_t io_addr = hw->io_base;
3585    uint32_t io_data = hw->io_base + 4;
3586
3587    e1000_io_write(hw, io_addr, offset);
3588    e1000_io_write(hw, io_data, value);
3589}
3590
3591
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.