Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
0003  * Copyright (C) 2006 Andrey Volkov, Varma Electronics
0004  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/slab.h>
0009 #include <linux/netdevice.h>
0010 #include <linux/if_arp.h>
0011 #include <linux/workqueue.h>
0012 #include <linux/can.h>
0013 #include <linux/can/can-ml.h>
0014 #include <linux/can/dev.h>
0015 #include <linux/can/skb.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/of.h>
0018 
0019 static void can_update_state_error_stats(struct net_device *dev,
0020                      enum can_state new_state)
0021 {
0022     struct can_priv *priv = netdev_priv(dev);
0023 
0024     if (new_state <= priv->state)
0025         return;
0026 
0027     switch (new_state) {
0028     case CAN_STATE_ERROR_WARNING:
0029         priv->can_stats.error_warning++;
0030         break;
0031     case CAN_STATE_ERROR_PASSIVE:
0032         priv->can_stats.error_passive++;
0033         break;
0034     case CAN_STATE_BUS_OFF:
0035         priv->can_stats.bus_off++;
0036         break;
0037     default:
0038         break;
0039     }
0040 }
0041 
0042 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
0043 {
0044     switch (state) {
0045     case CAN_STATE_ERROR_ACTIVE:
0046         return CAN_ERR_CRTL_ACTIVE;
0047     case CAN_STATE_ERROR_WARNING:
0048         return CAN_ERR_CRTL_TX_WARNING;
0049     case CAN_STATE_ERROR_PASSIVE:
0050         return CAN_ERR_CRTL_TX_PASSIVE;
0051     default:
0052         return 0;
0053     }
0054 }
0055 
0056 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
0057 {
0058     switch (state) {
0059     case CAN_STATE_ERROR_ACTIVE:
0060         return CAN_ERR_CRTL_ACTIVE;
0061     case CAN_STATE_ERROR_WARNING:
0062         return CAN_ERR_CRTL_RX_WARNING;
0063     case CAN_STATE_ERROR_PASSIVE:
0064         return CAN_ERR_CRTL_RX_PASSIVE;
0065     default:
0066         return 0;
0067     }
0068 }
0069 
0070 const char *can_get_state_str(const enum can_state state)
0071 {
0072     switch (state) {
0073     case CAN_STATE_ERROR_ACTIVE:
0074         return "Error Active";
0075     case CAN_STATE_ERROR_WARNING:
0076         return "Error Warning";
0077     case CAN_STATE_ERROR_PASSIVE:
0078         return "Error Passive";
0079     case CAN_STATE_BUS_OFF:
0080         return "Bus Off";
0081     case CAN_STATE_STOPPED:
0082         return "Stopped";
0083     case CAN_STATE_SLEEPING:
0084         return "Sleeping";
0085     default:
0086         return "<unknown>";
0087     }
0088 
0089     return "<unknown>";
0090 }
0091 EXPORT_SYMBOL_GPL(can_get_state_str);
0092 
0093 void can_change_state(struct net_device *dev, struct can_frame *cf,
0094               enum can_state tx_state, enum can_state rx_state)
0095 {
0096     struct can_priv *priv = netdev_priv(dev);
0097     enum can_state new_state = max(tx_state, rx_state);
0098 
0099     if (unlikely(new_state == priv->state)) {
0100         netdev_warn(dev, "%s: oops, state did not change", __func__);
0101         return;
0102     }
0103 
0104     netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
0105            can_get_state_str(priv->state), priv->state,
0106            can_get_state_str(new_state), new_state);
0107 
0108     can_update_state_error_stats(dev, new_state);
0109     priv->state = new_state;
0110 
0111     if (!cf)
0112         return;
0113 
0114     if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
0115         cf->can_id |= CAN_ERR_BUSOFF;
0116         return;
0117     }
0118 
0119     cf->can_id |= CAN_ERR_CRTL;
0120     cf->data[1] |= tx_state >= rx_state ?
0121                can_tx_state_to_frame(dev, tx_state) : 0;
0122     cf->data[1] |= tx_state <= rx_state ?
0123                can_rx_state_to_frame(dev, rx_state) : 0;
0124 }
0125 EXPORT_SYMBOL_GPL(can_change_state);
0126 
0127 /* CAN device restart for bus-off recovery */
0128 static void can_restart(struct net_device *dev)
0129 {
0130     struct can_priv *priv = netdev_priv(dev);
0131     struct sk_buff *skb;
0132     struct can_frame *cf;
0133     int err;
0134 
0135     BUG_ON(netif_carrier_ok(dev));
0136 
0137     /* No synchronization needed because the device is bus-off and
0138      * no messages can come in or go out.
0139      */
0140     can_flush_echo_skb(dev);
0141 
0142     /* send restart message upstream */
0143     skb = alloc_can_err_skb(dev, &cf);
0144     if (!skb)
0145         goto restart;
0146 
0147     cf->can_id |= CAN_ERR_RESTARTED;
0148 
0149     netif_rx(skb);
0150 
0151 restart:
0152     netdev_dbg(dev, "restarted\n");
0153     priv->can_stats.restarts++;
0154 
0155     /* Now restart the device */
0156     err = priv->do_set_mode(dev, CAN_MODE_START);
0157 
0158     netif_carrier_on(dev);
0159     if (err)
0160         netdev_err(dev, "Error %d during restart", err);
0161 }
0162 
0163 static void can_restart_work(struct work_struct *work)
0164 {
0165     struct delayed_work *dwork = to_delayed_work(work);
0166     struct can_priv *priv = container_of(dwork, struct can_priv,
0167                          restart_work);
0168 
0169     can_restart(priv->dev);
0170 }
0171 
0172 int can_restart_now(struct net_device *dev)
0173 {
0174     struct can_priv *priv = netdev_priv(dev);
0175 
0176     /* A manual restart is only permitted if automatic restart is
0177      * disabled and the device is in the bus-off state
0178      */
0179     if (priv->restart_ms)
0180         return -EINVAL;
0181     if (priv->state != CAN_STATE_BUS_OFF)
0182         return -EBUSY;
0183 
0184     cancel_delayed_work_sync(&priv->restart_work);
0185     can_restart(dev);
0186 
0187     return 0;
0188 }
0189 
0190 /* CAN bus-off
0191  *
0192  * This functions should be called when the device goes bus-off to
0193  * tell the netif layer that no more packets can be sent or received.
0194  * If enabled, a timer is started to trigger bus-off recovery.
0195  */
0196 void can_bus_off(struct net_device *dev)
0197 {
0198     struct can_priv *priv = netdev_priv(dev);
0199 
0200     if (priv->restart_ms)
0201         netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
0202                 priv->restart_ms);
0203     else
0204         netdev_info(dev, "bus-off\n");
0205 
0206     netif_carrier_off(dev);
0207 
0208     if (priv->restart_ms)
0209         schedule_delayed_work(&priv->restart_work,
0210                       msecs_to_jiffies(priv->restart_ms));
0211 }
0212 EXPORT_SYMBOL_GPL(can_bus_off);
0213 
0214 void can_setup(struct net_device *dev)
0215 {
0216     dev->type = ARPHRD_CAN;
0217     dev->mtu = CAN_MTU;
0218     dev->hard_header_len = 0;
0219     dev->addr_len = 0;
0220     dev->tx_queue_len = 10;
0221 
0222     /* New-style flags. */
0223     dev->flags = IFF_NOARP;
0224     dev->features = NETIF_F_HW_CSUM;
0225 }
0226 
0227 /* Allocate and setup space for the CAN network device */
0228 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
0229                     unsigned int txqs, unsigned int rxqs)
0230 {
0231     struct can_ml_priv *can_ml;
0232     struct net_device *dev;
0233     struct can_priv *priv;
0234     int size;
0235 
0236     /* We put the driver's priv, the CAN mid layer priv and the
0237      * echo skb into the netdevice's priv. The memory layout for
0238      * the netdev_priv is like this:
0239      *
0240      * +-------------------------+
0241      * | driver's priv           |
0242      * +-------------------------+
0243      * | struct can_ml_priv      |
0244      * +-------------------------+
0245      * | array of struct sk_buff |
0246      * +-------------------------+
0247      */
0248 
0249     size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
0250 
0251     if (echo_skb_max)
0252         size = ALIGN(size, sizeof(struct sk_buff *)) +
0253             echo_skb_max * sizeof(struct sk_buff *);
0254 
0255     dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
0256                    txqs, rxqs);
0257     if (!dev)
0258         return NULL;
0259 
0260     priv = netdev_priv(dev);
0261     priv->dev = dev;
0262 
0263     can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
0264     can_set_ml_priv(dev, can_ml);
0265 
0266     if (echo_skb_max) {
0267         priv->echo_skb_max = echo_skb_max;
0268         priv->echo_skb = (void *)priv +
0269             (size - echo_skb_max * sizeof(struct sk_buff *));
0270     }
0271 
0272     priv->state = CAN_STATE_STOPPED;
0273 
0274     INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
0275 
0276     return dev;
0277 }
0278 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
0279 
0280 /* Free space of the CAN network device */
0281 void free_candev(struct net_device *dev)
0282 {
0283     free_netdev(dev);
0284 }
0285 EXPORT_SYMBOL_GPL(free_candev);
0286 
0287 /* changing MTU and control mode for CAN/CANFD devices */
0288 int can_change_mtu(struct net_device *dev, int new_mtu)
0289 {
0290     struct can_priv *priv = netdev_priv(dev);
0291     u32 ctrlmode_static = can_get_static_ctrlmode(priv);
0292 
0293     /* Do not allow changing the MTU while running */
0294     if (dev->flags & IFF_UP)
0295         return -EBUSY;
0296 
0297     /* allow change of MTU according to the CANFD ability of the device */
0298     switch (new_mtu) {
0299     case CAN_MTU:
0300         /* 'CANFD-only' controllers can not switch to CAN_MTU */
0301         if (ctrlmode_static & CAN_CTRLMODE_FD)
0302             return -EINVAL;
0303 
0304         priv->ctrlmode &= ~CAN_CTRLMODE_FD;
0305         break;
0306 
0307     case CANFD_MTU:
0308         /* check for potential CANFD ability */
0309         if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
0310             !(ctrlmode_static & CAN_CTRLMODE_FD))
0311             return -EINVAL;
0312 
0313         priv->ctrlmode |= CAN_CTRLMODE_FD;
0314         break;
0315 
0316     default:
0317         return -EINVAL;
0318     }
0319 
0320     dev->mtu = new_mtu;
0321     return 0;
0322 }
0323 EXPORT_SYMBOL_GPL(can_change_mtu);
0324 
0325 /* generic implementation of netdev_ops::ndo_eth_ioctl for CAN devices
0326  * supporting hardware timestamps
0327  */
0328 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd)
0329 {
0330     struct hwtstamp_config hwts_cfg = { 0 };
0331 
0332     switch (cmd) {
0333     case SIOCSHWTSTAMP: /* set */
0334         if (copy_from_user(&hwts_cfg, ifr->ifr_data, sizeof(hwts_cfg)))
0335             return -EFAULT;
0336         if (hwts_cfg.tx_type == HWTSTAMP_TX_ON &&
0337             hwts_cfg.rx_filter == HWTSTAMP_FILTER_ALL)
0338             return 0;
0339         return -ERANGE;
0340 
0341     case SIOCGHWTSTAMP: /* get */
0342         hwts_cfg.tx_type = HWTSTAMP_TX_ON;
0343         hwts_cfg.rx_filter = HWTSTAMP_FILTER_ALL;
0344         if (copy_to_user(ifr->ifr_data, &hwts_cfg, sizeof(hwts_cfg)))
0345             return -EFAULT;
0346         return 0;
0347 
0348     default:
0349         return -EOPNOTSUPP;
0350     }
0351 }
0352 EXPORT_SYMBOL(can_eth_ioctl_hwts);
0353 
0354 /* generic implementation of ethtool_ops::get_ts_info for CAN devices
0355  * supporting hardware timestamps
0356  */
0357 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
0358                     struct ethtool_ts_info *info)
0359 {
0360     info->so_timestamping =
0361         SOF_TIMESTAMPING_TX_SOFTWARE |
0362         SOF_TIMESTAMPING_RX_SOFTWARE |
0363         SOF_TIMESTAMPING_SOFTWARE |
0364         SOF_TIMESTAMPING_TX_HARDWARE |
0365         SOF_TIMESTAMPING_RX_HARDWARE |
0366         SOF_TIMESTAMPING_RAW_HARDWARE;
0367     info->phc_index = -1;
0368     info->tx_types = BIT(HWTSTAMP_TX_ON);
0369     info->rx_filters = BIT(HWTSTAMP_FILTER_ALL);
0370 
0371     return 0;
0372 }
0373 EXPORT_SYMBOL(can_ethtool_op_get_ts_info_hwts);
0374 
0375 /* Common open function when the device gets opened.
0376  *
0377  * This function should be called in the open function of the device
0378  * driver.
0379  */
0380 int open_candev(struct net_device *dev)
0381 {
0382     struct can_priv *priv = netdev_priv(dev);
0383 
0384     if (!priv->bittiming.bitrate) {
0385         netdev_err(dev, "bit-timing not yet defined\n");
0386         return -EINVAL;
0387     }
0388 
0389     /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
0390     if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
0391         (!priv->data_bittiming.bitrate ||
0392          priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
0393         netdev_err(dev, "incorrect/missing data bit-timing\n");
0394         return -EINVAL;
0395     }
0396 
0397     /* Switch carrier on if device was stopped while in bus-off state */
0398     if (!netif_carrier_ok(dev))
0399         netif_carrier_on(dev);
0400 
0401     return 0;
0402 }
0403 EXPORT_SYMBOL_GPL(open_candev);
0404 
0405 #ifdef CONFIG_OF
0406 /* Common function that can be used to understand the limitation of
0407  * a transceiver when it provides no means to determine these limitations
0408  * at runtime.
0409  */
0410 void of_can_transceiver(struct net_device *dev)
0411 {
0412     struct device_node *dn;
0413     struct can_priv *priv = netdev_priv(dev);
0414     struct device_node *np = dev->dev.parent->of_node;
0415     int ret;
0416 
0417     dn = of_get_child_by_name(np, "can-transceiver");
0418     if (!dn)
0419         return;
0420 
0421     ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
0422     of_node_put(dn);
0423     if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
0424         netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
0425 }
0426 EXPORT_SYMBOL_GPL(of_can_transceiver);
0427 #endif
0428 
0429 /* Common close function for cleanup before the device gets closed.
0430  *
0431  * This function should be called in the close function of the device
0432  * driver.
0433  */
0434 void close_candev(struct net_device *dev)
0435 {
0436     struct can_priv *priv = netdev_priv(dev);
0437 
0438     cancel_delayed_work_sync(&priv->restart_work);
0439     can_flush_echo_skb(dev);
0440 }
0441 EXPORT_SYMBOL_GPL(close_candev);
0442 
0443 static int can_set_termination(struct net_device *ndev, u16 term)
0444 {
0445     struct can_priv *priv = netdev_priv(ndev);
0446     int set;
0447 
0448     if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
0449         set = 1;
0450     else
0451         set = 0;
0452 
0453     gpiod_set_value(priv->termination_gpio, set);
0454 
0455     return 0;
0456 }
0457 
0458 static int can_get_termination(struct net_device *ndev)
0459 {
0460     struct can_priv *priv = netdev_priv(ndev);
0461     struct device *dev = ndev->dev.parent;
0462     struct gpio_desc *gpio;
0463     u32 term;
0464     int ret;
0465 
0466     /* Disabling termination by default is the safe choice: Else if many
0467      * bus participants enable it, no communication is possible at all.
0468      */
0469     gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
0470     if (IS_ERR(gpio))
0471         return dev_err_probe(dev, PTR_ERR(gpio),
0472                      "Cannot get termination-gpios\n");
0473 
0474     if (!gpio)
0475         return 0;
0476 
0477     ret = device_property_read_u32(dev, "termination-ohms", &term);
0478     if (ret) {
0479         netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
0480                ERR_PTR(ret));
0481         return ret;
0482     }
0483 
0484     if (term > U16_MAX) {
0485         netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
0486                term, U16_MAX);
0487         return -EINVAL;
0488     }
0489 
0490     priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
0491     priv->termination_const = priv->termination_gpio_ohms;
0492     priv->termination_gpio = gpio;
0493     priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
0494         CAN_TERMINATION_DISABLED;
0495     priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
0496     priv->do_set_termination = can_set_termination;
0497 
0498     return 0;
0499 }
0500 
0501 /* Register the CAN network device */
0502 int register_candev(struct net_device *dev)
0503 {
0504     struct can_priv *priv = netdev_priv(dev);
0505     int err;
0506 
0507     /* Ensure termination_const, termination_const_cnt and
0508      * do_set_termination consistency. All must be either set or
0509      * unset.
0510      */
0511     if ((!priv->termination_const != !priv->termination_const_cnt) ||
0512         (!priv->termination_const != !priv->do_set_termination))
0513         return -EINVAL;
0514 
0515     if (!priv->bitrate_const != !priv->bitrate_const_cnt)
0516         return -EINVAL;
0517 
0518     if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
0519         return -EINVAL;
0520 
0521     if (!priv->termination_const) {
0522         err = can_get_termination(dev);
0523         if (err)
0524             return err;
0525     }
0526 
0527     dev->rtnl_link_ops = &can_link_ops;
0528     netif_carrier_off(dev);
0529 
0530     return register_netdev(dev);
0531 }
0532 EXPORT_SYMBOL_GPL(register_candev);
0533 
0534 /* Unregister the CAN network device */
0535 void unregister_candev(struct net_device *dev)
0536 {
0537     unregister_netdev(dev);
0538 }
0539 EXPORT_SYMBOL_GPL(unregister_candev);
0540 
0541 /* Test if a network device is a candev based device
0542  * and return the can_priv* if so.
0543  */
0544 struct can_priv *safe_candev_priv(struct net_device *dev)
0545 {
0546     if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
0547         return NULL;
0548 
0549     return netdev_priv(dev);
0550 }
0551 EXPORT_SYMBOL_GPL(safe_candev_priv);
0552 
0553 static __init int can_dev_init(void)
0554 {
0555     int err;
0556 
0557     err = can_netlink_register();
0558     if (!err)
0559         pr_info("CAN device driver interface\n");
0560 
0561     return err;
0562 }
0563 module_init(can_dev_init);
0564 
0565 static __exit void can_dev_exit(void)
0566 {
0567     can_netlink_unregister();
0568 }
0569 module_exit(can_dev_exit);
0570 
0571 MODULE_ALIAS_RTNL_LINK("can");