ucc_geth_ethtool.c 10.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Li Yang 已提交
2 3 4 5 6 7 8
/*
 * Copyright (c) 2007 Freescale Semiconductor, Inc. All rights reserved.
 *
 * Description: QE UCC Gigabit Ethernet Ethtool API Set
 *
 * Author: Li Yang <leoli@freescale.com>
 *
9
 * Limitation:
10
 * Can only get/set settings of the first queue.
11
 * Need to re-open the interface manually after changing some parameters.
L
Li Yang 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/stddef.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/phy.h>

#include <asm/io.h>
#include <asm/irq.h>
31
#include <linux/uaccess.h>
L
Li Yang 已提交
32 33 34 35
#include <asm/types.h>

#include "ucc_geth.h"

36
static const char hw_stat_gstrings[][ETH_GSTRING_LEN] = {
L
Li Yang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	"tx-64-frames",
	"tx-65-127-frames",
	"tx-128-255-frames",
	"rx-64-frames",
	"rx-65-127-frames",
	"rx-128-255-frames",
	"tx-bytes-ok",
	"tx-pause-frames",
	"tx-multicast-frames",
	"tx-broadcast-frames",
	"rx-frames",
	"rx-bytes-ok",
	"rx-bytes-all",
	"rx-multicast-frames",
	"rx-broadcast-frames",
	"stats-counter-carry",
	"stats-counter-mask",
	"rx-dropped-frames",
};

57
static const char tx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
L
Li Yang 已提交
58 59
	"tx-single-collision",
	"tx-multiple-collision",
60
	"tx-late-collision",
L
Li Yang 已提交
61 62 63 64 65 66
	"tx-aborted-frames",
	"tx-lost-frames",
	"tx-carrier-sense-errors",
	"tx-frames-ok",
	"tx-excessive-differ-frames",
	"tx-256-511-frames",
67
	"tx-512-1023-frames",
L
Li Yang 已提交
68 69 70 71
	"tx-1024-1518-frames",
	"tx-jumbo-frames",
};

72
static const char rx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
L
Li Yang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	"rx-crc-errors",
	"rx-alignment-errors",
	"rx-in-range-length-errors",
	"rx-out-of-range-length-errors",
	"rx-too-long-frames",
	"rx-runt",
	"rx-very-long-event",
	"rx-symbol-errors",
	"rx-busy-drop-frames",
	"reserved",
	"reserved",
	"rx-mismatch-drop-frames",
	"rx-small-than-64",
	"rx-256-511-frames",
	"rx-512-1023-frames",
	"rx-1024-1518-frames",
	"rx-jumbo-frames",
	"rx-mac-error-loss",
	"rx-pause-frames",
	"reserved",
	"rx-vlan-removed",
	"rx-vlan-replaced",
	"rx-vlan-inserted",
	"rx-ip-checksum-errors",
};

#define UEC_HW_STATS_LEN ARRAY_SIZE(hw_stat_gstrings)
#define UEC_TX_FW_STATS_LEN ARRAY_SIZE(tx_fw_stat_gstrings)
#define UEC_RX_FW_STATS_LEN ARRAY_SIZE(rx_fw_stat_gstrings)

static int
104
uec_get_ksettings(struct net_device *netdev, struct ethtool_link_ksettings *cmd)
L
Li Yang 已提交
105 106 107 108 109 110 111
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	struct phy_device *phydev = ugeth->phydev;

	if (!phydev)
		return -ENODEV;

112 113 114
	phy_ethtool_ksettings_get(phydev, cmd);

	return 0;
L
Li Yang 已提交
115 116 117
}

static int
118 119
uec_set_ksettings(struct net_device *netdev,
		  const struct ethtool_link_ksettings *cmd)
L
Li Yang 已提交
120 121 122 123 124 125 126
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	struct phy_device *phydev = ugeth->phydev;

	if (!phydev)
		return -ENODEV;

127
	return phy_ethtool_ksettings_set(phydev, cmd);
L
Li Yang 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}

static void
uec_get_pauseparam(struct net_device *netdev,
                     struct ethtool_pauseparam *pause)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);

	pause->autoneg = ugeth->phydev->autoneg;

	if (ugeth->ug_info->receiveFlowControl)
		pause->rx_pause = 1;
	if (ugeth->ug_info->transmitFlowControl)
		pause->tx_pause = 1;
}

static int
uec_set_pauseparam(struct net_device *netdev,
                     struct ethtool_pauseparam *pause)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	int ret = 0;

	ugeth->ug_info->receiveFlowControl = pause->rx_pause;
	ugeth->ug_info->transmitFlowControl = pause->tx_pause;
153

L
Li Yang 已提交
154 155 156
	if (ugeth->phydev->autoneg) {
		if (netif_running(netdev)) {
			/* FIXME: automatically restart */
