1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Intel Corporation */
5 #include <linux/delay.h>
10 /* forward declaration */
11 static s32 igc_set_default_fc(struct igc_hw *hw);
12 static s32 igc_set_fc_watermarks(struct igc_hw *hw);
15 * igc_disable_pcie_master - Disables PCI-express master access
16 * @hw: pointer to the HW structure
18 * Returns 0 (0) if successful, else returns -10
19 * (-IGC_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
20 * the master requests to be disabled.
22 * Disables PCI-Express master access and verifies there are no pending
25 s32 igc_disable_pcie_master(struct igc_hw *hw)
27 s32 timeout = MASTER_DISABLE_TIMEOUT;
31 ctrl = rd32(IGC_CTRL);
32 ctrl |= IGC_CTRL_GIO_MASTER_DISABLE;
36 if (!(rd32(IGC_STATUS) &
37 IGC_STATUS_GIO_MASTER_ENABLE))
39 usleep_range(2000, 3000);
44 hw_dbg("Master requests are pending.\n");
45 ret_val = -IGC_ERR_MASTER_REQUESTS_PENDING;
54 * igc_init_rx_addrs - Initialize receive addresses
55 * @hw: pointer to the HW structure
56 * @rar_count: receive address registers
58 * Setup the receive address registers by setting the base receive address
59 * register to the devices MAC address and clearing all the other receive
60 * address registers to 0.
62 void igc_init_rx_addrs(struct igc_hw *hw, u16 rar_count)
64 u8 mac_addr[ETH_ALEN] = {0};
67 /* Setup the receive address */
68 hw_dbg("Programming MAC Address into RAR[0]\n");
70 hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
72 /* Zero out the other (rar_entry_count - 1) receive addresses */
73 hw_dbg("Clearing RAR[1-%u]\n", rar_count - 1);
74 for (i = 1; i < rar_count; i++)
75 hw->mac.ops.rar_set(hw, mac_addr, i);
79 * igc_setup_link - Setup flow control and link settings
80 * @hw: pointer to the HW structure
82 * Determines which flow control settings to use, then configures flow
83 * control. Calls the appropriate media-specific link configuration
84 * function. Assuming the adapter has a valid link partner, a valid link
85 * should be established. Assumes the hardware has previously been reset
86 * and the transmitter and receiver are not enabled.
88 s32 igc_setup_link(struct igc_hw *hw)
92 /* In the case of the phy reset being blocked, we already have a link.
93 * We do not need to set it up again.
95 if (igc_check_reset_block(hw))
98 /* If requested flow control is set to default, set flow control
99 * based on the EEPROM flow control settings.
101 if (hw->fc.requested_mode == igc_fc_default) {
102 ret_val = igc_set_default_fc(hw);
107 /* We want to save off the original Flow Control configuration just
108 * in case we get disconnected and then reconnected into a different
109 * hub or switch with different Flow Control capabilities.
111 hw->fc.current_mode = hw->fc.requested_mode;
113 hw_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
115 /* Call the necessary media_type subroutine to configure the link. */
116 ret_val = hw->mac.ops.setup_physical_interface(hw);
120 /* Initialize the flow control address, type, and PAUSE timer
121 * registers to their default values. This is done even if flow
122 * control is disabled, because it does not hurt anything to
123 * initialize these registers.
125 hw_dbg("Initializing the Flow Control address, type and timer regs\n");
126 wr32(IGC_FCT, FLOW_CONTROL_TYPE);
127 wr32(IGC_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
128 wr32(IGC_FCAL, FLOW_CONTROL_ADDRESS_LOW);
130 wr32(IGC_FCTTV, hw->fc.pause_time);
132 ret_val = igc_set_fc_watermarks(hw);
139 * igc_set_default_fc - Set flow control default values
140 * @hw: pointer to the HW structure
142 * Read the EEPROM for the default values for flow control and store the
145 static s32 igc_set_default_fc(struct igc_hw *hw)
147 hw->fc.requested_mode = igc_fc_full;
152 * igc_force_mac_fc - Force the MAC's flow control settings
153 * @hw: pointer to the HW structure
155 * Force the MAC's flow control settings. Sets the TFCE and RFCE bits in the
156 * device control register to reflect the adapter settings. TFCE and RFCE
157 * need to be explicitly set by software when a copper PHY is used because
158 * autonegotiation is managed by the PHY rather than the MAC. Software must
159 * also configure these bits when link is forced on a fiber connection.
161 s32 igc_force_mac_fc(struct igc_hw *hw)
166 ctrl = rd32(IGC_CTRL);
168 /* Because we didn't get link via the internal auto-negotiation
169 * mechanism (we either forced link or we got link via PHY
170 * auto-neg), we have to manually enable/disable transmit an
171 * receive flow control.
173 * The "Case" statement below enables/disable flow control
174 * according to the "hw->fc.current_mode" parameter.
176 * The possible values of the "fc" parameter are:
177 * 0: Flow control is completely disabled
178 * 1: Rx flow control is enabled (we can receive pause
179 * frames but not send pause frames).
180 * 2: Tx flow control is enabled (we can send pause frames
181 * frames but we do not receive pause frames).
182 * 3: Both Rx and TX flow control (symmetric) is enabled.
183 * other: No other values should be possible at this point.
185 hw_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
187 switch (hw->fc.current_mode) {
189 ctrl &= (~(IGC_CTRL_TFCE | IGC_CTRL_RFCE));
191 case igc_fc_rx_pause:
192 ctrl &= (~IGC_CTRL_TFCE);
193 ctrl |= IGC_CTRL_RFCE;
195 case igc_fc_tx_pause:
196 ctrl &= (~IGC_CTRL_RFCE);
197 ctrl |= IGC_CTRL_TFCE;
200 ctrl |= (IGC_CTRL_TFCE | IGC_CTRL_RFCE);
203 hw_dbg("Flow control param set incorrectly\n");
204 ret_val = -IGC_ERR_CONFIG;
208 wr32(IGC_CTRL, ctrl);
215 * igc_set_fc_watermarks - Set flow control high/low watermarks
216 * @hw: pointer to the HW structure
218 * Sets the flow control high/low threshold (watermark) registers. If
219 * flow control XON frame transmission is enabled, then set XON frame
220 * transmission as well.
222 static s32 igc_set_fc_watermarks(struct igc_hw *hw)
224 u32 fcrtl = 0, fcrth = 0;
226 /* Set the flow control receive threshold registers. Normally,
227 * these registers will be set to a default threshold that may be
228 * adjusted later by the driver's runtime code. However, if the
229 * ability to transmit pause frames is not enabled, then these
230 * registers will be set to 0.
232 if (hw->fc.current_mode & igc_fc_tx_pause) {
233 /* We need to set up the Receive Threshold high and low water
234 * marks as well as (optionally) enabling the transmission of
237 fcrtl = hw->fc.low_water;
239 fcrtl |= IGC_FCRTL_XONE;
241 fcrth = hw->fc.high_water;
243 wr32(IGC_FCRTL, fcrtl);
244 wr32(IGC_FCRTH, fcrth);
250 * igc_clear_hw_cntrs_base - Clear base hardware counters
251 * @hw: pointer to the HW structure
253 * Clears the base hardware counters by reading the counter registers.
255 void igc_clear_hw_cntrs_base(struct igc_hw *hw)
344 * igc_rar_set - Set receive address register
345 * @hw: pointer to the HW structure
346 * @addr: pointer to the receive address
347 * @index: receive address array register
349 * Sets the receive address array register at index to the address passed
352 void igc_rar_set(struct igc_hw *hw, u8 *addr, u32 index)
354 u32 rar_low, rar_high;
356 /* HW expects these in little endian so we reverse the byte order
357 * from network order (big endian) to little endian
359 rar_low = ((u32)addr[0] |
360 ((u32)addr[1] << 8) |
361 ((u32)addr[2] << 16) | ((u32)addr[3] << 24));
363 rar_high = ((u32)addr[4] | ((u32)addr[5] << 8));
365 /* If MAC address zero, no need to set the AV bit */
366 if (rar_low || rar_high)
367 rar_high |= IGC_RAH_AV;
369 /* Some bridges will combine consecutive 32-bit writes into
370 * a single burst write, which will malfunction on some parts.
371 * The flushes avoid this.
373 wr32(IGC_RAL(index), rar_low);
375 wr32(IGC_RAH(index), rar_high);
380 * igc_check_for_copper_link - Check for link (Copper)
381 * @hw: pointer to the HW structure
383 * Checks to see of the link status of the hardware has changed. If a
384 * change in link status has been detected, then we read the PHY registers
385 * to get the current speed/duplex if link exists.
387 s32 igc_check_for_copper_link(struct igc_hw *hw)
389 struct igc_mac_info *mac = &hw->mac;
393 /* We only want to go out to the PHY registers to see if Auto-Neg
394 * has completed and/or if our link status has changed. The
395 * get_link_status flag is set upon receiving a Link Status
396 * Change or Rx Sequence Error interrupt.
398 if (!mac->get_link_status) {
403 /* First we want to see if the MII Status Register reports
404 * link. If so, then we want to get the current speed/duplex
407 ret_val = igc_phy_has_link(hw, 1, 0, &link);
412 goto out; /* No link detected */
414 mac->get_link_status = false;
416 /* Check if there was DownShift, must be checked
417 * immediately after link-up
419 igc_check_downshift(hw);
421 /* If we are forcing speed/duplex, then we simply return since
422 * we have already determined whether we have link or not.
425 ret_val = -IGC_ERR_CONFIG;
429 /* Auto-Neg is enabled. Auto Speed Detection takes care
430 * of MAC speed/duplex configuration. So we only need to
431 * configure Collision Distance in the MAC.
433 igc_config_collision_dist(hw);
435 /* Configure Flow Control now that Auto-Neg has completed.
436 * First, we need to restore the desired flow control
437 * settings because we may have had to re-autoneg with a
438 * different link partner.
440 ret_val = igc_config_fc_after_link_up(hw);
442 hw_dbg("Error configuring flow control\n");
449 * igc_config_collision_dist - Configure collision distance
450 * @hw: pointer to the HW structure
452 * Configures the collision distance to the default value and is used
453 * during link setup. Currently no func pointer exists and all
454 * implementations are handled in the generic version of this function.
456 void igc_config_collision_dist(struct igc_hw *hw)
460 tctl = rd32(IGC_TCTL);
462 tctl &= ~IGC_TCTL_COLD;
463 tctl |= IGC_COLLISION_DISTANCE << IGC_COLD_SHIFT;
465 wr32(IGC_TCTL, tctl);
470 * igc_config_fc_after_link_up - Configures flow control after link
471 * @hw: pointer to the HW structure
473 * Checks the status of auto-negotiation after link up to ensure that the
474 * speed and duplex were not forced. If the link needed to be forced, then
475 * flow control needs to be forced also. If auto-negotiation is enabled
476 * and did not fail, then we configure flow control based on our link
479 s32 igc_config_fc_after_link_up(struct igc_hw *hw)
481 u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
482 struct igc_mac_info *mac = &hw->mac;
486 /* Check for the case where we have fiber media and auto-neg failed
487 * so we had to force link. In this case, we need to force the
488 * configuration of the MAC to match the "fc" parameter.
490 if (mac->autoneg_failed) {
491 if (hw->phy.media_type == igc_media_type_copper)
492 ret_val = igc_force_mac_fc(hw);
496 hw_dbg("Error forcing flow control settings\n");
500 /* Check for the case where we have copper media and auto-neg is
501 * enabled. In this case, we need to check and see if Auto-Neg
502 * has completed, and if so, how the PHY and link partner has
503 * flow control configured.
505 if (hw->phy.media_type == igc_media_type_copper && mac->autoneg) {
506 /* Read the MII Status Register and check to see if AutoNeg
507 * has completed. We read this twice because this reg has
508 * some "sticky" (latched) bits.
510 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
514 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
519 if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
520 hw_dbg("Copper PHY and Auto Neg has not completed.\n");
524 /* The AutoNeg process has completed, so we now need to
525 * read both the Auto Negotiation Advertisement
526 * Register (Address 4) and the Auto_Negotiation Base
527 * Page Ability Register (Address 5) to determine how
528 * flow control was negotiated.
530 ret_val = hw->phy.ops.read_reg(hw, PHY_AUTONEG_ADV,
534 ret_val = hw->phy.ops.read_reg(hw, PHY_LP_ABILITY,
535 &mii_nway_lp_ability_reg);
538 /* Two bits in the Auto Negotiation Advertisement Register
539 * (Address 4) and two bits in the Auto Negotiation Base
540 * Page Ability Register (Address 5) determine flow control
541 * for both the PHY and the link partner. The following
542 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
543 * 1999, describes these PAUSE resolution bits and how flow
544 * control is determined based upon these settings.
545 * NOTE: DC = Don't Care
547 * LOCAL DEVICE | LINK PARTNER
548 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
549 *-------|---------|-------|---------|--------------------
550 * 0 | 0 | DC | DC | igc_fc_none
551 * 0 | 1 | 0 | DC | igc_fc_none
552 * 0 | 1 | 1 | 0 | igc_fc_none
553 * 0 | 1 | 1 | 1 | igc_fc_tx_pause
554 * 1 | 0 | 0 | DC | igc_fc_none
555 * 1 | DC | 1 | DC | igc_fc_full
556 * 1 | 1 | 0 | 0 | igc_fc_none
557 * 1 | 1 | 0 | 1 | igc_fc_rx_pause
559 * Are both PAUSE bits set to 1? If so, this implies
560 * Symmetric Flow Control is enabled at both ends. The
561 * ASM_DIR bits are irrelevant per the spec.
563 * For Symmetric Flow Control:
565 * LOCAL DEVICE | LINK PARTNER
566 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
567 *-------|---------|-------|---------|--------------------
568 * 1 | DC | 1 | DC | IGC_fc_full
571 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
572 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
573 /* Now we need to check if the user selected RX ONLY
574 * of pause frames. In this case, we had to advertise
575 * FULL flow control because we could not advertise RX
576 * ONLY. Hence, we must now check to see if we need to
577 * turn OFF the TRANSMISSION of PAUSE frames.
579 if (hw->fc.requested_mode == igc_fc_full) {
580 hw->fc.current_mode = igc_fc_full;
581 hw_dbg("Flow Control = FULL.\n");
583 hw->fc.current_mode = igc_fc_rx_pause;
584 hw_dbg("Flow Control = RX PAUSE frames only.\n");
588 /* For receiving PAUSE frames ONLY.
590 * LOCAL DEVICE | LINK PARTNER
591 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
592 *-------|---------|-------|---------|--------------------
593 * 0 | 1 | 1 | 1 | igc_fc_tx_pause
595 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
596 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
597 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
598 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
599 hw->fc.current_mode = igc_fc_tx_pause;
600 hw_dbg("Flow Control = TX PAUSE frames only.\n");
602 /* For transmitting PAUSE frames ONLY.
604 * LOCAL DEVICE | LINK PARTNER
605 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
606 *-------|---------|-------|---------|--------------------
607 * 1 | 1 | 0 | 1 | igc_fc_rx_pause
609 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
610 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
611 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
612 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
613 hw->fc.current_mode = igc_fc_rx_pause;
614 hw_dbg("Flow Control = RX PAUSE frames only.\n");
616 /* Per the IEEE spec, at this point flow control should be
617 * disabled. However, we want to consider that we could
618 * be connected to a legacy switch that doesn't advertise
619 * desired flow control, but can be forced on the link
620 * partner. So if we advertised no flow control, that is
621 * what we will resolve to. If we advertised some kind of
622 * receive capability (Rx Pause Only or Full Flow Control)
623 * and the link partner advertised none, we will configure
624 * ourselves to enable Rx Flow Control only. We can do
625 * this safely for two reasons: If the link partner really
626 * didn't want flow control enabled, and we enable Rx, no
627 * harm done since we won't be receiving any PAUSE frames
628 * anyway. If the intent on the link partner was to have
629 * flow control enabled, then by us enabling RX only, we
630 * can at least receive pause frames and process them.
631 * This is a good idea because in most cases, since we are
632 * predominantly a server NIC, more times than not we will
633 * be asked to delay transmission of packets than asking
634 * our link partner to pause transmission of frames.
636 else if ((hw->fc.requested_mode == igc_fc_none) ||
637 (hw->fc.requested_mode == igc_fc_tx_pause) ||
638 (hw->fc.strict_ieee)) {
639 hw->fc.current_mode = igc_fc_none;
640 hw_dbg("Flow Control = NONE.\n");
642 hw->fc.current_mode = igc_fc_rx_pause;
643 hw_dbg("Flow Control = RX PAUSE frames only.\n");
646 /* Now we need to do one last check... If we auto-
647 * negotiated to HALF DUPLEX, flow control should not be
648 * enabled per IEEE 802.3 spec.
650 ret_val = hw->mac.ops.get_speed_and_duplex(hw, &speed, &duplex);
652 hw_dbg("Error getting link speed and duplex\n");
656 if (duplex == HALF_DUPLEX)
657 hw->fc.current_mode = igc_fc_none;
659 /* Now we call a subroutine to actually force the MAC
660 * controller to use the correct flow control settings.
662 ret_val = igc_force_mac_fc(hw);
664 hw_dbg("Error forcing flow control settings\n");
674 * igc_get_auto_rd_done - Check for auto read completion
675 * @hw: pointer to the HW structure
677 * Check EEPROM for Auto Read done bit.
679 s32 igc_get_auto_rd_done(struct igc_hw *hw)
684 while (i < AUTO_READ_DONE_TIMEOUT) {
685 if (rd32(IGC_EECD) & IGC_EECD_AUTO_RD)
687 usleep_range(1000, 2000);
691 if (i == AUTO_READ_DONE_TIMEOUT) {
692 hw_dbg("Auto read by HW from NVM has not completed.\n");
693 ret_val = -IGC_ERR_RESET;
702 * igc_get_speed_and_duplex_copper - Retrieve current speed/duplex
703 * @hw: pointer to the HW structure
704 * @speed: stores the current speed
705 * @duplex: stores the current duplex
707 * Read the status register for the current speed/duplex and store the current
708 * speed and duplex for copper connections.
710 s32 igc_get_speed_and_duplex_copper(struct igc_hw *hw, u16 *speed,
715 status = rd32(IGC_STATUS);
716 if (status & IGC_STATUS_SPEED_1000) {
717 /* For I225, STATUS will indicate 1G speed in both 1 Gbps
718 * and 2.5 Gbps link modes. An additional bit is used
719 * to differentiate between 1 Gbps and 2.5 Gbps.
721 if (hw->mac.type == igc_i225 &&
722 (status & IGC_STATUS_SPEED_2500)) {
724 hw_dbg("2500 Mbs, ");
727 hw_dbg("1000 Mbs, ");
729 } else if (status & IGC_STATUS_SPEED_100) {
737 if (status & IGC_STATUS_FD) {
738 *duplex = FULL_DUPLEX;
739 hw_dbg("Full Duplex\n");
741 *duplex = HALF_DUPLEX;
742 hw_dbg("Half Duplex\n");
749 * igc_put_hw_semaphore - Release hardware semaphore
750 * @hw: pointer to the HW structure
752 * Release hardware semaphore used to access the PHY or NVM
754 void igc_put_hw_semaphore(struct igc_hw *hw)
758 swsm = rd32(IGC_SWSM);
760 swsm &= ~(IGC_SWSM_SMBI | IGC_SWSM_SWESMBI);
762 wr32(IGC_SWSM, swsm);
766 * igc_enable_mng_pass_thru - Enable processing of ARP's
767 * @hw: pointer to the HW structure
769 * Verifies the hardware needs to leave interface enabled so that frames can
770 * be directed to and from the management interface.
772 bool igc_enable_mng_pass_thru(struct igc_hw *hw)
774 bool ret_val = false;
778 if (!hw->mac.asf_firmware_present)
781 manc = rd32(IGC_MANC);
783 if (!(manc & IGC_MANC_RCV_TCO_EN))
786 if (hw->mac.arc_subsystem_valid) {
787 fwsm = rd32(IGC_FWSM);
788 factps = rd32(IGC_FACTPS);
790 if (!(factps & IGC_FACTPS_MNGCG) &&
791 ((fwsm & IGC_FWSM_MODE_MASK) ==
792 (igc_mng_mode_pt << IGC_FWSM_MODE_SHIFT))) {
797 if ((manc & IGC_MANC_SMBUS_EN) &&
798 !(manc & IGC_MANC_ASF_EN)) {