1 /**************************************************************************** 2 * Driver for Solarflare network controllers and boards 3 * Copyright 2005-2006 Fen Systems Ltd. 4 * Copyright 2006-2013 Solarflare Communications Inc. 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 as published 8 * by the Free Software Foundation, incorporated herein by reference. 9 */ 10 11 #include <linux/netdevice.h> 12 #include <linux/ethtool.h> 13 #include <linux/rtnetlink.h> 14 #include <linux/in.h> 15 #include "net_driver.h" 16 #include "workarounds.h" 17 #include "selftest.h" 18 #include "efx.h" 19 #include "filter.h" 20 #include "nic.h" 21 22 struct efx_sw_stat_desc { 23 const char *name; 24 enum { 25 EFX_ETHTOOL_STAT_SOURCE_nic, 26 EFX_ETHTOOL_STAT_SOURCE_channel, 27 EFX_ETHTOOL_STAT_SOURCE_tx_queue 28 } source; 29 unsigned offset; 30 u64(*get_stat) (void *field); /* Reader function */ 31 }; 32 33 /* Initialiser for a struct efx_sw_stat_desc with type-checking */ 34 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \ 35 get_stat_function) { \ 36 .name = #stat_name, \ 37 .source = EFX_ETHTOOL_STAT_SOURCE_##source_name, \ 38 .offset = ((((field_type *) 0) == \ 39 &((struct efx_##source_name *)0)->field) ? \ 40 offsetof(struct efx_##source_name, field) : \ 41 offsetof(struct efx_##source_name, field)), \ 42 .get_stat = get_stat_function, \ 43 } 44 45 static u64 efx_get_uint_stat(void *field) 46 { 47 return *(unsigned int *)field; 48 } 49 50 static u64 efx_get_atomic_stat(void *field) 51 { 52 return atomic_read((atomic_t *) field); 53 } 54 55 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field) \ 56 EFX_ETHTOOL_STAT(field, nic, field, \ 57 atomic_t, efx_get_atomic_stat) 58 59 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field) \ 60 EFX_ETHTOOL_STAT(field, channel, n_##field, \ 61 unsigned int, efx_get_uint_stat) 62 63 #define EFX_ETHTOOL_UINT_TXQ_STAT(field) \ 64 EFX_ETHTOOL_STAT(tx_##field, tx_queue, field, \ 65 unsigned int, efx_get_uint_stat) 66 67 static const struct efx_sw_stat_desc efx_sw_stat_desc[] = { 68 EFX_ETHTOOL_UINT_TXQ_STAT(merge_events), 69 EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts), 70 EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers), 71 EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets), 72 EFX_ETHTOOL_UINT_TXQ_STAT(pushes), 73 EFX_ETHTOOL_UINT_TXQ_STAT(pio_packets), 74 EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset), 75 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc), 76 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err), 77 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err), 78 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch), 79 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc), 80 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_nodesc_trunc), 81 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_events), 82 EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_packets), 83 }; 84 85 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc) 86 87 #define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB 88 89 /************************************************************************** 90 * 91 * Ethtool operations 92 * 93 ************************************************************************** 94 */ 95 96 /* Identify device by flashing LEDs */ 97 static int efx_ethtool_phys_id(struct net_device *net_dev, 98 enum ethtool_phys_id_state state) 99 { 100 struct efx_nic *efx = netdev_priv(net_dev); 101 enum efx_led_mode mode = EFX_LED_DEFAULT; 102 103 switch (state) { 104 case ETHTOOL_ID_ON: 105 mode = EFX_LED_ON; 106 break; 107 case ETHTOOL_ID_OFF: 108 mode = EFX_LED_OFF; 109 break; 110 case ETHTOOL_ID_INACTIVE: 111 mode = EFX_LED_DEFAULT; 112 break; 113 case ETHTOOL_ID_ACTIVE: 114 return 1; /* cycle on/off once per second */ 115 } 116 117 efx->type->set_id_led(efx, mode); 118 return 0; 119 } 120 121 /* This must be called with rtnl_lock held. */ 122 static int efx_ethtool_get_settings(struct net_device *net_dev, 123 struct ethtool_cmd *ecmd) 124 { 125 struct efx_nic *efx = netdev_priv(net_dev); 126 struct efx_link_state *link_state = &efx->link_state; 127 128 mutex_lock(&efx->mac_lock); 129 efx->phy_op->get_settings(efx, ecmd); 130 mutex_unlock(&efx->mac_lock); 131 132 /* Both MACs support pause frames (bidirectional and respond-only) */ 133 ecmd->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause; 134 135 if (LOOPBACK_INTERNAL(efx)) { 136 ethtool_cmd_speed_set(ecmd, link_state->speed); 137 ecmd->duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF; 138 } 139 140 return 0; 141 } 142 143 /* This must be called with rtnl_lock held. */ 144 static int efx_ethtool_set_settings(struct net_device *net_dev, 145 struct ethtool_cmd *ecmd) 146 { 147 struct efx_nic *efx = netdev_priv(net_dev); 148 int rc; 149 150 /* GMAC does not support 1000Mbps HD */ 151 if ((ethtool_cmd_speed(ecmd) == SPEED_1000) && 152 (ecmd->duplex != DUPLEX_FULL)) { 153 netif_dbg(efx, drv, efx->net_dev, 154 "rejecting unsupported 1000Mbps HD setting\n"); 155 return -EINVAL; 156 } 157 158 mutex_lock(&efx->mac_lock); 159 rc = efx->phy_op->set_settings(efx, ecmd); 160 mutex_unlock(&efx->mac_lock); 161 return rc; 162 } 163 164 static void efx_ethtool_get_drvinfo(struct net_device *net_dev, 165 struct ethtool_drvinfo *info) 166 { 167 struct efx_nic *efx = netdev_priv(net_dev); 168 169 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 170 strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version)); 171 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) 172 efx_mcdi_print_fwver(efx, info->fw_version, 173 sizeof(info->fw_version)); 174 strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info)); 175 } 176 177 static int efx_ethtool_get_regs_len(struct net_device *net_dev) 178 { 179 return efx_nic_get_regs_len(netdev_priv(net_dev)); 180 } 181 182 static void efx_ethtool_get_regs(struct net_device *net_dev, 183 struct ethtool_regs *regs, void *buf) 184 { 185 struct efx_nic *efx = netdev_priv(net_dev); 186 187 regs->version = efx->type->revision; 188 efx_nic_get_regs(efx, buf); 189 } 190 191 static u32 efx_ethtool_get_msglevel(struct net_device *net_dev) 192 { 193 struct efx_nic *efx = netdev_priv(net_dev); 194 return efx->msg_enable; 195 } 196 197 static void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable) 198 { 199 struct efx_nic *efx = netdev_priv(net_dev); 200 efx->msg_enable = msg_enable; 201 } 202 203 /** 204 * efx_fill_test - fill in an individual self-test entry 205 * @test_index: Index of the test 206 * @strings: Ethtool strings, or %NULL 207 * @data: Ethtool test results, or %NULL 208 * @test: Pointer to test result (used only if data != %NULL) 209 * @unit_format: Unit name format (e.g. "chan\%d") 210 * @unit_id: Unit id (e.g. 0 for "chan0") 211 * @test_format: Test name format (e.g. "loopback.\%s.tx.sent") 212 * @test_id: Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent") 213 * 214 * Fill in an individual self-test entry. 215 */ 216 static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data, 217 int *test, const char *unit_format, int unit_id, 218 const char *test_format, const char *test_id) 219 { 220 char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN]; 221 222 /* Fill data value, if applicable */ 223 if (data) 224 data[test_index] = *test; 225 226 /* Fill string, if applicable */ 227 if (strings) { 228 if (strchr(unit_format, '%')) 229 snprintf(unit_str, sizeof(unit_str), 230 unit_format, unit_id); 231 else 232 strcpy(unit_str, unit_format); 233 snprintf(test_str, sizeof(test_str), test_format, test_id); 234 snprintf(strings + test_index * ETH_GSTRING_LEN, 235 ETH_GSTRING_LEN, 236 "%-6s %-24s", unit_str, test_str); 237 } 238 } 239 240 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel 241 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue 242 #define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue 243 #define EFX_LOOPBACK_NAME(_mode, _counter) \ 244 "loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode) 245 246 /** 247 * efx_fill_loopback_test - fill in a block of loopback self-test entries 248 * @efx: Efx NIC 249 * @lb_tests: Efx loopback self-test results structure 250 * @mode: Loopback test mode 251 * @test_index: Starting index of the test 252 * @strings: Ethtool strings, or %NULL 253 * @data: Ethtool test results, or %NULL 254 */ 255 static int efx_fill_loopback_test(struct efx_nic *efx, 256 struct efx_loopback_self_tests *lb_tests, 257 enum efx_loopback_mode mode, 258 unsigned int test_index, 259 u8 *strings, u64 *data) 260 { 261 struct efx_channel *channel = 262 efx_get_channel(efx, efx->tx_channel_offset); 263 struct efx_tx_queue *tx_queue; 264 265 efx_for_each_channel_tx_queue(tx_queue, channel) { 266 efx_fill_test(test_index++, strings, data, 267 &lb_tests->tx_sent[tx_queue->queue], 268 EFX_TX_QUEUE_NAME(tx_queue), 269 EFX_LOOPBACK_NAME(mode, "tx_sent")); 270 efx_fill_test(test_index++, strings, data, 271 &lb_tests->tx_done[tx_queue->queue], 272 EFX_TX_QUEUE_NAME(tx_queue), 273 EFX_LOOPBACK_NAME(mode, "tx_done")); 274 } 275 efx_fill_test(test_index++, strings, data, 276 &lb_tests->rx_good, 277 "rx", 0, 278 EFX_LOOPBACK_NAME(mode, "rx_good")); 279 efx_fill_test(test_index++, strings, data, 280 &lb_tests->rx_bad, 281 "rx", 0, 282 EFX_LOOPBACK_NAME(mode, "rx_bad")); 283 284 return test_index; 285 } 286 287 /** 288 * efx_ethtool_fill_self_tests - get self-test details 289 * @efx: Efx NIC 290 * @tests: Efx self-test results structure, or %NULL 291 * @strings: Ethtool strings, or %NULL 292 * @data: Ethtool test results, or %NULL 293 */ 294 static int efx_ethtool_fill_self_tests(struct efx_nic *efx, 295 struct efx_self_tests *tests, 296 u8 *strings, u64 *data) 297 { 298 struct efx_channel *channel; 299 unsigned int n = 0, i; 300 enum efx_loopback_mode mode; 301 302 efx_fill_test(n++, strings, data, &tests->phy_alive, 303 "phy", 0, "alive", NULL); 304 efx_fill_test(n++, strings, data, &tests->nvram, 305 "core", 0, "nvram", NULL); 306 efx_fill_test(n++, strings, data, &tests->interrupt, 307 "core", 0, "interrupt", NULL); 308 309 /* Event queues */ 310 efx_for_each_channel(channel, efx) { 311 efx_fill_test(n++, strings, data, 312 &tests->eventq_dma[channel->channel], 313 EFX_CHANNEL_NAME(channel), 314 "eventq.dma", NULL); 315 efx_fill_test(n++, strings, data, 316 &tests->eventq_int[channel->channel], 317 EFX_CHANNEL_NAME(channel), 318 "eventq.int", NULL); 319 } 320 321 efx_fill_test(n++, strings, data, &tests->registers, 322 "core", 0, "registers", NULL); 323 324 if (efx->phy_op->run_tests != NULL) { 325 EFX_BUG_ON_PARANOID(efx->phy_op->test_name == NULL); 326 327 for (i = 0; true; ++i) { 328 const char *name; 329 330 EFX_BUG_ON_PARANOID(i >= EFX_MAX_PHY_TESTS); 331 name = efx->phy_op->test_name(efx, i); 332 if (name == NULL) 333 break; 334 335 efx_fill_test(n++, strings, data, &tests->phy_ext[i], 336 "phy", 0, name, NULL); 337 } 338 } 339 340 /* Loopback tests */ 341 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) { 342 if (!(efx->loopback_modes & (1 << mode))) 343 continue; 344 n = efx_fill_loopback_test(efx, 345 &tests->loopback[mode], mode, n, 346 strings, data); 347 } 348 349 return n; 350 } 351 352 static int efx_ethtool_get_sset_count(struct net_device *net_dev, 353 int string_set) 354 { 355 struct efx_nic *efx = netdev_priv(net_dev); 356 357 switch (string_set) { 358 case ETH_SS_STATS: 359 return efx->type->describe_stats(efx, NULL) + 360 EFX_ETHTOOL_SW_STAT_COUNT; 361 case ETH_SS_TEST: 362 return efx_ethtool_fill_self_tests(efx, NULL, NULL, NULL); 363 default: 364 return -EINVAL; 365 } 366 } 367 368 static void efx_ethtool_get_strings(struct net_device *net_dev, 369 u32 string_set, u8 *strings) 370 { 371 struct efx_nic *efx = netdev_priv(net_dev); 372 int i; 373 374 switch (string_set) { 375 case ETH_SS_STATS: 376 strings += (efx->type->describe_stats(efx, strings) * 377 ETH_GSTRING_LEN); 378 for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) 379 strlcpy(strings + i * ETH_GSTRING_LEN, 380 efx_sw_stat_desc[i].name, ETH_GSTRING_LEN); 381 break; 382 case ETH_SS_TEST: 383 efx_ethtool_fill_self_tests(efx, NULL, strings, NULL); 384 break; 385 default: 386 /* No other string sets */ 387 break; 388 } 389 } 390 391 static void efx_ethtool_get_stats(struct net_device *net_dev, 392 struct ethtool_stats *stats, 393 u64 *data) 394 { 395 struct efx_nic *efx = netdev_priv(net_dev); 396 const struct efx_sw_stat_desc *stat; 397 struct efx_channel *channel; 398 struct efx_tx_queue *tx_queue; 399 int i; 400 401 spin_lock_bh(&efx->stats_lock); 402 403 /* Get NIC statistics */ 404 data += efx->type->update_stats(efx, data, NULL); 405 406 /* Get software statistics */ 407 for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) { 408 stat = &efx_sw_stat_desc[i]; 409 switch (stat->source) { 410 case EFX_ETHTOOL_STAT_SOURCE_nic: 411 data[i] = stat->get_stat((void *)efx + stat->offset); 412 break; 413 case EFX_ETHTOOL_STAT_SOURCE_channel: 414 data[i] = 0; 415 efx_for_each_channel(channel, efx) 416 data[i] += stat->get_stat((void *)channel + 417 stat->offset); 418 break; 419 case EFX_ETHTOOL_STAT_SOURCE_tx_queue: 420 data[i] = 0; 421 efx_for_each_channel(channel, efx) { 422 efx_for_each_channel_tx_queue(tx_queue, channel) 423 data[i] += 424 stat->get_stat((void *)tx_queue 425 + stat->offset); 426 } 427 break; 428 } 429 } 430 431 spin_unlock_bh(&efx->stats_lock); 432 } 433 434 static void efx_ethtool_self_test(struct net_device *net_dev, 435 struct ethtool_test *test, u64 *data) 436 { 437 struct efx_nic *efx = netdev_priv(net_dev); 438 struct efx_self_tests *efx_tests; 439 int already_up; 440 int rc = -ENOMEM; 441 442 efx_tests = kzalloc(sizeof(*efx_tests), GFP_KERNEL); 443 if (!efx_tests) 444 goto fail; 445 446 if (efx->state != STATE_READY) { 447 rc = -EIO; 448 goto fail1; 449 } 450 451 netif_info(efx, drv, efx->net_dev, "starting %sline testing\n", 452 (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on"); 453 454 /* We need rx buffers and interrupts. */ 455 already_up = (efx->net_dev->flags & IFF_UP); 456 if (!already_up) { 457 rc = dev_open(efx->net_dev); 458 if (rc) { 459 netif_err(efx, drv, efx->net_dev, 460 "failed opening device.\n"); 461 goto fail1; 462 } 463 } 464 465 rc = efx_selftest(efx, efx_tests, test->flags); 466 467 if (!already_up) 468 dev_close(efx->net_dev); 469 470 netif_info(efx, drv, efx->net_dev, "%s %sline self-tests\n", 471 rc == 0 ? "passed" : "failed", 472 (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on"); 473 474 fail1: 475 /* Fill ethtool results structures */ 476 efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data); 477 kfree(efx_tests); 478 fail: 479 if (rc) 480 test->flags |= ETH_TEST_FL_FAILED; 481 } 482 483 /* Restart autonegotiation */ 484 static int efx_ethtool_nway_reset(struct net_device *net_dev) 485 { 486 struct efx_nic *efx = netdev_priv(net_dev); 487 488 return mdio45_nway_restart(&efx->mdio); 489 } 490 491 /* 492 * Each channel has a single IRQ and moderation timer, started by any 493 * completion (or other event). Unless the module parameter 494 * separate_tx_channels is set, IRQs and moderation are therefore 495 * shared between RX and TX completions. In this case, when RX IRQ 496 * moderation is explicitly changed then TX IRQ moderation is 497 * automatically changed too, but otherwise we fail if the two values 498 * are requested to be different. 499 * 500 * The hardware does not support a limit on the number of completions 501 * before an IRQ, so we do not use the max_frames fields. We should 502 * report and require that max_frames == (usecs != 0), but this would 503 * invalidate existing user documentation. 504 * 505 * The hardware does not have distinct settings for interrupt 506 * moderation while the previous IRQ is being handled, so we should 507 * not use the 'irq' fields. However, an earlier developer 508 * misunderstood the meaning of the 'irq' fields and the driver did 509 * not support the standard fields. To avoid invalidating existing 510 * user documentation, we report and accept changes through either the 511 * standard or 'irq' fields. If both are changed at the same time, we 512 * prefer the standard field. 513 * 514 * We implement adaptive IRQ moderation, but use a different algorithm 515 * from that assumed in the definition of struct ethtool_coalesce. 516 * Therefore we do not use any of the adaptive moderation parameters 517 * in it. 518 */ 519 520 static int efx_ethtool_get_coalesce(struct net_device *net_dev, 521 struct ethtool_coalesce *coalesce) 522 { 523 struct efx_nic *efx = netdev_priv(net_dev); 524 unsigned int tx_usecs, rx_usecs; 525 bool rx_adaptive; 526 527 efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &rx_adaptive); 528 529 coalesce->tx_coalesce_usecs = tx_usecs; 530 coalesce->tx_coalesce_usecs_irq = tx_usecs; 531 coalesce->rx_coalesce_usecs = rx_usecs; 532 coalesce->rx_coalesce_usecs_irq = rx_usecs; 533 coalesce->use_adaptive_rx_coalesce = rx_adaptive; 534 535 return 0; 536 } 537 538 static int efx_ethtool_set_coalesce(struct net_device *net_dev, 539 struct ethtool_coalesce *coalesce) 540 { 541 struct efx_nic *efx = netdev_priv(net_dev); 542 struct efx_channel *channel; 543 unsigned int tx_usecs, rx_usecs; 544 bool adaptive, rx_may_override_tx; 545 int rc; 546 547 if (coalesce->use_adaptive_tx_coalesce) 548 return -EINVAL; 549 550 efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &adaptive); 551 552 if (coalesce->rx_coalesce_usecs != rx_usecs) 553 rx_usecs = coalesce->rx_coalesce_usecs; 554 else 555 rx_usecs = coalesce->rx_coalesce_usecs_irq; 556 557 adaptive = coalesce->use_adaptive_rx_coalesce; 558 559 /* If channels are shared, TX IRQ moderation can be quietly 560 * overridden unless it is changed from its old value. 561 */ 562 rx_may_override_tx = (coalesce->tx_coalesce_usecs == tx_usecs && 563 coalesce->tx_coalesce_usecs_irq == tx_usecs); 564 if (coalesce->tx_coalesce_usecs != tx_usecs) 565 tx_usecs = coalesce->tx_coalesce_usecs; 566 else 567 tx_usecs = coalesce->tx_coalesce_usecs_irq; 568 569 rc = efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive, 570 rx_may_override_tx); 571 if (rc != 0) 572 return rc; 573 574 efx_for_each_channel(channel, efx) 575 efx->type->push_irq_moderation(channel); 576 577 return 0; 578 } 579 580 static void efx_ethtool_get_ringparam(struct net_device *net_dev, 581 struct ethtool_ringparam *ring) 582 { 583 struct efx_nic *efx = netdev_priv(net_dev); 584 585 ring->rx_max_pending = EFX_MAX_DMAQ_SIZE; 586 ring->tx_max_pending = EFX_MAX_DMAQ_SIZE; 587 ring->rx_pending = efx->rxq_entries; 588 ring->tx_pending = efx->txq_entries; 589 } 590 591 static int efx_ethtool_set_ringparam(struct net_device *net_dev, 592 struct ethtool_ringparam *ring) 593 { 594 struct efx_nic *efx = netdev_priv(net_dev); 595 u32 txq_entries; 596 597 if (ring->rx_mini_pending || ring->rx_jumbo_pending || 598 ring->rx_pending > EFX_MAX_DMAQ_SIZE || 599 ring->tx_pending > EFX_MAX_DMAQ_SIZE) 600 return -EINVAL; 601 602 if (ring->rx_pending < EFX_RXQ_MIN_ENT) { 603 netif_err(efx, drv, efx->net_dev, 604 "RX queues cannot be smaller than %u\n", 605 EFX_RXQ_MIN_ENT); 606 return -EINVAL; 607 } 608 609 txq_entries = max(ring->tx_pending, EFX_TXQ_MIN_ENT(efx)); 610 if (txq_entries != ring->tx_pending) 611 netif_warn(efx, drv, efx->net_dev, 612 "increasing TX queue size to minimum of %u\n", 613 txq_entries); 614 615 return efx_realloc_channels(efx, ring->rx_pending, txq_entries); 616 } 617 618 static int efx_ethtool_set_pauseparam(struct net_device *net_dev, 619 struct ethtool_pauseparam *pause) 620 { 621 struct efx_nic *efx = netdev_priv(net_dev); 622 u8 wanted_fc, old_fc; 623 u32 old_adv; 624 int rc = 0; 625 626 mutex_lock(&efx->mac_lock); 627 628 wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) | 629 (pause->tx_pause ? EFX_FC_TX : 0) | 630 (pause->autoneg ? EFX_FC_AUTO : 0)); 631 632 if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) { 633 netif_dbg(efx, drv, efx->net_dev, 634 "Flow control unsupported: tx ON rx OFF\n"); 635 rc = -EINVAL; 636 goto out; 637 } 638 639 if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising) { 640 netif_dbg(efx, drv, efx->net_dev, 641 "Autonegotiation is disabled\n"); 642 rc = -EINVAL; 643 goto out; 644 } 645 646 /* Hook for Falcon bug 11482 workaround */ 647 if (efx->type->prepare_enable_fc_tx && 648 (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX)) 649 efx->type->prepare_enable_fc_tx(efx); 650 651 old_adv = efx->link_advertising; 652 old_fc = efx->wanted_fc; 653 efx_link_set_wanted_fc(efx, wanted_fc); 654 if (efx->link_advertising != old_adv || 655 (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) { 656 rc = efx->phy_op->reconfigure(efx); 657 if (rc) { 658 netif_err(efx, drv, efx->net_dev, 659 "Unable to advertise requested flow " 660 "control setting\n"); 661 goto out; 662 } 663 } 664 665 /* Reconfigure the MAC. The PHY *may* generate a link state change event 666 * if the user just changed the advertised capabilities, but there's no 667 * harm doing this twice */ 668 efx->type->reconfigure_mac(efx); 669 670 out: 671 mutex_unlock(&efx->mac_lock); 672 673 return rc; 674 } 675 676 static void efx_ethtool_get_pauseparam(struct net_device *net_dev, 677 struct ethtool_pauseparam *pause) 678 { 679 struct efx_nic *efx = netdev_priv(net_dev); 680 681 pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX); 682 pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX); 683 pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO); 684 } 685 686 687 static void efx_ethtool_get_wol(struct net_device *net_dev, 688 struct ethtool_wolinfo *wol) 689 { 690 struct efx_nic *efx = netdev_priv(net_dev); 691 return efx->type->get_wol(efx, wol); 692 } 693 694 695 static int efx_ethtool_set_wol(struct net_device *net_dev, 696 struct ethtool_wolinfo *wol) 697 { 698 struct efx_nic *efx = netdev_priv(net_dev); 699 return efx->type->set_wol(efx, wol->wolopts); 700 } 701 702 static int efx_ethtool_reset(struct net_device *net_dev, u32 *flags) 703 { 704 struct efx_nic *efx = netdev_priv(net_dev); 705 int rc; 706 707 rc = efx->type->map_reset_flags(flags); 708 if (rc < 0) 709 return rc; 710 711 return efx_reset(efx, rc); 712 } 713 714 /* MAC address mask including only I/G bit */ 715 static const u8 mac_addr_ig_mask[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 }; 716 717 #define IP4_ADDR_FULL_MASK ((__force __be32)~0) 718 #define PORT_FULL_MASK ((__force __be16)~0) 719 #define ETHER_TYPE_FULL_MASK ((__force __be16)~0) 720 721 static int efx_ethtool_get_class_rule(struct efx_nic *efx, 722 struct ethtool_rx_flow_spec *rule) 723 { 724 struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec; 725 struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec; 726 struct ethhdr *mac_entry = &rule->h_u.ether_spec; 727 struct ethhdr *mac_mask = &rule->m_u.ether_spec; 728 struct efx_filter_spec spec; 729 int rc; 730 731 rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL, 732 rule->location, &spec); 733 if (rc) 734 return rc; 735 736 if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP) 737 rule->ring_cookie = RX_CLS_FLOW_DISC; 738 else 739 rule->ring_cookie = spec.dmaq_id; 740 741 if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) && 742 spec.ether_type == htons(ETH_P_IP) && 743 (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) && 744 (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) && 745 !(spec.match_flags & 746 ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID | 747 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST | 748 EFX_FILTER_MATCH_IP_PROTO | 749 EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) { 750 rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ? 751 TCP_V4_FLOW : UDP_V4_FLOW); 752 if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) { 753 ip_entry->ip4dst = spec.loc_host[0]; 754 ip_mask->ip4dst = IP4_ADDR_FULL_MASK; 755 } 756 if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) { 757 ip_entry->ip4src = spec.rem_host[0]; 758 ip_mask->ip4src = IP4_ADDR_FULL_MASK; 759 } 760 if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) { 761 ip_entry->pdst = spec.loc_port; 762 ip_mask->pdst = PORT_FULL_MASK; 763 } 764 if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) { 765 ip_entry->psrc = spec.rem_port; 766 ip_mask->psrc = PORT_FULL_MASK; 767 } 768 } else if (!(spec.match_flags & 769 ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG | 770 EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE | 771 EFX_FILTER_MATCH_OUTER_VID))) { 772 rule->flow_type = ETHER_FLOW; 773 if (spec.match_flags & 774 (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) { 775 memcpy(mac_entry->h_dest, spec.loc_mac, ETH_ALEN); 776 if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC) 777 memset(mac_mask->h_dest, ~0, ETH_ALEN); 778 else 779 memcpy(mac_mask->h_dest, mac_addr_ig_mask, 780 ETH_ALEN); 781 } 782 if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) { 783 memcpy(mac_entry->h_source, spec.rem_mac, ETH_ALEN); 784 memset(mac_mask->h_source, ~0, ETH_ALEN); 785 } 786 if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) { 787 mac_entry->h_proto = spec.ether_type; 788 mac_mask->h_proto = ETHER_TYPE_FULL_MASK; 789 } 790 } else { 791 /* The above should handle all filters that we insert */ 792 WARN_ON(1); 793 return -EINVAL; 794 } 795 796 if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) { 797 rule->flow_type |= FLOW_EXT; 798 rule->h_ext.vlan_tci = spec.outer_vid; 799 rule->m_ext.vlan_tci = htons(0xfff); 800 } 801 802 return rc; 803 } 804 805 static int 806 efx_ethtool_get_rxnfc(struct net_device *net_dev, 807 struct ethtool_rxnfc *info, u32 *rule_locs) 808 { 809 struct efx_nic *efx = netdev_priv(net_dev); 810 811 switch (info->cmd) { 812 case ETHTOOL_GRXRINGS: 813 info->data = efx->n_rx_channels; 814 return 0; 815 816 case ETHTOOL_GRXFH: { 817 unsigned min_revision = 0; 818 819 info->data = 0; 820 switch (info->flow_type) { 821 case TCP_V4_FLOW: 822 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 823 /* fall through */ 824 case UDP_V4_FLOW: 825 case SCTP_V4_FLOW: 826 case AH_ESP_V4_FLOW: 827 case IPV4_FLOW: 828 info->data |= RXH_IP_SRC | RXH_IP_DST; 829 min_revision = EFX_REV_FALCON_B0; 830 break; 831 case TCP_V6_FLOW: 832 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 833 /* fall through */ 834 case UDP_V6_FLOW: 835 case SCTP_V6_FLOW: 836 case AH_ESP_V6_FLOW: 837 case IPV6_FLOW: 838 info->data |= RXH_IP_SRC | RXH_IP_DST; 839 min_revision = EFX_REV_SIENA_A0; 840 break; 841 default: 842 break; 843 } 844 if (efx_nic_rev(efx) < min_revision) 845 info->data = 0; 846 return 0; 847 } 848 849 case ETHTOOL_GRXCLSRLCNT: 850 info->data = efx_filter_get_rx_id_limit(efx); 851 if (info->data == 0) 852 return -EOPNOTSUPP; 853 info->data |= RX_CLS_LOC_SPECIAL; 854 info->rule_cnt = 855 efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL); 856 return 0; 857 858 case ETHTOOL_GRXCLSRULE: 859 if (efx_filter_get_rx_id_limit(efx) == 0) 860 return -EOPNOTSUPP; 861 return efx_ethtool_get_class_rule(efx, &info->fs); 862 863 case ETHTOOL_GRXCLSRLALL: { 864 s32 rc; 865 info->data = efx_filter_get_rx_id_limit(efx); 866 if (info->data == 0) 867 return -EOPNOTSUPP; 868 rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL, 869 rule_locs, info->rule_cnt); 870 if (rc < 0) 871 return rc; 872 info->rule_cnt = rc; 873 return 0; 874 } 875 876 default: 877 return -EOPNOTSUPP; 878 } 879 } 880 881 static int efx_ethtool_set_class_rule(struct efx_nic *efx, 882 struct ethtool_rx_flow_spec *rule) 883 { 884 struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec; 885 struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec; 886 struct ethhdr *mac_entry = &rule->h_u.ether_spec; 887 struct ethhdr *mac_mask = &rule->m_u.ether_spec; 888 struct efx_filter_spec spec; 889 int rc; 890 891 /* Check that user wants us to choose the location */ 892 if (rule->location != RX_CLS_LOC_ANY) 893 return -EINVAL; 894 895 /* Range-check ring_cookie */ 896 if (rule->ring_cookie >= efx->n_rx_channels && 897 rule->ring_cookie != RX_CLS_FLOW_DISC) 898 return -EINVAL; 899 900 /* Check for unsupported extensions */ 901 if ((rule->flow_type & FLOW_EXT) && 902 (rule->m_ext.vlan_etype || rule->m_ext.data[0] || 903 rule->m_ext.data[1])) 904 return -EINVAL; 905 906 efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL, 907 efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0, 908 (rule->ring_cookie == RX_CLS_FLOW_DISC) ? 909 EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie); 910 911 switch (rule->flow_type & ~FLOW_EXT) { 912 case TCP_V4_FLOW: 913 case UDP_V4_FLOW: 914 spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE | 915 EFX_FILTER_MATCH_IP_PROTO); 916 spec.ether_type = htons(ETH_P_IP); 917 spec.ip_proto = ((rule->flow_type & ~FLOW_EXT) == TCP_V4_FLOW ? 918 IPPROTO_TCP : IPPROTO_UDP); 919 if (ip_mask->ip4dst) { 920 if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK) 921 return -EINVAL; 922 spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST; 923 spec.loc_host[0] = ip_entry->ip4dst; 924 } 925 if (ip_mask->ip4src) { 926 if (ip_mask->ip4src != IP4_ADDR_FULL_MASK) 927 return -EINVAL; 928 spec.match_flags |= EFX_FILTER_MATCH_REM_HOST; 929 spec.rem_host[0] = ip_entry->ip4src; 930 } 931 if (ip_mask->pdst) { 932 if (ip_mask->pdst != PORT_FULL_MASK) 933 return -EINVAL; 934 spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT; 935 spec.loc_port = ip_entry->pdst; 936 } 937 if (ip_mask->psrc) { 938 if (ip_mask->psrc != PORT_FULL_MASK) 939 return -EINVAL; 940 spec.match_flags |= EFX_FILTER_MATCH_REM_PORT; 941 spec.rem_port = ip_entry->psrc; 942 } 943 if (ip_mask->tos) 944 return -EINVAL; 945 break; 946 947 case ETHER_FLOW: 948 if (!is_zero_ether_addr(mac_mask->h_dest)) { 949 if (ether_addr_equal(mac_mask->h_dest, 950 mac_addr_ig_mask)) 951 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG; 952 else if (is_broadcast_ether_addr(mac_mask->h_dest)) 953 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC; 954 else 955 return -EINVAL; 956 memcpy(spec.loc_mac, mac_entry->h_dest, ETH_ALEN); 957 } 958 if (!is_zero_ether_addr(mac_mask->h_source)) { 959 if (!is_broadcast_ether_addr(mac_mask->h_source)) 960 return -EINVAL; 961 spec.match_flags |= EFX_FILTER_MATCH_REM_MAC; 962 memcpy(spec.rem_mac, mac_entry->h_source, ETH_ALEN); 963 } 964 if (mac_mask->h_proto) { 965 if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK) 966 return -EINVAL; 967 spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE; 968 spec.ether_type = mac_entry->h_proto; 969 } 970 break; 971 972 default: 973 return -EINVAL; 974 } 975 976 if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) { 977 if (rule->m_ext.vlan_tci != htons(0xfff)) 978 return -EINVAL; 979 spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID; 980 spec.outer_vid = rule->h_ext.vlan_tci; 981 } 982 983 rc = efx_filter_insert_filter(efx, &spec, true); 984 if (rc < 0) 985 return rc; 986 987 rule->location = rc; 988 return 0; 989 } 990 991 static int efx_ethtool_set_rxnfc(struct net_device *net_dev, 992 struct ethtool_rxnfc *info) 993 { 994 struct efx_nic *efx = netdev_priv(net_dev); 995 996 if (efx_filter_get_rx_id_limit(efx) == 0) 997 return -EOPNOTSUPP; 998 999 switch (info->cmd) { 1000 case ETHTOOL_SRXCLSRLINS: 1001 return efx_ethtool_set_class_rule(efx, &info->fs); 1002 1003 case ETHTOOL_SRXCLSRLDEL: 1004 return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL, 1005 info->fs.location); 1006 1007 default: 1008 return -EOPNOTSUPP; 1009 } 1010 } 1011 1012 static u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev) 1013 { 1014 struct efx_nic *efx = netdev_priv(net_dev); 1015 1016 return ((efx_nic_rev(efx) < EFX_REV_FALCON_B0 || 1017 efx->n_rx_channels == 1) ? 1018 0 : ARRAY_SIZE(efx->rx_indir_table)); 1019 } 1020 1021 static int efx_ethtool_get_rxfh_indir(struct net_device *net_dev, u32 *indir) 1022 { 1023 struct efx_nic *efx = netdev_priv(net_dev); 1024 1025 memcpy(indir, efx->rx_indir_table, sizeof(efx->rx_indir_table)); 1026 return 0; 1027 } 1028 1029 static int efx_ethtool_set_rxfh_indir(struct net_device *net_dev, 1030 const u32 *indir) 1031 { 1032 struct efx_nic *efx = netdev_priv(net_dev); 1033 1034 memcpy(efx->rx_indir_table, indir, sizeof(efx->rx_indir_table)); 1035 efx_nic_push_rx_indir_table(efx); 1036 return 0; 1037 } 1038 1039 static int efx_ethtool_get_ts_info(struct net_device *net_dev, 1040 struct ethtool_ts_info *ts_info) 1041 { 1042 struct efx_nic *efx = netdev_priv(net_dev); 1043 1044 /* Software capabilities */ 1045 ts_info->so_timestamping = (SOF_TIMESTAMPING_RX_SOFTWARE | 1046 SOF_TIMESTAMPING_SOFTWARE); 1047 ts_info->phc_index = -1; 1048 1049 efx_ptp_get_ts_info(efx, ts_info); 1050 return 0; 1051 } 1052 1053 static int efx_ethtool_get_module_eeprom(struct net_device *net_dev, 1054 struct ethtool_eeprom *ee, 1055 u8 *data) 1056 { 1057 struct efx_nic *efx = netdev_priv(net_dev); 1058 int ret; 1059 1060 if (!efx->phy_op || !efx->phy_op->get_module_eeprom) 1061 return -EOPNOTSUPP; 1062 1063 mutex_lock(&efx->mac_lock); 1064 ret = efx->phy_op->get_module_eeprom(efx, ee, data); 1065 mutex_unlock(&efx->mac_lock); 1066 1067 return ret; 1068 } 1069 1070 static int efx_ethtool_get_module_info(struct net_device *net_dev, 1071 struct ethtool_modinfo *modinfo) 1072 { 1073 struct efx_nic *efx = netdev_priv(net_dev); 1074 int ret; 1075 1076 if (!efx->phy_op || !efx->phy_op->get_module_info) 1077 return -EOPNOTSUPP; 1078 1079 mutex_lock(&efx->mac_lock); 1080 ret = efx->phy_op->get_module_info(efx, modinfo); 1081 mutex_unlock(&efx->mac_lock); 1082 1083 return ret; 1084 } 1085 1086 const struct ethtool_ops efx_ethtool_ops = { 1087 .get_settings = efx_ethtool_get_settings, 1088 .set_settings = efx_ethtool_set_settings, 1089 .get_drvinfo = efx_ethtool_get_drvinfo, 1090 .get_regs_len = efx_ethtool_get_regs_len, 1091 .get_regs = efx_ethtool_get_regs, 1092 .get_msglevel = efx_ethtool_get_msglevel, 1093 .set_msglevel = efx_ethtool_set_msglevel, 1094 .nway_reset = efx_ethtool_nway_reset, 1095 .get_link = ethtool_op_get_link, 1096 .get_coalesce = efx_ethtool_get_coalesce, 1097 .set_coalesce = efx_ethtool_set_coalesce, 1098 .get_ringparam = efx_ethtool_get_ringparam, 1099 .set_ringparam = efx_ethtool_set_ringparam, 1100 .get_pauseparam = efx_ethtool_get_pauseparam, 1101 .set_pauseparam = efx_ethtool_set_pauseparam, 1102 .get_sset_count = efx_ethtool_get_sset_count, 1103 .self_test = efx_ethtool_self_test, 1104 .get_strings = efx_ethtool_get_strings, 1105 .set_phys_id = efx_ethtool_phys_id, 1106 .get_ethtool_stats = efx_ethtool_get_stats, 1107 .get_wol = efx_ethtool_get_wol, 1108 .set_wol = efx_ethtool_set_wol, 1109 .reset = efx_ethtool_reset, 1110 .get_rxnfc = efx_ethtool_get_rxnfc, 1111 .set_rxnfc = efx_ethtool_set_rxnfc, 1112 .get_rxfh_indir_size = efx_ethtool_get_rxfh_indir_size, 1113 .get_rxfh_indir = efx_ethtool_get_rxfh_indir, 1114 .set_rxfh_indir = efx_ethtool_set_rxfh_indir, 1115 .get_ts_info = efx_ethtool_get_ts_info, 1116 .get_module_info = efx_ethtool_get_module_info, 1117 .get_module_eeprom = efx_ethtool_get_module_eeprom, 1118 }; 1119