Commit 2290aefa authored by Franklin S Cooper Jr's avatar Franklin S Cooper Jr Committed by Marc Kleine-Budde

can: dev: Add support for limiting configured bitrate

Various CAN or CAN-FD IP may be able to run at a faster rate than
what the transceiver the CAN node is connected to. This can lead to
unexpected errors. However, CAN transceivers typically have fixed
limitations and provide no means to discover these limitations at
runtime. Therefore, add support for a can-transceiver node that
can be reused by other CAN peripheral drivers to determine for both
CAN and CAN-FD what the max bitrate that can be used. If the user
tries to configure CAN to pass these maximum bitrates it will throw
an error.

Also add support for reading bitrate_max via the netlink interface.
Reviewed-by: default avatarSuman Anna <s-anna@ti.com>
Signed-off-by: default avatarFranklin S Cooper Jr <fcooper@ti.com>
[nsekhar@ti.com: fix build error with !CONFIG_OF]
Signed-off-by: default avatarSekhar Nori <nsekhar@ti.com>
Signed-off-by: default avatarFaiz Abbas <faiz_abbas@ti.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent 54a7fbcc
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/can/skb.h> #include <linux/can/skb.h>
#include <linux/can/netlink.h> #include <linux/can/netlink.h>
#include <linux/can/led.h> #include <linux/can/led.h>
#include <linux/of.h>
#include <net/rtnetlink.h> #include <net/rtnetlink.h>
#define MOD_DESC "CAN device driver interface" #define MOD_DESC "CAN device driver interface"
...@@ -814,6 +815,29 @@ int open_candev(struct net_device *dev) ...@@ -814,6 +815,29 @@ int open_candev(struct net_device *dev)
} }
EXPORT_SYMBOL_GPL(open_candev); EXPORT_SYMBOL_GPL(open_candev);
#ifdef CONFIG_OF
/* Common function that can be used to understand the limitation of
* a transceiver when it provides no means to determine these limitations
* at runtime.
*/
void of_can_transceiver(struct net_device *dev)
{
struct device_node *dn;
struct can_priv *priv = netdev_priv(dev);
struct device_node *np = dev->dev.parent->of_node;
int ret;
dn = of_get_child_by_name(np, "can-transceiver");
if (!dn)
return;
ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
}
EXPORT_SYMBOL_GPL(of_can_transceiver);
#endif
/* /*
* Common close function for cleanup before the device gets closed. * Common close function for cleanup before the device gets closed.
* *
...@@ -913,6 +937,13 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[], ...@@ -913,6 +937,13 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
priv->bitrate_const_cnt); priv->bitrate_const_cnt);
if (err) if (err)
return err; return err;
if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
priv->bitrate_max);
return -EINVAL;
}
memcpy(&priv->bittiming, &bt, sizeof(bt)); memcpy(&priv->bittiming, &bt, sizeof(bt));
if (priv->do_set_bittiming) { if (priv->do_set_bittiming) {
...@@ -997,6 +1028,13 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[], ...@@ -997,6 +1028,13 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
priv->data_bitrate_const_cnt); priv->data_bitrate_const_cnt);
if (err) if (err)
return err; return err;
if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
priv->bitrate_max);
return -EINVAL;
}
memcpy(&priv->data_bittiming, &dbt, sizeof(dbt)); memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
if (priv->do_set_data_bittiming) { if (priv->do_set_data_bittiming) {
...@@ -1064,6 +1102,7 @@ static size_t can_get_size(const struct net_device *dev) ...@@ -1064,6 +1102,7 @@ static size_t can_get_size(const struct net_device *dev)
if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */ if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
size += nla_total_size(sizeof(*priv->data_bitrate_const) * size += nla_total_size(sizeof(*priv->data_bitrate_const) *
priv->data_bitrate_const_cnt); priv->data_bitrate_const_cnt);
size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
return size; return size;
} }
...@@ -1121,7 +1160,11 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) ...@@ -1121,7 +1160,11 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST, nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
sizeof(*priv->data_bitrate_const) * sizeof(*priv->data_bitrate_const) *
priv->data_bitrate_const_cnt, priv->data_bitrate_const_cnt,
priv->data_bitrate_const)) priv->data_bitrate_const)) ||
(nla_put(skb, IFLA_CAN_BITRATE_MAX,
sizeof(priv->bitrate_max),
&priv->bitrate_max))
) )
return -EMSGSIZE; return -EMSGSIZE;
......
...@@ -46,6 +46,7 @@ struct can_priv { ...@@ -46,6 +46,7 @@ struct can_priv {
unsigned int bitrate_const_cnt; unsigned int bitrate_const_cnt;
const u32 *data_bitrate_const; const u32 *data_bitrate_const;
unsigned int data_bitrate_const_cnt; unsigned int data_bitrate_const_cnt;
u32 bitrate_max;
struct can_clock clock; struct can_clock clock;
enum can_state state; enum can_state state;
...@@ -166,6 +167,12 @@ void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, ...@@ -166,6 +167,12 @@ void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
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);
void can_free_echo_skb(struct net_device *dev, unsigned int idx); void can_free_echo_skb(struct net_device *dev, unsigned int idx);
#ifdef CONFIG_OF
void of_can_transceiver(struct net_device *dev);
#else
static inline void of_can_transceiver(struct net_device *dev) { }
#endif
struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf); struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
struct sk_buff *alloc_canfd_skb(struct net_device *dev, struct sk_buff *alloc_canfd_skb(struct net_device *dev,
struct canfd_frame **cfd); struct canfd_frame **cfd);
......
...@@ -132,6 +132,7 @@ enum { ...@@ -132,6 +132,7 @@ enum {
IFLA_CAN_TERMINATION_CONST, IFLA_CAN_TERMINATION_CONST,
IFLA_CAN_BITRATE_CONST, IFLA_CAN_BITRATE_CONST,
IFLA_CAN_DATA_BITRATE_CONST, IFLA_CAN_DATA_BITRATE_CONST,
IFLA_CAN_BITRATE_MAX,
__IFLA_CAN_MAX __IFLA_CAN_MAX
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment