Merge tag 'qcom-soc-for-4.7-2' into net-next
[linux-2.6-block.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_ethtool.c
CommitLineData
c0c050c5
MC
1/* Broadcom NetXtreme-C/E network driver.
2 *
11f15ed3 3 * Copyright (c) 2014-2016 Broadcom Corporation
c0c050c5
MC
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
3ebf6f0a 10#include <linux/ctype.h>
8ddc9aaa 11#include <linux/stringify.h>
c0c050c5
MC
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
3ebf6f0a
RS
25static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen);
26
c0c050c5
MC
27static u32 bnxt_get_msglevel(struct net_device *dev)
28{
29 struct bnxt *bp = netdev_priv(dev);
30
31 return bp->msg_enable;
32}
33
34static void bnxt_set_msglevel(struct net_device *dev, u32 value)
35{
36 struct bnxt *bp = netdev_priv(dev);
37
38 bp->msg_enable = value;
39}
40
41static int bnxt_get_coalesce(struct net_device *dev,
42 struct ethtool_coalesce *coal)
43{
44 struct bnxt *bp = netdev_priv(dev);
45
46 memset(coal, 0, sizeof(*coal));
47
dfb5b894
MC
48 coal->rx_coalesce_usecs = bp->rx_coal_ticks;
49 /* 2 completion records per rx packet */
50 coal->rx_max_coalesced_frames = bp->rx_coal_bufs / 2;
51 coal->rx_coalesce_usecs_irq = bp->rx_coal_ticks_irq;
52 coal->rx_max_coalesced_frames_irq = bp->rx_coal_bufs_irq / 2;
c0c050c5 53
dfc9c94a
MC
54 coal->tx_coalesce_usecs = bp->tx_coal_ticks;
55 coal->tx_max_coalesced_frames = bp->tx_coal_bufs;
56 coal->tx_coalesce_usecs_irq = bp->tx_coal_ticks_irq;
57 coal->tx_max_coalesced_frames_irq = bp->tx_coal_bufs_irq;
58
c0c050c5
MC
59 return 0;
60}
61
62static int bnxt_set_coalesce(struct net_device *dev,
63 struct ethtool_coalesce *coal)
64{
65 struct bnxt *bp = netdev_priv(dev);
66 int rc = 0;
67
dfb5b894
MC
68 bp->rx_coal_ticks = coal->rx_coalesce_usecs;
69 /* 2 completion records per rx packet */
70 bp->rx_coal_bufs = coal->rx_max_coalesced_frames * 2;
71 bp->rx_coal_ticks_irq = coal->rx_coalesce_usecs_irq;
72 bp->rx_coal_bufs_irq = coal->rx_max_coalesced_frames_irq * 2;
c0c050c5 73
dfc9c94a
MC
74 bp->tx_coal_ticks = coal->tx_coalesce_usecs;
75 bp->tx_coal_bufs = coal->tx_max_coalesced_frames;
76 bp->tx_coal_ticks_irq = coal->tx_coalesce_usecs_irq;
77 bp->tx_coal_bufs_irq = coal->tx_max_coalesced_frames_irq;
78
c0c050c5
MC
79 if (netif_running(dev))
80 rc = bnxt_hwrm_set_coal(bp);
81
82 return rc;
83}
84
85#define BNXT_NUM_STATS 21
86
8ddc9aaa
MC
87#define BNXT_RX_STATS_OFFSET(counter) \
88 (offsetof(struct rx_port_stats, counter) / 8)
89
90#define BNXT_RX_STATS_ENTRY(counter) \
91 { BNXT_RX_STATS_OFFSET(counter), __stringify(counter) }
92
93#define BNXT_TX_STATS_OFFSET(counter) \
94 ((offsetof(struct tx_port_stats, counter) + \
95 sizeof(struct rx_port_stats) + 512) / 8)
96
97#define BNXT_TX_STATS_ENTRY(counter) \
98 { BNXT_TX_STATS_OFFSET(counter), __stringify(counter) }
99
100static const struct {
101 long offset;
102 char string[ETH_GSTRING_LEN];
103} bnxt_port_stats_arr[] = {
104 BNXT_RX_STATS_ENTRY(rx_64b_frames),
105 BNXT_RX_STATS_ENTRY(rx_65b_127b_frames),
106 BNXT_RX_STATS_ENTRY(rx_128b_255b_frames),
107 BNXT_RX_STATS_ENTRY(rx_256b_511b_frames),
108 BNXT_RX_STATS_ENTRY(rx_512b_1023b_frames),
109 BNXT_RX_STATS_ENTRY(rx_1024b_1518_frames),
110 BNXT_RX_STATS_ENTRY(rx_good_vlan_frames),
111 BNXT_RX_STATS_ENTRY(rx_1519b_2047b_frames),
112 BNXT_RX_STATS_ENTRY(rx_2048b_4095b_frames),
113 BNXT_RX_STATS_ENTRY(rx_4096b_9216b_frames),
114 BNXT_RX_STATS_ENTRY(rx_9217b_16383b_frames),
115 BNXT_RX_STATS_ENTRY(rx_total_frames),
116 BNXT_RX_STATS_ENTRY(rx_ucast_frames),
117 BNXT_RX_STATS_ENTRY(rx_mcast_frames),
118 BNXT_RX_STATS_ENTRY(rx_bcast_frames),
119 BNXT_RX_STATS_ENTRY(rx_fcs_err_frames),
120 BNXT_RX_STATS_ENTRY(rx_ctrl_frames),
121 BNXT_RX_STATS_ENTRY(rx_pause_frames),
122 BNXT_RX_STATS_ENTRY(rx_pfc_frames),
123 BNXT_RX_STATS_ENTRY(rx_align_err_frames),
124 BNXT_RX_STATS_ENTRY(rx_ovrsz_frames),
125 BNXT_RX_STATS_ENTRY(rx_jbr_frames),
126 BNXT_RX_STATS_ENTRY(rx_mtu_err_frames),
127 BNXT_RX_STATS_ENTRY(rx_tagged_frames),
128 BNXT_RX_STATS_ENTRY(rx_double_tagged_frames),
129 BNXT_RX_STATS_ENTRY(rx_good_frames),
130 BNXT_RX_STATS_ENTRY(rx_undrsz_frames),
131 BNXT_RX_STATS_ENTRY(rx_eee_lpi_events),
132 BNXT_RX_STATS_ENTRY(rx_eee_lpi_duration),
133 BNXT_RX_STATS_ENTRY(rx_bytes),
134 BNXT_RX_STATS_ENTRY(rx_runt_bytes),
135 BNXT_RX_STATS_ENTRY(rx_runt_frames),
136
137 BNXT_TX_STATS_ENTRY(tx_64b_frames),
138 BNXT_TX_STATS_ENTRY(tx_65b_127b_frames),
139 BNXT_TX_STATS_ENTRY(tx_128b_255b_frames),
140 BNXT_TX_STATS_ENTRY(tx_256b_511b_frames),
141 BNXT_TX_STATS_ENTRY(tx_512b_1023b_frames),
142 BNXT_TX_STATS_ENTRY(tx_1024b_1518_frames),
143 BNXT_TX_STATS_ENTRY(tx_good_vlan_frames),
144 BNXT_TX_STATS_ENTRY(tx_1519b_2047_frames),
145 BNXT_TX_STATS_ENTRY(tx_2048b_4095b_frames),
146 BNXT_TX_STATS_ENTRY(tx_4096b_9216b_frames),
147 BNXT_TX_STATS_ENTRY(tx_9217b_16383b_frames),
148 BNXT_TX_STATS_ENTRY(tx_good_frames),
149 BNXT_TX_STATS_ENTRY(tx_total_frames),
150 BNXT_TX_STATS_ENTRY(tx_ucast_frames),
151 BNXT_TX_STATS_ENTRY(tx_mcast_frames),
152 BNXT_TX_STATS_ENTRY(tx_bcast_frames),
153 BNXT_TX_STATS_ENTRY(tx_pause_frames),
154 BNXT_TX_STATS_ENTRY(tx_pfc_frames),
155 BNXT_TX_STATS_ENTRY(tx_jabber_frames),
156 BNXT_TX_STATS_ENTRY(tx_fcs_err_frames),
157 BNXT_TX_STATS_ENTRY(tx_err),
158 BNXT_TX_STATS_ENTRY(tx_fifo_underruns),
159 BNXT_TX_STATS_ENTRY(tx_eee_lpi_events),
160 BNXT_TX_STATS_ENTRY(tx_eee_lpi_duration),
161 BNXT_TX_STATS_ENTRY(tx_total_collisions),
162 BNXT_TX_STATS_ENTRY(tx_bytes),
163};
164
165#define BNXT_NUM_PORT_STATS ARRAY_SIZE(bnxt_port_stats_arr)
166
c0c050c5
MC
167static int bnxt_get_sset_count(struct net_device *dev, int sset)
168{
169 struct bnxt *bp = netdev_priv(dev);
170
171 switch (sset) {
8ddc9aaa
MC
172 case ETH_SS_STATS: {
173 int num_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
174
175 if (bp->flags & BNXT_FLAG_PORT_STATS)
176 num_stats += BNXT_NUM_PORT_STATS;
177
178 return num_stats;
179 }
c0c050c5
MC
180 default:
181 return -EOPNOTSUPP;
182 }
183}
184
185static void bnxt_get_ethtool_stats(struct net_device *dev,
186 struct ethtool_stats *stats, u64 *buf)
187{
188 u32 i, j = 0;
189 struct bnxt *bp = netdev_priv(dev);
190 u32 buf_size = sizeof(struct ctx_hw_stats) * bp->cp_nr_rings;
191 u32 stat_fields = sizeof(struct ctx_hw_stats) / 8;
192
193 memset(buf, 0, buf_size);
194
195 if (!bp->bnapi)
196 return;
197
198 for (i = 0; i < bp->cp_nr_rings; i++) {
199 struct bnxt_napi *bnapi = bp->bnapi[i];
200 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
201 __le64 *hw_stats = (__le64 *)cpr->hw_stats;
202 int k;
203
204 for (k = 0; k < stat_fields; j++, k++)
205 buf[j] = le64_to_cpu(hw_stats[k]);
206 buf[j++] = cpr->rx_l4_csum_errors;
207 }
8ddc9aaa
MC
208 if (bp->flags & BNXT_FLAG_PORT_STATS) {
209 __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
210
211 for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
212 buf[j] = le64_to_cpu(*(port_stats +
213 bnxt_port_stats_arr[i].offset));
214 }
215 }
c0c050c5
MC
216}
217
218static void bnxt_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
219{
220 struct bnxt *bp = netdev_priv(dev);
221 u32 i;
222
223 switch (stringset) {
224 /* The number of strings must match BNXT_NUM_STATS defined above. */
225 case ETH_SS_STATS:
226 for (i = 0; i < bp->cp_nr_rings; i++) {
227 sprintf(buf, "[%d]: rx_ucast_packets", i);
228 buf += ETH_GSTRING_LEN;
229 sprintf(buf, "[%d]: rx_mcast_packets", i);
230 buf += ETH_GSTRING_LEN;
231 sprintf(buf, "[%d]: rx_bcast_packets", i);
232 buf += ETH_GSTRING_LEN;
233 sprintf(buf, "[%d]: rx_discards", i);
234 buf += ETH_GSTRING_LEN;
235 sprintf(buf, "[%d]: rx_drops", i);
236 buf += ETH_GSTRING_LEN;
237 sprintf(buf, "[%d]: rx_ucast_bytes", i);
238 buf += ETH_GSTRING_LEN;
239 sprintf(buf, "[%d]: rx_mcast_bytes", i);
240 buf += ETH_GSTRING_LEN;
241 sprintf(buf, "[%d]: rx_bcast_bytes", i);
242 buf += ETH_GSTRING_LEN;
243 sprintf(buf, "[%d]: tx_ucast_packets", i);
244 buf += ETH_GSTRING_LEN;
245 sprintf(buf, "[%d]: tx_mcast_packets", i);
246 buf += ETH_GSTRING_LEN;
247 sprintf(buf, "[%d]: tx_bcast_packets", i);
248 buf += ETH_GSTRING_LEN;
249 sprintf(buf, "[%d]: tx_discards", i);
250 buf += ETH_GSTRING_LEN;
251 sprintf(buf, "[%d]: tx_drops", i);
252 buf += ETH_GSTRING_LEN;
253 sprintf(buf, "[%d]: tx_ucast_bytes", i);
254 buf += ETH_GSTRING_LEN;
255 sprintf(buf, "[%d]: tx_mcast_bytes", i);
256 buf += ETH_GSTRING_LEN;
257 sprintf(buf, "[%d]: tx_bcast_bytes", i);
258 buf += ETH_GSTRING_LEN;
259 sprintf(buf, "[%d]: tpa_packets", i);
260 buf += ETH_GSTRING_LEN;
261 sprintf(buf, "[%d]: tpa_bytes", i);
262 buf += ETH_GSTRING_LEN;
263 sprintf(buf, "[%d]: tpa_events", i);
264 buf += ETH_GSTRING_LEN;
265 sprintf(buf, "[%d]: tpa_aborts", i);
266 buf += ETH_GSTRING_LEN;
267 sprintf(buf, "[%d]: rx_l4_csum_errors", i);
268 buf += ETH_GSTRING_LEN;
269 }
8ddc9aaa
MC
270 if (bp->flags & BNXT_FLAG_PORT_STATS) {
271 for (i = 0; i < BNXT_NUM_PORT_STATS; i++) {
272 strcpy(buf, bnxt_port_stats_arr[i].string);
273 buf += ETH_GSTRING_LEN;
274 }
275 }
c0c050c5
MC
276 break;
277 default:
278 netdev_err(bp->dev, "bnxt_get_strings invalid request %x\n",
279 stringset);
280 break;
281 }
282}
283
284static void bnxt_get_ringparam(struct net_device *dev,
285 struct ethtool_ringparam *ering)
286{
287 struct bnxt *bp = netdev_priv(dev);
288
289 ering->rx_max_pending = BNXT_MAX_RX_DESC_CNT;
290 ering->rx_jumbo_max_pending = BNXT_MAX_RX_JUM_DESC_CNT;
291 ering->tx_max_pending = BNXT_MAX_TX_DESC_CNT;
292
293 ering->rx_pending = bp->rx_ring_size;
294 ering->rx_jumbo_pending = bp->rx_agg_ring_size;
295 ering->tx_pending = bp->tx_ring_size;
296}
297
298static int bnxt_set_ringparam(struct net_device *dev,
299 struct ethtool_ringparam *ering)
300{
301 struct bnxt *bp = netdev_priv(dev);
302
303 if ((ering->rx_pending > BNXT_MAX_RX_DESC_CNT) ||
304 (ering->tx_pending > BNXT_MAX_TX_DESC_CNT) ||
305 (ering->tx_pending <= MAX_SKB_FRAGS))
306 return -EINVAL;
307
308 if (netif_running(dev))
309 bnxt_close_nic(bp, false, false);
310
311 bp->rx_ring_size = ering->rx_pending;
312 bp->tx_ring_size = ering->tx_pending;
313 bnxt_set_ring_params(bp);
314
315 if (netif_running(dev))
316 return bnxt_open_nic(bp, false, false);
317
318 return 0;
319}
320
321static void bnxt_get_channels(struct net_device *dev,
322 struct ethtool_channels *channel)
323{
324 struct bnxt *bp = netdev_priv(dev);
325 int max_rx_rings, max_tx_rings, tcs;
326
6e6c5a57 327 bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, true);
068c9ec6
MC
328 channel->max_combined = max_rx_rings;
329
330 bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, false);
c0c050c5
MC
331 tcs = netdev_get_num_tc(dev);
332 if (tcs > 1)
333 max_tx_rings /= tcs;
334
335 channel->max_rx = max_rx_rings;
336 channel->max_tx = max_tx_rings;
337 channel->max_other = 0;
068c9ec6
MC
338 if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
339 channel->combined_count = bp->rx_nr_rings;
340 } else {
341 channel->rx_count = bp->rx_nr_rings;
342 channel->tx_count = bp->tx_nr_rings_per_tc;
343 }
c0c050c5
MC
344}
345
346static int bnxt_set_channels(struct net_device *dev,
347 struct ethtool_channels *channel)
348{
349 struct bnxt *bp = netdev_priv(dev);
350 int max_rx_rings, max_tx_rings, tcs;
351 u32 rc = 0;
068c9ec6 352 bool sh = false;
c0c050c5 353
068c9ec6 354 if (channel->other_count)
c0c050c5
MC
355 return -EINVAL;
356
068c9ec6
MC
357 if (!channel->combined_count &&
358 (!channel->rx_count || !channel->tx_count))
359 return -EINVAL;
360
361 if (channel->combined_count &&
362 (channel->rx_count || channel->tx_count))
363 return -EINVAL;
364
365 if (channel->combined_count)
366 sh = true;
367
368 bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
369
c0c050c5
MC
370 tcs = netdev_get_num_tc(dev);
371 if (tcs > 1)
372 max_tx_rings /= tcs;
373
068c9ec6
MC
374 if (sh && (channel->combined_count > max_rx_rings ||
375 channel->combined_count > max_tx_rings))
376 return -ENOMEM;
377
378 if (!sh && (channel->rx_count > max_rx_rings ||
379 channel->tx_count > max_tx_rings))
380 return -ENOMEM;
c0c050c5
MC
381
382 if (netif_running(dev)) {
383 if (BNXT_PF(bp)) {
384 /* TODO CHIMP_FW: Send message to all VF's
385 * before PF unload
386 */
387 }
388 rc = bnxt_close_nic(bp, true, false);
389 if (rc) {
390 netdev_err(bp->dev, "Set channel failure rc :%x\n",
391 rc);
392 return rc;
393 }
394 }
395
068c9ec6
MC
396 if (sh) {
397 bp->flags |= BNXT_FLAG_SHARED_RINGS;
398 bp->rx_nr_rings = channel->combined_count;
399 bp->tx_nr_rings_per_tc = channel->combined_count;
400 } else {
401 bp->flags &= ~BNXT_FLAG_SHARED_RINGS;
402 bp->rx_nr_rings = channel->rx_count;
403 bp->tx_nr_rings_per_tc = channel->tx_count;
404 }
405
c0c050c5
MC
406 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
407 if (tcs > 1)
408 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
068c9ec6
MC
409
410 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
411 bp->tx_nr_rings + bp->rx_nr_rings;
412
c0c050c5
MC
413 bp->num_stat_ctxs = bp->cp_nr_rings;
414
2bcfa6f6
MC
415 /* After changing number of rx channels, update NTUPLE feature. */
416 netdev_update_features(dev);
c0c050c5
MC
417 if (netif_running(dev)) {
418 rc = bnxt_open_nic(bp, true, false);
419 if ((!rc) && BNXT_PF(bp)) {
420 /* TODO CHIMP_FW: Send message to all VF's
421 * to renable
422 */
423 }
424 }
425
426 return rc;
427}
428
429#ifdef CONFIG_RFS_ACCEL
430static int bnxt_grxclsrlall(struct bnxt *bp, struct ethtool_rxnfc *cmd,
431 u32 *rule_locs)
432{
433 int i, j = 0;
434
435 cmd->data = bp->ntp_fltr_count;
436 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
437 struct hlist_head *head;
438 struct bnxt_ntuple_filter *fltr;
439
440 head = &bp->ntp_fltr_hash_tbl[i];
441 rcu_read_lock();
442 hlist_for_each_entry_rcu(fltr, head, hash) {
443 if (j == cmd->rule_cnt)
444 break;
445 rule_locs[j++] = fltr->sw_id;
446 }
447 rcu_read_unlock();
448 if (j == cmd->rule_cnt)
449 break;
450 }
451 cmd->rule_cnt = j;
452 return 0;
453}
454
455static int bnxt_grxclsrule(struct bnxt *bp, struct ethtool_rxnfc *cmd)
456{
457 struct ethtool_rx_flow_spec *fs =
458 (struct ethtool_rx_flow_spec *)&cmd->fs;
459 struct bnxt_ntuple_filter *fltr;
460 struct flow_keys *fkeys;
461 int i, rc = -EINVAL;
462
463 if (fs->location < 0 || fs->location >= BNXT_NTP_FLTR_MAX_FLTR)
464 return rc;
465
466 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
467 struct hlist_head *head;
468
469 head = &bp->ntp_fltr_hash_tbl[i];
470 rcu_read_lock();
471 hlist_for_each_entry_rcu(fltr, head, hash) {
472 if (fltr->sw_id == fs->location)
473 goto fltr_found;
474 }
475 rcu_read_unlock();
476 }
477 return rc;
478
479fltr_found:
480 fkeys = &fltr->fkeys;
481 if (fkeys->basic.ip_proto == IPPROTO_TCP)
482 fs->flow_type = TCP_V4_FLOW;
483 else if (fkeys->basic.ip_proto == IPPROTO_UDP)
484 fs->flow_type = UDP_V4_FLOW;
485 else
486 goto fltr_err;
487
488 fs->h_u.tcp_ip4_spec.ip4src = fkeys->addrs.v4addrs.src;
489 fs->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(~0);
490
491 fs->h_u.tcp_ip4_spec.ip4dst = fkeys->addrs.v4addrs.dst;
492 fs->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(~0);
493
494 fs->h_u.tcp_ip4_spec.psrc = fkeys->ports.src;
495 fs->m_u.tcp_ip4_spec.psrc = cpu_to_be16(~0);
496
497 fs->h_u.tcp_ip4_spec.pdst = fkeys->ports.dst;
498 fs->m_u.tcp_ip4_spec.pdst = cpu_to_be16(~0);
499
500 fs->ring_cookie = fltr->rxq;
501 rc = 0;
502
503fltr_err:
504 rcu_read_unlock();
505
506 return rc;
507}
508
509static int bnxt_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
510 u32 *rule_locs)
511{
512 struct bnxt *bp = netdev_priv(dev);
513 int rc = 0;
514
515 switch (cmd->cmd) {
516 case ETHTOOL_GRXRINGS:
517 cmd->data = bp->rx_nr_rings;
518 break;
519
520 case ETHTOOL_GRXCLSRLCNT:
521 cmd->rule_cnt = bp->ntp_fltr_count;
522 cmd->data = BNXT_NTP_FLTR_MAX_FLTR;
523 break;
524
525 case ETHTOOL_GRXCLSRLALL:
526 rc = bnxt_grxclsrlall(bp, cmd, (u32 *)rule_locs);
527 break;
528
529 case ETHTOOL_GRXCLSRULE:
530 rc = bnxt_grxclsrule(bp, cmd);
531 break;
532
533 default:
534 rc = -EOPNOTSUPP;
535 break;
536 }
537
538 return rc;
539}
540#endif
541
542static u32 bnxt_get_rxfh_indir_size(struct net_device *dev)
543{
544 return HW_HASH_INDEX_SIZE;
545}
546
547static u32 bnxt_get_rxfh_key_size(struct net_device *dev)
548{
549 return HW_HASH_KEY_SIZE;
550}
551
552static int bnxt_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
553 u8 *hfunc)
554{
555 struct bnxt *bp = netdev_priv(dev);
556 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
557 int i = 0;
558
559 if (hfunc)
560 *hfunc = ETH_RSS_HASH_TOP;
561
562 if (indir)
563 for (i = 0; i < HW_HASH_INDEX_SIZE; i++)
564 indir[i] = le16_to_cpu(vnic->rss_table[i]);
565
566 if (key)
567 memcpy(key, vnic->rss_hash_key, HW_HASH_KEY_SIZE);
568
569 return 0;
570}
571
572static void bnxt_get_drvinfo(struct net_device *dev,
573 struct ethtool_drvinfo *info)
574{
575 struct bnxt *bp = netdev_priv(dev);
3ebf6f0a
RS
576 char *pkglog;
577 char *pkgver = NULL;
c0c050c5 578
3ebf6f0a
RS
579 pkglog = kmalloc(BNX_PKG_LOG_MAX_LENGTH, GFP_KERNEL);
580 if (pkglog)
581 pkgver = bnxt_get_pkgver(dev, pkglog, BNX_PKG_LOG_MAX_LENGTH);
c0c050c5
MC
582 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
583 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
3ebf6f0a
RS
584 if (pkgver && *pkgver != 0 && isdigit(*pkgver))
585 snprintf(info->fw_version, sizeof(info->fw_version) - 1,
586 "%s pkg %s", bp->fw_ver_str, pkgver);
587 else
588 strlcpy(info->fw_version, bp->fw_ver_str,
589 sizeof(info->fw_version));
c0c050c5
MC
590 strlcpy(info->bus_info, pci_name(bp->pdev), sizeof(info->bus_info));
591 info->n_stats = BNXT_NUM_STATS * bp->cp_nr_rings;
592 info->testinfo_len = BNXT_NUM_TESTS(bp);
593 /* TODO CHIMP_FW: eeprom dump details */
594 info->eedump_len = 0;
595 /* TODO CHIMP FW: reg dump details */
596 info->regdump_len = 0;
3ebf6f0a 597 kfree(pkglog);
c0c050c5
MC
598}
599
170ce013 600u32 _bnxt_fw_to_ethtool_adv_spds(u16 fw_speeds, u8 fw_pause)
c0c050c5 601{
c0c050c5
MC
602 u32 speed_mask = 0;
603
604 /* TODO: support 25GB, 40GB, 50GB with different cable type */
605 /* set the advertised speeds */
606 if (fw_speeds & BNXT_LINK_SPEED_MSK_100MB)
607 speed_mask |= ADVERTISED_100baseT_Full;
608 if (fw_speeds & BNXT_LINK_SPEED_MSK_1GB)
609 speed_mask |= ADVERTISED_1000baseT_Full;
610 if (fw_speeds & BNXT_LINK_SPEED_MSK_2_5GB)
611 speed_mask |= ADVERTISED_2500baseX_Full;
612 if (fw_speeds & BNXT_LINK_SPEED_MSK_10GB)
613 speed_mask |= ADVERTISED_10000baseT_Full;
c0c050c5 614 if (fw_speeds & BNXT_LINK_SPEED_MSK_40GB)
1c49c421 615 speed_mask |= ADVERTISED_40000baseCR4_Full;
27c4d578
MC
616
617 if ((fw_pause & BNXT_LINK_PAUSE_BOTH) == BNXT_LINK_PAUSE_BOTH)
618 speed_mask |= ADVERTISED_Pause;
619 else if (fw_pause & BNXT_LINK_PAUSE_TX)
620 speed_mask |= ADVERTISED_Asym_Pause;
621 else if (fw_pause & BNXT_LINK_PAUSE_RX)
622 speed_mask |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
623
c0c050c5
MC
624 return speed_mask;
625}
626
27c4d578
MC
627static u32 bnxt_fw_to_ethtool_advertised_spds(struct bnxt_link_info *link_info)
628{
629 u16 fw_speeds = link_info->auto_link_speeds;
630 u8 fw_pause = 0;
631
632 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
633 fw_pause = link_info->auto_pause_setting;
634
635 return _bnxt_fw_to_ethtool_adv_spds(fw_speeds, fw_pause);
636}
637
3277360e
MC
638static u32 bnxt_fw_to_ethtool_lp_adv(struct bnxt_link_info *link_info)
639{
640 u16 fw_speeds = link_info->lp_auto_link_speeds;
641 u8 fw_pause = 0;
642
643 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
644 fw_pause = link_info->lp_pause;
645
646 return _bnxt_fw_to_ethtool_adv_spds(fw_speeds, fw_pause);
647}
648
4b32cacc
MC
649static u32 bnxt_fw_to_ethtool_support_spds(struct bnxt_link_info *link_info)
650{
651 u16 fw_speeds = link_info->support_speeds;
652 u32 supported;
653
654 supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
655 return supported | SUPPORTED_Pause | SUPPORTED_Asym_Pause;
656}
657
c0c050c5
MC
658u32 bnxt_fw_to_ethtool_speed(u16 fw_link_speed)
659{
660 switch (fw_link_speed) {
661 case BNXT_LINK_SPEED_100MB:
662 return SPEED_100;
663 case BNXT_LINK_SPEED_1GB:
664 return SPEED_1000;
665 case BNXT_LINK_SPEED_2_5GB:
666 return SPEED_2500;
667 case BNXT_LINK_SPEED_10GB:
668 return SPEED_10000;
669 case BNXT_LINK_SPEED_20GB:
670 return SPEED_20000;
671 case BNXT_LINK_SPEED_25GB:
672 return SPEED_25000;
673 case BNXT_LINK_SPEED_40GB:
674 return SPEED_40000;
675 case BNXT_LINK_SPEED_50GB:
676 return SPEED_50000;
677 default:
678 return SPEED_UNKNOWN;
679 }
680}
681
682static int bnxt_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
683{
684 struct bnxt *bp = netdev_priv(dev);
685 struct bnxt_link_info *link_info = &bp->link_info;
686 u16 ethtool_speed;
687
688 cmd->supported = bnxt_fw_to_ethtool_support_spds(link_info);
689
690 if (link_info->auto_link_speeds)
691 cmd->supported |= SUPPORTED_Autoneg;
692
b763499e 693 if (link_info->autoneg) {
c0c050c5
MC
694 cmd->advertising =
695 bnxt_fw_to_ethtool_advertised_spds(link_info);
696 cmd->advertising |= ADVERTISED_Autoneg;
697 cmd->autoneg = AUTONEG_ENABLE;
3277360e
MC
698 if (link_info->phy_link_status == BNXT_LINK_LINK)
699 cmd->lp_advertising =
700 bnxt_fw_to_ethtool_lp_adv(link_info);
29c262fe
MC
701 ethtool_speed = bnxt_fw_to_ethtool_speed(link_info->link_speed);
702 if (!netif_carrier_ok(dev))
703 cmd->duplex = DUPLEX_UNKNOWN;
704 else if (link_info->duplex & BNXT_LINK_DUPLEX_FULL)
705 cmd->duplex = DUPLEX_FULL;
706 else
707 cmd->duplex = DUPLEX_HALF;
c0c050c5
MC
708 } else {
709 cmd->autoneg = AUTONEG_DISABLE;
710 cmd->advertising = 0;
29c262fe
MC
711 ethtool_speed =
712 bnxt_fw_to_ethtool_speed(link_info->req_link_speed);
713 cmd->duplex = DUPLEX_HALF;
714 if (link_info->req_duplex == BNXT_LINK_DUPLEX_FULL)
715 cmd->duplex = DUPLEX_FULL;
c0c050c5 716 }
29c262fe 717 ethtool_cmd_speed_set(cmd, ethtool_speed);
c0c050c5
MC
718
719 cmd->port = PORT_NONE;
720 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
721 cmd->port = PORT_TP;
722 cmd->supported |= SUPPORTED_TP;
723 cmd->advertising |= ADVERTISED_TP;
724 } else {
725 cmd->supported |= SUPPORTED_FIBRE;
726 cmd->advertising |= ADVERTISED_FIBRE;
727
728 if (link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_DAC)
729 cmd->port = PORT_DA;
730 else if (link_info->media_type ==
731 PORT_PHY_QCFG_RESP_MEDIA_TYPE_FIBRE)
732 cmd->port = PORT_FIBRE;
733 }
734
c0c050c5 735 if (link_info->transceiver ==
11f15ed3 736 PORT_PHY_QCFG_RESP_XCVR_PKG_TYPE_XCVR_INTERNAL)
c0c050c5
MC
737 cmd->transceiver = XCVR_INTERNAL;
738 else
739 cmd->transceiver = XCVR_EXTERNAL;
740 cmd->phy_address = link_info->phy_addr;
741
742 return 0;
743}
744
745static u32 bnxt_get_fw_speed(struct net_device *dev, u16 ethtool_speed)
746{
9d9cee08
MC
747 struct bnxt *bp = netdev_priv(dev);
748 struct bnxt_link_info *link_info = &bp->link_info;
749 u16 support_spds = link_info->support_speeds;
750 u32 fw_speed = 0;
751
c0c050c5
MC
752 switch (ethtool_speed) {
753 case SPEED_100:
9d9cee08
MC
754 if (support_spds & BNXT_LINK_SPEED_MSK_100MB)
755 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_100MB;
756 break;
c0c050c5 757 case SPEED_1000:
9d9cee08
MC
758 if (support_spds & BNXT_LINK_SPEED_MSK_1GB)
759 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_1GB;
760 break;
c0c050c5 761 case SPEED_2500:
9d9cee08
MC
762 if (support_spds & BNXT_LINK_SPEED_MSK_2_5GB)
763 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_2_5GB;
764 break;
c0c050c5 765 case SPEED_10000:
9d9cee08
MC
766 if (support_spds & BNXT_LINK_SPEED_MSK_10GB)
767 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_10GB;
768 break;
c0c050c5 769 case SPEED_20000:
9d9cee08
MC
770 if (support_spds & BNXT_LINK_SPEED_MSK_20GB)
771 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_20GB;
772 break;
c0c050c5 773 case SPEED_25000:
9d9cee08
MC
774 if (support_spds & BNXT_LINK_SPEED_MSK_25GB)
775 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_25GB;
776 break;
c0c050c5 777 case SPEED_40000:
9d9cee08
MC
778 if (support_spds & BNXT_LINK_SPEED_MSK_40GB)
779 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_40GB;
780 break;
c0c050c5 781 case SPEED_50000:
9d9cee08
MC
782 if (support_spds & BNXT_LINK_SPEED_MSK_50GB)
783 fw_speed = PORT_PHY_CFG_REQ_AUTO_LINK_SPEED_50GB;
784 break;
c0c050c5
MC
785 default:
786 netdev_err(dev, "unsupported speed!\n");
787 break;
788 }
9d9cee08 789 return fw_speed;
c0c050c5
MC
790}
791
939f7f0c 792u16 bnxt_get_fw_auto_link_speeds(u32 advertising)
c0c050c5
MC
793{
794 u16 fw_speed_mask = 0;
795
796 /* only support autoneg at speed 100, 1000, and 10000 */
797 if (advertising & (ADVERTISED_100baseT_Full |
798 ADVERTISED_100baseT_Half)) {
799 fw_speed_mask |= BNXT_LINK_SPEED_MSK_100MB;
800 }
801 if (advertising & (ADVERTISED_1000baseT_Full |
802 ADVERTISED_1000baseT_Half)) {
803 fw_speed_mask |= BNXT_LINK_SPEED_MSK_1GB;
804 }
805 if (advertising & ADVERTISED_10000baseT_Full)
806 fw_speed_mask |= BNXT_LINK_SPEED_MSK_10GB;
807
1c49c421
MC
808 if (advertising & ADVERTISED_40000baseCR4_Full)
809 fw_speed_mask |= BNXT_LINK_SPEED_MSK_40GB;
810
c0c050c5
MC
811 return fw_speed_mask;
812}
813
814static int bnxt_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
815{
816 int rc = 0;
817 struct bnxt *bp = netdev_priv(dev);
818 struct bnxt_link_info *link_info = &bp->link_info;
819 u32 speed, fw_advertising = 0;
820 bool set_pause = false;
821
822 if (BNXT_VF(bp))
823 return rc;
824
825 if (cmd->autoneg == AUTONEG_ENABLE) {
f1a082a6
MC
826 u32 supported_spds = bnxt_fw_to_ethtool_support_spds(link_info);
827
828 if (cmd->advertising & ~(supported_spds | ADVERTISED_Autoneg |
829 ADVERTISED_TP | ADVERTISED_FIBRE)) {
c0c050c5
MC
830 netdev_err(dev, "Unsupported advertising mask (adv: 0x%x)\n",
831 cmd->advertising);
832 rc = -EINVAL;
833 goto set_setting_exit;
834 }
835 fw_advertising = bnxt_get_fw_auto_link_speeds(cmd->advertising);
836 if (fw_advertising & ~link_info->support_speeds) {
837 netdev_err(dev, "Advertising parameters are not supported! (adv: 0x%x)\n",
838 cmd->advertising);
839 rc = -EINVAL;
840 goto set_setting_exit;
841 }
842 link_info->autoneg |= BNXT_AUTONEG_SPEED;
843 if (!fw_advertising)
844 link_info->advertising = link_info->support_speeds;
845 else
846 link_info->advertising = fw_advertising;
847 /* any change to autoneg will cause link change, therefore the
848 * driver should put back the original pause setting in autoneg
849 */
850 set_pause = true;
851 } else {
9d9cee08 852 u16 fw_speed;
03efbec0 853 u8 phy_type = link_info->phy_type;
9d9cee08 854
03efbec0
MC
855 if (phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASET ||
856 phy_type == PORT_PHY_QCFG_RESP_PHY_TYPE_BASETE ||
857 link_info->media_type == PORT_PHY_QCFG_RESP_MEDIA_TYPE_TP) {
858 netdev_err(dev, "10GBase-T devices must autoneg\n");
859 rc = -EINVAL;
860 goto set_setting_exit;
861 }
c0c050c5
MC
862 /* TODO: currently don't support half duplex */
863 if (cmd->duplex == DUPLEX_HALF) {
864 netdev_err(dev, "HALF DUPLEX is not supported!\n");
865 rc = -EINVAL;
866 goto set_setting_exit;
867 }
868 /* If received a request for an unknown duplex, assume full*/
869 if (cmd->duplex == DUPLEX_UNKNOWN)
870 cmd->duplex = DUPLEX_FULL;
871 speed = ethtool_cmd_speed(cmd);
9d9cee08
MC
872 fw_speed = bnxt_get_fw_speed(dev, speed);
873 if (!fw_speed) {
874 rc = -EINVAL;
875 goto set_setting_exit;
876 }
877 link_info->req_link_speed = fw_speed;
c0c050c5 878 link_info->req_duplex = BNXT_LINK_DUPLEX_FULL;
b763499e 879 link_info->autoneg = 0;
c0c050c5
MC
880 link_info->advertising = 0;
881 }
882
883 if (netif_running(dev))
939f7f0c 884 rc = bnxt_hwrm_set_link_setting(bp, set_pause, false);
c0c050c5
MC
885
886set_setting_exit:
887 return rc;
888}
889
890static void bnxt_get_pauseparam(struct net_device *dev,
891 struct ethtool_pauseparam *epause)
892{
893 struct bnxt *bp = netdev_priv(dev);
894 struct bnxt_link_info *link_info = &bp->link_info;
895
896 if (BNXT_VF(bp))
897 return;
b763499e 898 epause->autoneg = !!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL);
3c02d1bb
MC
899 epause->rx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_RX);
900 epause->tx_pause = !!(link_info->req_flow_ctrl & BNXT_LINK_PAUSE_TX);
c0c050c5
MC
901}
902
903static int bnxt_set_pauseparam(struct net_device *dev,
904 struct ethtool_pauseparam *epause)
905{
906 int rc = 0;
907 struct bnxt *bp = netdev_priv(dev);
908 struct bnxt_link_info *link_info = &bp->link_info;
909
910 if (BNXT_VF(bp))
911 return rc;
912
913 if (epause->autoneg) {
b763499e
MC
914 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED))
915 return -EINVAL;
916
c0c050c5 917 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
c9ee9516
MC
918 if (bp->hwrm_spec_code >= 0x10201)
919 link_info->req_flow_ctrl =
920 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
c0c050c5
MC
921 } else {
922 /* when transition from auto pause to force pause,
923 * force a link change
924 */
925 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
926 link_info->force_link_chng = true;
927 link_info->autoneg &= ~BNXT_AUTONEG_FLOW_CTRL;
c9ee9516 928 link_info->req_flow_ctrl = 0;
c0c050c5
MC
929 }
930 if (epause->rx_pause)
931 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_RX;
c0c050c5
MC
932
933 if (epause->tx_pause)
934 link_info->req_flow_ctrl |= BNXT_LINK_PAUSE_TX;
c0c050c5
MC
935
936 if (netif_running(dev))
937 rc = bnxt_hwrm_set_pause(bp);
938 return rc;
939}
940
941static u32 bnxt_get_link(struct net_device *dev)
942{
943 struct bnxt *bp = netdev_priv(dev);
944
945 /* TODO: handle MF, VF, driver close case */
946 return bp->link_info.link_up;
947}
948
949static int bnxt_flash_nvram(struct net_device *dev,
950 u16 dir_type,
951 u16 dir_ordinal,
952 u16 dir_ext,
953 u16 dir_attr,
954 const u8 *data,
955 size_t data_len)
956{
957 struct bnxt *bp = netdev_priv(dev);
958 int rc;
959 struct hwrm_nvm_write_input req = {0};
960 dma_addr_t dma_handle;
961 u8 *kmem;
962
963 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_WRITE, -1, -1);
964
965 req.dir_type = cpu_to_le16(dir_type);
966 req.dir_ordinal = cpu_to_le16(dir_ordinal);
967 req.dir_ext = cpu_to_le16(dir_ext);
968 req.dir_attr = cpu_to_le16(dir_attr);
969 req.dir_data_length = cpu_to_le32(data_len);
970
971 kmem = dma_alloc_coherent(&bp->pdev->dev, data_len, &dma_handle,
972 GFP_KERNEL);
973 if (!kmem) {
974 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
975 (unsigned)data_len);
976 return -ENOMEM;
977 }
978 memcpy(kmem, data, data_len);
979 req.host_src_addr = cpu_to_le64(dma_handle);
980
981 rc = hwrm_send_message(bp, &req, sizeof(req), FLASH_NVRAM_TIMEOUT);
982 dma_free_coherent(&bp->pdev->dev, data_len, kmem, dma_handle);
983
984 return rc;
985}
986
d2d6318c
RS
987static int bnxt_firmware_reset(struct net_device *dev,
988 u16 dir_type)
989{
990 struct bnxt *bp = netdev_priv(dev);
991 struct hwrm_fw_reset_input req = {0};
992
993 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
994
995 /* TODO: Support ASAP ChiMP self-reset (e.g. upon PF driver unload) */
996 /* TODO: Address self-reset of APE/KONG/BONO/TANG or ungraceful reset */
997 /* (e.g. when firmware isn't already running) */
998 switch (dir_type) {
999 case BNX_DIR_TYPE_CHIMP_PATCH:
1000 case BNX_DIR_TYPE_BOOTCODE:
1001 case BNX_DIR_TYPE_BOOTCODE_2:
1002 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_BOOT;
1003 /* Self-reset ChiMP upon next PCIe reset: */
1004 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTPCIERST;
1005 break;
1006 case BNX_DIR_TYPE_APE_FW:
1007 case BNX_DIR_TYPE_APE_PATCH:
1008 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_MGMT;
1009 break;
1010 case BNX_DIR_TYPE_KONG_FW:
1011 case BNX_DIR_TYPE_KONG_PATCH:
1012 req.embedded_proc_type =
1013 FW_RESET_REQ_EMBEDDED_PROC_TYPE_NETCTRL;
1014 break;
1015 case BNX_DIR_TYPE_BONO_FW:
1016 case BNX_DIR_TYPE_BONO_PATCH:
1017 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_ROCE;
1018 break;
1019 default:
1020 return -EINVAL;
1021 }
1022
1023 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1024}
1025
c0c050c5
MC
1026static int bnxt_flash_firmware(struct net_device *dev,
1027 u16 dir_type,
1028 const u8 *fw_data,
1029 size_t fw_size)
1030{
1031 int rc = 0;
1032 u16 code_type;
1033 u32 stored_crc;
1034 u32 calculated_crc;
1035 struct bnxt_fw_header *header = (struct bnxt_fw_header *)fw_data;
1036
1037 switch (dir_type) {
1038 case BNX_DIR_TYPE_BOOTCODE:
1039 case BNX_DIR_TYPE_BOOTCODE_2:
1040 code_type = CODE_BOOT;
1041 break;
2731d70f
RS
1042 case BNX_DIR_TYPE_APE_FW:
1043 code_type = CODE_MCTP_PASSTHRU;
1044 break;
c0c050c5
MC
1045 default:
1046 netdev_err(dev, "Unsupported directory entry type: %u\n",
1047 dir_type);
1048 return -EINVAL;
1049 }
1050 if (fw_size < sizeof(struct bnxt_fw_header)) {
1051 netdev_err(dev, "Invalid firmware file size: %u\n",
1052 (unsigned int)fw_size);
1053 return -EINVAL;
1054 }
1055 if (header->signature != cpu_to_le32(BNXT_FIRMWARE_BIN_SIGNATURE)) {
1056 netdev_err(dev, "Invalid firmware signature: %08X\n",
1057 le32_to_cpu(header->signature));
1058 return -EINVAL;
1059 }
1060 if (header->code_type != code_type) {
1061 netdev_err(dev, "Expected firmware type: %d, read: %d\n",
1062 code_type, header->code_type);
1063 return -EINVAL;
1064 }
1065 if (header->device != DEVICE_CUMULUS_FAMILY) {
1066 netdev_err(dev, "Expected firmware device family %d, read: %d\n",
1067 DEVICE_CUMULUS_FAMILY, header->device);
1068 return -EINVAL;
1069 }
1070 /* Confirm the CRC32 checksum of the file: */
1071 stored_crc = le32_to_cpu(*(__le32 *)(fw_data + fw_size -
1072 sizeof(stored_crc)));
1073 calculated_crc = ~crc32(~0, fw_data, fw_size - sizeof(stored_crc));
1074 if (calculated_crc != stored_crc) {
1075 netdev_err(dev, "Firmware file CRC32 checksum (%08lX) does not match calculated checksum (%08lX)\n",
1076 (unsigned long)stored_crc,
1077 (unsigned long)calculated_crc);
1078 return -EINVAL;
1079 }
1080 /* TODO: Validate digital signature (RSA-encrypted SHA-256 hash) here */
1081 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1082 0, 0, fw_data, fw_size);
d2d6318c
RS
1083 if (rc == 0) /* Firmware update successful */
1084 rc = bnxt_firmware_reset(dev, dir_type);
1085
c0c050c5
MC
1086 return rc;
1087}
1088
1089static bool bnxt_dir_type_is_ape_bin_format(u16 dir_type)
1090{
1091 switch (dir_type) {
1092 case BNX_DIR_TYPE_CHIMP_PATCH:
1093 case BNX_DIR_TYPE_BOOTCODE:
1094 case BNX_DIR_TYPE_BOOTCODE_2:
1095 case BNX_DIR_TYPE_APE_FW:
1096 case BNX_DIR_TYPE_APE_PATCH:
1097 case BNX_DIR_TYPE_KONG_FW:
1098 case BNX_DIR_TYPE_KONG_PATCH:
1099 return true;
1100 }
1101
1102 return false;
1103}
1104
1105static bool bnxt_dir_type_is_unprotected_exec_format(u16 dir_type)
1106{
1107 switch (dir_type) {
1108 case BNX_DIR_TYPE_AVS:
1109 case BNX_DIR_TYPE_EXP_ROM_MBA:
1110 case BNX_DIR_TYPE_PCIE:
1111 case BNX_DIR_TYPE_TSCF_UCODE:
1112 case BNX_DIR_TYPE_EXT_PHY:
1113 case BNX_DIR_TYPE_CCM:
1114 case BNX_DIR_TYPE_ISCSI_BOOT:
1115 case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
1116 case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
1117 return true;
1118 }
1119
1120 return false;
1121}
1122
1123static bool bnxt_dir_type_is_executable(u16 dir_type)
1124{
1125 return bnxt_dir_type_is_ape_bin_format(dir_type) ||
1126 bnxt_dir_type_is_unprotected_exec_format(dir_type);
1127}
1128
1129static int bnxt_flash_firmware_from_file(struct net_device *dev,
1130 u16 dir_type,
1131 const char *filename)
1132{
1133 const struct firmware *fw;
1134 int rc;
1135
1136 if (bnxt_dir_type_is_executable(dir_type) == false)
1137 return -EINVAL;
1138
1139 rc = request_firmware(&fw, filename, &dev->dev);
1140 if (rc != 0) {
1141 netdev_err(dev, "Error %d requesting firmware file: %s\n",
1142 rc, filename);
1143 return rc;
1144 }
1145 if (bnxt_dir_type_is_ape_bin_format(dir_type) == true)
1146 rc = bnxt_flash_firmware(dev, dir_type, fw->data, fw->size);
1147 else
1148 rc = bnxt_flash_nvram(dev, dir_type, BNX_DIR_ORDINAL_FIRST,
1149 0, 0, fw->data, fw->size);
1150 release_firmware(fw);
1151 return rc;
1152}
1153
1154static int bnxt_flash_package_from_file(struct net_device *dev,
1155 char *filename)
1156{
1157 netdev_err(dev, "packages are not yet supported\n");
1158 return -EINVAL;
1159}
1160
1161static int bnxt_flash_device(struct net_device *dev,
1162 struct ethtool_flash *flash)
1163{
1164 if (!BNXT_PF((struct bnxt *)netdev_priv(dev))) {
1165 netdev_err(dev, "flashdev not supported from a virtual function\n");
1166 return -EINVAL;
1167 }
1168
1169 if (flash->region == ETHTOOL_FLASH_ALL_REGIONS)
1170 return bnxt_flash_package_from_file(dev, flash->data);
1171
1172 return bnxt_flash_firmware_from_file(dev, flash->region, flash->data);
1173}
1174
1175static int nvm_get_dir_info(struct net_device *dev, u32 *entries, u32 *length)
1176{
1177 struct bnxt *bp = netdev_priv(dev);
1178 int rc;
1179 struct hwrm_nvm_get_dir_info_input req = {0};
1180 struct hwrm_nvm_get_dir_info_output *output = bp->hwrm_cmd_resp_addr;
1181
1182 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_INFO, -1, -1);
1183
1184 mutex_lock(&bp->hwrm_cmd_lock);
1185 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1186 if (!rc) {
1187 *entries = le32_to_cpu(output->entries);
1188 *length = le32_to_cpu(output->entry_length);
1189 }
1190 mutex_unlock(&bp->hwrm_cmd_lock);
1191 return rc;
1192}
1193
1194static int bnxt_get_eeprom_len(struct net_device *dev)
1195{
1196 /* The -1 return value allows the entire 32-bit range of offsets to be
1197 * passed via the ethtool command-line utility.
1198 */
1199 return -1;
1200}
1201
1202static int bnxt_get_nvram_directory(struct net_device *dev, u32 len, u8 *data)
1203{
1204 struct bnxt *bp = netdev_priv(dev);
1205 int rc;
1206 u32 dir_entries;
1207 u32 entry_length;
1208 u8 *buf;
1209 size_t buflen;
1210 dma_addr_t dma_handle;
1211 struct hwrm_nvm_get_dir_entries_input req = {0};
1212
1213 rc = nvm_get_dir_info(dev, &dir_entries, &entry_length);
1214 if (rc != 0)
1215 return rc;
1216
1217 /* Insert 2 bytes of directory info (count and size of entries) */
1218 if (len < 2)
1219 return -EINVAL;
1220
1221 *data++ = dir_entries;
1222 *data++ = entry_length;
1223 len -= 2;
1224 memset(data, 0xff, len);
1225
1226 buflen = dir_entries * entry_length;
1227 buf = dma_alloc_coherent(&bp->pdev->dev, buflen, &dma_handle,
1228 GFP_KERNEL);
1229 if (!buf) {
1230 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1231 (unsigned)buflen);
1232 return -ENOMEM;
1233 }
1234 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_DIR_ENTRIES, -1, -1);
1235 req.host_dest_addr = cpu_to_le64(dma_handle);
1236 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1237 if (rc == 0)
1238 memcpy(data, buf, len > buflen ? buflen : len);
1239 dma_free_coherent(&bp->pdev->dev, buflen, buf, dma_handle);
1240 return rc;
1241}
1242
1243static int bnxt_get_nvram_item(struct net_device *dev, u32 index, u32 offset,
1244 u32 length, u8 *data)
1245{
1246 struct bnxt *bp = netdev_priv(dev);
1247 int rc;
1248 u8 *buf;
1249 dma_addr_t dma_handle;
1250 struct hwrm_nvm_read_input req = {0};
1251
1252 buf = dma_alloc_coherent(&bp->pdev->dev, length, &dma_handle,
1253 GFP_KERNEL);
1254 if (!buf) {
1255 netdev_err(dev, "dma_alloc_coherent failure, length = %u\n",
1256 (unsigned)length);
1257 return -ENOMEM;
1258 }
1259 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_READ, -1, -1);
1260 req.host_dest_addr = cpu_to_le64(dma_handle);
1261 req.dir_idx = cpu_to_le16(index);
1262 req.offset = cpu_to_le32(offset);
1263 req.len = cpu_to_le32(length);
1264
1265 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1266 if (rc == 0)
1267 memcpy(data, buf, length);
1268 dma_free_coherent(&bp->pdev->dev, length, buf, dma_handle);
1269 return rc;
1270}
1271
3ebf6f0a
RS
1272static int bnxt_find_nvram_item(struct net_device *dev, u16 type, u16 ordinal,
1273 u16 ext, u16 *index, u32 *item_length,
1274 u32 *data_length)
1275{
1276 struct bnxt *bp = netdev_priv(dev);
1277 int rc;
1278 struct hwrm_nvm_find_dir_entry_input req = {0};
1279 struct hwrm_nvm_find_dir_entry_output *output = bp->hwrm_cmd_resp_addr;
1280
1281 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_FIND_DIR_ENTRY, -1, -1);
1282 req.enables = 0;
1283 req.dir_idx = 0;
1284 req.dir_type = cpu_to_le16(type);
1285 req.dir_ordinal = cpu_to_le16(ordinal);
1286 req.dir_ext = cpu_to_le16(ext);
1287 req.opt_ordinal = NVM_FIND_DIR_ENTRY_REQ_OPT_ORDINAL_EQ;
90e20921 1288 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3ebf6f0a
RS
1289 if (rc == 0) {
1290 if (index)
1291 *index = le16_to_cpu(output->dir_idx);
1292 if (item_length)
1293 *item_length = le32_to_cpu(output->dir_item_length);
1294 if (data_length)
1295 *data_length = le32_to_cpu(output->dir_data_length);
1296 }
1297 return rc;
1298}
1299
1300static char *bnxt_parse_pkglog(int desired_field, u8 *data, size_t datalen)
1301{
1302 char *retval = NULL;
1303 char *p;
1304 char *value;
1305 int field = 0;
1306
1307 if (datalen < 1)
1308 return NULL;
1309 /* null-terminate the log data (removing last '\n'): */
1310 data[datalen - 1] = 0;
1311 for (p = data; *p != 0; p++) {
1312 field = 0;
1313 retval = NULL;
1314 while (*p != 0 && *p != '\n') {
1315 value = p;
1316 while (*p != 0 && *p != '\t' && *p != '\n')
1317 p++;
1318 if (field == desired_field)
1319 retval = value;
1320 if (*p != '\t')
1321 break;
1322 *p = 0;
1323 field++;
1324 p++;
1325 }
1326 if (*p == 0)
1327 break;
1328 *p = 0;
1329 }
1330 return retval;
1331}
1332
1333static char *bnxt_get_pkgver(struct net_device *dev, char *buf, size_t buflen)
1334{
1335 u16 index = 0;
1336 u32 datalen;
1337
1338 if (bnxt_find_nvram_item(dev, BNX_DIR_TYPE_PKG_LOG,
1339 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
1340 &index, NULL, &datalen) != 0)
1341 return NULL;
1342
1343 memset(buf, 0, buflen);
1344 if (bnxt_get_nvram_item(dev, index, 0, datalen, buf) != 0)
1345 return NULL;
1346
1347 return bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, buf,
1348 datalen);
1349}
1350
c0c050c5
MC
1351static int bnxt_get_eeprom(struct net_device *dev,
1352 struct ethtool_eeprom *eeprom,
1353 u8 *data)
1354{
1355 u32 index;
1356 u32 offset;
1357
1358 if (eeprom->offset == 0) /* special offset value to get directory */
1359 return bnxt_get_nvram_directory(dev, eeprom->len, data);
1360
1361 index = eeprom->offset >> 24;
1362 offset = eeprom->offset & 0xffffff;
1363
1364 if (index == 0) {
1365 netdev_err(dev, "unsupported index value: %d\n", index);
1366 return -EINVAL;
1367 }
1368
1369 return bnxt_get_nvram_item(dev, index - 1, offset, eeprom->len, data);
1370}
1371
1372static int bnxt_erase_nvram_directory(struct net_device *dev, u8 index)
1373{
1374 struct bnxt *bp = netdev_priv(dev);
1375 struct hwrm_nvm_erase_dir_entry_input req = {0};
1376
1377 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_ERASE_DIR_ENTRY, -1, -1);
1378 req.dir_idx = cpu_to_le16(index);
1379 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1380}
1381
1382static int bnxt_set_eeprom(struct net_device *dev,
1383 struct ethtool_eeprom *eeprom,
1384 u8 *data)
1385{
1386 struct bnxt *bp = netdev_priv(dev);
1387 u8 index, dir_op;
1388 u16 type, ext, ordinal, attr;
1389
1390 if (!BNXT_PF(bp)) {
1391 netdev_err(dev, "NVM write not supported from a virtual function\n");
1392 return -EINVAL;
1393 }
1394
1395 type = eeprom->magic >> 16;
1396
1397 if (type == 0xffff) { /* special value for directory operations */
1398 index = eeprom->magic & 0xff;
1399 dir_op = eeprom->magic >> 8;
1400 if (index == 0)
1401 return -EINVAL;
1402 switch (dir_op) {
1403 case 0x0e: /* erase */
1404 if (eeprom->offset != ~eeprom->magic)
1405 return -EINVAL;
1406 return bnxt_erase_nvram_directory(dev, index - 1);
1407 default:
1408 return -EINVAL;
1409 }
1410 }
1411
1412 /* Create or re-write an NVM item: */
1413 if (bnxt_dir_type_is_executable(type) == true)
1414 return -EINVAL;
1415 ext = eeprom->magic & 0xffff;
1416 ordinal = eeprom->offset >> 16;
1417 attr = eeprom->offset & 0xffff;
1418
1419 return bnxt_flash_nvram(dev, type, ordinal, ext, attr, data,
1420 eeprom->len);
1421}
1422
72b34f04
MC
1423static int bnxt_set_eee(struct net_device *dev, struct ethtool_eee *edata)
1424{
1425 struct bnxt *bp = netdev_priv(dev);
1426 struct ethtool_eee *eee = &bp->eee;
1427 struct bnxt_link_info *link_info = &bp->link_info;
1428 u32 advertising =
1429 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
1430 int rc = 0;
1431
1432 if (BNXT_VF(bp))
1433 return 0;
1434
1435 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1436 return -EOPNOTSUPP;
1437
1438 if (!edata->eee_enabled)
1439 goto eee_ok;
1440
1441 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
1442 netdev_warn(dev, "EEE requires autoneg\n");
1443 return -EINVAL;
1444 }
1445 if (edata->tx_lpi_enabled) {
1446 if (bp->lpi_tmr_hi && (edata->tx_lpi_timer > bp->lpi_tmr_hi ||
1447 edata->tx_lpi_timer < bp->lpi_tmr_lo)) {
1448 netdev_warn(dev, "Valid LPI timer range is %d and %d microsecs\n",
1449 bp->lpi_tmr_lo, bp->lpi_tmr_hi);
1450 return -EINVAL;
1451 } else if (!bp->lpi_tmr_hi) {
1452 edata->tx_lpi_timer = eee->tx_lpi_timer;
1453 }
1454 }
1455 if (!edata->advertised) {
1456 edata->advertised = advertising & eee->supported;
1457 } else if (edata->advertised & ~advertising) {
1458 netdev_warn(dev, "EEE advertised %x must be a subset of autoneg advertised speeds %x\n",
1459 edata->advertised, advertising);
1460 return -EINVAL;
1461 }
1462
1463 eee->advertised = edata->advertised;
1464 eee->tx_lpi_enabled = edata->tx_lpi_enabled;
1465 eee->tx_lpi_timer = edata->tx_lpi_timer;
1466eee_ok:
1467 eee->eee_enabled = edata->eee_enabled;
1468
1469 if (netif_running(dev))
1470 rc = bnxt_hwrm_set_link_setting(bp, false, true);
1471
1472 return rc;
1473}
1474
1475static int bnxt_get_eee(struct net_device *dev, struct ethtool_eee *edata)
1476{
1477 struct bnxt *bp = netdev_priv(dev);
1478
1479 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
1480 return -EOPNOTSUPP;
1481
1482 *edata = bp->eee;
1483 if (!bp->eee.eee_enabled) {
1484 /* Preserve tx_lpi_timer so that the last value will be used
1485 * by default when it is re-enabled.
1486 */
1487 edata->advertised = 0;
1488 edata->tx_lpi_enabled = 0;
1489 }
1490
1491 if (!bp->eee.eee_active)
1492 edata->lp_advertised = 0;
1493
1494 return 0;
1495}
1496
c0c050c5
MC
1497const struct ethtool_ops bnxt_ethtool_ops = {
1498 .get_settings = bnxt_get_settings,
1499 .set_settings = bnxt_set_settings,
1500 .get_pauseparam = bnxt_get_pauseparam,
1501 .set_pauseparam = bnxt_set_pauseparam,
1502 .get_drvinfo = bnxt_get_drvinfo,
1503 .get_coalesce = bnxt_get_coalesce,
1504 .set_coalesce = bnxt_set_coalesce,
1505 .get_msglevel = bnxt_get_msglevel,
1506 .set_msglevel = bnxt_set_msglevel,
1507 .get_sset_count = bnxt_get_sset_count,
1508 .get_strings = bnxt_get_strings,
1509 .get_ethtool_stats = bnxt_get_ethtool_stats,
1510 .set_ringparam = bnxt_set_ringparam,
1511 .get_ringparam = bnxt_get_ringparam,
1512 .get_channels = bnxt_get_channels,
1513 .set_channels = bnxt_set_channels,
1514#ifdef CONFIG_RFS_ACCEL
1515 .get_rxnfc = bnxt_get_rxnfc,
1516#endif
1517 .get_rxfh_indir_size = bnxt_get_rxfh_indir_size,
1518 .get_rxfh_key_size = bnxt_get_rxfh_key_size,
1519 .get_rxfh = bnxt_get_rxfh,
1520 .flash_device = bnxt_flash_device,
1521 .get_eeprom_len = bnxt_get_eeprom_len,
1522 .get_eeprom = bnxt_get_eeprom,
1523 .set_eeprom = bnxt_set_eeprom,
1524 .get_link = bnxt_get_link,
72b34f04
MC
1525 .get_eee = bnxt_get_eee,
1526 .set_eee = bnxt_set_eee,
c0c050c5 1527};