[PATCH 9/9] can: purge socket error queue on sock destruct

2019-06-07 Thread Marc Kleine-Budde
From: Willem de Bruijn 

CAN supports software tx timestamps as of the below commit. Purge
any queued timestamp packets on socket destroy.

Fixes: 51f31cabe3ce ("ip: support for TX timestamps on UDP and RAW sockets")
Reported-by: syzbot+a90604060cb40f5bd...@syzkaller.appspotmail.com
Signed-off-by: Willem de Bruijn 
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 net/can/af_can.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/net/can/af_can.c b/net/can/af_can.c
index 743470680127..80281ef2ccbd 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -99,6 +99,7 @@ EXPORT_SYMBOL(can_ioctl);
 static void can_sock_destruct(struct sock *sk)
 {
skb_queue_purge(&sk->sk_receive_queue);
+   skb_queue_purge(&sk->sk_error_queue);
 }
 
 static const struct can_proto *can_get_proto(int protocol)
-- 
2.20.1



[PATCH 2/9] can: flexcan: fix timeout when set small bitrate

2019-06-07 Thread Marc Kleine-Budde
From: Joakim Zhang 

Current we can meet timeout issue when setting a small bitrate like
1 as follows on i.MX6UL EVK board (ipg clock = 66MHZ, per clock =
30MHZ):

| root@imx6ul7d:~# ip link set can0 up type can bitrate 1

A link change request failed with some changes committed already.
Interface can0 may have been left with an inconsistent configuration,
please check.

| RTNETLINK answers: Connection timed out

It is caused by calling of flexcan_chip_unfreeze() timeout.

Originally the code is using usleep_range(10, 20) for unfreeze
operation, but the patch (8badd65 can: flexcan: avoid calling
usleep_range from interrupt context) changed it into udelay(10) which is
only a half delay of before, there're also some other delay changes.

After double to FLEXCAN_TIMEOUT_US to 100 can fix the issue.

Meanwhile, Rasmus Villemoes reported that even with a timeout of 100,
flexcan_probe() fails on the MPC8309, which requires a value of at least
140 to work reliably. 250 works for everyone.

Signed-off-by: Joakim Zhang 
Reviewed-by: Dong Aisheng 
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/flexcan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index 1c66fb2ad76b..f97c628eb2ad 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -166,7 +166,7 @@
 #define FLEXCAN_MB_CNT_LENGTH(x)   (((x) & 0xf) << 16)
 #define FLEXCAN_MB_CNT_TIMESTAMP(x)((x) & 0x)
 
-#define FLEXCAN_TIMEOUT_US (50)
+#define FLEXCAN_TIMEOUT_US (250)
 
 /* FLEXCAN hardware feature flags
  *
-- 
2.20.1



[PATCH 5/9] can: mcp251x: add support for mcp25625

2019-06-07 Thread Marc Kleine-Budde
From: Sean Nyekjaer 

Fully compatible with mcp2515, the mcp25625 have integrated transceiver.

This patch adds support for the mcp25625 to the existing mcp251x driver.

Signed-off-by: Sean Nyekjaer 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/spi/Kconfig   |  5 +++--
 drivers/net/can/spi/mcp251x.c | 25 -
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/drivers/net/can/spi/Kconfig b/drivers/net/can/spi/Kconfig
index 2e7e535e9237..1c50788055cb 100644
--- a/drivers/net/can/spi/Kconfig
+++ b/drivers/net/can/spi/Kconfig
@@ -9,9 +9,10 @@ config CAN_HI311X
  Driver for the Holt HI311x SPI CAN controllers.
 
 config CAN_MCP251X
-   tristate "Microchip MCP251x SPI CAN controllers"
+   tristate "Microchip MCP251x and MCP25625 SPI CAN controllers"
depends on HAS_DMA
---help---
- Driver for the Microchip MCP251x SPI CAN controllers.
+ Driver for the Microchip MCP251x and MCP25625 SPI CAN
+ controllers.
 
 endmenu
diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
index e90817608645..da64e71a62ee 100644
--- a/drivers/net/can/spi/mcp251x.c
+++ b/drivers/net/can/spi/mcp251x.c
@@ -1,5 +1,5 @@
 /*
- * CAN bus driver for Microchip 251x CAN Controller with SPI Interface
+ * CAN bus driver for Microchip 251x/25625 CAN Controller with SPI Interface
  *
  * MCP2510 support and bug fixes by Christian Pellegrin
  * 
@@ -41,7 +41,7 @@
  * static struct spi_board_info spi_board_info[] = {
  * {
  * .modalias = "mcp2510",
- * // or "mcp2515" depending on your controller
+ * // "mcp2515" or "mcp25625" depending on your controller
  * .platform_data = &mcp251x_info,
  * .irq = IRQ_EINT13,
  * .max_speed_hz = 2*1000*1000,
@@ -238,6 +238,7 @@ static const struct can_bittiming_const 
mcp251x_bittiming_const = {
 enum mcp251x_model {
CAN_MCP251X_MCP2510 = 0x2510,
CAN_MCP251X_MCP2515 = 0x2515,
+   CAN_MCP251X_MCP25625= 0x25625,
 };
 
 struct mcp251x_priv {
@@ -280,7 +281,6 @@ static inline int mcp251x_is_##_model(struct spi_device 
*spi) \
 }
 
 MCP251X_IS(2510);
-MCP251X_IS(2515);
 
 static void mcp251x_clean(struct net_device *net)
 {
@@ -639,7 +639,7 @@ static int mcp251x_hw_reset(struct spi_device *spi)
 
/* Wait for oscillator startup timer after reset */
mdelay(MCP251X_OST_DELAY_MS);
-   
+
reg = mcp251x_read_reg(spi, CANSTAT);
if ((reg & CANCTRL_REQOP_MASK) != CANCTRL_REQOP_CONF)
return -ENODEV;
@@ -820,9 +820,8 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
/* receive buffer 0 */
if (intf & CANINTF_RX0IF) {
mcp251x_hw_rx(spi, 0);
-   /*
-* Free one buffer ASAP
-* (The MCP2515 does this automatically.)
+   /* Free one buffer ASAP
+* (The MCP2515/25625 does this automatically.)
 */
if (mcp251x_is_2510(spi))
mcp251x_write_bits(spi, CANINTF, CANINTF_RX0IF, 
0x00);
@@ -831,7 +830,7 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
/* receive buffer 1 */
if (intf & CANINTF_RX1IF) {
mcp251x_hw_rx(spi, 1);
-   /* the MCP2515 does this automatically */
+   /* The MCP2515/25625 does this automatically. */
if (mcp251x_is_2510(spi))
clear_intf |= CANINTF_RX1IF;
}
@@ -1006,6 +1005,10 @@ static const struct of_device_id mcp251x_of_match[] = {
.compatible = "microchip,mcp2515",
.data   = (void *)CAN_MCP251X_MCP2515,
},
+   {
+   .compatible = "microchip,mcp25625",
+   .data   = (void *)CAN_MCP251X_MCP25625,
+   },
{ }
 };
 MODULE_DEVICE_TABLE(of, mcp251x_of_match);
@@ -1019,6 +1022,10 @@ static const struct spi_device_id mcp251x_id_table[] = {
.name   = "mcp2515",
.driver_data= (kernel_ulong_t)CAN_MCP251X_MCP2515,
},
+   {
+   .name   = "mcp25625",
+   .driver_data= (kernel_ulong_t)CAN_MCP251X_MCP25625,
+   },
{ }
 };
 MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
@@ -1259,5 +1266,5 @@ module_spi_driver(mcp251x_can_driver);
 
 MODULE_AUTHOR("Chris Elston , "
  "Christian Pellegrin ");
-MODULE_DESCRIPTION("Microchip 251x CAN driver");
+MODULE_DESCRIPTION("Microchip 251x/25625 CAN driver");
 MODULE_LICENSE("GPL v2");
-- 
2.20.1



[PATCH 3/9] can: xilinx_can: use correct bittiming_const for CAN FD core

2019-06-07 Thread Marc Kleine-Budde
From: Anssi Hannula 

Commit 9e5f1b273e6a ("can: xilinx_can: add support for Xilinx CAN FD
core") added a new can_bittiming_const structure for CAN FD cores that
support larger values for tseg1, tseg2, and sjw than previous Xilinx CAN
cores, but the commit did not actually take that into use.

Fix that.

Tested with CAN FD core on a ZynqMP board.

Fixes: 9e5f1b273e6a ("can: xilinx_can: add support for Xilinx CAN FD core")
Reported-by: Shubhrajyoti Datta 
Signed-off-by: Anssi Hannula 
Cc: Michal Simek 
Reviewed-by: Shubhrajyoti Datta 
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/xilinx_can.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/xilinx_can.c b/drivers/net/can/xilinx_can.c
index f2024404b8d6..63203ff452b5 100644
--- a/drivers/net/can/xilinx_can.c
+++ b/drivers/net/can/xilinx_can.c
@@ -1435,7 +1435,7 @@ static const struct xcan_devtype_data xcan_canfd_data = {
 XCAN_FLAG_RXMNF |
 XCAN_FLAG_TX_MAILBOXES |
 XCAN_FLAG_RX_FIFO_MULTI,
-   .bittiming_const = &xcan_bittiming_const,
+   .bittiming_const = &xcan_bittiming_const_canfd,
.btr_ts2_shift = XCAN_BTR_TS2_SHIFT_CANFD,
.btr_sjw_shift = XCAN_BTR_SJW_SHIFT_CANFD,
.bus_clk_name = "s_axi_aclk",
-- 
2.20.1



[PATCH 4/9] dt-bindings: can: mcp251x: add mcp25625 support

2019-06-07 Thread Marc Kleine-Budde
From: Sean Nyekjaer 

Fully compatible with mcp2515, the mcp25625 have integrated transceiver.

This patch add the mcp25625 to the device tree bindings documentation.

Signed-off-by: Sean Nyekjaer 
Signed-off-by: Marc Kleine-Budde 
---
 Documentation/devicetree/bindings/net/can/microchip,mcp251x.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/net/can/microchip,mcp251x.txt 
b/Documentation/devicetree/bindings/net/can/microchip,mcp251x.txt
index 188c8bd4eb67..5a0111d4de58 100644
--- a/Documentation/devicetree/bindings/net/can/microchip,mcp251x.txt
+++ b/Documentation/devicetree/bindings/net/can/microchip,mcp251x.txt
@@ -4,6 +4,7 @@ Required properties:
  - compatible: Should be one of the following:
- "microchip,mcp2510" for MCP2510.
- "microchip,mcp2515" for MCP2515.
+   - "microchip,mcp25625" for MCP25625.
  - reg: SPI chip select.
  - clocks: The clock feeding the CAN controller.
  - interrupts: Should contain IRQ line for the CAN controller.
-- 
2.20.1



[PATCH 7/9] can: af_can: Fix error path of can_init()

2019-06-07 Thread Marc Kleine-Budde
From: YueHaibing 

This patch add error path for can_init() to avoid possible crash if some
error occurs.

Fixes: 0d66548a10cb ("[CAN]: Add PF_CAN core module")
Signed-off-by: YueHaibing 
Acked-by: Oliver Hartkopp 
Signed-off-by: Marc Kleine-Budde 
---
 net/can/af_can.c | 24 +---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/net/can/af_can.c b/net/can/af_can.c
index e8fd5dc1780a..743470680127 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -952,6 +952,8 @@ static struct pernet_operations can_pernet_ops 
__read_mostly = {
 
 static __init int can_init(void)
 {
+   int err;
+
/* check for correct padding to be able to use the structs similarly */
BUILD_BUG_ON(offsetof(struct can_frame, can_dlc) !=
 offsetof(struct canfd_frame, len) ||
@@ -965,15 +967,31 @@ static __init int can_init(void)
if (!rcv_cache)
return -ENOMEM;
 
-   register_pernet_subsys(&can_pernet_ops);
+   err = register_pernet_subsys(&can_pernet_ops);
+   if (err)
+   goto out_pernet;
 
/* protocol register */
-   sock_register(&can_family_ops);
-   register_netdevice_notifier(&can_netdev_notifier);
+   err = sock_register(&can_family_ops);
+   if (err)
+   goto out_sock;
+   err = register_netdevice_notifier(&can_netdev_notifier);
+   if (err)
+   goto out_notifier;
+
dev_add_pack(&can_packet);
dev_add_pack(&canfd_packet);
 
return 0;
+
+out_notifier:
+   sock_unregister(PF_CAN);
+out_sock:
+   unregister_pernet_subsys(&can_pernet_ops);
+out_pernet:
+   kmem_cache_destroy(rcv_cache);
+
+   return err;
 }
 
 static __exit void can_exit(void)
-- 
2.20.1



[PATCH 8/9] can: flexcan: Remove unneeded registration message

2019-06-07 Thread Marc Kleine-Budde
From: Fabio Estevam 

Currently the following message is observed when the flexcan
driver is probed:

flexcan 209.flexcan: device registered (reg_base=(ptrval), irq=23)

The reason for printing 'ptrval' is explained at
Documentation/core-api/printk-formats.rst:

"Pointers printed without a specifier extension (i.e unadorned %p) are
hashed to prevent leaking information about the kernel memory layout. This
has the added benefit of providing a unique identifier. On 64-bit machines
the first 32 bits are zeroed. The kernel will print ``(ptrval)`` until it
gathers enough entropy."

Instead of passing %pK, which can print the correct address, simply
remove the entire message as it is not really that useful.

Signed-off-by: Fabio Estevam 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/flexcan.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index f97c628eb2ad..f2fe344593d5 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -1583,9 +1583,6 @@ static int flexcan_probe(struct platform_device *pdev)
dev_dbg(&pdev->dev, "failed to setup stop-mode\n");
}
 
-   dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
-priv->regs, dev->irq);
-
return 0;
 
  failed_register:
-- 
2.20.1



[PATCH 1/9] can: usb: Kconfig: Remove duplicate menu entry

2019-06-07 Thread Marc Kleine-Budde
From: Alexander Dahl 

This seems to have slipped in by accident when sorting the entries.

Fixes: ffbdd9172ee2f53020f763574b4cdad8d9760a4f
Signed-off-by: Alexander Dahl 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/usb/Kconfig | 6 --
 1 file changed, 6 deletions(-)

diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig
index ac3522b77303..4b3d0ddcda79 100644
--- a/drivers/net/can/usb/Kconfig
+++ b/drivers/net/can/usb/Kconfig
@@ -102,12 +102,6 @@ config CAN_PEAK_USB
 
  (see also http://www.peak-system.com).
 
-config CAN_MCBA_USB
-   tristate "Microchip CAN BUS Analyzer interface"
-   ---help---
- This driver supports the CAN BUS Analyzer interface
- from Microchip (http://www.microchip.com/development-tools/).
-
 config CAN_UCAN
tristate "Theobroma Systems UCAN interface"
---help---
-- 
2.20.1



pull-request: can 2019-06-07

2019-06-07 Thread Marc Kleine-Budde
Hello David,

this is a pull reqeust of 9 patches for net/master.

The first patch is by Alexander Dahl and removes a duplicate menu entry from
the Kconfig. The next patch by Joakim Zhang fixes the timeout in the flexcan
driver when setting small bit rates. Anssi Hannula's patch for the xilinx_can
driver fixes the bittiming_const for CAN FD core. The two patches by Sean
Nyekjaer bring mcp25625 to the existing mcp251x driver. The patch by Eugen
Hristev implements an errata for the m_can driver. YueHaibing's patch fixes the
error handling ing can_init(). The patch by Fabio Estevam for the flexcan
driver removes an unneeded registration message during flexcan_probe(). And the
last patch is by Willem de Bruijn and adds the missing purging the  socket
error queue on sock destruct.

regards,
Marc

---

The following changes since commit c7e3c93abbc1382923c7f4fe5ba9ea6aa0fa8d0e:

  Merge tag 'wireless-drivers-for-davem-2019-06-07' of 
git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/wireless-drivers 
(2019-06-07 12:16:26 -0700)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git 
tags/linux-can-fixes-for-5.2-20190607

for you to fetch changes up to fd704bd5ee749d560e86c4f1fd2ef486d8abf7cf:

  can: purge socket error queue on sock destruct (2019-06-07 23:03:54 +0200)


linux-can-fixes-for-5.2-20190607


Alexander Dahl (1):
  can: usb: Kconfig: Remove duplicate menu entry

Anssi Hannula (1):
  can: xilinx_can: use correct bittiming_const for CAN FD core

Eugen Hristev (1):
  can: m_can: implement errata "Needless activation of MRAF irq"

Fabio Estevam (1):
  can: flexcan: Remove unneeded registration message

Joakim Zhang (1):
  can: flexcan: fix timeout when set small bitrate

Sean Nyekjaer (2):
  dt-bindings: can: mcp251x: add mcp25625 support
  can: mcp251x: add support for mcp25625

Willem de Bruijn (1):
  can: purge socket error queue on sock destruct

YueHaibing (1):
  can: af_can: Fix error path of can_init()

 .../bindings/net/can/microchip,mcp251x.txt |  1 +
 drivers/net/can/flexcan.c  |  5 +
 drivers/net/can/m_can/m_can.c  | 21 ++
 drivers/net/can/spi/Kconfig|  5 +++--
 drivers/net/can/spi/mcp251x.c  | 25 ++
 drivers/net/can/usb/Kconfig|  6 --
 drivers/net/can/xilinx_can.c   |  2 +-
 net/can/af_can.c   | 25 +++---
 8 files changed, 65 insertions(+), 25 deletions(-)





[PATCH 6/9] can: m_can: implement errata "Needless activation of MRAF irq"

2019-06-07 Thread Marc Kleine-Budde
From: Eugen Hristev 

During frame reception while the MCAN is in Error Passive state and the
Receive Error Counter has thevalue MCAN_ECR.REC = 127, it may happen
that MCAN_IR.MRAF is set although there was no Message RAM access
failure. If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is
generated.

Work around:
The Message RAM Access Failure interrupt routine needs to check whether

MCAN_ECR.RP = '1' and MCAN_ECR.REC = '127'.

In this case, reset MCAN_IR.MRAF. No further action is required.
This affects versions older than 3.2.0

Errata explained on Sama5d2 SoC which includes this hardware block:
http://ww1.microchip.com/downloads/en/DeviceDoc/SAMA5D2-Family-Silicon-Errata-and-Data-Sheet-Clarification-DS8803B.pdf
chapter 6.2

Reproducibility: If 2 devices with m_can are connected back to back,
configuring different bitrate on them will lead to interrupt storm on
the receiving side, with error "Message RAM access failure occurred".
Another way is to have a bad hardware connection. Bad wire connection
can lead to this issue as well.

This patch fixes the issue according to provided workaround.

Signed-off-by: Eugen Hristev 
Reviewed-by: Ludovic Desroches 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/m_can/m_can.c | 21 +
 1 file changed, 21 insertions(+)

diff --git a/drivers/net/can/m_can/m_can.c b/drivers/net/can/m_can/m_can.c
index 9b449400376b..deb274a19ba0 100644
--- a/drivers/net/can/m_can/m_can.c
+++ b/drivers/net/can/m_can/m_can.c
@@ -822,6 +822,27 @@ static int m_can_poll(struct napi_struct *napi, int quota)
if (!irqstatus)
goto end;
 
+   /* Errata workaround for issue "Needless activation of MRAF irq"
+* During frame reception while the MCAN is in Error Passive state
+* and the Receive Error Counter has the value MCAN_ECR.REC = 127,
+* it may happen that MCAN_IR.MRAF is set although there was no
+* Message RAM access failure.
+* If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated
+* The Message RAM Access Failure interrupt routine needs to check
+* whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127.
+* In this case, reset MCAN_IR.MRAF. No further action is required.
+*/
+   if ((priv->version <= 31) && (irqstatus & IR_MRAF) &&
+   (m_can_read(priv, M_CAN_ECR) & ECR_RP)) {
+   struct can_berr_counter bec;
+
+   __m_can_get_berr_counter(dev, &bec);
+   if (bec.rxerr == 127) {
+   m_can_write(priv, M_CAN_IR, IR_MRAF);
+   irqstatus &= ~IR_MRAF;
+   }
+   }
+
psr = m_can_read(priv, M_CAN_PSR);
if (irqstatus & IR_ERR_STATE)
work_done += m_can_handle_state_errors(dev, psr);
-- 
2.20.1



pull-request: can-next 2019-07-24

2019-07-24 Thread Marc Kleine-Budde
Hello David,

this is a pull request for net-next/master consisting of 26 patches.

The first two patches are by me. One adds missing files of the CAN
subsystem to the MAINTAINERS file, while the other sorts the
Makefile/Kconfig of the sja1000 drivers sub directory. In the next patch
Ji-Ze Hong (Peter Hong) provides a driver for the "Fintek PCIE to 2 CAN"
controller, based on the the sja1000 IP core.

Gustavo A. R. Silva's patch for the kvaser_usb driver introduces the use
of struct_size() instead of open coding it. Henning Colliander's patch
adds a driver for the "Kvaser PCIEcan" devices.

Another patch by Gustavo A. R. Silva marks expected switch fall-throughs
properly.

Dan Murphy provides 5 patches for the m_can. After cleanups a framework
is introduced so that the driver can be used from memory mapped IO as
well as SPI attached devices. Finally he adds a driver for the tcan4x5x
which uses this framework.

A series of 5 patches by Appana Durga Kedareswara rao for the xilinx_can
driver, first clean up,then add support for CANFD. Colin Ian King
contributes another cleanup for the xilinx_can driver.

Robert P. J. Day's patch corrects the brief history of the CAN protocol
given in the Kconfig menu entry.

2 patches by Dong Aisheng for the flexcan driver provide PE clock source
select support and dt-bindings description.
2 patches by Sean Nyekjaer for the flexcan driver provide add CAN
wakeup-source property and dt-bindings description.

Jeroen Hofstee's patch converts the ti_hecc driver to make use of the
rx-offload helper fixing a number of outstanding bugs.

The first patch of Oliver Hartkopp removes the now obsolete empty
ioctl() handler for the CAN protocols. The second patch adds SPDX
license identifiers for CAN subsystem.

regards,
Marc

---

The following changes since commit 3e3bb69589e482e0783f28d4cd1d8e56fda0bcbb:

  tc-testing: added tdc tests for [b|p]fifo qdisc (2019-07-23 14:08:15 -0700)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git 
tags/linux-can-next-for-5.4-20190724

for you to fetch changes up to fba76a58452694b9b13c07e48839fa84c75f57af:

  can: Add SPDX license identifiers for CAN subsystem (2019-07-24 10:31:55 
+0200)


linux-can-next-for-5.4-20190724


Aisheng Dong (1):
  can: flexcan: implement can Runtime PM

Appana Durga Kedareswara rao (5):
  can: xilinx_can: Fix style issues
  can: xilinx_can: Fix kernel doc warnings
  can: xilinx_can: Fix flags field initialization for axi can and canps
  can: xilinx_can: Add cantype parameter in xcan_devtype_data struct
  can: xilinx_can: Add support for CANFD FD frames

Colin Ian King (1):
  can: xilinx_can: clean up indentation issue

Dan Murphy (5):
  can: m_can: Fix checkpatch issues on existing code
  can: m_can: Create a m_can platform framework
  can: m_can: Rename m_can_priv to m_can_classdev
  dt-bindings: can: tcan4x5x: Add DT bindings for TCAN4x5X driver
  can: tcan4x5x: Add tcan4x5x driver to the kernel

Dong Aisheng (2):
  dt-bindings: can: flexcan: add PE clock source property to device tree
  can: flexcan: add support for PE clock source select

Gustavo A. R. Silva (2):
  can: kvaser_usb: Use struct_size() in alloc_candev()
  can: mark expected switch fall-throughs

Henning Colliander (1):
  can: kvaser_pciefd: Add driver for Kvaser PCIEcan devices

Jeroen Hofstee (1):
  can: ti_hecc: use timestamp based rx-offloading

Ji-Ze Hong (Peter Hong) (1):
  can: sja1000: f81601: add Fintek F81601 support

Marc Kleine-Budde (2):
  MAINTAINERS: can: add missing files to CAN NETWORK DRIVERS and CAN 
NETWORK LAYER
  can: sja1000: Makefile/Kconfig: sort alphabetically

Oliver Hartkopp (2):
  can: remove obsolete empty ioctl() handler
  can: Add SPDX license identifiers for CAN subsystem

Robert P. J. Day (1):
  can: Kconfig: correct history of the CAN protocol

Sean Nyekjaer (2):
  dt-bindings: can: flexcan: add can wakeup property
  can: flexcan: add support for DT property 'wakeup-source'

 .../devicetree/bindings/net/can/fsl-flexcan.txt|   10 +
 .../devicetree/bindings/net/can/tcan4x5x.txt   |   37 +
 MAINTAINERS|5 +
 drivers/net/can/Kconfig|   13 +
 drivers/net/can/Makefile   |1 +
 drivers/net/can/at91_can.c |6 +-
 drivers/net/can/flexcan.c  |  136 +-
 drivers/net/can/kvaser_pciefd.c| 1912 
 drivers/net/can/m_can/Kconfig  |   22 +-
 drivers/net/can/m_can/Makefile |2 +
 drivers/net/can/m_can/m_can.c  | 1079 +--
 drivers

Re: pull-request: can-next 2019-07-24

2019-07-24 Thread Marc Kleine-Budde
On 7/24/19 11:22 AM, Oliver Hartkopp wrote:
> why didn't you include the CAN FD support for the can-gw?

I wanted to have a look at the patches. But the other patches are
already reviewed.

I'll do another pull-request this week.

> [PATCH 1/2] can: gw: use struct canfd_frame as internal data structure
> https://marc.info/?l=linux-can&m=156388681922741&w=2
> 
> [PATCH 2/2] can: gw: add support for CAN FD frames
> https://marc.info/?l=linux-can&m=156388682022742&w=2
> 
> The patches have already been sent in January as RFC and I did extensive 
> testing since then.

Good.

regards,
Marc

-- 
Pengutronix e.K.  | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


Re: pull-request: can-next 2019-07-24

2019-07-24 Thread Marc Kleine-Budde
On 7/24/19 11:24 AM, Stéphane Grosjean wrote:
> I hope you're fine.

Yes, the summer holidays of our $CUSTOMERS gave me a bit more time for CAN.

> Did you see the attached patch I've sent earlier this month?
Yes, it will be included in "linux-can-fixes-for-5.3-20190724".

https://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git/log/?h=linux-can-fixes-for-5.3-20190724

regards,
Marc

-- 
Pengutronix e.K.      | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


[PATCH 1/7] can: dev: call netif_carrier_off() in register_candev()

2019-07-24 Thread Marc Kleine-Budde
From: Rasmus Villemoes 

CONFIG_CAN_LEDS is deprecated. When trying to use the generic netdev
trigger as suggested, there's a small inconsistency with the link
property: The LED is on initially, stays on when the device is brought
up, and then turns off (as expected) when the device is brought down.

Make sure the LED always reflects the state of the CAN device.

Signed-off-by: Rasmus Villemoes 
Acked-by: Willem de Bruijn 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c
index b6b93a2d93a5..483d270664cc 100644
--- a/drivers/net/can/dev.c
+++ b/drivers/net/can/dev.c
@@ -1249,6 +1249,8 @@ int register_candev(struct net_device *dev)
return -EINVAL;
 
dev->rtnl_link_ops = &can_link_ops;
+   netif_carrier_off(dev);
+
return register_netdev(dev);
 }
 EXPORT_SYMBOL_GPL(register_candev);
-- 
2.20.1



[PATCH 6/7] can: peak_usb: fix potential double kfree_skb()

2019-07-24 Thread Marc Kleine-Budde
From: Stephane Grosjean 

When closing the CAN device while tx skbs are inflight, echo skb could
be released twice. By calling close_candev() before unlinking all
pending tx urbs, then the internal echo_skb[] array is fully and
correctly cleared before the USB write callback and, therefore,
can_get_echo_skb() are called, for each aborted URB.

Fixes: bb4785551f64 ("can: usb: PEAK-System Technik USB adapters driver core")
Signed-off-by: Stephane Grosjean 
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c 
b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index 458154c9b482..22b9c8e6d040 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -568,16 +568,16 @@ static int peak_usb_ndo_stop(struct net_device *netdev)
dev->state &= ~PCAN_USB_STATE_STARTED;
netif_stop_queue(netdev);
 
+   close_candev(netdev);
+
+   dev->can.state = CAN_STATE_STOPPED;
+
/* unlink all pending urbs and free used memory */
peak_usb_unlink_all_urbs(dev);
 
if (dev->adapter->dev_stop)
dev->adapter->dev_stop(dev);
 
-   close_candev(netdev);
-
-   dev->can.state = CAN_STATE_STOPPED;
-
/* can set bus off now */
if (dev->adapter->dev_set_bus) {
int err = dev->adapter->dev_set_bus(dev, 0);
-- 
2.20.1



[PATCH 5/7] can: flexcan: fix stop mode acknowledgment

2019-07-24 Thread Marc Kleine-Budde
From: Joakim Zhang 

To enter stop mode, the CPU should manually assert a global Stop Mode
request and check the acknowledgment asserted by FlexCAN. The CPU must
only consider the FlexCAN in stop mode when both request and
acknowledgment conditions are satisfied.

Fixes: de3578c198c6 ("can: flexcan: add self wakeup support")
Reported-by: Marc Kleine-Budde 
Signed-off-by: Joakim Zhang 
Cc: linux-stable  # >= v5.0
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/flexcan.c | 31 +++
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index 33ce45d51e15..fcec8bcb53d6 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -400,9 +400,10 @@ static void flexcan_enable_wakeup_irq(struct flexcan_priv 
*priv, bool enable)
priv->write(reg_mcr, ®s->mcr);
 }
 
-static inline void flexcan_enter_stop_mode(struct flexcan_priv *priv)
+static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
 {
struct flexcan_regs __iomem *regs = priv->regs;
+   unsigned int ackval;
u32 reg_mcr;
 
reg_mcr = priv->read(®s->mcr);
@@ -412,20 +413,37 @@ static inline void flexcan_enter_stop_mode(struct 
flexcan_priv *priv)
/* enable stop request */
regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
   1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
+
+   /* get stop acknowledgment */
+   if (regmap_read_poll_timeout(priv->stm.gpr, priv->stm.ack_gpr,
+ackval, ackval & (1 << priv->stm.ack_bit),
+0, FLEXCAN_TIMEOUT_US))
+   return -ETIMEDOUT;
+
+   return 0;
 }
 
-static inline void flexcan_exit_stop_mode(struct flexcan_priv *priv)
+static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv)
 {
struct flexcan_regs __iomem *regs = priv->regs;
+   unsigned int ackval;
u32 reg_mcr;
 
/* remove stop request */
regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
   1 << priv->stm.req_bit, 0);
 
+   /* get stop acknowledgment */
+   if (regmap_read_poll_timeout(priv->stm.gpr, priv->stm.ack_gpr,
+ackval, !(ackval & (1 << 
priv->stm.ack_bit)),
+0, FLEXCAN_TIMEOUT_US))
+   return -ETIMEDOUT;
+
reg_mcr = priv->read(®s->mcr);
reg_mcr &= ~FLEXCAN_MCR_SLF_WAK;
priv->write(reg_mcr, ®s->mcr);
+
+   return 0;
 }
 
 static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
@@ -1614,7 +1632,9 @@ static int __maybe_unused flexcan_suspend(struct device 
*device)
 */
if (device_may_wakeup(device)) {
enable_irq_wake(dev->irq);
-   flexcan_enter_stop_mode(priv);
+   err = flexcan_enter_stop_mode(priv);
+   if (err)
+   return err;
} else {
err = flexcan_chip_disable(priv);
if (err)
@@ -1664,10 +1684,13 @@ static int __maybe_unused flexcan_noirq_resume(struct 
device *device)
 {
struct net_device *dev = dev_get_drvdata(device);
struct flexcan_priv *priv = netdev_priv(dev);
+   int err;
 
if (netif_running(dev) && device_may_wakeup(device)) {
flexcan_enable_wakeup_irq(priv, false);
-   flexcan_exit_stop_mode(priv);
+   err = flexcan_exit_stop_mode(priv);
+   if (err)
+   return err;
}
 
return 0;
-- 
2.20.1



[PATCH 2/7] can: rcar_canfd: fix possible IRQ storm on high load

2019-07-24 Thread Marc Kleine-Budde
From: Nikita Yushchenko 

We have observed rcar_canfd driver entering IRQ storm under high load,
with following scenario:
- rcar_canfd_global_interrupt() in entered due to Rx available,
- napi_schedule_prep() is called, and sets NAPIF_STATE_SCHED in state
- Rx fifo interrupts are masked,
- rcar_canfd_global_interrupt() is entered again, this time due to
  error interrupt (e.g. due to overflow),
- since scheduled napi poller has not yet executed, condition for calling
  napi_schedule_prep() from rcar_canfd_global_interrupt() remains true,
  thus napi_schedule_prep() gets called and sets NAPIF_STATE_MISSED flag
  in state,
- later, napi poller function rcar_canfd_rx_poll() gets executed, and
  calls napi_complete_done(),
- due to NAPIF_STATE_MISSED flag in state, this call does not clear
  NAPIF_STATE_SCHED flag from state,
- on return from napi_complete_done(), rcar_canfd_rx_poll() unmasks Rx
  interrutps,
- Rx interrupt happens, rcar_canfd_global_interrupt() gets called
  and calls napi_schedule_prep(),
- since NAPIF_STATE_SCHED is set in state at this time, this call
  returns false,
- due to that false return, rcar_canfd_global_interrupt() returns
  without masking Rx interrupt
- and this results into IRQ storm: unmasked Rx interrupt happens again
  and again is misprocessed in the same way.

This patch fixes that scenario by unmasking Rx interrupts only when
napi_complete_done() returns true, which means it has cleared
NAPIF_STATE_SCHED in state.

Fixes: dd3bd23eb438 ("can: rcar_canfd: Add Renesas R-Car CAN FD driver")
Signed-off-by: Nikita Yushchenko 
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/rcar/rcar_canfd.c | 9 +
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/rcar/rcar_canfd.c 
b/drivers/net/can/rcar/rcar_canfd.c
index 05410008aa6b..de34a4b82d4a 100644
--- a/drivers/net/can/rcar/rcar_canfd.c
+++ b/drivers/net/can/rcar/rcar_canfd.c
@@ -1508,10 +1508,11 @@ static int rcar_canfd_rx_poll(struct napi_struct *napi, 
int quota)
 
/* All packets processed */
if (num_pkts < quota) {
-   napi_complete_done(napi, num_pkts);
-   /* Enable Rx FIFO interrupts */
-   rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx),
-  RCANFD_RFCC_RFIE);
+   if (napi_complete_done(napi, num_pkts)) {
+   /* Enable Rx FIFO interrupts */
+   rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx),
+  RCANFD_RFCC_RFIE);
+   }
}
return num_pkts;
 }
