stmmac_ethtool.c 26.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12
/*******************************************************************************
  STMMAC Ethtool support

  Copyright (C) 2007-2009  STMicroelectronics Ltd


  Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
*******************************************************************************/

#include <linux/etherdevice.h>
#include <linux/ethtool.h>
13
#include <linux/interrupt.h>
14
#include <linux/mii.h>
15
#include <linux/phylink.h>
16
#include <linux/net_tstamp.h>
17
#include <asm/io.h>
18 19

#include "stmmac.h"
20
#include "dwmac_dma.h"
21
#include "dwxgmac2.h"
22

23
#define REG_SPACE_SIZE	0x1060
24 25
#define MAC100_ETHTOOL_NAME	"st_mac100"
#define GMAC_ETHTOOL_NAME	"st_gmac"
26
#define XGMAC_ETHTOOL_NAME	"st_xgmac"
27

28 29
#define ETHTOOL_DMA_OFFSET	55

30 31 32 33 34 35 36
struct stmmac_stats {
	char stat_string[ETH_GSTRING_LEN];
	int sizeof_stat;
	int stat_offset;
};

#define STMMAC_STAT(m)	\
37
	{ #m, sizeof_field(struct stmmac_extra_stats, m),	\
38 39
	offsetof(struct stmmac_priv, xstats.m)}

40
static const struct stmmac_stats stmmac_gstrings_stats[] = {
41
	/* Transmit errors */
42 43 44
	STMMAC_STAT(tx_underflow),
	STMMAC_STAT(tx_carrier),
	STMMAC_STAT(tx_losscarrier),
45
	STMMAC_STAT(vlan_tag),
46 47 48 49 50 51
	STMMAC_STAT(tx_deferred),
	STMMAC_STAT(tx_vlan),
	STMMAC_STAT(tx_jabber),
	STMMAC_STAT(tx_frame_flushed),
	STMMAC_STAT(tx_payload_error),
	STMMAC_STAT(tx_ip_header_error),
52
	/* Receive errors */
53
	STMMAC_STAT(rx_desc),
54 55 56
	STMMAC_STAT(sa_filter_fail),
	STMMAC_STAT(overflow_error),
	STMMAC_STAT(ipc_csum_error),
57
	STMMAC_STAT(rx_collision),
58
	STMMAC_STAT(rx_crc_errors),
59
	STMMAC_STAT(dribbling_bit),
60
	STMMAC_STAT(rx_length),
61 62 63 64 65 66 67 68
	STMMAC_STAT(rx_mii),
	STMMAC_STAT(rx_multicast),
	STMMAC_STAT(rx_gmac_overflow),
	STMMAC_STAT(rx_watchdog),
	STMMAC_STAT(da_rx_filter_fail),
	STMMAC_STAT(sa_rx_filter_fail),
	STMMAC_STAT(rx_missed_cntr),
	STMMAC_STAT(rx_overflow_cntr),
69
	STMMAC_STAT(rx_vlan),
70
	STMMAC_STAT(rx_split_hdr_pkt_n),
71
	/* Tx/Rx IRQ error info */
72 73 74 75 76 77 78 79 80
	STMMAC_STAT(tx_undeflow_irq),
	STMMAC_STAT(tx_process_stopped_irq),
	STMMAC_STAT(tx_jabber_irq),
	STMMAC_STAT(rx_overflow_irq),
	STMMAC_STAT(rx_buf_unav_irq),
	STMMAC_STAT(rx_process_stopped_irq),
	STMMAC_STAT(rx_watchdog_irq),
	STMMAC_STAT(tx_early_irq),
	STMMAC_STAT(fatal_bus_error_irq),
81 82
	/* Tx/Rx IRQ Events */
	STMMAC_STAT(rx_early_irq),
83 84 85
	STMMAC_STAT(threshold),
	STMMAC_STAT(tx_pkt_n),
	STMMAC_STAT(rx_pkt_n),
86
	STMMAC_STAT(normal_irq_n),
87 88 89 90
	STMMAC_STAT(rx_normal_irq_n),
	STMMAC_STAT(napi_poll),
	STMMAC_STAT(tx_normal_irq_n),
	STMMAC_STAT(tx_clean),
91
	STMMAC_STAT(tx_set_ic_bit),
92 93
	STMMAC_STAT(irq_receive_pmt_irq_n),
	/* MMC info */
94 95 96
	STMMAC_STAT(mmc_tx_irq_n),
	STMMAC_STAT(mmc_rx_irq_n),
	STMMAC_STAT(mmc_rx_csum_offload_irq_n),
97
	/* EEE */
98 99 100 101 102
	STMMAC_STAT(irq_tx_path_in_lpi_mode_n),
	STMMAC_STAT(irq_tx_path_exit_lpi_mode_n),
	STMMAC_STAT(irq_rx_path_in_lpi_mode_n),
	STMMAC_STAT(irq_rx_path_exit_lpi_mode_n),
	STMMAC_STAT(phy_eee_wakeup_error_n),
103 104 105 106 107 108
	/* Extended RDES status */
	STMMAC_STAT(ip_hdr_err),
	STMMAC_STAT(ip_payload_err),
	STMMAC_STAT(ip_csum_bypassed),
	STMMAC_STAT(ipv4_pkt_rcvd),
	STMMAC_STAT(ipv6_pkt_rcvd),
109 110 111 112 113 114 115 116 117 118 119
	STMMAC_STAT(no_ptp_rx_msg_type_ext),
	STMMAC_STAT(ptp_rx_msg_type_sync),
	STMMAC_STAT(ptp_rx_msg_type_follow_up),
	STMMAC_STAT(ptp_rx_msg_type_delay_req),
	STMMAC_STAT(ptp_rx_msg_type_delay_resp),
	STMMAC_STAT(ptp_rx_msg_type_pdelay_req),
	STMMAC_STAT(ptp_rx_msg_type_pdelay_resp),
	STMMAC_STAT(ptp_rx_msg_type_pdelay_follow_up),
	STMMAC_STAT(ptp_rx_msg_type_announce),
	STMMAC_STAT(ptp_rx_msg_type_management),
	STMMAC_STAT(ptp_rx_msg_pkt_reserved_type),
120 121 122 123 124 125 126 127 128
	STMMAC_STAT(ptp_frame_type),
	STMMAC_STAT(ptp_ver),
	STMMAC_STAT(timestamp_dropped),
	STMMAC_STAT(av_pkt_rcvd),
	STMMAC_STAT(av_tagged_pkt_rcvd),
	STMMAC_STAT(vlan_tag_priority_val),
	STMMAC_STAT(l3_filter_match),
	STMMAC_STAT(l4_filter_match),
	STMMAC_STAT(l3_l4_filter_no_match),
129 130 131 132
	/* PCS */
	STMMAC_STAT(irq_pcs_ane_n),
	STMMAC_STAT(irq_pcs_link_n),
	STMMAC_STAT(irq_rgmii_n),
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	/* DEBUG */
	STMMAC_STAT(mtl_tx_status_fifo_full),
	STMMAC_STAT(mtl_tx_fifo_not_empty),
	STMMAC_STAT(mmtl_fifo_ctrl),
	STMMAC_STAT(mtl_tx_fifo_read_ctrl_write),
	STMMAC_STAT(mtl_tx_fifo_read_ctrl_wait),
	STMMAC_STAT(mtl_tx_fifo_read_ctrl_read),
	STMMAC_STAT(mtl_tx_fifo_read_ctrl_idle),
	STMMAC_STAT(mac_tx_in_pause),
	STMMAC_STAT(mac_tx_frame_ctrl_xfer),
	STMMAC_STAT(mac_tx_frame_ctrl_idle),
	STMMAC_STAT(mac_tx_frame_ctrl_wait),
	STMMAC_STAT(mac_tx_frame_ctrl_pause),
	STMMAC_STAT(mac_gmii_tx_proto_engine),
	STMMAC_STAT(mtl_rx_fifo_fill_level_full),
	STMMAC_STAT(mtl_rx_fifo_fill_above_thresh),
	STMMAC_STAT(mtl_rx_fifo_fill_below_thresh),
	STMMAC_STAT(mtl_rx_fifo_fill_level_empty),
	STMMAC_STAT(mtl_rx_fifo_read_ctrl_flush),
	STMMAC_STAT(mtl_rx_fifo_read_ctrl_read_data),
	STMMAC_STAT(mtl_rx_fifo_read_ctrl_status),
	STMMAC_STAT(mtl_rx_fifo_read_ctrl_idle),
	STMMAC_STAT(mtl_rx_fifo_ctrl_active),
	STMMAC_STAT(mac_rx_frame_ctrl_fifo),
	STMMAC_STAT(mac_gmii_rx_proto_engine),
A
Alexandre TORGUE 已提交
158 159 160
	/* TSO */
	STMMAC_STAT(tx_tso_frames),
	STMMAC_STAT(tx_tso_nfrags),
161 162 163
};
#define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)

