dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/if_arp.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/can.h>
  12. #include <linux/can/can-ml.h>
  13. #include <linux/can/dev.h>
  14. #include <linux/can/skb.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/of.h>
  17. static void can_update_state_error_stats(struct net_device *dev,
  18. enum can_state new_state)
  19. {
  20. struct can_priv *priv = netdev_priv(dev);
  21. if (new_state <= priv->state)
  22. return;
  23. switch (new_state) {
  24. case CAN_STATE_ERROR_WARNING:
  25. priv->can_stats.error_warning++;
  26. break;
  27. case CAN_STATE_ERROR_PASSIVE:
  28. priv->can_stats.error_passive++;
  29. break;
  30. case CAN_STATE_BUS_OFF:
  31. priv->can_stats.bus_off++;
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
  38. {
  39. switch (state) {
  40. case CAN_STATE_ERROR_ACTIVE:
  41. return CAN_ERR_CRTL_ACTIVE;
  42. case CAN_STATE_ERROR_WARNING:
  43. return CAN_ERR_CRTL_TX_WARNING;
  44. case CAN_STATE_ERROR_PASSIVE:
  45. return CAN_ERR_CRTL_TX_PASSIVE;
  46. default:
  47. return 0;
  48. }
  49. }
  50. static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
  51. {
  52. switch (state) {
  53. case CAN_STATE_ERROR_ACTIVE:
  54. return CAN_ERR_CRTL_ACTIVE;
  55. case CAN_STATE_ERROR_WARNING:
  56. return CAN_ERR_CRTL_RX_WARNING;
  57. case CAN_STATE_ERROR_PASSIVE:
  58. return CAN_ERR_CRTL_RX_PASSIVE;
  59. default:
  60. return 0;
  61. }
  62. }
  63. const char *can_get_state_str(const enum can_state state)
  64. {
  65. switch (state) {
  66. case CAN_STATE_ERROR_ACTIVE:
  67. return "Error Active";
  68. case CAN_STATE_ERROR_WARNING:
  69. return "Error Warning";
  70. case CAN_STATE_ERROR_PASSIVE:
  71. return "Error Passive";
  72. case CAN_STATE_BUS_OFF:
  73. return "Bus Off";
  74. case CAN_STATE_STOPPED:
  75. return "Stopped";
  76. case CAN_STATE_SLEEPING:
  77. return "Sleeping";
  78. default:
  79. return "<unknown>";
  80. }
  81. return "<unknown>";
  82. }
  83. EXPORT_SYMBOL_GPL(can_get_state_str);
  84. void can_change_state(struct net_device *dev, struct can_frame *cf,
  85. enum can_state tx_state, enum can_state rx_state)
  86. {
  87. struct can_priv *priv = netdev_priv(dev);
  88. enum can_state new_state = max(tx_state, rx_state);
  89. if (unlikely(new_state == priv->state)) {
  90. netdev_warn(dev, "%s: oops, state did not change", __func__);
  91. return;
  92. }
  93. netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
  94. can_get_state_str(priv->state), priv->state,
  95. can_get_state_str(new_state), new_state);
  96. can_update_state_error_stats(dev, new_state);
  97. priv->state = new_state;
  98. if (!cf)
  99. return;
  100. if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  101. cf->can_id |= CAN_ERR_BUSOFF;
  102. return;
  103. }
  104. cf->can_id |= CAN_ERR_CRTL;
  105. cf->data[1] |= tx_state >= rx_state ?
  106. can_tx_state_to_frame(dev, tx_state) : 0;
  107. cf->data[1] |= tx_state <= rx_state ?
  108. can_rx_state_to_frame(dev, rx_state) : 0;
  109. }
  110. EXPORT_SYMBOL_GPL(can_change_state);
  111. /* CAN device restart for bus-off recovery */
  112. static void can_restart(struct net_device *dev)
  113. {
  114. struct can_priv *priv = netdev_priv(dev);
  115. struct sk_buff *skb;
  116. struct can_frame *cf;
  117. int err;
  118. if (netif_carrier_ok(dev))
  119. netdev_err(dev, "Attempt to restart for bus-off recovery, but carrier is OK?\n");
  120. /* No synchronization needed because the device is bus-off and
  121. * no messages can come in or go out.
  122. */
  123. can_flush_echo_skb(dev);
  124. /* send restart message upstream */
  125. skb = alloc_can_err_skb(dev, &cf);
  126. if (!skb)
  127. goto restart;
  128. cf->can_id |= CAN_ERR_RESTARTED;
  129. netif_rx(skb);
  130. restart:
  131. netdev_dbg(dev, "restarted\n");
  132. priv->can_stats.restarts++;
  133. /* Now restart the device */
  134. netif_carrier_on(dev);
  135. err = priv->do_set_mode(dev, CAN_MODE_START);
  136. if (err) {
  137. netdev_err(dev, "Error %d during restart", err);
  138. netif_carrier_off(dev);
  139. }
  140. }
  141. static void can_restart_work(struct work_struct *work)
  142. {
  143. struct delayed_work *dwork = to_delayed_work(work);
  144. struct can_priv *priv = container_of(dwork, struct can_priv,
  145. restart_work);
  146. can_restart(priv->dev);
  147. }
  148. int can_restart_now(struct net_device *dev)
  149. {
  150. struct can_priv *priv = netdev_priv(dev);
  151. /* A manual restart is only permitted if automatic restart is
  152. * disabled and the device is in the bus-off state
  153. */
  154. if (priv->restart_ms)
  155. return -EINVAL;
  156. if (priv->state != CAN_STATE_BUS_OFF)
  157. return -EBUSY;
  158. cancel_delayed_work_sync(&priv->restart_work);
  159. can_restart(dev);
  160. return 0;
  161. }
  162. /* CAN bus-off
  163. *
  164. * This functions should be called when the device goes bus-off to
  165. * tell the netif layer that no more packets can be sent or received.
  166. * If enabled, a timer is started to trigger bus-off recovery.
  167. */
  168. void can_bus_off(struct net_device *dev)
  169. {
  170. struct can_priv *priv = netdev_priv(dev);
  171. if (priv->restart_ms)
  172. netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
  173. priv->restart_ms);
  174. else
  175. netdev_info(dev, "bus-off\n");
  176. netif_carrier_off(dev);
  177. if (priv->restart_ms)
  178. schedule_delayed_work(&priv->restart_work,
  179. msecs_to_jiffies(priv->restart_ms));
  180. }
  181. EXPORT_SYMBOL_GPL(can_bus_off);
  182. void can_setup(struct net_device *dev)
  183. {
  184. dev->type = ARPHRD_CAN;
  185. dev->mtu = CAN_MTU;
  186. dev->hard_header_len = 0;
  187. dev->addr_len = 0;
  188. dev->tx_queue_len = 10;
  189. /* New-style flags. */
  190. dev->flags = IFF_NOARP;
  191. dev->features = NETIF_F_HW_CSUM;
  192. }
  193. /* Allocate and setup space for the CAN network device */
  194. struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  195. unsigned int txqs, unsigned int rxqs)
  196. {
  197. struct can_ml_priv *can_ml;
  198. struct net_device *dev;
  199. struct can_priv *priv;
  200. int size;
  201. /* We put the driver's priv, the CAN mid layer priv and the
  202. * echo skb into the netdevice's priv. The memory layout for
  203. * the netdev_priv is like this:
  204. *
  205. * +-------------------------+
  206. * | driver's priv |
  207. * +-------------------------+
  208. * | struct can_ml_priv |
  209. * +-------------------------+
  210. * | array of struct sk_buff |
  211. * +-------------------------+
  212. */
  213. size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
  214. if (echo_skb_max)
  215. size = ALIGN(size, sizeof(struct sk_buff *)) +
  216. echo_skb_max * sizeof(struct sk_buff *);
  217. dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
  218. txqs, rxqs);
  219. if (!dev)
  220. return NULL;
  221. priv = netdev_priv(dev);
  222. priv->dev = dev;
  223. can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
  224. can_set_ml_priv(dev, can_ml);
  225. if (echo_skb_max) {
  226. priv->echo_skb_max = echo_skb_max;
  227. priv->echo_skb = (void *)priv +
  228. (size - echo_skb_max * sizeof(struct sk_buff *));
  229. }
  230. priv->state = CAN_STATE_STOPPED;
  231. INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
  232. return dev;
  233. }
  234. EXPORT_SYMBOL_GPL(alloc_candev_mqs);
  235. /* Free space of the CAN network device */
  236. void free_candev(struct net_device *dev)
  237. {
  238. free_netdev(dev);
  239. }
  240. EXPORT_SYMBOL_GPL(free_candev);
  241. /* changing MTU and control mode for CAN/CANFD devices */
  242. int can_change_mtu(struct net_device *dev, int new_mtu)
  243. {
  244. struct can_priv *priv = netdev_priv(dev);
  245. u32 ctrlmode_static = can_get_static_ctrlmode(priv);
  246. /* Do not allow changing the MTU while running */
  247. if (dev->flags & IFF_UP)
  248. return -EBUSY;
  249. /* allow change of MTU according to the CANFD ability of the device */
  250. switch (new_mtu) {
  251. case CAN_MTU:
  252. /* 'CANFD-only' controllers can not switch to CAN_MTU */
  253. if (ctrlmode_static & CAN_CTRLMODE_FD)
  254. return -EINVAL;
  255. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  256. break;
  257. case CANFD_MTU:
  258. /* check for potential CANFD ability */
  259. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
  260. !(ctrlmode_static & CAN_CTRLMODE_FD))
  261. return -EINVAL;
  262. priv->ctrlmode |= CAN_CTRLMODE_FD;
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. dev->mtu = new_mtu;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL_GPL(can_change_mtu);
  271. /* generic implementation of netdev_ops::ndo_eth_ioctl for CAN devices
  272. * supporting hardware timestamps
  273. */
  274. int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd)
  275. {
  276. struct hwtstamp_config hwts_cfg = { 0 };
  277. switch (cmd) {
  278. case SIOCSHWTSTAMP: /* set */
  279. if (copy_from_user(&hwts_cfg, ifr->ifr_data, sizeof(hwts_cfg)))
  280. return -EFAULT;
  281. if (hwts_cfg.tx_type == HWTSTAMP_TX_ON &&
  282. hwts_cfg.rx_filter == HWTSTAMP_FILTER_ALL)
  283. return 0;
  284. return -ERANGE;
  285. case SIOCGHWTSTAMP: /* get */
  286. hwts_cfg.tx_type = HWTSTAMP_TX_ON;
  287. hwts_cfg.rx_filter = HWTSTAMP_FILTER_ALL;
  288. if (copy_to_user(ifr->ifr_data, &hwts_cfg, sizeof(hwts_cfg)))
  289. return -EFAULT;
  290. return 0;
  291. default:
  292. return -EOPNOTSUPP;
  293. }
  294. }
  295. EXPORT_SYMBOL(can_eth_ioctl_hwts);
  296. /* generic implementation of ethtool_ops::get_ts_info for CAN devices
  297. * supporting hardware timestamps
  298. */
  299. int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
  300. struct ethtool_ts_info *info)
  301. {
  302. info->so_timestamping =
  303. SOF_TIMESTAMPING_TX_SOFTWARE |
  304. SOF_TIMESTAMPING_RX_SOFTWARE |
  305. SOF_TIMESTAMPING_SOFTWARE |
  306. SOF_TIMESTAMPING_TX_HARDWARE |
  307. SOF_TIMESTAMPING_RX_HARDWARE |
  308. SOF_TIMESTAMPING_RAW_HARDWARE;
  309. info->phc_index = -1;
  310. info->tx_types = BIT(HWTSTAMP_TX_ON);
  311. info->rx_filters = BIT(HWTSTAMP_FILTER_ALL);
  312. return 0;
  313. }
  314. EXPORT_SYMBOL(can_ethtool_op_get_ts_info_hwts);
  315. /* Common open function when the device gets opened.
  316. *
  317. * This function should be called in the open function of the device
  318. * driver.
  319. */
  320. int open_candev(struct net_device *dev)
  321. {
  322. struct can_priv *priv = netdev_priv(dev);
  323. if (!priv->bittiming.bitrate) {
  324. netdev_err(dev, "bit-timing not yet defined\n");
  325. return -EINVAL;
  326. }
  327. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  328. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  329. (!priv->data_bittiming.bitrate ||
  330. priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
  331. netdev_err(dev, "incorrect/missing data bit-timing\n");
  332. return -EINVAL;
  333. }
  334. /* Switch carrier on if device was stopped while in bus-off state */
  335. if (!netif_carrier_ok(dev))
  336. netif_carrier_on(dev);
  337. return 0;
  338. }
  339. EXPORT_SYMBOL_GPL(open_candev);
  340. #ifdef CONFIG_OF
  341. /* Common function that can be used to understand the limitation of
  342. * a transceiver when it provides no means to determine these limitations
  343. * at runtime.
  344. */
  345. void of_can_transceiver(struct net_device *dev)
  346. {
  347. struct device_node *dn;
  348. struct can_priv *priv = netdev_priv(dev);
  349. struct device_node *np = dev->dev.parent->of_node;
  350. int ret;
  351. dn = of_get_child_by_name(np, "can-transceiver");
  352. if (!dn)
  353. return;
  354. ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
  355. of_node_put(dn);
  356. if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
  357. netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
  358. }
  359. EXPORT_SYMBOL_GPL(of_can_transceiver);
  360. #endif
  361. /* Common close function for cleanup before the device gets closed.
  362. *
  363. * This function should be called in the close function of the device
  364. * driver.
  365. */
  366. void close_candev(struct net_device *dev)
  367. {
  368. struct can_priv *priv = netdev_priv(dev);
  369. cancel_delayed_work_sync(&priv->restart_work);
  370. can_flush_echo_skb(dev);
  371. }
  372. EXPORT_SYMBOL_GPL(close_candev);
  373. static int can_set_termination(struct net_device *ndev, u16 term)
  374. {
  375. struct can_priv *priv = netdev_priv(ndev);
  376. int set;
  377. if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
  378. set = 1;
  379. else
  380. set = 0;
  381. gpiod_set_value(priv->termination_gpio, set);
  382. return 0;
  383. }
  384. static int can_get_termination(struct net_device *ndev)
  385. {
  386. struct can_priv *priv = netdev_priv(ndev);
  387. struct device *dev = ndev->dev.parent;
  388. struct gpio_desc *gpio;
  389. u32 term;
  390. int ret;
  391. /* Disabling termination by default is the safe choice: Else if many
  392. * bus participants enable it, no communication is possible at all.
  393. */
  394. gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
  395. if (IS_ERR(gpio))
  396. return dev_err_probe(dev, PTR_ERR(gpio),
  397. "Cannot get termination-gpios\n");
  398. if (!gpio)
  399. return 0;
  400. ret = device_property_read_u32(dev, "termination-ohms", &term);
  401. if (ret) {
  402. netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
  403. ERR_PTR(ret));
  404. return ret;
  405. }
  406. if (term > U16_MAX) {
  407. netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
  408. term, U16_MAX);
  409. return -EINVAL;
  410. }
  411. priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
  412. priv->termination_const = priv->termination_gpio_ohms;
  413. priv->termination_gpio = gpio;
  414. priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
  415. CAN_TERMINATION_DISABLED;
  416. priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
  417. priv->do_set_termination = can_set_termination;
  418. return 0;
  419. }
  420. /* Register the CAN network device */
  421. int register_candev(struct net_device *dev)
  422. {
  423. struct can_priv *priv = netdev_priv(dev);
  424. int err;
  425. /* Ensure termination_const, termination_const_cnt and
  426. * do_set_termination consistency. All must be either set or
  427. * unset.
  428. */
  429. if ((!priv->termination_const != !priv->termination_const_cnt) ||
  430. (!priv->termination_const != !priv->do_set_termination))
  431. return -EINVAL;
  432. if (!priv->bitrate_const != !priv->bitrate_const_cnt)
  433. return -EINVAL;
  434. if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
  435. return -EINVAL;
  436. if (!priv->termination_const) {
  437. err = can_get_termination(dev);
  438. if (err)
  439. return err;
  440. }
  441. dev->rtnl_link_ops = &can_link_ops;
  442. netif_carrier_off(dev);
  443. return register_netdev(dev);
  444. }
  445. EXPORT_SYMBOL_GPL(register_candev);
  446. /* Unregister the CAN network device */
  447. void unregister_candev(struct net_device *dev)
  448. {
  449. unregister_netdev(dev);
  450. }
  451. EXPORT_SYMBOL_GPL(unregister_candev);
  452. /* Test if a network device is a candev based device
  453. * and return the can_priv* if so.
  454. */
  455. struct can_priv *safe_candev_priv(struct net_device *dev)
  456. {
  457. if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
  458. return NULL;
  459. return netdev_priv(dev);
  460. }
  461. EXPORT_SYMBOL_GPL(safe_candev_priv);
  462. static __init int can_dev_init(void)
  463. {
  464. int err;
  465. err = can_netlink_register();
  466. if (!err)
  467. pr_info("CAN device driver interface\n");
  468. return err;
  469. }
  470. module_init(can_dev_init);
  471. static __exit void can_dev_exit(void)
  472. {
  473. can_netlink_unregister();
  474. }
  475. module_exit(can_dev_exit);
  476. MODULE_ALIAS_RTNL_LINK("can");