Skip to contentSkip to conversation

Introducing ETAS ES58X CAN USB interfaces

PATCH Superseded Vincent Mailhol 2 participants 9 messages Mar 21, 2021 – Apr 2, 2021

Subsystems (2)

v14 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v14 cover Introducing ETAS ES58X CAN USB interfaces

Here comes the 14th iteration of the patch. This new version addresses the comments received from Jimmy and Marc (thanks again for the review!).

** Changelog **

Changes in v14 (2021-03-21):
- Rework the split into core support, es581_4, es58x_fd (cosmetic change) so that es581_4.h and es58x_fd.h headers can be added in one block.
- Add a fourth patch to introduce a helper function es58x_netdev_queue_set_dql_min_limit() to set up dql minimum limit (in parallel, I will try to have this merged in the network device header)
- Remove unused function es58x_add_skb_idx(): leftover from the old FIFO logic, should have been removed in v11.
- Fix memory leak in es58x_get_product_info(): buffer was not freed in case of error in usb_sting().
- s/loopback/echo and s/self reception/echo: in the driver the terms "loopback, self reception, and echo" were all used to designated the same thing. Renamed structures, variables and comments accordingly to make it more consistent.
- Remove CAN_CTRLMODE_LOOPBACK. The driver never supported this feature. It was added due to a confusion with the echo skb.
- Use the new can_free_echo_skb() which return the frame_len.
- Do the statistics handling in es58x_rx_err_msg() even if kalloc() fails.
- Replace array es58x_cmd_ret_desc[] by helper function

    es58x_cmd_ret_desc().

- Other trivial changes (c.f. below link for details)
Reference: https://lore.kernel.org/linux-can/[email protected]/T/#mbcace9c13b19a504cd28d81591f983b95eb66657

Changes in v13 (2021-03-19 by Marc Kleine-Budde):
- split the driver into 3 patches, so that it can be send via

    mailing lists (core support, es581_4, es58x_fd)

- Remove the dql.min_limit settings
- typo and kernel doc fixes
Reference: https://lore.kernel.org/linux-can/[email protected]/T/#t

Changes in v12 (2021-03-09):
- Rework the queue stop/wake management so that spinlocks are not
needed anymore.
- es58x_start_xmit(): check for valid SKB using
can_dropped_invalid_skb().
- Implemented TDC according to latest patches in linux-can-next.
Reference: https://lore.kernel.org/linux-can/[email protected]/T/#t

Changes in v11 (2021-01-23):
- Remove all WARN_ON() calls: these were use during development, relevant tests are done not to trigger these.
- es58x_start_xmit(): added net_ratelimit() condition to prevent spamming dmesg.
- add a new es58x_xmit_more() function and simplify the code of

    es58x_start_xmit().

- Removed functions {es581_4,es58x_fd}_print_conf() which were only there for debug.
- Additional comment for es58x_fd_param.bitrate_max.
- Make the device FIFO size a power of two and modify the echo_skb indexes logic to prevent the use of spinlocks.

Changes in v10 (2021-01-12):
- Rebased on linux-can-next/testing and modified according to latest
BQL patches.
Reference: https://lore.kernel.org/linux-can/[email protected]/T/#m5f99d4da8e8934a75f9481ecc3137b59f3762413
- Replaced __netdev_sent_queue() by netdev_sent_queue().

Changes in v9 (2021-01-09):
- es58x_start_xmit(): do not use skb anymore after the call of can_put_echo_skb(). Rationale: can_put_echo_skb() calls skb_clone() and thus the original skb gets consumed (i.e. use after free issue).
- es58x_start_xmit(): Add a "drop_skb" label to free the skb when errors occur.

Changes in v8 (2021-01-04):
- The driver requires CRC16. Modified Kconfig accordingly.

Changes in v7 (2020-11-17):
- Fix compilation issue if CONFIG_BQL is not set.
Reference: https://lkml.org/lkml/2020/11/15/163

Changes in v6 (2020-11-15):
- Rebase the patch on the testing branch of linux-can-next.
- Rename the helper functions according latest changes

    (e.g. can_cc_get_len() -> can_cc_dlc2len())
  - Fix comments of enum es58x_physical_layer and enum
    es58x_sync_edge.

Changes in v5 (2020-11-07):
- Add support for DLC greater than 8.
- All other patches from the previous series were either accepted or dismissed. As such, this is not a series any more but a single patch.

Changes in v4 (2020-10-17):
- Remove struct es58x_abstracted_can_frame.
- Fix formatting (spaces, comment style).
- Transform macros into static inline functions when possible.
- Fix the ctrlmode_supported flags in es581_4.c and removed misleading comments in enum es58x_samples_per_bit.
- Rename enums according to the type.
- Remove function es58x_can_put_echo_skb().
Reference: https://lkml.org/lkml/2020/10/10/53

Changes in v3 (2020-10-03):
- Remove all the calls to likely() and unlikely().
Reference: https://lkml.org/lkml/2020/9/30/995

Changes in v2 (2020-09-30):
- Fixed -W1 warnings (v1 was tested with GCC -WExtra but not with -W1).

v1 (2020-09-27):
- First release

Vincent Mailhol (4):
can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces
can: etas_es58x: add support for ETAS ES581.4 CAN USB interface
can: etas_es58x: add support for the ETAS ES58X_FD CAN USB interfaces
can: etas_es58x: add es58x_netdev_queue_set_dql_min_limit()

 drivers/net/can/usb/Kconfig                 |   10 +
 drivers/net/can/usb/Makefile                |    1 +
 drivers/net/can/usb/etas_es58x/Makefile     |    3 +
 drivers/net/can/usb/etas_es58x/es581_4.c    |  501 ++++
 drivers/net/can/usb/etas_es58x/es581_4.h    |  207 ++
 drivers/net/can/usb/etas_es58x/es58x_core.c | 2403 +++++++++++++++++++
 drivers/net/can/usb/etas_es58x/es58x_core.h |  696 ++++++
 drivers/net/can/usb/etas_es58x/es58x_fd.c   |  577 +++++
 drivers/net/can/usb/etas_es58x/es58x_fd.h   |  243 ++
 9 files changed, 4641 insertions(+)
 create mode 100644 drivers/net/can/usb/etas_es58x/Makefile
 create mode 100644 drivers/net/can/usb/etas_es58x/es581_4.c
 create mode 100644 drivers/net/can/usb/etas_es58x/es581_4.h
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_core.c
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_core.h
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_fd.c
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_fd.h
Signature · 1 line
2.26.2
#1 lore raw

PATCH v14 1/4 can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces

This patch adds the core support for various USB CAN interfaces from ETAS GmbH (https://www.etas.com/en/products/es58x.php). The next patches add the glue code drivers for the individual interfaces.

---
 drivers/net/can/usb/Kconfig                 |   10 +
 drivers/net/can/usb/Makefile                |    1 +
 drivers/net/can/usb/etas_es58x/Makefile     |    3 +
 drivers/net/can/usb/etas_es58x/es58x_core.c | 2374 +++++++++++++++++++
 drivers/net/can/usb/etas_es58x/es58x_core.h |  662 ++++++
 5 files changed, 3050 insertions(+)
 create mode 100644 drivers/net/can/usb/etas_es58x/Makefile
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_core.c
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_core.h
drivers/net/can/usb/Kconfig +10 -0
index c1e5d5b570b6..978746ce8873 100644
@@ -20,6 +20,16 @@ config CAN_ESD_USB2
 	  This driver supports the CAN-USB/2 interface
 	  from esd electronic system design gmbh (http://www.esd.eu).
 
+config CAN_ETAS_ES58X
+	tristate "ETAS ES58X CAN/USB interfaces"
+	select CRC16
+	help
+	  This driver supports the ES581.4, ES582.1 and ES584.1 interfaces
+	  from ETAS GmbH (https://www.etas.com/en/products/es58x.php).
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called etas_es58x.
+
 config CAN_GS_USB
 	tristate "Geschwister Schneider UG interfaces"
 	help
drivers/net/can/usb/Makefile +1 -0
index aa0f17c0b2ed..748cf31a0d53 100644
@@ -6,6 +6,7 @@
 obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o
 obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
 obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o
+obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x/
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
drivers/net/can/usb/etas_es58x/Makefile new file +3 -0
new file mode 100644
index 000000000000..60a1ac935a69
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x.o
+etas_es58x-y = es58x_core.o
drivers/net/can/usb/etas_es58x/es58x_core.c new file +2374 -0

2,374 lines not on this page · raw message

drivers/net/can/usb/etas_es58x/es58x_core.h new file +662 -0

600 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.26.2

Part of the diff is not shown here. View the raw message.

#2 lore raw

Re: [PATCH v14 1/4] can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces

Quoted 8 lines · Vincent Mailhol

This patch adds the core support for various USB CAN interfaces from ETAS GmbH (https://www.etas.com/en/products/es58x.php). The next patches add the glue code drivers for the individual interfaces.

---

[...]

Quoted 29 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +29 -0
+/**
+ * es58x_rx_err_msg() - Handle a received CAN event or error message.
+ * @netdev: CAN network device.
+ * @error: Error code.
+ * @event: Event code.
+ * @timestamp: Timestamp received from a ES58X device.
+ *
+ * Handle the errors and events received by the ES58X device, create
+ * a CAN error skb and post it.
+ *
+ * In some rare cases the devices might get stuck alternating between
+ * CAN_STATE_ERROR_PASSIVE and CAN_STATE_ERROR_WARNING. To prevent
+ * this behavior, we force a bus off state if the device goes in
+ * CAN_STATE_ERROR_WARNING for ES58X_MAX_CONSECUTIVE_WARN consecutive
+ * times with no successful transmission or reception in between.
+ *
+ * Once the device is in bus off state, the only way to restart it is
+ * through the drivers/net/can/dev.c:can_restart() function. The
+ * device is technically capable to recover by itself under certain
+ * circumstances, however, allowing self recovery would create
+ * complex race conditions with drivers/net/can/dev.c:can_restart()
+ * and thus was not implemented. To activate automatic restart, please
+ * set the restart-ms parameter (e.g. ip link set can0 type can
+ * restart-ms 100).
+ *
+ * If the bus is really instable, this function would try to send a
+ * lot of log messages. Those are rate limited (i.e. you will see
+ * messages such as "net_ratelimit: XXX callbacks suppressed" in
+ * dmesg).

drivers/net/can/usb/etas_es58x/es58x_core.c line 641

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
+		     enum es58x_event event, u64 timestamp)
+{
+	struct es58x_priv *priv = es58x_priv(netdev);
+	struct can_priv *can = netdev_priv(netdev);
+	struct can_device_stats *can_stats = &can->can_stats;
+	struct can_frame *cf;

= NULL;

So that the if (cf) properly works...

Quoted 16 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+	struct sk_buff *skb;
+
+	if (!netif_running(netdev)) {
+		if (net_ratelimit())
+			netdev_info(netdev, "%s: %s is down, dropping packet\n",
+				    __func__, netdev->name);
+		netdev->stats.rx_dropped++;
+		return 0;
+	}
+
+	if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {
+		netdev_err(netdev, "%s: Both error and event are zero\n",
+			   __func__);
+		return -EINVAL;
+	}
+

drivers/net/can/usb/etas_es58x/es58x_core.c line 667

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+	skb = alloc_can_err_skb(netdev, &cf);
+
+	switch (error) {
+	case ES58X_ERR_OK:	/* 0: No error */
+		break;
+
+	case ES58X_ERR_PROT_STUFF:
+		if (net_ratelimit())
+			netdev_dbg(netdev, "Error BITSUFF\n");
+		if (cf)

..like here.

Quoted 2 lines
(quoted diff) +2 -0
+			cf->data[2] |= CAN_ERR_PROT_STUFF;
+		break;

[...]

drivers/net/can/usb/etas_es58x/es58x_core.c line 1191

Quoted 7 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +7 -0
+/**
+ * es58x_flush_cmd_buf() - Reset the buffer for URB commands.
+ * @es58x_dev: ES58X device.
+ */
+static void es58x_flush_cmd_buf(struct es58x_device *es58x_dev)
+{
+	memset(&es58x_dev->rx_cmd_buf, 0, es58x_dev->param->rx_urb_cmd_max_len);

I think you can skip the memset, as you're overwriting the data anyways and account the valid length.

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+	es58x_dev->rx_cmd_buf_len = 0;
+}
+

[...]

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+/**
+ * es58x_split_urb() - Cut the received URB in individual URB commands.
+ * @es58x_dev: ES58X device.
+ * @urb: last urb buffer received.
+ *
+ * The device might send urb in bulk format (i.e. several URB commands
+ * concatenated together). This function will split all the commands
+ * contained in the urb.
+ *
+ * Return:

drivers/net/can/usb/etas_es58x/es58x_core.c line 1343

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+ * number of bytes consumed from @urb if successful.
+ *
+ * -ENODATA if the URB command is still incomplete.
+ *
+ * -EBADMSG if the URB command is incorrect.
+ */
+static signed int es58x_split_urb(struct es58x_device *es58x_dev,
+				  struct urb *urb)
+{
+	const u8 es58x_is_alive = 0x11;

We usually use #defines for this

drivers/net/can/usb/etas_es58x/es58x_core.c line 1346

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+	union es58x_urb_cmd *urb_cmd;
+	u8 *raw_cmd = urb->transfer_buffer;
+	ssize_t raw_cmd_len = urb->actual_length;

urb->actual_length is an u32

Quoted 12 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +12 -0
+	int ret;
+
+	if (es58x_dev->rx_cmd_buf_len != 0) {
+		ret = es58x_handle_incomplete_cmd(es58x_dev, urb);
+		if (ret != -ENODATA)
+			es58x_flush_cmd_buf(es58x_dev);
+		if (ret < 0)
+			return ret;
+
+		raw_cmd += ret;
+		raw_cmd_len -= ret;
+	}

drivers/net/can/usb/etas_es58x/es58x_core.c line 1368

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+
+	while (raw_cmd_len > 0) {
+		if (raw_cmd[0] == es58x_is_alive) {
+			raw_cmd++;
+			raw_cmd_len--;
+			continue;
+		}
+		urb_cmd = (union es58x_urb_cmd *)raw_cmd;
+		ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
+		if (ret > 0) {

Here the length field in the usb_cmd is valid, it lies within the total length of the rx'ed urb.

drivers/net/can/usb/etas_es58x/es58x_core.c line 1369

Quoted 1 line
drivers/net/can/usb/etas_es58x/es58x_core.c new file +1 -0
+			es58x_handle_urb_cmd(es58x_dev, urb_cmd);

As far as I see you're checking in ops->handle_urb_cmd() if the urb_cmd's length is long enough for the command that's currently processed, right?

Quoted 16 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+		} else if (ret == -ENODATA) {
+			es58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len);
+			return -ENODATA;
+		} else if (ret < 0) {
+			ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,
+							   raw_cmd_len);
+			if (ret < 0)
+				return ret;
+		}
+		raw_cmd += ret;
+		raw_cmd_len -= ret;
+	}
+
+	return 0;
+}
+

[...]

Quoted 9 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +9 -0
+/**
+ * es58x_open() - Open and start network device.
+ * @netdev: CAN network device.
+ *
+ * Called when the network transitions to the up state.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_open(struct net_device *netdev)

drivers/net/can/usb/etas_es58x/es58x_core.c line 1780

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+{
+	int ret;
+
+	ret = open_candev(netdev);
+	if (ret)
+		return ret;
+
+	ret = es58x_enable_channel(netdev);
+	if (ret)
+		return ret;

Please do an as complete as possible reset and configuration during open(). If there is any error a ifdown/ifup should fix it. Here on a USB device with multiple interfaces it's not as easy as on devices with only one CAN interface.

Quoted 5 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +5 -0
+
+	netif_start_queue(netdev);
+
+	return ret;
+}

[...]

Quoted 34 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +34 -0
+/**
+ * es58x_start_xmit() - Transmit an skb.
+ * @skb: socket buffer of a CAN message.
+ * @netdev: CAN network device.
+ *
+ * Called when a packet needs to be transmitted.
+ *
+ * This function relies on Byte Queue Limits (BQL). The main benefit
+ * it to increase the throughput by allowing bulk transfers
+ * (c.f. xmit_more flag).
+ *
+ * Queues up to tx_bulk_max messages in &tx_urb buffer and does
+ * a bulk send of all messages in one single URB.
+ *
+ * Return:
+ * NETDEV_TX_OK if we could manage the @skb (either transmit it or
+ * drop it)
+ *
+ * NETDEV_TX_BUSY if the device is busy (this is a bug, the network
+ * device stop/wake management should prevent this return code to
+ * occur).
+ */
+static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
+				    struct net_device *netdev)
+{
+	struct es58x_priv *priv = es58x_priv(netdev);
+	struct es58x_device *es58x_dev = priv->es58x_dev;
+	unsigned int packet_idx, frame_len;
+	int ret;
+
+	if (can_dropped_invalid_skb(netdev, skb)) {
+		if (priv->tx_urb)
+			goto xmit_commit;
+		return NETDEV_TX_OK;

drivers/net/can/usb/etas_es58x/es58x_core.c line 1901

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+	}
+
+	if (!priv->tx_urb) {
+		priv->tx_urb = es58x_get_tx_urb(es58x_dev);
+		if (!priv->tx_urb) {
+			ret = -ENOMEM;
+			goto drop_skb;
+		}
+		memset(priv->tx_urb->transfer_buffer, 0,
+		       es58x_dev->param->tx_urb_cmd_max_len);

Is this memset() needed?

drivers/net/can/usb/etas_es58x/es58x_core.c line 1911

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+		priv->tx_urb->transfer_buffer_length =
+		    es58x_dev->param->urb_cmd_header_len;
+		priv->tx_can_msg_cnt = 0;
+		priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
+	} else if (priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
+		/* Can not do bulk send with mixed CAN and CAN FD frames. */
+		ret = es58x_xmit_commit(netdev);
+		if (ret)
+			goto drop_skb;
+		return es58x_start_xmit(skb, netdev);

In the kernel the stack is limited, does it make sense to re-arrange this to avoid recursion?

Quoted 4 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+	}
+
+	ret = es58x_dev->ops->tx_can_msg(priv, skb);
+	if (ret)

drivers/net/can/usb/etas_es58x/es58x_core.c line 1925

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+		goto drop_skb;
+
+	frame_len = can_skb_get_frame_len(skb);
+	netdev_sent_queue(netdev, frame_len);
+	packet_idx = priv->tx_head + priv->tx_can_msg_cnt;
+	ret = can_put_echo_skb(skb, netdev,
+			       packet_idx & es58x_dev->param->fifo_mask,
+			       frame_len);
+	if (ret)
+		goto xmit_failure;

In case can_put_echo_skb() fails, there will be no echo skb, so your cleanup will miss the last skb put into netdev_sent_queue(). I think its best to move the netdev_sent_queue() after the can_put_echo_skb().

Quoted 30 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +30 -0
+
+	priv->tx_can_msg_cnt++;
+
+ xmit_commit:
+	if (!es58x_xmit_more(priv)) {
+		ret = es58x_xmit_commit(netdev);
+		if (ret)
+			goto xmit_failure;
+	}
+
+	return NETDEV_TX_OK;
+
+ drop_skb:
+	dev_kfree_skb(skb);
+	netdev->stats.tx_dropped++;
+ xmit_failure:
+	netdev->stats.tx_errors++;
+	es58x_flush_pending_tx_msg(netdev);
+	netdev_warn(netdev, "%s: send message failure: %pe\n",
+		    __func__, ERR_PTR(ret));
+	return NETDEV_TX_OK;
+}
+
+static const struct net_device_ops es58x_netdev_ops = {
+	.ndo_open = es58x_open,
+	.ndo_stop = es58x_stop,
+	.ndo_start_xmit = es58x_start_xmit
+};
+
+/**

drivers/net/can/usb/etas_es58x/es58x_core.c line 1965

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+ * es58x_set_bittiming() - Configure all the nominal bit timing
+ *	parameters.
+ * @netdev: CAN network device.
+ *
+ * Only relevant for ES581.4 (the ES58X FD family sets the bittiming
+ * parameters at the same time as the channel gets enabled).
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_set_bittiming(struct net_device *netdev)

There's no need to implement this callback, you can configure the bitiming during open().

Quoted 19 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +19 -0
+{
+	struct es58x_priv *priv = es58x_priv(netdev);
+	struct es58x_device *es58x_dev = priv->es58x_dev;
+	int ret;
+
+	/* Make sure that channel is closed before sending configuration. */
+	ret = es58x_dev->ops->disable_channel(priv);
+	if (ret)
+		netdev_err(netdev, "%s: Could not disable the channel: %pe\n",
+			   __func__, ERR_PTR(ret));
+	ret = es58x_dev->ops->set_bittiming(priv);
+	if (ret)
+		netdev_err(netdev, "%s: Could not set bittiming: %pe\n",
+			   __func__, ERR_PTR(ret));
+
+	return ret;
+}
+
+/**

drivers/net/can/usb/etas_es58x/es58x_core.c line 1994

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+ * es58x_set_data_bittiming() - Configure all the data bit timing parameters.
+ * @netdev: CAN network device.
+ *
+ * Dummy function: ES58X FD family sets the bittiming
+ * parameters at the same time as the channel gets enabled.
+ *
+ * Return: zero.
+ */
+static int es58x_set_data_bittiming(struct net_device *netdev)
+{

no need to implement a no-op here.

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+	return 0;
+}
+