164 165
/* HW MAC Management counters (if supported) */
#define STMMAC_MMC_STAT(m)	\
166
	{ #m, sizeof_field(struct stmmac_counters, m),	\
167 168
	offsetof(struct stmmac_priv, mmc.m)}

169
static const struct stmmac_stats stmmac_mmc[] = {
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	STMMAC_MMC_STAT(mmc_tx_octetcount_gb),
	STMMAC_MMC_STAT(mmc_tx_framecount_gb),
	STMMAC_MMC_STAT(mmc_tx_broadcastframe_g),
	STMMAC_MMC_STAT(mmc_tx_multicastframe_g),
	STMMAC_MMC_STAT(mmc_tx_64_octets_gb),
	STMMAC_MMC_STAT(mmc_tx_65_to_127_octets_gb),
	STMMAC_MMC_STAT(mmc_tx_128_to_255_octets_gb),
	STMMAC_MMC_STAT(mmc_tx_256_to_511_octets_gb),
	STMMAC_MMC_STAT(mmc_tx_512_to_1023_octets_gb),
	STMMAC_MMC_STAT(mmc_tx_1024_to_max_octets_gb),
	STMMAC_MMC_STAT(mmc_tx_unicast_gb),
	STMMAC_MMC_STAT(mmc_tx_multicast_gb),
	STMMAC_MMC_STAT(mmc_tx_broadcast_gb),
	STMMAC_MMC_STAT(mmc_tx_underflow_error),
	STMMAC_MMC_STAT(mmc_tx_singlecol_g),
	STMMAC_MMC_STAT(mmc_tx_multicol_g),
	STMMAC_MMC_STAT(mmc_tx_deferred),
	STMMAC_MMC_STAT(mmc_tx_latecol),
	STMMAC_MMC_STAT(mmc_tx_exesscol),
	STMMAC_MMC_STAT(mmc_tx_carrier_error),
	STMMAC_MMC_STAT(mmc_tx_octetcount_g),
	STMMAC_MMC_STAT(mmc_tx_framecount_g),
	STMMAC_MMC_STAT(mmc_tx_excessdef),
	STMMAC_MMC_STAT(mmc_tx_pause_frame),
	STMMAC_MMC_STAT(mmc_tx_vlan_frame_g),
	STMMAC_MMC_STAT(mmc_rx_framecount_gb),
	STMMAC_MMC_STAT(mmc_rx_octetcount_gb),
	STMMAC_MMC_STAT(mmc_rx_octetcount_g),
	STMMAC_MMC_STAT(mmc_rx_broadcastframe_g),
	STMMAC_MMC_STAT(mmc_rx_multicastframe_g),
200
	STMMAC_MMC_STAT(mmc_rx_crc_error),
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	STMMAC_MMC_STAT(mmc_rx_align_error),
	STMMAC_MMC_STAT(mmc_rx_run_error),
	STMMAC_MMC_STAT(mmc_rx_jabber_error),
	STMMAC_MMC_STAT(mmc_rx_undersize_g),
	STMMAC_MMC_STAT(mmc_rx_oversize_g),
	STMMAC_MMC_STAT(mmc_rx_64_octets_gb),
	STMMAC_MMC_STAT(mmc_rx_65_to_127_octets_gb),
	STMMAC_MMC_STAT(mmc_rx_128_to_255_octets_gb),
	STMMAC_MMC_STAT(mmc_rx_256_to_511_octets_gb),
	STMMAC_MMC_STAT(mmc_rx_512_to_1023_octets_gb),
	STMMAC_MMC_STAT(mmc_rx_1024_to_max_octets_gb),
	STMMAC_MMC_STAT(mmc_rx_unicast_g),
	STMMAC_MMC_STAT(mmc_rx_length_error),
	STMMAC_MMC_STAT(mmc_rx_autofrangetype),
	STMMAC_MMC_STAT(mmc_rx_pause_frames),
	STMMAC_MMC_STAT(mmc_rx_fifo_overflow),
	STMMAC_MMC_STAT(mmc_rx_vlan_frames_gb),
	STMMAC_MMC_STAT(mmc_rx_watchdog_error),
	STMMAC_MMC_STAT(mmc_rx_ipc_intr_mask),
	STMMAC_MMC_STAT(mmc_rx_ipc_intr),
	STMMAC_MMC_STAT(mmc_rx_ipv4_gd),
	STMMAC_MMC_STAT(mmc_rx_ipv4_hderr),
	STMMAC_MMC_STAT(mmc_rx_ipv4_nopay),
	STMMAC_MMC_STAT(mmc_rx_ipv4_frag),
	STMMAC_MMC_STAT(mmc_rx_ipv4_udsbl),
	STMMAC_MMC_STAT(mmc_rx_ipv4_gd_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv4_hderr_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv4_nopay_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv4_frag_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv4_udsbl_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv6_gd_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv6_hderr_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv6_nopay_octets),
	STMMAC_MMC_STAT(mmc_rx_ipv6_gd),
	STMMAC_MMC_STAT(mmc_rx_ipv6_hderr),
	STMMAC_MMC_STAT(mmc_rx_ipv6_nopay),
	STMMAC_MMC_STAT(mmc_rx_udp_gd),
	STMMAC_MMC_STAT(mmc_rx_udp_err),
	STMMAC_MMC_STAT(mmc_rx_tcp_gd),
	STMMAC_MMC_STAT(mmc_rx_tcp_err),
	STMMAC_MMC_STAT(mmc_rx_icmp_gd),
	STMMAC_MMC_STAT(mmc_rx_icmp_err),
	STMMAC_MMC_STAT(mmc_rx_udp_gd_octets),
	STMMAC_MMC_STAT(mmc_rx_udp_err_octets),
	STMMAC_MMC_STAT(mmc_rx_tcp_gd_octets),
	STMMAC_MMC_STAT(mmc_rx_tcp_err_octets),
	STMMAC_MMC_STAT(mmc_rx_icmp_gd_octets),
	STMMAC_MMC_STAT(mmc_rx_icmp_err_octets),
249 250 251 252 253 254
	STMMAC_MMC_STAT(mmc_tx_fpe_fragment_cntr),
	STMMAC_MMC_STAT(mmc_tx_hold_req_cntr),
	STMMAC_MMC_STAT(mmc_rx_packet_assembly_err_cntr),
	STMMAC_MMC_STAT(mmc_rx_packet_smd_err_cntr),
	STMMAC_MMC_STAT(mmc_rx_packet_assembly_ok_cntr),
	STMMAC_MMC_STAT(mmc_rx_fpe_fragment_cntr),
255
};
256
#define STMMAC_MMC_STATS_LEN ARRAY_SIZE(stmmac_mmc)
257

