1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */ 3 4 /* 5 * nfp_net_ethtool.c 6 * Netronome network device driver: ethtool support 7 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com> 8 * Jason McMullan <jason.mcmullan@netronome.com> 9 * Rolf Neugebauer <rolf.neugebauer@netronome.com> 10 * Brad Petrus <brad.petrus@netronome.com> 11 */ 12 13 #include <linux/bitfield.h> 14 #include <linux/kernel.h> 15 #include <linux/netdevice.h> 16 #include <linux/etherdevice.h> 17 #include <linux/interrupt.h> 18 #include <linux/pci.h> 19 #include <linux/ethtool.h> 20 #include <linux/firmware.h> 21 #include <linux/sfp.h> 22 23 #include "nfpcore/nfp.h" 24 #include "nfpcore/nfp_dev.h" 25 #include "nfpcore/nfp_nsp.h" 26 #include "nfp_app.h" 27 #include "nfp_main.h" 28 #include "nfp_net_ctrl.h" 29 #include "nfp_net_dp.h" 30 #include "nfp_net.h" 31 #include "nfp_port.h" 32 #include "nfpcore/nfp_cpp.h" 33 34 struct nfp_et_stat { 35 char name[ETH_GSTRING_LEN]; 36 int off; 37 }; 38 39 static const struct nfp_et_stat nfp_net_et_stats[] = { 40 /* Stats from the device */ 41 { "dev_rx_discards", NFP_NET_CFG_STATS_RX_DISCARDS }, 42 { "dev_rx_errors", NFP_NET_CFG_STATS_RX_ERRORS }, 43 { "dev_rx_bytes", NFP_NET_CFG_STATS_RX_OCTETS }, 44 { "dev_rx_uc_bytes", NFP_NET_CFG_STATS_RX_UC_OCTETS }, 45 { "dev_rx_mc_bytes", NFP_NET_CFG_STATS_RX_MC_OCTETS }, 46 { "dev_rx_bc_bytes", NFP_NET_CFG_STATS_RX_BC_OCTETS }, 47 { "dev_rx_pkts", NFP_NET_CFG_STATS_RX_FRAMES }, 48 { "dev_rx_mc_pkts", NFP_NET_CFG_STATS_RX_MC_FRAMES }, 49 { "dev_rx_bc_pkts", NFP_NET_CFG_STATS_RX_BC_FRAMES }, 50 51 { "dev_tx_discards", NFP_NET_CFG_STATS_TX_DISCARDS }, 52 { "dev_tx_errors", NFP_NET_CFG_STATS_TX_ERRORS }, 53 { "dev_tx_bytes", NFP_NET_CFG_STATS_TX_OCTETS }, 54 { "dev_tx_uc_bytes", NFP_NET_CFG_STATS_TX_UC_OCTETS }, 55 { "dev_tx_mc_bytes", NFP_NET_CFG_STATS_TX_MC_OCTETS }, 56 { "dev_tx_bc_bytes", NFP_NET_CFG_STATS_TX_BC_OCTETS }, 57 { "dev_tx_pkts", NFP_NET_CFG_STATS_TX_FRAMES }, 58 { "dev_tx_mc_pkts", NFP_NET_CFG_STATS_TX_MC_FRAMES }, 59 { "dev_tx_bc_pkts", NFP_NET_CFG_STATS_TX_BC_FRAMES }, 60 61 { "bpf_pass_pkts", NFP_NET_CFG_STATS_APP0_FRAMES }, 62 { "bpf_pass_bytes", NFP_NET_CFG_STATS_APP0_BYTES }, 63 /* see comments in outro functions in nfp_bpf_jit.c to find out 64 * how different BPF modes use app-specific counters 65 */ 66 { "bpf_app1_pkts", NFP_NET_CFG_STATS_APP1_FRAMES }, 67 { "bpf_app1_bytes", NFP_NET_CFG_STATS_APP1_BYTES }, 68 { "bpf_app2_pkts", NFP_NET_CFG_STATS_APP2_FRAMES }, 69 { "bpf_app2_bytes", NFP_NET_CFG_STATS_APP2_BYTES }, 70 { "bpf_app3_pkts", NFP_NET_CFG_STATS_APP3_FRAMES }, 71 { "bpf_app3_bytes", NFP_NET_CFG_STATS_APP3_BYTES }, 72 }; 73 74 static const struct nfp_et_stat nfp_mac_et_stats[] = { 75 { "rx_octets", NFP_MAC_STATS_RX_IN_OCTETS, }, 76 { "rx_frame_too_long_errors", 77 NFP_MAC_STATS_RX_FRAME_TOO_LONG_ERRORS, }, 78 { "rx_range_length_errors", NFP_MAC_STATS_RX_RANGE_LENGTH_ERRORS, }, 79 { "rx_vlan_received_ok", NFP_MAC_STATS_RX_VLAN_RECEIVED_OK, }, 80 { "rx_errors", NFP_MAC_STATS_RX_IN_ERRORS, }, 81 { "rx_broadcast_pkts", NFP_MAC_STATS_RX_IN_BROADCAST_PKTS, }, 82 { "rx_drop_events", NFP_MAC_STATS_RX_DROP_EVENTS, }, 83 { "rx_alignment_errors", NFP_MAC_STATS_RX_ALIGNMENT_ERRORS, }, 84 { "rx_pause_mac_ctrl_frames", 85 NFP_MAC_STATS_RX_PAUSE_MAC_CTRL_FRAMES, }, 86 { "rx_frames_received_ok", NFP_MAC_STATS_RX_FRAMES_RECEIVED_OK, }, 87 { "rx_frame_check_sequence_errors", 88 NFP_MAC_STATS_RX_FRAME_CHECK_SEQUENCE_ERRORS, }, 89 { "rx_unicast_pkts", NFP_MAC_STATS_RX_UNICAST_PKTS, }, 90 { "rx_multicast_pkts", NFP_MAC_STATS_RX_MULTICAST_PKTS, }, 91 { "rx_pkts", NFP_MAC_STATS_RX_PKTS, }, 92 { "rx_undersize_pkts", NFP_MAC_STATS_RX_UNDERSIZE_PKTS, }, 93 { "rx_pkts_64_octets", NFP_MAC_STATS_RX_PKTS_64_OCTETS, }, 94 { "rx_pkts_65_to_127_octets", 95 NFP_MAC_STATS_RX_PKTS_65_TO_127_OCTETS, }, 96 { "rx_pkts_128_to_255_octets", 97 NFP_MAC_STATS_RX_PKTS_128_TO_255_OCTETS, }, 98 { "rx_pkts_256_to_511_octets", 99 NFP_MAC_STATS_RX_PKTS_256_TO_511_OCTETS, }, 100 { "rx_pkts_512_to_1023_octets", 101 NFP_MAC_STATS_RX_PKTS_512_TO_1023_OCTETS, }, 102 { "rx_pkts_1024_to_1518_octets", 103 NFP_MAC_STATS_RX_PKTS_1024_TO_1518_OCTETS, }, 104 { "rx_pkts_1519_to_max_octets", 105 NFP_MAC_STATS_RX_PKTS_1519_TO_MAX_OCTETS, }, 106 { "rx_jabbers", NFP_MAC_STATS_RX_JABBERS, }, 107 { "rx_fragments", NFP_MAC_STATS_RX_FRAGMENTS, }, 108 { "rx_oversize_pkts", NFP_MAC_STATS_RX_OVERSIZE_PKTS, }, 109 { "rx_pause_frames_class0", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS0, }, 110 { "rx_pause_frames_class1", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS1, }, 111 { "rx_pause_frames_class2", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS2, }, 112 { "rx_pause_frames_class3", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS3, }, 113 { "rx_pause_frames_class4", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS4, }, 114 { "rx_pause_frames_class5", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS5, }, 115 { "rx_pause_frames_class6", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS6, }, 116 { "rx_pause_frames_class7", NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS7, }, 117 { "rx_mac_ctrl_frames_received", 118 NFP_MAC_STATS_RX_MAC_CTRL_FRAMES_RECEIVED, }, 119 { "rx_mac_head_drop", NFP_MAC_STATS_RX_MAC_HEAD_DROP, }, 120 { "tx_queue_drop", NFP_MAC_STATS_TX_QUEUE_DROP, }, 121 { "tx_octets", NFP_MAC_STATS_TX_OUT_OCTETS, }, 122 { "tx_vlan_transmitted_ok", NFP_MAC_STATS_TX_VLAN_TRANSMITTED_OK, }, 123 { "tx_errors", NFP_MAC_STATS_TX_OUT_ERRORS, }, 124 { "tx_broadcast_pkts", NFP_MAC_STATS_TX_BROADCAST_PKTS, }, 125 { "tx_pause_mac_ctrl_frames", 126 NFP_MAC_STATS_TX_PAUSE_MAC_CTRL_FRAMES, }, 127 { "tx_frames_transmitted_ok", 128 NFP_MAC_STATS_TX_FRAMES_TRANSMITTED_OK, }, 129 { "tx_unicast_pkts", NFP_MAC_STATS_TX_UNICAST_PKTS, }, 130 { "tx_multicast_pkts", NFP_MAC_STATS_TX_MULTICAST_PKTS, }, 131 { "tx_pkts_64_octets", NFP_MAC_STATS_TX_PKTS_64_OCTETS, }, 132 { "tx_pkts_65_to_127_octets", 133 NFP_MAC_STATS_TX_PKTS_65_TO_127_OCTETS, }, 134 { "tx_pkts_128_to_255_octets", 135 NFP_MAC_STATS_TX_PKTS_128_TO_255_OCTETS, }, 136 { "tx_pkts_256_to_511_octets", 137 NFP_MAC_STATS_TX_PKTS_256_TO_511_OCTETS, }, 138 { "tx_pkts_512_to_1023_octets", 139 NFP_MAC_STATS_TX_PKTS_512_TO_1023_OCTETS, }, 140 { "tx_pkts_1024_to_1518_octets", 141 NFP_MAC_STATS_TX_PKTS_1024_TO_1518_OCTETS, }, 142 { "tx_pkts_1519_to_max_octets", 143 NFP_MAC_STATS_TX_PKTS_1519_TO_MAX_OCTETS, }, 144 { "tx_pause_frames_class0", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS0, }, 145 { "tx_pause_frames_class1", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS1, }, 146 { "tx_pause_frames_class2", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS2, }, 147 { "tx_pause_frames_class3", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS3, }, 148 { "tx_pause_frames_class4", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS4, }, 149 { "tx_pause_frames_class5", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS5, }, 150 { "tx_pause_frames_class6", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS6, }, 151 { "tx_pause_frames_class7", NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS7, }, 152 }; 153 154 static const char nfp_tlv_stat_names[][ETH_GSTRING_LEN] = { 155 [1] = "dev_rx_discards", 156 [2] = "dev_rx_errors", 157 [3] = "dev_rx_bytes", 158 [4] = "dev_rx_uc_bytes", 159 [5] = "dev_rx_mc_bytes", 160 [6] = "dev_rx_bc_bytes", 161 [7] = "dev_rx_pkts", 162 [8] = "dev_rx_mc_pkts", 163 [9] = "dev_rx_bc_pkts", 164 165 [10] = "dev_tx_discards", 166 [11] = "dev_tx_errors", 167 [12] = "dev_tx_bytes", 168 [13] = "dev_tx_uc_bytes", 169 [14] = "dev_tx_mc_bytes", 170 [15] = "dev_tx_bc_bytes", 171 [16] = "dev_tx_pkts", 172 [17] = "dev_tx_mc_pkts", 173 [18] = "dev_tx_bc_pkts", 174 }; 175 176 #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats) 177 #define NN_ET_SWITCH_STATS_LEN 9 178 #define NN_RVEC_GATHER_STATS 13 179 #define NN_RVEC_PER_Q_STATS 3 180 #define NN_CTRL_PATH_STATS 4 181 182 #define SFP_SFF_REV_COMPLIANCE 1 183 184 static void nfp_net_get_nspinfo(struct nfp_app *app, char *version) 185 { 186 struct nfp_nsp *nsp; 187 188 if (!app) 189 return; 190 191 nsp = nfp_nsp_open(app->cpp); 192 if (IS_ERR(nsp)) 193 return; 194 195 snprintf(version, ETHTOOL_FWVERS_LEN, "%hu.%hu", 196 nfp_nsp_get_abi_ver_major(nsp), 197 nfp_nsp_get_abi_ver_minor(nsp)); 198 199 nfp_nsp_close(nsp); 200 } 201 202 static void 203 nfp_get_drvinfo(struct nfp_app *app, struct pci_dev *pdev, 204 const char *vnic_version, struct ethtool_drvinfo *drvinfo) 205 { 206 char nsp_version[ETHTOOL_FWVERS_LEN] = {}; 207 208 strscpy(drvinfo->driver, dev_driver_string(&pdev->dev), 209 sizeof(drvinfo->driver)); 210 nfp_net_get_nspinfo(app, nsp_version); 211 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 212 "%s %s %s %s", vnic_version, nsp_version, 213 nfp_app_mip_name(app), nfp_app_name(app)); 214 } 215 216 static void 217 nfp_net_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo) 218 { 219 char vnic_version[ETHTOOL_FWVERS_LEN] = {}; 220 struct nfp_net *nn = netdev_priv(netdev); 221 222 snprintf(vnic_version, sizeof(vnic_version), "%d.%d.%d.%d", 223 nn->fw_ver.extend, nn->fw_ver.class, 224 nn->fw_ver.major, nn->fw_ver.minor); 225 strscpy(drvinfo->bus_info, pci_name(nn->pdev), 226 sizeof(drvinfo->bus_info)); 227 228 nfp_get_drvinfo(nn->app, nn->pdev, vnic_version, drvinfo); 229 } 230 231 static int 232 nfp_net_nway_reset(struct net_device *netdev) 233 { 234 struct nfp_eth_table_port *eth_port; 235 struct nfp_port *port; 236 int err; 237 238 port = nfp_port_from_netdev(netdev); 239 eth_port = nfp_port_get_eth_port(port); 240 if (!eth_port) 241 return -EOPNOTSUPP; 242 243 if (!netif_running(netdev)) 244 return 0; 245 246 err = nfp_eth_set_configured(port->app->cpp, eth_port->index, false); 247 if (err) { 248 netdev_info(netdev, "Link down failed: %d\n", err); 249 return err; 250 } 251 252 err = nfp_eth_set_configured(port->app->cpp, eth_port->index, true); 253 if (err) { 254 netdev_info(netdev, "Link up failed: %d\n", err); 255 return err; 256 } 257 258 netdev_info(netdev, "Link reset succeeded\n"); 259 return 0; 260 } 261 262 static void 263 nfp_app_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo) 264 { 265 struct nfp_app *app = nfp_app_from_netdev(netdev); 266 267 strscpy(drvinfo->bus_info, pci_name(app->pdev), 268 sizeof(drvinfo->bus_info)); 269 nfp_get_drvinfo(app, app->pdev, "*", drvinfo); 270 } 271 272 static void 273 nfp_net_set_fec_link_mode(struct nfp_eth_table_port *eth_port, 274 struct ethtool_link_ksettings *c) 275 { 276 unsigned int modes; 277 278 ethtool_link_ksettings_add_link_mode(c, supported, FEC_NONE); 279 if (!nfp_eth_can_support_fec(eth_port)) { 280 ethtool_link_ksettings_add_link_mode(c, advertising, FEC_NONE); 281 return; 282 } 283 284 modes = nfp_eth_supported_fec_modes(eth_port); 285 if (modes & NFP_FEC_BASER) { 286 ethtool_link_ksettings_add_link_mode(c, supported, FEC_BASER); 287 ethtool_link_ksettings_add_link_mode(c, advertising, FEC_BASER); 288 } 289 290 if (modes & NFP_FEC_REED_SOLOMON) { 291 ethtool_link_ksettings_add_link_mode(c, supported, FEC_RS); 292 ethtool_link_ksettings_add_link_mode(c, advertising, FEC_RS); 293 } 294 } 295 296 static const struct nfp_eth_media_link_mode { 297 u16 ethtool_link_mode; 298 u16 speed; 299 } nfp_eth_media_table[NFP_MEDIA_LINK_MODES_NUMBER] = { 300 [NFP_MEDIA_1000BASE_CX] = { 301 .ethtool_link_mode = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 302 .speed = NFP_SPEED_1G, 303 }, 304 [NFP_MEDIA_1000BASE_KX] = { 305 .ethtool_link_mode = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 306 .speed = NFP_SPEED_1G, 307 }, 308 [NFP_MEDIA_10GBASE_KX4] = { 309 .ethtool_link_mode = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 310 .speed = NFP_SPEED_10G, 311 }, 312 [NFP_MEDIA_10GBASE_KR] = { 313 .ethtool_link_mode = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 314 .speed = NFP_SPEED_10G, 315 }, 316 [NFP_MEDIA_10GBASE_CX4] = { 317 .ethtool_link_mode = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 318 .speed = NFP_SPEED_10G, 319 }, 320 [NFP_MEDIA_10GBASE_CR] = { 321 .ethtool_link_mode = ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, 322 .speed = NFP_SPEED_10G, 323 }, 324 [NFP_MEDIA_10GBASE_SR] = { 325 .ethtool_link_mode = ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, 326 .speed = NFP_SPEED_10G, 327 }, 328 [NFP_MEDIA_10GBASE_ER] = { 329 .ethtool_link_mode = ETHTOOL_LINK_MODE_10000baseER_Full_BIT, 330 .speed = NFP_SPEED_10G, 331 }, 332 [NFP_MEDIA_25GBASE_KR] = { 333 .ethtool_link_mode = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, 334 .speed = NFP_SPEED_25G, 335 }, 336 [NFP_MEDIA_25GBASE_KR_S] = { 337 .ethtool_link_mode = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, 338 .speed = NFP_SPEED_25G, 339 }, 340 [NFP_MEDIA_25GBASE_CR] = { 341 .ethtool_link_mode = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, 342 .speed = NFP_SPEED_25G, 343 }, 344 [NFP_MEDIA_25GBASE_CR_S] = { 345 .ethtool_link_mode = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, 346 .speed = NFP_SPEED_25G, 347 }, 348 [NFP_MEDIA_25GBASE_SR] = { 349 .ethtool_link_mode = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, 350 .speed = NFP_SPEED_25G, 351 }, 352 [NFP_MEDIA_40GBASE_CR4] = { 353 .ethtool_link_mode = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, 354 .speed = NFP_SPEED_40G, 355 }, 356 [NFP_MEDIA_40GBASE_KR4] = { 357 .ethtool_link_mode = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, 358 .speed = NFP_SPEED_40G, 359 }, 360 [NFP_MEDIA_40GBASE_SR4] = { 361 .ethtool_link_mode = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, 362 .speed = NFP_SPEED_40G, 363 }, 364 [NFP_MEDIA_40GBASE_LR4] = { 365 .ethtool_link_mode = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, 366 .speed = NFP_SPEED_40G, 367 }, 368 [NFP_MEDIA_50GBASE_KR] = { 369 .ethtool_link_mode = ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, 370 .speed = NFP_SPEED_50G, 371 }, 372 [NFP_MEDIA_50GBASE_SR] = { 373 .ethtool_link_mode = ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, 374 .speed = NFP_SPEED_50G, 375 }, 376 [NFP_MEDIA_50GBASE_CR] = { 377 .ethtool_link_mode = ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, 378 .speed = NFP_SPEED_50G, 379 }, 380 [NFP_MEDIA_50GBASE_LR] = { 381 .ethtool_link_mode = ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT, 382 .speed = NFP_SPEED_50G, 383 }, 384 [NFP_MEDIA_50GBASE_ER] = { 385 .ethtool_link_mode = ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT, 386 .speed = NFP_SPEED_50G, 387 }, 388 [NFP_MEDIA_50GBASE_FR] = { 389 .ethtool_link_mode = ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT, 390 .speed = NFP_SPEED_50G, 391 }, 392 [NFP_MEDIA_100GBASE_KR4] = { 393 .ethtool_link_mode = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, 394 .speed = NFP_SPEED_100G, 395 }, 396 [NFP_MEDIA_100GBASE_SR4] = { 397 .ethtool_link_mode = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, 398 .speed = NFP_SPEED_100G, 399 }, 400 [NFP_MEDIA_100GBASE_CR4] = { 401 .ethtool_link_mode = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, 402 .speed = NFP_SPEED_100G, 403 }, 404 [NFP_MEDIA_100GBASE_KP4] = { 405 .ethtool_link_mode = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, 406 .speed = NFP_SPEED_100G, 407 }, 408 [NFP_MEDIA_100GBASE_CR10] = { 409 .ethtool_link_mode = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, 410 .speed = NFP_SPEED_100G, 411 }, 412 }; 413 414 static const unsigned int nfp_eth_speed_map[NFP_SUP_SPEED_NUMBER] = { 415 [NFP_SPEED_1G] = SPEED_1000, 416 [NFP_SPEED_10G] = SPEED_10000, 417 [NFP_SPEED_25G] = SPEED_25000, 418 [NFP_SPEED_40G] = SPEED_40000, 419 [NFP_SPEED_50G] = SPEED_50000, 420 [NFP_SPEED_100G] = SPEED_100000, 421 }; 422 423 static void nfp_add_media_link_mode(struct nfp_port *port, 424 struct nfp_eth_table_port *eth_port, 425 struct ethtool_link_ksettings *cmd) 426 { 427 u64 supported_modes[2], advertised_modes[2]; 428 struct nfp_eth_media_buf ethm = { 429 .eth_index = eth_port->eth_index, 430 }; 431 struct nfp_cpp *cpp = port->app->cpp; 432 433 if (nfp_eth_read_media(cpp, ðm)) { 434 bitmap_fill(port->speed_bitmap, NFP_SUP_SPEED_NUMBER); 435 return; 436 } 437 438 bitmap_zero(port->speed_bitmap, NFP_SUP_SPEED_NUMBER); 439 440 for (u32 i = 0; i < 2; i++) { 441 supported_modes[i] = le64_to_cpu(ethm.supported_modes[i]); 442 advertised_modes[i] = le64_to_cpu(ethm.advertised_modes[i]); 443 } 444 445 for (u32 i = 0; i < NFP_MEDIA_LINK_MODES_NUMBER; i++) { 446 if (i < 64) { 447 if (supported_modes[0] & BIT_ULL(i)) { 448 __set_bit(nfp_eth_media_table[i].ethtool_link_mode, 449 cmd->link_modes.supported); 450 __set_bit(nfp_eth_media_table[i].speed, 451 port->speed_bitmap); 452 } 453 454 if (advertised_modes[0] & BIT_ULL(i)) 455 __set_bit(nfp_eth_media_table[i].ethtool_link_mode, 456 cmd->link_modes.advertising); 457 } else { 458 if (supported_modes[1] & BIT_ULL(i - 64)) { 459 __set_bit(nfp_eth_media_table[i].ethtool_link_mode, 460 cmd->link_modes.supported); 461 __set_bit(nfp_eth_media_table[i].speed, 462 port->speed_bitmap); 463 } 464 465 if (advertised_modes[1] & BIT_ULL(i - 64)) 466 __set_bit(nfp_eth_media_table[i].ethtool_link_mode, 467 cmd->link_modes.advertising); 468 } 469 } 470 } 471 472 /** 473 * nfp_net_get_link_ksettings - Get Link Speed settings 474 * @netdev: network interface device structure 475 * @cmd: ethtool command 476 * 477 * Reports speed settings based on info in the BAR provided by the fw. 478 */ 479 static int 480 nfp_net_get_link_ksettings(struct net_device *netdev, 481 struct ethtool_link_ksettings *cmd) 482 { 483 struct nfp_eth_table_port *eth_port; 484 struct nfp_port *port; 485 struct nfp_net *nn; 486 unsigned int speed; 487 u16 sts; 488 489 /* Init to unknowns */ 490 ethtool_link_ksettings_zero_link_mode(cmd, supported); 491 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 492 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE); 493 cmd->base.port = PORT_OTHER; 494 cmd->base.speed = SPEED_UNKNOWN; 495 cmd->base.duplex = DUPLEX_UNKNOWN; 496 497 port = nfp_port_from_netdev(netdev); 498 eth_port = nfp_port_get_eth_port(port); 499 if (eth_port) { 500 ethtool_link_ksettings_add_link_mode(cmd, supported, Pause); 501 ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause); 502 nfp_add_media_link_mode(port, eth_port, cmd); 503 if (eth_port->supp_aneg) { 504 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg); 505 if (eth_port->aneg == NFP_ANEG_AUTO) { 506 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg); 507 cmd->base.autoneg = AUTONEG_ENABLE; 508 } 509 } 510 nfp_net_set_fec_link_mode(eth_port, cmd); 511 } 512 513 if (!netif_carrier_ok(netdev)) 514 return 0; 515 516 /* Use link speed from ETH table if available, otherwise try the BAR */ 517 if (eth_port) { 518 cmd->base.port = eth_port->port_type; 519 cmd->base.speed = eth_port->speed; 520 cmd->base.duplex = DUPLEX_FULL; 521 return 0; 522 } 523 524 if (!nfp_netdev_is_nfp_net(netdev)) 525 return -EOPNOTSUPP; 526 nn = netdev_priv(netdev); 527 528 sts = nn_readw(nn, NFP_NET_CFG_STS); 529 speed = nfp_net_lr2speed(FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts)); 530 if (!speed) 531 return -EOPNOTSUPP; 532 533 if (speed != SPEED_UNKNOWN) { 534 cmd->base.speed = speed; 535 cmd->base.duplex = DUPLEX_FULL; 536 } 537 538 return 0; 539 } 540 541 static int 542 nfp_net_set_link_ksettings(struct net_device *netdev, 543 const struct ethtool_link_ksettings *cmd) 544 { 545 bool req_aneg = (cmd->base.autoneg == AUTONEG_ENABLE); 546 struct nfp_eth_table_port *eth_port; 547 struct nfp_port *port; 548 struct nfp_nsp *nsp; 549 int err; 550 551 port = nfp_port_from_netdev(netdev); 552 eth_port = __nfp_port_get_eth_port(port); 553 if (!eth_port) 554 return -EOPNOTSUPP; 555 556 if (netif_running(netdev)) { 557 netdev_warn(netdev, "Changing settings not allowed on an active interface. It may cause the port to be disabled until driver reload.\n"); 558 return -EBUSY; 559 } 560 561 nsp = nfp_eth_config_start(port->app->cpp, eth_port->index); 562 if (IS_ERR(nsp)) 563 return PTR_ERR(nsp); 564 565 if (req_aneg && !eth_port->supp_aneg) { 566 netdev_warn(netdev, "Autoneg is not supported.\n"); 567 err = -EOPNOTSUPP; 568 goto err_bad_set; 569 } 570 571 err = __nfp_eth_set_aneg(nsp, req_aneg ? NFP_ANEG_AUTO : NFP_ANEG_DISABLED); 572 if (err) 573 goto err_bad_set; 574 575 if (cmd->base.speed != SPEED_UNKNOWN) { 576 u32 speed = cmd->base.speed / eth_port->lanes; 577 bool is_supported = false; 578 579 for (u32 i = 0; i < NFP_SUP_SPEED_NUMBER; i++) { 580 if (cmd->base.speed == nfp_eth_speed_map[i] && 581 test_bit(i, port->speed_bitmap)) { 582 is_supported = true; 583 break; 584 } 585 } 586 587 if (!is_supported) { 588 netdev_err(netdev, "Speed %u is not supported.\n", 589 cmd->base.speed); 590 err = -EINVAL; 591 goto err_bad_set; 592 } 593 594 if (req_aneg) { 595 netdev_err(netdev, "Speed changing is not allowed when working on autoneg mode.\n"); 596 err = -EINVAL; 597 goto err_bad_set; 598 } 599 600 err = __nfp_eth_set_speed(nsp, speed); 601 if (err) 602 goto err_bad_set; 603 } 604 605 err = nfp_eth_config_commit_end(nsp); 606 if (err > 0) 607 return 0; /* no change */ 608 609 nfp_net_refresh_port_table(port); 610 611 return err; 612 613 err_bad_set: 614 nfp_eth_config_cleanup_end(nsp); 615 return err; 616 } 617 618 static void nfp_net_get_ringparam(struct net_device *netdev, 619 struct ethtool_ringparam *ring, 620 struct kernel_ethtool_ringparam *kernel_ring, 621 struct netlink_ext_ack *extack) 622 { 623 struct nfp_net *nn = netdev_priv(netdev); 624 u32 qc_max = nn->dev_info->max_qc_size; 625 626 ring->rx_max_pending = qc_max; 627 ring->tx_max_pending = qc_max / nn->dp.ops->tx_min_desc_per_pkt; 628 ring->rx_pending = nn->dp.rxd_cnt; 629 ring->tx_pending = nn->dp.txd_cnt; 630 } 631 632 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt) 633 { 634 struct nfp_net_dp *dp; 635 636 dp = nfp_net_clone_dp(nn); 637 if (!dp) 638 return -ENOMEM; 639 640 dp->rxd_cnt = rxd_cnt; 641 dp->txd_cnt = txd_cnt; 642 643 return nfp_net_ring_reconfig(nn, dp, NULL); 644 } 645 646 static int nfp_net_set_ringparam(struct net_device *netdev, 647 struct ethtool_ringparam *ring, 648 struct kernel_ethtool_ringparam *kernel_ring, 649 struct netlink_ext_ack *extack) 650 { 651 u32 tx_dpp, qc_min, qc_max, rxd_cnt, txd_cnt; 652 struct nfp_net *nn = netdev_priv(netdev); 653 654 /* We don't have separate queues/rings for small/large frames. */ 655 if (ring->rx_mini_pending || ring->rx_jumbo_pending) 656 return -EINVAL; 657 658 qc_min = nn->dev_info->min_qc_size; 659 qc_max = nn->dev_info->max_qc_size; 660 tx_dpp = nn->dp.ops->tx_min_desc_per_pkt; 661 /* Round up to supported values */ 662 rxd_cnt = roundup_pow_of_two(ring->rx_pending); 663 txd_cnt = roundup_pow_of_two(ring->tx_pending); 664 665 if (rxd_cnt < qc_min || rxd_cnt > qc_max || 666 txd_cnt < qc_min / tx_dpp || txd_cnt > qc_max / tx_dpp) 667 return -EINVAL; 668 669 if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt) 670 return 0; 671 672 nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n", 673 nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt); 674 675 return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt); 676 } 677 678 static int nfp_test_link(struct net_device *netdev) 679 { 680 if (!netif_carrier_ok(netdev) || !(netdev->flags & IFF_UP)) 681 return 1; 682 683 return 0; 684 } 685 686 static int nfp_test_nsp(struct net_device *netdev) 687 { 688 struct nfp_app *app = nfp_app_from_netdev(netdev); 689 struct nfp_nsp_identify *nspi; 690 struct nfp_nsp *nsp; 691 int err; 692 693 nsp = nfp_nsp_open(app->cpp); 694 if (IS_ERR(nsp)) { 695 err = PTR_ERR(nsp); 696 netdev_info(netdev, "NSP Test: failed to access the NSP: %d\n", err); 697 goto exit; 698 } 699 700 if (nfp_nsp_get_abi_ver_minor(nsp) < 15) { 701 err = -EOPNOTSUPP; 702 goto exit_close_nsp; 703 } 704 705 nspi = kzalloc(sizeof(*nspi), GFP_KERNEL); 706 if (!nspi) { 707 err = -ENOMEM; 708 goto exit_close_nsp; 709 } 710 711 err = nfp_nsp_read_identify(nsp, nspi, sizeof(*nspi)); 712 if (err < 0) 713 netdev_info(netdev, "NSP Test: reading bsp version failed %d\n", err); 714 715 kfree(nspi); 716 exit_close_nsp: 717 nfp_nsp_close(nsp); 718 exit: 719 return err; 720 } 721 722 static int nfp_test_fw(struct net_device *netdev) 723 { 724 struct nfp_net *nn = netdev_priv(netdev); 725 int err; 726 727 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN); 728 if (err) 729 netdev_info(netdev, "FW Test: update failed %d\n", err); 730 731 return err; 732 } 733 734 static int nfp_test_reg(struct net_device *netdev) 735 { 736 struct nfp_app *app = nfp_app_from_netdev(netdev); 737 struct nfp_cpp *cpp = app->cpp; 738 u32 model = nfp_cpp_model(cpp); 739 u32 value; 740 int err; 741 742 err = nfp_cpp_model_autodetect(cpp, &value); 743 if (err < 0) { 744 netdev_info(netdev, "REG Test: NFP model detection failed %d\n", err); 745 return err; 746 } 747 748 return (value == model) ? 0 : 1; 749 } 750 751 static bool link_test_supported(struct net_device *netdev) 752 { 753 return true; 754 } 755 756 static bool nsp_test_supported(struct net_device *netdev) 757 { 758 if (nfp_app_from_netdev(netdev)) 759 return true; 760 761 return false; 762 } 763 764 static bool fw_test_supported(struct net_device *netdev) 765 { 766 if (nfp_netdev_is_nfp_net(netdev)) 767 return true; 768 769 return false; 770 } 771 772 static bool reg_test_supported(struct net_device *netdev) 773 { 774 if (nfp_app_from_netdev(netdev)) 775 return true; 776 777 return false; 778 } 779 780 static struct nfp_self_test_item { 781 char name[ETH_GSTRING_LEN]; 782 bool (*is_supported)(struct net_device *dev); 783 int (*func)(struct net_device *dev); 784 } nfp_self_test[] = { 785 {"Link Test", link_test_supported, nfp_test_link}, 786 {"NSP Test", nsp_test_supported, nfp_test_nsp}, 787 {"Firmware Test", fw_test_supported, nfp_test_fw}, 788 {"Register Test", reg_test_supported, nfp_test_reg} 789 }; 790 791 #define NFP_TEST_TOTAL_NUM ARRAY_SIZE(nfp_self_test) 792 793 static void nfp_get_self_test_strings(struct net_device *netdev, u8 *data) 794 { 795 int i; 796 797 for (i = 0; i < NFP_TEST_TOTAL_NUM; i++) 798 if (nfp_self_test[i].is_supported(netdev)) 799 ethtool_sprintf(&data, nfp_self_test[i].name); 800 } 801 802 static int nfp_get_self_test_count(struct net_device *netdev) 803 { 804 int i, count = 0; 805 806 for (i = 0; i < NFP_TEST_TOTAL_NUM; i++) 807 if (nfp_self_test[i].is_supported(netdev)) 808 count++; 809 810 return count; 811 } 812 813 static void nfp_net_self_test(struct net_device *netdev, struct ethtool_test *eth_test, 814 u64 *data) 815 { 816 int i, ret, count = 0; 817 818 netdev_info(netdev, "Start self test\n"); 819 820 for (i = 0; i < NFP_TEST_TOTAL_NUM; i++) { 821 if (nfp_self_test[i].is_supported(netdev)) { 822 ret = nfp_self_test[i].func(netdev); 823 if (ret) 824 eth_test->flags |= ETH_TEST_FL_FAILED; 825 data[count++] = ret; 826 } 827 } 828 829 netdev_info(netdev, "Test end\n"); 830 } 831 832 static unsigned int nfp_vnic_get_sw_stats_count(struct net_device *netdev) 833 { 834 struct nfp_net *nn = netdev_priv(netdev); 835 836 return NN_RVEC_GATHER_STATS + nn->max_r_vecs * NN_RVEC_PER_Q_STATS + 837 NN_CTRL_PATH_STATS; 838 } 839 840 static u8 *nfp_vnic_get_sw_stats_strings(struct net_device *netdev, u8 *data) 841 { 842 struct nfp_net *nn = netdev_priv(netdev); 843 int i; 844 845 for (i = 0; i < nn->max_r_vecs; i++) { 846 ethtool_sprintf(&data, "rvec_%u_rx_pkts", i); 847 ethtool_sprintf(&data, "rvec_%u_tx_pkts", i); 848 ethtool_sprintf(&data, "rvec_%u_tx_busy", i); 849 } 850 851 ethtool_sprintf(&data, "hw_rx_csum_ok"); 852 ethtool_sprintf(&data, "hw_rx_csum_inner_ok"); 853 ethtool_sprintf(&data, "hw_rx_csum_complete"); 854 ethtool_sprintf(&data, "hw_rx_csum_err"); 855 ethtool_sprintf(&data, "rx_replace_buf_alloc_fail"); 856 ethtool_sprintf(&data, "rx_tls_decrypted_packets"); 857 ethtool_sprintf(&data, "hw_tx_csum"); 858 ethtool_sprintf(&data, "hw_tx_inner_csum"); 859 ethtool_sprintf(&data, "tx_gather"); 860 ethtool_sprintf(&data, "tx_lso"); 861 ethtool_sprintf(&data, "tx_tls_encrypted_packets"); 862 ethtool_sprintf(&data, "tx_tls_ooo"); 863 ethtool_sprintf(&data, "tx_tls_drop_no_sync_data"); 864 865 ethtool_sprintf(&data, "hw_tls_no_space"); 866 ethtool_sprintf(&data, "rx_tls_resync_req_ok"); 867 ethtool_sprintf(&data, "rx_tls_resync_req_ign"); 868 ethtool_sprintf(&data, "rx_tls_resync_sent"); 869 870 return data; 871 } 872 873 static u64 *nfp_vnic_get_sw_stats(struct net_device *netdev, u64 *data) 874 { 875 u64 gathered_stats[NN_RVEC_GATHER_STATS] = {}; 876 struct nfp_net *nn = netdev_priv(netdev); 877 u64 tmp[NN_RVEC_GATHER_STATS]; 878 unsigned int i, j; 879 880 for (i = 0; i < nn->max_r_vecs; i++) { 881 unsigned int start; 882 883 do { 884 start = u64_stats_fetch_begin(&nn->r_vecs[i].rx_sync); 885 data[0] = nn->r_vecs[i].rx_pkts; 886 tmp[0] = nn->r_vecs[i].hw_csum_rx_ok; 887 tmp[1] = nn->r_vecs[i].hw_csum_rx_inner_ok; 888 tmp[2] = nn->r_vecs[i].hw_csum_rx_complete; 889 tmp[3] = nn->r_vecs[i].hw_csum_rx_error; 890 tmp[4] = nn->r_vecs[i].rx_replace_buf_alloc_fail; 891 tmp[5] = nn->r_vecs[i].hw_tls_rx; 892 } while (u64_stats_fetch_retry(&nn->r_vecs[i].rx_sync, start)); 893 894 do { 895 start = u64_stats_fetch_begin(&nn->r_vecs[i].tx_sync); 896 data[1] = nn->r_vecs[i].tx_pkts; 897 data[2] = nn->r_vecs[i].tx_busy; 898 tmp[6] = nn->r_vecs[i].hw_csum_tx; 899 tmp[7] = nn->r_vecs[i].hw_csum_tx_inner; 900 tmp[8] = nn->r_vecs[i].tx_gather; 901 tmp[9] = nn->r_vecs[i].tx_lso; 902 tmp[10] = nn->r_vecs[i].hw_tls_tx; 903 tmp[11] = nn->r_vecs[i].tls_tx_fallback; 904 tmp[12] = nn->r_vecs[i].tls_tx_no_fallback; 905 } while (u64_stats_fetch_retry(&nn->r_vecs[i].tx_sync, start)); 906 907 data += NN_RVEC_PER_Q_STATS; 908 909 for (j = 0; j < NN_RVEC_GATHER_STATS; j++) 910 gathered_stats[j] += tmp[j]; 911 } 912 913 for (j = 0; j < NN_RVEC_GATHER_STATS; j++) 914 *data++ = gathered_stats[j]; 915 916 *data++ = atomic_read(&nn->ktls_no_space); 917 *data++ = atomic_read(&nn->ktls_rx_resync_req); 918 *data++ = atomic_read(&nn->ktls_rx_resync_ign); 919 *data++ = atomic_read(&nn->ktls_rx_resync_sent); 920 921 return data; 922 } 923 924 static unsigned int nfp_vnic_get_hw_stats_count(unsigned int num_vecs) 925 { 926 return NN_ET_GLOBAL_STATS_LEN + num_vecs * 4; 927 } 928 929 static u8 * 930 nfp_vnic_get_hw_stats_strings(u8 *data, unsigned int num_vecs, bool repr) 931 { 932 int swap_off, i; 933 934 BUILD_BUG_ON(NN_ET_GLOBAL_STATS_LEN < NN_ET_SWITCH_STATS_LEN * 2); 935 /* If repr is true first add SWITCH_STATS_LEN and then subtract it 936 * effectively swapping the RX and TX statistics (giving us the RX 937 * and TX from perspective of the switch). 938 */ 939 swap_off = repr * NN_ET_SWITCH_STATS_LEN; 940 941 for (i = 0; i < NN_ET_SWITCH_STATS_LEN; i++) 942 ethtool_sprintf(&data, nfp_net_et_stats[i + swap_off].name); 943 944 for (i = NN_ET_SWITCH_STATS_LEN; i < NN_ET_SWITCH_STATS_LEN * 2; i++) 945 ethtool_sprintf(&data, nfp_net_et_stats[i - swap_off].name); 946 947 for (i = NN_ET_SWITCH_STATS_LEN * 2; i < NN_ET_GLOBAL_STATS_LEN; i++) 948 ethtool_sprintf(&data, nfp_net_et_stats[i].name); 949 950 for (i = 0; i < num_vecs; i++) { 951 ethtool_sprintf(&data, "rxq_%u_pkts", i); 952 ethtool_sprintf(&data, "rxq_%u_bytes", i); 953 ethtool_sprintf(&data, "txq_%u_pkts", i); 954 ethtool_sprintf(&data, "txq_%u_bytes", i); 955 } 956 957 return data; 958 } 959 960 static u64 * 961 nfp_vnic_get_hw_stats(u64 *data, u8 __iomem *mem, unsigned int num_vecs) 962 { 963 unsigned int i; 964 965 for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) 966 *data++ = readq(mem + nfp_net_et_stats[i].off); 967 968 for (i = 0; i < num_vecs; i++) { 969 *data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i)); 970 *data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8); 971 *data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i)); 972 *data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8); 973 } 974 975 return data; 976 } 977 978 static unsigned int nfp_vnic_get_tlv_stats_count(struct nfp_net *nn) 979 { 980 return nn->tlv_caps.vnic_stats_cnt + nn->max_r_vecs * 4; 981 } 982 983 static u8 *nfp_vnic_get_tlv_stats_strings(struct nfp_net *nn, u8 *data) 984 { 985 unsigned int i, id; 986 u8 __iomem *mem; 987 u64 id_word = 0; 988 989 mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off; 990 for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++) { 991 if (!(i % 4)) 992 id_word = readq(mem + i * 2); 993 994 id = (u16)id_word; 995 id_word >>= 16; 996 997 if (id < ARRAY_SIZE(nfp_tlv_stat_names) && 998 nfp_tlv_stat_names[id][0]) { 999 memcpy(data, nfp_tlv_stat_names[id], ETH_GSTRING_LEN); 1000 data += ETH_GSTRING_LEN; 1001 } else { 1002 ethtool_sprintf(&data, "dev_unknown_stat%u", id); 1003 } 1004 } 1005 1006 for (i = 0; i < nn->max_r_vecs; i++) { 1007 ethtool_sprintf(&data, "rxq_%u_pkts", i); 1008 ethtool_sprintf(&data, "rxq_%u_bytes", i); 1009 ethtool_sprintf(&data, "txq_%u_pkts", i); 1010 ethtool_sprintf(&data, "txq_%u_bytes", i); 1011 } 1012 1013 return data; 1014 } 1015 1016 static u64 *nfp_vnic_get_tlv_stats(struct nfp_net *nn, u64 *data) 1017 { 1018 u8 __iomem *mem; 1019 unsigned int i; 1020 1021 mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off; 1022 mem += roundup(2 * nn->tlv_caps.vnic_stats_cnt, 8); 1023 for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++) 1024 *data++ = readq(mem + i * 8); 1025 1026 mem = nn->dp.ctrl_bar; 1027 for (i = 0; i < nn->max_r_vecs; i++) { 1028 *data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i)); 1029 *data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8); 1030 *data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i)); 1031 *data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8); 1032 } 1033 1034 return data; 1035 } 1036 1037 static unsigned int nfp_mac_get_stats_count(struct net_device *netdev) 1038 { 1039 struct nfp_port *port; 1040 1041 port = nfp_port_from_netdev(netdev); 1042 if (!__nfp_port_get_eth_port(port) || !port->eth_stats) 1043 return 0; 1044 1045 return ARRAY_SIZE(nfp_mac_et_stats); 1046 } 1047 1048 static u8 *nfp_mac_get_stats_strings(struct net_device *netdev, u8 *data) 1049 { 1050 struct nfp_port *port; 1051 unsigned int i; 1052 1053 port = nfp_port_from_netdev(netdev); 1054 if (!__nfp_port_get_eth_port(port) || !port->eth_stats) 1055 return data; 1056 1057 for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++) 1058 ethtool_sprintf(&data, "mac.%s", nfp_mac_et_stats[i].name); 1059 1060 return data; 1061 } 1062 1063 static u64 *nfp_mac_get_stats(struct net_device *netdev, u64 *data) 1064 { 1065 struct nfp_port *port; 1066 unsigned int i; 1067 1068 port = nfp_port_from_netdev(netdev); 1069 if (!__nfp_port_get_eth_port(port) || !port->eth_stats) 1070 return data; 1071 1072 for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++) 1073 *data++ = readq(port->eth_stats + nfp_mac_et_stats[i].off); 1074 1075 return data; 1076 } 1077 1078 static void nfp_net_get_strings(struct net_device *netdev, 1079 u32 stringset, u8 *data) 1080 { 1081 struct nfp_net *nn = netdev_priv(netdev); 1082 1083 switch (stringset) { 1084 case ETH_SS_STATS: 1085 data = nfp_vnic_get_sw_stats_strings(netdev, data); 1086 if (!nn->tlv_caps.vnic_stats_off) 1087 data = nfp_vnic_get_hw_stats_strings(data, 1088 nn->max_r_vecs, 1089 false); 1090 else 1091 data = nfp_vnic_get_tlv_stats_strings(nn, data); 1092 data = nfp_mac_get_stats_strings(netdev, data); 1093 data = nfp_app_port_get_stats_strings(nn->port, data); 1094 break; 1095 case ETH_SS_TEST: 1096 nfp_get_self_test_strings(netdev, data); 1097 break; 1098 } 1099 } 1100 1101 static void 1102 nfp_net_get_stats(struct net_device *netdev, struct ethtool_stats *stats, 1103 u64 *data) 1104 { 1105 struct nfp_net *nn = netdev_priv(netdev); 1106 1107 data = nfp_vnic_get_sw_stats(netdev, data); 1108 if (!nn->tlv_caps.vnic_stats_off) 1109 data = nfp_vnic_get_hw_stats(data, nn->dp.ctrl_bar, 1110 nn->max_r_vecs); 1111 else 1112 data = nfp_vnic_get_tlv_stats(nn, data); 1113 data = nfp_mac_get_stats(netdev, data); 1114 data = nfp_app_port_get_stats(nn->port, data); 1115 } 1116 1117 static int nfp_net_get_sset_count(struct net_device *netdev, int sset) 1118 { 1119 struct nfp_net *nn = netdev_priv(netdev); 1120 unsigned int cnt; 1121 1122 switch (sset) { 1123 case ETH_SS_STATS: 1124 cnt = nfp_vnic_get_sw_stats_count(netdev); 1125 if (!nn->tlv_caps.vnic_stats_off) 1126 cnt += nfp_vnic_get_hw_stats_count(nn->max_r_vecs); 1127 else 1128 cnt += nfp_vnic_get_tlv_stats_count(nn); 1129 cnt += nfp_mac_get_stats_count(netdev); 1130 cnt += nfp_app_port_get_stats_count(nn->port); 1131 return cnt; 1132 case ETH_SS_TEST: 1133 return nfp_get_self_test_count(netdev); 1134 default: 1135 return -EOPNOTSUPP; 1136 } 1137 } 1138 1139 static void nfp_port_get_strings(struct net_device *netdev, 1140 u32 stringset, u8 *data) 1141 { 1142 struct nfp_port *port = nfp_port_from_netdev(netdev); 1143 1144 switch (stringset) { 1145 case ETH_SS_STATS: 1146 if (nfp_port_is_vnic(port)) 1147 data = nfp_vnic_get_hw_stats_strings(data, 0, true); 1148 else 1149 data = nfp_mac_get_stats_strings(netdev, data); 1150 data = nfp_app_port_get_stats_strings(port, data); 1151 break; 1152 case ETH_SS_TEST: 1153 nfp_get_self_test_strings(netdev, data); 1154 break; 1155 } 1156 } 1157 1158 static void 1159 nfp_port_get_stats(struct net_device *netdev, struct ethtool_stats *stats, 1160 u64 *data) 1161 { 1162 struct nfp_port *port = nfp_port_from_netdev(netdev); 1163 1164 if (nfp_port_is_vnic(port)) 1165 data = nfp_vnic_get_hw_stats(data, port->vnic, 0); 1166 else 1167 data = nfp_mac_get_stats(netdev, data); 1168 data = nfp_app_port_get_stats(port, data); 1169 } 1170 1171 static int nfp_port_get_sset_count(struct net_device *netdev, int sset) 1172 { 1173 struct nfp_port *port = nfp_port_from_netdev(netdev); 1174 unsigned int count; 1175 1176 switch (sset) { 1177 case ETH_SS_STATS: 1178 if (nfp_port_is_vnic(port)) 1179 count = nfp_vnic_get_hw_stats_count(0); 1180 else 1181 count = nfp_mac_get_stats_count(netdev); 1182 count += nfp_app_port_get_stats_count(port); 1183 return count; 1184 case ETH_SS_TEST: 1185 return nfp_get_self_test_count(netdev); 1186 default: 1187 return -EOPNOTSUPP; 1188 } 1189 } 1190 1191 static int nfp_port_fec_ethtool_to_nsp(u32 fec) 1192 { 1193 switch (fec) { 1194 case ETHTOOL_FEC_AUTO: 1195 return NFP_FEC_AUTO_BIT; 1196 case ETHTOOL_FEC_OFF: 1197 return NFP_FEC_DISABLED_BIT; 1198 case ETHTOOL_FEC_RS: 1199 return NFP_FEC_REED_SOLOMON_BIT; 1200 case ETHTOOL_FEC_BASER: 1201 return NFP_FEC_BASER_BIT; 1202 default: 1203 /* NSP only supports a single mode at a time */ 1204 return -EOPNOTSUPP; 1205 } 1206 } 1207 1208 static u32 nfp_port_fec_nsp_to_ethtool(u32 fec) 1209 { 1210 u32 result = 0; 1211 1212 if (fec & NFP_FEC_AUTO) 1213 result |= ETHTOOL_FEC_AUTO; 1214 if (fec & NFP_FEC_BASER) 1215 result |= ETHTOOL_FEC_BASER; 1216 if (fec & NFP_FEC_REED_SOLOMON) 1217 result |= ETHTOOL_FEC_RS; 1218 if (fec & NFP_FEC_DISABLED) 1219 result |= ETHTOOL_FEC_OFF; 1220 1221 return result ?: ETHTOOL_FEC_NONE; 1222 } 1223 1224 static int 1225 nfp_port_get_fecparam(struct net_device *netdev, 1226 struct ethtool_fecparam *param) 1227 { 1228 struct nfp_eth_table_port *eth_port; 1229 struct nfp_port *port; 1230 1231 param->active_fec = ETHTOOL_FEC_NONE; 1232 param->fec = ETHTOOL_FEC_NONE; 1233 1234 port = nfp_port_from_netdev(netdev); 1235 eth_port = nfp_port_get_eth_port(port); 1236 if (!eth_port) 1237 return -EOPNOTSUPP; 1238 1239 if (!nfp_eth_can_support_fec(eth_port)) 1240 return 0; 1241 1242 param->fec = nfp_port_fec_nsp_to_ethtool(eth_port->fec_modes_supported); 1243 param->active_fec = nfp_port_fec_nsp_to_ethtool(BIT(eth_port->act_fec)); 1244 1245 return 0; 1246 } 1247 1248 static int 1249 nfp_port_set_fecparam(struct net_device *netdev, 1250 struct ethtool_fecparam *param) 1251 { 1252 struct nfp_eth_table_port *eth_port; 1253 struct nfp_port *port; 1254 int err, fec; 1255 1256 port = nfp_port_from_netdev(netdev); 1257 eth_port = nfp_port_get_eth_port(port); 1258 if (!eth_port) 1259 return -EOPNOTSUPP; 1260 1261 if (!nfp_eth_can_support_fec(eth_port)) 1262 return -EOPNOTSUPP; 1263 1264 fec = nfp_port_fec_ethtool_to_nsp(param->fec); 1265 if (fec < 0) 1266 return fec; 1267 1268 err = nfp_eth_set_fec(port->app->cpp, eth_port->index, fec); 1269 if (!err) 1270 /* Only refresh if we did something */ 1271 nfp_net_refresh_port_table(port); 1272 1273 return err < 0 ? err : 0; 1274 } 1275 1276 /* RX network flow classification (RSS, filters, etc) 1277 */ 1278 static u32 ethtool_flow_to_nfp_flag(u32 flow_type) 1279 { 1280 static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = { 1281 [TCP_V4_FLOW] = NFP_NET_CFG_RSS_IPV4_TCP, 1282 [TCP_V6_FLOW] = NFP_NET_CFG_RSS_IPV6_TCP, 1283 [UDP_V4_FLOW] = NFP_NET_CFG_RSS_IPV4_UDP, 1284 [UDP_V6_FLOW] = NFP_NET_CFG_RSS_IPV6_UDP, 1285 [IPV4_FLOW] = NFP_NET_CFG_RSS_IPV4, 1286 [IPV6_FLOW] = NFP_NET_CFG_RSS_IPV6, 1287 }; 1288 1289 if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp)) 1290 return 0; 1291 1292 return xlate_ethtool_to_nfp[flow_type]; 1293 } 1294 1295 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn, 1296 struct ethtool_rxnfc *cmd) 1297 { 1298 u32 nfp_rss_flag; 1299 1300 cmd->data = 0; 1301 1302 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)) 1303 return -EOPNOTSUPP; 1304 1305 nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type); 1306 if (!nfp_rss_flag) 1307 return -EINVAL; 1308 1309 cmd->data |= RXH_IP_SRC | RXH_IP_DST; 1310 if (nn->rss_cfg & nfp_rss_flag) 1311 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; 1312 1313 return 0; 1314 } 1315 1316 static int nfp_net_get_rxnfc(struct net_device *netdev, 1317 struct ethtool_rxnfc *cmd, u32 *rule_locs) 1318 { 1319 struct nfp_net *nn = netdev_priv(netdev); 1320 1321 switch (cmd->cmd) { 1322 case ETHTOOL_GRXRINGS: 1323 cmd->data = nn->dp.num_rx_rings; 1324 return 0; 1325 case ETHTOOL_GRXFH: 1326 return nfp_net_get_rss_hash_opts(nn, cmd); 1327 default: 1328 return -EOPNOTSUPP; 1329 } 1330 } 1331 1332 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn, 1333 struct ethtool_rxnfc *nfc) 1334 { 1335 u32 new_rss_cfg = nn->rss_cfg; 1336 u32 nfp_rss_flag; 1337 int err; 1338 1339 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)) 1340 return -EOPNOTSUPP; 1341 1342 /* RSS only supports IP SA/DA and L4 src/dst ports */ 1343 if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST | 1344 RXH_L4_B_0_1 | RXH_L4_B_2_3)) 1345 return -EINVAL; 1346 1347 /* We need at least the IP SA/DA fields for hashing */ 1348 if (!(nfc->data & RXH_IP_SRC) || 1349 !(nfc->data & RXH_IP_DST)) 1350 return -EINVAL; 1351 1352 nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type); 1353 if (!nfp_rss_flag) 1354 return -EINVAL; 1355 1356 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) { 1357 case 0: 1358 new_rss_cfg &= ~nfp_rss_flag; 1359 break; 1360 case (RXH_L4_B_0_1 | RXH_L4_B_2_3): 1361 new_rss_cfg |= nfp_rss_flag; 1362 break; 1363 default: 1364 return -EINVAL; 1365 } 1366 1367 new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc); 1368 new_rss_cfg |= NFP_NET_CFG_RSS_MASK; 1369 1370 if (new_rss_cfg == nn->rss_cfg) 1371 return 0; 1372 1373 writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL); 1374 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS); 1375 if (err) 1376 return err; 1377 1378 nn->rss_cfg = new_rss_cfg; 1379 1380 nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg); 1381 return 0; 1382 } 1383 1384 static int nfp_net_set_rxnfc(struct net_device *netdev, 1385 struct ethtool_rxnfc *cmd) 1386 { 1387 struct nfp_net *nn = netdev_priv(netdev); 1388 1389 switch (cmd->cmd) { 1390 case ETHTOOL_SRXFH: 1391 return nfp_net_set_rss_hash_opt(nn, cmd); 1392 default: 1393 return -EOPNOTSUPP; 1394 } 1395 } 1396 1397 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev) 1398 { 1399 struct nfp_net *nn = netdev_priv(netdev); 1400 1401 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)) 1402 return 0; 1403 1404 return ARRAY_SIZE(nn->rss_itbl); 1405 } 1406 1407 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev) 1408 { 1409 struct nfp_net *nn = netdev_priv(netdev); 1410 1411 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)) 1412 return -EOPNOTSUPP; 1413 1414 return nfp_net_rss_key_sz(nn); 1415 } 1416 1417 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, 1418 u8 *hfunc) 1419 { 1420 struct nfp_net *nn = netdev_priv(netdev); 1421 int i; 1422 1423 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)) 1424 return -EOPNOTSUPP; 1425 1426 if (indir) 1427 for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++) 1428 indir[i] = nn->rss_itbl[i]; 1429 if (key) 1430 memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn)); 1431 if (hfunc) { 1432 *hfunc = nn->rss_hfunc; 1433 if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT) 1434 *hfunc = ETH_RSS_HASH_UNKNOWN; 1435 } 1436 1437 return 0; 1438 } 1439 1440 static int nfp_net_set_rxfh(struct net_device *netdev, 1441 const u32 *indir, const u8 *key, 1442 const u8 hfunc) 1443 { 1444 struct nfp_net *nn = netdev_priv(netdev); 1445 int i; 1446 1447 if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) || 1448 !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc)) 1449 return -EOPNOTSUPP; 1450 1451 if (!key && !indir) 1452 return 0; 1453 1454 if (key) { 1455 memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn)); 1456 nfp_net_rss_write_key(nn); 1457 } 1458 if (indir) { 1459 for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++) 1460 nn->rss_itbl[i] = indir[i]; 1461 1462 nfp_net_rss_write_itbl(nn); 1463 } 1464 1465 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS); 1466 } 1467 1468 /* Dump BAR registers 1469 */ 1470 static int nfp_net_get_regs_len(struct net_device *netdev) 1471 { 1472 return NFP_NET_CFG_BAR_SZ; 1473 } 1474 1475 static void nfp_net_get_regs(struct net_device *netdev, 1476 struct ethtool_regs *regs, void *p) 1477 { 1478 struct nfp_net *nn = netdev_priv(netdev); 1479 u32 *regs_buf = p; 1480 int i; 1481 1482 regs->version = nn_readl(nn, NFP_NET_CFG_VERSION); 1483 1484 for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++) 1485 regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32))); 1486 } 1487 1488 static int nfp_net_get_coalesce(struct net_device *netdev, 1489 struct ethtool_coalesce *ec, 1490 struct kernel_ethtool_coalesce *kernel_coal, 1491 struct netlink_ext_ack *extack) 1492 { 1493 struct nfp_net *nn = netdev_priv(netdev); 1494 1495 if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD)) 1496 return -EINVAL; 1497 1498 ec->use_adaptive_rx_coalesce = nn->rx_coalesce_adapt_on; 1499 ec->use_adaptive_tx_coalesce = nn->tx_coalesce_adapt_on; 1500 1501 ec->rx_coalesce_usecs = nn->rx_coalesce_usecs; 1502 ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames; 1503 ec->tx_coalesce_usecs = nn->tx_coalesce_usecs; 1504 ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames; 1505 1506 return 0; 1507 } 1508 1509 /* Other debug dumps 1510 */ 1511 static int 1512 nfp_dump_nsp_diag(struct nfp_app *app, struct ethtool_dump *dump, void *buffer) 1513 { 1514 struct nfp_resource *res; 1515 int ret; 1516 1517 if (!app) 1518 return -EOPNOTSUPP; 1519 1520 dump->version = 1; 1521 dump->flag = NFP_DUMP_NSP_DIAG; 1522 1523 res = nfp_resource_acquire(app->cpp, NFP_RESOURCE_NSP_DIAG); 1524 if (IS_ERR(res)) 1525 return PTR_ERR(res); 1526 1527 if (buffer) { 1528 if (dump->len != nfp_resource_size(res)) { 1529 ret = -EINVAL; 1530 goto exit_release; 1531 } 1532 1533 ret = nfp_cpp_read(app->cpp, nfp_resource_cpp_id(res), 1534 nfp_resource_address(res), 1535 buffer, dump->len); 1536 if (ret != dump->len) 1537 ret = ret < 0 ? ret : -EIO; 1538 else 1539 ret = 0; 1540 } else { 1541 dump->len = nfp_resource_size(res); 1542 ret = 0; 1543 } 1544 exit_release: 1545 nfp_resource_release(res); 1546 1547 return ret; 1548 } 1549 1550 /* Set the dump flag/level. Calculate the dump length for flag > 0 only (new TLV 1551 * based dumps), since flag 0 (default) calculates the length in 1552 * nfp_app_get_dump_flag(), and we need to support triggering a level 0 dump 1553 * without setting the flag first, for backward compatibility. 1554 */ 1555 static int nfp_app_set_dump(struct net_device *netdev, struct ethtool_dump *val) 1556 { 1557 struct nfp_app *app = nfp_app_from_netdev(netdev); 1558 s64 len; 1559 1560 if (!app) 1561 return -EOPNOTSUPP; 1562 1563 if (val->flag == NFP_DUMP_NSP_DIAG) { 1564 app->pf->dump_flag = val->flag; 1565 return 0; 1566 } 1567 1568 if (!app->pf->dumpspec) 1569 return -EOPNOTSUPP; 1570 1571 len = nfp_net_dump_calculate_size(app->pf, app->pf->dumpspec, 1572 val->flag); 1573 if (len < 0) 1574 return len; 1575 1576 app->pf->dump_flag = val->flag; 1577 app->pf->dump_len = len; 1578 1579 return 0; 1580 } 1581 1582 static int 1583 nfp_app_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump) 1584 { 1585 struct nfp_app *app = nfp_app_from_netdev(netdev); 1586 1587 if (!app) 1588 return -EOPNOTSUPP; 1589 1590 if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG) 1591 return nfp_dump_nsp_diag(app, dump, NULL); 1592 1593 dump->flag = app->pf->dump_flag; 1594 dump->len = app->pf->dump_len; 1595 1596 return 0; 1597 } 1598 1599 static int 1600 nfp_app_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump, 1601 void *buffer) 1602 { 1603 struct nfp_app *app = nfp_app_from_netdev(netdev); 1604 1605 if (!app) 1606 return -EOPNOTSUPP; 1607 1608 if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG) 1609 return nfp_dump_nsp_diag(app, dump, buffer); 1610 1611 dump->flag = app->pf->dump_flag; 1612 dump->len = app->pf->dump_len; 1613 1614 return nfp_net_dump_populate_buffer(app->pf, app->pf->dumpspec, dump, 1615 buffer); 1616 } 1617 1618 static int 1619 nfp_port_get_module_info(struct net_device *netdev, 1620 struct ethtool_modinfo *modinfo) 1621 { 1622 struct nfp_eth_table_port *eth_port; 1623 struct nfp_port *port; 1624 unsigned int read_len; 1625 struct nfp_nsp *nsp; 1626 int err = 0; 1627 u8 data; 1628 1629 port = nfp_port_from_netdev(netdev); 1630 if (!port) 1631 return -EOPNOTSUPP; 1632 1633 /* update port state to get latest interface */ 1634 set_bit(NFP_PORT_CHANGED, &port->flags); 1635 eth_port = nfp_port_get_eth_port(port); 1636 if (!eth_port) 1637 return -EOPNOTSUPP; 1638 1639 nsp = nfp_nsp_open(port->app->cpp); 1640 if (IS_ERR(nsp)) { 1641 err = PTR_ERR(nsp); 1642 netdev_err(netdev, "Failed to access the NSP: %d\n", err); 1643 return err; 1644 } 1645 1646 if (!nfp_nsp_has_read_module_eeprom(nsp)) { 1647 netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n"); 1648 err = -EOPNOTSUPP; 1649 goto exit_close_nsp; 1650 } 1651 1652 switch (eth_port->interface) { 1653 case NFP_INTERFACE_SFP: 1654 case NFP_INTERFACE_SFP28: 1655 err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index, 1656 SFP_SFF8472_COMPLIANCE, &data, 1657 1, &read_len); 1658 if (err < 0) 1659 goto exit_close_nsp; 1660 1661 if (!data) { 1662 modinfo->type = ETH_MODULE_SFF_8079; 1663 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; 1664 } else { 1665 modinfo->type = ETH_MODULE_SFF_8472; 1666 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 1667 } 1668 break; 1669 case NFP_INTERFACE_QSFP: 1670 err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index, 1671 SFP_SFF_REV_COMPLIANCE, &data, 1672 1, &read_len); 1673 if (err < 0) 1674 goto exit_close_nsp; 1675 1676 if (data < 0x3) { 1677 modinfo->type = ETH_MODULE_SFF_8436; 1678 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1679 } else { 1680 modinfo->type = ETH_MODULE_SFF_8636; 1681 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1682 } 1683 break; 1684 case NFP_INTERFACE_QSFP28: 1685 modinfo->type = ETH_MODULE_SFF_8636; 1686 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1687 break; 1688 default: 1689 netdev_err(netdev, "Unsupported module 0x%x detected\n", 1690 eth_port->interface); 1691 err = -EINVAL; 1692 } 1693 1694 exit_close_nsp: 1695 nfp_nsp_close(nsp); 1696 return err; 1697 } 1698 1699 static int 1700 nfp_port_get_module_eeprom(struct net_device *netdev, 1701 struct ethtool_eeprom *eeprom, u8 *data) 1702 { 1703 struct nfp_eth_table_port *eth_port; 1704 struct nfp_port *port; 1705 struct nfp_nsp *nsp; 1706 int err; 1707 1708 port = nfp_port_from_netdev(netdev); 1709 eth_port = __nfp_port_get_eth_port(port); 1710 if (!eth_port) 1711 return -EOPNOTSUPP; 1712 1713 nsp = nfp_nsp_open(port->app->cpp); 1714 if (IS_ERR(nsp)) { 1715 err = PTR_ERR(nsp); 1716 netdev_err(netdev, "Failed to access the NSP: %d\n", err); 1717 return err; 1718 } 1719 1720 if (!nfp_nsp_has_read_module_eeprom(nsp)) { 1721 netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n"); 1722 err = -EOPNOTSUPP; 1723 goto exit_close_nsp; 1724 } 1725 1726 err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index, 1727 eeprom->offset, data, eeprom->len, 1728 &eeprom->len); 1729 if (err < 0) { 1730 if (eeprom->len) { 1731 netdev_warn(netdev, 1732 "Incomplete read from module EEPROM: %d\n", 1733 err); 1734 err = 0; 1735 } else { 1736 netdev_err(netdev, 1737 "Reading from module EEPROM failed: %d\n", 1738 err); 1739 } 1740 } 1741 1742 exit_close_nsp: 1743 nfp_nsp_close(nsp); 1744 return err; 1745 } 1746 1747 static int nfp_net_set_coalesce(struct net_device *netdev, 1748 struct ethtool_coalesce *ec, 1749 struct kernel_ethtool_coalesce *kernel_coal, 1750 struct netlink_ext_ack *extack) 1751 { 1752 struct nfp_net *nn = netdev_priv(netdev); 1753 unsigned int factor; 1754 1755 /* Compute factor used to convert coalesce '_usecs' parameters to 1756 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp 1757 * count. 1758 */ 1759 factor = nn->tlv_caps.me_freq_mhz / 16; 1760 1761 /* Each pair of (usecs, max_frames) fields specifies that interrupts 1762 * should be coalesced until 1763 * (usecs > 0 && time_since_first_completion >= usecs) || 1764 * (max_frames > 0 && completed_frames >= max_frames) 1765 * 1766 * It is illegal to set both usecs and max_frames to zero as this would 1767 * cause interrupts to never be generated. To disable coalescing, set 1768 * usecs = 0 and max_frames = 1. 1769 * 1770 * Some implementations ignore the value of max_frames and use the 1771 * condition time_since_first_completion >= usecs 1772 */ 1773 1774 if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD)) 1775 return -EINVAL; 1776 1777 /* ensure valid configuration */ 1778 if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames) 1779 return -EINVAL; 1780 1781 if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames) 1782 return -EINVAL; 1783 1784 if (nfp_net_coalesce_para_check(ec->rx_coalesce_usecs * factor, 1785 ec->rx_max_coalesced_frames)) 1786 return -EINVAL; 1787 1788 if (nfp_net_coalesce_para_check(ec->tx_coalesce_usecs * factor, 1789 ec->tx_max_coalesced_frames)) 1790 return -EINVAL; 1791 1792 /* configuration is valid */ 1793 nn->rx_coalesce_adapt_on = !!ec->use_adaptive_rx_coalesce; 1794 nn->tx_coalesce_adapt_on = !!ec->use_adaptive_tx_coalesce; 1795 1796 nn->rx_coalesce_usecs = ec->rx_coalesce_usecs; 1797 nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames; 1798 nn->tx_coalesce_usecs = ec->tx_coalesce_usecs; 1799 nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames; 1800 1801 /* write configuration to device */ 1802 nfp_net_coalesce_write_cfg(nn); 1803 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD); 1804 } 1805 1806 static void nfp_net_get_channels(struct net_device *netdev, 1807 struct ethtool_channels *channel) 1808 { 1809 struct nfp_net *nn = netdev_priv(netdev); 1810 unsigned int num_tx_rings; 1811 1812 num_tx_rings = nn->dp.num_tx_rings; 1813 if (nn->dp.xdp_prog) 1814 num_tx_rings -= nn->dp.num_rx_rings; 1815 1816 channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs); 1817 channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs); 1818 channel->max_combined = min(channel->max_rx, channel->max_tx); 1819 channel->max_other = NFP_NET_NON_Q_VECTORS; 1820 channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings); 1821 channel->rx_count = nn->dp.num_rx_rings - channel->combined_count; 1822 channel->tx_count = num_tx_rings - channel->combined_count; 1823 channel->other_count = NFP_NET_NON_Q_VECTORS; 1824 } 1825 1826 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx, 1827 unsigned int total_tx) 1828 { 1829 struct nfp_net_dp *dp; 1830 1831 dp = nfp_net_clone_dp(nn); 1832 if (!dp) 1833 return -ENOMEM; 1834 1835 dp->num_rx_rings = total_rx; 1836 dp->num_tx_rings = total_tx; 1837 /* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */ 1838 if (dp->xdp_prog) 1839 dp->num_tx_rings += total_rx; 1840 1841 return nfp_net_ring_reconfig(nn, dp, NULL); 1842 } 1843 1844 static int nfp_net_set_channels(struct net_device *netdev, 1845 struct ethtool_channels *channel) 1846 { 1847 struct nfp_net *nn = netdev_priv(netdev); 1848 unsigned int total_rx, total_tx; 1849 1850 /* Reject unsupported */ 1851 if (channel->other_count != NFP_NET_NON_Q_VECTORS || 1852 (channel->rx_count && channel->tx_count)) 1853 return -EINVAL; 1854 1855 total_rx = channel->combined_count + channel->rx_count; 1856 total_tx = channel->combined_count + channel->tx_count; 1857 1858 if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) || 1859 total_tx > min(nn->max_tx_rings, nn->max_r_vecs)) 1860 return -EINVAL; 1861 1862 return nfp_net_set_num_rings(nn, total_rx, total_tx); 1863 } 1864 1865 static void nfp_port_get_pauseparam(struct net_device *netdev, 1866 struct ethtool_pauseparam *pause) 1867 { 1868 struct nfp_eth_table_port *eth_port; 1869 struct nfp_port *port; 1870 1871 port = nfp_port_from_netdev(netdev); 1872 eth_port = nfp_port_get_eth_port(port); 1873 if (!eth_port) 1874 return; 1875 1876 /* Currently pause frame support is fixed */ 1877 pause->autoneg = AUTONEG_DISABLE; 1878 pause->rx_pause = 1; 1879 pause->tx_pause = 1; 1880 } 1881 1882 static int nfp_net_set_phys_id(struct net_device *netdev, 1883 enum ethtool_phys_id_state state) 1884 { 1885 struct nfp_eth_table_port *eth_port; 1886 struct nfp_port *port; 1887 int err; 1888 1889 port = nfp_port_from_netdev(netdev); 1890 eth_port = __nfp_port_get_eth_port(port); 1891 if (!eth_port) 1892 return -EOPNOTSUPP; 1893 1894 switch (state) { 1895 case ETHTOOL_ID_ACTIVE: 1896 /* Control LED to blink */ 1897 err = nfp_eth_set_idmode(port->app->cpp, eth_port->index, 1); 1898 break; 1899 1900 case ETHTOOL_ID_INACTIVE: 1901 /* Control LED to normal mode */ 1902 err = nfp_eth_set_idmode(port->app->cpp, eth_port->index, 0); 1903 break; 1904 1905 case ETHTOOL_ID_ON: 1906 case ETHTOOL_ID_OFF: 1907 default: 1908 return -EOPNOTSUPP; 1909 } 1910 1911 return err; 1912 } 1913 1914 #define NFP_EEPROM_LEN ETH_ALEN 1915 1916 static int 1917 nfp_net_get_eeprom_len(struct net_device *netdev) 1918 { 1919 struct nfp_eth_table_port *eth_port; 1920 struct nfp_port *port; 1921 1922 port = nfp_port_from_netdev(netdev); 1923 eth_port = __nfp_port_get_eth_port(port); 1924 if (!eth_port) 1925 return 0; 1926 1927 return NFP_EEPROM_LEN; 1928 } 1929 1930 static int 1931 nfp_net_get_nsp_hwindex(struct net_device *netdev, 1932 struct nfp_nsp **nspptr, 1933 u32 *index) 1934 { 1935 struct nfp_eth_table_port *eth_port; 1936 struct nfp_port *port; 1937 struct nfp_nsp *nsp; 1938 int err; 1939 1940 port = nfp_port_from_netdev(netdev); 1941 eth_port = __nfp_port_get_eth_port(port); 1942 if (!eth_port) 1943 return -EOPNOTSUPP; 1944 1945 nsp = nfp_nsp_open(port->app->cpp); 1946 if (IS_ERR(nsp)) { 1947 err = PTR_ERR(nsp); 1948 netdev_err(netdev, "Failed to access the NSP: %d\n", err); 1949 return err; 1950 } 1951 1952 if (!nfp_nsp_has_hwinfo_lookup(nsp)) { 1953 netdev_err(netdev, "NSP doesn't support PF MAC generation\n"); 1954 nfp_nsp_close(nsp); 1955 return -EOPNOTSUPP; 1956 } 1957 1958 *nspptr = nsp; 1959 *index = eth_port->eth_index; 1960 1961 return 0; 1962 } 1963 1964 static int 1965 nfp_net_get_port_mac_by_hwinfo(struct net_device *netdev, 1966 u8 *mac_addr) 1967 { 1968 char hwinfo[32] = {}; 1969 struct nfp_nsp *nsp; 1970 u32 index; 1971 int err; 1972 1973 err = nfp_net_get_nsp_hwindex(netdev, &nsp, &index); 1974 if (err) 1975 return err; 1976 1977 snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac", index); 1978 err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo)); 1979 nfp_nsp_close(nsp); 1980 if (err) { 1981 netdev_err(netdev, "Reading persistent MAC address failed: %d\n", 1982 err); 1983 return -EOPNOTSUPP; 1984 } 1985 1986 if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 1987 &mac_addr[0], &mac_addr[1], &mac_addr[2], 1988 &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) { 1989 netdev_err(netdev, "Can't parse persistent MAC address (%s)\n", 1990 hwinfo); 1991 return -EOPNOTSUPP; 1992 } 1993 1994 return 0; 1995 } 1996 1997 static int 1998 nfp_net_set_port_mac_by_hwinfo(struct net_device *netdev, 1999 u8 *mac_addr) 2000 { 2001 char hwinfo[32] = {}; 2002 struct nfp_nsp *nsp; 2003 u32 index; 2004 int err; 2005 2006 err = nfp_net_get_nsp_hwindex(netdev, &nsp, &index); 2007 if (err) 2008 return err; 2009 2010 snprintf(hwinfo, sizeof(hwinfo), 2011 "eth%u.mac=%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 2012 index, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], 2013 mac_addr[4], mac_addr[5]); 2014 2015 err = nfp_nsp_hwinfo_set(nsp, hwinfo, sizeof(hwinfo)); 2016 nfp_nsp_close(nsp); 2017 if (err) { 2018 netdev_err(netdev, "HWinfo set failed: %d, hwinfo: %s\n", 2019 err, hwinfo); 2020 return -EOPNOTSUPP; 2021 } 2022 2023 return 0; 2024 } 2025 2026 static int 2027 nfp_net_get_eeprom(struct net_device *netdev, 2028 struct ethtool_eeprom *eeprom, u8 *bytes) 2029 { 2030 struct nfp_app *app = nfp_app_from_netdev(netdev); 2031 u8 buf[NFP_EEPROM_LEN] = {}; 2032 2033 if (nfp_net_get_port_mac_by_hwinfo(netdev, buf)) 2034 return -EOPNOTSUPP; 2035 2036 if (eeprom->len == 0) 2037 return -EINVAL; 2038 2039 eeprom->magic = app->pdev->vendor | (app->pdev->device << 16); 2040 memcpy(bytes, buf + eeprom->offset, eeprom->len); 2041 2042 return 0; 2043 } 2044 2045 static int 2046 nfp_net_set_eeprom(struct net_device *netdev, 2047 struct ethtool_eeprom *eeprom, u8 *bytes) 2048 { 2049 struct nfp_app *app = nfp_app_from_netdev(netdev); 2050 u8 buf[NFP_EEPROM_LEN] = {}; 2051 2052 if (nfp_net_get_port_mac_by_hwinfo(netdev, buf)) 2053 return -EOPNOTSUPP; 2054 2055 if (eeprom->len == 0) 2056 return -EINVAL; 2057 2058 if (eeprom->magic != (app->pdev->vendor | app->pdev->device << 16)) 2059 return -EINVAL; 2060 2061 memcpy(buf + eeprom->offset, bytes, eeprom->len); 2062 if (nfp_net_set_port_mac_by_hwinfo(netdev, buf)) 2063 return -EOPNOTSUPP; 2064 2065 return 0; 2066 } 2067 2068 static const struct ethtool_ops nfp_net_ethtool_ops = { 2069 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 2070 ETHTOOL_COALESCE_MAX_FRAMES | 2071 ETHTOOL_COALESCE_USE_ADAPTIVE, 2072 .get_drvinfo = nfp_net_get_drvinfo, 2073 .nway_reset = nfp_net_nway_reset, 2074 .get_link = ethtool_op_get_link, 2075 .get_ringparam = nfp_net_get_ringparam, 2076 .set_ringparam = nfp_net_set_ringparam, 2077 .self_test = nfp_net_self_test, 2078 .get_strings = nfp_net_get_strings, 2079 .get_ethtool_stats = nfp_net_get_stats, 2080 .get_sset_count = nfp_net_get_sset_count, 2081 .get_rxnfc = nfp_net_get_rxnfc, 2082 .set_rxnfc = nfp_net_set_rxnfc, 2083 .get_rxfh_indir_size = nfp_net_get_rxfh_indir_size, 2084 .get_rxfh_key_size = nfp_net_get_rxfh_key_size, 2085 .get_rxfh = nfp_net_get_rxfh, 2086 .set_rxfh = nfp_net_set_rxfh, 2087 .get_regs_len = nfp_net_get_regs_len, 2088 .get_regs = nfp_net_get_regs, 2089 .set_dump = nfp_app_set_dump, 2090 .get_dump_flag = nfp_app_get_dump_flag, 2091 .get_dump_data = nfp_app_get_dump_data, 2092 .get_eeprom_len = nfp_net_get_eeprom_len, 2093 .get_eeprom = nfp_net_get_eeprom, 2094 .set_eeprom = nfp_net_set_eeprom, 2095 .get_module_info = nfp_port_get_module_info, 2096 .get_module_eeprom = nfp_port_get_module_eeprom, 2097 .get_coalesce = nfp_net_get_coalesce, 2098 .set_coalesce = nfp_net_set_coalesce, 2099 .get_channels = nfp_net_get_channels, 2100 .set_channels = nfp_net_set_channels, 2101 .get_link_ksettings = nfp_net_get_link_ksettings, 2102 .set_link_ksettings = nfp_net_set_link_ksettings, 2103 .get_fecparam = nfp_port_get_fecparam, 2104 .set_fecparam = nfp_port_set_fecparam, 2105 .get_pauseparam = nfp_port_get_pauseparam, 2106 .set_phys_id = nfp_net_set_phys_id, 2107 }; 2108 2109 const struct ethtool_ops nfp_port_ethtool_ops = { 2110 .get_drvinfo = nfp_app_get_drvinfo, 2111 .nway_reset = nfp_net_nway_reset, 2112 .get_link = ethtool_op_get_link, 2113 .get_strings = nfp_port_get_strings, 2114 .get_ethtool_stats = nfp_port_get_stats, 2115 .self_test = nfp_net_self_test, 2116 .get_sset_count = nfp_port_get_sset_count, 2117 .set_dump = nfp_app_set_dump, 2118 .get_dump_flag = nfp_app_get_dump_flag, 2119 .get_dump_data = nfp_app_get_dump_data, 2120 .get_eeprom_len = nfp_net_get_eeprom_len, 2121 .get_eeprom = nfp_net_get_eeprom, 2122 .set_eeprom = nfp_net_set_eeprom, 2123 .get_module_info = nfp_port_get_module_info, 2124 .get_module_eeprom = nfp_port_get_module_eeprom, 2125 .get_link_ksettings = nfp_net_get_link_ksettings, 2126 .set_link_ksettings = nfp_net_set_link_ksettings, 2127 .get_fecparam = nfp_port_get_fecparam, 2128 .set_fecparam = nfp_port_set_fecparam, 2129 .get_pauseparam = nfp_port_get_pauseparam, 2130 .set_phys_id = nfp_net_set_phys_id, 2131 }; 2132 2133 void nfp_net_set_ethtool_ops(struct net_device *netdev) 2134 { 2135 netdev->ethtool_ops = &nfp_net_ethtool_ops; 2136 } 2137