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