258 259
static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
				      struct ethtool_drvinfo *info)
260 261 262
{
	struct stmmac_priv *priv = netdev_priv(dev);

263
	if (priv->plat->has_gmac || priv->plat->has_gmac4)
264
		strlcpy(info->driver, GMAC_ETHTOOL_NAME, sizeof(info->driver));
265 266
	else if (priv->plat->has_xgmac)
		strlcpy(info->driver, XGMAC_ETHTOOL_NAME, sizeof(info->driver));
267
	else
268 269
		strlcpy(info->driver, MAC100_ETHTOOL_NAME,
			sizeof(info->driver));
270

271
	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
272 273
}

274 275
static int stmmac_ethtool_get_link_ksettings(struct net_device *dev,
					     struct ethtool_link_ksettings *cmd)
276 277
{
	struct stmmac_priv *priv = netdev_priv(dev);
278

279 280
	if (priv->hw->pcs & STMMAC_PCS_RGMII ||
	    priv->hw->pcs & STMMAC_PCS_SGMII) {
281
		struct rgmii_adv adv;
282
		u32 supported, advertising, lp_advertising;
283 284

		if (!priv->xstats.pcs_link) {
285 286
			cmd->base.speed = SPEED_UNKNOWN;
			cmd->base.duplex = DUPLEX_UNKNOWN;
287 288
			return 0;
		}
289
		cmd->base.duplex = priv->xstats.pcs_duplex;
290

291
		cmd->base.speed = priv->xstats.pcs_speed;
292 293

		/* Get and convert ADV/LP_ADV from the HW AN registers */
294
		if (stmmac_pcs_get_adv_lp(priv, priv->ioaddr, &adv))
295 296 297 298
			return -EOPNOTSUPP;	/* should never happen indeed */

		/* Encoding of PSE bits is defined in 802.3z, 37.2.1.4 */

299 300 301 302 303 304 305
		ethtool_convert_link_mode_to_legacy_u32(
			&supported, cmd->link_modes.supported);
		ethtool_convert_link_mode_to_legacy_u32(
			&advertising, cmd->link_modes.advertising);
		ethtool_convert_link_mode_to_legacy_u32(
			&lp_advertising, cmd->link_modes.lp_advertising);

306
		if (adv.pause & STMMAC_PCS_PAUSE)
307
			advertising |= ADVERTISED_Pause;
308
		if (adv.pause & STMMAC_PCS_ASYM_PAUSE)
309
			advertising |= ADVERTISED_Asym_Pause;
310
		if (adv.lp_pause & STMMAC_PCS_PAUSE)
311
			lp_advertising |= ADVERTISED_Pause;
312
		if (adv.lp_pause & STMMAC_PCS_ASYM_PAUSE)
313
			lp_advertising |= ADVERTISED_Asym_Pause;
314 315

		/* Reg49[3] always set because ANE is always supported */
316 317 318 319
		cmd->base.autoneg = ADVERTISED_Autoneg;
		supported |= SUPPORTED_Autoneg;
		advertising |= ADVERTISED_Autoneg;
		lp_advertising |= ADVERTISED_Autoneg;
320 321

		if (adv.duplex) {
322 323 324 325 326 327
			supported |= (SUPPORTED_1000baseT_Full |
				      SUPPORTED_100baseT_Full |
				      SUPPORTED_10baseT_Full);
			advertising |= (ADVERTISED_1000baseT_Full |
					ADVERTISED_100baseT_Full |
					ADVERTISED_10baseT_Full);
328
		} else {
329 330 331 332 333 334
			supported |= (SUPPORTED_1000baseT_Half |
				      SUPPORTED_100baseT_Half |
				      SUPPORTED_10baseT_Half);
			advertising |= (ADVERTISED_1000baseT_Half |
					ADVERTISED_100baseT_Half |
					ADVERTISED_10baseT_Half);
335 336
		}
		if (adv.lp_duplex)
337 338 339
			lp_advertising |= (ADVERTISED_1000baseT_Full |
					   ADVERTISED_100baseT_Full |
					   ADVERTISED_10baseT_Full);
340
		else
341 342 343 344 345 346 347 348 349 350 351
			lp_advertising |= (ADVERTISED_1000baseT_Half |
					   ADVERTISED_100baseT_Half |
					   ADVERTISED_10baseT_Half);
		cmd->base.port = PORT_OTHER;

		ethtool_convert_legacy_u32_to_link_mode(
			cmd->link_modes.supported, supported);
		ethtool_convert_legacy_u32_to_link_mode(
			cmd->link_modes.advertising, advertising);
		ethtool_convert_legacy_u32_to_link_mode(
			cmd->link_modes.lp_advertising, lp_advertising);
352 353 354 355

		return 0;
	}

356
	return phylink_ethtool_ksettings_get(priv->phylink, cmd);
357 358
}