-- 
2.20.1



pull-request: can 2019-07-24

2019-07-24 Thread Marc Kleine-Budde
Hello David,

this is a pull reqeust of 7 patches for net/master.

The first patch is by Rasmus Villemoes add a missing netif_carrier_off() to
register_candev() so that generic netdev trigger based LEDs are initially off.

Nikita Yushchenko's patch for the rcar_canfd driver fixes a possible IRQ storm
on high load.

The patch by Weitao Hou for the mcp251x driver add missing error checking to
the work queue allocation.

Both Wen Yang's and Joakim Zhang's patch for the flexcan driver fix a problem
with the stop-mode.

Stephane Grosjean contributes a patch for the peak_usb driver to fix a
potential double kfree_skb().

The last patch is by YueHaibing and fixes the error path in can-gw's
cgw_module_init() function.

regards,
Marc

---

The following changes since commit d86afb89305de205b0d2f20c2160adf039e9508d:

  net: thunderx: Use fwnode_get_mac_address() (2019-07-23 14:09:21 -0700)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git 
tags/linux-can-fixes-for-5.3-20190724

for you to fetch changes up to b7a14297f102b6e2ce6f16feffebbb9bde1e9b55:

  can: gw: Fix error path of cgw_module_init (2019-07-24 11:19:03 +0200)


linux-can-fixes-for-5.3-20190724


Joakim Zhang (1):
  can: flexcan: fix stop mode acknowledgment

Nikita Yushchenko (1):
  can: rcar_canfd: fix possible IRQ storm on high load

Rasmus Villemoes (1):
  can: dev: call netif_carrier_off() in register_candev()

Stephane Grosjean (1):
  can: peak_usb: fix potential double kfree_skb()

Weitao Hou (1):
  can: mcp251x: add error check when wq alloc failed

Wen Yang (1):
  can: flexcan: fix an use-after-free in flexcan_setup_stop_mode()

YueHaibing (1):
  can: gw: Fix error path of cgw_module_init

 drivers/net/can/dev.c|  2 ++
 drivers/net/can/flexcan.c| 39 ++
 drivers/net/can/rcar/rcar_canfd.c|  9 ++---
 drivers/net/can/spi/mcp251x.c| 49 +---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c |  8 ++---
 net/can/gw.c | 48 ++-
 6 files changed, 98 insertions(+), 57 deletions(-)




[PATCH 3/7] can: mcp251x: add error check when wq alloc failed

2019-07-24 Thread Marc Kleine-Budde
From: Weitao Hou 

add error check when workqueue alloc failed, and remove redundant code
to make it clear.

Fixes: e163e30e ("can: Driver for the Microchip MCP251x SPI CAN 
controllers")
Signed-off-by: Weitao Hou 
Acked-by: Willem de Bruijn 
Tested-by: Sean Nyekjaer 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/spi/mcp251x.c | 49 ---
 1 file changed, 22 insertions(+), 27 deletions(-)

diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
index 44e99e3d7134..2aec934fab0c 100644
--- a/drivers/net/can/spi/mcp251x.c
+++ b/drivers/net/can/spi/mcp251x.c
@@ -664,17 +664,6 @@ static int mcp251x_power_enable(struct regulator *reg, int 
enable)
return regulator_disable(reg);
 }
 
-static void mcp251x_open_clean(struct net_device *net)
-{
-   struct mcp251x_priv *priv = netdev_priv(net);
-   struct spi_device *spi = priv->spi;
-
-   free_irq(spi->irq, priv);
-   mcp251x_hw_sleep(spi);
-   mcp251x_power_enable(priv->transceiver, 0);
-   close_candev(net);
-}
-
 static int mcp251x_stop(struct net_device *net)
 {
struct mcp251x_priv *priv = netdev_priv(net);
@@ -940,37 +929,43 @@ static int mcp251x_open(struct net_device *net)
   flags | IRQF_ONESHOT, DEVICE_NAME, priv);
if (ret) {
dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
-   mcp251x_power_enable(priv->transceiver, 0);
-   close_candev(net);
-   goto open_unlock;
+   goto out_close;
}
 
priv->wq = alloc_workqueue("mcp251x_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM,
   0);
+   if (!priv->wq) {
+   ret = -ENOMEM;
+   goto out_clean;
+   }
INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler);
INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler);
 
ret = mcp251x_hw_reset(spi);
-   if (ret) {
-   mcp251x_open_clean(net);
-   goto open_unlock;
-   }
+   if (ret)
+   goto out_free_wq;
ret = mcp251x_setup(net, spi);
-   if (ret) {
-   mcp251x_open_clean(net);
-   goto open_unlock;
-   }
+   if (ret)
+   goto out_free_wq;
ret = mcp251x_set_normal_mode(spi);
-   if (ret) {
-   mcp251x_open_clean(net);
-   goto open_unlock;
-   }
+   if (ret)
+   goto out_free_wq;
 
can_led_event(net, CAN_LED_EVENT_OPEN);
 
netif_wake_queue(net);
+   mutex_unlock(&priv->mcp_lock);
 
-open_unlock:
+   return 0;
+
+out_free_wq:
+   destroy_workqueue(priv->wq);
+out_clean:
+   free_irq(spi->irq, priv);
+   mcp251x_hw_sleep(spi);
+out_close:
+   mcp251x_power_enable(priv->transceiver, 0);
+   close_candev(net);
mutex_unlock(&priv->mcp_lock);
return ret;
 }
-- 
2.20.1



[PATCH 7/7] can: gw: Fix error path of cgw_module_init

2019-07-24 Thread Marc Kleine-Budde
From: YueHaibing 

This patch add error path for cgw_module_init to avoid possible crash if
some error occurs.

Fixes: c1aabdf379bc ("can-gw: add netlink based CAN routing")
Signed-off-by: YueHaibing 
Acked-by: Oliver Hartkopp 
Signed-off-by: Marc Kleine-Budde 
---
 net/can/gw.c | 48 +---
 1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/net/can/gw.c b/net/can/gw.c
index 5275ddf580bc..72711053ebe6 100644
--- a/net/can/gw.c
+++ b/net/can/gw.c
@@ -1046,32 +1046,50 @@ static __init int cgw_module_init(void)
pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
max_hops);
 
-   register_pernet_subsys(&cangw_pernet_ops);
+   ret = register_pernet_subsys(&cangw_pernet_ops);
+   if (ret)
+   return ret;
+
+   ret = -ENOMEM;
cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  0, 0, NULL);
-
if (!cgw_cache)
-   return -ENOMEM;
+   goto out_cache_create;
 
/* set notifier */
notifier.notifier_call = cgw_notifier;
-   register_netdevice_notifier(¬ifier);
+   ret = register_netdevice_notifier(¬ifier);
+   if (ret)
+   goto out_register_notifier;
 
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
   NULL, cgw_dump_jobs, 0);
-   if (ret) {
-   unregister_netdevice_notifier(¬ifier);
-   kmem_cache_destroy(cgw_cache);
-   return -ENOBUFS;
-   }
-
-   /* Only the first call to rtnl_register_module can fail */
-   rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
-cgw_create_job, NULL, 0);
-   rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
-cgw_remove_job, NULL, 0);
+   if (ret)
+   goto out_rtnl_register1;
+
+   ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
+  cgw_create_job, NULL, 0);
+   if (ret)
+   goto out_rtnl_register2;
+   ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
+  cgw_remove_job, NULL, 0);
+   if (ret)
+   goto out_rtnl_register3;
 
return 0;
+
+out_rtnl_register3:
+   rtnl_unregister(PF_CAN, RTM_NEWROUTE);
+out_rtnl_register2:
+   rtnl_unregister(PF_CAN, RTM_GETROUTE);
+out_rtnl_register1:
+   unregister_netdevice_notifier(¬ifier);
+out_register_notifier:
+   kmem_cache_destroy(cgw_cache);
+out_cache_create:
+   unregister_pernet_subsys(&cangw_pernet_ops);
+
+   return ret;
 }
 
 static __exit void cgw_module_exit(void)
-- 
2.20.1



[PATCH 4/7] can: flexcan: fix an use-after-free in flexcan_setup_stop_mode()

2019-07-24 Thread Marc Kleine-Budde
From: Wen Yang 

The gpr_np variable is still being used in dev_dbg() after the
of_node_put() call, which may result in use-after-free.

Fixes: de3578c198c6 ("can: flexcan: add self wakeup support")
Signed-off-by: Wen Yang 
Cc: linux-stable  # >= v5.0
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/flexcan.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index f2fe344593d5..33ce45d51e15 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -1437,10 +1437,10 @@ static int flexcan_setup_stop_mode(struct 
platform_device *pdev)
 
priv = netdev_priv(dev);
priv->stm.gpr = syscon_node_to_regmap(gpr_np);
-   of_node_put(gpr_np);
if (IS_ERR(priv->stm.gpr)) {
dev_dbg(&pdev->dev, "could not find gpr regmap\n");
-   return PTR_ERR(priv->stm.gpr);
+   ret = PTR_ERR(priv->stm.gpr);
+   goto out_put_node;
}
 
priv->stm.req_gpr = out_val[1];
@@ -1455,7 +1455,9 @@ static int flexcan_setup_stop_mode(struct platform_device 
*pdev)
 
device_set_wakeup_capable(&pdev->dev, true);
 
-   return 0;
+out_put_node:
+   of_node_put(gpr_np);
+   return ret;
 }
 
 static const struct of_device_id flexcan_of_match[] = {
-- 
2.20.1



Re: [PATCH 0/8] can: flexcan: add CAN FD support for NXP Flexcan

2019-07-25 Thread Marc Kleine-Budde
On 7/25/19 9:38 AM, Joakim Zhang wrote:
> Kindly pinging...
> 
> After you git pull request for linux-can-next-for-5.4-20190724, some patches 
> are missing from linux-can-next/testing.
> can: flexcan: flexcan_mailbox_read() make use of flexcan_write64() to mark 
> the mailbox as read
> can: flexcan: flexcan_irq(): add support for TX mailbox in iflag1
> can: flexcan: flexcan_read_reg_iflag_rx(): optimize reading
> can: flexcan: introduce struct flexcan_priv::tx_mask and make use of it
> can: flexcan: convert struct flexcan_priv::rx_mask{1,2} to rx_mask
> can: flexcan: remove TX mailbox bit from struct flexcan_priv::rx_mask{1,2}
> can: flexcan: rename struct flexcan_priv::reg_imask{1,2}_default to 
> rx_mask{1,2}
> can: flexcan: flexcan_irq(): rename variable reg_iflag -> reg_iflag_rx
> can: flexcan: rename macro FLEXCAN_IFLAG_MB() -> FLEXCAN_IFLAG2_MB()
> 
> You can refer to below link for the reason of adding above patches:
> https://www.spinics.net/lists/linux-can/msg00777.html
> https://www.spinics.net/lists/linux-can/msg01150.html
> 
> Are you prepared to add back these patches as they are necessary for
> Flexcan CAN FD? And this Flexcan CAN FD patch set is based on these
> patches.

Yes, these patches will be added back.

Marc

-- 
Pengutronix e.K.  | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH 0/8] can: flexcan: add CAN FD support for NXP Flexcan

2019-07-25 Thread Marc Kleine-Budde
On 7/25/19 9:53 AM, Marc Kleine-Budde wrote:
> On 7/25/19 9:38 AM, Joakim Zhang wrote:
>> Kindly pinging...
>>
>> After you git pull request for linux-can-next-for-5.4-20190724, some patches 
>> are missing from linux-can-next/testing.
>> can: flexcan: flexcan_mailbox_read() make use of flexcan_write64() to mark 
>> the mailbox as read
>> can: flexcan: flexcan_irq(): add support for TX mailbox in iflag1
>> can: flexcan: flexcan_read_reg_iflag_rx(): optimize reading
>> can: flexcan: introduce struct flexcan_priv::tx_mask and make use of it
>> can: flexcan: convert struct flexcan_priv::rx_mask{1,2} to rx_mask
>> can: flexcan: remove TX mailbox bit from struct flexcan_priv::rx_mask{1,2}
>> can: flexcan: rename struct flexcan_priv::reg_imask{1,2}_default to 
>> rx_mask{1,2}
>> can: flexcan: flexcan_irq(): rename variable reg_iflag -> reg_iflag_rx
>> can: flexcan: rename macro FLEXCAN_IFLAG_MB() -> FLEXCAN_IFLAG2_MB()
>>
>> You can refer to below link for the reason of adding above patches:
>> https://www.spinics.net/lists/linux-can/msg00777.html
>> https://www.spinics.net/lists/linux-can/msg01150.html
>>
>> Are you prepared to add back these patches as they are necessary for
>> Flexcan CAN FD? And this Flexcan CAN FD patch set is based on these
>> patches.
> 
> Yes, these patches will be added back.

I've cleaned up the first patch a bit, and pushed everything to the
testing branch. Can you give it a test.

regards,
Marc

-- 
Pengutronix e.K.  | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH 2/7] can: rcar_canfd: fix possible IRQ storm on high load

2019-07-25 Thread Marc Kleine-Budde
On 7/25/19 3:29 PM, Nikita Yushchenko wrote:
>> NOTE: The patch will not be queued to stable trees until it is upstream.
>>
>> How should we proceed with this patch?
> 
> I don't know.
> Maintainer did not respond, nor to original send nor to resend.

You can backport this patch to v4.9.186 and send it to Sasha

Marc

-- 
Pengutronix e.K.  | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH net-next] can: fix ioctl function removal

2019-07-29 Thread Marc Kleine-Budde
On 7/29/19 10:40 PM, Oliver Hartkopp wrote:
> Commit 60649d4e0af ("can: remove obsolete empty ioctl() handler") replaced the
> almost empty can_ioctl() function with sock_no_ioctl() which always returns
> -EOPNOTSUPP.
> 
> Even though we don't have any ioctl() functions on socket/network layer we 
> need
> to return -ENOIOCTLCMD to be able to forward ioctl commands like SIOCGIFINDEX
> to the network driver layer.
> 
> This patch fixes the wrong return codes in the CAN network layer protocols.
> 
> Reported-by: kernel test robot 
> Fixes: 60649d4e0af ("can: remove obsolete empty ioctl() handler")
> Signed-off-by: Oliver Hartkopp 

David Miller has already applied the patch.

| commit 473d924d7d46cb57aa4c1863261d18366af345af
| Author: Oliver Hartkopp 
| Date:   Mon Jul 29 22:40:56 2019 +0200

Thanks,
Marc

-- 
Pengutronix e.K.  | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


[PATCH 4/4] can: peak_usb: pcan_usb_pro: Fix info-leaks to USB devices

2019-08-02 Thread Marc Kleine-Budde
From: Tomas Bortoli 

Uninitialized Kernel memory can leak to USB devices.

Fix by using kzalloc() instead of kmalloc() on the affected buffers.

Signed-off-by: Tomas Bortoli 
Reported-by: syzbot+d6a5a1a3657b596ef...@syzkaller.appspotmail.com
Fixes: f14e22435a27 ("net: can: peak_usb: Do not do dma on the stack")
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c 
b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index 178bb7cff0c1..53cb2f72bdd0 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -494,7 +494,7 @@ static int pcan_usb_pro_drv_loaded(struct peak_usb_device 
*dev, int loaded)
u8 *buffer;
int err;
 
-   buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
+   buffer = kzalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
 
-- 
2.20.1



[PATCH 3/4] can: peak_usb: pcan_usb_fd: uix info-leaks to USB devices

2019-08-02 Thread Marc Kleine-Budde
From: Tomas Bortoli 

Uninitialized Kernel memory can leak to USB devices.

Fix by using kzalloc() instead of kmalloc() on the affected buffers.

