1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
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 &&
180 !priv->bitrate_const)
183 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
184 err = can_get_bittiming(dev, &bt,
185 priv->bittiming_const,
187 priv->bitrate_const_cnt);
191 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
192 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
197 memcpy(&priv->bittiming, &bt, sizeof(bt));
199 if (priv->do_set_bittiming) {
200 /* Finally, set the bit-timing registers */
201 err = priv->do_set_bittiming(dev);
207 if (data[IFLA_CAN_CTRLMODE]) {
208 struct can_ctrlmode *cm;
212 /* Do not allow changing controller mode while running */
213 if (dev->flags & IFF_UP)
215 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
216 ctrlstatic = can_get_static_ctrlmode(priv);
217 maskedflags = cm->flags & cm->mask;
219 /* check whether provided bits are allowed to be passed */
220 if (maskedflags & ~(priv->ctrlmode_supported | ctrlstatic))
223 /* do not check for static fd-non-iso if 'fd' is disabled */
224 if (!(maskedflags & CAN_CTRLMODE_FD))
225 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
227 /* make sure static options are provided by configuration */
228 if ((maskedflags & ctrlstatic) != ctrlstatic)
231 /* clear bits to be modified and copy the flag values */
232 priv->ctrlmode &= ~cm->mask;
233 priv->ctrlmode |= maskedflags;
235 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
236 if (priv->ctrlmode & CAN_CTRLMODE_FD) {
237 dev->mtu = CANFD_MTU;
240 memset(&priv->data_bittiming, 0,
241 sizeof(priv->data_bittiming));
242 priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
243 memset(&priv->tdc, 0, sizeof(priv->tdc));
246 tdc_mask = cm->mask & CAN_CTRLMODE_TDC_MASK;
247 /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
248 * exclusive: make sure to turn the other one off
251 priv->ctrlmode &= cm->flags | ~CAN_CTRLMODE_TDC_MASK;
254 if (data[IFLA_CAN_RESTART_MS]) {
255 /* Do not allow changing restart delay while running */
256 if (dev->flags & IFF_UP)
258 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
261 if (data[IFLA_CAN_RESTART]) {
262 /* Do not allow a restart while not running */
263 if (!(dev->flags & IFF_UP))
265 err = can_restart_now(dev);
270 if (data[IFLA_CAN_DATA_BITTIMING]) {
271 struct can_bittiming dbt;
273 /* Do not allow changing bittiming while running */
274 if (dev->flags & IFF_UP)
277 /* Calculate bittiming parameters based on
278 * data_bittiming_const if set, otherwise pass bitrate
279 * directly via do_set_bitrate(). Bail out if neither
282 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming &&
283 !priv->data_bitrate_const)
286 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
288 err = can_get_bittiming(dev, &dbt,
289 priv->data_bittiming_const,
290 priv->data_bitrate_const,
291 priv->data_bitrate_const_cnt);
295 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
296 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
301 memset(&priv->tdc, 0, sizeof(priv->tdc));
302 if (data[IFLA_CAN_TDC]) {
303 /* TDC parameters are provided: use them */
304 err = can_tdc_changelink(priv, data[IFLA_CAN_TDC],
307 priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
310 } else if (!tdc_mask) {
311 /* Neither of TDC parameters nor TDC flags are
312 * provided: do calculation
314 can_calc_tdco(&priv->tdc, priv->tdc_const, &priv->data_bittiming,
315 &priv->ctrlmode, priv->ctrlmode_supported);
316 } /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly
317 * turned off. TDC is disabled: do nothing
320 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
322 if (priv->do_set_data_bittiming) {
323 /* Finally, set the bit-timing registers */
324 err = priv->do_set_data_bittiming(dev);
330 if (data[IFLA_CAN_TERMINATION]) {
331 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
332 const unsigned int num_term = priv->termination_const_cnt;
335 if (!priv->do_set_termination)
338 /* check whether given value is supported by the interface */
339 for (i = 0; i < num_term; i++) {
340 if (termval == priv->termination_const[i])
346 /* Finally, set the termination value */
347 err = priv->do_set_termination(dev, termval);
351 priv->termination = termval;
357 static size_t can_tdc_get_size(const struct net_device *dev)
359 struct can_priv *priv = netdev_priv(dev);
362 if (!priv->tdc_const)
365 size = nla_total_size(0); /* nest IFLA_CAN_TDC */
366 if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL) {
367 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MIN */
368 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MAX */
370 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MIN */
371 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MAX */
372 if (priv->tdc_const->tdcf_max) {
373 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MIN */
374 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MAX */
377 if (can_tdc_is_enabled(priv)) {
378 if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL ||
379 priv->do_get_auto_tdcv)
380 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV */
381 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO */
382 if (priv->tdc_const->tdcf_max)
383 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF */
389 static size_t can_ctrlmode_ext_get_size(void)
391 return nla_total_size(0) + /* nest IFLA_CAN_CTRLMODE_EXT */
392 nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */
395 static size_t can_get_size(const struct net_device *dev)
397 struct can_priv *priv = netdev_priv(dev);
400 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
401 size += nla_total_size(sizeof(struct can_bittiming));
402 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
403 size += nla_total_size(sizeof(struct can_bittiming_const));
404 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
405 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
406 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
407 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
408 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
409 size += nla_total_size(sizeof(struct can_berr_counter));
410 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
411 size += nla_total_size(sizeof(struct can_bittiming));
412 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
413 size += nla_total_size(sizeof(struct can_bittiming_const));
414 if (priv->termination_const) {
415 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
416 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
417 priv->termination_const_cnt);
419 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
420 size += nla_total_size(sizeof(*priv->bitrate_const) *
421 priv->bitrate_const_cnt);
422 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
423 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
424 priv->data_bitrate_const_cnt);
425 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
426 size += can_tdc_get_size(dev); /* IFLA_CAN_TDC */
427 size += can_ctrlmode_ext_get_size(); /* IFLA_CAN_CTRLMODE_EXT */
432 static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
435 struct can_priv *priv = netdev_priv(dev);
436 struct can_tdc *tdc = &priv->tdc;
437 const struct can_tdc_const *tdc_const = priv->tdc_const;
442 nest = nla_nest_start(skb, IFLA_CAN_TDC);
446 if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL &&
447 (nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MIN, tdc_const->tdcv_min) ||
448 nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MAX, tdc_const->tdcv_max)))
450 if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MIN, tdc_const->tdco_min) ||
451 nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MAX, tdc_const->tdco_max))
453 if (tdc_const->tdcf_max &&
454 (nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MIN, tdc_const->tdcf_min) ||
455 nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)))
458 if (can_tdc_is_enabled(priv)) {
462 if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL) {
465 } else if (priv->do_get_auto_tdcv) {
466 err = priv->do_get_auto_tdcv(dev, &tdcv);
468 if (!err && nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdcv))
470 if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
472 if (tdc_const->tdcf_max &&
473 nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf))
477 nla_nest_end(skb, nest);
481 nla_nest_cancel(skb, nest);
485 static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
486 const struct can_priv *priv)
490 nest = nla_nest_start(skb, IFLA_CAN_CTRLMODE_EXT);
494 if (nla_put_u32(skb, IFLA_CAN_CTRLMODE_SUPPORTED,
495 priv->ctrlmode_supported)) {
496 nla_nest_cancel(skb, nest);
500 nla_nest_end(skb, nest);
504 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
506 struct can_priv *priv = netdev_priv(dev);
507 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
508 struct can_berr_counter bec = { };
509 enum can_state state = priv->state;
511 if (priv->do_get_state)
512 priv->do_get_state(dev, &state);
514 if ((priv->bittiming.bitrate != CAN_BITRATE_UNSET &&
515 priv->bittiming.bitrate != CAN_BITRATE_UNKNOWN &&
516 nla_put(skb, IFLA_CAN_BITTIMING,
517 sizeof(priv->bittiming), &priv->bittiming)) ||
519 (priv->bittiming_const &&
520 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
521 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
523 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
524 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
525 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
526 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
528 (priv->do_get_berr_counter &&
529 !priv->do_get_berr_counter(dev, &bec) &&
530 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
532 (priv->data_bittiming.bitrate &&
533 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
534 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
536 (priv->data_bittiming_const &&
537 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
538 sizeof(*priv->data_bittiming_const),
539 priv->data_bittiming_const)) ||
541 (priv->termination_const &&
542 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
543 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
544 sizeof(*priv->termination_const) *
545 priv->termination_const_cnt,
546 priv->termination_const))) ||
548 (priv->bitrate_const &&
549 nla_put(skb, IFLA_CAN_BITRATE_CONST,
550 sizeof(*priv->bitrate_const) *
551 priv->bitrate_const_cnt,
552 priv->bitrate_const)) ||
554 (priv->data_bitrate_const &&
555 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
556 sizeof(*priv->data_bitrate_const) *
557 priv->data_bitrate_const_cnt,
558 priv->data_bitrate_const)) ||
560 (nla_put(skb, IFLA_CAN_BITRATE_MAX,
561 sizeof(priv->bitrate_max),
562 &priv->bitrate_max)) ||
564 can_tdc_fill_info(skb, dev) ||
566 can_ctrlmode_ext_fill_info(skb, priv)
574 static size_t can_get_xstats_size(const struct net_device *dev)
576 return sizeof(struct can_device_stats);
579 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
581 struct can_priv *priv = netdev_priv(dev);
583 if (nla_put(skb, IFLA_INFO_XSTATS,
584 sizeof(priv->can_stats), &priv->can_stats))
585 goto nla_put_failure;
592 static int can_newlink(struct net *src_net, struct net_device *dev,
593 struct nlattr *tb[], struct nlattr *data[],
594 struct netlink_ext_ack *extack)
599 static void can_dellink(struct net_device *dev, struct list_head *head)
603 struct rtnl_link_ops can_link_ops __read_mostly = {
605 .netns_refund = true,
606 .maxtype = IFLA_CAN_MAX,
607 .policy = can_policy,
609 .validate = can_validate,
610 .newlink = can_newlink,
611 .changelink = can_changelink,
612 .dellink = can_dellink,
613 .get_size = can_get_size,
614 .fill_info = can_fill_info,
615 .get_xstats_size = can_get_xstats_size,
616 .fill_xstats = can_fill_xstats,
619 int can_netlink_register(void)
621 return rtnl_link_register(&can_link_ops);
624 void can_netlink_unregister(void)
626 rtnl_link_unregister(&can_link_ops);