359 360 361
static int
stmmac_ethtool_set_link_ksettings(struct net_device *dev,
				  const struct ethtool_link_ksettings *cmd)
362 363 364
{
	struct stmmac_priv *priv = netdev_priv(dev);

365 366
	if (priv->hw->pcs & STMMAC_PCS_RGMII ||
	    priv->hw->pcs & STMMAC_PCS_SGMII) {
367 368 369
		u32 mask = ADVERTISED_Autoneg | ADVERTISED_Pause;

		/* Only support ANE */
370
		if (cmd->base.autoneg != AUTONEG_ENABLE)
371 372
			return -EINVAL;

P
Pavel Machek 已提交
373
		mask &= (ADVERTISED_1000baseT_Half |
374 375 376 377 378 379
			ADVERTISED_1000baseT_Full |
			ADVERTISED_100baseT_Half |
			ADVERTISED_100baseT_Full |
			ADVERTISED_10baseT_Half |
			ADVERTISED_10baseT_Full);

380
		mutex_lock(&priv->lock);
381
		stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
382
		mutex_unlock(&priv->lock);
383 384 385 386

		return 0;
	}

387
	return phylink_ethtool_ksettings_set(priv->phylink, cmd);
388 389
}

390
static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
391 392 393 394 395
{
	struct stmmac_priv *priv = netdev_priv(dev);
	return priv->msg_enable;
}

