1 /* Intel PRO/1000 Linux driver 2 * Copyright(c) 1999 - 2015 Intel Corporation. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms and conditions of the GNU General Public License, 6 * version 2, as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * The full GNU General Public License is included in this distribution in 14 * the file called "COPYING". 15 * 16 * Contact Information: 17 * Linux NICS <linux.nics@intel.com> 18 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> 19 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 20 */ 21 22 /* ethtool support for e1000 */ 23 24 #include <linux/netdevice.h> 25 #include <linux/interrupt.h> 26 #include <linux/ethtool.h> 27 #include <linux/pci.h> 28 #include <linux/slab.h> 29 #include <linux/delay.h> 30 #include <linux/vmalloc.h> 31 #include <linux/pm_runtime.h> 32 33 #include "e1000.h" 34 35 enum { NETDEV_STATS, E1000_STATS }; 36 37 struct e1000_stats { 38 char stat_string[ETH_GSTRING_LEN]; 39 int type; 40 int sizeof_stat; 41 int stat_offset; 42 }; 43 44 #define E1000_STAT(str, m) { \ 45 .stat_string = str, \ 46 .type = E1000_STATS, \ 47 .sizeof_stat = sizeof(((struct e1000_adapter *)0)->m), \ 48 .stat_offset = offsetof(struct e1000_adapter, m) } 49 #define E1000_NETDEV_STAT(str, m) { \ 50 .stat_string = str, \ 51 .type = NETDEV_STATS, \ 52 .sizeof_stat = sizeof(((struct rtnl_link_stats64 *)0)->m), \ 53 .stat_offset = offsetof(struct rtnl_link_stats64, m) } 54 55 static const struct e1000_stats e1000_gstrings_stats[] = { 56 E1000_STAT("rx_packets", stats.gprc), 57 E1000_STAT("tx_packets", stats.gptc), 58 E1000_STAT("rx_bytes", stats.gorc), 59 E1000_STAT("tx_bytes", stats.gotc), 60 E1000_STAT("rx_broadcast", stats.bprc), 61 E1000_STAT("tx_broadcast", stats.bptc), 62 E1000_STAT("rx_multicast", stats.mprc), 63 E1000_STAT("tx_multicast", stats.mptc), 64 E1000_NETDEV_STAT("rx_errors", rx_errors), 65 E1000_NETDEV_STAT("tx_errors", tx_errors), 66 E1000_NETDEV_STAT("tx_dropped", tx_dropped), 67 E1000_STAT("multicast", stats.mprc), 68 E1000_STAT("collisions", stats.colc), 69 E1000_NETDEV_STAT("rx_length_errors", rx_length_errors), 70 E1000_NETDEV_STAT("rx_over_errors", rx_over_errors), 71 E1000_STAT("rx_crc_errors", stats.crcerrs), 72 E1000_NETDEV_STAT("rx_frame_errors", rx_frame_errors), 73 E1000_STAT("rx_no_buffer_count", stats.rnbc), 74 E1000_STAT("rx_missed_errors", stats.mpc), 75 E1000_STAT("tx_aborted_errors", stats.ecol), 76 E1000_STAT("tx_carrier_errors", stats.tncrs), 77 E1000_NETDEV_STAT("tx_fifo_errors", tx_fifo_errors), 78 E1000_NETDEV_STAT("tx_heartbeat_errors", tx_heartbeat_errors), 79 E1000_STAT("tx_window_errors", stats.latecol), 80 E1000_STAT("tx_abort_late_coll", stats.latecol), 81 E1000_STAT("tx_deferred_ok", stats.dc), 82 E1000_STAT("tx_single_coll_ok", stats.scc), 83 E1000_STAT("tx_multi_coll_ok", stats.mcc), 84 E1000_STAT("tx_timeout_count", tx_timeout_count), 85 E1000_STAT("tx_restart_queue", restart_queue), 86 E1000_STAT("rx_long_length_errors", stats.roc), 87 E1000_STAT("rx_short_length_errors", stats.ruc), 88 E1000_STAT("rx_align_errors", stats.algnerrc), 89 E1000_STAT("tx_tcp_seg_good", stats.tsctc), 90 E1000_STAT("tx_tcp_seg_failed", stats.tsctfc), 91 E1000_STAT("rx_flow_control_xon", stats.xonrxc), 92 E1000_STAT("rx_flow_control_xoff", stats.xoffrxc), 93 E1000_STAT("tx_flow_control_xon", stats.xontxc), 94 E1000_STAT("tx_flow_control_xoff", stats.xofftxc), 95 E1000_STAT("rx_csum_offload_good", hw_csum_good), 96 E1000_STAT("rx_csum_offload_errors", hw_csum_err), 97 E1000_STAT("rx_header_split", rx_hdr_split), 98 E1000_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed), 99 E1000_STAT("tx_smbus", stats.mgptc), 100 E1000_STAT("rx_smbus", stats.mgprc), 101 E1000_STAT("dropped_smbus", stats.mgpdc), 102 E1000_STAT("rx_dma_failed", rx_dma_failed), 103 E1000_STAT("tx_dma_failed", tx_dma_failed), 104 E1000_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared), 105 E1000_STAT("uncorr_ecc_errors", uncorr_errors), 106 E1000_STAT("corr_ecc_errors", corr_errors), 107 E1000_STAT("tx_hwtstamp_timeouts", tx_hwtstamp_timeouts), 108 }; 109 110 #define E1000_GLOBAL_STATS_LEN ARRAY_SIZE(e1000_gstrings_stats) 111 #define E1000_STATS_LEN (E1000_GLOBAL_STATS_LEN) 112 static const char e1000_gstrings_test[][ETH_GSTRING_LEN] = { 113 "Register test (offline)", "Eeprom test (offline)", 114 "Interrupt test (offline)", "Loopback test (offline)", 115 "Link test (on/offline)" 116 }; 117 118 #define E1000_TEST_LEN ARRAY_SIZE(e1000_gstrings_test) 119 120 static int e1000_get_link_ksettings(struct net_device *netdev, 121 struct ethtool_link_ksettings *cmd) 122 { 123 struct e1000_adapter *adapter = netdev_priv(netdev); 124 struct e1000_hw *hw = &adapter->hw; 125 u32 speed, supported, advertising; 126 127 if (hw->phy.media_type == e1000_media_type_copper) { 128 supported = (SUPPORTED_10baseT_Half | 129 SUPPORTED_10baseT_Full | 130 SUPPORTED_100baseT_Half | 131 SUPPORTED_100baseT_Full | 132 SUPPORTED_1000baseT_Full | 133 SUPPORTED_Autoneg | 134 SUPPORTED_TP); 135 if (hw->phy.type == e1000_phy_ife) 136 supported &= ~SUPPORTED_1000baseT_Full; 137 advertising = ADVERTISED_TP; 138 139 if (hw->mac.autoneg == 1) { 140 advertising |= ADVERTISED_Autoneg; 141 /* the e1000 autoneg seems to match ethtool nicely */ 142 advertising |= hw->phy.autoneg_advertised; 143 } 144 145 cmd->base.port = PORT_TP; 146 cmd->base.phy_address = hw->phy.addr; 147 } else { 148 supported = (SUPPORTED_1000baseT_Full | 149 SUPPORTED_FIBRE | 150 SUPPORTED_Autoneg); 151 152 advertising = (ADVERTISED_1000baseT_Full | 153 ADVERTISED_FIBRE | 154 ADVERTISED_Autoneg); 155 156 cmd->base.port = PORT_FIBRE; 157 } 158 159 speed = SPEED_UNKNOWN; 160 cmd->base.duplex = DUPLEX_UNKNOWN; 161 162 if (netif_running(netdev)) { 163 if (netif_carrier_ok(netdev)) { 164 speed = adapter->link_speed; 165 cmd->base.duplex = adapter->link_duplex - 1; 166 } 167 } else if (!pm_runtime_suspended(netdev->dev.parent)) { 168 u32 status = er32(STATUS); 169 170 if (status & E1000_STATUS_LU) { 171 if (status & E1000_STATUS_SPEED_1000) 172 speed = SPEED_1000; 173 else if (status & E1000_STATUS_SPEED_100) 174 speed = SPEED_100; 175 else 176 speed = SPEED_10; 177 178 if (status & E1000_STATUS_FD) 179 cmd->base.duplex = DUPLEX_FULL; 180 else 181 cmd->base.duplex = DUPLEX_HALF; 182 } 183 } 184 185 cmd->base.speed = speed; 186 cmd->base.autoneg = ((hw->phy.media_type == e1000_media_type_fiber) || 187 hw->mac.autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE; 188 189 /* MDI-X => 2; MDI =>1; Invalid =>0 */ 190 if ((hw->phy.media_type == e1000_media_type_copper) && 191 netif_carrier_ok(netdev)) 192 cmd->base.eth_tp_mdix = hw->phy.is_mdix ? 193 ETH_TP_MDI_X : ETH_TP_MDI; 194 else 195 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID; 196 197 if (hw->phy.mdix == AUTO_ALL_MODES) 198 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO; 199 else 200 cmd->base.eth_tp_mdix_ctrl = hw->phy.mdix; 201 202 if (hw->phy.media_type != e1000_media_type_copper) 203 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID; 204 205 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 206 supported); 207 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 208 advertising); 209 210 return 0; 211 } 212 213 static int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx) 214 { 215 struct e1000_mac_info *mac = &adapter->hw.mac; 216 217 mac->autoneg = 0; 218 219 /* Make sure dplx is at most 1 bit and lsb of speed is not set 220 * for the switch() below to work 221 */ 222 if ((spd & 1) || (dplx & ~1)) 223 goto err_inval; 224 225 /* Fiber NICs only allow 1000 gbps Full duplex */ 226 if ((adapter->hw.phy.media_type == e1000_media_type_fiber) && 227 (spd != SPEED_1000) && (dplx != DUPLEX_FULL)) { 228 goto err_inval; 229 } 230 231 switch (spd + dplx) { 232 case SPEED_10 + DUPLEX_HALF: 233 mac->forced_speed_duplex = ADVERTISE_10_HALF; 234 break; 235 case SPEED_10 + DUPLEX_FULL: 236 mac->forced_speed_duplex = ADVERTISE_10_FULL; 237 break; 238 case SPEED_100 + DUPLEX_HALF: 239 mac->forced_speed_duplex = ADVERTISE_100_HALF; 240 break; 241 case SPEED_100 + DUPLEX_FULL: 242 mac->forced_speed_duplex = ADVERTISE_100_FULL; 243 break; 244 case SPEED_1000 + DUPLEX_FULL: 245 if (adapter->hw.phy.media_type == e1000_media_type_copper) { 246 mac->autoneg = 1; 247 adapter->hw.phy.autoneg_advertised = 248 ADVERTISE_1000_FULL; 249 } else { 250 mac->forced_speed_duplex = ADVERTISE_1000_FULL; 251 } 252 break; 253 case SPEED_1000 + DUPLEX_HALF: /* not supported */ 254 default: 255 goto err_inval; 256 } 257 258 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */ 259 adapter->hw.phy.mdix = AUTO_ALL_MODES; 260 261 return 0; 262 263 err_inval: 264 e_err("Unsupported Speed/Duplex configuration\n"); 265 return -EINVAL; 266 } 267 268 static int e1000_set_link_ksettings(struct net_device *netdev, 269 const struct ethtool_link_ksettings *cmd) 270 { 271 struct e1000_adapter *adapter = netdev_priv(netdev); 272 struct e1000_hw *hw = &adapter->hw; 273 int ret_val = 0; 274 u32 advertising; 275 276 ethtool_convert_link_mode_to_legacy_u32(&advertising, 277 cmd->link_modes.advertising); 278 279 pm_runtime_get_sync(netdev->dev.parent); 280 281 /* When SoL/IDER sessions are active, autoneg/speed/duplex 282 * cannot be changed 283 */ 284 if (hw->phy.ops.check_reset_block && 285 hw->phy.ops.check_reset_block(hw)) { 286 e_err("Cannot change link characteristics when SoL/IDER is active.\n"); 287 ret_val = -EINVAL; 288 goto out; 289 } 290 291 /* MDI setting is only allowed when autoneg enabled because 292 * some hardware doesn't allow MDI setting when speed or 293 * duplex is forced. 294 */ 295 if (cmd->base.eth_tp_mdix_ctrl) { 296 if (hw->phy.media_type != e1000_media_type_copper) { 297 ret_val = -EOPNOTSUPP; 298 goto out; 299 } 300 301 if ((cmd->base.eth_tp_mdix_ctrl != ETH_TP_MDI_AUTO) && 302 (cmd->base.autoneg != AUTONEG_ENABLE)) { 303 e_err("forcing MDI/MDI-X state is not supported when link speed and/or duplex are forced\n"); 304 ret_val = -EINVAL; 305 goto out; 306 } 307 } 308 309 while (test_and_set_bit(__E1000_RESETTING, &adapter->state)) 310 usleep_range(1000, 2000); 311 312 if (cmd->base.autoneg == AUTONEG_ENABLE) { 313 hw->mac.autoneg = 1; 314 if (hw->phy.media_type == e1000_media_type_fiber) 315 hw->phy.autoneg_advertised = ADVERTISED_1000baseT_Full | 316 ADVERTISED_FIBRE | ADVERTISED_Autoneg; 317 else 318 hw->phy.autoneg_advertised = advertising | 319 ADVERTISED_TP | ADVERTISED_Autoneg; 320 advertising = hw->phy.autoneg_advertised; 321 if (adapter->fc_autoneg) 322 hw->fc.requested_mode = e1000_fc_default; 323 } else { 324 u32 speed = cmd->base.speed; 325 /* calling this overrides forced MDI setting */ 326 if (e1000_set_spd_dplx(adapter, speed, cmd->base.duplex)) { 327 ret_val = -EINVAL; 328 goto out; 329 } 330 } 331 332 /* MDI-X => 2; MDI => 1; Auto => 3 */ 333 if (cmd->base.eth_tp_mdix_ctrl) { 334 /* fix up the value for auto (3 => 0) as zero is mapped 335 * internally to auto 336 */ 337 if (cmd->base.eth_tp_mdix_ctrl == ETH_TP_MDI_AUTO) 338 hw->phy.mdix = AUTO_ALL_MODES; 339 else 340 hw->phy.mdix = cmd->base.eth_tp_mdix_ctrl; 341 } 342 343 /* reset the link */ 344 if (netif_running(adapter->netdev)) { 345 e1000e_down(adapter, true); 346 e1000e_up(adapter); 347 } else { 348 e1000e_reset(adapter); 349 } 350 351 out: 352 pm_runtime_put_sync(netdev->dev.parent); 353 clear_bit(__E1000_RESETTING, &adapter->state); 354 return ret_val; 355 } 356 357 static void e1000_get_pauseparam(struct net_device *netdev, 358 struct ethtool_pauseparam *pause) 359 { 360 struct e1000_adapter *adapter = netdev_priv(netdev); 361 struct e1000_hw *hw = &adapter->hw; 362 363 pause->autoneg = 364 (adapter->fc_autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE); 365 366 if (hw->fc.current_mode == e1000_fc_rx_pause) { 367 pause->rx_pause = 1; 368 } else if (hw->fc.current_mode == e1000_fc_tx_pause) { 369 pause->tx_pause = 1; 370 } else if (hw->fc.current_mode == e1000_fc_full) { 371 pause->rx_pause = 1; 372 pause->tx_pause = 1; 373 } 374 } 375 376 static int e1000_set_pauseparam(struct net_device *netdev, 377 struct ethtool_pauseparam *pause) 378 { 379 struct e1000_adapter *adapter = netdev_priv(netdev); 380 struct e1000_hw *hw = &adapter->hw; 381 int retval = 0; 382 383 adapter->fc_autoneg = pause->autoneg; 384 385 while (test_and_set_bit(__E1000_RESETTING, &adapter->state)) 386 usleep_range(1000, 2000); 387 388 pm_runtime_get_sync(netdev->dev.parent); 389 390 if (adapter->fc_autoneg == AUTONEG_ENABLE) { 391 hw->fc.requested_mode = e1000_fc_default; 392 if (netif_running(adapter->netdev)) { 393 e1000e_down(adapter, true); 394 e1000e_up(adapter); 395 } else { 396 e1000e_reset(adapter); 397 } 398 } else { 399 if (pause->rx_pause && pause->tx_pause) 400 hw->fc.requested_mode = e1000_fc_full; 401 else if (pause->rx_pause && !pause->tx_pause) 402 hw->fc.requested_mode = e1000_fc_rx_pause; 403 else if (!pause->rx_pause && pause->tx_pause) 404 hw->fc.requested_mode = e1000_fc_tx_pause; 405 else if (!pause->rx_pause && !pause->tx_pause) 406 hw->fc.requested_mode = e1000_fc_none; 407 408 hw->fc.current_mode = hw->fc.requested_mode; 409 410 if (hw->phy.media_type == e1000_media_type_fiber) { 411 retval = hw->mac.ops.setup_link(hw); 412 /* implicit goto out */ 413 } else { 414 retval = e1000e_force_mac_fc(hw); 415 if (retval) 416 goto out; 417 e1000e_set_fc_watermarks(hw); 418 } 419 } 420 421 out: 422 pm_runtime_put_sync(netdev->dev.parent); 423 clear_bit(__E1000_RESETTING, &adapter->state); 424 return retval; 425 } 426 427 static u32 e1000_get_msglevel(struct net_device *netdev) 428 { 429 struct e1000_adapter *adapter = netdev_priv(netdev); 430 return adapter->msg_enable; 431 } 432 433 static void e1000_set_msglevel(struct net_device *netdev, u32 data) 434 { 435 struct e1000_adapter *adapter = netdev_priv(netdev); 436 adapter->msg_enable = data; 437 } 438 439 static int e1000_get_regs_len(struct net_device __always_unused *netdev) 440 { 441 #define E1000_REGS_LEN 32 /* overestimate */ 442 return E1000_REGS_LEN * sizeof(u32); 443 } 444 445 static void e1000_get_regs(struct net_device *netdev, 446 struct ethtool_regs *regs, void *p) 447 { 448 struct e1000_adapter *adapter = netdev_priv(netdev); 449 struct e1000_hw *hw = &adapter->hw; 450 u32 *regs_buff = p; 451 u16 phy_data; 452 453 pm_runtime_get_sync(netdev->dev.parent); 454 455 memset(p, 0, E1000_REGS_LEN * sizeof(u32)); 456 457 regs->version = (1u << 24) | 458 (adapter->pdev->revision << 16) | 459 adapter->pdev->device; 460 461 regs_buff[0] = er32(CTRL); 462 regs_buff[1] = er32(STATUS); 463 464 regs_buff[2] = er32(RCTL); 465 regs_buff[3] = er32(RDLEN(0)); 466 regs_buff[4] = er32(RDH(0)); 467 regs_buff[5] = er32(RDT(0)); 468 regs_buff[6] = er32(RDTR); 469 470 regs_buff[7] = er32(TCTL); 471 regs_buff[8] = er32(TDLEN(0)); 472 regs_buff[9] = er32(TDH(0)); 473 regs_buff[10] = er32(TDT(0)); 474 regs_buff[11] = er32(TIDV); 475 476 regs_buff[12] = adapter->hw.phy.type; /* PHY type (IGP=1, M88=0) */ 477 478 /* ethtool doesn't use anything past this point, so all this 479 * code is likely legacy junk for apps that may or may not exist 480 */ 481 if (hw->phy.type == e1000_phy_m88) { 482 e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data); 483 regs_buff[13] = (u32)phy_data; /* cable length */ 484 regs_buff[14] = 0; /* Dummy (to align w/ IGP phy reg dump) */ 485 regs_buff[15] = 0; /* Dummy (to align w/ IGP phy reg dump) */ 486 regs_buff[16] = 0; /* Dummy (to align w/ IGP phy reg dump) */ 487 e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data); 488 regs_buff[17] = (u32)phy_data; /* extended 10bt distance */ 489 regs_buff[18] = regs_buff[13]; /* cable polarity */ 490 regs_buff[19] = 0; /* Dummy (to align w/ IGP phy reg dump) */ 491 regs_buff[20] = regs_buff[17]; /* polarity correction */ 492 /* phy receive errors */ 493 regs_buff[22] = adapter->phy_stats.receive_errors; 494 regs_buff[23] = regs_buff[13]; /* mdix mode */ 495 } 496 regs_buff[21] = 0; /* was idle_errors */ 497 e1e_rphy(hw, MII_STAT1000, &phy_data); 498 regs_buff[24] = (u32)phy_data; /* phy local receiver status */ 499 regs_buff[25] = regs_buff[24]; /* phy remote receiver status */ 500 501 pm_runtime_put_sync(netdev->dev.parent); 502 } 503 504 static int e1000_get_eeprom_len(struct net_device *netdev) 505 { 506 struct e1000_adapter *adapter = netdev_priv(netdev); 507 return adapter->hw.nvm.word_size * 2; 508 } 509 510 static int e1000_get_eeprom(struct net_device *netdev, 511 struct ethtool_eeprom *eeprom, u8 *bytes) 512 { 513 struct e1000_adapter *adapter = netdev_priv(netdev); 514 struct e1000_hw *hw = &adapter->hw; 515 u16 *eeprom_buff; 516 int first_word; 517 int last_word; 518 int ret_val = 0; 519 u16 i; 520 521 if (eeprom->len == 0) 522 return -EINVAL; 523 524 eeprom->magic = adapter->pdev->vendor | (adapter->pdev->device << 16); 525 526 first_word = eeprom->offset >> 1; 527 last_word = (eeprom->offset + eeprom->len - 1) >> 1; 528 529 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1), 530 GFP_KERNEL); 531 if (!eeprom_buff) 532 return -ENOMEM; 533 534 pm_runtime_get_sync(netdev->dev.parent); 535 536 if (hw->nvm.type == e1000_nvm_eeprom_spi) { 537 ret_val = e1000_read_nvm(hw, first_word, 538 last_word - first_word + 1, 539 eeprom_buff); 540 } else { 541 for (i = 0; i < last_word - first_word + 1; i++) { 542 ret_val = e1000_read_nvm(hw, first_word + i, 1, 543 &eeprom_buff[i]); 544 if (ret_val) 545 break; 546 } 547 } 548 549 pm_runtime_put_sync(netdev->dev.parent); 550 551 if (ret_val) { 552 /* a read error occurred, throw away the result */ 553 memset(eeprom_buff, 0xff, sizeof(u16) * 554 (last_word - first_word + 1)); 555 } else { 556 /* Device's eeprom is always little-endian, word addressable */ 557 for (i = 0; i < last_word - first_word + 1; i++) 558 le16_to_cpus(&eeprom_buff[i]); 559 } 560 561 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len); 562 kfree(eeprom_buff); 563 564 return ret_val; 565 } 566 567 static int e1000_set_eeprom(struct net_device *netdev, 568 struct ethtool_eeprom *eeprom, u8 *bytes) 569 { 570 struct e1000_adapter *adapter = netdev_priv(netdev); 571 struct e1000_hw *hw = &adapter->hw; 572 u16 *eeprom_buff; 573 void *ptr; 574 int max_len; 575 int first_word; 576 int last_word; 577 int ret_val = 0; 578 u16 i; 579 580 if (eeprom->len == 0) 581 return -EOPNOTSUPP; 582 583 if (eeprom->magic != 584 (adapter->pdev->vendor | (adapter->pdev->device << 16))) 585 return -EFAULT; 586 587 if (adapter->flags & FLAG_READ_ONLY_NVM) 588 return -EINVAL; 589 590 max_len = hw->nvm.word_size * 2; 591 592 first_word = eeprom->offset >> 1; 593 last_word = (eeprom->offset + eeprom->len - 1) >> 1; 594 eeprom_buff = kmalloc(max_len, GFP_KERNEL); 595 if (!eeprom_buff) 596 return -ENOMEM; 597 598 ptr = (void *)eeprom_buff; 599 600 pm_runtime_get_sync(netdev->dev.parent); 601 602 if (eeprom->offset & 1) { 603 /* need read/modify/write of first changed EEPROM word */ 604 /* only the second byte of the word is being modified */ 605 ret_val = e1000_read_nvm(hw, first_word, 1, &eeprom_buff[0]); 606 ptr++; 607 } 608 if (((eeprom->offset + eeprom->len) & 1) && (!ret_val)) 609 /* need read/modify/write of last changed EEPROM word */ 610 /* only the first byte of the word is being modified */ 611 ret_val = e1000_read_nvm(hw, last_word, 1, 612 &eeprom_buff[last_word - first_word]); 613 614 if (ret_val) 615 goto out; 616 617 /* Device's eeprom is always little-endian, word addressable */ 618 for (i = 0; i < last_word - first_word + 1; i++) 619 le16_to_cpus(&eeprom_buff[i]); 620 621 memcpy(ptr, bytes, eeprom->len); 622 623 for (i = 0; i < last_word - first_word + 1; i++) 624 cpu_to_le16s(&eeprom_buff[i]); 625 626 ret_val = e1000_write_nvm(hw, first_word, 627 last_word - first_word + 1, eeprom_buff); 628 629 if (ret_val) 630 goto out; 631 632 /* Update the checksum over the first part of the EEPROM if needed 633 * and flush shadow RAM for applicable controllers 634 */ 635 if ((first_word <= NVM_CHECKSUM_REG) || 636 (hw->mac.type == e1000_82583) || 637 (hw->mac.type == e1000_82574) || 638 (hw->mac.type == e1000_82573)) 639 ret_val = e1000e_update_nvm_checksum(hw); 640 641 out: 642 pm_runtime_put_sync(netdev->dev.parent); 643 kfree(eeprom_buff); 644 return ret_val; 645 } 646 647 static void e1000_get_drvinfo(struct net_device *netdev, 648 struct ethtool_drvinfo *drvinfo) 649 { 650 struct e1000_adapter *adapter = netdev_priv(netdev); 651 652 strlcpy(drvinfo->driver, e1000e_driver_name, sizeof(drvinfo->driver)); 653 strlcpy(drvinfo->version, e1000e_driver_version, 654 sizeof(drvinfo->version)); 655 656 /* EEPROM image version # is reported as firmware version # for 657 * PCI-E controllers 658 */ 659 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 660 "%d.%d-%d", 661 (adapter->eeprom_vers & 0xF000) >> 12, 662 (adapter->eeprom_vers & 0x0FF0) >> 4, 663 (adapter->eeprom_vers & 0x000F)); 664 665 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 666 sizeof(drvinfo->bus_info)); 667 } 668 669 static void e1000_get_ringparam(struct net_device *netdev, 670 struct ethtool_ringparam *ring) 671 { 672 struct e1000_adapter *adapter = netdev_priv(netdev); 673 674 ring->rx_max_pending = E1000_MAX_RXD; 675 ring->tx_max_pending = E1000_MAX_TXD; 676 ring->rx_pending = adapter->rx_ring_count; 677 ring->tx_pending = adapter->tx_ring_count; 678 } 679 680 static int e1000_set_ringparam(struct net_device *netdev, 681 struct ethtool_ringparam *ring) 682 { 683 struct e1000_adapter *adapter = netdev_priv(netdev); 684 struct e1000_ring *temp_tx = NULL, *temp_rx = NULL; 685 int err = 0, size = sizeof(struct e1000_ring); 686 bool set_tx = false, set_rx = false; 687 u16 new_rx_count, new_tx_count; 688 689 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending)) 690 return -EINVAL; 691 692 new_rx_count = clamp_t(u32, ring->rx_pending, E1000_MIN_RXD, 693 E1000_MAX_RXD); 694 new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE); 695 696 new_tx_count = clamp_t(u32, ring->tx_pending, E1000_MIN_TXD, 697 E1000_MAX_TXD); 698 new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE); 699 700 if ((new_tx_count == adapter->tx_ring_count) && 701 (new_rx_count == adapter->rx_ring_count)) 702 /* nothing to do */ 703 return 0; 704 705 while (test_and_set_bit(__E1000_RESETTING, &adapter->state)) 706 usleep_range(1000, 2000); 707 708 if (!netif_running(adapter->netdev)) { 709 /* Set counts now and allocate resources during open() */ 710 adapter->tx_ring->count = new_tx_count; 711 adapter->rx_ring->count = new_rx_count; 712 adapter->tx_ring_count = new_tx_count; 713 adapter->rx_ring_count = new_rx_count; 714 goto clear_reset; 715 } 716 717 set_tx = (new_tx_count != adapter->tx_ring_count); 718 set_rx = (new_rx_count != adapter->rx_ring_count); 719 720 /* Allocate temporary storage for ring updates */ 721 if (set_tx) { 722 temp_tx = vmalloc(size); 723 if (!temp_tx) { 724 err = -ENOMEM; 725 goto free_temp; 726 } 727 } 728 if (set_rx) { 729 temp_rx = vmalloc(size); 730 if (!temp_rx) { 731 err = -ENOMEM; 732 goto free_temp; 733 } 734 } 735 736 pm_runtime_get_sync(netdev->dev.parent); 737 738 e1000e_down(adapter, true); 739 740 /* We can't just free everything and then setup again, because the 741 * ISRs in MSI-X mode get passed pointers to the Tx and Rx ring 742 * structs. First, attempt to allocate new resources... 743 */ 744 if (set_tx) { 745 memcpy(temp_tx, adapter->tx_ring, size); 746 temp_tx->count = new_tx_count; 747 err = e1000e_setup_tx_resources(temp_tx); 748 if (err) 749 goto err_setup; 750 } 751 if (set_rx) { 752 memcpy(temp_rx, adapter->rx_ring, size); 753 temp_rx->count = new_rx_count; 754 err = e1000e_setup_rx_resources(temp_rx); 755 if (err) 756 goto err_setup_rx; 757 } 758 759 /* ...then free the old resources and copy back any new ring data */ 760 if (set_tx) { 761 e1000e_free_tx_resources(adapter->tx_ring); 762 memcpy(adapter->tx_ring, temp_tx, size); 763 adapter->tx_ring_count = new_tx_count; 764 } 765 if (set_rx) { 766 e1000e_free_rx_resources(adapter->rx_ring); 767 memcpy(adapter->rx_ring, temp_rx, size); 768 adapter->rx_ring_count = new_rx_count; 769 } 770 771 err_setup_rx: 772 if (err && set_tx) 773 e1000e_free_tx_resources(temp_tx); 774 err_setup: 775 e1000e_up(adapter); 776 pm_runtime_put_sync(netdev->dev.parent); 777 free_temp: 778 vfree(temp_tx); 779 vfree(temp_rx); 780 clear_reset: 781 clear_bit(__E1000_RESETTING, &adapter->state); 782 return err; 783 } 784 785 static bool reg_pattern_test(struct e1000_adapter *adapter, u64 *data, 786 int reg, int offset, u32 mask, u32 write) 787 { 788 u32 pat, val; 789 static const u32 test[] = { 790 0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF 791 }; 792 for (pat = 0; pat < ARRAY_SIZE(test); pat++) { 793 E1000_WRITE_REG_ARRAY(&adapter->hw, reg, offset, 794 (test[pat] & write)); 795 val = E1000_READ_REG_ARRAY(&adapter->hw, reg, offset); 796 if (val != (test[pat] & write & mask)) { 797 e_err("pattern test failed (reg 0x%05X): got 0x%08X expected 0x%08X\n", 798 reg + (offset << 2), val, 799 (test[pat] & write & mask)); 800 *data = reg; 801 return true; 802 } 803 } 804 return false; 805 } 806 807 static bool reg_set_and_check(struct e1000_adapter *adapter, u64 *data, 808 int reg, u32 mask, u32 write) 809 { 810 u32 val; 811 812 __ew32(&adapter->hw, reg, write & mask); 813 val = __er32(&adapter->hw, reg); 814 if ((write & mask) != (val & mask)) { 815 e_err("set/check test failed (reg 0x%05X): got 0x%08X expected 0x%08X\n", 816 reg, (val & mask), (write & mask)); 817 *data = reg; 818 return true; 819 } 820 return false; 821 } 822 823 #define REG_PATTERN_TEST_ARRAY(reg, offset, mask, write) \ 824 do { \ 825 if (reg_pattern_test(adapter, data, reg, offset, mask, write)) \ 826 return 1; \ 827 } while (0) 828 #define REG_PATTERN_TEST(reg, mask, write) \ 829 REG_PATTERN_TEST_ARRAY(reg, 0, mask, write) 830 831 #define REG_SET_AND_CHECK(reg, mask, write) \ 832 do { \ 833 if (reg_set_and_check(adapter, data, reg, mask, write)) \ 834 return 1; \ 835 } while (0) 836 837 static int e1000_reg_test(struct e1000_adapter *adapter, u64 *data) 838 { 839 struct e1000_hw *hw = &adapter->hw; 840 struct e1000_mac_info *mac = &adapter->hw.mac; 841 u32 value; 842 u32 before; 843 u32 after; 844 u32 i; 845 u32 toggle; 846 u32 mask; 847 u32 wlock_mac = 0; 848 849 /* The status register is Read Only, so a write should fail. 850 * Some bits that get toggled are ignored. There are several bits 851 * on newer hardware that are r/w. 852 */ 853 switch (mac->type) { 854 case e1000_82571: 855 case e1000_82572: 856 case e1000_80003es2lan: 857 toggle = 0x7FFFF3FF; 858 break; 859 default: 860 toggle = 0x7FFFF033; 861 break; 862 } 863 864 before = er32(STATUS); 865 value = (er32(STATUS) & toggle); 866 ew32(STATUS, toggle); 867 after = er32(STATUS) & toggle; 868 if (value != after) { 869 e_err("failed STATUS register test got: 0x%08X expected: 0x%08X\n", 870 after, value); 871 *data = 1; 872 return 1; 873 } 874 /* restore previous status */ 875 ew32(STATUS, before); 876 877 if (!(adapter->flags & FLAG_IS_ICH)) { 878 REG_PATTERN_TEST(E1000_FCAL, 0xFFFFFFFF, 0xFFFFFFFF); 879 REG_PATTERN_TEST(E1000_FCAH, 0x0000FFFF, 0xFFFFFFFF); 880 REG_PATTERN_TEST(E1000_FCT, 0x0000FFFF, 0xFFFFFFFF); 881 REG_PATTERN_TEST(E1000_VET, 0x0000FFFF, 0xFFFFFFFF); 882 } 883 884 REG_PATTERN_TEST(E1000_RDTR, 0x0000FFFF, 0xFFFFFFFF); 885 REG_PATTERN_TEST(E1000_RDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF); 886 REG_PATTERN_TEST(E1000_RDLEN(0), 0x000FFF80, 0x000FFFFF); 887 REG_PATTERN_TEST(E1000_RDH(0), 0x0000FFFF, 0x0000FFFF); 888 REG_PATTERN_TEST(E1000_RDT(0), 0x0000FFFF, 0x0000FFFF); 889 REG_PATTERN_TEST(E1000_FCRTH, 0x0000FFF8, 0x0000FFF8); 890 REG_PATTERN_TEST(E1000_FCTTV, 0x0000FFFF, 0x0000FFFF); 891 REG_PATTERN_TEST(E1000_TIPG, 0x3FFFFFFF, 0x3FFFFFFF); 892 REG_PATTERN_TEST(E1000_TDBAH(0), 0xFFFFFFFF, 0xFFFFFFFF); 893 REG_PATTERN_TEST(E1000_TDLEN(0), 0x000FFF80, 0x000FFFFF); 894 895 REG_SET_AND_CHECK(E1000_RCTL, 0xFFFFFFFF, 0x00000000); 896 897 before = ((adapter->flags & FLAG_IS_ICH) ? 0x06C3B33E : 0x06DFB3FE); 898 REG_SET_AND_CHECK(E1000_RCTL, before, 0x003FFFFB); 899 REG_SET_AND_CHECK(E1000_TCTL, 0xFFFFFFFF, 0x00000000); 900 901 REG_SET_AND_CHECK(E1000_RCTL, before, 0xFFFFFFFF); 902 REG_PATTERN_TEST(E1000_RDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF); 903 if (!(adapter->flags & FLAG_IS_ICH)) 904 REG_PATTERN_TEST(E1000_TXCW, 0xC000FFFF, 0x0000FFFF); 905 REG_PATTERN_TEST(E1000_TDBAL(0), 0xFFFFFFF0, 0xFFFFFFFF); 906 REG_PATTERN_TEST(E1000_TIDV, 0x0000FFFF, 0x0000FFFF); 907 mask = 0x8003FFFF; 908 switch (mac->type) { 909 case e1000_ich10lan: 910 case e1000_pchlan: 911 case e1000_pch2lan: 912 case e1000_pch_lpt: 913 case e1000_pch_spt: 914 /* fall through */ 915 case e1000_pch_cnp: 916 mask |= BIT(18); 917 break; 918 default: 919 break; 920 } 921 922 if (mac->type >= e1000_pch_lpt) 923 wlock_mac = (er32(FWSM) & E1000_FWSM_WLOCK_MAC_MASK) >> 924 E1000_FWSM_WLOCK_MAC_SHIFT; 925 926 for (i = 0; i < mac->rar_entry_count; i++) { 927 if (mac->type >= e1000_pch_lpt) { 928 /* Cannot test write-protected SHRAL[n] registers */ 929 if ((wlock_mac == 1) || (wlock_mac && (i > wlock_mac))) 930 continue; 931 932 /* SHRAH[9] different than the others */ 933 if (i == 10) 934 mask |= BIT(30); 935 else 936 mask &= ~BIT(30); 937 } 938 if (mac->type == e1000_pch2lan) { 939 /* SHRAH[0,1,2] different than previous */ 940 if (i == 1) 941 mask &= 0xFFF4FFFF; 942 /* SHRAH[3] different than SHRAH[0,1,2] */ 943 if (i == 4) 944 mask |= BIT(30); 945 /* RAR[1-6] owned by management engine - skipping */ 946 if (i > 0) 947 i += 6; 948 } 949 950 REG_PATTERN_TEST_ARRAY(E1000_RA, ((i << 1) + 1), mask, 951 0xFFFFFFFF); 952 /* reset index to actual value */ 953 if ((mac->type == e1000_pch2lan) && (i > 6)) 954 i -= 6; 955 } 956 957 for (i = 0; i < mac->mta_reg_count; i++) 958 REG_PATTERN_TEST_ARRAY(E1000_MTA, i, 0xFFFFFFFF, 0xFFFFFFFF); 959 960 *data = 0; 961 962 return 0; 963 } 964 965 static int e1000_eeprom_test(struct e1000_adapter *adapter, u64 *data) 966 { 967 u16 temp; 968 u16 checksum = 0; 969 u16 i; 970 971 *data = 0; 972 /* Read and add up the contents of the EEPROM */ 973 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) { 974 if ((e1000_read_nvm(&adapter->hw, i, 1, &temp)) < 0) { 975 *data = 1; 976 return *data; 977 } 978 checksum += temp; 979 } 980 981 /* If Checksum is not Correct return error else test passed */ 982 if ((checksum != (u16)NVM_SUM) && !(*data)) 983 *data = 2; 984 985 return *data; 986 } 987 988 static irqreturn_t e1000_test_intr(int __always_unused irq, void *data) 989 { 990 struct net_device *netdev = (struct net_device *)data; 991 struct e1000_adapter *adapter = netdev_priv(netdev); 992 struct e1000_hw *hw = &adapter->hw; 993 994 adapter->test_icr |= er32(ICR); 995 996 return IRQ_HANDLED; 997 } 998 999 static int e1000_intr_test(struct e1000_adapter *adapter, u64 *data) 1000 { 1001 struct net_device *netdev = adapter->netdev; 1002 struct e1000_hw *hw = &adapter->hw; 1003 u32 mask; 1004 u32 shared_int = 1; 1005 u32 irq = adapter->pdev->irq; 1006 int i; 1007 int ret_val = 0; 1008 int int_mode = E1000E_INT_MODE_LEGACY; 1009 1010 *data = 0; 1011 1012 /* NOTE: we don't test MSI/MSI-X interrupts here, yet */ 1013 if (adapter->int_mode == E1000E_INT_MODE_MSIX) { 1014 int_mode = adapter->int_mode; 1015 e1000e_reset_interrupt_capability(adapter); 1016 adapter->int_mode = E1000E_INT_MODE_LEGACY; 1017 e1000e_set_interrupt_capability(adapter); 1018 } 1019 /* Hook up test interrupt handler just for this test */ 1020 if (!request_irq(irq, e1000_test_intr, IRQF_PROBE_SHARED, netdev->name, 1021 netdev)) { 1022 shared_int = 0; 1023 } else if (request_irq(irq, e1000_test_intr, IRQF_SHARED, netdev->name, 1024 netdev)) { 1025 *data = 1; 1026 ret_val = -1; 1027 goto out; 1028 } 1029 e_info("testing %s interrupt\n", (shared_int ? "shared" : "unshared")); 1030 1031 /* Disable all the interrupts */ 1032 ew32(IMC, 0xFFFFFFFF); 1033 e1e_flush(); 1034 usleep_range(10000, 20000); 1035 1036 /* Test each interrupt */ 1037 for (i = 0; i < 10; i++) { 1038 /* Interrupt to test */ 1039 mask = BIT(i); 1040 1041 if (adapter->flags & FLAG_IS_ICH) { 1042 switch (mask) { 1043 case E1000_ICR_RXSEQ: 1044 continue; 1045 case 0x00000100: 1046 if (adapter->hw.mac.type == e1000_ich8lan || 1047 adapter->hw.mac.type == e1000_ich9lan) 1048 continue; 1049 break; 1050 default: 1051 break; 1052 } 1053 } 1054 1055 if (!shared_int) { 1056 /* Disable the interrupt to be reported in 1057 * the cause register and then force the same 1058 * interrupt and see if one gets posted. If 1059 * an interrupt was posted to the bus, the 1060 * test failed. 1061 */ 1062 adapter->test_icr = 0; 1063 ew32(IMC, mask); 1064 ew32(ICS, mask); 1065 e1e_flush(); 1066 usleep_range(10000, 20000); 1067 1068 if (adapter->test_icr & mask) { 1069 *data = 3; 1070 break; 1071 } 1072 } 1073 1074 /* Enable the interrupt to be reported in 1075 * the cause register and then force the same 1076 * interrupt and see if one gets posted. If 1077 * an interrupt was not posted to the bus, the 1078 * test failed. 1079 */ 1080 adapter->test_icr = 0; 1081 ew32(IMS, mask); 1082 ew32(ICS, mask); 1083 e1e_flush(); 1084 usleep_range(10000, 20000); 1085 1086 if (!(adapter->test_icr & mask)) { 1087 *data = 4; 1088 break; 1089 } 1090 1091 if (!shared_int) { 1092 /* Disable the other interrupts to be reported in 1093 * the cause register and then force the other 1094 * interrupts and see if any get posted. If 1095 * an interrupt was posted to the bus, the 1096 * test failed. 1097 */ 1098 adapter->test_icr = 0; 1099 ew32(IMC, ~mask & 0x00007FFF); 1100 ew32(ICS, ~mask & 0x00007FFF); 1101 e1e_flush(); 1102 usleep_range(10000, 20000); 1103 1104 if (adapter->test_icr) { 1105 *data = 5; 1106 break; 1107 } 1108 } 1109 } 1110 1111 /* Disable all the interrupts */ 1112 ew32(IMC, 0xFFFFFFFF); 1113 e1e_flush(); 1114 usleep_range(10000, 20000); 1115 1116 /* Unhook test interrupt handler */ 1117 free_irq(irq, netdev); 1118 1119 out: 1120 if (int_mode == E1000E_INT_MODE_MSIX) { 1121 e1000e_reset_interrupt_capability(adapter); 1122 adapter->int_mode = int_mode; 1123 e1000e_set_interrupt_capability(adapter); 1124 } 1125 1126 return ret_val; 1127 } 1128 1129 static void e1000_free_desc_rings(struct e1000_adapter *adapter) 1130 { 1131 struct e1000_ring *tx_ring = &adapter->test_tx_ring; 1132 struct e1000_ring *rx_ring = &adapter->test_rx_ring; 1133 struct pci_dev *pdev = adapter->pdev; 1134 struct e1000_buffer *buffer_info; 1135 int i; 1136 1137 if (tx_ring->desc && tx_ring->buffer_info) { 1138 for (i = 0; i < tx_ring->count; i++) { 1139 buffer_info = &tx_ring->buffer_info[i]; 1140 1141 if (buffer_info->dma) 1142 dma_unmap_single(&pdev->dev, 1143 buffer_info->dma, 1144 buffer_info->length, 1145 DMA_TO_DEVICE); 1146 if (buffer_info->skb) 1147 dev_kfree_skb(buffer_info->skb); 1148 } 1149 } 1150 1151 if (rx_ring->desc && rx_ring->buffer_info) { 1152 for (i = 0; i < rx_ring->count; i++) { 1153 buffer_info = &rx_ring->buffer_info[i]; 1154 1155 if (buffer_info->dma) 1156 dma_unmap_single(&pdev->dev, 1157 buffer_info->dma, 1158 2048, DMA_FROM_DEVICE); 1159 if (buffer_info->skb) 1160 dev_kfree_skb(buffer_info->skb); 1161 } 1162 } 1163 1164 if (tx_ring->desc) { 1165 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc, 1166 tx_ring->dma); 1167 tx_ring->desc = NULL; 1168 } 1169 if (rx_ring->desc) { 1170 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc, 1171 rx_ring->dma); 1172 rx_ring->desc = NULL; 1173 } 1174 1175 kfree(tx_ring->buffer_info); 1176 tx_ring->buffer_info = NULL; 1177 kfree(rx_ring->buffer_info); 1178 rx_ring->buffer_info = NULL; 1179 } 1180 1181 static int e1000_setup_desc_rings(struct e1000_adapter *adapter) 1182 { 1183 struct e1000_ring *tx_ring = &adapter->test_tx_ring; 1184 struct e1000_ring *rx_ring = &adapter->test_rx_ring; 1185 struct pci_dev *pdev = adapter->pdev; 1186 struct e1000_hw *hw = &adapter->hw; 1187 u32 rctl; 1188 int i; 1189 int ret_val; 1190 1191 /* Setup Tx descriptor ring and Tx buffers */ 1192 1193 if (!tx_ring->count) 1194 tx_ring->count = E1000_DEFAULT_TXD; 1195 1196 tx_ring->buffer_info = kcalloc(tx_ring->count, 1197 sizeof(struct e1000_buffer), GFP_KERNEL); 1198 if (!tx_ring->buffer_info) { 1199 ret_val = 1; 1200 goto err_nomem; 1201 } 1202 1203 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc); 1204 tx_ring->size = ALIGN(tx_ring->size, 4096); 1205 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size, 1206 &tx_ring->dma, GFP_KERNEL); 1207 if (!tx_ring->desc) { 1208 ret_val = 2; 1209 goto err_nomem; 1210 } 1211 tx_ring->next_to_use = 0; 1212 tx_ring->next_to_clean = 0; 1213 1214 ew32(TDBAL(0), ((u64)tx_ring->dma & 0x00000000FFFFFFFF)); 1215 ew32(TDBAH(0), ((u64)tx_ring->dma >> 32)); 1216 ew32(TDLEN(0), tx_ring->count * sizeof(struct e1000_tx_desc)); 1217 ew32(TDH(0), 0); 1218 ew32(TDT(0), 0); 1219 ew32(TCTL, E1000_TCTL_PSP | E1000_TCTL_EN | E1000_TCTL_MULR | 1220 E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT | 1221 E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT); 1222 1223 for (i = 0; i < tx_ring->count; i++) { 1224 struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i); 1225 struct sk_buff *skb; 1226 unsigned int skb_size = 1024; 1227 1228 skb = alloc_skb(skb_size, GFP_KERNEL); 1229 if (!skb) { 1230 ret_val = 3; 1231 goto err_nomem; 1232 } 1233 skb_put(skb, skb_size); 1234 tx_ring->buffer_info[i].skb = skb; 1235 tx_ring->buffer_info[i].length = skb->len; 1236 tx_ring->buffer_info[i].dma = 1237 dma_map_single(&pdev->dev, skb->data, skb->len, 1238 DMA_TO_DEVICE); 1239 if (dma_mapping_error(&pdev->dev, 1240 tx_ring->buffer_info[i].dma)) { 1241 ret_val = 4; 1242 goto err_nomem; 1243 } 1244 tx_desc->buffer_addr = cpu_to_le64(tx_ring->buffer_info[i].dma); 1245 tx_desc->lower.data = cpu_to_le32(skb->len); 1246 tx_desc->lower.data |= cpu_to_le32(E1000_TXD_CMD_EOP | 1247 E1000_TXD_CMD_IFCS | 1248 E1000_TXD_CMD_RS); 1249 tx_desc->upper.data = 0; 1250 } 1251 1252 /* Setup Rx descriptor ring and Rx buffers */ 1253 1254 if (!rx_ring->count) 1255 rx_ring->count = E1000_DEFAULT_RXD; 1256 1257 rx_ring->buffer_info = kcalloc(rx_ring->count, 1258 sizeof(struct e1000_buffer), GFP_KERNEL); 1259 if (!rx_ring->buffer_info) { 1260 ret_val = 5; 1261 goto err_nomem; 1262 } 1263 1264 rx_ring->size = rx_ring->count * sizeof(union e1000_rx_desc_extended); 1265 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size, 1266 &rx_ring->dma, GFP_KERNEL); 1267 if (!rx_ring->desc) { 1268 ret_val = 6; 1269 goto err_nomem; 1270 } 1271 rx_ring->next_to_use = 0; 1272 rx_ring->next_to_clean = 0; 1273 1274 rctl = er32(RCTL); 1275 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX)) 1276 ew32(RCTL, rctl & ~E1000_RCTL_EN); 1277 ew32(RDBAL(0), ((u64)rx_ring->dma & 0xFFFFFFFF)); 1278 ew32(RDBAH(0), ((u64)rx_ring->dma >> 32)); 1279 ew32(RDLEN(0), rx_ring->size); 1280 ew32(RDH(0), 0); 1281 ew32(RDT(0), 0); 1282 rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 | 1283 E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_LPE | 1284 E1000_RCTL_SBP | E1000_RCTL_SECRC | 1285 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF | 1286 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT); 1287 ew32(RCTL, rctl); 1288 1289 for (i = 0; i < rx_ring->count; i++) { 1290 union e1000_rx_desc_extended *rx_desc; 1291 struct sk_buff *skb; 1292 1293 skb = alloc_skb(2048 + NET_IP_ALIGN, GFP_KERNEL); 1294 if (!skb) { 1295 ret_val = 7; 1296 goto err_nomem; 1297 } 1298 skb_reserve(skb, NET_IP_ALIGN); 1299 rx_ring->buffer_info[i].skb = skb; 1300 rx_ring->buffer_info[i].dma = 1301 dma_map_single(&pdev->dev, skb->data, 2048, 1302 DMA_FROM_DEVICE); 1303 if (dma_mapping_error(&pdev->dev, 1304 rx_ring->buffer_info[i].dma)) { 1305 ret_val = 8; 1306 goto err_nomem; 1307 } 1308 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i); 1309 rx_desc->read.buffer_addr = 1310 cpu_to_le64(rx_ring->buffer_info[i].dma); 1311 memset(skb->data, 0x00, skb->len); 1312 } 1313 1314 return 0; 1315 1316 err_nomem: 1317 e1000_free_desc_rings(adapter); 1318 return ret_val; 1319 } 1320 1321 static void e1000_phy_disable_receiver(struct e1000_adapter *adapter) 1322 { 1323 /* Write out to PHY registers 29 and 30 to disable the Receiver. */ 1324 e1e_wphy(&adapter->hw, 29, 0x001F); 1325 e1e_wphy(&adapter->hw, 30, 0x8FFC); 1326 e1e_wphy(&adapter->hw, 29, 0x001A); 1327 e1e_wphy(&adapter->hw, 30, 0x8FF0); 1328 } 1329 1330 static int e1000_integrated_phy_loopback(struct e1000_adapter *adapter) 1331 { 1332 struct e1000_hw *hw = &adapter->hw; 1333 u32 ctrl_reg = 0; 1334 u16 phy_reg = 0; 1335 s32 ret_val = 0; 1336 1337 hw->mac.autoneg = 0; 1338 1339 if (hw->phy.type == e1000_phy_ife) { 1340 /* force 100, set loopback */ 1341 e1e_wphy(hw, MII_BMCR, 0x6100); 1342 1343 /* Now set up the MAC to the same speed/duplex as the PHY. */ 1344 ctrl_reg = er32(CTRL); 1345 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */ 1346 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */ 1347 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */ 1348 E1000_CTRL_SPD_100 |/* Force Speed to 100 */ 1349 E1000_CTRL_FD); /* Force Duplex to FULL */ 1350 1351 ew32(CTRL, ctrl_reg); 1352 e1e_flush(); 1353 usleep_range(500, 1000); 1354 1355 return 0; 1356 } 1357 1358 /* Specific PHY configuration for loopback */ 1359 switch (hw->phy.type) { 1360 case e1000_phy_m88: 1361 /* Auto-MDI/MDIX Off */ 1362 e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, 0x0808); 1363 /* reset to update Auto-MDI/MDIX */ 1364 e1e_wphy(hw, MII_BMCR, 0x9140); 1365 /* autoneg off */ 1366 e1e_wphy(hw, MII_BMCR, 0x8140); 1367 break; 1368 case e1000_phy_gg82563: 1369 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x1CC); 1370 break; 1371 case e1000_phy_bm: 1372 /* Set Default MAC Interface speed to 1GB */ 1373 e1e_rphy(hw, PHY_REG(2, 21), &phy_reg); 1374 phy_reg &= ~0x0007; 1375 phy_reg |= 0x006; 1376 e1e_wphy(hw, PHY_REG(2, 21), phy_reg); 1377 /* Assert SW reset for above settings to take effect */ 1378 hw->phy.ops.commit(hw); 1379 usleep_range(1000, 2000); 1380 /* Force Full Duplex */ 1381 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg); 1382 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x000C); 1383 /* Set Link Up (in force link) */ 1384 e1e_rphy(hw, PHY_REG(776, 16), &phy_reg); 1385 e1e_wphy(hw, PHY_REG(776, 16), phy_reg | 0x0040); 1386 /* Force Link */ 1387 e1e_rphy(hw, PHY_REG(769, 16), &phy_reg); 1388 e1e_wphy(hw, PHY_REG(769, 16), phy_reg | 0x0040); 1389 /* Set Early Link Enable */ 1390 e1e_rphy(hw, PHY_REG(769, 20), &phy_reg); 1391 e1e_wphy(hw, PHY_REG(769, 20), phy_reg | 0x0400); 1392 break; 1393 case e1000_phy_82577: 1394 case e1000_phy_82578: 1395 /* Workaround: K1 must be disabled for stable 1Gbps operation */ 1396 ret_val = hw->phy.ops.acquire(hw); 1397 if (ret_val) { 1398 e_err("Cannot setup 1Gbps loopback.\n"); 1399 return ret_val; 1400 } 1401 e1000_configure_k1_ich8lan(hw, false); 1402 hw->phy.ops.release(hw); 1403 break; 1404 case e1000_phy_82579: 1405 /* Disable PHY energy detect power down */ 1406 e1e_rphy(hw, PHY_REG(0, 21), &phy_reg); 1407 e1e_wphy(hw, PHY_REG(0, 21), phy_reg & ~BIT(3)); 1408 /* Disable full chip energy detect */ 1409 e1e_rphy(hw, PHY_REG(776, 18), &phy_reg); 1410 e1e_wphy(hw, PHY_REG(776, 18), phy_reg | 1); 1411 /* Enable loopback on the PHY */ 1412 e1e_wphy(hw, I82577_PHY_LBK_CTRL, 0x8001); 1413 break; 1414 default: 1415 break; 1416 } 1417 1418 /* force 1000, set loopback */ 1419 e1e_wphy(hw, MII_BMCR, 0x4140); 1420 msleep(250); 1421 1422 /* Now set up the MAC to the same speed/duplex as the PHY. */ 1423 ctrl_reg = er32(CTRL); 1424 ctrl_reg &= ~E1000_CTRL_SPD_SEL; /* Clear the speed sel bits */ 1425 ctrl_reg |= (E1000_CTRL_FRCSPD | /* Set the Force Speed Bit */ 1426 E1000_CTRL_FRCDPX | /* Set the Force Duplex Bit */ 1427 E1000_CTRL_SPD_1000 |/* Force Speed to 1000 */ 1428 E1000_CTRL_FD); /* Force Duplex to FULL */ 1429 1430 if (adapter->flags & FLAG_IS_ICH) 1431 ctrl_reg |= E1000_CTRL_SLU; /* Set Link Up */ 1432 1433 if (hw->phy.media_type == e1000_media_type_copper && 1434 hw->phy.type == e1000_phy_m88) { 1435 ctrl_reg |= E1000_CTRL_ILOS; /* Invert Loss of Signal */ 1436 } else { 1437 /* Set the ILOS bit on the fiber Nic if half duplex link is 1438 * detected. 1439 */ 1440 if ((er32(STATUS) & E1000_STATUS_FD) == 0) 1441 ctrl_reg |= (E1000_CTRL_ILOS | E1000_CTRL_SLU); 1442 } 1443 1444 ew32(CTRL, ctrl_reg); 1445 1446 /* Disable the receiver on the PHY so when a cable is plugged in, the 1447 * PHY does not begin to autoneg when a cable is reconnected to the NIC. 1448 */ 1449 if (hw->phy.type == e1000_phy_m88) 1450 e1000_phy_disable_receiver(adapter); 1451 1452 usleep_range(500, 1000); 1453 1454 return 0; 1455 } 1456 1457 static int e1000_set_82571_fiber_loopback(struct e1000_adapter *adapter) 1458 { 1459 struct e1000_hw *hw = &adapter->hw; 1460 u32 ctrl = er32(CTRL); 1461 int link; 1462 1463 /* special requirements for 82571/82572 fiber adapters */ 1464 1465 /* jump through hoops to make sure link is up because serdes 1466 * link is hardwired up 1467 */ 1468 ctrl |= E1000_CTRL_SLU; 1469 ew32(CTRL, ctrl); 1470 1471 /* disable autoneg */ 1472 ctrl = er32(TXCW); 1473 ctrl &= ~BIT(31); 1474 ew32(TXCW, ctrl); 1475 1476 link = (er32(STATUS) & E1000_STATUS_LU); 1477 1478 if (!link) { 1479 /* set invert loss of signal */ 1480 ctrl = er32(CTRL); 1481 ctrl |= E1000_CTRL_ILOS; 1482 ew32(CTRL, ctrl); 1483 } 1484 1485 /* special write to serdes control register to enable SerDes analog 1486 * loopback 1487 */ 1488 ew32(SCTL, E1000_SCTL_ENABLE_SERDES_LOOPBACK); 1489 e1e_flush(); 1490 usleep_range(10000, 20000); 1491 1492 return 0; 1493 } 1494 1495 /* only call this for fiber/serdes connections to es2lan */ 1496 static int e1000_set_es2lan_mac_loopback(struct e1000_adapter *adapter) 1497 { 1498 struct e1000_hw *hw = &adapter->hw; 1499 u32 ctrlext = er32(CTRL_EXT); 1500 u32 ctrl = er32(CTRL); 1501 1502 /* save CTRL_EXT to restore later, reuse an empty variable (unused 1503 * on mac_type 80003es2lan) 1504 */ 1505 adapter->tx_fifo_head = ctrlext; 1506 1507 /* clear the serdes mode bits, putting the device into mac loopback */ 1508 ctrlext &= ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES; 1509 ew32(CTRL_EXT, ctrlext); 1510 1511 /* force speed to 1000/FD, link up */ 1512 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100); 1513 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | 1514 E1000_CTRL_SPD_1000 | E1000_CTRL_FD); 1515 ew32(CTRL, ctrl); 1516 1517 /* set mac loopback */ 1518 ctrl = er32(RCTL); 1519 ctrl |= E1000_RCTL_LBM_MAC; 1520 ew32(RCTL, ctrl); 1521 1522 /* set testing mode parameters (no need to reset later) */ 1523 #define KMRNCTRLSTA_OPMODE (0x1F << 16) 1524 #define KMRNCTRLSTA_OPMODE_1GB_FD_GMII 0x0582 1525 ew32(KMRNCTRLSTA, 1526 (KMRNCTRLSTA_OPMODE | KMRNCTRLSTA_OPMODE_1GB_FD_GMII)); 1527 1528 return 0; 1529 } 1530 1531 static int e1000_setup_loopback_test(struct e1000_adapter *adapter) 1532 { 1533 struct e1000_hw *hw = &adapter->hw; 1534 u32 rctl, fext_nvm11, tarc0; 1535 1536 if (hw->mac.type >= e1000_pch_spt) { 1537 fext_nvm11 = er32(FEXTNVM11); 1538 fext_nvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX; 1539 ew32(FEXTNVM11, fext_nvm11); 1540 tarc0 = er32(TARC(0)); 1541 /* clear bits 28 & 29 (control of MULR concurrent requests) */ 1542 tarc0 &= 0xcfffffff; 1543 /* set bit 29 (value of MULR requests is now 2) */ 1544 tarc0 |= 0x20000000; 1545 ew32(TARC(0), tarc0); 1546 } 1547 if (hw->phy.media_type == e1000_media_type_fiber || 1548 hw->phy.media_type == e1000_media_type_internal_serdes) { 1549 switch (hw->mac.type) { 1550 case e1000_80003es2lan: 1551 return e1000_set_es2lan_mac_loopback(adapter); 1552 case e1000_82571: 1553 case e1000_82572: 1554 return e1000_set_82571_fiber_loopback(adapter); 1555 default: 1556 rctl = er32(RCTL); 1557 rctl |= E1000_RCTL_LBM_TCVR; 1558 ew32(RCTL, rctl); 1559 return 0; 1560 } 1561 } else if (hw->phy.media_type == e1000_media_type_copper) { 1562 return e1000_integrated_phy_loopback(adapter); 1563 } 1564 1565 return 7; 1566 } 1567 1568 static void e1000_loopback_cleanup(struct e1000_adapter *adapter) 1569 { 1570 struct e1000_hw *hw = &adapter->hw; 1571 u32 rctl, fext_nvm11, tarc0; 1572 u16 phy_reg; 1573 1574 rctl = er32(RCTL); 1575 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC); 1576 ew32(RCTL, rctl); 1577 1578 switch (hw->mac.type) { 1579 case e1000_pch_spt: 1580 case e1000_pch_cnp: 1581 fext_nvm11 = er32(FEXTNVM11); 1582 fext_nvm11 &= ~E1000_FEXTNVM11_DISABLE_MULR_FIX; 1583 ew32(FEXTNVM11, fext_nvm11); 1584 tarc0 = er32(TARC(0)); 1585 /* clear bits 28 & 29 (control of MULR concurrent requests) */ 1586 /* set bit 29 (value of MULR requests is now 0) */ 1587 tarc0 &= 0xcfffffff; 1588 ew32(TARC(0), tarc0); 1589 /* fall through */ 1590 case e1000_80003es2lan: 1591 if (hw->phy.media_type == e1000_media_type_fiber || 1592 hw->phy.media_type == e1000_media_type_internal_serdes) { 1593 /* restore CTRL_EXT, stealing space from tx_fifo_head */ 1594 ew32(CTRL_EXT, adapter->tx_fifo_head); 1595 adapter->tx_fifo_head = 0; 1596 } 1597 /* fall through */ 1598 case e1000_82571: 1599 case e1000_82572: 1600 if (hw->phy.media_type == e1000_media_type_fiber || 1601 hw->phy.media_type == e1000_media_type_internal_serdes) { 1602 ew32(SCTL, E1000_SCTL_DISABLE_SERDES_LOOPBACK); 1603 e1e_flush(); 1604 usleep_range(10000, 20000); 1605 break; 1606 } 1607 /* Fall Through */ 1608 default: 1609 hw->mac.autoneg = 1; 1610 if (hw->phy.type == e1000_phy_gg82563) 1611 e1e_wphy(hw, GG82563_PHY_KMRN_MODE_CTRL, 0x180); 1612 e1e_rphy(hw, MII_BMCR, &phy_reg); 1613 if (phy_reg & BMCR_LOOPBACK) { 1614 phy_reg &= ~BMCR_LOOPBACK; 1615 e1e_wphy(hw, MII_BMCR, phy_reg); 1616 if (hw->phy.ops.commit) 1617 hw->phy.ops.commit(hw); 1618 } 1619 break; 1620 } 1621 } 1622 1623 static void e1000_create_lbtest_frame(struct sk_buff *skb, 1624 unsigned int frame_size) 1625 { 1626 memset(skb->data, 0xFF, frame_size); 1627 frame_size &= ~1; 1628 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1); 1629 memset(&skb->data[frame_size / 2 + 10], 0xBE, 1); 1630 memset(&skb->data[frame_size / 2 + 12], 0xAF, 1); 1631 } 1632 1633 static int e1000_check_lbtest_frame(struct sk_buff *skb, 1634 unsigned int frame_size) 1635 { 1636 frame_size &= ~1; 1637 if (*(skb->data + 3) == 0xFF) 1638 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) && 1639 (*(skb->data + frame_size / 2 + 12) == 0xAF)) 1640 return 0; 1641 return 13; 1642 } 1643 1644 static int e1000_run_loopback_test(struct e1000_adapter *adapter) 1645 { 1646 struct e1000_ring *tx_ring = &adapter->test_tx_ring; 1647 struct e1000_ring *rx_ring = &adapter->test_rx_ring; 1648 struct pci_dev *pdev = adapter->pdev; 1649 struct e1000_hw *hw = &adapter->hw; 1650 struct e1000_buffer *buffer_info; 1651 int i, j, k, l; 1652 int lc; 1653 int good_cnt; 1654 int ret_val = 0; 1655 unsigned long time; 1656 1657 ew32(RDT(0), rx_ring->count - 1); 1658 1659 /* Calculate the loop count based on the largest descriptor ring 1660 * The idea is to wrap the largest ring a number of times using 64 1661 * send/receive pairs during each loop 1662 */ 1663 1664 if (rx_ring->count <= tx_ring->count) 1665 lc = ((tx_ring->count / 64) * 2) + 1; 1666 else 1667 lc = ((rx_ring->count / 64) * 2) + 1; 1668 1669 k = 0; 1670 l = 0; 1671 /* loop count loop */ 1672 for (j = 0; j <= lc; j++) { 1673 /* send the packets */ 1674 for (i = 0; i < 64; i++) { 1675 buffer_info = &tx_ring->buffer_info[k]; 1676 1677 e1000_create_lbtest_frame(buffer_info->skb, 1024); 1678 dma_sync_single_for_device(&pdev->dev, 1679 buffer_info->dma, 1680 buffer_info->length, 1681 DMA_TO_DEVICE); 1682 k++; 1683 if (k == tx_ring->count) 1684 k = 0; 1685 } 1686 ew32(TDT(0), k); 1687 e1e_flush(); 1688 msleep(200); 1689 time = jiffies; /* set the start time for the receive */ 1690 good_cnt = 0; 1691 /* receive the sent packets */ 1692 do { 1693 buffer_info = &rx_ring->buffer_info[l]; 1694 1695 dma_sync_single_for_cpu(&pdev->dev, 1696 buffer_info->dma, 2048, 1697 DMA_FROM_DEVICE); 1698 1699 ret_val = e1000_check_lbtest_frame(buffer_info->skb, 1700 1024); 1701 if (!ret_val) 1702 good_cnt++; 1703 l++; 1704 if (l == rx_ring->count) 1705 l = 0; 1706 /* time + 20 msecs (200 msecs on 2.4) is more than 1707 * enough time to complete the receives, if it's 1708 * exceeded, break and error off 1709 */ 1710 } while ((good_cnt < 64) && !time_after(jiffies, time + 20)); 1711 if (good_cnt != 64) { 1712 ret_val = 13; /* ret_val is the same as mis-compare */ 1713 break; 1714 } 1715 if (time_after(jiffies, time + 20)) { 1716 ret_val = 14; /* error code for time out error */ 1717 break; 1718 } 1719 } 1720 return ret_val; 1721 } 1722 1723 static int e1000_loopback_test(struct e1000_adapter *adapter, u64 *data) 1724 { 1725 struct e1000_hw *hw = &adapter->hw; 1726 1727 /* PHY loopback cannot be performed if SoL/IDER sessions are active */ 1728 if (hw->phy.ops.check_reset_block && 1729 hw->phy.ops.check_reset_block(hw)) { 1730 e_err("Cannot do PHY loopback test when SoL/IDER is active.\n"); 1731 *data = 0; 1732 goto out; 1733 } 1734 1735 *data = e1000_setup_desc_rings(adapter); 1736 if (*data) 1737 goto out; 1738 1739 *data = e1000_setup_loopback_test(adapter); 1740 if (*data) 1741 goto err_loopback; 1742 1743 *data = e1000_run_loopback_test(adapter); 1744 e1000_loopback_cleanup(adapter); 1745 1746 err_loopback: 1747 e1000_free_desc_rings(adapter); 1748 out: 1749 return *data; 1750 } 1751 1752 static int e1000_link_test(struct e1000_adapter *adapter, u64 *data) 1753 { 1754 struct e1000_hw *hw = &adapter->hw; 1755 1756 *data = 0; 1757 if (hw->phy.media_type == e1000_media_type_internal_serdes) { 1758 int i = 0; 1759 1760 hw->mac.serdes_has_link = false; 1761 1762 /* On some blade server designs, link establishment 1763 * could take as long as 2-3 minutes 1764 */ 1765 do { 1766 hw->mac.ops.check_for_link(hw); 1767 if (hw->mac.serdes_has_link) 1768 return *data; 1769 msleep(20); 1770 } while (i++ < 3750); 1771 1772 *data = 1; 1773 } else { 1774 hw->mac.ops.check_for_link(hw); 1775 if (hw->mac.autoneg) 1776 /* On some Phy/switch combinations, link establishment 1777 * can take a few seconds more than expected. 1778 */ 1779 msleep_interruptible(5000); 1780 1781 if (!(er32(STATUS) & E1000_STATUS_LU)) 1782 *data = 1; 1783 } 1784 return *data; 1785 } 1786 1787 static int e1000e_get_sset_count(struct net_device __always_unused *netdev, 1788 int sset) 1789 { 1790 switch (sset) { 1791 case ETH_SS_TEST: 1792 return E1000_TEST_LEN; 1793 case ETH_SS_STATS: 1794 return E1000_STATS_LEN; 1795 default: 1796 return -EOPNOTSUPP; 1797 } 1798 } 1799 1800 static void e1000_diag_test(struct net_device *netdev, 1801 struct ethtool_test *eth_test, u64 *data) 1802 { 1803 struct e1000_adapter *adapter = netdev_priv(netdev); 1804 u16 autoneg_advertised; 1805 u8 forced_speed_duplex; 1806 u8 autoneg; 1807 bool if_running = netif_running(netdev); 1808 1809 pm_runtime_get_sync(netdev->dev.parent); 1810 1811 set_bit(__E1000_TESTING, &adapter->state); 1812 1813 if (!if_running) { 1814 /* Get control of and reset hardware */ 1815 if (adapter->flags & FLAG_HAS_AMT) 1816 e1000e_get_hw_control(adapter); 1817 1818 e1000e_power_up_phy(adapter); 1819 1820 adapter->hw.phy.autoneg_wait_to_complete = 1; 1821 e1000e_reset(adapter); 1822 adapter->hw.phy.autoneg_wait_to_complete = 0; 1823 } 1824 1825 if (eth_test->flags == ETH_TEST_FL_OFFLINE) { 1826 /* Offline tests */ 1827 1828 /* save speed, duplex, autoneg settings */ 1829 autoneg_advertised = adapter->hw.phy.autoneg_advertised; 1830 forced_speed_duplex = adapter->hw.mac.forced_speed_duplex; 1831 autoneg = adapter->hw.mac.autoneg; 1832 1833 e_info("offline testing starting\n"); 1834 1835 if (if_running) 1836 /* indicate we're in test mode */ 1837 e1000e_close(netdev); 1838 1839 if (e1000_reg_test(adapter, &data[0])) 1840 eth_test->flags |= ETH_TEST_FL_FAILED; 1841 1842 e1000e_reset(adapter); 1843 if (e1000_eeprom_test(adapter, &data[1])) 1844 eth_test->flags |= ETH_TEST_FL_FAILED; 1845 1846 e1000e_reset(adapter); 1847 if (e1000_intr_test(adapter, &data[2])) 1848 eth_test->flags |= ETH_TEST_FL_FAILED; 1849 1850 e1000e_reset(adapter); 1851 if (e1000_loopback_test(adapter, &data[3])) 1852 eth_test->flags |= ETH_TEST_FL_FAILED; 1853 1854 /* force this routine to wait until autoneg complete/timeout */ 1855 adapter->hw.phy.autoneg_wait_to_complete = 1; 1856 e1000e_reset(adapter); 1857 adapter->hw.phy.autoneg_wait_to_complete = 0; 1858 1859 if (e1000_link_test(adapter, &data[4])) 1860 eth_test->flags |= ETH_TEST_FL_FAILED; 1861 1862 /* restore speed, duplex, autoneg settings */ 1863 adapter->hw.phy.autoneg_advertised = autoneg_advertised; 1864 adapter->hw.mac.forced_speed_duplex = forced_speed_duplex; 1865 adapter->hw.mac.autoneg = autoneg; 1866 e1000e_reset(adapter); 1867 1868 clear_bit(__E1000_TESTING, &adapter->state); 1869 if (if_running) 1870 e1000e_open(netdev); 1871 } else { 1872 /* Online tests */ 1873 1874 e_info("online testing starting\n"); 1875 1876 /* register, eeprom, intr and loopback tests not run online */ 1877 data[0] = 0; 1878 data[1] = 0; 1879 data[2] = 0; 1880 data[3] = 0; 1881 1882 if (e1000_link_test(adapter, &data[4])) 1883 eth_test->flags |= ETH_TEST_FL_FAILED; 1884 1885 clear_bit(__E1000_TESTING, &adapter->state); 1886 } 1887 1888 if (!if_running) { 1889 e1000e_reset(adapter); 1890 1891 if (adapter->flags & FLAG_HAS_AMT) 1892 e1000e_release_hw_control(adapter); 1893 } 1894 1895 msleep_interruptible(4 * 1000); 1896 1897 pm_runtime_put_sync(netdev->dev.parent); 1898 } 1899 1900 static void e1000_get_wol(struct net_device *netdev, 1901 struct ethtool_wolinfo *wol) 1902 { 1903 struct e1000_adapter *adapter = netdev_priv(netdev); 1904 1905 wol->supported = 0; 1906 wol->wolopts = 0; 1907 1908 if (!(adapter->flags & FLAG_HAS_WOL) || 1909 !device_can_wakeup(&adapter->pdev->dev)) 1910 return; 1911 1912 wol->supported = WAKE_UCAST | WAKE_MCAST | 1913 WAKE_BCAST | WAKE_MAGIC | WAKE_PHY; 1914 1915 /* apply any specific unsupported masks here */ 1916 if (adapter->flags & FLAG_NO_WAKE_UCAST) { 1917 wol->supported &= ~WAKE_UCAST; 1918 1919 if (adapter->wol & E1000_WUFC_EX) 1920 e_err("Interface does not support directed (unicast) frame wake-up packets\n"); 1921 } 1922 1923 if (adapter->wol & E1000_WUFC_EX) 1924 wol->wolopts |= WAKE_UCAST; 1925 if (adapter->wol & E1000_WUFC_MC) 1926 wol->wolopts |= WAKE_MCAST; 1927 if (adapter->wol & E1000_WUFC_BC) 1928 wol->wolopts |= WAKE_BCAST; 1929 if (adapter->wol & E1000_WUFC_MAG) 1930 wol->wolopts |= WAKE_MAGIC; 1931 if (adapter->wol & E1000_WUFC_LNKC) 1932 wol->wolopts |= WAKE_PHY; 1933 } 1934 1935 static int e1000_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 1936 { 1937 struct e1000_adapter *adapter = netdev_priv(netdev); 1938 1939 if (!(adapter->flags & FLAG_HAS_WOL) || 1940 !device_can_wakeup(&adapter->pdev->dev) || 1941 (wol->wolopts & ~(WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | 1942 WAKE_MAGIC | WAKE_PHY))) 1943 return -EOPNOTSUPP; 1944 1945 /* these settings will always override what we currently have */ 1946 adapter->wol = 0; 1947 1948 if (wol->wolopts & WAKE_UCAST) 1949 adapter->wol |= E1000_WUFC_EX; 1950 if (wol->wolopts & WAKE_MCAST) 1951 adapter->wol |= E1000_WUFC_MC; 1952 if (wol->wolopts & WAKE_BCAST) 1953 adapter->wol |= E1000_WUFC_BC; 1954 if (wol->wolopts & WAKE_MAGIC) 1955 adapter->wol |= E1000_WUFC_MAG; 1956 if (wol->wolopts & WAKE_PHY) 1957 adapter->wol |= E1000_WUFC_LNKC; 1958 1959 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol); 1960 1961 return 0; 1962 } 1963 1964 static int e1000_set_phys_id(struct net_device *netdev, 1965 enum ethtool_phys_id_state state) 1966 { 1967 struct e1000_adapter *adapter = netdev_priv(netdev); 1968 struct e1000_hw *hw = &adapter->hw; 1969 1970 switch (state) { 1971 case ETHTOOL_ID_ACTIVE: 1972 pm_runtime_get_sync(netdev->dev.parent); 1973 1974 if (!hw->mac.ops.blink_led) 1975 return 2; /* cycle on/off twice per second */ 1976 1977 hw->mac.ops.blink_led(hw); 1978 break; 1979 1980 case ETHTOOL_ID_INACTIVE: 1981 if (hw->phy.type == e1000_phy_ife) 1982 e1e_wphy(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0); 1983 hw->mac.ops.led_off(hw); 1984 hw->mac.ops.cleanup_led(hw); 1985 pm_runtime_put_sync(netdev->dev.parent); 1986 break; 1987 1988 case ETHTOOL_ID_ON: 1989 hw->mac.ops.led_on(hw); 1990 break; 1991 1992 case ETHTOOL_ID_OFF: 1993 hw->mac.ops.led_off(hw); 1994 break; 1995 } 1996 1997 return 0; 1998 } 1999 2000 static int e1000_get_coalesce(struct net_device *netdev, 2001 struct ethtool_coalesce *ec) 2002 { 2003 struct e1000_adapter *adapter = netdev_priv(netdev); 2004 2005 if (adapter->itr_setting <= 4) 2006 ec->rx_coalesce_usecs = adapter->itr_setting; 2007 else 2008 ec->rx_coalesce_usecs = 1000000 / adapter->itr_setting; 2009 2010 return 0; 2011 } 2012 2013 static int e1000_set_coalesce(struct net_device *netdev, 2014 struct ethtool_coalesce *ec) 2015 { 2016 struct e1000_adapter *adapter = netdev_priv(netdev); 2017 2018 if ((ec->rx_coalesce_usecs > E1000_MAX_ITR_USECS) || 2019 ((ec->rx_coalesce_usecs > 4) && 2020 (ec->rx_coalesce_usecs < E1000_MIN_ITR_USECS)) || 2021 (ec->rx_coalesce_usecs == 2)) 2022 return -EINVAL; 2023 2024 if (ec->rx_coalesce_usecs == 4) { 2025 adapter->itr_setting = 4; 2026 adapter->itr = adapter->itr_setting; 2027 } else if (ec->rx_coalesce_usecs <= 3) { 2028 adapter->itr = 20000; 2029 adapter->itr_setting = ec->rx_coalesce_usecs; 2030 } else { 2031 adapter->itr = (1000000 / ec->rx_coalesce_usecs); 2032 adapter->itr_setting = adapter->itr & ~3; 2033 } 2034 2035 pm_runtime_get_sync(netdev->dev.parent); 2036 2037 if (adapter->itr_setting != 0) 2038 e1000e_write_itr(adapter, adapter->itr); 2039 else 2040 e1000e_write_itr(adapter, 0); 2041 2042 pm_runtime_put_sync(netdev->dev.parent); 2043 2044 return 0; 2045 } 2046 2047 static int e1000_nway_reset(struct net_device *netdev) 2048 { 2049 struct e1000_adapter *adapter = netdev_priv(netdev); 2050 2051 if (!netif_running(netdev)) 2052 return -EAGAIN; 2053 2054 if (!adapter->hw.mac.autoneg) 2055 return -EINVAL; 2056 2057 pm_runtime_get_sync(netdev->dev.parent); 2058 e1000e_reinit_locked(adapter); 2059 pm_runtime_put_sync(netdev->dev.parent); 2060 2061 return 0; 2062 } 2063 2064 static void e1000_get_ethtool_stats(struct net_device *netdev, 2065 struct ethtool_stats __always_unused *stats, 2066 u64 *data) 2067 { 2068 struct e1000_adapter *adapter = netdev_priv(netdev); 2069 struct rtnl_link_stats64 net_stats; 2070 int i; 2071 char *p = NULL; 2072 2073 pm_runtime_get_sync(netdev->dev.parent); 2074 2075 e1000e_get_stats64(netdev, &net_stats); 2076 2077 pm_runtime_put_sync(netdev->dev.parent); 2078 2079 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) { 2080 switch (e1000_gstrings_stats[i].type) { 2081 case NETDEV_STATS: 2082 p = (char *)&net_stats + 2083 e1000_gstrings_stats[i].stat_offset; 2084 break; 2085 case E1000_STATS: 2086 p = (char *)adapter + 2087 e1000_gstrings_stats[i].stat_offset; 2088 break; 2089 default: 2090 data[i] = 0; 2091 continue; 2092 } 2093 2094 data[i] = (e1000_gstrings_stats[i].sizeof_stat == 2095 sizeof(u64)) ? *(u64 *)p : *(u32 *)p; 2096 } 2097 } 2098 2099 static void e1000_get_strings(struct net_device __always_unused *netdev, 2100 u32 stringset, u8 *data) 2101 { 2102 u8 *p = data; 2103 int i; 2104 2105 switch (stringset) { 2106 case ETH_SS_TEST: 2107 memcpy(data, e1000_gstrings_test, sizeof(e1000_gstrings_test)); 2108 break; 2109 case ETH_SS_STATS: 2110 for (i = 0; i < E1000_GLOBAL_STATS_LEN; i++) { 2111 memcpy(p, e1000_gstrings_stats[i].stat_string, 2112 ETH_GSTRING_LEN); 2113 p += ETH_GSTRING_LEN; 2114 } 2115 break; 2116 } 2117 } 2118 2119 static int e1000_get_rxnfc(struct net_device *netdev, 2120 struct ethtool_rxnfc *info, 2121 u32 __always_unused *rule_locs) 2122 { 2123 info->data = 0; 2124 2125 switch (info->cmd) { 2126 case ETHTOOL_GRXFH: { 2127 struct e1000_adapter *adapter = netdev_priv(netdev); 2128 struct e1000_hw *hw = &adapter->hw; 2129 u32 mrqc; 2130 2131 pm_runtime_get_sync(netdev->dev.parent); 2132 mrqc = er32(MRQC); 2133 pm_runtime_put_sync(netdev->dev.parent); 2134 2135 if (!(mrqc & E1000_MRQC_RSS_FIELD_MASK)) 2136 return 0; 2137 2138 switch (info->flow_type) { 2139 case TCP_V4_FLOW: 2140 if (mrqc & E1000_MRQC_RSS_FIELD_IPV4_TCP) 2141 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 2142 /* fall through */ 2143 case UDP_V4_FLOW: 2144 case SCTP_V4_FLOW: 2145 case AH_ESP_V4_FLOW: 2146 case IPV4_FLOW: 2147 if (mrqc & E1000_MRQC_RSS_FIELD_IPV4) 2148 info->data |= RXH_IP_SRC | RXH_IP_DST; 2149 break; 2150 case TCP_V6_FLOW: 2151 if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_TCP) 2152 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 2153 /* fall through */ 2154 case UDP_V6_FLOW: 2155 case SCTP_V6_FLOW: 2156 case AH_ESP_V6_FLOW: 2157 case IPV6_FLOW: 2158 if (mrqc & E1000_MRQC_RSS_FIELD_IPV6) 2159 info->data |= RXH_IP_SRC | RXH_IP_DST; 2160 break; 2161 default: 2162 break; 2163 } 2164 return 0; 2165 } 2166 default: 2167 return -EOPNOTSUPP; 2168 } 2169 } 2170 2171 static int e1000e_get_eee(struct net_device *netdev, struct ethtool_eee *edata) 2172 { 2173 struct e1000_adapter *adapter = netdev_priv(netdev); 2174 struct e1000_hw *hw = &adapter->hw; 2175 u16 cap_addr, lpa_addr, pcs_stat_addr, phy_data; 2176 u32 ret_val; 2177 2178 if (!(adapter->flags2 & FLAG2_HAS_EEE)) 2179 return -EOPNOTSUPP; 2180 2181 switch (hw->phy.type) { 2182 case e1000_phy_82579: 2183 cap_addr = I82579_EEE_CAPABILITY; 2184 lpa_addr = I82579_EEE_LP_ABILITY; 2185 pcs_stat_addr = I82579_EEE_PCS_STATUS; 2186 break; 2187 case e1000_phy_i217: 2188 cap_addr = I217_EEE_CAPABILITY; 2189 lpa_addr = I217_EEE_LP_ABILITY; 2190 pcs_stat_addr = I217_EEE_PCS_STATUS; 2191 break; 2192 default: 2193 return -EOPNOTSUPP; 2194 } 2195 2196 pm_runtime_get_sync(netdev->dev.parent); 2197 2198 ret_val = hw->phy.ops.acquire(hw); 2199 if (ret_val) { 2200 pm_runtime_put_sync(netdev->dev.parent); 2201 return -EBUSY; 2202 } 2203 2204 /* EEE Capability */ 2205 ret_val = e1000_read_emi_reg_locked(hw, cap_addr, &phy_data); 2206 if (ret_val) 2207 goto release; 2208 edata->supported = mmd_eee_cap_to_ethtool_sup_t(phy_data); 2209 2210 /* EEE Advertised */ 2211 edata->advertised = mmd_eee_adv_to_ethtool_adv_t(adapter->eee_advert); 2212 2213 /* EEE Link Partner Advertised */ 2214 ret_val = e1000_read_emi_reg_locked(hw, lpa_addr, &phy_data); 2215 if (ret_val) 2216 goto release; 2217 edata->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(phy_data); 2218 2219 /* EEE PCS Status */ 2220 ret_val = e1000_read_emi_reg_locked(hw, pcs_stat_addr, &phy_data); 2221 if (ret_val) 2222 goto release; 2223 if (hw->phy.type == e1000_phy_82579) 2224 phy_data <<= 8; 2225 2226 /* Result of the EEE auto negotiation - there is no register that 2227 * has the status of the EEE negotiation so do a best-guess based 2228 * on whether Tx or Rx LPI indications have been received. 2229 */ 2230 if (phy_data & (E1000_EEE_TX_LPI_RCVD | E1000_EEE_RX_LPI_RCVD)) 2231 edata->eee_active = true; 2232 2233 edata->eee_enabled = !hw->dev_spec.ich8lan.eee_disable; 2234 edata->tx_lpi_enabled = true; 2235 edata->tx_lpi_timer = er32(LPIC) >> E1000_LPIC_LPIET_SHIFT; 2236 2237 release: 2238 hw->phy.ops.release(hw); 2239 if (ret_val) 2240 ret_val = -ENODATA; 2241 2242 pm_runtime_put_sync(netdev->dev.parent); 2243 2244 return ret_val; 2245 } 2246 2247 static int e1000e_set_eee(struct net_device *netdev, struct ethtool_eee *edata) 2248 { 2249 struct e1000_adapter *adapter = netdev_priv(netdev); 2250 struct e1000_hw *hw = &adapter->hw; 2251 struct ethtool_eee eee_curr; 2252 s32 ret_val; 2253 2254 ret_val = e1000e_get_eee(netdev, &eee_curr); 2255 if (ret_val) 2256 return ret_val; 2257 2258 if (eee_curr.tx_lpi_enabled != edata->tx_lpi_enabled) { 2259 e_err("Setting EEE tx-lpi is not supported\n"); 2260 return -EINVAL; 2261 } 2262 2263 if (eee_curr.tx_lpi_timer != edata->tx_lpi_timer) { 2264 e_err("Setting EEE Tx LPI timer is not supported\n"); 2265 return -EINVAL; 2266 } 2267 2268 if (edata->advertised & ~(ADVERTISE_100_FULL | ADVERTISE_1000_FULL)) { 2269 e_err("EEE advertisement supports only 100TX and/or 1000T full-duplex\n"); 2270 return -EINVAL; 2271 } 2272 2273 adapter->eee_advert = ethtool_adv_to_mmd_eee_adv_t(edata->advertised); 2274 2275 hw->dev_spec.ich8lan.eee_disable = !edata->eee_enabled; 2276 2277 pm_runtime_get_sync(netdev->dev.parent); 2278 2279 /* reset the link */ 2280 if (netif_running(netdev)) 2281 e1000e_reinit_locked(adapter); 2282 else 2283 e1000e_reset(adapter); 2284 2285 pm_runtime_put_sync(netdev->dev.parent); 2286 2287 return 0; 2288 } 2289 2290 static int e1000e_get_ts_info(struct net_device *netdev, 2291 struct ethtool_ts_info *info) 2292 { 2293 struct e1000_adapter *adapter = netdev_priv(netdev); 2294 2295 ethtool_op_get_ts_info(netdev, info); 2296 2297 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP)) 2298 return 0; 2299 2300 info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE | 2301 SOF_TIMESTAMPING_RX_HARDWARE | 2302 SOF_TIMESTAMPING_RAW_HARDWARE); 2303 2304 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON); 2305 2306 info->rx_filters = (BIT(HWTSTAMP_FILTER_NONE) | 2307 BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) | 2308 BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) | 2309 BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) | 2310 BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) | 2311 BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) | 2312 BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) | 2313 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) | 2314 BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) | 2315 BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) | 2316 BIT(HWTSTAMP_FILTER_ALL)); 2317 2318 if (adapter->ptp_clock) 2319 info->phc_index = ptp_clock_index(adapter->ptp_clock); 2320 2321 return 0; 2322 } 2323 2324 static const struct ethtool_ops e1000_ethtool_ops = { 2325 .get_drvinfo = e1000_get_drvinfo, 2326 .get_regs_len = e1000_get_regs_len, 2327 .get_regs = e1000_get_regs, 2328 .get_wol = e1000_get_wol, 2329 .set_wol = e1000_set_wol, 2330 .get_msglevel = e1000_get_msglevel, 2331 .set_msglevel = e1000_set_msglevel, 2332 .nway_reset = e1000_nway_reset, 2333 .get_link = ethtool_op_get_link, 2334 .get_eeprom_len = e1000_get_eeprom_len, 2335 .get_eeprom = e1000_get_eeprom, 2336 .set_eeprom = e1000_set_eeprom, 2337 .get_ringparam = e1000_get_ringparam, 2338 .set_ringparam = e1000_set_ringparam, 2339 .get_pauseparam = e1000_get_pauseparam, 2340 .set_pauseparam = e1000_set_pauseparam, 2341 .self_test = e1000_diag_test, 2342 .get_strings = e1000_get_strings, 2343 .set_phys_id = e1000_set_phys_id, 2344 .get_ethtool_stats = e1000_get_ethtool_stats, 2345 .get_sset_count = e1000e_get_sset_count, 2346 .get_coalesce = e1000_get_coalesce, 2347 .set_coalesce = e1000_set_coalesce, 2348 .get_rxnfc = e1000_get_rxnfc, 2349 .get_ts_info = e1000e_get_ts_info, 2350 .get_eee = e1000e_get_eee, 2351 .set_eee = e1000e_set_eee, 2352 .get_link_ksettings = e1000_get_link_ksettings, 2353 .set_link_ksettings = e1000_set_link_ksettings, 2354 }; 2355 2356 void e1000e_set_ethtool_ops(struct net_device *netdev) 2357 { 2358 netdev->ethtool_ops = &e1000_ethtool_ops; 2359 } 2360