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