396
static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
397 398 399 400 401 402
{
	struct stmmac_priv *priv = netdev_priv(dev);
	priv->msg_enable = level;

}

403
static int stmmac_check_if_running(struct net_device *dev)
404 405 406 407 408 409
{
	if (!netif_running(dev))
		return -EBUSY;
	return 0;
}

410
static int stmmac_ethtool_get_regs_len(struct net_device *dev)
411
{
412 413 414 415
	struct stmmac_priv *priv = netdev_priv(dev);

	if (priv->plat->has_xgmac)
		return XGMAC_REGSIZE * 4;
416 417 418
	return REG_SPACE_SIZE;
}

419
static void stmmac_ethtool_gregs(struct net_device *dev,
420 421 422
			  struct ethtool_regs *regs, void *space)
{
	struct stmmac_priv *priv = netdev_priv(dev);
423
	u32 *reg_space = (u32 *) space;
424

425
	stmmac_dump_mac_regs(priv, priv->hw, reg_space);
426
	stmmac_dump_dma_regs(priv, priv->ioaddr, reg_space);
427 428 429 430 431 432 433

	if (!priv->plat->has_xgmac) {
		/* Copy DMA registers to where ethtool expects them */
		memcpy(&reg_space[ETHTOOL_DMA_OFFSET],
		       &reg_space[DMA_BUS_MODE / 4],
		       NUM_DWMAC1000_DMA_REGS * 4);
	}
434 435
}

436 437 438 439 440 441 442
static int stmmac_nway_reset(struct net_device *dev)
{
	struct stmmac_priv *priv = netdev_priv(dev);

	return phylink_ethtool_nway_reset(priv->phylink);
}

443 444 445 446 447
static void
stmmac_get_pauseparam(struct net_device *netdev,
		      struct ethtool_pauseparam *pause)
{
	struct stmmac_priv *priv = netdev_priv(netdev);
448
	struct rgmii_adv adv_lp;
449

450
	if (priv->hw->pcs && !stmmac_pcs_get_adv_lp(priv, priv->ioaddr, &adv_lp)) {
451 452 453 454
		pause->autoneg = 1;
		if (!adv_lp.pause)
			return;
	} else {
455
		phylink_ethtool_get_pauseparam(priv->phylink, pause);
456
	}
457 458 459 460 461 462 463
}

static int
stmmac_set_pauseparam(struct net_device *netdev,
		      struct ethtool_pauseparam *pause)
{
	struct stmmac_priv *priv = netdev_priv(netdev);
464
	struct rgmii_adv adv_lp;
465

466
	if (priv->hw->pcs && !stmmac_pcs_get_adv_lp(priv, priv->ioaddr, &adv_lp)) {
467 468 469
		pause->autoneg = 1;
		if (!adv_lp.pause)
			return -EOPNOTSUPP;
470
		return 0;
471
	} else {
472
		return phylink_ethtool_set_pauseparam(priv->phylink, pause);
473
	}
474 475 476 477 478 479
}

static void stmmac_get_ethtool_stats(struct net_device *dev,
				 struct ethtool_stats *dummy, u64 *data)
{
	struct stmmac_priv *priv = netdev_priv(dev);
480 481
	u32 rx_queues_count = priv->plat->rx_queues_to_use;
	u32 tx_queues_count = priv->plat->tx_queues_to_use;
482
	unsigned long count;
483
	int i, j = 0, ret;
484

485
	if (priv->dma_cap.asp) {
486
		for (i = 0; i < STMMAC_SAFETY_FEAT_SIZE; i++) {
487 488
			if (!stmmac_safety_feat_dump(priv, &priv->sstats, i,
						&count, NULL))
489 490 491 492
				data[j++] = count;
		}
	}

493
	/* Update the DMA HW counters for dwmac10/100 */
494 495 496
	ret = stmmac_dma_diagnostic_fr(priv, &dev->stats, (void *) &priv->xstats,
			priv->ioaddr);
	if (ret) {
497
		/* If supported, for new GMAC chips expose the MMC counters */
498
		if (priv->dma_cap.rmon) {
499
			stmmac_mmc_read(priv, priv->mmcaddr, &priv->mmc);
500

501 502 503
			for (i = 0; i < STMMAC_MMC_STATS_LEN; i++) {
				char *p;
				p = (char *)priv + stmmac_mmc[i].stat_offset;
504

505 506 507 508
				data[j++] = (stmmac_mmc[i].sizeof_stat ==
					     sizeof(u64)) ? (*(u64 *)p) :
					     (*(u32 *)p);
			}
509
		}
510
		if (priv->eee_enabled) {
511
			int val = phylink_get_eee_err(priv->phylink);
512 513 514
			if (val)
				priv->xstats.phy_eee_wakeup_error_n = val;
		}
515

516 517 518 519
		if (priv->synopsys_id >= DWMAC_CORE_3_50)
			stmmac_mac_debug(priv, priv->ioaddr,
					(void *)&priv->xstats,
					rx_queues_count, tx_queues_count);
520
	}
521 522
	for (i = 0; i < STMMAC_STATS_LEN; i++) {
		char *p = (char *)priv + stmmac_gstrings_stats[i].stat_offset;
523 524
		data[j++] = (stmmac_gstrings_stats[i].sizeof_stat ==
			     sizeof(u64)) ? (*(u64 *)p) : (*(u32 *)p);
525 526 527 528 529
	}
}