[...]

Quoted 11 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +11 -0
+/**
+ * es58x_set_mode() - Change network device mode.
+ * @netdev: CAN network device.
+ * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP
+ *
+ * This function is used by drivers/net/can/dev.c:can_restart() to
+ * recover from a bus off state. Thus only the only value expected for
+ * @mode is %CAN_MODE_START. None the less, other modes were
+ * implemented as well.
+ *
+ * Return: zero on success, errno when any error occurs.

drivers/net/can/usb/etas_es58x/es58x_core.c line 2051

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+ */
+static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
+{
+	struct can_priv *can = netdev_priv(netdev);
+
+	switch (mode) {
+	case CAN_MODE_START:
+		switch (can->state) {
+		case CAN_STATE_BUS_OFF:
+			return es58x_reset(netdev);

If you shutdown the channel in case of a bus off, this get easier. See bittiming comments below, too.

Quoted 39 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +39 -0
+
+		case CAN_STATE_STOPPED:
+		case CAN_STATE_SLEEPING:
+			netif_wake_queue(netdev);
+			can->state = CAN_STATE_ERROR_ACTIVE;
+			return 0;
+
+		case CAN_STATE_ERROR_ACTIVE:
+		case CAN_STATE_ERROR_WARNING:
+		case CAN_STATE_ERROR_PASSIVE:
+		default:
+			return 0;
+		}
+
+	case CAN_MODE_STOP:
+	case CAN_MODE_SLEEP:
+		netif_stop_queue(netdev);
+		if (mode == CAN_MODE_STOP)
+			can->state = CAN_STATE_STOPPED;
+		else
+			can->state = CAN_STATE_SLEEPING;
+		return 0;
+
+	default:
+		netdev_err(netdev, "%s: Invalid can_mode value: %d\n",
+			   __func__, mode);
+		return -EOPNOTSUPP;
+	}
+}
+
+/**
+ * es58x_init_priv() - Initialize private parameters.
+ * @es58x_dev: ES58X device.
+ * @priv: ES58X private parameters related to the network device.
+ * @channel_idx: Index of the network device.
+ */
+static void es58x_init_priv(struct es58x_device *es58x_dev,
+			    struct es58x_priv *priv, int channel_idx)
+{

drivers/net/can/usb/etas_es58x/es58x_core.c line 2100

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+	const struct es58x_parameters *param = es58x_dev->param;
+	struct can_priv *can = &priv->can;
+
+	priv->es58x_dev = es58x_dev;
+	priv->channel_idx = channel_idx;
+	priv->tx_urb = NULL;
+	priv->tx_can_msg_cnt = 0;
+
+	can->bittiming_const = param->bittiming_const;
+	can->do_set_bittiming = es58x_set_bittiming;

Can you keep the channels and/or devices disabled while the interface(s) are down? You don't need to implement this callback, it's sufficient to set the bittiming during the ndo_open() callback.

drivers/net/can/usb/etas_es58x/es58x_core.c line 2104

Quoted 4 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+	if (param->ctrlmode_supported & CAN_CTRLMODE_FD) {
+		can->data_bittiming_const = param->data_bittiming_const;
+		can->tdc_const = param->tdc_const;
+		can->do_set_data_bittiming = es58x_set_data_bittiming;

no need to provide this callback.

Quoted 29 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +29 -0
+	}
+	can->bitrate_max = param->bitrate_max;
+	can->clock = param->clock;
+	can->state = CAN_STATE_STOPPED;
+	can->ctrlmode_supported = param->ctrlmode_supported;
+	can->do_set_mode = es58x_set_mode;
+}
+
+/**
+ * es58x_init_netdev() - Initialize the network device.
+ * @es58x_dev: ES58X device.
+ * @channel_idx: Index of the network device.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
+{
+	struct net_device *netdev;
+	struct device *dev = es58x_dev->dev;
+	int ret;
+
+	netdev = alloc_candev(sizeof(struct es58x_priv),
+			      es58x_dev->param->fifo_mask + 1);
+	if (!netdev) {
+		dev_err(dev, "Could not allocate candev\n");
+		return -ENOMEM;
+	}
+	SET_NETDEV_DEV(netdev, dev);
+	es58x_dev->netdev[channel_idx] = netdev;

drivers/net/can/usb/etas_es58x/es58x_core.c line 2143

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+	es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);
+
+	netdev->netdev_ops = &es58x_netdev_ops;
+	netdev->flags |= IFF_ECHO;	/* We support local echo */
+
+	ret = register_candev(netdev);
+	if (ret)
+		return ret;
+	netdev_dbg(netdev, "%s: Registered channel %s\n",
+		   es58x_dev->udev->product, netdev->name);

In this function and in the following ones there are a lot of netdev_dbg(), please have a look if you can remove some of those.

[...]

Quoted 7 lines

+/**
+ * struct es58x_priv - All information specific to a CAN channel.
+ * @can: struct can_priv must be the first member (Socket CAN relies
+ * on the fact that function netdev_priv() returns a pointer to
+ * a struct can_priv).
+ * @es58x_dev: pointer to the corresponding ES58X device.
+ * @tx_urb: Used as a buffer to concatenate the TX messages and to do

Quoted 8 lines

+ * a bulk send. Please refer to es58x_start_xmit() for more
+ * details.
+ * @tx_tail: Index of the oldest packet still pending for
+ * completion. @tx_tail & echo_skb_mask represents the beginning
+ * of the echo skb FIFO, i.e. index of the first element.
+ * @tx_head: Index of the next packet to be sent to the
+ * device. @tx_head & echo_skb_mask represents the end of the
+ * echo skb FIFO plus one, i.e. the first free index.

Is this the description 100% correct? In xmit() you're using tx_head + tx_can_msg_cnt to address the next packet to be send.

Quoted 1 line

drivers/net/can/usb/etas_es58x/es58x_core.c line 1920

Quoted 1 line
drivers/net/can/usb/etas_es58x/es58x_core.c new file +1 -0
+	packet_idx = priv->tx_head + priv->tx_can_msg_cnt;

But in xmit_more() you're not taking tx_can_msg_cnt into account.

Quoted 18 lines

+ * @tx_can_msg_cnt: Number of messages in @tx_urb.
+ * @tx_can_msg_is_fd: false: all messages in @tx_urb are Classical
+ * CAN, true: all messages in @tx_urb are CAN FD. Rationale:
+ * ES58X FD devices do not allow to mix Classical CAN and FD CAN
+ * frames in one single bulk transmission.
+ * @err_passive_before_rtx_success: The ES58X device might enter in a
+ * state in which it keeps alternating between error passive
+ * and active states. This counter keeps track of the number of
+ * error passive and if it gets bigger than
+ * ES58X_CONSECUTIVE_ERR_PASSIVE_MAX, es58x_rx_err_msg() will
+ * force the status to bus-off.
+ * @channel_idx: Channel index, starts at zero.
+ */

drivers/net/can/usb/etas_es58x/es58x_core.h new file +5 -0
+struct es58x_priv {
+	struct can_priv can;
+	struct es58x_device *es58x_dev;
+	struct urb *tx_urb;
+

drivers/net/can/usb/etas_es58x/es58x_core.h line 247

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.h new file +10 -0
+	u32 tx_tail;
+	u32 tx_head;
+
+	u8 tx_can_msg_cnt;
+	bool tx_can_msg_is_fd;
+
+	u8 err_passive_before_rtx_success;
+
+	u8 channel_idx;
+};

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#3 lore raw

Re: [PATCH v14 1/4] can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces

Hi Marc,

Quoted 52 lines · Marc Kleine-Budde
Quoted 8 lines · Vincent Mailhol

