peak_canfd.c 19 KB

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