static int stmmac_get_sset_count(struct net_device *netdev, int sset)
{
530
	struct stmmac_priv *priv = netdev_priv(netdev);
531
	int i, len, safety_len = 0;
532

533 534
	switch (sset) {
	case ETH_SS_STATS:
535 536
		len = STMMAC_STATS_LEN;

537
		if (priv->dma_cap.rmon)
538
			len += STMMAC_MMC_STATS_LEN;
539
		if (priv->dma_cap.asp) {
540
			for (i = 0; i < STMMAC_SAFETY_FEAT_SIZE; i++) {
541 542 543
				if (!stmmac_safety_feat_dump(priv,
							&priv->sstats, i,
							NULL, NULL))
544 545 546 547 548
					safety_len++;
			}

			len += safety_len;
		}
549 550

		return len;
551 552
	case ETH_SS_TEST:
		return stmmac_selftest_get_count(priv);
553 554 555 556 557 558 559 560 561
	default:
		return -EOPNOTSUPP;
	}
}

static void stmmac_get_strings(struct net_device *dev, u32 stringset, u8 *data)
{
	int i;
	u8 *p = data;
562
	struct stmmac_priv *priv = netdev_priv(dev);
563 564 565

	switch (stringset) {
	case ETH_SS_STATS:
566
		if (priv->dma_cap.asp) {
567
			for (i = 0; i < STMMAC_SAFETY_FEAT_SIZE; i++) {
568 569 570 571
				const char *desc;
				if (!stmmac_safety_feat_dump(priv,
							&priv->sstats, i,
							NULL, &desc)) {
572 573 574 575 576
					memcpy(p, desc, ETH_GSTRING_LEN);
					p += ETH_GSTRING_LEN;
				}
			}
		}
577
		if (priv->dma_cap.rmon)
578
			for (i = 0; i < STMMAC_MMC_STATS_LEN; i++) {
579
				memcpy(p, stmmac_mmc[i].stat_string,
580 581 582
				       ETH_GSTRING_LEN);
				p += ETH_GSTRING_LEN;
			}
583 584 585 586 587 588
		for (i = 0; i < STMMAC_STATS_LEN; i++) {
			memcpy(p, stmmac_gstrings_stats[i].stat_string,
				ETH_GSTRING_LEN);
			p += ETH_GSTRING_LEN;
		}
		break;
589 590 591
	case ETH_SS_TEST:
		stmmac_selftest_get_strings(priv, p);
		break;
592 593 594 595 596 597 598 599 600 601 602
	default:
		WARN_ON(1);
		break;
	}
}

/* Currently only support WOL through Magic packet. */
static void stmmac_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct stmmac_priv *priv = netdev_priv(dev);

603
	mutex_lock(&priv->lock);
604
	if (device_can_wakeup(priv->device)) {
605
		wol->supported = WAKE_MAGIC | WAKE_UCAST;
606 607
		wol->wolopts = priv->wolopts;
	}
608
	mutex_unlock(&priv->lock);
609 610 611 612 613
}

static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct stmmac_priv *priv = netdev_priv(dev);
614
	u32 support = WAKE_MAGIC | WAKE_UCAST;
615

616 617 618 619 620 621
	/* By default almost all GMAC devices support the WoL via
	 * magic frame but we can disable it if the HW capability
	 * register shows no support for pmt_magic_frame. */
	if ((priv->hw_cap_support) && (!priv->dma_cap.pmt_magic_frame))
		wol->wolopts &= ~WAKE_MAGIC;

622
	if (!device_can_wakeup(priv->device))
623 624 625 626 627
		return -EINVAL;

	if (wol->wolopts & ~support)
		return -EINVAL;

628 629
	if (wol->wolopts) {
		pr_info("stmmac: wakeup enable\n");
630
		device_set_wakeup_enable(priv->device, 1);
631
		enable_irq_wake(priv->wol_irq);
632 633
	} else {
		device_set_wakeup_enable(priv->device, 0);
634
		disable_irq_wake(priv->wol_irq);
635
	}
636

637
	mutex_lock(&priv->lock);
638
	priv->wolopts = wol->wolopts;
639
	mutex_unlock(&priv->lock);
