at v6.19-rc7 689 lines 17 kB view raw
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 */ 6 7#include <linux/can.h> 8#include <linux/can/can-ml.h> 9#include <linux/can/dev.h> 10#include <linux/can/skb.h> 11#include <linux/gpio/consumer.h> 12#include <linux/if_arp.h> 13#include <linux/kernel.h> 14#include <linux/netdevice.h> 15#include <linux/of.h> 16#include <linux/slab.h> 17#include <linux/workqueue.h> 18 19static void can_update_state_error_stats(struct net_device *dev, 20 enum can_state new_state) 21{ 22 struct can_priv *priv = netdev_priv(dev); 23 24 if (new_state <= priv->state) 25 return; 26 27 switch (new_state) { 28 case CAN_STATE_ERROR_WARNING: 29 priv->can_stats.error_warning++; 30 break; 31 case CAN_STATE_ERROR_PASSIVE: 32 priv->can_stats.error_passive++; 33 break; 34 case CAN_STATE_BUS_OFF: 35 priv->can_stats.bus_off++; 36 break; 37 default: 38 break; 39 } 40} 41 42static int can_tx_state_to_frame(struct net_device *dev, enum can_state state) 43{ 44 switch (state) { 45 case CAN_STATE_ERROR_ACTIVE: 46 return CAN_ERR_CRTL_ACTIVE; 47 case CAN_STATE_ERROR_WARNING: 48 return CAN_ERR_CRTL_TX_WARNING; 49 case CAN_STATE_ERROR_PASSIVE: 50 return CAN_ERR_CRTL_TX_PASSIVE; 51 default: 52 return 0; 53 } 54} 55 56static int can_rx_state_to_frame(struct net_device *dev, enum can_state state) 57{ 58 switch (state) { 59 case CAN_STATE_ERROR_ACTIVE: 60 return CAN_ERR_CRTL_ACTIVE; 61 case CAN_STATE_ERROR_WARNING: 62 return CAN_ERR_CRTL_RX_WARNING; 63 case CAN_STATE_ERROR_PASSIVE: 64 return CAN_ERR_CRTL_RX_PASSIVE; 65 default: 66 return 0; 67 } 68} 69 70const char *can_get_state_str(const enum can_state state) 71{ 72 switch (state) { 73 case CAN_STATE_ERROR_ACTIVE: 74 return "Error Active"; 75 case CAN_STATE_ERROR_WARNING: 76 return "Error Warning"; 77 case CAN_STATE_ERROR_PASSIVE: 78 return "Error Passive"; 79 case CAN_STATE_BUS_OFF: 80 return "Bus Off"; 81 case CAN_STATE_STOPPED: 82 return "Stopped"; 83 case CAN_STATE_SLEEPING: 84 return "Sleeping"; 85 default: 86 return "<unknown>"; 87 } 88} 89EXPORT_SYMBOL_GPL(can_get_state_str); 90 91const char *can_get_ctrlmode_str(u32 ctrlmode) 92{ 93 switch (ctrlmode & ~(ctrlmode - 1)) { 94 case 0: 95 return "(none)"; 96 case CAN_CTRLMODE_LOOPBACK: 97 return "LOOPBACK"; 98 case CAN_CTRLMODE_LISTENONLY: 99 return "LISTEN-ONLY"; 100 case CAN_CTRLMODE_3_SAMPLES: 101 return "TRIPLE-SAMPLING"; 102 case CAN_CTRLMODE_ONE_SHOT: 103 return "ONE-SHOT"; 104 case CAN_CTRLMODE_BERR_REPORTING: 105 return "BERR-REPORTING"; 106 case CAN_CTRLMODE_FD: 107 return "FD"; 108 case CAN_CTRLMODE_PRESUME_ACK: 109 return "PRESUME-ACK"; 110 case CAN_CTRLMODE_FD_NON_ISO: 111 return "FD-NON-ISO"; 112 case CAN_CTRLMODE_CC_LEN8_DLC: 113 return "CC-LEN8-DLC"; 114 case CAN_CTRLMODE_TDC_AUTO: 115 return "TDC-AUTO"; 116 case CAN_CTRLMODE_TDC_MANUAL: 117 return "TDC-MANUAL"; 118 case CAN_CTRLMODE_RESTRICTED: 119 return "RESTRICTED"; 120 case CAN_CTRLMODE_XL: 121 return "XL"; 122 case CAN_CTRLMODE_XL_TDC_AUTO: 123 return "XL-TDC-AUTO"; 124 case CAN_CTRLMODE_XL_TDC_MANUAL: 125 return "XL-TDC-MANUAL"; 126 case CAN_CTRLMODE_XL_TMS: 127 return "TMS"; 128 default: 129 return "<unknown>"; 130 } 131} 132EXPORT_SYMBOL_GPL(can_get_ctrlmode_str); 133 134static enum can_state can_state_err_to_state(u16 err) 135{ 136 if (err < CAN_ERROR_WARNING_THRESHOLD) 137 return CAN_STATE_ERROR_ACTIVE; 138 if (err < CAN_ERROR_PASSIVE_THRESHOLD) 139 return CAN_STATE_ERROR_WARNING; 140 if (err < CAN_BUS_OFF_THRESHOLD) 141 return CAN_STATE_ERROR_PASSIVE; 142 143 return CAN_STATE_BUS_OFF; 144} 145 146void can_state_get_by_berr_counter(const struct net_device *dev, 147 const struct can_berr_counter *bec, 148 enum can_state *tx_state, 149 enum can_state *rx_state) 150{ 151 *tx_state = can_state_err_to_state(bec->txerr); 152 *rx_state = can_state_err_to_state(bec->rxerr); 153} 154EXPORT_SYMBOL_GPL(can_state_get_by_berr_counter); 155 156void can_change_state(struct net_device *dev, struct can_frame *cf, 157 enum can_state tx_state, enum can_state rx_state) 158{ 159 struct can_priv *priv = netdev_priv(dev); 160 enum can_state new_state = max(tx_state, rx_state); 161 162 if (unlikely(new_state == priv->state)) { 163 netdev_warn(dev, "%s: oops, state did not change", __func__); 164 return; 165 } 166 167 netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n", 168 can_get_state_str(priv->state), priv->state, 169 can_get_state_str(new_state), new_state); 170 171 can_update_state_error_stats(dev, new_state); 172 priv->state = new_state; 173 174 if (!cf) 175 return; 176 177 if (unlikely(new_state == CAN_STATE_BUS_OFF)) { 178 cf->can_id |= CAN_ERR_BUSOFF; 179 return; 180 } 181 182 cf->can_id |= CAN_ERR_CRTL; 183 cf->data[1] |= tx_state >= rx_state ? 184 can_tx_state_to_frame(dev, tx_state) : 0; 185 cf->data[1] |= tx_state <= rx_state ? 186 can_rx_state_to_frame(dev, rx_state) : 0; 187} 188EXPORT_SYMBOL_GPL(can_change_state); 189 190/* CAN device restart for bus-off recovery */ 191static int can_restart(struct net_device *dev) 192{ 193 struct can_priv *priv = netdev_priv(dev); 194 struct sk_buff *skb; 195 struct can_frame *cf; 196 int err; 197 198 if (!priv->do_set_mode) 199 return -EOPNOTSUPP; 200 201 if (netif_carrier_ok(dev)) 202 netdev_err(dev, "Attempt to restart for bus-off recovery, but carrier is OK?\n"); 203 204 /* No synchronization needed because the device is bus-off and 205 * no messages can come in or go out. 206 */ 207 can_flush_echo_skb(dev); 208 209 /* send restart message upstream */ 210 skb = alloc_can_err_skb(dev, &cf); 211 if (skb) { 212 cf->can_id |= CAN_ERR_RESTARTED; 213 netif_rx(skb); 214 } 215 216 /* Now restart the device */ 217 netif_carrier_on(dev); 218 err = priv->do_set_mode(dev, CAN_MODE_START); 219 if (err) { 220 netdev_err(dev, "Restart failed, error %pe\n", ERR_PTR(err)); 221 netif_carrier_off(dev); 222 223 return err; 224 } else { 225 netdev_dbg(dev, "Restarted\n"); 226 priv->can_stats.restarts++; 227 } 228 229 return 0; 230} 231 232static void can_restart_work(struct work_struct *work) 233{ 234 struct delayed_work *dwork = to_delayed_work(work); 235 struct can_priv *priv = container_of(dwork, struct can_priv, 236 restart_work); 237 238 can_restart(priv->dev); 239} 240 241int can_restart_now(struct net_device *dev) 242{ 243 struct can_priv *priv = netdev_priv(dev); 244 245 /* A manual restart is only permitted if automatic restart is 246 * disabled and the device is in the bus-off state 247 */ 248 if (priv->restart_ms) 249 return -EINVAL; 250 if (priv->state != CAN_STATE_BUS_OFF) 251 return -EBUSY; 252 253 cancel_delayed_work_sync(&priv->restart_work); 254 255 return can_restart(dev); 256} 257 258/* CAN bus-off 259 * 260 * This functions should be called when the device goes bus-off to 261 * tell the netif layer that no more packets can be sent or received. 262 * If enabled, a timer is started to trigger bus-off recovery. 263 */ 264void can_bus_off(struct net_device *dev) 265{ 266 struct can_priv *priv = netdev_priv(dev); 267 268 if (priv->restart_ms) 269 netdev_info(dev, "bus-off, scheduling restart in %d ms\n", 270 priv->restart_ms); 271 else 272 netdev_info(dev, "bus-off\n"); 273 274 netif_carrier_off(dev); 275 276 if (priv->restart_ms) 277 schedule_delayed_work(&priv->restart_work, 278 msecs_to_jiffies(priv->restart_ms)); 279} 280EXPORT_SYMBOL_GPL(can_bus_off); 281 282void can_setup(struct net_device *dev) 283{ 284 dev->type = ARPHRD_CAN; 285 dev->mtu = CAN_MTU; 286 dev->min_mtu = CAN_MTU; 287 dev->max_mtu = CAN_MTU; 288 dev->hard_header_len = 0; 289 dev->addr_len = 0; 290 dev->tx_queue_len = 10; 291 292 /* New-style flags. */ 293 dev->flags = IFF_NOARP; 294 dev->features = NETIF_F_HW_CSUM; 295} 296 297/* Allocate and setup space for the CAN network device */ 298struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, 299 unsigned int txqs, unsigned int rxqs) 300{ 301 struct can_ml_priv *can_ml; 302 struct net_device *dev; 303 struct can_priv *priv; 304 int size; 305 306 /* We put the driver's priv, the CAN mid layer priv and the 307 * echo skb into the netdevice's priv. The memory layout for 308 * the netdev_priv is like this: 309 * 310 * +-------------------------+ 311 * | driver's priv | 312 * +-------------------------+ 313 * | struct can_ml_priv | 314 * +-------------------------+ 315 * | array of struct sk_buff | 316 * +-------------------------+ 317 */ 318 319 size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv); 320 321 if (echo_skb_max) 322 size = ALIGN(size, sizeof(struct sk_buff *)) + 323 echo_skb_max * sizeof(struct sk_buff *); 324 325 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup, 326 txqs, rxqs); 327 if (!dev) 328 return NULL; 329 330 priv = netdev_priv(dev); 331 priv->dev = dev; 332 333 can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN); 334 can_set_ml_priv(dev, can_ml); 335 can_set_cap(dev, CAN_CAP_CC); 336 337 if (echo_skb_max) { 338 priv->echo_skb_max = echo_skb_max; 339 priv->echo_skb = (void *)priv + 340 (size - echo_skb_max * sizeof(struct sk_buff *)); 341 } 342 343 priv->state = CAN_STATE_STOPPED; 344 345 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work); 346 347 return dev; 348} 349EXPORT_SYMBOL_GPL(alloc_candev_mqs); 350 351/* Free space of the CAN network device */ 352void free_candev(struct net_device *dev) 353{ 354 free_netdev(dev); 355} 356EXPORT_SYMBOL_GPL(free_candev); 357 358void can_set_default_mtu(struct net_device *dev) 359{ 360 struct can_priv *priv = netdev_priv(dev); 361 362 if (priv->ctrlmode & CAN_CTRLMODE_XL) { 363 if (can_is_canxl_dev_mtu(dev->mtu)) 364 return; 365 dev->mtu = CANXL_MTU; 366 dev->min_mtu = CANXL_MIN_MTU; 367 dev->max_mtu = CANXL_MAX_MTU; 368 } else if (priv->ctrlmode & CAN_CTRLMODE_FD) { 369 dev->mtu = CANFD_MTU; 370 dev->min_mtu = CANFD_MTU; 371 dev->max_mtu = CANFD_MTU; 372 } else { 373 dev->mtu = CAN_MTU; 374 dev->min_mtu = CAN_MTU; 375 dev->max_mtu = CAN_MTU; 376 } 377} 378 379void can_set_cap_info(struct net_device *dev) 380{ 381 struct can_priv *priv = netdev_priv(dev); 382 u32 can_cap; 383 384 if (can_dev_in_xl_only_mode(priv)) { 385 /* XL only mode => no CC/FD capability */ 386 can_cap = CAN_CAP_XL; 387 } else { 388 /* mixed mode => CC + FD/XL capability */ 389 can_cap = CAN_CAP_CC; 390 391 if (priv->ctrlmode & CAN_CTRLMODE_FD) 392 can_cap |= CAN_CAP_FD; 393 394 if (priv->ctrlmode & CAN_CTRLMODE_XL) 395 can_cap |= CAN_CAP_XL; 396 } 397 398 if (priv->ctrlmode & (CAN_CTRLMODE_LISTENONLY | 399 CAN_CTRLMODE_RESTRICTED)) 400 can_cap |= CAN_CAP_RO; 401 402 can_set_cap(dev, can_cap); 403} 404 405/* helper to define static CAN controller features at device creation time */ 406int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode) 407{ 408 struct can_priv *priv = netdev_priv(dev); 409 410 /* alloc_candev() succeeded => netdev_priv() is valid at this point */ 411 if (priv->ctrlmode_supported & static_mode) { 412 netdev_warn(dev, 413 "Controller features can not be supported and static at the same time\n"); 414 return -EINVAL; 415 } 416 priv->ctrlmode = static_mode; 417 418 /* override MTU which was set by default in can_setup()? */ 419 can_set_default_mtu(dev); 420 can_set_cap_info(dev); 421 422 return 0; 423} 424EXPORT_SYMBOL_GPL(can_set_static_ctrlmode); 425 426/* generic implementation of netdev_ops::ndo_hwtstamp_get for CAN devices 427 * supporting hardware timestamps 428 */ 429int can_hwtstamp_get(struct net_device *netdev, 430 struct kernel_hwtstamp_config *cfg) 431{ 432 cfg->tx_type = HWTSTAMP_TX_ON; 433 cfg->rx_filter = HWTSTAMP_FILTER_ALL; 434 435 return 0; 436} 437EXPORT_SYMBOL(can_hwtstamp_get); 438 439/* generic implementation of netdev_ops::ndo_hwtstamp_set for CAN devices 440 * supporting hardware timestamps 441 */ 442int can_hwtstamp_set(struct net_device *netdev, 443 struct kernel_hwtstamp_config *cfg, 444 struct netlink_ext_ack *extack) 445{ 446 if (cfg->tx_type == HWTSTAMP_TX_ON && 447 cfg->rx_filter == HWTSTAMP_FILTER_ALL) 448 return 0; 449 NL_SET_ERR_MSG_MOD(extack, "Only TX on and RX all packets filter supported"); 450 return -ERANGE; 451} 452EXPORT_SYMBOL(can_hwtstamp_set); 453 454/* generic implementation of ethtool_ops::get_ts_info for CAN devices 455 * supporting hardware timestamps 456 */ 457int can_ethtool_op_get_ts_info_hwts(struct net_device *dev, 458 struct kernel_ethtool_ts_info *info) 459{ 460 info->so_timestamping = 461 SOF_TIMESTAMPING_TX_SOFTWARE | 462 SOF_TIMESTAMPING_TX_HARDWARE | 463 SOF_TIMESTAMPING_RX_HARDWARE | 464 SOF_TIMESTAMPING_RAW_HARDWARE; 465 info->tx_types = BIT(HWTSTAMP_TX_ON); 466 info->rx_filters = BIT(HWTSTAMP_FILTER_ALL); 467 468 return 0; 469} 470EXPORT_SYMBOL(can_ethtool_op_get_ts_info_hwts); 471 472/* Common open function when the device gets opened. 473 * 474 * This function should be called in the open function of the device 475 * driver. 476 */ 477int open_candev(struct net_device *dev) 478{ 479 struct can_priv *priv = netdev_priv(dev); 480 481 if (!priv->bittiming.bitrate) { 482 netdev_err(dev, "bit-timing not yet defined\n"); 483 return -EINVAL; 484 } 485 486 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */ 487 if ((priv->ctrlmode & CAN_CTRLMODE_FD) && 488 (!priv->fd.data_bittiming.bitrate || 489 priv->fd.data_bittiming.bitrate < priv->bittiming.bitrate)) { 490 netdev_err(dev, "incorrect/missing data bit-timing\n"); 491 return -EINVAL; 492 } 493 494 /* Switch carrier on if device was stopped while in bus-off state */ 495 if (!netif_carrier_ok(dev)) 496 netif_carrier_on(dev); 497 498 return 0; 499} 500EXPORT_SYMBOL_GPL(open_candev); 501 502#ifdef CONFIG_OF 503/* Common function that can be used to understand the limitation of 504 * a transceiver when it provides no means to determine these limitations 505 * at runtime. 506 */ 507void of_can_transceiver(struct net_device *dev) 508{ 509 struct device_node *dn; 510 struct can_priv *priv = netdev_priv(dev); 511 struct device_node *np = dev->dev.parent->of_node; 512 int ret; 513 514 dn = of_get_child_by_name(np, "can-transceiver"); 515 if (!dn) 516 return; 517 518 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max); 519 of_node_put(dn); 520 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max)) 521 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n"); 522} 523EXPORT_SYMBOL_GPL(of_can_transceiver); 524#endif 525 526/* Common close function for cleanup before the device gets closed. 527 * 528 * This function should be called in the close function of the device 529 * driver. 530 */ 531void close_candev(struct net_device *dev) 532{ 533 struct can_priv *priv = netdev_priv(dev); 534 535 cancel_delayed_work_sync(&priv->restart_work); 536 can_flush_echo_skb(dev); 537} 538EXPORT_SYMBOL_GPL(close_candev); 539 540static int can_set_termination(struct net_device *ndev, u16 term) 541{ 542 struct can_priv *priv = netdev_priv(ndev); 543 int set; 544 545 if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED]) 546 set = 1; 547 else 548 set = 0; 549 550 gpiod_set_value_cansleep(priv->termination_gpio, set); 551 552 return 0; 553} 554 555static int can_get_termination(struct net_device *ndev) 556{ 557 struct can_priv *priv = netdev_priv(ndev); 558 struct device *dev = ndev->dev.parent; 559 struct gpio_desc *gpio; 560 u32 term; 561 int ret; 562 563 /* Disabling termination by default is the safe choice: Else if many 564 * bus participants enable it, no communication is possible at all. 565 */ 566 gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW); 567 if (IS_ERR(gpio)) 568 return dev_err_probe(dev, PTR_ERR(gpio), 569 "Cannot get termination-gpios\n"); 570 571 if (!gpio) 572 return 0; 573 574 ret = device_property_read_u32(dev, "termination-ohms", &term); 575 if (ret) { 576 netdev_err(ndev, "Cannot get termination-ohms: %pe\n", 577 ERR_PTR(ret)); 578 return ret; 579 } 580 581 if (term > U16_MAX) { 582 netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n", 583 term, U16_MAX); 584 return -EINVAL; 585 } 586 587 priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms); 588 priv->termination_const = priv->termination_gpio_ohms; 589 priv->termination_gpio = gpio; 590 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] = 591 CAN_TERMINATION_DISABLED; 592 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term; 593 priv->do_set_termination = can_set_termination; 594 595 return 0; 596} 597 598static bool 599can_bittiming_const_valid(const struct can_bittiming_const *btc) 600{ 601 if (!btc) 602 return true; 603 604 if (!btc->sjw_max) 605 return false; 606 607 return true; 608} 609 610/* Register the CAN network device */ 611int register_candev(struct net_device *dev) 612{ 613 struct can_priv *priv = netdev_priv(dev); 614 int err; 615 616 /* Ensure termination_const, termination_const_cnt and 617 * do_set_termination consistency. All must be either set or 618 * unset. 619 */ 620 if ((!priv->termination_const != !priv->termination_const_cnt) || 621 (!priv->termination_const != !priv->do_set_termination)) 622 return -EINVAL; 623 624 if (!priv->bitrate_const != !priv->bitrate_const_cnt) 625 return -EINVAL; 626 627 if (!priv->fd.data_bitrate_const != !priv->fd.data_bitrate_const_cnt) 628 return -EINVAL; 629 630 /* We only support either fixed bit rates or bit timing const. */ 631 if ((priv->bitrate_const || priv->fd.data_bitrate_const) && 632 (priv->bittiming_const || priv->fd.data_bittiming_const)) 633 return -EINVAL; 634 635 if (!can_bittiming_const_valid(priv->bittiming_const) || 636 !can_bittiming_const_valid(priv->fd.data_bittiming_const)) 637 return -EINVAL; 638 639 if (!priv->termination_const) { 640 err = can_get_termination(dev); 641 if (err) 642 return err; 643 } 644 645 dev->rtnl_link_ops = &can_link_ops; 646 netif_carrier_off(dev); 647 648 return register_netdev(dev); 649} 650EXPORT_SYMBOL_GPL(register_candev); 651 652/* Unregister the CAN network device */ 653void unregister_candev(struct net_device *dev) 654{ 655 unregister_netdev(dev); 656} 657EXPORT_SYMBOL_GPL(unregister_candev); 658 659/* Test if a network device is a candev based device 660 * and return the can_priv* if so. 661 */ 662struct can_priv *safe_candev_priv(struct net_device *dev) 663{ 664 if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops) 665 return NULL; 666 667 return netdev_priv(dev); 668} 669EXPORT_SYMBOL_GPL(safe_candev_priv); 670 671static __init int can_dev_init(void) 672{ 673 int err; 674 675 err = can_netlink_register(); 676 if (!err) 677 pr_info("CAN device driver interface\n"); 678 679 return err; 680} 681module_init(can_dev_init); 682 683static __exit void can_dev_exit(void) 684{ 685 can_netlink_unregister(); 686} 687module_exit(can_dev_exit); 688 689MODULE_ALIAS_RTNL_LINK("can");