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 bnad_get_txf_strings(u8 **string, int f_num)
528 {
529 	ethtool_sprintf(string, "txf%d_ucast_octets", f_num);
530 	ethtool_sprintf(string, "txf%d_ucast", f_num);
531 	ethtool_sprintf(string, "txf%d_ucast_vlan", f_num);
532 	ethtool_sprintf(string, "txf%d_mcast_octets", f_num);
533 	ethtool_sprintf(string, "txf%d_mcast", f_num);
534 	ethtool_sprintf(string, "txf%d_mcast_vlan", f_num);
535 	ethtool_sprintf(string, "txf%d_bcast_octets", f_num);
536 	ethtool_sprintf(string, "txf%d_bcast", f_num);
537 	ethtool_sprintf(string, "txf%d_bcast_vlan", f_num);
538 	ethtool_sprintf(string, "txf%d_errors", f_num);
539 	ethtool_sprintf(string, "txf%d_filter_vlan", f_num);
540 	ethtool_sprintf(string, "txf%d_filter_mac_sa", f_num);
541 }
542 
543 static void bnad_get_rxf_strings(u8 **string, int f_num)
544 {
545 	ethtool_sprintf(string, "rxf%d_ucast_octets", f_num);
546 	ethtool_sprintf(string, "rxf%d_ucast", f_num);
547 	ethtool_sprintf(string, "rxf%d_ucast_vlan", f_num);
548 	ethtool_sprintf(string, "rxf%d_mcast_octets", f_num);
549 	ethtool_sprintf(string, "rxf%d_mcast", f_num);
550 	ethtool_sprintf(string, "rxf%d_mcast_vlan", f_num);
551 	ethtool_sprintf(string, "rxf%d_bcast_octets", f_num);
552 	ethtool_sprintf(string, "rxf%d_bcast", f_num);
553 	ethtool_sprintf(string, "rxf%d_bcast_vlan", f_num);
554 	ethtool_sprintf(string, "rxf%d_frame_drops", f_num);
555 }
556 
557 static void bnad_get_cq_strings(u8 **string, int q_num)
558 {
559 	ethtool_sprintf(string, "cq%d_producer_index", q_num);
560 	ethtool_sprintf(string, "cq%d_consumer_index", q_num);
561 	ethtool_sprintf(string, "cq%d_hw_producer_index", q_num);
562 	ethtool_sprintf(string, "cq%d_intr", q_num);
563 	ethtool_sprintf(string, "cq%d_poll", q_num);
564 	ethtool_sprintf(string, "cq%d_schedule", q_num);
565 	ethtool_sprintf(string, "cq%d_keep_poll", q_num);
566 	ethtool_sprintf(string, "cq%d_complete", q_num);
567 }
568 
569 static void bnad_get_rxq_strings(u8 **string, int q_num)
570 {
571 	ethtool_sprintf(string, "rxq%d_packets", q_num);
572 	ethtool_sprintf(string, "rxq%d_bytes", q_num);
573 	ethtool_sprintf(string, "rxq%d_packets_with_error", q_num);
574 	ethtool_sprintf(string, "rxq%d_allocbuf_failed", q_num);
575 	ethtool_sprintf(string, "rxq%d_mapbuf_failed", q_num);
576 	ethtool_sprintf(string, "rxq%d_producer_index", q_num);
577 	ethtool_sprintf(string, "rxq%d_consumer_index", q_num);
578 }
579 
580 static void bnad_get_txq_strings(u8 **string, int q_num)
581 {
582 	ethtool_sprintf(string, "txq%d_packets", q_num);
583 	ethtool_sprintf(string, "txq%d_bytes", q_num);
584 	ethtool_sprintf(string, "txq%d_producer_index", q_num);
585 	ethtool_sprintf(string, "txq%d_consumer_index", q_num);
586 	ethtool_sprintf(string, "txq%d_hw_consumer_index", q_num);
587 }
588 
589 static void
590 bnad_get_strings(struct net_device *netdev, u32 stringset, u8 *string)
591 {
592 	struct bnad *bnad = netdev_priv(netdev);
593 	int i, j, q_num;
594 	u32 bmap;
595 
596 	if (stringset != ETH_SS_STATS)
597 		return;
598 
599 	mutex_lock(&bnad->conf_mutex);
600 
601 	for (i = 0; i < BNAD_ETHTOOL_STATS_NUM; i++) {
602 		BUG_ON(!(strlen(bnad_net_stats_strings[i]) < ETH_GSTRING_LEN));
603 		ethtool_sprintf(&string, bnad_net_stats_strings[i]);
604 	}
605 
606 	bmap = bna_tx_rid_mask(&bnad->bna);
607 	for (i = 0; bmap; i++) {
608 		if (bmap & 1)
609 			bnad_get_txf_strings(&string, i);
610 		bmap >>= 1;
611 	}
612 
613 	bmap = bna_rx_rid_mask(&bnad->bna);
614 	for (i = 0; bmap; i++, bmap >>= 1) {
615 		if (bmap & 1)
616 			bnad_get_rxf_strings(&string, i);
617 		bmap >>= 1;
618 	}
619 
620 	q_num = 0;
621 	for (i = 0; i < bnad->num_rx; i++) {
622 		if (!bnad->rx_info[i].rx)
623 			continue;
624 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
625 			bnad_get_cq_strings(&string, q_num++);
626 	}
627 
628 	q_num = 0;
629 	for (i = 0; i < bnad->num_rx; i++) {
630 		if (!bnad->rx_info[i].rx)
631 			continue;
632 		for (j = 0; j < bnad->num_rxp_per_rx; j++) {
633 			bnad_get_rxq_strings(&string, q_num++);
634 			if (bnad->rx_info[i].rx_ctrl[j].ccb &&
635 			    bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
636 			    bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1]->rxq)
637 				bnad_get_rxq_strings(&string, q_num++);
638 		}
639 	}
640 
641 	q_num = 0;
642 	for (i = 0; i < bnad->num_tx; i++) {
643 		if (!bnad->tx_info[i].tx)
644 			continue;
645 		for (j = 0; j < bnad->num_txq_per_tx; j++)
646 			bnad_get_txq_strings(&string, q_num++);
647 	}
648 
649 	mutex_unlock(&bnad->conf_mutex);
650 }
651 
652 static int
653 bnad_get_stats_count_locked(struct net_device *netdev)
654 {
655 	struct bnad *bnad = netdev_priv(netdev);
656 	int i, j, count = 0, rxf_active_num = 0, txf_active_num = 0;
657 	u32 bmap;
658 
659 	bmap = bna_tx_rid_mask(&bnad->bna);
660 	for (i = 0; bmap; i++) {
661 		if (bmap & 1)
662 			txf_active_num++;
663 		bmap >>= 1;
664 	}
665 	bmap = bna_rx_rid_mask(&bnad->bna);
666 	for (i = 0; bmap; i++) {
667 		if (bmap & 1)
668 			rxf_active_num++;
669 		bmap >>= 1;
670 	}
671 	count = BNAD_ETHTOOL_STATS_NUM +
672 		txf_active_num * BNAD_NUM_TXF_COUNTERS +
673 		rxf_active_num * BNAD_NUM_RXF_COUNTERS;
674 
675 	for (i = 0; i < bnad->num_rx; i++) {
676 		if (!bnad->rx_info[i].rx)
677 			continue;
678 		count += bnad->num_rxp_per_rx * BNAD_NUM_CQ_COUNTERS;
679 		count += bnad->num_rxp_per_rx * BNAD_NUM_RXQ_COUNTERS;
680 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
681 			if (bnad->rx_info[i].rx_ctrl[j].ccb &&
682 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
683 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1]->rxq)
684 				count +=  BNAD_NUM_RXQ_COUNTERS;
685 	}
686 
687 	for (i = 0; i < bnad->num_tx; i++) {
688 		if (!bnad->tx_info[i].tx)
689 			continue;
690 		count += bnad->num_txq_per_tx * BNAD_NUM_TXQ_COUNTERS;
691 	}
692 	return count;
693 }
694 
695 static int
696 bnad_per_q_stats_fill(struct bnad *bnad, u64 *buf, int bi)
697 {
698 	int i, j;
699 	struct bna_rcb *rcb = NULL;
700 	struct bna_tcb *tcb = NULL;
701 
702 	for (i = 0; i < bnad->num_rx; i++) {
703 		if (!bnad->rx_info[i].rx)
704 			continue;
705 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
706 			if (bnad->rx_info[i].rx_ctrl[j].ccb &&
707 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[0] &&
708 				bnad->rx_info[i].rx_ctrl[j].ccb->rcb[0]->rxq) {
709 				buf[bi++] = bnad->rx_info[i].rx_ctrl[j].
710 						ccb->producer_index;
711 				buf[bi++] = 0; /* ccb->consumer_index */
712 				buf[bi++] = *(bnad->rx_info[i].rx_ctrl[j].
713 						ccb->hw_producer_index);
714 
715 				buf[bi++] = bnad->rx_info[i].
716 						rx_ctrl[j].rx_intr_ctr;
717 				buf[bi++] = bnad->rx_info[i].
718 						rx_ctrl[j].rx_poll_ctr;
719 				buf[bi++] = bnad->rx_info[i].
720 						rx_ctrl[j].rx_schedule;
721 				buf[bi++] = bnad->rx_info[i].
722 						rx_ctrl[j].rx_keep_poll;
723 				buf[bi++] = bnad->rx_info[i].
724 						rx_ctrl[j].rx_complete;
725 			}
726 	}
727 	for (i = 0; i < bnad->num_rx; i++) {
728 		if (!bnad->rx_info[i].rx)
729 			continue;
730 		for (j = 0; j < bnad->num_rxp_per_rx; j++)
731 			if (bnad->rx_info[i].rx_ctrl[j].ccb) {
732 				if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[0] &&
733 					bnad->rx_info[i].rx_ctrl[j].ccb->
734 					rcb[0]->rxq) {
735 					rcb = bnad->rx_info[i].rx_ctrl[j].
736 							ccb->rcb[0];
737 					buf[bi++] = rcb->rxq->rx_packets;
738 					buf[bi++] = rcb->rxq->rx_bytes;
739 					buf[bi++] = rcb->rxq->
740 							rx_packets_with_error;
741 					buf[bi++] = rcb->rxq->
742 							rxbuf_alloc_failed;
743 					buf[bi++] = rcb->rxq->rxbuf_map_failed;
744 					buf[bi++] = rcb->producer_index;
745 					buf[bi++] = rcb->consumer_index;
746 				}
747 				if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
748 					bnad->rx_info[i].rx_ctrl[j].ccb->
749 					rcb[1]->rxq) {
750 					rcb = bnad->rx_info[i].rx_ctrl[j].
751 								ccb->rcb[1];
752 					buf[bi++] = rcb->rxq->rx_packets;
753 					buf[bi++] = rcb->rxq->rx_bytes;
754 					buf[bi++] = rcb->rxq->
755 							rx_packets_with_error;
756 					buf[bi++] = rcb->rxq->
757 							rxbuf_alloc_failed;
758 					buf[bi++] = rcb->rxq->rxbuf_map_failed;
759 					buf[bi++] = rcb->producer_index;
760 					buf[bi++] = rcb->consumer_index;
761 				}
762 			}
763 	}
764 
765 	for (i = 0; i < bnad->num_tx; i++) {
766 		if (!bnad->tx_info[i].tx)
767 			continue;
768 		for (j = 0; j < bnad->num_txq_per_tx; j++)
769 			if (bnad->tx_info[i].tcb[j] &&
770 				bnad->tx_info[i].tcb[j]->txq) {
771 				tcb = bnad->tx_info[i].tcb[j];
772 				buf[bi++] = tcb->txq->tx_packets;
773 				buf[bi++] = tcb->txq->tx_bytes;
774 				buf[bi++] = tcb->producer_index;
775 				buf[bi++] = tcb->consumer_index;
776 				buf[bi++] = *(tcb->hw_consumer_index);
777 			}
778 	}
779 
780 	return bi;
781 }
782 
783 static void
784 bnad_get_ethtool_stats(struct net_device *netdev, struct ethtool_stats *stats,
785 		       u64 *buf)
786 {
787 	struct bnad *bnad = netdev_priv(netdev);
788 	int i, j, bi = 0;
789 	unsigned long flags;
790 	struct rtnl_link_stats64 net_stats64;
791 	u64 *stats64;
792 	u32 bmap;
793 
794 	mutex_lock(&bnad->conf_mutex);
795 	if (bnad_get_stats_count_locked(netdev) != stats->n_stats) {
796 		mutex_unlock(&bnad->conf_mutex);
797 		return;
798 	}
799 
800 	/*
801 	 * Used bna_lock to sync reads from bna_stats, which is written
802 	 * under the same lock
803 	 */
804 	spin_lock_irqsave(&bnad->bna_lock, flags);
805 
806 	memset(&net_stats64, 0, sizeof(net_stats64));
807 	bnad_netdev_qstats_fill(bnad, &net_stats64);
808 	bnad_netdev_hwstats_fill(bnad, &net_stats64);
809 
810 	buf[bi++] = net_stats64.rx_packets;
811 	buf[bi++] = net_stats64.tx_packets;
812 	buf[bi++] = net_stats64.rx_bytes;
813 	buf[bi++] = net_stats64.tx_bytes;
814 	buf[bi++] = net_stats64.rx_errors;
815 	buf[bi++] = net_stats64.tx_errors;
816 	buf[bi++] = net_stats64.rx_dropped;
817 	buf[bi++] = net_stats64.tx_dropped;
818 	buf[bi++] = net_stats64.multicast;
819 	buf[bi++] = net_stats64.collisions;
820 	buf[bi++] = net_stats64.rx_length_errors;
821 	buf[bi++] = net_stats64.rx_crc_errors;
822 	buf[bi++] = net_stats64.rx_frame_errors;
823 	buf[bi++] = net_stats64.tx_fifo_errors;
824 
825 	/* Get netif_queue_stopped from stack */
826 	bnad->stats.drv_stats.netif_queue_stopped = netif_queue_stopped(netdev);
827 
828 	/* Fill driver stats into ethtool buffers */
829 	stats64 = (u64 *)&bnad->stats.drv_stats;
830 	for (i = 0; i < sizeof(struct bnad_drv_stats) / sizeof(u64); i++)
831 		buf[bi++] = stats64[i];
832 
833 	/* Fill hardware stats excluding the rxf/txf into ethtool bufs */
834 	stats64 = (u64 *) &bnad->stats.bna_stats->hw_stats;
835 	for (i = 0;
836 	     i < offsetof(struct bfi_enet_stats, rxf_stats[0]) /
837 		sizeof(u64);
838 	     i++)
839 		buf[bi++] = stats64[i];
840 
841 	/* Fill txf stats into ethtool buffers */
842 	bmap = bna_tx_rid_mask(&bnad->bna);
843 	for (i = 0; bmap; i++) {
844 		if (bmap & 1) {
845 			stats64 = (u64 *)&bnad->stats.bna_stats->
846 						hw_stats.txf_stats[i];
847 			for (j = 0; j < sizeof(struct bfi_enet_stats_txf) /
848 					sizeof(u64); j++)
849 				buf[bi++] = stats64[j];
850 		}
851 		bmap >>= 1;
852 	}
853 
854 	/*  Fill rxf stats into ethtool buffers */
855 	bmap = bna_rx_rid_mask(&bnad->bna);
856 	for (i = 0; bmap; i++) {
857 		if (bmap & 1) {
858 			stats64 = (u64 *)&bnad->stats.bna_stats->
859 						hw_stats.rxf_stats[i];
860 			for (j = 0; j < sizeof(struct bfi_enet_stats_rxf) /
861 					sizeof(u64); j++)
862 				buf[bi++] = stats64[j];
863 		}
864 		bmap >>= 1;
865 	}
866 
867 	/* Fill per Q stats into ethtool buffers */
868 	bi = bnad_per_q_stats_fill(bnad, buf, bi);
869 
870 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
871 
872 	mutex_unlock(&bnad->conf_mutex);
873 }
874 
875 static int
876 bnad_get_sset_count(struct net_device *netdev, int sset)
877 {
878 	switch (sset) {
879 	case ETH_SS_STATS:
880 		return bnad_get_stats_count_locked(netdev);
881 	default:
882 		return -EOPNOTSUPP;
883 	}
884 }
885 
886 static u32
887 bnad_get_flash_partition_by_offset(struct bnad *bnad, u32 offset,
888 				u32 *base_offset)
889 {
890 	struct bfa_flash_attr *flash_attr;
891 	struct bnad_iocmd_comp fcomp;
892 	u32 i, flash_part = 0, ret;
893 	unsigned long flags = 0;
894 
895 	flash_attr = kzalloc(sizeof(struct bfa_flash_attr), GFP_KERNEL);
896 	if (!flash_attr)
897 		return 0;
898 
899 	fcomp.bnad = bnad;
900 	fcomp.comp_status = 0;
901 
902 	init_completion(&fcomp.comp);
903 	spin_lock_irqsave(&bnad->bna_lock, flags);
904 	ret = bfa_nw_flash_get_attr(&bnad->bna.flash, flash_attr,
905 				bnad_cb_completion, &fcomp);
906 	if (ret != BFA_STATUS_OK) {
907 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
908 		kfree(flash_attr);
909 		return 0;
910 	}
911 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
912 	wait_for_completion(&fcomp.comp);
913 	ret = fcomp.comp_status;
914 
915 	/* Check for the flash type & base offset value */
916 	if (ret == BFA_STATUS_OK) {
917 		for (i = 0; i < flash_attr->npart; i++) {
918 			if (offset >= flash_attr->part[i].part_off &&
919 			    offset < (flash_attr->part[i].part_off +
920 				      flash_attr->part[i].part_size)) {
921 				flash_part = flash_attr->part[i].part_type;
922 				*base_offset = flash_attr->part[i].part_off;
923 				break;
924 			}
925 		}
926 	}
927 	kfree(flash_attr);
928 	return flash_part;
929 }
930 
931 static int
932 bnad_get_eeprom_len(struct net_device *netdev)
933 {
934 	return BFA_TOTAL_FLASH_SIZE;
935 }
936 
937 static int
938 bnad_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
939 		u8 *bytes)
940 {
941 	struct bnad *bnad = netdev_priv(netdev);
942 	struct bnad_iocmd_comp fcomp;
943 	u32 flash_part = 0, base_offset = 0;
944 	unsigned long flags = 0;
945 	int ret = 0;
946 
947 	/* Fill the magic value */
948 	eeprom->magic = bnad->pcidev->vendor | (bnad->pcidev->device << 16);
949 
950 	/* Query the flash partition based on the offset */
951 	flash_part = bnad_get_flash_partition_by_offset(bnad,
952 				eeprom->offset, &base_offset);
953 	if (flash_part == 0)
954 		return -EFAULT;
955 
956 	fcomp.bnad = bnad;
957 	fcomp.comp_status = 0;
958 
959 	init_completion(&fcomp.comp);
960 	spin_lock_irqsave(&bnad->bna_lock, flags);
961 	ret = bfa_nw_flash_read_part(&bnad->bna.flash, flash_part,
962 				bnad->id, bytes, eeprom->len,
963 				eeprom->offset - base_offset,
964 				bnad_cb_completion, &fcomp);
965 	if (ret != BFA_STATUS_OK) {
966 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
967 		goto done;
968 	}
969 
970 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
971 	wait_for_completion(&fcomp.comp);
972 	ret = fcomp.comp_status;
973 done:
974 	return ret;
975 }
976 
977 static int
978 bnad_set_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
979 		u8 *bytes)
980 {
981 	struct bnad *bnad = netdev_priv(netdev);
982 	struct bnad_iocmd_comp fcomp;
983 	u32 flash_part = 0, base_offset = 0;
984 	unsigned long flags = 0;
985 	int ret = 0;
986 
987 	/* Check if the flash update request is valid */
988 	if (eeprom->magic != (bnad->pcidev->vendor |
989 			     (bnad->pcidev->device << 16)))
990 		return -EINVAL;
991 
992 	/* Query the flash partition based on the offset */
993 	flash_part = bnad_get_flash_partition_by_offset(bnad,
994 				eeprom->offset, &base_offset);
995 	if (flash_part == 0)
996 		return -EFAULT;
997 
998 	fcomp.bnad = bnad;
999 	fcomp.comp_status = 0;
1000 
1001 	init_completion(&fcomp.comp);
1002 	spin_lock_irqsave(&bnad->bna_lock, flags);
1003 	ret = bfa_nw_flash_update_part(&bnad->bna.flash, flash_part,
1004 				bnad->id, bytes, eeprom->len,
1005 				eeprom->offset - base_offset,
1006 				bnad_cb_completion, &fcomp);
1007 	if (ret != BFA_STATUS_OK) {
1008 		spin_unlock_irqrestore(&bnad->bna_lock, flags);
1009 		goto done;
1010 	}
1011 
1012 	spin_unlock_irqrestore(&bnad->bna_lock, flags);
1013 	wait_for_completion(&fcomp.comp);
1014 	ret = fcomp.comp_status;
1015 done:
1016 	return ret;
1017 }
1018 
1019 static int
1020 bnad_flash_device(struct net_device *netdev, struct ethtool_flash *eflash)
1021 {
1022 	struct bnad *bnad = netdev_priv(netdev);
1023 	struct bnad_iocmd_comp fcomp;
1024 	const struct firmware *fw;
1025 	int ret = 0;
1026 
1027 	ret = request_firmware(&fw, eflash->data, &bnad->pcidev->dev);
1028 	if (ret) {
1029 		netdev_err(netdev, "can't load firmware %s\n", eflash->data);
1030 		goto out;
1031 	}
1032 
1033 	fcomp.bnad = bnad;
1034 	fcomp.comp_status = 0;
1035 
1036 	init_completion(&fcomp.comp);
1037 	spin_lock_irq(&bnad->bna_lock);
1038 	ret = bfa_nw_flash_update_part(&bnad->bna.flash, BFA_FLASH_PART_FWIMG,
1039 				bnad->id, (u8 *)fw->data, fw->size, 0,
1040 				bnad_cb_completion, &fcomp);
1041 	if (ret != BFA_STATUS_OK) {
1042 		netdev_warn(netdev, "flash update failed with err=%d\n", ret);
1043 		ret = -EIO;
1044 		spin_unlock_irq(&bnad->bna_lock);
1045 		goto out;
1046 	}
1047 
1048 	spin_unlock_irq(&bnad->bna_lock);
1049 	wait_for_completion(&fcomp.comp);
1050 	if (fcomp.comp_status != BFA_STATUS_OK) {
1051 		ret = -EIO;
1052 		netdev_warn(netdev,
1053 			    "firmware image update failed with err=%d\n",
1054 			    fcomp.comp_status);
1055 	}
1056 out:
1057 	release_firmware(fw);
1058 	return ret;
1059 }
1060 
1061 static const struct ethtool_ops bnad_ethtool_ops = {
1062 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1063 				     ETHTOOL_COALESCE_TX_MAX_FRAMES |
1064 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1065 	.get_drvinfo = bnad_get_drvinfo,
1066 	.get_wol = bnad_get_wol,
1067 	.get_link = ethtool_op_get_link,
1068 	.get_coalesce = bnad_get_coalesce,
1069 	.set_coalesce = bnad_set_coalesce,
1070 	.get_ringparam = bnad_get_ringparam,
1071 	.set_ringparam = bnad_set_ringparam,
1072 	.get_pauseparam = bnad_get_pauseparam,
1073 	.set_pauseparam = bnad_set_pauseparam,
1074 	.get_strings = bnad_get_strings,
1075 	.get_ethtool_stats = bnad_get_ethtool_stats,
1076 	.get_sset_count = bnad_get_sset_count,
1077 	.get_eeprom_len = bnad_get_eeprom_len,
1078 	.get_eeprom = bnad_get_eeprom,
1079 	.set_eeprom = bnad_set_eeprom,
1080 	.flash_device = bnad_flash_device,
1081 	.get_ts_info = ethtool_op_get_ts_info,
1082 	.get_link_ksettings = bnad_get_link_ksettings,
1083 	.set_link_ksettings = bnad_set_link_ksettings,
1084 };
1085 
1086 void
1087 bnad_set_ethtool_ops(struct net_device *netdev)
1088 {
1089 	netdev->ethtool_ops = &bnad_ethtool_ops;
1090 }
1091