640 641 642 643

	return 0;
}

644 645 646 647 648 649 650 651 652 653 654 655
static int stmmac_ethtool_op_get_eee(struct net_device *dev,
				     struct ethtool_eee *edata)
{
	struct stmmac_priv *priv = netdev_priv(dev);

	if (!priv->dma_cap.eee)
		return -EOPNOTSUPP;

	edata->eee_enabled = priv->eee_enabled;
	edata->eee_active = priv->eee_active;
	edata->tx_lpi_timer = priv->tx_lpi_timer;

656
	return phylink_ethtool_get_eee(priv->phylink, edata);
657 658 659 660 661 662
}

static int stmmac_ethtool_op_set_eee(struct net_device *dev,
				     struct ethtool_eee *edata)
{
	struct stmmac_priv *priv = netdev_priv(dev);
663
	int ret;
664

665
	if (!edata->eee_enabled) {
666
		stmmac_disable_eee_mode(priv);
667
	} else {
668 669 670 671
		/* We are asking for enabling the EEE but it is safe
		 * to verify all by invoking the eee_init function.
		 * In case of failure it will return an error.
		 */
672 673
		edata->eee_enabled = stmmac_eee_init(priv);
		if (!edata->eee_enabled)
674 675 676
			return -EOPNOTSUPP;
	}

677
	ret = phylink_ethtool_set_eee(priv->phylink, edata);
678 679 680 681 682 683
	if (ret)
		return ret;

	priv->eee_enabled = edata->eee_enabled;
	priv->tx_lpi_timer = edata->tx_lpi_timer;
	return 0;
684 685
}

686 687
static u32 stmmac_usec2riwt(u32 usec, struct stmmac_priv *priv)
{
688
	unsigned long clk = clk_get_rate(priv->plat->stmmac_clk);
689

690 691 692 693 694
	if (!clk) {
		clk = priv->plat->clk_ref_rate;
		if (!clk)
			return 0;
	}
695 696 697 698 699 700

	return (usec * (clk / 1000000)) / 256;
}

static u32 stmmac_riwt2usec(u32 riwt, struct stmmac_priv *priv)
{
701
	unsigned long clk = clk_get_rate(priv->plat->stmmac_clk);
702

703 704 705 706 707
	if (!clk) {
		clk = priv->plat->clk_ref_rate;
		if (!clk)
			return 0;
	}
708 709 710 711 712 713 714 715 716 717 718 719

	return (riwt * 256) / (clk / 1000000);
}

static int stmmac_get_coalesce(struct net_device *dev,
			       struct ethtool_coalesce *ec)
{
	struct stmmac_priv *priv = netdev_priv(dev);

	ec->tx_coalesce_usecs = priv->tx_coal_timer;
	ec->tx_max_coalesced_frames = priv->tx_coal_frames;

720 721
	if (priv->use_riwt) {
		ec->rx_max_coalesced_frames = priv->rx_coal_frames;
722
		ec->rx_coalesce_usecs = stmmac_riwt2usec(priv->rx_riwt, priv);
723
	}
724 725 726 727 728 729 730 731

	return 0;
}

static int stmmac_set_coalesce(struct net_device *dev,
			       struct ethtool_coalesce *ec)
{
	struct stmmac_priv *priv = netdev_priv(dev);
732
	u32 rx_cnt = priv->plat->rx_queues_to_use;
733 734
	unsigned int rx_riwt;

735 736 737 738 739 740 741 742 743
	if (priv->use_riwt && (ec->rx_coalesce_usecs > 0)) {
		rx_riwt = stmmac_usec2riwt(ec->rx_coalesce_usecs, priv);

		if ((rx_riwt > MAX_DMA_RIWT) || (rx_riwt < MIN_DMA_RIWT))
			return -EINVAL;

		priv->rx_riwt = rx_riwt;
		stmmac_rx_watchdog(priv, priv->ioaddr, priv->rx_riwt, rx_cnt);
	}
744 745 746 747 748

	if ((ec->tx_coalesce_usecs == 0) &&
	    (ec->tx_max_coalesced_frames == 0))
		return -EINVAL;

749
	if ((ec->tx_coalesce_usecs > STMMAC_MAX_COAL_TX_TICK) ||
750 751 752 753 754 755
	    (ec->tx_max_coalesced_frames > STMMAC_TX_MAX_FRAMES))
		return -EINVAL;

	/* Only copy relevant parameters, ignore all others. */
	priv->tx_coal_frames = ec->tx_max_coalesced_frames;
	priv->tx_coal_timer = ec->tx_coalesce_usecs;
756
	priv->rx_coal_frames = ec->rx_max_coalesced_frames;
757 758 759
	return 0;
}

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
static int stmmac_get_rxnfc(struct net_device *dev,
			    struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
{
	struct stmmac_priv *priv = netdev_priv(dev);

	switch (rxnfc->cmd) {
	case ETHTOOL_GRXRINGS:
		rxnfc->data = priv->plat->rx_queues_to_use;
		break;
	default:
		return -EOPNOTSUPP;
	}

	return 0;
}

static u32 stmmac_get_rxfh_key_size(struct net_device *dev)
{
	struct stmmac_priv *priv = netdev_priv(dev);

	return sizeof(priv->rss.key);
}

static u32 stmmac_get_rxfh_indir_size(struct net_device *dev)
{
	struct stmmac_priv *priv = netdev_priv(dev);

	return ARRAY_SIZE(priv->rss.table);
}

static int stmmac_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
			   u8 *hfunc)
{
	struct stmmac_priv *priv = netdev_priv(dev);
	int i;

	if (indir) {
		for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
			indir[i] = priv->rss.table[i];
	}

	if (key)
		memcpy(key, priv->rss.key, sizeof(priv->rss.key));
	if (hfunc)
		*hfunc = ETH_RSS_HASH_TOP;

	return 0;
}