This patch adds the core support for various USB CAN interfaces from ETAS GmbH (https://www.etas.com/en/products/es58x.php). The next patches add the glue code drivers for the individual interfaces.

---

[...]

Quoted 29 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +29 -0
+/**
+ * es58x_rx_err_msg() - Handle a received CAN event or error message.
+ * @netdev: CAN network device.
+ * @error: Error code.
+ * @event: Event code.
+ * @timestamp: Timestamp received from a ES58X device.
+ *
+ * Handle the errors and events received by the ES58X device, create
+ * a CAN error skb and post it.
+ *
+ * In some rare cases the devices might get stuck alternating between
+ * CAN_STATE_ERROR_PASSIVE and CAN_STATE_ERROR_WARNING. To prevent
+ * this behavior, we force a bus off state if the device goes in
+ * CAN_STATE_ERROR_WARNING for ES58X_MAX_CONSECUTIVE_WARN consecutive
+ * times with no successful transmission or reception in between.
+ *
+ * Once the device is in bus off state, the only way to restart it is
+ * through the drivers/net/can/dev.c:can_restart() function. The
+ * device is technically capable to recover by itself under certain
+ * circumstances, however, allowing self recovery would create
+ * complex race conditions with drivers/net/can/dev.c:can_restart()
+ * and thus was not implemented. To activate automatic restart, please
+ * set the restart-ms parameter (e.g. ip link set can0 type can
+ * restart-ms 100).
+ *
+ * If the bus is really instable, this function would try to send a
+ * lot of log messages. Those are rate limited (i.e. you will see
+ * messages such as "net_ratelimit: XXX callbacks suppressed" in
+ * dmesg).

drivers/net/can/usb/etas_es58x/es58x_core.c line 641

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
+                  enum es58x_event event, u64 timestamp)
+{
+     struct es58x_priv *priv = es58x_priv(netdev);
+     struct can_priv *can = netdev_priv(netdev);
+     struct can_device_stats *can_stats = &can->can_stats;
+     struct can_frame *cf;

= NULL;

Quoted 1 line

So that the if (cf) properly works...

I actually expected cfto be set to NULL when an error occurred (same as skb).

But indeed, cf is not initialised if the allocation fails. I was wondering if it would not be better to make both alloc_can_skb() and and alloc_canfd_skb() set cf to NULL. That would remove a pitfall.

If you like the idea, I can submit a patch.

Quoted 20 lines
Quoted 20 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +20 -0
+     struct sk_buff *skb;
+
+     if (!netif_running(netdev)) {
+             if (net_ratelimit())
+                     netdev_info(netdev, "%s: %s is down, dropping packet\n",
+                                 __func__, netdev->name);
+             netdev->stats.rx_dropped++;
+             return 0;
+     }
+
+     if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {
+             netdev_err(netdev, "%s: Both error and event are zero\n",
+                        __func__);
+             return -EINVAL;
+     }
+
+     skb = alloc_can_err_skb(netdev, &cf);
+
+     switch (error) {
+     case ES58X_ERR_OK:      /* 0: No error */
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 667

Quoted 6 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +6 -0
+             break;
+
+     case ES58X_ERR_PROT_STUFF:
+             if (net_ratelimit())
+                     netdev_dbg(netdev, "Error BITSUFF\n");
+             if (cf)

..like here.

ACK.

Quoted 5 lines
Quoted 2 lines
(quoted diff) +2 -0
+                     cf->data[2] |= CAN_ERR_PROT_STUFF;
+             break;

[...]

Quoted 10 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1191

Quoted 7 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +7 -0
+/**
+ * es58x_flush_cmd_buf() - Reset the buffer for URB commands.
+ * @es58x_dev: ES58X device.
+ */
+static void es58x_flush_cmd_buf(struct es58x_device *es58x_dev)
+{
+     memset(&es58x_dev->rx_cmd_buf, 0, es58x_dev->param->rx_urb_cmd_max_len);

I think you can skip the memset, as you're overwriting the data anyways and account the valid length.

The idea was that this buffer might contain confidential data (e.g. an UDS seed and key pair or a VIN) so I cleared it out of precaution.

I recognize that this is useless as an attacker can just retrieve the information from a candump, no need to look into some kernel temporary buffer for that...

I will remove it.

Quoted 20 lines
Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     es58x_dev->rx_cmd_buf_len = 0;
+}
+

[...]

Quoted 14 lines

+/**
+ * es58x_split_urb() - Cut the received URB in individual URB commands.
+ * @es58x_dev: ES58X device.
+ * @urb: last urb buffer received.
+ *
+ * The device might send urb in bulk format (i.e. several URB commands
+ * concatenated together). This function will split all the commands
+ * contained in the urb.
+ *
+ * Return:
+ * number of bytes consumed from @urb if successful.
+ *
+ * -ENODATA if the URB command is still incomplete.
+ *

Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1343

Quoted 6 lines

+ * -EBADMSG if the URB command is incorrect.
+ */

drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+static signed int es58x_split_urb(struct es58x_device *es58x_dev,
+                               struct urb *urb)
+{
+     const u8 es58x_is_alive = 0x11;

We usually use #defines for this

ACK.

Quoted 5 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1346

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     union es58x_urb_cmd *urb_cmd;
+     u8 *raw_cmd = urb->transfer_buffer;
+     ssize_t raw_cmd_len = urb->actual_length;

urb->actual_length is an u32

Below, I test that (raw_cmd_len > 0) in a while loop. I want to make this variable signed to avoid an infinite loop in case of an off by one error (which should not occur, see this as a safety net).

I will make this a s32.

Also, the size is represented by a u16 on the device so s32 is more than enough.

Quoted 26 lines
Quoted 12 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +12 -0
+     int ret;
+
+     if (es58x_dev->rx_cmd_buf_len != 0) {
+             ret = es58x_handle_incomplete_cmd(es58x_dev, urb);
+             if (ret != -ENODATA)
+                     es58x_flush_cmd_buf(es58x_dev);
+             if (ret < 0)
+                     return ret;
+
+             raw_cmd += ret;
+             raw_cmd_len -= ret;
+     }

drivers/net/can/usb/etas_es58x/es58x_core.c line 1368

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+
+     while (raw_cmd_len > 0) {
+             if (raw_cmd[0] == es58x_is_alive) {
+                     raw_cmd++;
+                     raw_cmd_len--;
+                     continue;
+             }
+             urb_cmd = (union es58x_urb_cmd *)raw_cmd;
+             ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
+             if (ret > 0) {

Here the length field in the usb_cmd is valid, it lies within the total length of the rx'ed urb.

Quoted 5 lines
Quoted 1 line

+ es58x_handle_urb_cmd(es58x_dev, urb_cmd);

As far as I see you're checking in ops->handle_urb_cmd() if the urb_cmd's length is long enough for the command that's currently processed, right?

The check of the urb_cmd's length occurs in es58x_check_rx_urb():

    if (urb_cmd_len > param->rx_urb_cmd_max_len) {
In that same function, I then check that urb_cmd's length is long enough:
    } else if (urb_actual_len < urb_cmd_len) {

If it is not long enough, the not-yet-consumed data of the urb is buffered and once a new urb comes up, those are concatenated.

After that, ops->handle_urb_cmd() will dispatch the command depending on the cmd_id and additional checks will be done on the size of the payload.

Quoted 35 lines
Quoted 16 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+             } else if (ret == -ENODATA) {
+                     es58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len);
+                     return -ENODATA;
+             } else if (ret < 0) {
+                     ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,
+                                                        raw_cmd_len);
+                     if (ret < 0)
+                             return ret;
+             }
+             raw_cmd += ret;
+             raw_cmd_len -= ret;
+     }
+
+     return 0;
+}
+

[...]

