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 <[email protected]> 5 */ 6 7 #include <linux/module.h> 8 #include <linux/kernel.h> 9 #include <linux/slab.h> 10 #include <linux/netdevice.h> 11 #include <linux/if_arp.h> 12 #include <linux/workqueue.h> 13 #include <linux/can.h> 14 #include <linux/can/can-ml.h> 15 #include <linux/can/dev.h> 16 #include <linux/can/skb.h> 17 #include <linux/can/netlink.h> 18 #include <linux/can/led.h> 19 #include <linux/of.h> 20 #include <net/rtnetlink.h> 21 22 #define MOD_DESC "CAN device driver interface" 23 24 MODULE_DESCRIPTION(MOD_DESC); 25 MODULE_LICENSE("GPL v2"); 26 MODULE_AUTHOR("Wolfgang Grandegger <[email protected]>"); 27 28 static void can_update_state_error_stats(struct net_device *dev, 29 enum can_state new_state) 30 { 31 struct can_priv *priv = netdev_priv(dev); 32 33 if (new_state <= priv->state) 34 return; 35 36 switch (new_state) { 37 case CAN_STATE_ERROR_WARNING: 38 priv->can_stats.error_warning++; 39 break; 40 case CAN_STATE_ERROR_PASSIVE: 41 priv->can_stats.error_passive++; 42 break; 43 case CAN_STATE_BUS_OFF: 44 priv->can_stats.bus_off++; 45 break; 46 default: 47 break; 48 } 49 } 50 51 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state) 52 { 53 switch (state) { 54 case CAN_STATE_ERROR_ACTIVE: 55 return CAN_ERR_CRTL_ACTIVE; 56 case CAN_STATE_ERROR_WARNING: 57 return CAN_ERR_CRTL_TX_WARNING; 58 case CAN_STATE_ERROR_PASSIVE: 59 return CAN_ERR_CRTL_TX_PASSIVE; 60 default: 61 return 0; 62 } 63 } 64 65 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state) 66 { 67 switch (state) { 68 case CAN_STATE_ERROR_ACTIVE: 69 return CAN_ERR_CRTL_ACTIVE; 70 case CAN_STATE_ERROR_WARNING: 71 return CAN_ERR_CRTL_RX_WARNING; 72 case CAN_STATE_ERROR_PASSIVE: 73 return CAN_ERR_CRTL_RX_PASSIVE; 74 default: 75 return 0; 76 } 77 } 78 79 static const char *can_get_state_str(const enum can_state state) 80 { 81 switch (state) { 82 case CAN_STATE_ERROR_ACTIVE: 83 return "Error Active"; 84 case CAN_STATE_ERROR_WARNING: 85 return "Error Warning"; 86 case CAN_STATE_ERROR_PASSIVE: 87 return "Error Passive"; 88 case CAN_STATE_BUS_OFF: 89 return "Bus Off"; 90 case CAN_STATE_STOPPED: 91 return "Stopped"; 92 case CAN_STATE_SLEEPING: 93 return "Sleeping"; 94 default: 95 return "<unknown>"; 96 } 97 98 return "<unknown>"; 99 } 100 101 void can_change_state(struct net_device *dev, struct can_frame *cf, 102 enum can_state tx_state, enum can_state rx_state) 103 { 104 struct can_priv *priv = netdev_priv(dev); 105 enum can_state new_state = max(tx_state, rx_state); 106 107 if (unlikely(new_state == priv->state)) { 108 netdev_warn(dev, "%s: oops, state did not change", __func__); 109 return; 110 } 111 112 netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n", 113 can_get_state_str(priv->state), priv->state, 114 can_get_state_str(new_state), new_state); 115 116 can_update_state_error_stats(dev, new_state); 117 priv->state = new_state; 118 119 if (!cf) 120 return; 121 122 if (unlikely(new_state == CAN_STATE_BUS_OFF)) { 123 cf->can_id |= CAN_ERR_BUSOFF; 124 return; 125 } 126 127 cf->can_id |= CAN_ERR_CRTL; 128 cf->data[1] |= tx_state >= rx_state ? 129 can_tx_state_to_frame(dev, tx_state) : 0; 130 cf->data[1] |= tx_state <= rx_state ? 131 can_rx_state_to_frame(dev, rx_state) : 0; 132 } 133 EXPORT_SYMBOL_GPL(can_change_state); 134 135 /* Local echo of CAN messages 136 * 137 * CAN network devices *should* support a local echo functionality 138 * (see Documentation/networking/can.rst). To test the handling of CAN 139 * interfaces that do not support the local echo both driver types are 140 * implemented. In the case that the driver does not support the echo 141 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core 142 * to perform the echo as a fallback solution. 143 */ 144 static void can_flush_echo_skb(struct net_device *dev) 145 { 146 struct can_priv *priv = netdev_priv(dev); 147 struct net_device_stats *stats = &dev->stats; 148 int i; 149 150 for (i = 0; i < priv->echo_skb_max; i++) { 151 if (priv->echo_skb[i]) { 152 kfree_skb(priv->echo_skb[i]); 153 priv->echo_skb[i] = NULL; 154 stats->tx_dropped++; 155 stats->tx_aborted_errors++; 156 } 157 } 158 } 159 160 /* Put the skb on the stack to be looped backed locally lateron 161 * 162 * The function is typically called in the start_xmit function 163 * of the device driver. The driver must protect access to 164 * priv->echo_skb, if necessary. 165 */ 166 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, 167 unsigned int idx) 168 { 169 struct can_priv *priv = netdev_priv(dev); 170 171 BUG_ON(idx >= priv->echo_skb_max); 172 173 /* check flag whether this packet has to be looped back */ 174 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK || 175 (skb->protocol != htons(ETH_P_CAN) && 176 skb->protocol != htons(ETH_P_CANFD))) { 177 kfree_skb(skb); 178 return 0; 179 } 180 181 if (!priv->echo_skb[idx]) { 182 skb = can_create_echo_skb(skb); 183 if (!skb) 184 return -ENOMEM; 185 186 /* make settings for echo to reduce code in irq context */ 187 skb->pkt_type = PACKET_BROADCAST; 188 skb->ip_summed = CHECKSUM_UNNECESSARY; 189 skb->dev = dev; 190 191 /* save this skb for tx interrupt echo handling */ 192 priv->echo_skb[idx] = skb; 193 } else { 194 /* locking problem with netif_stop_queue() ?? */ 195 netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx); 196 kfree_skb(skb); 197 return -EBUSY; 198 } 199 200 return 0; 201 } 202 EXPORT_SYMBOL_GPL(can_put_echo_skb); 203 204 struct sk_buff * 205 __can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr) 206 { 207 struct can_priv *priv = netdev_priv(dev); 208 209 if (idx >= priv->echo_skb_max) { 210 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n", 211 __func__, idx, priv->echo_skb_max); 212 return NULL; 213 } 214 215 if (priv->echo_skb[idx]) { 216 /* Using "struct canfd_frame::len" for the frame 217 * length is supported on both CAN and CANFD frames. 218 */ 219 struct sk_buff *skb = priv->echo_skb[idx]; 220 struct canfd_frame *cf = (struct canfd_frame *)skb->data; 221 222 /* get the real payload length for netdev statistics */ 223 if (cf->can_id & CAN_RTR_FLAG) 224 *len_ptr = 0; 225 else 226 *len_ptr = cf->len; 227 228 priv->echo_skb[idx] = NULL; 229 230 return skb; 231 } 232 233 return NULL; 234 } 235 236 /* Get the skb from the stack and loop it back locally 237 * 238 * The function is typically called when the TX done interrupt 239 * is handled in the device driver. The driver must protect 240 * access to priv->echo_skb, if necessary. 241 */ 242 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx) 243 { 244 struct sk_buff *skb; 245 u8 len; 246 247 skb = __can_get_echo_skb(dev, idx, &len); 248 if (!skb) 249 return 0; 250 251 skb_get(skb); 252 if (netif_rx(skb) == NET_RX_SUCCESS) 253 dev_consume_skb_any(skb); 254 else 255 dev_kfree_skb_any(skb); 256 257 return len; 258 } 259 EXPORT_SYMBOL_GPL(can_get_echo_skb); 260 261 /* Remove the skb from the stack and free it. 262 * 263 * The function is typically called when TX failed. 264 */ 265 void can_free_echo_skb(struct net_device *dev, unsigned int idx) 266 { 267 struct can_priv *priv = netdev_priv(dev); 268 269 BUG_ON(idx >= priv->echo_skb_max); 270 271 if (priv->echo_skb[idx]) { 272 dev_kfree_skb_any(priv->echo_skb[idx]); 273 priv->echo_skb[idx] = NULL; 274 } 275 } 276 EXPORT_SYMBOL_GPL(can_free_echo_skb); 277 278 /* CAN device restart for bus-off recovery */ 279 static void can_restart(struct net_device *dev) 280 { 281 struct can_priv *priv = netdev_priv(dev); 282 struct net_device_stats *stats = &dev->stats; 283 struct sk_buff *skb; 284 struct can_frame *cf; 285 int err; 286 287 BUG_ON(netif_carrier_ok(dev)); 288 289 /* No synchronization needed because the device is bus-off and 290 * no messages can come in or go out. 291 */ 292 can_flush_echo_skb(dev); 293 294 /* send restart message upstream */ 295 skb = alloc_can_err_skb(dev, &cf); 296 if (!skb) 297 goto restart; 298 299 cf->can_id |= CAN_ERR_RESTARTED; 300 301 netif_rx_ni(skb); 302 303 stats->rx_packets++; 304 stats->rx_bytes += cf->len; 305 306 restart: 307 netdev_dbg(dev, "restarted\n"); 308 priv->can_stats.restarts++; 309 310 /* Now restart the device */ 311 err = priv->do_set_mode(dev, CAN_MODE_START); 312 313 netif_carrier_on(dev); 314 if (err) 315 netdev_err(dev, "Error %d during restart", err); 316 } 317 318 static void can_restart_work(struct work_struct *work) 319 { 320 struct delayed_work *dwork = to_delayed_work(work); 321 struct can_priv *priv = container_of(dwork, struct can_priv, 322 restart_work); 323 324 can_restart(priv->dev); 325 } 326 327 int can_restart_now(struct net_device *dev) 328 { 329 struct can_priv *priv = netdev_priv(dev); 330 331 /* A manual restart is only permitted if automatic restart is 332 * disabled and the device is in the bus-off state 333 */ 334 if (priv->restart_ms) 335 return -EINVAL; 336 if (priv->state != CAN_STATE_BUS_OFF) 337 return -EBUSY; 338 339 cancel_delayed_work_sync(&priv->restart_work); 340 can_restart(dev); 341 342 return 0; 343 } 344 345 /* CAN bus-off 346 * 347 * This functions should be called when the device goes bus-off to 348 * tell the netif layer that no more packets can be sent or received. 349 * If enabled, a timer is started to trigger bus-off recovery. 350 */ 351 void can_bus_off(struct net_device *dev) 352 { 353 struct can_priv *priv = netdev_priv(dev); 354 355 if (priv->restart_ms) 356 netdev_info(dev, "bus-off, scheduling restart in %d ms\n", 357 priv->restart_ms); 358 else 359 netdev_info(dev, "bus-off\n"); 360 361 netif_carrier_off(dev); 362 363 if (priv->restart_ms) 364 schedule_delayed_work(&priv->restart_work, 365 msecs_to_jiffies(priv->restart_ms)); 366 } 367 EXPORT_SYMBOL_GPL(can_bus_off); 368 369 static void can_setup(struct net_device *dev) 370 { 371 dev->type = ARPHRD_CAN; 372 dev->mtu = CAN_MTU; 373 dev->hard_header_len = 0; 374 dev->addr_len = 0; 375 dev->tx_queue_len = 10; 376 377 /* New-style flags. */ 378 dev->flags = IFF_NOARP; 379 dev->features = NETIF_F_HW_CSUM; 380 } 381 382 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) 383 { 384 struct sk_buff *skb; 385 386 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + 387 sizeof(struct can_frame)); 388 if (unlikely(!skb)) 389 return NULL; 390 391 skb->protocol = htons(ETH_P_CAN); 392 skb->pkt_type = PACKET_BROADCAST; 393 skb->ip_summed = CHECKSUM_UNNECESSARY; 394 395 skb_reset_mac_header(skb); 396 skb_reset_network_header(skb); 397 skb_reset_transport_header(skb); 398 399 can_skb_reserve(skb); 400 can_skb_prv(skb)->ifindex = dev->ifindex; 401 can_skb_prv(skb)->skbcnt = 0; 402 403 *cf = skb_put_zero(skb, sizeof(struct can_frame)); 404 405 return skb; 406 } 407 EXPORT_SYMBOL_GPL(alloc_can_skb); 408 409 struct sk_buff *alloc_canfd_skb(struct net_device *dev, 410 struct canfd_frame **cfd) 411 { 412 struct sk_buff *skb; 413 414 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + 415 sizeof(struct canfd_frame)); 416 if (unlikely(!skb)) 417 return NULL; 418 419 skb->protocol = htons(ETH_P_CANFD); 420 skb->pkt_type = PACKET_BROADCAST; 421 skb->ip_summed = CHECKSUM_UNNECESSARY; 422 423 skb_reset_mac_header(skb); 424 skb_reset_network_header(skb); 425 skb_reset_transport_header(skb); 426 427 can_skb_reserve(skb); 428 can_skb_prv(skb)->ifindex = dev->ifindex; 429 can_skb_prv(skb)->skbcnt = 0; 430 431 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame)); 432 433 return skb; 434 } 435 EXPORT_SYMBOL_GPL(alloc_canfd_skb); 436 437 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) 438 { 439 struct sk_buff *skb; 440 441 skb = alloc_can_skb(dev, cf); 442 if (unlikely(!skb)) 443 return NULL; 444 445 (*cf)->can_id = CAN_ERR_FLAG; 446 (*cf)->len = CAN_ERR_DLC; 447 448 return skb; 449 } 450 EXPORT_SYMBOL_GPL(alloc_can_err_skb); 451 452 /* Allocate and setup space for the CAN network device */ 453 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, 454 unsigned int txqs, unsigned int rxqs) 455 { 456 struct net_device *dev; 457 struct can_priv *priv; 458 int size; 459 460 /* We put the driver's priv, the CAN mid layer priv and the 461 * echo skb into the netdevice's priv. The memory layout for 462 * the netdev_priv is like this: 463 * 464 * +-------------------------+ 465 * | driver's priv | 466 * +-------------------------+ 467 * | struct can_ml_priv | 468 * +-------------------------+ 469 * | array of struct sk_buff | 470 * +-------------------------+ 471 */ 472 473 size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv); 474 475 if (echo_skb_max) 476 size = ALIGN(size, sizeof(struct sk_buff *)) + 477 echo_skb_max * sizeof(struct sk_buff *); 478 479 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup, 480 txqs, rxqs); 481 if (!dev) 482 return NULL; 483 484 priv = netdev_priv(dev); 485 priv->dev = dev; 486 487 dev->ml_priv = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN); 488 489 if (echo_skb_max) { 490 priv->echo_skb_max = echo_skb_max; 491 priv->echo_skb = (void *)priv + 492 (size - echo_skb_max * sizeof(struct sk_buff *)); 493 } 494 495 priv->state = CAN_STATE_STOPPED; 496 497 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work); 498 499 return dev; 500 } 501 EXPORT_SYMBOL_GPL(alloc_candev_mqs); 502 503 /* Free space of the CAN network device */ 504 void free_candev(struct net_device *dev) 505 { 506 free_netdev(dev); 507 } 508 EXPORT_SYMBOL_GPL(free_candev); 509 510 /* changing MTU and control mode for CAN/CANFD devices */ 511 int can_change_mtu(struct net_device *dev, int new_mtu) 512 { 513 struct can_priv *priv = netdev_priv(dev); 514 515 /* Do not allow changing the MTU while running */ 516 if (dev->flags & IFF_UP) 517 return -EBUSY; 518 519 /* allow change of MTU according to the CANFD ability of the device */ 520 switch (new_mtu) { 521 case CAN_MTU: 522 /* 'CANFD-only' controllers can not switch to CAN_MTU */ 523 if (priv->ctrlmode_static & CAN_CTRLMODE_FD) 524 return -EINVAL; 525 526 priv->ctrlmode &= ~CAN_CTRLMODE_FD; 527 break; 528 529 case CANFD_MTU: 530 /* check for potential CANFD ability */ 531 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) && 532 !(priv->ctrlmode_static & CAN_CTRLMODE_FD)) 533 return -EINVAL; 534 535 priv->ctrlmode |= CAN_CTRLMODE_FD; 536 break; 537 538 default: 539 return -EINVAL; 540 } 541 542 dev->mtu = new_mtu; 543 return 0; 544 } 545 EXPORT_SYMBOL_GPL(can_change_mtu); 546 547 /* Common open function when the device gets opened. 548 * 549 * This function should be called in the open function of the device 550 * driver. 551 */ 552 int open_candev(struct net_device *dev) 553 { 554 struct can_priv *priv = netdev_priv(dev); 555 556 if (!priv->bittiming.bitrate) { 557 netdev_err(dev, "bit-timing not yet defined\n"); 558 return -EINVAL; 559 } 560 561 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */ 562 if ((priv->ctrlmode & CAN_CTRLMODE_FD) && 563 (!priv->data_bittiming.bitrate || 564 priv->data_bittiming.bitrate < priv->bittiming.bitrate)) { 565 netdev_err(dev, "incorrect/missing data bit-timing\n"); 566 return -EINVAL; 567 } 568 569 /* Switch carrier on if device was stopped while in bus-off state */ 570 if (!netif_carrier_ok(dev)) 571 netif_carrier_on(dev); 572 573 return 0; 574 } 575 EXPORT_SYMBOL_GPL(open_candev); 576 577 #ifdef CONFIG_OF 578 /* Common function that can be used to understand the limitation of 579 * a transceiver when it provides no means to determine these limitations 580 * at runtime. 581 */ 582 void of_can_transceiver(struct net_device *dev) 583 { 584 struct device_node *dn; 585 struct can_priv *priv = netdev_priv(dev); 586 struct device_node *np = dev->dev.parent->of_node; 587 int ret; 588 589 dn = of_get_child_by_name(np, "can-transceiver"); 590 if (!dn) 591 return; 592 593 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max); 594 of_node_put(dn); 595 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max)) 596 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n"); 597 } 598 EXPORT_SYMBOL_GPL(of_can_transceiver); 599 #endif 600 601 /* Common close function for cleanup before the device gets closed. 602 * 603 * This function should be called in the close function of the device 604 * driver. 605 */ 606 void close_candev(struct net_device *dev) 607 { 608 struct can_priv *priv = netdev_priv(dev); 609 610 cancel_delayed_work_sync(&priv->restart_work); 611 can_flush_echo_skb(dev); 612 } 613 EXPORT_SYMBOL_GPL(close_candev); 614 615 /* CAN netlink interface */ 616 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { 617 [IFLA_CAN_STATE] = { .type = NLA_U32 }, 618 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) }, 619 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 }, 620 [IFLA_CAN_RESTART] = { .type = NLA_U32 }, 621 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) }, 622 [IFLA_CAN_BITTIMING_CONST] 623 = { .len = sizeof(struct can_bittiming_const) }, 624 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, 625 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, 626 [IFLA_CAN_DATA_BITTIMING] 627 = { .len = sizeof(struct can_bittiming) }, 628 [IFLA_CAN_DATA_BITTIMING_CONST] 629 = { .len = sizeof(struct can_bittiming_const) }, 630 [IFLA_CAN_TERMINATION] = { .type = NLA_U16 }, 631 }; 632 633 static int can_validate(struct nlattr *tb[], struct nlattr *data[], 634 struct netlink_ext_ack *extack) 635 { 636 bool is_can_fd = false; 637 638 /* Make sure that valid CAN FD configurations always consist of 639 * - nominal/arbitration bittiming 640 * - data bittiming 641 * - control mode with CAN_CTRLMODE_FD set 642 */ 643 644 if (!data) 645 return 0; 646 647 if (data[IFLA_CAN_CTRLMODE]) { 648 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); 649 650 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD; 651 } 652 653 if (is_can_fd) { 654 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING]) 655 return -EOPNOTSUPP; 656 } 657 658 if (data[IFLA_CAN_DATA_BITTIMING]) { 659 if (!is_can_fd || !data[IFLA_CAN_BITTIMING]) 660 return -EOPNOTSUPP; 661 } 662 663 return 0; 664 } 665 666 static int can_changelink(struct net_device *dev, struct nlattr *tb[], 667 struct nlattr *data[], 668 struct netlink_ext_ack *extack) 669 { 670 struct can_priv *priv = netdev_priv(dev); 671 int err; 672 673 /* We need synchronization with dev->stop() */ 674 ASSERT_RTNL(); 675 676 if (data[IFLA_CAN_BITTIMING]) { 677 struct can_bittiming bt; 678 679 /* Do not allow changing bittiming while running */ 680 if (dev->flags & IFF_UP) 681 return -EBUSY; 682 683 /* Calculate bittiming parameters based on 684 * bittiming_const if set, otherwise pass bitrate 685 * directly via do_set_bitrate(). Bail out if neither 686 * is given. 687 */ 688 if (!priv->bittiming_const && !priv->do_set_bittiming) 689 return -EOPNOTSUPP; 690 691 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); 692 err = can_get_bittiming(dev, &bt, 693 priv->bittiming_const, 694 priv->bitrate_const, 695 priv->bitrate_const_cnt); 696 if (err) 697 return err; 698 699 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) { 700 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n", 701 priv->bitrate_max); 702 return -EINVAL; 703 } 704 705 memcpy(&priv->bittiming, &bt, sizeof(bt)); 706 707 if (priv->do_set_bittiming) { 708 /* Finally, set the bit-timing registers */ 709 err = priv->do_set_bittiming(dev); 710 if (err) 711 return err; 712 } 713 } 714 715 if (data[IFLA_CAN_CTRLMODE]) { 716 struct can_ctrlmode *cm; 717 u32 ctrlstatic; 718 u32 maskedflags; 719 720 /* Do not allow changing controller mode while running */ 721 if (dev->flags & IFF_UP) 722 return -EBUSY; 723 cm = nla_data(data[IFLA_CAN_CTRLMODE]); 724 ctrlstatic = priv->ctrlmode_static; 725 maskedflags = cm->flags & cm->mask; 726 727 /* check whether provided bits are allowed to be passed */ 728 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic)) 729 return -EOPNOTSUPP; 730 731 /* do not check for static fd-non-iso if 'fd' is disabled */ 732 if (!(maskedflags & CAN_CTRLMODE_FD)) 733 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO; 734 735 /* make sure static options are provided by configuration */ 736 if ((maskedflags & ctrlstatic) != ctrlstatic) 737 return -EOPNOTSUPP; 738 739 /* clear bits to be modified and copy the flag values */ 740 priv->ctrlmode &= ~cm->mask; 741 priv->ctrlmode |= maskedflags; 742 743 /* CAN_CTRLMODE_FD can only be set when driver supports FD */ 744 if (priv->ctrlmode & CAN_CTRLMODE_FD) 745 dev->mtu = CANFD_MTU; 746 else 747 dev->mtu = CAN_MTU; 748 } 749 750 if (data[IFLA_CAN_RESTART_MS]) { 751 /* Do not allow changing restart delay while running */ 752 if (dev->flags & IFF_UP) 753 return -EBUSY; 754 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]); 755 } 756 757 if (data[IFLA_CAN_RESTART]) { 758 /* Do not allow a restart while not running */ 759 if (!(dev->flags & IFF_UP)) 760 return -EINVAL; 761 err = can_restart_now(dev); 762 if (err) 763 return err; 764 } 765 766 if (data[IFLA_CAN_DATA_BITTIMING]) { 767 struct can_bittiming dbt; 768 769 /* Do not allow changing bittiming while running */ 770 if (dev->flags & IFF_UP) 771 return -EBUSY; 772 773 /* Calculate bittiming parameters based on 774 * data_bittiming_const if set, otherwise pass bitrate 775 * directly via do_set_bitrate(). Bail out if neither 776 * is given. 777 */ 778 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming) 779 return -EOPNOTSUPP; 780 781 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), 782 sizeof(dbt)); 783 err = can_get_bittiming(dev, &dbt, 784 priv->data_bittiming_const, 785 priv->data_bitrate_const, 786 priv->data_bitrate_const_cnt); 787 if (err) 788 return err; 789 790 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) { 791 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n", 792 priv->bitrate_max); 793 return -EINVAL; 794 } 795 796 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt)); 797 798 if (priv->do_set_data_bittiming) { 799 /* Finally, set the bit-timing registers */ 800 err = priv->do_set_data_bittiming(dev); 801 if (err) 802 return err; 803 } 804 } 805 806 if (data[IFLA_CAN_TERMINATION]) { 807 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]); 808 const unsigned int num_term = priv->termination_const_cnt; 809 unsigned int i; 810 811 if (!priv->do_set_termination) 812 return -EOPNOTSUPP; 813 814 /* check whether given value is supported by the interface */ 815 for (i = 0; i < num_term; i++) { 816 if (termval == priv->termination_const[i]) 817 break; 818 } 819 if (i >= num_term) 820 return -EINVAL; 821 822 /* Finally, set the termination value */ 823 err = priv->do_set_termination(dev, termval); 824 if (err) 825 return err; 826 827 priv->termination = termval; 828 } 829 830 return 0; 831 } 832 833 static size_t can_get_size(const struct net_device *dev) 834 { 835 struct can_priv *priv = netdev_priv(dev); 836 size_t size = 0; 837 838 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */ 839 size += nla_total_size(sizeof(struct can_bittiming)); 840 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ 841 size += nla_total_size(sizeof(struct can_bittiming_const)); 842 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */ 843 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */ 844 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */ 845 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ 846 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ 847 size += nla_total_size(sizeof(struct can_berr_counter)); 848 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */ 849 size += nla_total_size(sizeof(struct can_bittiming)); 850 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */ 851 size += nla_total_size(sizeof(struct can_bittiming_const)); 852 if (priv->termination_const) { 853 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */ 854 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */ 855 priv->termination_const_cnt); 856 } 857 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */ 858 size += nla_total_size(sizeof(*priv->bitrate_const) * 859 priv->bitrate_const_cnt); 860 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */ 861 size += nla_total_size(sizeof(*priv->data_bitrate_const) * 862 priv->data_bitrate_const_cnt); 863 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */ 864 865 return size; 866 } 867 868 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) 869 { 870 struct can_priv *priv = netdev_priv(dev); 871 struct can_ctrlmode cm = {.flags = priv->ctrlmode}; 872 struct can_berr_counter bec; 873 enum can_state state = priv->state; 874 875 if (priv->do_get_state) 876 priv->do_get_state(dev, &state); 877 878 if ((priv->bittiming.bitrate && 879 nla_put(skb, IFLA_CAN_BITTIMING, 880 sizeof(priv->bittiming), &priv->bittiming)) || 881 882 (priv->bittiming_const && 883 nla_put(skb, IFLA_CAN_BITTIMING_CONST, 884 sizeof(*priv->bittiming_const), priv->bittiming_const)) || 885 886 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) || 887 nla_put_u32(skb, IFLA_CAN_STATE, state) || 888 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || 889 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || 890 891 (priv->do_get_berr_counter && 892 !priv->do_get_berr_counter(dev, &bec) && 893 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) || 894 895 (priv->data_bittiming.bitrate && 896 nla_put(skb, IFLA_CAN_DATA_BITTIMING, 897 sizeof(priv->data_bittiming), &priv->data_bittiming)) || 898 899 (priv->data_bittiming_const && 900 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST, 901 sizeof(*priv->data_bittiming_const), 902 priv->data_bittiming_const)) || 903 904 (priv->termination_const && 905 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) || 906 nla_put(skb, IFLA_CAN_TERMINATION_CONST, 907 sizeof(*priv->termination_const) * 908 priv->termination_const_cnt, 909 priv->termination_const))) || 910 911 (priv->bitrate_const && 912 nla_put(skb, IFLA_CAN_BITRATE_CONST, 913 sizeof(*priv->bitrate_const) * 914 priv->bitrate_const_cnt, 915 priv->bitrate_const)) || 916 917 (priv->data_bitrate_const && 918 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST, 919 sizeof(*priv->data_bitrate_const) * 920 priv->data_bitrate_const_cnt, 921 priv->data_bitrate_const)) || 922 923 (nla_put(skb, IFLA_CAN_BITRATE_MAX, 924 sizeof(priv->bitrate_max), 925 &priv->bitrate_max)) 926 ) 927 928 return -EMSGSIZE; 929 930 return 0; 931 } 932 933 static size_t can_get_xstats_size(const struct net_device *dev) 934 { 935 return sizeof(struct can_device_stats); 936 } 937 938 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) 939 { 940 struct can_priv *priv = netdev_priv(dev); 941 942 if (nla_put(skb, IFLA_INFO_XSTATS, 943 sizeof(priv->can_stats), &priv->can_stats)) 944 goto nla_put_failure; 945 return 0; 946 947 nla_put_failure: 948 return -EMSGSIZE; 949 } 950 951 static int can_newlink(struct net *src_net, struct net_device *dev, 952 struct nlattr *tb[], struct nlattr *data[], 953 struct netlink_ext_ack *extack) 954 { 955 return -EOPNOTSUPP; 956 } 957 958 static void can_dellink(struct net_device *dev, struct list_head *head) 959 { 960 } 961 962 static struct rtnl_link_ops can_link_ops __read_mostly = { 963 .kind = "can", 964 .maxtype = IFLA_CAN_MAX, 965 .policy = can_policy, 966 .setup = can_setup, 967 .validate = can_validate, 968 .newlink = can_newlink, 969 .changelink = can_changelink, 970 .dellink = can_dellink, 971 .get_size = can_get_size, 972 .fill_info = can_fill_info, 973 .get_xstats_size = can_get_xstats_size, 974 .fill_xstats = can_fill_xstats, 975 }; 976 977 /* Register the CAN network device */ 978 int register_candev(struct net_device *dev) 979 { 980 struct can_priv *priv = netdev_priv(dev); 981 982 /* Ensure termination_const, termination_const_cnt and 983 * do_set_termination consistency. All must be either set or 984 * unset. 985 */ 986 if ((!priv->termination_const != !priv->termination_const_cnt) || 987 (!priv->termination_const != !priv->do_set_termination)) 988 return -EINVAL; 989 990 if (!priv->bitrate_const != !priv->bitrate_const_cnt) 991 return -EINVAL; 992 993 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt) 994 return -EINVAL; 995 996 dev->rtnl_link_ops = &can_link_ops; 997 netif_carrier_off(dev); 998 999 return register_netdev(dev); 1000 } 1001 EXPORT_SYMBOL_GPL(register_candev); 1002 1003 /* Unregister the CAN network device */ 1004 void unregister_candev(struct net_device *dev) 1005 { 1006 unregister_netdev(dev); 1007 } 1008 EXPORT_SYMBOL_GPL(unregister_candev); 1009 1010 /* Test if a network device is a candev based device 1011 * and return the can_priv* if so. 1012 */ 1013 struct can_priv *safe_candev_priv(struct net_device *dev) 1014 { 1015 if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops) 1016 return NULL; 1017 1018 return netdev_priv(dev); 1019 } 1020 EXPORT_SYMBOL_GPL(safe_candev_priv); 1021 1022 static __init int can_dev_init(void) 1023 { 1024 int err; 1025 1026 can_led_notifier_init(); 1027 1028 err = rtnl_link_register(&can_link_ops); 1029 if (!err) 1030 pr_info(MOD_DESC "\n"); 1031 1032 return err; 1033 } 1034 module_init(can_dev_init); 1035 1036 static __exit void can_dev_exit(void) 1037 { 1038 rtnl_link_unregister(&can_link_ops); 1039 1040 can_led_notifier_exit(); 1041 } 1042 module_exit(can_dev_exit); 1043 1044 MODULE_ALIAS_RTNL_LINK("can"); 1045