1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * Copyright (c) 2016-2017 Broadcom Limited 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation. 9 */ 10 11 #include <linux/ctype.h> 12 #include <linux/stringify.h> 13 #include <linux/ethtool.h> 14 #include <linux/interrupt.h> 15 #include <linux/pci.h> 16 #include <linux/etherdevice.h> 17 #include <linux/crc32.h> 18 #include <linux/firmware.h> 19 #include "bnxt_hsi.h" 20 #include "bnxt.h" 21 #include "bnxt_xdp.h" 22 #include "bnxt_ethtool.h" 23 #include "bnxt_nvm_defs.h" /* NVRAM content constant and structure defs */ 24 #include "bnxt_fw_hdr.h" /* Firmware hdr constant and structure defs */ 25 #define FLASH_NVRAM_TIMEOUT ((HWRM_CMD_TIMEOUT) * 100) 26 #define FLASH_PACKAGE_TIMEOUT ((HWRM_CMD_TIMEOUT) * 200) 27 #define INSTALL_PACKAGE_TIMEOUT ((HWRM_CMD_TIMEOUT) * 200) 28 29 static u32 bnxt_get_msglevel(struct net_device *dev) 30 { 31 struct bnxt *bp = netdev_priv(dev); 32 33 return bp->msg_enable; 34 } 35 36 static void bnxt_set_msglevel(struct net_device *dev, u32 value) 37 { 38 struct bnxt *bp = netdev_priv(dev); 39 40 bp->msg_enable = value; 41 } 42 43 static int bnxt_get_coalesce(struct net_device *dev, 44 struct ethtool_coalesce *coal) 45 { 46 struct bnxt *bp = netdev_priv(dev); 47 struct bnxt_coal *hw_coal; 48 u16 mult; 49 50 memset(coal, 0, sizeof(*coal)); 51 52 coal->use_adaptive_rx_coalesce = bp->flags & BNXT_FLAG_DIM; 53 54 hw_coal = &bp->rx_coal; 55 mult = hw_coal->bufs_per_record; 56 coal->rx_coalesce_usecs = hw_coal->coal_ticks; 57 coal->rx_max_coalesced_frames = hw_coal->coal_bufs / mult; 58 coal->rx_coalesce_usecs_irq = hw_coal->coal_ticks_irq; 59 coal->rx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult; 60 61 hw_coal = &bp->tx_coal; 62 mult = hw_coal->bufs_per_record; 63 coal->tx_coalesce_usecs = hw_coal->coal_ticks; 64 coal->tx_max_coalesced_frames = hw_coal->coal_bufs / mult; 65 coal->tx_coalesce_usecs_irq = hw_coal->coal_ticks_irq; 66 coal->tx_max_coalesced_frames_irq = hw_coal->coal_bufs_irq / mult; 67 68 coal->stats_block_coalesce_usecs = bp->stats_coal_ticks; 69 70 return 0; 71 } 72 73 static int bnxt_set_coalesce(struct net_device *dev, 74 struct ethtool_coalesce *coal) 75 { 76 struct bnxt *bp = netdev_priv(dev); 77 bool update_stats = false; 78 struct bnxt_coal *hw_coal; 79 int rc = 0; 80 u16 mult; 81 82 if (coal->use_adaptive_rx_coalesce) { 83 bp->flags |= BNXT_FLAG_DIM; 84 } else { 85 if (bp->flags & BNXT_FLAG_DIM) { 86 bp->flags &= ~(BNXT_FLAG_DIM); 87 goto reset_coalesce; 88 } 89 } 90 91 hw_coal = &bp->rx_coal; 92 mult = hw_coal->bufs_per_record; 93 hw_coal->coal_ticks = coal->rx_coalesce_usecs; 94 hw_coal->coal_bufs = coal->rx_max_coalesced_frames * mult; 95 hw_coal->coal_ticks_irq = coal->rx_coalesce_usecs_irq; 96 hw_coal->coal_bufs_irq = coal->rx_max_coalesced_frames_irq * mult; 97 98 hw_coal = &bp->tx_coal; 99 mult = hw_coal->bufs_per_record; 100 hw_coal->coal_ticks = coal->tx_coalesce_usecs; 101 hw_coal->coal_bufs = coal->tx_max_coalesced_frames * mult; 102 hw_coal->coal_ticks_irq = coal->tx_coalesce_usecs_irq; 103 hw_coal->coal_bufs_irq = coal->tx_max_coalesced_frames_irq * mult; 104 105 if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) { 106 u32 stats_ticks = coal->stats_block_coalesce_usecs; 107 108 /* Allow 0, which means disable. */ 109 if (stats_ticks) 110 stats_ticks = clamp_t(u32, stats_ticks, 111 BNXT_MIN_STATS_COAL_TICKS, 112 BNXT_MAX_STATS_COAL_TICKS); 113 stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS); 114 bp->stats_coal_ticks = stats_ticks; 115 update_stats = true; 116 } 117 118 reset_coalesce: 119 if (netif_running(dev)) { 120 if (update_stats) { 121 rc = bnxt_close_nic(bp, true, false); 122 if (!rc) 123 rc = bnxt_open_nic(bp, true, false); 124 } else { 125 rc = bnxt_hwrm_set_coal(bp); 126 } 127 } 128 129 return rc; 130 } 131 132 #define BNXT_NUM_STATS 21 133 134 #define BNXT_RX_STATS_ENTRY(counter) \ 135 { BNXT_RX_STATS_OFFSET(counter), __stringify(counter) } 136 137 #define BNXT_TX_STATS_ENTRY(counter) \ 138 { BNXT_TX_STATS_OFFSET(counter), __stringify(counter) } 139 140 static const struct { 141 long offset; 142 char string[ETH_GSTRING_LEN]; 143 } bnxt_port_stats_arr[] = { 144 BNXT_RX_STATS_ENTRY(rx_64b_frames), 145 BNXT_RX_STATS_ENTRY(rx_65b_127b_frames), 146 BNXT_RX_STATS_ENTRY(rx_128b_255b_frames), 147 BNXT_RX_STATS_ENTRY(rx_256b_511b_frames), 148 BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames), 149 BNXT_RX_STATS_ENTRY(rx_1024b_1518_frames), 150 BNXT_RX_STATS_ENTRY(rx_good_vlan_frames), 151 BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames), 152 BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames), 153 BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames), 154 BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames), 155 BNXT_RX_STATS_ENTRY(rx_total_frames), 156 BNXT_RX_STATS_ENTRY(rx_ucast_frames), 157 BNXT_RX_STATS_ENTRY(rx_mcast_frames), 158 BNXT_RX_STATS_ENTRY(rx_bcast_frames), 159 BNXT_RX_STATS_ENTRY(rx_fcs_err_frames), 160 BNXT_RX_STATS_ENTRY(rx_ctrl_frames), 161 BNXT_RX_STATS_ENTRY(rx_pause_frames), 162 BNXT_RX_STATS_ENTRY(rx_pfc_frames), 163 BNXT_RX_STATS_ENTRY(rx_align_err_frames), 164 BNXT_RX_STATS_ENTRY(rx_ovrsz_frames), 165 BNXT_RX_STATS_ENTRY(rx_jbr_frames), 166 BNXT_RX_STATS_ENTRY(rx_mtu_err_frames), 167 BNXT_RX_STATS_ENTRY(rx_tagged_frames), 168 BNXT_RX_STATS_ENTRY(rx_double_tagged_frames), 169 BNXT_RX_STATS_ENTRY(rx_good_frames), 170 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0), 171 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1), 172 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2), 173 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3), 174 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4), 175 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5), 176 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6), 177 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7), 178 BNXT_RX_STATS_ENTRY(rx_undrsz_frames), 179 BNXT_RX_STATS_ENTRY(rx_eee_lpi_events), 180 BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration), 181 BNXT_RX_STATS_ENTRY(rx_bytes), 182 BNXT_RX_STATS_ENTRY(rx_runt_bytes), 183 BNXT_RX_STATS_ENTRY(rx_runt_frames), 184 185 BNXT_TX_STATS_ENTRY(tx_64b_frames), 186 BNXT_TX_STATS_ENTRY(tx_65b_127b_frames), 187 BNXT_TX_STATS_ENTRY(tx_128b_255b_frames), 188 BNXT_TX_STATS_ENTRY(tx_256b_511b_frames), 189 BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames), 190 BNXT_TX_STATS_ENTRY(tx_1024b_1518_frames), 191 BNXT_TX_STATS_ENTRY(tx_good_vlan_frames), 192 BNXT_TX_STATS_ENTRY(tx_1519b_2047_frames), 193 BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames), 194 BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames), 195 BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames), 196 BNXT_TX_STATS_ENTRY(tx_good_frames), 197 BNXT_TX_STATS_ENTRY(tx_total_frames), 198 BNXT_TX_STATS_ENTRY(tx_ucast_frames), 199 BNXT_TX_STATS_ENTRY(tx_mcast_frames), 200 BNXT_TX_STATS_ENTRY(tx_bcast_frames), 201 BNXT_TX_STATS_ENTRY(tx_pause_frames), 202 BNXT_TX_STATS_ENTRY(tx_pfc_frames), 203 BNXT_TX_STATS_ENTRY(tx_jabber_frames), 204 BNXT_TX_STATS_ENTRY(tx_fcs_err_frames), 205 BNXT_TX_STATS_ENTRY(tx_err), 206 BNXT_TX_STATS_ENTRY(tx_fifo_underruns), 207 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0), 208 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1), 209 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2), 210 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3), 211 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4), 212 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5), 213 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6), 214 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7), 215 BNXT_TX_STATS_ENTRY(tx_eee_lpi_events), 216 BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration), 217 BNXT_TX_STATS_ENTRY(tx_total_collisions), 218 BNXT_TX_STATS_ENTRY(tx_bytes), 219 }; 220 221 #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr) 222 223 static int bnxt_get_num_stats(struct bnxt *bp) 224 { 225 int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings; 226 227 if (bp->flags & BNXT_FLAG_PORT_STATS) 228 num_stats += BNXT_NUM_PORT_STATS; 229 230 return num_stats; 231 } 232 233 static int bnxt_get_sset_count(struct net_device *dev, int sset) 234 { 235 struct bnxt *bp = netdev_priv(dev); 236 237 switch (sset) { 238 case ETH_SS_STATS: 239 return bnxt_get_num_stats(bp); 240 case ETH_SS_TEST: 241 if (!bp->num_tests) 242 return -EOPNOTSUPP; 243 return bp->num_tests; 244 default: 245 return -EOPNOTSUPP; 246 } 247 } 248 249 static void bnxt_get_ethtool_stats(struct net_device *dev, 250 struct ethtool_stats *stats, u64 *buf) 251 { 252 u32 i, j = 0; 253 struct bnxt *bp = netdev_priv(dev); 254 u32 stat_fields = sizeof(struct ctx_hw_stats) / 8; 255 256 if (!bp->bnapi) 257 return; 258 259 for (i = 0; i < bp->cp_nr_rings; i++) { 260 struct bnxt_napi *bnapi = bp->bnapi[i]; 261 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 262 __le64 *hw_stats = (__le64 *)cpr->hw_stats; 263 int k; 264 265 for (k = 0; k < stat_fields; j++, k++) 266 buf[j] = le64_to_cpu(hw_stats[k]); 267 buf[j++] = cpr->rx_l4_csum_errors; 268 } 269 if (bp->flags & BNXT_FLAG_PORT_STATS) { 270 __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats; 271 272 for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) { 273 buf[j] = le64_to_cpu(*(port_stats + 274 bnxt_port_stats_arr[i].offset)); 275 } 276 } 277 } 278 279 static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 280 { 281 struct bnxt *bp = netdev_priv(dev); 282 u32 i; 283 284 switch (stringset) { 285 /* The number of strings must match BNXT_NUM_STATS defined above. */ 286 case ETH_SS_STATS: 287 for (i = 0; i < bp->cp_nr_rings; i++) { 288 sprintf(buf, "[%d]: rx_ucast_packets", i); 289 buf += ETH_GSTRING_LEN; 290 sprintf(buf, "[%d]: rx_mcast_packets", i); 291 buf += ETH_GSTRING_LEN; 292 sprintf(buf, "[%d]: rx_bcast_packets", i); 293 buf += ETH_GSTRING_LEN; 294 sprintf(buf, "[%d]: rx_discards", i); 295 buf += ETH_GSTRING_LEN; 296 sprintf(buf, "[%d]: rx_drops", i); 297 buf += ETH_GSTRING_LEN; 298 sprintf(buf, "[%d]: rx_ucast_bytes", i); 299 buf += ETH_GSTRING_LEN; 300 sprintf(buf, "[%d]: rx_mcast_bytes", i); 301 buf += ETH_GSTRING_LEN; 302 sprintf(buf, "[%d]: rx_bcast_bytes", i); 303 buf += ETH_GSTRING_LEN; 304 sprintf(buf, "[%d]: tx_ucast_packets", i); 305 buf += ETH_GSTRING_LEN; 306 sprintf(buf, "[%d]: tx_mcast_packets", i); 307 buf += ETH_GSTRING_LEN; 308 sprintf(buf, "[%d]: tx_bcast_packets", i); 309 buf += ETH_GSTRING_LEN; 310 sprintf(buf, "[%d]: tx_discards", i); 311 buf += ETH_GSTRING_LEN; 312 sprintf(buf, "[%d]: tx_drops", i); 313 buf += ETH_GSTRING_LEN; 314 sprintf(buf, "[%d]: tx_ucast_bytes", i); 315 buf += ETH_GSTRING_LEN; 316 sprintf(buf, "[%d]: tx_mcast_bytes", i); 317 buf += ETH_GSTRING_LEN; 318 sprintf(buf, "[%d]: tx_bcast_bytes", i); 319 buf += ETH_GSTRING_LEN; 320 sprintf(buf, "[%d]: tpa_packets", i); 321 buf += ETH_GSTRING_LEN; 322 sprintf(buf, "[%d]: tpa_bytes", i); 323 buf += ETH_GSTRING_LEN; 324 sprintf(buf, "[%d]: tpa_events", i); 325 buf += ETH_GSTRING_LEN; 326 sprintf(buf, "[%d]: tpa_aborts", i); 327 buf += ETH_GSTRING_LEN; 328 sprintf(buf, "[%d]: rx_l4_csum_errors", i); 329 buf += ETH_GSTRING_LEN; 330 } 331 if (bp->flags & BNXT_FLAG_PORT_STATS) { 332 for (i = 0; i < BNXT_NUM_PORT_STATS; i++) { 333 strcpy(buf, bnxt_port_stats_arr[i].string); 334 buf += ETH_GSTRING_LEN; 335 } 336 } 337 break; 338 case ETH_SS_TEST: 339 if (bp->num_tests) 340 memcpy(buf, bp->test_info->string, 341 bp->num_tests * ETH_GSTRING_LEN); 342 break; 343 default: 344 netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n", 345 stringset); 346 break; 347 } 348 } 349 350 static void bnxt_get_ringparam(struct net_device *dev, 351 struct ethtool_ringparam *ering) 352 { 353 struct bnxt *bp = netdev_priv(dev); 354 355 ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT; 356 ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT; 357 ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT; 358 359 ering->rx_pending = bp->rx_ring_size; 360 ering->rx_jumbo_pending = bp->rx_agg_ring_size; 361 ering->tx_pending = bp->tx_ring_size; 362 } 363 364 static int bnxt_set_ringparam(struct net_device *dev, 365 struct ethtool_ringparam *ering) 366 { 367 struct bnxt *bp = netdev_priv(dev); 368 369 if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) || 370 (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) || 371 (ering->tx_pending <= MAX_SKB_FRAGS)) 372 return -EINVAL; 373 374 if (netif_running(dev)) 375 bnxt_close_nic(bp, false, false); 376 377 bp->rx_ring_size = ering->rx_pending; 378 bp->tx_ring_size = ering->tx_pending; 379 bnxt_set_ring_params(bp); 380 381 if (netif_running(dev)) 382 return bnxt_open_nic(bp, false, false); 383 384 return 0; 385 } 386 387 static void bnxt_get_channels(struct net_device *dev, 388 struct ethtool_channels *channel) 389 { 390 struct bnxt *bp = netdev_priv(dev); 391 int max_rx_rings, max_tx_rings, tcs; 392 393 bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true); 394 channel->max_combined = min_t(int, max_rx_rings, max_tx_rings); 395 396 if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) { 397 max_rx_rings = 0; 398 max_tx_rings = 0; 399 } 400 401 tcs = netdev_get_num_tc(dev); 402 if (tcs > 1) 403 max_tx_rings /= tcs; 404 405 channel->max_rx = max_rx_rings; 406 channel->max_tx = max_tx_rings; 407 channel->max_other = 0; 408 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 409 channel->combined_count = bp->rx_nr_rings; 410 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 411 channel->combined_count--; 412 } else { 413 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) { 414 channel->rx_count = bp->rx_nr_rings; 415 channel->tx_count = bp->tx_nr_rings_per_tc; 416 } 417 } 418 } 419 420 static int bnxt_set_channels(struct net_device *dev, 421 struct ethtool_channels *channel) 422 { 423 struct bnxt *bp = netdev_priv(dev); 424 int req_tx_rings, req_rx_rings, tcs; 425 bool sh = false; 426 int tx_xdp = 0; 427 int rc = 0; 428 429 if (channel->other_count) 430 return -EINVAL; 431 432 if (!channel->combined_count && 433 (!channel->rx_count || !channel->tx_count)) 434 return -EINVAL; 435 436 if (channel->combined_count && 437 (channel->rx_count || channel->tx_count)) 438 return -EINVAL; 439 440 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count || 441 channel->tx_count)) 442 return -EINVAL; 443 444 if (channel->combined_count) 445 sh = true; 446 447 tcs = netdev_get_num_tc(dev); 448 449 req_tx_rings = sh ? channel->combined_count : channel->tx_count; 450 req_rx_rings = sh ? channel->combined_count : channel->rx_count; 451 if (bp->tx_nr_rings_xdp) { 452 if (!sh) { 453 netdev_err(dev, "Only combined mode supported when XDP is enabled.\n"); 454 return -EINVAL; 455 } 456 tx_xdp = req_rx_rings; 457 } 458 rc = bnxt_check_rings(bp, req_tx_rings, req_rx_rings, sh, tcs, tx_xdp); 459 if (rc) { 460 netdev_warn(dev, "Unable to allocate the requested rings\n"); 461 return rc; 462 } 463 464 if (netif_running(dev)) { 465 if (BNXT_PF(bp)) { 466 /* TODO CHIMP_FW: Send message to all VF's 467 * before PF unload 468 */ 469 } 470 rc = bnxt_close_nic(bp, true, false); 471 if (rc) { 472 netdev_err(bp->dev, "Set channel failure rc :%x\n", 473 rc); 474 return rc; 475 } 476 } 477 478 if (sh) { 479 bp->flags |= BNXT_FLAG_SHARED_RINGS; 480 bp->rx_nr_rings = channel->combined_count; 481 bp->tx_nr_rings_per_tc = channel->combined_count; 482 } else { 483 bp->flags &= ~BNXT_FLAG_SHARED_RINGS; 484 bp->rx_nr_rings = channel->rx_count; 485 bp->tx_nr_rings_per_tc = channel->tx_count; 486 } 487 bp->tx_nr_rings_xdp = tx_xdp; 488 bp->tx_nr_rings = bp->tx_nr_rings_per_tc + tx_xdp; 489 if (tcs > 1) 490 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs + tx_xdp; 491 492 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) : 493 bp->tx_nr_rings + bp->rx_nr_rings; 494 495 bp->num_stat_ctxs = bp->cp_nr_rings; 496 497 /* After changing number of rx channels, update NTUPLE feature. */ 498 netdev_update_features(dev); 499 if (netif_running(dev)) { 500 rc = bnxt_open_nic(bp, true, false); 501 if ((!rc) && BNXT_PF(bp)) { 502 /* TODO CHIMP_FW: Send message to all VF's 503 * to renable 504 */ 505 } 506 } 507 508 return rc; 509 } 510 511 #ifdef CONFIG_RFS_ACCEL 512 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd, 513 u32 *rule_locs) 514 { 515 int i, j = 0; 516 517 cmd->data = bp->ntp_fltr_count; 518 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 519 struct hlist_head *head; 520 struct bnxt_ntuple_filter *fltr; 521 522 head = &bp->ntp_fltr_hash_tbl[i]; 523 rcu_read_lock(); 524 hlist_for_each_entry_rcu(fltr, head, hash) { 525 if (j == cmd->rule_cnt) 526 break; 527 rule_locs[j++] = fltr->sw_id; 528 } 529 rcu_read_unlock(); 530 if (j == cmd->rule_cnt) 531 break; 532 } 533 cmd->rule_cnt = j; 534 return 0; 535 } 536 537 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd) 538 { 539 struct ethtool_rx_flow_spec *fs = 540 (struct ethtool_rx_flow_spec *)&cmd->fs; 541 struct bnxt_ntuple_filter *fltr; 542 struct flow_keys *fkeys; 543 int i, rc = -EINVAL; 544 545 if (fs->location >= BNXT_NTP_FLTR_MAX_FLTR) 546 return rc; 547 548 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 549 struct hlist_head *head; 550 551 head = &bp->ntp_fltr_hash_tbl[i]; 552 rcu_read_lock(); 553 hlist_for_each_entry_rcu(fltr, head, hash) { 554 if (fltr->sw_id == fs->location) 555 goto fltr_found; 556 } 557 rcu_read_unlock(); 558 } 559 return rc; 560 561 fltr_found: 562 fkeys = &fltr->fkeys; 563 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 564 if (fkeys->basic.ip_proto == IPPROTO_TCP) 565 fs->flow_type = TCP_V4_FLOW; 566 else if (fkeys->basic.ip_proto == IPPROTO_UDP) 567 fs->flow_type = UDP_V4_FLOW; 568 else 569 goto fltr_err; 570 571 fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src; 572 fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0); 573 574 fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst; 575 fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0); 576 577 fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src; 578 fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0); 579 580 fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst; 581 fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0); 582 } else { 583 int i; 584 585 if (fkeys->basic.ip_proto == IPPROTO_TCP) 586 fs->flow_type = TCP_V6_FLOW; 587 else if (fkeys->basic.ip_proto == IPPROTO_UDP) 588 fs->flow_type = UDP_V6_FLOW; 589 else 590 goto fltr_err; 591 592 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] = 593 fkeys->addrs.v6addrs.src; 594 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] = 595 fkeys->addrs.v6addrs.dst; 596 for (i = 0; i < 4; i++) { 597 fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0); 598 fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0); 599 } 600 fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src; 601 fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0); 602 603 fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst; 604 fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0); 605 } 606 607 fs->ring_cookie = fltr->rxq; 608 rc = 0; 609 610 fltr_err: 611 rcu_read_unlock(); 612 613 return rc; 614 } 615 #endif 616 617 static u64 get_ethtool_ipv4_rss(struct bnxt *bp) 618 { 619 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 620 return RXH_IP_SRC | RXH_IP_DST; 621 return 0; 622 } 623 624 static u64 get_ethtool_ipv6_rss(struct bnxt *bp) 625 { 626 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 627 return RXH_IP_SRC | RXH_IP_DST; 628 return 0; 629 } 630 631 static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd) 632 { 633 cmd->data = 0; 634 switch (cmd->flow_type) { 635 case TCP_V4_FLOW: 636 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) 637 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 638 RXH_L4_B_0_1 | RXH_L4_B_2_3; 639 cmd->data |= get_ethtool_ipv4_rss(bp); 640 break; 641 case UDP_V4_FLOW: 642 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4) 643 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 644 RXH_L4_B_0_1 | RXH_L4_B_2_3; 645 /* fall through */ 646 case SCTP_V4_FLOW: 647 case AH_ESP_V4_FLOW: 648 case AH_V4_FLOW: 649 case ESP_V4_FLOW: 650 case IPV4_FLOW: 651 cmd->data |= get_ethtool_ipv4_rss(bp); 652 break; 653 654 case TCP_V6_FLOW: 655 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) 656 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 657 RXH_L4_B_0_1 | RXH_L4_B_2_3; 658 cmd->data |= get_ethtool_ipv6_rss(bp); 659 break; 660 case UDP_V6_FLOW: 661 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6) 662 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 663 RXH_L4_B_0_1 | RXH_L4_B_2_3; 664 /* fall through */ 665 case SCTP_V6_FLOW: 666 case AH_ESP_V6_FLOW: 667 case AH_V6_FLOW: 668 case ESP_V6_FLOW: 669 case IPV6_FLOW: 670 cmd->data |= get_ethtool_ipv6_rss(bp); 671 break; 672 } 673 return 0; 674 } 675 676 #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3) 677 #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST) 678 679 static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd) 680 { 681 u32 rss_hash_cfg = bp->rss_hash_cfg; 682 int tuple, rc = 0; 683 684 if (cmd->data == RXH_4TUPLE) 685 tuple = 4; 686 else if (cmd->data == RXH_2TUPLE) 687 tuple = 2; 688 else if (!cmd->data) 689 tuple = 0; 690 else 691 return -EINVAL; 692 693 if (cmd->flow_type == TCP_V4_FLOW) { 694 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4; 695 if (tuple == 4) 696 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4; 697 } else if (cmd->flow_type == UDP_V4_FLOW) { 698 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP)) 699 return -EINVAL; 700 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4; 701 if (tuple == 4) 702 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4; 703 } else if (cmd->flow_type == TCP_V6_FLOW) { 704 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 705 if (tuple == 4) 706 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 707 } else if (cmd->flow_type == UDP_V6_FLOW) { 708 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP)) 709 return -EINVAL; 710 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 711 if (tuple == 4) 712 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 713 } else if (tuple == 4) { 714 return -EINVAL; 715 } 716 717 switch (cmd->flow_type) { 718 case TCP_V4_FLOW: 719 case UDP_V4_FLOW: 720 case SCTP_V4_FLOW: 721 case AH_ESP_V4_FLOW: 722 case AH_V4_FLOW: 723 case ESP_V4_FLOW: 724 case IPV4_FLOW: 725 if (tuple == 2) 726 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4; 727 else if (!tuple) 728 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4; 729 break; 730 731 case TCP_V6_FLOW: 732 case UDP_V6_FLOW: 733 case SCTP_V6_FLOW: 734 case AH_ESP_V6_FLOW: 735 case AH_V6_FLOW: 736 case ESP_V6_FLOW: 737 case IPV6_FLOW: 738 if (tuple == 2) 739 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6; 740 else if (!tuple) 741 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6; 742 break; 743 } 744 745 if (bp->rss_hash_cfg == rss_hash_cfg) 746 return 0; 747 748 bp->rss_hash_cfg = rss_hash_cfg; 749 if (netif_running(bp->dev)) { 750 bnxt_close_nic(bp, false, false); 751 rc = bnxt_open_nic(bp, false, false); 752 } 753 return rc; 754 } 755 756 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd, 757 u32 *rule_locs) 758 { 759 struct bnxt *bp = netdev_priv(dev); 760 int rc = 0; 761 762 switch (cmd->cmd) { 763 #ifdef CONFIG_RFS_ACCEL 764 case ETHTOOL_GRXRINGS: 765 cmd->data = bp->rx_nr_rings; 766 break; 767 768 case ETHTOOL_GRXCLSRLCNT: 769 cmd->rule_cnt = bp->ntp_fltr_count; 770 cmd->data = BNXT_NTP_FLTR_MAX_FLTR; 771 break; 772 773 case ETHTOOL_GRXCLSRLALL: 774 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs); 775 break; 776 777 case ETHTOOL_GRXCLSRULE: 778 rc = bnxt_grxclsrule(bp, cmd); 779 break; 780 #endif 781 782 case ETHTOOL_GRXFH: 783 rc = bnxt_grxfh(bp, cmd); 784 break; 785 786 default: 787 rc = -EOPNOTSUPP; 788 break; 789 } 790 791 return rc; 792 } 793 794 static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd) 795 { 796 struct bnxt *bp = netdev_priv(dev); 797 int rc; 798 799 switch (cmd->cmd) { 800 case ETHTOOL_SRXFH: 801 rc = bnxt_srxfh(bp, cmd); 802 break; 803 804 default: 805 rc = -EOPNOTSUPP; 806 break; 807 } 808 return rc; 809 } 810 811 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev) 812 { 813 return HW_HASH_INDEX_SIZE; 814 } 815 816 static u32 bnxt_get_rxfh_key_size(struct net_device *dev) 817 { 818 return HW_HASH_KEY_SIZE; 819 } 820 821 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, 822 u8 *hfunc) 823 { 824 struct bnxt *bp = netdev_priv(dev); 825 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 826 int i = 0; 827 828 if (hfunc) 829 *hfunc = ETH_RSS_HASH_TOP; 830 831 if (indir) 832 for (i = 0; i < HW_HASH_INDEX_SIZE; i++) 833 indir[i] = le16_to_cpu(vnic->rss_table[i]); 834 835 if (key) 836 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE); 837 838 return 0; 839 } 840 841 static void bnxt_get_drvinfo(struct net_device *dev, 842 struct ethtool_drvinfo *info) 843 { 844 struct bnxt *bp = netdev_priv(dev); 845 846 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 847 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); 848 strlcpy(info->fw_version, bp->fw_ver_str, sizeof(info->fw_version)); 849 strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info)); 850 info->n_stats = bnxt_get_num_stats(bp); 851 info->testinfo_len = bp->num_tests; 852 /* TODO CHIMP_FW: eeprom dump details */ 853 info->eedump_len = 0; 854 /* TODO CHIMP FW: reg dump details */ 855 info->regdump_len = 0; 856 } 857 858 static void bnxt_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 859 { 860 struct bnxt *bp = netdev_priv(dev); 861 862 wol->supported = 0; 863 wol->wolopts = 0; 864 memset(&wol->sopass, 0, sizeof(wol->sopass)); 865 if (bp->flags & BNXT_FLAG_WOL_CAP) { 866 wol->supported = WAKE_MAGIC; 867 if (bp->wol) 868 wol->wolopts = WAKE_MAGIC; 869 } 870 } 871 872 static int bnxt_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 873 { 874 struct bnxt *bp = netdev_priv(dev); 875 876 if (wol->wolopts & ~WAKE_MAGIC) 877 return -EINVAL; 878 879 if (wol->wolopts & WAKE_MAGIC) { 880 if (!(bp->flags & BNXT_FLAG_WOL_CAP)) 881 return -EINVAL; 882 if (!bp->wol) { 883 if (bnxt_hwrm_alloc_wol_fltr(bp)) 884 return -EBUSY; 885 bp->wol = 1; 886 } 887 } else { 888 if (bp->wol) { 889 if (bnxt_hwrm_free_wol_fltr(bp)) 890 return -EBUSY; 891 bp->wol = 0; 892 } 893 } 894 return 0; 895 } 896 897 u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause) 898 { 899 u32 speed_mask = 0; 900 901 /* TODO: support 25GB, 40GB, 50GB with different cable type */ 902 /* set the advertised speeds */ 903 if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB) 904 speed_mask |= ADVERTISED_100baseT_Full; 905 if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB) 906 speed_mask |= ADVERTISED_1000baseT_Full; 907 if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB) 908 speed_mask |= ADVERTISED_2500baseX_Full; 909 if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB) 910 speed_mask |= ADVERTISED_10000baseT_Full; 911 if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB) 912 speed_mask |= ADVERTISED_40000baseCR4_Full; 913 914 if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH) 915 speed_mask |= ADVERTISED_Pause; 916 else if (fw_pause & BNXT_LINK_PAUSE_TX) 917 speed_mask |= ADVERTISED_Asym_Pause; 918 else if (fw_pause & BNXT_LINK_PAUSE_RX) 919 speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause; 920 921 return speed_mask; 922 } 923 924 #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\ 925 { \ 926 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB) \ 927 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 928 100baseT_Full); \ 929 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB) \ 930 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 931 1000baseT_Full); \ 932 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB) \ 933 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 934 10000baseT_Full); \ 935 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB) \ 936 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 937 25000baseCR_Full); \ 938 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB) \ 939 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 940 40000baseCR4_Full);\ 941 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB) \ 942 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 943 50000baseCR2_Full);\ 944 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100GB) \ 945 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 946 100000baseCR4_Full);\ 947 if ((fw_pause) & BNXT_LINK_PAUSE_RX) { \ 948 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 949 Pause); \ 950 if (!((fw_pause) & BNXT_LINK_PAUSE_TX)) \ 951 ethtool_link_ksettings_add_link_mode( \ 952 lk_ksettings, name, Asym_Pause);\ 953 } else if ((fw_pause) & BNXT_LINK_PAUSE_TX) { \ 954 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 955 Asym_Pause); \ 956 } \ 957 } 958 959 #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name) \ 960 { \ 961 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 962 100baseT_Full) || \ 963 ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 964 100baseT_Half)) \ 965 (fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB; \ 966 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 967 1000baseT_Full) || \ 968 ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 969 1000baseT_Half)) \ 970 (fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB; \ 971 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 972 10000baseT_Full)) \ 973 (fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB; \ 974 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 975 25000baseCR_Full)) \ 976 (fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB; \ 977 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 978 40000baseCR4_Full)) \ 979 (fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB; \ 980 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 981 50000baseCR2_Full)) \ 982 (fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB; \ 983 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 984 100000baseCR4_Full)) \ 985 (fw_speeds) |= BNXT_LINK_SPEED_MSK_100GB; \ 986 } 987 988 static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info, 989 struct ethtool_link_ksettings *lk_ksettings) 990 { 991 u16 fw_speeds = link_info->advertising; 992 u8 fw_pause = 0; 993 994 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 995 fw_pause = link_info->auto_pause_setting; 996 997 BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising); 998 } 999 1000 static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info, 1001 struct ethtool_link_ksettings *lk_ksettings) 1002 { 1003 u16 fw_speeds = link_info->lp_auto_link_speeds; 1004 u8 fw_pause = 0; 1005 1006 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 1007 fw_pause = link_info->lp_pause; 1008 1009 BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, 1010 lp_advertising); 1011 } 1012 1013 static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info, 1014 struct ethtool_link_ksettings *lk_ksettings) 1015 { 1016 u16 fw_speeds = link_info->support_speeds; 1017 1018 BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported); 1019 1020 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause); 1021 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 1022 Asym_Pause); 1023 1024 if (link_info->support_auto_speeds) 1025 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 1026 Autoneg); 1027 } 1028 1029 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed) 1030 { 1031 switch (fw_link_speed) { 1032 case BNXT_LINK_SPEED_100MB: 1033 return SPEED_100; 1034 case BNXT_LINK_SPEED_1GB: 1035 return SPEED_1000; 1036 case BNXT_LINK_SPEED_2_5GB: 1037 return SPEED_2500; 1038 case BNXT_LINK_SPEED_10GB: 1039 return SPEED_10000; 1040 case BNXT_LINK_SPEED_20GB: 1041 return SPEED_20000; 1042 case BNXT_LINK_SPEED_25GB: 1043 return SPEED_25000; 1044 case BNXT_LINK_SPEED_40GB: 1045 return SPEED_40000; 1046 case BNXT_LINK_SPEED_50GB: 1047 return SPEED_50000; 1048 case BNXT_LINK_SPEED_100GB: 1049 return SPEED_100000; 1050 default: 1051 return SPEED_UNKNOWN; 1052 } 1053 } 1054 1055 static int bnxt_get_link_ksettings(struct net_device *dev, 1056 struct ethtool_link_ksettings *lk_ksettings) 1057 { 1058 struct bnxt *bp = netdev_priv(dev); 1059 struct bnxt_link_info *link_info = &bp->link_info; 1060 struct ethtool_link_settings *base = &lk_ksettings->base; 1061 u32 ethtool_speed; 1062 1063 ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported); 1064 mutex_lock(&bp->link_lock); 1065 bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings); 1066 1067 ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising); 1068 if (link_info->autoneg) { 1069 bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings); 1070 ethtool_link_ksettings_add_link_mode(lk_ksettings, 1071 advertising, Autoneg); 1072 base->autoneg = AUTONEG_ENABLE; 1073 if (link_info->phy_link_status == BNXT_LINK_LINK) 1074 bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings); 1075 ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed); 1076 if (!netif_carrier_ok(dev)) 1077 base->duplex = DUPLEX_UNKNOWN; 1078 else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL) 1079 base->duplex = DUPLEX_FULL; 1080 else 1081 base->duplex = DUPLEX_HALF; 1082 } else { 1083 base->autoneg = AUTONEG_DISABLE; 1084 ethtool_speed = 1085 bnxt_fw_to_ethtool_speed(link_info->req_link_speed); 1086 base->duplex = DUPLEX_HALF; 1087 if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL) 1088 base->duplex = DUPLEX_FULL; 1089 } 1090 base->speed = ethtool_speed; 1091 1092 base->port = PORT_NONE; 1093 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) { 1094 base->port = PORT_TP; 1095 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 1096 TP); 1097 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising, 1098 TP); 1099 } else { 1100 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 1101 FIBRE); 1102 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising, 1103 FIBRE); 1104 1105 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC) 1106 base->port = PORT_DA; 1107 else if (link_info->media_type == 1108 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE) 1109 base->port = PORT_FIBRE; 1110 } 1111 base->phy_address = link_info->phy_addr; 1112 mutex_unlock(&bp->link_lock); 1113 1114 return 0; 1115 } 1116 1117 static u32 bnxt_get_fw_speed(struct net_device *dev, u32 ethtool_speed) 1118 { 1119 struct bnxt *bp = netdev_priv(dev); 1120 struct bnxt_link_info *link_info = &bp->link_info; 1121 u16 support_spds = link_info->support_speeds; 1122 u32 fw_speed = 0; 1123 1124 switch (ethtool_speed) { 1125 case SPEED_100: 1126 if (support_spds & BNXT_LINK_SPEED_MSK_100MB) 1127 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB; 1128 break; 1129 case SPEED_1000: 1130 if (support_spds & BNXT_LINK_SPEED_MSK_1GB) 1131 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB; 1132 break; 1133 case SPEED_2500: 1134 if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB) 1135 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB; 1136 break; 1137 case SPEED_10000: 1138 if (support_spds & BNXT_LINK_SPEED_MSK_10GB) 1139 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB; 1140 break; 1141 case SPEED_20000: 1142 if (support_spds & BNXT_LINK_SPEED_MSK_20GB) 1143 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB; 1144 break; 1145 case SPEED_25000: 1146 if (support_spds & BNXT_LINK_SPEED_MSK_25GB) 1147 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB; 1148 break; 1149 case SPEED_40000: 1150 if (support_spds & BNXT_LINK_SPEED_MSK_40GB) 1151 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB; 1152 break; 1153 case SPEED_50000: 1154 if (support_spds & BNXT_LINK_SPEED_MSK_50GB) 1155 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB; 1156 break; 1157 case SPEED_100000: 1158 if (support_spds & BNXT_LINK_SPEED_MSK_100GB) 1159 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100GB; 1160 break; 1161 default: 1162 netdev_err(dev, "unsupported speed!\n"); 1163 break; 1164 } 1165 return fw_speed; 1166 } 1167 1168 u16 bnxt_get_fw_auto_link_speeds(u32 advertising) 1169 { 1170 u16 fw_speed_mask = 0; 1171 1172 /* only support autoneg at speed 100, 1000, and 10000 */ 1173 if (advertising & (ADVERTISED_100baseT_Full | 1174 ADVERTISED_100baseT_Half)) { 1175 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB; 1176 } 1177 if (advertising & (ADVERTISED_1000baseT_Full | 1178 ADVERTISED_1000baseT_Half)) { 1179 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB; 1180 } 1181 if (advertising & ADVERTISED_10000baseT_Full) 1182 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB; 1183 1184 if (advertising & ADVERTISED_40000baseCR4_Full) 1185 fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB; 1186 1187 return fw_speed_mask; 1188 } 1189 1190 static int bnxt_set_link_ksettings(struct net_device *dev, 1191 const struct ethtool_link_ksettings *lk_ksettings) 1192 { 1193 struct bnxt *bp = netdev_priv(dev); 1194 struct bnxt_link_info *link_info = &bp->link_info; 1195 const struct ethtool_link_settings *base = &lk_ksettings->base; 1196 bool set_pause = false; 1197 u16 fw_advertising = 0; 1198 u32 speed; 1199 int rc = 0; 1200 1201 if (!BNXT_SINGLE_PF(bp)) 1202 return -EOPNOTSUPP; 1203 1204 mutex_lock(&bp->link_lock); 1205 if (base->autoneg == AUTONEG_ENABLE) { 1206 BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings, 1207 advertising); 1208 link_info->autoneg |= BNXT_AUTONEG_SPEED; 1209 if (!fw_advertising) 1210 link_info->advertising = link_info->support_auto_speeds; 1211 else 1212 link_info->advertising = fw_advertising; 1213 /* any change to autoneg will cause link change, therefore the 1214 * driver should put back the original pause setting in autoneg 1215 */ 1216 set_pause = true; 1217 } else { 1218 u16 fw_speed; 1219 u8 phy_type = link_info->phy_type; 1220 1221 if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET || 1222 phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE || 1223 link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) { 1224 netdev_err(dev, "10GBase-T devices must autoneg\n"); 1225 rc = -EINVAL; 1226 goto set_setting_exit; 1227 } 1228 if (base->duplex == DUPLEX_HALF) { 1229 netdev_err(dev, "HALF DUPLEX is not supported!\n"); 1230 rc = -EINVAL; 1231 goto set_setting_exit; 1232 } 1233 speed = base->speed; 1234 fw_speed = bnxt_get_fw_speed(dev, speed); 1235 if (!fw_speed) { 1236 rc = -EINVAL; 1237 goto set_setting_exit; 1238 } 1239 link_info->req_link_speed = fw_speed; 1240 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL; 1241 link_info->autoneg = 0; 1242 link_info->advertising = 0; 1243 } 1244 1245 if (netif_running(dev)) 1246 rc = bnxt_hwrm_set_link_setting(bp, set_pause, false); 1247 1248 set_setting_exit: 1249 mutex_unlock(&bp->link_lock); 1250 return rc; 1251 } 1252 1253 static void bnxt_get_pauseparam(struct net_device *dev, 1254 struct ethtool_pauseparam *epause) 1255 { 1256 struct bnxt *bp = netdev_priv(dev); 1257 struct bnxt_link_info *link_info = &bp->link_info; 1258 1259 if (BNXT_VF(bp)) 1260 return; 1261 epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL); 1262 epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX); 1263 epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX); 1264 } 1265 1266 static int bnxt_set_pauseparam(struct net_device *dev, 1267 struct ethtool_pauseparam *epause) 1268 { 1269 int rc = 0; 1270 struct bnxt *bp = netdev_priv(dev); 1271 struct bnxt_link_info *link_info = &bp->link_info; 1272 1273 if (!BNXT_SINGLE_PF(bp)) 1274 return -EOPNOTSUPP; 1275 1276 if (epause->autoneg) { 1277 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) 1278 return -EINVAL; 1279 1280 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 1281 if (bp->hwrm_spec_code >= 0x10201) 1282 link_info->req_flow_ctrl = 1283 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 1284 } else { 1285 /* when transition from auto pause to force pause, 1286 * force a link change 1287 */ 1288 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 1289 link_info->force_link_chng = true; 1290 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL; 1291 link_info->req_flow_ctrl = 0; 1292 } 1293 if (epause->rx_pause) 1294 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX; 1295 1296 if (epause->tx_pause) 1297 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX; 1298 1299 if (netif_running(dev)) 1300 rc = bnxt_hwrm_set_pause(bp); 1301 return rc; 1302 } 1303 1304 static u32 bnxt_get_link(struct net_device *dev) 1305 { 1306 struct bnxt *bp = netdev_priv(dev); 1307 1308 /* TODO: handle MF, VF, driver close case */ 1309 return bp->link_info.link_up; 1310 } 1311 1312 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal, 1313 u16 ext, u16 *index, u32 *item_length, 1314 u32 *data_length); 1315 1316 static int bnxt_flash_nvram(struct net_device *dev, 1317 u16 dir_type, 1318 u16 dir_ordinal, 1319 u16 dir_ext, 1320 u16 dir_attr, 1321 const u8 *data, 1322 size_t data_len) 1323 { 1324 struct bnxt *bp = netdev_priv(dev); 1325 int rc; 1326 struct hwrm_nvm_write_input req = {0}; 1327 dma_addr_t dma_handle; 1328 u8 *kmem; 1329 1330 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1); 1331 1332 req.dir_type = cpu_to_le16(dir_type); 1333 req.dir_ordinal = cpu_to_le16(dir_ordinal); 1334 req.dir_ext = cpu_to_le16(dir_ext); 1335 req.dir_attr = cpu_to_le16(dir_attr); 1336 req.dir_data_length = cpu_to_le32(data_len); 1337 1338 kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle, 1339 GFP_KERNEL); 1340 if (!kmem) { 1341 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n", 1342 (unsigned)data_len); 1343 return -ENOMEM; 1344 } 1345 memcpy(kmem, data, data_len); 1346 req.host_src_addr = cpu_to_le64(dma_handle); 1347 1348 rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT); 1349 dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle); 1350 1351 return rc; 1352 } 1353 1354 static int bnxt_firmware_reset(struct net_device *dev, 1355 u16 dir_type) 1356 { 1357 struct bnxt *bp = netdev_priv(dev); 1358 struct hwrm_fw_reset_input req = {0}; 1359 1360 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1); 1361 1362 /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */ 1363 /* (e.g. when firmware isn't already running) */ 1364 switch (dir_type) { 1365 case BNX_DIR_TYPE_CHIMP_PATCH: 1366 case BNX_DIR_TYPE_BOOTCODE: 1367 case BNX_DIR_TYPE_BOOTCODE_2: 1368 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT; 1369 /* Self-reset ChiMP upon next PCIe reset: */ 1370 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST; 1371 break; 1372 case BNX_DIR_TYPE_APE_FW: 1373 case BNX_DIR_TYPE_APE_PATCH: 1374 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT; 1375 /* Self-reset APE upon next PCIe reset: */ 1376 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST; 1377 break; 1378 case BNX_DIR_TYPE_KONG_FW: 1379 case BNX_DIR_TYPE_KONG_PATCH: 1380 req.embedded_proc_type = 1381 FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL; 1382 break; 1383 case BNX_DIR_TYPE_BONO_FW: 1384 case BNX_DIR_TYPE_BONO_PATCH: 1385 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE; 1386 break; 1387 case BNXT_FW_RESET_CHIP: 1388 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 1389 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 1390 break; 1391 case BNXT_FW_RESET_AP: 1392 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_AP; 1393 break; 1394 default: 1395 return -EINVAL; 1396 } 1397 1398 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1399 } 1400 1401 static int bnxt_flash_firmware(struct net_device *dev, 1402 u16 dir_type, 1403 const u8 *fw_data, 1404 size_t fw_size) 1405 { 1406 int rc = 0; 1407 u16 code_type; 1408 u32 stored_crc; 1409 u32 calculated_crc; 1410 struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data; 1411 1412 switch (dir_type) { 1413 case BNX_DIR_TYPE_BOOTCODE: 1414 case BNX_DIR_TYPE_BOOTCODE_2: 1415 code_type = CODE_BOOT; 1416 break; 1417 case BNX_DIR_TYPE_CHIMP_PATCH: 1418 code_type = CODE_CHIMP_PATCH; 1419 break; 1420 case BNX_DIR_TYPE_APE_FW: 1421 code_type = CODE_MCTP_PASSTHRU; 1422 break; 1423 case BNX_DIR_TYPE_APE_PATCH: 1424 code_type = CODE_APE_PATCH; 1425 break; 1426 case BNX_DIR_TYPE_KONG_FW: 1427 code_type = CODE_KONG_FW; 1428 break; 1429 case BNX_DIR_TYPE_KONG_PATCH: 1430 code_type = CODE_KONG_PATCH; 1431 break; 1432 case BNX_DIR_TYPE_BONO_FW: 1433 code_type = CODE_BONO_FW; 1434 break; 1435 case BNX_DIR_TYPE_BONO_PATCH: 1436 code_type = CODE_BONO_PATCH; 1437 break; 1438 default: 1439 netdev_err(dev, "Unsupported directory entry type: %u\n", 1440 dir_type); 1441 return -EINVAL; 1442 } 1443 if (fw_size < sizeof(struct bnxt_fw_header)) { 1444 netdev_err(dev, "Invalid firmware file size: %u\n", 1445 (unsigned int)fw_size); 1446 return -EINVAL; 1447 } 1448 if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) { 1449 netdev_err(dev, "Invalid firmware signature: %08X\n", 1450 le32_to_cpu(header->signature)); 1451 return -EINVAL; 1452 } 1453 if (header->code_type != code_type) { 1454 netdev_err(dev, "Expected firmware type: %d, read: %d\n", 1455 code_type, header->code_type); 1456 return -EINVAL; 1457 } 1458 if (header->device != DEVICE_CUMULUS_FAMILY) { 1459 netdev_err(dev, "Expected firmware device family %d, read: %d\n", 1460 DEVICE_CUMULUS_FAMILY, header->device); 1461 return -EINVAL; 1462 } 1463 /* Confirm the CRC32 checksum of the file: */ 1464 stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size - 1465 sizeof(stored_crc))); 1466 calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc)); 1467 if (calculated_crc != stored_crc) { 1468 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n", 1469 (unsigned long)stored_crc, 1470 (unsigned long)calculated_crc); 1471 return -EINVAL; 1472 } 1473 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST, 1474 0, 0, fw_data, fw_size); 1475 if (rc == 0) /* Firmware update successful */ 1476 rc = bnxt_firmware_reset(dev, dir_type); 1477 1478 return rc; 1479 } 1480 1481 static int bnxt_flash_microcode(struct net_device *dev, 1482 u16 dir_type, 1483 const u8 *fw_data, 1484 size_t fw_size) 1485 { 1486 struct bnxt_ucode_trailer *trailer; 1487 u32 calculated_crc; 1488 u32 stored_crc; 1489 int rc = 0; 1490 1491 if (fw_size < sizeof(struct bnxt_ucode_trailer)) { 1492 netdev_err(dev, "Invalid microcode file size: %u\n", 1493 (unsigned int)fw_size); 1494 return -EINVAL; 1495 } 1496 trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size - 1497 sizeof(*trailer))); 1498 if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) { 1499 netdev_err(dev, "Invalid microcode trailer signature: %08X\n", 1500 le32_to_cpu(trailer->sig)); 1501 return -EINVAL; 1502 } 1503 if (le16_to_cpu(trailer->dir_type) != dir_type) { 1504 netdev_err(dev, "Expected microcode type: %d, read: %d\n", 1505 dir_type, le16_to_cpu(trailer->dir_type)); 1506 return -EINVAL; 1507 } 1508 if (le16_to_cpu(trailer->trailer_length) < 1509 sizeof(struct bnxt_ucode_trailer)) { 1510 netdev_err(dev, "Invalid microcode trailer length: %d\n", 1511 le16_to_cpu(trailer->trailer_length)); 1512 return -EINVAL; 1513 } 1514 1515 /* Confirm the CRC32 checksum of the file: */ 1516 stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size - 1517 sizeof(stored_crc))); 1518 calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc)); 1519 if (calculated_crc != stored_crc) { 1520 netdev_err(dev, 1521 "CRC32 (%08lX) does not match calculated: %08lX\n", 1522 (unsigned long)stored_crc, 1523 (unsigned long)calculated_crc); 1524 return -EINVAL; 1525 } 1526 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST, 1527 0, 0, fw_data, fw_size); 1528 1529 return rc; 1530 } 1531 1532 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type) 1533 { 1534 switch (dir_type) { 1535 case BNX_DIR_TYPE_CHIMP_PATCH: 1536 case BNX_DIR_TYPE_BOOTCODE: 1537 case BNX_DIR_TYPE_BOOTCODE_2: 1538 case BNX_DIR_TYPE_APE_FW: 1539 case BNX_DIR_TYPE_APE_PATCH: 1540 case BNX_DIR_TYPE_KONG_FW: 1541 case BNX_DIR_TYPE_KONG_PATCH: 1542 case BNX_DIR_TYPE_BONO_FW: 1543 case BNX_DIR_TYPE_BONO_PATCH: 1544 return true; 1545 } 1546 1547 return false; 1548 } 1549 1550 static bool bnxt_dir_type_is_other_exec_format(u16 dir_type) 1551 { 1552 switch (dir_type) { 1553 case BNX_DIR_TYPE_AVS: 1554 case BNX_DIR_TYPE_EXP_ROM_MBA: 1555 case BNX_DIR_TYPE_PCIE: 1556 case BNX_DIR_TYPE_TSCF_UCODE: 1557 case BNX_DIR_TYPE_EXT_PHY: 1558 case BNX_DIR_TYPE_CCM: 1559 case BNX_DIR_TYPE_ISCSI_BOOT: 1560 case BNX_DIR_TYPE_ISCSI_BOOT_IPV6: 1561 case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6: 1562 return true; 1563 } 1564 1565 return false; 1566 } 1567 1568 static bool bnxt_dir_type_is_executable(u16 dir_type) 1569 { 1570 return bnxt_dir_type_is_ape_bin_format(dir_type) || 1571 bnxt_dir_type_is_other_exec_format(dir_type); 1572 } 1573 1574 static int bnxt_flash_firmware_from_file(struct net_device *dev, 1575 u16 dir_type, 1576 const char *filename) 1577 { 1578 const struct firmware *fw; 1579 int rc; 1580 1581 rc = request_firmware(&fw, filename, &dev->dev); 1582 if (rc != 0) { 1583 netdev_err(dev, "Error %d requesting firmware file: %s\n", 1584 rc, filename); 1585 return rc; 1586 } 1587 if (bnxt_dir_type_is_ape_bin_format(dir_type) == true) 1588 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size); 1589 else if (bnxt_dir_type_is_other_exec_format(dir_type) == true) 1590 rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size); 1591 else 1592 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST, 1593 0, 0, fw->data, fw->size); 1594 release_firmware(fw); 1595 return rc; 1596 } 1597 1598 static int bnxt_flash_package_from_file(struct net_device *dev, 1599 char *filename, u32 install_type) 1600 { 1601 struct bnxt *bp = netdev_priv(dev); 1602 struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr; 1603 struct hwrm_nvm_install_update_input install = {0}; 1604 const struct firmware *fw; 1605 u32 item_len; 1606 u16 index; 1607 int rc; 1608 1609 bnxt_hwrm_fw_set_time(bp); 1610 1611 if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE, 1612 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE, 1613 &index, &item_len, NULL) != 0) { 1614 netdev_err(dev, "PKG update area not created in nvram\n"); 1615 return -ENOBUFS; 1616 } 1617 1618 rc = request_firmware(&fw, filename, &dev->dev); 1619 if (rc != 0) { 1620 netdev_err(dev, "PKG error %d requesting file: %s\n", 1621 rc, filename); 1622 return rc; 1623 } 1624 1625 if (fw->size > item_len) { 1626 netdev_err(dev, "PKG insufficient update area in nvram: %lu", 1627 (unsigned long)fw->size); 1628 rc = -EFBIG; 1629 } else { 1630 dma_addr_t dma_handle; 1631 u8 *kmem; 1632 struct hwrm_nvm_modify_input modify = {0}; 1633 1634 bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1); 1635 1636 modify.dir_idx = cpu_to_le16(index); 1637 modify.len = cpu_to_le32(fw->size); 1638 1639 kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size, 1640 &dma_handle, GFP_KERNEL); 1641 if (!kmem) { 1642 netdev_err(dev, 1643 "dma_alloc_coherent failure, length = %u\n", 1644 (unsigned int)fw->size); 1645 rc = -ENOMEM; 1646 } else { 1647 memcpy(kmem, fw->data, fw->size); 1648 modify.host_src_addr = cpu_to_le64(dma_handle); 1649 1650 rc = hwrm_send_message(bp, &modify, sizeof(modify), 1651 FLASH_PACKAGE_TIMEOUT); 1652 dma_free_coherent(&bp->pdev->dev, fw->size, kmem, 1653 dma_handle); 1654 } 1655 } 1656 release_firmware(fw); 1657 if (rc) 1658 return rc; 1659 1660 if ((install_type & 0xffff) == 0) 1661 install_type >>= 16; 1662 bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1); 1663 install.install_type = cpu_to_le32(install_type); 1664 1665 mutex_lock(&bp->hwrm_cmd_lock); 1666 rc = _hwrm_send_message(bp, &install, sizeof(install), 1667 INSTALL_PACKAGE_TIMEOUT); 1668 if (rc) { 1669 rc = -EOPNOTSUPP; 1670 goto flash_pkg_exit; 1671 } 1672 1673 if (resp->error_code) { 1674 u8 error_code = ((struct hwrm_err_output *)resp)->cmd_err; 1675 1676 if (error_code == NVM_INSTALL_UPDATE_CMD_ERR_CODE_FRAG_ERR) { 1677 install.flags |= cpu_to_le16( 1678 NVM_INSTALL_UPDATE_REQ_FLAGS_ALLOWED_TO_DEFRAG); 1679 rc = _hwrm_send_message(bp, &install, sizeof(install), 1680 INSTALL_PACKAGE_TIMEOUT); 1681 if (rc) { 1682 rc = -EOPNOTSUPP; 1683 goto flash_pkg_exit; 1684 } 1685 } 1686 } 1687 1688 if (resp->result) { 1689 netdev_err(dev, "PKG install error = %d, problem_item = %d\n", 1690 (s8)resp->result, (int)resp->problem_item); 1691 rc = -ENOPKG; 1692 } 1693 flash_pkg_exit: 1694 mutex_unlock(&bp->hwrm_cmd_lock); 1695 return rc; 1696 } 1697 1698 static int bnxt_flash_device(struct net_device *dev, 1699 struct ethtool_flash *flash) 1700 { 1701 if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) { 1702 netdev_err(dev, "flashdev not supported from a virtual function\n"); 1703 return -EINVAL; 1704 } 1705 1706 if (flash->region == ETHTOOL_FLASH_ALL_REGIONS || 1707 flash->region > 0xffff) 1708 return bnxt_flash_package_from_file(dev, flash->data, 1709 flash->region); 1710 1711 return bnxt_flash_firmware_from_file(dev, flash->region, flash->data); 1712 } 1713 1714 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length) 1715 { 1716 struct bnxt *bp = netdev_priv(dev); 1717 int rc; 1718 struct hwrm_nvm_get_dir_info_input req = {0}; 1719 struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr; 1720 1721 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1); 1722 1723 mutex_lock(&bp->hwrm_cmd_lock); 1724 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1725 if (!rc) { 1726 *entries = le32_to_cpu(output->entries); 1727 *length = le32_to_cpu(output->entry_length); 1728 } 1729 mutex_unlock(&bp->hwrm_cmd_lock); 1730 return rc; 1731 } 1732 1733 static int bnxt_get_eeprom_len(struct net_device *dev) 1734 { 1735 /* The -1 return value allows the entire 32-bit range of offsets to be 1736 * passed via the ethtool command-line utility. 1737 */ 1738 return -1; 1739 } 1740 1741 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data) 1742 { 1743 struct bnxt *bp = netdev_priv(dev); 1744 int rc; 1745 u32 dir_entries; 1746 u32 entry_length; 1747 u8 *buf; 1748 size_t buflen; 1749 dma_addr_t dma_handle; 1750 struct hwrm_nvm_get_dir_entries_input req = {0}; 1751 1752 rc = nvm_get_dir_info(dev, &dir_entries, &entry_length); 1753 if (rc != 0) 1754 return rc; 1755 1756 /* Insert 2 bytes of directory info (count and size of entries) */ 1757 if (len < 2) 1758 return -EINVAL; 1759 1760 *data++ = dir_entries; 1761 *data++ = entry_length; 1762 len -= 2; 1763 memset(data, 0xff, len); 1764 1765 buflen = dir_entries * entry_length; 1766 buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle, 1767 GFP_KERNEL); 1768 if (!buf) { 1769 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n", 1770 (unsigned)buflen); 1771 return -ENOMEM; 1772 } 1773 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1); 1774 req.host_dest_addr = cpu_to_le64(dma_handle); 1775 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1776 if (rc == 0) 1777 memcpy(data, buf, len > buflen ? buflen : len); 1778 dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle); 1779 return rc; 1780 } 1781 1782 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset, 1783 u32 length, u8 *data) 1784 { 1785 struct bnxt *bp = netdev_priv(dev); 1786 int rc; 1787 u8 *buf; 1788 dma_addr_t dma_handle; 1789 struct hwrm_nvm_read_input req = {0}; 1790 1791 if (!length) 1792 return -EINVAL; 1793 1794 buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle, 1795 GFP_KERNEL); 1796 if (!buf) { 1797 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n", 1798 (unsigned)length); 1799 return -ENOMEM; 1800 } 1801 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1); 1802 req.host_dest_addr = cpu_to_le64(dma_handle); 1803 req.dir_idx = cpu_to_le16(index); 1804 req.offset = cpu_to_le32(offset); 1805 req.len = cpu_to_le32(length); 1806 1807 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1808 if (rc == 0) 1809 memcpy(data, buf, length); 1810 dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle); 1811 return rc; 1812 } 1813 1814 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal, 1815 u16 ext, u16 *index, u32 *item_length, 1816 u32 *data_length) 1817 { 1818 struct bnxt *bp = netdev_priv(dev); 1819 int rc; 1820 struct hwrm_nvm_find_dir_entry_input req = {0}; 1821 struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr; 1822 1823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1); 1824 req.enables = 0; 1825 req.dir_idx = 0; 1826 req.dir_type = cpu_to_le16(type); 1827 req.dir_ordinal = cpu_to_le16(ordinal); 1828 req.dir_ext = cpu_to_le16(ext); 1829 req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ; 1830 mutex_lock(&bp->hwrm_cmd_lock); 1831 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1832 if (rc == 0) { 1833 if (index) 1834 *index = le16_to_cpu(output->dir_idx); 1835 if (item_length) 1836 *item_length = le32_to_cpu(output->dir_item_length); 1837 if (data_length) 1838 *data_length = le32_to_cpu(output->dir_data_length); 1839 } 1840 mutex_unlock(&bp->hwrm_cmd_lock); 1841 return rc; 1842 } 1843 1844 static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen) 1845 { 1846 char *retval = NULL; 1847 char *p; 1848 char *value; 1849 int field = 0; 1850 1851 if (datalen < 1) 1852 return NULL; 1853 /* null-terminate the log data (removing last '\n'): */ 1854 data[datalen - 1] = 0; 1855 for (p = data; *p != 0; p++) { 1856 field = 0; 1857 retval = NULL; 1858 while (*p != 0 && *p != '\n') { 1859 value = p; 1860 while (*p != 0 && *p != '\t' && *p != '\n') 1861 p++; 1862 if (field == desired_field) 1863 retval = value; 1864 if (*p != '\t') 1865 break; 1866 *p = 0; 1867 field++; 1868 p++; 1869 } 1870 if (*p == 0) 1871 break; 1872 *p = 0; 1873 } 1874 return retval; 1875 } 1876 1877 static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen) 1878 { 1879 u16 index = 0; 1880 u32 datalen; 1881 1882 if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG, 1883 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE, 1884 &index, NULL, &datalen) != 0) 1885 return NULL; 1886 1887 memset(buf, 0, buflen); 1888 if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0) 1889 return NULL; 1890 1891 return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf, 1892 datalen); 1893 } 1894 1895 static int bnxt_get_eeprom(struct net_device *dev, 1896 struct ethtool_eeprom *eeprom, 1897 u8 *data) 1898 { 1899 u32 index; 1900 u32 offset; 1901 1902 if (eeprom->offset == 0) /* special offset value to get directory */ 1903 return bnxt_get_nvram_directory(dev, eeprom->len, data); 1904 1905 index = eeprom->offset >> 24; 1906 offset = eeprom->offset & 0xffffff; 1907 1908 if (index == 0) { 1909 netdev_err(dev, "unsupported index value: %d\n", index); 1910 return -EINVAL; 1911 } 1912 1913 return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data); 1914 } 1915 1916 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index) 1917 { 1918 struct bnxt *bp = netdev_priv(dev); 1919 struct hwrm_nvm_erase_dir_entry_input req = {0}; 1920 1921 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1); 1922 req.dir_idx = cpu_to_le16(index); 1923 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1924 } 1925 1926 static int bnxt_set_eeprom(struct net_device *dev, 1927 struct ethtool_eeprom *eeprom, 1928 u8 *data) 1929 { 1930 struct bnxt *bp = netdev_priv(dev); 1931 u8 index, dir_op; 1932 u16 type, ext, ordinal, attr; 1933 1934 if (!BNXT_PF(bp)) { 1935 netdev_err(dev, "NVM write not supported from a virtual function\n"); 1936 return -EINVAL; 1937 } 1938 1939 type = eeprom->magic >> 16; 1940 1941 if (type == 0xffff) { /* special value for directory operations */ 1942 index = eeprom->magic & 0xff; 1943 dir_op = eeprom->magic >> 8; 1944 if (index == 0) 1945 return -EINVAL; 1946 switch (dir_op) { 1947 case 0x0e: /* erase */ 1948 if (eeprom->offset != ~eeprom->magic) 1949 return -EINVAL; 1950 return bnxt_erase_nvram_directory(dev, index - 1); 1951 default: 1952 return -EINVAL; 1953 } 1954 } 1955 1956 /* Create or re-write an NVM item: */ 1957 if (bnxt_dir_type_is_executable(type) == true) 1958 return -EOPNOTSUPP; 1959 ext = eeprom->magic & 0xffff; 1960 ordinal = eeprom->offset >> 16; 1961 attr = eeprom->offset & 0xffff; 1962 1963 return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data, 1964 eeprom->len); 1965 } 1966 1967 static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata) 1968 { 1969 struct bnxt *bp = netdev_priv(dev); 1970 struct ethtool_eee *eee = &bp->eee; 1971 struct bnxt_link_info *link_info = &bp->link_info; 1972 u32 advertising = 1973 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0); 1974 int rc = 0; 1975 1976 if (!BNXT_SINGLE_PF(bp)) 1977 return -EOPNOTSUPP; 1978 1979 if (!(bp->flags & BNXT_FLAG_EEE_CAP)) 1980 return -EOPNOTSUPP; 1981 1982 if (!edata->eee_enabled) 1983 goto eee_ok; 1984 1985 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 1986 netdev_warn(dev, "EEE requires autoneg\n"); 1987 return -EINVAL; 1988 } 1989 if (edata->tx_lpi_enabled) { 1990 if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi || 1991 edata->tx_lpi_timer < bp->lpi_tmr_lo)) { 1992 netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n", 1993 bp->lpi_tmr_lo, bp->lpi_tmr_hi); 1994 return -EINVAL; 1995 } else if (!bp->lpi_tmr_hi) { 1996 edata->tx_lpi_timer = eee->tx_lpi_timer; 1997 } 1998 } 1999 if (!edata->advertised) { 2000 edata->advertised = advertising & eee->supported; 2001 } else if (edata->advertised & ~advertising) { 2002 netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n", 2003 edata->advertised, advertising); 2004 return -EINVAL; 2005 } 2006 2007 eee->advertised = edata->advertised; 2008 eee->tx_lpi_enabled = edata->tx_lpi_enabled; 2009 eee->tx_lpi_timer = edata->tx_lpi_timer; 2010 eee_ok: 2011 eee->eee_enabled = edata->eee_enabled; 2012 2013 if (netif_running(dev)) 2014 rc = bnxt_hwrm_set_link_setting(bp, false, true); 2015 2016 return rc; 2017 } 2018 2019 static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata) 2020 { 2021 struct bnxt *bp = netdev_priv(dev); 2022 2023 if (!(bp->flags & BNXT_FLAG_EEE_CAP)) 2024 return -EOPNOTSUPP; 2025 2026 *edata = bp->eee; 2027 if (!bp->eee.eee_enabled) { 2028 /* Preserve tx_lpi_timer so that the last value will be used 2029 * by default when it is re-enabled. 2030 */ 2031 edata->advertised = 0; 2032 edata->tx_lpi_enabled = 0; 2033 } 2034 2035 if (!bp->eee.eee_active) 2036 edata->lp_advertised = 0; 2037 2038 return 0; 2039 } 2040 2041 static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr, 2042 u16 page_number, u16 start_addr, 2043 u16 data_length, u8 *buf) 2044 { 2045 struct hwrm_port_phy_i2c_read_input req = {0}; 2046 struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr; 2047 int rc, byte_offset = 0; 2048 2049 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1); 2050 req.i2c_slave_addr = i2c_addr; 2051 req.page_number = cpu_to_le16(page_number); 2052 req.port_id = cpu_to_le16(bp->pf.port_id); 2053 do { 2054 u16 xfer_size; 2055 2056 xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE); 2057 data_length -= xfer_size; 2058 req.page_offset = cpu_to_le16(start_addr + byte_offset); 2059 req.data_length = xfer_size; 2060 req.enables = cpu_to_le32(start_addr + byte_offset ? 2061 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0); 2062 mutex_lock(&bp->hwrm_cmd_lock); 2063 rc = _hwrm_send_message(bp, &req, sizeof(req), 2064 HWRM_CMD_TIMEOUT); 2065 if (!rc) 2066 memcpy(buf + byte_offset, output->data, xfer_size); 2067 mutex_unlock(&bp->hwrm_cmd_lock); 2068 byte_offset += xfer_size; 2069 } while (!rc && data_length > 0); 2070 2071 return rc; 2072 } 2073 2074 static int bnxt_get_module_info(struct net_device *dev, 2075 struct ethtool_modinfo *modinfo) 2076 { 2077 struct bnxt *bp = netdev_priv(dev); 2078 struct hwrm_port_phy_i2c_read_input req = {0}; 2079 struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr; 2080 int rc; 2081 2082 /* No point in going further if phy status indicates 2083 * module is not inserted or if it is powered down or 2084 * if it is of type 10GBase-T 2085 */ 2086 if (bp->link_info.module_status > 2087 PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG) 2088 return -EOPNOTSUPP; 2089 2090 /* This feature is not supported in older firmware versions */ 2091 if (bp->hwrm_spec_code < 0x10202) 2092 return -EOPNOTSUPP; 2093 2094 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1); 2095 req.i2c_slave_addr = I2C_DEV_ADDR_A0; 2096 req.page_number = 0; 2097 req.page_offset = cpu_to_le16(SFP_EEPROM_SFF_8472_COMP_ADDR); 2098 req.data_length = SFP_EEPROM_SFF_8472_COMP_SIZE; 2099 req.port_id = cpu_to_le16(bp->pf.port_id); 2100 mutex_lock(&bp->hwrm_cmd_lock); 2101 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2102 if (!rc) { 2103 u32 module_id = le32_to_cpu(output->data[0]); 2104 2105 switch (module_id) { 2106 case SFF_MODULE_ID_SFP: 2107 modinfo->type = ETH_MODULE_SFF_8472; 2108 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 2109 break; 2110 case SFF_MODULE_ID_QSFP: 2111 case SFF_MODULE_ID_QSFP_PLUS: 2112 modinfo->type = ETH_MODULE_SFF_8436; 2113 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN; 2114 break; 2115 case SFF_MODULE_ID_QSFP28: 2116 modinfo->type = ETH_MODULE_SFF_8636; 2117 modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN; 2118 break; 2119 default: 2120 rc = -EOPNOTSUPP; 2121 break; 2122 } 2123 } 2124 mutex_unlock(&bp->hwrm_cmd_lock); 2125 return rc; 2126 } 2127 2128 static int bnxt_get_module_eeprom(struct net_device *dev, 2129 struct ethtool_eeprom *eeprom, 2130 u8 *data) 2131 { 2132 struct bnxt *bp = netdev_priv(dev); 2133 u16 start = eeprom->offset, length = eeprom->len; 2134 int rc = 0; 2135 2136 memset(data, 0, eeprom->len); 2137 2138 /* Read A0 portion of the EEPROM */ 2139 if (start < ETH_MODULE_SFF_8436_LEN) { 2140 if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN) 2141 length = ETH_MODULE_SFF_8436_LEN - start; 2142 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 2143 start, length, data); 2144 if (rc) 2145 return rc; 2146 start += length; 2147 data += length; 2148 length = eeprom->len - length; 2149 } 2150 2151 /* Read A2 portion of the EEPROM */ 2152 if (length) { 2153 start -= ETH_MODULE_SFF_8436_LEN; 2154 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1, 2155 start, length, data); 2156 } 2157 return rc; 2158 } 2159 2160 static int bnxt_nway_reset(struct net_device *dev) 2161 { 2162 int rc = 0; 2163 2164 struct bnxt *bp = netdev_priv(dev); 2165 struct bnxt_link_info *link_info = &bp->link_info; 2166 2167 if (!BNXT_SINGLE_PF(bp)) 2168 return -EOPNOTSUPP; 2169 2170 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) 2171 return -EINVAL; 2172 2173 if (netif_running(dev)) 2174 rc = bnxt_hwrm_set_link_setting(bp, true, false); 2175 2176 return rc; 2177 } 2178 2179 static int bnxt_set_phys_id(struct net_device *dev, 2180 enum ethtool_phys_id_state state) 2181 { 2182 struct hwrm_port_led_cfg_input req = {0}; 2183 struct bnxt *bp = netdev_priv(dev); 2184 struct bnxt_pf_info *pf = &bp->pf; 2185 struct bnxt_led_cfg *led_cfg; 2186 u8 led_state; 2187 __le16 duration; 2188 int i, rc; 2189 2190 if (!bp->num_leds || BNXT_VF(bp)) 2191 return -EOPNOTSUPP; 2192 2193 if (state == ETHTOOL_ID_ACTIVE) { 2194 led_state = PORT_LED_CFG_REQ_LED0_STATE_BLINKALT; 2195 duration = cpu_to_le16(500); 2196 } else if (state == ETHTOOL_ID_INACTIVE) { 2197 led_state = PORT_LED_CFG_REQ_LED1_STATE_DEFAULT; 2198 duration = cpu_to_le16(0); 2199 } else { 2200 return -EINVAL; 2201 } 2202 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_CFG, -1, -1); 2203 req.port_id = cpu_to_le16(pf->port_id); 2204 req.num_leds = bp->num_leds; 2205 led_cfg = (struct bnxt_led_cfg *)&req.led0_id; 2206 for (i = 0; i < bp->num_leds; i++, led_cfg++) { 2207 req.enables |= BNXT_LED_DFLT_ENABLES(i); 2208 led_cfg->led_id = bp->leds[i].led_id; 2209 led_cfg->led_state = led_state; 2210 led_cfg->led_blink_on = duration; 2211 led_cfg->led_blink_off = duration; 2212 led_cfg->led_group_id = bp->leds[i].led_group_id; 2213 } 2214 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2215 if (rc) 2216 rc = -EIO; 2217 return rc; 2218 } 2219 2220 static int bnxt_hwrm_selftest_irq(struct bnxt *bp, u16 cmpl_ring) 2221 { 2222 struct hwrm_selftest_irq_input req = {0}; 2223 2224 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_IRQ, cmpl_ring, -1); 2225 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2226 } 2227 2228 static int bnxt_test_irq(struct bnxt *bp) 2229 { 2230 int i; 2231 2232 for (i = 0; i < bp->cp_nr_rings; i++) { 2233 u16 cmpl_ring = bp->grp_info[i].cp_fw_ring_id; 2234 int rc; 2235 2236 rc = bnxt_hwrm_selftest_irq(bp, cmpl_ring); 2237 if (rc) 2238 return rc; 2239 } 2240 return 0; 2241 } 2242 2243 static int bnxt_hwrm_mac_loopback(struct bnxt *bp, bool enable) 2244 { 2245 struct hwrm_port_mac_cfg_input req = {0}; 2246 2247 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1); 2248 2249 req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_LPBK); 2250 if (enable) 2251 req.lpbk = PORT_MAC_CFG_REQ_LPBK_LOCAL; 2252 else 2253 req.lpbk = PORT_MAC_CFG_REQ_LPBK_NONE; 2254 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2255 } 2256 2257 static int bnxt_disable_an_for_lpbk(struct bnxt *bp, 2258 struct hwrm_port_phy_cfg_input *req) 2259 { 2260 struct bnxt_link_info *link_info = &bp->link_info; 2261 u16 fw_advertising = link_info->advertising; 2262 u16 fw_speed; 2263 int rc; 2264 2265 if (!link_info->autoneg) 2266 return 0; 2267 2268 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_1GB; 2269 if (netif_carrier_ok(bp->dev)) 2270 fw_speed = bp->link_info.link_speed; 2271 else if (fw_advertising & BNXT_LINK_SPEED_MSK_10GB) 2272 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_10GB; 2273 else if (fw_advertising & BNXT_LINK_SPEED_MSK_25GB) 2274 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_25GB; 2275 else if (fw_advertising & BNXT_LINK_SPEED_MSK_40GB) 2276 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_40GB; 2277 else if (fw_advertising & BNXT_LINK_SPEED_MSK_50GB) 2278 fw_speed = PORT_PHY_CFG_REQ_FORCE_LINK_SPEED_50GB; 2279 2280 req->force_link_speed = cpu_to_le16(fw_speed); 2281 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE | 2282 PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 2283 rc = hwrm_send_message(bp, req, sizeof(*req), HWRM_CMD_TIMEOUT); 2284 req->flags = 0; 2285 req->force_link_speed = cpu_to_le16(0); 2286 return rc; 2287 } 2288 2289 static int bnxt_hwrm_phy_loopback(struct bnxt *bp, bool enable) 2290 { 2291 struct hwrm_port_phy_cfg_input req = {0}; 2292 2293 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1); 2294 2295 if (enable) { 2296 bnxt_disable_an_for_lpbk(bp, &req); 2297 req.lpbk = PORT_PHY_CFG_REQ_LPBK_LOCAL; 2298 } else { 2299 req.lpbk = PORT_PHY_CFG_REQ_LPBK_NONE; 2300 } 2301 req.enables = cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_LPBK); 2302 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2303 } 2304 2305 static int bnxt_rx_loopback(struct bnxt *bp, struct bnxt_napi *bnapi, 2306 u32 raw_cons, int pkt_size) 2307 { 2308 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2309 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2310 struct bnxt_sw_rx_bd *rx_buf; 2311 struct rx_cmp *rxcmp; 2312 u16 cp_cons, cons; 2313 u8 *data; 2314 u32 len; 2315 int i; 2316 2317 cp_cons = RING_CMP(raw_cons); 2318 rxcmp = (struct rx_cmp *) 2319 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2320 cons = rxcmp->rx_cmp_opaque; 2321 rx_buf = &rxr->rx_buf_ring[cons]; 2322 data = rx_buf->data_ptr; 2323 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT; 2324 if (len != pkt_size) 2325 return -EIO; 2326 i = ETH_ALEN; 2327 if (!ether_addr_equal(data + i, bnapi->bp->dev->dev_addr)) 2328 return -EIO; 2329 i += ETH_ALEN; 2330 for ( ; i < pkt_size; i++) { 2331 if (data[i] != (u8)(i & 0xff)) 2332 return -EIO; 2333 } 2334 return 0; 2335 } 2336 2337 static int bnxt_poll_loopback(struct bnxt *bp, int pkt_size) 2338 { 2339 struct bnxt_napi *bnapi = bp->bnapi[0]; 2340 struct bnxt_cp_ring_info *cpr; 2341 struct tx_cmp *txcmp; 2342 int rc = -EIO; 2343 u32 raw_cons; 2344 u32 cons; 2345 int i; 2346 2347 cpr = &bnapi->cp_ring; 2348 raw_cons = cpr->cp_raw_cons; 2349 for (i = 0; i < 200; i++) { 2350 cons = RING_CMP(raw_cons); 2351 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2352 2353 if (!TX_CMP_VALID(txcmp, raw_cons)) { 2354 udelay(5); 2355 continue; 2356 } 2357 2358 /* The valid test of the entry must be done first before 2359 * reading any further. 2360 */ 2361 dma_rmb(); 2362 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_RX_L2_CMP) { 2363 rc = bnxt_rx_loopback(bp, bnapi, raw_cons, pkt_size); 2364 raw_cons = NEXT_RAW_CMP(raw_cons); 2365 raw_cons = NEXT_RAW_CMP(raw_cons); 2366 break; 2367 } 2368 raw_cons = NEXT_RAW_CMP(raw_cons); 2369 } 2370 cpr->cp_raw_cons = raw_cons; 2371 return rc; 2372 } 2373 2374 static int bnxt_run_loopback(struct bnxt *bp) 2375 { 2376 struct bnxt_tx_ring_info *txr = &bp->tx_ring[0]; 2377 int pkt_size, i = 0; 2378 struct sk_buff *skb; 2379 dma_addr_t map; 2380 u8 *data; 2381 int rc; 2382 2383 pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_copy_thresh); 2384 skb = netdev_alloc_skb(bp->dev, pkt_size); 2385 if (!skb) 2386 return -ENOMEM; 2387 data = skb_put(skb, pkt_size); 2388 eth_broadcast_addr(data); 2389 i += ETH_ALEN; 2390 ether_addr_copy(&data[i], bp->dev->dev_addr); 2391 i += ETH_ALEN; 2392 for ( ; i < pkt_size; i++) 2393 data[i] = (u8)(i & 0xff); 2394 2395 map = dma_map_single(&bp->pdev->dev, skb->data, pkt_size, 2396 PCI_DMA_TODEVICE); 2397 if (dma_mapping_error(&bp->pdev->dev, map)) { 2398 dev_kfree_skb(skb); 2399 return -EIO; 2400 } 2401 bnxt_xmit_xdp(bp, txr, map, pkt_size, 0); 2402 2403 /* Sync BD data before updating doorbell */ 2404 wmb(); 2405 2406 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | txr->tx_prod); 2407 rc = bnxt_poll_loopback(bp, pkt_size); 2408 2409 dma_unmap_single(&bp->pdev->dev, map, pkt_size, PCI_DMA_TODEVICE); 2410 dev_kfree_skb(skb); 2411 return rc; 2412 } 2413 2414 static int bnxt_run_fw_tests(struct bnxt *bp, u8 test_mask, u8 *test_results) 2415 { 2416 struct hwrm_selftest_exec_output *resp = bp->hwrm_cmd_resp_addr; 2417 struct hwrm_selftest_exec_input req = {0}; 2418 int rc; 2419 2420 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_EXEC, -1, -1); 2421 mutex_lock(&bp->hwrm_cmd_lock); 2422 resp->test_success = 0; 2423 req.flags = test_mask; 2424 rc = _hwrm_send_message(bp, &req, sizeof(req), bp->test_info->timeout); 2425 *test_results = resp->test_success; 2426 mutex_unlock(&bp->hwrm_cmd_lock); 2427 return rc; 2428 } 2429 2430 #define BNXT_DRV_TESTS 3 2431 #define BNXT_MACLPBK_TEST_IDX (bp->num_tests - BNXT_DRV_TESTS) 2432 #define BNXT_PHYLPBK_TEST_IDX (BNXT_MACLPBK_TEST_IDX + 1) 2433 #define BNXT_IRQ_TEST_IDX (BNXT_MACLPBK_TEST_IDX + 2) 2434 2435 static void bnxt_self_test(struct net_device *dev, struct ethtool_test *etest, 2436 u64 *buf) 2437 { 2438 struct bnxt *bp = netdev_priv(dev); 2439 bool offline = false; 2440 u8 test_results = 0; 2441 u8 test_mask = 0; 2442 int rc, i; 2443 2444 if (!bp->num_tests || !BNXT_SINGLE_PF(bp)) 2445 return; 2446 memset(buf, 0, sizeof(u64) * bp->num_tests); 2447 if (!netif_running(dev)) { 2448 etest->flags |= ETH_TEST_FL_FAILED; 2449 return; 2450 } 2451 2452 if (etest->flags & ETH_TEST_FL_OFFLINE) { 2453 if (bp->pf.active_vfs) { 2454 etest->flags |= ETH_TEST_FL_FAILED; 2455 netdev_warn(dev, "Offline tests cannot be run with active VFs\n"); 2456 return; 2457 } 2458 offline = true; 2459 } 2460 2461 for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) { 2462 u8 bit_val = 1 << i; 2463 2464 if (!(bp->test_info->offline_mask & bit_val)) 2465 test_mask |= bit_val; 2466 else if (offline) 2467 test_mask |= bit_val; 2468 } 2469 if (!offline) { 2470 bnxt_run_fw_tests(bp, test_mask, &test_results); 2471 } else { 2472 rc = bnxt_close_nic(bp, false, false); 2473 if (rc) 2474 return; 2475 bnxt_run_fw_tests(bp, test_mask, &test_results); 2476 2477 buf[BNXT_MACLPBK_TEST_IDX] = 1; 2478 bnxt_hwrm_mac_loopback(bp, true); 2479 msleep(250); 2480 rc = bnxt_half_open_nic(bp); 2481 if (rc) { 2482 bnxt_hwrm_mac_loopback(bp, false); 2483 etest->flags |= ETH_TEST_FL_FAILED; 2484 return; 2485 } 2486 if (bnxt_run_loopback(bp)) 2487 etest->flags |= ETH_TEST_FL_FAILED; 2488 else 2489 buf[BNXT_MACLPBK_TEST_IDX] = 0; 2490 2491 bnxt_hwrm_mac_loopback(bp, false); 2492 bnxt_hwrm_phy_loopback(bp, true); 2493 msleep(1000); 2494 if (bnxt_run_loopback(bp)) { 2495 buf[BNXT_PHYLPBK_TEST_IDX] = 1; 2496 etest->flags |= ETH_TEST_FL_FAILED; 2497 } 2498 bnxt_hwrm_phy_loopback(bp, false); 2499 bnxt_half_close_nic(bp); 2500 bnxt_open_nic(bp, false, true); 2501 } 2502 if (bnxt_test_irq(bp)) { 2503 buf[BNXT_IRQ_TEST_IDX] = 1; 2504 etest->flags |= ETH_TEST_FL_FAILED; 2505 } 2506 for (i = 0; i < bp->num_tests - BNXT_DRV_TESTS; i++) { 2507 u8 bit_val = 1 << i; 2508 2509 if ((test_mask & bit_val) && !(test_results & bit_val)) { 2510 buf[i] = 1; 2511 etest->flags |= ETH_TEST_FL_FAILED; 2512 } 2513 } 2514 } 2515 2516 static int bnxt_reset(struct net_device *dev, u32 *flags) 2517 { 2518 struct bnxt *bp = netdev_priv(dev); 2519 int rc = 0; 2520 2521 if (!BNXT_PF(bp)) { 2522 netdev_err(dev, "Reset is not supported from a VF\n"); 2523 return -EOPNOTSUPP; 2524 } 2525 2526 if (pci_vfs_assigned(bp->pdev)) { 2527 netdev_err(dev, 2528 "Reset not allowed when VFs are assigned to VMs\n"); 2529 return -EBUSY; 2530 } 2531 2532 if (*flags == ETH_RESET_ALL) { 2533 /* This feature is not supported in older firmware versions */ 2534 if (bp->hwrm_spec_code < 0x10803) 2535 return -EOPNOTSUPP; 2536 2537 rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_CHIP); 2538 if (!rc) 2539 netdev_info(dev, "Reset request successful. Reload driver to complete reset\n"); 2540 } else if (*flags == ETH_RESET_AP) { 2541 /* This feature is not supported in older firmware versions */ 2542 if (bp->hwrm_spec_code < 0x10803) 2543 return -EOPNOTSUPP; 2544 2545 rc = bnxt_firmware_reset(dev, BNXT_FW_RESET_AP); 2546 if (!rc) 2547 netdev_info(dev, "Reset Application Processor request successful.\n"); 2548 } else { 2549 rc = -EINVAL; 2550 } 2551 2552 return rc; 2553 } 2554 2555 void bnxt_ethtool_init(struct bnxt *bp) 2556 { 2557 struct hwrm_selftest_qlist_output *resp = bp->hwrm_cmd_resp_addr; 2558 struct hwrm_selftest_qlist_input req = {0}; 2559 struct bnxt_test_info *test_info; 2560 struct net_device *dev = bp->dev; 2561 char *pkglog; 2562 int i, rc; 2563 2564 pkglog = kzalloc(BNX_PKG_LOG_MAX_LENGTH, GFP_KERNEL); 2565 if (pkglog) { 2566 char *pkgver; 2567 int len; 2568 2569 pkgver = bnxt_get_pkgver(dev, pkglog, BNX_PKG_LOG_MAX_LENGTH); 2570 if (pkgver && *pkgver != 0 && isdigit(*pkgver)) { 2571 len = strlen(bp->fw_ver_str); 2572 snprintf(bp->fw_ver_str + len, FW_VER_STR_LEN - len - 1, 2573 "/pkg %s", pkgver); 2574 } 2575 kfree(pkglog); 2576 } 2577 if (bp->hwrm_spec_code < 0x10704 || !BNXT_SINGLE_PF(bp)) 2578 return; 2579 2580 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_SELFTEST_QLIST, -1, -1); 2581 mutex_lock(&bp->hwrm_cmd_lock); 2582 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2583 if (rc) 2584 goto ethtool_init_exit; 2585 2586 test_info = kzalloc(sizeof(*bp->test_info), GFP_KERNEL); 2587 if (!test_info) 2588 goto ethtool_init_exit; 2589 2590 bp->test_info = test_info; 2591 bp->num_tests = resp->num_tests + BNXT_DRV_TESTS; 2592 if (bp->num_tests > BNXT_MAX_TEST) 2593 bp->num_tests = BNXT_MAX_TEST; 2594 2595 test_info->offline_mask = resp->offline_tests; 2596 test_info->timeout = le16_to_cpu(resp->test_timeout); 2597 if (!test_info->timeout) 2598 test_info->timeout = HWRM_CMD_TIMEOUT; 2599 for (i = 0; i < bp->num_tests; i++) { 2600 char *str = test_info->string[i]; 2601 char *fw_str = resp->test0_name + i * 32; 2602 2603 if (i == BNXT_MACLPBK_TEST_IDX) { 2604 strcpy(str, "Mac loopback test (offline)"); 2605 } else if (i == BNXT_PHYLPBK_TEST_IDX) { 2606 strcpy(str, "Phy loopback test (offline)"); 2607 } else if (i == BNXT_IRQ_TEST_IDX) { 2608 strcpy(str, "Interrupt_test (offline)"); 2609 } else { 2610 strlcpy(str, fw_str, ETH_GSTRING_LEN); 2611 strncat(str, " test", ETH_GSTRING_LEN - strlen(str)); 2612 if (test_info->offline_mask & (1 << i)) 2613 strncat(str, " (offline)", 2614 ETH_GSTRING_LEN - strlen(str)); 2615 else 2616 strncat(str, " (online)", 2617 ETH_GSTRING_LEN - strlen(str)); 2618 } 2619 } 2620 2621 ethtool_init_exit: 2622 mutex_unlock(&bp->hwrm_cmd_lock); 2623 } 2624 2625 void bnxt_ethtool_free(struct bnxt *bp) 2626 { 2627 kfree(bp->test_info); 2628 bp->test_info = NULL; 2629 } 2630 2631 const struct ethtool_ops bnxt_ethtool_ops = { 2632 .get_link_ksettings = bnxt_get_link_ksettings, 2633 .set_link_ksettings = bnxt_set_link_ksettings, 2634 .get_pauseparam = bnxt_get_pauseparam, 2635 .set_pauseparam = bnxt_set_pauseparam, 2636 .get_drvinfo = bnxt_get_drvinfo, 2637 .get_wol = bnxt_get_wol, 2638 .set_wol = bnxt_set_wol, 2639 .get_coalesce = bnxt_get_coalesce, 2640 .set_coalesce = bnxt_set_coalesce, 2641 .get_msglevel = bnxt_get_msglevel, 2642 .set_msglevel = bnxt_set_msglevel, 2643 .get_sset_count = bnxt_get_sset_count, 2644 .get_strings = bnxt_get_strings, 2645 .get_ethtool_stats = bnxt_get_ethtool_stats, 2646 .set_ringparam = bnxt_set_ringparam, 2647 .get_ringparam = bnxt_get_ringparam, 2648 .get_channels = bnxt_get_channels, 2649 .set_channels = bnxt_set_channels, 2650 .get_rxnfc = bnxt_get_rxnfc, 2651 .set_rxnfc = bnxt_set_rxnfc, 2652 .get_rxfh_indir_size = bnxt_get_rxfh_indir_size, 2653 .get_rxfh_key_size = bnxt_get_rxfh_key_size, 2654 .get_rxfh = bnxt_get_rxfh, 2655 .flash_device = bnxt_flash_device, 2656 .get_eeprom_len = bnxt_get_eeprom_len, 2657 .get_eeprom = bnxt_get_eeprom, 2658 .set_eeprom = bnxt_set_eeprom, 2659 .get_link = bnxt_get_link, 2660 .get_eee = bnxt_get_eee, 2661 .set_eee = bnxt_set_eee, 2662 .get_module_info = bnxt_get_module_info, 2663 .get_module_eeprom = bnxt_get_module_eeprom, 2664 .nway_reset = bnxt_nway_reset, 2665 .set_phys_id = bnxt_set_phys_id, 2666 .self_test = bnxt_self_test, 2667 .reset = bnxt_reset, 2668 }; 2669