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