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