can: add support for ETAS ES58X CAN USB
v4 cover can: add support for ETAS ES58X CAN USB
The purpose of this patch series is to introduce a new CAN USB driver to support ETAS USB interfaces (ES58X series).
During development, issues in drivers/net/can/dev.c were discovered, the fix for those issues are included in this patch series.
We also propose to add one helper functions in include/linux/can/dev.h which we think can benefit other drivers: get_can_len().
*Side notes*: scripts/checkpatch.pl returns 4 'checks' findings in [PATCH 4/4]. All those findings are of type: "Macro argument reuse 'x' possible side-effects?". Those arguments reuse are actually made by calling either __stringify() or sizeof_field() which are both pre-processor constant. Furthermore, those macro are never called with arguments sensible to side-effects. So no actual side effect would occur.
Changes in v4:
- Remove from the series the patches with have already been merged into net-next.
Reference: https://lkml.org/lkml/2020/10/4/78
Reference: https://lkml.org/lkml/2020/10/5/355
- Modify [PATCH 4/4] according to comments from Marc.
Reference: https://lkml.org/lkml/2020/10/4/80)
Changes in v3:
- Added one additional patch: [PATCH v3 2/7] can: dev: fix type of
get_can_dlc() and get_canfd_dlc() macros.
- Make get_can_len() return u8 and make the skb const in PATCH 3/7.
- Remove all the calls to likely() and unlikely() in PATCH 6/7.
Changes in v2:
- Fixed -W1 warnings in PATCH 6/7 (v1 was tested with GCC -WExtra but not with -W1).
- Added lsusb -v information in PATCH 7/7 and rephrased the comment.
- Take care to put everyone in CC of each of the patch of the series (sorry for the mess in v1...)
Vincent Mailhol (4):
can: dev: can_get_echo_skb(): prevent call to kfree_skb() in hard IRQ
context
can: dev: add a helper function to get the correct length of Classical
frames
can: dev: __can_get_echo_skb(): fix the return length
can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces
drivers/net/can/dev.c | 13 +- drivers/net/can/usb/Kconfig | 9 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/etas_es58x/Makefile | 3 + drivers/net/can/usb/etas_es58x/es581_4.c | 556 ++++ drivers/net/can/usb/etas_es58x/es581_4.h | 209 ++ drivers/net/can/usb/etas_es58x/es58x_core.c | 2639 +++++++++++++++++++ drivers/net/can/usb/etas_es58x/es58x_core.h | 704 +++++ drivers/net/can/usb/etas_es58x/es58x_fd.c | 657 +++++ drivers/net/can/usb/etas_es58x/es58x_fd.h | 241 ++ include/linux/can/dev.h | 23 + 11 files changed, 5048 insertions(+), 7 deletions(-) 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
PATCH v4 1/4 can: dev: can_get_echo_skb(): prevent call to kfree_skb() in hard IRQ context
If a driver calls can_get_echo_skb() during a hardware IRQ (which is often, but not always, the case), the 'WARN_ON(in_irq)' in net/core/skbuff.c#skb_release_head_state() might be triggered, under network congestion circumstances, together with the potential risk of a NULL pointer dereference.
The root cause of this issue is the call to kfree_skb() instead of dev_kfree_skb_irq() in net/core/dev.c#enqueue_to_backlog().
This patch prevents the skb to be freed within the call to netif_rx() by incrementing its reference count with skb_get(). The skb is finally freed by one of the in-irq-context safe functions:dev_consume_skb_any() or dev_kfree_skb_any(). The "any" version is used because some drivers might call can_get_echo_skb() in a normal context.
The reason for this issue to occur is that initially, in the core network stack, loopback skb were not supposed to be received in hardware IRQ context. The CAN stack is an exeption.
This bug was previously reported back in 2017 in [1] but the proposed patch never got accepted.
While [1] directly modifies net/core/dev.c, we try to propose here a smoother modification local to CAN network stack (the assumption behind is that only CAN devices are affected by this issue).
[1] https://patchwork.ozlabs.org/patch/835236/
---
Changes in v3 and v4: None
Changes in v2:
- Minor changes of link format in the changelog.
--- drivers/net/can/dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/can/dev.c +5 -1
@@ -538,7 +538,11 @@ unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)if (!skb)return 0 ; -netif_rx (skb); +skb_get (skb); +if (netif_rx (skb) ==NET_RX_SUCCESS ) +dev_consume_skb_any (skb); +else +dev_kfree_skb_any (skb);return len; }
Signature · 1 line
2.26.2
PATCH v4 2/4 can: dev: add a helper function to get the correct length of Classical frames
In reply to Vincent MailholIn classical CAN, the length of the data (i.e. CAN payload) is not always equal to the DLC! If the frame is a Remote Transmission Request (RTR), data length is always zero regardless of DLC value and else, if the DLC is greater than 8, the length is 8. Contrary to common belief, ISO 11898-1 Chapter 8.4.2.3 (DLC field) do allow DLCs greater than 8 for Classical Frames and specifies that those DLCs shall indicate that the data field is 8 bytes long.
Above facts are widely unknown and so many developpers uses the "len" field of "struct canfd_frame" to get the length of classical CAN
frames: this is incorrect!
This patch introduces function get_can_len() which can be used in remediation. The function takes the SKB as an input in order to be able to determine if the frame is classical or FD.
---
Changes in v4: None
Changes in v3:
- Make get_can_len() return u8.
- Make the skb const.
Reference: https://lkml.org/lkml/2020/9/30/883
Changes in v2: None
--- include/linux/can/dev.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
include/linux/can/dev.h +23 -0
@@ -192,6 +192,29 @@ u8 can_dlc2len(u8 can_dlc);/* map the sanitized data length to an appropriate data length code */ u8 can_len2dlc (u8 len); +/* +* get_can_len(skb) - get the length of the CAN payload. +* +* In classical CAN, the length of the data (i.e. CAN payload) is not +* always equal to the DLC! If the frame is a Remote Transmission +* Request (RTR), data length is always zero regardless of DLC value +* and else, if the DLC is greater than 8, the length is 8. Contrary +* to common belief, ISO 11898-1 Chapter 8.4.2.3 (DLC field) do allow +* DLCs greater than 8 for Classical Frames and specifies that those +* DLCs shall indicate that the data field is 8 bytes long. +*/ +static inline u8 get_can_len (const struct sk_buff *skb) +{ +const struct canfd_frame *cf = (const struct canfd_frame *)skb->data; + +if (can_is_canfd_skb (skb)) +return min_t (u8, cf->len,CANFD_MAX_DLEN ); +else if (cf->can_id &CAN_RTR_FLAG ) +return 0 ; +else +return min_t (u8, cf->len,CAN_MAX_DLEN ); +} +struct net_device *alloc_candev_mqs (int sizeof_priv,unsigned int echo_skb_max,unsigned int txqs,unsigned int rxqs);#define alloc_candev (sizeof_priv, echo_skb_max) \
Signature · 1 line
2.26.2
PATCH v4 3/4 can: dev: __can_get_echo_skb(): fix the return length
In reply to Vincent MailholThe length of Remote Transmission Request (RTR) frames is always 0 bytes. The DLC represents the requested length, not the actual length of the RTR. But __can_get_echo_skb() returns the DLC value regardless.
Apply get_can_len() function to retrieve the correct length.
---
Changes in v2 to v4: None
--- drivers/net/can/dev.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/net/can/dev.c +1 -6
@@ -507,14 +507,9 @@ __can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr) }if (priv->echo_skb[idx]) { -/* Using "struct canfd_frame::len" for the frame -* length is supported on both CAN and CANFD frames. -*/ struct sk_buff *skb = priv->echo_skb[idx]; -struct canfd_frame *cf = (struct canfd_frame *)skb->data; -u8 len = cf->len; - *len_ptr = len; + *len_ptr =get_can_len (skb); priv->echo_skb[idx] =NULL ;return skb;
Signature · 1 line
2.26.2
PATCH v4 4/4 can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces
In reply to Vincent MailholThis driver supports the ES581.4, ES582.1 and ES584.1 interfaces from ETAS GmbH (https://www.etas.com/en/products/es58x.php).
---
Changes in v4:
- Remove struct es58x_abstracted_can_frame.
branch is already done inside net/can/af_can.c#can_send().
- Fix formating (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:
- Remove all the calls to likely() and unlikely().
Reference: https://lkml.org/lkml/2020/9/30/995
Changes in v2:
- Fixed -W1 warnings (v1 was tested with GCC -WExtra but not with -W1).
--- drivers/net/can/usb/Kconfig | 9 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/etas_es58x/Makefile | 3 + drivers/net/can/usb/etas_es58x/es581_4.c | 556 ++++ drivers/net/can/usb/etas_es58x/es581_4.h | 209 ++ drivers/net/can/usb/etas_es58x/es58x_core.c | 2639 +++++++++++++++++++ drivers/net/can/usb/etas_es58x/es58x_core.h | 715 +++++ drivers/net/can/usb/etas_es58x/es58x_fd.c | 657 +++++ drivers/net/can/usb/etas_es58x/es58x_fd.h | 241 ++ 9 files changed, 5030 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
drivers/net/can/usb/Kconfig +9 -0
@@ -20,6 +20,15 @@ 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" + 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
@@ -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
@@ -0,0 +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 es58x_fd.o
drivers/net/can/usb/etas_es58x/es581_4.c new file +556 -0
556 lines not on this page · raw message
drivers/net/can/usb/etas_es58x/es581_4.h new file +209 -0
209 lines not on this page · raw message
drivers/net/can/usb/etas_es58x/es58x_core.c new file +2639 -0
2,210 lines not on this page · raw message
drivers/net/can/usb/etas_es58x/es58x_core.h new file +715 -0
715 lines not on this page · raw message
drivers/net/can/usb/etas_es58x/es58x_fd.c new file +657 -0
657 lines not on this page · raw message
drivers/net/can/usb/etas_es58x/es58x_fd.h new file +241 -0
241 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.
Marc Kleine-Budde
maintainerQuoted 8 lines · Vincent Mailhol
The purpose of this patch series is to introduce a new CAN USB driver to support ETAS USB interfaces (ES58X series).
During development, issues in
drivers/net/can/dev.cwere discovered, the fix for those issues are included in this patch series.We also propose to add one helper functions in
include/linux/can/dev.hwhich we think can benefit other drivers:get_can_len().
I applied patches 1-3 to linux-can, I've changed get_can_len() -> can_get_len() to use a common can_ prefix for all CAN related functions.
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 |