xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c (revision 02000b55850deeadffe433e4b4930a8831f477de)
1 /*
2  *  Copyright (C) 2013-2015 Chelsio Communications.  All rights reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms and conditions of the GNU General Public License,
6  *  version 2, as published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope it will be useful, but WITHOUT
9  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  *  more details.
12  *
13  *  The full GNU General Public License is included in this distribution in
14  *  the file called "COPYING".
15  *
16  */
17 
18 #include <linux/firmware.h>
19 #include <linux/mdio.h>
20 
21 #include "cxgb4.h"
22 #include "t4_regs.h"
23 #include "t4fw_api.h"
24 #include "cxgb4_cudbg.h"
25 
26 #define EEPROM_MAGIC 0x38E2F10C
27 
28 static u32 get_msglevel(struct net_device *dev)
29 {
30 	return netdev2adap(dev)->msg_enable;
31 }
32 
33 static void set_msglevel(struct net_device *dev, u32 val)
34 {
35 	netdev2adap(dev)->msg_enable = val;
36 }
37 
38 static const char stats_strings[][ETH_GSTRING_LEN] = {
39 	"tx_octets_ok           ",
40 	"tx_frames_ok           ",
41 	"tx_broadcast_frames    ",
42 	"tx_multicast_frames    ",
43 	"tx_unicast_frames      ",
44 	"tx_error_frames        ",
45 
46 	"tx_frames_64           ",
47 	"tx_frames_65_to_127    ",
48 	"tx_frames_128_to_255   ",
49 	"tx_frames_256_to_511   ",
50 	"tx_frames_512_to_1023  ",
51 	"tx_frames_1024_to_1518 ",
52 	"tx_frames_1519_to_max  ",
53 
54 	"tx_frames_dropped      ",
55 	"tx_pause_frames        ",
56 	"tx_ppp0_frames         ",
57 	"tx_ppp1_frames         ",
58 	"tx_ppp2_frames         ",
59 	"tx_ppp3_frames         ",
60 	"tx_ppp4_frames         ",
61 	"tx_ppp5_frames         ",
62 	"tx_ppp6_frames         ",
63 	"tx_ppp7_frames         ",
64 
65 	"rx_octets_ok           ",
66 	"rx_frames_ok           ",
67 	"rx_broadcast_frames    ",
68 	"rx_multicast_frames    ",
69 	"rx_unicast_frames      ",
70 
71 	"rx_frames_too_long     ",
72 	"rx_jabber_errors       ",
73 	"rx_fcs_errors          ",
74 	"rx_length_errors       ",
75 	"rx_symbol_errors       ",
76 	"rx_runt_frames         ",
77 
78 	"rx_frames_64           ",
79 	"rx_frames_65_to_127    ",
80 	"rx_frames_128_to_255   ",
81 	"rx_frames_256_to_511   ",
82 	"rx_frames_512_to_1023  ",
83 	"rx_frames_1024_to_1518 ",
84 	"rx_frames_1519_to_max  ",
85 
86 	"rx_pause_frames        ",
87 	"rx_ppp0_frames         ",
88 	"rx_ppp1_frames         ",
89 	"rx_ppp2_frames         ",
90 	"rx_ppp3_frames         ",
91 	"rx_ppp4_frames         ",
92 	"rx_ppp5_frames         ",
93 	"rx_ppp6_frames         ",
94 	"rx_ppp7_frames         ",
95 
96 	"rx_bg0_frames_dropped  ",
97 	"rx_bg1_frames_dropped  ",
98 	"rx_bg2_frames_dropped  ",
99 	"rx_bg3_frames_dropped  ",
100 	"rx_bg0_frames_trunc    ",
101 	"rx_bg1_frames_trunc    ",
102 	"rx_bg2_frames_trunc    ",
103 	"rx_bg3_frames_trunc    ",
104 
105 	"tso                    ",
106 	"tx_csum_offload        ",
107 	"rx_csum_good           ",
108 	"vlan_extractions       ",
109 	"vlan_insertions        ",
110 	"gro_packets            ",
111 	"gro_merged             ",
112 };
113 
114 static char adapter_stats_strings[][ETH_GSTRING_LEN] = {
115 	"db_drop                ",
116 	"db_full                ",
117 	"db_empty               ",
118 	"tcp_ipv4_out_rsts      ",
119 	"tcp_ipv4_in_segs       ",
120 	"tcp_ipv4_out_segs      ",
121 	"tcp_ipv4_retrans_segs  ",
122 	"tcp_ipv6_out_rsts      ",
123 	"tcp_ipv6_in_segs       ",
124 	"tcp_ipv6_out_segs      ",
125 	"tcp_ipv6_retrans_segs  ",
126 	"usm_ddp_frames         ",
127 	"usm_ddp_octets         ",
128 	"usm_ddp_drops          ",
129 	"rdma_no_rqe_mod_defer  ",
130 	"rdma_no_rqe_pkt_defer  ",
131 	"tp_err_ofld_no_neigh   ",
132 	"tp_err_ofld_cong_defer ",
133 	"write_coal_success     ",
134 	"write_coal_fail        ",
135 };
136 
137 static char channel_stats_strings[][ETH_GSTRING_LEN] = {
138 	"--------Channel--------- ",
139 	"tp_cpl_requests        ",
140 	"tp_cpl_responses       ",
141 	"tp_mac_in_errs         ",
142 	"tp_hdr_in_errs         ",
143 	"tp_tcp_in_errs         ",
144 	"tp_tcp6_in_errs        ",
145 	"tp_tnl_cong_drops      ",
146 	"tp_tnl_tx_drops        ",
147 	"tp_ofld_vlan_drops     ",
148 	"tp_ofld_chan_drops     ",
149 	"fcoe_octets_ddp        ",
150 	"fcoe_frames_ddp        ",
151 	"fcoe_frames_drop       ",
152 };
153 
154 static char loopback_stats_strings[][ETH_GSTRING_LEN] = {
155 	"-------Loopback----------- ",
156 	"octets_ok              ",
157 	"frames_ok              ",
158 	"bcast_frames           ",
159 	"mcast_frames           ",
160 	"ucast_frames           ",
161 	"error_frames           ",
162 	"frames_64              ",
163 	"frames_65_to_127       ",
164 	"frames_128_to_255      ",
165 	"frames_256_to_511      ",
166 	"frames_512_to_1023     ",
167 	"frames_1024_to_1518    ",
168 	"frames_1519_to_max     ",
169 	"frames_dropped         ",
170 	"bg0_frames_dropped     ",
171 	"bg1_frames_dropped     ",
172 	"bg2_frames_dropped     ",
173 	"bg3_frames_dropped     ",
174 	"bg0_frames_trunc       ",
175 	"bg1_frames_trunc       ",
176 	"bg2_frames_trunc       ",
177 	"bg3_frames_trunc       ",
178 };
179 
180 static const char cxgb4_priv_flags_strings[][ETH_GSTRING_LEN] = {
181 	[PRIV_FLAG_PORT_TX_VM_BIT] = "port_tx_vm_wr",
182 };
183 
184 static int get_sset_count(struct net_device *dev, int sset)
185 {
186 	switch (sset) {
187 	case ETH_SS_STATS:
188 		return ARRAY_SIZE(stats_strings) +
189 		       ARRAY_SIZE(adapter_stats_strings) +
190 		       ARRAY_SIZE(channel_stats_strings) +
191 		       ARRAY_SIZE(loopback_stats_strings);
192 	case ETH_SS_PRIV_FLAGS:
193 		return ARRAY_SIZE(cxgb4_priv_flags_strings);
194 	default:
195 		return -EOPNOTSUPP;
196 	}
197 }
198 
199 static int get_regs_len(struct net_device *dev)
200 {
201 	struct adapter *adap = netdev2adap(dev);
202 
203 	return t4_get_regs_len(adap);
204 }
205 
206 static int get_eeprom_len(struct net_device *dev)
207 {
208 	return EEPROMSIZE;
209 }
210 
211 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
212 {
213 	struct adapter *adapter = netdev2adap(dev);
214 	u32 exprom_vers;
215 
216 	strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
217 	strlcpy(info->version, cxgb4_driver_version,
218 		sizeof(info->version));
219 	strlcpy(info->bus_info, pci_name(adapter->pdev),
220 		sizeof(info->bus_info));
221 	info->regdump_len = get_regs_len(dev);
222 
223 	if (!adapter->params.fw_vers)
224 		strcpy(info->fw_version, "N/A");
225 	else
226 		snprintf(info->fw_version, sizeof(info->fw_version),
227 			 "%u.%u.%u.%u, TP %u.%u.%u.%u",
228 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
229 			 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
230 			 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
231 			 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers),
232 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
233 			 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
234 			 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
235 			 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
236 
237 	if (!t4_get_exprom_version(adapter, &exprom_vers))
238 		snprintf(info->erom_version, sizeof(info->erom_version),
239 			 "%u.%u.%u.%u",
240 			 FW_HDR_FW_VER_MAJOR_G(exprom_vers),
241 			 FW_HDR_FW_VER_MINOR_G(exprom_vers),
242 			 FW_HDR_FW_VER_MICRO_G(exprom_vers),
243 			 FW_HDR_FW_VER_BUILD_G(exprom_vers));
244 	info->n_priv_flags = ARRAY_SIZE(cxgb4_priv_flags_strings);
245 }
246 
247 static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
248 {
249 	if (stringset == ETH_SS_STATS) {
250 		memcpy(data, stats_strings, sizeof(stats_strings));
251 		data += sizeof(stats_strings);
252 		memcpy(data, adapter_stats_strings,
253 		       sizeof(adapter_stats_strings));
254 		data += sizeof(adapter_stats_strings);
255 		memcpy(data, channel_stats_strings,
256 		       sizeof(channel_stats_strings));
257 		data += sizeof(channel_stats_strings);
258 		memcpy(data, loopback_stats_strings,
259 		       sizeof(loopback_stats_strings));
260 	} else if (stringset == ETH_SS_PRIV_FLAGS) {
261 		memcpy(data, cxgb4_priv_flags_strings,
262 		       sizeof(cxgb4_priv_flags_strings));
263 	}
264 }
265 
266 /* port stats maintained per queue of the port. They should be in the same
267  * order as in stats_strings above.
268  */
269 struct queue_port_stats {
270 	u64 tso;
271 	u64 tx_csum;
272 	u64 rx_csum;
273 	u64 vlan_ex;
274 	u64 vlan_ins;
275 	u64 gro_pkts;
276 	u64 gro_merged;
277 };
278 
279 struct adapter_stats {
280 	u64 db_drop;
281 	u64 db_full;
282 	u64 db_empty;
283 	u64 tcp_v4_out_rsts;
284 	u64 tcp_v4_in_segs;
285 	u64 tcp_v4_out_segs;
286 	u64 tcp_v4_retrans_segs;
287 	u64 tcp_v6_out_rsts;
288 	u64 tcp_v6_in_segs;
289 	u64 tcp_v6_out_segs;
290 	u64 tcp_v6_retrans_segs;
291 	u64 frames;
292 	u64 octets;
293 	u64 drops;
294 	u64 rqe_dfr_mod;
295 	u64 rqe_dfr_pkt;
296 	u64 ofld_no_neigh;
297 	u64 ofld_cong_defer;
298 	u64 wc_success;
299 	u64 wc_fail;
300 };
301 
302 struct channel_stats {
303 	u64 cpl_req;
304 	u64 cpl_rsp;
305 	u64 mac_in_errs;
306 	u64 hdr_in_errs;
307 	u64 tcp_in_errs;
308 	u64 tcp6_in_errs;
309 	u64 tnl_cong_drops;
310 	u64 tnl_tx_drops;
311 	u64 ofld_vlan_drops;
312 	u64 ofld_chan_drops;
313 	u64 octets_ddp;
314 	u64 frames_ddp;
315 	u64 frames_drop;
316 };
317 
318 static void collect_sge_port_stats(const struct adapter *adap,
319 				   const struct port_info *p,
320 				   struct queue_port_stats *s)
321 {
322 	int i;
323 	const struct sge_eth_txq *tx = &adap->sge.ethtxq[p->first_qset];
324 	const struct sge_eth_rxq *rx = &adap->sge.ethrxq[p->first_qset];
325 
326 	memset(s, 0, sizeof(*s));
327 	for (i = 0; i < p->nqsets; i++, rx++, tx++) {
328 		s->tso += tx->tso;
329 		s->tx_csum += tx->tx_cso;
330 		s->rx_csum += rx->stats.rx_cso;
331 		s->vlan_ex += rx->stats.vlan_ex;
332 		s->vlan_ins += tx->vlan_ins;
333 		s->gro_pkts += rx->stats.lro_pkts;
334 		s->gro_merged += rx->stats.lro_merged;
335 	}
336 }
337 
338 static void collect_adapter_stats(struct adapter *adap, struct adapter_stats *s)
339 {
340 	struct tp_tcp_stats v4, v6;
341 	struct tp_rdma_stats rdma_stats;
342 	struct tp_err_stats err_stats;
343 	struct tp_usm_stats usm_stats;
344 	u64 val1, val2;
345 
346 	memset(s, 0, sizeof(*s));
347 
348 	spin_lock(&adap->stats_lock);
349 	t4_tp_get_tcp_stats(adap, &v4, &v6, false);
350 	t4_tp_get_rdma_stats(adap, &rdma_stats, false);
351 	t4_get_usm_stats(adap, &usm_stats, false);
352 	t4_tp_get_err_stats(adap, &err_stats, false);
353 	spin_unlock(&adap->stats_lock);
354 
355 	s->db_drop = adap->db_stats.db_drop;
356 	s->db_full = adap->db_stats.db_full;
357 	s->db_empty = adap->db_stats.db_empty;
358 
359 	s->tcp_v4_out_rsts = v4.tcp_out_rsts;
360 	s->tcp_v4_in_segs = v4.tcp_in_segs;
361 	s->tcp_v4_out_segs = v4.tcp_out_segs;
362 	s->tcp_v4_retrans_segs = v4.tcp_retrans_segs;
363 	s->tcp_v6_out_rsts = v6.tcp_out_rsts;
364 	s->tcp_v6_in_segs = v6.tcp_in_segs;
365 	s->tcp_v6_out_segs = v6.tcp_out_segs;
366 	s->tcp_v6_retrans_segs = v6.tcp_retrans_segs;
367 
368 	if (is_offload(adap)) {
369 		s->frames = usm_stats.frames;
370 		s->octets = usm_stats.octets;
371 		s->drops = usm_stats.drops;
372 		s->rqe_dfr_mod = rdma_stats.rqe_dfr_mod;
373 		s->rqe_dfr_pkt = rdma_stats.rqe_dfr_pkt;
374 	}
375 
376 	s->ofld_no_neigh = err_stats.ofld_no_neigh;
377 	s->ofld_cong_defer = err_stats.ofld_cong_defer;
378 
379 	if (!is_t4(adap->params.chip)) {
380 		int v;
381 
382 		v = t4_read_reg(adap, SGE_STAT_CFG_A);
383 		if (STATSOURCE_T5_G(v) == 7) {
384 			val2 = t4_read_reg(adap, SGE_STAT_MATCH_A);
385 			val1 = t4_read_reg(adap, SGE_STAT_TOTAL_A);
386 			s->wc_success = val1 - val2;
387 			s->wc_fail = val2;
388 		}
389 	}
390 }
391 
392 static void collect_channel_stats(struct adapter *adap, struct channel_stats *s,
393 				  u8 i)
394 {
395 	struct tp_cpl_stats cpl_stats;
396 	struct tp_err_stats err_stats;
397 	struct tp_fcoe_stats fcoe_stats;
398 
399 	memset(s, 0, sizeof(*s));
400 
401 	spin_lock(&adap->stats_lock);
402 	t4_tp_get_cpl_stats(adap, &cpl_stats, false);
403 	t4_tp_get_err_stats(adap, &err_stats, false);
404 	t4_get_fcoe_stats(adap, i, &fcoe_stats, false);
405 	spin_unlock(&adap->stats_lock);
406 
407 	s->cpl_req = cpl_stats.req[i];
408 	s->cpl_rsp = cpl_stats.rsp[i];
409 	s->mac_in_errs = err_stats.mac_in_errs[i];
410 	s->hdr_in_errs = err_stats.hdr_in_errs[i];
411 	s->tcp_in_errs = err_stats.tcp_in_errs[i];
412 	s->tcp6_in_errs = err_stats.tcp6_in_errs[i];
413 	s->tnl_cong_drops = err_stats.tnl_cong_drops[i];
414 	s->tnl_tx_drops = err_stats.tnl_tx_drops[i];
415 	s->ofld_vlan_drops = err_stats.ofld_vlan_drops[i];
416 	s->ofld_chan_drops = err_stats.ofld_chan_drops[i];
417 	s->octets_ddp = fcoe_stats.octets_ddp;
418 	s->frames_ddp = fcoe_stats.frames_ddp;
419 	s->frames_drop = fcoe_stats.frames_drop;
420 }
421 
422 static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
423 		      u64 *data)
424 {
425 	struct port_info *pi = netdev_priv(dev);
426 	struct adapter *adapter = pi->adapter;
427 	struct lb_port_stats s;
428 	int i;
429 	u64 *p0;
430 
431 	t4_get_port_stats_offset(adapter, pi->tx_chan,
432 				 (struct port_stats *)data,
433 				 &pi->stats_base);
434 
435 	data += sizeof(struct port_stats) / sizeof(u64);
436 	collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
437 	data += sizeof(struct queue_port_stats) / sizeof(u64);
438 	collect_adapter_stats(adapter, (struct adapter_stats *)data);
439 	data += sizeof(struct adapter_stats) / sizeof(u64);
440 
441 	*data++ = (u64)pi->port_id;
442 	collect_channel_stats(adapter, (struct channel_stats *)data,
443 			      pi->port_id);
444 	data += sizeof(struct channel_stats) / sizeof(u64);
445 
446 	*data++ = (u64)pi->port_id;
447 	memset(&s, 0, sizeof(s));
448 	t4_get_lb_stats(adapter, pi->port_id, &s);
449 
450 	p0 = &s.octets;
451 	for (i = 0; i < ARRAY_SIZE(loopback_stats_strings) - 1; i++)
452 		*data++ = (unsigned long long)*p0++;
453 }
454 
455 static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
456 		     void *buf)
457 {
458 	struct adapter *adap = netdev2adap(dev);
459 	size_t buf_size;
460 
461 	buf_size = t4_get_regs_len(adap);
462 	regs->version = mk_adap_vers(adap);
463 	t4_get_regs(adap, buf, buf_size);
464 }
465 
466 static int restart_autoneg(struct net_device *dev)
467 {
468 	struct port_info *p = netdev_priv(dev);
469 
470 	if (!netif_running(dev))
471 		return -EAGAIN;
472 	if (p->link_cfg.autoneg != AUTONEG_ENABLE)
473 		return -EINVAL;
474 	t4_restart_aneg(p->adapter, p->adapter->pf, p->tx_chan);
475 	return 0;
476 }
477 
478 static int identify_port(struct net_device *dev,
479 			 enum ethtool_phys_id_state state)
480 {
481 	unsigned int val;
482 	struct adapter *adap = netdev2adap(dev);
483 
484 	if (state == ETHTOOL_ID_ACTIVE)
485 		val = 0xffff;
486 	else if (state == ETHTOOL_ID_INACTIVE)
487 		val = 0;
488 	else
489 		return -EINVAL;
490 
491 	return t4_identify_port(adap, adap->pf, netdev2pinfo(dev)->viid, val);
492 }
493 
494 /**
495  *	from_fw_port_mod_type - translate Firmware Port/Module type to Ethtool
496  *	@port_type: Firmware Port Type
497  *	@mod_type: Firmware Module Type
498  *
499  *	Translate Firmware Port/Module type to Ethtool Port Type.
500  */
501 static int from_fw_port_mod_type(enum fw_port_type port_type,
502 				 enum fw_port_module_type mod_type)
503 {
504 	if (port_type == FW_PORT_TYPE_BT_SGMII ||
505 	    port_type == FW_PORT_TYPE_BT_XFI ||
506 	    port_type == FW_PORT_TYPE_BT_XAUI) {
507 		return PORT_TP;
508 	} else if (port_type == FW_PORT_TYPE_FIBER_XFI ||
509 		   port_type == FW_PORT_TYPE_FIBER_XAUI) {
510 		return PORT_FIBRE;
511 	} else if (port_type == FW_PORT_TYPE_SFP ||
512 		   port_type == FW_PORT_TYPE_QSFP_10G ||
513 		   port_type == FW_PORT_TYPE_QSA ||
514 		   port_type == FW_PORT_TYPE_QSFP ||
515 		   port_type == FW_PORT_TYPE_CR4_QSFP ||
516 		   port_type == FW_PORT_TYPE_CR_QSFP ||
517 		   port_type == FW_PORT_TYPE_CR2_QSFP ||
518 		   port_type == FW_PORT_TYPE_SFP28) {
519 		if (mod_type == FW_PORT_MOD_TYPE_LR ||
520 		    mod_type == FW_PORT_MOD_TYPE_SR ||
521 		    mod_type == FW_PORT_MOD_TYPE_ER ||
522 		    mod_type == FW_PORT_MOD_TYPE_LRM)
523 			return PORT_FIBRE;
524 		else if (mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
525 			 mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
526 			return PORT_DA;
527 		else
528 			return PORT_OTHER;
529 	} else if (port_type == FW_PORT_TYPE_KR4_100G ||
530 		   port_type == FW_PORT_TYPE_KR_SFP28 ||
531 		   port_type == FW_PORT_TYPE_KR_XLAUI) {
532 		return PORT_NONE;
533 	}
534 
535 	return PORT_OTHER;
536 }
537 
538 /**
539  *	speed_to_fw_caps - translate Port Speed to Firmware Port Capabilities
540  *	@speed: speed in Kb/s
541  *
542  *	Translates a specific Port Speed into a Firmware Port Capabilities
543  *	value.
544  */
545 static unsigned int speed_to_fw_caps(int speed)
546 {
547 	if (speed == 100)
548 		return FW_PORT_CAP32_SPEED_100M;
549 	if (speed == 1000)
550 		return FW_PORT_CAP32_SPEED_1G;
551 	if (speed == 10000)
552 		return FW_PORT_CAP32_SPEED_10G;
553 	if (speed == 25000)
554 		return FW_PORT_CAP32_SPEED_25G;
555 	if (speed == 40000)
556 		return FW_PORT_CAP32_SPEED_40G;
557 	if (speed == 50000)
558 		return FW_PORT_CAP32_SPEED_50G;
559 	if (speed == 100000)
560 		return FW_PORT_CAP32_SPEED_100G;
561 	if (speed == 200000)
562 		return FW_PORT_CAP32_SPEED_200G;
563 	if (speed == 400000)
564 		return FW_PORT_CAP32_SPEED_400G;
565 	return 0;
566 }
567 
568 /**
569  *	fw_caps_to_lmm - translate Firmware to ethtool Link Mode Mask
570  *	@port_type: Firmware Port Type
571  *	@fw_caps: Firmware Port Capabilities
572  *	@link_mode_mask: ethtool Link Mode Mask
573  *
574  *	Translate a Firmware Port Capabilities specification to an ethtool
575  *	Link Mode Mask.
576  */
577 static void fw_caps_to_lmm(enum fw_port_type port_type,
578 			   unsigned int fw_caps,
579 			   unsigned long *link_mode_mask)
580 {
581 	#define SET_LMM(__lmm_name) \
582 		__set_bit(ETHTOOL_LINK_MODE_ ## __lmm_name ## _BIT, \
583 			  link_mode_mask)
584 
585 	#define FW_CAPS_TO_LMM(__fw_name, __lmm_name) \
586 		do { \
587 			if (fw_caps & FW_PORT_CAP32_ ## __fw_name) \
588 				SET_LMM(__lmm_name); \
589 		} while (0)
590 
591 	switch (port_type) {
592 	case FW_PORT_TYPE_BT_SGMII:
593 	case FW_PORT_TYPE_BT_XFI:
594 	case FW_PORT_TYPE_BT_XAUI:
595 		SET_LMM(TP);
596 		FW_CAPS_TO_LMM(SPEED_100M, 100baseT_Full);
597 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
598 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
599 		break;
600 
601 	case FW_PORT_TYPE_KX4:
602 	case FW_PORT_TYPE_KX:
603 		SET_LMM(Backplane);
604 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseKX_Full);
605 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKX4_Full);
606 		break;
607 
608 	case FW_PORT_TYPE_KR:
609 		SET_LMM(Backplane);
610 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKR_Full);
611 		break;
612 
613 	case FW_PORT_TYPE_BP_AP:
614 		SET_LMM(Backplane);
615 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseKX_Full);
616 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseR_FEC);
617 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKR_Full);
618 		break;
619 
620 	case FW_PORT_TYPE_BP4_AP:
621 		SET_LMM(Backplane);
622 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseKX_Full);
623 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseR_FEC);
624 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKR_Full);
625 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKX4_Full);
626 		break;
627 
628 	case FW_PORT_TYPE_FIBER_XFI:
629 	case FW_PORT_TYPE_FIBER_XAUI:
630 	case FW_PORT_TYPE_SFP:
631 	case FW_PORT_TYPE_QSFP_10G:
632 	case FW_PORT_TYPE_QSA:
633 		SET_LMM(FIBRE);
634 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
635 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
636 		break;
637 
638 	case FW_PORT_TYPE_BP40_BA:
639 	case FW_PORT_TYPE_QSFP:
640 		SET_LMM(FIBRE);
641 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
642 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
643 		FW_CAPS_TO_LMM(SPEED_40G, 40000baseSR4_Full);
644 		break;
645 
646 	case FW_PORT_TYPE_CR_QSFP:
647 	case FW_PORT_TYPE_SFP28:
648 		SET_LMM(FIBRE);
649 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
650 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
651 		FW_CAPS_TO_LMM(SPEED_25G, 25000baseCR_Full);
652 		break;
653 
654 	case FW_PORT_TYPE_KR_SFP28:
655 		SET_LMM(Backplane);
656 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
657 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKR_Full);
658 		FW_CAPS_TO_LMM(SPEED_25G, 25000baseKR_Full);
659 		break;
660 
661 	case FW_PORT_TYPE_KR_XLAUI:
662 		SET_LMM(Backplane);
663 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseKX_Full);
664 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKR_Full);
665 		FW_CAPS_TO_LMM(SPEED_40G, 40000baseKR4_Full);
666 		break;
667 
668 	case FW_PORT_TYPE_CR2_QSFP:
669 		SET_LMM(FIBRE);
670 		FW_CAPS_TO_LMM(SPEED_50G, 50000baseSR2_Full);
671 		break;
672 
673 	case FW_PORT_TYPE_KR4_100G:
674 	case FW_PORT_TYPE_CR4_QSFP:
675 		SET_LMM(FIBRE);
676 		FW_CAPS_TO_LMM(SPEED_1G,  1000baseT_Full);
677 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseSR_Full);
678 		FW_CAPS_TO_LMM(SPEED_40G, 40000baseSR4_Full);
679 		FW_CAPS_TO_LMM(SPEED_25G, 25000baseCR_Full);
680 		FW_CAPS_TO_LMM(SPEED_50G, 50000baseCR2_Full);
681 		FW_CAPS_TO_LMM(SPEED_100G, 100000baseCR4_Full);
682 		break;
683 
684 	default:
685 		break;
686 	}
687 
688 	FW_CAPS_TO_LMM(ANEG, Autoneg);
689 	FW_CAPS_TO_LMM(802_3_PAUSE, Pause);
690 	FW_CAPS_TO_LMM(802_3_ASM_DIR, Asym_Pause);
691 
692 	#undef FW_CAPS_TO_LMM
693 	#undef SET_LMM
694 }
695 
696 /**
697  *	lmm_to_fw_caps - translate ethtool Link Mode Mask to Firmware
698  *	capabilities
699  *	@et_lmm: ethtool Link Mode Mask
700  *
701  *	Translate ethtool Link Mode Mask into a Firmware Port capabilities
702  *	value.
703  */
704 static unsigned int lmm_to_fw_caps(const unsigned long *link_mode_mask)
705 {
706 	unsigned int fw_caps = 0;
707 
708 	#define LMM_TO_FW_CAPS(__lmm_name, __fw_name) \
709 		do { \
710 			if (test_bit(ETHTOOL_LINK_MODE_ ## __lmm_name ## _BIT, \
711 				     link_mode_mask)) \
712 				fw_caps |= FW_PORT_CAP32_ ## __fw_name; \
713 		} while (0)
714 
715 	LMM_TO_FW_CAPS(100baseT_Full, SPEED_100M);
716 	LMM_TO_FW_CAPS(1000baseT_Full, SPEED_1G);
717 	LMM_TO_FW_CAPS(10000baseT_Full, SPEED_10G);
718 	LMM_TO_FW_CAPS(40000baseSR4_Full, SPEED_40G);
719 	LMM_TO_FW_CAPS(25000baseCR_Full, SPEED_25G);
720 	LMM_TO_FW_CAPS(50000baseCR2_Full, SPEED_50G);
721 	LMM_TO_FW_CAPS(100000baseCR4_Full, SPEED_100G);
722 
723 	#undef LMM_TO_FW_CAPS
724 
725 	return fw_caps;
726 }
727 
728 static int get_link_ksettings(struct net_device *dev,
729 			      struct ethtool_link_ksettings *link_ksettings)
730 {
731 	struct port_info *pi = netdev_priv(dev);
732 	struct ethtool_link_settings *base = &link_ksettings->base;
733 
734 	/* For the nonce, the Firmware doesn't send up Port State changes
735 	 * when the Virtual Interface attached to the Port is down.  So
736 	 * if it's down, let's grab any changes.
737 	 */
738 	if (!netif_running(dev))
739 		(void)t4_update_port_info(pi);
740 
741 	ethtool_link_ksettings_zero_link_mode(link_ksettings, supported);
742 	ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
743 	ethtool_link_ksettings_zero_link_mode(link_ksettings, lp_advertising);
744 
745 	base->port = from_fw_port_mod_type(pi->port_type, pi->mod_type);
746 
747 	if (pi->mdio_addr >= 0) {
748 		base->phy_address = pi->mdio_addr;
749 		base->mdio_support = (pi->port_type == FW_PORT_TYPE_BT_SGMII
750 				      ? ETH_MDIO_SUPPORTS_C22
751 				      : ETH_MDIO_SUPPORTS_C45);
752 	} else {
753 		base->phy_address = 255;
754 		base->mdio_support = 0;
755 	}
756 
757 	fw_caps_to_lmm(pi->port_type, pi->link_cfg.pcaps,
758 		       link_ksettings->link_modes.supported);
759 	fw_caps_to_lmm(pi->port_type, pi->link_cfg.acaps,
760 		       link_ksettings->link_modes.advertising);
761 	fw_caps_to_lmm(pi->port_type, pi->link_cfg.lpacaps,
762 		       link_ksettings->link_modes.lp_advertising);
763 
764 	if (netif_carrier_ok(dev)) {
765 		base->speed = pi->link_cfg.speed;
766 		base->duplex = DUPLEX_FULL;
767 	} else {
768 		base->speed = SPEED_UNKNOWN;
769 		base->duplex = DUPLEX_UNKNOWN;
770 	}
771 
772 	if (pi->link_cfg.fc & PAUSE_RX) {
773 		if (pi->link_cfg.fc & PAUSE_TX) {
774 			ethtool_link_ksettings_add_link_mode(link_ksettings,
775 							     advertising,
776 							     Pause);
777 		} else {
778 			ethtool_link_ksettings_add_link_mode(link_ksettings,
779 							     advertising,
780 							     Asym_Pause);
781 		}
782 	} else if (pi->link_cfg.fc & PAUSE_TX) {
783 		ethtool_link_ksettings_add_link_mode(link_ksettings,
784 						     advertising,
785 						     Asym_Pause);
786 	}
787 
788 	base->autoneg = pi->link_cfg.autoneg;
789 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_ANEG)
790 		ethtool_link_ksettings_add_link_mode(link_ksettings,
791 						     supported, Autoneg);
792 	if (pi->link_cfg.autoneg)
793 		ethtool_link_ksettings_add_link_mode(link_ksettings,
794 						     advertising, Autoneg);
795 
796 	return 0;
797 }
798 
799 static int set_link_ksettings(struct net_device *dev,
800 			    const struct ethtool_link_ksettings *link_ksettings)
801 {
802 	struct port_info *pi = netdev_priv(dev);
803 	struct link_config *lc = &pi->link_cfg;
804 	const struct ethtool_link_settings *base = &link_ksettings->base;
805 	struct link_config old_lc;
806 	unsigned int fw_caps;
807 	int ret = 0;
808 
809 	/* only full-duplex supported */
810 	if (base->duplex != DUPLEX_FULL)
811 		return -EINVAL;
812 
813 	old_lc = *lc;
814 	if (!(lc->pcaps & FW_PORT_CAP32_ANEG) ||
815 	    base->autoneg == AUTONEG_DISABLE) {
816 		fw_caps = speed_to_fw_caps(base->speed);
817 
818 		/* Must only specify a single speed which must be supported
819 		 * as part of the Physical Port Capabilities.
820 		 */
821 		if ((fw_caps & (fw_caps - 1)) != 0 ||
822 		    !(lc->pcaps & fw_caps))
823 			return -EINVAL;
824 
825 		lc->speed_caps = fw_caps;
826 		lc->acaps = fw_caps;
827 	} else {
828 		fw_caps =
829 			 lmm_to_fw_caps(link_ksettings->link_modes.advertising);
830 		if (!(lc->pcaps & fw_caps))
831 			return -EINVAL;
832 		lc->speed_caps = 0;
833 		lc->acaps = fw_caps | FW_PORT_CAP32_ANEG;
834 	}
835 	lc->autoneg = base->autoneg;
836 
837 	/* If the firmware rejects the Link Configuration request, back out
838 	 * the changes and report the error.
839 	 */
840 	ret = t4_link_l1cfg(pi->adapter, pi->adapter->mbox, pi->tx_chan, lc);
841 	if (ret)
842 		*lc = old_lc;
843 
844 	return ret;
845 }
846 
847 /* Translate the Firmware FEC value into the ethtool value. */
848 static inline unsigned int fwcap_to_eth_fec(unsigned int fw_fec)
849 {
850 	unsigned int eth_fec = 0;
851 
852 	if (fw_fec & FW_PORT_CAP32_FEC_RS)
853 		eth_fec |= ETHTOOL_FEC_RS;
854 	if (fw_fec & FW_PORT_CAP32_FEC_BASER_RS)
855 		eth_fec |= ETHTOOL_FEC_BASER;
856 
857 	/* if nothing is set, then FEC is off */
858 	if (!eth_fec)
859 		eth_fec = ETHTOOL_FEC_OFF;
860 
861 	return eth_fec;
862 }
863 
864 /* Translate Common Code FEC value into ethtool value. */
865 static inline unsigned int cc_to_eth_fec(unsigned int cc_fec)
866 {
867 	unsigned int eth_fec = 0;
868 
869 	if (cc_fec & FEC_AUTO)
870 		eth_fec |= ETHTOOL_FEC_AUTO;
871 	if (cc_fec & FEC_RS)
872 		eth_fec |= ETHTOOL_FEC_RS;
873 	if (cc_fec & FEC_BASER_RS)
874 		eth_fec |= ETHTOOL_FEC_BASER;
875 
876 	/* if nothing is set, then FEC is off */
877 	if (!eth_fec)
878 		eth_fec = ETHTOOL_FEC_OFF;
879 
880 	return eth_fec;
881 }
882 
883 /* Translate ethtool FEC value into Common Code value. */
884 static inline unsigned int eth_to_cc_fec(unsigned int eth_fec)
885 {
886 	unsigned int cc_fec = 0;
887 
888 	if (eth_fec & ETHTOOL_FEC_OFF)
889 		return cc_fec;
890 
891 	if (eth_fec & ETHTOOL_FEC_AUTO)
892 		cc_fec |= FEC_AUTO;
893 	if (eth_fec & ETHTOOL_FEC_RS)
894 		cc_fec |= FEC_RS;
895 	if (eth_fec & ETHTOOL_FEC_BASER)
896 		cc_fec |= FEC_BASER_RS;
897 
898 	return cc_fec;
899 }
900 
901 static int get_fecparam(struct net_device *dev, struct ethtool_fecparam *fec)
902 {
903 	const struct port_info *pi = netdev_priv(dev);
904 	const struct link_config *lc = &pi->link_cfg;
905 
906 	/* Translate the Firmware FEC Support into the ethtool value.  We
907 	 * always support IEEE 802.3 "automatic" selection of Link FEC type if
908 	 * any FEC is supported.
909 	 */
910 	fec->fec = fwcap_to_eth_fec(lc->pcaps);
911 	if (fec->fec != ETHTOOL_FEC_OFF)
912 		fec->fec |= ETHTOOL_FEC_AUTO;
913 
914 	/* Translate the current internal FEC parameters into the
915 	 * ethtool values.
916 	 */
917 	fec->active_fec = cc_to_eth_fec(lc->fec);
918 
919 	return 0;
920 }
921 
922 static int set_fecparam(struct net_device *dev, struct ethtool_fecparam *fec)
923 {
924 	struct port_info *pi = netdev_priv(dev);
925 	struct link_config *lc = &pi->link_cfg;
926 	struct link_config old_lc;
927 	int ret;
928 
929 	/* Save old Link Configuration in case the L1 Configure below
930 	 * fails.
931 	 */
932 	old_lc = *lc;
933 
934 	/* Try to perform the L1 Configure and return the result of that
935 	 * effort.  If it fails, revert the attempted change.
936 	 */
937 	lc->requested_fec = eth_to_cc_fec(fec->fec);
938 	ret = t4_link_l1cfg(pi->adapter, pi->adapter->mbox,
939 			    pi->tx_chan, lc);
940 	if (ret)
941 		*lc = old_lc;
942 	return ret;
943 }
944 
945 static void get_pauseparam(struct net_device *dev,
946 			   struct ethtool_pauseparam *epause)
947 {
948 	struct port_info *p = netdev_priv(dev);
949 
950 	epause->autoneg = (p->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
951 	epause->rx_pause = (p->link_cfg.fc & PAUSE_RX) != 0;
952 	epause->tx_pause = (p->link_cfg.fc & PAUSE_TX) != 0;
953 }
954 
955 static int set_pauseparam(struct net_device *dev,
956 			  struct ethtool_pauseparam *epause)
957 {
958 	struct port_info *p = netdev_priv(dev);
959 	struct link_config *lc = &p->link_cfg;
960 
961 	if (epause->autoneg == AUTONEG_DISABLE)
962 		lc->requested_fc = 0;
963 	else if (lc->pcaps & FW_PORT_CAP32_ANEG)
964 		lc->requested_fc = PAUSE_AUTONEG;
965 	else
966 		return -EINVAL;
967 
968 	if (epause->rx_pause)
969 		lc->requested_fc |= PAUSE_RX;
970 	if (epause->tx_pause)
971 		lc->requested_fc |= PAUSE_TX;
972 	if (netif_running(dev))
973 		return t4_link_l1cfg(p->adapter, p->adapter->mbox, p->tx_chan,
974 				     lc);
975 	return 0;
976 }
977 
978 static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
979 {
980 	const struct port_info *pi = netdev_priv(dev);
981 	const struct sge *s = &pi->adapter->sge;
982 
983 	e->rx_max_pending = MAX_RX_BUFFERS;
984 	e->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
985 	e->rx_jumbo_max_pending = 0;
986 	e->tx_max_pending = MAX_TXQ_ENTRIES;
987 
988 	e->rx_pending = s->ethrxq[pi->first_qset].fl.size - 8;
989 	e->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
990 	e->rx_jumbo_pending = 0;
991 	e->tx_pending = s->ethtxq[pi->first_qset].q.size;
992 }
993 
994 static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
995 {
996 	int i;
997 	const struct port_info *pi = netdev_priv(dev);
998 	struct adapter *adapter = pi->adapter;
999 	struct sge *s = &adapter->sge;
1000 
1001 	if (e->rx_pending > MAX_RX_BUFFERS || e->rx_jumbo_pending ||
1002 	    e->tx_pending > MAX_TXQ_ENTRIES ||
1003 	    e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
1004 	    e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
1005 	    e->rx_pending < MIN_FL_ENTRIES || e->tx_pending < MIN_TXQ_ENTRIES)
1006 		return -EINVAL;
1007 
1008 	if (adapter->flags & FULL_INIT_DONE)
1009 		return -EBUSY;
1010 
1011 	for (i = 0; i < pi->nqsets; ++i) {
1012 		s->ethtxq[pi->first_qset + i].q.size = e->tx_pending;
1013 		s->ethrxq[pi->first_qset + i].fl.size = e->rx_pending + 8;
1014 		s->ethrxq[pi->first_qset + i].rspq.size = e->rx_mini_pending;
1015 	}
1016 	return 0;
1017 }
1018 
1019 /**
1020  * set_rx_intr_params - set a net devices's RX interrupt holdoff paramete!
1021  * @dev: the network device
1022  * @us: the hold-off time in us, or 0 to disable timer
1023  * @cnt: the hold-off packet count, or 0 to disable counter
1024  *
1025  * Set the RX interrupt hold-off parameters for a network device.
1026  */
1027 static int set_rx_intr_params(struct net_device *dev,
1028 			      unsigned int us, unsigned int cnt)
1029 {
1030 	int i, err;
1031 	struct port_info *pi = netdev_priv(dev);
1032 	struct adapter *adap = pi->adapter;
1033 	struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
1034 
1035 	for (i = 0; i < pi->nqsets; i++, q++) {
1036 		err = cxgb4_set_rspq_intr_params(&q->rspq, us, cnt);
1037 		if (err)
1038 			return err;
1039 	}
1040 	return 0;
1041 }
1042 
1043 static int set_adaptive_rx_setting(struct net_device *dev, int adaptive_rx)
1044 {
1045 	int i;
1046 	struct port_info *pi = netdev_priv(dev);
1047 	struct adapter *adap = pi->adapter;
1048 	struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
1049 
1050 	for (i = 0; i < pi->nqsets; i++, q++)
1051 		q->rspq.adaptive_rx = adaptive_rx;
1052 
1053 	return 0;
1054 }
1055 
1056 static int get_adaptive_rx_setting(struct net_device *dev)
1057 {
1058 	struct port_info *pi = netdev_priv(dev);
1059 	struct adapter *adap = pi->adapter;
1060 	struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
1061 
1062 	return q->rspq.adaptive_rx;
1063 }
1064 
1065 static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1066 {
1067 	set_adaptive_rx_setting(dev, c->use_adaptive_rx_coalesce);
1068 	return set_rx_intr_params(dev, c->rx_coalesce_usecs,
1069 				  c->rx_max_coalesced_frames);
1070 }
1071 
1072 static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1073 {
1074 	const struct port_info *pi = netdev_priv(dev);
1075 	const struct adapter *adap = pi->adapter;
1076 	const struct sge_rspq *rq = &adap->sge.ethrxq[pi->first_qset].rspq;
1077 
1078 	c->rx_coalesce_usecs = qtimer_val(adap, rq);
1079 	c->rx_max_coalesced_frames = (rq->intr_params & QINTR_CNT_EN_F) ?
1080 		adap->sge.counter_val[rq->pktcnt_idx] : 0;
1081 	c->use_adaptive_rx_coalesce = get_adaptive_rx_setting(dev);
1082 	return 0;
1083 }
1084 
1085 /* The next two routines implement eeprom read/write from physical addresses.
1086  */
1087 static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
1088 {
1089 	int vaddr = t4_eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
1090 
1091 	if (vaddr >= 0)
1092 		vaddr = pci_read_vpd(adap->pdev, vaddr, sizeof(u32), v);
1093 	return vaddr < 0 ? vaddr : 0;
1094 }
1095 
1096 static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
1097 {
1098 	int vaddr = t4_eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
1099 
1100 	if (vaddr >= 0)
1101 		vaddr = pci_write_vpd(adap->pdev, vaddr, sizeof(u32), &v);
1102 	return vaddr < 0 ? vaddr : 0;
1103 }
1104 
1105 #define EEPROM_MAGIC 0x38E2F10C
1106 
1107 static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
1108 		      u8 *data)
1109 {
1110 	int i, err = 0;
1111 	struct adapter *adapter = netdev2adap(dev);
1112 	u8 *buf = kvzalloc(EEPROMSIZE, GFP_KERNEL);
1113 
1114 	if (!buf)
1115 		return -ENOMEM;
1116 
1117 	e->magic = EEPROM_MAGIC;
1118 	for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
1119 		err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
1120 
1121 	if (!err)
1122 		memcpy(data, buf + e->offset, e->len);
1123 	kvfree(buf);
1124 	return err;
1125 }
1126 
1127 static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
1128 		      u8 *data)
1129 {
1130 	u8 *buf;
1131 	int err = 0;
1132 	u32 aligned_offset, aligned_len, *p;
1133 	struct adapter *adapter = netdev2adap(dev);
1134 
1135 	if (eeprom->magic != EEPROM_MAGIC)
1136 		return -EINVAL;
1137 
1138 	aligned_offset = eeprom->offset & ~3;
1139 	aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
1140 
1141 	if (adapter->pf > 0) {
1142 		u32 start = 1024 + adapter->pf * EEPROMPFSIZE;
1143 
1144 		if (aligned_offset < start ||
1145 		    aligned_offset + aligned_len > start + EEPROMPFSIZE)
1146 			return -EPERM;
1147 	}
1148 
1149 	if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
1150 		/* RMW possibly needed for first or last words.
1151 		 */
1152 		buf = kvzalloc(aligned_len, GFP_KERNEL);
1153 		if (!buf)
1154 			return -ENOMEM;
1155 		err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
1156 		if (!err && aligned_len > 4)
1157 			err = eeprom_rd_phys(adapter,
1158 					     aligned_offset + aligned_len - 4,
1159 					     (u32 *)&buf[aligned_len - 4]);
1160 		if (err)
1161 			goto out;
1162 		memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
1163 	} else {
1164 		buf = data;
1165 	}
1166 
1167 	err = t4_seeprom_wp(adapter, false);
1168 	if (err)
1169 		goto out;
1170 
1171 	for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
1172 		err = eeprom_wr_phys(adapter, aligned_offset, *p);
1173 		aligned_offset += 4;
1174 	}
1175 
1176 	if (!err)
1177 		err = t4_seeprom_wp(adapter, true);
1178 out:
1179 	if (buf != data)
1180 		kvfree(buf);
1181 	return err;
1182 }
1183 
1184 static int set_flash(struct net_device *netdev, struct ethtool_flash *ef)
1185 {
1186 	int ret;
1187 	const struct firmware *fw;
1188 	struct adapter *adap = netdev2adap(netdev);
1189 	unsigned int mbox = PCIE_FW_MASTER_M + 1;
1190 	u32 pcie_fw;
1191 	unsigned int master;
1192 	u8 master_vld = 0;
1193 
1194 	pcie_fw = t4_read_reg(adap, PCIE_FW_A);
1195 	master = PCIE_FW_MASTER_G(pcie_fw);
1196 	if (pcie_fw & PCIE_FW_MASTER_VLD_F)
1197 		master_vld = 1;
1198 	/* if csiostor is the master return */
1199 	if (master_vld && (master != adap->pf)) {
1200 		dev_warn(adap->pdev_dev,
1201 			 "cxgb4 driver needs to be loaded as MASTER to support FW flash\n");
1202 		return -EOPNOTSUPP;
1203 	}
1204 
1205 	ef->data[sizeof(ef->data) - 1] = '\0';
1206 	ret = request_firmware(&fw, ef->data, adap->pdev_dev);
1207 	if (ret < 0)
1208 		return ret;
1209 
1210 	/* If the adapter has been fully initialized then we'll go ahead and
1211 	 * try to get the firmware's cooperation in upgrading to the new
1212 	 * firmware image otherwise we'll try to do the entire job from the
1213 	 * host ... and we always "force" the operation in this path.
1214 	 */
1215 	if (adap->flags & FULL_INIT_DONE)
1216 		mbox = adap->mbox;
1217 
1218 	ret = t4_fw_upgrade(adap, mbox, fw->data, fw->size, 1);
1219 	release_firmware(fw);
1220 	if (!ret)
1221 		dev_info(adap->pdev_dev,
1222 			 "loaded firmware %s, reload cxgb4 driver\n", ef->data);
1223 	return ret;
1224 }
1225 
1226 static int get_ts_info(struct net_device *dev, struct ethtool_ts_info *ts_info)
1227 {
1228 	struct port_info *pi = netdev_priv(dev);
1229 	struct  adapter *adapter = pi->adapter;
1230 
1231 	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1232 				   SOF_TIMESTAMPING_RX_SOFTWARE |
1233 				   SOF_TIMESTAMPING_SOFTWARE;
1234 
1235 	ts_info->so_timestamping |= SOF_TIMESTAMPING_RX_HARDWARE |
1236 				    SOF_TIMESTAMPING_TX_HARDWARE |
1237 				    SOF_TIMESTAMPING_RAW_HARDWARE;
1238 
1239 	ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1240 			    (1 << HWTSTAMP_TX_ON);
1241 
1242 	ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1243 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1244 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1245 			      (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1246 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1247 			      (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
1248 
1249 	if (adapter->ptp_clock)
1250 		ts_info->phc_index = ptp_clock_index(adapter->ptp_clock);
1251 	else
1252 		ts_info->phc_index = -1;
1253 
1254 	return 0;
1255 }
1256 
1257 static u32 get_rss_table_size(struct net_device *dev)
1258 {
1259 	const struct port_info *pi = netdev_priv(dev);
1260 
1261 	return pi->rss_size;
1262 }
1263 
1264 static int get_rss_table(struct net_device *dev, u32 *p, u8 *key, u8 *hfunc)
1265 {
1266 	const struct port_info *pi = netdev_priv(dev);
1267 	unsigned int n = pi->rss_size;
1268 
1269 	if (hfunc)
1270 		*hfunc = ETH_RSS_HASH_TOP;
1271 	if (!p)
1272 		return 0;
1273 	while (n--)
1274 		p[n] = pi->rss[n];
1275 	return 0;
1276 }
1277 
1278 static int set_rss_table(struct net_device *dev, const u32 *p, const u8 *key,
1279 			 const u8 hfunc)
1280 {
1281 	unsigned int i;
1282 	struct port_info *pi = netdev_priv(dev);
1283 
1284 	/* We require at least one supported parameter to be changed and no
1285 	 * change in any of the unsupported parameters
1286 	 */
1287 	if (key ||
1288 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1289 		return -EOPNOTSUPP;
1290 	if (!p)
1291 		return 0;
1292 
1293 	/* Interface must be brought up atleast once */
1294 	if (pi->adapter->flags & FULL_INIT_DONE) {
1295 		for (i = 0; i < pi->rss_size; i++)
1296 			pi->rss[i] = p[i];
1297 
1298 		return cxgb4_write_rss(pi, pi->rss);
1299 	}
1300 
1301 	return -EPERM;
1302 }
1303 
1304 static int get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1305 		     u32 *rules)
1306 {
1307 	const struct port_info *pi = netdev_priv(dev);
1308 
1309 	switch (info->cmd) {
1310 	case ETHTOOL_GRXFH: {
1311 		unsigned int v = pi->rss_mode;
1312 
1313 		info->data = 0;
1314 		switch (info->flow_type) {
1315 		case TCP_V4_FLOW:
1316 			if (v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F)
1317 				info->data = RXH_IP_SRC | RXH_IP_DST |
1318 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1319 			else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1320 				info->data = RXH_IP_SRC | RXH_IP_DST;
1321 			break;
1322 		case UDP_V4_FLOW:
1323 			if ((v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) &&
1324 			    (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
1325 				info->data = RXH_IP_SRC | RXH_IP_DST |
1326 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1327 			else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1328 				info->data = RXH_IP_SRC | RXH_IP_DST;
1329 			break;
1330 		case SCTP_V4_FLOW:
1331 		case AH_ESP_V4_FLOW:
1332 		case IPV4_FLOW:
1333 			if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1334 				info->data = RXH_IP_SRC | RXH_IP_DST;
1335 			break;
1336 		case TCP_V6_FLOW:
1337 			if (v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F)
1338 				info->data = RXH_IP_SRC | RXH_IP_DST |
1339 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1340 			else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1341 				info->data = RXH_IP_SRC | RXH_IP_DST;
1342 			break;
1343 		case UDP_V6_FLOW:
1344 			if ((v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) &&
1345 			    (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
1346 				info->data = RXH_IP_SRC | RXH_IP_DST |
1347 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
1348 			else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1349 				info->data = RXH_IP_SRC | RXH_IP_DST;
1350 			break;
1351 		case SCTP_V6_FLOW:
1352 		case AH_ESP_V6_FLOW:
1353 		case IPV6_FLOW:
1354 			if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1355 				info->data = RXH_IP_SRC | RXH_IP_DST;
1356 			break;
1357 		}
1358 		return 0;
1359 	}
1360 	case ETHTOOL_GRXRINGS:
1361 		info->data = pi->nqsets;
1362 		return 0;
1363 	}
1364 	return -EOPNOTSUPP;
1365 }
1366 
1367 static int set_dump(struct net_device *dev, struct ethtool_dump *eth_dump)
1368 {
1369 	struct adapter *adapter = netdev2adap(dev);
1370 	u32 len = 0;
1371 
1372 	len = sizeof(struct cudbg_hdr) +
1373 	      sizeof(struct cudbg_entity_hdr) * CUDBG_MAX_ENTITY;
1374 	len += cxgb4_get_dump_length(adapter, eth_dump->flag);
1375 
1376 	adapter->eth_dump.flag = eth_dump->flag;
1377 	adapter->eth_dump.len = len;
1378 	return 0;
1379 }
1380 
1381 static int get_dump_flag(struct net_device *dev, struct ethtool_dump *eth_dump)
1382 {
1383 	struct adapter *adapter = netdev2adap(dev);
1384 
1385 	eth_dump->flag = adapter->eth_dump.flag;
1386 	eth_dump->len = adapter->eth_dump.len;
1387 	eth_dump->version = adapter->eth_dump.version;
1388 	return 0;
1389 }
1390 
1391 static int get_dump_data(struct net_device *dev, struct ethtool_dump *eth_dump,
1392 			 void *buf)
1393 {
1394 	struct adapter *adapter = netdev2adap(dev);
1395 	u32 len = 0;
1396 	int ret = 0;
1397 
1398 	if (adapter->eth_dump.flag == CXGB4_ETH_DUMP_NONE)
1399 		return -ENOENT;
1400 
1401 	len = sizeof(struct cudbg_hdr) +
1402 	      sizeof(struct cudbg_entity_hdr) * CUDBG_MAX_ENTITY;
1403 	len += cxgb4_get_dump_length(adapter, adapter->eth_dump.flag);
1404 	if (eth_dump->len < len)
1405 		return -ENOMEM;
1406 
1407 	ret = cxgb4_cudbg_collect(adapter, buf, &len, adapter->eth_dump.flag);
1408 	if (ret)
1409 		return ret;
1410 
1411 	eth_dump->flag = adapter->eth_dump.flag;
1412 	eth_dump->len = len;
1413 	eth_dump->version = adapter->eth_dump.version;
1414 	return 0;
1415 }
1416 
1417 static int cxgb4_get_module_info(struct net_device *dev,
1418 				 struct ethtool_modinfo *modinfo)
1419 {
1420 	struct port_info *pi = netdev_priv(dev);
1421 	u8 sff8472_comp, sff_diag_type, sff_rev;
1422 	struct adapter *adapter = pi->adapter;
1423 	int ret;
1424 
1425 	if (!t4_is_inserted_mod_type(pi->mod_type))
1426 		return -EINVAL;
1427 
1428 	switch (pi->port_type) {
1429 	case FW_PORT_TYPE_SFP:
1430 	case FW_PORT_TYPE_QSA:
1431 	case FW_PORT_TYPE_SFP28:
1432 		ret = t4_i2c_rd(adapter, adapter->mbox, pi->tx_chan,
1433 				I2C_DEV_ADDR_A0, SFF_8472_COMP_ADDR,
1434 				SFF_8472_COMP_LEN, &sff8472_comp);
1435 		if (ret)
1436 			return ret;
1437 		ret = t4_i2c_rd(adapter, adapter->mbox, pi->tx_chan,
1438 				I2C_DEV_ADDR_A0, SFP_DIAG_TYPE_ADDR,
1439 				SFP_DIAG_TYPE_LEN, &sff_diag_type);
1440 		if (ret)
1441 			return ret;
1442 
1443 		if (!sff8472_comp || (sff_diag_type & 4)) {
1444 			modinfo->type = ETH_MODULE_SFF_8079;
1445 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1446 		} else {
1447 			modinfo->type = ETH_MODULE_SFF_8472;
1448 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1449 		}
1450 		break;
1451 
1452 	case FW_PORT_TYPE_QSFP:
1453 	case FW_PORT_TYPE_QSFP_10G:
1454 	case FW_PORT_TYPE_CR_QSFP:
1455 	case FW_PORT_TYPE_CR2_QSFP:
1456 	case FW_PORT_TYPE_CR4_QSFP:
1457 		ret = t4_i2c_rd(adapter, adapter->mbox, pi->tx_chan,
1458 				I2C_DEV_ADDR_A0, SFF_REV_ADDR,
1459 				SFF_REV_LEN, &sff_rev);
1460 		/* For QSFP type ports, revision value >= 3
1461 		 * means the SFP is 8636 compliant.
1462 		 */
1463 		if (ret)
1464 			return ret;
1465 		if (sff_rev >= 0x3) {
1466 			modinfo->type = ETH_MODULE_SFF_8636;
1467 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1468 		} else {
1469 			modinfo->type = ETH_MODULE_SFF_8436;
1470 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1471 		}
1472 		break;
1473 
1474 	default:
1475 		return -EINVAL;
1476 	}
1477 
1478 	return 0;
1479 }
1480 
1481 static int cxgb4_get_module_eeprom(struct net_device *dev,
1482 				   struct ethtool_eeprom *eprom, u8 *data)
1483 {
1484 	int ret = 0, offset = eprom->offset, len = eprom->len;
1485 	struct port_info *pi = netdev_priv(dev);
1486 	struct adapter *adapter = pi->adapter;
1487 
1488 	memset(data, 0, eprom->len);
1489 	if (offset + len <= I2C_PAGE_SIZE)
1490 		return t4_i2c_rd(adapter, adapter->mbox, pi->tx_chan,
1491 				 I2C_DEV_ADDR_A0, offset, len, data);
1492 
1493 	/* offset + len spans 0xa0 and 0xa1 pages */
1494 	if (offset <= I2C_PAGE_SIZE) {
1495 		/* read 0xa0 page */
1496 		len = I2C_PAGE_SIZE - offset;
1497 		ret =  t4_i2c_rd(adapter, adapter->mbox, pi->tx_chan,
1498 				 I2C_DEV_ADDR_A0, offset, len, data);
1499 		if (ret)
1500 			return ret;
1501 		offset = I2C_PAGE_SIZE;
1502 		/* Remaining bytes to be read from second page =
1503 		 * Total length - bytes read from first page
1504 		 */
1505 		len = eprom->len - len;
1506 	}
1507 	/* Read additional optical diagnostics from page 0xa2 if supported */
1508 	return t4_i2c_rd(adapter, adapter->mbox, pi->tx_chan, I2C_DEV_ADDR_A2,
1509 			 offset, len, &data[eprom->len - len]);
1510 }
1511 
1512 static u32 cxgb4_get_priv_flags(struct net_device *netdev)
1513 {
1514 	struct port_info *pi = netdev_priv(netdev);
1515 	struct adapter *adapter = pi->adapter;
1516 
1517 	return (adapter->eth_flags | pi->eth_flags);
1518 }
1519 
1520 /**
1521  *	set_flags - set/unset specified flags if passed in new_flags
1522  *	@cur_flags: pointer to current flags
1523  *	@new_flags: new incoming flags
1524  *	@flags: set of flags to set/unset
1525  */
1526 static inline void set_flags(u32 *cur_flags, u32 new_flags, u32 flags)
1527 {
1528 	*cur_flags = (*cur_flags & ~flags) | (new_flags & flags);
1529 }
1530 
1531 static int cxgb4_set_priv_flags(struct net_device *netdev, u32 flags)
1532 {
1533 	struct port_info *pi = netdev_priv(netdev);
1534 	struct adapter *adapter = pi->adapter;
1535 
1536 	set_flags(&adapter->eth_flags, flags, PRIV_FLAGS_ADAP);
1537 	set_flags(&pi->eth_flags, flags, PRIV_FLAGS_PORT);
1538 
1539 	return 0;
1540 }
1541 
1542 static const struct ethtool_ops cxgb_ethtool_ops = {
1543 	.get_link_ksettings = get_link_ksettings,
1544 	.set_link_ksettings = set_link_ksettings,
1545 	.get_fecparam      = get_fecparam,
1546 	.set_fecparam      = set_fecparam,
1547 	.get_drvinfo       = get_drvinfo,
1548 	.get_msglevel      = get_msglevel,
1549 	.set_msglevel      = set_msglevel,
1550 	.get_ringparam     = get_sge_param,
1551 	.set_ringparam     = set_sge_param,
1552 	.get_coalesce      = get_coalesce,
1553 	.set_coalesce      = set_coalesce,
1554 	.get_eeprom_len    = get_eeprom_len,
1555 	.get_eeprom        = get_eeprom,
1556 	.set_eeprom        = set_eeprom,
1557 	.get_pauseparam    = get_pauseparam,
1558 	.set_pauseparam    = set_pauseparam,
1559 	.get_link          = ethtool_op_get_link,
1560 	.get_strings       = get_strings,
1561 	.set_phys_id       = identify_port,
1562 	.nway_reset        = restart_autoneg,
1563 	.get_sset_count    = get_sset_count,
1564 	.get_ethtool_stats = get_stats,
1565 	.get_regs_len      = get_regs_len,
1566 	.get_regs          = get_regs,
1567 	.get_rxnfc         = get_rxnfc,
1568 	.get_rxfh_indir_size = get_rss_table_size,
1569 	.get_rxfh	   = get_rss_table,
1570 	.set_rxfh	   = set_rss_table,
1571 	.flash_device      = set_flash,
1572 	.get_ts_info       = get_ts_info,
1573 	.set_dump          = set_dump,
1574 	.get_dump_flag     = get_dump_flag,
1575 	.get_dump_data     = get_dump_data,
1576 	.get_module_info   = cxgb4_get_module_info,
1577 	.get_module_eeprom = cxgb4_get_module_eeprom,
1578 	.get_priv_flags    = cxgb4_get_priv_flags,
1579 	.set_priv_flags    = cxgb4_set_priv_flags,
1580 };
1581 
1582 void cxgb4_set_ethtool_ops(struct net_device *netdev)
1583 {
1584 	netdev->ethtool_ops = &cxgb_ethtool_ops;
1585 }
1586