1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Linux network driver for QLogic BR-series Converged Network Adapter.
4  */
5 /*
6  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
7  * Copyright (c) 2014-2015 QLogic Corporation
8  * All rights reserved
9  * www.qlogic.com
10  */
11 
12 #include "cna.h"
13 
14 #include <linux/netdevice.h>
15 #include <linux/skbuff.h>
16 #include <linux/ethtool.h>
17 #include <linux/rtnetlink.h>
18 
19 #include "bna.h"
20 
21 #include "bnad.h"
22 
23 #define BNAD_NUM_TXF_COUNTERS 12
24 #define BNAD_NUM_RXF_COUNTERS 10
25 #define BNAD_NUM_CQ_COUNTERS (3 + 5)
26 #define BNAD_NUM_RXQ_COUNTERS 7
27 #define BNAD_NUM_TXQ_COUNTERS 5
28 
29 static const char *bnad_net_stats_strings[] = {
30 	"rx_packets",
31 	"tx_packets",
32 	"rx_bytes",
33 	"tx_bytes",
34 	"rx_errors",
35 	"tx_errors",
36 	"rx_dropped",
37 	"tx_dropped",
38 	"multicast",
39 	"collisions",
40 	"rx_length_errors",
41 	"rx_crc_errors",
42 	"rx_frame_errors",
43 	"tx_fifo_errors",
44 
45 	"netif_queue_stop",
46 	"netif_queue_wakeup",
47 	"netif_queue_stopped",
48 	"tso4",
49 	"tso6",
50 	"tso_err",
51 	"tcpcsum_offload",
52 	"udpcsum_offload",
53 	"csum_help",
54 	"tx_skb_too_short",
55 	"tx_skb_stopping",
56 	"tx_skb_max_vectors",
57 	"tx_skb_mss_too_long",
58 	"tx_skb_tso_too_short",
59 	"tx_skb_tso_prepare",
60 	"tx_skb_non_tso_too_long",
61 	"tx_skb_tcp_hdr",
62 	"tx_skb_udp_hdr",
63 	"tx_skb_csum_err",
64 	"tx_skb_headlen_too_long",
65 	"tx_skb_headlen_zero",
66 	"tx_skb_frag_zero",
67 	"tx_skb_len_mismatch",
68 	"tx_skb_map_failed",
69 	"hw_stats_updates",
70 	"netif_rx_dropped",
71 
72 	"link_toggle",
73 	"cee_toggle",
74 
75 	"rxp_info_alloc_failed",
76 	"mbox_intr_disabled",
77 	"mbox_intr_enabled",
78 	"tx_unmap_q_alloc_failed",
79 	"rx_unmap_q_alloc_failed",
80 	"rxbuf_alloc_failed",
81 	"rxbuf_map_failed",
82 
83 	"mac_stats_clr_cnt",
84 	"mac_frame_64",
85 	"mac_frame_65_127",
86 	"mac_frame_128_255",
87 	"mac_frame_256_511",
88 	"mac_frame_512_1023",
89 	"mac_frame_1024_1518",
90 	"mac_frame_1518_1522",
91 	"mac_rx_bytes",
92 	"mac_rx_packets",
93 	"mac_rx_fcs_error",
94 	"mac_rx_multicast",
95 	"mac_rx_broadcast",
96 	"mac_rx_control_frames",
97 	"mac_rx_pause",
98 	"mac_rx_unknown_opcode",
99 	"mac_rx_alignment_error",
100 	"mac_rx_frame_length_error",
101 	"mac_rx_code_error",
102 	"mac_rx_carrier_sense_error",
103 	"mac_rx_undersize",
104 	"mac_rx_oversize",
105 	"mac_rx_fragments",
106 	"mac_rx_jabber",
107 	"mac_rx_drop",
108 
109 	"mac_tx_bytes",
110 	"mac_tx_packets",
111 	"mac_tx_multicast",
112 	"mac_tx_broadcast",
113 	"mac_tx_pause",
114 	"mac_tx_deferral",
115 	"mac_tx_excessive_deferral",
116 	"mac_tx_single_collision",
117 	"mac_tx_muliple_collision",
118 	"mac_tx_late_collision",
119 	"mac_tx_excessive_collision",
120 	"mac_tx_total_collision",
121 	"mac_tx_pause_honored",
122 	"mac_tx_drop",
123 	"mac_tx_jabber",
124 	"mac_tx_fcs_error",
125 	"mac_tx_control_frame",
126 	"mac_tx_oversize",
127 	"mac_tx_undersize",
128 	"mac_tx_fragments",
129 
130 	"bpc_tx_pause_0",
131 	"bpc_tx_pause_1",
132 	"bpc_tx_pause_2",
133 	"bpc_tx_pause_3",
134 	"bpc_tx_pause_4",
135 	"bpc_tx_pause_5",
136 	"bpc_tx_pause_6",
137 	"bpc_tx_pause_7",
138 	"bpc_tx_zero_pause_0",
139 	"bpc_tx_zero_pause_1",
140 	"bpc_tx_zero_pause_2",
141 	"bpc_tx_zero_pause_3",
142 	"bpc_tx_zero_pause_4",
143 	"bpc_tx_zero_pause_5",
144 	"bpc_tx_zero_pause_6",
145 	"bpc_tx_zero_pause_7",
146 	"bpc_tx_first_pause_0",
147 	"bpc_tx_first_pause_1",
148 	"bpc_tx_first_pause_2",
149 	"bpc_tx_first_pause_3",
150 	"bpc_tx_first_pause_4",
151 	"bpc_tx_first_pause_5",
152 	"bpc_tx_first_pause_6",
153 	"bpc_tx_first_pause_7",
154 
155 	"bpc_rx_pause_0",
156 	"bpc_rx_pause_1",
157 	"bpc_rx_pause_2",
158 	"bpc_rx_pause_3",
159 	"bpc_rx_pause_4",
160 	"bpc_rx_pause_5",
161 	"bpc_rx_pause_6",
162 	"bpc_rx_pause_7",
163 	"bpc_rx_zero_pause_0",
164 	"bpc_rx_zero_pause_1",
165 	"bpc_rx_zero_pause_2",
166 	"bpc_rx_zero_pause_3",
167 	"bpc_rx_zero_pause_4",
168 	"bpc_rx_zero_pause_5",
169 	"bpc_rx_zero_pause_6",
170 	"bpc_rx_zero_pause_7",
171 	"bpc_rx_first_pause_0",
172 	"bpc_rx_first_pause_1",
173 	"bpc_rx_first_pause_2",
174 	"bpc_rx_first_pause_3",
175 	"bpc_rx_first_pause_4",
176 	"bpc_rx_first_pause_5",
177 	"bpc_rx_first_pause_6",
178 	"bpc_rx_first_pause_7",
179 
180 	"rad_rx_frames",
181 	"rad_rx_octets",
182 	"rad_rx_vlan_frames",
183 	"rad_rx_ucast",
184 	"rad_rx_ucast_octets",
185 	"rad_rx_ucast_vlan",
186 	"rad_rx_mcast",
187 	"rad_rx_mcast_octets",
188 	"rad_rx_mcast_vlan",
189 	"rad_rx_bcast",
190 	"rad_rx_bcast_octets",
191 	"rad_rx_bcast_vlan",
192 	"rad_rx_drops",
193 
194 	"rlb_rad_rx_frames",
195 	"rlb_rad_rx_octets",
196 	"rlb_rad_rx_vlan_frames",
197 	"rlb_rad_rx_ucast",
198 	"rlb_rad_rx_ucast_octets",
199 	"rlb_rad_rx_ucast_vlan",
200 	"rlb_rad_rx_mcast",
201 	"rlb_rad_rx_mcast_octets",
202 	"rlb_rad_rx_mcast_vlan",
203 	"rlb_rad_rx_bcast",
204 	"rlb_rad_rx_bcast_octets",
205 	"rlb_rad_rx_bcast_vlan",
206 	"rlb_rad_rx_drops",
207 
208 	"fc_rx_ucast_octets",
209 	"fc_rx_ucast",
210 	"fc_rx_ucast_vlan",
211 	"fc_rx_mcast_octets",
212 	"fc_rx_mcast",
213 	"fc_rx_mcast_vlan",
214 	"fc_rx_bcast_octets",
215 	"fc_rx_bcast",
216 	"fc_rx_bcast_vlan",
217 
218 	"fc_tx_ucast_octets",
219 	"fc_tx_ucast",
220 	"fc_tx_ucast_vlan",
221 	"fc_tx_mcast_octets",
222 	"fc_tx_mcast",
223 	"fc_tx_mcast_vlan",
224 	"fc_tx_bcast_octets",
225 	"fc_tx_bcast",
226 	"fc_tx_bcast_vlan",
227 	"fc_tx_parity_errors",
228 	"fc_tx_timeout",
229 	"fc_tx_fid_parity_errors",
230 };
231 
232 #define BNAD_ETHTOOL_STATS_NUM	ARRAY_SIZE(bnad_net_stats_strings)
233 
234 static int
235 bnad_get_link_ksettings(struct net_device *netdev,
236 			struct ethtool_link_ksettings *cmd)
237 {
238 	u32 supported, advertising;
239 
240 	supported = SUPPORTED_10000baseT_Full;
241 	advertising = ADVERTISED_10000baseT_Full;
242 	cmd->base.autoneg = AUTONEG_DISABLE;
243 	supported |= SUPPORTED_FIBRE;
244 	advertising |= ADVERTISED_FIBRE;
245 	cmd->base.port = PORT_FIBRE;
246 	cmd->base.phy_address = 0;
247 
248 	if (netif_carrier_ok(netdev)) {
249 		cmd->base.speed = SPEED_10000;
250 		cmd->base.duplex = DUPLEX_FULL;
251 	} else {
252 		cmd->base.speed = SPEED_UNKNOWN;
253 		cmd->base.duplex = DUPLEX_UNKNOWN;
254 	}
255 
256 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
257 						supported);
258 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
259 						advertising);
260 
261 	return 0;
262 }
263 
264 static int
265 bnad_set_link_ksettings(struct net_device *netdev,
266 			const struct ethtool_link_ksettings *cmd)
267 {
268 	/* 10G full duplex setting supported only */
269 	if (cmd->base.autoneg == AUTONEG_ENABLE)
270 		return -EOPNOTSUPP;
271 
272 	if ((cmd->base.speed == SPEED_10000) &&
273 	    (cmd->base.duplex == DUPLEX_FULL))
274 		return 0;
275 
276 	return -EOPNOTSUPP;
277 }
278 
279 static void
280 bnad_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
281 {
282 	struct bnad *bnad = netdev_priv(netdev);
283 	struct bfa_ioc_attr *ioc_attr;
284 	unsigned long flags;
285 
286 	strlcpy(drvinfo->driver, BNAD_NAME, sizeof(drvinfo->driver));
287 
288 	ioc_attr = kzalloc(sizeof(*ioc_attr), GFP_KERNEL);
289 	if (ioc_attr) {
290 		spin_lock_irqsave(&bnad->bna_lock, flags);
291 		bfa_nw_ioc_get_attr(&bnad->bna.ioceth.ioc, ioc_attr);
292 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
293 
294 		strlcpy(drvinfo->fw_version, ioc_attr->adapter_attr.fw_ver,
295 			sizeof(drvinfo->fw_version));
296 		kfree(ioc_attr);
297 	}
298 
299 	strlcpy(drvinfo->bus_info, pci_name(bnad->pcidev),
300 		sizeof(drvinfo->bus_info));
301 }
302 
303 static void
304 bnad_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wolinfo)
305 {
306 	wolinfo->supported = 0;
307 	wolinfo->wolopts = 0;
308 }
309 
310 static int
311 bnad_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
312 {
313 	struct bnad *bnad = netdev_priv(netdev);
314 	unsigned long flags;
315 
316 	/* Lock rqd. to access bnad->bna_lock */
317 	spin_lock_irqsave(&bnad->bna_lock, flags);
318 	coalesce->use_adaptive_rx_coalesce =
319 		(bnad->cfg_flags & BNAD_CF_DIM_ENABLED) ? true : false;
320 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
321 
322 	coalesce->rx_coalesce_usecs = bnad->rx_coalescing_timeo *
323 					BFI_COALESCING_TIMER_UNIT;
324 	coalesce->tx_coalesce_usecs = bnad->tx_coalescing_timeo *
325 					BFI_COALESCING_TIMER_UNIT;
326 	coalesce->tx_max_coalesced_frames = BFI_TX_INTERPKT_COUNT;
327 
328 	return 0;
329 }
330 
331 static int
332 bnad_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
333 {
334 	struct bnad *bnad = netdev_priv(netdev);
335 	unsigned long flags;
336 	int to_del = 0;
337 
338 	if (coalesce->rx_coalesce_usecs == 0 ||
339 	    coalesce->rx_coalesce_usecs >
340 	    BFI_MAX_COALESCING_TIMEO * BFI_COALESCING_TIMER_UNIT)
341 		return -EINVAL;
342 
343 	if (coalesce->tx_coalesce_usecs == 0 ||
344 	    coalesce->tx_coalesce_usecs >
345 	    BFI_MAX_COALESCING_TIMEO * BFI_COALESCING_TIMER_UNIT)
346 		return -EINVAL;
347 
348 	mutex_lock(&bnad->conf_mutex);
349 	/*
350 	 * Do not need to store rx_coalesce_usecs here
351 	 * Every time DIM is disabled, we can get it from the
352 	 * stack.
353 	 */
354 	spin_lock_irqsave(&bnad->bna_lock, flags);
355 	if (coalesce->use_adaptive_rx_coalesce) {
356 		if (!(bnad->cfg_flags & BNAD_CF_DIM_ENABLED)) {
357 			bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
358 			bnad_dim_timer_start(bnad);
359 		}
360 	} else {
361 		if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED) {
362 			bnad->cfg_flags &= ~BNAD_CF_DIM_ENABLED;
363 			if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
364 			    test_bit(BNAD_RF_DIM_TIMER_RUNNING,
365 			    &bnad->run_flags)) {
366 				clear_bit(BNAD_RF_DIM_TIMER_RUNNING,
367 							&bnad->run_flags);
368 				to_del = 1;
369 			}
370 			spin_unlock_irqrestore(&bnad->bna_lock, flags);
371 			if (to_del)
372 				del_timer_sync(&bnad->dim_timer);
373 			spin_lock_irqsave(&bnad->bna_lock, flags);
374 			bnad_rx_coalescing_timeo_set(bnad);
375 		}
376 	}
377 	if (bnad->tx_coalescing_timeo != coalesce->tx_coalesce_usecs /
378 					BFI_COALESCING_TIMER_UNIT) {
379 		bnad->tx_coalescing_timeo = coalesce->tx_coalesce_usecs /
380 						BFI_COALESCING_TIMER_UNIT;
381 		bnad_tx_coalescing_timeo_set(bnad);
382 	}
383 
384 	if (bnad->rx_coalescing_timeo != coalesce->rx_coalesce_usecs /
385 					BFI_COALESCING_TIMER_UNIT) {
386 		bnad->rx_coalescing_timeo = coalesce->rx_coalesce_usecs /
387 						BFI_COALESCING_TIMER_UNIT;
388 
389 		if (!(bnad->cfg_flags & BNAD_CF_DIM_ENABLED))
390 			bnad_rx_coalescing_timeo_set(bnad);
391 
392 	}
393 
394 	/* Add Tx Inter-pkt DMA count?  */
395 
396 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
397 
398 	mutex_unlock(&bnad->conf_mutex);
399 	return 0;
400 }
401 
402 static void
403 bnad_get_ringparam(struct net_device *netdev,
404 		   struct ethtool_ringparam *ringparam)
405 {
406 	struct bnad *bnad = netdev_priv(netdev);
407 
408 	ringparam->rx_max_pending = BNAD_MAX_RXQ_DEPTH;
409 	ringparam->tx_max_pending = BNAD_MAX_TXQ_DEPTH;
410 
411 	ringparam->rx_pending = bnad->rxq_depth;
412 	ringparam->tx_pending = bnad->txq_depth;
413 }
414 
415 static int
416 bnad_set_ringparam(struct net_device *netdev,
417 		   struct ethtool_ringparam *ringparam)
418 {
419 	int i, current_err, err = 0;
420 	struct bnad *bnad = netdev_priv(netdev);
421 	unsigned long flags;
422 
423 	mutex_lock(&bnad->conf_mutex);
424 	if (ringparam->rx_pending == bnad->rxq_depth &&
425 	    ringparam->tx_pending == bnad->txq_depth) {
426 		mutex_unlock(&bnad->conf_mutex);
427 		return 0;
428 	}
429 
430 	if (ringparam->rx_pending < BNAD_MIN_Q_DEPTH ||
431 	    ringparam->rx_pending > BNAD_MAX_RXQ_DEPTH ||
432 	    !is_power_of_2(ringparam->rx_pending)) {
433 		mutex_unlock(&bnad->conf_mutex);
434 		return -EINVAL;
435 	}
436 	if (ringparam->tx_pending < BNAD_MIN_Q_DEPTH ||
437 	    ringparam->tx_pending > BNAD_MAX_TXQ_DEPTH ||
438 	    !is_power_of_2(ringparam->tx_pending)) {
439 		mutex_unlock(&bnad->conf_mutex);
440 		return -EINVAL;
441 	}
442 
443 	if (ringparam->rx_pending != bnad->rxq_depth) {
444 		bnad->rxq_depth = ringparam->rx_pending;
445 		if (!netif_running(netdev)) {
446 			mutex_unlock(&bnad->conf_mutex);
447 			return 0;
448 		}
449 
450 		for (i = 0; i < bnad->num_rx; i++) {
451 			if (!bnad->rx_info[i].rx)
452 				continue;
453 			bnad_destroy_rx(bnad, i);
454 			current_err = bnad_setup_rx(bnad, i);
455 			if (current_err && !err)
456 				err = current_err;
457 		}
458 
459 		if (!err && bnad->rx_info[0].rx) {
460 			/* restore rx configuration */
461 			bnad_restore_vlans(bnad, 0);
462 			bnad_enable_default_bcast(bnad);
463 			spin_lock_irqsave(&bnad->bna_lock, flags);
464 			bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
465 			spin_unlock_irqrestore(&bnad->bna_lock, flags);
466 			bnad->cfg_flags &= ~(BNAD_CF_ALLMULTI |
467 					     BNAD_CF_PROMISC);
468 			bnad_set_rx_mode(netdev);
469 		}
470 	}
471 	if (ringparam->tx_pending != bnad->txq_depth) {
472 		bnad->txq_depth = ringparam->tx_pending;
473 		if (!netif_running(netdev)) {
474 			mutex_unlock(&bnad->conf_mutex);
475 			return 0;
476 		}
477 
478 		for (i = 0; i < bnad->num_tx; i++) {
479 			if (!bnad->tx_info[i].tx)
480 				continue;
481 			bnad_destroy_tx(bnad, i);
482 			current_err = bnad_setup_tx(bnad, i);
483 			if (current_err && !err)
484 				err = current_err;
485 		}
486 	}
487 
488 	mutex_unlock(&bnad->conf_mutex);
489 	return err;
490 }
491 
492 static void
493 bnad_get_pauseparam(struct net_device *netdev,
494 		    struct ethtool_pauseparam *pauseparam)
495 {
496 	struct bnad *bnad = netdev_priv(netdev);
497 
498 	pauseparam->autoneg = 0;
499 	pauseparam->rx_pause = bnad->bna.enet.pause_config.rx_pause;
500 	pauseparam->tx_pause = bnad->bna.enet.pause_config.tx_pause;
501 }
502 
503 static int
504 bnad_set_pauseparam(struct net_device *netdev,
505 		    struct ethtool_pauseparam *pauseparam)
506 {
507 	struct bnad *bnad = netdev_priv(netdev);
508 	struct bna_pause_config pause_config;
509 	unsigned long flags;
510 
511 	if (pauseparam->autoneg == AUTONEG_ENABLE)
512 		return -EINVAL;
513 
514 	mutex_lock(&bnad->conf_mutex);
515 	if (pauseparam->rx_pause != bnad->bna.enet.pause_config.rx_pause ||
516 	    pauseparam->tx_pause != bnad->bna.enet.pause_config.tx_pause) {
517 		pause_config.rx_pause = pauseparam->rx_pause;
518 		pause_config.tx_pause = pauseparam->tx_pause;
519 		spin_lock_irqsave(&bnad->bna_lock, flags);
520 		bna_enet_pause_config(&bnad->bna.enet, &pause_config);
521 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
522 	}
523 	mutex_unlock(&bnad->conf_mutex);
524 	return 0;
525 }
526 
527 static void
528 bnad_get_strings(struct net_device *netdev, u32 stringset, u8 *string)
529 {
530 	struct bnad *bnad = netdev_priv(netdev);
531 	int i, j, q_num;
532 	u32 bmap;
533 
534 	mutex_lock(&bnad->conf_mutex);
535 
536 	switch (stringset) {
537 	case ETH_SS_STATS:
538 		for (i = 0; i < BNAD_ETHTOOL_STATS_NUM; i++) {
539 			BUG_ON(!(strlen(bnad_net_stats_strings[i]) <
540 				   ETH_GSTRING_LEN));
541 			strncpy(string, bnad_net_stats_strings[i],
542 				ETH_GSTRING_LEN);
543 			string += ETH_GSTRING_LEN;
544 		}
545 		bmap = bna_tx_rid_mask(&bnad->bna);
546 		for (i = 0; bmap; i++) {
547 			if (bmap & 1) {
548 				sprintf(string, "txf%d_ucast_octets", i);
549 				string += ETH_GSTRING_LEN;
550 				sprintf(string, "txf%d_ucast", i);
551 				string += ETH_GSTRING_LEN;
552 				sprintf(string, "txf%d_ucast_vlan", i);
553 				string += ETH_GSTRING_LEN;
554 				sprintf(string, "txf%d_mcast_octets", i);
555 				string += ETH_GSTRING_LEN;
556 				sprintf(string, "txf%d_mcast", i);
557 				string += ETH_GSTRING_LEN;
558 				sprintf(string, "txf%d_mcast_vlan", i);
559 				string += ETH_GSTRING_LEN;
560 				sprintf(string, "txf%d_bcast_octets", i);
561 				string += ETH_GSTRING_LEN;
562 				sprintf(string, "txf%d_bcast", i);
563 				string += ETH_GSTRING_LEN;
564 				sprintf(string, "txf%d_bcast_vlan", i);
565 				string += ETH_GSTRING_LEN;
566 				sprintf(string, "txf%d_errors", i);
567 				string += ETH_GSTRING_LEN;
568 				sprintf(string, "txf%d_filter_vlan", i);
569 				string += ETH_GSTRING_LEN;
570 				sprintf(string, "txf%d_filter_mac_sa", i);
571 				string += ETH_GSTRING_LEN;
572 			}
573 			bmap >>= 1;
574 		}
575 
576 		bmap = bna_rx_rid_mask(&bnad->bna);
577 		for (i = 0; bmap; i++) {
578 			if (bmap & 1) {
579 				sprintf(string, "rxf%d_ucast_octets", i);
580 				string += ETH_GSTRING_LEN;
581 				sprintf(string, "rxf%d_ucast", i);
582 				string += ETH_GSTRING_LEN;
583 				sprintf(string, "rxf%d_ucast_vlan", i);
584 				string += ETH_GSTRING_LEN;
585 				sprintf(string, "rxf%d_mcast_octets", i);
586 				string += ETH_GSTRING_LEN;
587 				sprintf(string, "rxf%d_mcast", i);
588 				string += ETH_GSTRING_LEN;
589 				sprintf(string, "rxf%d_mcast_vlan", i);
590 				string += ETH_GSTRING_LEN;
591 				sprintf(string, "rxf%d_bcast_octets", i);
592 				string += ETH_GSTRING_LEN;
593 				sprintf(string, "rxf%d_bcast", i);
594 				string += ETH_GSTRING_LEN;
595 				sprintf(string, "rxf%d_bcast_vlan", i);
596 				string += ETH_GSTRING_LEN;
597 				sprintf(string, "rxf%d_frame_drops", i);
598 				string += ETH_GSTRING_LEN;
599 			}
600 			bmap >>= 1;
601 		}
602 
603 		q_num = 0;
604 		for (i = 0; i < bnad->num_rx; i++) {
605 			if (!bnad->rx_info[i].rx)
606 				continue;
607 			for (j = 0; j < bnad->num_rxp_per_rx; j++) {
608 				sprintf(string, "cq%d_producer_index", q_num);
609 				string += ETH_GSTRING_LEN;
610 				sprintf(string, "cq%d_consumer_index", q_num);
611 				string += ETH_GSTRING_LEN;
612 				sprintf(string, "cq%d_hw_producer_index",
613 					q_num);
614 				string += ETH_GSTRING_LEN;
615 				sprintf(string, "cq%d_intr", q_num);
616 				string += ETH_GSTRING_LEN;
617 				sprintf(string, "cq%d_poll", q_num);
618 				string += ETH_GSTRING_LEN;
619 				sprintf(string, "cq%d_schedule", q_num);
620 				string += ETH_GSTRING_LEN;
621 				sprintf(string, "cq%d_keep_poll", q_num);
622 				string += ETH_GSTRING_LEN;
623 				sprintf(string, "cq%d_complete", q_num);
624 				string += ETH_GSTRING_LEN;
625 				q_num++;
626 			}
627 		}
628 
629 		q_num = 0;
630 		for (i = 0; i < bnad->num_rx; i++) {
631 			if (!bnad->rx_info[i].rx)
632 				continue;
633 			for (j = 0; j < bnad->num_rxp_per_rx; j++) {
634 				sprintf(string, "rxq%d_packets", q_num);
635 				string += ETH_GSTRING_LEN;
636 				sprintf(string, "rxq%d_bytes", q_num);
637 				string += ETH_GSTRING_LEN;
638 				sprintf(string, "rxq%d_packets_with_error",
639 								q_num);
640 				string += ETH_GSTRING_LEN;
641 				sprintf(string, "rxq%d_allocbuf_failed", q_num);
642 				string += ETH_GSTRING_LEN;
643 				sprintf(string, "rxq%d_mapbuf_failed", q_num);
644 				string += ETH_GSTRING_LEN;
645 				sprintf(string, "rxq%d_producer_index", q_num);
646 				string += ETH_GSTRING_LEN;
647 				sprintf(string, "rxq%d_consumer_index", q_num);
648 				string += ETH_GSTRING_LEN;
649 				q_num++;
650 				if (bnad->rx_info[i].rx_ctrl[j].ccb &&
651 					bnad->rx_info[i].rx_ctrl[j].ccb->
652 					rcb[1] &&
653 					bnad->rx_info[i].rx_ctrl[j].ccb->
654 					rcb[1]->rxq) {
655 					sprintf(string, "rxq%d_packets", q_num);
656 					string += ETH_GSTRING_LEN;
657 					sprintf(string, "rxq%d_bytes", q_num);
658 					string += ETH_GSTRING_LEN;
659 					sprintf(string,
660 					"rxq%d_packets_with_error", q_num);
661 					string += ETH_GSTRING_LEN;
662 					sprintf(string, "rxq%d_allocbuf_failed",
663 								q_num);
664 					string += ETH_GSTRING_LEN;
665 					sprintf(string, "rxq%d_mapbuf_failed",
666 						q_num);
667 					string += ETH_GSTRING_LEN;
668 					sprintf(string, "rxq%d_producer_index",
669 								q_num);
670 					string += ETH_GSTRING_LEN;
671 					sprintf(string, "rxq%d_consumer_index",
672 								q_num);
673 					string += ETH_GSTRING_LEN;
674 					q_num++;
675 				}
676 			}
677 		}
678 
679 		q_num = 0;
680 		for (i = 0; i < bnad->num_tx; i++) {
681 			if (!bnad->tx_info[i].tx)
682 				continue;
683 			for (j = 0; j < bnad->num_txq_per_tx; j++) {
684 				sprintf(string, "txq%d_packets", q_num);
685 				string += ETH_GSTRING_LEN;
686 				sprintf(string, "txq%d_bytes", q_num);
687 				string += ETH_GSTRING_LEN;
688 				sprintf(string, "txq%d_producer_index", q_num);
689 				string += ETH_GSTRING_LEN;
690 				sprintf(string, "txq%d_consumer_index", q_num);
691 				string += ETH_GSTRING_LEN;
692 				sprintf(string, "txq%d_hw_consumer_index",
693 									q_num);
694 				string += ETH_GSTRING_LEN;
695 				q_num++;
696 			}
697 		}
698 
699 		break;
700 
701 	default:
702 		break;
703 	}
704 
705 	mutex_unlock(&bnad->conf_mutex);
706 }
707 
708 static int
709 bnad_get_stats_count_locked(struct net_device *netdev)
710 {
711 	struct bnad *bnad = netdev_priv(netdev);
712 	int i, j, count = 0, rxf_active_num = 0, txf_active_num = 0;
713 	u32 bmap;
714 
715 	bmap = bna_tx_rid_mask(&bnad->bna);
716 	for (i = 0; bmap; i++) {
717 		if (bmap & 1)
718 			txf_active_num++;
719 		bmap >>= 1;
720 	}
721 	bmap = bna_rx_rid_mask(&bnad->bna);
722 	for (i = 0; bmap; i++) {
723 		if (bmap & 1)
724 			rxf_active_num++;
725 		bmap >>= 1;
726 	}
727 	count = BNAD_ETHTOOL_STATS_NUM +
728 		txf_active_num * BNAD_NUM_TXF_COUNTERS +
729 		rxf_active_num * BNAD_NUM_RXF_COUNTERS;
730 
731 	for (i = 0; i < bnad->num_rx; i++) {
732 		if (!bnad->rx_info[i].rx)
733 			continue;
734 		count += bnad->num_rxp_per_rx * BNAD_NUM_CQ_COUNTERS;
735 		count += bnad->num_rxp_per_rx * BNAD_NUM_RXQ_COUNTERS;
736 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
737 			if (bnad->rx_info[i].rx_ctrl[j].ccb &&
738 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
739 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1]->rxq)
740 				count +=  BNAD_NUM_RXQ_COUNTERS;
741 	}
742 
743 	for (i = 0; i < bnad->num_tx; i++) {
744 		if (!bnad->tx_info[i].tx)
745 			continue;
746 		count += bnad->num_txq_per_tx * BNAD_NUM_TXQ_COUNTERS;
747 	}
748 	return count;
749 }
750 
751 static int
752 bnad_per_q_stats_fill(struct bnad *bnad, u64 *buf, int bi)
753 {
754 	int i, j;
755 	struct bna_rcb *rcb = NULL;
756 	struct bna_tcb *tcb = NULL;
757 
758 	for (i = 0; i < bnad->num_rx; i++) {
759 		if (!bnad->rx_info[i].rx)
760 			continue;
761 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
762 			if (bnad->rx_info[i].rx_ctrl[j].ccb &&
763 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[0] &&
764 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[0]->rxq) {
765 				buf[bi++] = bnad->rx_info[i].rx_ctrl[j].
766 						ccb->producer_index;
767 				buf[bi++] = 0; /* ccb->consumer_index */
768 				buf[bi++] = *(bnad->rx_info[i].rx_ctrl[j].
769 						ccb->hw_producer_index);
770 
771 				buf[bi++] = bnad->rx_info[i].
772 						rx_ctrl[j].rx_intr_ctr;
773 				buf[bi++] = bnad->rx_info[i].
774 						rx_ctrl[j].rx_poll_ctr;
775 				buf[bi++] = bnad->rx_info[i].
776 						rx_ctrl[j].rx_schedule;
777 				buf[bi++] = bnad->rx_info[i].
778 						rx_ctrl[j].rx_keep_poll;
779 				buf[bi++] = bnad->rx_info[i].
780 						rx_ctrl[j].rx_complete;
781 			}
782 	}
783 	for (i = 0; i < bnad->num_rx; i++) {
784 		if (!bnad->rx_info[i].rx)
785 			continue;
786 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
787 			if (bnad->rx_info[i].rx_ctrl[j].ccb) {
788 				if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[0] &&
789 					bnad->rx_info[i].rx_ctrl[j].ccb->
790 					rcb[0]->rxq) {
791 					rcb = bnad->rx_info[i].rx_ctrl[j].
792 							ccb->rcb[0];
793 					buf[bi++] = rcb->rxq->rx_packets;
794 					buf[bi++] = rcb->rxq->rx_bytes;
795 					buf[bi++] = rcb->rxq->
796 							rx_packets_with_error;
797 					buf[bi++] = rcb->rxq->
798 							rxbuf_alloc_failed;
799 					buf[bi++] = rcb->rxq->rxbuf_map_failed;
800 					buf[bi++] = rcb->producer_index;
801 					buf[bi++] = rcb->consumer_index;
802 				}
803 				if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
804 					bnad->rx_info[i].rx_ctrl[j].ccb->
805 					rcb[1]->rxq) {
806 					rcb = bnad->rx_info[i].rx_ctrl[j].
807 								ccb->rcb[1];
808 					buf[bi++] = rcb->rxq->rx_packets;
809 					buf[bi++] = rcb->rxq->rx_bytes;
810 					buf[bi++] = rcb->rxq->
811 							rx_packets_with_error;
812 					buf[bi++] = rcb->rxq->
813 							rxbuf_alloc_failed;
814 					buf[bi++] = rcb->rxq->rxbuf_map_failed;
815 					buf[bi++] = rcb->producer_index;
816 					buf[bi++] = rcb->consumer_index;
817 				}
818 			}
819 	}
820 
821 	for (i = 0; i < bnad->num_tx; i++) {
822 		if (!bnad->tx_info[i].tx)
823 			continue;
824 		for (j = 0; j < bnad->num_txq_per_tx; j++)
825 			if (bnad->tx_info[i].tcb[j] &&
826 				bnad->tx_info[i].tcb[j]->txq) {
827 				tcb = bnad->tx_info[i].tcb[j];
828 				buf[bi++] = tcb->txq->tx_packets;
829 				buf[bi++] = tcb->txq->tx_bytes;
830 				buf[bi++] = tcb->producer_index;
831 				buf[bi++] = tcb->consumer_index;
832 				buf[bi++] = *(tcb->hw_consumer_index);
833 			}
834 	}
835 
836 	return bi;
837 }
838 
839 static void
840 bnad_get_ethtool_stats(struct net_device *netdev, struct ethtool_stats *stats,
841 		       u64 *buf)
842 {
843 	struct bnad *bnad = netdev_priv(netdev);
844 	int i, j, bi = 0;
845 	unsigned long flags;
846 	struct rtnl_link_stats64 net_stats64;
847 	u64 *stats64;
848 	u32 bmap;
849 
850 	mutex_lock(&bnad->conf_mutex);
851 	if (bnad_get_stats_count_locked(netdev) != stats->n_stats) {
852 		mutex_unlock(&bnad->conf_mutex);
853 		return;
854 	}
855 
856 	/*
857 	 * Used bna_lock to sync reads from bna_stats, which is written
858 	 * under the same lock
859 	 */
860 	spin_lock_irqsave(&bnad->bna_lock, flags);
861 
862 	memset(&net_stats64, 0, sizeof(net_stats64));
863 	bnad_netdev_qstats_fill(bnad, &net_stats64);
864 	bnad_netdev_hwstats_fill(bnad, &net_stats64);
865 
866 	buf[bi++] = net_stats64.rx_packets;
867 	buf[bi++] = net_stats64.tx_packets;
868 	buf[bi++] = net_stats64.rx_bytes;
869 	buf[bi++] = net_stats64.tx_bytes;
870 	buf[bi++] = net_stats64.rx_errors;
871 	buf[bi++] = net_stats64.tx_errors;
872 	buf[bi++] = net_stats64.rx_dropped;
873 	buf[bi++] = net_stats64.tx_dropped;
874 	buf[bi++] = net_stats64.multicast;
875 	buf[bi++] = net_stats64.collisions;
876 	buf[bi++] = net_stats64.rx_length_errors;
877 	buf[bi++] = net_stats64.rx_crc_errors;
878 	buf[bi++] = net_stats64.rx_frame_errors;
879 	buf[bi++] = net_stats64.tx_fifo_errors;
880 
881 	/* Get netif_queue_stopped from stack */
882 	bnad->stats.drv_stats.netif_queue_stopped = netif_queue_stopped(netdev);
883 
884 	/* Fill driver stats into ethtool buffers */
885 	stats64 = (u64 *)&bnad->stats.drv_stats;
886 	for (i = 0; i < sizeof(struct bnad_drv_stats) / sizeof(u64); i++)
887 		buf[bi++] = stats64[i];
888 
889 	/* Fill hardware stats excluding the rxf/txf into ethtool bufs */
890 	stats64 = (u64 *) &bnad->stats.bna_stats->hw_stats;
891 	for (i = 0;
892 	     i < offsetof(struct bfi_enet_stats, rxf_stats[0]) /
893 		sizeof(u64);
894 	     i++)
895 		buf[bi++] = stats64[i];
896 
897 	/* Fill txf stats into ethtool buffers */
898 	bmap = bna_tx_rid_mask(&bnad->bna);
899 	for (i = 0; bmap; i++) {
900 		if (bmap & 1) {
901 			stats64 = (u64 *)&bnad->stats.bna_stats->
902 						hw_stats.txf_stats[i];
903 			for (j = 0; j < sizeof(struct bfi_enet_stats_txf) /
904 					sizeof(u64); j++)
905 				buf[bi++] = stats64[j];
906 		}
907 		bmap >>= 1;
908 	}
909 
910 	/*  Fill rxf stats into ethtool buffers */
911 	bmap = bna_rx_rid_mask(&bnad->bna);
912 	for (i = 0; bmap; i++) {
913 		if (bmap & 1) {
914 			stats64 = (u64 *)&bnad->stats.bna_stats->
915 						hw_stats.rxf_stats[i];
916 			for (j = 0; j < sizeof(struct bfi_enet_stats_rxf) /
917 					sizeof(u64); j++)
918 				buf[bi++] = stats64[j];
919 		}
920 		bmap >>= 1;
921 	}
922 
923 	/* Fill per Q stats into ethtool buffers */
924 	bi = bnad_per_q_stats_fill(bnad, buf, bi);
925 
926 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
927 
928 	mutex_unlock(&bnad->conf_mutex);
929 }
930 
931 static int
932 bnad_get_sset_count(struct net_device *netdev, int sset)
933 {
934 	switch (sset) {
935 	case ETH_SS_STATS:
936 		return bnad_get_stats_count_locked(netdev);
937 	default:
938 		return -EOPNOTSUPP;
939 	}
940 }
941 
942 static u32
943 bnad_get_flash_partition_by_offset(struct bnad *bnad, u32 offset,
944 				u32 *base_offset)
945 {
946 	struct bfa_flash_attr *flash_attr;
947 	struct bnad_iocmd_comp fcomp;
948 	u32 i, flash_part = 0, ret;
949 	unsigned long flags = 0;
950 
951 	flash_attr = kzalloc(sizeof(struct bfa_flash_attr), GFP_KERNEL);
952 	if (!flash_attr)
953 		return 0;
954 
955 	fcomp.bnad = bnad;
956 	fcomp.comp_status = 0;
957 
958 	init_completion(&fcomp.comp);
959 	spin_lock_irqsave(&bnad->bna_lock, flags);
960 	ret = bfa_nw_flash_get_attr(&bnad->bna.flash, flash_attr,
961 				bnad_cb_completion, &fcomp);
962 	if (ret != BFA_STATUS_OK) {
963 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
964 		kfree(flash_attr);
965 		return 0;
966 	}
967 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
968 	wait_for_completion(&fcomp.comp);
969 	ret = fcomp.comp_status;
970 
971 	/* Check for the flash type & base offset value */
972 	if (ret == BFA_STATUS_OK) {
973 		for (i = 0; i < flash_attr->npart; i++) {
974 			if (offset >= flash_attr->part[i].part_off &&
975 			    offset < (flash_attr->part[i].part_off +
976 				      flash_attr->part[i].part_size)) {
977 				flash_part = flash_attr->part[i].part_type;
978 				*base_offset = flash_attr->part[i].part_off;
979 				break;
980 			}
981 		}
982 	}
983 	kfree(flash_attr);
984 	return flash_part;
985 }
986 
987 static int
988 bnad_get_eeprom_len(struct net_device *netdev)
989 {
990 	return BFA_TOTAL_FLASH_SIZE;
991 }
992 
993 static int
994 bnad_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
995 		u8 *bytes)
996 {
997 	struct bnad *bnad = netdev_priv(netdev);
998 	struct bnad_iocmd_comp fcomp;
999 	u32 flash_part = 0, base_offset = 0;
1000 	unsigned long flags = 0;
1001 	int ret = 0;
1002 
1003 	/* Fill the magic value */
1004 	eeprom->magic = bnad->pcidev->vendor | (bnad->pcidev->device << 16);
1005 
1006 	/* Query the flash partition based on the offset */
1007 	flash_part = bnad_get_flash_partition_by_offset(bnad,
1008 				eeprom->offset, &base_offset);
1009 	if (flash_part == 0)
1010 		return -EFAULT;
1011 
1012 	fcomp.bnad = bnad;
1013 	fcomp.comp_status = 0;
1014 
1015 	init_completion(&fcomp.comp);
1016 	spin_lock_irqsave(&bnad->bna_lock, flags);
1017 	ret = bfa_nw_flash_read_part(&bnad->bna.flash, flash_part,
1018 				bnad->id, bytes, eeprom->len,
1019 				eeprom->offset - base_offset,
1020 				bnad_cb_completion, &fcomp);
1021 	if (ret != BFA_STATUS_OK) {
1022 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
1023 		goto done;
1024 	}
1025 
1026 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
1027 	wait_for_completion(&fcomp.comp);
1028 	ret = fcomp.comp_status;
1029 done:
1030 	return ret;
1031 }
1032 
1033 static int
1034 bnad_set_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
1035 		u8 *bytes)
1036 {
1037 	struct bnad *bnad = netdev_priv(netdev);
1038 	struct bnad_iocmd_comp fcomp;
1039 	u32 flash_part = 0, base_offset = 0;
1040 	unsigned long flags = 0;
1041 	int ret = 0;
1042 
1043 	/* Check if the flash update request is valid */
1044 	if (eeprom->magic != (bnad->pcidev->vendor |
1045 			     (bnad->pcidev->device << 16)))
1046 		return -EINVAL;
1047 
1048 	/* Query the flash partition based on the offset */
1049 	flash_part = bnad_get_flash_partition_by_offset(bnad,
1050 				eeprom->offset, &base_offset);
1051 	if (flash_part == 0)
1052 		return -EFAULT;
1053 
1054 	fcomp.bnad = bnad;
1055 	fcomp.comp_status = 0;
1056 
1057 	init_completion(&fcomp.comp);
1058 	spin_lock_irqsave(&bnad->bna_lock, flags);
1059 	ret = bfa_nw_flash_update_part(&bnad->bna.flash, flash_part,
1060 				bnad->id, bytes, eeprom->len,
1061 				eeprom->offset - base_offset,
1062 				bnad_cb_completion, &fcomp);
1063 	if (ret != BFA_STATUS_OK) {
1064 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
1065 		goto done;
1066 	}
1067 
1068 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
1069 	wait_for_completion(&fcomp.comp);
1070 	ret = fcomp.comp_status;
1071 done:
1072 	return ret;
1073 }
1074 
1075 static int
1076 bnad_flash_device(struct net_device *netdev, struct ethtool_flash *eflash)
1077 {
1078 	struct bnad *bnad = netdev_priv(netdev);
1079 	struct bnad_iocmd_comp fcomp;
1080 	const struct firmware *fw;
1081 	int ret = 0;
1082 
1083 	ret = request_firmware(&fw, eflash->data, &bnad->pcidev->dev);
1084 	if (ret) {
1085 		netdev_err(netdev, "can't load firmware %s\n", eflash->data);
1086 		goto out;
1087 	}
1088 
1089 	fcomp.bnad = bnad;
1090 	fcomp.comp_status = 0;
1091 
1092 	init_completion(&fcomp.comp);
1093 	spin_lock_irq(&bnad->bna_lock);
1094 	ret = bfa_nw_flash_update_part(&bnad->bna.flash, BFA_FLASH_PART_FWIMG,
1095 				bnad->id, (u8 *)fw->data, fw->size, 0,
1096 				bnad_cb_completion, &fcomp);
1097 	if (ret != BFA_STATUS_OK) {
1098 		netdev_warn(netdev, "flash update failed with err=%d\n", ret);
1099 		ret = -EIO;
1100 		spin_unlock_irq(&bnad->bna_lock);
1101 		goto out;
1102 	}
1103 
1104 	spin_unlock_irq(&bnad->bna_lock);
1105 	wait_for_completion(&fcomp.comp);
1106 	if (fcomp.comp_status != BFA_STATUS_OK) {
1107 		ret = -EIO;
1108 		netdev_warn(netdev,
1109 			    "firmware image update failed with err=%d\n",
1110 			    fcomp.comp_status);
1111 	}
1112 out:
1113 	release_firmware(fw);
1114 	return ret;
1115 }
1116 
1117 static const struct ethtool_ops bnad_ethtool_ops = {
1118 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1119 				     ETHTOOL_COALESCE_TX_MAX_FRAMES |
1120 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1121 	.get_drvinfo = bnad_get_drvinfo,
1122 	.get_wol = bnad_get_wol,
1123 	.get_link = ethtool_op_get_link,
1124 	.get_coalesce = bnad_get_coalesce,
1125 	.set_coalesce = bnad_set_coalesce,
1126 	.get_ringparam = bnad_get_ringparam,
1127 	.set_ringparam = bnad_set_ringparam,
1128 	.get_pauseparam = bnad_get_pauseparam,
1129 	.set_pauseparam = bnad_set_pauseparam,
1130 	.get_strings = bnad_get_strings,
1131 	.get_ethtool_stats = bnad_get_ethtool_stats,
1132 	.get_sset_count = bnad_get_sset_count,
1133 	.get_eeprom_len = bnad_get_eeprom_len,
1134 	.get_eeprom = bnad_get_eeprom,
1135 	.set_eeprom = bnad_set_eeprom,
1136 	.flash_device = bnad_flash_device,
1137 	.get_ts_info = ethtool_op_get_ts_info,
1138 	.get_link_ksettings = bnad_get_link_ksettings,
1139 	.set_link_ksettings = bnad_set_link_ksettings,
1140 };
1141 
1142 void
1143 bnad_set_ethtool_ops(struct net_device *netdev)
1144 {
1145 	netdev->ethtool_ops = &bnad_ethtool_ops;
1146 }
1147