1 /*
2  * Copyright (C) 2003 - 2009 NetXen, Inc.
3  * Copyright (C) 2009 - QLogic Corporation.
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * The full GNU General Public License is included in this distribution
20  * in the file called "COPYING".
21  *
22  */
23 
24 #include <linux/types.h>
25 #include <linux/delay.h>
26 #include <linux/pci.h>
27 #include <asm/io.h>
28 #include <linux/netdevice.h>
29 #include <linux/ethtool.h>
30 
31 #include "netxen_nic.h"
32 #include "netxen_nic_hw.h"
33 
34 struct netxen_nic_stats {
35 	char stat_string[ETH_GSTRING_LEN];
36 	int sizeof_stat;
37 	int stat_offset;
38 };
39 
40 #define NETXEN_NIC_STAT(m) sizeof(((struct netxen_adapter *)0)->m), \
41 			offsetof(struct netxen_adapter, m)
42 
43 #define NETXEN_NIC_PORT_WINDOW 0x10000
44 #define NETXEN_NIC_INVALID_DATA 0xDEADBEEF
45 
46 static const struct netxen_nic_stats netxen_nic_gstrings_stats[] = {
47 	{"xmit_called", NETXEN_NIC_STAT(stats.xmitcalled)},
48 	{"xmit_finished", NETXEN_NIC_STAT(stats.xmitfinished)},
49 	{"rx_dropped", NETXEN_NIC_STAT(stats.rxdropped)},
50 	{"tx_dropped", NETXEN_NIC_STAT(stats.txdropped)},
51 	{"csummed", NETXEN_NIC_STAT(stats.csummed)},
52 	{"rx_pkts", NETXEN_NIC_STAT(stats.rx_pkts)},
53 	{"lro_pkts", NETXEN_NIC_STAT(stats.lro_pkts)},
54 	{"rx_bytes", NETXEN_NIC_STAT(stats.rxbytes)},
55 	{"tx_bytes", NETXEN_NIC_STAT(stats.txbytes)},
56 };
57 
58 #define NETXEN_NIC_STATS_LEN	ARRAY_SIZE(netxen_nic_gstrings_stats)
59 
60 static const char netxen_nic_gstrings_test[][ETH_GSTRING_LEN] = {
61 	"Register_Test_on_offline",
62 	"Link_Test_on_offline"
63 };
64 
65 #define NETXEN_NIC_TEST_LEN	ARRAY_SIZE(netxen_nic_gstrings_test)
66 
67 #define NETXEN_NIC_REGS_COUNT 30
68 #define NETXEN_NIC_REGS_LEN (NETXEN_NIC_REGS_COUNT * sizeof(__le32))
69 #define NETXEN_MAX_EEPROM_LEN   1024
70 
71 static int netxen_nic_get_eeprom_len(struct net_device *dev)
72 {
73 	return NETXEN_FLASH_TOTAL_SIZE;
74 }
75 
76 static void
77 netxen_nic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
78 {
79 	struct netxen_adapter *adapter = netdev_priv(dev);
80 	u32 fw_major = 0;
81 	u32 fw_minor = 0;
82 	u32 fw_build = 0;
83 
84 	strlcpy(drvinfo->driver, netxen_nic_driver_name,
85 		sizeof(drvinfo->driver));
86 	strlcpy(drvinfo->version, NETXEN_NIC_LINUX_VERSIONID,
87 		sizeof(drvinfo->version));
88 	fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
89 	fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
90 	fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
91 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
92 		"%d.%d.%d", fw_major, fw_minor, fw_build);
93 
94 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
95 		sizeof(drvinfo->bus_info));
96 }
97 
98 static int
99 netxen_nic_get_link_ksettings(struct net_device *dev,
100 			      struct ethtool_link_ksettings *cmd)
101 {
102 	struct netxen_adapter *adapter = netdev_priv(dev);
103 	int check_sfp_module = 0;
104 	u32 supported, advertising;
105 
106 	/* read which mode */
107 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
108 		supported = (SUPPORTED_10baseT_Half |
109 				   SUPPORTED_10baseT_Full |
110 				   SUPPORTED_100baseT_Half |
111 				   SUPPORTED_100baseT_Full |
112 				   SUPPORTED_1000baseT_Half |
113 				   SUPPORTED_1000baseT_Full);
114 
115 		advertising = (ADVERTISED_100baseT_Half |
116 				     ADVERTISED_100baseT_Full |
117 				     ADVERTISED_1000baseT_Half |
118 				     ADVERTISED_1000baseT_Full);
119 
120 		cmd->base.port = PORT_TP;
121 
122 		cmd->base.speed = adapter->link_speed;
123 		cmd->base.duplex = adapter->link_duplex;
124 		cmd->base.autoneg = adapter->link_autoneg;
125 
126 	} else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
127 		u32 val;
128 
129 		val = NXRD32(adapter, NETXEN_PORT_MODE_ADDR);
130 		if (val == NETXEN_PORT_MODE_802_3_AP) {
131 			supported = SUPPORTED_1000baseT_Full;
132 			advertising = ADVERTISED_1000baseT_Full;
133 		} else {
134 			supported = SUPPORTED_10000baseT_Full;
135 			advertising = ADVERTISED_10000baseT_Full;
136 		}
137 
138 		if (netif_running(dev) && adapter->has_link_events) {
139 			cmd->base.speed = adapter->link_speed;
140 			cmd->base.autoneg = adapter->link_autoneg;
141 			cmd->base.duplex = adapter->link_duplex;
142 			goto skip;
143 		}
144 
145 		cmd->base.port = PORT_TP;
146 
147 		if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
148 			u16 pcifn = adapter->ahw.pci_func;
149 
150 			val = NXRD32(adapter, P3_LINK_SPEED_REG(pcifn));
151 			cmd->base.speed = P3_LINK_SPEED_MHZ *
152 				P3_LINK_SPEED_VAL(pcifn, val);
153 		} else
154 			cmd->base.speed = SPEED_10000;
155 
156 		cmd->base.duplex = DUPLEX_FULL;
157 		cmd->base.autoneg = AUTONEG_DISABLE;
158 	} else
159 		return -EIO;
160 
161 skip:
162 	cmd->base.phy_address = adapter->physical_port;
163 
164 	switch (adapter->ahw.board_type) {
165 	case NETXEN_BRDTYPE_P2_SB35_4G:
166 	case NETXEN_BRDTYPE_P2_SB31_2G:
167 	case NETXEN_BRDTYPE_P3_REF_QG:
168 	case NETXEN_BRDTYPE_P3_4_GB:
169 	case NETXEN_BRDTYPE_P3_4_GB_MM:
170 
171 		supported |= SUPPORTED_Autoneg;
172 		advertising |= ADVERTISED_Autoneg;
173 	case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
174 	case NETXEN_BRDTYPE_P3_10G_CX4:
175 	case NETXEN_BRDTYPE_P3_10G_CX4_LP:
176 	case NETXEN_BRDTYPE_P3_10000_BASE_T:
177 		supported |= SUPPORTED_TP;
178 		advertising |= ADVERTISED_TP;
179 		cmd->base.port = PORT_TP;
180 		cmd->base.autoneg = (adapter->ahw.board_type ==
181 				 NETXEN_BRDTYPE_P2_SB31_10G_CX4) ?
182 		    (AUTONEG_DISABLE) : (adapter->link_autoneg);
183 		break;
184 	case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
185 	case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
186 	case NETXEN_BRDTYPE_P3_IMEZ:
187 	case NETXEN_BRDTYPE_P3_XG_LOM:
188 	case NETXEN_BRDTYPE_P3_HMEZ:
189 		supported |= SUPPORTED_MII;
190 		advertising |= ADVERTISED_MII;
191 		cmd->base.port = PORT_MII;
192 		cmd->base.autoneg = AUTONEG_DISABLE;
193 		break;
194 	case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
195 	case NETXEN_BRDTYPE_P3_10G_SFP_CT:
196 	case NETXEN_BRDTYPE_P3_10G_SFP_QT:
197 		advertising |= ADVERTISED_TP;
198 		supported |= SUPPORTED_TP;
199 		check_sfp_module = netif_running(dev) &&
200 			adapter->has_link_events;
201 	case NETXEN_BRDTYPE_P2_SB31_10G:
202 	case NETXEN_BRDTYPE_P3_10G_XFP:
203 		supported |= SUPPORTED_FIBRE;
204 		advertising |= ADVERTISED_FIBRE;
205 		cmd->base.port = PORT_FIBRE;
206 		cmd->base.autoneg = AUTONEG_DISABLE;
207 		break;
208 	case NETXEN_BRDTYPE_P3_10G_TP:
209 		if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
210 			cmd->base.autoneg = AUTONEG_DISABLE;
211 			supported |= (SUPPORTED_FIBRE | SUPPORTED_TP);
212 			advertising |=
213 				(ADVERTISED_FIBRE | ADVERTISED_TP);
214 			cmd->base.port = PORT_FIBRE;
215 			check_sfp_module = netif_running(dev) &&
216 				adapter->has_link_events;
217 		} else {
218 			supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
219 			advertising |=
220 				(ADVERTISED_TP | ADVERTISED_Autoneg);
221 			cmd->base.port = PORT_TP;
222 		}
223 		break;
224 	default:
225 		printk(KERN_ERR "netxen-nic: Unsupported board model %d\n",
226 				adapter->ahw.board_type);
227 		return -EIO;
228 	}
229 
230 	if (check_sfp_module) {
231 		switch (adapter->module_type) {
232 		case LINKEVENT_MODULE_OPTICAL_UNKNOWN:
233 		case LINKEVENT_MODULE_OPTICAL_SRLR:
234 		case LINKEVENT_MODULE_OPTICAL_LRM:
235 		case LINKEVENT_MODULE_OPTICAL_SFP_1G:
236 			cmd->base.port = PORT_FIBRE;
237 			break;
238 		case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE:
239 		case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN:
240 		case LINKEVENT_MODULE_TWINAX:
241 			cmd->base.port = PORT_TP;
242 			break;
243 		default:
244 			cmd->base.port = -1;
245 		}
246 	}
247 
248 	if (!netif_running(dev) || !adapter->ahw.linkup) {
249 		cmd->base.duplex = DUPLEX_UNKNOWN;
250 		cmd->base.speed = SPEED_UNKNOWN;
251 	}
252 
253 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
254 						supported);
255 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
256 						advertising);
257 
258 	return 0;
259 }
260 
261 static int
262 netxen_nic_set_link_ksettings(struct net_device *dev,
263 			      const struct ethtool_link_ksettings *cmd)
264 {
265 	struct netxen_adapter *adapter = netdev_priv(dev);
266 	u32 speed = cmd->base.speed;
267 	int ret;
268 
269 	if (adapter->ahw.port_type != NETXEN_NIC_GBE)
270 		return -EOPNOTSUPP;
271 
272 	if (!(adapter->capabilities & NX_FW_CAPABILITY_GBE_LINK_CFG))
273 		return -EOPNOTSUPP;
274 
275 	ret = nx_fw_cmd_set_gbe_port(adapter, speed, cmd->base.duplex,
276 				     cmd->base.autoneg);
277 	if (ret == NX_RCODE_NOT_SUPPORTED)
278 		return -EOPNOTSUPP;
279 	else if (ret)
280 		return -EIO;
281 
282 	adapter->link_speed = speed;
283 	adapter->link_duplex = cmd->base.duplex;
284 	adapter->link_autoneg = cmd->base.autoneg;
285 
286 	if (!netif_running(dev))
287 		return 0;
288 
289 	dev->netdev_ops->ndo_stop(dev);
290 	return dev->netdev_ops->ndo_open(dev);
291 }
292 
293 static int netxen_nic_get_regs_len(struct net_device *dev)
294 {
295 	return NETXEN_NIC_REGS_LEN;
296 }
297 
298 static void
299 netxen_nic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
300 {
301 	struct netxen_adapter *adapter = netdev_priv(dev);
302 	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
303 	struct nx_host_sds_ring *sds_ring;
304 	u32 *regs_buff = p;
305 	int ring, i = 0;
306 	int port = adapter->physical_port;
307 
308 	memset(p, 0, NETXEN_NIC_REGS_LEN);
309 
310 	regs->version = (1 << 24) | (adapter->ahw.revision_id << 16) |
311 	    (adapter->pdev)->device;
312 
313 	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
314 		return;
315 
316 	regs_buff[i++] = NXRD32(adapter, CRB_CMDPEG_STATE);
317 	regs_buff[i++] = NXRD32(adapter, CRB_RCVPEG_STATE);
318 	regs_buff[i++] = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
319 	regs_buff[i++] = NXRDIO(adapter, adapter->crb_int_state_reg);
320 	regs_buff[i++] = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
321 	regs_buff[i++] = NXRD32(adapter, NX_CRB_DEV_STATE);
322 	regs_buff[i++] = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
323 	regs_buff[i++] = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
324 	regs_buff[i++] = NXRD32(adapter, NETXEN_PEG_HALT_STATUS2);
325 
326 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_0+0x3c);
327 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_1+0x3c);
328 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_2+0x3c);
329 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_3+0x3c);
330 
331 	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
332 
333 		regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_4+0x3c);
334 		i += 2;
335 
336 		regs_buff[i++] = NXRD32(adapter, CRB_XG_STATE_P3);
337 		regs_buff[i++] = le32_to_cpu(*(adapter->tx_ring->hw_consumer));
338 
339 	} else {
340 		i++;
341 
342 		regs_buff[i++] = NXRD32(adapter,
343 					NETXEN_NIU_XGE_CONFIG_0+(0x10000*port));
344 		regs_buff[i++] = NXRD32(adapter,
345 					NETXEN_NIU_XGE_CONFIG_1+(0x10000*port));
346 
347 		regs_buff[i++] = NXRD32(adapter, CRB_XG_STATE);
348 		regs_buff[i++] = NXRDIO(adapter,
349 				 adapter->tx_ring->crb_cmd_consumer);
350 	}
351 
352 	regs_buff[i++] = NXRDIO(adapter, adapter->tx_ring->crb_cmd_producer);
353 
354 	regs_buff[i++] = NXRDIO(adapter,
355 			 recv_ctx->rds_rings[0].crb_rcv_producer);
356 	regs_buff[i++] = NXRDIO(adapter,
357 			 recv_ctx->rds_rings[1].crb_rcv_producer);
358 
359 	regs_buff[i++] = adapter->max_sds_rings;
360 
361 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
362 		sds_ring = &(recv_ctx->sds_rings[ring]);
363 		regs_buff[i++] = NXRDIO(adapter,
364 					sds_ring->crb_sts_consumer);
365 	}
366 }
367 
368 static u32 netxen_nic_test_link(struct net_device *dev)
369 {
370 	struct netxen_adapter *adapter = netdev_priv(dev);
371 	u32 val, port;
372 
373 	port = adapter->physical_port;
374 	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
375 		val = NXRD32(adapter, CRB_XG_STATE_P3);
376 		val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
377 		return (val == XG_LINK_UP_P3) ? 0 : 1;
378 	} else {
379 		val = NXRD32(adapter, CRB_XG_STATE);
380 		val = (val >> port*8) & 0xff;
381 		return (val == XG_LINK_UP) ? 0 : 1;
382 	}
383 }
384 
385 static int
386 netxen_nic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
387 		      u8 *bytes)
388 {
389 	struct netxen_adapter *adapter = netdev_priv(dev);
390 	int offset;
391 	int ret;
392 
393 	if (eeprom->len == 0)
394 		return -EINVAL;
395 
396 	eeprom->magic = (adapter->pdev)->vendor |
397 			((adapter->pdev)->device << 16);
398 	offset = eeprom->offset;
399 
400 	ret = netxen_rom_fast_read_words(adapter, offset, bytes,
401 						eeprom->len);
402 	if (ret < 0)
403 		return ret;
404 
405 	return 0;
406 }
407 
408 static void
409 netxen_nic_get_ringparam(struct net_device *dev,
410 		struct ethtool_ringparam *ring)
411 {
412 	struct netxen_adapter *adapter = netdev_priv(dev);
413 
414 	ring->rx_pending = adapter->num_rxd;
415 	ring->rx_jumbo_pending = adapter->num_jumbo_rxd;
416 	ring->rx_jumbo_pending += adapter->num_lro_rxd;
417 	ring->tx_pending = adapter->num_txd;
418 
419 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
420 		ring->rx_max_pending = MAX_RCV_DESCRIPTORS_1G;
421 		ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_1G;
422 	} else {
423 		ring->rx_max_pending = MAX_RCV_DESCRIPTORS_10G;
424 		ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_10G;
425 	}
426 
427 	ring->tx_max_pending = MAX_CMD_DESCRIPTORS;
428 }
429 
430 static u32
431 netxen_validate_ringparam(u32 val, u32 min, u32 max, char *r_name)
432 {
433 	u32 num_desc;
434 	num_desc = max(val, min);
435 	num_desc = min(num_desc, max);
436 	num_desc = roundup_pow_of_two(num_desc);
437 
438 	if (val != num_desc) {
439 		printk(KERN_INFO "%s: setting %s ring size %d instead of %d\n",
440 		       netxen_nic_driver_name, r_name, num_desc, val);
441 	}
442 
443 	return num_desc;
444 }
445 
446 static int
447 netxen_nic_set_ringparam(struct net_device *dev,
448 		struct ethtool_ringparam *ring)
449 {
450 	struct netxen_adapter *adapter = netdev_priv(dev);
451 	u16 max_rcv_desc = MAX_RCV_DESCRIPTORS_10G;
452 	u16 max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
453 	u16 num_rxd, num_jumbo_rxd, num_txd;
454 
455 	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
456 		return -EOPNOTSUPP;
457 
458 	if (ring->rx_mini_pending)
459 		return -EOPNOTSUPP;
460 
461 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
462 		max_rcv_desc = MAX_RCV_DESCRIPTORS_1G;
463 		max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
464 	}
465 
466 	num_rxd = netxen_validate_ringparam(ring->rx_pending,
467 			MIN_RCV_DESCRIPTORS, max_rcv_desc, "rx");
468 
469 	num_jumbo_rxd = netxen_validate_ringparam(ring->rx_jumbo_pending,
470 			MIN_JUMBO_DESCRIPTORS, max_jumbo_desc, "rx jumbo");
471 
472 	num_txd = netxen_validate_ringparam(ring->tx_pending,
473 			MIN_CMD_DESCRIPTORS, MAX_CMD_DESCRIPTORS, "tx");
474 
475 	if (num_rxd == adapter->num_rxd && num_txd == adapter->num_txd &&
476 			num_jumbo_rxd == adapter->num_jumbo_rxd)
477 		return 0;
478 
479 	adapter->num_rxd = num_rxd;
480 	adapter->num_jumbo_rxd = num_jumbo_rxd;
481 	adapter->num_txd = num_txd;
482 
483 	return netxen_nic_reset_context(adapter);
484 }
485 
486 static void
487 netxen_nic_get_pauseparam(struct net_device *dev,
488 			  struct ethtool_pauseparam *pause)
489 {
490 	struct netxen_adapter *adapter = netdev_priv(dev);
491 	__u32 val;
492 	int port = adapter->physical_port;
493 
494 	pause->autoneg = 0;
495 
496 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
497 		if ((port < 0) || (port >= NETXEN_NIU_MAX_GBE_PORTS))
498 			return;
499 		/* get flow control settings */
500 		val = NXRD32(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port));
501 		pause->rx_pause = netxen_gb_get_rx_flowctl(val);
502 		val = NXRD32(adapter, NETXEN_NIU_GB_PAUSE_CTL);
503 		switch (port) {
504 		case 0:
505 			pause->tx_pause = !(netxen_gb_get_gb0_mask(val));
506 			break;
507 		case 1:
508 			pause->tx_pause = !(netxen_gb_get_gb1_mask(val));
509 			break;
510 		case 2:
511 			pause->tx_pause = !(netxen_gb_get_gb2_mask(val));
512 			break;
513 		case 3:
514 		default:
515 			pause->tx_pause = !(netxen_gb_get_gb3_mask(val));
516 			break;
517 		}
518 	} else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
519 		if ((port < 0) || (port >= NETXEN_NIU_MAX_XG_PORTS))
520 			return;
521 		pause->rx_pause = 1;
522 		val = NXRD32(adapter, NETXEN_NIU_XG_PAUSE_CTL);
523 		if (port == 0)
524 			pause->tx_pause = !(netxen_xg_get_xg0_mask(val));
525 		else
526 			pause->tx_pause = !(netxen_xg_get_xg1_mask(val));
527 	} else {
528 		printk(KERN_ERR"%s: Unknown board type: %x\n",
529 				netxen_nic_driver_name, adapter->ahw.port_type);
530 	}
531 }
532 
533 static int
534 netxen_nic_set_pauseparam(struct net_device *dev,
535 			  struct ethtool_pauseparam *pause)
536 {
537 	struct netxen_adapter *adapter = netdev_priv(dev);
538 	__u32 val;
539 	int port = adapter->physical_port;
540 
541 	/* not supported */
542 	if (pause->autoneg)
543 		return -EINVAL;
544 
545 	/* read mode */
546 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
547 		if ((port < 0) || (port >= NETXEN_NIU_MAX_GBE_PORTS))
548 			return -EIO;
549 		/* set flow control */
550 		val = NXRD32(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port));
551 
552 		if (pause->rx_pause)
553 			netxen_gb_rx_flowctl(val);
554 		else
555 			netxen_gb_unset_rx_flowctl(val);
556 
557 		NXWR32(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
558 				val);
559 		/* set autoneg */
560 		val = NXRD32(adapter, NETXEN_NIU_GB_PAUSE_CTL);
561 		switch (port) {
562 		case 0:
563 			if (pause->tx_pause)
564 				netxen_gb_unset_gb0_mask(val);
565 			else
566 				netxen_gb_set_gb0_mask(val);
567 			break;
568 		case 1:
569 			if (pause->tx_pause)
570 				netxen_gb_unset_gb1_mask(val);
571 			else
572 				netxen_gb_set_gb1_mask(val);
573 			break;
574 		case 2:
575 			if (pause->tx_pause)
576 				netxen_gb_unset_gb2_mask(val);
577 			else
578 				netxen_gb_set_gb2_mask(val);
579 			break;
580 		case 3:
581 		default:
582 			if (pause->tx_pause)
583 				netxen_gb_unset_gb3_mask(val);
584 			else
585 				netxen_gb_set_gb3_mask(val);
586 			break;
587 		}
588 		NXWR32(adapter, NETXEN_NIU_GB_PAUSE_CTL, val);
589 	} else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
590 		if ((port < 0) || (port >= NETXEN_NIU_MAX_XG_PORTS))
591 			return -EIO;
592 		val = NXRD32(adapter, NETXEN_NIU_XG_PAUSE_CTL);
593 		if (port == 0) {
594 			if (pause->tx_pause)
595 				netxen_xg_unset_xg0_mask(val);
596 			else
597 				netxen_xg_set_xg0_mask(val);
598 		} else {
599 			if (pause->tx_pause)
600 				netxen_xg_unset_xg1_mask(val);
601 			else
602 				netxen_xg_set_xg1_mask(val);
603 		}
604 		NXWR32(adapter, NETXEN_NIU_XG_PAUSE_CTL, val);
605 	} else {
606 		printk(KERN_ERR "%s: Unknown board type: %x\n",
607 				netxen_nic_driver_name,
608 				adapter->ahw.port_type);
609 	}
610 	return 0;
611 }
612 
613 static int netxen_nic_reg_test(struct net_device *dev)
614 {
615 	struct netxen_adapter *adapter = netdev_priv(dev);
616 	u32 data_read, data_written;
617 
618 	data_read = NXRD32(adapter, NETXEN_PCIX_PH_REG(0));
619 	if ((data_read & 0xffff) != adapter->pdev->vendor)
620 		return 1;
621 
622 	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
623 		return 0;
624 
625 	data_written = (u32)0xa5a5a5a5;
626 
627 	NXWR32(adapter, CRB_SCRATCHPAD_TEST, data_written);
628 	data_read = NXRD32(adapter, CRB_SCRATCHPAD_TEST);
629 	if (data_written != data_read)
630 		return 1;
631 
632 	return 0;
633 }
634 
635 static int netxen_get_sset_count(struct net_device *dev, int sset)
636 {
637 	switch (sset) {
638 	case ETH_SS_TEST:
639 		return NETXEN_NIC_TEST_LEN;
640 	case ETH_SS_STATS:
641 		return NETXEN_NIC_STATS_LEN;
642 	default:
643 		return -EOPNOTSUPP;
644 	}
645 }
646 
647 static void
648 netxen_nic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
649 		     u64 *data)
650 {
651 	memset(data, 0, sizeof(uint64_t) * NETXEN_NIC_TEST_LEN);
652 	if ((data[0] = netxen_nic_reg_test(dev)))
653 		eth_test->flags |= ETH_TEST_FL_FAILED;
654 	/* link test */
655 	if ((data[1] = (u64) netxen_nic_test_link(dev)))
656 		eth_test->flags |= ETH_TEST_FL_FAILED;
657 }
658 
659 static void
660 netxen_nic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
661 {
662 	int index;
663 
664 	switch (stringset) {
665 	case ETH_SS_TEST:
666 		memcpy(data, *netxen_nic_gstrings_test,
667 		       NETXEN_NIC_TEST_LEN * ETH_GSTRING_LEN);
668 		break;
669 	case ETH_SS_STATS:
670 		for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
671 			memcpy(data + index * ETH_GSTRING_LEN,
672 			       netxen_nic_gstrings_stats[index].stat_string,
673 			       ETH_GSTRING_LEN);
674 		}
675 		break;
676 	}
677 }
678 
679 static void
680 netxen_nic_get_ethtool_stats(struct net_device *dev,
681 			     struct ethtool_stats *stats, u64 *data)
682 {
683 	struct netxen_adapter *adapter = netdev_priv(dev);
684 	int index;
685 
686 	for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
687 		char *p =
688 		    (char *)adapter +
689 		    netxen_nic_gstrings_stats[index].stat_offset;
690 		data[index] =
691 		    (netxen_nic_gstrings_stats[index].sizeof_stat ==
692 		     sizeof(u64)) ? *(u64 *) p : *(u32 *) p;
693 	}
694 }
695 
696 static void
697 netxen_nic_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
698 {
699 	struct netxen_adapter *adapter = netdev_priv(dev);
700 	u32 wol_cfg = 0;
701 
702 	wol->supported = 0;
703 	wol->wolopts = 0;
704 
705 	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
706 		return;
707 
708 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG_NV);
709 	if (wol_cfg & (1UL << adapter->portnum))
710 		wol->supported |= WAKE_MAGIC;
711 
712 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG);
713 	if (wol_cfg & (1UL << adapter->portnum))
714 		wol->wolopts |= WAKE_MAGIC;
715 }
716 
717 static int
718 netxen_nic_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
719 {
720 	struct netxen_adapter *adapter = netdev_priv(dev);
721 	u32 wol_cfg = 0;
722 
723 	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
724 		return -EOPNOTSUPP;
725 
726 	if (wol->wolopts & ~WAKE_MAGIC)
727 		return -EOPNOTSUPP;
728 
729 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG_NV);
730 	if (!(wol_cfg & (1 << adapter->portnum)))
731 		return -EOPNOTSUPP;
732 
733 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG);
734 	if (wol->wolopts & WAKE_MAGIC)
735 		wol_cfg |= 1UL << adapter->portnum;
736 	else
737 		wol_cfg &= ~(1UL << adapter->portnum);
738 	NXWR32(adapter, NETXEN_WOL_CONFIG, wol_cfg);
739 
740 	return 0;
741 }
742 
743 /*
744  * Set the coalescing parameters. Currently only normal is supported.
745  * If rx_coalesce_usecs == 0 or rx_max_coalesced_frames == 0 then set the
746  * firmware coalescing to default.
747  */
748 static int netxen_set_intr_coalesce(struct net_device *netdev,
749 			struct ethtool_coalesce *ethcoal)
750 {
751 	struct netxen_adapter *adapter = netdev_priv(netdev);
752 
753 	if (!NX_IS_REVISION_P3(adapter->ahw.revision_id))
754 		return -EINVAL;
755 
756 	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
757 		return -EINVAL;
758 
759 	/*
760 	* Return Error if unsupported values or
761 	* unsupported parameters are set.
762 	*/
763 	if (ethcoal->rx_coalesce_usecs > 0xffff ||
764 		ethcoal->rx_max_coalesced_frames > 0xffff ||
765 		ethcoal->tx_coalesce_usecs > 0xffff ||
766 		ethcoal->tx_max_coalesced_frames > 0xffff ||
767 		ethcoal->rx_coalesce_usecs_irq ||
768 		ethcoal->rx_max_coalesced_frames_irq ||
769 		ethcoal->tx_coalesce_usecs_irq ||
770 		ethcoal->tx_max_coalesced_frames_irq ||
771 		ethcoal->stats_block_coalesce_usecs ||
772 		ethcoal->use_adaptive_rx_coalesce ||
773 		ethcoal->use_adaptive_tx_coalesce ||
774 		ethcoal->pkt_rate_low ||
775 		ethcoal->rx_coalesce_usecs_low ||
776 		ethcoal->rx_max_coalesced_frames_low ||
777 		ethcoal->tx_coalesce_usecs_low ||
778 		ethcoal->tx_max_coalesced_frames_low ||
779 		ethcoal->pkt_rate_high ||
780 		ethcoal->rx_coalesce_usecs_high ||
781 		ethcoal->rx_max_coalesced_frames_high ||
782 		ethcoal->tx_coalesce_usecs_high ||
783 		ethcoal->tx_max_coalesced_frames_high)
784 		return -EINVAL;
785 
786 	if (!ethcoal->rx_coalesce_usecs ||
787 		!ethcoal->rx_max_coalesced_frames) {
788 		adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
789 		adapter->coal.normal.data.rx_time_us =
790 			NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
791 		adapter->coal.normal.data.rx_packets =
792 			NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
793 	} else {
794 		adapter->coal.flags = 0;
795 		adapter->coal.normal.data.rx_time_us =
796 		ethcoal->rx_coalesce_usecs;
797 		adapter->coal.normal.data.rx_packets =
798 		ethcoal->rx_max_coalesced_frames;
799 	}
800 	adapter->coal.normal.data.tx_time_us = ethcoal->tx_coalesce_usecs;
801 	adapter->coal.normal.data.tx_packets =
802 	ethcoal->tx_max_coalesced_frames;
803 
804 	netxen_config_intr_coalesce(adapter);
805 
806 	return 0;
807 }
808 
809 static int netxen_get_intr_coalesce(struct net_device *netdev,
810 			struct ethtool_coalesce *ethcoal)
811 {
812 	struct netxen_adapter *adapter = netdev_priv(netdev);
813 
814 	if (!NX_IS_REVISION_P3(adapter->ahw.revision_id))
815 		return -EINVAL;
816 
817 	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
818 		return -EINVAL;
819 
820 	ethcoal->rx_coalesce_usecs = adapter->coal.normal.data.rx_time_us;
821 	ethcoal->tx_coalesce_usecs = adapter->coal.normal.data.tx_time_us;
822 	ethcoal->rx_max_coalesced_frames =
823 		adapter->coal.normal.data.rx_packets;
824 	ethcoal->tx_max_coalesced_frames =
825 		adapter->coal.normal.data.tx_packets;
826 
827 	return 0;
828 }
829 
830 static int
831 netxen_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
832 {
833 	struct netxen_adapter *adapter = netdev_priv(netdev);
834 	struct netxen_minidump *mdump = &adapter->mdump;
835 	if (adapter->fw_mdump_rdy)
836 		dump->len = mdump->md_dump_size;
837 	else
838 		dump->len = 0;
839 
840 	if (!mdump->md_enabled)
841 		dump->flag = ETH_FW_DUMP_DISABLE;
842 	else
843 		dump->flag = mdump->md_capture_mask;
844 
845 	dump->version = adapter->fw_version;
846 	return 0;
847 }
848 
849 static int
850 netxen_set_dump(struct net_device *netdev, struct ethtool_dump *val)
851 {
852 	int i;
853 	struct netxen_adapter *adapter = netdev_priv(netdev);
854 	struct netxen_minidump *mdump = &adapter->mdump;
855 
856 	switch (val->flag) {
857 	case NX_FORCE_FW_DUMP_KEY:
858 		if (!mdump->md_enabled) {
859 			netdev_info(netdev, "FW dump not enabled\n");
860 			return 0;
861 		}
862 		if (adapter->fw_mdump_rdy) {
863 			netdev_info(netdev, "Previous dump not cleared, not forcing dump\n");
864 			return 0;
865 		}
866 		netdev_info(netdev, "Forcing a fw dump\n");
867 		nx_dev_request_reset(adapter);
868 		break;
869 	case NX_DISABLE_FW_DUMP:
870 		if (mdump->md_enabled) {
871 			netdev_info(netdev, "Disabling FW Dump\n");
872 			mdump->md_enabled = 0;
873 		}
874 		break;
875 	case NX_ENABLE_FW_DUMP:
876 		if (!mdump->md_enabled) {
877 			netdev_info(netdev, "Enabling FW dump\n");
878 			mdump->md_enabled = 1;
879 		}
880 		break;
881 	case NX_FORCE_FW_RESET:
882 		netdev_info(netdev, "Forcing FW reset\n");
883 		nx_dev_request_reset(adapter);
884 		adapter->flags &= ~NETXEN_FW_RESET_OWNER;
885 		break;
886 	default:
887 		for (i = 0; i < ARRAY_SIZE(FW_DUMP_LEVELS); i++) {
888 			if (val->flag == FW_DUMP_LEVELS[i]) {
889 				mdump->md_capture_mask = val->flag;
890 				netdev_info(netdev,
891 					"Driver mask changed to: 0x%x\n",
892 					mdump->md_capture_mask);
893 				return 0;
894 			}
895 		}
896 		netdev_info(netdev,
897 			"Invalid dump level: 0x%x\n", val->flag);
898 		return -EINVAL;
899 	}
900 
901 	return 0;
902 }
903 
904 static int
905 netxen_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
906 			void *buffer)
907 {
908 	int i, copy_sz;
909 	u32 *hdr_ptr, *data;
910 	struct netxen_adapter *adapter = netdev_priv(netdev);
911 	struct netxen_minidump *mdump = &adapter->mdump;
912 
913 
914 	if (!adapter->fw_mdump_rdy) {
915 		netdev_info(netdev, "Dump not available\n");
916 		return -EINVAL;
917 	}
918 	/* Copy template header first */
919 	copy_sz = mdump->md_template_size;
920 	hdr_ptr = (u32 *) mdump->md_template;
921 	data = buffer;
922 	for (i = 0; i < copy_sz/sizeof(u32); i++)
923 		*data++ = cpu_to_le32(*hdr_ptr++);
924 
925 	/* Copy captured dump data */
926 	memcpy(buffer + copy_sz,
927 		mdump->md_capture_buff + mdump->md_template_size,
928 			mdump->md_capture_size);
929 	dump->len = copy_sz + mdump->md_capture_size;
930 	dump->flag = mdump->md_capture_mask;
931 
932 	/* Free dump area once data has been captured */
933 	vfree(mdump->md_capture_buff);
934 	mdump->md_capture_buff = NULL;
935 	adapter->fw_mdump_rdy = 0;
936 	netdev_info(netdev, "extracted the fw dump Successfully\n");
937 	return 0;
938 }
939 
940 const struct ethtool_ops netxen_nic_ethtool_ops = {
941 	.get_drvinfo = netxen_nic_get_drvinfo,
942 	.get_regs_len = netxen_nic_get_regs_len,
943 	.get_regs = netxen_nic_get_regs,
944 	.get_link = ethtool_op_get_link,
945 	.get_eeprom_len = netxen_nic_get_eeprom_len,
946 	.get_eeprom = netxen_nic_get_eeprom,
947 	.get_ringparam = netxen_nic_get_ringparam,
948 	.set_ringparam = netxen_nic_set_ringparam,
949 	.get_pauseparam = netxen_nic_get_pauseparam,
950 	.set_pauseparam = netxen_nic_set_pauseparam,
951 	.get_wol = netxen_nic_get_wol,
952 	.set_wol = netxen_nic_set_wol,
953 	.self_test = netxen_nic_diag_test,
954 	.get_strings = netxen_nic_get_strings,
955 	.get_ethtool_stats = netxen_nic_get_ethtool_stats,
956 	.get_sset_count = netxen_get_sset_count,
957 	.get_coalesce = netxen_get_intr_coalesce,
958 	.set_coalesce = netxen_set_intr_coalesce,
959 	.get_dump_flag = netxen_get_dump_flag,
960 	.get_dump_data = netxen_get_dump_data,
961 	.set_dump = netxen_set_dump,
962 	.get_link_ksettings = netxen_nic_get_link_ksettings,
963 	.set_link_ksettings = netxen_nic_set_link_ksettings,
964 };
965