Quoted 16 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+/**
+ * es58x_open() - Open and start network device.
+ * @netdev: CAN network device.
+ *
+ * Called when the network transitions to the up state.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_open(struct net_device *netdev)
+{
+     int ret;
+
+     ret = open_candev(netdev);
+     if (ret)
+             return ret;
+
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1780

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     ret = es58x_enable_channel(netdev);
+     if (ret)
+             return ret;

Please do an as complete as possible reset and configuration during open(). If there is any error a ifdown/ifup should fix it. Here on a USB device with multiple interfaces it's not as easy as on devices with only one CAN interface.

ACK.

I will use a function as below to check if all interfaces are down.

static bool es58x_are_all_channels_closed(struct es58x_device *es58x_dev)

{
    int i;

    for (i = 0; i < es58x_dev->num_can_ch; i++) {
        struct can_priv *can_priv = netdev_priv(es58x_dev->netdev[i]);
        if (can_priv->state != CAN_STATE_STOPPED)
            return false;
    }
    return true;
}

I will modify both es58x_open() and es58x_close().

Quoted 46 lines
Quoted 5 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +5 -0
+
+     netif_start_queue(netdev);
+
+     return ret;
+}

[...]

Quoted 38 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +38 -0
+/**
+ * es58x_start_xmit() - Transmit an skb.
+ * @skb: socket buffer of a CAN message.
+ * @netdev: CAN network device.
+ *
+ * Called when a packet needs to be transmitted.
+ *
+ * This function relies on Byte Queue Limits (BQL). The main benefit
+ * it to increase the throughput by allowing bulk transfers
+ * (c.f. xmit_more flag).
+ *
+ * Queues up to tx_bulk_max messages in &tx_urb buffer and does
+ * a bulk send of all messages in one single URB.
+ *
+ * Return:
+ * NETDEV_TX_OK if we could manage the @skb (either transmit it or
+ * drop it)
+ *
+ * NETDEV_TX_BUSY if the device is busy (this is a bug, the network
+ * device stop/wake management should prevent this return code to
+ * occur).
+ */
+static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
+                                 struct net_device *netdev)
+{
+     struct es58x_priv *priv = es58x_priv(netdev);
+     struct es58x_device *es58x_dev = priv->es58x_dev;
+     unsigned int packet_idx, frame_len;
+     int ret;
+
+     if (can_dropped_invalid_skb(netdev, skb)) {
+             if (priv->tx_urb)
+                     goto xmit_commit;
+             return NETDEV_TX_OK;
+     }
+
+     if (!priv->tx_urb) {
+             priv->tx_urb = es58x_get_tx_urb(es58x_dev);
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1901

Quoted 6 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +6 -0
+             if (!priv->tx_urb) {
+                     ret = -ENOMEM;
+                     goto drop_skb;
+             }
+             memset(priv->tx_urb->transfer_buffer, 0,
+                    es58x_dev->param->tx_urb_cmd_max_len);

Is this memset() needed?

In current state, it is, but a small modification should be enough to have it removed.

Quoted 11 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1911

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+             priv->tx_urb->transfer_buffer_length =
+                 es58x_dev->param->urb_cmd_header_len;
+             priv->tx_can_msg_cnt = 0;
+             priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
+     } else if (priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
+             /* Can not do bulk send with mixed CAN and CAN FD frames. */
+             ret = es58x_xmit_commit(netdev);
+             if (ret)
+                     goto drop_skb;
+             return es58x_start_xmit(skb, netdev);
Quoted 2 lines

In the kernel the stack is limited, does it make sense to re-arrange this to avoid recursion?

This is a tail recursion so with the good optimisation, it will not increase the stack. And also, the recursive call occurs one time max. So I thought it was acceptable.

Regardless, rearranging the code is trivial, so I will remove this recursion.

Quoted 10 lines
Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+     }
+
+     ret = es58x_dev->ops->tx_can_msg(priv, skb);
+     if (ret)
+             goto drop_skb;
+
+     frame_len = can_skb_get_frame_len(skb);
+     netdev_sent_queue(netdev, frame_len);
+     packet_idx = priv->tx_head + priv->tx_can_msg_cnt;
+     ret = can_put_echo_skb(skb, netdev,
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1925

Quoted 4 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+                            packet_idx & es58x_dev->param->fifo_mask,
+                            frame_len);
+     if (ret)
+             goto xmit_failure;

In case can_put_echo_skb() fails, there will be no echo skb, so your cleanup will miss the last skb put into netdev_sent_queue(). I think its best to move the netdev_sent_queue() after the can_put_echo_skb().

ACK.

Quoted 35 lines
Quoted 35 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +35 -0
+
+     priv->tx_can_msg_cnt++;
+
+ xmit_commit:
+     if (!es58x_xmit_more(priv)) {
+             ret = es58x_xmit_commit(netdev);
+             if (ret)
+                     goto xmit_failure;
+     }
+
+     return NETDEV_TX_OK;
+
+ drop_skb:
+     dev_kfree_skb(skb);
+     netdev->stats.tx_dropped++;
+ xmit_failure:
+     netdev->stats.tx_errors++;
+     es58x_flush_pending_tx_msg(netdev);
+     netdev_warn(netdev, "%s: send message failure: %pe\n",
+                 __func__, ERR_PTR(ret));
+     return NETDEV_TX_OK;
+}
+
+static const struct net_device_ops es58x_netdev_ops = {
+     .ndo_open = es58x_open,
+     .ndo_stop = es58x_stop,
+     .ndo_start_xmit = es58x_start_xmit
+};
+
+/**
+ * es58x_set_bittiming() - Configure all the nominal bit timing
+ *   parameters.
+ * @netdev: CAN network device.
+ *
+ * Only relevant for ES581.4 (the ES58X FD family sets the bittiming
Quoted 8 lines
Quoted 5 lines

+ * parameters at the same time as the channel gets enabled).
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_set_bittiming(struct net_device *netdev)

There's no need to implement this callback, you can configure the bitiming during open().

ACK. Will remove both es58x_set_bittiming() and
es58x_set_data_bittiming().

Quoted 23 lines
Quoted 23 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +23 -0
+{
+     struct es58x_priv *priv = es58x_priv(netdev);
+     struct es58x_device *es58x_dev = priv->es58x_dev;
+     int ret;
+
+     /* Make sure that channel is closed before sending configuration. */
+     ret = es58x_dev->ops->disable_channel(priv);
+     if (ret)
+             netdev_err(netdev, "%s: Could not disable the channel: %pe\n",
+                        __func__, ERR_PTR(ret));
+     ret = es58x_dev->ops->set_bittiming(priv);
+     if (ret)
+             netdev_err(netdev, "%s: Could not set bittiming: %pe\n",
+                        __func__, ERR_PTR(ret));
+
+     return ret;
+}
+
+/**
+ * es58x_set_data_bittiming() - Configure all the data bit timing parameters.
+ * @netdev: CAN network device.
+ *
+ * Dummy function: ES58X FD family sets the bittiming
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1994

Quoted 6 lines

+ * parameters at the same time as the channel gets enabled.
+ *
+ * Return: zero.
+ */

drivers/net/can/usb/etas_es58x/es58x_core.c new file +2 -0
+static int es58x_set_data_bittiming(struct net_device *netdev)
+{

no need to implement a no-op here.

ACK.

Quoted 22 lines
Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     return 0;
+}
+

[...]

Quoted 16 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+/**
+ * es58x_set_mode() - Change network device mode.
+ * @netdev: CAN network device.
+ * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP
+ *
+ * This function is used by drivers/net/can/dev.c:can_restart() to
+ * recover from a bus off state. Thus only the only value expected for
+ * @mode is %CAN_MODE_START. None the less, other modes were
+ * implemented as well.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
+{
+     struct can_priv *can = netdev_priv(netdev);
+
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 2051

Quoted 5 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +5 -0
+     switch (mode) {
+     case CAN_MODE_START:
+             switch (can->state) {
+             case CAN_STATE_BUS_OFF:
+                     return es58x_reset(netdev);

If you shutdown the channel in case of a bus off, this get easier. See bittiming comments below, too.

Noted. I will disable the channel in es58x_rx_err_msg() when an error occurs. With this, I will also remove es58x_reset() which will not be needed anymore.

Quoted 45 lines
Quoted 45 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +45 -0
+
+             case CAN_STATE_STOPPED:
+             case CAN_STATE_SLEEPING:
+                     netif_wake_queue(netdev);
+                     can->state = CAN_STATE_ERROR_ACTIVE;
+                     return 0;
+
+             case CAN_STATE_ERROR_ACTIVE:
+             case CAN_STATE_ERROR_WARNING:
+             case CAN_STATE_ERROR_PASSIVE:
+             default:
+                     return 0;
+             }
+
+     case CAN_MODE_STOP:
+     case CAN_MODE_SLEEP:
+             netif_stop_queue(netdev);
+             if (mode == CAN_MODE_STOP)
+                     can->state = CAN_STATE_STOPPED;
+             else
+                     can->state = CAN_STATE_SLEEPING;
+             return 0;
+
+     default:
+             netdev_err(netdev, "%s: Invalid can_mode value: %d\n",
+                        __func__, mode);
+             return -EOPNOTSUPP;
+     }
+}
+
+/**
+ * es58x_init_priv() - Initialize private parameters.
+ * @es58x_dev: ES58X device.
+ * @priv: ES58X private parameters related to the network device.
+ * @channel_idx: Index of the network device.
+ */
+static void es58x_init_priv(struct es58x_device *es58x_dev,
+                         struct es58x_priv *priv, int channel_idx)
+{
+     const struct es58x_parameters *param = es58x_dev->param;
+     struct can_priv *can = &priv->can;
+
+     priv->es58x_dev = es58x_dev;
+     priv->channel_idx = channel_idx;
+     priv->tx_urb = NULL;
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 2100

Quoted 4 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+     priv->tx_can_msg_cnt = 0;
+
+     can->bittiming_const = param->bittiming_const;
+     can->do_set_bittiming = es58x_set_bittiming;

Can you keep the channels and/or devices disabled while the interface(s) are down? You don't need to implement this callback, it's sufficient to set the bittiming during the ndo_open() callback.

ACK.

Quoted 6 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 2104

Quoted 4 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+     if (param->ctrlmode_supported & CAN_CTRLMODE_FD) {
+             can->data_bittiming_const = param->data_bittiming_const;
+             can->tdc_const = param->tdc_const;
+             can->do_set_data_bittiming = es58x_set_data_bittiming;

no need to provide this callback.

ACK.

Quoted 34 lines
Quoted 34 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +34 -0
+     }
+     can->bitrate_max = param->bitrate_max;
+     can->clock = param->clock;
+     can->state = CAN_STATE_STOPPED;
+     can->ctrlmode_supported = param->ctrlmode_supported;
+     can->do_set_mode = es58x_set_mode;
+}
+
+/**
+ * es58x_init_netdev() - Initialize the network device.
+ * @es58x_dev: ES58X device.
+ * @channel_idx: Index of the network device.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
+{
+     struct net_device *netdev;
+     struct device *dev = es58x_dev->dev;
+     int ret;
+
+     netdev = alloc_candev(sizeof(struct es58x_priv),
+                           es58x_dev->param->fifo_mask + 1);
+     if (!netdev) {
+             dev_err(dev, "Could not allocate candev\n");
+             return -ENOMEM;
+     }
+     SET_NETDEV_DEV(netdev, dev);
+     es58x_dev->netdev[channel_idx] = netdev;
+     es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);
+
+     netdev->netdev_ops = &es58x_netdev_ops;
+     netdev->flags |= IFF_ECHO;      /* We support local echo */
+
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 2143

Quoted 5 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +5 -0
+     ret = register_candev(netdev);
+     if (ret)
+             return ret;
+     netdev_dbg(netdev, "%s: Registered channel %s\n",
+                es58x_dev->udev->product, netdev->name);

In this function and in the following ones there are a lot of netdev_dbg(), please have a look if you can remove some of those.

ACK, I will make the driver less verbose in debug mode.

Quoted 18 lines

[...]

Quoted 7 lines