Signed-off-by: Tomas Bortoli 
Reported-by: syzbot+513e4d0985298538b...@syzkaller.appspotmail.com
Fixes: 0a25e1f4f185 ("can: peak_usb: add support for PEAK new CANFD USB 
adapters")
Cc: linux-stable 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c 
b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
index 34761c3a6286..47cc1ff5b88e 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -841,7 +841,7 @@ static int pcan_usb_fd_init(struct peak_usb_device *dev)
goto err_out;
 
/* allocate command buffer once for all for the interface */
-   pdev->cmd_buffer_addr = kmalloc(PCAN_UFD_CMD_BUFFER_SIZE,
+   pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE,
GFP_KERNEL);
if (!pdev->cmd_buffer_addr)
goto err_out_1;
-- 
2.20.1



[PATCH 2/4] can: peak_usb: force the string buffer NULL-terminated

2019-08-02 Thread Marc Kleine-Budde
From: Wang Xiayang 

strncpy() does not ensure NULL-termination when the input string size
equals to the destination buffer size IFNAMSIZ. The output string is
passed to dev_info() which relies on the NULL-termination.

Use strlcpy() instead.

This issue is identified by a Coccinelle script.

Signed-off-by: Wang Xiayang 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c 
b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index 22b9c8e6d040..65dce642b86b 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -855,7 +855,7 @@ static void peak_usb_disconnect(struct usb_interface *intf)
 
dev_prev_siblings = dev->prev_siblings;
dev->state &= ~PCAN_USB_STATE_CONNECTED;
-   strncpy(name, netdev->name, IFNAMSIZ);
+   strlcpy(name, netdev->name, IFNAMSIZ);
 
unregister_netdev(netdev);
 
-- 
2.20.1



[PATCH 1/4] can: sja1000: force the string buffer NULL-terminated

2019-08-02 Thread Marc Kleine-Budde
From: Wang Xiayang 

strncpy() does not ensure NULL-termination when the input string size
equals to the destination buffer size IFNAMSIZ. The output string
'name' is passed to dev_info which relies on NULL-termination.

Use strlcpy() instead.

This issue is identified by a Coccinelle script.

Signed-off-by: Wang Xiayang 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/sja1000/peak_pcmcia.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/sja1000/peak_pcmcia.c 
b/drivers/net/can/sja1000/peak_pcmcia.c
index 185c7f7d38a4..5e0d5e8101c8 100644
--- a/drivers/net/can/sja1000/peak_pcmcia.c
+++ b/drivers/net/can/sja1000/peak_pcmcia.c
@@ -479,7 +479,7 @@ static void pcan_free_channels(struct pcan_pccard *card)
if (!netdev)
continue;
 
-   strncpy(name, netdev->name, IFNAMSIZ);
+   strlcpy(name, netdev->name, IFNAMSIZ);
 
unregister_sja1000dev(netdev);
 
-- 
2.20.1



pull-request: can 2019-08-02

2019-08-02 Thread Marc Kleine-Budde
Hello David,

this is a pull request of 4 patches for net/master.

The first two patches are by Wang Xiayang, they force that the string buffer
during a dev_info() is properly NULL terminated.

The last two patches are by Tomas Bortoli and fix both a potential info leak of
kernel memory to USB devices.

regards,
Marc

---

The following changes since commit 224c04973db1125fcebefffd86115f99f50f8277:

  net: usb: pegasus: fix improper read if get_registers() fail (2019-08-01 
18:18:27 -0400)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git 
tags/linux-can-fixes-for-5.3-20190802

for you to fetch changes up to ead16e53c2f0ed946d82d4037c630e2f60f4ab69:

  can: peak_usb: pcan_usb_pro: Fix info-leaks to USB devices (2019-08-02 
13:58:01 +0200)


linux-can-fixes-for-5.3-20190802


Tomas Bortoli (2):
  can: peak_usb: pcan_usb_fd: Fix info-leaks to USB devices
  can: peak_usb: pcan_usb_pro: Fix info-leaks to USB devices

Wang Xiayang (2):
  can: sja1000: force the string buffer NULL-terminated
  can: peak_usb: force the string buffer NULL-terminated

 drivers/net/can/sja1000/peak_pcmcia.c| 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c   | 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c  | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)





Re: [PATCH] pcan_usb_fd: zero out the common command buffer

2019-08-08 Thread Marc Kleine-Budde
On 8/8/19 8:03 PM, David Miller wrote:
> From: Oliver Neukum 
> Date: Thu,  8 Aug 2019 11:28:25 +0200
> 
>> Lest we leak kernel memory to a device we better zero out buffers.
>>
>> Reported-by: syzbot+513e4d0985298538b...@syzkaller.appspotmail.com
>> Signed-off-by: Oliver Neukum 
> 
> Please CC: the CAN subsystem maintainers, as this is clearly listed in the
> MAINTAINERS file.

The issue is already fixed and already mainline (stable@v.k.o is on Cc):

30a8beeb3042 can: peak_usb: pcan_usb_fd: Fix info-leaks to USB devices

Marc

-- 
Pengutronix e.K.  | Marc Kleine-Budde   |
Industrial Linux Solutions| Phone: +49-231-2826-924 |
Vertretung West/Dortmund  | Fax:   +49-5121-206917- |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH] can: mcp251xfd: fix wrong check in mcp251xfd_handle_rxif_one

2021-01-12 Thread Marc Kleine-Budde
On 1/13/21 8:31 AM, Qinglang Miao wrote:
> If alloc_canfd_skb returns NULL, 'cfg' is an uninitialized
> variable, so we should check 'skb' rather than 'cfd' after
> calling alloc_canfd_skb(priv->ndev, &cfd).
> 
> Fixes: 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI 
> CAN")
> Signed-off-by: Qinglang Miao 

applied to linux-can/testing

Tnx,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v4 1/1] can: dev: add software tx timestamps

2021-01-13 Thread Marc Kleine-Budde
On 1/13/21 2:48 AM, Jakub Kicinski wrote:
>> Please make sure to address the warnings before this hits net-next:
>>
>> https://patchwork.kernel.org/project/netdevbpf/patch/20210112130538.14912-2-mailhol.vinc...@wanadoo.fr/
>>
>> Actually it appears not to build with allmodconfig..?
> 
> Erm, apologies, I confused different CAN patches, this one did not get
> build tested.

For the record:

The patch that was tested by the kbuild robot and failed to build, failed
because the patch didn't include a git base reference [1] and was applied to the
wrong tree. Using the correct tree it compiles.

regards,
Marc

[1] The submitter already knows to include that next time.

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v10 1/1] can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces

2021-01-13 Thread Marc Kleine-Budde
 * @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 state. 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.
> + */
> +struct es58x_priv {
> + struct can_priv can;
> + struct es58x_device *es58x_dev;
> + struct urb *tx_urb;
> +
> + spinlock_t echo_skb_spinlock;   /* Comments: c.f. supra */
> + u32 current_packet_idx;
> + u16 echo_skb_tail_idx;
> + u16 echo_skb_head_idx;
> + u16 num_echo_skb;

Can you explain me how the tx-path works, especially why you need the
current_packet_idx.

In the mcp251xfd driver, the number of TX buffers is a power of two, that makes
things easier. tx_heads % len points to the next buffer to be filled, tx_tail %
len points to the next buffer to be completed. tx_head - tx_tail is the fill
level of the FIFO. This works without spinlocks.

> +
> + u16 tx_total_frame_len;
> + u8 tx_can_msg_cnt;
> + bool tx_can_msg_is_fd;
> +
> + u8 err_passive_before_rtx_success;
> +
> + u8 channel_idx;
> +};

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v10 1/1] can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces

2021-01-13 Thread Marc Kleine-Budde
On 1/13/21 1:15 PM, Vincent MAILHOL wrote:
>>> +/**
>>> + * es58x_calculate_crc() - Compute the crc16 of a given URB.
>>> + * @urb_cmd: The URB command for which we want to calculate the CRC.
>>> + * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
>>> + *   (ES58X_CRC_CALC_OFFSET + sizeof(crc))
>>> + *
>>> + * Return: crc16 value.
>>> + */
>>> +static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 
>>> urb_len)
>>> +{
>>> + u16 crc;
>>> + ssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc);
>>> +
>>> + WARN_ON(len < 0);
>>
>> Is it possible to ensure earlier, that the urbs are of correct length?
> 
> Easy answer: it is ensured.

Okay, then get rid of those checks :)

> On the Tx branch, I create the urbs so I
> know for sure that the length is correct. On the Rx branch, I have a
> dedicated function: es58x_check_rx_urb() for this purpose.  I
> will remove that WARN_ON() and the one in es58x_get_crc().
> 
> I will also check the other WARN_ON() in my code to see if they
> can be removed (none on my test throughout the last ten months or
> so could trigger any of these WARN_ON() so should be fine to
> remove but I will double check).

[..]

>>> +struct es58x_priv {
>>> + struct can_priv can;
>>> + struct es58x_device *es58x_dev;
>>> + struct urb *tx_urb;
>>> +
>>> + spinlock_t echo_skb_spinlock;   /* Comments: c.f. supra */
>>> + u32 current_packet_idx;
>>> + u16 echo_skb_tail_idx;
>>> + u16 echo_skb_head_idx;
>>> + u16 num_echo_skb;
>>
>> Can you explain me how the tx-path works, especially why you need the
>> current_packet_idx.
>>
>> In the mcp251xfd driver, the number of TX buffers is a power of two, that 
>> makes
>> things easier. tx_heads % len points to the next buffer to be filled, 
>> tx_tail %
>> len points to the next buffer to be completed. tx_head - tx_tail is the fill
>> level of the FIFO. This works without spinlocks.
> 
> For what I understand of your explanations here are the equivalences
> between the etas_es58x and the mcp251xfd drivers:
> 
>  ++---+
>  | etas_es58x | mcp251xfd |
>  ++---+
>  | current_packet_idx | tx_head   |
>  | echo_skb_tail_idx  | tx_tail % len |
>  | echo_skb_head_idx  | tx_head % len |
>  | num_echo_skb   | tx_head - tx_tail |
>  ++---+
> 
> Especially, the current_packet_idx is sent to the device and returned
> to the driver upon completion.

Is current_packet_idx used only for the TX-PATH?

> I wish the TX buffers were a power of two which is unfortunately not
> the case. The theoretical TX buffer sizes are 330 and 500 for the two
> devices so I wrote the code to work with those values. The exact size
> of the TX buffer is actually even more of a mystery because during
> testing both devices were unstable when using the theoretical values
> and I had to lower these. There is a comment at the bottom of
> es581_4.c and es58x_fd.c to reflect those issues.

What are the performance penalties for using 256 for the fd and 64 ofr the 
other?

> Because I do not
> have access to the source code of the firmware, I could not identify
> the root cause.

ok

> My understanding is that having a queue size being a power of two is
> required in order not to use spinlocks (else, modulo operations would
> break when the index wraparound back to zero). I tried to minimize the
> number of spinlock: only one per bulk send or bulk receive.

With queue size being power of two the modulo can be written as a mask
operation, so it's reasonable fast. So you only need to care about tx_head and
tx_tail, and there is only one writer for each variable. With a little dance and
barriers when stopping and starting the queue it's race-free without spinlocks.

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v10 1/1] can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces

2021-01-13 Thread Marc Kleine-Budde
On 1/13/21 3:35 PM, Vincent MAILHOL wrote:
>> My understanding is that having a queue size being a power of two is
>> required in order not to use spinlocks (else, modulo operations would
>> break when the index wraparound back to zero). I tried to minimize the
>> number of spinlock: only one per bulk send or bulk receive.
> 
> Or do you mean to round up the skb_echo array length to the next power
> of two in the driver despite the actual size of the device FIFO? Did
> not think about that in the past but that should work.
> 
> I am going to think a bit more of how to improve that.

For the echo_skb rounding up might work, but you should round down, so that your
can directly use the masked value.

Marc
-- 
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- |



signature.asc
Description: OpenPGP digital signature


[net-next 03/17] can: dev: move driver related infrastructure into separate subdir

2021-01-13 Thread Marc Kleine-Budde
This patch moves the CAN driver related infrastructure into a separate subdir.
It will be split into more files in the coming patches.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-3-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/Makefile   | 7 +--
 drivers/net/can/dev/Makefile   | 7 +++
 drivers/net/can/{ => dev}/dev.c| 0
 drivers/net/can/{ => dev}/rx-offload.c | 0
 4 files changed, 8 insertions(+), 6 deletions(-)
 create mode 100644 drivers/net/can/dev/Makefile
 rename drivers/net/can/{ => dev}/dev.c (100%)
 rename drivers/net/can/{ => dev}/rx-offload.c (100%)

diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile
index 22164300122d..a2b4463d8480 100644
--- a/drivers/net/can/Makefile
+++ b/drivers/net/can/Makefile
@@ -7,12 +7,7 @@ obj-$(CONFIG_CAN_VCAN) += vcan.o
 obj-$(CONFIG_CAN_VXCAN)+= vxcan.o
 obj-$(CONFIG_CAN_SLCAN)+= slcan.o
 
-obj-$(CONFIG_CAN_DEV)  += can-dev.o
-can-dev-y  += dev.o
-can-dev-y  += rx-offload.o
-
-can-dev-$(CONFIG_CAN_LEDS) += led.o
-
+obj-y  += dev/
 obj-y  += rcar/
 obj-y  += spi/
 obj-y  += usb/
diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
new file mode 100644
index ..cba92e6bcf6f
--- /dev/null
+++ b/drivers/net/can/dev/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_CAN_DEV)  += can-dev.o
+can-dev-y  += dev.o
+can-dev-y  += rx-offload.o
+
+can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev/dev.c
similarity index 100%
rename from drivers/net/can/dev.c
rename to drivers/net/can/dev/dev.c
diff --git a/drivers/net/can/rx-offload.c b/drivers/net/can/dev/rx-offload.c
similarity index 100%
rename from drivers/net/can/rx-offload.c
rename to drivers/net/can/dev/rx-offload.c
-- 
2.29.2




pull-request: can 2021-01-13

2021-01-13 Thread Marc Kleine-Budde
Hello Jakub, hello David,

this is a pull request of 2 patches for net/master.

The first patch is by Oliver Hartkopp for the CAn ISO-TP protocol and fixes a
kernel information leak to userspace.

The last patch is by Qinglang Miao for the mcp251xfd driver and fixes a NULL
pointer check to work on the correct variable.

regards,
Marc

---

The following changes since commit a95d25dd7b94a5ba18246da09b4218f132fed60e:

  rxrpc: Call state should be read with READ_ONCE() under some circumstances 
(2021-01-13 10:38:20 -0800)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git 
tags/linux-can-fixes-for-5.11-20210113

for you to fetch changes up to ca4c6ebeeb50112f5178f14bfb6d9e8ddf148545:

  can: mcp251xfd: mcp251xfd_handle_rxif_one(): fix wrong NULL pointer check 
(2021-01-13 22:16:16 +0100)


linux-can-fixes-for-5.11-20210113


Oliver Hartkopp (1):
  can: isotp: isotp_getname(): fix kernel information leak

Qinglang Miao (1):
  can: mcp251xfd: mcp251xfd_handle_rxif_one(): fix wrong NULL pointer check

 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 2 +-
 net/can/isotp.c| 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)




[net 1/2] can: isotp: isotp_getname(): fix kernel information leak

2021-01-13 Thread Marc Kleine-Budde
From: Oliver Hartkopp 

Initialize the sockaddr_can structure to prevent a data leak to user space.

Suggested-by: Cong Wang 
Reported-by: syzbot+057884e2f453e8afe...@syzkaller.appspotmail.com
Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Signed-off-by: Oliver Hartkopp 
Link: https://lore.kernel.org/r/20210112091643.11789-1-socket...@hartkopp.net
Signed-off-by: Marc Kleine-Budde 
---
 net/can/isotp.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 7839c3b9e5be..3ef7f78e553b 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1155,6 +1155,7 @@ static int isotp_getname(struct socket *sock, struct 
sockaddr *uaddr, int peer)
if (peer)
return -EOPNOTSUPP;
 
+   memset(addr, 0, sizeof(*addr));
addr->can_family = AF_CAN;
addr->can_ifindex = so->ifindex;
addr->can_addr.tp.rx_id = so->rxid;

base-commit: a95d25dd7b94a5ba18246da09b4218f132fed60e
-- 
2.29.2




[net-next 13/17] can: dev: can_put_echo_skb(): extend to handle frame_len

2021-01-13 Thread Marc Kleine-Budde
From: Vincent Mailhol 

Add a frame_len argument to can_put_echo_skb() which is used to save length of
the CAN frame into field frame_len of struct can_skb_priv so that it can be
later used after transmission completion. Convert all users of this function,
too.

Drivers which implement BQL call can_put_echo_skb() with the output of
can_skb_get_frame_len(skb) and drivers which do not simply pass zero as an
input (in the same way that NULL would be given to can_get_echo_skb()). This
way, we have a nice symmetry between the two echo functions.

Link: 
https://lore.kernel.org/r/20210111061335.39983-1-mailhol.vinc...@wanadoo.fr
Signed-off-by: Marc Kleine-Budde 
Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-13-...@pengutronix.de
Signed-off-by: Vincent Mailhol 
---
 drivers/net/can/at91_can.c   | 2 +-
 drivers/net/can/c_can/c_can.c| 2 +-
 drivers/net/can/cc770/cc770.c| 2 +-
 drivers/net/can/dev/skb.c| 5 -
 drivers/net/can/flexcan.c| 2 +-
 drivers/net/can/grcan.c  | 2 +-
 drivers/net/can/ifi_canfd/ifi_canfd.c| 2 +-
 drivers/net/can/kvaser_pciefd.c  | 2 +-
 drivers/net/can/m_can/m_can.c| 4 ++--
 drivers/net/can/mscan/mscan.c| 2 +-
 drivers/net/can/pch_can.c| 2 +-
 drivers/net/can/peak_canfd/peak_canfd.c  | 2 +-
 drivers/net/can/rcar/rcar_can.c  | 2 +-
 drivers/net/can/rcar/rcar_canfd.c| 2 +-
 drivers/net/can/sja1000/sja1000.c| 2 +-
 drivers/net/can/softing/softing_main.c   | 2 +-
 drivers/net/can/spi/hi311x.c | 2 +-
 drivers/net/can/spi/mcp251x.c| 2 +-
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c   | 2 +-
 drivers/net/can/sun4i_can.c  | 2 +-
 drivers/net/can/ti_hecc.c| 2 +-
 drivers/net/can/usb/ems_usb.c| 2 +-
 drivers/net/can/usb/esd_usb2.c   | 2 +-
 drivers/net/can/usb/gs_usb.c | 2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 2 +-
 drivers/net/can/usb/mcba_usb.c   | 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 2 +-
 drivers/net/can/usb/ucan.c   | 2 +-
 drivers/net/can/usb/usb_8dev.c   | 2 +-
 drivers/net/can/xilinx_can.c | 4 ++--
 include/linux/can/skb.h  | 2 +-
 31 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/drivers/net/can/at91_can.c b/drivers/net/can/at91_can.c
index 5284f0ab3b06..90b223a80ed4 100644
--- a/drivers/net/can/at91_can.c
+++ b/drivers/net/can/at91_can.c
@@ -484,7 +484,7 @@ static netdev_tx_t at91_start_xmit(struct sk_buff *skb, 
struct net_device *dev)
stats->tx_bytes += cf->len;
 
/* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
-   can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv));
+   can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv), 0);
 
/*
 * we have to stop the queue and deliver all messages in case
diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c
index 63f48b016ecd..13638954a25c 100644
--- a/drivers/net/can/c_can/c_can.c
+++ b/drivers/net/can/c_can/c_can.c
@@ -476,7 +476,7 @@ static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
 */
c_can_setup_tx_object(dev, IF_TX, frame, idx);
priv->dlc[idx] = frame->len;
-   can_put_echo_skb(skb, dev, idx);
+   can_put_echo_skb(skb, dev, idx, 0);
 
/* Update the active bits */
atomic_add((1 << idx), &priv->tx_active);
diff --git a/drivers/net/can/cc770/cc770.c b/drivers/net/can/cc770/cc770.c
index 8d9f332c35e0..e53ca338368a 100644
--- a/drivers/net/can/cc770/cc770.c
+++ b/drivers/net/can/cc770/cc770.c
@@ -702,7 +702,7 @@ static void cc770_tx_interrupt(struct net_device *dev, 
unsigned int o)
stats->tx_bytes += cf->len;
stats->tx_packets++;
 
-   can_put_echo_skb(priv->tx_skb, dev, 0);
+   can_put_echo_skb(priv->tx_skb, dev, 0, 0);
can_get_echo_skb(dev, 0);
priv->tx_skb = NULL;
 
diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index 24f782a23409..c184b4dce19e 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -38,7 +38,7 @@ void can_flush_echo_skb(struct net_device *dev)
  * priv->echo_skb, if necessary.
  */
 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