static int stmmac_set_rxfh(struct net_device *dev, const u32 *indir,
			   const u8 *key, const u8 hfunc)
{
	struct stmmac_priv *priv = netdev_priv(dev);
	int i;

	if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP))
		return -EOPNOTSUPP;

	if (indir) {
		for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
			priv->rss.table[i] = indir[i];
	}

	if (key)
		memcpy(priv->rss.key, key, sizeof(priv->rss.key));

	return stmmac_rss_configure(priv, priv->hw, &priv->rss,
				    priv->plat->rx_queues_to_use);
}

830 831 832 833 834
static int stmmac_get_ts_info(struct net_device *dev,
			      struct ethtool_ts_info *info)
{
	struct stmmac_priv *priv = netdev_priv(dev);

835
	if ((priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) {
836

837 838 839
		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
					SOF_TIMESTAMPING_TX_HARDWARE |
					SOF_TIMESTAMPING_RX_SOFTWARE |
840
					SOF_TIMESTAMPING_RX_HARDWARE |
841
					SOF_TIMESTAMPING_SOFTWARE |
842 843
					SOF_TIMESTAMPING_RAW_HARDWARE;

844 845 846
		if (priv->ptp_clock)
			info->phc_index = ptp_clock_index(priv->ptp_clock);

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
		info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);

		info->rx_filters = ((1 << HWTSTAMP_FILTER_NONE) |
				    (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
				    (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
				    (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
				    (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
				    (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
				    (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
				    (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
				    (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
				    (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
				    (1 << HWTSTAMP_FILTER_ALL));
		return 0;
	} else
		return ethtool_op_get_ts_info(dev, info);
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
static int stmmac_get_tunable(struct net_device *dev,
			      const struct ethtool_tunable *tuna, void *data)
{
	struct stmmac_priv *priv = netdev_priv(dev);
	int ret = 0;

	switch (tuna->id) {
	case ETHTOOL_RX_COPYBREAK:
		*(u32 *)data = priv->rx_copybreak;
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static int stmmac_set_tunable(struct net_device *dev,
			      const struct ethtool_tunable *tuna,
			      const void *data)
{
	struct stmmac_priv *priv = netdev_priv(dev);
	int ret = 0;

	switch (tuna->id) {
	case ETHTOOL_RX_COPYBREAK:
		priv->rx_copybreak = *(u32 *)data;
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

S
stephen hemminger 已提交
902
static const struct ethtool_ops stmmac_ethtool_ops = {
903 904
	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
				     ETHTOOL_COALESCE_MAX_FRAMES,
905 906 907 908 909 910 911
	.begin = stmmac_check_if_running,
	.get_drvinfo = stmmac_ethtool_getdrvinfo,
	.get_msglevel = stmmac_ethtool_getmsglevel,
	.set_msglevel = stmmac_ethtool_setmsglevel,
	.get_regs = stmmac_ethtool_gregs,
	.get_regs_len = stmmac_ethtool_get_regs_len,
	.get_link = ethtool_op_get_link,
912
	.nway_reset = stmmac_nway_reset,
913 914
	.get_pauseparam = stmmac_get_pauseparam,
	.set_pauseparam = stmmac_set_pauseparam,
915
	.self_test = stmmac_selftest_run,
916 917 918 919
	.get_ethtool_stats = stmmac_get_ethtool_stats,
	.get_strings = stmmac_get_strings,
	.get_wol = stmmac_get_wol,
	.set_wol = stmmac_set_wol,
920 921
	.get_eee = stmmac_ethtool_op_get_eee,
	.set_eee = stmmac_ethtool_op_set_eee,
922
	.get_sset_count	= stmmac_get_sset_count,
923 924 925 926 927
	.get_rxnfc = stmmac_get_rxnfc,
	.get_rxfh_key_size = stmmac_get_rxfh_key_size,
	.get_rxfh_indir_size = stmmac_get_rxfh_indir_size,
	.get_rxfh = stmmac_get_rxfh,
	.set_rxfh = stmmac_set_rxfh,
928
	.get_ts_info = stmmac_get_ts_info,
929 930
	.get_coalesce = stmmac_get_coalesce,
	.set_coalesce = stmmac_set_coalesce,
931 932
	.get_tunable = stmmac_get_tunable,
	.set_tunable = stmmac_set_tunable,
933 934
	.get_link_ksettings = stmmac_ethtool_get_link_ksettings,
	.set_link_ksettings = stmmac_ethtool_set_link_ksettings,
935 936 937 938
};

void stmmac_set_ethtool_ops(struct net_device *netdev)
{
939
	netdev->ethtool_ops = &stmmac_ethtool_ops;
940
}