Commit 4de0e8ef authored by Dario Binacchi's avatar Dario Binacchi Committed by Marc Kleine-Budde
Browse files

can: slcan: add ethtool support to reset adapter errors

This patch adds a private flag to the slcan driver to switch the
"err-rst-on-open" setting on and off.

"err-rst-on-open" on  - Reset error states on opening command

"err-rst-on-open" off - Don't reset error states on opening command
                        (default)

The setting can only be changed if the interface is down:

    ip link set dev can0 down
    ethtool --set-priv-flags can0 err-rst-on-open {off|on}
    ip link set dev can0 up

Link: https://lore.kernel.org/all/20220628163137.413025-11-dario.binacchi@amarulasolutions.com


Signed-off-by: default avatarDario Binacchi <dario.binacchi@amarulasolutions.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent 98b12064
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -4,3 +4,4 @@ obj-$(CONFIG_CAN_SLCAN) += slcan.o

slcan-objs :=
slcan-objs += slcan-core.o
slcan-objs += slcan-ethtool.o
+36 −0
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@
#include <linux/can/dev.h>
#include <linux/can/skb.h>

#include "slcan.h"

MODULE_ALIAS_LDISC(N_SLCAN);
MODULE_DESCRIPTION("serial line CAN interface");
MODULE_LICENSE("GPL");
@@ -98,6 +100,8 @@ struct slcan {
#define SLF_INUSE		0		/* Channel in use            */
#define SLF_ERROR		1               /* Parity, etc. error        */
#define SLF_XCMD		2               /* Command transmission      */
	unsigned long           cmd_flags;      /* Command flags             */
#define CF_ERR_RST		0               /* Reset errors on open      */
	wait_queue_head_t       xcmd_wait;      /* Wait queue for commands   */
						/* transmission              */
};
@@ -109,6 +113,28 @@ static const u32 slcan_bitrate_const[] = {
	250000, 500000, 800000, 1000000
};

bool slcan_err_rst_on_open(struct net_device *ndev)
{
	struct slcan *sl = netdev_priv(ndev);

	return !!test_bit(CF_ERR_RST, &sl->cmd_flags);
}

int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
{
	struct slcan *sl = netdev_priv(ndev);

	if (netif_running(ndev))
		return -EBUSY;

	if (on)
		set_bit(CF_ERR_RST, &sl->cmd_flags);
	else
		clear_bit(CF_ERR_RST, &sl->cmd_flags);

	return 0;
}

 /************************************************************************
  *			SLCAN ENCAPSULATION FORMAT			 *
  ************************************************************************/
@@ -510,6 +536,15 @@ static int slc_open(struct net_device *dev)
			goto cmd_transmit_failed;
		}

		if (test_bit(CF_ERR_RST, &sl->cmd_flags)) {
			err = slcan_transmit_cmd(sl, "F\r");
			if (err) {
				netdev_err(dev,
					   "failed to send error command 'F\\r'\n");
				goto cmd_transmit_failed;
			}
		}

		err = slcan_transmit_cmd(sl, "O\r");
		if (err) {
			netdev_err(dev, "failed to send open command 'O\\r'\n");
@@ -629,6 +664,7 @@ static struct slcan *slc_alloc(void)
	snprintf(dev->name, sizeof(dev->name), "slcan%d", i);
	dev->netdev_ops = &slc_netdev_ops;
	dev->base_addr  = i;
	slcan_set_ethtool_ops(dev);
	sl = netdev_priv(dev);

	/* Initialize channel control data */
+65 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0+
/* Copyright (c) 2022 Amarula Solutions, Dario Binacchi <dario.binacchi@amarulasolutions.com>
 *
 */

#include <linux/can/dev.h>
#include <linux/ethtool.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/platform_device.h>

#include "slcan.h"

static const char slcan_priv_flags_strings[][ETH_GSTRING_LEN] = {
#define SLCAN_PRIV_FLAGS_ERR_RST_ON_OPEN BIT(0)
	"err-rst-on-open",
};

static void slcan_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
{
	switch (stringset) {
	case ETH_SS_PRIV_FLAGS:
		memcpy(data, slcan_priv_flags_strings,
		       sizeof(slcan_priv_flags_strings));
	}
}

static u32 slcan_get_priv_flags(struct net_device *ndev)
{
	u32 flags = 0;

	if (slcan_err_rst_on_open(ndev))
		flags |= SLCAN_PRIV_FLAGS_ERR_RST_ON_OPEN;

	return flags;
}

static int slcan_set_priv_flags(struct net_device *ndev, u32 flags)
{
	bool err_rst_op_open = !!(flags & SLCAN_PRIV_FLAGS_ERR_RST_ON_OPEN);

	return slcan_enable_err_rst_on_open(ndev, err_rst_op_open);
}

static int slcan_get_sset_count(struct net_device *netdev, int sset)
{
	switch (sset) {
	case ETH_SS_PRIV_FLAGS:
		return ARRAY_SIZE(slcan_priv_flags_strings);
	default:
		return -EOPNOTSUPP;
	}
}

static const struct ethtool_ops slcan_ethtool_ops = {
	.get_strings = slcan_get_strings,
	.get_priv_flags = slcan_get_priv_flags,
	.set_priv_flags = slcan_set_priv_flags,
	.get_sset_count = slcan_get_sset_count,
};

void slcan_set_ethtool_ops(struct net_device *netdev)
{
	netdev->ethtool_ops = &slcan_ethtool_ops;
}
+18 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0
 * slcan.h - serial line CAN interface driver
 *
 * Copyright (C) Laurence Culhane <loz@holmes.demon.co.uk>
 * Copyright (C) Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
 * Copyright (C) Oliver Hartkopp <socketcan@hartkopp.net>
 * Copyright (C) 2022 Amarula Solutions, Dario Binacchi <dario.binacchi@amarulasolutions.com>
 *
 */

#ifndef _SLCAN_H
#define _SLCAN_H

bool slcan_err_rst_on_open(struct net_device *ndev);
int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on);
void slcan_set_ethtool_ops(struct net_device *ndev);

#endif /* _SLCAN_H */