1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/ctype.h> 11 #include <linux/stringify.h> 12 #include <linux/ethtool.h> 13 #include <linux/interrupt.h> 14 #include <linux/pci.h> 15 #include <linux/etherdevice.h> 16 #include <linux/crc32.h> 17 #include <linux/firmware.h> 18 #include "bnxt_hsi.h" 19 #include "bnxt.h" 20 #include "bnxt_ethtool.h" 21 #include "bnxt_nvm_defs.h" /* NVRAM content constant and structure defs */ 22 #include "bnxt_fw_hdr.h" /* Firmware hdr constant and structure defs */ 23 #define FLASH_NVRAM_TIMEOUT ((HWRM_CMD_TIMEOUT) * 100) 24 #define FLASH_PACKAGE_TIMEOUT ((HWRM_CMD_TIMEOUT) * 200) 25 #define INSTALL_PACKAGE_TIMEOUT ((HWRM_CMD_TIMEOUT) * 200) 26 27 static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen); 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 48 memset(coal, 0, sizeof(*coal)); 49 50 coal->rx_coalesce_usecs = bp->rx_coal_ticks; 51 /* 2 completion records per rx packet */ 52 coal->rx_max_coalesced_frames = bp->rx_coal_bufs / 2; 53 coal->rx_coalesce_usecs_irq = bp->rx_coal_ticks_irq; 54 coal->rx_max_coalesced_frames_irq = bp->rx_coal_bufs_irq / 2; 55 56 coal->tx_coalesce_usecs = bp->tx_coal_ticks; 57 coal->tx_max_coalesced_frames = bp->tx_coal_bufs; 58 coal->tx_coalesce_usecs_irq = bp->tx_coal_ticks_irq; 59 coal->tx_max_coalesced_frames_irq = bp->tx_coal_bufs_irq; 60 61 coal->stats_block_coalesce_usecs = bp->stats_coal_ticks; 62 63 return 0; 64 } 65 66 static int bnxt_set_coalesce(struct net_device *dev, 67 struct ethtool_coalesce *coal) 68 { 69 struct bnxt *bp = netdev_priv(dev); 70 bool update_stats = false; 71 int rc = 0; 72 73 bp->rx_coal_ticks = coal->rx_coalesce_usecs; 74 /* 2 completion records per rx packet */ 75 bp->rx_coal_bufs = coal->rx_max_coalesced_frames * 2; 76 bp->rx_coal_ticks_irq = coal->rx_coalesce_usecs_irq; 77 bp->rx_coal_bufs_irq = coal->rx_max_coalesced_frames_irq * 2; 78 79 bp->tx_coal_ticks = coal->tx_coalesce_usecs; 80 bp->tx_coal_bufs = coal->tx_max_coalesced_frames; 81 bp->tx_coal_ticks_irq = coal->tx_coalesce_usecs_irq; 82 bp->tx_coal_bufs_irq = coal->tx_max_coalesced_frames_irq; 83 84 if (bp->stats_coal_ticks != coal->stats_block_coalesce_usecs) { 85 u32 stats_ticks = coal->stats_block_coalesce_usecs; 86 87 stats_ticks = clamp_t(u32, stats_ticks, 88 BNXT_MIN_STATS_COAL_TICKS, 89 BNXT_MAX_STATS_COAL_TICKS); 90 stats_ticks = rounddown(stats_ticks, BNXT_MIN_STATS_COAL_TICKS); 91 bp->stats_coal_ticks = stats_ticks; 92 update_stats = true; 93 } 94 95 if (netif_running(dev)) { 96 if (update_stats) { 97 rc = bnxt_close_nic(bp, true, false); 98 if (!rc) 99 rc = bnxt_open_nic(bp, true, false); 100 } else { 101 rc = bnxt_hwrm_set_coal(bp); 102 } 103 } 104 105 return rc; 106 } 107 108 #define BNXT_NUM_STATS 21 109 110 #define BNXT_RX_STATS_ENTRY(counter) \ 111 { BNXT_RX_STATS_OFFSET(counter), __stringify(counter) } 112 113 #define BNXT_TX_STATS_ENTRY(counter) \ 114 { BNXT_TX_STATS_OFFSET(counter), __stringify(counter) } 115 116 static const struct { 117 long offset; 118 char string[ETH_GSTRING_LEN]; 119 } bnxt_port_stats_arr[] = { 120 BNXT_RX_STATS_ENTRY(rx_64b_frames), 121 BNXT_RX_STATS_ENTRY(rx_65b_127b_frames), 122 BNXT_RX_STATS_ENTRY(rx_128b_255b_frames), 123 BNXT_RX_STATS_ENTRY(rx_256b_511b_frames), 124 BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames), 125 BNXT_RX_STATS_ENTRY(rx_1024b_1518_frames), 126 BNXT_RX_STATS_ENTRY(rx_good_vlan_frames), 127 BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames), 128 BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames), 129 BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames), 130 BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames), 131 BNXT_RX_STATS_ENTRY(rx_total_frames), 132 BNXT_RX_STATS_ENTRY(rx_ucast_frames), 133 BNXT_RX_STATS_ENTRY(rx_mcast_frames), 134 BNXT_RX_STATS_ENTRY(rx_bcast_frames), 135 BNXT_RX_STATS_ENTRY(rx_fcs_err_frames), 136 BNXT_RX_STATS_ENTRY(rx_ctrl_frames), 137 BNXT_RX_STATS_ENTRY(rx_pause_frames), 138 BNXT_RX_STATS_ENTRY(rx_pfc_frames), 139 BNXT_RX_STATS_ENTRY(rx_align_err_frames), 140 BNXT_RX_STATS_ENTRY(rx_ovrsz_frames), 141 BNXT_RX_STATS_ENTRY(rx_jbr_frames), 142 BNXT_RX_STATS_ENTRY(rx_mtu_err_frames), 143 BNXT_RX_STATS_ENTRY(rx_tagged_frames), 144 BNXT_RX_STATS_ENTRY(rx_double_tagged_frames), 145 BNXT_RX_STATS_ENTRY(rx_good_frames), 146 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri0), 147 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri1), 148 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri2), 149 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri3), 150 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri4), 151 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri5), 152 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri6), 153 BNXT_RX_STATS_ENTRY(rx_pfc_ena_frames_pri7), 154 BNXT_RX_STATS_ENTRY(rx_undrsz_frames), 155 BNXT_RX_STATS_ENTRY(rx_eee_lpi_events), 156 BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration), 157 BNXT_RX_STATS_ENTRY(rx_bytes), 158 BNXT_RX_STATS_ENTRY(rx_runt_bytes), 159 BNXT_RX_STATS_ENTRY(rx_runt_frames), 160 161 BNXT_TX_STATS_ENTRY(tx_64b_frames), 162 BNXT_TX_STATS_ENTRY(tx_65b_127b_frames), 163 BNXT_TX_STATS_ENTRY(tx_128b_255b_frames), 164 BNXT_TX_STATS_ENTRY(tx_256b_511b_frames), 165 BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames), 166 BNXT_TX_STATS_ENTRY(tx_1024b_1518_frames), 167 BNXT_TX_STATS_ENTRY(tx_good_vlan_frames), 168 BNXT_TX_STATS_ENTRY(tx_1519b_2047_frames), 169 BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames), 170 BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames), 171 BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames), 172 BNXT_TX_STATS_ENTRY(tx_good_frames), 173 BNXT_TX_STATS_ENTRY(tx_total_frames), 174 BNXT_TX_STATS_ENTRY(tx_ucast_frames), 175 BNXT_TX_STATS_ENTRY(tx_mcast_frames), 176 BNXT_TX_STATS_ENTRY(tx_bcast_frames), 177 BNXT_TX_STATS_ENTRY(tx_pause_frames), 178 BNXT_TX_STATS_ENTRY(tx_pfc_frames), 179 BNXT_TX_STATS_ENTRY(tx_jabber_frames), 180 BNXT_TX_STATS_ENTRY(tx_fcs_err_frames), 181 BNXT_TX_STATS_ENTRY(tx_err), 182 BNXT_TX_STATS_ENTRY(tx_fifo_underruns), 183 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri0), 184 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri1), 185 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri2), 186 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri3), 187 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri4), 188 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri5), 189 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri6), 190 BNXT_TX_STATS_ENTRY(tx_pfc_ena_frames_pri7), 191 BNXT_TX_STATS_ENTRY(tx_eee_lpi_events), 192 BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration), 193 BNXT_TX_STATS_ENTRY(tx_total_collisions), 194 BNXT_TX_STATS_ENTRY(tx_bytes), 195 }; 196 197 #define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr) 198 199 static int bnxt_get_sset_count(struct net_device *dev, int sset) 200 { 201 struct bnxt *bp = netdev_priv(dev); 202 203 switch (sset) { 204 case ETH_SS_STATS: { 205 int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings; 206 207 if (bp->flags & BNXT_FLAG_PORT_STATS) 208 num_stats += BNXT_NUM_PORT_STATS; 209 210 return num_stats; 211 } 212 default: 213 return -EOPNOTSUPP; 214 } 215 } 216 217 static void bnxt_get_ethtool_stats(struct net_device *dev, 218 struct ethtool_stats *stats, u64 *buf) 219 { 220 u32 i, j = 0; 221 struct bnxt *bp = netdev_priv(dev); 222 u32 buf_size = sizeof(struct ctx_hw_stats) * bp->cp_nr_rings; 223 u32 stat_fields = sizeof(struct ctx_hw_stats) / 8; 224 225 memset(buf, 0, buf_size); 226 227 if (!bp->bnapi) 228 return; 229 230 for (i = 0; i < bp->cp_nr_rings; i++) { 231 struct bnxt_napi *bnapi = bp->bnapi[i]; 232 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 233 __le64 *hw_stats = (__le64 *)cpr->hw_stats; 234 int k; 235 236 for (k = 0; k < stat_fields; j++, k++) 237 buf[j] = le64_to_cpu(hw_stats[k]); 238 buf[j++] = cpr->rx_l4_csum_errors; 239 } 240 if (bp->flags & BNXT_FLAG_PORT_STATS) { 241 __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats; 242 243 for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) { 244 buf[j] = le64_to_cpu(*(port_stats + 245 bnxt_port_stats_arr[i].offset)); 246 } 247 } 248 } 249 250 static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 251 { 252 struct bnxt *bp = netdev_priv(dev); 253 u32 i; 254 255 switch (stringset) { 256 /* The number of strings must match BNXT_NUM_STATS defined above. */ 257 case ETH_SS_STATS: 258 for (i = 0; i < bp->cp_nr_rings; i++) { 259 sprintf(buf, "[%d]: rx_ucast_packets", i); 260 buf += ETH_GSTRING_LEN; 261 sprintf(buf, "[%d]: rx_mcast_packets", i); 262 buf += ETH_GSTRING_LEN; 263 sprintf(buf, "[%d]: rx_bcast_packets", i); 264 buf += ETH_GSTRING_LEN; 265 sprintf(buf, "[%d]: rx_discards", i); 266 buf += ETH_GSTRING_LEN; 267 sprintf(buf, "[%d]: rx_drops", i); 268 buf += ETH_GSTRING_LEN; 269 sprintf(buf, "[%d]: rx_ucast_bytes", i); 270 buf += ETH_GSTRING_LEN; 271 sprintf(buf, "[%d]: rx_mcast_bytes", i); 272 buf += ETH_GSTRING_LEN; 273 sprintf(buf, "[%d]: rx_bcast_bytes", i); 274 buf += ETH_GSTRING_LEN; 275 sprintf(buf, "[%d]: tx_ucast_packets", i); 276 buf += ETH_GSTRING_LEN; 277 sprintf(buf, "[%d]: tx_mcast_packets", i); 278 buf += ETH_GSTRING_LEN; 279 sprintf(buf, "[%d]: tx_bcast_packets", i); 280 buf += ETH_GSTRING_LEN; 281 sprintf(buf, "[%d]: tx_discards", i); 282 buf += ETH_GSTRING_LEN; 283 sprintf(buf, "[%d]: tx_drops", i); 284 buf += ETH_GSTRING_LEN; 285 sprintf(buf, "[%d]: tx_ucast_bytes", i); 286 buf += ETH_GSTRING_LEN; 287 sprintf(buf, "[%d]: tx_mcast_bytes", i); 288 buf += ETH_GSTRING_LEN; 289 sprintf(buf, "[%d]: tx_bcast_bytes", i); 290 buf += ETH_GSTRING_LEN; 291 sprintf(buf, "[%d]: tpa_packets", i); 292 buf += ETH_GSTRING_LEN; 293 sprintf(buf, "[%d]: tpa_bytes", i); 294 buf += ETH_GSTRING_LEN; 295 sprintf(buf, "[%d]: tpa_events", i); 296 buf += ETH_GSTRING_LEN; 297 sprintf(buf, "[%d]: tpa_aborts", i); 298 buf += ETH_GSTRING_LEN; 299 sprintf(buf, "[%d]: rx_l4_csum_errors", i); 300 buf += ETH_GSTRING_LEN; 301 } 302 if (bp->flags & BNXT_FLAG_PORT_STATS) { 303 for (i = 0; i < BNXT_NUM_PORT_STATS; i++) { 304 strcpy(buf, bnxt_port_stats_arr[i].string); 305 buf += ETH_GSTRING_LEN; 306 } 307 } 308 break; 309 default: 310 netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n", 311 stringset); 312 break; 313 } 314 } 315 316 static void bnxt_get_ringparam(struct net_device *dev, 317 struct ethtool_ringparam *ering) 318 { 319 struct bnxt *bp = netdev_priv(dev); 320 321 ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT; 322 ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT; 323 ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT; 324 325 ering->rx_pending = bp->rx_ring_size; 326 ering->rx_jumbo_pending = bp->rx_agg_ring_size; 327 ering->tx_pending = bp->tx_ring_size; 328 } 329 330 static int bnxt_set_ringparam(struct net_device *dev, 331 struct ethtool_ringparam *ering) 332 { 333 struct bnxt *bp = netdev_priv(dev); 334 335 if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) || 336 (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) || 337 (ering->tx_pending <= MAX_SKB_FRAGS)) 338 return -EINVAL; 339 340 if (netif_running(dev)) 341 bnxt_close_nic(bp, false, false); 342 343 bp->rx_ring_size = ering->rx_pending; 344 bp->tx_ring_size = ering->tx_pending; 345 bnxt_set_ring_params(bp); 346 347 if (netif_running(dev)) 348 return bnxt_open_nic(bp, false, false); 349 350 return 0; 351 } 352 353 static void bnxt_get_channels(struct net_device *dev, 354 struct ethtool_channels *channel) 355 { 356 struct bnxt *bp = netdev_priv(dev); 357 int max_rx_rings, max_tx_rings, tcs; 358 359 bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true); 360 channel->max_combined = max_t(int, max_rx_rings, max_tx_rings); 361 362 if (bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false)) { 363 max_rx_rings = 0; 364 max_tx_rings = 0; 365 } 366 367 tcs = netdev_get_num_tc(dev); 368 if (tcs > 1) 369 max_tx_rings /= tcs; 370 371 channel->max_rx = max_rx_rings; 372 channel->max_tx = max_tx_rings; 373 channel->max_other = 0; 374 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 375 channel->combined_count = bp->rx_nr_rings; 376 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 377 channel->combined_count--; 378 } else { 379 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) { 380 channel->rx_count = bp->rx_nr_rings; 381 channel->tx_count = bp->tx_nr_rings_per_tc; 382 } 383 } 384 } 385 386 static int bnxt_set_channels(struct net_device *dev, 387 struct ethtool_channels *channel) 388 { 389 struct bnxt *bp = netdev_priv(dev); 390 int max_rx_rings, max_tx_rings, tcs; 391 int req_tx_rings, rsv_tx_rings; 392 u32 rc = 0; 393 bool sh = false; 394 395 if (channel->other_count) 396 return -EINVAL; 397 398 if (!channel->combined_count && 399 (!channel->rx_count || !channel->tx_count)) 400 return -EINVAL; 401 402 if (channel->combined_count && 403 (channel->rx_count || channel->tx_count)) 404 return -EINVAL; 405 406 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && (channel->rx_count || 407 channel->tx_count)) 408 return -EINVAL; 409 410 if (channel->combined_count) 411 sh = true; 412 413 bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh); 414 415 tcs = netdev_get_num_tc(dev); 416 if (tcs > 1) 417 max_tx_rings /= tcs; 418 419 if (sh && 420 channel->combined_count > max_t(int, max_rx_rings, max_tx_rings)) 421 return -ENOMEM; 422 423 if (!sh && (channel->rx_count > max_rx_rings || 424 channel->tx_count > max_tx_rings)) 425 return -ENOMEM; 426 427 req_tx_rings = sh ? channel->combined_count : channel->tx_count; 428 req_tx_rings = min_t(int, req_tx_rings, max_tx_rings); 429 if (tcs > 1) 430 req_tx_rings *= tcs; 431 432 rsv_tx_rings = req_tx_rings; 433 if (bnxt_hwrm_reserve_tx_rings(bp, &rsv_tx_rings)) 434 return -ENOMEM; 435 436 if (rsv_tx_rings < req_tx_rings) { 437 netdev_warn(dev, "Unable to allocate the requested tx rings\n"); 438 return -ENOMEM; 439 } 440 441 if (netif_running(dev)) { 442 if (BNXT_PF(bp)) { 443 /* TODO CHIMP_FW: Send message to all VF's 444 * before PF unload 445 */ 446 } 447 rc = bnxt_close_nic(bp, true, false); 448 if (rc) { 449 netdev_err(bp->dev, "Set channel failure rc :%x\n", 450 rc); 451 return rc; 452 } 453 } 454 455 if (sh) { 456 bp->flags |= BNXT_FLAG_SHARED_RINGS; 457 bp->rx_nr_rings = min_t(int, channel->combined_count, 458 max_rx_rings); 459 bp->tx_nr_rings_per_tc = min_t(int, channel->combined_count, 460 max_tx_rings); 461 } else { 462 bp->flags &= ~BNXT_FLAG_SHARED_RINGS; 463 bp->rx_nr_rings = channel->rx_count; 464 bp->tx_nr_rings_per_tc = channel->tx_count; 465 } 466 467 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 468 if (tcs > 1) 469 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs; 470 471 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) : 472 bp->tx_nr_rings + bp->rx_nr_rings; 473 474 bp->num_stat_ctxs = bp->cp_nr_rings; 475 476 /* After changing number of rx channels, update NTUPLE feature. */ 477 netdev_update_features(dev); 478 if (netif_running(dev)) { 479 rc = bnxt_open_nic(bp, true, false); 480 if ((!rc) && BNXT_PF(bp)) { 481 /* TODO CHIMP_FW: Send message to all VF's 482 * to renable 483 */ 484 } 485 } 486 487 return rc; 488 } 489 490 #ifdef CONFIG_RFS_ACCEL 491 static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd, 492 u32 *rule_locs) 493 { 494 int i, j = 0; 495 496 cmd->data = bp->ntp_fltr_count; 497 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 498 struct hlist_head *head; 499 struct bnxt_ntuple_filter *fltr; 500 501 head = &bp->ntp_fltr_hash_tbl[i]; 502 rcu_read_lock(); 503 hlist_for_each_entry_rcu(fltr, head, hash) { 504 if (j == cmd->rule_cnt) 505 break; 506 rule_locs[j++] = fltr->sw_id; 507 } 508 rcu_read_unlock(); 509 if (j == cmd->rule_cnt) 510 break; 511 } 512 cmd->rule_cnt = j; 513 return 0; 514 } 515 516 static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd) 517 { 518 struct ethtool_rx_flow_spec *fs = 519 (struct ethtool_rx_flow_spec *)&cmd->fs; 520 struct bnxt_ntuple_filter *fltr; 521 struct flow_keys *fkeys; 522 int i, rc = -EINVAL; 523 524 if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR) 525 return rc; 526 527 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 528 struct hlist_head *head; 529 530 head = &bp->ntp_fltr_hash_tbl[i]; 531 rcu_read_lock(); 532 hlist_for_each_entry_rcu(fltr, head, hash) { 533 if (fltr->sw_id == fs->location) 534 goto fltr_found; 535 } 536 rcu_read_unlock(); 537 } 538 return rc; 539 540 fltr_found: 541 fkeys = &fltr->fkeys; 542 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 543 if (fkeys->basic.ip_proto == IPPROTO_TCP) 544 fs->flow_type = TCP_V4_FLOW; 545 else if (fkeys->basic.ip_proto == IPPROTO_UDP) 546 fs->flow_type = UDP_V4_FLOW; 547 else 548 goto fltr_err; 549 550 fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src; 551 fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0); 552 553 fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst; 554 fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0); 555 556 fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src; 557 fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0); 558 559 fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst; 560 fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0); 561 } else { 562 int i; 563 564 if (fkeys->basic.ip_proto == IPPROTO_TCP) 565 fs->flow_type = TCP_V6_FLOW; 566 else if (fkeys->basic.ip_proto == IPPROTO_UDP) 567 fs->flow_type = UDP_V6_FLOW; 568 else 569 goto fltr_err; 570 571 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6src[0] = 572 fkeys->addrs.v6addrs.src; 573 *(struct in6_addr *)&fs->h_u.tcp_ip6_spec.ip6dst[0] = 574 fkeys->addrs.v6addrs.dst; 575 for (i = 0; i < 4; i++) { 576 fs->m_u.tcp_ip6_spec.ip6src[i] = cpu_to_be32(~0); 577 fs->m_u.tcp_ip6_spec.ip6dst[i] = cpu_to_be32(~0); 578 } 579 fs->h_u.tcp_ip6_spec.psrc = fkeys->ports.src; 580 fs->m_u.tcp_ip6_spec.psrc = cpu_to_be16(~0); 581 582 fs->h_u.tcp_ip6_spec.pdst = fkeys->ports.dst; 583 fs->m_u.tcp_ip6_spec.pdst = cpu_to_be16(~0); 584 } 585 586 fs->ring_cookie = fltr->rxq; 587 rc = 0; 588 589 fltr_err: 590 rcu_read_unlock(); 591 592 return rc; 593 } 594 #endif 595 596 static u64 get_ethtool_ipv4_rss(struct bnxt *bp) 597 { 598 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 599 return RXH_IP_SRC | RXH_IP_DST; 600 return 0; 601 } 602 603 static u64 get_ethtool_ipv6_rss(struct bnxt *bp) 604 { 605 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 606 return RXH_IP_SRC | RXH_IP_DST; 607 return 0; 608 } 609 610 static int bnxt_grxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd) 611 { 612 cmd->data = 0; 613 switch (cmd->flow_type) { 614 case TCP_V4_FLOW: 615 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) 616 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 617 RXH_L4_B_0_1 | RXH_L4_B_2_3; 618 cmd->data |= get_ethtool_ipv4_rss(bp); 619 break; 620 case UDP_V4_FLOW: 621 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4) 622 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 623 RXH_L4_B_0_1 | RXH_L4_B_2_3; 624 /* fall through */ 625 case SCTP_V4_FLOW: 626 case AH_ESP_V4_FLOW: 627 case AH_V4_FLOW: 628 case ESP_V4_FLOW: 629 case IPV4_FLOW: 630 cmd->data |= get_ethtool_ipv4_rss(bp); 631 break; 632 633 case TCP_V6_FLOW: 634 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) 635 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 636 RXH_L4_B_0_1 | RXH_L4_B_2_3; 637 cmd->data |= get_ethtool_ipv6_rss(bp); 638 break; 639 case UDP_V6_FLOW: 640 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6) 641 cmd->data |= RXH_IP_SRC | RXH_IP_DST | 642 RXH_L4_B_0_1 | RXH_L4_B_2_3; 643 /* fall through */ 644 case SCTP_V6_FLOW: 645 case AH_ESP_V6_FLOW: 646 case AH_V6_FLOW: 647 case ESP_V6_FLOW: 648 case IPV6_FLOW: 649 cmd->data |= get_ethtool_ipv6_rss(bp); 650 break; 651 } 652 return 0; 653 } 654 655 #define RXH_4TUPLE (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3) 656 #define RXH_2TUPLE (RXH_IP_SRC | RXH_IP_DST) 657 658 static int bnxt_srxfh(struct bnxt *bp, struct ethtool_rxnfc *cmd) 659 { 660 u32 rss_hash_cfg = bp->rss_hash_cfg; 661 int tuple, rc = 0; 662 663 if (cmd->data == RXH_4TUPLE) 664 tuple = 4; 665 else if (cmd->data == RXH_2TUPLE) 666 tuple = 2; 667 else if (!cmd->data) 668 tuple = 0; 669 else 670 return -EINVAL; 671 672 if (cmd->flow_type == TCP_V4_FLOW) { 673 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4; 674 if (tuple == 4) 675 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4; 676 } else if (cmd->flow_type == UDP_V4_FLOW) { 677 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP)) 678 return -EINVAL; 679 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4; 680 if (tuple == 4) 681 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4; 682 } else if (cmd->flow_type == TCP_V6_FLOW) { 683 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 684 if (tuple == 4) 685 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 686 } else if (cmd->flow_type == UDP_V6_FLOW) { 687 if (tuple == 4 && !(bp->flags & BNXT_FLAG_UDP_RSS_CAP)) 688 return -EINVAL; 689 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 690 if (tuple == 4) 691 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 692 } else if (tuple == 4) { 693 return -EINVAL; 694 } 695 696 switch (cmd->flow_type) { 697 case TCP_V4_FLOW: 698 case UDP_V4_FLOW: 699 case SCTP_V4_FLOW: 700 case AH_ESP_V4_FLOW: 701 case AH_V4_FLOW: 702 case ESP_V4_FLOW: 703 case IPV4_FLOW: 704 if (tuple == 2) 705 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4; 706 else if (!tuple) 707 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4; 708 break; 709 710 case TCP_V6_FLOW: 711 case UDP_V6_FLOW: 712 case SCTP_V6_FLOW: 713 case AH_ESP_V6_FLOW: 714 case AH_V6_FLOW: 715 case ESP_V6_FLOW: 716 case IPV6_FLOW: 717 if (tuple == 2) 718 rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6; 719 else if (!tuple) 720 rss_hash_cfg &= ~VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6; 721 break; 722 } 723 724 if (bp->rss_hash_cfg == rss_hash_cfg) 725 return 0; 726 727 bp->rss_hash_cfg = rss_hash_cfg; 728 if (netif_running(bp->dev)) { 729 bnxt_close_nic(bp, false, false); 730 rc = bnxt_open_nic(bp, false, false); 731 } 732 return rc; 733 } 734 735 static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd, 736 u32 *rule_locs) 737 { 738 struct bnxt *bp = netdev_priv(dev); 739 int rc = 0; 740 741 switch (cmd->cmd) { 742 #ifdef CONFIG_RFS_ACCEL 743 case ETHTOOL_GRXRINGS: 744 cmd->data = bp->rx_nr_rings; 745 break; 746 747 case ETHTOOL_GRXCLSRLCNT: 748 cmd->rule_cnt = bp->ntp_fltr_count; 749 cmd->data = BNXT_NTP_FLTR_MAX_FLTR; 750 break; 751 752 case ETHTOOL_GRXCLSRLALL: 753 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs); 754 break; 755 756 case ETHTOOL_GRXCLSRULE: 757 rc = bnxt_grxclsrule(bp, cmd); 758 break; 759 #endif 760 761 case ETHTOOL_GRXFH: 762 rc = bnxt_grxfh(bp, cmd); 763 break; 764 765 default: 766 rc = -EOPNOTSUPP; 767 break; 768 } 769 770 return rc; 771 } 772 773 static int bnxt_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd) 774 { 775 struct bnxt *bp = netdev_priv(dev); 776 int rc; 777 778 switch (cmd->cmd) { 779 case ETHTOOL_SRXFH: 780 rc = bnxt_srxfh(bp, cmd); 781 break; 782 783 default: 784 rc = -EOPNOTSUPP; 785 break; 786 } 787 return rc; 788 } 789 790 static u32 bnxt_get_rxfh_indir_size(struct net_device *dev) 791 { 792 return HW_HASH_INDEX_SIZE; 793 } 794 795 static u32 bnxt_get_rxfh_key_size(struct net_device *dev) 796 { 797 return HW_HASH_KEY_SIZE; 798 } 799 800 static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, 801 u8 *hfunc) 802 { 803 struct bnxt *bp = netdev_priv(dev); 804 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 805 int i = 0; 806 807 if (hfunc) 808 *hfunc = ETH_RSS_HASH_TOP; 809 810 if (indir) 811 for (i = 0; i < HW_HASH_INDEX_SIZE; i++) 812 indir[i] = le16_to_cpu(vnic->rss_table[i]); 813 814 if (key) 815 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE); 816 817 return 0; 818 } 819 820 static void bnxt_get_drvinfo(struct net_device *dev, 821 struct ethtool_drvinfo *info) 822 { 823 struct bnxt *bp = netdev_priv(dev); 824 char *pkglog; 825 char *pkgver = NULL; 826 827 pkglog = kmalloc(BNX_PKG_LOG_MAX_LENGTH, GFP_KERNEL); 828 if (pkglog) 829 pkgver = bnxt_get_pkgver(dev, pkglog, BNX_PKG_LOG_MAX_LENGTH); 830 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 831 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); 832 if (pkgver && *pkgver != 0 && isdigit(*pkgver)) 833 snprintf(info->fw_version, sizeof(info->fw_version) - 1, 834 "%s pkg %s", bp->fw_ver_str, pkgver); 835 else 836 strlcpy(info->fw_version, bp->fw_ver_str, 837 sizeof(info->fw_version)); 838 strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info)); 839 info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings; 840 info->testinfo_len = BNXT_NUM_TESTS(bp); 841 /* TODO CHIMP_FW: eeprom dump details */ 842 info->eedump_len = 0; 843 /* TODO CHIMP FW: reg dump details */ 844 info->regdump_len = 0; 845 kfree(pkglog); 846 } 847 848 u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause) 849 { 850 u32 speed_mask = 0; 851 852 /* TODO: support 25GB, 40GB, 50GB with different cable type */ 853 /* set the advertised speeds */ 854 if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB) 855 speed_mask |= ADVERTISED_100baseT_Full; 856 if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB) 857 speed_mask |= ADVERTISED_1000baseT_Full; 858 if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB) 859 speed_mask |= ADVERTISED_2500baseX_Full; 860 if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB) 861 speed_mask |= ADVERTISED_10000baseT_Full; 862 if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB) 863 speed_mask |= ADVERTISED_40000baseCR4_Full; 864 865 if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH) 866 speed_mask |= ADVERTISED_Pause; 867 else if (fw_pause & BNXT_LINK_PAUSE_TX) 868 speed_mask |= ADVERTISED_Asym_Pause; 869 else if (fw_pause & BNXT_LINK_PAUSE_RX) 870 speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause; 871 872 return speed_mask; 873 } 874 875 #define BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, name)\ 876 { \ 877 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_100MB) \ 878 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 879 100baseT_Full); \ 880 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_1GB) \ 881 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 882 1000baseT_Full); \ 883 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_10GB) \ 884 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 885 10000baseT_Full); \ 886 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_25GB) \ 887 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 888 25000baseCR_Full); \ 889 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_40GB) \ 890 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 891 40000baseCR4_Full);\ 892 if ((fw_speeds) & BNXT_LINK_SPEED_MSK_50GB) \ 893 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 894 50000baseCR2_Full);\ 895 if ((fw_pause) & BNXT_LINK_PAUSE_RX) { \ 896 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 897 Pause); \ 898 if (!((fw_pause) & BNXT_LINK_PAUSE_TX)) \ 899 ethtool_link_ksettings_add_link_mode( \ 900 lk_ksettings, name, Asym_Pause);\ 901 } else if ((fw_pause) & BNXT_LINK_PAUSE_TX) { \ 902 ethtool_link_ksettings_add_link_mode(lk_ksettings, name,\ 903 Asym_Pause); \ 904 } \ 905 } 906 907 #define BNXT_ETHTOOL_TO_FW_SPDS(fw_speeds, lk_ksettings, name) \ 908 { \ 909 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 910 100baseT_Full) || \ 911 ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 912 100baseT_Half)) \ 913 (fw_speeds) |= BNXT_LINK_SPEED_MSK_100MB; \ 914 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 915 1000baseT_Full) || \ 916 ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 917 1000baseT_Half)) \ 918 (fw_speeds) |= BNXT_LINK_SPEED_MSK_1GB; \ 919 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 920 10000baseT_Full)) \ 921 (fw_speeds) |= BNXT_LINK_SPEED_MSK_10GB; \ 922 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 923 25000baseCR_Full)) \ 924 (fw_speeds) |= BNXT_LINK_SPEED_MSK_25GB; \ 925 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 926 40000baseCR4_Full)) \ 927 (fw_speeds) |= BNXT_LINK_SPEED_MSK_40GB; \ 928 if (ethtool_link_ksettings_test_link_mode(lk_ksettings, name, \ 929 50000baseCR2_Full)) \ 930 (fw_speeds) |= BNXT_LINK_SPEED_MSK_50GB; \ 931 } 932 933 static void bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info, 934 struct ethtool_link_ksettings *lk_ksettings) 935 { 936 u16 fw_speeds = link_info->advertising; 937 u8 fw_pause = 0; 938 939 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 940 fw_pause = link_info->auto_pause_setting; 941 942 BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, advertising); 943 } 944 945 static void bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info, 946 struct ethtool_link_ksettings *lk_ksettings) 947 { 948 u16 fw_speeds = link_info->lp_auto_link_speeds; 949 u8 fw_pause = 0; 950 951 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 952 fw_pause = link_info->lp_pause; 953 954 BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, fw_pause, lk_ksettings, 955 lp_advertising); 956 } 957 958 static void bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info, 959 struct ethtool_link_ksettings *lk_ksettings) 960 { 961 u16 fw_speeds = link_info->support_speeds; 962 963 BNXT_FW_TO_ETHTOOL_SPDS(fw_speeds, 0, lk_ksettings, supported); 964 965 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, Pause); 966 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 967 Asym_Pause); 968 969 if (link_info->support_auto_speeds) 970 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 971 Autoneg); 972 } 973 974 u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed) 975 { 976 switch (fw_link_speed) { 977 case BNXT_LINK_SPEED_100MB: 978 return SPEED_100; 979 case BNXT_LINK_SPEED_1GB: 980 return SPEED_1000; 981 case BNXT_LINK_SPEED_2_5GB: 982 return SPEED_2500; 983 case BNXT_LINK_SPEED_10GB: 984 return SPEED_10000; 985 case BNXT_LINK_SPEED_20GB: 986 return SPEED_20000; 987 case BNXT_LINK_SPEED_25GB: 988 return SPEED_25000; 989 case BNXT_LINK_SPEED_40GB: 990 return SPEED_40000; 991 case BNXT_LINK_SPEED_50GB: 992 return SPEED_50000; 993 default: 994 return SPEED_UNKNOWN; 995 } 996 } 997 998 static int bnxt_get_link_ksettings(struct net_device *dev, 999 struct ethtool_link_ksettings *lk_ksettings) 1000 { 1001 struct bnxt *bp = netdev_priv(dev); 1002 struct bnxt_link_info *link_info = &bp->link_info; 1003 struct ethtool_link_settings *base = &lk_ksettings->base; 1004 u32 ethtool_speed; 1005 1006 ethtool_link_ksettings_zero_link_mode(lk_ksettings, supported); 1007 bnxt_fw_to_ethtool_support_spds(link_info, lk_ksettings); 1008 1009 ethtool_link_ksettings_zero_link_mode(lk_ksettings, advertising); 1010 if (link_info->autoneg) { 1011 bnxt_fw_to_ethtool_advertised_spds(link_info, lk_ksettings); 1012 ethtool_link_ksettings_add_link_mode(lk_ksettings, 1013 advertising, Autoneg); 1014 base->autoneg = AUTONEG_ENABLE; 1015 if (link_info->phy_link_status == BNXT_LINK_LINK) 1016 bnxt_fw_to_ethtool_lp_adv(link_info, lk_ksettings); 1017 ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed); 1018 if (!netif_carrier_ok(dev)) 1019 base->duplex = DUPLEX_UNKNOWN; 1020 else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL) 1021 base->duplex = DUPLEX_FULL; 1022 else 1023 base->duplex = DUPLEX_HALF; 1024 } else { 1025 base->autoneg = AUTONEG_DISABLE; 1026 ethtool_speed = 1027 bnxt_fw_to_ethtool_speed(link_info->req_link_speed); 1028 base->duplex = DUPLEX_HALF; 1029 if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL) 1030 base->duplex = DUPLEX_FULL; 1031 } 1032 base->speed = ethtool_speed; 1033 1034 base->port = PORT_NONE; 1035 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) { 1036 base->port = PORT_TP; 1037 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 1038 TP); 1039 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising, 1040 TP); 1041 } else { 1042 ethtool_link_ksettings_add_link_mode(lk_ksettings, supported, 1043 FIBRE); 1044 ethtool_link_ksettings_add_link_mode(lk_ksettings, advertising, 1045 FIBRE); 1046 1047 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC) 1048 base->port = PORT_DA; 1049 else if (link_info->media_type == 1050 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE) 1051 base->port = PORT_FIBRE; 1052 } 1053 base->phy_address = link_info->phy_addr; 1054 1055 return 0; 1056 } 1057 1058 static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed) 1059 { 1060 struct bnxt *bp = netdev_priv(dev); 1061 struct bnxt_link_info *link_info = &bp->link_info; 1062 u16 support_spds = link_info->support_speeds; 1063 u32 fw_speed = 0; 1064 1065 switch (ethtool_speed) { 1066 case SPEED_100: 1067 if (support_spds & BNXT_LINK_SPEED_MSK_100MB) 1068 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB; 1069 break; 1070 case SPEED_1000: 1071 if (support_spds & BNXT_LINK_SPEED_MSK_1GB) 1072 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB; 1073 break; 1074 case SPEED_2500: 1075 if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB) 1076 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB; 1077 break; 1078 case SPEED_10000: 1079 if (support_spds & BNXT_LINK_SPEED_MSK_10GB) 1080 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB; 1081 break; 1082 case SPEED_20000: 1083 if (support_spds & BNXT_LINK_SPEED_MSK_20GB) 1084 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB; 1085 break; 1086 case SPEED_25000: 1087 if (support_spds & BNXT_LINK_SPEED_MSK_25GB) 1088 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB; 1089 break; 1090 case SPEED_40000: 1091 if (support_spds & BNXT_LINK_SPEED_MSK_40GB) 1092 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB; 1093 break; 1094 case SPEED_50000: 1095 if (support_spds & BNXT_LINK_SPEED_MSK_50GB) 1096 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB; 1097 break; 1098 default: 1099 netdev_err(dev, "unsupported speed!\n"); 1100 break; 1101 } 1102 return fw_speed; 1103 } 1104 1105 u16 bnxt_get_fw_auto_link_speeds(u32 advertising) 1106 { 1107 u16 fw_speed_mask = 0; 1108 1109 /* only support autoneg at speed 100, 1000, and 10000 */ 1110 if (advertising & (ADVERTISED_100baseT_Full | 1111 ADVERTISED_100baseT_Half)) { 1112 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB; 1113 } 1114 if (advertising & (ADVERTISED_1000baseT_Full | 1115 ADVERTISED_1000baseT_Half)) { 1116 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB; 1117 } 1118 if (advertising & ADVERTISED_10000baseT_Full) 1119 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB; 1120 1121 if (advertising & ADVERTISED_40000baseCR4_Full) 1122 fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB; 1123 1124 return fw_speed_mask; 1125 } 1126 1127 static int bnxt_set_link_ksettings(struct net_device *dev, 1128 const struct ethtool_link_ksettings *lk_ksettings) 1129 { 1130 struct bnxt *bp = netdev_priv(dev); 1131 struct bnxt_link_info *link_info = &bp->link_info; 1132 const struct ethtool_link_settings *base = &lk_ksettings->base; 1133 bool set_pause = false; 1134 u16 fw_advertising = 0; 1135 u32 speed; 1136 int rc = 0; 1137 1138 if (!BNXT_SINGLE_PF(bp)) 1139 return -EOPNOTSUPP; 1140 1141 if (base->autoneg == AUTONEG_ENABLE) { 1142 BNXT_ETHTOOL_TO_FW_SPDS(fw_advertising, lk_ksettings, 1143 advertising); 1144 link_info->autoneg |= BNXT_AUTONEG_SPEED; 1145 if (!fw_advertising) 1146 link_info->advertising = link_info->support_auto_speeds; 1147 else 1148 link_info->advertising = fw_advertising; 1149 /* any change to autoneg will cause link change, therefore the 1150 * driver should put back the original pause setting in autoneg 1151 */ 1152 set_pause = true; 1153 } else { 1154 u16 fw_speed; 1155 u8 phy_type = link_info->phy_type; 1156 1157 if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET || 1158 phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE || 1159 link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) { 1160 netdev_err(dev, "10GBase-T devices must autoneg\n"); 1161 rc = -EINVAL; 1162 goto set_setting_exit; 1163 } 1164 if (base->duplex == DUPLEX_HALF) { 1165 netdev_err(dev, "HALF DUPLEX is not supported!\n"); 1166 rc = -EINVAL; 1167 goto set_setting_exit; 1168 } 1169 speed = base->speed; 1170 fw_speed = bnxt_get_fw_speed(dev, speed); 1171 if (!fw_speed) { 1172 rc = -EINVAL; 1173 goto set_setting_exit; 1174 } 1175 link_info->req_link_speed = fw_speed; 1176 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL; 1177 link_info->autoneg = 0; 1178 link_info->advertising = 0; 1179 } 1180 1181 if (netif_running(dev)) 1182 rc = bnxt_hwrm_set_link_setting(bp, set_pause, false); 1183 1184 set_setting_exit: 1185 return rc; 1186 } 1187 1188 static void bnxt_get_pauseparam(struct net_device *dev, 1189 struct ethtool_pauseparam *epause) 1190 { 1191 struct bnxt *bp = netdev_priv(dev); 1192 struct bnxt_link_info *link_info = &bp->link_info; 1193 1194 if (BNXT_VF(bp)) 1195 return; 1196 epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL); 1197 epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX); 1198 epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX); 1199 } 1200 1201 static int bnxt_set_pauseparam(struct net_device *dev, 1202 struct ethtool_pauseparam *epause) 1203 { 1204 int rc = 0; 1205 struct bnxt *bp = netdev_priv(dev); 1206 struct bnxt_link_info *link_info = &bp->link_info; 1207 1208 if (!BNXT_SINGLE_PF(bp)) 1209 return -EOPNOTSUPP; 1210 1211 if (epause->autoneg) { 1212 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) 1213 return -EINVAL; 1214 1215 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 1216 if (bp->hwrm_spec_code >= 0x10201) 1217 link_info->req_flow_ctrl = 1218 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 1219 } else { 1220 /* when transition from auto pause to force pause, 1221 * force a link change 1222 */ 1223 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 1224 link_info->force_link_chng = true; 1225 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL; 1226 link_info->req_flow_ctrl = 0; 1227 } 1228 if (epause->rx_pause) 1229 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX; 1230 1231 if (epause->tx_pause) 1232 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX; 1233 1234 if (netif_running(dev)) 1235 rc = bnxt_hwrm_set_pause(bp); 1236 return rc; 1237 } 1238 1239 static u32 bnxt_get_link(struct net_device *dev) 1240 { 1241 struct bnxt *bp = netdev_priv(dev); 1242 1243 /* TODO: handle MF, VF, driver close case */ 1244 return bp->link_info.link_up; 1245 } 1246 1247 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal, 1248 u16 ext, u16 *index, u32 *item_length, 1249 u32 *data_length); 1250 1251 static int bnxt_flash_nvram(struct net_device *dev, 1252 u16 dir_type, 1253 u16 dir_ordinal, 1254 u16 dir_ext, 1255 u16 dir_attr, 1256 const u8 *data, 1257 size_t data_len) 1258 { 1259 struct bnxt *bp = netdev_priv(dev); 1260 int rc; 1261 struct hwrm_nvm_write_input req = {0}; 1262 dma_addr_t dma_handle; 1263 u8 *kmem; 1264 1265 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1); 1266 1267 req.dir_type = cpu_to_le16(dir_type); 1268 req.dir_ordinal = cpu_to_le16(dir_ordinal); 1269 req.dir_ext = cpu_to_le16(dir_ext); 1270 req.dir_attr = cpu_to_le16(dir_attr); 1271 req.dir_data_length = cpu_to_le32(data_len); 1272 1273 kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle, 1274 GFP_KERNEL); 1275 if (!kmem) { 1276 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n", 1277 (unsigned)data_len); 1278 return -ENOMEM; 1279 } 1280 memcpy(kmem, data, data_len); 1281 req.host_src_addr = cpu_to_le64(dma_handle); 1282 1283 rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT); 1284 dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle); 1285 1286 return rc; 1287 } 1288 1289 static int bnxt_firmware_reset(struct net_device *dev, 1290 u16 dir_type) 1291 { 1292 struct bnxt *bp = netdev_priv(dev); 1293 struct hwrm_fw_reset_input req = {0}; 1294 1295 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1); 1296 1297 /* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */ 1298 /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */ 1299 /* (e.g. when firmware isn't already running) */ 1300 switch (dir_type) { 1301 case BNX_DIR_TYPE_CHIMP_PATCH: 1302 case BNX_DIR_TYPE_BOOTCODE: 1303 case BNX_DIR_TYPE_BOOTCODE_2: 1304 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT; 1305 /* Self-reset ChiMP upon next PCIe reset: */ 1306 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST; 1307 break; 1308 case BNX_DIR_TYPE_APE_FW: 1309 case BNX_DIR_TYPE_APE_PATCH: 1310 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT; 1311 /* Self-reset APE upon next PCIe reset: */ 1312 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST; 1313 break; 1314 case BNX_DIR_TYPE_KONG_FW: 1315 case BNX_DIR_TYPE_KONG_PATCH: 1316 req.embedded_proc_type = 1317 FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL; 1318 break; 1319 case BNX_DIR_TYPE_BONO_FW: 1320 case BNX_DIR_TYPE_BONO_PATCH: 1321 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE; 1322 break; 1323 default: 1324 return -EINVAL; 1325 } 1326 1327 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1328 } 1329 1330 static int bnxt_flash_firmware(struct net_device *dev, 1331 u16 dir_type, 1332 const u8 *fw_data, 1333 size_t fw_size) 1334 { 1335 int rc = 0; 1336 u16 code_type; 1337 u32 stored_crc; 1338 u32 calculated_crc; 1339 struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data; 1340 1341 switch (dir_type) { 1342 case BNX_DIR_TYPE_BOOTCODE: 1343 case BNX_DIR_TYPE_BOOTCODE_2: 1344 code_type = CODE_BOOT; 1345 break; 1346 case BNX_DIR_TYPE_CHIMP_PATCH: 1347 code_type = CODE_CHIMP_PATCH; 1348 break; 1349 case BNX_DIR_TYPE_APE_FW: 1350 code_type = CODE_MCTP_PASSTHRU; 1351 break; 1352 case BNX_DIR_TYPE_APE_PATCH: 1353 code_type = CODE_APE_PATCH; 1354 break; 1355 case BNX_DIR_TYPE_KONG_FW: 1356 code_type = CODE_KONG_FW; 1357 break; 1358 case BNX_DIR_TYPE_KONG_PATCH: 1359 code_type = CODE_KONG_PATCH; 1360 break; 1361 case BNX_DIR_TYPE_BONO_FW: 1362 code_type = CODE_BONO_FW; 1363 break; 1364 case BNX_DIR_TYPE_BONO_PATCH: 1365 code_type = CODE_BONO_PATCH; 1366 break; 1367 default: 1368 netdev_err(dev, "Unsupported directory entry type: %u\n", 1369 dir_type); 1370 return -EINVAL; 1371 } 1372 if (fw_size < sizeof(struct bnxt_fw_header)) { 1373 netdev_err(dev, "Invalid firmware file size: %u\n", 1374 (unsigned int)fw_size); 1375 return -EINVAL; 1376 } 1377 if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) { 1378 netdev_err(dev, "Invalid firmware signature: %08X\n", 1379 le32_to_cpu(header->signature)); 1380 return -EINVAL; 1381 } 1382 if (header->code_type != code_type) { 1383 netdev_err(dev, "Expected firmware type: %d, read: %d\n", 1384 code_type, header->code_type); 1385 return -EINVAL; 1386 } 1387 if (header->device != DEVICE_CUMULUS_FAMILY) { 1388 netdev_err(dev, "Expected firmware device family %d, read: %d\n", 1389 DEVICE_CUMULUS_FAMILY, header->device); 1390 return -EINVAL; 1391 } 1392 /* Confirm the CRC32 checksum of the file: */ 1393 stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size - 1394 sizeof(stored_crc))); 1395 calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc)); 1396 if (calculated_crc != stored_crc) { 1397 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n", 1398 (unsigned long)stored_crc, 1399 (unsigned long)calculated_crc); 1400 return -EINVAL; 1401 } 1402 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST, 1403 0, 0, fw_data, fw_size); 1404 if (rc == 0) /* Firmware update successful */ 1405 rc = bnxt_firmware_reset(dev, dir_type); 1406 1407 return rc; 1408 } 1409 1410 static int bnxt_flash_microcode(struct net_device *dev, 1411 u16 dir_type, 1412 const u8 *fw_data, 1413 size_t fw_size) 1414 { 1415 struct bnxt_ucode_trailer *trailer; 1416 u32 calculated_crc; 1417 u32 stored_crc; 1418 int rc = 0; 1419 1420 if (fw_size < sizeof(struct bnxt_ucode_trailer)) { 1421 netdev_err(dev, "Invalid microcode file size: %u\n", 1422 (unsigned int)fw_size); 1423 return -EINVAL; 1424 } 1425 trailer = (struct bnxt_ucode_trailer *)(fw_data + (fw_size - 1426 sizeof(*trailer))); 1427 if (trailer->sig != cpu_to_le32(BNXT_UCODE_TRAILER_SIGNATURE)) { 1428 netdev_err(dev, "Invalid microcode trailer signature: %08X\n", 1429 le32_to_cpu(trailer->sig)); 1430 return -EINVAL; 1431 } 1432 if (le16_to_cpu(trailer->dir_type) != dir_type) { 1433 netdev_err(dev, "Expected microcode type: %d, read: %d\n", 1434 dir_type, le16_to_cpu(trailer->dir_type)); 1435 return -EINVAL; 1436 } 1437 if (le16_to_cpu(trailer->trailer_length) < 1438 sizeof(struct bnxt_ucode_trailer)) { 1439 netdev_err(dev, "Invalid microcode trailer length: %d\n", 1440 le16_to_cpu(trailer->trailer_length)); 1441 return -EINVAL; 1442 } 1443 1444 /* Confirm the CRC32 checksum of the file: */ 1445 stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size - 1446 sizeof(stored_crc))); 1447 calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc)); 1448 if (calculated_crc != stored_crc) { 1449 netdev_err(dev, 1450 "CRC32 (%08lX) does not match calculated: %08lX\n", 1451 (unsigned long)stored_crc, 1452 (unsigned long)calculated_crc); 1453 return -EINVAL; 1454 } 1455 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST, 1456 0, 0, fw_data, fw_size); 1457 1458 return rc; 1459 } 1460 1461 static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type) 1462 { 1463 switch (dir_type) { 1464 case BNX_DIR_TYPE_CHIMP_PATCH: 1465 case BNX_DIR_TYPE_BOOTCODE: 1466 case BNX_DIR_TYPE_BOOTCODE_2: 1467 case BNX_DIR_TYPE_APE_FW: 1468 case BNX_DIR_TYPE_APE_PATCH: 1469 case BNX_DIR_TYPE_KONG_FW: 1470 case BNX_DIR_TYPE_KONG_PATCH: 1471 case BNX_DIR_TYPE_BONO_FW: 1472 case BNX_DIR_TYPE_BONO_PATCH: 1473 return true; 1474 } 1475 1476 return false; 1477 } 1478 1479 static bool bnxt_dir_type_is_other_exec_format(u16 dir_type) 1480 { 1481 switch (dir_type) { 1482 case BNX_DIR_TYPE_AVS: 1483 case BNX_DIR_TYPE_EXP_ROM_MBA: 1484 case BNX_DIR_TYPE_PCIE: 1485 case BNX_DIR_TYPE_TSCF_UCODE: 1486 case BNX_DIR_TYPE_EXT_PHY: 1487 case BNX_DIR_TYPE_CCM: 1488 case BNX_DIR_TYPE_ISCSI_BOOT: 1489 case BNX_DIR_TYPE_ISCSI_BOOT_IPV6: 1490 case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6: 1491 return true; 1492 } 1493 1494 return false; 1495 } 1496 1497 static bool bnxt_dir_type_is_executable(u16 dir_type) 1498 { 1499 return bnxt_dir_type_is_ape_bin_format(dir_type) || 1500 bnxt_dir_type_is_other_exec_format(dir_type); 1501 } 1502 1503 static int bnxt_flash_firmware_from_file(struct net_device *dev, 1504 u16 dir_type, 1505 const char *filename) 1506 { 1507 const struct firmware *fw; 1508 int rc; 1509 1510 rc = request_firmware(&fw, filename, &dev->dev); 1511 if (rc != 0) { 1512 netdev_err(dev, "Error %d requesting firmware file: %s\n", 1513 rc, filename); 1514 return rc; 1515 } 1516 if (bnxt_dir_type_is_ape_bin_format(dir_type) == true) 1517 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size); 1518 else if (bnxt_dir_type_is_other_exec_format(dir_type) == true) 1519 rc = bnxt_flash_microcode(dev, dir_type, fw->data, fw->size); 1520 else 1521 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST, 1522 0, 0, fw->data, fw->size); 1523 release_firmware(fw); 1524 return rc; 1525 } 1526 1527 static int bnxt_flash_package_from_file(struct net_device *dev, 1528 char *filename, u32 install_type) 1529 { 1530 struct bnxt *bp = netdev_priv(dev); 1531 struct hwrm_nvm_install_update_output *resp = bp->hwrm_cmd_resp_addr; 1532 struct hwrm_nvm_install_update_input install = {0}; 1533 const struct firmware *fw; 1534 u32 item_len; 1535 u16 index; 1536 int rc; 1537 1538 bnxt_hwrm_fw_set_time(bp); 1539 1540 if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_UPDATE, 1541 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE, 1542 &index, &item_len, NULL) != 0) { 1543 netdev_err(dev, "PKG update area not created in nvram\n"); 1544 return -ENOBUFS; 1545 } 1546 1547 rc = request_firmware(&fw, filename, &dev->dev); 1548 if (rc != 0) { 1549 netdev_err(dev, "PKG error %d requesting file: %s\n", 1550 rc, filename); 1551 return rc; 1552 } 1553 1554 if (fw->size > item_len) { 1555 netdev_err(dev, "PKG insufficient update area in nvram: %lu", 1556 (unsigned long)fw->size); 1557 rc = -EFBIG; 1558 } else { 1559 dma_addr_t dma_handle; 1560 u8 *kmem; 1561 struct hwrm_nvm_modify_input modify = {0}; 1562 1563 bnxt_hwrm_cmd_hdr_init(bp, &modify, HWRM_NVM_MODIFY, -1, -1); 1564 1565 modify.dir_idx = cpu_to_le16(index); 1566 modify.len = cpu_to_le32(fw->size); 1567 1568 kmem = dma_alloc_coherent(&bp->pdev->dev, fw->size, 1569 &dma_handle, GFP_KERNEL); 1570 if (!kmem) { 1571 netdev_err(dev, 1572 "dma_alloc_coherent failure, length = %u\n", 1573 (unsigned int)fw->size); 1574 rc = -ENOMEM; 1575 } else { 1576 memcpy(kmem, fw->data, fw->size); 1577 modify.host_src_addr = cpu_to_le64(dma_handle); 1578 1579 rc = hwrm_send_message(bp, &modify, sizeof(modify), 1580 FLASH_PACKAGE_TIMEOUT); 1581 dma_free_coherent(&bp->pdev->dev, fw->size, kmem, 1582 dma_handle); 1583 } 1584 } 1585 release_firmware(fw); 1586 if (rc) 1587 return rc; 1588 1589 if ((install_type & 0xffff) == 0) 1590 install_type >>= 16; 1591 bnxt_hwrm_cmd_hdr_init(bp, &install, HWRM_NVM_INSTALL_UPDATE, -1, -1); 1592 install.install_type = cpu_to_le32(install_type); 1593 1594 rc = hwrm_send_message(bp, &install, sizeof(install), 1595 INSTALL_PACKAGE_TIMEOUT); 1596 if (rc) 1597 return -EOPNOTSUPP; 1598 1599 if (resp->result) { 1600 netdev_err(dev, "PKG install error = %d, problem_item = %d\n", 1601 (s8)resp->result, (int)resp->problem_item); 1602 return -ENOPKG; 1603 } 1604 return 0; 1605 } 1606 1607 static int bnxt_flash_device(struct net_device *dev, 1608 struct ethtool_flash *flash) 1609 { 1610 if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) { 1611 netdev_err(dev, "flashdev not supported from a virtual function\n"); 1612 return -EINVAL; 1613 } 1614 1615 if (flash->region == ETHTOOL_FLASH_ALL_REGIONS || 1616 flash->region > 0xffff) 1617 return bnxt_flash_package_from_file(dev, flash->data, 1618 flash->region); 1619 1620 return bnxt_flash_firmware_from_file(dev, flash->region, flash->data); 1621 } 1622 1623 static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length) 1624 { 1625 struct bnxt *bp = netdev_priv(dev); 1626 int rc; 1627 struct hwrm_nvm_get_dir_info_input req = {0}; 1628 struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr; 1629 1630 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1); 1631 1632 mutex_lock(&bp->hwrm_cmd_lock); 1633 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1634 if (!rc) { 1635 *entries = le32_to_cpu(output->entries); 1636 *length = le32_to_cpu(output->entry_length); 1637 } 1638 mutex_unlock(&bp->hwrm_cmd_lock); 1639 return rc; 1640 } 1641 1642 static int bnxt_get_eeprom_len(struct net_device *dev) 1643 { 1644 /* The -1 return value allows the entire 32-bit range of offsets to be 1645 * passed via the ethtool command-line utility. 1646 */ 1647 return -1; 1648 } 1649 1650 static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data) 1651 { 1652 struct bnxt *bp = netdev_priv(dev); 1653 int rc; 1654 u32 dir_entries; 1655 u32 entry_length; 1656 u8 *buf; 1657 size_t buflen; 1658 dma_addr_t dma_handle; 1659 struct hwrm_nvm_get_dir_entries_input req = {0}; 1660 1661 rc = nvm_get_dir_info(dev, &dir_entries, &entry_length); 1662 if (rc != 0) 1663 return rc; 1664 1665 /* Insert 2 bytes of directory info (count and size of entries) */ 1666 if (len < 2) 1667 return -EINVAL; 1668 1669 *data++ = dir_entries; 1670 *data++ = entry_length; 1671 len -= 2; 1672 memset(data, 0xff, len); 1673 1674 buflen = dir_entries * entry_length; 1675 buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle, 1676 GFP_KERNEL); 1677 if (!buf) { 1678 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n", 1679 (unsigned)buflen); 1680 return -ENOMEM; 1681 } 1682 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1); 1683 req.host_dest_addr = cpu_to_le64(dma_handle); 1684 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1685 if (rc == 0) 1686 memcpy(data, buf, len > buflen ? buflen : len); 1687 dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle); 1688 return rc; 1689 } 1690 1691 static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset, 1692 u32 length, u8 *data) 1693 { 1694 struct bnxt *bp = netdev_priv(dev); 1695 int rc; 1696 u8 *buf; 1697 dma_addr_t dma_handle; 1698 struct hwrm_nvm_read_input req = {0}; 1699 1700 buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle, 1701 GFP_KERNEL); 1702 if (!buf) { 1703 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n", 1704 (unsigned)length); 1705 return -ENOMEM; 1706 } 1707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1); 1708 req.host_dest_addr = cpu_to_le64(dma_handle); 1709 req.dir_idx = cpu_to_le16(index); 1710 req.offset = cpu_to_le32(offset); 1711 req.len = cpu_to_le32(length); 1712 1713 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1714 if (rc == 0) 1715 memcpy(data, buf, length); 1716 dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle); 1717 return rc; 1718 } 1719 1720 static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal, 1721 u16 ext, u16 *index, u32 *item_length, 1722 u32 *data_length) 1723 { 1724 struct bnxt *bp = netdev_priv(dev); 1725 int rc; 1726 struct hwrm_nvm_find_dir_entry_input req = {0}; 1727 struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr; 1728 1729 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1); 1730 req.enables = 0; 1731 req.dir_idx = 0; 1732 req.dir_type = cpu_to_le16(type); 1733 req.dir_ordinal = cpu_to_le16(ordinal); 1734 req.dir_ext = cpu_to_le16(ext); 1735 req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ; 1736 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1737 if (rc == 0) { 1738 if (index) 1739 *index = le16_to_cpu(output->dir_idx); 1740 if (item_length) 1741 *item_length = le32_to_cpu(output->dir_item_length); 1742 if (data_length) 1743 *data_length = le32_to_cpu(output->dir_data_length); 1744 } 1745 return rc; 1746 } 1747 1748 static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen) 1749 { 1750 char *retval = NULL; 1751 char *p; 1752 char *value; 1753 int field = 0; 1754 1755 if (datalen < 1) 1756 return NULL; 1757 /* null-terminate the log data (removing last '\n'): */ 1758 data[datalen - 1] = 0; 1759 for (p = data; *p != 0; p++) { 1760 field = 0; 1761 retval = NULL; 1762 while (*p != 0 && *p != '\n') { 1763 value = p; 1764 while (*p != 0 && *p != '\t' && *p != '\n') 1765 p++; 1766 if (field == desired_field) 1767 retval = value; 1768 if (*p != '\t') 1769 break; 1770 *p = 0; 1771 field++; 1772 p++; 1773 } 1774 if (*p == 0) 1775 break; 1776 *p = 0; 1777 } 1778 return retval; 1779 } 1780 1781 static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen) 1782 { 1783 u16 index = 0; 1784 u32 datalen; 1785 1786 if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG, 1787 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE, 1788 &index, NULL, &datalen) != 0) 1789 return NULL; 1790 1791 memset(buf, 0, buflen); 1792 if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0) 1793 return NULL; 1794 1795 return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf, 1796 datalen); 1797 } 1798 1799 static int bnxt_get_eeprom(struct net_device *dev, 1800 struct ethtool_eeprom *eeprom, 1801 u8 *data) 1802 { 1803 u32 index; 1804 u32 offset; 1805 1806 if (eeprom->offset == 0) /* special offset value to get directory */ 1807 return bnxt_get_nvram_directory(dev, eeprom->len, data); 1808 1809 index = eeprom->offset >> 24; 1810 offset = eeprom->offset & 0xffffff; 1811 1812 if (index == 0) { 1813 netdev_err(dev, "unsupported index value: %d\n", index); 1814 return -EINVAL; 1815 } 1816 1817 return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data); 1818 } 1819 1820 static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index) 1821 { 1822 struct bnxt *bp = netdev_priv(dev); 1823 struct hwrm_nvm_erase_dir_entry_input req = {0}; 1824 1825 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1); 1826 req.dir_idx = cpu_to_le16(index); 1827 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 1828 } 1829 1830 static int bnxt_set_eeprom(struct net_device *dev, 1831 struct ethtool_eeprom *eeprom, 1832 u8 *data) 1833 { 1834 struct bnxt *bp = netdev_priv(dev); 1835 u8 index, dir_op; 1836 u16 type, ext, ordinal, attr; 1837 1838 if (!BNXT_PF(bp)) { 1839 netdev_err(dev, "NVM write not supported from a virtual function\n"); 1840 return -EINVAL; 1841 } 1842 1843 type = eeprom->magic >> 16; 1844 1845 if (type == 0xffff) { /* special value for directory operations */ 1846 index = eeprom->magic & 0xff; 1847 dir_op = eeprom->magic >> 8; 1848 if (index == 0) 1849 return -EINVAL; 1850 switch (dir_op) { 1851 case 0x0e: /* erase */ 1852 if (eeprom->offset != ~eeprom->magic) 1853 return -EINVAL; 1854 return bnxt_erase_nvram_directory(dev, index - 1); 1855 default: 1856 return -EINVAL; 1857 } 1858 } 1859 1860 /* Create or re-write an NVM item: */ 1861 if (bnxt_dir_type_is_executable(type) == true) 1862 return -EOPNOTSUPP; 1863 ext = eeprom->magic & 0xffff; 1864 ordinal = eeprom->offset >> 16; 1865 attr = eeprom->offset & 0xffff; 1866 1867 return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data, 1868 eeprom->len); 1869 } 1870 1871 static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata) 1872 { 1873 struct bnxt *bp = netdev_priv(dev); 1874 struct ethtool_eee *eee = &bp->eee; 1875 struct bnxt_link_info *link_info = &bp->link_info; 1876 u32 advertising = 1877 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0); 1878 int rc = 0; 1879 1880 if (!BNXT_SINGLE_PF(bp)) 1881 return -EOPNOTSUPP; 1882 1883 if (!(bp->flags & BNXT_FLAG_EEE_CAP)) 1884 return -EOPNOTSUPP; 1885 1886 if (!edata->eee_enabled) 1887 goto eee_ok; 1888 1889 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 1890 netdev_warn(dev, "EEE requires autoneg\n"); 1891 return -EINVAL; 1892 } 1893 if (edata->tx_lpi_enabled) { 1894 if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi || 1895 edata->tx_lpi_timer < bp->lpi_tmr_lo)) { 1896 netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n", 1897 bp->lpi_tmr_lo, bp->lpi_tmr_hi); 1898 return -EINVAL; 1899 } else if (!bp->lpi_tmr_hi) { 1900 edata->tx_lpi_timer = eee->tx_lpi_timer; 1901 } 1902 } 1903 if (!edata->advertised) { 1904 edata->advertised = advertising & eee->supported; 1905 } else if (edata->advertised & ~advertising) { 1906 netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n", 1907 edata->advertised, advertising); 1908 return -EINVAL; 1909 } 1910 1911 eee->advertised = edata->advertised; 1912 eee->tx_lpi_enabled = edata->tx_lpi_enabled; 1913 eee->tx_lpi_timer = edata->tx_lpi_timer; 1914 eee_ok: 1915 eee->eee_enabled = edata->eee_enabled; 1916 1917 if (netif_running(dev)) 1918 rc = bnxt_hwrm_set_link_setting(bp, false, true); 1919 1920 return rc; 1921 } 1922 1923 static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata) 1924 { 1925 struct bnxt *bp = netdev_priv(dev); 1926 1927 if (!(bp->flags & BNXT_FLAG_EEE_CAP)) 1928 return -EOPNOTSUPP; 1929 1930 *edata = bp->eee; 1931 if (!bp->eee.eee_enabled) { 1932 /* Preserve tx_lpi_timer so that the last value will be used 1933 * by default when it is re-enabled. 1934 */ 1935 edata->advertised = 0; 1936 edata->tx_lpi_enabled = 0; 1937 } 1938 1939 if (!bp->eee.eee_active) 1940 edata->lp_advertised = 0; 1941 1942 return 0; 1943 } 1944 1945 static int bnxt_read_sfp_module_eeprom_info(struct bnxt *bp, u16 i2c_addr, 1946 u16 page_number, u16 start_addr, 1947 u16 data_length, u8 *buf) 1948 { 1949 struct hwrm_port_phy_i2c_read_input req = {0}; 1950 struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr; 1951 int rc, byte_offset = 0; 1952 1953 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1); 1954 req.i2c_slave_addr = i2c_addr; 1955 req.page_number = cpu_to_le16(page_number); 1956 req.port_id = cpu_to_le16(bp->pf.port_id); 1957 do { 1958 u16 xfer_size; 1959 1960 xfer_size = min_t(u16, data_length, BNXT_MAX_PHY_I2C_RESP_SIZE); 1961 data_length -= xfer_size; 1962 req.page_offset = cpu_to_le16(start_addr + byte_offset); 1963 req.data_length = xfer_size; 1964 req.enables = cpu_to_le32(start_addr + byte_offset ? 1965 PORT_PHY_I2C_READ_REQ_ENABLES_PAGE_OFFSET : 0); 1966 mutex_lock(&bp->hwrm_cmd_lock); 1967 rc = _hwrm_send_message(bp, &req, sizeof(req), 1968 HWRM_CMD_TIMEOUT); 1969 if (!rc) 1970 memcpy(buf + byte_offset, output->data, xfer_size); 1971 mutex_unlock(&bp->hwrm_cmd_lock); 1972 byte_offset += xfer_size; 1973 } while (!rc && data_length > 0); 1974 1975 return rc; 1976 } 1977 1978 static int bnxt_get_module_info(struct net_device *dev, 1979 struct ethtool_modinfo *modinfo) 1980 { 1981 struct bnxt *bp = netdev_priv(dev); 1982 struct hwrm_port_phy_i2c_read_input req = {0}; 1983 struct hwrm_port_phy_i2c_read_output *output = bp->hwrm_cmd_resp_addr; 1984 int rc; 1985 1986 /* No point in going further if phy status indicates 1987 * module is not inserted or if it is powered down or 1988 * if it is of type 10GBase-T 1989 */ 1990 if (bp->link_info.module_status > 1991 PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG) 1992 return -EOPNOTSUPP; 1993 1994 /* This feature is not supported in older firmware versions */ 1995 if (bp->hwrm_spec_code < 0x10202) 1996 return -EOPNOTSUPP; 1997 1998 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_I2C_READ, -1, -1); 1999 req.i2c_slave_addr = I2C_DEV_ADDR_A0; 2000 req.page_number = 0; 2001 req.page_offset = cpu_to_le16(SFP_EEPROM_SFF_8472_COMP_ADDR); 2002 req.data_length = SFP_EEPROM_SFF_8472_COMP_SIZE; 2003 req.port_id = cpu_to_le16(bp->pf.port_id); 2004 mutex_lock(&bp->hwrm_cmd_lock); 2005 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 2006 if (!rc) { 2007 u32 module_id = le32_to_cpu(output->data[0]); 2008 2009 switch (module_id) { 2010 case SFF_MODULE_ID_SFP: 2011 modinfo->type = ETH_MODULE_SFF_8472; 2012 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 2013 break; 2014 case SFF_MODULE_ID_QSFP: 2015 case SFF_MODULE_ID_QSFP_PLUS: 2016 modinfo->type = ETH_MODULE_SFF_8436; 2017 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN; 2018 break; 2019 case SFF_MODULE_ID_QSFP28: 2020 modinfo->type = ETH_MODULE_SFF_8636; 2021 modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN; 2022 break; 2023 default: 2024 rc = -EOPNOTSUPP; 2025 break; 2026 } 2027 } 2028 mutex_unlock(&bp->hwrm_cmd_lock); 2029 return rc; 2030 } 2031 2032 static int bnxt_get_module_eeprom(struct net_device *dev, 2033 struct ethtool_eeprom *eeprom, 2034 u8 *data) 2035 { 2036 struct bnxt *bp = netdev_priv(dev); 2037 u16 start = eeprom->offset, length = eeprom->len; 2038 int rc = 0; 2039 2040 memset(data, 0, eeprom->len); 2041 2042 /* Read A0 portion of the EEPROM */ 2043 if (start < ETH_MODULE_SFF_8436_LEN) { 2044 if (start + eeprom->len > ETH_MODULE_SFF_8436_LEN) 2045 length = ETH_MODULE_SFF_8436_LEN - start; 2046 rc = bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A0, 0, 2047 start, length, data); 2048 if (rc) 2049 return rc; 2050 start += length; 2051 data += length; 2052 length = eeprom->len - length; 2053 } 2054 2055 /* Read A2 portion of the EEPROM */ 2056 if (length) { 2057 start -= ETH_MODULE_SFF_8436_LEN; 2058 bnxt_read_sfp_module_eeprom_info(bp, I2C_DEV_ADDR_A2, 1, start, 2059 length, data); 2060 } 2061 return rc; 2062 } 2063 2064 static int bnxt_nway_reset(struct net_device *dev) 2065 { 2066 int rc = 0; 2067 2068 struct bnxt *bp = netdev_priv(dev); 2069 struct bnxt_link_info *link_info = &bp->link_info; 2070 2071 if (!BNXT_SINGLE_PF(bp)) 2072 return -EOPNOTSUPP; 2073 2074 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) 2075 return -EINVAL; 2076 2077 if (netif_running(dev)) 2078 rc = bnxt_hwrm_set_link_setting(bp, true, false); 2079 2080 return rc; 2081 } 2082 2083 const struct ethtool_ops bnxt_ethtool_ops = { 2084 .get_link_ksettings = bnxt_get_link_ksettings, 2085 .set_link_ksettings = bnxt_set_link_ksettings, 2086 .get_pauseparam = bnxt_get_pauseparam, 2087 .set_pauseparam = bnxt_set_pauseparam, 2088 .get_drvinfo = bnxt_get_drvinfo, 2089 .get_coalesce = bnxt_get_coalesce, 2090 .set_coalesce = bnxt_set_coalesce, 2091 .get_msglevel = bnxt_get_msglevel, 2092 .set_msglevel = bnxt_set_msglevel, 2093 .get_sset_count = bnxt_get_sset_count, 2094 .get_strings = bnxt_get_strings, 2095 .get_ethtool_stats = bnxt_get_ethtool_stats, 2096 .set_ringparam = bnxt_set_ringparam, 2097 .get_ringparam = bnxt_get_ringparam, 2098 .get_channels = bnxt_get_channels, 2099 .set_channels = bnxt_set_channels, 2100 .get_rxnfc = bnxt_get_rxnfc, 2101 .set_rxnfc = bnxt_set_rxnfc, 2102 .get_rxfh_indir_size = bnxt_get_rxfh_indir_size, 2103 .get_rxfh_key_size = bnxt_get_rxfh_key_size, 2104 .get_rxfh = bnxt_get_rxfh, 2105 .flash_device = bnxt_flash_device, 2106 .get_eeprom_len = bnxt_get_eeprom_len, 2107 .get_eeprom = bnxt_get_eeprom, 2108 .set_eeprom = bnxt_set_eeprom, 2109 .get_link = bnxt_get_link, 2110 .get_eee = bnxt_get_eee, 2111 .set_eee = bnxt_set_eee, 2112 .get_module_info = bnxt_get_module_info, 2113 .get_module_eeprom = bnxt_get_module_eeprom, 2114 .nway_reset = bnxt_nway_reset 2115 }; 2116