dev.c 15 KB

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