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