-unsigned int idx)
+unsigned int idx, unsigned int frame_len)
 {
struct can_priv *priv = netdev_priv(dev);
 
@@ -62,6 +62,9 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device 
*dev,
skb->ip_summed = CHECKSUM_UNNECESSARY;
skb->dev = 

[net 2/2] can: mcp251xfd: mcp251xfd_handle_rxif_one(): fix wrong NULL pointer check

2021-01-13 Thread Marc Kleine-Budde
From: Qinglang Miao 

If alloc_canfd_skb() returns NULL, 'cfg' is an uninitialized variable, so we
should check 'skb' rather than 'cfd' after calling alloc_canfd_skb(priv->ndev,
&cfd).

Fixes: 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI 
CAN")
Signed-off-by: Qinglang Miao 
Reviewed-by: Manivannan Sadhasivam 
Link: https://lore.kernel.org/r/20210113073100.79552-1-miaoqingl...@huawei.com
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c 
b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
index 36235afb0bc6..f07e8b737d31 100644
--- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
+++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
@@ -1491,7 +1491,7 @@ mcp251xfd_handle_rxif_one(struct mcp251xfd_priv *priv,
else
skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cfd);
 
-   if (!cfd) {
+   if (!skb) {
stats->rx_dropped++;
return 0;
}
-- 
2.29.2




[net-next 16/17] can: dev: can_put_echo_skb(): add software tx timestamps

2021-01-13 Thread Marc Kleine-Budde
From: Vincent Mailhol 

Call skb_tx_timestamp() within can_put_echo_skb() so that a software tx
timestamp gets attached to the skb.

There two main reasons to include this call in can_put_echo_skb():

  * It easily allow to enable the tx timestamp on all devices with
just one small change.

  * According to Documentation/networking/timestamping.rst, the tx
timestamps should be generated in the device driver as close as possible,
but always prior to passing the packet to the network interface. During the
call to can_put_echo_skb(), the skb gets cloned meaning that the driver
should not dereference the skb variable anymore after can_put_echo_skb()
returns. This makes can_put_echo_skb() the very last place we can use the
skb without having to access the echo_skb[] array.

Remark: by default, skb_tx_timestamp() does nothing. It needs to be activated
by passing the SOF_TIMESTAMPING_TX_SOFTWARE flag either through socket options
or control messages.

References:

 * Support for the error queue in CAN RAW sockets (which is needed for
   tx timestamps) was introduced in:
   https://git.kernel.org//torvalds/c/eb88531bdbfaafb827192d1fc6c5a3fcc4fadd96

  * Put the call to skb_tx_timestamp() just before adding it to the
array:

https://lore.kernel.org/r/043c3ea1-6bdd-59c0-0269-27b2b5b36...@victronenergy.com

  * About Tx hardware timestamps
https://lore.kernel.org/r/2021071152.gb11...@hoboy.vegasvil.org

Signed-off-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/20210112095437.6488-2-mailhol.vinc...@wanadoo.fr
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/skb.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index 53683d4312f1..6a64fe410987 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -65,6 +65,8 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device 
*dev,
/* save frame_len to reuse it when transmission is completed */
can_skb_prv(skb)->frame_len = frame_len;
 
+   skb_tx_timestamp(skb);
+
/* save this skb for tx interrupt echo handling */
priv->echo_skb[idx] = skb;
} else {
-- 
2.29.2




[net-next 11/17] can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer

2021-01-13 Thread Marc Kleine-Budde
From: Vincent Mailhol 

This patch adds the function can_skb_get_frame_len() which returns the length
of a CAN frame on the data link layer, including Start-of-frame, Identifier,
various other bits, the actual data, the CRC, the End-of-frame, the Inter frame
spacing.

Co-developed-by: Arunachalam Santhanam 
Signed-off-by: Arunachalam Santhanam 
Co-developed-by: Vincent Mailhol 
Signed-off-by: Vincent Mailhol 
Acked-by: Vincent Mailhol 
Reviewed-by: Vincent Mailhol 
Co-developed-by: Marc Kleine-Budde 
Link: https://lore.kernel.org/r/2021041930.693847-11-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/length.c |  50 +++
 include/linux/can/length.h   | 120 +++
 2 files changed, 170 insertions(+)

diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index d695a3bee1ed..20cd153a45a5 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -38,3 +38,53 @@ u8 can_fd_len2dlc(u8 len)
return len2dlc[len];
 }
 EXPORT_SYMBOL_GPL(can_fd_len2dlc);
+
+/**
+ * can_skb_get_frame_len() - Calculate the CAN Frame length in bytes
+ * of a given skb.
+ * @skb: socket buffer of a CAN message.
+ *
+ * Do a rough calculation: bit stuffing is ignored and length in bits
+ * is rounded up to a length in bytes.
+ *
+ * Rationale: this function is to be used for the BQL functions
+ * (netdev_sent_queue() and netdev_completed_queue()) which expect a
+ * value in bytes. Just using skb->len is insufficient because it will
+ * return the constant value of CAN(FD)_MTU. Doing the bit stuffing
+ * calculation would be too expensive in term of computing resources
+ * for no noticeable gain.
+ *
+ * Remarks: The payload of CAN FD frames with BRS flag are sent at a
+ * different bitrate. Currently, the can-utils canbusload tool does
+ * not support CAN-FD yet and so we could not run any benchmark to
+ * measure the impact. There might be possible improvement here.
+ *
+ * Return: length in bytes.
+ */
+unsigned int can_skb_get_frame_len(const struct sk_buff *skb)
+{
+   const struct canfd_frame *cf = (const struct canfd_frame *)skb->data;
+   u8 len;
+
+   if (can_is_canfd_skb(skb))
+   len = canfd_sanitize_len(cf->len);
+   else if (cf->can_id & CAN_RTR_FLAG)
+   len = 0;
+   else
+   len = cf->len;
+
+   if (can_is_canfd_skb(skb)) {
+   if (cf->can_id & CAN_EFF_FLAG)
+   len += CANFD_FRAME_OVERHEAD_EFF;
+   else
+   len += CANFD_FRAME_OVERHEAD_SFF;
+   } else {
+   if (cf->can_id & CAN_EFF_FLAG)
+   len += CAN_FRAME_OVERHEAD_EFF;
+   else
+   len += CAN_FRAME_OVERHEAD_SFF;
+   }
+
+   return len;
+}
+EXPORT_SYMBOL_GPL(can_skb_get_frame_len);
diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index b2313b2a0b02..6995092b774e 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -1,10 +1,127 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /* Copyright (C) 2020 Oliver Hartkopp 
+ * Copyright (C) 2020 Marc Kleine-Budde 
  */
 
 #ifndef _CAN_LENGTH_H
 #define _CAN_LENGTH_H
 
+/*
+ * Size of a Classical CAN Standard Frame
+ *
+ * Name of Field   Bits
+ * -
+ * Start-of-frame  1
+ * Identifier  11
+ * Remote transmission request (RTR)   1
+ * Identifier extension bit (IDE)  1
+ * Reserved bit (r0)   1
+ * Data length code (DLC)  4
+ * Data field  0...64
+ * CRC 15
+ * CRC delimiter   1
+ * ACK slot1
+ * ACK delimiter   1
+ * End-of-frame (EOF)  7
+ * Inter frame spacing 3
+ *
+ * rounded up and ignoring bitstuffing
+ */
+#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8)
+
+/*
+ * Size of a Classical CAN Extended Frame
+ *
+ * Name of Field   Bits
+ * -
+ * Start-of-frame  1
+ * Identifier A11
+ * Substitute remote request (SRR) 1
+ * Identifier extension bit (IDE)  1
+ * Identifier B18
+ * Remote transmission request (RTR)   1
+ * Reserved bits (r1, r0)  2
+ * Data length code (DLC)  4
+ * Data field  0...64
+ * CRC 15
+ * CRC delimiter   1
+ * ACK slot1
+ * ACK delimiter   1
+ * End-of-frame (EOF)  7
+ * Inter frame spacing 3
+ *
+ * rounded up and ignoring bitstuffing
+ */
+#define CAN_FRAME_OVERHEAD_EFF DIV_

[net-next 09/17] can: length: can_fd_len2dlc(): simplify length calculcation

2021-01-13 Thread Marc Kleine-Budde
If the length paramter in len2dlc() exceeds the size of the len2dlc array, we
return 0xF. This is equal to the last 16 members of the array.

This patch removes these members from the array, uses ARRAY_SIZE() for the
length check, and returns CANFD_MAX_DLC (which is 0xf).

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-9-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/length.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index 5e7d481717ea..d695a3bee1ed 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -27,15 +27,13 @@ static const u8 len2dlc[] = {
13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
-   15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
-   15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
 };
 
 /* map the sanitized data length to an appropriate data length code */
 u8 can_fd_len2dlc(u8 len)
 {
-   if (unlikely(len > 64))
-   return 0xF;
+   if (len > ARRAY_SIZE(len2dlc))
+   return CANFD_MAX_DLC;
 
return len2dlc[len];
 }
-- 
2.29.2




[net-next 17/17] can: tcan4x5x: remove __packed attribute from struct tcan4x5x_map_buf

2021-01-13 Thread Marc Kleine-Budde
The first member of struct tcan4x5x_map_buf is the struct tcan4x5x_buf_cmd,
which has a size of 4 bytes. It's followed by an array of u8. The compiler
places the array directly after the struct tcan4x5x_buf_cmd.

This patch removes the not needed attribute __packed from the struct
tcan4x5x_map_buf.

Suggested-by: Jakub Kicinski 
Link: https://lore.kernel.org/r/20210113203955.912916-1-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/m_can/tcan4x5x.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/m_can/tcan4x5x.h b/drivers/net/can/m_can/tcan4x5x.h
index 7bf264f8e81f..c66da829b795 100644
--- a/drivers/net/can/m_can/tcan4x5x.h
+++ b/drivers/net/can/m_can/tcan4x5x.h
@@ -25,7 +25,7 @@ struct __packed tcan4x5x_buf_cmd {
u8 len;
 };
 
-struct __packed tcan4x5x_map_buf {
+struct tcan4x5x_map_buf {
struct tcan4x5x_buf_cmd cmd;
u8 data[256 * sizeof(u32)];
 } cacheline_aligned;
-- 
2.29.2




[net-next 15/17] can: dev: can_rx_offload_get_echo_skb(): extend to return can frame length

2021-01-13 Thread Marc Kleine-Budde
In order to implement byte queue limits (bql) in CAN drivers, the length of the
CAN frame needs to be passed into the networking stack after queueing and after
transmission completion.

To avoid to calculate this length twice, extend can_rx_offload_get_echo_skb()
to return that value. Convert all users of this function, too.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-15-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/rx-offload.c   | 5 +++--
 drivers/net/can/flexcan.c  | 5 +++--
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 2 +-
 drivers/net/can/ti_hecc.c  | 2 +-
 include/linux/can/rx-offload.h | 3 ++-
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 6a26b5282df1..ab2c1543786c 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -263,7 +263,8 @@ int can_rx_offload_queue_sorted(struct can_rx_offload 
*offload,
 EXPORT_SYMBOL_GPL(can_rx_offload_queue_sorted);
 
 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
-unsigned int idx, u32 timestamp)
+unsigned int idx, u32 timestamp,
+unsigned int *frame_len_ptr)
 {
struct net_device *dev = offload->dev;
struct net_device_stats *stats = &dev->stats;
@@ -271,7 +272,7 @@ unsigned int can_rx_offload_get_echo_skb(struct 
can_rx_offload *offload,
u8 len;
int err;
 
-   skb = __can_get_echo_skb(dev, idx, &len, NULL);
+   skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
if (!skb)
return 0;
 
diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index 202d08f8e1a4..5d9157c655e9 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -1122,8 +1122,9 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl);
 
handled = IRQ_HANDLED;
-   stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload,
-  0, reg_ctrl << 
16);
+   stats->tx_bytes +=
+   can_rx_offload_get_echo_skb(&priv->offload, 0,
+   reg_ctrl << 16, NULL);
stats->tx_packets++;
can_led_event(dev, CAN_LED_EVENT_TX);
 
diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c 
b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
index 95bba456a4cd..63bbe0930e53 100644
--- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
+++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
@@ -1271,7 +1271,7 @@ mcp251xfd_handle_tefif_one(struct mcp251xfd_priv *priv,
stats->tx_bytes +=
can_rx_offload_get_echo_skb(&priv->offload,
mcp251xfd_get_tef_tail(priv),
-   hw_tef_obj->ts);
+   hw_tef_obj->ts, NULL);
stats->tx_packets++;
priv->tef->tail++;
 
diff --git a/drivers/net/can/ti_hecc.c b/drivers/net/can/ti_hecc.c
index 485c19bc98c2..73245d8836a9 100644
--- a/drivers/net/can/ti_hecc.c
+++ b/drivers/net/can/ti_hecc.c
@@ -757,7 +757,7 @@ static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
stamp = hecc_read_stamp(priv, mbxno);
stats->tx_bytes +=
can_rx_offload_get_echo_skb(&priv->offload,
-   mbxno, stamp);
+   mbxno, stamp, NULL);
stats->tx_packets++;
can_led_event(ndev, CAN_LED_EVENT_TX);
--priv->tx_tail;
diff --git a/include/linux/can/rx-offload.h b/include/linux/can/rx-offload.h
index f1b38088b765..40882df7105e 100644
--- a/include/linux/can/rx-offload.h
+++ b/include/linux/can/rx-offload.h
@@ -44,7 +44,8 @@ int can_rx_offload_irq_offload_fifo(struct can_rx_offload 
*offload);
 int can_rx_offload_queue_sorted(struct can_rx_offload *offload,
struct sk_buff *skb, u32 timestamp);
 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
-unsigned int idx, u32 timestamp);
+unsigned int idx, u32 timestamp,
+unsigned int *frame_len_ptr);
 int can_rx_offload_queue_tail(struct can_rx_offload *offload,
  struct sk_buff *skb);
 void can_rx_offload_del(struct can_rx_offload *offload);
-- 
2.29.2




[net-next 12/17] can: dev: extend struct can_skb_priv to hold CAN frame length

2021-01-13 Thread Marc Kleine-Budde
In order to implement byte queue limits (bql) in CAN drivers, the length of the
CAN frame needs to be passed into the networking stack after queueing and after
transmission completion.

To avoid to calculate this length twice, extend the struct can_skb_priv to hold
the length of the CAN frame and extend __can_get_echo_skb() to return that
value.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-12-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/rx-offload.c | 2 +-
 drivers/net/can/dev/skb.c| 9 +++--
 include/linux/can/skb.h  | 4 +++-
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 3c1912c0430b..6a26b5282df1 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -271,7 +271,7 @@ unsigned int can_rx_offload_get_echo_skb(struct 
can_rx_offload *offload,
u8 len;
int err;
 
-   skb = __can_get_echo_skb(dev, idx, &len);
+   skb = __can_get_echo_skb(dev, idx, &len, NULL);
if (!skb)
return 0;
 
diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index 26cd597ff771..24f782a23409 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -76,7 +76,8 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device 
*dev,
 EXPORT_SYMBOL_GPL(can_put_echo_skb);
 
 struct sk_buff *
-__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
+__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr,
+  unsigned int *frame_len_ptr)
 {
struct can_priv *priv = netdev_priv(dev);
 
@@ -91,6 +92,7 @@ __can_get_echo_skb(struct net_device *dev, unsigned int idx, 
u8 *len_ptr)
 * length is supported on both CAN and CANFD frames.
 */
struct sk_buff *skb = priv->echo_skb[idx];
+   struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
struct canfd_frame *cf = (struct canfd_frame *)skb->data;
 
/* get the real payload length for netdev statistics */
@@ -99,6 +101,9 @@ __can_get_echo_skb(struct net_device *dev, unsigned int idx, 
u8 *len_ptr)
else
*len_ptr = cf->len;
 
+   if (frame_len_ptr)
+   *frame_len_ptr = can_skb_priv->frame_len;
+
priv->echo_skb[idx] = NULL;
 
return skb;
@@ -118,7 +123,7 @@ unsigned int can_get_echo_skb(struct net_device *dev, 
unsigned int idx)
struct sk_buff *skb;
u8 len;
 
-   skb = __can_get_echo_skb(dev, idx, &len);
+   skb = __can_get_echo_skb(dev, idx, &len, NULL);
if (!skb)
return 0;
 
diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h
index c90ebbd3008c..5db9da30843c 100644
--- a/include/linux/can/skb.h
+++ b/include/linux/can/skb.h
@@ -20,7 +20,7 @@ void can_flush_echo_skb(struct net_device *dev);
 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
 unsigned int idx);
 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
-  u8 *len_ptr);
+  u8 *len_ptr, unsigned int *frame_len_ptr);
 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
@@ -42,11 +42,13 @@ struct sk_buff *alloc_can_err_skb(struct net_device *dev,
  * struct can_skb_priv - private additional data inside CAN sk_buffs
  * @ifindex:   ifindex of the first interface the CAN frame appeared on
  * @skbcnt:atomic counter to have an unique id together with skb pointer
+ * @frame_len: length of CAN frame in data link layer
  * @cf:align to the following CAN frame at skb->data
  */
 struct can_skb_priv {
int ifindex;
int skbcnt;
+   unsigned int frame_len;
struct can_frame cf[];
 };
 
-- 
2.29.2




[net-next 08/17] can: length: convert to kernel coding style

2021-01-13 Thread Marc Kleine-Budde
This patch converts the file into the kernel coding style.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-8-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/length.c | 28 
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index 6fe18aa23ec9..5e7d481717ea 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -6,8 +6,10 @@
 
 /* CAN DLC to real data length conversion helpers */
 
-static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
-8, 12, 16, 20, 24, 32, 48, 64};
+static const u8 dlc2len[] = {
+   0, 1, 2, 3, 4, 5, 6, 7,
+   8, 12, 16, 20, 24, 32, 48, 64
+};
 
 /* get data length from raw data length code (DLC) */
 u8 can_fd_dlc2len(u8 dlc)
@@ -16,16 +18,18 @@ u8 can_fd_dlc2len(u8 dlc)
 }
 EXPORT_SYMBOL_GPL(can_fd_dlc2len);
 
-static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,/* 0 - 
8 */
-9, 9, 9, 9,/* 9 - 12 */
-10, 10, 10, 10,/* 13 - 16 */
-11, 11, 11, 11,/* 17 - 20 */
-12, 12, 12, 12,/* 21 - 24 */
-13, 13, 13, 13, 13, 13, 13, 13,/* 25 - 32 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 33 - 40 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 41 - 48 */
-15, 15, 15, 15, 15, 15, 15, 15,/* 49 - 56 */
-15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
+static const u8 len2dlc[] = {
+   0, 1, 2, 3, 4, 5, 6, 7, 8,  /* 0 - 8 */
+   9, 9, 9, 9, /* 9 - 12 */
+   10, 10, 10, 10, /* 13 - 16 */
+   11, 11, 11, 11, /* 17 - 20 */
+   12, 12, 12, 12, /* 21 - 24 */
+   13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
+   14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
+   14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
+   15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
+   15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
+};
 
 /* map the sanitized data length to an appropriate data length code */
 u8 can_fd_len2dlc(u8 len)
-- 
2.29.2




[net-next 14/17] can: dev: can_get_echo_skb(): extend to return can frame length

2021-01-13 Thread Marc Kleine-Budde
In order to implement byte queue limits (bql) in CAN drivers, the length of the
CAN frame needs to be passed into the networking stack after queueing and after
transmission completion.

To avoid to calculate this length twice, extend can_get_echo_skb() to return
that value. Convert all users of this function, too.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-14-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/at91_can.c| 2 +-
 drivers/net/can/c_can/c_can.c | 2 +-
 drivers/net/can/cc770/cc770.c | 2 +-
 drivers/net/can/dev/skb.c | 5 +++--
 drivers/net/can/grcan.c   | 2 +-
 drivers/net/can/ifi_canfd/ifi_canfd.c | 2 +-
 drivers/net/can/kvaser_pciefd.c   | 4 ++--
 drivers/net/can/m_can/m_can.c | 4 ++--
 drivers/net/can/mscan/mscan.c | 2 +-
 drivers/net/can/pch_can.c | 2 +-
 drivers/net/can/peak_canfd/peak_canfd.c   | 2 +-
 drivers/net/can/rcar/rcar_can.c   | 2 +-
 drivers/net/can/rcar/rcar_canfd.c | 2 +-
 drivers/net/can/sja1000/sja1000.c | 2 +-
 drivers/net/can/softing/softing_main.c| 2 +-
 drivers/net/can/spi/hi311x.c  | 2 +-
 drivers/net/can/spi/mcp251x.c | 2 +-
 drivers/net/can/sun4i_can.c   | 2 +-
 drivers/net/can/usb/ems_usb.c | 2 +-
 drivers/net/can/usb/esd_usb2.c| 2 +-
 drivers/net/can/usb/gs_usb.c  | 2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 2 +-
 drivers/net/can/usb/mcba_usb.c| 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_core.c  | 2 +-
 drivers/net/can/usb/ucan.c| 2 +-
 drivers/net/can/usb/usb_8dev.c| 2 +-
 drivers/net/can/xilinx_can.c  | 2 +-
 include/linux/can/skb.h   | 3 ++-
 29 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/drivers/net/can/at91_can.c b/drivers/net/can/at91_can.c
index 90b223a80ed4..9ad9b39f480e 100644
--- a/drivers/net/can/at91_can.c
+++ b/drivers/net/can/at91_can.c
@@ -856,7 +856,7 @@ static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
if (likely(reg_msr & AT91_MSR_MRDY &&
   ~reg_msr & AT91_MSR_MABT)) {
/* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
-   can_get_echo_skb(dev, mb - get_mb_tx_first(priv));
+   can_get_echo_skb(dev, mb - get_mb_tx_first(priv), NULL);
dev->stats.tx_packets++;
can_led_event(dev, CAN_LED_EVENT_TX);
}
diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c
index 13638954a25c..ef474bae47a1 100644
--- a/drivers/net/can/c_can/c_can.c
+++ b/drivers/net/can/c_can/c_can.c
@@ -733,7 +733,7 @@ static void c_can_do_tx(struct net_device *dev)
pend &= ~(1 << idx);
obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
c_can_inval_tx_object(dev, IF_RX, obj);
-   can_get_echo_skb(dev, idx);
+   can_get_echo_skb(dev, idx, NULL);
bytes += priv->dlc[idx];
pkts++;
}
diff --git a/drivers/net/can/cc770/cc770.c b/drivers/net/can/cc770/cc770.c
index e53ca338368a..f8a130f594e2 100644
--- a/drivers/net/can/cc770/cc770.c
+++ b/drivers/net/can/cc770/cc770.c
@@ -703,7 +703,7 @@ static void cc770_tx_interrupt(struct net_device *dev, 
unsigned int o)
stats->tx_packets++;
 
can_put_echo_skb(priv->tx_skb, dev, 0, 0);
-   can_get_echo_skb(dev, 0);
+   can_get_echo_skb(dev, 0, NULL);
priv->tx_skb = NULL;
 
netif_wake_queue(dev);
diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index c184b4dce19e..53683d4312f1 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -121,12 +121,13 @@ __can_get_echo_skb(struct net_device *dev, unsigned int 
idx, u8 *len_ptr,
  * is handled in the device driver. The driver must protect
  * access to priv->echo_skb, if necessary.
  */
-unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
+unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
+ unsigned int *frame_len_ptr)
 {
struct sk_buff *skb;
u8 len;
 
-   skb = __can_get_echo_skb(dev, idx, &len, NULL);
+   skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
if (!skb)
return 0;
 
diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
index 8086cdc1..4a8453290530 100644
--- a/drivers/net/can/grcan.c
+++ b/dr

[net-next 10/17] can: length: canfd_sanitize_len(): add function to sanitize CAN-FD data length

2021-01-13 Thread Marc Kleine-Budde
The data field in CAN-FD frames have specifig frame length (0, 1, 2, 3, 4, 5,
6, 7, 8, 12, 16, 20, 24, 32, 48, 64). This function "rounds" up a given length
to the next valid CAN-FD frame length.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-10-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 include/linux/can/length.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index 156b9d17969a..b2313b2a0b02 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -45,4 +45,10 @@ u8 can_fd_dlc2len(u8 dlc);
 /* map the sanitized data length to an appropriate data length code */
 u8 can_fd_len2dlc(u8 len);
 
+/* map the data length to an appropriate data link layer length */
+static inline u8 canfd_sanitize_len(u8 len)
+{
+   return can_fd_dlc2len(can_fd_len2dlc(len));
+}
+
 #endif /* !_CAN_LENGTH_H */
-- 
2.29.2




[net-next 07/17] can: dev: move netlink related code into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves the netlink related code of the CAN device infrastructure into
a separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-7-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/Makefile  |   1 +
 drivers/net/can/dev/dev.c | 370 +
 drivers/net/can/dev/netlink.c | 379 ++
 include/linux/can/dev.h   |   6 +
 4 files changed, 389 insertions(+), 367 deletions(-)
 create mode 100644 drivers/net/can/dev/netlink.c

diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index 188bd53b113c..3e2e207861fc 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_CAN_DEV)   += can-dev.o
 can-dev-y  += bittiming.o
 can-dev-y  += dev.o
 can-dev-y  += length.o
+can-dev-y  += netlink.o
 can-dev-y  += rx-offload.o
 can-dev-y   += skb.o
 
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index fe71ff9ef8c9..f580f0ac6497 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -14,10 +14,8 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
-#include 
 
 #define MOD_DESC "CAN device driver interface"
 
@@ -223,7 +221,7 @@ void can_bus_off(struct net_device *dev)
 }
 EXPORT_SYMBOL_GPL(can_bus_off);
 
-static void can_setup(struct net_device *dev)
+void can_setup(struct net_device *dev)
 {
dev->type = ARPHRD_CAN;
dev->mtu = CAN_MTU;
@@ -399,368 +397,6 @@ void close_candev(struct net_device *dev)
 }
 EXPORT_SYMBOL_GPL(close_candev);
 
