can: slcan: add ethtool support to reset adapter errors
authorDario Binacchi <dario.binacchi@amarulasolutions.com>
Tue, 28 Jun 2022 16:31:34 +0000 (18:31 +0200)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Sun, 3 Jul 2022 09:34:44 +0000 (11:34 +0200)
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: Dario Binacchi <dario.binacchi@amarulasolutions.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/slcan/Makefile
drivers/net/can/slcan/slcan-core.c
drivers/net/can/slcan/slcan-ethtool.c [new file with mode: 0644]
drivers/net/can/slcan/slcan.h [new file with mode: 0644]

index 2e84f7b..8a88e48 100644 (file)
@@ -4,3 +4,4 @@ obj-$(CONFIG_CAN_SLCAN) += slcan.o
 
 slcan-objs :=
 slcan-objs += slcan-core.o
+slcan-objs += slcan-ethtool.o
index 249b5ad..c1fd1e9 100644 (file)
@@ -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 */
diff --git a/drivers/net/can/slcan/slcan-ethtool.c b/drivers/net/can/slcan/slcan-ethtool.c
new file mode 100644 (file)
index 0000000..bf0afdc
--- /dev/null
@@ -0,0 +1,65 @@
+// 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;
+}
diff --git a/drivers/net/can/slcan/slcan.h b/drivers/net/can/slcan/slcan.h
new file mode 100644 (file)
index 0000000..d463c8d
--- /dev/null
@@ -0,0 +1,18 @@
+/* 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 */