+/**
+ * struct es58x_priv - All information specific to a CAN channel.
+ * @can: struct can_priv must be the first member (Socket CAN relies
+ * on the fact that function netdev_priv() returns a pointer to
+ * a struct can_priv).
+ * @es58x_dev: pointer to the corresponding ES58X device.
+ * @tx_urb: Used as a buffer to concatenate the TX messages and to do

Quoted 8 lines

+ * a bulk send. Please refer to es58x_start_xmit() for more
+ * details.
+ * @tx_tail: Index of the oldest packet still pending for
+ * completion. @tx_tail & echo_skb_mask represents the beginning
+ * of the echo skb FIFO, i.e. index of the first element.
+ * @tx_head: Index of the next packet to be sent to the
+ * device. @tx_head & echo_skb_mask represents the end of the
+ * echo skb FIFO plus one, i.e. the first free index.

Quoted 6 lines

Is this the description 100% correct? In xmit() you're using tx_head + tx_can_msg_cnt to address the next packet to be send.

Quoted 1 line
Quoted 1 line

+ packet_idx = priv->tx_head + priv->tx_can_msg_cnt;

But in xmit_more() you're not taking tx_can_msg_cnt into account.

The idea was that in es58x_start_xmit() and es58x_xmit_more(), the changes were not yet committed and thus tx_head not yet updated.

After a bit more thinking, it seems better to directly update tx_head each time a packet is queued in can_echo_skb[]. I will keep above description and modify the code.

Quoted 20 lines
Quoted 20 lines

+ * @tx_can_msg_cnt: Number of messages in @tx_urb.
+ * @tx_can_msg_is_fd: false: all messages in @tx_urb are Classical

+ *   CAN, true: all messages in @tx_urb are CAN FD. Rationale:
+ *   ES58X FD devices do not allow to mix Classical CAN and FD CAN
+ *   frames in one single bulk transmission.
+ * @err_passive_before_rtx_success: The ES58X device might enter in a
+ *   state in which it keeps alternating between error passive
+ *   and active states. This counter keeps track of the number of
+ *   error passive and if it gets bigger than
+ *   ES58X_CONSECUTIVE_ERR_PASSIVE_MAX, es58x_rx_err_msg() will
+ *   force the status to bus-off.

+ * @channel_idx: Channel index, starts at zero.
+ */

drivers/net/can/usb/etas_es58x/es58x_core.h new file +7 -0
+struct es58x_priv {
+     struct can_priv can;
+     struct es58x_device *es58x_dev;
+     struct urb *tx_urb;
+
+     u32 tx_tail;
+     u32 tx_head;
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.h line 247

Quoted 8 lines
drivers/net/can/usb/etas_es58x/es58x_core.h new file +8 -0
+
+     u8 tx_can_msg_cnt;
+     bool tx_can_msg_is_fd;
+
+     u8 err_passive_before_rtx_success;
+
+     u8 channel_idx;
+};

Yours sincerely,
Vincent

#4 lore raw

Re: [PATCH v14 1/4] can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces

Quoted 12 lines · Vincent MAILHOL
Quoted 11 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 641

Quoted 7 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +7 -0
+int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
+                  enum es58x_event event, u64 timestamp)
+{
+     struct es58x_priv *priv = es58x_priv(netdev);
+     struct can_priv *can = netdev_priv(netdev);
+     struct can_device_stats *can_stats = &can->can_stats;
+     struct can_frame *cf;

= NULL;

So that the if (cf) properly works...

Quoted 7 lines

I actually expected cfto be set to NULL when an error occurred (same as skb).

But indeed, cf is not initialised if the allocation fails. I was wondering if it would not be better to make both alloc_can_skb() and and alloc_canfd_skb() set cf to NULL. That would remove a pitfall.

Makes sense.

Quoted 1 line

If you like the idea, I can submit a patch.

Sorry creating this drive by patch. The patch description took longer than the actual patch :)

[...]

Quoted 11 lines
Quoted 10 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1191