157
			netdev_info(netdev, "Please re-open the interface\n");
L
Li Yang 已提交
158 159 160 161 162 163 164 165 166 167 168 169 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 200 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
		}
	} else {
		struct ucc_geth_info *ug_info = ugeth->ug_info;

		ret = init_flow_control_params(ug_info->aufc,
					ug_info->receiveFlowControl,
					ug_info->transmitFlowControl,
					ug_info->pausePeriod,
					ug_info->extensionField,
					&ugeth->uccf->uf_regs->upsmr,
					&ugeth->ug_regs->uempr,
					&ugeth->ug_regs->maccfg1);
	}

	return ret;
}

static uint32_t
uec_get_msglevel(struct net_device *netdev)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	return ugeth->msg_enable;
}

static void
uec_set_msglevel(struct net_device *netdev, uint32_t data)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	ugeth->msg_enable = data;
}

static int
uec_get_regs_len(struct net_device *netdev)
{
	return sizeof(struct ucc_geth);
}

static void
uec_get_regs(struct net_device *netdev,
               struct ethtool_regs *regs, void *p)
{
	int i;
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	u32 __iomem *ug_regs = (u32 __iomem *)ugeth->ug_regs;
	u32 *buff = p;

	for (i = 0; i < sizeof(struct ucc_geth) / sizeof(u32); i++)
		buff[i] = in_be32(&ug_regs[i]);
}

static void
uec_get_ringparam(struct net_device *netdev,
                    struct ethtool_ringparam *ring)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	struct ucc_geth_info *ug_info = ugeth->ug_info;
	int queue = 0;

	ring->rx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
	ring->rx_mini_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
	ring->rx_jumbo_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
	ring->tx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;

	ring->rx_pending = ug_info->bdRingLenRx[queue];
	ring->rx_mini_pending = ug_info->bdRingLenRx[queue];
	ring->rx_jumbo_pending = ug_info->bdRingLenRx[queue];
	ring->tx_pending = ug_info->bdRingLenTx[queue];
}

static int
uec_set_ringparam(struct net_device *netdev,
                    struct ethtool_ringparam *ring)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	struct ucc_geth_info *ug_info = ugeth->ug_info;
	int queue = 0, ret = 0;

	if (ring->rx_pending < UCC_GETH_RX_BD_RING_SIZE_MIN) {
236 237
		netdev_info(netdev, "RxBD ring size must be no smaller than %d\n",
			    UCC_GETH_RX_BD_RING_SIZE_MIN);
L
Li Yang 已提交
238 239 240
		return -EINVAL;
	}
	if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
241 242
		netdev_info(netdev, "RxBD ring size must be multiple of %d\n",
			    UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
L
Li Yang 已提交
243 244 245
		return -EINVAL;
	}
	if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
246 247
		netdev_info(netdev, "TxBD ring size must be no smaller than %d\n",
			    UCC_GETH_TX_BD_RING_SIZE_MIN);
L
Li Yang 已提交
248 249 250
		return -EINVAL;
	}

251 252 253
	if (netif_running(netdev))
		return -EBUSY;

L
Li Yang 已提交
254 255 256 257 258 259
	ug_info->bdRingLenRx[queue] = ring->rx_pending;
	ug_info->bdRingLenTx[queue] = ring->tx_pending;

	return ret;
}

260
static int uec_get_sset_count(struct net_device *netdev, int sset)
L
Li Yang 已提交
261 262 263 264 265
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	u32 stats_mode = ugeth->ug_info->statisticsMode;
	int len = 0;

266 267 268 269 270 271 272 273 274 275
	switch (sset) {
	case ETH_SS_STATS:
		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
			len += UEC_HW_STATS_LEN;
		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
			len += UEC_TX_FW_STATS_LEN;
		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
			len += UEC_RX_FW_STATS_LEN;

		return len;
L
Li Yang 已提交
276

277 278 279
	default:
		return -EOPNOTSUPP;
	}
L
Li Yang 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

static void uec_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	u32 stats_mode = ugeth->ug_info->statisticsMode;

	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
		memcpy(buf, hw_stat_gstrings, UEC_HW_STATS_LEN *
			       	ETH_GSTRING_LEN);
		buf += UEC_HW_STATS_LEN * ETH_GSTRING_LEN;
	}
	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
		memcpy(buf, tx_fw_stat_gstrings, UEC_TX_FW_STATS_LEN *
			       	ETH_GSTRING_LEN);
		buf += UEC_TX_FW_STATS_LEN * ETH_GSTRING_LEN;
	}
	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
