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 	if (!netif_running(dev) || !adapter->ahw.linkup) {
252 		ecmd->duplex = DUPLEX_UNKNOWN;
253 		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
254 	}
255 
256 	return 0;
257 }
258 
259 static int
netxen_nic_set_settings(struct net_device * dev,struct ethtool_cmd * ecmd)260 netxen_nic_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
261 {
262 	struct netxen_adapter *adapter = netdev_priv(dev);
263 	u32 speed = ethtool_cmd_speed(ecmd);
264 	int ret;
265 
266 	if (adapter->ahw.port_type != NETXEN_NIC_GBE)
267 		return -EOPNOTSUPP;
268 
269 	if (!(adapter->capabilities & NX_FW_CAPABILITY_GBE_LINK_CFG))
270 		return -EOPNOTSUPP;
271 
272 	ret = nx_fw_cmd_set_gbe_port(adapter, speed, ecmd->duplex,
273 				     ecmd->autoneg);
274 	if (ret == NX_RCODE_NOT_SUPPORTED)
275 		return -EOPNOTSUPP;
276 	else if (ret)
277 		return -EIO;
278 
279 	adapter->link_speed = speed;
280 	adapter->link_duplex = ecmd->duplex;
281 	adapter->link_autoneg = ecmd->autoneg;
282 
283 	if (!netif_running(dev))
284 		return 0;
285 
286 	dev->netdev_ops->ndo_stop(dev);
287 	return dev->netdev_ops->ndo_open(dev);
288 }
289 
netxen_nic_get_regs_len(struct net_device * dev)290 static int netxen_nic_get_regs_len(struct net_device *dev)
291 {
292 	return NETXEN_NIC_REGS_LEN;
293 }
294 
295 static void
netxen_nic_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * p)296 netxen_nic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
297 {
298 	struct netxen_adapter *adapter = netdev_priv(dev);
299 	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
300 	struct nx_host_sds_ring *sds_ring;
301 	u32 *regs_buff = p;
302 	int ring, i = 0;
303 	int port = adapter->physical_port;
304 
305 	memset(p, 0, NETXEN_NIC_REGS_LEN);
306 
307 	regs->version = (1 << 24) | (adapter->ahw.revision_id << 16) |
308 	    (adapter->pdev)->device;
309 
310 	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
311 		return;
312 
313 	regs_buff[i++] = NXRD32(adapter, CRB_CMDPEG_STATE);
314 	regs_buff[i++] = NXRD32(adapter, CRB_RCVPEG_STATE);
315 	regs_buff[i++] = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
316 	regs_buff[i++] = NXRDIO(adapter, adapter->crb_int_state_reg);
317 	regs_buff[i++] = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
318 	regs_buff[i++] = NXRD32(adapter, NX_CRB_DEV_STATE);
319 	regs_buff[i++] = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
320 	regs_buff[i++] = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
321 	regs_buff[i++] = NXRD32(adapter, NETXEN_PEG_HALT_STATUS2);
322 
323 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_0+0x3c);
324 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_1+0x3c);
325 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_2+0x3c);
326 	regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_3+0x3c);
327 
328 	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
329 
330 		regs_buff[i++] = NXRD32(adapter, NETXEN_CRB_PEG_NET_4+0x3c);
331 		i += 2;
332 
333 		regs_buff[i++] = NXRD32(adapter, CRB_XG_STATE_P3);
334 		regs_buff[i++] = le32_to_cpu(*(adapter->tx_ring->hw_consumer));
335 
336 	} else {
337 		i++;
338 
339 		regs_buff[i++] = NXRD32(adapter,
340 					NETXEN_NIU_XGE_CONFIG_0+(0x10000*port));
341 		regs_buff[i++] = NXRD32(adapter,
342 					NETXEN_NIU_XGE_CONFIG_1+(0x10000*port));
343 
344 		regs_buff[i++] = NXRD32(adapter, CRB_XG_STATE);
345 		regs_buff[i++] = NXRDIO(adapter,
346 				 adapter->tx_ring->crb_cmd_consumer);
347 	}
348 
349 	regs_buff[i++] = NXRDIO(adapter, adapter->tx_ring->crb_cmd_producer);
350 
351 	regs_buff[i++] = NXRDIO(adapter,
352 			 recv_ctx->rds_rings[0].crb_rcv_producer);
353 	regs_buff[i++] = NXRDIO(adapter,
354 			 recv_ctx->rds_rings[1].crb_rcv_producer);
355 
356 	regs_buff[i++] = adapter->max_sds_rings;
357 
358 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
359 		sds_ring = &(recv_ctx->sds_rings[ring]);
360 		regs_buff[i++] = NXRDIO(adapter,
361 					sds_ring->crb_sts_consumer);
362 	}
363 }
364 
netxen_nic_test_link(struct net_device * dev)365 static u32 netxen_nic_test_link(struct net_device *dev)
366 {
367 	struct netxen_adapter *adapter = netdev_priv(dev);
368 	u32 val, port;
369 
370 	port = adapter->physical_port;
371 	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
372 		val = NXRD32(adapter, CRB_XG_STATE_P3);
373 		val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
374 		return (val == XG_LINK_UP_P3) ? 0 : 1;
375 	} else {
376 		val = NXRD32(adapter, CRB_XG_STATE);
377 		val = (val >> port*8) & 0xff;
378 		return (val == XG_LINK_UP) ? 0 : 1;
379 	}
380 }
381 
382 static int
netxen_nic_get_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * bytes)383 netxen_nic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
384 		      u8 * bytes)
385 {
386 	struct netxen_adapter *adapter = netdev_priv(dev);
387 	int offset;
388 	int ret;
389 
390 	if (eeprom->len == 0)
391 		return -EINVAL;
392 
393 	eeprom->magic = (adapter->pdev)->vendor |
394 			((adapter->pdev)->device << 16);
395 	offset = eeprom->offset;
396 
397 	ret = netxen_rom_fast_read_words(adapter, offset, bytes,
398 						eeprom->len);
399 	if (ret < 0)
400 		return ret;
401 
402 	return 0;
403 }
404 
405 static void
netxen_nic_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)406 netxen_nic_get_ringparam(struct net_device *dev,
407 		struct ethtool_ringparam *ring)
408 {
409 	struct netxen_adapter *adapter = netdev_priv(dev);
410 
411 	ring->rx_pending = adapter->num_rxd;
412 	ring->rx_jumbo_pending = adapter->num_jumbo_rxd;
413 	ring->rx_jumbo_pending += adapter->num_lro_rxd;
414 	ring->tx_pending = adapter->num_txd;
415 
416 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
417 		ring->rx_max_pending = MAX_RCV_DESCRIPTORS_1G;
418 		ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_1G;
419 	} else {
420 		ring->rx_max_pending = MAX_RCV_DESCRIPTORS_10G;
421 		ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_10G;
422 	}
423 
424 	ring->tx_max_pending = MAX_CMD_DESCRIPTORS;
425 }
426 
427 static u32
netxen_validate_ringparam(u32 val,u32 min,u32 max,char * r_name)428 netxen_validate_ringparam(u32 val, u32 min, u32 max, char *r_name)
429 {
430 	u32 num_desc;
431 	num_desc = max(val, min);
432 	num_desc = min(num_desc, max);
433 	num_desc = roundup_pow_of_two(num_desc);
434 
435 	if (val != num_desc) {
436 		printk(KERN_INFO "%s: setting %s ring size %d instead of %d\n",
437 		       netxen_nic_driver_name, r_name, num_desc, val);
438 	}
439 
440 	return num_desc;
441 }
442 
443 static int
netxen_nic_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)444 netxen_nic_set_ringparam(struct net_device *dev,
445 		struct ethtool_ringparam *ring)
446 {
447 	struct netxen_adapter *adapter = netdev_priv(dev);
448 	u16 max_rcv_desc = MAX_RCV_DESCRIPTORS_10G;
449 	u16 max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
450 	u16 num_rxd, num_jumbo_rxd, num_txd;
451 
452 	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
453 		return -EOPNOTSUPP;
454 
455 	if (ring->rx_mini_pending)
456 		return -EOPNOTSUPP;
457 
458 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
459 		max_rcv_desc = MAX_RCV_DESCRIPTORS_1G;
460 		max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
461 	}
462 
463 	num_rxd = netxen_validate_ringparam(ring->rx_pending,
464 			MIN_RCV_DESCRIPTORS, max_rcv_desc, "rx");
465 
466 	num_jumbo_rxd = netxen_validate_ringparam(ring->rx_jumbo_pending,
467 			MIN_JUMBO_DESCRIPTORS, max_jumbo_desc, "rx jumbo");
468 
469 	num_txd = netxen_validate_ringparam(ring->tx_pending,
470 			MIN_CMD_DESCRIPTORS, MAX_CMD_DESCRIPTORS, "tx");
471 
472 	if (num_rxd == adapter->num_rxd && num_txd == adapter->num_txd &&
473 			num_jumbo_rxd == adapter->num_jumbo_rxd)
474 		return 0;
475 
476 	adapter->num_rxd = num_rxd;
477 	adapter->num_jumbo_rxd = num_jumbo_rxd;
478 	adapter->num_txd = num_txd;
479 
480 	return netxen_nic_reset_context(adapter);
481 }
482 
483 static void
netxen_nic_get_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)484 netxen_nic_get_pauseparam(struct net_device *dev,
485 			  struct ethtool_pauseparam *pause)
486 {
487 	struct netxen_adapter *adapter = netdev_priv(dev);
488 	__u32 val;
489 	int port = adapter->physical_port;
490 
491 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
492 		if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
493 			return;
494 		/* get flow control settings */
495 		val = NXRD32(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port));
496 		pause->rx_pause = netxen_gb_get_rx_flowctl(val);
497 		val = NXRD32(adapter, NETXEN_NIU_GB_PAUSE_CTL);
498 		switch (port) {
499 			case 0:
500 				pause->tx_pause = !(netxen_gb_get_gb0_mask(val));
501 				break;
502 			case 1:
503 				pause->tx_pause = !(netxen_gb_get_gb1_mask(val));
504 				break;
505 			case 2:
506 				pause->tx_pause = !(netxen_gb_get_gb2_mask(val));
507 				break;
508 			case 3:
509 			default:
510 				pause->tx_pause = !(netxen_gb_get_gb3_mask(val));
511 				break;
512 		}
513 	} else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
514 		if ((port < 0) || (port > NETXEN_NIU_MAX_XG_PORTS))
515 			return;
516 		pause->rx_pause = 1;
517 		val = NXRD32(adapter, NETXEN_NIU_XG_PAUSE_CTL);
518 		if (port == 0)
519 			pause->tx_pause = !(netxen_xg_get_xg0_mask(val));
520 		else
521 			pause->tx_pause = !(netxen_xg_get_xg1_mask(val));
522 	} else {
523 		printk(KERN_ERR"%s: Unknown board type: %x\n",
524 				netxen_nic_driver_name, adapter->ahw.port_type);
525 	}
526 }
527 
528 static int
netxen_nic_set_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)529 netxen_nic_set_pauseparam(struct net_device *dev,
530 			  struct ethtool_pauseparam *pause)
531 {
532 	struct netxen_adapter *adapter = netdev_priv(dev);
533 	__u32 val;
534 	int port = adapter->physical_port;
535 	/* read mode */
536 	if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
537 		if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
538 			return -EIO;
539 		/* set flow control */
540 		val = NXRD32(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port));
541 
542 		if (pause->rx_pause)
543 			netxen_gb_rx_flowctl(val);
544 		else
545 			netxen_gb_unset_rx_flowctl(val);
546 
547 		NXWR32(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
548 				val);
549 		/* set autoneg */
550 		val = NXRD32(adapter, NETXEN_NIU_GB_PAUSE_CTL);
551 		switch (port) {
552 			case 0:
553 				if (pause->tx_pause)
554 					netxen_gb_unset_gb0_mask(val);
555 				else
556 					netxen_gb_set_gb0_mask(val);
557 				break;
558 			case 1:
559 				if (pause->tx_pause)
560 					netxen_gb_unset_gb1_mask(val);
561 				else
562 					netxen_gb_set_gb1_mask(val);
563 				break;
564 			case 2:
565 				if (pause->tx_pause)
566 					netxen_gb_unset_gb2_mask(val);
567 				else
568 					netxen_gb_set_gb2_mask(val);
569 				break;
570 			case 3:
571 			default:
572 				if (pause->tx_pause)
573 					netxen_gb_unset_gb3_mask(val);
574 				else
575 					netxen_gb_set_gb3_mask(val);
576 				break;
577 		}
578 		NXWR32(adapter, NETXEN_NIU_GB_PAUSE_CTL, val);
579 	} else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
580 		if ((port < 0) || (port > NETXEN_NIU_MAX_XG_PORTS))
581 			return -EIO;
582 		val = NXRD32(adapter, NETXEN_NIU_XG_PAUSE_CTL);
583 		if (port == 0) {
584 			if (pause->tx_pause)
585 				netxen_xg_unset_xg0_mask(val);
586 			else
587 				netxen_xg_set_xg0_mask(val);
588 		} else {
589 			if (pause->tx_pause)
590 				netxen_xg_unset_xg1_mask(val);
591 			else
592 				netxen_xg_set_xg1_mask(val);
593 		}
594 		NXWR32(adapter, NETXEN_NIU_XG_PAUSE_CTL, val);
595 	} else {
596 		printk(KERN_ERR "%s: Unknown board type: %x\n",
597 				netxen_nic_driver_name,
598 				adapter->ahw.port_type);
599 	}
600 	return 0;
601 }
602 
netxen_nic_reg_test(struct net_device * dev)603 static int netxen_nic_reg_test(struct net_device *dev)
604 {
605 	struct netxen_adapter *adapter = netdev_priv(dev);
606 	u32 data_read, data_written;
607 
608 	data_read = NXRD32(adapter, NETXEN_PCIX_PH_REG(0));
609 	if ((data_read & 0xffff) != adapter->pdev->vendor)
610 		return 1;
611 
612 	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
613 		return 0;
614 
615 	data_written = (u32)0xa5a5a5a5;
616 
617 	NXWR32(adapter, CRB_SCRATCHPAD_TEST, data_written);
618 	data_read = NXRD32(adapter, CRB_SCRATCHPAD_TEST);
619 	if (data_written != data_read)
620 		return 1;
621 
622 	return 0;
623 }
624 
netxen_get_sset_count(struct net_device * dev,int sset)625 static int netxen_get_sset_count(struct net_device *dev, int sset)
626 {
627 	switch (sset) {
628 	case ETH_SS_TEST:
629 		return NETXEN_NIC_TEST_LEN;
630 	case ETH_SS_STATS:
631 		return NETXEN_NIC_STATS_LEN;
632 	default:
633 		return -EOPNOTSUPP;
634 	}
635 }
636 
637 static void
netxen_nic_diag_test(struct net_device * dev,struct ethtool_test * eth_test,u64 * data)638 netxen_nic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
639 		     u64 * data)
640 {
641 	memset(data, 0, sizeof(uint64_t) * NETXEN_NIC_TEST_LEN);
642 	if ((data[0] = netxen_nic_reg_test(dev)))
643 		eth_test->flags |= ETH_TEST_FL_FAILED;
644 	/* link test */
645 	if ((data[1] = (u64) netxen_nic_test_link(dev)))
646 		eth_test->flags |= ETH_TEST_FL_FAILED;
647 }
648 
649 static void
netxen_nic_get_strings(struct net_device * dev,u32 stringset,u8 * data)650 netxen_nic_get_strings(struct net_device *dev, u32 stringset, u8 * data)
651 {
652 	int index;
653 
654 	switch (stringset) {
655 	case ETH_SS_TEST:
656 		memcpy(data, *netxen_nic_gstrings_test,
657 		       NETXEN_NIC_TEST_LEN * ETH_GSTRING_LEN);
658 		break;
659 	case ETH_SS_STATS:
660 		for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
661 			memcpy(data + index * ETH_GSTRING_LEN,
662 			       netxen_nic_gstrings_stats[index].stat_string,
663 			       ETH_GSTRING_LEN);
664 		}
665 		break;
666 	}
667 }
668 
669 static void
netxen_nic_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)670 netxen_nic_get_ethtool_stats(struct net_device *dev,
671 			     struct ethtool_stats *stats, u64 * data)
672 {
673 	struct netxen_adapter *adapter = netdev_priv(dev);
674 	int index;
675 
676 	for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
677 		char *p =
678 		    (char *)adapter +
679 		    netxen_nic_gstrings_stats[index].stat_offset;
680 		data[index] =
681 		    (netxen_nic_gstrings_stats[index].sizeof_stat ==
682 		     sizeof(u64)) ? *(u64 *) p : *(u32 *) p;
683 	}
684 }
685 
686 static void
netxen_nic_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)687 netxen_nic_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
688 {
689 	struct netxen_adapter *adapter = netdev_priv(dev);
690 	u32 wol_cfg = 0;
691 
692 	wol->supported = 0;
693 	wol->wolopts = 0;
694 
695 	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
696 		return;
697 
698 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG_NV);
699 	if (wol_cfg & (1UL << adapter->portnum))
700 		wol->supported |= WAKE_MAGIC;
701 
702 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG);
703 	if (wol_cfg & (1UL << adapter->portnum))
704 		wol->wolopts |= WAKE_MAGIC;
705 }
706 
707 static int
netxen_nic_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)708 netxen_nic_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
709 {
710 	struct netxen_adapter *adapter = netdev_priv(dev);
711 	u32 wol_cfg = 0;
712 
713 	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
714 		return -EOPNOTSUPP;
715 
716 	if (wol->wolopts & ~WAKE_MAGIC)
717 		return -EOPNOTSUPP;
718 
719 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG_NV);
720 	if (!(wol_cfg & (1 << adapter->portnum)))
721 		return -EOPNOTSUPP;
722 
723 	wol_cfg = NXRD32(adapter, NETXEN_WOL_CONFIG);
724 	if (wol->wolopts & WAKE_MAGIC)
725 		wol_cfg |= 1UL << adapter->portnum;
726 	else
727 		wol_cfg &= ~(1UL << adapter->portnum);
728 	NXWR32(adapter, NETXEN_WOL_CONFIG, wol_cfg);
729 
730 	return 0;
731 }
732 
733 /*
734  * Set the coalescing parameters. Currently only normal is supported.
735  * If rx_coalesce_usecs == 0 or rx_max_coalesced_frames == 0 then set the
736  * firmware coalescing to default.
737  */
netxen_set_intr_coalesce(struct net_device * netdev,struct ethtool_coalesce * ethcoal)738 static int netxen_set_intr_coalesce(struct net_device *netdev,
739 			struct ethtool_coalesce *ethcoal)
740 {
741 	struct netxen_adapter *adapter = netdev_priv(netdev);
742 
743 	if (!NX_IS_REVISION_P3(adapter->ahw.revision_id))
744 		return -EINVAL;
745 
746 	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
747 		return -EINVAL;
748 
749 	/*
750 	* Return Error if unsupported values or
751 	* unsupported parameters are set.
752 	*/
753 	if (ethcoal->rx_coalesce_usecs > 0xffff ||
754 		ethcoal->rx_max_coalesced_frames > 0xffff ||
755 		ethcoal->tx_coalesce_usecs > 0xffff ||
756 		ethcoal->tx_max_coalesced_frames > 0xffff ||
757 		ethcoal->rx_coalesce_usecs_irq ||
758 		ethcoal->rx_max_coalesced_frames_irq ||
759 		ethcoal->tx_coalesce_usecs_irq ||
760 		ethcoal->tx_max_coalesced_frames_irq ||
761 		ethcoal->stats_block_coalesce_usecs ||
762 		ethcoal->use_adaptive_rx_coalesce ||
763 		ethcoal->use_adaptive_tx_coalesce ||
764 		ethcoal->pkt_rate_low ||
765 		ethcoal->rx_coalesce_usecs_low ||
766 		ethcoal->rx_max_coalesced_frames_low ||
767 		ethcoal->tx_coalesce_usecs_low ||
768 		ethcoal->tx_max_coalesced_frames_low ||
769 		ethcoal->pkt_rate_high ||
770 		ethcoal->rx_coalesce_usecs_high ||
771 		ethcoal->rx_max_coalesced_frames_high ||
772 		ethcoal->tx_coalesce_usecs_high ||
773 		ethcoal->tx_max_coalesced_frames_high)
774 		return -EINVAL;
775 
776 	if (!ethcoal->rx_coalesce_usecs ||
777 		!ethcoal->rx_max_coalesced_frames) {
778 		adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
779 		adapter->coal.normal.data.rx_time_us =
780 			NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
781 		adapter->coal.normal.data.rx_packets =
782 			NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
783 	} else {
784 		adapter->coal.flags = 0;
785 		adapter->coal.normal.data.rx_time_us =
786 		ethcoal->rx_coalesce_usecs;
787 		adapter->coal.normal.data.rx_packets =
788 		ethcoal->rx_max_coalesced_frames;
789 	}
790 	adapter->coal.normal.data.tx_time_us = ethcoal->tx_coalesce_usecs;
791 	adapter->coal.normal.data.tx_packets =
792 	ethcoal->tx_max_coalesced_frames;
793 
794 	netxen_config_intr_coalesce(adapter);
795 
796 	return 0;
797 }
798 
netxen_get_intr_coalesce(struct net_device * netdev,struct ethtool_coalesce * ethcoal)799 static int netxen_get_intr_coalesce(struct net_device *netdev,
800 			struct ethtool_coalesce *ethcoal)
801 {
802 	struct netxen_adapter *adapter = netdev_priv(netdev);
803 
804 	if (!NX_IS_REVISION_P3(adapter->ahw.revision_id))
805 		return -EINVAL;
806 
807 	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
808 		return -EINVAL;
809 
810 	ethcoal->rx_coalesce_usecs = adapter->coal.normal.data.rx_time_us;
811 	ethcoal->tx_coalesce_usecs = adapter->coal.normal.data.tx_time_us;
812 	ethcoal->rx_max_coalesced_frames =
813 		adapter->coal.normal.data.rx_packets;
814 	ethcoal->tx_max_coalesced_frames =
815 		adapter->coal.normal.data.tx_packets;
816 
817 	return 0;
818 }
819 
820 static int
netxen_get_dump_flag(struct net_device * netdev,struct ethtool_dump * dump)821 netxen_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
822 {
823 	struct netxen_adapter *adapter = netdev_priv(netdev);
824 	struct netxen_minidump *mdump = &adapter->mdump;
825 	if (adapter->fw_mdump_rdy)
826 		dump->len = mdump->md_dump_size;
827 	else
828 		dump->len = 0;
829 	dump->flag = mdump->md_capture_mask;
830 	dump->version = adapter->fw_version;
831 	return 0;
832 }
833 
834 static int
netxen_set_dump(struct net_device * netdev,struct ethtool_dump * val)835 netxen_set_dump(struct net_device *netdev, struct ethtool_dump *val)
836 {
837 	int ret = 0;
838 	struct netxen_adapter *adapter = netdev_priv(netdev);
839 	struct netxen_minidump *mdump = &adapter->mdump;
840 
841 	switch (val->flag) {
842 	case NX_FORCE_FW_DUMP_KEY:
843 		if (!mdump->md_enabled)
844 			mdump->md_enabled = 1;
845 		if (adapter->fw_mdump_rdy) {
846 			netdev_info(netdev, "Previous dump not cleared, not forcing dump\n");
847 			return ret;
848 		}
849 		netdev_info(netdev, "Forcing a fw dump\n");
850 		nx_dev_request_reset(adapter);
851 		break;
852 	case NX_DISABLE_FW_DUMP:
853 		if (mdump->md_enabled) {
854 			netdev_info(netdev, "Disabling FW Dump\n");
855 			mdump->md_enabled = 0;
856 		}
857 		break;
858 	case NX_ENABLE_FW_DUMP:
859 		if (!mdump->md_enabled) {
860 			netdev_info(netdev, "Enabling FW dump\n");
861 			mdump->md_enabled = 1;
862 		}
863 		break;
864 	case NX_FORCE_FW_RESET:
865 		netdev_info(netdev, "Forcing FW reset\n");
866 		nx_dev_request_reset(adapter);
867 		adapter->flags &= ~NETXEN_FW_RESET_OWNER;
868 		break;
869 	default:
870 		if (val->flag <= NX_DUMP_MASK_MAX &&
871 			val->flag >= NX_DUMP_MASK_MIN) {
872 			mdump->md_capture_mask = val->flag & 0xff;
873 			netdev_info(netdev, "Driver mask changed to: 0x%x\n",
874 					mdump->md_capture_mask);
875 			break;
876 		}
877 		netdev_info(netdev,
878 			"Invalid dump level: 0x%x\n", val->flag);
879 		return -EINVAL;
880 	}
881 
882 	return ret;
883 }
884 
885 static int
netxen_get_dump_data(struct net_device * netdev,struct ethtool_dump * dump,void * buffer)886 netxen_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
887 			void *buffer)
888 {
889 	int i, copy_sz;
890 	u32 *hdr_ptr, *data;
891 	struct netxen_adapter *adapter = netdev_priv(netdev);
892 	struct netxen_minidump *mdump = &adapter->mdump;
893 
894 
895 	if (!adapter->fw_mdump_rdy) {
896 		netdev_info(netdev, "Dump not available\n");
897 		return -EINVAL;
898 	}
899 	/* Copy template header first */
900 	copy_sz = mdump->md_template_size;
901 	hdr_ptr = (u32 *) mdump->md_template;
902 	data = buffer;
903 	for (i = 0; i < copy_sz/sizeof(u32); i++)
904 		*data++ = cpu_to_le32(*hdr_ptr++);
905 
906 	/* Copy captured dump data */
907 	memcpy(buffer + copy_sz,
908 		mdump->md_capture_buff + mdump->md_template_size,
909 			mdump->md_capture_size);
910 	dump->len = copy_sz + mdump->md_capture_size;
911 	dump->flag = mdump->md_capture_mask;
912 
913 	/* Free dump area once data has been captured */
914 	vfree(mdump->md_capture_buff);
915 	mdump->md_capture_buff = NULL;
916 	adapter->fw_mdump_rdy = 0;
917 	netdev_info(netdev, "extracted the fw dump Successfully\n");
918 	return 0;
919 }
920 
921 const struct ethtool_ops netxen_nic_ethtool_ops = {
922 	.get_settings = netxen_nic_get_settings,
923 	.set_settings = netxen_nic_set_settings,
924 	.get_drvinfo = netxen_nic_get_drvinfo,
925 	.get_regs_len = netxen_nic_get_regs_len,
926 	.get_regs = netxen_nic_get_regs,
927 	.get_link = ethtool_op_get_link,
928 	.get_eeprom_len = netxen_nic_get_eeprom_len,
929 	.get_eeprom = netxen_nic_get_eeprom,
930 	.get_ringparam = netxen_nic_get_ringparam,
931 	.set_ringparam = netxen_nic_set_ringparam,
932 	.get_pauseparam = netxen_nic_get_pauseparam,
933 	.set_pauseparam = netxen_nic_set_pauseparam,
934 	.get_wol = netxen_nic_get_wol,
935 	.set_wol = netxen_nic_set_wol,
936 	.self_test = netxen_nic_diag_test,
937 	.get_strings = netxen_nic_get_strings,
938 	.get_ethtool_stats = netxen_nic_get_ethtool_stats,
939 	.get_sset_count = netxen_get_sset_count,
940 	.get_coalesce = netxen_get_intr_coalesce,
941 	.set_coalesce = netxen_set_intr_coalesce,
942 	.get_dump_flag = netxen_get_dump_flag,
943 	.get_dump_data = netxen_get_dump_data,
944 	.set_dump = netxen_set_dump,
945 };
946