1 // SPDX-License-Identifier: GPL-2.0 2 /* Intel(R) Ethernet Switch Host Interface Driver 3 * Copyright(c) 2013 - 2017 Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * The full GNU General Public License is included in this distribution in 15 * the file called "COPYING". 16 * 17 * Contact Information: 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 #include <linux/vmalloc.h> 23 24 #include "fm10k.h" 25 26 struct fm10k_stats { 27 char stat_string[ETH_GSTRING_LEN]; 28 int sizeof_stat; 29 int stat_offset; 30 }; 31 32 #define FM10K_NETDEV_STAT(_net_stat) { \ 33 .stat_string = #_net_stat, \ 34 .sizeof_stat = FIELD_SIZEOF(struct net_device_stats, _net_stat), \ 35 .stat_offset = offsetof(struct net_device_stats, _net_stat) \ 36 } 37 38 static const struct fm10k_stats fm10k_gstrings_net_stats[] = { 39 FM10K_NETDEV_STAT(tx_packets), 40 FM10K_NETDEV_STAT(tx_bytes), 41 FM10K_NETDEV_STAT(tx_errors), 42 FM10K_NETDEV_STAT(rx_packets), 43 FM10K_NETDEV_STAT(rx_bytes), 44 FM10K_NETDEV_STAT(rx_errors), 45 FM10K_NETDEV_STAT(rx_dropped), 46 47 /* detailed Rx errors */ 48 FM10K_NETDEV_STAT(rx_length_errors), 49 FM10K_NETDEV_STAT(rx_crc_errors), 50 FM10K_NETDEV_STAT(rx_fifo_errors), 51 }; 52 53 #define FM10K_NETDEV_STATS_LEN ARRAY_SIZE(fm10k_gstrings_net_stats) 54 55 #define FM10K_STAT(_name, _stat) { \ 56 .stat_string = _name, \ 57 .sizeof_stat = FIELD_SIZEOF(struct fm10k_intfc, _stat), \ 58 .stat_offset = offsetof(struct fm10k_intfc, _stat) \ 59 } 60 61 static const struct fm10k_stats fm10k_gstrings_global_stats[] = { 62 FM10K_STAT("tx_restart_queue", restart_queue), 63 FM10K_STAT("tx_busy", tx_busy), 64 FM10K_STAT("tx_csum_errors", tx_csum_errors), 65 FM10K_STAT("rx_alloc_failed", alloc_failed), 66 FM10K_STAT("rx_csum_errors", rx_csum_errors), 67 68 FM10K_STAT("tx_packets_nic", tx_packets_nic), 69 FM10K_STAT("tx_bytes_nic", tx_bytes_nic), 70 FM10K_STAT("rx_packets_nic", rx_packets_nic), 71 FM10K_STAT("rx_bytes_nic", rx_bytes_nic), 72 FM10K_STAT("rx_drops_nic", rx_drops_nic), 73 FM10K_STAT("rx_overrun_pf", rx_overrun_pf), 74 FM10K_STAT("rx_overrun_vf", rx_overrun_vf), 75 76 FM10K_STAT("swapi_status", hw.swapi.status), 77 FM10K_STAT("mac_rules_used", hw.swapi.mac.used), 78 FM10K_STAT("mac_rules_avail", hw.swapi.mac.avail), 79 80 FM10K_STAT("reset_while_pending", hw.mac.reset_while_pending), 81 82 FM10K_STAT("tx_hang_count", tx_timeout_count), 83 }; 84 85 static const struct fm10k_stats fm10k_gstrings_pf_stats[] = { 86 FM10K_STAT("timeout", stats.timeout.count), 87 FM10K_STAT("ur", stats.ur.count), 88 FM10K_STAT("ca", stats.ca.count), 89 FM10K_STAT("um", stats.um.count), 90 FM10K_STAT("xec", stats.xec.count), 91 FM10K_STAT("vlan_drop", stats.vlan_drop.count), 92 FM10K_STAT("loopback_drop", stats.loopback_drop.count), 93 FM10K_STAT("nodesc_drop", stats.nodesc_drop.count), 94 }; 95 96 #define FM10K_MBX_STAT(_name, _stat) { \ 97 .stat_string = _name, \ 98 .sizeof_stat = FIELD_SIZEOF(struct fm10k_mbx_info, _stat), \ 99 .stat_offset = offsetof(struct fm10k_mbx_info, _stat) \ 100 } 101 102 static const struct fm10k_stats fm10k_gstrings_mbx_stats[] = { 103 FM10K_MBX_STAT("mbx_tx_busy", tx_busy), 104 FM10K_MBX_STAT("mbx_tx_dropped", tx_dropped), 105 FM10K_MBX_STAT("mbx_tx_messages", tx_messages), 106 FM10K_MBX_STAT("mbx_tx_dwords", tx_dwords), 107 FM10K_MBX_STAT("mbx_tx_mbmem_pulled", tx_mbmem_pulled), 108 FM10K_MBX_STAT("mbx_rx_messages", rx_messages), 109 FM10K_MBX_STAT("mbx_rx_dwords", rx_dwords), 110 FM10K_MBX_STAT("mbx_rx_parse_err", rx_parse_err), 111 FM10K_MBX_STAT("mbx_rx_mbmem_pushed", rx_mbmem_pushed), 112 }; 113 114 #define FM10K_QUEUE_STAT(_name, _stat) { \ 115 .stat_string = _name, \ 116 .sizeof_stat = FIELD_SIZEOF(struct fm10k_ring, _stat), \ 117 .stat_offset = offsetof(struct fm10k_ring, _stat) \ 118 } 119 120 static const struct fm10k_stats fm10k_gstrings_queue_stats[] = { 121 FM10K_QUEUE_STAT("packets", stats.packets), 122 FM10K_QUEUE_STAT("bytes", stats.bytes), 123 }; 124 125 #define FM10K_GLOBAL_STATS_LEN ARRAY_SIZE(fm10k_gstrings_global_stats) 126 #define FM10K_PF_STATS_LEN ARRAY_SIZE(fm10k_gstrings_pf_stats) 127 #define FM10K_MBX_STATS_LEN ARRAY_SIZE(fm10k_gstrings_mbx_stats) 128 #define FM10K_QUEUE_STATS_LEN ARRAY_SIZE(fm10k_gstrings_queue_stats) 129 130 #define FM10K_STATIC_STATS_LEN (FM10K_GLOBAL_STATS_LEN + \ 131 FM10K_NETDEV_STATS_LEN + \ 132 FM10K_MBX_STATS_LEN) 133 134 static const char fm10k_gstrings_test[][ETH_GSTRING_LEN] = { 135 "Mailbox test (on/offline)" 136 }; 137 138 #define FM10K_TEST_LEN (sizeof(fm10k_gstrings_test) / ETH_GSTRING_LEN) 139 140 enum fm10k_self_test_types { 141 FM10K_TEST_MBX, 142 FM10K_TEST_MAX = FM10K_TEST_LEN 143 }; 144 145 enum { 146 FM10K_PRV_FLAG_LEN, 147 }; 148 149 static const char fm10k_prv_flags[FM10K_PRV_FLAG_LEN][ETH_GSTRING_LEN] = { 150 }; 151 152 static void fm10k_add_stat_strings(u8 **p, const char *prefix, 153 const struct fm10k_stats stats[], 154 const unsigned int size) 155 { 156 unsigned int i; 157 158 for (i = 0; i < size; i++) { 159 snprintf(*p, ETH_GSTRING_LEN, "%s%s", 160 prefix, stats[i].stat_string); 161 *p += ETH_GSTRING_LEN; 162 } 163 } 164 165 static void fm10k_get_stat_strings(struct net_device *dev, u8 *data) 166 { 167 struct fm10k_intfc *interface = netdev_priv(dev); 168 unsigned int i; 169 170 fm10k_add_stat_strings(&data, "", fm10k_gstrings_net_stats, 171 FM10K_NETDEV_STATS_LEN); 172 173 fm10k_add_stat_strings(&data, "", fm10k_gstrings_global_stats, 174 FM10K_GLOBAL_STATS_LEN); 175 176 fm10k_add_stat_strings(&data, "", fm10k_gstrings_mbx_stats, 177 FM10K_MBX_STATS_LEN); 178 179 if (interface->hw.mac.type != fm10k_mac_vf) 180 fm10k_add_stat_strings(&data, "", fm10k_gstrings_pf_stats, 181 FM10K_PF_STATS_LEN); 182 183 for (i = 0; i < interface->hw.mac.max_queues; i++) { 184 char prefix[ETH_GSTRING_LEN]; 185 186 snprintf(prefix, ETH_GSTRING_LEN, "tx_queue_%u_", i); 187 fm10k_add_stat_strings(&data, prefix, 188 fm10k_gstrings_queue_stats, 189 FM10K_QUEUE_STATS_LEN); 190 191 snprintf(prefix, ETH_GSTRING_LEN, "rx_queue_%u_", i); 192 fm10k_add_stat_strings(&data, prefix, 193 fm10k_gstrings_queue_stats, 194 FM10K_QUEUE_STATS_LEN); 195 } 196 } 197 198 static void fm10k_get_strings(struct net_device *dev, 199 u32 stringset, u8 *data) 200 { 201 switch (stringset) { 202 case ETH_SS_TEST: 203 memcpy(data, fm10k_gstrings_test, 204 FM10K_TEST_LEN * ETH_GSTRING_LEN); 205 break; 206 case ETH_SS_STATS: 207 fm10k_get_stat_strings(dev, data); 208 break; 209 case ETH_SS_PRIV_FLAGS: 210 memcpy(data, fm10k_prv_flags, 211 FM10K_PRV_FLAG_LEN * ETH_GSTRING_LEN); 212 break; 213 } 214 } 215 216 static int fm10k_get_sset_count(struct net_device *dev, int sset) 217 { 218 struct fm10k_intfc *interface = netdev_priv(dev); 219 struct fm10k_hw *hw = &interface->hw; 220 int stats_len = FM10K_STATIC_STATS_LEN; 221 222 switch (sset) { 223 case ETH_SS_TEST: 224 return FM10K_TEST_LEN; 225 case ETH_SS_STATS: 226 stats_len += hw->mac.max_queues * 2 * FM10K_QUEUE_STATS_LEN; 227 228 if (hw->mac.type != fm10k_mac_vf) 229 stats_len += FM10K_PF_STATS_LEN; 230 231 return stats_len; 232 case ETH_SS_PRIV_FLAGS: 233 return FM10K_PRV_FLAG_LEN; 234 default: 235 return -EOPNOTSUPP; 236 } 237 } 238 239 static void fm10k_add_ethtool_stats(u64 **data, void *pointer, 240 const struct fm10k_stats stats[], 241 const unsigned int size) 242 { 243 unsigned int i; 244 char *p; 245 246 if (!pointer) { 247 /* memory is not zero allocated so we have to clear it */ 248 for (i = 0; i < size; i++) 249 *((*data)++) = 0; 250 return; 251 } 252 253 for (i = 0; i < size; i++) { 254 p = (char *)pointer + stats[i].stat_offset; 255 256 switch (stats[i].sizeof_stat) { 257 case sizeof(u64): 258 *((*data)++) = *(u64 *)p; 259 break; 260 case sizeof(u32): 261 *((*data)++) = *(u32 *)p; 262 break; 263 case sizeof(u16): 264 *((*data)++) = *(u16 *)p; 265 break; 266 case sizeof(u8): 267 *((*data)++) = *(u8 *)p; 268 break; 269 default: 270 *((*data)++) = 0; 271 } 272 } 273 } 274 275 static void fm10k_get_ethtool_stats(struct net_device *netdev, 276 struct ethtool_stats __always_unused *stats, 277 u64 *data) 278 { 279 struct fm10k_intfc *interface = netdev_priv(netdev); 280 struct net_device_stats *net_stats = &netdev->stats; 281 int i; 282 283 fm10k_update_stats(interface); 284 285 fm10k_add_ethtool_stats(&data, net_stats, fm10k_gstrings_net_stats, 286 FM10K_NETDEV_STATS_LEN); 287 288 fm10k_add_ethtool_stats(&data, interface, fm10k_gstrings_global_stats, 289 FM10K_GLOBAL_STATS_LEN); 290 291 fm10k_add_ethtool_stats(&data, &interface->hw.mbx, 292 fm10k_gstrings_mbx_stats, 293 FM10K_MBX_STATS_LEN); 294 295 if (interface->hw.mac.type != fm10k_mac_vf) { 296 fm10k_add_ethtool_stats(&data, interface, 297 fm10k_gstrings_pf_stats, 298 FM10K_PF_STATS_LEN); 299 } 300 301 for (i = 0; i < interface->hw.mac.max_queues; i++) { 302 struct fm10k_ring *ring; 303 304 ring = interface->tx_ring[i]; 305 fm10k_add_ethtool_stats(&data, ring, 306 fm10k_gstrings_queue_stats, 307 FM10K_QUEUE_STATS_LEN); 308 309 ring = interface->rx_ring[i]; 310 fm10k_add_ethtool_stats(&data, ring, 311 fm10k_gstrings_queue_stats, 312 FM10K_QUEUE_STATS_LEN); 313 } 314 } 315 316 /* If function below adds more registers this define needs to be updated */ 317 #define FM10K_REGS_LEN_Q 29 318 319 static void fm10k_get_reg_q(struct fm10k_hw *hw, u32 *buff, int i) 320 { 321 int idx = 0; 322 323 buff[idx++] = fm10k_read_reg(hw, FM10K_RDBAL(i)); 324 buff[idx++] = fm10k_read_reg(hw, FM10K_RDBAH(i)); 325 buff[idx++] = fm10k_read_reg(hw, FM10K_RDLEN(i)); 326 buff[idx++] = fm10k_read_reg(hw, FM10K_TPH_RXCTRL(i)); 327 buff[idx++] = fm10k_read_reg(hw, FM10K_RDH(i)); 328 buff[idx++] = fm10k_read_reg(hw, FM10K_RDT(i)); 329 buff[idx++] = fm10k_read_reg(hw, FM10K_RXQCTL(i)); 330 buff[idx++] = fm10k_read_reg(hw, FM10K_RXDCTL(i)); 331 buff[idx++] = fm10k_read_reg(hw, FM10K_RXINT(i)); 332 buff[idx++] = fm10k_read_reg(hw, FM10K_SRRCTL(i)); 333 buff[idx++] = fm10k_read_reg(hw, FM10K_QPRC(i)); 334 buff[idx++] = fm10k_read_reg(hw, FM10K_QPRDC(i)); 335 buff[idx++] = fm10k_read_reg(hw, FM10K_QBRC_L(i)); 336 buff[idx++] = fm10k_read_reg(hw, FM10K_QBRC_H(i)); 337 buff[idx++] = fm10k_read_reg(hw, FM10K_TDBAL(i)); 338 buff[idx++] = fm10k_read_reg(hw, FM10K_TDBAH(i)); 339 buff[idx++] = fm10k_read_reg(hw, FM10K_TDLEN(i)); 340 buff[idx++] = fm10k_read_reg(hw, FM10K_TPH_TXCTRL(i)); 341 buff[idx++] = fm10k_read_reg(hw, FM10K_TDH(i)); 342 buff[idx++] = fm10k_read_reg(hw, FM10K_TDT(i)); 343 buff[idx++] = fm10k_read_reg(hw, FM10K_TXDCTL(i)); 344 buff[idx++] = fm10k_read_reg(hw, FM10K_TXQCTL(i)); 345 buff[idx++] = fm10k_read_reg(hw, FM10K_TXINT(i)); 346 buff[idx++] = fm10k_read_reg(hw, FM10K_QPTC(i)); 347 buff[idx++] = fm10k_read_reg(hw, FM10K_QBTC_L(i)); 348 buff[idx++] = fm10k_read_reg(hw, FM10K_QBTC_H(i)); 349 buff[idx++] = fm10k_read_reg(hw, FM10K_TQDLOC(i)); 350 buff[idx++] = fm10k_read_reg(hw, FM10K_TX_SGLORT(i)); 351 buff[idx++] = fm10k_read_reg(hw, FM10K_PFVTCTL(i)); 352 353 BUG_ON(idx != FM10K_REGS_LEN_Q); 354 } 355 356 /* If function above adds more registers this define needs to be updated */ 357 #define FM10K_REGS_LEN_VSI 43 358 359 static void fm10k_get_reg_vsi(struct fm10k_hw *hw, u32 *buff, int i) 360 { 361 int idx = 0, j; 362 363 buff[idx++] = fm10k_read_reg(hw, FM10K_MRQC(i)); 364 for (j = 0; j < 10; j++) 365 buff[idx++] = fm10k_read_reg(hw, FM10K_RSSRK(i, j)); 366 for (j = 0; j < 32; j++) 367 buff[idx++] = fm10k_read_reg(hw, FM10K_RETA(i, j)); 368 369 BUG_ON(idx != FM10K_REGS_LEN_VSI); 370 } 371 372 static void fm10k_get_regs(struct net_device *netdev, 373 struct ethtool_regs *regs, void *p) 374 { 375 struct fm10k_intfc *interface = netdev_priv(netdev); 376 struct fm10k_hw *hw = &interface->hw; 377 u32 *buff = p; 378 u16 i; 379 380 regs->version = BIT(24) | (hw->revision_id << 16) | hw->device_id; 381 382 switch (hw->mac.type) { 383 case fm10k_mac_pf: 384 /* General PF Registers */ 385 *(buff++) = fm10k_read_reg(hw, FM10K_CTRL); 386 *(buff++) = fm10k_read_reg(hw, FM10K_CTRL_EXT); 387 *(buff++) = fm10k_read_reg(hw, FM10K_GCR); 388 *(buff++) = fm10k_read_reg(hw, FM10K_GCR_EXT); 389 390 for (i = 0; i < 8; i++) { 391 *(buff++) = fm10k_read_reg(hw, FM10K_DGLORTMAP(i)); 392 *(buff++) = fm10k_read_reg(hw, FM10K_DGLORTDEC(i)); 393 } 394 395 for (i = 0; i < 65; i++) { 396 fm10k_get_reg_vsi(hw, buff, i); 397 buff += FM10K_REGS_LEN_VSI; 398 } 399 400 *(buff++) = fm10k_read_reg(hw, FM10K_DMA_CTRL); 401 *(buff++) = fm10k_read_reg(hw, FM10K_DMA_CTRL2); 402 403 for (i = 0; i < FM10K_MAX_QUEUES_PF; i++) { 404 fm10k_get_reg_q(hw, buff, i); 405 buff += FM10K_REGS_LEN_Q; 406 } 407 408 *(buff++) = fm10k_read_reg(hw, FM10K_TPH_CTRL); 409 410 for (i = 0; i < 8; i++) 411 *(buff++) = fm10k_read_reg(hw, FM10K_INT_MAP(i)); 412 413 /* Interrupt Throttling Registers */ 414 for (i = 0; i < 130; i++) 415 *(buff++) = fm10k_read_reg(hw, FM10K_ITR(i)); 416 417 break; 418 case fm10k_mac_vf: 419 /* General VF registers */ 420 *(buff++) = fm10k_read_reg(hw, FM10K_VFCTRL); 421 *(buff++) = fm10k_read_reg(hw, FM10K_VFINT_MAP); 422 *(buff++) = fm10k_read_reg(hw, FM10K_VFSYSTIME); 423 424 /* Interrupt Throttling Registers */ 425 for (i = 0; i < 8; i++) 426 *(buff++) = fm10k_read_reg(hw, FM10K_VFITR(i)); 427 428 fm10k_get_reg_vsi(hw, buff, 0); 429 buff += FM10K_REGS_LEN_VSI; 430 431 for (i = 0; i < FM10K_MAX_QUEUES_POOL; i++) { 432 if (i < hw->mac.max_queues) 433 fm10k_get_reg_q(hw, buff, i); 434 else 435 memset(buff, 0, sizeof(u32) * FM10K_REGS_LEN_Q); 436 buff += FM10K_REGS_LEN_Q; 437 } 438 439 break; 440 default: 441 return; 442 } 443 } 444 445 /* If function above adds more registers these define need to be updated */ 446 #define FM10K_REGS_LEN_PF \ 447 (162 + (65 * FM10K_REGS_LEN_VSI) + (FM10K_MAX_QUEUES_PF * FM10K_REGS_LEN_Q)) 448 #define FM10K_REGS_LEN_VF \ 449 (11 + FM10K_REGS_LEN_VSI + (FM10K_MAX_QUEUES_POOL * FM10K_REGS_LEN_Q)) 450 451 static int fm10k_get_regs_len(struct net_device *netdev) 452 { 453 struct fm10k_intfc *interface = netdev_priv(netdev); 454 struct fm10k_hw *hw = &interface->hw; 455 456 switch (hw->mac.type) { 457 case fm10k_mac_pf: 458 return FM10K_REGS_LEN_PF * sizeof(u32); 459 case fm10k_mac_vf: 460 return FM10K_REGS_LEN_VF * sizeof(u32); 461 default: 462 return 0; 463 } 464 } 465 466 static void fm10k_get_drvinfo(struct net_device *dev, 467 struct ethtool_drvinfo *info) 468 { 469 struct fm10k_intfc *interface = netdev_priv(dev); 470 471 strncpy(info->driver, fm10k_driver_name, 472 sizeof(info->driver) - 1); 473 strncpy(info->version, fm10k_driver_version, 474 sizeof(info->version) - 1); 475 strncpy(info->bus_info, pci_name(interface->pdev), 476 sizeof(info->bus_info) - 1); 477 } 478 479 static void fm10k_get_pauseparam(struct net_device *dev, 480 struct ethtool_pauseparam *pause) 481 { 482 struct fm10k_intfc *interface = netdev_priv(dev); 483 484 /* record fixed values for autoneg and tx pause */ 485 pause->autoneg = 0; 486 pause->tx_pause = 1; 487 488 pause->rx_pause = interface->rx_pause ? 1 : 0; 489 } 490 491 static int fm10k_set_pauseparam(struct net_device *dev, 492 struct ethtool_pauseparam *pause) 493 { 494 struct fm10k_intfc *interface = netdev_priv(dev); 495 struct fm10k_hw *hw = &interface->hw; 496 497 if (pause->autoneg || !pause->tx_pause) 498 return -EINVAL; 499 500 /* we can only support pause on the PF to avoid head-of-line blocking */ 501 if (hw->mac.type == fm10k_mac_pf) 502 interface->rx_pause = pause->rx_pause ? ~0 : 0; 503 else if (pause->rx_pause) 504 return -EINVAL; 505 506 if (netif_running(dev)) 507 fm10k_update_rx_drop_en(interface); 508 509 return 0; 510 } 511 512 static u32 fm10k_get_msglevel(struct net_device *netdev) 513 { 514 struct fm10k_intfc *interface = netdev_priv(netdev); 515 516 return interface->msg_enable; 517 } 518 519 static void fm10k_set_msglevel(struct net_device *netdev, u32 data) 520 { 521 struct fm10k_intfc *interface = netdev_priv(netdev); 522 523 interface->msg_enable = data; 524 } 525 526 static void fm10k_get_ringparam(struct net_device *netdev, 527 struct ethtool_ringparam *ring) 528 { 529 struct fm10k_intfc *interface = netdev_priv(netdev); 530 531 ring->rx_max_pending = FM10K_MAX_RXD; 532 ring->tx_max_pending = FM10K_MAX_TXD; 533 ring->rx_mini_max_pending = 0; 534 ring->rx_jumbo_max_pending = 0; 535 ring->rx_pending = interface->rx_ring_count; 536 ring->tx_pending = interface->tx_ring_count; 537 ring->rx_mini_pending = 0; 538 ring->rx_jumbo_pending = 0; 539 } 540 541 static int fm10k_set_ringparam(struct net_device *netdev, 542 struct ethtool_ringparam *ring) 543 { 544 struct fm10k_intfc *interface = netdev_priv(netdev); 545 struct fm10k_ring *temp_ring; 546 int i, err = 0; 547 u32 new_rx_count, new_tx_count; 548 549 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending)) 550 return -EINVAL; 551 552 new_tx_count = clamp_t(u32, ring->tx_pending, 553 FM10K_MIN_TXD, FM10K_MAX_TXD); 554 new_tx_count = ALIGN(new_tx_count, FM10K_REQ_TX_DESCRIPTOR_MULTIPLE); 555 556 new_rx_count = clamp_t(u32, ring->rx_pending, 557 FM10K_MIN_RXD, FM10K_MAX_RXD); 558 new_rx_count = ALIGN(new_rx_count, FM10K_REQ_RX_DESCRIPTOR_MULTIPLE); 559 560 if ((new_tx_count == interface->tx_ring_count) && 561 (new_rx_count == interface->rx_ring_count)) { 562 /* nothing to do */ 563 return 0; 564 } 565 566 while (test_and_set_bit(__FM10K_RESETTING, interface->state)) 567 usleep_range(1000, 2000); 568 569 if (!netif_running(interface->netdev)) { 570 for (i = 0; i < interface->num_tx_queues; i++) 571 interface->tx_ring[i]->count = new_tx_count; 572 for (i = 0; i < interface->num_rx_queues; i++) 573 interface->rx_ring[i]->count = new_rx_count; 574 interface->tx_ring_count = new_tx_count; 575 interface->rx_ring_count = new_rx_count; 576 goto clear_reset; 577 } 578 579 /* allocate temporary buffer to store rings in */ 580 i = max_t(int, interface->num_tx_queues, interface->num_rx_queues); 581 temp_ring = vmalloc(i * sizeof(struct fm10k_ring)); 582 583 if (!temp_ring) { 584 err = -ENOMEM; 585 goto clear_reset; 586 } 587 588 fm10k_down(interface); 589 590 /* Setup new Tx resources and free the old Tx resources in that order. 591 * We can then assign the new resources to the rings via a memcpy. 592 * The advantage to this approach is that we are guaranteed to still 593 * have resources even in the case of an allocation failure. 594 */ 595 if (new_tx_count != interface->tx_ring_count) { 596 for (i = 0; i < interface->num_tx_queues; i++) { 597 memcpy(&temp_ring[i], interface->tx_ring[i], 598 sizeof(struct fm10k_ring)); 599 600 temp_ring[i].count = new_tx_count; 601 err = fm10k_setup_tx_resources(&temp_ring[i]); 602 if (err) { 603 while (i) { 604 i--; 605 fm10k_free_tx_resources(&temp_ring[i]); 606 } 607 goto err_setup; 608 } 609 } 610 611 for (i = 0; i < interface->num_tx_queues; i++) { 612 fm10k_free_tx_resources(interface->tx_ring[i]); 613 614 memcpy(interface->tx_ring[i], &temp_ring[i], 615 sizeof(struct fm10k_ring)); 616 } 617 618 interface->tx_ring_count = new_tx_count; 619 } 620 621 /* Repeat the process for the Rx rings if needed */ 622 if (new_rx_count != interface->rx_ring_count) { 623 for (i = 0; i < interface->num_rx_queues; i++) { 624 memcpy(&temp_ring[i], interface->rx_ring[i], 625 sizeof(struct fm10k_ring)); 626 627 temp_ring[i].count = new_rx_count; 628 err = fm10k_setup_rx_resources(&temp_ring[i]); 629 if (err) { 630 while (i) { 631 i--; 632 fm10k_free_rx_resources(&temp_ring[i]); 633 } 634 goto err_setup; 635 } 636 } 637 638 for (i = 0; i < interface->num_rx_queues; i++) { 639 fm10k_free_rx_resources(interface->rx_ring[i]); 640 641 memcpy(interface->rx_ring[i], &temp_ring[i], 642 sizeof(struct fm10k_ring)); 643 } 644 645 interface->rx_ring_count = new_rx_count; 646 } 647 648 err_setup: 649 fm10k_up(interface); 650 vfree(temp_ring); 651 clear_reset: 652 clear_bit(__FM10K_RESETTING, interface->state); 653 return err; 654 } 655 656 static int fm10k_get_coalesce(struct net_device *dev, 657 struct ethtool_coalesce *ec) 658 { 659 struct fm10k_intfc *interface = netdev_priv(dev); 660 661 ec->use_adaptive_tx_coalesce = ITR_IS_ADAPTIVE(interface->tx_itr); 662 ec->tx_coalesce_usecs = interface->tx_itr & ~FM10K_ITR_ADAPTIVE; 663 664 ec->use_adaptive_rx_coalesce = ITR_IS_ADAPTIVE(interface->rx_itr); 665 ec->rx_coalesce_usecs = interface->rx_itr & ~FM10K_ITR_ADAPTIVE; 666 667 return 0; 668 } 669 670 static int fm10k_set_coalesce(struct net_device *dev, 671 struct ethtool_coalesce *ec) 672 { 673 struct fm10k_intfc *interface = netdev_priv(dev); 674 struct fm10k_q_vector *qv; 675 u16 tx_itr, rx_itr; 676 int i; 677 678 /* verify limits */ 679 if ((ec->rx_coalesce_usecs > FM10K_ITR_MAX) || 680 (ec->tx_coalesce_usecs > FM10K_ITR_MAX)) 681 return -EINVAL; 682 683 /* record settings */ 684 tx_itr = ec->tx_coalesce_usecs; 685 rx_itr = ec->rx_coalesce_usecs; 686 687 /* set initial values for adaptive ITR */ 688 if (ec->use_adaptive_tx_coalesce) 689 tx_itr = FM10K_ITR_ADAPTIVE | FM10K_TX_ITR_DEFAULT; 690 691 if (ec->use_adaptive_rx_coalesce) 692 rx_itr = FM10K_ITR_ADAPTIVE | FM10K_RX_ITR_DEFAULT; 693 694 /* update interface */ 695 interface->tx_itr = tx_itr; 696 interface->rx_itr = rx_itr; 697 698 /* update q_vectors */ 699 for (i = 0; i < interface->num_q_vectors; i++) { 700 qv = interface->q_vector[i]; 701 qv->tx.itr = tx_itr; 702 qv->rx.itr = rx_itr; 703 } 704 705 return 0; 706 } 707 708 static int fm10k_get_rss_hash_opts(struct fm10k_intfc *interface, 709 struct ethtool_rxnfc *cmd) 710 { 711 cmd->data = 0; 712 713 /* Report default options for RSS on fm10k */ 714 switch (cmd->flow_type) { 715 case TCP_V4_FLOW: 716 case TCP_V6_FLOW: 717 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 718 /* fall through */ 719 case UDP_V4_FLOW: 720 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, 721 interface->flags)) 722 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 723 /* fall through */ 724 case SCTP_V4_FLOW: 725 case SCTP_V6_FLOW: 726 case AH_ESP_V4_FLOW: 727 case AH_ESP_V6_FLOW: 728 case AH_V4_FLOW: 729 case AH_V6_FLOW: 730 case ESP_V4_FLOW: 731 case ESP_V6_FLOW: 732 case IPV4_FLOW: 733 case IPV6_FLOW: 734 cmd->data |= RXH_IP_SRC | RXH_IP_DST; 735 break; 736 case UDP_V6_FLOW: 737 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, 738 interface->flags)) 739 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 740 cmd->data |= RXH_IP_SRC | RXH_IP_DST; 741 break; 742 default: 743 return -EINVAL; 744 } 745 746 return 0; 747 } 748 749 static int fm10k_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd, 750 u32 __always_unused *rule_locs) 751 { 752 struct fm10k_intfc *interface = netdev_priv(dev); 753 int ret = -EOPNOTSUPP; 754 755 switch (cmd->cmd) { 756 case ETHTOOL_GRXRINGS: 757 cmd->data = interface->num_rx_queues; 758 ret = 0; 759 break; 760 case ETHTOOL_GRXFH: 761 ret = fm10k_get_rss_hash_opts(interface, cmd); 762 break; 763 default: 764 break; 765 } 766 767 return ret; 768 } 769 770 static int fm10k_set_rss_hash_opt(struct fm10k_intfc *interface, 771 struct ethtool_rxnfc *nfc) 772 { 773 int rss_ipv4_udp = test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, 774 interface->flags); 775 int rss_ipv6_udp = test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, 776 interface->flags); 777 778 /* RSS does not support anything other than hashing 779 * to queues on src and dst IPs and ports 780 */ 781 if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST | 782 RXH_L4_B_0_1 | RXH_L4_B_2_3)) 783 return -EINVAL; 784 785 switch (nfc->flow_type) { 786 case TCP_V4_FLOW: 787 case TCP_V6_FLOW: 788 if (!(nfc->data & RXH_IP_SRC) || 789 !(nfc->data & RXH_IP_DST) || 790 !(nfc->data & RXH_L4_B_0_1) || 791 !(nfc->data & RXH_L4_B_2_3)) 792 return -EINVAL; 793 break; 794 case UDP_V4_FLOW: 795 if (!(nfc->data & RXH_IP_SRC) || 796 !(nfc->data & RXH_IP_DST)) 797 return -EINVAL; 798 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) { 799 case 0: 800 clear_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, 801 interface->flags); 802 break; 803 case (RXH_L4_B_0_1 | RXH_L4_B_2_3): 804 set_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, 805 interface->flags); 806 break; 807 default: 808 return -EINVAL; 809 } 810 break; 811 case UDP_V6_FLOW: 812 if (!(nfc->data & RXH_IP_SRC) || 813 !(nfc->data & RXH_IP_DST)) 814 return -EINVAL; 815 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) { 816 case 0: 817 clear_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, 818 interface->flags); 819 break; 820 case (RXH_L4_B_0_1 | RXH_L4_B_2_3): 821 set_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, 822 interface->flags); 823 break; 824 default: 825 return -EINVAL; 826 } 827 break; 828 case AH_ESP_V4_FLOW: 829 case AH_V4_FLOW: 830 case ESP_V4_FLOW: 831 case SCTP_V4_FLOW: 832 case AH_ESP_V6_FLOW: 833 case AH_V6_FLOW: 834 case ESP_V6_FLOW: 835 case SCTP_V6_FLOW: 836 if (!(nfc->data & RXH_IP_SRC) || 837 !(nfc->data & RXH_IP_DST) || 838 (nfc->data & RXH_L4_B_0_1) || 839 (nfc->data & RXH_L4_B_2_3)) 840 return -EINVAL; 841 break; 842 default: 843 return -EINVAL; 844 } 845 846 /* If something changed we need to update the MRQC register. Note that 847 * test_bit() is guaranteed to return strictly 0 or 1, so testing for 848 * equality is safe. 849 */ 850 if ((rss_ipv4_udp != test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, 851 interface->flags)) || 852 (rss_ipv6_udp != test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, 853 interface->flags))) { 854 struct fm10k_hw *hw = &interface->hw; 855 bool warn = false; 856 u32 mrqc; 857 858 /* Perform hash on these packet types */ 859 mrqc = FM10K_MRQC_IPV4 | 860 FM10K_MRQC_TCP_IPV4 | 861 FM10K_MRQC_IPV6 | 862 FM10K_MRQC_TCP_IPV6; 863 864 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, 865 interface->flags)) { 866 mrqc |= FM10K_MRQC_UDP_IPV4; 867 warn = true; 868 } 869 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, 870 interface->flags)) { 871 mrqc |= FM10K_MRQC_UDP_IPV6; 872 warn = true; 873 } 874 875 /* If we enable UDP RSS display a warning that this may cause 876 * fragmented UDP packets to arrive out of order. 877 */ 878 if (warn) 879 netif_warn(interface, drv, interface->netdev, 880 "enabling UDP RSS: fragmented packets may arrive out of order to the stack above\n"); 881 882 fm10k_write_reg(hw, FM10K_MRQC(0), mrqc); 883 } 884 885 return 0; 886 } 887 888 static int fm10k_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd) 889 { 890 struct fm10k_intfc *interface = netdev_priv(dev); 891 int ret = -EOPNOTSUPP; 892 893 switch (cmd->cmd) { 894 case ETHTOOL_SRXFH: 895 ret = fm10k_set_rss_hash_opt(interface, cmd); 896 break; 897 default: 898 break; 899 } 900 901 return ret; 902 } 903 904 static int fm10k_mbx_test(struct fm10k_intfc *interface, u64 *data) 905 { 906 struct fm10k_hw *hw = &interface->hw; 907 struct fm10k_mbx_info *mbx = &hw->mbx; 908 u32 attr_flag, test_msg[6]; 909 unsigned long timeout; 910 int err = -EINVAL; 911 912 /* For now this is a VF only feature */ 913 if (hw->mac.type != fm10k_mac_vf) 914 return 0; 915 916 /* loop through both nested and unnested attribute types */ 917 for (attr_flag = BIT(FM10K_TEST_MSG_UNSET); 918 attr_flag < BIT(2 * FM10K_TEST_MSG_NESTED); 919 attr_flag += attr_flag) { 920 /* generate message to be tested */ 921 fm10k_tlv_msg_test_create(test_msg, attr_flag); 922 923 fm10k_mbx_lock(interface); 924 mbx->test_result = FM10K_NOT_IMPLEMENTED; 925 err = mbx->ops.enqueue_tx(hw, mbx, test_msg); 926 fm10k_mbx_unlock(interface); 927 928 /* wait up to 1 second for response */ 929 timeout = jiffies + HZ; 930 do { 931 if (err < 0) 932 goto err_out; 933 934 usleep_range(500, 1000); 935 936 fm10k_mbx_lock(interface); 937 mbx->ops.process(hw, mbx); 938 fm10k_mbx_unlock(interface); 939 940 err = mbx->test_result; 941 if (!err) 942 break; 943 } while (time_is_after_jiffies(timeout)); 944 945 /* reporting errors */ 946 if (err) 947 goto err_out; 948 } 949 950 err_out: 951 *data = err < 0 ? (attr_flag) : (err > 0); 952 return err; 953 } 954 955 static void fm10k_self_test(struct net_device *dev, 956 struct ethtool_test *eth_test, u64 *data) 957 { 958 struct fm10k_intfc *interface = netdev_priv(dev); 959 struct fm10k_hw *hw = &interface->hw; 960 961 memset(data, 0, sizeof(*data) * FM10K_TEST_LEN); 962 963 if (FM10K_REMOVED(hw->hw_addr)) { 964 netif_err(interface, drv, dev, 965 "Interface removed - test blocked\n"); 966 eth_test->flags |= ETH_TEST_FL_FAILED; 967 return; 968 } 969 970 if (fm10k_mbx_test(interface, &data[FM10K_TEST_MBX])) 971 eth_test->flags |= ETH_TEST_FL_FAILED; 972 } 973 974 static u32 fm10k_get_priv_flags(struct net_device *netdev) 975 { 976 return 0; 977 } 978 979 static int fm10k_set_priv_flags(struct net_device *netdev, u32 priv_flags) 980 { 981 if (priv_flags >= BIT(FM10K_PRV_FLAG_LEN)) 982 return -EINVAL; 983 984 return 0; 985 } 986 987 static u32 fm10k_get_reta_size(struct net_device __always_unused *netdev) 988 { 989 return FM10K_RETA_SIZE * FM10K_RETA_ENTRIES_PER_REG; 990 } 991 992 void fm10k_write_reta(struct fm10k_intfc *interface, const u32 *indir) 993 { 994 u16 rss_i = interface->ring_feature[RING_F_RSS].indices; 995 struct fm10k_hw *hw = &interface->hw; 996 u32 table[4]; 997 int i, j; 998 999 /* record entries to reta table */ 1000 for (i = 0; i < FM10K_RETA_SIZE; i++) { 1001 u32 reta, n; 1002 1003 /* generate a new table if we weren't given one */ 1004 for (j = 0; j < 4; j++) { 1005 if (indir) 1006 n = indir[4 * i + j]; 1007 else 1008 n = ethtool_rxfh_indir_default(4 * i + j, 1009 rss_i); 1010 1011 table[j] = n; 1012 } 1013 1014 reta = table[0] | 1015 (table[1] << 8) | 1016 (table[2] << 16) | 1017 (table[3] << 24); 1018 1019 if (interface->reta[i] == reta) 1020 continue; 1021 1022 interface->reta[i] = reta; 1023 fm10k_write_reg(hw, FM10K_RETA(0, i), reta); 1024 } 1025 } 1026 1027 static int fm10k_get_reta(struct net_device *netdev, u32 *indir) 1028 { 1029 struct fm10k_intfc *interface = netdev_priv(netdev); 1030 int i; 1031 1032 if (!indir) 1033 return 0; 1034 1035 for (i = 0; i < FM10K_RETA_SIZE; i++, indir += 4) { 1036 u32 reta = interface->reta[i]; 1037 1038 indir[0] = (reta << 24) >> 24; 1039 indir[1] = (reta << 16) >> 24; 1040 indir[2] = (reta << 8) >> 24; 1041 indir[3] = (reta) >> 24; 1042 } 1043 1044 return 0; 1045 } 1046 1047 static int fm10k_set_reta(struct net_device *netdev, const u32 *indir) 1048 { 1049 struct fm10k_intfc *interface = netdev_priv(netdev); 1050 int i; 1051 u16 rss_i; 1052 1053 if (!indir) 1054 return 0; 1055 1056 /* Verify user input. */ 1057 rss_i = interface->ring_feature[RING_F_RSS].indices; 1058 for (i = fm10k_get_reta_size(netdev); i--;) { 1059 if (indir[i] < rss_i) 1060 continue; 1061 return -EINVAL; 1062 } 1063 1064 fm10k_write_reta(interface, indir); 1065 1066 return 0; 1067 } 1068 1069 static u32 fm10k_get_rssrk_size(struct net_device __always_unused *netdev) 1070 { 1071 return FM10K_RSSRK_SIZE * FM10K_RSSRK_ENTRIES_PER_REG; 1072 } 1073 1074 static int fm10k_get_rssh(struct net_device *netdev, u32 *indir, u8 *key, 1075 u8 *hfunc) 1076 { 1077 struct fm10k_intfc *interface = netdev_priv(netdev); 1078 int i, err; 1079 1080 if (hfunc) 1081 *hfunc = ETH_RSS_HASH_TOP; 1082 1083 err = fm10k_get_reta(netdev, indir); 1084 if (err || !key) 1085 return err; 1086 1087 for (i = 0; i < FM10K_RSSRK_SIZE; i++, key += 4) 1088 *(__le32 *)key = cpu_to_le32(interface->rssrk[i]); 1089 1090 return 0; 1091 } 1092 1093 static int fm10k_set_rssh(struct net_device *netdev, const u32 *indir, 1094 const u8 *key, const u8 hfunc) 1095 { 1096 struct fm10k_intfc *interface = netdev_priv(netdev); 1097 struct fm10k_hw *hw = &interface->hw; 1098 int i, err; 1099 1100 /* We do not allow change in unsupported parameters */ 1101 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 1102 return -EOPNOTSUPP; 1103 1104 err = fm10k_set_reta(netdev, indir); 1105 if (err || !key) 1106 return err; 1107 1108 for (i = 0; i < FM10K_RSSRK_SIZE; i++, key += 4) { 1109 u32 rssrk = le32_to_cpu(*(__le32 *)key); 1110 1111 if (interface->rssrk[i] == rssrk) 1112 continue; 1113 1114 interface->rssrk[i] = rssrk; 1115 fm10k_write_reg(hw, FM10K_RSSRK(0, i), rssrk); 1116 } 1117 1118 return 0; 1119 } 1120 1121 static unsigned int fm10k_max_channels(struct net_device *dev) 1122 { 1123 struct fm10k_intfc *interface = netdev_priv(dev); 1124 unsigned int max_combined = interface->hw.mac.max_queues; 1125 u8 tcs = netdev_get_num_tc(dev); 1126 1127 /* For QoS report channels per traffic class */ 1128 if (tcs > 1) 1129 max_combined = BIT((fls(max_combined / tcs) - 1)); 1130 1131 return max_combined; 1132 } 1133 1134 static void fm10k_get_channels(struct net_device *dev, 1135 struct ethtool_channels *ch) 1136 { 1137 struct fm10k_intfc *interface = netdev_priv(dev); 1138 struct fm10k_hw *hw = &interface->hw; 1139 1140 /* report maximum channels */ 1141 ch->max_combined = fm10k_max_channels(dev); 1142 1143 /* report info for other vector */ 1144 ch->max_other = NON_Q_VECTORS(hw); 1145 ch->other_count = ch->max_other; 1146 1147 /* record RSS queues */ 1148 ch->combined_count = interface->ring_feature[RING_F_RSS].indices; 1149 } 1150 1151 static int fm10k_set_channels(struct net_device *dev, 1152 struct ethtool_channels *ch) 1153 { 1154 struct fm10k_intfc *interface = netdev_priv(dev); 1155 unsigned int count = ch->combined_count; 1156 struct fm10k_hw *hw = &interface->hw; 1157 1158 /* verify they are not requesting separate vectors */ 1159 if (!count || ch->rx_count || ch->tx_count) 1160 return -EINVAL; 1161 1162 /* verify other_count has not changed */ 1163 if (ch->other_count != NON_Q_VECTORS(hw)) 1164 return -EINVAL; 1165 1166 /* verify the number of channels does not exceed hardware limits */ 1167 if (count > fm10k_max_channels(dev)) 1168 return -EINVAL; 1169 1170 interface->ring_feature[RING_F_RSS].limit = count; 1171 1172 /* use setup TC to update any traffic class queue mapping */ 1173 return fm10k_setup_tc(dev, netdev_get_num_tc(dev)); 1174 } 1175 1176 static const struct ethtool_ops fm10k_ethtool_ops = { 1177 .get_strings = fm10k_get_strings, 1178 .get_sset_count = fm10k_get_sset_count, 1179 .get_ethtool_stats = fm10k_get_ethtool_stats, 1180 .get_drvinfo = fm10k_get_drvinfo, 1181 .get_link = ethtool_op_get_link, 1182 .get_pauseparam = fm10k_get_pauseparam, 1183 .set_pauseparam = fm10k_set_pauseparam, 1184 .get_msglevel = fm10k_get_msglevel, 1185 .set_msglevel = fm10k_set_msglevel, 1186 .get_ringparam = fm10k_get_ringparam, 1187 .set_ringparam = fm10k_set_ringparam, 1188 .get_coalesce = fm10k_get_coalesce, 1189 .set_coalesce = fm10k_set_coalesce, 1190 .get_rxnfc = fm10k_get_rxnfc, 1191 .set_rxnfc = fm10k_set_rxnfc, 1192 .get_regs = fm10k_get_regs, 1193 .get_regs_len = fm10k_get_regs_len, 1194 .self_test = fm10k_self_test, 1195 .get_priv_flags = fm10k_get_priv_flags, 1196 .set_priv_flags = fm10k_set_priv_flags, 1197 .get_rxfh_indir_size = fm10k_get_reta_size, 1198 .get_rxfh_key_size = fm10k_get_rssrk_size, 1199 .get_rxfh = fm10k_get_rssh, 1200 .set_rxfh = fm10k_set_rssh, 1201 .get_channels = fm10k_get_channels, 1202 .set_channels = fm10k_set_channels, 1203 .get_ts_info = ethtool_op_get_ts_info, 1204 }; 1205 1206 void fm10k_set_ethtool_ops(struct net_device *dev) 1207 { 1208 dev->ethtool_ops = &fm10k_ethtool_ops; 1209 } 1210