J
Joakim Tjernlund 已提交
298
		memcpy(buf, rx_fw_stat_gstrings, UEC_RX_FW_STATS_LEN *
L
Li Yang 已提交
299 300 301 302 303 304 305 306 307 308 309 310
			       	ETH_GSTRING_LEN);
}

static void uec_get_ethtool_stats(struct net_device *netdev,
		struct ethtool_stats *stats, uint64_t *data)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	u32 stats_mode = ugeth->ug_info->statisticsMode;
	u32 __iomem *base;
	int i, j = 0;

	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
311 312 313 314 315
		if (ugeth->ug_regs)
			base = (u32 __iomem *)&ugeth->ug_regs->tx64;
		else
			base = NULL;

L
Li Yang 已提交
316
		for (i = 0; i < UEC_HW_STATS_LEN; i++)
317
			data[j++] = base ? in_be32(&base[i]) : 0;
L
Li Yang 已提交
318 319 320 321
	}
	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
		base = (u32 __iomem *)ugeth->p_tx_fw_statistics_pram;
		for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
322
			data[j++] = base ? in_be32(&base[i]) : 0;
L
Li Yang 已提交
323 324 325 326
	}
	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
		base = (u32 __iomem *)ugeth->p_rx_fw_statistics_pram;
		for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
327
			data[j++] = base ? in_be32(&base[i]) : 0;
L
Li Yang 已提交
328 329 330 331 332 333 334 335
	}
}

/* Report driver information */
static void
uec_get_drvinfo(struct net_device *netdev,
                       struct ethtool_drvinfo *drvinfo)
{
336 337 338
	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
	strlcpy(drvinfo->bus_info, "QUICC ENGINE", sizeof(drvinfo->bus_info));
L
Li Yang 已提交
339 340
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
#ifdef CONFIG_PM

static void uec_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	struct phy_device *phydev = ugeth->phydev;

	if (phydev && phydev->irq)
		wol->supported |= WAKE_PHY;
	if (qe_alive_during_sleep())
		wol->supported |= WAKE_MAGIC;

	wol->wolopts = ugeth->wol_en;
}

static int uec_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
{
	struct ucc_geth_private *ugeth = netdev_priv(netdev);
	struct phy_device *phydev = ugeth->phydev;

	if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
		return -EINVAL;
	else if (wol->wolopts & WAKE_PHY && (!phydev || !phydev->irq))
		return -EINVAL;
	else if (wol->wolopts & WAKE_MAGIC && !qe_alive_during_sleep())
		return -EINVAL;

	ugeth->wol_en = wol->wolopts;
	device_set_wakeup_enable(&netdev->dev, ugeth->wol_en);

	return 0;
}

#else
#define uec_get_wol NULL
#define uec_set_wol NULL
#endif /* CONFIG_PM */

L
Li Yang 已提交
379 380 381 382 383 384
static const struct ethtool_ops uec_ethtool_ops = {
	.get_drvinfo            = uec_get_drvinfo,
	.get_regs_len           = uec_get_regs_len,
	.get_regs               = uec_get_regs,
	.get_msglevel           = uec_get_msglevel,
	.set_msglevel           = uec_set_msglevel,
385
	.nway_reset             = phy_ethtool_nway_reset,
L
Li Yang 已提交
386 387 388 389 390
	.get_link               = ethtool_op_get_link,
	.get_ringparam          = uec_get_ringparam,
	.set_ringparam          = uec_set_ringparam,
	.get_pauseparam         = uec_get_pauseparam,
	.set_pauseparam         = uec_set_pauseparam,
391
	.get_sset_count		= uec_get_sset_count,
L
Li Yang 已提交
392 393
	.get_strings            = uec_get_strings,
	.get_ethtool_stats      = uec_get_ethtool_stats,
394 395
	.get_wol		= uec_get_wol,
	.set_wol		= uec_set_wol,
396
	.get_ts_info		= ethtool_op_get_ts_info,
397 398
	.get_link_ksettings	= uec_get_ksettings,
	.set_link_ksettings	= uec_set_ksettings,
L
Li Yang 已提交
399 400 401 402
};

void uec_set_ethtool_ops(struct net_device *netdev)
{
403
	netdev->ethtool_ops = &uec_ethtool_ops;
L
Li Yang 已提交
404
}