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 	"TxOctetsOK         ",
39 	"TxFramesOK         ",
40 	"TxBroadcastFrames  ",
41 	"TxMulticastFrames  ",
42 	"TxUnicastFrames    ",
43 	"TxErrorFrames      ",
44 
45 	"TxFrames64         ",
46 	"TxFrames65To127    ",
47 	"TxFrames128To255   ",
48 	"TxFrames256To511   ",
49 	"TxFrames512To1023  ",
50 	"TxFrames1024To1518 ",
51 	"TxFrames1519ToMax  ",
52 
53 	"TxFramesDropped    ",
54 	"TxPauseFrames      ",
55 	"TxPPP0Frames       ",
56 	"TxPPP1Frames       ",
57 	"TxPPP2Frames       ",
58 	"TxPPP3Frames       ",
59 	"TxPPP4Frames       ",
60 	"TxPPP5Frames       ",
61 	"TxPPP6Frames       ",
62 	"TxPPP7Frames       ",
63 
64 	"RxOctetsOK         ",
65 	"RxFramesOK         ",
66 	"RxBroadcastFrames  ",
67 	"RxMulticastFrames  ",
68 	"RxUnicastFrames    ",
69 
70 	"RxFramesTooLong    ",
71 	"RxJabberErrors     ",
72 	"RxFCSErrors        ",
73 	"RxLengthErrors     ",
74 	"RxSymbolErrors     ",
75 	"RxRuntFrames       ",
76 
77 	"RxFrames64         ",
78 	"RxFrames65To127    ",
79 	"RxFrames128To255   ",
80 	"RxFrames256To511   ",
81 	"RxFrames512To1023  ",
82 	"RxFrames1024To1518 ",
83 	"RxFrames1519ToMax  ",
84 
85 	"RxPauseFrames      ",
86 	"RxPPP0Frames       ",
87 	"RxPPP1Frames       ",
88 	"RxPPP2Frames       ",
89 	"RxPPP3Frames       ",
90 	"RxPPP4Frames       ",
91 	"RxPPP5Frames       ",
92 	"RxPPP6Frames       ",
93 	"RxPPP7Frames       ",
94 
95 	"RxBG0FramesDropped ",
96 	"RxBG1FramesDropped ",
97 	"RxBG2FramesDropped ",
98 	"RxBG3FramesDropped ",
99 	"RxBG0FramesTrunc   ",
100 	"RxBG1FramesTrunc   ",
101 	"RxBG2FramesTrunc   ",
102 	"RxBG3FramesTrunc   ",
103 
104 	"TSO                ",
105 	"TxCsumOffload      ",
106 	"RxCsumGood         ",
107 	"VLANextractions    ",
108 	"VLANinsertions     ",
109 	"GROpackets         ",
110 	"GROmerged          ",
111 	"WriteCoalSuccess   ",
112 	"WriteCoalFail      ",
113 };
114 
115 static int get_sset_count(struct net_device *dev, int sset)
116 {
117 	switch (sset) {
118 	case ETH_SS_STATS:
119 		return ARRAY_SIZE(stats_strings);
120 	default:
121 		return -EOPNOTSUPP;
122 	}
123 }
124 
125 static int get_regs_len(struct net_device *dev)
126 {
127 	struct adapter *adap = netdev2adap(dev);
128 
129 	return t4_get_regs_len(adap);
130 }
131 
132 static int get_eeprom_len(struct net_device *dev)
133 {
134 	return EEPROMSIZE;
135 }
136 
137 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
138 {
139 	struct adapter *adapter = netdev2adap(dev);
140 	u32 exprom_vers;
141 
142 	strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
143 	strlcpy(info->version, cxgb4_driver_version,
144 		sizeof(info->version));
145 	strlcpy(info->bus_info, pci_name(adapter->pdev),
146 		sizeof(info->bus_info));
147 
148 	if (adapter->params.fw_vers)
149 		snprintf(info->fw_version, sizeof(info->fw_version),
150 			 "%u.%u.%u.%u, TP %u.%u.%u.%u",
151 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
152 			 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
153 			 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
154 			 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers),
155 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
156 			 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
157 			 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
158 			 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
159 
160 	if (!t4_get_exprom_version(adapter, &exprom_vers))
161 		snprintf(info->erom_version, sizeof(info->erom_version),
162 			 "%u.%u.%u.%u",
163 			 FW_HDR_FW_VER_MAJOR_G(exprom_vers),
164 			 FW_HDR_FW_VER_MINOR_G(exprom_vers),
165 			 FW_HDR_FW_VER_MICRO_G(exprom_vers),
166 			 FW_HDR_FW_VER_BUILD_G(exprom_vers));
167 }
168 
169 static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
170 {
171 	if (stringset == ETH_SS_STATS)
172 		memcpy(data, stats_strings, sizeof(stats_strings));
173 }
174 
175 /* port stats maintained per queue of the port. They should be in the same
176  * order as in stats_strings above.
177  */
178 struct queue_port_stats {
179 	u64 tso;
180 	u64 tx_csum;
181 	u64 rx_csum;
182 	u64 vlan_ex;
183 	u64 vlan_ins;
184 	u64 gro_pkts;
185 	u64 gro_merged;
186 };
187 
188 static void collect_sge_port_stats(const struct adapter *adap,
189 				   const struct port_info *p,
190 				   struct queue_port_stats *s)
191 {
192 	int i;
193 	const struct sge_eth_txq *tx = &adap->sge.ethtxq[p->first_qset];
194 	const struct sge_eth_rxq *rx = &adap->sge.ethrxq[p->first_qset];
195 
196 	memset(s, 0, sizeof(*s));
197 	for (i = 0; i < p->nqsets; i++, rx++, tx++) {
198 		s->tso += tx->tso;
199 		s->tx_csum += tx->tx_cso;
200 		s->rx_csum += rx->stats.rx_cso;
201 		s->vlan_ex += rx->stats.vlan_ex;
202 		s->vlan_ins += tx->vlan_ins;
203 		s->gro_pkts += rx->stats.lro_pkts;
204 		s->gro_merged += rx->stats.lro_merged;
205 	}
206 }
207 
208 static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
209 		      u64 *data)
210 {
211 	struct port_info *pi = netdev_priv(dev);
212 	struct adapter *adapter = pi->adapter;
213 	u32 val1, val2;
214 
215 	t4_get_port_stats(adapter, pi->tx_chan, (struct port_stats *)data);
216 
217 	data += sizeof(struct port_stats) / sizeof(u64);
218 	collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
219 	data += sizeof(struct queue_port_stats) / sizeof(u64);
220 	if (!is_t4(adapter->params.chip)) {
221 		t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7));
222 		val1 = t4_read_reg(adapter, SGE_STAT_TOTAL_A);
223 		val2 = t4_read_reg(adapter, SGE_STAT_MATCH_A);
224 		*data = val1 - val2;
225 		data++;
226 		*data = val2;
227 		data++;
228 	} else {
229 		memset(data, 0, 2 * sizeof(u64));
230 		*data += 2;
231 	}
232 }
233 
234 static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
235 		     void *buf)
236 {
237 	struct adapter *adap = netdev2adap(dev);
238 	size_t buf_size;
239 
240 	buf_size = t4_get_regs_len(adap);
241 	regs->version = mk_adap_vers(adap);
242 	t4_get_regs(adap, buf, buf_size);
243 }
244 
245 static int restart_autoneg(struct net_device *dev)
246 {
247 	struct port_info *p = netdev_priv(dev);
248 
249 	if (!netif_running(dev))
250 		return -EAGAIN;
251 	if (p->link_cfg.autoneg != AUTONEG_ENABLE)
252 		return -EINVAL;
253 	t4_restart_aneg(p->adapter, p->adapter->fn, p->tx_chan);
254 	return 0;
255 }
256 
257 static int identify_port(struct net_device *dev,
258 			 enum ethtool_phys_id_state state)
259 {
260 	unsigned int val;
261 	struct adapter *adap = netdev2adap(dev);
262 
263 	if (state == ETHTOOL_ID_ACTIVE)
264 		val = 0xffff;
265 	else if (state == ETHTOOL_ID_INACTIVE)
266 		val = 0;
267 	else
268 		return -EINVAL;
269 
270 	return t4_identify_port(adap, adap->fn, netdev2pinfo(dev)->viid, val);
271 }
272 
273 static unsigned int from_fw_linkcaps(enum fw_port_type type, unsigned int caps)
274 {
275 	unsigned int v = 0;
276 
277 	if (type == FW_PORT_TYPE_BT_SGMII || type == FW_PORT_TYPE_BT_XFI ||
278 	    type == FW_PORT_TYPE_BT_XAUI) {
279 		v |= SUPPORTED_TP;
280 		if (caps & FW_PORT_CAP_SPEED_100M)
281 			v |= SUPPORTED_100baseT_Full;
282 		if (caps & FW_PORT_CAP_SPEED_1G)
283 			v |= SUPPORTED_1000baseT_Full;
284 		if (caps & FW_PORT_CAP_SPEED_10G)
285 			v |= SUPPORTED_10000baseT_Full;
286 	} else if (type == FW_PORT_TYPE_KX4 || type == FW_PORT_TYPE_KX) {
287 		v |= SUPPORTED_Backplane;
288 		if (caps & FW_PORT_CAP_SPEED_1G)
289 			v |= SUPPORTED_1000baseKX_Full;
290 		if (caps & FW_PORT_CAP_SPEED_10G)
291 			v |= SUPPORTED_10000baseKX4_Full;
292 	} else if (type == FW_PORT_TYPE_KR) {
293 		v |= SUPPORTED_Backplane | SUPPORTED_10000baseKR_Full;
294 	} else if (type == FW_PORT_TYPE_BP_AP) {
295 		v |= SUPPORTED_Backplane | SUPPORTED_10000baseR_FEC |
296 		     SUPPORTED_10000baseKR_Full | SUPPORTED_1000baseKX_Full;
297 	} else if (type == FW_PORT_TYPE_BP4_AP) {
298 		v |= SUPPORTED_Backplane | SUPPORTED_10000baseR_FEC |
299 		     SUPPORTED_10000baseKR_Full | SUPPORTED_1000baseKX_Full |
300 		     SUPPORTED_10000baseKX4_Full;
301 	} else if (type == FW_PORT_TYPE_FIBER_XFI ||
302 		   type == FW_PORT_TYPE_FIBER_XAUI ||
303 		   type == FW_PORT_TYPE_SFP ||
304 		   type == FW_PORT_TYPE_QSFP_10G ||
305 		   type == FW_PORT_TYPE_QSA) {
306 		v |= SUPPORTED_FIBRE;
307 		if (caps & FW_PORT_CAP_SPEED_1G)
308 			v |= SUPPORTED_1000baseT_Full;
309 		if (caps & FW_PORT_CAP_SPEED_10G)
310 			v |= SUPPORTED_10000baseT_Full;
311 	} else if (type == FW_PORT_TYPE_BP40_BA ||
312 		   type == FW_PORT_TYPE_QSFP) {
313 		v |= SUPPORTED_40000baseSR4_Full;
314 		v |= SUPPORTED_FIBRE;
315 	}
316 
317 	if (caps & FW_PORT_CAP_ANEG)
318 		v |= SUPPORTED_Autoneg;
319 	return v;
320 }
321 
322 static unsigned int to_fw_linkcaps(unsigned int caps)
323 {
324 	unsigned int v = 0;
325 
326 	if (caps & ADVERTISED_100baseT_Full)
327 		v |= FW_PORT_CAP_SPEED_100M;
328 	if (caps & ADVERTISED_1000baseT_Full)
329 		v |= FW_PORT_CAP_SPEED_1G;
330 	if (caps & ADVERTISED_10000baseT_Full)
331 		v |= FW_PORT_CAP_SPEED_10G;
332 	if (caps & ADVERTISED_40000baseSR4_Full)
333 		v |= FW_PORT_CAP_SPEED_40G;
334 	return v;
335 }
336 
337 static int get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
338 {
339 	const struct port_info *p = netdev_priv(dev);
340 
341 	if (p->port_type == FW_PORT_TYPE_BT_SGMII ||
342 	    p->port_type == FW_PORT_TYPE_BT_XFI ||
343 	    p->port_type == FW_PORT_TYPE_BT_XAUI) {
344 		cmd->port = PORT_TP;
345 	} else if (p->port_type == FW_PORT_TYPE_FIBER_XFI ||
346 		   p->port_type == FW_PORT_TYPE_FIBER_XAUI) {
347 		cmd->port = PORT_FIBRE;
348 	} else if (p->port_type == FW_PORT_TYPE_SFP ||
349 		   p->port_type == FW_PORT_TYPE_QSFP_10G ||
350 		   p->port_type == FW_PORT_TYPE_QSA ||
351 		   p->port_type == FW_PORT_TYPE_QSFP) {
352 		if (p->mod_type == FW_PORT_MOD_TYPE_LR ||
353 		    p->mod_type == FW_PORT_MOD_TYPE_SR ||
354 		    p->mod_type == FW_PORT_MOD_TYPE_ER ||
355 		    p->mod_type == FW_PORT_MOD_TYPE_LRM)
356 			cmd->port = PORT_FIBRE;
357 		else if (p->mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
358 			 p->mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
359 			cmd->port = PORT_DA;
360 		else
361 			cmd->port = PORT_OTHER;
362 	} else {
363 		cmd->port = PORT_OTHER;
364 	}
365 
366 	if (p->mdio_addr >= 0) {
367 		cmd->phy_address = p->mdio_addr;
368 		cmd->transceiver = XCVR_EXTERNAL;
369 		cmd->mdio_support = p->port_type == FW_PORT_TYPE_BT_SGMII ?
370 			MDIO_SUPPORTS_C22 : MDIO_SUPPORTS_C45;
371 	} else {
372 		cmd->phy_address = 0;  /* not really, but no better option */
373 		cmd->transceiver = XCVR_INTERNAL;
374 		cmd->mdio_support = 0;
375 	}
376 
377 	cmd->supported = from_fw_linkcaps(p->port_type, p->link_cfg.supported);
378 	cmd->advertising = from_fw_linkcaps(p->port_type,
379 					    p->link_cfg.advertising);
380 	ethtool_cmd_speed_set(cmd,
381 			      netif_carrier_ok(dev) ? p->link_cfg.speed : 0);
382 	cmd->duplex = DUPLEX_FULL;
383 	cmd->autoneg = p->link_cfg.autoneg;
384 	cmd->maxtxpkt = 0;
385 	cmd->maxrxpkt = 0;
386 	return 0;
387 }
388 
389 static unsigned int speed_to_caps(int speed)
390 {
391 	if (speed == 100)
392 		return FW_PORT_CAP_SPEED_100M;
393 	if (speed == 1000)
394 		return FW_PORT_CAP_SPEED_1G;
395 	if (speed == 10000)
396 		return FW_PORT_CAP_SPEED_10G;
397 	if (speed == 40000)
398 		return FW_PORT_CAP_SPEED_40G;
399 	return 0;
400 }
401 
402 static int set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
403 {
404 	unsigned int cap;
405 	struct port_info *p = netdev_priv(dev);
406 	struct link_config *lc = &p->link_cfg;
407 	u32 speed = ethtool_cmd_speed(cmd);
408 
409 	if (cmd->duplex != DUPLEX_FULL)     /* only full-duplex supported */
410 		return -EINVAL;
411 
412 	if (!(lc->supported & FW_PORT_CAP_ANEG)) {
413 		/* PHY offers a single speed.  See if that's what's
414 		 * being requested.
415 		 */
416 		if (cmd->autoneg == AUTONEG_DISABLE &&
417 		    (lc->supported & speed_to_caps(speed)))
418 			return 0;
419 		return -EINVAL;
420 	}
421 
422 	if (cmd->autoneg == AUTONEG_DISABLE) {
423 		cap = speed_to_caps(speed);
424 
425 		if (!(lc->supported & cap) ||
426 		    (speed == 1000) ||
427 		    (speed == 10000) ||
428 		    (speed == 40000))
429 			return -EINVAL;
430 		lc->requested_speed = cap;
431 		lc->advertising = 0;
432 	} else {
433 		cap = to_fw_linkcaps(cmd->advertising);
434 		if (!(lc->supported & cap))
435 			return -EINVAL;
436 		lc->requested_speed = 0;
437 		lc->advertising = cap | FW_PORT_CAP_ANEG;
438 	}
439 	lc->autoneg = cmd->autoneg;
440 
441 	if (netif_running(dev))
442 		return t4_link_start(p->adapter, p->adapter->fn, p->tx_chan,
443 				     lc);
444 	return 0;
445 }
446 
447 static void get_pauseparam(struct net_device *dev,
448 			   struct ethtool_pauseparam *epause)
449 {
450 	struct port_info *p = netdev_priv(dev);
451 
452 	epause->autoneg = (p->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
453 	epause->rx_pause = (p->link_cfg.fc & PAUSE_RX) != 0;
454 	epause->tx_pause = (p->link_cfg.fc & PAUSE_TX) != 0;
455 }
456 
457 static int set_pauseparam(struct net_device *dev,
458 			  struct ethtool_pauseparam *epause)
459 {
460 	struct port_info *p = netdev_priv(dev);
461 	struct link_config *lc = &p->link_cfg;
462 
463 	if (epause->autoneg == AUTONEG_DISABLE)
464 		lc->requested_fc = 0;
465 	else if (lc->supported & FW_PORT_CAP_ANEG)
466 		lc->requested_fc = PAUSE_AUTONEG;
467 	else
468 		return -EINVAL;
469 
470 	if (epause->rx_pause)
471 		lc->requested_fc |= PAUSE_RX;
472 	if (epause->tx_pause)
473 		lc->requested_fc |= PAUSE_TX;
474 	if (netif_running(dev))
475 		return t4_link_start(p->adapter, p->adapter->fn, p->tx_chan,
476 				     lc);
477 	return 0;
478 }
479 
480 static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
481 {
482 	const struct port_info *pi = netdev_priv(dev);
483 	const struct sge *s = &pi->adapter->sge;
484 
485 	e->rx_max_pending = MAX_RX_BUFFERS;
486 	e->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
487 	e->rx_jumbo_max_pending = 0;
488 	e->tx_max_pending = MAX_TXQ_ENTRIES;
489 
490 	e->rx_pending = s->ethrxq[pi->first_qset].fl.size - 8;
491 	e->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
492 	e->rx_jumbo_pending = 0;
493 	e->tx_pending = s->ethtxq[pi->first_qset].q.size;
494 }
495 
496 static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
497 {
498 	int i;
499 	const struct port_info *pi = netdev_priv(dev);
500 	struct adapter *adapter = pi->adapter;
501 	struct sge *s = &adapter->sge;
502 
503 	if (e->rx_pending > MAX_RX_BUFFERS || e->rx_jumbo_pending ||
504 	    e->tx_pending > MAX_TXQ_ENTRIES ||
505 	    e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
506 	    e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
507 	    e->rx_pending < MIN_FL_ENTRIES || e->tx_pending < MIN_TXQ_ENTRIES)
508 		return -EINVAL;
509 
510 	if (adapter->flags & FULL_INIT_DONE)
511 		return -EBUSY;
512 
513 	for (i = 0; i < pi->nqsets; ++i) {
514 		s->ethtxq[pi->first_qset + i].q.size = e->tx_pending;
515 		s->ethrxq[pi->first_qset + i].fl.size = e->rx_pending + 8;
516 		s->ethrxq[pi->first_qset + i].rspq.size = e->rx_mini_pending;
517 	}
518 	return 0;
519 }
520 
521 /**
522  * set_rx_intr_params - set a net devices's RX interrupt holdoff paramete!
523  * @dev: the network device
524  * @us: the hold-off time in us, or 0 to disable timer
525  * @cnt: the hold-off packet count, or 0 to disable counter
526  *
527  * Set the RX interrupt hold-off parameters for a network device.
528  */
529 static int set_rx_intr_params(struct net_device *dev,
530 			      unsigned int us, unsigned int cnt)
531 {
532 	int i, err;
533 	struct port_info *pi = netdev_priv(dev);
534 	struct adapter *adap = pi->adapter;
535 	struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
536 
537 	for (i = 0; i < pi->nqsets; i++, q++) {
538 		err = cxgb4_set_rspq_intr_params(&q->rspq, us, cnt);
539 		if (err)
540 			return err;
541 	}
542 	return 0;
543 }
544 
545 static int set_adaptive_rx_setting(struct net_device *dev, int adaptive_rx)
546 {
547 	int i;
548 	struct port_info *pi = netdev_priv(dev);
549 	struct adapter *adap = pi->adapter;
550 	struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
551 
552 	for (i = 0; i < pi->nqsets; i++, q++)
553 		q->rspq.adaptive_rx = adaptive_rx;
554 
555 	return 0;
556 }
557 
558 static int get_adaptive_rx_setting(struct net_device *dev)
559 {
560 	struct port_info *pi = netdev_priv(dev);
561 	struct adapter *adap = pi->adapter;
562 	struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
563 
564 	return q->rspq.adaptive_rx;
565 }
566 
567 static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
568 {
569 	set_adaptive_rx_setting(dev, c->use_adaptive_rx_coalesce);
570 	return set_rx_intr_params(dev, c->rx_coalesce_usecs,
571 				  c->rx_max_coalesced_frames);
572 }
573 
574 static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
575 {
576 	const struct port_info *pi = netdev_priv(dev);
577 	const struct adapter *adap = pi->adapter;
578 	const struct sge_rspq *rq = &adap->sge.ethrxq[pi->first_qset].rspq;
579 
580 	c->rx_coalesce_usecs = qtimer_val(adap, rq);
581 	c->rx_max_coalesced_frames = (rq->intr_params & QINTR_CNT_EN) ?
582 		adap->sge.counter_val[rq->pktcnt_idx] : 0;
583 	c->use_adaptive_rx_coalesce = get_adaptive_rx_setting(dev);
584 	return 0;
585 }
586 
587 /**
588  *	eeprom_ptov - translate a physical EEPROM address to virtual
589  *	@phys_addr: the physical EEPROM address
590  *	@fn: the PCI function number
591  *	@sz: size of function-specific area
592  *
593  *	Translate a physical EEPROM address to virtual.  The first 1K is
594  *	accessed through virtual addresses starting at 31K, the rest is
595  *	accessed through virtual addresses starting at 0.
596  *
597  *	The mapping is as follows:
598  *	[0..1K) -> [31K..32K)
599  *	[1K..1K+A) -> [31K-A..31K)
600  *	[1K+A..ES) -> [0..ES-A-1K)
601  *
602  *	where A = @fn * @sz, and ES = EEPROM size.
603  */
604 static int eeprom_ptov(unsigned int phys_addr, unsigned int fn, unsigned int sz)
605 {
606 	fn *= sz;
607 	if (phys_addr < 1024)
608 		return phys_addr + (31 << 10);
609 	if (phys_addr < 1024 + fn)
610 		return 31744 - fn + phys_addr - 1024;
611 	if (phys_addr < EEPROMSIZE)
612 		return phys_addr - 1024 - fn;
613 	return -EINVAL;
614 }
615 
616 /* The next two routines implement eeprom read/write from physical addresses.
617  */
618 static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
619 {
620 	int vaddr = eeprom_ptov(phys_addr, adap->fn, EEPROMPFSIZE);
621 
622 	if (vaddr >= 0)
623 		vaddr = pci_read_vpd(adap->pdev, vaddr, sizeof(u32), v);
624 	return vaddr < 0 ? vaddr : 0;
625 }
626 
627 static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
628 {
629 	int vaddr = eeprom_ptov(phys_addr, adap->fn, EEPROMPFSIZE);
630 
631 	if (vaddr >= 0)
632 		vaddr = pci_write_vpd(adap->pdev, vaddr, sizeof(u32), &v);
633 	return vaddr < 0 ? vaddr : 0;
634 }
635 
636 #define EEPROM_MAGIC 0x38E2F10C
637 
638 static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
639 		      u8 *data)
640 {
641 	int i, err = 0;
642 	struct adapter *adapter = netdev2adap(dev);
643 	u8 *buf = kmalloc(EEPROMSIZE, GFP_KERNEL);
644 
645 	if (!buf)
646 		return -ENOMEM;
647 
648 	e->magic = EEPROM_MAGIC;
649 	for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
650 		err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
651 
652 	if (!err)
653 		memcpy(data, buf + e->offset, e->len);
654 	kfree(buf);
655 	return err;
656 }
657 
658 static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
659 		      u8 *data)
660 {
661 	u8 *buf;
662 	int err = 0;
663 	u32 aligned_offset, aligned_len, *p;
664 	struct adapter *adapter = netdev2adap(dev);
665 
666 	if (eeprom->magic != EEPROM_MAGIC)
667 		return -EINVAL;
668 
669 	aligned_offset = eeprom->offset & ~3;
670 	aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
671 
672 	if (adapter->fn > 0) {
673 		u32 start = 1024 + adapter->fn * EEPROMPFSIZE;
674 
675 		if (aligned_offset < start ||
676 		    aligned_offset + aligned_len > start + EEPROMPFSIZE)
677 			return -EPERM;
678 	}
679 
680 	if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
681 		/* RMW possibly needed for first or last words.
682 		 */
683 		buf = kmalloc(aligned_len, GFP_KERNEL);
684 		if (!buf)
685 			return -ENOMEM;
686 		err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
687 		if (!err && aligned_len > 4)
688 			err = eeprom_rd_phys(adapter,
689 					     aligned_offset + aligned_len - 4,
690 					     (u32 *)&buf[aligned_len - 4]);
691 		if (err)
692 			goto out;
693 		memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
694 	} else {
695 		buf = data;
696 	}
697 
698 	err = t4_seeprom_wp(adapter, false);
699 	if (err)
700 		goto out;
701 
702 	for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
703 		err = eeprom_wr_phys(adapter, aligned_offset, *p);
704 		aligned_offset += 4;
705 	}
706 
707 	if (!err)
708 		err = t4_seeprom_wp(adapter, true);
709 out:
710 	if (buf != data)
711 		kfree(buf);
712 	return err;
713 }
714 
715 static int set_flash(struct net_device *netdev, struct ethtool_flash *ef)
716 {
717 	int ret;
718 	const struct firmware *fw;
719 	struct adapter *adap = netdev2adap(netdev);
720 	unsigned int mbox = PCIE_FW_MASTER_M + 1;
721 
722 	ef->data[sizeof(ef->data) - 1] = '\0';
723 	ret = request_firmware(&fw, ef->data, adap->pdev_dev);
724 	if (ret < 0)
725 		return ret;
726 
727 	/* If the adapter has been fully initialized then we'll go ahead and
728 	 * try to get the firmware's cooperation in upgrading to the new
729 	 * firmware image otherwise we'll try to do the entire job from the
730 	 * host ... and we always "force" the operation in this path.
731 	 */
732 	if (adap->flags & FULL_INIT_DONE)
733 		mbox = adap->mbox;
734 
735 	ret = t4_fw_upgrade(adap, mbox, fw->data, fw->size, 1);
736 	release_firmware(fw);
737 	if (!ret)
738 		dev_info(adap->pdev_dev,
739 			 "loaded firmware %s, reload cxgb4 driver\n", ef->data);
740 	return ret;
741 }
742 
743 #define WOL_SUPPORTED (WAKE_BCAST | WAKE_MAGIC)
744 #define BCAST_CRC 0xa0ccc1a6
745 
746 static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
747 {
748 	wol->supported = WAKE_BCAST | WAKE_MAGIC;
749 	wol->wolopts = netdev2adap(dev)->wol;
750 	memset(&wol->sopass, 0, sizeof(wol->sopass));
751 }
752 
753 static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
754 {
755 	int err = 0;
756 	struct port_info *pi = netdev_priv(dev);
757 
758 	if (wol->wolopts & ~WOL_SUPPORTED)
759 		return -EINVAL;
760 	t4_wol_magic_enable(pi->adapter, pi->tx_chan,
761 			    (wol->wolopts & WAKE_MAGIC) ? dev->dev_addr : NULL);
762 	if (wol->wolopts & WAKE_BCAST) {
763 		err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0xfe, ~0ULL,
764 					~0ULL, 0, false);
765 		if (!err)
766 			err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 1,
767 						~6ULL, ~0ULL, BCAST_CRC, true);
768 	} else {
769 		t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0, 0, 0, 0, false);
770 	}
771 	return err;
772 }
773 
774 static u32 get_rss_table_size(struct net_device *dev)
775 {
776 	const struct port_info *pi = netdev_priv(dev);
777 
778 	return pi->rss_size;
779 }
780 
781 static int get_rss_table(struct net_device *dev, u32 *p, u8 *key, u8 *hfunc)
782 {
783 	const struct port_info *pi = netdev_priv(dev);
784 	unsigned int n = pi->rss_size;
785 
786 	if (hfunc)
787 		*hfunc = ETH_RSS_HASH_TOP;
788 	if (!p)
789 		return 0;
790 	while (n--)
791 		p[n] = pi->rss[n];
792 	return 0;
793 }
794 
795 static int set_rss_table(struct net_device *dev, const u32 *p, const u8 *key,
796 			 const u8 hfunc)
797 {
798 	unsigned int i;
799 	struct port_info *pi = netdev_priv(dev);
800 
801 	/* We require at least one supported parameter to be changed and no
802 	 * change in any of the unsupported parameters
803 	 */
804 	if (key ||
805 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
806 		return -EOPNOTSUPP;
807 	if (!p)
808 		return 0;
809 
810 	for (i = 0; i < pi->rss_size; i++)
811 		pi->rss[i] = p[i];
812 	if (pi->adapter->flags & FULL_INIT_DONE)
813 		return cxgb4_write_rss(pi, pi->rss);
814 	return 0;
815 }
816 
817 static int get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
818 		     u32 *rules)
819 {
820 	const struct port_info *pi = netdev_priv(dev);
821 
822 	switch (info->cmd) {
823 	case ETHTOOL_GRXFH: {
824 		unsigned int v = pi->rss_mode;
825 
826 		info->data = 0;
827 		switch (info->flow_type) {
828 		case TCP_V4_FLOW:
829 			if (v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F)
830 				info->data = RXH_IP_SRC | RXH_IP_DST |
831 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
832 			else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
833 				info->data = RXH_IP_SRC | RXH_IP_DST;
834 			break;
835 		case UDP_V4_FLOW:
836 			if ((v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) &&
837 			    (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
838 				info->data = RXH_IP_SRC | RXH_IP_DST |
839 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
840 			else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
841 				info->data = RXH_IP_SRC | RXH_IP_DST;
842 			break;
843 		case SCTP_V4_FLOW:
844 		case AH_ESP_V4_FLOW:
845 		case IPV4_FLOW:
846 			if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
847 				info->data = RXH_IP_SRC | RXH_IP_DST;
848 			break;
849 		case TCP_V6_FLOW:
850 			if (v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F)
851 				info->data = RXH_IP_SRC | RXH_IP_DST |
852 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
853 			else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
854 				info->data = RXH_IP_SRC | RXH_IP_DST;
855 			break;
856 		case UDP_V6_FLOW:
857 			if ((v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) &&
858 			    (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
859 				info->data = RXH_IP_SRC | RXH_IP_DST |
860 					     RXH_L4_B_0_1 | RXH_L4_B_2_3;
861 			else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
862 				info->data = RXH_IP_SRC | RXH_IP_DST;
863 			break;
864 		case SCTP_V6_FLOW:
865 		case AH_ESP_V6_FLOW:
866 		case IPV6_FLOW:
867 			if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
868 				info->data = RXH_IP_SRC | RXH_IP_DST;
869 			break;
870 		}
871 		return 0;
872 	}
873 	case ETHTOOL_GRXRINGS:
874 		info->data = pi->nqsets;
875 		return 0;
876 	}
877 	return -EOPNOTSUPP;
878 }
879 
880 static const struct ethtool_ops cxgb_ethtool_ops = {
881 	.get_settings      = get_settings,
882 	.set_settings      = set_settings,
883 	.get_drvinfo       = get_drvinfo,
884 	.get_msglevel      = get_msglevel,
885 	.set_msglevel      = set_msglevel,
886 	.get_ringparam     = get_sge_param,
887 	.set_ringparam     = set_sge_param,
888 	.get_coalesce      = get_coalesce,
889 	.set_coalesce      = set_coalesce,
890 	.get_eeprom_len    = get_eeprom_len,
891 	.get_eeprom        = get_eeprom,
892 	.set_eeprom        = set_eeprom,
893 	.get_pauseparam    = get_pauseparam,
894 	.set_pauseparam    = set_pauseparam,
895 	.get_link          = ethtool_op_get_link,
896 	.get_strings       = get_strings,
897 	.set_phys_id       = identify_port,
898 	.nway_reset        = restart_autoneg,
899 	.get_sset_count    = get_sset_count,
900 	.get_ethtool_stats = get_stats,
901 	.get_regs_len      = get_regs_len,
902 	.get_regs          = get_regs,
903 	.get_wol           = get_wol,
904 	.set_wol           = set_wol,
905 	.get_rxnfc         = get_rxnfc,
906 	.get_rxfh_indir_size = get_rss_table_size,
907 	.get_rxfh	   = get_rss_table,
908 	.set_rxfh	   = set_rss_table,
909 	.flash_device      = set_flash,
910 };
911 
912 void cxgb4_set_ethtool_ops(struct net_device *netdev)
913 {
914 	netdev->ethtool_ops = &cxgb_ethtool_ops;
915 }
916