1 /* 2 * Copyright (C) 1999 - 2010 Intel Corporation. 3 * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD. 4 * 5 * This code was derived from the Intel e1000e Linux driver. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; version 2 of the License. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 #include "pch_gbe.h" 20 #include "pch_gbe_phy.h" 21 22 /** 23 * pch_gbe_stats - Stats item information 24 */ 25 struct pch_gbe_stats { 26 char string[ETH_GSTRING_LEN]; 27 size_t size; 28 size_t offset; 29 }; 30 31 #define PCH_GBE_STAT(m) \ 32 { \ 33 .string = #m, \ 34 .size = FIELD_SIZEOF(struct pch_gbe_hw_stats, m), \ 35 .offset = offsetof(struct pch_gbe_hw_stats, m), \ 36 } 37 38 /** 39 * pch_gbe_gstrings_stats - ethtool information status name list 40 */ 41 static const struct pch_gbe_stats pch_gbe_gstrings_stats[] = { 42 PCH_GBE_STAT(rx_packets), 43 PCH_GBE_STAT(tx_packets), 44 PCH_GBE_STAT(rx_bytes), 45 PCH_GBE_STAT(tx_bytes), 46 PCH_GBE_STAT(rx_errors), 47 PCH_GBE_STAT(tx_errors), 48 PCH_GBE_STAT(rx_dropped), 49 PCH_GBE_STAT(tx_dropped), 50 PCH_GBE_STAT(multicast), 51 PCH_GBE_STAT(collisions), 52 PCH_GBE_STAT(rx_crc_errors), 53 PCH_GBE_STAT(rx_frame_errors), 54 PCH_GBE_STAT(rx_alloc_buff_failed), 55 PCH_GBE_STAT(tx_length_errors), 56 PCH_GBE_STAT(tx_aborted_errors), 57 PCH_GBE_STAT(tx_carrier_errors), 58 PCH_GBE_STAT(tx_timeout_count), 59 PCH_GBE_STAT(tx_restart_count), 60 PCH_GBE_STAT(intr_rx_dsc_empty_count), 61 PCH_GBE_STAT(intr_rx_frame_err_count), 62 PCH_GBE_STAT(intr_rx_fifo_err_count), 63 PCH_GBE_STAT(intr_rx_dma_err_count), 64 PCH_GBE_STAT(intr_tx_fifo_err_count), 65 PCH_GBE_STAT(intr_tx_dma_err_count), 66 PCH_GBE_STAT(intr_tcpip_err_count) 67 }; 68 69 #define PCH_GBE_QUEUE_STATS_LEN 0 70 #define PCH_GBE_GLOBAL_STATS_LEN ARRAY_SIZE(pch_gbe_gstrings_stats) 71 #define PCH_GBE_STATS_LEN (PCH_GBE_GLOBAL_STATS_LEN + PCH_GBE_QUEUE_STATS_LEN) 72 73 #define PCH_GBE_MAC_REGS_LEN (sizeof(struct pch_gbe_regs) / 4) 74 #define PCH_GBE_REGS_LEN (PCH_GBE_MAC_REGS_LEN + PCH_GBE_PHY_REGS_LEN) 75 /** 76 * pch_gbe_get_link_ksettings - Get device-specific settings 77 * @netdev: Network interface device structure 78 * @ecmd: Ethtool command 79 * Returns: 80 * 0: Successful. 81 * Negative value: Failed. 82 */ 83 static int pch_gbe_get_link_ksettings(struct net_device *netdev, 84 struct ethtool_link_ksettings *ecmd) 85 { 86 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 87 u32 supported, advertising; 88 89 mii_ethtool_get_link_ksettings(&adapter->mii, ecmd); 90 91 ethtool_convert_link_mode_to_legacy_u32(&supported, 92 ecmd->link_modes.supported); 93 ethtool_convert_link_mode_to_legacy_u32(&advertising, 94 ecmd->link_modes.advertising); 95 96 supported &= ~(SUPPORTED_TP | SUPPORTED_1000baseT_Half); 97 advertising &= ~(ADVERTISED_TP | ADVERTISED_1000baseT_Half); 98 99 ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported, 100 supported); 101 ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.advertising, 102 advertising); 103 104 if (!netif_carrier_ok(adapter->netdev)) 105 ecmd->base.speed = SPEED_UNKNOWN; 106 107 return 0; 108 } 109 110 /** 111 * pch_gbe_set_link_ksettings - Set device-specific settings 112 * @netdev: Network interface device structure 113 * @ecmd: Ethtool command 114 * Returns: 115 * 0: Successful. 116 * Negative value: Failed. 117 */ 118 static int pch_gbe_set_link_ksettings(struct net_device *netdev, 119 const struct ethtool_link_ksettings *ecmd) 120 { 121 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 122 struct pch_gbe_hw *hw = &adapter->hw; 123 struct ethtool_link_ksettings copy_ecmd; 124 u32 speed = ecmd->base.speed; 125 u32 advertising; 126 int ret; 127 128 pch_gbe_phy_write_reg_miic(hw, MII_BMCR, BMCR_RESET); 129 130 memcpy(©_ecmd, ecmd, sizeof(*ecmd)); 131 132 /* when set_settings() is called with a ethtool_cmd previously 133 * filled by get_settings() on a down link, speed is -1: */ 134 if (speed == UINT_MAX) { 135 speed = SPEED_1000; 136 copy_ecmd.base.speed = speed; 137 copy_ecmd.base.duplex = DUPLEX_FULL; 138 } 139 ret = mii_ethtool_set_link_ksettings(&adapter->mii, ©_ecmd); 140 if (ret) { 141 netdev_err(netdev, "Error: mii_ethtool_set_link_ksettings\n"); 142 return ret; 143 } 144 hw->mac.link_speed = speed; 145 hw->mac.link_duplex = copy_ecmd.base.duplex; 146 ethtool_convert_link_mode_to_legacy_u32( 147 &advertising, copy_ecmd.link_modes.advertising); 148 hw->phy.autoneg_advertised = advertising; 149 hw->mac.autoneg = copy_ecmd.base.autoneg; 150 151 /* reset the link */ 152 if (netif_running(adapter->netdev)) { 153 pch_gbe_down(adapter); 154 ret = pch_gbe_up(adapter); 155 } else { 156 pch_gbe_reset(adapter); 157 } 158 return ret; 159 } 160 161 /** 162 * pch_gbe_get_regs_len - Report the size of device registers 163 * @netdev: Network interface device structure 164 * Returns: the size of device registers. 165 */ 166 static int pch_gbe_get_regs_len(struct net_device *netdev) 167 { 168 return PCH_GBE_REGS_LEN * (int)sizeof(u32); 169 } 170 171 /** 172 * pch_gbe_get_drvinfo - Report driver information 173 * @netdev: Network interface device structure 174 * @drvinfo: Driver information structure 175 */ 176 static void pch_gbe_get_drvinfo(struct net_device *netdev, 177 struct ethtool_drvinfo *drvinfo) 178 { 179 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 180 181 strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver)); 182 strlcpy(drvinfo->version, pch_driver_version, sizeof(drvinfo->version)); 183 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 184 sizeof(drvinfo->bus_info)); 185 } 186 187 /** 188 * pch_gbe_get_regs - Get device registers 189 * @netdev: Network interface device structure 190 * @regs: Ethtool register structure 191 * @p: Buffer pointer of read device register date 192 */ 193 static void pch_gbe_get_regs(struct net_device *netdev, 194 struct ethtool_regs *regs, void *p) 195 { 196 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 197 struct pch_gbe_hw *hw = &adapter->hw; 198 struct pci_dev *pdev = adapter->pdev; 199 u32 *regs_buff = p; 200 u16 i, tmp; 201 202 regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device; 203 for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++) 204 *regs_buff++ = ioread32(&hw->reg->INT_ST + i); 205 /* PHY register */ 206 for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) { 207 pch_gbe_phy_read_reg_miic(&adapter->hw, i, &tmp); 208 *regs_buff++ = tmp; 209 } 210 } 211 212 /** 213 * pch_gbe_get_wol - Report whether Wake-on-Lan is enabled 214 * @netdev: Network interface device structure 215 * @wol: Wake-on-Lan information 216 */ 217 static void pch_gbe_get_wol(struct net_device *netdev, 218 struct ethtool_wolinfo *wol) 219 { 220 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 221 222 wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC; 223 wol->wolopts = 0; 224 225 if ((adapter->wake_up_evt & PCH_GBE_WLC_IND)) 226 wol->wolopts |= WAKE_UCAST; 227 if ((adapter->wake_up_evt & PCH_GBE_WLC_MLT)) 228 wol->wolopts |= WAKE_MCAST; 229 if ((adapter->wake_up_evt & PCH_GBE_WLC_BR)) 230 wol->wolopts |= WAKE_BCAST; 231 if ((adapter->wake_up_evt & PCH_GBE_WLC_MP)) 232 wol->wolopts |= WAKE_MAGIC; 233 } 234 235 /** 236 * pch_gbe_set_wol - Turn Wake-on-Lan on or off 237 * @netdev: Network interface device structure 238 * @wol: Pointer of wake-on-Lan information straucture 239 * Returns: 240 * 0: Successful. 241 * Negative value: Failed. 242 */ 243 static int pch_gbe_set_wol(struct net_device *netdev, 244 struct ethtool_wolinfo *wol) 245 { 246 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 247 248 if ((wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))) 249 return -EOPNOTSUPP; 250 /* these settings will always override what we currently have */ 251 adapter->wake_up_evt = 0; 252 253 if ((wol->wolopts & WAKE_UCAST)) 254 adapter->wake_up_evt |= PCH_GBE_WLC_IND; 255 if ((wol->wolopts & WAKE_MCAST)) 256 adapter->wake_up_evt |= PCH_GBE_WLC_MLT; 257 if ((wol->wolopts & WAKE_BCAST)) 258 adapter->wake_up_evt |= PCH_GBE_WLC_BR; 259 if ((wol->wolopts & WAKE_MAGIC)) 260 adapter->wake_up_evt |= PCH_GBE_WLC_MP; 261 return 0; 262 } 263 264 /** 265 * pch_gbe_nway_reset - Restart autonegotiation 266 * @netdev: Network interface device structure 267 * Returns: 268 * 0: Successful. 269 * Negative value: Failed. 270 */ 271 static int pch_gbe_nway_reset(struct net_device *netdev) 272 { 273 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 274 275 return mii_nway_restart(&adapter->mii); 276 } 277 278 /** 279 * pch_gbe_get_ringparam - Report ring sizes 280 * @netdev: Network interface device structure 281 * @ring: Ring param structure 282 */ 283 static void pch_gbe_get_ringparam(struct net_device *netdev, 284 struct ethtool_ringparam *ring) 285 { 286 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 287 struct pch_gbe_tx_ring *txdr = adapter->tx_ring; 288 struct pch_gbe_rx_ring *rxdr = adapter->rx_ring; 289 290 ring->rx_max_pending = PCH_GBE_MAX_RXD; 291 ring->tx_max_pending = PCH_GBE_MAX_TXD; 292 ring->rx_pending = rxdr->count; 293 ring->tx_pending = txdr->count; 294 } 295 296 /** 297 * pch_gbe_set_ringparam - Set ring sizes 298 * @netdev: Network interface device structure 299 * @ring: Ring param structure 300 * Returns 301 * 0: Successful. 302 * Negative value: Failed. 303 */ 304 static int pch_gbe_set_ringparam(struct net_device *netdev, 305 struct ethtool_ringparam *ring) 306 { 307 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 308 struct pch_gbe_tx_ring *txdr, *tx_old; 309 struct pch_gbe_rx_ring *rxdr, *rx_old; 310 int tx_ring_size, rx_ring_size; 311 int err = 0; 312 313 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending)) 314 return -EINVAL; 315 tx_ring_size = (int)sizeof(struct pch_gbe_tx_ring); 316 rx_ring_size = (int)sizeof(struct pch_gbe_rx_ring); 317 318 if ((netif_running(adapter->netdev))) 319 pch_gbe_down(adapter); 320 tx_old = adapter->tx_ring; 321 rx_old = adapter->rx_ring; 322 323 txdr = kzalloc(tx_ring_size, GFP_KERNEL); 324 if (!txdr) { 325 err = -ENOMEM; 326 goto err_alloc_tx; 327 } 328 rxdr = kzalloc(rx_ring_size, GFP_KERNEL); 329 if (!rxdr) { 330 err = -ENOMEM; 331 goto err_alloc_rx; 332 } 333 adapter->tx_ring = txdr; 334 adapter->rx_ring = rxdr; 335 336 rxdr->count = 337 clamp_val(ring->rx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD); 338 rxdr->count = roundup(rxdr->count, PCH_GBE_RX_DESC_MULTIPLE); 339 340 txdr->count = 341 clamp_val(ring->tx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD); 342 txdr->count = roundup(txdr->count, PCH_GBE_TX_DESC_MULTIPLE); 343 344 if ((netif_running(adapter->netdev))) { 345 /* Try to get new resources before deleting old */ 346 err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring); 347 if (err) 348 goto err_setup_rx; 349 err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring); 350 if (err) 351 goto err_setup_tx; 352 pch_gbe_free_rx_resources(adapter, rx_old); 353 pch_gbe_free_tx_resources(adapter, tx_old); 354 kfree(tx_old); 355 kfree(rx_old); 356 adapter->rx_ring = rxdr; 357 adapter->tx_ring = txdr; 358 err = pch_gbe_up(adapter); 359 } 360 return err; 361 362 err_setup_tx: 363 pch_gbe_free_rx_resources(adapter, adapter->rx_ring); 364 err_setup_rx: 365 adapter->rx_ring = rx_old; 366 adapter->tx_ring = tx_old; 367 kfree(rxdr); 368 err_alloc_rx: 369 kfree(txdr); 370 err_alloc_tx: 371 if (netif_running(adapter->netdev)) 372 pch_gbe_up(adapter); 373 return err; 374 } 375 376 /** 377 * pch_gbe_get_pauseparam - Report pause parameters 378 * @netdev: Network interface device structure 379 * @pause: Pause parameters structure 380 */ 381 static void pch_gbe_get_pauseparam(struct net_device *netdev, 382 struct ethtool_pauseparam *pause) 383 { 384 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 385 struct pch_gbe_hw *hw = &adapter->hw; 386 387 pause->autoneg = 388 ((hw->mac.fc_autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE); 389 390 if (hw->mac.fc == PCH_GBE_FC_RX_PAUSE) { 391 pause->rx_pause = 1; 392 } else if (hw->mac.fc == PCH_GBE_FC_TX_PAUSE) { 393 pause->tx_pause = 1; 394 } else if (hw->mac.fc == PCH_GBE_FC_FULL) { 395 pause->rx_pause = 1; 396 pause->tx_pause = 1; 397 } 398 } 399 400 /** 401 * pch_gbe_set_pauseparam - Set pause parameters 402 * @netdev: Network interface device structure 403 * @pause: Pause parameters structure 404 * Returns: 405 * 0: Successful. 406 * Negative value: Failed. 407 */ 408 static int pch_gbe_set_pauseparam(struct net_device *netdev, 409 struct ethtool_pauseparam *pause) 410 { 411 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 412 struct pch_gbe_hw *hw = &adapter->hw; 413 int ret = 0; 414 415 hw->mac.fc_autoneg = pause->autoneg; 416 if ((pause->rx_pause) && (pause->tx_pause)) 417 hw->mac.fc = PCH_GBE_FC_FULL; 418 else if ((pause->rx_pause) && (!pause->tx_pause)) 419 hw->mac.fc = PCH_GBE_FC_RX_PAUSE; 420 else if ((!pause->rx_pause) && (pause->tx_pause)) 421 hw->mac.fc = PCH_GBE_FC_TX_PAUSE; 422 else if ((!pause->rx_pause) && (!pause->tx_pause)) 423 hw->mac.fc = PCH_GBE_FC_NONE; 424 425 if (hw->mac.fc_autoneg == AUTONEG_ENABLE) { 426 if ((netif_running(adapter->netdev))) { 427 pch_gbe_down(adapter); 428 ret = pch_gbe_up(adapter); 429 } else { 430 pch_gbe_reset(adapter); 431 } 432 } else { 433 ret = pch_gbe_mac_force_mac_fc(hw); 434 } 435 return ret; 436 } 437 438 /** 439 * pch_gbe_get_strings - Return a set of strings that describe the requested 440 * objects 441 * @netdev: Network interface device structure 442 * @stringset: Select the stringset. [ETH_SS_TEST] [ETH_SS_STATS] 443 * @data: Pointer of read string data. 444 */ 445 static void pch_gbe_get_strings(struct net_device *netdev, u32 stringset, 446 u8 *data) 447 { 448 u8 *p = data; 449 int i; 450 451 switch (stringset) { 452 case (u32) ETH_SS_STATS: 453 for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) { 454 memcpy(p, pch_gbe_gstrings_stats[i].string, 455 ETH_GSTRING_LEN); 456 p += ETH_GSTRING_LEN; 457 } 458 break; 459 } 460 } 461 462 /** 463 * pch_gbe_get_ethtool_stats - Return statistics about the device 464 * @netdev: Network interface device structure 465 * @stats: Ethtool statue structure 466 * @data: Pointer of read status area 467 */ 468 static void pch_gbe_get_ethtool_stats(struct net_device *netdev, 469 struct ethtool_stats *stats, u64 *data) 470 { 471 struct pch_gbe_adapter *adapter = netdev_priv(netdev); 472 int i; 473 const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats; 474 char *hw_stats = (char *)&adapter->stats; 475 476 pch_gbe_update_stats(adapter); 477 for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) { 478 char *p = hw_stats + gstats->offset; 479 data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p); 480 gstats++; 481 } 482 } 483 484 static int pch_gbe_get_sset_count(struct net_device *netdev, int sset) 485 { 486 switch (sset) { 487 case ETH_SS_STATS: 488 return PCH_GBE_STATS_LEN; 489 default: 490 return -EOPNOTSUPP; 491 } 492 } 493 494 static const struct ethtool_ops pch_gbe_ethtool_ops = { 495 .get_drvinfo = pch_gbe_get_drvinfo, 496 .get_regs_len = pch_gbe_get_regs_len, 497 .get_regs = pch_gbe_get_regs, 498 .get_wol = pch_gbe_get_wol, 499 .set_wol = pch_gbe_set_wol, 500 .nway_reset = pch_gbe_nway_reset, 501 .get_link = ethtool_op_get_link, 502 .get_ringparam = pch_gbe_get_ringparam, 503 .set_ringparam = pch_gbe_set_ringparam, 504 .get_pauseparam = pch_gbe_get_pauseparam, 505 .set_pauseparam = pch_gbe_set_pauseparam, 506 .get_strings = pch_gbe_get_strings, 507 .get_ethtool_stats = pch_gbe_get_ethtool_stats, 508 .get_sset_count = pch_gbe_get_sset_count, 509 .get_link_ksettings = pch_gbe_get_link_ksettings, 510 .set_link_ksettings = pch_gbe_set_link_ksettings, 511 }; 512 513 void pch_gbe_set_ethtool_ops(struct net_device *netdev) 514 { 515 netdev->ethtool_ops = &pch_gbe_ethtool_ops; 516 } 517