Quoted 7 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +7 -0
+/**
+ * es58x_flush_cmd_buf() - Reset the buffer for URB commands.
+ * @es58x_dev: ES58X device.
+ */
+static void es58x_flush_cmd_buf(struct es58x_device *es58x_dev)
+{
+     memset(&es58x_dev->rx_cmd_buf, 0, es58x_dev->param->rx_urb_cmd_max_len);

I think you can skip the memset, as you're overwriting the data anyways and account the valid length.

Quoted 3 lines

The idea was that this buffer might contain confidential data (e.g. an UDS seed and key pair or a VIN) so I cleared it out of precaution.

I see, I think the kernel takes care of explicitly clearing not key material after using, but not unencrypted (i.e. plaintext) network traffic.

Quoted 5 lines

I recognize that this is useless as an attacker can just retrieve the information from a candump, no need to look into some kernel temporary buffer for that...

I will remove it.

Ok.

[...]

Quoted 13 lines
Quoted 6 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1343

Quoted 4 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +4 -0
+static signed int es58x_split_urb(struct es58x_device *es58x_dev,
+                               struct urb *urb)
+{
+     const u8 es58x_is_alive = 0x11;

We usually use #defines for this

ACK.

Quoted 4 lines
Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     union es58x_urb_cmd *urb_cmd;
+     u8 *raw_cmd = urb->transfer_buffer;
+     ssize_t raw_cmd_len = urb->actual_length;
Quoted 6 lines
Quoted 1 line

urb->actual_length is an u32

Below, I test that (raw_cmd_len > 0) in a while loop. I want to make this variable signed to avoid an infinite loop in case of an off by one error (which should not occur, see this as a safety net).

makes sense

Quoted 41 lines

I will make this a s32.

Also, the size is represented by a u16 on the device so s32 is more than enough.

Quoted 26 lines
Quoted 12 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +12 -0
+     int ret;
+
+     if (es58x_dev->rx_cmd_buf_len != 0) {
+             ret = es58x_handle_incomplete_cmd(es58x_dev, urb);
+             if (ret != -ENODATA)
+                     es58x_flush_cmd_buf(es58x_dev);
+             if (ret < 0)
+                     return ret;
+
+             raw_cmd += ret;
+             raw_cmd_len -= ret;
+     }

drivers/net/can/usb/etas_es58x/es58x_core.c line 1368

Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+
+     while (raw_cmd_len > 0) {
+             if (raw_cmd[0] == es58x_is_alive) {
+                     raw_cmd++;
+                     raw_cmd_len--;
+                     continue;
+             }
+             urb_cmd = (union es58x_urb_cmd *)raw_cmd;
+             ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
+             if (ret > 0) {

Here the length field in the usb_cmd is valid, it lies within the total length of the rx'ed urb.

Quoted 5 lines
Quoted 1 line

+ es58x_handle_urb_cmd(es58x_dev, urb_cmd);

As far as I see you're checking in ops->handle_urb_cmd() if the urb_cmd's length is long enough for the command that's currently processed, right?

The check of the urb_cmd's length occurs in es58x_check_rx_urb():

    if (urb_cmd_len > param->rx_urb_cmd_max_len) {
In that same function, I then check that urb_cmd's length is long enough:
    } else if (urb_actual_len < urb_cmd_len) {
Quoted 6 lines

If it is not long enough, the not-yet-consumed data of the urb is buffered and once a new urb comes up, those are concatenated.

After that, ops->handle_urb_cmd() will dispatch the command depending on the cmd_id and additional checks will be done on the size of the payload.

Good, this is how I understood the code.

[...]

Quoted 42 lines
Quoted 16 lines
Quoted 16 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+/**
+ * es58x_open() - Open and start network device.
+ * @netdev: CAN network device.
+ *
+ * Called when the network transitions to the up state.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_open(struct net_device *netdev)
+{
+     int ret;
+
+     ret = open_candev(netdev);
+     if (ret)
+             return ret;
+
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1780

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     ret = es58x_enable_channel(netdev);
+     if (ret)
+             return ret;

Please do an as complete as possible reset and configuration during open(). If there is any error a ifdown/ifup should fix it. Here on a USB device with multiple interfaces it's not as easy as on devices with only one CAN interface.

ACK.

I will use a function as below to check if all interfaces are down.

static bool es58x_are_all_channels_closed(struct es58x_device *es58x_dev)

{
    int i;

    for (i = 0; i < es58x_dev->num_can_ch; i++) {
        struct can_priv *can_priv = netdev_priv(es58x_dev->netdev[i]);
        if (can_priv->state != CAN_STATE_STOPPED)
            return false;
    }
    return true;
}
Quoted 1 line

I will modify both es58x_open() and es58x_close().

Have a look at
https://elixir.bootlin.com/linux/v5.11/source/include/linux/kref.h

I'm not sure if kref is overkill here :)

[...]

Quoted 47 lines
Quoted 38 lines
Quoted 38 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +38 -0
+/**
+ * es58x_start_xmit() - Transmit an skb.
+ * @skb: socket buffer of a CAN message.
+ * @netdev: CAN network device.
+ *
+ * Called when a packet needs to be transmitted.
+ *
+ * This function relies on Byte Queue Limits (BQL). The main benefit
+ * it to increase the throughput by allowing bulk transfers
+ * (c.f. xmit_more flag).
+ *
+ * Queues up to tx_bulk_max messages in &tx_urb buffer and does
+ * a bulk send of all messages in one single URB.
+ *
+ * Return:
+ * NETDEV_TX_OK if we could manage the @skb (either transmit it or
+ * drop it)
+ *
+ * NETDEV_TX_BUSY if the device is busy (this is a bug, the network
+ * device stop/wake management should prevent this return code to
+ * occur).
+ */
+static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
+                                 struct net_device *netdev)
+{
+     struct es58x_priv *priv = es58x_priv(netdev);
+     struct es58x_device *es58x_dev = priv->es58x_dev;
+     unsigned int packet_idx, frame_len;
+     int ret;
+
+     if (can_dropped_invalid_skb(netdev, skb)) {
+             if (priv->tx_urb)
+                     goto xmit_commit;
+             return NETDEV_TX_OK;
+     }
+
+     if (!priv->tx_urb) {
+             priv->tx_urb = es58x_get_tx_urb(es58x_dev);
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1901

Quoted 6 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +6 -0
+             if (!priv->tx_urb) {
+                     ret = -ENOMEM;
+                     goto drop_skb;
+             }
+             memset(priv->tx_urb->transfer_buffer, 0,
+                    es58x_dev->param->tx_urb_cmd_max_len);

Is this memset() needed?

Quoted 2 lines

In current state, it is, but a small modification should be enough to have it removed.

nice!

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +10 -0
+             priv->tx_urb->transfer_buffer_length =
+                 es58x_dev->param->urb_cmd_header_len;
+             priv->tx_can_msg_cnt = 0;
+             priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
+     } else if (priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
+             /* Can not do bulk send with mixed CAN and CAN FD frames. */
+             ret = es58x_xmit_commit(netdev);
+             if (ret)
+                     goto drop_skb;
+             return es58x_start_xmit(skb, netdev);
Quoted 6 lines
Quoted 2 lines

In the kernel the stack is limited, does it make sense to re-arrange this to avoid recursion?

This is a tail recursion so with the good optimisation, it will not increase the stack. And also, the recursive call occurs one time max. So I thought it was acceptable.

This were my thoughts as well.

Quoted 2 lines

Regardless, rearranging the code is trivial, so I will remove this recursion.

fine
[...]

Quoted 23 lines
Quoted 16 lines
Quoted 7 lines

+/**
+ * struct es58x_priv - All information specific to a CAN channel.
+ * @can: struct can_priv must be the first member (Socket CAN relies
+ * on the fact that function netdev_priv() returns a pointer to
+ * a struct can_priv).
+ * @es58x_dev: pointer to the corresponding ES58X device.
+ * @tx_urb: Used as a buffer to concatenate the TX messages and to do

Quoted 8 lines

+ * a bulk send. Please refer to es58x_start_xmit() for more
+ * details.
+ * @tx_tail: Index of the oldest packet still pending for
+ * completion. @tx_tail & echo_skb_mask represents the beginning
+ * of the echo skb FIFO, i.e. index of the first element.
+ * @tx_head: Index of the next packet to be sent to the
+ * device. @tx_head & echo_skb_mask represents the end of the
+ * echo skb FIFO plus one, i.e. the first free index.

Quoted 6 lines

Is this the description 100% correct? In xmit() you're using tx_head + tx_can_msg_cnt to address the next packet to be send.

Quoted 1 line
Quoted 1 line

+ packet_idx = priv->tx_head + priv->tx_can_msg_cnt;

But in xmit_more() you're not taking tx_can_msg_cnt into account.

Quoted 7 lines

The idea was that in es58x_start_xmit() and es58x_xmit_more(), the changes were not yet committed and thus tx_head not yet updated.

After a bit more thinking, it seems better to directly update tx_head each time a packet is queued in can_echo_skb[]. I will keep above description and modify the code.

fine

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#5 lore raw

Re: [PATCH v14 1/4] can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces

Quoted 21 lines · Marc Kleine-Budde
Quoted 12 lines · Vincent MAILHOL
Quoted 11 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 641

Quoted 7 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +7 -0
+int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
+                  enum es58x_event event, u64 timestamp)
+{
+     struct es58x_priv *priv = es58x_priv(netdev);
+     struct can_priv *can = netdev_priv(netdev);
+     struct can_device_stats *can_stats = &can->can_stats;
+     struct can_frame *cf;

= NULL;

So that the if (cf) properly works...

Quoted 7 lines

I actually expected cfto be set to NULL when an error occurred (same as skb).

But indeed, cf is not initialised if the allocation fails. I was wondering if it would not be better to make both alloc_can_skb() and and alloc_canfd_skb() set cf to NULL. That would remove a pitfall.

Quoted 6 lines

Makes sense.

Quoted 1 line

If you like the idea, I can submit a patch.

Sorry creating this drive by patch. The patch description took longer than the actual patch :)

This is often the case for any patches where the diff is one or two lines!

[...]

Quoted 43 lines
Quoted 43 lines
Quoted 17 lines
Quoted 17 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +16 -0
+/**
+ * es58x_open() - Open and start network device.
+ * @netdev: CAN network device.
+ *
+ * Called when the network transitions to the up state.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_open(struct net_device *netdev)
+{
+     int ret;
+
+     ret = open_candev(netdev);
+     if (ret)
+             return ret;
+
Quoted 8 lines

drivers/net/can/usb/etas_es58x/es58x_core.c line 1780

Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_core.c new file +3 -0
+     ret = es58x_enable_channel(netdev);
+     if (ret)
+             return ret;

Please do an as complete as possible reset and configuration during open(). If there is any error a ifdown/ifup should fix it. Here on a USB device with multiple interfaces it's not as easy as on devices with only one CAN interface.

ACK.

I will use a function as below to check if all interfaces are down.

static bool es58x_are_all_channels_closed(struct es58x_device *es58x_dev)

{
    int i;

    for (i = 0; i < es58x_dev->num_can_ch; i++) {
        struct can_priv *can_priv = netdev_priv(es58x_dev->netdev[i]);
        if (can_priv->state != CAN_STATE_STOPPED)
            return false;
    }
    return true;
}
Quoted 6 lines
Quoted 1 line

I will modify both es58x_open() and es58x_close().

Have a look at
https://elixir.bootlin.com/linux/v5.11/source/include/linux/kref.h

I'm not sure if kref is overkill here :)

There is indeed a race condition in which the above function might return true when it is not supposed to.

I checked the struct kref and the refcount_t, but here, I think that a simple atomic_t is enough because there is no risk of saturation.

Something like:
| if (atomic_inc_return(opened_channel_cnt) == 1)
| /* configure */
and
| if (atomic_dec_and_test(opened_channel_cnt))
| /* release */

[...]

Yours sincerely,
Vincent

#6 lore raw

PATCH v14 2/4 can: etas_es58x: add support for ETAS ES581.4 CAN USB interface

In reply to Vincent Mailhol

This patch adds support for the ES581.4 interface from ETAS GmbH (https://www.etas.com/en/products/es58x.php).

---
 drivers/net/can/usb/etas_es58x/Makefile     |   2 +-
 drivers/net/can/usb/etas_es58x/es581_4.c    | 499 ++++++++++++++++++++
 drivers/net/can/usb/etas_es58x/es581_4.h    | 207 ++++++++
 drivers/net/can/usb/etas_es58x/es58x_core.c |   8 +
 drivers/net/can/usb/etas_es58x/es58x_core.h |   5 +-
 5 files changed, 719 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/can/usb/etas_es58x/es581_4.c
 create mode 100644 drivers/net/can/usb/etas_es58x/es581_4.h
drivers/net/can/usb/etas_es58x/Makefile +1 -1
index 60a1ac935a69..e4753ec9cb60 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x.o
-etas_es58x-y = es58x_core.o
+etas_es58x-y = es58x_core.o es581_4.o
drivers/net/can/usb/etas_es58x/es581_4.c new file +499 -0

499 lines not on this page · raw message

drivers/net/can/usb/etas_es58x/es581_4.h new file +207 -0

207 lines not on this page · raw message

drivers/net/can/usb/etas_es58x/es58x_core.c +8 -0
index 69d506d3e934..b0c4297244ca 100644
@@ -28,9 +28,11 @@ MODULE_LICENSE("GPL v2");
 /* Vendor and product id */
 #define ES58X_MODULE_NAME "etas_es58x"
 #define ES58X_VENDOR_ID 0x108C
+#define ES581_4_PRODUCT_ID 0x0159
 
 /* Table of devices which work with this driver */
 static const struct usb_device_id es58x_id_table[] = {
+	{USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID)},
 	{ /* Terminating entry */ }
 };
 
@@ -2217,6 +2219,12 @@ static int es58x_init_es58x_dev(struct usb_interface *intf,
 		return ret;
 
 	switch (usb_id_product) {
+	case ES581_4_PRODUCT_ID:	/* 2 Classical CAN channels */
+		param = &es581_4_param;
+		ops = &es581_4_ops;
+		num_can_ch = ES581_4_NUM_CAN_CH;
+		break;
+
 	default:
 		return -ENODEV;
 	}
drivers/net/can/usb/etas_es58x/es58x_core.h +4 -1
index 1c7f7334e678..3f7a2c91a1b8 100644
@@ -18,11 +18,13 @@
 #include <linux/can.h>
 #include <linux/can/dev.h>
 
+#include "es581_4.h"
+
 /* Driver constants */
 #define ES58X_RX_URBS_MAX 5	/* Empirical value */
 #define ES58X_TX_URBS_MAX 6	/* Empirical value */
 
-#define ES58X_MAX(param) 0
+#define ES58X_MAX(param) (ES581_4_##param)
 #define ES58X_TX_BULK_MAX ES58X_MAX(TX_BULK_MAX)
 #define ES58X_RX_BULK_MAX ES58X_MAX(RX_BULK_MAX)
 #define ES58X_ECHO_BULK_MAX ES58X_MAX(ECHO_BULK_MAX)
@@ -194,6 +196,7 @@ enum es58x_ret_type {
 };
 
 union es58x_urb_cmd {
+	struct es581_4_urb_cmd es581_4_urb_cmd;
 	struct {		/* Common header parts of all variants */
 		__le16 sof;
 		u8 cmd_type;
Signature · 1 line
2.26.2
#7 lore raw

PATCH v14 3/4 can: etas_es58x: add support for the ETAS ES58X_FD CAN USB interfaces

In reply to Vincent Mailhol

This patch add support for the ES582.1 and ES584.1 interfaces from ETAS GmbH (https://www.etas.com/en/products/es58x.php).

---
 drivers/net/can/usb/etas_es58x/Makefile     |   2 +-
 drivers/net/can/usb/etas_es58x/es58x_core.c |  16 +
 drivers/net/can/usb/etas_es58x/es58x_core.h |   6 +-
 drivers/net/can/usb/etas_es58x/es58x_fd.c   | 575 ++++++++++++++++++++
 drivers/net/can/usb/etas_es58x/es58x_fd.h   | 243 +++++++++
 5 files changed, 840 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_fd.c
 create mode 100644 drivers/net/can/usb/etas_es58x/es58x_fd.h
drivers/net/can/usb/etas_es58x/Makefile +1 -1
index e4753ec9cb60..a129b4aa0215 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x.o
-etas_es58x-y = es58x_core.o es581_4.o
+etas_es58x-y = es58x_core.o es581_4.o es58x_fd.o
drivers/net/can/usb/etas_es58x/es58x_core.c +16 -0
index b0c4297244ca..7b9b993c0b5b 100644
@@ -29,10 +29,14 @@ MODULE_LICENSE("GPL v2");
 #define ES58X_MODULE_NAME "etas_es58x"
 #define ES58X_VENDOR_ID 0x108C
 #define ES581_4_PRODUCT_ID 0x0159
+#define ES582_1_PRODUCT_ID 0x0168
+#define ES584_1_PRODUCT_ID 0x0169
 
 /* Table of devices which work with this driver */
 static const struct usb_device_id es58x_id_table[] = {
 	{USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID)},
+	{USB_DEVICE(ES58X_VENDOR_ID, ES582_1_PRODUCT_ID)},
+	{USB_DEVICE(ES58X_VENDOR_ID, ES584_1_PRODUCT_ID)},
 	{ /* Terminating entry */ }
 };
 
@@ -2225,6 +2229,18 @@ static int es58x_init_es58x_dev(struct usb_interface *intf,
 		num_can_ch = ES581_4_NUM_CAN_CH;
 		break;
 
+	case ES582_1_PRODUCT_ID:	/* 2 CAN(-FD) channels */
+		param = &es58x_fd_param;
+		ops = &es58x_fd_ops;
+		num_can_ch = ES582_1_NUM_CAN_CH;
+		break;
+
+	case ES584_1_PRODUCT_ID:	/* 1 CAN(-FD) and 1 LIN channel */
+		param = &es58x_fd_param;
+		ops = &es58x_fd_ops;
+		num_can_ch = ES584_1_NUM_CAN_CH;
+		break;
+
 	default:
 		return -ENODEV;
 	}
drivers/net/can/usb/etas_es58x/es58x_core.h +5 -1
index 3f7a2c91a1b8..f0cf88a96816 100644
@@ -19,12 +19,15 @@
 #include <linux/can/dev.h>
 
 #include "es581_4.h"
+#include "es58x_fd.h"
 
 /* Driver constants */
 #define ES58X_RX_URBS_MAX 5	/* Empirical value */
 #define ES58X_TX_URBS_MAX 6	/* Empirical value */
 
-#define ES58X_MAX(param) (ES581_4_##param)
+#define ES58X_MAX(param)				\
+	(ES581_4_##param > ES58X_FD_##param ?		\
+		ES581_4_##param : ES58X_FD_##param)
 #define ES58X_TX_BULK_MAX ES58X_MAX(TX_BULK_MAX)
 #define ES58X_RX_BULK_MAX ES58X_MAX(RX_BULK_MAX)
 #define ES58X_ECHO_BULK_MAX ES58X_MAX(ECHO_BULK_MAX)
@@ -197,6 +200,7 @@ enum es58x_ret_type {
 
 union es58x_urb_cmd {
 	struct es581_4_urb_cmd es581_4_urb_cmd;
+	struct es58x_fd_urb_cmd es58x_fd_urb_cmd;
 	struct {		/* Common header parts of all variants */
 		__le16 sof;
 		u8 cmd_type;
drivers/net/can/usb/etas_es58x/es58x_fd.c new file +575 -0

575 lines not on this page · raw message

drivers/net/can/usb/etas_es58x/es58x_fd.h new file +243 -0

243 lines not on this page · raw message

Signature · 1 line
2.26.2
#8 lore raw

PATCH v14 4/4 can: etas_es58x: add es58x_netdev_queue_set_dql_min_limit()

In reply to Vincent Mailhol

The size of a classical CAN frame is roughly 16 bytes, way less than an USB packet (varies between devices but often between 64 and 512 bytes).

This es58x_netdev_queue_set_dql_min_limit() forces to do a bulk send up to a threshold given by dql_min_limit in order to prevent sending many small USB packets even when small burst occurs (BQL algorithm is not able to immediately adjust dql.min_limit value).

Doing so increases the throughput with a minimum impact on the
latency: the time lost to stuff several frame in a single USB packet is negligible compared to the gain of not having to send several USB packets.

---
Note: Contrary to previous version, the dql_min_limit field is not guarded any more by #ifdef CONFIG_BQL: removing that field would create a hole, so there is no gain to do so. If CONFIG_BQL is indeed deactivated the field will just be unused.

---
 drivers/net/can/usb/etas_es58x/es581_4.c    |  2 ++
 drivers/net/can/usb/etas_es58x/es58x_core.c |  5 ++++
 drivers/net/can/usb/etas_es58x/es58x_core.h | 27 +++++++++++++++++++++
 drivers/net/can/usb/etas_es58x/es58x_fd.c   |  2 ++
 4 files changed, 36 insertions(+)
drivers/net/can/usb/etas_es58x/es581_4.c +2 -0
index a4b21dfd1bff..6b39d0cad7bb 100644
@@ -481,6 +481,8 @@ const struct es58x_parameters es581_4_param = {
 	 * unclear.
 	 */
 	.fifo_mask = 63, /* echo_skb_max = 64 */
+	/* Empirical value. */
+	.dql_min_limit = CAN_FRAME_LEN_MAX * 50,
 	.tx_bulk_max = ES581_4_TX_BULK_MAX,
 	.urb_cmd_header_len = ES581_4_URB_CMD_HEADER_LEN,
 	.rx_urb_max = ES58X_RX_URBS_MAX,
drivers/net/can/usb/etas_es58x/es58x_core.c +5 -0
index 7b9b993c0b5b..b07b9f085687 100644
@@ -461,6 +461,8 @@ static void es58x_can_flush_echo_skb(struct net_device *netdev)
 	priv->tx_tail = 0;
 	priv->tx_head = 0;
 	netdev_reset_queue(netdev);
+	es58x_netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
+					     priv->es58x_dev->param->dql_min_limit);
 }
 
 /**
@@ -2148,6 +2150,9 @@ static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
 	netdev_dbg(netdev, "%s: Registered channel %s\n",
 		   es58x_dev->udev->product, netdev->name);
 
+	es58x_netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
+					     es58x_dev->param->dql_min_limit);
+
 	return ret;
 }
 
drivers/net/can/usb/etas_es58x/es58x_core.h +27 -0
index f0cf88a96816..086c17693f9a 100644
@@ -276,6 +276,15 @@ struct es58x_priv {
  *	not exceed the maximum size of the device internal TX FIFO
  *	length. This parameter is used to control the network queue
  *	wake/stop logic.
+ * @dql_min_limit: Dynamic Queue Limits (DQL) absolute minimum limit
+ *	of bytes allowed to be queued on this network device transmit
+ *	queue. Used by the Byte Queue Limits (BQL) to determine how
+ *	frequently the xmit_more flag will be set to true in
+ *	es58x_start_xmit(). Set this value higher to optimize for
+ *	throughput but be aware that it might have a negative impact
+ *	on the latency! This value can also be set dynamically. Please
+ *	refer to Documentation/ABI/testing/sysfs-class-net-queues for
+ *	more details.
  * @tx_bulk_max: Maximum number of TX messages that can be sent in one
  *	single URB packet.
  * @urb_cmd_header_len: Length of the URB command header.
@@ -294,6 +303,7 @@ struct es58x_parameters {
 	u16 tx_urb_cmd_max_len;
 	u16 rx_urb_cmd_max_len;
 	u16 fifo_mask;
+	u16 dql_min_limit;
 	u8 tx_bulk_max;
 	u8 urb_cmd_header_len;
 	u8 rx_urb_max;
@@ -643,6 +653,23 @@ static inline enum es58x_flag es58x_get_flags(const struct sk_buff *skb)
 	return es58x_flags;
 }
 
+/**
+ * es58x_netdev_queue_set_dql_min_limit - set dql minimum limit
+ * @q: pointer to transmit queue
+ * @min_limit: dql minimum limit
+ *
+ * Forces xmit_more() to return true until the minimum threshold
+ * defined by @min_limit is reached (or until the tx queue is
+ * empty).
+ */
+static inline void es58x_netdev_queue_set_dql_min_limit(struct netdev_queue *q,
+							unsigned int min_limit)
+{
+#ifdef CONFIG_BQL
+	q->dql.min_limit = min_limit;
+#endif
+}
+
 int es58x_can_get_echo_skb(struct net_device *netdev, u32 packet_idx,
 			   u64 *tstamps, unsigned int pkts);
 int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
drivers/net/can/usb/etas_es58x/es58x_fd.c +2 -0
index a4e0867c9d4c..578a01580488 100644
@@ -557,6 +557,8 @@ const struct es58x_parameters es58x_fd_param = {
 	 * maximum, so no impact on performance was measured.
 	 */
 	.fifo_mask = 255, /* echo_skb_max = 256 */
+	/* Empirical value. */
+	.dql_min_limit = CAN_FRAME_LEN_MAX * 15,
 	.tx_bulk_max = ES58X_FD_TX_BULK_MAX,
 	.urb_cmd_header_len = ES58X_FD_URB_CMD_HEADER_LEN,
 	.rx_urb_max = ES58X_RX_URBS_MAX,
Signature · 1 line
2.26.2