1 /* 2 * Copyright (C) 2005 - 2014 Emulex 3 * All rights reserved. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License version 2 7 * as published by the Free Software Foundation. The full GNU General 8 * Public License is included in this distribution in the file called COPYING. 9 * 10 * Contact Information: 11 * linux-drivers@emulex.com 12 * 13 * Emulex 14 * 3333 Susan Street 15 * Costa Mesa, CA 92626 16 */ 17 18 #include "be.h" 19 #include "be_cmds.h" 20 #include <linux/ethtool.h> 21 22 struct be_ethtool_stat { 23 char desc[ETH_GSTRING_LEN]; 24 int type; 25 int size; 26 int offset; 27 }; 28 29 enum {DRVSTAT_TX, DRVSTAT_RX, DRVSTAT}; 30 #define FIELDINFO(_struct, field) FIELD_SIZEOF(_struct, field), \ 31 offsetof(_struct, field) 32 #define DRVSTAT_TX_INFO(field) #field, DRVSTAT_TX,\ 33 FIELDINFO(struct be_tx_stats, field) 34 #define DRVSTAT_RX_INFO(field) #field, DRVSTAT_RX,\ 35 FIELDINFO(struct be_rx_stats, field) 36 #define DRVSTAT_INFO(field) #field, DRVSTAT,\ 37 FIELDINFO(struct be_drv_stats, field) 38 39 static const struct be_ethtool_stat et_stats[] = { 40 {DRVSTAT_INFO(rx_crc_errors)}, 41 {DRVSTAT_INFO(rx_alignment_symbol_errors)}, 42 {DRVSTAT_INFO(rx_pause_frames)}, 43 {DRVSTAT_INFO(rx_control_frames)}, 44 /* Received packets dropped when the Ethernet length field 45 * is not equal to the actual Ethernet data length. 46 */ 47 {DRVSTAT_INFO(rx_in_range_errors)}, 48 /* Received packets dropped when their length field is >= 1501 bytes 49 * and <= 1535 bytes. 50 */ 51 {DRVSTAT_INFO(rx_out_range_errors)}, 52 /* Received packets dropped when they are longer than 9216 bytes */ 53 {DRVSTAT_INFO(rx_frame_too_long)}, 54 /* Received packets dropped when they don't pass the unicast or 55 * multicast address filtering. 56 */ 57 {DRVSTAT_INFO(rx_address_filtered)}, 58 /* Received packets dropped when IP packet length field is less than 59 * the IP header length field. 60 */ 61 {DRVSTAT_INFO(rx_dropped_too_small)}, 62 /* Received packets dropped when IP length field is greater than 63 * the actual packet length. 64 */ 65 {DRVSTAT_INFO(rx_dropped_too_short)}, 66 /* Received packets dropped when the IP header length field is less 67 * than 5. 68 */ 69 {DRVSTAT_INFO(rx_dropped_header_too_small)}, 70 /* Received packets dropped when the TCP header length field is less 71 * than 5 or the TCP header length + IP header length is more 72 * than IP packet length. 73 */ 74 {DRVSTAT_INFO(rx_dropped_tcp_length)}, 75 {DRVSTAT_INFO(rx_dropped_runt)}, 76 /* Number of received packets dropped when a fifo for descriptors going 77 * into the packet demux block overflows. In normal operation, this 78 * fifo must never overflow. 79 */ 80 {DRVSTAT_INFO(rxpp_fifo_overflow_drop)}, 81 {DRVSTAT_INFO(rx_input_fifo_overflow_drop)}, 82 {DRVSTAT_INFO(rx_ip_checksum_errs)}, 83 {DRVSTAT_INFO(rx_tcp_checksum_errs)}, 84 {DRVSTAT_INFO(rx_udp_checksum_errs)}, 85 {DRVSTAT_INFO(tx_pauseframes)}, 86 {DRVSTAT_INFO(tx_controlframes)}, 87 {DRVSTAT_INFO(rx_priority_pause_frames)}, 88 {DRVSTAT_INFO(tx_priority_pauseframes)}, 89 /* Received packets dropped when an internal fifo going into 90 * main packet buffer tank (PMEM) overflows. 91 */ 92 {DRVSTAT_INFO(pmem_fifo_overflow_drop)}, 93 {DRVSTAT_INFO(jabber_events)}, 94 /* Received packets dropped due to lack of available HW packet buffers 95 * used to temporarily hold the received packets. 96 */ 97 {DRVSTAT_INFO(rx_drops_no_pbuf)}, 98 /* Received packets dropped due to input receive buffer 99 * descriptor fifo overflowing. 100 */ 101 {DRVSTAT_INFO(rx_drops_no_erx_descr)}, 102 /* Packets dropped because the internal FIFO to the offloaded TCP 103 * receive processing block is full. This could happen only for 104 * offloaded iSCSI or FCoE trarffic. 105 */ 106 {DRVSTAT_INFO(rx_drops_no_tpre_descr)}, 107 /* Received packets dropped when they need more than 8 108 * receive buffers. This cannot happen as the driver configures 109 * 2048 byte receive buffers. 110 */ 111 {DRVSTAT_INFO(rx_drops_too_many_frags)}, 112 {DRVSTAT_INFO(forwarded_packets)}, 113 /* Received packets dropped when the frame length 114 * is more than 9018 bytes 115 */ 116 {DRVSTAT_INFO(rx_drops_mtu)}, 117 /* Number of packets dropped due to random early drop function */ 118 {DRVSTAT_INFO(eth_red_drops)}, 119 {DRVSTAT_INFO(be_on_die_temperature)}, 120 {DRVSTAT_INFO(rx_roce_bytes_lsd)}, 121 {DRVSTAT_INFO(rx_roce_bytes_msd)}, 122 {DRVSTAT_INFO(rx_roce_frames)}, 123 {DRVSTAT_INFO(roce_drops_payload_len)}, 124 {DRVSTAT_INFO(roce_drops_crc)} 125 }; 126 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats) 127 128 /* Stats related to multi RX queues: get_stats routine assumes bytes, pkts 129 * are first and second members respectively. 130 */ 131 static const struct be_ethtool_stat et_rx_stats[] = { 132 {DRVSTAT_RX_INFO(rx_bytes)},/* If moving this member see above note */ 133 {DRVSTAT_RX_INFO(rx_pkts)}, /* If moving this member see above note */ 134 {DRVSTAT_RX_INFO(rx_compl)}, 135 {DRVSTAT_RX_INFO(rx_compl_err)}, 136 {DRVSTAT_RX_INFO(rx_mcast_pkts)}, 137 /* Number of page allocation failures while posting receive buffers 138 * to HW. 139 */ 140 {DRVSTAT_RX_INFO(rx_post_fail)}, 141 /* Recevied packets dropped due to skb allocation failure */ 142 {DRVSTAT_RX_INFO(rx_drops_no_skbs)}, 143 /* Received packets dropped due to lack of available fetched buffers 144 * posted by the driver. 145 */ 146 {DRVSTAT_RX_INFO(rx_drops_no_frags)} 147 }; 148 #define ETHTOOL_RXSTATS_NUM (ARRAY_SIZE(et_rx_stats)) 149 150 /* Stats related to multi TX queues: get_stats routine assumes compl is the 151 * first member 152 */ 153 static const struct be_ethtool_stat et_tx_stats[] = { 154 {DRVSTAT_TX_INFO(tx_compl)}, /* If moving this member see above note */ 155 {DRVSTAT_TX_INFO(tx_bytes)}, 156 {DRVSTAT_TX_INFO(tx_pkts)}, 157 /* Number of skbs queued for trasmission by the driver */ 158 {DRVSTAT_TX_INFO(tx_reqs)}, 159 /* Number of TX work request blocks DMAed to HW */ 160 {DRVSTAT_TX_INFO(tx_wrbs)}, 161 /* Number of times the TX queue was stopped due to lack 162 * of spaces in the TXQ. 163 */ 164 {DRVSTAT_TX_INFO(tx_stops)}, 165 /* Pkts dropped in the driver's transmit path */ 166 {DRVSTAT_TX_INFO(tx_drv_drops)} 167 }; 168 #define ETHTOOL_TXSTATS_NUM (ARRAY_SIZE(et_tx_stats)) 169 170 static const char et_self_tests[][ETH_GSTRING_LEN] = { 171 "MAC Loopback test", 172 "PHY Loopback test", 173 "External Loopback test", 174 "DDR DMA test", 175 "Link test" 176 }; 177 178 #define ETHTOOL_TESTS_NUM ARRAY_SIZE(et_self_tests) 179 #define BE_MAC_LOOPBACK 0x0 180 #define BE_PHY_LOOPBACK 0x1 181 #define BE_ONE_PORT_EXT_LOOPBACK 0x2 182 #define BE_NO_LOOPBACK 0xff 183 184 static void be_get_drvinfo(struct net_device *netdev, 185 struct ethtool_drvinfo *drvinfo) 186 { 187 struct be_adapter *adapter = netdev_priv(netdev); 188 189 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver)); 190 strlcpy(drvinfo->version, DRV_VER, sizeof(drvinfo->version)); 191 if (!memcmp(adapter->fw_ver, adapter->fw_on_flash, FW_VER_LEN)) 192 strlcpy(drvinfo->fw_version, adapter->fw_ver, 193 sizeof(drvinfo->fw_version)); 194 else 195 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 196 "%s [%s]", adapter->fw_ver, adapter->fw_on_flash); 197 198 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 199 sizeof(drvinfo->bus_info)); 200 drvinfo->testinfo_len = 0; 201 drvinfo->regdump_len = 0; 202 drvinfo->eedump_len = 0; 203 } 204 205 static u32 lancer_cmd_get_file_len(struct be_adapter *adapter, u8 *file_name) 206 { 207 u32 data_read = 0, eof; 208 u8 addn_status; 209 struct be_dma_mem data_len_cmd; 210 int status; 211 212 memset(&data_len_cmd, 0, sizeof(data_len_cmd)); 213 /* data_offset and data_size should be 0 to get reg len */ 214 status = lancer_cmd_read_object(adapter, &data_len_cmd, 0, 0, 215 file_name, &data_read, &eof, 216 &addn_status); 217 218 return data_read; 219 } 220 221 static int lancer_cmd_read_file(struct be_adapter *adapter, u8 *file_name, 222 u32 buf_len, void *buf) 223 { 224 struct be_dma_mem read_cmd; 225 u32 read_len = 0, total_read_len = 0, chunk_size; 226 u32 eof = 0; 227 u8 addn_status; 228 int status = 0; 229 230 read_cmd.size = LANCER_READ_FILE_CHUNK; 231 read_cmd.va = pci_alloc_consistent(adapter->pdev, read_cmd.size, 232 &read_cmd.dma); 233 234 if (!read_cmd.va) { 235 dev_err(&adapter->pdev->dev, 236 "Memory allocation failure while reading dump\n"); 237 return -ENOMEM; 238 } 239 240 while ((total_read_len < buf_len) && !eof) { 241 chunk_size = min_t(u32, (buf_len - total_read_len), 242 LANCER_READ_FILE_CHUNK); 243 chunk_size = ALIGN(chunk_size, 4); 244 status = lancer_cmd_read_object(adapter, &read_cmd, chunk_size, 245 total_read_len, file_name, 246 &read_len, &eof, &addn_status); 247 if (!status) { 248 memcpy(buf + total_read_len, read_cmd.va, read_len); 249 total_read_len += read_len; 250 eof &= LANCER_READ_FILE_EOF_MASK; 251 } else { 252 status = -EIO; 253 break; 254 } 255 } 256 pci_free_consistent(adapter->pdev, read_cmd.size, read_cmd.va, 257 read_cmd.dma); 258 259 return status; 260 } 261 262 static int be_get_reg_len(struct net_device *netdev) 263 { 264 struct be_adapter *adapter = netdev_priv(netdev); 265 u32 log_size = 0; 266 267 if (!check_privilege(adapter, MAX_PRIVILEGES)) 268 return 0; 269 270 if (be_physfn(adapter)) { 271 if (lancer_chip(adapter)) 272 log_size = lancer_cmd_get_file_len(adapter, 273 LANCER_FW_DUMP_FILE); 274 else 275 be_cmd_get_reg_len(adapter, &log_size); 276 } 277 return log_size; 278 } 279 280 static void 281 be_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *buf) 282 { 283 struct be_adapter *adapter = netdev_priv(netdev); 284 285 if (be_physfn(adapter)) { 286 memset(buf, 0, regs->len); 287 if (lancer_chip(adapter)) 288 lancer_cmd_read_file(adapter, LANCER_FW_DUMP_FILE, 289 regs->len, buf); 290 else 291 be_cmd_get_regs(adapter, regs->len, buf); 292 } 293 } 294 295 static int be_get_coalesce(struct net_device *netdev, 296 struct ethtool_coalesce *et) 297 { 298 struct be_adapter *adapter = netdev_priv(netdev); 299 struct be_aic_obj *aic = &adapter->aic_obj[0]; 300 301 302 et->rx_coalesce_usecs = aic->prev_eqd; 303 et->rx_coalesce_usecs_high = aic->max_eqd; 304 et->rx_coalesce_usecs_low = aic->min_eqd; 305 306 et->tx_coalesce_usecs = aic->prev_eqd; 307 et->tx_coalesce_usecs_high = aic->max_eqd; 308 et->tx_coalesce_usecs_low = aic->min_eqd; 309 310 et->use_adaptive_rx_coalesce = aic->enable; 311 et->use_adaptive_tx_coalesce = aic->enable; 312 313 return 0; 314 } 315 316 /* TX attributes are ignored. Only RX attributes are considered 317 * eqd cmd is issued in the worker thread. 318 */ 319 static int be_set_coalesce(struct net_device *netdev, 320 struct ethtool_coalesce *et) 321 { 322 struct be_adapter *adapter = netdev_priv(netdev); 323 struct be_aic_obj *aic = &adapter->aic_obj[0]; 324 struct be_eq_obj *eqo; 325 int i; 326 327 for_all_evt_queues(adapter, eqo, i) { 328 aic->enable = et->use_adaptive_rx_coalesce; 329 aic->max_eqd = min(et->rx_coalesce_usecs_high, BE_MAX_EQD); 330 aic->min_eqd = min(et->rx_coalesce_usecs_low, aic->max_eqd); 331 aic->et_eqd = min(et->rx_coalesce_usecs, aic->max_eqd); 332 aic->et_eqd = max(aic->et_eqd, aic->min_eqd); 333 aic++; 334 } 335 336 return 0; 337 } 338 339 static void be_get_ethtool_stats(struct net_device *netdev, 340 struct ethtool_stats *stats, uint64_t *data) 341 { 342 struct be_adapter *adapter = netdev_priv(netdev); 343 struct be_rx_obj *rxo; 344 struct be_tx_obj *txo; 345 void *p; 346 unsigned int i, j, base = 0, start; 347 348 for (i = 0; i < ETHTOOL_STATS_NUM; i++) { 349 p = (u8 *)&adapter->drv_stats + et_stats[i].offset; 350 data[i] = *(u32 *)p; 351 } 352 base += ETHTOOL_STATS_NUM; 353 354 for_all_rx_queues(adapter, rxo, j) { 355 struct be_rx_stats *stats = rx_stats(rxo); 356 357 do { 358 start = u64_stats_fetch_begin_irq(&stats->sync); 359 data[base] = stats->rx_bytes; 360 data[base + 1] = stats->rx_pkts; 361 } while (u64_stats_fetch_retry_irq(&stats->sync, start)); 362 363 for (i = 2; i < ETHTOOL_RXSTATS_NUM; i++) { 364 p = (u8 *)stats + et_rx_stats[i].offset; 365 data[base + i] = *(u32 *)p; 366 } 367 base += ETHTOOL_RXSTATS_NUM; 368 } 369 370 for_all_tx_queues(adapter, txo, j) { 371 struct be_tx_stats *stats = tx_stats(txo); 372 373 do { 374 start = u64_stats_fetch_begin_irq(&stats->sync_compl); 375 data[base] = stats->tx_compl; 376 } while (u64_stats_fetch_retry_irq(&stats->sync_compl, start)); 377 378 do { 379 start = u64_stats_fetch_begin_irq(&stats->sync); 380 for (i = 1; i < ETHTOOL_TXSTATS_NUM; i++) { 381 p = (u8 *)stats + et_tx_stats[i].offset; 382 data[base + i] = 383 (et_tx_stats[i].size == sizeof(u64)) ? 384 *(u64 *)p : *(u32 *)p; 385 } 386 } while (u64_stats_fetch_retry_irq(&stats->sync, start)); 387 base += ETHTOOL_TXSTATS_NUM; 388 } 389 } 390 391 static void be_get_stat_strings(struct net_device *netdev, uint32_t stringset, 392 uint8_t *data) 393 { 394 struct be_adapter *adapter = netdev_priv(netdev); 395 int i, j; 396 397 switch (stringset) { 398 case ETH_SS_STATS: 399 for (i = 0; i < ETHTOOL_STATS_NUM; i++) { 400 memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN); 401 data += ETH_GSTRING_LEN; 402 } 403 for (i = 0; i < adapter->num_rx_qs; i++) { 404 for (j = 0; j < ETHTOOL_RXSTATS_NUM; j++) { 405 sprintf(data, "rxq%d: %s", i, 406 et_rx_stats[j].desc); 407 data += ETH_GSTRING_LEN; 408 } 409 } 410 for (i = 0; i < adapter->num_tx_qs; i++) { 411 for (j = 0; j < ETHTOOL_TXSTATS_NUM; j++) { 412 sprintf(data, "txq%d: %s", i, 413 et_tx_stats[j].desc); 414 data += ETH_GSTRING_LEN; 415 } 416 } 417 break; 418 case ETH_SS_TEST: 419 for (i = 0; i < ETHTOOL_TESTS_NUM; i++) { 420 memcpy(data, et_self_tests[i], ETH_GSTRING_LEN); 421 data += ETH_GSTRING_LEN; 422 } 423 break; 424 } 425 } 426 427 static int be_get_sset_count(struct net_device *netdev, int stringset) 428 { 429 struct be_adapter *adapter = netdev_priv(netdev); 430 431 switch (stringset) { 432 case ETH_SS_TEST: 433 return ETHTOOL_TESTS_NUM; 434 case ETH_SS_STATS: 435 return ETHTOOL_STATS_NUM + 436 adapter->num_rx_qs * ETHTOOL_RXSTATS_NUM + 437 adapter->num_tx_qs * ETHTOOL_TXSTATS_NUM; 438 default: 439 return -EINVAL; 440 } 441 } 442 443 static u32 be_get_port_type(u32 phy_type, u32 dac_cable_len) 444 { 445 u32 port; 446 447 switch (phy_type) { 448 case PHY_TYPE_BASET_1GB: 449 case PHY_TYPE_BASEX_1GB: 450 case PHY_TYPE_SGMII: 451 port = PORT_TP; 452 break; 453 case PHY_TYPE_SFP_PLUS_10GB: 454 port = dac_cable_len ? PORT_DA : PORT_FIBRE; 455 break; 456 case PHY_TYPE_XFP_10GB: 457 case PHY_TYPE_SFP_1GB: 458 port = PORT_FIBRE; 459 break; 460 case PHY_TYPE_BASET_10GB: 461 port = PORT_TP; 462 break; 463 default: 464 port = PORT_OTHER; 465 } 466 467 return port; 468 } 469 470 static u32 convert_to_et_setting(u32 if_type, u32 if_speeds) 471 { 472 u32 val = 0; 473 474 switch (if_type) { 475 case PHY_TYPE_BASET_1GB: 476 case PHY_TYPE_BASEX_1GB: 477 case PHY_TYPE_SGMII: 478 val |= SUPPORTED_TP; 479 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS) 480 val |= SUPPORTED_1000baseT_Full; 481 if (if_speeds & BE_SUPPORTED_SPEED_100MBPS) 482 val |= SUPPORTED_100baseT_Full; 483 if (if_speeds & BE_SUPPORTED_SPEED_10MBPS) 484 val |= SUPPORTED_10baseT_Full; 485 break; 486 case PHY_TYPE_KX4_10GB: 487 val |= SUPPORTED_Backplane; 488 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS) 489 val |= SUPPORTED_1000baseKX_Full; 490 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS) 491 val |= SUPPORTED_10000baseKX4_Full; 492 break; 493 case PHY_TYPE_KR_10GB: 494 val |= SUPPORTED_Backplane | 495 SUPPORTED_10000baseKR_Full; 496 break; 497 case PHY_TYPE_SFP_PLUS_10GB: 498 case PHY_TYPE_XFP_10GB: 499 case PHY_TYPE_SFP_1GB: 500 val |= SUPPORTED_FIBRE; 501 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS) 502 val |= SUPPORTED_10000baseT_Full; 503 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS) 504 val |= SUPPORTED_1000baseT_Full; 505 break; 506 case PHY_TYPE_BASET_10GB: 507 val |= SUPPORTED_TP; 508 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS) 509 val |= SUPPORTED_10000baseT_Full; 510 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS) 511 val |= SUPPORTED_1000baseT_Full; 512 if (if_speeds & BE_SUPPORTED_SPEED_100MBPS) 513 val |= SUPPORTED_100baseT_Full; 514 break; 515 default: 516 val |= SUPPORTED_TP; 517 } 518 519 return val; 520 } 521 522 bool be_pause_supported(struct be_adapter *adapter) 523 { 524 return (adapter->phy.interface_type == PHY_TYPE_SFP_PLUS_10GB || 525 adapter->phy.interface_type == PHY_TYPE_XFP_10GB) ? 526 false : true; 527 } 528 529 static int be_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd) 530 { 531 struct be_adapter *adapter = netdev_priv(netdev); 532 u8 link_status; 533 u16 link_speed = 0; 534 int status; 535 u32 auto_speeds; 536 u32 fixed_speeds; 537 u32 dac_cable_len; 538 u16 interface_type; 539 540 if (adapter->phy.link_speed < 0) { 541 status = be_cmd_link_status_query(adapter, &link_speed, 542 &link_status, 0); 543 if (!status) 544 be_link_status_update(adapter, link_status); 545 ethtool_cmd_speed_set(ecmd, link_speed); 546 547 status = be_cmd_get_phy_info(adapter); 548 if (!status) { 549 interface_type = adapter->phy.interface_type; 550 auto_speeds = adapter->phy.auto_speeds_supported; 551 fixed_speeds = adapter->phy.fixed_speeds_supported; 552 dac_cable_len = adapter->phy.dac_cable_len; 553 554 ecmd->supported = 555 convert_to_et_setting(interface_type, 556 auto_speeds | 557 fixed_speeds); 558 ecmd->advertising = 559 convert_to_et_setting(interface_type, 560 auto_speeds); 561 562 ecmd->port = be_get_port_type(interface_type, 563 dac_cable_len); 564 565 if (adapter->phy.auto_speeds_supported) { 566 ecmd->supported |= SUPPORTED_Autoneg; 567 ecmd->autoneg = AUTONEG_ENABLE; 568 ecmd->advertising |= ADVERTISED_Autoneg; 569 } 570 571 ecmd->supported |= SUPPORTED_Pause; 572 if (be_pause_supported(adapter)) 573 ecmd->advertising |= ADVERTISED_Pause; 574 575 switch (adapter->phy.interface_type) { 576 case PHY_TYPE_KR_10GB: 577 case PHY_TYPE_KX4_10GB: 578 ecmd->transceiver = XCVR_INTERNAL; 579 break; 580 default: 581 ecmd->transceiver = XCVR_EXTERNAL; 582 break; 583 } 584 } else { 585 ecmd->port = PORT_OTHER; 586 ecmd->autoneg = AUTONEG_DISABLE; 587 ecmd->transceiver = XCVR_DUMMY1; 588 } 589 590 /* Save for future use */ 591 adapter->phy.link_speed = ethtool_cmd_speed(ecmd); 592 adapter->phy.port_type = ecmd->port; 593 adapter->phy.transceiver = ecmd->transceiver; 594 adapter->phy.autoneg = ecmd->autoneg; 595 adapter->phy.advertising = ecmd->advertising; 596 adapter->phy.supported = ecmd->supported; 597 } else { 598 ethtool_cmd_speed_set(ecmd, adapter->phy.link_speed); 599 ecmd->port = adapter->phy.port_type; 600 ecmd->transceiver = adapter->phy.transceiver; 601 ecmd->autoneg = adapter->phy.autoneg; 602 ecmd->advertising = adapter->phy.advertising; 603 ecmd->supported = adapter->phy.supported; 604 } 605 606 ecmd->duplex = netif_carrier_ok(netdev) ? DUPLEX_FULL : DUPLEX_UNKNOWN; 607 ecmd->phy_address = adapter->port_num; 608 609 return 0; 610 } 611 612 static void be_get_ringparam(struct net_device *netdev, 613 struct ethtool_ringparam *ring) 614 { 615 struct be_adapter *adapter = netdev_priv(netdev); 616 617 ring->rx_max_pending = ring->rx_pending = adapter->rx_obj[0].q.len; 618 ring->tx_max_pending = ring->tx_pending = adapter->tx_obj[0].q.len; 619 } 620 621 static void 622 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd) 623 { 624 struct be_adapter *adapter = netdev_priv(netdev); 625 626 be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause); 627 ecmd->autoneg = adapter->phy.fc_autoneg; 628 } 629 630 static int 631 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd) 632 { 633 struct be_adapter *adapter = netdev_priv(netdev); 634 int status; 635 636 if (ecmd->autoneg != adapter->phy.fc_autoneg) 637 return -EINVAL; 638 adapter->tx_fc = ecmd->tx_pause; 639 adapter->rx_fc = ecmd->rx_pause; 640 641 status = be_cmd_set_flow_control(adapter, 642 adapter->tx_fc, adapter->rx_fc); 643 if (status) 644 dev_warn(&adapter->pdev->dev, "Pause param set failed.\n"); 645 646 return be_cmd_status(status); 647 } 648 649 static int be_set_phys_id(struct net_device *netdev, 650 enum ethtool_phys_id_state state) 651 { 652 struct be_adapter *adapter = netdev_priv(netdev); 653 654 switch (state) { 655 case ETHTOOL_ID_ACTIVE: 656 be_cmd_get_beacon_state(adapter, adapter->hba_port_num, 657 &adapter->beacon_state); 658 return 1; /* cycle on/off once per second */ 659 660 case ETHTOOL_ID_ON: 661 be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0, 662 BEACON_STATE_ENABLED); 663 break; 664 665 case ETHTOOL_ID_OFF: 666 be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0, 667 BEACON_STATE_DISABLED); 668 break; 669 670 case ETHTOOL_ID_INACTIVE: 671 be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0, 672 adapter->beacon_state); 673 } 674 675 return 0; 676 } 677 678 static int be_set_dump(struct net_device *netdev, struct ethtool_dump *dump) 679 { 680 struct be_adapter *adapter = netdev_priv(netdev); 681 struct device *dev = &adapter->pdev->dev; 682 int status; 683 684 if (!lancer_chip(adapter) || 685 !check_privilege(adapter, MAX_PRIVILEGES)) 686 return -EOPNOTSUPP; 687 688 switch (dump->flag) { 689 case LANCER_INITIATE_FW_DUMP: 690 status = lancer_initiate_dump(adapter); 691 if (!status) 692 dev_info(dev, "FW dump initiated successfully\n"); 693 break; 694 case LANCER_DELETE_FW_DUMP: 695 status = lancer_delete_dump(adapter); 696 if (!status) 697 dev_info(dev, "FW dump deleted successfully\n"); 698 break; 699 default: 700 dev_err(dev, "Invalid dump level: 0x%x\n", dump->flag); 701 return -EINVAL; 702 } 703 return status; 704 } 705 706 static void be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 707 { 708 struct be_adapter *adapter = netdev_priv(netdev); 709 710 if (adapter->wol_cap & BE_WOL_CAP) { 711 wol->supported |= WAKE_MAGIC; 712 if (adapter->wol_en) 713 wol->wolopts |= WAKE_MAGIC; 714 } else { 715 wol->wolopts = 0; 716 } 717 memset(&wol->sopass, 0, sizeof(wol->sopass)); 718 } 719 720 static int be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 721 { 722 struct be_adapter *adapter = netdev_priv(netdev); 723 724 if (wol->wolopts & ~WAKE_MAGIC) 725 return -EOPNOTSUPP; 726 727 if (!(adapter->wol_cap & BE_WOL_CAP)) { 728 dev_warn(&adapter->pdev->dev, "WOL not supported\n"); 729 return -EOPNOTSUPP; 730 } 731 732 if (wol->wolopts & WAKE_MAGIC) 733 adapter->wol_en = true; 734 else 735 adapter->wol_en = false; 736 737 return 0; 738 } 739 740 static int be_test_ddr_dma(struct be_adapter *adapter) 741 { 742 int ret, i; 743 struct be_dma_mem ddrdma_cmd; 744 static const u64 pattern[2] = { 745 0x5a5a5a5a5a5a5a5aULL, 0xa5a5a5a5a5a5a5a5ULL 746 }; 747 748 ddrdma_cmd.size = sizeof(struct be_cmd_req_ddrdma_test); 749 ddrdma_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, ddrdma_cmd.size, 750 &ddrdma_cmd.dma, GFP_KERNEL); 751 if (!ddrdma_cmd.va) 752 return -ENOMEM; 753 754 for (i = 0; i < 2; i++) { 755 ret = be_cmd_ddr_dma_test(adapter, pattern[i], 756 4096, &ddrdma_cmd); 757 if (ret != 0) 758 goto err; 759 } 760 761 err: 762 dma_free_coherent(&adapter->pdev->dev, ddrdma_cmd.size, ddrdma_cmd.va, 763 ddrdma_cmd.dma); 764 return be_cmd_status(ret); 765 } 766 767 static u64 be_loopback_test(struct be_adapter *adapter, u8 loopback_type, 768 u64 *status) 769 { 770 be_cmd_set_loopback(adapter, adapter->hba_port_num, loopback_type, 1); 771 *status = be_cmd_loopback_test(adapter, adapter->hba_port_num, 772 loopback_type, 1500, 2, 0xabc); 773 be_cmd_set_loopback(adapter, adapter->hba_port_num, BE_NO_LOOPBACK, 1); 774 return *status; 775 } 776 777 static void be_self_test(struct net_device *netdev, struct ethtool_test *test, 778 u64 *data) 779 { 780 struct be_adapter *adapter = netdev_priv(netdev); 781 int status; 782 u8 link_status = 0; 783 784 if (adapter->function_caps & BE_FUNCTION_CAPS_SUPER_NIC) { 785 dev_err(&adapter->pdev->dev, "Self test not supported\n"); 786 test->flags |= ETH_TEST_FL_FAILED; 787 return; 788 } 789 790 memset(data, 0, sizeof(u64) * ETHTOOL_TESTS_NUM); 791 792 if (test->flags & ETH_TEST_FL_OFFLINE) { 793 if (be_loopback_test(adapter, BE_MAC_LOOPBACK, &data[0]) != 0) 794 test->flags |= ETH_TEST_FL_FAILED; 795 796 if (be_loopback_test(adapter, BE_PHY_LOOPBACK, &data[1]) != 0) 797 test->flags |= ETH_TEST_FL_FAILED; 798 799 if (test->flags & ETH_TEST_FL_EXTERNAL_LB) { 800 if (be_loopback_test(adapter, BE_ONE_PORT_EXT_LOOPBACK, 801 &data[2]) != 0) 802 test->flags |= ETH_TEST_FL_FAILED; 803 test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE; 804 } 805 } 806 807 if (!lancer_chip(adapter) && be_test_ddr_dma(adapter) != 0) { 808 data[3] = 1; 809 test->flags |= ETH_TEST_FL_FAILED; 810 } 811 812 status = be_cmd_link_status_query(adapter, NULL, &link_status, 0); 813 if (status) { 814 test->flags |= ETH_TEST_FL_FAILED; 815 data[4] = -1; 816 } else if (!link_status) { 817 test->flags |= ETH_TEST_FL_FAILED; 818 data[4] = 1; 819 } 820 } 821 822 static int be_do_flash(struct net_device *netdev, struct ethtool_flash *efl) 823 { 824 struct be_adapter *adapter = netdev_priv(netdev); 825 826 return be_load_fw(adapter, efl->data); 827 } 828 829 static int be_get_eeprom_len(struct net_device *netdev) 830 { 831 struct be_adapter *adapter = netdev_priv(netdev); 832 833 if (!check_privilege(adapter, MAX_PRIVILEGES)) 834 return 0; 835 836 if (lancer_chip(adapter)) { 837 if (be_physfn(adapter)) 838 return lancer_cmd_get_file_len(adapter, 839 LANCER_VPD_PF_FILE); 840 else 841 return lancer_cmd_get_file_len(adapter, 842 LANCER_VPD_VF_FILE); 843 } else { 844 return BE_READ_SEEPROM_LEN; 845 } 846 } 847 848 static int be_read_eeprom(struct net_device *netdev, 849 struct ethtool_eeprom *eeprom, uint8_t *data) 850 { 851 struct be_adapter *adapter = netdev_priv(netdev); 852 struct be_dma_mem eeprom_cmd; 853 struct be_cmd_resp_seeprom_read *resp; 854 int status; 855 856 if (!eeprom->len) 857 return -EINVAL; 858 859 if (lancer_chip(adapter)) { 860 if (be_physfn(adapter)) 861 return lancer_cmd_read_file(adapter, LANCER_VPD_PF_FILE, 862 eeprom->len, data); 863 else 864 return lancer_cmd_read_file(adapter, LANCER_VPD_VF_FILE, 865 eeprom->len, data); 866 } 867 868 eeprom->magic = BE_VENDOR_ID | (adapter->pdev->device<<16); 869 870 memset(&eeprom_cmd, 0, sizeof(struct be_dma_mem)); 871 eeprom_cmd.size = sizeof(struct be_cmd_req_seeprom_read); 872 eeprom_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, eeprom_cmd.size, 873 &eeprom_cmd.dma, GFP_KERNEL); 874 875 if (!eeprom_cmd.va) 876 return -ENOMEM; 877 878 status = be_cmd_get_seeprom_data(adapter, &eeprom_cmd); 879 880 if (!status) { 881 resp = eeprom_cmd.va; 882 memcpy(data, resp->seeprom_data + eeprom->offset, eeprom->len); 883 } 884 dma_free_coherent(&adapter->pdev->dev, eeprom_cmd.size, eeprom_cmd.va, 885 eeprom_cmd.dma); 886 887 return be_cmd_status(status); 888 } 889 890 static u32 be_get_msg_level(struct net_device *netdev) 891 { 892 struct be_adapter *adapter = netdev_priv(netdev); 893 894 return adapter->msg_enable; 895 } 896 897 static void be_set_msg_level(struct net_device *netdev, u32 level) 898 { 899 struct be_adapter *adapter = netdev_priv(netdev); 900 901 if (adapter->msg_enable == level) 902 return; 903 904 if ((level & NETIF_MSG_HW) != (adapter->msg_enable & NETIF_MSG_HW)) 905 if (BEx_chip(adapter)) 906 be_cmd_set_fw_log_level(adapter, level & NETIF_MSG_HW ? 907 FW_LOG_LEVEL_DEFAULT : 908 FW_LOG_LEVEL_FATAL); 909 adapter->msg_enable = level; 910 911 return; 912 } 913 914 static u64 be_get_rss_hash_opts(struct be_adapter *adapter, u64 flow_type) 915 { 916 u64 data = 0; 917 918 switch (flow_type) { 919 case TCP_V4_FLOW: 920 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV4) 921 data |= RXH_IP_DST | RXH_IP_SRC; 922 if (adapter->rss_info.rss_flags & RSS_ENABLE_TCP_IPV4) 923 data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 924 break; 925 case UDP_V4_FLOW: 926 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV4) 927 data |= RXH_IP_DST | RXH_IP_SRC; 928 if (adapter->rss_info.rss_flags & RSS_ENABLE_UDP_IPV4) 929 data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 930 break; 931 case TCP_V6_FLOW: 932 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV6) 933 data |= RXH_IP_DST | RXH_IP_SRC; 934 if (adapter->rss_info.rss_flags & RSS_ENABLE_TCP_IPV6) 935 data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 936 break; 937 case UDP_V6_FLOW: 938 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV6) 939 data |= RXH_IP_DST | RXH_IP_SRC; 940 if (adapter->rss_info.rss_flags & RSS_ENABLE_UDP_IPV6) 941 data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 942 break; 943 } 944 945 return data; 946 } 947 948 static int be_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd, 949 u32 *rule_locs) 950 { 951 struct be_adapter *adapter = netdev_priv(netdev); 952 953 if (!be_multi_rxq(adapter)) { 954 dev_info(&adapter->pdev->dev, 955 "ethtool::get_rxnfc: RX flow hashing is disabled\n"); 956 return -EINVAL; 957 } 958 959 switch (cmd->cmd) { 960 case ETHTOOL_GRXFH: 961 cmd->data = be_get_rss_hash_opts(adapter, cmd->flow_type); 962 break; 963 case ETHTOOL_GRXRINGS: 964 cmd->data = adapter->num_rx_qs - 1; 965 break; 966 default: 967 return -EINVAL; 968 } 969 970 return 0; 971 } 972 973 static int be_set_rss_hash_opts(struct be_adapter *adapter, 974 struct ethtool_rxnfc *cmd) 975 { 976 struct be_rx_obj *rxo; 977 int status = 0, i, j; 978 u8 rsstable[128]; 979 u32 rss_flags = adapter->rss_info.rss_flags; 980 981 if (cmd->data != L3_RSS_FLAGS && 982 cmd->data != (L3_RSS_FLAGS | L4_RSS_FLAGS)) 983 return -EINVAL; 984 985 switch (cmd->flow_type) { 986 case TCP_V4_FLOW: 987 if (cmd->data == L3_RSS_FLAGS) 988 rss_flags &= ~RSS_ENABLE_TCP_IPV4; 989 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) 990 rss_flags |= RSS_ENABLE_IPV4 | 991 RSS_ENABLE_TCP_IPV4; 992 break; 993 case TCP_V6_FLOW: 994 if (cmd->data == L3_RSS_FLAGS) 995 rss_flags &= ~RSS_ENABLE_TCP_IPV6; 996 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) 997 rss_flags |= RSS_ENABLE_IPV6 | 998 RSS_ENABLE_TCP_IPV6; 999 break; 1000 case UDP_V4_FLOW: 1001 if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) && 1002 BEx_chip(adapter)) 1003 return -EINVAL; 1004 1005 if (cmd->data == L3_RSS_FLAGS) 1006 rss_flags &= ~RSS_ENABLE_UDP_IPV4; 1007 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) 1008 rss_flags |= RSS_ENABLE_IPV4 | 1009 RSS_ENABLE_UDP_IPV4; 1010 break; 1011 case UDP_V6_FLOW: 1012 if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) && 1013 BEx_chip(adapter)) 1014 return -EINVAL; 1015 1016 if (cmd->data == L3_RSS_FLAGS) 1017 rss_flags &= ~RSS_ENABLE_UDP_IPV6; 1018 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) 1019 rss_flags |= RSS_ENABLE_IPV6 | 1020 RSS_ENABLE_UDP_IPV6; 1021 break; 1022 default: 1023 return -EINVAL; 1024 } 1025 1026 if (rss_flags == adapter->rss_info.rss_flags) 1027 return status; 1028 1029 if (be_multi_rxq(adapter)) { 1030 for (j = 0; j < 128; j += adapter->num_rx_qs - 1) { 1031 for_all_rss_queues(adapter, rxo, i) { 1032 if ((j + i) >= 128) 1033 break; 1034 rsstable[j + i] = rxo->rss_id; 1035 } 1036 } 1037 } 1038 1039 status = be_cmd_rss_config(adapter, adapter->rss_info.rsstable, 1040 rss_flags, 128, adapter->rss_info.rss_hkey); 1041 if (!status) 1042 adapter->rss_info.rss_flags = rss_flags; 1043 1044 return be_cmd_status(status); 1045 } 1046 1047 static int be_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 1048 { 1049 struct be_adapter *adapter = netdev_priv(netdev); 1050 int status = 0; 1051 1052 if (!be_multi_rxq(adapter)) { 1053 dev_err(&adapter->pdev->dev, 1054 "ethtool::set_rxnfc: RX flow hashing is disabled\n"); 1055 return -EINVAL; 1056 } 1057 1058 switch (cmd->cmd) { 1059 case ETHTOOL_SRXFH: 1060 status = be_set_rss_hash_opts(adapter, cmd); 1061 break; 1062 default: 1063 return -EINVAL; 1064 } 1065 1066 return status; 1067 } 1068 1069 static void be_get_channels(struct net_device *netdev, 1070 struct ethtool_channels *ch) 1071 { 1072 struct be_adapter *adapter = netdev_priv(netdev); 1073 1074 ch->combined_count = adapter->num_evt_qs; 1075 ch->max_combined = be_max_qs(adapter); 1076 } 1077 1078 static int be_set_channels(struct net_device *netdev, 1079 struct ethtool_channels *ch) 1080 { 1081 struct be_adapter *adapter = netdev_priv(netdev); 1082 int status; 1083 1084 if (ch->rx_count || ch->tx_count || ch->other_count || 1085 !ch->combined_count || ch->combined_count > be_max_qs(adapter)) 1086 return -EINVAL; 1087 1088 adapter->cfg_num_qs = ch->combined_count; 1089 1090 status = be_update_queues(adapter); 1091 return be_cmd_status(status); 1092 } 1093 1094 static u32 be_get_rxfh_indir_size(struct net_device *netdev) 1095 { 1096 return RSS_INDIR_TABLE_LEN; 1097 } 1098 1099 static u32 be_get_rxfh_key_size(struct net_device *netdev) 1100 { 1101 return RSS_HASH_KEY_LEN; 1102 } 1103 1104 static int be_get_rxfh(struct net_device *netdev, u32 *indir, u8 *hkey) 1105 { 1106 struct be_adapter *adapter = netdev_priv(netdev); 1107 int i; 1108 struct rss_info *rss = &adapter->rss_info; 1109 1110 if (indir) { 1111 for (i = 0; i < RSS_INDIR_TABLE_LEN; i++) 1112 indir[i] = rss->rss_queue[i]; 1113 } 1114 1115 if (hkey) 1116 memcpy(hkey, rss->rss_hkey, RSS_HASH_KEY_LEN); 1117 1118 return 0; 1119 } 1120 1121 static int be_set_rxfh(struct net_device *netdev, const u32 *indir, 1122 const u8 *hkey) 1123 { 1124 int rc = 0, i, j; 1125 struct be_adapter *adapter = netdev_priv(netdev); 1126 u8 rsstable[RSS_INDIR_TABLE_LEN]; 1127 1128 if (indir) { 1129 struct be_rx_obj *rxo; 1130 for (i = 0; i < RSS_INDIR_TABLE_LEN; i++) { 1131 j = indir[i]; 1132 rxo = &adapter->rx_obj[j]; 1133 rsstable[i] = rxo->rss_id; 1134 adapter->rss_info.rss_queue[i] = j; 1135 } 1136 } else { 1137 memcpy(rsstable, adapter->rss_info.rsstable, 1138 RSS_INDIR_TABLE_LEN); 1139 } 1140 1141 if (!hkey) 1142 hkey = adapter->rss_info.rss_hkey; 1143 1144 rc = be_cmd_rss_config(adapter, rsstable, 1145 adapter->rss_info.rss_flags, 1146 RSS_INDIR_TABLE_LEN, hkey); 1147 if (rc) { 1148 adapter->rss_info.rss_flags = RSS_ENABLE_NONE; 1149 return -EIO; 1150 } 1151 memcpy(adapter->rss_info.rss_hkey, hkey, RSS_HASH_KEY_LEN); 1152 memcpy(adapter->rss_info.rsstable, rsstable, 1153 RSS_INDIR_TABLE_LEN); 1154 return 0; 1155 } 1156 1157 const struct ethtool_ops be_ethtool_ops = { 1158 .get_settings = be_get_settings, 1159 .get_drvinfo = be_get_drvinfo, 1160 .get_wol = be_get_wol, 1161 .set_wol = be_set_wol, 1162 .get_link = ethtool_op_get_link, 1163 .get_eeprom_len = be_get_eeprom_len, 1164 .get_eeprom = be_read_eeprom, 1165 .get_coalesce = be_get_coalesce, 1166 .set_coalesce = be_set_coalesce, 1167 .get_ringparam = be_get_ringparam, 1168 .get_pauseparam = be_get_pauseparam, 1169 .set_pauseparam = be_set_pauseparam, 1170 .get_strings = be_get_stat_strings, 1171 .set_phys_id = be_set_phys_id, 1172 .set_dump = be_set_dump, 1173 .get_msglevel = be_get_msg_level, 1174 .set_msglevel = be_set_msg_level, 1175 .get_sset_count = be_get_sset_count, 1176 .get_ethtool_stats = be_get_ethtool_stats, 1177 .get_regs_len = be_get_reg_len, 1178 .get_regs = be_get_regs, 1179 .flash_device = be_do_flash, 1180 .self_test = be_self_test, 1181 .get_rxnfc = be_get_rxnfc, 1182 .set_rxnfc = be_set_rxnfc, 1183 .get_rxfh_indir_size = be_get_rxfh_indir_size, 1184 .get_rxfh_key_size = be_get_rxfh_key_size, 1185 .get_rxfh = be_get_rxfh, 1186 .set_rxfh = be_set_rxfh, 1187 .get_channels = be_get_channels, 1188 .set_channels = be_set_channels 1189 }; 1190