-/* CAN netlink interface */
-static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
-   [IFLA_CAN_STATE]= { .type = NLA_U32 },
-   [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
-   [IFLA_CAN_RESTART_MS]   = { .type = NLA_U32 },
-   [IFLA_CAN_RESTART]  = { .type = NLA_U32 },
-   [IFLA_CAN_BITTIMING]= { .len = sizeof(struct can_bittiming) },
-   [IFLA_CAN_BITTIMING_CONST]
-   = { .len = sizeof(struct can_bittiming_const) },
-   [IFLA_CAN_CLOCK]= { .len = sizeof(struct can_clock) },
-   [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
-   [IFLA_CAN_DATA_BITTIMING]
-   = { .len = sizeof(struct can_bittiming) },
-   [IFLA_CAN_DATA_BITTIMING_CONST]
-   = { .len = sizeof(struct can_bittiming_const) },
-   [IFLA_CAN_TERMINATION]  = { .type = NLA_U16 },
-};
-
-static int can_validate(struct nlattr *tb[], struct nlattr *data[],
-   struct netlink_ext_ack *extack)
-{
-   bool is_can_fd = false;
-
-   /* Make sure that valid CAN FD configurations always consist of
-* - nominal/arbitration bittiming
-* - data bittiming
-* - control mode with CAN_CTRLMODE_FD set
-*/
-
-   if (!data)
-   return 0;
-
-   if (data[IFLA_CAN_CTRLMODE]) {
-   struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
-
-   is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
-   }
-
-   if (is_can_fd) {
-   if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
-   return -EOPNOTSUPP;
-   }
-
-   if (data[IFLA_CAN_DATA_BITTIMING]) {
-   if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
-   return -EOPNOTSUPP;
-   }
-
-   return 0;
-}
-
-static int can_changelink(struct net_device *dev, struct nlattr *tb[],
- struct nlattr *data[],
- struct netlink_ext_ack *extack)
-{
-   struct can_priv *priv = netdev_priv(dev);
-   int err;
-
-   /* We need synchronization with dev->stop() */
-   ASSERT_RTNL();
-
-   if (data[IFLA_CAN_BITTIMING]) {
-   struct can_bittiming bt;
-
-   /* Do not allow changing bittiming while running */
-   if (dev->flags & IFF_UP)
-   return -EBUSY;
-
-   /* Calculate bittiming parameters based on
-* bittiming_const if set, otherwise pass bitrate
-* directly via do_set_bitrate(). Bail out if neither
-* is given.
-*/
-   if (!priv->bittiming_const && !priv->do_set_bittiming)
-   return -EOPNOTSUPP;
-
-   memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
-   err = can_get_bittiming(dev, &bt,
-   priv->bittiming_const,
-   priv->bitrate_const,
-   priv->bitrate_

[net-next 05/17] can: dev: move length related code into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves all CAN frame length related code of the CAN device
infrastructure into a separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-5-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS  |  1 +
 drivers/net/can/dev/Makefile |  1 +
 drivers/net/can/dev/dev.c| 33 -
 drivers/net/can/dev/length.c | 38 
 include/linux/can/dev.h  | 41 +-
 include/linux/can/length.h   | 48 
 6 files changed, 89 insertions(+), 73 deletions(-)
 create mode 100644 drivers/net/can/dev/length.c
 create mode 100644 include/linux/can/length.h

diff --git a/MAINTAINERS b/MAINTAINERS
index d17662df1cd7..21780c4e2e71 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3946,6 +3946,7 @@ F:drivers/net/can/
 F: include/linux/can/bittiming.h
 F: include/linux/can/dev.h
 F: include/linux/can/led.h
+F: include/linux/can/length.h
 F: include/linux/can/platform/
 F: include/linux/can/rx-offload.h
 F: include/uapi/linux/can/error.h
diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index b5c6bb848d9d..5c647951e06d 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -3,6 +3,7 @@
 obj-$(CONFIG_CAN_DEV)  += can-dev.o
 can-dev-y  += bittiming.o
 can-dev-y  += dev.o
+can-dev-y  += length.o
 can-dev-y  += rx-offload.o
 
 can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 1b3ab95b3fd1..3372e99d5db7 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -25,39 +25,6 @@ MODULE_DESCRIPTION(MOD_DESC);
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Wolfgang Grandegger ");
 
-/* CAN DLC to real data length conversion helpers */
-
-static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
-8, 12, 16, 20, 24, 32, 48, 64};
-
-/* get data length from raw data length code (DLC) */
-u8 can_fd_dlc2len(u8 dlc)
-{
-   return dlc2len[dlc & 0x0F];
-}
-EXPORT_SYMBOL_GPL(can_fd_dlc2len);
-
-static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,/* 0 - 
8 */
-9, 9, 9, 9,/* 9 - 12 */
-10, 10, 10, 10,/* 13 - 16 */
-11, 11, 11, 11,/* 17 - 20 */
-12, 12, 12, 12,/* 21 - 24 */
-13, 13, 13, 13, 13, 13, 13, 13,/* 25 - 32 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 33 - 40 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 41 - 48 */
-15, 15, 15, 15, 15, 15, 15, 15,/* 49 - 56 */
-15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
-
-/* map the sanitized data length to an appropriate data length code */
-u8 can_fd_len2dlc(u8 len)
-{
-   if (unlikely(len > 64))
-   return 0xF;
-
-   return len2dlc[len];
-}
-EXPORT_SYMBOL_GPL(can_fd_len2dlc);
-
 static void can_update_state_error_stats(struct net_device *dev,
 enum can_state new_state)
 {
diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
new file mode 100644
index ..6fe18aa23ec9
--- /dev/null
+++ b/drivers/net/can/dev/length.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2012, 2020 Oliver Hartkopp 
+ */
+
+#include 
+
+/* CAN DLC to real data length conversion helpers */
+
+static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
+8, 12, 16, 20, 24, 32, 48, 64};
+
+/* get data length from raw data length code (DLC) */
+u8 can_fd_dlc2len(u8 dlc)
+{
+   return dlc2len[dlc & 0x0F];
+}
+EXPORT_SYMBOL_GPL(can_fd_dlc2len);
+
+static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,/* 0 - 
8 */
+9, 9, 9, 9,/* 9 - 12 */
+10, 10, 10, 10,/* 13 - 16 */
+11, 11, 11, 11,/* 17 - 20 */
+12, 12, 12, 12,/* 21 - 24 */
+13, 13, 13, 13, 13, 13, 13, 13,/* 25 - 32 */
+14, 14, 14, 14, 14, 14, 14, 14,/* 33 - 40 */
+14, 14, 14, 14, 14, 14, 14, 14,/* 41 - 48 */
+15, 15, 15, 15, 15, 15, 15, 15,/* 49 - 56 */
+15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
+
+/* map the sanitized data length to an appropriate data length code */
+u8 can_fd_len2dlc(u8 len)
+{
+   if (unlikely(len &g

[net-next 04/17] can: dev: move bittiming related code into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves the bittiming related code of the CAN device infrastructure
into a separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-4-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS |   1 +
 drivers/net/can/dev/Makefile|   1 +
 drivers/net/can/dev/bittiming.c | 261 
 drivers/net/can/dev/dev.c   | 261 
 include/linux/can/bittiming.h   |  44 ++
 include/linux/can/dev.h |  16 +-
 6 files changed, 308 insertions(+), 276 deletions(-)
 create mode 100644 drivers/net/can/dev/bittiming.c
 create mode 100644 include/linux/can/bittiming.h

diff --git a/MAINTAINERS b/MAINTAINERS
index c3091a91ebbf..d17662df1cd7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3943,6 +3943,7 @@ T:git 
git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git
 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git
 F: Documentation/devicetree/bindings/net/can/
 F: drivers/net/can/
+F: include/linux/can/bittiming.h
 F: include/linux/can/dev.h
 F: include/linux/can/led.h
 F: include/linux/can/platform/
diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index cba92e6bcf6f..b5c6bb848d9d 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_CAN_DEV)  += can-dev.o
+can-dev-y  += bittiming.o
 can-dev-y  += dev.o
 can-dev-y  += rx-offload.o
 
diff --git a/drivers/net/can/dev/bittiming.c b/drivers/net/can/dev/bittiming.c
new file mode 100644
index ..f7fe226bb395
--- /dev/null
+++ b/drivers/net/can/dev/bittiming.c
@@ -0,0 +1,261 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
+ * Copyright (C) 2006 Andrey Volkov, Varma Electronics
+ * Copyright (C) 2008-2009 Wolfgang Grandegger 
+ */
+
+#include 
+
+#ifdef CONFIG_CAN_CALC_BITTIMING
+#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
+
+/* Bit-timing calculation derived from:
+ *
+ * Code based on LinCAN sources and H8S2638 project
+ * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
+ * Copyright 2005  Stanislav Marek
+ * email: p...@cmp.felk.cvut.cz
+ *
+ * Calculates proper bit-timing parameters for a specified bit-rate
+ * and sample-point, which can then be used to set the bit-timing
+ * registers of the CAN controller. You can find more information
+ * in the header file linux/can/netlink.h.
+ */
+static int
+can_update_sample_point(const struct can_bittiming_const *btc,
+   unsigned int sample_point_nominal, unsigned int tseg,
+   unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
+   unsigned int *sample_point_error_ptr)
+{
+   unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
+   unsigned int sample_point, best_sample_point = 0;
+   unsigned int tseg1, tseg2;
+   int i;
+
+   for (i = 0; i <= 1; i++) {
+   tseg2 = tseg + CAN_SYNC_SEG -
+   (sample_point_nominal * (tseg + CAN_SYNC_SEG)) /
+   1000 - i;
+   tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
+   tseg1 = tseg - tseg2;
+   if (tseg1 > btc->tseg1_max) {
+   tseg1 = btc->tseg1_max;
+   tseg2 = tseg - tseg1;
+   }
+
+   sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
+   (tseg + CAN_SYNC_SEG);
+   sample_point_error = abs(sample_point_nominal - sample_point);
+
+   if (sample_point <= sample_point_nominal &&
+   sample_point_error < best_sample_point_error) {
+   best_sample_point = sample_point;
+   best_sample_point_error = sample_point_error;
+   *tseg1_ptr = tseg1;
+   *tseg2_ptr = tseg2;
+   }
+   }
+
+   if (sample_point_error_ptr)
+   *sample_point_error_ptr = best_sample_point_error;
+
+   return best_sample_point;
+}
+
+int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
+  const struct can_bittiming_const *btc)
+{
+   struct can_priv *priv = netdev_priv(dev);
+   unsigned int bitrate;   /* current bitrate */
+   unsigned int bitrate_error; /* difference between current 
and nominal value */
+   unsigned int best_bitrate_error = UINT_MAX;
+   unsigned int sample_point_error;/* difference between current 
and nominal value */
+   unsigned int best_sample_point_error = UINT_MAX;
+   unsigned int sample_point_nominal;  /* nominal sample point */
+ 

[net-next 06/17] can: dev: move skb related into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves the skb related code of the CAN device infrastructure into a
separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-6-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/Makefile |   1 +
 drivers/net/can/dev/dev.c| 213 -
 drivers/net/can/dev/skb.c| 220 +++
 include/linux/can/dev.h  |  76 
 include/linux/can/skb.h  |  77 
 5 files changed, 298 insertions(+), 289 deletions(-)
 create mode 100644 drivers/net/can/dev/skb.c

diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index 5c647951e06d..188bd53b113c 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -5,5 +5,6 @@ can-dev-y   += bittiming.o
 can-dev-y  += dev.o
 can-dev-y  += length.o
 can-dev-y  += rx-offload.o
+can-dev-y   += skb.o
 
 can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 3372e99d5db7..fe71ff9ef8c9 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -132,149 +132,6 @@ void can_change_state(struct net_device *dev, struct 
can_frame *cf,
 }
 EXPORT_SYMBOL_GPL(can_change_state);
 
-/* Local echo of CAN messages
- *
- * CAN network devices *should* support a local echo functionality
- * (see Documentation/networking/can.rst). To test the handling of CAN
- * interfaces that do not support the local echo both driver types are
- * implemented. In the case that the driver does not support the echo
- * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
- * to perform the echo as a fallback solution.
- */
-static void can_flush_echo_skb(struct net_device *dev)
-{
-   struct can_priv *priv = netdev_priv(dev);
-   struct net_device_stats *stats = &dev->stats;
-   int i;
-
-   for (i = 0; i < priv->echo_skb_max; i++) {
-   if (priv->echo_skb[i]) {
-   kfree_skb(priv->echo_skb[i]);
-   priv->echo_skb[i] = NULL;
-   stats->tx_dropped++;
-   stats->tx_aborted_errors++;
-   }
-   }
-}
-
-/* Put the skb on the stack to be looped backed locally lateron
- *
- * The function is typically called in the start_xmit function
- * of the device driver. The driver must protect access to
- * priv->echo_skb, if necessary.
- */
-int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
-unsigned int idx)
-{
-   struct can_priv *priv = netdev_priv(dev);
-
-   BUG_ON(idx >= priv->echo_skb_max);
-
-   /* check flag whether this packet has to be looped back */
-   if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
-   (skb->protocol != htons(ETH_P_CAN) &&
-skb->protocol != htons(ETH_P_CANFD))) {
-   kfree_skb(skb);
-   return 0;
-   }
-
-   if (!priv->echo_skb[idx]) {
-   skb = can_create_echo_skb(skb);
-   if (!skb)
-   return -ENOMEM;
-
-   /* make settings for echo to reduce code in irq context */
-   skb->pkt_type = PACKET_BROADCAST;
-   skb->ip_summed = CHECKSUM_UNNECESSARY;
-   skb->dev = dev;
-
-   /* save this skb for tx interrupt echo handling */
-   priv->echo_skb[idx] = skb;
-   } else {
-   /* locking problem with netif_stop_queue() ?? */
-   netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", 
__func__, idx);
-   kfree_skb(skb);
-   return -EBUSY;
-   }
-
-   return 0;
-}
-EXPORT_SYMBOL_GPL(can_put_echo_skb);
-
-struct sk_buff *
-__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
-{
-   struct can_priv *priv = netdev_priv(dev);
-
-   if (idx >= priv->echo_skb_max) {
-   netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb 
out of bounds (%u/max %u)\n",
-  __func__, idx, priv->echo_skb_max);
-   return NULL;
-   }
-
-   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;
-
-   /* get the real payload length for netdev statistics */
-   if (cf->can_id & CAN_RTR_FLAG)
-   *len_ptr = 0;
-   else
-   *len_ptr = cf->len;
-
-   priv->echo_

[net-next 02/17] MAINTAINERS: CAN network layer: add missing header file can-ml.h

2021-01-13 Thread Marc Kleine-Budde
This patch add the can-ml.h to the list of maintained files of the CAN network
layer.

Fixes: ffd956eef69b ("can: introduce CAN midlayer private and allocate it 
automatically")
Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-2-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 54fcd5fe572d..c3091a91ebbf 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3960,6 +3960,7 @@ W:https://github.com/linux-can
 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git
 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git
 F: Documentation/networking/can.rst
+F: include/linux/can/can-ml.h
 F: include/linux/can/core.h
 F: include/linux/can/skb.h
 F: include/net/netns/can.h
-- 
2.29.2




pull-request: can-next 2021-01-13

2021-01-13 Thread Marc Kleine-Budde
Hello Jakub, hello David,

this is a pull request of 17 patches for net-next/master.

The first two patches update the MAINTAINERS file, Lukas Bulwahn's patch fixes
the files entry for the tcan4x5x driver, which was broken by me in net-next.
A patch by me adds the a missing header file to the CAN Networking Layer.

The next 5 patches are by me and split the the CAN driver related
infrastructure code into more files in a separate subdir. The next two patches
by me clean up the CAN length related code. This is followed by 6 patches by
Vincent Mailhol and me, they add helper code for for CAN frame length
calculation neede for BQL support.

A patch by Vincent Mailhol adds software TX timestamp support.

The last patch is by me, targets the tcan4x5x driver, and removes the unneeded
__packed attribute from the struct tcan4x5x_map_buf.

regards,
Marc

---

The following changes since commit f50e2f9f791647aa4e5b19d0064f5cabf630bf6e:

  hci: llc_shdlc: style: Simplify bool comparison (2021-01-12 20:18:30 -0800)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git 
tags/linux-can-next-for-5.12-20210113

for you to fetch changes up to b42e45a913f2ae76807e656a1c053808d7d34352:

  can: tcan4x5x: remove __packed attribute from struct tcan4x5x_map_buf 
(2021-01-13 21:48:28 +0100)


linux-can-next-for-5.12-20210113


Lukas Bulwahn (1):
  MAINTAINERS: adjust entry to tcan4x5x file split

Marc Kleine-Budde (13):
  MAINTAINERS: CAN network layer: add missing header file can-ml.h
  can: dev: move driver related infrastructure into separate subdir
  can: dev: move bittiming related code into seperate file
  can: dev: move length related code into seperate file
  can: dev: move skb related into seperate file
  can: dev: move netlink related code into seperate file
  can: length: convert to kernel coding style
  can: length: can_fd_len2dlc(): simplify length calculcation
  can: length: canfd_sanitize_len(): add function to sanitize CAN-FD data 
length
  can: dev: extend struct can_skb_priv to hold CAN frame length
  can: dev: can_get_echo_skb(): extend to return can frame length
  can: dev: can_rx_offload_get_echo_skb(): extend to return can frame length
  can: tcan4x5x: remove __packed attribute from struct tcan4x5x_map_buf

Vincent Mailhol (3):
  can: length: can_skb_get_frame_len(): introduce function to get data 
length of frame in data link layer
  can: dev: can_put_echo_skb(): extend to handle frame_len
  can: dev: can_put_echo_skb(): add software tx timestamps

 MAINTAINERS   |5 +-
 drivers/net/can/Makefile  |7 +-
 drivers/net/can/at91_can.c|4 +-
 drivers/net/can/c_can/c_can.c |4 +-
 drivers/net/can/cc770/cc770.c |4 +-
 drivers/net/can/dev.c | 1338 -
 drivers/net/can/dev/Makefile  |   11 +
 drivers/net/can/dev/bittiming.c   |  261 
 drivers/net/can/dev/dev.c |  467 +++
 drivers/net/can/dev/length.c  |   90 ++
 drivers/net/can/dev/netlink.c |  379 ++
 drivers/net/can/{ => dev}/rx-offload.c|5 +-
 drivers/net/can/dev/skb.c |  231 
 drivers/net/can/flexcan.c |7 +-
 drivers/net/can/grcan.c   |4 +-
 drivers/net/can/ifi_canfd/ifi_canfd.c |4 +-
 drivers/net/can/kvaser_pciefd.c   |6 +-
 drivers/net/can/m_can/m_can.c |8 +-
 drivers/net/can/m_can/tcan4x5x.h  |2 +-
 drivers/net/can/mscan/mscan.c |4 +-
 drivers/net/can/pch_can.c |4 +-
 drivers/net/can/peak_canfd/peak_canfd.c   |4 +-
 drivers/net/can/rcar/rcar_can.c   |4 +-
 drivers/net/can/rcar/rcar_canfd.c |4 +-
 drivers/net/can/sja1000/sja1000.c |4 +-
 drivers/net/can/softing/softing_main.c|4 +-
 drivers/net/can/spi/hi311x.c  |4 +-
 drivers/net/can/spi/mcp251x.c |4 +-
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c|4 +-
 drivers/net/can/sun4i_can.c   |4 +-
 drivers/net/can/ti_hecc.c |4 +-
 drivers/net/can/usb/ems_usb.c |4 +-
 drivers/net/can/usb/esd_usb2.c|4 +-
 drivers/net/can/usb/gs_usb.c  |4 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c  |2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c |2 +-
 drivers/net

[net-next 01/17] MAINTAINERS: adjust entry to tcan4x5x file split

2021-01-13 Thread Marc Kleine-Budde
From: Lukas Bulwahn 

Commit 7813887ea972 ("can: tcan4x5x: rename tcan4x5x.c -> tcan4x5x-core.c") and
commit 67def4ef8bb9 ("can: tcan4x5x: move regmap code into seperate file")
split the file tcan4x5x.c into two files, but missed to adjust the TI TCAN4X5X
DEVICE DRIVER section in MAINTAINERS.

Hence, ./scripts/get_maintainer.pl --self-test=patterns complains:

  warning: no file matchesF:drivers/net/can/m_can/tcan4x5x.c

Adjust the file entry in MAINTAINERS to the tcan4x5x file splitting.

Signed-off-by: Lukas Bulwahn 
Fixes: 67def4ef8bb9 ("can: tcan4x5x: move regmap code into seperate file")
Fixes: 7813887ea972 ("can: tcan4x5x: rename tcan4x5x.c -> tcan4x5x-core.c")
Link: https://lore.kernel.org/r/20210108073932.20804-1-lukas.bulw...@gmail.com
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index c2cb79198288..54fcd5fe572d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17839,7 +17839,7 @@ M:  Dan Murphy 
 L: linux-...@vger.kernel.org
 S: Maintained
 F: Documentation/devicetree/bindings/net/can/tcan4x5x.txt
-F: drivers/net/can/m_can/tcan4x5x.c
+F: drivers/net/can/m_can/tcan4x5x*
 
 TI TRF7970A NFC DRIVER
 M: Mark Greer 

base-commit: f50e2f9f791647aa4e5b19d0064f5cabf630bf6e
-- 
2.29.2




Re: pull-request: can-next 2021-01-13

2021-01-13 Thread Marc Kleine-Budde
On 1/13/21 10:13 PM, Marc Kleine-Budde wrote:
> Hello Jakub, hello David,
> 
> this is a pull request of 17 patches for net-next/master.
> 
> The first two patches update the MAINTAINERS file, Lukas Bulwahn's patch fixes
> the files entry for the tcan4x5x driver, which was broken by me in net-next.
> A patch by me adds the a missing header file to the CAN Networking Layer.
> 
> The next 5 patches are by me and split the the CAN driver related
> infrastructure code into more files in a separate subdir. The next two patches
> by me clean up the CAN length related code. This is followed by 6 patches by
> Vincent Mailhol and me, they add helper code for for CAN frame length
> calculation neede for BQL support.
> 
> A patch by Vincent Mailhol adds software TX timestamp support.
> 
> The last patch is by me, targets the tcan4x5x driver, and removes the unneeded
> __packed attribute from the struct tcan4x5x_map_buf.
> 
> regards,
> Marc
> 
> ---
> 
> The following changes since commit f50e2f9f791647aa4e5b19d0064f5cabf630bf6e:
> 
>   hci: llc_shdlc: style: Simplify bool comparison (2021-01-12 20:18:30 -0800)
> 
> are available in the Git repository at:
> 
>   git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git 
> tags/linux-can-next-for-5.12-20210113

Please don't pull. This request contains a bug. I've deleted the tag and send a
fixed pull request soon.

sorry for the noise,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [net-next 09/17] can: length: can_fd_len2dlc(): simplify length calculcation

2021-01-13 Thread Marc Kleine-Budde
On 1/14/21 2:59 AM, Vincent MAILHOL wrote:
> On Tue. 14 Jan 2021 at 06:14, Marc Kleine-Budde  wrote:
>>
>> If the length paramter in len2dlc() exceeds the size of the len2dlc array, we
>> return 0xF. This is equal to the last 16 members of the array.
>>
>> This patch removes these members from the array, uses ARRAY_SIZE() for the
>> length check, and returns CANFD_MAX_DLC (which is 0xf).
>>
>> Reviewed-by: Vincent Mailhol 
>> Link: https://lore.kernel.org/r/20210111141930.693847-9-...@pengutronix.de
>> Signed-off-by: Marc Kleine-Budde 
>> ---
>>  drivers/net/can/dev/length.c | 6 ++
>>  1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
>> index 5e7d481717ea..d695a3bee1ed 100644
>> --- a/drivers/net/can/dev/length.c
>> +++ b/drivers/net/can/dev/length.c
>> @@ -27,15 +27,13 @@ static const u8 len2dlc[] = {
>> 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
>> 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
>> 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
>> -   15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
>> -   15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
>>  };
>>
>>  /* map the sanitized data length to an appropriate data length code */
>>  u8 can_fd_len2dlc(u8 len)
>>  {
>> -   if (unlikely(len > 64))
>> -   return 0xF;
>> +   if (len > ARRAY_SIZE(len2dlc))
> 
> Sorry but I missed an of-by-one issue when I did my first
> review. Don't know why but it popped to my eyes this morning when
> casually reading the emails.
> 
> ARRAY_SIZE(len2dlc) is 49. If len is between 0 and 48, use the
> array, if len is greater *or equal* return CANFD_MAX_DLC.

Doh!

Looking for his brown paper bag,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


pull-request: can-next 2021-01-14

2021-01-13 Thread Marc Kleine-Budde
Hello Jakub, hello David,

this is the corrected version of yesterday's pull request, it consists of 17
patches for net-next/master.

The first two patches update the MAINTAINERS file, Lukas Bulwahn's patch fixes
the files entry for the tcan4x5x driver, which was broken by me in net-next.
A patch by me adds the a missing header file to the CAN Networking Layer.

The next 5 patches are by me and split the the CAN driver related
infrastructure code into more files in a separate subdir. The next two patches
by me clean up the CAN length related code. This is followed by 6 patches by
Vincent Mailhol and me, they add helper code for for CAN frame length
calculation neede for BQL support.

A patch by Vincent Mailhol adds software TX timestamp support.

The last patch is by me, targets the tcan4x5x driver, and removes the unneeded
__packed attribute from the struct tcan4x5x_map_buf.

regards,
Marc

---

The following changes since commit f50e2f9f791647aa4e5b19d0064f5cabf630bf6e:

  hci: llc_shdlc: style: Simplify bool comparison (2021-01-12 20:18:30 -0800)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git 
tags/linux-can-next-for-5.12-20210114

for you to fetch changes up to 1105592cb8fdfcc96f2c9c693ff4106bac5fac7c:

  can: tcan4x5x: remove __packed attribute from struct tcan4x5x_map_buf 
(2021-01-14 08:43:44 +0100)


linux-can-next-for-5.12-20210114


Lukas Bulwahn (1):
  MAINTAINERS: adjust entry to tcan4x5x file split

Marc Kleine-Budde (13):
  MAINTAINERS: CAN network layer: add missing header file can-ml.h
  can: dev: move driver related infrastructure into separate subdir
  can: dev: move bittiming related code into seperate file
  can: dev: move length related code into seperate file
  can: dev: move skb related into seperate file
  can: dev: move netlink related code into seperate file
  can: length: convert to kernel coding style
  can: length: can_fd_len2dlc(): simplify length calculcation
  can: length: canfd_sanitize_len(): add function to sanitize CAN-FD data 
length
  can: dev: extend struct can_skb_priv to hold CAN frame length
  can: dev: can_get_echo_skb(): extend to return can frame length
  can: dev: can_rx_offload_get_echo_skb(): extend to return can frame length
  can: tcan4x5x: remove __packed attribute from struct tcan4x5x_map_buf

Vincent Mailhol (3):
  can: length: can_skb_get_frame_len(): introduce function to get data 
length of frame in data link layer
  can: dev: can_put_echo_skb(): extend to handle frame_len
  can: dev: can_put_echo_skb(): add software tx timestamps

 MAINTAINERS   |5 +-
 drivers/net/can/Makefile  |7 +-
 drivers/net/can/at91_can.c|4 +-
 drivers/net/can/c_can/c_can.c |4 +-
 drivers/net/can/cc770/cc770.c |4 +-
 drivers/net/can/dev.c | 1338 -
 drivers/net/can/dev/Makefile  |   11 +
 drivers/net/can/dev/bittiming.c   |  261 
 drivers/net/can/dev/dev.c |  467 +++
 drivers/net/can/dev/length.c  |   90 ++
 drivers/net/can/dev/netlink.c |  379 ++
 drivers/net/can/{ => dev}/rx-offload.c|5 +-
 drivers/net/can/dev/skb.c |  231 
 drivers/net/can/flexcan.c |7 +-
 drivers/net/can/grcan.c   |4 +-
 drivers/net/can/ifi_canfd/ifi_canfd.c |4 +-
 drivers/net/can/kvaser_pciefd.c   |6 +-
 drivers/net/can/m_can/m_can.c |8 +-
 drivers/net/can/m_can/tcan4x5x.h  |2 +-
 drivers/net/can/mscan/mscan.c |4 +-
 drivers/net/can/pch_can.c |4 +-
 drivers/net/can/peak_canfd/peak_canfd.c   |4 +-
 drivers/net/can/rcar/rcar_can.c   |4 +-
 drivers/net/can/rcar/rcar_canfd.c |4 +-
 drivers/net/can/sja1000/sja1000.c |4 +-
 drivers/net/can/softing/softing_main.c|4 +-
 drivers/net/can/spi/hi311x.c  |4 +-
 drivers/net/can/spi/mcp251x.c |4 +-
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c|4 +-
 drivers/net/can/sun4i_can.c   |4 +-
 drivers/net/can/ti_hecc.c |4 +-
 drivers/net/can/usb/ems_usb.c |4 +-
 drivers/net/can/usb/esd_usb2.c|4 +-
 drivers/net/can/usb/gs_usb.c  |4 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c  |2 +-
 drivers/net/can/u

[net-next 05/17] can: dev: move length related code into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves all CAN frame length related code of the CAN device
infrastructure into a separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-5-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS  |  1 +
 drivers/net/can/dev/Makefile |  1 +
 drivers/net/can/dev/dev.c| 33 -
 drivers/net/can/dev/length.c | 38 
 include/linux/can/dev.h  | 41 +-
 include/linux/can/length.h   | 48 
 6 files changed, 89 insertions(+), 73 deletions(-)
 create mode 100644 drivers/net/can/dev/length.c
 create mode 100644 include/linux/can/length.h

diff --git a/MAINTAINERS b/MAINTAINERS
index d17662df1cd7..21780c4e2e71 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3946,6 +3946,7 @@ F:drivers/net/can/
 F: include/linux/can/bittiming.h
 F: include/linux/can/dev.h
 F: include/linux/can/led.h
+F: include/linux/can/length.h
 F: include/linux/can/platform/
 F: include/linux/can/rx-offload.h
 F: include/uapi/linux/can/error.h
diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index b5c6bb848d9d..5c647951e06d 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -3,6 +3,7 @@
 obj-$(CONFIG_CAN_DEV)  += can-dev.o
 can-dev-y  += bittiming.o
 can-dev-y  += dev.o
+can-dev-y  += length.o
 can-dev-y  += rx-offload.o
 
 can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 1b3ab95b3fd1..3372e99d5db7 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -25,39 +25,6 @@ MODULE_DESCRIPTION(MOD_DESC);
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Wolfgang Grandegger ");
 
-/* CAN DLC to real data length conversion helpers */
-
-static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
-8, 12, 16, 20, 24, 32, 48, 64};
-
-/* get data length from raw data length code (DLC) */
-u8 can_fd_dlc2len(u8 dlc)
-{
-   return dlc2len[dlc & 0x0F];
-}
-EXPORT_SYMBOL_GPL(can_fd_dlc2len);
-
-static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,/* 0 - 
8 */
-9, 9, 9, 9,/* 9 - 12 */
-10, 10, 10, 10,/* 13 - 16 */
-11, 11, 11, 11,/* 17 - 20 */
-12, 12, 12, 12,/* 21 - 24 */
-13, 13, 13, 13, 13, 13, 13, 13,/* 25 - 32 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 33 - 40 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 41 - 48 */
-15, 15, 15, 15, 15, 15, 15, 15,/* 49 - 56 */
-15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
-
-/* map the sanitized data length to an appropriate data length code */
-u8 can_fd_len2dlc(u8 len)
-{
-   if (unlikely(len > 64))
-   return 0xF;
-
-   return len2dlc[len];
-}
-EXPORT_SYMBOL_GPL(can_fd_len2dlc);
-
 static void can_update_state_error_stats(struct net_device *dev,
 enum can_state new_state)
 {
diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
new file mode 100644
index ..6fe18aa23ec9
--- /dev/null
+++ b/drivers/net/can/dev/length.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2012, 2020 Oliver Hartkopp 
+ */
+
+#include 
+
+/* CAN DLC to real data length conversion helpers */
+
+static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
+8, 12, 16, 20, 24, 32, 48, 64};
+
+/* get data length from raw data length code (DLC) */
+u8 can_fd_dlc2len(u8 dlc)
+{
+   return dlc2len[dlc & 0x0F];
+}
+EXPORT_SYMBOL_GPL(can_fd_dlc2len);
+
+static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,/* 0 - 
8 */
+9, 9, 9, 9,/* 9 - 12 */
+10, 10, 10, 10,/* 13 - 16 */
+11, 11, 11, 11,/* 17 - 20 */
+12, 12, 12, 12,/* 21 - 24 */
+13, 13, 13, 13, 13, 13, 13, 13,/* 25 - 32 */
+14, 14, 14, 14, 14, 14, 14, 14,/* 33 - 40 */
+14, 14, 14, 14, 14, 14, 14, 14,/* 41 - 48 */
+15, 15, 15, 15, 15, 15, 15, 15,/* 49 - 56 */
+15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
+
+/* map the sanitized data length to an appropriate data length code */
+u8 can_fd_len2dlc(u8 len)
+{
+   if (unlikely(len &g

[net-next 01/17] MAINTAINERS: adjust entry to tcan4x5x file split

2021-01-13 Thread Marc Kleine-Budde
From: Lukas Bulwahn 

Commit 7813887ea972 ("can: tcan4x5x: rename tcan4x5x.c -> tcan4x5x-core.c") and
commit 67def4ef8bb9 ("can: tcan4x5x: move regmap code into seperate file")
split the file tcan4x5x.c into two files, but missed to adjust the TI TCAN4X5X
DEVICE DRIVER section in MAINTAINERS.

Hence, ./scripts/get_maintainer.pl --self-test=patterns complains:

  warning: no file matchesF:drivers/net/can/m_can/tcan4x5x.c

Adjust the file entry in MAINTAINERS to the tcan4x5x file splitting.

Signed-off-by: Lukas Bulwahn 
Fixes: 67def4ef8bb9 ("can: tcan4x5x: move regmap code into seperate file")
Fixes: 7813887ea972 ("can: tcan4x5x: rename tcan4x5x.c -> tcan4x5x-core.c")
Link: https://lore.kernel.org/r/20210108073932.20804-1-lukas.bulw...@gmail.com
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index c2cb79198288..54fcd5fe572d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17839,7 +17839,7 @@ M:  Dan Murphy 
 L: linux-...@vger.kernel.org
 S: Maintained
 F: Documentation/devicetree/bindings/net/can/tcan4x5x.txt
-F: drivers/net/can/m_can/tcan4x5x.c
+F: drivers/net/can/m_can/tcan4x5x*
 
 TI TRF7970A NFC DRIVER
 M: Mark Greer 

base-commit: f50e2f9f791647aa4e5b19d0064f5cabf630bf6e
-- 
2.29.2




[net-next 07/17] can: dev: move netlink related code into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves the netlink related code of the CAN device infrastructure into
a separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-7-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/Makefile  |   1 +
 drivers/net/can/dev/dev.c | 370 +
 drivers/net/can/dev/netlink.c | 379 ++
 include/linux/can/dev.h   |   6 +
 4 files changed, 389 insertions(+), 367 deletions(-)
 create mode 100644 drivers/net/can/dev/netlink.c

diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index 188bd53b113c..3e2e207861fc 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_CAN_DEV)   += can-dev.o
 can-dev-y  += bittiming.o
 can-dev-y  += dev.o
 can-dev-y  += length.o
+can-dev-y  += netlink.o
 can-dev-y  += rx-offload.o
 can-dev-y   += skb.o
 
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index fe71ff9ef8c9..f580f0ac6497 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -14,10 +14,8 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
-#include 
 
 #define MOD_DESC "CAN device driver interface"
 
@@ -223,7 +221,7 @@ void can_bus_off(struct net_device *dev)
 }
 EXPORT_SYMBOL_GPL(can_bus_off);
 
-static void can_setup(struct net_device *dev)
+void can_setup(struct net_device *dev)
 {
dev->type = ARPHRD_CAN;
dev->mtu = CAN_MTU;
@@ -399,368 +397,6 @@ void close_candev(struct net_device *dev)
 }
 EXPORT_SYMBOL_GPL(close_candev);
 
-/* CAN netlink interface */
-static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
-   [IFLA_CAN_STATE]= { .type = NLA_U32 },
-   [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
-   [IFLA_CAN_RESTART_MS]   = { .type = NLA_U32 },
-   [IFLA_CAN_RESTART]  = { .type = NLA_U32 },
-   [IFLA_CAN_BITTIMING]= { .len = sizeof(struct can_bittiming) },
-   [IFLA_CAN_BITTIMING_CONST]
-   = { .len = sizeof(struct can_bittiming_const) },
-   [IFLA_CAN_CLOCK]= { .len = sizeof(struct can_clock) },
-   [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
-   [IFLA_CAN_DATA_BITTIMING]
-   = { .len = sizeof(struct can_bittiming) },
-   [IFLA_CAN_DATA_BITTIMING_CONST]
-   = { .len = sizeof(struct can_bittiming_const) },
-   [IFLA_CAN_TERMINATION]  = { .type = NLA_U16 },
-};
-
-static int can_validate(struct nlattr *tb[], struct nlattr *data[],
-   struct netlink_ext_ack *extack)
-{
-   bool is_can_fd = false;
-
-   /* Make sure that valid CAN FD configurations always consist of
-* - nominal/arbitration bittiming
-* - data bittiming
-* - control mode with CAN_CTRLMODE_FD set
-*/
-
-   if (!data)
-   return 0;
-
-   if (data[IFLA_CAN_CTRLMODE]) {
-   struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
-
-   is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
-   }
-
-   if (is_can_fd) {
-   if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
-   return -EOPNOTSUPP;
-   }
-
-   if (data[IFLA_CAN_DATA_BITTIMING]) {
-   if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
-   return -EOPNOTSUPP;
-   }
-
-   return 0;
-}
-
-static int can_changelink(struct net_device *dev, struct nlattr *tb[],
- struct nlattr *data[],
- struct netlink_ext_ack *extack)
-{
-   struct can_priv *priv = netdev_priv(dev);
-   int err;
-
-   /* We need synchronization with dev->stop() */
-   ASSERT_RTNL();
-
-   if (data[IFLA_CAN_BITTIMING]) {
-   struct can_bittiming bt;
-
-   /* Do not allow changing bittiming while running */
-   if (dev->flags & IFF_UP)
-   return -EBUSY;
-
-   /* Calculate bittiming parameters based on
-* bittiming_const if set, otherwise pass bitrate
-* directly via do_set_bitrate(). Bail out if neither
-* is given.
-*/
-   if (!priv->bittiming_const && !priv->do_set_bittiming)
-   return -EOPNOTSUPP;
-
-   memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
-   err = can_get_bittiming(dev, &bt,
-   priv->bittiming_const,
-   priv->bitrate_const,
-   priv->bitrate_

[net-next 06/17] can: dev: move skb related into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves the skb related code of the CAN device infrastructure into a
separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-6-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/Makefile |   1 +
 drivers/net/can/dev/dev.c| 213 -
 drivers/net/can/dev/skb.c| 220 +++
 include/linux/can/dev.h  |  76 
 include/linux/can/skb.h  |  77 
 5 files changed, 298 insertions(+), 289 deletions(-)
 create mode 100644 drivers/net/can/dev/skb.c

diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index 5c647951e06d..188bd53b113c 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -5,5 +5,6 @@ can-dev-y   += bittiming.o
 can-dev-y  += dev.o
 can-dev-y  += length.o
 can-dev-y  += rx-offload.o
+can-dev-y   += skb.o
 
 can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 3372e99d5db7..fe71ff9ef8c9 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -132,149 +132,6 @@ void can_change_state(struct net_device *dev, struct 
can_frame *cf,
 }
 EXPORT_SYMBOL_GPL(can_change_state);
 
-/* Local echo of CAN messages
- *
- * CAN network devices *should* support a local echo functionality
- * (see Documentation/networking/can.rst). To test the handling of CAN
- * interfaces that do not support the local echo both driver types are
- * implemented. In the case that the driver does not support the echo
- * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
- * to perform the echo as a fallback solution.
- */
-static void can_flush_echo_skb(struct net_device *dev)
-{
-   struct can_priv *priv = netdev_priv(dev);
-   struct net_device_stats *stats = &dev->stats;
-   int i;
-
-   for (i = 0; i < priv->echo_skb_max; i++) {
-   if (priv->echo_skb[i]) {
-   kfree_skb(priv->echo_skb[i]);
-   priv->echo_skb[i] = NULL;
-   stats->tx_dropped++;
-   stats->tx_aborted_errors++;
-   }
-   }
-}
-
-/* Put the skb on the stack to be looped backed locally lateron
- *
- * The function is typically called in the start_xmit function
- * of the device driver. The driver must protect access to
- * priv->echo_skb, if necessary.
- */
-int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
-unsigned int idx)
-{
-   struct can_priv *priv = netdev_priv(dev);
-
-   BUG_ON(idx >= priv->echo_skb_max);
-
-   /* check flag whether this packet has to be looped back */
-   if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
-   (skb->protocol != htons(ETH_P_CAN) &&
-skb->protocol != htons(ETH_P_CANFD))) {
-   kfree_skb(skb);
-   return 0;
-   }
-
-   if (!priv->echo_skb[idx]) {
-   skb = can_create_echo_skb(skb);
-   if (!skb)
-   return -ENOMEM;
-
-   /* make settings for echo to reduce code in irq context */
-   skb->pkt_type = PACKET_BROADCAST;
-   skb->ip_summed = CHECKSUM_UNNECESSARY;
-   skb->dev = dev;
-
-   /* save this skb for tx interrupt echo handling */
-   priv->echo_skb[idx] = skb;
-   } else {
-   /* locking problem with netif_stop_queue() ?? */
-   netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", 
__func__, idx);
-   kfree_skb(skb);
-   return -EBUSY;
-   }
-
-   return 0;
-}
-EXPORT_SYMBOL_GPL(can_put_echo_skb);
-
-struct sk_buff *
-__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
-{
-   struct can_priv *priv = netdev_priv(dev);
-
-   if (idx >= priv->echo_skb_max) {
-   netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb 
out of bounds (%u/max %u)\n",
-  __func__, idx, priv->echo_skb_max);
-   return NULL;
-   }
-
-   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;
-
-   /* get the real payload length for netdev statistics */
-   if (cf->can_id & CAN_RTR_FLAG)
-   *len_ptr = 0;
-   else
-   *len_ptr = cf->len;
-
-   priv->echo_

[net-next 16/17] can: dev: can_put_echo_skb(): add software tx timestamps

2021-01-13 Thread Marc Kleine-Budde
From: Vincent Mailhol 

Call skb_tx_timestamp() within can_put_echo_skb() so that a software tx
timestamp gets attached to the skb.

There two main reasons to include this call in can_put_echo_skb():

  * It easily allow to enable the tx timestamp on all devices with
just one small change.

  * According to Documentation/networking/timestamping.rst, the tx
timestamps should be generated in the device driver as close as possible,
but always prior to passing the packet to the network interface. During the
call to can_put_echo_skb(), the skb gets cloned meaning that the driver
should not dereference the skb variable anymore after can_put_echo_skb()
returns. This makes can_put_echo_skb() the very last place we can use the
skb without having to access the echo_skb[] array.

Remark: by default, skb_tx_timestamp() does nothing. It needs to be activated
by passing the SOF_TIMESTAMPING_TX_SOFTWARE flag either through socket options
or control messages.

References:

 * Support for the error queue in CAN RAW sockets (which is needed for
   tx timestamps) was introduced in:
   https://git.kernel.org//torvalds/c/eb88531bdbfaafb827192d1fc6c5a3fcc4fadd96

  * Put the call to skb_tx_timestamp() just before adding it to the
array:

https://lore.kernel.org/r/043c3ea1-6bdd-59c0-0269-27b2b5b36...@victronenergy.com

  * About Tx hardware timestamps
https://lore.kernel.org/r/2021071152.gb11...@hoboy.vegasvil.org

Signed-off-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/20210112095437.6488-2-mailhol.vinc...@wanadoo.fr
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/skb.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index 53683d4312f1..6a64fe410987 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -65,6 +65,8 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device 
*dev,
/* save frame_len to reuse it when transmission is completed */
can_skb_prv(skb)->frame_len = frame_len;
 
+   skb_tx_timestamp(skb);
+
/* save this skb for tx interrupt echo handling */
priv->echo_skb[idx] = skb;
} else {
-- 
2.29.2




[net-next 17/17] can: tcan4x5x: remove __packed attribute from struct tcan4x5x_map_buf

2021-01-13 Thread Marc Kleine-Budde
The first member of struct tcan4x5x_map_buf is the struct tcan4x5x_buf_cmd,
which has a size of 4 bytes. It's followed by an array of u8. The compiler
places the array directly after the struct tcan4x5x_buf_cmd.

This patch removes the not needed attribute __packed from the struct
tcan4x5x_map_buf.

Suggested-by: Jakub Kicinski 
Link: https://lore.kernel.org/r/20210113203955.912916-1-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/m_can/tcan4x5x.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/m_can/tcan4x5x.h b/drivers/net/can/m_can/tcan4x5x.h
index 7bf264f8e81f..c66da829b795 100644
--- a/drivers/net/can/m_can/tcan4x5x.h
+++ b/drivers/net/can/m_can/tcan4x5x.h
@@ -25,7 +25,7 @@ struct __packed tcan4x5x_buf_cmd {
u8 len;
 };
 
-struct __packed tcan4x5x_map_buf {
+struct tcan4x5x_map_buf {
struct tcan4x5x_buf_cmd cmd;
u8 data[256 * sizeof(u32)];
 } cacheline_aligned;
-- 
2.29.2




[net-next 02/17] MAINTAINERS: CAN network layer: add missing header file can-ml.h

2021-01-13 Thread Marc Kleine-Budde
This patch add the can-ml.h to the list of maintained files of the CAN network
layer.

Fixes: ffd956eef69b ("can: introduce CAN midlayer private and allocate it 
automatically")
Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-2-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 54fcd5fe572d..c3091a91ebbf 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3960,6 +3960,7 @@ W:https://github.com/linux-can
 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git
 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git
 F: Documentation/networking/can.rst
+F: include/linux/can/can-ml.h
 F: include/linux/can/core.h
 F: include/linux/can/skb.h
 F: include/net/netns/can.h
-- 
2.29.2




[net-next 03/17] can: dev: move driver related infrastructure into separate subdir

2021-01-13 Thread Marc Kleine-Budde
This patch moves the CAN driver related infrastructure into a separate subdir.
It will be split into more files in the coming patches.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-3-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/Makefile   | 7 +--
 drivers/net/can/dev/Makefile   | 7 +++
 drivers/net/can/{ => dev}/dev.c| 0
 drivers/net/can/{ => dev}/rx-offload.c | 0
 4 files changed, 8 insertions(+), 6 deletions(-)
 create mode 100644 drivers/net/can/dev/Makefile
 rename drivers/net/can/{ => dev}/dev.c (100%)
 rename drivers/net/can/{ => dev}/rx-offload.c (100%)

diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile
index 22164300122d..a2b4463d8480 100644
--- a/drivers/net/can/Makefile
+++ b/drivers/net/can/Makefile
@@ -7,12 +7,7 @@ obj-$(CONFIG_CAN_VCAN) += vcan.o
 obj-$(CONFIG_CAN_VXCAN)+= vxcan.o
 obj-$(CONFIG_CAN_SLCAN)+= slcan.o
 
-obj-$(CONFIG_CAN_DEV)  += can-dev.o
-can-dev-y  += dev.o
-can-dev-y  += rx-offload.o
-
-can-dev-$(CONFIG_CAN_LEDS) += led.o
-
+obj-y  += dev/
 obj-y  += rcar/
 obj-y  += spi/
 obj-y  += usb/
diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
new file mode 100644
index ..cba92e6bcf6f
--- /dev/null
+++ b/drivers/net/can/dev/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_CAN_DEV)  += can-dev.o
+can-dev-y  += dev.o
+can-dev-y  += rx-offload.o
+
+can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev/dev.c
similarity index 100%
rename from drivers/net/can/dev.c
rename to drivers/net/can/dev/dev.c
diff --git a/drivers/net/can/rx-offload.c b/drivers/net/can/dev/rx-offload.c
similarity index 100%
rename from drivers/net/can/rx-offload.c
rename to drivers/net/can/dev/rx-offload.c
-- 
2.29.2




[net-next 04/17] can: dev: move bittiming related code into seperate file

2021-01-13 Thread Marc Kleine-Budde
This patch moves the bittiming related code of the CAN device infrastructure
into a separate file.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-4-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 MAINTAINERS |   1 +
 drivers/net/can/dev/Makefile|   1 +
 drivers/net/can/dev/bittiming.c | 261 
 drivers/net/can/dev/dev.c   | 261 
 include/linux/can/bittiming.h   |  44 ++
 include/linux/can/dev.h |  16 +-
 6 files changed, 308 insertions(+), 276 deletions(-)
 create mode 100644 drivers/net/can/dev/bittiming.c
 create mode 100644 include/linux/can/bittiming.h

diff --git a/MAINTAINERS b/MAINTAINERS
index c3091a91ebbf..d17662df1cd7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3943,6 +3943,7 @@ T:git 
git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git
 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git
 F: Documentation/devicetree/bindings/net/can/
 F: drivers/net/can/
+F: include/linux/can/bittiming.h
 F: include/linux/can/dev.h
 F: include/linux/can/led.h
 F: include/linux/can/platform/
diff --git a/drivers/net/can/dev/Makefile b/drivers/net/can/dev/Makefile
index cba92e6bcf6f..b5c6bb848d9d 100644
--- a/drivers/net/can/dev/Makefile
+++ b/drivers/net/can/dev/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_CAN_DEV)  += can-dev.o
+can-dev-y  += bittiming.o
 can-dev-y  += dev.o
 can-dev-y  += rx-offload.o
 
diff --git a/drivers/net/can/dev/bittiming.c b/drivers/net/can/dev/bittiming.c
new file mode 100644
index ..f7fe226bb395
--- /dev/null
+++ b/drivers/net/can/dev/bittiming.c
@@ -0,0 +1,261 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
+ * Copyright (C) 2006 Andrey Volkov, Varma Electronics
+ * Copyright (C) 2008-2009 Wolfgang Grandegger 
+ */
+
+#include 
+
+#ifdef CONFIG_CAN_CALC_BITTIMING
+#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
+
+/* Bit-timing calculation derived from:
+ *
+ * Code based on LinCAN sources and H8S2638 project
+ * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
+ * Copyright 2005  Stanislav Marek
+ * email: p...@cmp.felk.cvut.cz
+ *
+ * Calculates proper bit-timing parameters for a specified bit-rate
+ * and sample-point, which can then be used to set the bit-timing
+ * registers of the CAN controller. You can find more information
+ * in the header file linux/can/netlink.h.
+ */
+static int
+can_update_sample_point(const struct can_bittiming_const *btc,
+   unsigned int sample_point_nominal, unsigned int tseg,
+   unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
+   unsigned int *sample_point_error_ptr)
+{
+   unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
+   unsigned int sample_point, best_sample_point = 0;
+   unsigned int tseg1, tseg2;
+   int i;
+
+   for (i = 0; i <= 1; i++) {
+   tseg2 = tseg + CAN_SYNC_SEG -
+   (sample_point_nominal * (tseg + CAN_SYNC_SEG)) /
+   1000 - i;
+   tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
+   tseg1 = tseg - tseg2;
+   if (tseg1 > btc->tseg1_max) {
+   tseg1 = btc->tseg1_max;
+   tseg2 = tseg - tseg1;
+   }
+
+   sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
+   (tseg + CAN_SYNC_SEG);
+   sample_point_error = abs(sample_point_nominal - sample_point);
+
+   if (sample_point <= sample_point_nominal &&
+   sample_point_error < best_sample_point_error) {
+   best_sample_point = sample_point;
+   best_sample_point_error = sample_point_error;
+   *tseg1_ptr = tseg1;
+   *tseg2_ptr = tseg2;
+   }
+   }
+
+   if (sample_point_error_ptr)
+   *sample_point_error_ptr = best_sample_point_error;
+
+   return best_sample_point;
+}
+
+int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
+  const struct can_bittiming_const *btc)
+{
+   struct can_priv *priv = netdev_priv(dev);
+   unsigned int bitrate;   /* current bitrate */
+   unsigned int bitrate_error; /* difference between current 
and nominal value */
+   unsigned int best_bitrate_error = UINT_MAX;
+   unsigned int sample_point_error;/* difference between current 
and nominal value */
+   unsigned int best_sample_point_error = UINT_MAX;
+   unsigned int sample_point_nominal;  /* nominal sample point */
+ 

[net-next 12/17] can: dev: extend struct can_skb_priv to hold CAN frame length

2021-01-13 Thread Marc Kleine-Budde
In order to implement byte queue limits (bql) in CAN drivers, the length of the
CAN frame needs to be passed into the networking stack after queueing and after
transmission completion.

To avoid to calculate this length twice, extend the struct can_skb_priv to hold
the length of the CAN frame and extend __can_get_echo_skb() to return that
value.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-12-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/rx-offload.c | 2 +-
 drivers/net/can/dev/skb.c| 9 +++--
 include/linux/can/skb.h  | 4 +++-
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 3c1912c0430b..6a26b5282df1 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -271,7 +271,7 @@ unsigned int can_rx_offload_get_echo_skb(struct 
can_rx_offload *offload,
u8 len;
int err;
 
-   skb = __can_get_echo_skb(dev, idx, &len);
+   skb = __can_get_echo_skb(dev, idx, &len, NULL);
if (!skb)
return 0;
 
diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index 26cd597ff771..24f782a23409 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -76,7 +76,8 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device 
*dev,
 EXPORT_SYMBOL_GPL(can_put_echo_skb);
 
 struct sk_buff *
-__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
+__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr,
+  unsigned int *frame_len_ptr)
 {
struct can_priv *priv = netdev_priv(dev);
 
@@ -91,6 +92,7 @@ __can_get_echo_skb(struct net_device *dev, unsigned int idx, 
u8 *len_ptr)
 * length is supported on both CAN and CANFD frames.
 */
struct sk_buff *skb = priv->echo_skb[idx];
+   struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
struct canfd_frame *cf = (struct canfd_frame *)skb->data;
 
/* get the real payload length for netdev statistics */
@@ -99,6 +101,9 @@ __can_get_echo_skb(struct net_device *dev, unsigned int idx, 
u8 *len_ptr)
else
*len_ptr = cf->len;
 
+   if (frame_len_ptr)
+   *frame_len_ptr = can_skb_priv->frame_len;
+
priv->echo_skb[idx] = NULL;
 
return skb;
@@ -118,7 +123,7 @@ unsigned int can_get_echo_skb(struct net_device *dev, 
unsigned int idx)
struct sk_buff *skb;
u8 len;
 
-   skb = __can_get_echo_skb(dev, idx, &len);
+   skb = __can_get_echo_skb(dev, idx, &len, NULL);
if (!skb)
return 0;
 
diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h
index c90ebbd3008c..5db9da30843c 100644
--- a/include/linux/can/skb.h
+++ b/include/linux/can/skb.h
@@ -20,7 +20,7 @@ void can_flush_echo_skb(struct net_device *dev);
 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
 unsigned int idx);
 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
-  u8 *len_ptr);
+  u8 *len_ptr, unsigned int *frame_len_ptr);
 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
@@ -42,11 +42,13 @@ struct sk_buff *alloc_can_err_skb(struct net_device *dev,
  * struct can_skb_priv - private additional data inside CAN sk_buffs
  * @ifindex:   ifindex of the first interface the CAN frame appeared on
  * @skbcnt:atomic counter to have an unique id together with skb pointer
+ * @frame_len: length of CAN frame in data link layer
  * @cf:align to the following CAN frame at skb->data
  */
 struct can_skb_priv {
int ifindex;
int skbcnt;
+   unsigned int frame_len;
struct can_frame cf[];
 };
 
-- 
2.29.2




[net-next 08/17] can: length: convert to kernel coding style

2021-01-14 Thread Marc Kleine-Budde
This patch converts the file into the kernel coding style.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-8-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/length.c | 28 
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index 6fe18aa23ec9..5e7d481717ea 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -6,8 +6,10 @@
 
 /* CAN DLC to real data length conversion helpers */
 
-static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
-8, 12, 16, 20, 24, 32, 48, 64};
+static const u8 dlc2len[] = {
+   0, 1, 2, 3, 4, 5, 6, 7,
+   8, 12, 16, 20, 24, 32, 48, 64
+};
 
 /* get data length from raw data length code (DLC) */
 u8 can_fd_dlc2len(u8 dlc)
@@ -16,16 +18,18 @@ u8 can_fd_dlc2len(u8 dlc)
 }
 EXPORT_SYMBOL_GPL(can_fd_dlc2len);
 
-static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,/* 0 - 
8 */
-9, 9, 9, 9,/* 9 - 12 */
-10, 10, 10, 10,/* 13 - 16 */
-11, 11, 11, 11,/* 17 - 20 */
-12, 12, 12, 12,/* 21 - 24 */
-13, 13, 13, 13, 13, 13, 13, 13,/* 25 - 32 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 33 - 40 */
-14, 14, 14, 14, 14, 14, 14, 14,/* 41 - 48 */
-15, 15, 15, 15, 15, 15, 15, 15,/* 49 - 56 */
-15, 15, 15, 15, 15, 15, 15, 15};   /* 57 - 64 */
+static const u8 len2dlc[] = {
+   0, 1, 2, 3, 4, 5, 6, 7, 8,  /* 0 - 8 */
+   9, 9, 9, 9, /* 9 - 12 */
+   10, 10, 10, 10, /* 13 - 16 */
+   11, 11, 11, 11, /* 17 - 20 */
+   12, 12, 12, 12, /* 21 - 24 */
+   13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
+   14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
+   14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
+   15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
+   15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
+};
 
 /* map the sanitized data length to an appropriate data length code */
 u8 can_fd_len2dlc(u8 len)
-- 
2.29.2




[net-next 10/17] can: length: canfd_sanitize_len(): add function to sanitize CAN-FD data length

2021-01-14 Thread Marc Kleine-Budde
The data field in CAN-FD frames have specifig frame length (0, 1, 2, 3, 4, 5,
6, 7, 8, 12, 16, 20, 24, 32, 48, 64). This function "rounds" up a given length
to the next valid CAN-FD frame length.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-10-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 include/linux/can/length.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index 156b9d17969a..b2313b2a0b02 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -45,4 +45,10 @@ u8 can_fd_dlc2len(u8 dlc);
 /* map the sanitized data length to an appropriate data length code */
 u8 can_fd_len2dlc(u8 len);
 
+/* map the data length to an appropriate data link layer length */
+static inline u8 canfd_sanitize_len(u8 len)
+{
+   return can_fd_dlc2len(can_fd_len2dlc(len));
+}
+
 #endif /* !_CAN_LENGTH_H */
-- 
2.29.2




[net-next 13/17] can: dev: can_put_echo_skb(): extend to handle frame_len

2021-01-14 Thread Marc Kleine-Budde
From: Vincent Mailhol 

Add a frame_len argument to can_put_echo_skb() which is used to save length of
the CAN frame into field frame_len of struct can_skb_priv so that it can be
later used after transmission completion. Convert all users of this function,
too.

Drivers which implement BQL call can_put_echo_skb() with the output of
can_skb_get_frame_len(skb) and drivers which do not simply pass zero as an
input (in the same way that NULL would be given to can_get_echo_skb()). This
way, we have a nice symmetry between the two echo functions.

Link: 
https://lore.kernel.org/r/20210111061335.39983-1-mailhol.vinc...@wanadoo.fr
Signed-off-by: Marc Kleine-Budde 
Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-13-...@pengutronix.de
Signed-off-by: Vincent Mailhol 
---
 drivers/net/can/at91_can.c   | 2 +-
 drivers/net/can/c_can/c_can.c| 2 +-
 drivers/net/can/cc770/cc770.c| 2 +-
 drivers/net/can/dev/skb.c| 5 -
 drivers/net/can/flexcan.c| 2 +-
 drivers/net/can/grcan.c  | 2 +-
 drivers/net/can/ifi_canfd/ifi_canfd.c| 2 +-
 drivers/net/can/kvaser_pciefd.c  | 2 +-
 drivers/net/can/m_can/m_can.c| 4 ++--
 drivers/net/can/mscan/mscan.c| 2 +-
 drivers/net/can/pch_can.c| 2 +-
 drivers/net/can/peak_canfd/peak_canfd.c  | 2 +-
 drivers/net/can/rcar/rcar_can.c  | 2 +-
 drivers/net/can/rcar/rcar_canfd.c| 2 +-
 drivers/net/can/sja1000/sja1000.c| 2 +-
 drivers/net/can/softing/softing_main.c   | 2 +-
 drivers/net/can/spi/hi311x.c | 2 +-
 drivers/net/can/spi/mcp251x.c| 2 +-
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c   | 2 +-
 drivers/net/can/sun4i_can.c  | 2 +-
 drivers/net/can/ti_hecc.c| 2 +-
 drivers/net/can/usb/ems_usb.c| 2 +-
 drivers/net/can/usb/esd_usb2.c   | 2 +-
 drivers/net/can/usb/gs_usb.c | 2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 2 +-
 drivers/net/can/usb/mcba_usb.c   | 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 2 +-
 drivers/net/can/usb/ucan.c   | 2 +-
 drivers/net/can/usb/usb_8dev.c   | 2 +-
 drivers/net/can/xilinx_can.c | 4 ++--
 include/linux/can/skb.h  | 2 +-
 31 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/drivers/net/can/at91_can.c b/drivers/net/can/at91_can.c
index 5284f0ab3b06..90b223a80ed4 100644
--- a/drivers/net/can/at91_can.c
+++ b/drivers/net/can/at91_can.c
@@ -484,7 +484,7 @@ static netdev_tx_t at91_start_xmit(struct sk_buff *skb, 
struct net_device *dev)
stats->tx_bytes += cf->len;
 
/* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
-   can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv));
+   can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv), 0);
 
/*
 * we have to stop the queue and deliver all messages in case
diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c
index 63f48b016ecd..13638954a25c 100644
--- a/drivers/net/can/c_can/c_can.c
+++ b/drivers/net/can/c_can/c_can.c
@@ -476,7 +476,7 @@ static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
 */
c_can_setup_tx_object(dev, IF_TX, frame, idx);
priv->dlc[idx] = frame->len;
-   can_put_echo_skb(skb, dev, idx);
+   can_put_echo_skb(skb, dev, idx, 0);
 
/* Update the active bits */
atomic_add((1 << idx), &priv->tx_active);
diff --git a/drivers/net/can/cc770/cc770.c b/drivers/net/can/cc770/cc770.c
index 8d9f332c35e0..e53ca338368a 100644
--- a/drivers/net/can/cc770/cc770.c
+++ b/drivers/net/can/cc770/cc770.c
@@ -702,7 +702,7 @@ static void cc770_tx_interrupt(struct net_device *dev, 
unsigned int o)
stats->tx_bytes += cf->len;
stats->tx_packets++;
 
-   can_put_echo_skb(priv->tx_skb, dev, 0);
+   can_put_echo_skb(priv->tx_skb, dev, 0, 0);
can_get_echo_skb(dev, 0);
priv->tx_skb = NULL;
 
diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index 24f782a23409..c184b4dce19e 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -38,7 +38,7 @@ void can_flush_echo_skb(struct net_device *dev)
  * priv->echo_skb, if necessary.
  */
 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
-unsigned int idx)
+unsigned int idx, unsigned int frame_len)
 {
struct can_priv *priv = netdev_priv(dev);
 
@@ -62,6 +62,9 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device 
*dev,
skb->ip_summed = CHECKSUM_UNNECESSARY;
skb->dev = 

[net-next 14/17] can: dev: can_get_echo_skb(): extend to return can frame length

2021-01-14 Thread Marc Kleine-Budde
In order to implement byte queue limits (bql) in CAN drivers, the length of the
CAN frame needs to be passed into the networking stack after queueing and after
transmission completion.

To avoid to calculate this length twice, extend can_get_echo_skb() to return
that value. Convert all users of this function, too.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-14-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/at91_can.c| 2 +-
 drivers/net/can/c_can/c_can.c | 2 +-
 drivers/net/can/cc770/cc770.c | 2 +-
 drivers/net/can/dev/skb.c | 5 +++--
 drivers/net/can/grcan.c   | 2 +-
 drivers/net/can/ifi_canfd/ifi_canfd.c | 2 +-
 drivers/net/can/kvaser_pciefd.c   | 4 ++--
 drivers/net/can/m_can/m_can.c | 4 ++--
 drivers/net/can/mscan/mscan.c | 2 +-
 drivers/net/can/pch_can.c | 2 +-
 drivers/net/can/peak_canfd/peak_canfd.c   | 2 +-
 drivers/net/can/rcar/rcar_can.c   | 2 +-
 drivers/net/can/rcar/rcar_canfd.c | 2 +-
 drivers/net/can/sja1000/sja1000.c | 2 +-
 drivers/net/can/softing/softing_main.c| 2 +-
 drivers/net/can/spi/hi311x.c  | 2 +-
 drivers/net/can/spi/mcp251x.c | 2 +-
 drivers/net/can/sun4i_can.c   | 2 +-
 drivers/net/can/usb/ems_usb.c | 2 +-
 drivers/net/can/usb/esd_usb2.c| 2 +-
 drivers/net/can/usb/gs_usb.c  | 2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 2 +-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 2 +-
 drivers/net/can/usb/mcba_usb.c| 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_core.c  | 2 +-
 drivers/net/can/usb/ucan.c| 2 +-
 drivers/net/can/usb/usb_8dev.c| 2 +-
 drivers/net/can/xilinx_can.c  | 2 +-
 include/linux/can/skb.h   | 3 ++-
 29 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/drivers/net/can/at91_can.c b/drivers/net/can/at91_can.c
index 90b223a80ed4..9ad9b39f480e 100644
--- a/drivers/net/can/at91_can.c
+++ b/drivers/net/can/at91_can.c
@@ -856,7 +856,7 @@ static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
if (likely(reg_msr & AT91_MSR_MRDY &&
   ~reg_msr & AT91_MSR_MABT)) {
/* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
-   can_get_echo_skb(dev, mb - get_mb_tx_first(priv));
+   can_get_echo_skb(dev, mb - get_mb_tx_first(priv), NULL);
dev->stats.tx_packets++;
can_led_event(dev, CAN_LED_EVENT_TX);
}
diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c
index 13638954a25c..ef474bae47a1 100644
--- a/drivers/net/can/c_can/c_can.c
+++ b/drivers/net/can/c_can/c_can.c
@@ -733,7 +733,7 @@ static void c_can_do_tx(struct net_device *dev)
pend &= ~(1 << idx);
obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
c_can_inval_tx_object(dev, IF_RX, obj);
-   can_get_echo_skb(dev, idx);
+   can_get_echo_skb(dev, idx, NULL);
bytes += priv->dlc[idx];
pkts++;
}
diff --git a/drivers/net/can/cc770/cc770.c b/drivers/net/can/cc770/cc770.c
index e53ca338368a..f8a130f594e2 100644
--- a/drivers/net/can/cc770/cc770.c
+++ b/drivers/net/can/cc770/cc770.c
@@ -703,7 +703,7 @@ static void cc770_tx_interrupt(struct net_device *dev, 
unsigned int o)
stats->tx_packets++;
 
can_put_echo_skb(priv->tx_skb, dev, 0, 0);
-   can_get_echo_skb(dev, 0);
+   can_get_echo_skb(dev, 0, NULL);
priv->tx_skb = NULL;
 
netif_wake_queue(dev);
diff --git a/drivers/net/can/dev/skb.c b/drivers/net/can/dev/skb.c
index c184b4dce19e..53683d4312f1 100644
--- a/drivers/net/can/dev/skb.c
+++ b/drivers/net/can/dev/skb.c
@@ -121,12 +121,13 @@ __can_get_echo_skb(struct net_device *dev, unsigned int 
idx, u8 *len_ptr,
  * is handled in the device driver. The driver must protect
  * access to priv->echo_skb, if necessary.
  */
-unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
+unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
+ unsigned int *frame_len_ptr)
 {
struct sk_buff *skb;
u8 len;
 
-   skb = __can_get_echo_skb(dev, idx, &len, NULL);
+   skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
if (!skb)
return 0;
 
diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
index 8086cdc1..4a8453290530 100644
--- a/drivers/net/can/grcan.c
+++ b/dr

[net-next 11/17] can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer

2021-01-14 Thread Marc Kleine-Budde
From: Vincent Mailhol 

This patch adds the function can_skb_get_frame_len() which returns the length
of a CAN frame on the data link layer, including Start-of-frame, Identifier,
various other bits, the actual data, the CRC, the End-of-frame, the Inter frame
spacing.

Co-developed-by: Arunachalam Santhanam 
Signed-off-by: Arunachalam Santhanam 
Co-developed-by: Vincent Mailhol 
Signed-off-by: Vincent Mailhol 
Acked-by: Vincent Mailhol 
Reviewed-by: Vincent Mailhol 
Co-developed-by: Marc Kleine-Budde 
Link: https://lore.kernel.org/r/2021041930.693847-11-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/length.c |  50 +++
 include/linux/can/length.h   | 120 +++
 2 files changed, 170 insertions(+)

diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index 64673a8d1234..d35c4e82314d 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -38,3 +38,53 @@ u8 can_fd_len2dlc(u8 len)
return len2dlc[len];
 }
 EXPORT_SYMBOL_GPL(can_fd_len2dlc);
+
+/**
+ * can_skb_get_frame_len() - Calculate the CAN Frame length in bytes
+ * of a given skb.
+ * @skb: socket buffer of a CAN message.
+ *
+ * Do a rough calculation: bit stuffing is ignored and length in bits
+ * is rounded up to a length in bytes.
+ *
+ * Rationale: this function is to be used for the BQL functions
+ * (netdev_sent_queue() and netdev_completed_queue()) which expect a
+ * value in bytes. Just using skb->len is insufficient because it will
+ * return the constant value of CAN(FD)_MTU. Doing the bit stuffing
+ * calculation would be too expensive in term of computing resources
+ * for no noticeable gain.
+ *
+ * Remarks: The payload of CAN FD frames with BRS flag are sent at a
+ * different bitrate. Currently, the can-utils canbusload tool does
+ * not support CAN-FD yet and so we could not run any benchmark to
+ * measure the impact. There might be possible improvement here.
+ *
+ * Return: length in bytes.
+ */
+unsigned int can_skb_get_frame_len(const struct sk_buff *skb)
+{
+   const struct canfd_frame *cf = (const struct canfd_frame *)skb->data;
+   u8 len;
+
+   if (can_is_canfd_skb(skb))
+   len = canfd_sanitize_len(cf->len);
+   else if (cf->can_id & CAN_RTR_FLAG)
+   len = 0;
+   else
+   len = cf->len;
+
+   if (can_is_canfd_skb(skb)) {
+   if (cf->can_id & CAN_EFF_FLAG)
+   len += CANFD_FRAME_OVERHEAD_EFF;
+   else
+   len += CANFD_FRAME_OVERHEAD_SFF;
+   } else {
+   if (cf->can_id & CAN_EFF_FLAG)
+   len += CAN_FRAME_OVERHEAD_EFF;
+   else
+   len += CAN_FRAME_OVERHEAD_SFF;
+   }
+
+   return len;
+}
+EXPORT_SYMBOL_GPL(can_skb_get_frame_len);
diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index b2313b2a0b02..6995092b774e 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -1,10 +1,127 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /* Copyright (C) 2020 Oliver Hartkopp 
+ * Copyright (C) 2020 Marc Kleine-Budde 
  */
 
 #ifndef _CAN_LENGTH_H
 #define _CAN_LENGTH_H
 
+/*
+ * Size of a Classical CAN Standard Frame
+ *
+ * Name of Field   Bits
+ * -
+ * Start-of-frame  1
+ * Identifier  11
+ * Remote transmission request (RTR)   1
+ * Identifier extension bit (IDE)  1
+ * Reserved bit (r0)   1
+ * Data length code (DLC)  4
+ * Data field  0...64
+ * CRC 15
+ * CRC delimiter   1
+ * ACK slot1
+ * ACK delimiter   1
+ * End-of-frame (EOF)  7
+ * Inter frame spacing 3
+ *
+ * rounded up and ignoring bitstuffing
+ */
+#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8)
+
+/*
+ * Size of a Classical CAN Extended Frame
+ *
+ * Name of Field   Bits
+ * -
+ * Start-of-frame  1
+ * Identifier A11
+ * Substitute remote request (SRR) 1
+ * Identifier extension bit (IDE)  1
+ * Identifier B18
+ * Remote transmission request (RTR)   1
+ * Reserved bits (r1, r0)  2
+ * Data length code (DLC)  4
+ * Data field  0...64
+ * CRC 15
+ * CRC delimiter   1
+ * ACK slot1
+ * ACK delimiter   1
+ * End-of-frame (EOF)  7
+ * Inter frame spacing 3
+ *
+ * rounded up and ignoring bitstuffing
+ */
+#define CAN_FRAME_OVERHEAD_EFF DIV_

[net-next 15/17] can: dev: can_rx_offload_get_echo_skb(): extend to return can frame length

2021-01-14 Thread Marc Kleine-Budde
In order to implement byte queue limits (bql) in CAN drivers, the length of the
CAN frame needs to be passed into the networking stack after queueing and after
transmission completion.

To avoid to calculate this length twice, extend can_rx_offload_get_echo_skb()
to return that value. Convert all users of this function, too.

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-15-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/rx-offload.c   | 5 +++--
 drivers/net/can/flexcan.c  | 5 +++--
 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 2 +-
 drivers/net/can/ti_hecc.c  | 2 +-
 include/linux/can/rx-offload.h | 3 ++-
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 6a26b5282df1..ab2c1543786c 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -263,7 +263,8 @@ int can_rx_offload_queue_sorted(struct can_rx_offload 
*offload,
 EXPORT_SYMBOL_GPL(can_rx_offload_queue_sorted);
 
 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
-unsigned int idx, u32 timestamp)
+unsigned int idx, u32 timestamp,
+unsigned int *frame_len_ptr)
 {
struct net_device *dev = offload->dev;
struct net_device_stats *stats = &dev->stats;
@@ -271,7 +272,7 @@ unsigned int can_rx_offload_get_echo_skb(struct 
can_rx_offload *offload,
u8 len;
int err;
 
-   skb = __can_get_echo_skb(dev, idx, &len, NULL);
+   skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
if (!skb)
return 0;
 
diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index 202d08f8e1a4..5d9157c655e9 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -1122,8 +1122,9 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl);
 
handled = IRQ_HANDLED;
-   stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload,
-  0, reg_ctrl << 
16);
+   stats->tx_bytes +=
+   can_rx_offload_get_echo_skb(&priv->offload, 0,
+   reg_ctrl << 16, NULL);
stats->tx_packets++;
can_led_event(dev, CAN_LED_EVENT_TX);
 
diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c 
b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
index 95bba456a4cd..63bbe0930e53 100644
--- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
+++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
@@ -1271,7 +1271,7 @@ mcp251xfd_handle_tefif_one(struct mcp251xfd_priv *priv,
stats->tx_bytes +=
can_rx_offload_get_echo_skb(&priv->offload,
mcp251xfd_get_tef_tail(priv),
-   hw_tef_obj->ts);
+   hw_tef_obj->ts, NULL);
stats->tx_packets++;
priv->tef->tail++;
 
diff --git a/drivers/net/can/ti_hecc.c b/drivers/net/can/ti_hecc.c
index 485c19bc98c2..73245d8836a9 100644
--- a/drivers/net/can/ti_hecc.c
+++ b/drivers/net/can/ti_hecc.c
@@ -757,7 +757,7 @@ static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
stamp = hecc_read_stamp(priv, mbxno);
stats->tx_bytes +=
can_rx_offload_get_echo_skb(&priv->offload,
-   mbxno, stamp);
+   mbxno, stamp, NULL);
stats->tx_packets++;
can_led_event(ndev, CAN_LED_EVENT_TX);
--priv->tx_tail;
diff --git a/include/linux/can/rx-offload.h b/include/linux/can/rx-offload.h
index f1b38088b765..40882df7105e 100644
--- a/include/linux/can/rx-offload.h
+++ b/include/linux/can/rx-offload.h
@@ -44,7 +44,8 @@ int can_rx_offload_irq_offload_fifo(struct can_rx_offload 
*offload);
 int can_rx_offload_queue_sorted(struct can_rx_offload *offload,
struct sk_buff *skb, u32 timestamp);
 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
