peak_canfd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
  3. *
  4. * Copyright (C) 2016-2025 PEAK System-Technik GmbH
  5. * Author: Stéphane Grosjean <stephane.grosjean@hms-networks.com>
  6. */
  7. #include <linux/can.h>
  8. #include <linux/can/dev.h>
  9. #include <linux/ethtool.h>
  10. #include "peak_canfd_user.h"
  11. /* internal IP core cache size (used as default echo skbs max number) */
  12. #define PCANFD_ECHO_SKB_MAX 24
  13. /* bittiming ranges of the PEAK-System PC CAN-FD interfaces */
  14. static const struct can_bittiming_const peak_canfd_nominal_const = {
  15. .name = "peak_canfd",
  16. .tseg1_min = 1,
  17. .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
  18. .tseg2_min = 1,
  19. .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
  20. .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
  21. .brp_min = 1,
  22. .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
  23. .brp_inc = 1,
  24. };
  25. static const struct can_bittiming_const peak_canfd_data_const = {
  26. .name = "peak_canfd",
  27. .tseg1_min = 1,
  28. .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
  29. .tseg2_min = 1,
  30. .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
  31. .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
  32. .brp_min = 1,
  33. .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
  34. .brp_inc = 1,
  35. };
  36. static struct peak_canfd_priv *pucan_init_cmd(struct peak_canfd_priv *priv)
  37. {
  38. priv->cmd_len = 0;
  39. return priv;
  40. }
  41. static void *pucan_add_cmd(struct peak_canfd_priv *priv, int cmd_op)
  42. {
  43. struct pucan_command *cmd;
  44. if (priv->cmd_len + sizeof(*cmd) > priv->cmd_maxlen)
  45. return NULL;
  46. cmd = priv->cmd_buffer + priv->cmd_len;
  47. /* reset all unused bit to default */
  48. memset(cmd, 0, sizeof(*cmd));
  49. cmd->opcode_channel = pucan_cmd_opcode_channel(priv->index, cmd_op);
  50. priv->cmd_len += sizeof(*cmd);
  51. return cmd;
  52. }
  53. static int pucan_write_cmd(struct peak_canfd_priv *priv)
  54. {
  55. int err;
  56. if (priv->pre_cmd) {
  57. err = priv->pre_cmd(priv);
  58. if (err)
  59. return err;
  60. }
  61. err = priv->write_cmd(priv);
  62. if (err)
  63. return err;
  64. if (priv->post_cmd)
  65. err = priv->post_cmd(priv);
  66. return err;
  67. }
  68. /* uCAN commands interface functions */
  69. static int pucan_set_reset_mode(struct peak_canfd_priv *priv)
  70. {
  71. pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RESET_MODE);
  72. return pucan_write_cmd(priv);
  73. }
  74. static int pucan_set_normal_mode(struct peak_canfd_priv *priv)
  75. {
  76. int err;
  77. pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_NORMAL_MODE);
  78. err = pucan_write_cmd(priv);
  79. if (!err)
  80. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  81. return err;
  82. }
  83. static int pucan_set_listen_only_mode(struct peak_canfd_priv *priv)
  84. {
  85. int err;
  86. pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_LISTEN_ONLY_MODE);
  87. err = pucan_write_cmd(priv);
  88. if (!err)
  89. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  90. return err;
  91. }
  92. static int pucan_set_timing_slow(struct peak_canfd_priv *priv,
  93. const struct can_bittiming *pbt)
  94. {
  95. struct pucan_timing_slow *cmd;
  96. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_SLOW);
  97. cmd->sjw_t = PUCAN_TSLOW_SJW_T(pbt->sjw - 1,
  98. priv->can.ctrlmode &
  99. CAN_CTRLMODE_3_SAMPLES);
  100. cmd->tseg1 = PUCAN_TSLOW_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
  101. cmd->tseg2 = PUCAN_TSLOW_TSEG2(pbt->phase_seg2 - 1);
  102. cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(pbt->brp - 1));
  103. cmd->ewl = 96; /* default */
  104. netdev_dbg(priv->ndev,
  105. "nominal: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
  106. le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw_t);
  107. return pucan_write_cmd(priv);
  108. }
  109. static int pucan_set_timing_fast(struct peak_canfd_priv *priv,
  110. const struct can_bittiming *pbt)
  111. {
  112. struct pucan_timing_fast *cmd;
  113. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_FAST);
  114. cmd->sjw = PUCAN_TFAST_SJW(pbt->sjw - 1);
  115. cmd->tseg1 = PUCAN_TFAST_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
  116. cmd->tseg2 = PUCAN_TFAST_TSEG2(pbt->phase_seg2 - 1);
  117. cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(pbt->brp - 1));
  118. netdev_dbg(priv->ndev,
  119. "data: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
  120. le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw);
  121. return pucan_write_cmd(priv);
  122. }
  123. static int pucan_set_std_filter(struct peak_canfd_priv *priv, u8 row, u32 mask)
  124. {
  125. struct pucan_std_filter *cmd;
  126. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_STD_FILTER);
  127. /* all the 11-bits CAN ID values are represented by one bit in a
  128. * 64 rows array of 32 bits: the upper 6 bits of the CAN ID select the
  129. * row while the lowest 5 bits select the bit in that row.
  130. *
  131. * bit filter
  132. * 1 passed
  133. * 0 discarded
  134. */
  135. /* select the row */
  136. cmd->idx = row;
  137. /* set/unset bits in the row */
  138. cmd->mask = cpu_to_le32(mask);
  139. return pucan_write_cmd(priv);
  140. }
  141. static int pucan_tx_abort(struct peak_canfd_priv *priv, u16 flags)
  142. {
  143. struct pucan_tx_abort *cmd;
  144. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TX_ABORT);
  145. cmd->flags = cpu_to_le16(flags);
  146. return pucan_write_cmd(priv);
  147. }
  148. static int pucan_clr_err_counters(struct peak_canfd_priv *priv)
  149. {
  150. struct pucan_wr_err_cnt *cmd;
  151. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_WR_ERR_CNT);
  152. cmd->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE | PUCAN_WRERRCNT_RE);
  153. cmd->tx_counter = 0;
  154. cmd->rx_counter = 0;
  155. return pucan_write_cmd(priv);
  156. }
  157. static int pucan_set_options(struct peak_canfd_priv *priv, u16 opt_mask)
  158. {
  159. struct pucan_options *cmd;
  160. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_EN_OPTION);
  161. cmd->options = cpu_to_le16(opt_mask);
  162. return pucan_write_cmd(priv);
  163. }
  164. static int pucan_clr_options(struct peak_canfd_priv *priv, u16 opt_mask)
  165. {
  166. struct pucan_options *cmd;
  167. cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_CLR_DIS_OPTION);
  168. cmd->options = cpu_to_le16(opt_mask);
  169. return pucan_write_cmd(priv);
  170. }
  171. static int pucan_setup_rx_barrier(struct peak_canfd_priv *priv)
  172. {
  173. pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RX_BARRIER);
  174. return pucan_write_cmd(priv);
  175. }
  176. static int pucan_netif_rx(struct sk_buff *skb, __le32 ts_low, __le32 ts_high)
  177. {
  178. struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
  179. u64 ts_us;
  180. ts_us = (u64)le32_to_cpu(ts_high) << 32;
  181. ts_us |= le32_to_cpu(ts_low);
  182. /* IP core timestamps are µs. */
  183. hwts->hwtstamp = ns_to_ktime(ts_us * NSEC_PER_USEC);
  184. return netif_rx(skb);
  185. }
  186. /* handle the reception of one CAN frame */
  187. static int pucan_handle_can_rx(struct peak_canfd_priv *priv,
  188. struct pucan_rx_msg *msg)
  189. {
  190. struct net_device_stats *stats = &priv->ndev->stats;
  191. struct canfd_frame *cf;
  192. struct sk_buff *skb;
  193. const u16 rx_msg_flags = le16_to_cpu(msg->flags);
  194. u8 cf_len;
  195. if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN)
  196. cf_len = can_fd_dlc2len(pucan_msg_get_dlc(msg));
  197. else
  198. cf_len = can_cc_dlc2len(pucan_msg_get_dlc(msg));
  199. /* if this frame is an echo, */
  200. if (rx_msg_flags & PUCAN_MSG_LOOPED_BACK) {
  201. unsigned long flags;
  202. spin_lock_irqsave(&priv->echo_lock, flags);
  203. /* count bytes of the echo instead of skb */
  204. stats->tx_bytes += can_get_echo_skb(priv->ndev, msg->client, NULL);
  205. stats->tx_packets++;
  206. /* restart tx queue (a slot is free) */
  207. netif_wake_queue(priv->ndev);
  208. spin_unlock_irqrestore(&priv->echo_lock, flags);
  209. /* if this frame is only an echo, stop here. Otherwise,
  210. * continue to push this application self-received frame into
  211. * its own rx queue.
  212. */
  213. if (!(rx_msg_flags & PUCAN_MSG_SELF_RECEIVE))
  214. return 0;
  215. }
  216. /* otherwise, it should be pushed into rx fifo */
  217. if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
  218. /* CANFD frame case */
  219. skb = alloc_canfd_skb(priv->ndev, &cf);
  220. if (!skb)
  221. return -ENOMEM;
  222. if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
  223. cf->flags |= CANFD_BRS;
  224. if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
  225. cf->flags |= CANFD_ESI;
  226. } else {
  227. /* CAN 2.0 frame case */
  228. skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
  229. if (!skb)
  230. return -ENOMEM;
  231. }
  232. cf->can_id = le32_to_cpu(msg->can_id);
  233. cf->len = cf_len;
  234. if (rx_msg_flags & PUCAN_MSG_EXT_ID)
  235. cf->can_id |= CAN_EFF_FLAG;
  236. if (rx_msg_flags & PUCAN_MSG_RTR) {
  237. cf->can_id |= CAN_RTR_FLAG;
  238. } else {
  239. memcpy(cf->data, msg->d, cf->len);
  240. stats->rx_bytes += cf->len;
  241. }
  242. stats->rx_packets++;
  243. pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
  244. return 0;
  245. }
  246. /* handle rx/tx error counters notification */
  247. static int pucan_handle_error(struct peak_canfd_priv *priv,
  248. struct pucan_error_msg *msg)
  249. {
  250. priv->bec.txerr = msg->tx_err_cnt;
  251. priv->bec.rxerr = msg->rx_err_cnt;
  252. return 0;
  253. }
  254. /* handle status notification */
  255. static int pucan_handle_status(struct peak_canfd_priv *priv,
  256. struct pucan_status_msg *msg)
  257. {
  258. struct net_device *ndev = priv->ndev;
  259. struct net_device_stats *stats = &ndev->stats;
  260. struct can_frame *cf;
  261. struct sk_buff *skb;
  262. /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */
  263. if (pucan_status_is_rx_barrier(msg)) {
  264. if (priv->enable_tx_path) {
  265. int err = priv->enable_tx_path(priv);
  266. if (err)
  267. return err;
  268. }
  269. /* wake network queue up (echo_skb array is empty) */
  270. netif_wake_queue(ndev);
  271. return 0;
  272. }
  273. skb = alloc_can_err_skb(ndev, &cf);
  274. /* test state error bits according to their priority */
  275. if (pucan_status_is_busoff(msg)) {
  276. netdev_dbg(ndev, "Bus-off entry status\n");
  277. priv->can.state = CAN_STATE_BUS_OFF;
  278. priv->can.can_stats.bus_off++;
  279. can_bus_off(ndev);
  280. if (skb)
  281. cf->can_id |= CAN_ERR_BUSOFF;
  282. } else if (pucan_status_is_passive(msg)) {
  283. netdev_dbg(ndev, "Error passive status\n");
  284. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  285. priv->can.can_stats.error_passive++;
  286. if (skb) {
  287. cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
  288. cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
  289. CAN_ERR_CRTL_TX_PASSIVE :
  290. CAN_ERR_CRTL_RX_PASSIVE;
  291. cf->data[6] = priv->bec.txerr;
  292. cf->data[7] = priv->bec.rxerr;
  293. }
  294. } else if (pucan_status_is_warning(msg)) {
  295. netdev_dbg(ndev, "Error warning status\n");
  296. priv->can.state = CAN_STATE_ERROR_WARNING;
  297. priv->can.can_stats.error_warning++;
  298. if (skb) {
  299. cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
  300. cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
  301. CAN_ERR_CRTL_TX_WARNING :
  302. CAN_ERR_CRTL_RX_WARNING;
  303. cf->data[6] = priv->bec.txerr;
  304. cf->data[7] = priv->bec.rxerr;
  305. }
  306. } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) {
  307. /* back to ERROR_ACTIVE */
  308. netdev_dbg(ndev, "Error active status\n");
  309. can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE,
  310. CAN_STATE_ERROR_ACTIVE);
  311. } else {
  312. dev_kfree_skb(skb);
  313. return 0;
  314. }
  315. if (!skb) {
  316. stats->rx_dropped++;
  317. return -ENOMEM;
  318. }
  319. pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
  320. return 0;
  321. }
  322. /* handle uCAN Rx overflow notification */
  323. static int pucan_handle_cache_critical(struct peak_canfd_priv *priv)
  324. {
  325. struct net_device_stats *stats = &priv->ndev->stats;
  326. struct can_frame *cf;
  327. struct sk_buff *skb;
  328. stats->rx_over_errors++;
  329. stats->rx_errors++;
  330. skb = alloc_can_err_skb(priv->ndev, &cf);
  331. if (!skb) {
  332. stats->rx_dropped++;
  333. return -ENOMEM;
  334. }
  335. cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
  336. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  337. cf->data[6] = priv->bec.txerr;
  338. cf->data[7] = priv->bec.rxerr;
  339. netif_rx(skb);
  340. return 0;
  341. }
  342. /* handle a single uCAN message */
  343. int peak_canfd_handle_msg(struct peak_canfd_priv *priv,
  344. struct pucan_rx_msg *msg)
  345. {
  346. u16 msg_type = le16_to_cpu(msg->type);
  347. int msg_size = le16_to_cpu(msg->size);
  348. int err;
  349. if (!msg_size || !msg_type) {
  350. /* null packet found: end of list */
  351. goto exit;
  352. }
  353. switch (msg_type) {
  354. case PUCAN_MSG_CAN_RX:
  355. err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg);
  356. break;
  357. case PUCAN_MSG_ERROR:
  358. err = pucan_handle_error(priv, (struct pucan_error_msg *)msg);
  359. break;
  360. case PUCAN_MSG_STATUS:
  361. err = pucan_handle_status(priv, (struct pucan_status_msg *)msg);
  362. break;
  363. case PUCAN_MSG_CACHE_CRITICAL:
  364. err = pucan_handle_cache_critical(priv);
  365. break;
  366. default:
  367. err = 0;
  368. }
  369. if (err < 0)
  370. return err;
  371. exit:
  372. return msg_size;
  373. }
  374. /* handle a list of rx_count messages from rx_msg memory address */
  375. int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv,
  376. struct pucan_rx_msg *msg_list, int msg_count)
  377. {
  378. void *msg_ptr = msg_list;
  379. int i, msg_size = 0;
  380. for (i = 0; i < msg_count; i++) {
  381. msg_size = peak_canfd_handle_msg(priv, msg_ptr);
  382. /* a null packet can be found at the end of a list */
  383. if (msg_size <= 0)
  384. break;
  385. msg_ptr += ALIGN(msg_size, 4);
  386. }
  387. if (msg_size < 0)
  388. return msg_size;
  389. return i;
  390. }
  391. static int peak_canfd_start(struct peak_canfd_priv *priv)
  392. {
  393. int err;
  394. err = pucan_clr_err_counters(priv);
  395. if (err)
  396. goto err_exit;
  397. priv->echo_idx = 0;
  398. priv->bec.txerr = 0;
  399. priv->bec.rxerr = 0;
  400. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  401. err = pucan_set_listen_only_mode(priv);
  402. else
  403. err = pucan_set_normal_mode(priv);
  404. err_exit:
  405. return err;
  406. }
  407. static void peak_canfd_stop(struct peak_canfd_priv *priv)
  408. {
  409. int err;
  410. /* go back to RESET mode */
  411. err = pucan_set_reset_mode(priv);
  412. if (err) {
  413. netdev_err(priv->ndev, "channel %u reset failed\n",
  414. priv->index);
  415. } else {
  416. /* abort last Tx (MUST be done in RESET mode only!) */
  417. pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH);
  418. }
  419. }
  420. static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
  421. {
  422. struct peak_canfd_priv *priv = netdev_priv(ndev);
  423. switch (mode) {
  424. case CAN_MODE_START:
  425. peak_canfd_start(priv);
  426. netif_wake_queue(ndev);
  427. break;
  428. default:
  429. return -EOPNOTSUPP;
  430. }
  431. return 0;
  432. }
  433. static int peak_canfd_get_berr_counter(const struct net_device *ndev,
  434. struct can_berr_counter *bec)
  435. {
  436. struct peak_canfd_priv *priv = netdev_priv(ndev);
  437. *bec = priv->bec;
  438. return 0;
  439. }
  440. static int peak_canfd_open(struct net_device *ndev)
  441. {
  442. struct peak_canfd_priv *priv = netdev_priv(ndev);
  443. int i, err = 0;
  444. err = open_candev(ndev);
  445. if (err) {
  446. netdev_err(ndev, "open_candev() failed, error %d\n", err);
  447. goto err_exit;
  448. }
  449. err = pucan_set_reset_mode(priv);
  450. if (err)
  451. goto err_close;
  452. if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
  453. if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
  454. err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO);
  455. else
  456. err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO);
  457. if (err)
  458. goto err_close;
  459. }
  460. /* set option: get rx/tx error counters */
  461. err = pucan_set_options(priv, PUCAN_OPTION_ERROR);
  462. if (err)
  463. goto err_close;
  464. /* accept all standard CAN ID */
  465. for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++)
  466. pucan_set_std_filter(priv, i, 0xffffffff);
  467. err = peak_canfd_start(priv);
  468. if (err)
  469. goto err_close;
  470. /* receiving the RB status says when Tx path is ready */
  471. err = pucan_setup_rx_barrier(priv);
  472. if (!err)
  473. goto err_exit;
  474. err_close:
  475. close_candev(ndev);
  476. err_exit:
  477. return err;
  478. }
  479. static int peak_canfd_set_bittiming(struct net_device *ndev)
  480. {
  481. struct peak_canfd_priv *priv = netdev_priv(ndev);
  482. return pucan_set_timing_slow(priv, &priv->can.bittiming);
  483. }
  484. static int peak_canfd_set_data_bittiming(struct net_device *ndev)
  485. {
  486. struct peak_canfd_priv *priv = netdev_priv(ndev);
  487. return pucan_set_timing_fast(priv, &priv->can.fd.data_bittiming);
  488. }
  489. static int peak_canfd_close(struct net_device *ndev)
  490. {
  491. struct peak_canfd_priv *priv = netdev_priv(ndev);
  492. netif_stop_queue(ndev);
  493. peak_canfd_stop(priv);
  494. close_candev(ndev);
  495. return 0;
  496. }
  497. static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb,
  498. struct net_device *ndev)
  499. {
  500. struct peak_canfd_priv *priv = netdev_priv(ndev);
  501. struct net_device_stats *stats = &ndev->stats;
  502. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  503. struct pucan_tx_msg *msg;
  504. u16 msg_size, msg_flags;
  505. unsigned long flags;
  506. bool should_stop_tx_queue;
  507. int room_left;
  508. u8 len;
  509. if (can_dev_dropped_skb(ndev, skb))
  510. return NETDEV_TX_OK;
  511. msg_size = ALIGN(sizeof(*msg) + cf->len, 4);
  512. msg = priv->alloc_tx_msg(priv, msg_size, &room_left);
  513. /* should never happen except under bus-off condition and (auto-)restart
  514. * mechanism
  515. */
  516. if (!msg) {
  517. stats->tx_dropped++;
  518. netif_stop_queue(ndev);
  519. return NETDEV_TX_BUSY;
  520. }
  521. msg->size = cpu_to_le16(msg_size);
  522. msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
  523. msg_flags = 0;
  524. if (cf->can_id & CAN_EFF_FLAG) {
  525. msg_flags |= PUCAN_MSG_EXT_ID;
  526. msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK);
  527. } else {
  528. msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK);
  529. }
  530. if (can_is_canfd_skb(skb)) {
  531. /* CAN FD frame format */
  532. len = can_fd_len2dlc(cf->len);
  533. msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
  534. if (cf->flags & CANFD_BRS)
  535. msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
  536. if (cf->flags & CANFD_ESI)
  537. msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
  538. } else {
  539. /* CAN 2.0 frame format */
  540. len = cf->len;
  541. if (cf->can_id & CAN_RTR_FLAG)
  542. msg_flags |= PUCAN_MSG_RTR;
  543. }
  544. /* always ask loopback for echo management */
  545. msg_flags |= PUCAN_MSG_LOOPED_BACK;
  546. /* set driver specific bit to differentiate with application loopback */
  547. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  548. msg_flags |= PUCAN_MSG_SELF_RECEIVE;
  549. msg->flags = cpu_to_le16(msg_flags);
  550. msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, len);
  551. memcpy(msg->d, cf->data, cf->len);
  552. /* struct msg client field is used as an index in the echo skbs ring */
  553. msg->client = priv->echo_idx;
  554. spin_lock_irqsave(&priv->echo_lock, flags);
  555. /* prepare and save echo skb in internal slot */
  556. can_put_echo_skb(skb, ndev, priv->echo_idx, 0);
  557. /* move echo index to the next slot */
  558. priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max;
  559. /* if next slot is not free, stop network queue (no slot free in echo
  560. * skb ring means that the controller did not write these frames on
  561. * the bus: no need to continue).
  562. */
  563. should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]);
  564. /* stop network tx queue if not enough room to save one more msg too */
  565. if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
  566. should_stop_tx_queue |= (room_left <
  567. (sizeof(*msg) + CANFD_MAX_DLEN));
  568. else
  569. should_stop_tx_queue |= (room_left <
  570. (sizeof(*msg) + CAN_MAX_DLEN));
  571. if (should_stop_tx_queue)
  572. netif_stop_queue(ndev);
  573. spin_unlock_irqrestore(&priv->echo_lock, flags);
  574. /* write the skb on the interface */
  575. priv->write_tx_msg(priv, msg);
  576. return NETDEV_TX_OK;
  577. }
  578. static int peak_eth_hwtstamp_get(struct net_device *netdev,
  579. struct kernel_hwtstamp_config *config)
  580. {
  581. config->tx_type = HWTSTAMP_TX_OFF;
  582. config->rx_filter = HWTSTAMP_FILTER_ALL;
  583. return 0;
  584. }
  585. static int peak_eth_hwtstamp_set(struct net_device *netdev,
  586. struct kernel_hwtstamp_config *config,
  587. struct netlink_ext_ack *extack)
  588. {
  589. if (config->tx_type == HWTSTAMP_TX_OFF &&
  590. config->rx_filter == HWTSTAMP_FILTER_ALL)
  591. return 0;
  592. NL_SET_ERR_MSG_MOD(extack, "Only RX HWTSTAMP_FILTER_ALL is supported");
  593. return -ERANGE;
  594. }
  595. static const struct net_device_ops peak_canfd_netdev_ops = {
  596. .ndo_open = peak_canfd_open,
  597. .ndo_stop = peak_canfd_close,
  598. .ndo_start_xmit = peak_canfd_start_xmit,
  599. .ndo_hwtstamp_get = peak_eth_hwtstamp_get,
  600. .ndo_hwtstamp_set = peak_eth_hwtstamp_set,
  601. };
  602. static int peak_get_ts_info(struct net_device *dev,
  603. struct kernel_ethtool_ts_info *info)
  604. {
  605. info->so_timestamping =
  606. SOF_TIMESTAMPING_TX_SOFTWARE |
  607. SOF_TIMESTAMPING_RX_HARDWARE |
  608. SOF_TIMESTAMPING_RAW_HARDWARE;
  609. info->tx_types = BIT(HWTSTAMP_TX_OFF);
  610. info->rx_filters = BIT(HWTSTAMP_FILTER_ALL);
  611. return 0;
  612. }
  613. static const struct ethtool_ops peak_canfd_ethtool_ops = {
  614. .get_ts_info = peak_get_ts_info,
  615. };
  616. struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index,
  617. int echo_skb_max)
  618. {
  619. struct net_device *ndev;
  620. struct peak_canfd_priv *priv;
  621. /* we DO support local echo */
  622. if (echo_skb_max < 0)
  623. echo_skb_max = PCANFD_ECHO_SKB_MAX;
  624. /* allocate the candev object */
  625. ndev = alloc_candev(sizeof_priv, echo_skb_max);
  626. if (!ndev)
  627. return NULL;
  628. priv = netdev_priv(ndev);
  629. /* complete now socket-can initialization side */
  630. priv->can.state = CAN_STATE_STOPPED;
  631. priv->can.bittiming_const = &peak_canfd_nominal_const;
  632. priv->can.fd.data_bittiming_const = &peak_canfd_data_const;
  633. priv->can.do_set_mode = peak_canfd_set_mode;
  634. priv->can.do_get_berr_counter = peak_canfd_get_berr_counter;
  635. priv->can.do_set_bittiming = peak_canfd_set_bittiming;
  636. priv->can.fd.do_set_data_bittiming = peak_canfd_set_data_bittiming;
  637. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  638. CAN_CTRLMODE_LISTENONLY |
  639. CAN_CTRLMODE_3_SAMPLES |
  640. CAN_CTRLMODE_FD |
  641. CAN_CTRLMODE_FD_NON_ISO |
  642. CAN_CTRLMODE_BERR_REPORTING;
  643. priv->ndev = ndev;
  644. priv->index = index;
  645. priv->cmd_len = 0;
  646. spin_lock_init(&priv->echo_lock);
  647. ndev->flags |= IFF_ECHO;
  648. ndev->netdev_ops = &peak_canfd_netdev_ops;
  649. ndev->ethtool_ops = &peak_canfd_ethtool_ops;
  650. ndev->dev_id = index;
  651. return ndev;
  652. }