1/*
2 * Copyright (C) 1999 - 2010 Intel Corporation.
3 * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
4 *
5 * This code was derived from the Intel e1000e Linux driver.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but 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#include "pch_gbe.h"
20#include "pch_gbe_api.h"
21
22/**
23 * pch_gbe_stats - Stats item information
24 */
25struct pch_gbe_stats {
26	char string[ETH_GSTRING_LEN];
27	size_t size;
28	size_t offset;
29};
30
31#define PCH_GBE_STAT(m)						\
32{								\
33	.string = #m,						\
34	.size = FIELD_SIZEOF(struct pch_gbe_hw_stats, m),	\
35	.offset = offsetof(struct pch_gbe_hw_stats, m),		\
36}
37
38/**
39 * pch_gbe_gstrings_stats - ethtool information status name list
40 */
41static const struct pch_gbe_stats pch_gbe_gstrings_stats[] = {
42	PCH_GBE_STAT(rx_packets),
43	PCH_GBE_STAT(tx_packets),
44	PCH_GBE_STAT(rx_bytes),
45	PCH_GBE_STAT(tx_bytes),
46	PCH_GBE_STAT(rx_errors),
47	PCH_GBE_STAT(tx_errors),
48	PCH_GBE_STAT(rx_dropped),
49	PCH_GBE_STAT(tx_dropped),
50	PCH_GBE_STAT(multicast),
51	PCH_GBE_STAT(collisions),
52	PCH_GBE_STAT(rx_crc_errors),
53	PCH_GBE_STAT(rx_frame_errors),
54	PCH_GBE_STAT(rx_alloc_buff_failed),
55	PCH_GBE_STAT(tx_length_errors),
56	PCH_GBE_STAT(tx_aborted_errors),
57	PCH_GBE_STAT(tx_carrier_errors),
58	PCH_GBE_STAT(tx_timeout_count),
59	PCH_GBE_STAT(tx_restart_count),
60	PCH_GBE_STAT(intr_rx_dsc_empty_count),
61	PCH_GBE_STAT(intr_rx_frame_err_count),
62	PCH_GBE_STAT(intr_rx_fifo_err_count),
63	PCH_GBE_STAT(intr_rx_dma_err_count),
64	PCH_GBE_STAT(intr_tx_fifo_err_count),
65	PCH_GBE_STAT(intr_tx_dma_err_count),
66	PCH_GBE_STAT(intr_tcpip_err_count)
67};
68
69#define PCH_GBE_QUEUE_STATS_LEN 0
70#define PCH_GBE_GLOBAL_STATS_LEN	ARRAY_SIZE(pch_gbe_gstrings_stats)
71#define PCH_GBE_STATS_LEN (PCH_GBE_GLOBAL_STATS_LEN + PCH_GBE_QUEUE_STATS_LEN)
72
73#define PCH_GBE_MAC_REGS_LEN    (sizeof(struct pch_gbe_regs) / 4)
74#define PCH_GBE_REGS_LEN        (PCH_GBE_MAC_REGS_LEN + PCH_GBE_PHY_REGS_LEN)
75/**
76 * pch_gbe_get_settings - Get device-specific settings
77 * @netdev: Network interface device structure
78 * @ecmd:   Ethtool command
79 * Returns:
80 *	0:			Successful.
81 *	Negative value:		Failed.
82 */
83static int pch_gbe_get_settings(struct net_device *netdev,
84				 struct ethtool_cmd *ecmd)
85{
86	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
87	int ret;
88
89	ret = mii_ethtool_gset(&adapter->mii, ecmd);
90	ecmd->supported &= ~(SUPPORTED_TP | SUPPORTED_1000baseT_Half);
91	ecmd->advertising &= ~(ADVERTISED_TP | ADVERTISED_1000baseT_Half);
92
93	if (!netif_carrier_ok(adapter->netdev))
94		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
95	return ret;
96}
97
98/**
99 * pch_gbe_set_settings - Set device-specific settings
100 * @netdev: Network interface device structure
101 * @ecmd:   Ethtool command
102 * Returns:
103 *	0:			Successful.
104 *	Negative value:		Failed.
105 */
106static int pch_gbe_set_settings(struct net_device *netdev,
107				 struct ethtool_cmd *ecmd)
108{
109	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
110	struct pch_gbe_hw *hw = &adapter->hw;
111	u32 speed = ethtool_cmd_speed(ecmd);
112	int ret;
113
114	pch_gbe_hal_write_phy_reg(hw, MII_BMCR, BMCR_RESET);
115
116	/* when set_settings() is called with a ethtool_cmd previously
117	 * filled by get_settings() on a down link, speed is -1: */
118	if (speed == UINT_MAX) {
119		speed = SPEED_1000;
120		ethtool_cmd_speed_set(ecmd, speed);
121		ecmd->duplex = DUPLEX_FULL;
122	}
123	ret = mii_ethtool_sset(&adapter->mii, ecmd);
124	if (ret) {
125		netdev_err(netdev, "Error: mii_ethtool_sset\n");
126		return ret;
127	}
128	hw->mac.link_speed = speed;
129	hw->mac.link_duplex = ecmd->duplex;
130	hw->phy.autoneg_advertised = ecmd->advertising;
131	hw->mac.autoneg = ecmd->autoneg;
132
133	/* reset the link */
134	if (netif_running(adapter->netdev)) {
135		pch_gbe_down(adapter);
136		ret = pch_gbe_up(adapter);
137	} else {
138		pch_gbe_reset(adapter);
139	}
140	return ret;
141}
142
143/**
144 * pch_gbe_get_regs_len - Report the size of device registers
145 * @netdev: Network interface device structure
146 * Returns: the size of device registers.
147 */
148static int pch_gbe_get_regs_len(struct net_device *netdev)
149{
150	return PCH_GBE_REGS_LEN * (int)sizeof(u32);
151}
152
153/**
154 * pch_gbe_get_drvinfo - Report driver information
155 * @netdev:  Network interface device structure
156 * @drvinfo: Driver information structure
157 */
158static void pch_gbe_get_drvinfo(struct net_device *netdev,
159				 struct ethtool_drvinfo *drvinfo)
160{
161	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
162
163	strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
164	strlcpy(drvinfo->version, pch_driver_version, sizeof(drvinfo->version));
165	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
166		sizeof(drvinfo->bus_info));
167}
168
169/**
170 * pch_gbe_get_regs - Get device registers
171 * @netdev: Network interface device structure
172 * @regs:   Ethtool register structure
173 * @p:      Buffer pointer of read device register date
174 */
175static void pch_gbe_get_regs(struct net_device *netdev,
176				struct ethtool_regs *regs, void *p)
177{
178	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
179	struct pch_gbe_hw *hw = &adapter->hw;
180	struct pci_dev *pdev = adapter->pdev;
181	u32 *regs_buff = p;
182	u16 i, tmp;
183
184	regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device;
185	for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
186		*regs_buff++ = ioread32(&hw->reg->INT_ST + i);
187	/* PHY register */
188	for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) {
189		pch_gbe_hal_read_phy_reg(&adapter->hw, i, &tmp);
190		*regs_buff++ = tmp;
191	}
192}
193
194/**
195 * pch_gbe_get_wol - Report whether Wake-on-Lan is enabled
196 * @netdev: Network interface device structure
197 * @wol:    Wake-on-Lan information
198 */
199static void pch_gbe_get_wol(struct net_device *netdev,
200				struct ethtool_wolinfo *wol)
201{
202	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
203
204	wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
205	wol->wolopts = 0;
206
207	if ((adapter->wake_up_evt & PCH_GBE_WLC_IND))
208		wol->wolopts |= WAKE_UCAST;
209	if ((adapter->wake_up_evt & PCH_GBE_WLC_MLT))
210		wol->wolopts |= WAKE_MCAST;
211	if ((adapter->wake_up_evt & PCH_GBE_WLC_BR))
212		wol->wolopts |= WAKE_BCAST;
213	if ((adapter->wake_up_evt & PCH_GBE_WLC_MP))
214		wol->wolopts |= WAKE_MAGIC;
215}
216
217/**
218 * pch_gbe_set_wol - Turn Wake-on-Lan on or off
219 * @netdev: Network interface device structure
220 * @wol:    Pointer of wake-on-Lan information straucture
221 * Returns:
222 *	0:			Successful.
223 *	Negative value:		Failed.
224 */
225static int pch_gbe_set_wol(struct net_device *netdev,
226				struct ethtool_wolinfo *wol)
227{
228	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
229
230	if ((wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE)))
231		return -EOPNOTSUPP;
232	/* these settings will always override what we currently have */
233	adapter->wake_up_evt = 0;
234
235	if ((wol->wolopts & WAKE_UCAST))
236		adapter->wake_up_evt |= PCH_GBE_WLC_IND;
237	if ((wol->wolopts & WAKE_MCAST))
238		adapter->wake_up_evt |= PCH_GBE_WLC_MLT;
239	if ((wol->wolopts & WAKE_BCAST))
240		adapter->wake_up_evt |= PCH_GBE_WLC_BR;
241	if ((wol->wolopts & WAKE_MAGIC))
242		adapter->wake_up_evt |= PCH_GBE_WLC_MP;
243	return 0;
244}
245
246/**
247 * pch_gbe_nway_reset - Restart autonegotiation
248 * @netdev: Network interface device structure
249 * Returns:
250 *	0:			Successful.
251 *	Negative value:		Failed.
252 */
253static int pch_gbe_nway_reset(struct net_device *netdev)
254{
255	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
256
257	return mii_nway_restart(&adapter->mii);
258}
259
260/**
261 * pch_gbe_get_ringparam - Report ring sizes
262 * @netdev:  Network interface device structure
263 * @ring:    Ring param structure
264 */
265static void pch_gbe_get_ringparam(struct net_device *netdev,
266					struct ethtool_ringparam *ring)
267{
268	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
269	struct pch_gbe_tx_ring *txdr = adapter->tx_ring;
270	struct pch_gbe_rx_ring *rxdr = adapter->rx_ring;
271
272	ring->rx_max_pending = PCH_GBE_MAX_RXD;
273	ring->tx_max_pending = PCH_GBE_MAX_TXD;
274	ring->rx_pending = rxdr->count;
275	ring->tx_pending = txdr->count;
276}
277
278/**
279 * pch_gbe_set_ringparam - Set ring sizes
280 * @netdev:  Network interface device structure
281 * @ring:    Ring param structure
282 * Returns
283 *	0:			Successful.
284 *	Negative value:		Failed.
285 */
286static int pch_gbe_set_ringparam(struct net_device *netdev,
287					struct ethtool_ringparam *ring)
288{
289	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
290	struct pch_gbe_tx_ring *txdr, *tx_old;
291	struct pch_gbe_rx_ring *rxdr, *rx_old;
292	int tx_ring_size, rx_ring_size;
293	int err = 0;
294
295	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
296		return -EINVAL;
297	tx_ring_size = (int)sizeof(struct pch_gbe_tx_ring);
298	rx_ring_size = (int)sizeof(struct pch_gbe_rx_ring);
299
300	if ((netif_running(adapter->netdev)))
301		pch_gbe_down(adapter);
302	tx_old = adapter->tx_ring;
303	rx_old = adapter->rx_ring;
304
305	txdr = kzalloc(tx_ring_size, GFP_KERNEL);
306	if (!txdr) {
307		err = -ENOMEM;
308		goto err_alloc_tx;
309	}
310	rxdr = kzalloc(rx_ring_size, GFP_KERNEL);
311	if (!rxdr) {
312		err = -ENOMEM;
313		goto err_alloc_rx;
314	}
315	adapter->tx_ring = txdr;
316	adapter->rx_ring = rxdr;
317
318	rxdr->count =
319		clamp_val(ring->rx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
320	rxdr->count = roundup(rxdr->count, PCH_GBE_RX_DESC_MULTIPLE);
321
322	txdr->count =
323		clamp_val(ring->tx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
324	txdr->count = roundup(txdr->count, PCH_GBE_TX_DESC_MULTIPLE);
325
326	if ((netif_running(adapter->netdev))) {
327		/* Try to get new resources before deleting old */
328		err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring);
329		if (err)
330			goto err_setup_rx;
331		err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring);
332		if (err)
333			goto err_setup_tx;
334		/* save the new, restore the old in order to free it,
335		 * then restore the new back again */
336#ifdef RINGFREE
337		adapter->rx_ring = rx_old;
338		adapter->tx_ring = tx_old;
339		pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
340		pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
341		kfree(tx_old);
342		kfree(rx_old);
343		adapter->rx_ring = rxdr;
344		adapter->tx_ring = txdr;
345#else
346		pch_gbe_free_rx_resources(adapter, rx_old);
347		pch_gbe_free_tx_resources(adapter, tx_old);
348		kfree(tx_old);
349		kfree(rx_old);
350		adapter->rx_ring = rxdr;
351		adapter->tx_ring = txdr;
352#endif
353		err = pch_gbe_up(adapter);
354	}
355	return err;
356
357err_setup_tx:
358	pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
359err_setup_rx:
360	adapter->rx_ring = rx_old;
361	adapter->tx_ring = tx_old;
362	kfree(rxdr);
363err_alloc_rx:
364	kfree(txdr);
365err_alloc_tx:
366	if (netif_running(adapter->netdev))
367		pch_gbe_up(adapter);
368	return err;
369}
370
371/**
372 * pch_gbe_get_pauseparam - Report pause parameters
373 * @netdev:  Network interface device structure
374 * @pause:   Pause parameters structure
375 */
376static void pch_gbe_get_pauseparam(struct net_device *netdev,
377				       struct ethtool_pauseparam *pause)
378{
379	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
380	struct pch_gbe_hw *hw = &adapter->hw;
381
382	pause->autoneg =
383	    ((hw->mac.fc_autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE);
384
385	if (hw->mac.fc == PCH_GBE_FC_RX_PAUSE) {
386		pause->rx_pause = 1;
387	} else if (hw->mac.fc == PCH_GBE_FC_TX_PAUSE) {
388		pause->tx_pause = 1;
389	} else if (hw->mac.fc == PCH_GBE_FC_FULL) {
390		pause->rx_pause = 1;
391		pause->tx_pause = 1;
392	}
393}
394
395/**
396 * pch_gbe_set_pauseparam - Set pause parameters
397 * @netdev:  Network interface device structure
398 * @pause:   Pause parameters structure
399 * Returns:
400 *	0:			Successful.
401 *	Negative value:		Failed.
402 */
403static int pch_gbe_set_pauseparam(struct net_device *netdev,
404				       struct ethtool_pauseparam *pause)
405{
406	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
407	struct pch_gbe_hw *hw = &adapter->hw;
408	int ret = 0;
409
410	hw->mac.fc_autoneg = pause->autoneg;
411	if ((pause->rx_pause) && (pause->tx_pause))
412		hw->mac.fc = PCH_GBE_FC_FULL;
413	else if ((pause->rx_pause) && (!pause->tx_pause))
414		hw->mac.fc = PCH_GBE_FC_RX_PAUSE;
415	else if ((!pause->rx_pause) && (pause->tx_pause))
416		hw->mac.fc = PCH_GBE_FC_TX_PAUSE;
417	else if ((!pause->rx_pause) && (!pause->tx_pause))
418		hw->mac.fc = PCH_GBE_FC_NONE;
419
420	if (hw->mac.fc_autoneg == AUTONEG_ENABLE) {
421		if ((netif_running(adapter->netdev))) {
422			pch_gbe_down(adapter);
423			ret = pch_gbe_up(adapter);
424		} else {
425			pch_gbe_reset(adapter);
426		}
427	} else {
428		ret = pch_gbe_mac_force_mac_fc(hw);
429	}
430	return ret;
431}
432
433/**
434 * pch_gbe_get_strings - Return a set of strings that describe the requested
435 *			 objects
436 * @netdev:    Network interface device structure
437 * @stringset: Select the stringset. [ETH_SS_TEST] [ETH_SS_STATS]
438 * @data:      Pointer of read string data.
439 */
440static void pch_gbe_get_strings(struct net_device *netdev, u32 stringset,
441					u8 *data)
442{
443	u8 *p = data;
444	int i;
445
446	switch (stringset) {
447	case (u32) ETH_SS_STATS:
448		for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
449			memcpy(p, pch_gbe_gstrings_stats[i].string,
450			       ETH_GSTRING_LEN);
451			p += ETH_GSTRING_LEN;
452		}
453		break;
454	}
455}
456
457/**
458 * pch_gbe_get_ethtool_stats - Return statistics about the device
459 * @netdev: Network interface device structure
460 * @stats:  Ethtool statue structure
461 * @data:   Pointer of read status area
462 */
463static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
464				  struct ethtool_stats *stats, u64 *data)
465{
466	struct pch_gbe_adapter *adapter = netdev_priv(netdev);
467	int i;
468	const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
469	char *hw_stats = (char *)&adapter->stats;
470
471	pch_gbe_update_stats(adapter);
472	for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
473		char *p = hw_stats + gstats->offset;
474		data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);
475		gstats++;
476	}
477}
478
479static int pch_gbe_get_sset_count(struct net_device *netdev, int sset)
480{
481	switch (sset) {
482	case ETH_SS_STATS:
483		return PCH_GBE_STATS_LEN;
484	default:
485		return -EOPNOTSUPP;
486	}
487}
488
489static const struct ethtool_ops pch_gbe_ethtool_ops = {
490	.get_settings = pch_gbe_get_settings,
491	.set_settings = pch_gbe_set_settings,
492	.get_drvinfo = pch_gbe_get_drvinfo,
493	.get_regs_len = pch_gbe_get_regs_len,
494	.get_regs = pch_gbe_get_regs,
495	.get_wol = pch_gbe_get_wol,
496	.set_wol = pch_gbe_set_wol,
497	.nway_reset = pch_gbe_nway_reset,
498	.get_link = ethtool_op_get_link,
499	.get_ringparam = pch_gbe_get_ringparam,
500	.set_ringparam = pch_gbe_set_ringparam,
501	.get_pauseparam = pch_gbe_get_pauseparam,
502	.set_pauseparam = pch_gbe_set_pauseparam,
503	.get_strings = pch_gbe_get_strings,
504	.get_ethtool_stats = pch_gbe_get_ethtool_stats,
505	.get_sset_count = pch_gbe_get_sset_count,
506};
507
508void pch_gbe_set_ethtool_ops(struct net_device *netdev)
509{
510	netdev->ethtool_ops = &pch_gbe_ethtool_ops;
511}
512