-unsigned int idx, u32 timestamp);
+unsigned int idx, u32 timestamp,
+unsigned int *frame_len_ptr);
 int can_rx_offload_queue_tail(struct can_rx_offload *offload,
  struct sk_buff *skb);
 void can_rx_offload_del(struct can_rx_offload *offload);
-- 
2.29.2




[net-next 09/17] can: length: can_fd_len2dlc(): simplify length calculcation

2021-01-14 Thread Marc Kleine-Budde
If the length paramter in len2dlc() exceeds the size of the len2dlc array, we
return 0xF. This is equal to the last 16 members of the array.

This patch removes these members from the array, uses ARRAY_SIZE() for the
length check, and returns CANFD_MAX_DLC (which is 0xf).

Reviewed-by: Vincent Mailhol 
Link: https://lore.kernel.org/r/2021041930.693847-9-...@pengutronix.de
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev/length.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index 5e7d481717ea..64673a8d1234 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -27,15 +27,13 @@ static const u8 len2dlc[] = {
13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
-   15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
-   15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
 };
 
 /* map the sanitized data length to an appropriate data length code */
 u8 can_fd_len2dlc(u8 len)
 {
-   if (unlikely(len > 64))
-   return 0xF;
+   if (len >= ARRAY_SIZE(len2dlc))
+   return CANFD_MAX_DLC;
 
return len2dlc[len];
 }
-- 
2.29.2




Re: [PATCH v10 1/1] can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces

2021-01-14 Thread Marc Kleine-Budde
uld just like your opinion on one topic: the tdco is specific
> to CAN FD. If we add it, we have two choices:
>   1. put it in struct can_bittiming: that will mean that we will
>  have an unused field for classical CAN (field bittiming of
>  struct can_priv).
>   2. put it in struct can_priv (but outside of struct
>  can_bittiming): no unused field but less pretty.

3. Deprecate struct can_bittiming as the user space interface
   and transfer each member individually via netlink. Extend
   the kernel-only can_bittiming by the tdc related
   parameters, and add these to the new netlink interface.

I prefer this, as I want to extend the bittiming_const in this way, too. There
are CAN controllers, where the bit timing calculation:

>   bt->prop_seg = tseg1 / 2;
>   bt->phase_seg1 = tseg1 - bt->prop_seg;

doesn't work anymore, as they have asymmetric prog_seg and phase_seg1, so that
splitting tseg1 in half doesn't work anymore.

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH] can: mcp251xfd: mcp251xfd_handle_ivmif(): fix wrong NULL pointer check

2021-01-18 Thread Marc Kleine-Budde
On 1/19/21 3:02 AM, angkery wrote:
> From: Junlin Yang 
> 
> if alloc_can_err_skb() returns NULL, we should check skb instead of cf.

Have a look at the function, there is a check for cf several times before this.
But as cf is initialized as NULL this is should be no problem.

While in:

| ca4c6ebeeb50 can: mcp251xfd: mcp251xfd_handle_rxif_one():
| fix wrong NULL pointer check

a real problem has been fixed.

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


[net 1/3] can: dev: can_restart: fix use after free bug

2021-01-20 Thread Marc Kleine-Budde
From: Vincent Mailhol 

After calling netif_rx_ni(skb), dereferencing skb is unsafe.
Especially, the can_frame cf which aliases skb memory is accessed
after the netif_rx_ni() in:
  stats->rx_bytes += cf->len;

Reordering the lines solves the issue.

Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface")
Link: 
https://lore.kernel.org/r/20210120114137.200019-2-mailhol.vinc...@wanadoo.fr
Signed-off-by: Vincent Mailhol 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/dev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c
index 3486704c8a95..8b1ae023cb21 100644
--- a/drivers/net/can/dev.c
+++ b/drivers/net/can/dev.c
@@ -592,11 +592,11 @@ static void can_restart(struct net_device *dev)
 
cf->can_id |= CAN_ERR_RESTARTED;
 
-   netif_rx_ni(skb);
-
stats->rx_packets++;
stats->rx_bytes += cf->len;
 
+   netif_rx_ni(skb);
+
 restart:
netdev_dbg(dev, "restarted\n");
priv->can_stats.restarts++;

base-commit: 9c30ae8398b0813e237bde387d67a7f74ab2db2d
-- 
2.29.2




[net 2/3] can: vxcan: vxcan_xmit: fix use after free bug

2021-01-20 Thread Marc Kleine-Budde
From: Vincent Mailhol 

After calling netif_rx_ni(skb), dereferencing skb is unsafe.
Especially, the canfd_frame cfd which aliases skb memory is accessed
after the netif_rx_ni().

Fixes: a8f820a380a2 ("can: add Virtual CAN Tunnel driver (vxcan)")
Link: 
https://lore.kernel.org/r/20210120114137.200019-3-mailhol.vinc...@wanadoo.fr
Signed-off-by: Vincent Mailhol 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/vxcan.c | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/vxcan.c b/drivers/net/can/vxcan.c
index fa47bab510bb..f9a524c5f6d6 100644
--- a/drivers/net/can/vxcan.c
+++ b/drivers/net/can/vxcan.c
@@ -39,6 +39,7 @@ static netdev_tx_t vxcan_xmit(struct sk_buff *skb, struct 
net_device *dev)
struct net_device *peer;
struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
struct net_device_stats *peerstats, *srcstats = &dev->stats;
+   u8 len;
 
if (can_dropped_invalid_skb(dev, skb))
return NETDEV_TX_OK;
@@ -61,12 +62,13 @@ static netdev_tx_t vxcan_xmit(struct sk_buff *skb, struct 
net_device *dev)
skb->dev= peer;
skb->ip_summed  = CHECKSUM_UNNECESSARY;
 
+   len = cfd->len;
if (netif_rx_ni(skb) == NET_RX_SUCCESS) {
srcstats->tx_packets++;
-   srcstats->tx_bytes += cfd->len;
+   srcstats->tx_bytes += len;
peerstats = &peer->stats;
peerstats->rx_packets++;
-   peerstats->rx_bytes += cfd->len;
+   peerstats->rx_bytes += len;
}
 
 out_unlock:
-- 
2.29.2




Re: [PATCH v4 1/3] can: dev: can_restart: fix use after free bug

2021-01-20 Thread Marc Kleine-Budde
On 1/20/21 12:41 PM, Vincent Mailhol wrote:
> After calling netif_rx_ni(skb), dereferencing skb is unsafe.
> Especially, the can_frame cf which aliases skb memory is accessed
> after the netif_rx_ni() in:
>   stats->rx_bytes += cf->len;
> 
> Reordering the lines solves the issue.
> 
> Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface")
> Signed-off-by: Vincent Mailhol 
> ---
> *Remark for upstream*
> drivers/net/can/dev.c has been moved to drivers/net/can/dev/dev.c in
> below commit, please carry the patch forward.
> Reference: 3e77f70e7345 ("can: dev: move driver related infrastructure
> into separate subdir")

I've send a pull request to Jakub and David. Let's see what happens :)

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


[net 3/3] can: peak_usb: fix use after free bugs

2021-01-20 Thread Marc Kleine-Budde
From: Vincent Mailhol 

After calling peak_usb_netif_rx_ni(skb), dereferencing skb is unsafe.
Especially, the can_frame cf which aliases skb memory is accessed
after the peak_usb_netif_rx_ni().

Reordering the lines solves the issue.

Fixes: 0a25e1f4f185 ("can: peak_usb: add support for PEAK new CANFD USB 
adapters")
Link: 
https://lore.kernel.org/r/20210120114137.200019-4-mailhol.vinc...@wanadoo.fr
Signed-off-by: Vincent Mailhol 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c 
b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
index 61631f4fd92a..f347ecc79aef 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -514,11 +514,11 @@ static int pcan_usb_fd_decode_canmsg(struct 
pcan_usb_fd_if *usb_if,
else
memcpy(cfd->data, rm->d, cfd->len);
 
-   peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
-
netdev->stats.rx_packets++;
netdev->stats.rx_bytes += cfd->len;
 
+   peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(rm->ts_low));
+
return 0;
 }
 
