1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
5 * Copyright (C) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
8 #include <linux/can/dev.h>
9 #include <net/rtnetlink.h>
11 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
12 [IFLA_CAN_STATE] = { .type = NLA_U32 },
13 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
14 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
15 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
16 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
17 [IFLA_CAN_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
18 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
19 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
20 [IFLA_CAN_DATA_BITTIMING] = { .len = sizeof(struct can_bittiming) },
21 [IFLA_CAN_DATA_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) },
22 [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
23 [IFLA_CAN_TDC] = { .type = NLA_NESTED },
24 [IFLA_CAN_CTRLMODE_EXT] = { .type = NLA_NESTED },
27 static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {
28 [IFLA_CAN_TDC_TDCV_MIN] = { .type = NLA_U32 },
29 [IFLA_CAN_TDC_TDCV_MAX] = { .type = NLA_U32 },
30 [IFLA_CAN_TDC_TDCO_MIN] = { .type = NLA_U32 },
31 [IFLA_CAN_TDC_TDCO_MAX] = { .type = NLA_U32 },
32 [IFLA_CAN_TDC_TDCF_MIN] = { .type = NLA_U32 },
33 [IFLA_CAN_TDC_TDCF_MAX] = { .type = NLA_U32 },
34 [IFLA_CAN_TDC_TDCV] = { .type = NLA_U32 },
35 [IFLA_CAN_TDC_TDCO] = { .type = NLA_U32 },
36 [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
39 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
40 struct netlink_ext_ack *extack)
42 bool is_can_fd = false;
44 /* Make sure that valid CAN FD configurations always consist of
45 * - nominal/arbitration bittiming
47 * - control mode with CAN_CTRLMODE_FD set
48 * - TDC parameters are coherent (details below)
54 if (data[IFLA_CAN_CTRLMODE]) {
55 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
56 u32 tdc_flags = cm->flags & CAN_CTRLMODE_TDC_MASK;
58 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
60 /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive */
61 if (tdc_flags == CAN_CTRLMODE_TDC_MASK)
63 /* If one of the CAN_CTRLMODE_TDC_* flag is set then
64 * TDC must be set and vice-versa
66 if (!!tdc_flags != !!data[IFLA_CAN_TDC])
68 /* If providing TDC parameters, at least TDCO is
69 * needed. TDCV is needed if and only if
70 * CAN_CTRLMODE_TDC_MANUAL is set
72 if (data[IFLA_CAN_TDC]) {
73 struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
76 err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX,
78 can_tdc_policy, extack);
82 if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
83 if (tdc_flags & CAN_CTRLMODE_TDC_AUTO)
86 if (tdc_flags & CAN_CTRLMODE_TDC_MANUAL)
90 if (!tb_tdc[IFLA_CAN_TDC_TDCO])
96 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
100 if (data[IFLA_CAN_DATA_BITTIMING] || data[IFLA_CAN_TDC]) {
108 static int can_tdc_changelink(struct can_priv *priv, const struct nlattr *nla,
109 struct netlink_ext_ack *extack)
111 struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
112 struct can_tdc tdc = { 0 };
113 const struct can_tdc_const *tdc_const = priv->tdc_const;
116 if (!tdc_const || !can_tdc_is_enabled(priv))
119 err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX, nla,
120 can_tdc_policy, extack);
124 if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
125 u32 tdcv = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCV]);
127 if (tdcv < tdc_const->tdcv_min || tdcv > tdc_const->tdcv_max)
133 if (tb_tdc[IFLA_CAN_TDC_TDCO]) {
134 u32 tdco = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCO]);
136 if (tdco < tdc_const->tdco_min || tdco > tdc_const->tdco_max)
142 if (tb_tdc[IFLA_CAN_TDC_TDCF]) {
143 u32 tdcf = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCF]);
145 if (tdcf < tdc_const->tdcf_min || tdcf > tdc_const->tdcf_max)
156 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
157 struct nlattr *data[],
158 struct netlink_ext_ack *extack)
160 struct can_priv *priv = netdev_priv(dev);
164 /* We need synchronization with dev->stop() */
167 if (data[IFLA_CAN_BITTIMING]) {
168 struct can_bittiming bt;
170 /* Do not allow changing bittiming while running */
171 if (dev->flags & IFF_UP)
174 /* Calculate bittiming parameters based on
175 * bittiming_const if set, otherwise pass bitrate
176 * directly via do_set_bitrate(). Bail out if neither
179 if (!priv->bittiming_const && !priv->do_set_bittiming)
182 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
183 err = can_get_bittiming(dev, &bt,
184 priv->bittiming_const,
186 priv->bitrate_const_cnt);
190 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
191 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
196 memcpy(&priv->bittiming, &bt, sizeof(bt));
198 if (priv->do_set_bittiming) {
199 /* Finally, set the bit-timing registers */
200 err = priv->do_set_bittiming(dev);
206 if (data[IFLA_CAN_CTRLMODE]) {
207 struct can_ctrlmode *cm;
211 /* Do not allow changing controller mode while running */
212 if (dev->flags & IFF_UP)
214 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
215 ctrlstatic = can_get_static_ctrlmode(priv);
216 maskedflags = cm->flags & cm->mask;
218 /* check whether provided bits are allowed to be passed */
219 if (maskedflags & ~(priv->ctrlmode_supported | ctrlstatic))
222 /* do not check for static fd-non-iso if 'fd' is disabled */
223 if (!(maskedflags & CAN_CTRLMODE_FD))
224 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
226 /* make sure static options are provided by configuration */
227 if ((maskedflags & ctrlstatic) != ctrlstatic)
230 /* clear bits to be modified and copy the flag values */
231 priv->ctrlmode &= ~cm->mask;
232 priv->ctrlmode |= maskedflags;
234 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
235 if (priv->ctrlmode & CAN_CTRLMODE_FD) {
236 dev->mtu = CANFD_MTU;
239 memset(&priv->data_bittiming, 0,
240 sizeof(priv->data_bittiming));
241 priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
242 memset(&priv->tdc, 0, sizeof(priv->tdc));
245 tdc_mask = cm->mask & CAN_CTRLMODE_TDC_MASK;
246 /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
247 * exclusive: make sure to turn the other one off
250 priv->ctrlmode &= cm->flags | ~CAN_CTRLMODE_TDC_MASK;
253 if (data[IFLA_CAN_RESTART_MS]) {
254 /* Do not allow changing restart delay while running */
255 if (dev->flags & IFF_UP)
257 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
260 if (data[IFLA_CAN_RESTART]) {
261 /* Do not allow a restart while not running */
262 if (!(dev->flags & IFF_UP))
264 err = can_restart_now(dev);
269 if (data[IFLA_CAN_DATA_BITTIMING]) {
270 struct can_bittiming dbt;
272 /* Do not allow changing bittiming while running */
273 if (dev->flags & IFF_UP)
276 /* Calculate bittiming parameters based on
277 * data_bittiming_const if set, otherwise pass bitrate
278 * directly via do_set_bitrate(). Bail out if neither
281 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
284 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
286 err = can_get_bittiming(dev, &dbt,
287 priv->data_bittiming_const,
288 priv->data_bitrate_const,
289 priv->data_bitrate_const_cnt);
293 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
294 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
299 memset(&priv->tdc, 0, sizeof(priv->tdc));
300 if (data[IFLA_CAN_TDC]) {
301 /* TDC parameters are provided: use them */
302 err = can_tdc_changelink(priv, data[IFLA_CAN_TDC],
305 priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
308 } else if (!tdc_mask) {
309 /* Neither of TDC parameters nor TDC flags are
310 * provided: do calculation
312 can_calc_tdco(&priv->tdc, priv->tdc_const, &priv->data_bittiming,
313 &priv->ctrlmode, priv->ctrlmode_supported);
314 } /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly
315 * turned off. TDC is disabled: do nothing
318 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
320 if (priv->do_set_data_bittiming) {
321 /* Finally, set the bit-timing registers */
322 err = priv->do_set_data_bittiming(dev);
328 if (data[IFLA_CAN_TERMINATION]) {
329 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
330 const unsigned int num_term = priv->termination_const_cnt;
333 if (!priv->do_set_termination)
336 /* check whether given value is supported by the interface */
337 for (i = 0; i < num_term; i++) {
338 if (termval == priv->termination_const[i])
344 /* Finally, set the termination value */
345 err = priv->do_set_termination(dev, termval);
349 priv->termination = termval;
355 static size_t can_tdc_get_size(const struct net_device *dev)
357 struct can_priv *priv = netdev_priv(dev);
360 if (!priv->tdc_const)
363 size = nla_total_size(0); /* nest IFLA_CAN_TDC */
364 if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL) {
365 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MIN */
366 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MAX */
368 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MIN */
369 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MAX */
370 if (priv->tdc_const->tdcf_max) {
371 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MIN */
372 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MAX */
375 if (can_tdc_is_enabled(priv)) {
376 if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL ||
377 priv->do_get_auto_tdcv)
378 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV */
379 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO */
380 if (priv->tdc_const->tdcf_max)
381 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF */
387 static size_t can_ctrlmode_ext_get_size(void)
389 return nla_total_size(0) + /* nest IFLA_CAN_CTRLMODE_EXT */
390 nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */
393 static size_t can_get_size(const struct net_device *dev)
395 struct can_priv *priv = netdev_priv(dev);
398 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
399 size += nla_total_size(sizeof(struct can_bittiming));
400 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
401 size += nla_total_size(sizeof(struct can_bittiming_const));
402 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
403 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
404 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
405 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
406 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
407 size += nla_total_size(sizeof(struct can_berr_counter));
408 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
409 size += nla_total_size(sizeof(struct can_bittiming));
410 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
411 size += nla_total_size(sizeof(struct can_bittiming_const));
412 if (priv->termination_const) {
413 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
414 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
415 priv->termination_const_cnt);
417 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
418 size += nla_total_size(sizeof(*priv->bitrate_const) *
419 priv->bitrate_const_cnt);
420 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
421 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
422 priv->data_bitrate_const_cnt);
423 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
424 size += can_tdc_get_size(dev); /* IFLA_CAN_TDC */
425 size += can_ctrlmode_ext_get_size(); /* IFLA_CAN_CTRLMODE_EXT */
430 static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
433 struct can_priv *priv = netdev_priv(dev);
434 struct can_tdc *tdc = &priv->tdc;
435 const struct can_tdc_const *tdc_const = priv->tdc_const;
440 nest = nla_nest_start(skb, IFLA_CAN_TDC);
444 if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL &&
445 (nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MIN, tdc_const->tdcv_min) ||
446 nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MAX, tdc_const->tdcv_max)))
448 if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MIN, tdc_const->tdco_min) ||
449 nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MAX, tdc_const->tdco_max))
451 if (tdc_const->tdcf_max &&
452 (nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MIN, tdc_const->tdcf_min) ||
453 nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)))
456 if (can_tdc_is_enabled(priv)) {
460 if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL) {
463 } else if (priv->do_get_auto_tdcv) {
464 err = priv->do_get_auto_tdcv(dev, &tdcv);
466 if (!err && nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdcv))
468 if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
470 if (tdc_const->tdcf_max &&
471 nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf))
475 nla_nest_end(skb, nest);
479 nla_nest_cancel(skb, nest);
483 static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
484 const struct can_priv *priv)
488 nest = nla_nest_start(skb, IFLA_CAN_CTRLMODE_EXT);
492 if (nla_put_u32(skb, IFLA_CAN_CTRLMODE_SUPPORTED,
493 priv->ctrlmode_supported)) {
494 nla_nest_cancel(skb, nest);
498 nla_nest_end(skb, nest);
502 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
504 struct can_priv *priv = netdev_priv(dev);
505 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
506 struct can_berr_counter bec = { };
507 enum can_state state = priv->state;
509 if (priv->do_get_state)
510 priv->do_get_state(dev, &state);
512 if ((priv->bittiming.bitrate &&
513 nla_put(skb, IFLA_CAN_BITTIMING,
514 sizeof(priv->bittiming), &priv->bittiming)) ||
516 (priv->bittiming_const &&
517 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
518 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
520 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
521 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
522 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
523 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
525 (priv->do_get_berr_counter &&
526 !priv->do_get_berr_counter(dev, &bec) &&
527 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
529 (priv->data_bittiming.bitrate &&
530 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
531 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
533 (priv->data_bittiming_const &&
534 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
535 sizeof(*priv->data_bittiming_const),
536 priv->data_bittiming_const)) ||
538 (priv->termination_const &&
539 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
540 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
541 sizeof(*priv->termination_const) *
542 priv->termination_const_cnt,
543 priv->termination_const))) ||
545 (priv->bitrate_const &&
546 nla_put(skb, IFLA_CAN_BITRATE_CONST,
547 sizeof(*priv->bitrate_const) *
548 priv->bitrate_const_cnt,
549 priv->bitrate_const)) ||
551 (priv->data_bitrate_const &&
552 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
553 sizeof(*priv->data_bitrate_const) *
554 priv->data_bitrate_const_cnt,
555 priv->data_bitrate_const)) ||
557 (nla_put(skb, IFLA_CAN_BITRATE_MAX,
558 sizeof(priv->bitrate_max),
559 &priv->bitrate_max)) ||
561 can_tdc_fill_info(skb, dev) ||
563 can_ctrlmode_ext_fill_info(skb, priv)
571 static size_t can_get_xstats_size(const struct net_device *dev)
573 return sizeof(struct can_device_stats);
576 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
578 struct can_priv *priv = netdev_priv(dev);
580 if (nla_put(skb, IFLA_INFO_XSTATS,
581 sizeof(priv->can_stats), &priv->can_stats))
582 goto nla_put_failure;
589 static int can_newlink(struct net *src_net, struct net_device *dev,
590 struct nlattr *tb[], struct nlattr *data[],
591 struct netlink_ext_ack *extack)
596 static void can_dellink(struct net_device *dev, struct list_head *head)
600 struct rtnl_link_ops can_link_ops __read_mostly = {
602 .netns_refund = true,
603 .maxtype = IFLA_CAN_MAX,
604 .policy = can_policy,
606 .validate = can_validate,
607 .newlink = can_newlink,
608 .changelink = can_changelink,
609 .dellink = can_dellink,
610 .get_size = can_get_size,
611 .fill_info = can_fill_info,
612 .get_xstats_size = can_get_xstats_size,
613 .fill_xstats = can_fill_xstats,
616 int can_netlink_register(void)
618 return rtnl_link_register(&can_link_ops);
621 void can_netlink_unregister(void)
623 rtnl_link_unregister(&can_link_ops);