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