@@ -580,11 +580,11 @@ static int pcan_usb_fd_decode_status(struct 
pcan_usb_fd_if *usb_if,
if (!skb)
return -ENOMEM;
 
-   peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
-
netdev->stats.rx_packets++;
netdev->stats.rx_bytes += cf->len;
 
+   peak_usb_netif_rx(skb, &usb_if->time_ref, le32_to_cpu(sm->ts_low));
+
return 0;
 }
 
-- 
2.29.2




pull-request: can 2021-01-20

2021-01-20 Thread Marc Kleine-Budde
Hello Jakub, hello David,

this is a pull request of 3 patches for net/master.

All three patches are by Vincent Mailhol and fix a potential use after free bug
in the CAN device infrastructure, the vxcan driver, and the peak_usk driver. In
the TX-path the skb is used to read from after it was passed to the networking
stack with netif_rx_ni().

Note: Patch 1/3 touches "drivers/net/can/dev.c". In net-next/master this file
has been moved to drivers/net/can/dev/dev.c [1] and parts of it have been
transfered into separate files. This may result in a merge conflict. Please
carry this patch forward, the change is rather simple. Drop us a note if
needed. Are any actions needed with regards to linux-next?

[1] 3e77f70e7345 can: dev: move driver related infrastructure into separate 
subdir

regards,
Marc

---

The following changes since commit 9c30ae8398b0813e237bde387d67a7f74ab2db2d:

  tcp: fix TCP socket rehash stats mis-accounting (2021-01-19 19:47:20 -0800)

are available in the Git repository at:

  git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git 
tags/linux-can-fixes-for-5.11-20210120

for you to fetch changes up to 50aca891d7a554db0901b245167cd653d73aaa71:

  can: peak_usb: fix use after free bugs (2021-01-20 13:33:28 +0100)


linux-can-fixes-for-5.11-20210120


Vincent Mailhol (3):
  can: dev: can_restart: fix use after free bug
  can: vxcan: vxcan_xmit: fix use after free bug
  can: peak_usb: fix use after free bugs

 drivers/net/can/dev.c  | 4 ++--
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c | 8 
 drivers/net/can/vxcan.c| 6 --
 3 files changed, 10 insertions(+), 8 deletions(-)




Re: pull-request: can 2021-01-20

2021-01-20 Thread Marc Kleine-Budde
On 1/20/21 6:19 PM, Jakub Kicinski wrote:
>> this is a pull request of 3 patches for net/master.
>>
>> All three patches are by Vincent Mailhol and fix a potential use after free 
>> bug
>> in the CAN device infrastructure, the vxcan driver, and the peak_usk driver. 
>> In
>> the TX-path the skb is used to read from after it was passed to the 
>> networking
>> stack with netif_rx_ni().
> 
> Pulled, thanks.
> 
> Seems like the PR didn't show up in patchwork at all :S Hopefully I can
> still pull reight manually without the scripts :)

Fingers crossed. :D

Today I noticed a lag of >4h on vger.kernel.org. Even this mail of yours hasn't
made it to the linux-can list, yet. It's 3h delayed.

>> Note: Patch 1/3 touches "drivers/net/can/dev.c". In net-next/master this file
>> has been moved to drivers/net/can/dev/dev.c [1] and parts of it have been
>> transfered into separate files. This may result in a merge conflict. Please
>> carry this patch forward, the change is rather simple. Drop us a note if
>> needed. Are any actions needed with regards to linux-next?
> 
> Thanks for the note, I'm sending the PR to Linus now, so I think
> linux-next may never see the the conflict.

thanks,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v4 0/3] Fix several use after free bugs

2021-01-20 Thread Marc Kleine-Budde
On 1/20/21 12:41 PM, Vincent Mailhol wrote:
> This series fix three bugs which all have the same root cause.
> 
> When calling netif_rx(skb) and its variants, the skb will eventually
> get consumed (or freed) and thus it is unsafe to dereference it after
> the call returns.
> 
> This remark especially applies to any variable with aliases the skb
> memory which is the case of the can(fd)_frame.
> 
> The pattern is as this:
> skb = alloc_can_skb(dev, &cf);
> /* Do stuff */
> netif_rx(skb);
> stats->rx_bytes += cf->len;
> 
> Increasing the stats should be done *before* the call to netif_rx()
> while the skb is still safe to use.
> 
> Changes since v3:
>   - Patch 1/3: move the comments for upstream after the --- scissors
> 
> Changes since v2:
>   - rebase on net/master
>   - Patch 1/3: Added a comment towards upstream to inform about a
> conflict which will occur when net-next and net are merged
> Ref: 
> https://lore.kernel.org/linux-can/20210120085356.m7nabbw5zhy7p...@hardanger.blackshift.org/
> 
> Changes since v1:
>   - fix a silly typo in patch 2/3 (variable len was declared twice...)
> 
> Vincent Mailhol (3):
>   can: dev: can_restart: fix use after free bug
>   can: vxcan: vxcan_xmit: fix use after free bug
>   can: peak_usb: fix use after free bugs
> 
>  drivers/net/can/dev.c  | 4 ++--
>  drivers/net/can/usb/peak_usb/pcan_usb_fd.c | 8 
>  drivers/net/can/vxcan.c| 6 --
>  3 files changed, 10 insertions(+), 8 deletions(-)

Applied to linux-can-testing. I don't know why 2/3 hasn't made it to the mail
archive yet.

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: pull-request: can 2021-01-20

2021-01-20 Thread Marc Kleine-Budde
On 1/20/21 9:36 PM, Jakub Kicinski wrote:
>>>> Note: Patch 1/3 touches "drivers/net/can/dev.c". In net-next/master this 
>>>> file
>>>> has been moved to drivers/net/can/dev/dev.c [1] and parts of it have been
>>>> transfered into separate files. This may result in a merge conflict. Please
>>>> carry this patch forward, the change is rather simple. Drop us a note if
>>>> needed. Are any actions needed with regards to linux-next?  
>>>
>>> Thanks for the note, I'm sending the PR to Linus now, so I think
>>> linux-next may never see the the conflict.  
> 
> The merge has been done now, could you double check?

Looks good!

Thanks,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v1] can: mcp251xfd: replace sizeof(u32) with val_bytes in regmap

2021-01-21 Thread Marc Kleine-Budde
On Thu, Jan 21, 2021 at 04:33:13PM +0800, Su Yanjun wrote:
> No functional effect.
> 
> Signed-off-by: Su Yanjun 

Fails to build from source:

  CC [M]  drivers/net/can/spi/mcp251xfd/mcp251xfd-core.o
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c: In function 
‘mcp251xfd_get_val_bytes’:
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:187:22: error: invalid use of 
undefined type ‘struct regmap’
  187 |  return priv->map_reg->format.val_bytes;
  |  ^~
rivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:188:1: error: control reaches end 
of non-void function [-Werror=return-type]
  188 | }
  | ^
cc1: some warnings being treated as errors

Marc

-- 
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- |


signature.asc
Description: PGP signature


Re: [PATCH v2] can: mcp251xfd: replace sizeof(u32) with val_bytes in regmap

2021-01-21 Thread Marc Kleine-Budde
On Thu, Jan 21, 2021 at 05:10:05PM +0800, Su Yanjun wrote:

Please describe why you change this.

> No functional effect.

Not quite:

scripts/bloat-o-meter shows:

add/remove: 0/0 grow/shrink: 3/0 up/down: 104/0 (104)
Function old new   delta
mcp251xfd_handle_tefif   9801028 +48
mcp251xfd_irq   37163756 +40
mcp251xfd_handle_rxif_ring   964 980 +16
Total: Before=20832, After=20936, chg +0.50%

> 
> Signed-off-by: Su Yanjun 
> ---
>  drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 15 ---
>  1 file changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c 
> b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
> index f07e8b737d31..b15bfd50b863 100644
> --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
> +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
> @@ -181,6 +181,12 @@ static int mcp251xfd_clks_and_vdd_disable(const struct 
> mcp251xfd_priv *priv)
>   return 0;
>  }
>  
> +static inline int
> +mcp251xfd_get_val_bytes(const struct mcp251xfd_priv *priv)
> +{
> + return regmap_get_val_bytes(priv->map_reg);

You're always using the "map_reg" here

> +}
> +
>  static inline u8
>  mcp251xfd_cmd_prepare_write_reg(const struct mcp251xfd_priv *priv,
>   union mcp251xfd_write_reg_buf *write_reg_buf,
> @@ -1308,6 +1314,7 @@ mcp251xfd_tef_obj_read(const struct mcp251xfd_priv 
> *priv,
>  const u8 offset, const u8 len)
>  {
>   const struct mcp251xfd_tx_ring *tx_ring = priv->tx;
> + int val_bytes = mcp251xfd_get_val_bytes(priv);
>  
>   if (IS_ENABLED(CONFIG_CAN_MCP251XFD_SANITY) &&
>   (offset > tx_ring->obj_num ||
> @@ -1322,7 +1329,7 @@ mcp251xfd_tef_obj_read(const struct mcp251xfd_priv 
> *priv,
>   return regmap_bulk_read(priv->map_rx,

But this works on map_rx.

>   mcp251xfd_get_tef_obj_addr(offset),
>   hw_tef_obj,
> - sizeof(*hw_tef_obj) / sizeof(u32) * len);
> + sizeof(*hw_tef_obj) / val_bytes * len);
>  }
>  
>  static int mcp251xfd_handle_tefif(struct mcp251xfd_priv *priv)
> @@ -1511,11 +1518,12 @@ mcp251xfd_rx_obj_read(const struct mcp251xfd_priv 
> *priv,
> const u8 offset, const u8 len)
>  {
>   int err;
> + int val_bytes = mcp251xfd_get_val_bytes(priv);
>  
>   err = regmap_bulk_read(priv->map_rx,

Same here

>  mcp251xfd_get_rx_obj_addr(ring, offset),
>  hw_rx_obj,
> -len * ring->obj_size / sizeof(u32));
> +len * ring->obj_size / val_bytes);
>  
>   return err;
>  }
> @@ -2139,6 +2147,7 @@ static irqreturn_t mcp251xfd_irq(int irq, void *dev_id)
>   struct mcp251xfd_priv *priv = dev_id;
>   irqreturn_t handled = IRQ_NONE;
>   int err;
> + int val_bytes = mcp251xfd_get_val_bytes(priv);
>  
>   if (priv->rx_int)
>   do {
> @@ -2162,7 +2171,7 @@ static irqreturn_t mcp251xfd_irq(int irq, void *dev_id)
>   err = regmap_bulk_read(priv->map_reg, MCP251XFD_REG_INT,

Here it's map_reg.

>  &priv->regs_status,
>          sizeof(priv->regs_status) /
> -sizeof(u32));
> +val_bytes);
>   if (err)
>   goto out_fail;
>  
> -- 
> 2.25.1

Marc

-- 
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- |


signature.asc
Description: PGP signature


Re: [PATCH v2] can: mcp251xfd: replace sizeof(u32) with val_bytes in regmap

2021-01-21 Thread Marc Kleine-Budde
On Thu, Jan 21, 2021 at 05:33:40PM +0800, Su wrote:
> The sizeof(u32) is hardcoded. IMO it's better to use the config value in
> regmap.

I got why you want to change this. Please update the patch description, comment
on the increase of the object size and address the other issues I pointed out.
I think it makes no sense to have the function mcp251xfd_get_val_bytes() as you
have to use several regmaps anyways.

> > > No functional effect.
> > Not quite:
> > 
> > scripts/bloat-o-meter shows:
> > 
> > add/remove: 0/0 grow/shrink: 3/0 up/down: 104/0 (104)
> > Function old new   delta
> > mcp251xfd_handle_tefif   9801028 +48
> > mcp251xfd_irq   37163756 +40
> > mcp251xfd_handle_rxif_ring   964 980 +16
> > Total: Before=20832, After=20936, chg +0.50%

Marc

-- 
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- |


signature.asc
Description: PGP signature


Re: [PATCH v1] can: mcp251xfd: Add some sysfs debug interfaces for registers r/w

2021-01-21 Thread Marc Kleine-Budde
On 1/22/21 7:22 AM, Su Yanjun wrote:
> When i debug mcp2518fd, some method to track registers is
> needed. This easy debug interface will be ok.

NACK

As the driver uses regmap, everything should be there already.

To read use:

| cat /sys/kernel/debug/regmap/spi0.0-crc/registers

Register write support for devices that are handles by proper kernel drivers is
a pure debugging tool, thus not enabled by default, not even with a Kconfig
switch. You have to enable it manually, have a look at commit:

09c6ecd39410 regmap: Add support for writing to regmap registers via debugfs

regards,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v1] can: mcp251xfd: use regmap_bulk_write for compatibility

2021-01-21 Thread Marc Kleine-Budde
On 1/22/21 4:02 AM, Su Yanjun wrote:
> Recently i use mcp2518fd on 4.x kernel which multiple write is not
> backported, regmap_raw_write will cause old kernel crash because the
> tx buffer in driver is smaller then 2K. Use regmap_bulk_write instead
> for compatibility.

Hmmm, this patch will never be backported to any 4.x kernel, as the driver is
not available on these kernels. You have to carry patches for these kernels
anyway, so I think I'll not take that patch. Sorry. Drop me a note if you are
interested in updating your kernel to a recent v5.11 kernel.

regards,
Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v3] can: mcp251xfd: replace sizeof(u32) with val_bytes in regmap

2021-01-21 Thread Marc Kleine-Budde
On 1/22/21 4:01 AM, Su Yanjun wrote:
> The sizeof(u32) is hardcoded. It's better to use the config value in
> regmap.
> 
> It increases the size of target object, but it's flexible when new mcp chip
> need other val_bytes.
> 
> Signed-off-by: Su Yanjun 
> ---
>  drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 9 ++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c 
> b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
> index f07e8b737d31..3dde52669343 100644
> --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
> +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c
> @@ -1308,6 +1308,7 @@ mcp251xfd_tef_obj_read(const struct mcp251xfd_priv 
> *priv,
>  const u8 offset, const u8 len)
>  {
>   const struct mcp251xfd_tx_ring *tx_ring = priv->tx;
> + int val_bytes = regmap_get_val_bytes(priv->map_reg);

You're using the wrong regmap here.

>  
>   if (IS_ENABLED(CONFIG_CAN_MCP251XFD_SANITY) &&
>   (offset > tx_ring->obj_num ||
> @@ -1322,7 +1323,7 @@ mcp251xfd_tef_obj_read(const struct mcp251xfd_priv 
> *priv,
>   return regmap_bulk_read(priv->map_rx,
>   mcp251xfd_get_tef_obj_addr(offset),
>   hw_tef_obj,
> - sizeof(*hw_tef_obj) / sizeof(u32) * len);
> + sizeof(*hw_tef_obj) / val_bytes * len);
>  }
>  
>  static int mcp251xfd_handle_tefif(struct mcp251xfd_priv *priv)
> @@ -1511,11 +1512,12 @@ mcp251xfd_rx_obj_read(const struct mcp251xfd_priv 
> *priv,
> const u8 offset, const u8 len)
>  {
>   int err;
> + int val_bytes = regmap_get_val_bytes(priv->map_reg);

You're using the wrong regmap here.

>  
>   err = regmap_bulk_read(priv->map_rx,
>  mcp251xfd_get_rx_obj_addr(ring, offset),
>  hw_rx_obj,
> -len * ring->obj_size / sizeof(u32));
> +len * ring->obj_size / val_bytes);
>  
>   return err;
>  }
> @@ -2139,6 +2141,7 @@ static irqreturn_t mcp251xfd_irq(int irq, void *dev_id)
>   struct mcp251xfd_priv *priv = dev_id;
>   irqreturn_t handled = IRQ_NONE;
>   int err;
> + int val_bytes = regmap_get_val_bytes(priv->map_reg);
>  
>   if (priv->rx_int)
>   do {
> @@ -2162,7 +2165,7 @@ static irqreturn_t mcp251xfd_irq(int irq, void *dev_id)
>   err = regmap_bulk_read(priv->map_reg, MCP251XFD_REG_INT,
>  &priv->regs_status,
>      sizeof(priv->regs_status) /
> -sizeof(u32));
> +val_bytes);
>   if (err)
>   goto out_fail;
>  
> 

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v1 2/2] isa: Make the remove callback for isa drivers return void

2021-01-21 Thread Marc Kleine-Budde
On Thu, Jan 21, 2021 at 09:48:12PM +0100, Uwe Kleine-König wrote:
> The driver core ignores the return value of the remove callback, so
> don't give isa drivers the chance to provide a value.
> 
> Adapt all isa_drivers with a remove callbacks accordingly; they all
> return 0 unconditionally anyhow.
> 
> Signed-off-by: Uwe Kleine-König 
> ---
>  drivers/net/can/sja1000/tscan1.c | 4 +---

For the can driver:

Acked-by: Marc Kleine-Budde 

Marc

-- 
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- |


signature.asc
Description: PGP signature


Re: [PATCH v1] can: mcp251xfd: use regmap_bulk_write for compatibility

2021-01-22 Thread Marc Kleine-Budde
On 1/22/21 8:59 AM, Su wrote:
> 
> 在 2021/1/22 下午3:26, Marc Kleine-Budde 写道:
>> On 1/22/21 4:02 AM, Su Yanjun wrote:
>>> Recently i use mcp2518fd on 4.x kernel which multiple write is not
>>> backported, regmap_raw_write will cause old kernel crash because the
>>> tx buffer in driver is smaller then 2K. Use regmap_bulk_write instead
>>> for compatibility.
>> Hmmm, this patch will never be backported to any 4.x kernel, as the driver is
>> not available on these kernels. You have to carry patches for these kernels
>> anyway, so I think I'll not take that patch. Sorry. Drop me a note if you are
>> interested in updating your kernel to a recent v5.11 kernel.
> 
> I got it. I have already port it to 4.x kernel. I just want anyone 
> working on old kernels to use the driver more easier.

Ok, you can post a link yo your repo with the patches for the interested reader.

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH v4] can: mcp251xfd: replace sizeof(u32) with val_bytes in regmap

2021-01-22 Thread Marc Kleine-Budde
On Fri, Jan 22, 2021 at 04:13:34PM +0800, Su Yanjun wrote:
> The sizeof(u32) is hardcoded. It's better to use the config value in
> regmap.
> 
> It increases the size of target object, but it's flexible when new mcp chip
> need other val_bytes.
> 
> Signed-off-by: Su Yanjun 

Applied to linux-can-next/testing.

Thanks,
Marc

-- 
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- |


signature.asc
Description: PGP signature


Re: [RFC PATCH net 1/2] net: introduce CAN specific pointer in the struct net_device

2021-01-22 Thread Marc Kleine-Budde
On Fri, Jan 15, 2021 at 04:07:23PM -0800, Jakub Kicinski wrote:
> On Fri, 15 Jan 2021 15:30:35 +0100 Oleksij Rempel wrote:
> > Since 20dd3850bcf8 ("can: Speed up CAN frame receiption by using
> > ml_priv") the CAN framework uses per device specific data in the AF_CAN
> > protocol. For this purpose the struct net_device->ml_priv is used. Later
> > the ml_priv usage in CAN was extended for other users, one of them being
> > CAN_J1939.
> > 
> > Later in the kernel ml_priv was converted to an union, used by other
> > drivers. E.g. the tun driver started storing it's stats pointer.
> > 
> > Since tun devices can claim to be a CAN device, CAN specific protocols
> > will wrongly interpret this pointer, which will cause system crashes.
> > Mostly this issue is visible in the CAN_J1939 stack.
> > 
> > To fix this issue, we request a dedicated CAN pointer within the
> > net_device struct.
> 
> No strong objection, others already added their pointers, but 
> I wonder if we can't save those couple of bytes by adding a
> ml_priv_type, to check instead of dev->type? And leave it 0
> for drivers using stats?

Sounds good.

If we want to save even more bytes, it might be possible, to move the
wireless and wpan pointers to ml_priv.

struct wireless_dev *ieee80211_ptr;
struct wpan_dev *ieee802154_ptr;

> That way other device types which are limited by all being 
> ARPHDR_ETHER can start using ml_priv as well?
> 
> > +#if IS_ENABLED(CONFIG_CAN)
> > +   struct can_ml_priv  *can;
> > +#endif
> 
> Perhaps put it next to all the _ptr fields?

Makes sense. Oleksij will resping the series.

regards,
Marc

-- 
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- |


signature.asc
Description: PGP signature


[PATCH] net: dp83tc811: fix link detection and possbile IRQ storm

2021-01-22 Thread Marc Kleine-Budde
In our setup the dp83tc811 is configure to master mode. When there is no slave
connected the dp83tc811 triggers an interrupt is triggerd, several bits in the
status registers are set, and te INT_N pin goes low. One of interrupt bits is
the NO_FRAME interrupt.

Reading the status register acknowledges the interrupt, but as there is still
no slave connected, the NO_FRAME interrupt stays set and the INT_N pin low. For
level triggered IRQs this results in an interrupt storm, as long as the slave
is not connected. For edge triggered interupts the link change event, when the
slave comes online, is lost.

To fix this problem the NO_FRAME interrupt is not enabled. At least in our
testcase with edge triggered interrupts link change events are now properly
detected.

Cc: Dan Murphy 
Signed-off-by: Marc Kleine-Budde 
---
 drivers/net/phy/dp83tc811.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/net/phy/dp83tc811.c b/drivers/net/phy/dp83tc811.c
index b8d802cbf615..2801492bfaad 100644
--- a/drivers/net/phy/dp83tc811.c
+++ b/drivers/net/phy/dp83tc811.c
@@ -222,7 +222,6 @@ static int dp83811_config_intr(struct phy_device *phydev)
return err;
 
misr_status = (DP83811_LPS_INT_EN |
-  DP83811_NO_FRAME_INT_EN |
   DP83811_POR_DONE_INT_EN);
 
err = phy_write(phydev, MII_DP83811_INT_STAT3, misr_status);
-- 
2.29.2




Re: [PATCH] net: dp83tc811: fix link detection and possbile IRQ storm

2021-01-22 Thread Marc Kleine-Budde
On 1/22/21 4:03 PM, Marc Kleine-Budde wrote:
> In our setup the dp83tc811 is configure to master mode. When there is no slave
> connected the dp83tc811 triggers an interrupt is triggerd, several bits in the
> status registers are set, and te INT_N pin goes low. One of interrupt bits is
> the NO_FRAME interrupt.
> 
> Reading the status register acknowledges the interrupt, but as there is still
> no slave connected, the NO_FRAME interrupt stays set and the INT_N pin low. 
> For
> level triggered IRQs this results in an interrupt storm, as long as the slave
> is not connected. For edge triggered interupts the link change event, when the
> slave comes online, is lost.
> 
> To fix this problem the NO_FRAME interrupt is not enabled. At least in our
> testcase with edge triggered interrupts link change events are now properly
> detected.
> 
> Cc: Dan Murphy 
> Signed-off-by: Marc Kleine-Budde 

Sorry - this patch will not apply as is, I'll send an updated version.

Marc

-- 
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- |



signature.asc
Description: OpenPGP digital signature


Re: [PATCH 2/2 net] lan78xx: workaround of forced 100 Full/Half duplex mode error

2021-01-22 Thread Marc Kleine-Budde
On Mon, Apr 25, 2016 at 10:22:36PM +, woojung@microchip.com wrote:
> From: Woojung Huh 
> 
> At forced 100 Full & Half duplex mode, chip may fail to set mode correctly
> when cable is switched between long(~50+m) and short one.
> As workaround, set to 10 before setting to 100 at forced 100 F/H mode.

Sorry to picking up this old patch. We're using a LAN7801T, with an
external TI dp83tc811 PHY...

> Signed-off-by: Woojung Huh 
> ---
>  drivers/net/usb/lan78xx.c | 29 -
>  1 file changed, 28 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c
> index 0460b81..f64778a 100644
> --- a/drivers/net/usb/lan78xx.c
> +++ b/drivers/net/usb/lan78xx.c
> @@ -1804,7 +1804,34 @@ static void lan78xx_remove_mdio(struct lan78xx_net 
> *dev)
>  
>  static void lan78xx_link_status_change(struct net_device *net)
>  {
> - /* nothing to do */
> + struct phy_device *phydev = net->phydev;
> + int ret, temp;
> +
> + /* At forced 100 F/H mode, chip may fail to set mode correctly
> +  * when cable is switched between long(~50+m) and short one.
> +  * As workaround, set to 10 before setting to 100
> +  * at forced 100 F/H mode.
> +  */
> + if (!phydev->autoneg && (phydev->speed == 100)) {
> + /* disable phy interrupt */
> + temp = phy_read(phydev, LAN88XX_INT_MASK);
> + temp &= ~LAN88XX_INT_MASK_MDINTPIN_EN_;
> + ret = phy_write(phydev, LAN88XX_INT_MASK, temp);

It seems here you are assuming a microchip PHY attached, where the
INT_MASK register is at 0x19. In think 0x19 is a reserved
register. You better not write to it.

Are there some microchips components where the MAC and the PHY are in
one chip? Is that combination identifiable by the USB-ID?

> + temp = phy_read(phydev, MII_BMCR);
> + temp &= ~(BMCR_SPEED100 | BMCR_SPEED1000);
> + phy_write(phydev, MII_BMCR, temp); /* set to 10 first */
> + temp |= BMCR_SPEED100;
> + phy_write(phydev, MII_BMCR, temp); /* set to 100 later */
> +
> + /* clear pending interrupt generated while workaround */
> + temp = phy_read(phydev, LAN88XX_INT_STS);
> +
> + /* enable phy interrupt back */
> + temp = phy_read(phydev, LAN88XX_INT_MASK);
> + temp |= LAN88XX_INT_MASK_MDINTPIN_EN_;
> + ret = phy_write(phydev, LAN88XX_INT_MASK, temp);
> + }
>  }

regards,
Marc

-- 
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- |


signature.asc
Description: PGP signature


  1   2   3   4   5   6   7   8   9   10   >