peak_canfd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. unsigned long flags;
  200. spin_lock_irqsave(&priv->echo_lock, flags);
  201. can_get_echo_skb(priv->ndev, msg->client);
  202. /* count bytes of the echo instead of skb */
  203. stats->tx_bytes += cf_len;
  204. stats->tx_packets++;
  205. /* restart tx queue (a slot is free) */
  206. netif_wake_queue(priv->ndev);
  207. spin_unlock_irqrestore(&priv->echo_lock, flags);
  208. return 0;
  209. }
  210. /* otherwise, it should be pushed into rx fifo */
  211. if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
  212. /* CANFD frame case */
  213. skb = alloc_canfd_skb(priv->ndev, &cf);
  214. if (!skb)
  215. return -ENOMEM;
  216. if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
  217. cf->flags |= CANFD_BRS;
  218. if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
  219. cf->flags |= CANFD_ESI;
  220. } else {
  221. /* CAN 2.0 frame case */
  222. skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
  223. if (!skb)
  224. return -ENOMEM;
  225. }
  226. cf->can_id = le32_to_cpu(msg->can_id);
  227. cf->len = cf_len;
  228. if (rx_msg_flags & PUCAN_MSG_EXT_ID)
  229. cf->can_id |= CAN_EFF_FLAG;
  230. if (rx_msg_flags & PUCAN_MSG_RTR)
  231. cf->can_id |= CAN_RTR_FLAG;
  232. else
  233. memcpy(cf->data, msg->d, cf->len);
  234. stats->rx_bytes += cf->len;
  235. stats->rx_packets++;
  236. netif_rx(skb);
  237. return 0;
  238. }
  239. /* handle rx/tx error counters notification */
  240. static int pucan_handle_error(struct peak_canfd_priv *priv,
  241. struct pucan_error_msg *msg)
  242. {
  243. priv->bec.txerr = msg->tx_err_cnt;
  244. priv->bec.rxerr = msg->rx_err_cnt;
  245. return 0;
  246. }
  247. /* handle status notification */
  248. static int pucan_handle_status(struct peak_canfd_priv *priv,
  249. struct pucan_status_msg *msg)
  250. {
  251. struct net_device *ndev = priv->ndev;
  252. struct net_device_stats *stats = &ndev->stats;
  253. struct can_frame *cf;
  254. struct sk_buff *skb;
  255. /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */
  256. if (pucan_status_is_rx_barrier(msg)) {
  257. if (priv->enable_tx_path) {
  258. int err = priv->enable_tx_path(priv);
  259. if (err)
  260. return err;
  261. }
  262. /* start network queue (echo_skb array is empty) */
  263. netif_start_queue(ndev);
  264. return 0;
  265. }
  266. skb = alloc_can_err_skb(ndev, &cf);
  267. /* test state error bits according to their priority */
  268. if (pucan_status_is_busoff(msg)) {
  269. netdev_dbg(ndev, "Bus-off entry status\n");
  270. priv->can.state = CAN_STATE_BUS_OFF;
  271. priv->can.can_stats.bus_off++;
  272. can_bus_off(ndev);
  273. if (skb)
  274. cf->can_id |= CAN_ERR_BUSOFF;
  275. } else if (pucan_status_is_passive(msg)) {
  276. netdev_dbg(ndev, "Error passive status\n");
  277. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  278. priv->can.can_stats.error_passive++;
  279. if (skb) {
  280. cf->can_id |= CAN_ERR_CRTL;
  281. cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
  282. CAN_ERR_CRTL_TX_PASSIVE :
  283. CAN_ERR_CRTL_RX_PASSIVE;
  284. cf->data[6] = priv->bec.txerr;
  285. cf->data[7] = priv->bec.rxerr;
  286. }
  287. } else if (pucan_status_is_warning(msg)) {
  288. netdev_dbg(ndev, "Error warning status\n");
  289. priv->can.state = CAN_STATE_ERROR_WARNING;
  290. priv->can.can_stats.error_warning++;
  291. if (skb) {
  292. cf->can_id |= CAN_ERR_CRTL;
  293. cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
  294. CAN_ERR_CRTL_TX_WARNING :
  295. CAN_ERR_CRTL_RX_WARNING;
  296. cf->data[6] = priv->bec.txerr;
  297. cf->data[7] = priv->bec.rxerr;
  298. }
  299. } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) {
  300. /* back to ERROR_ACTIVE */
  301. netdev_dbg(ndev, "Error active status\n");
  302. can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE,
  303. CAN_STATE_ERROR_ACTIVE);
  304. } else {
  305. dev_kfree_skb(skb);
  306. return 0;
  307. }
  308. if (!skb) {
  309. stats->rx_dropped++;
  310. return -ENOMEM;
  311. }
  312. stats->rx_packets++;
  313. stats->rx_bytes += cf->can_dlc;
  314. netif_rx(skb);
  315. return 0;
  316. }
  317. /* handle uCAN Rx overflow notification */
  318. static int pucan_handle_cache_critical(struct peak_canfd_priv *priv)
  319. {
  320. struct net_device_stats *stats = &priv->ndev->stats;
  321. struct can_frame *cf;
  322. struct sk_buff *skb;
  323. stats->rx_over_errors++;
  324. stats->rx_errors++;
  325. skb = alloc_can_err_skb(priv->ndev, &cf);
  326. if (!skb) {
  327. stats->rx_dropped++;
  328. return -ENOMEM;
  329. }
  330. cf->can_id |= CAN_ERR_CRTL;
  331. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  332. cf->data[6] = priv->bec.txerr;
  333. cf->data[7] = priv->bec.rxerr;
  334. stats->rx_bytes += cf->can_dlc;
  335. stats->rx_packets++;
  336. netif_rx(skb);
  337. return 0;
  338. }
  339. /* handle a single uCAN message */
  340. int peak_canfd_handle_msg(struct peak_canfd_priv *priv,
  341. struct pucan_rx_msg *msg)
  342. {
  343. u16 msg_type = le16_to_cpu(msg->type);
  344. int msg_size = le16_to_cpu(msg->size);
  345. int err;
  346. if (!msg_size || !msg_type) {
  347. /* null packet found: end of list */
  348. goto exit;
  349. }
  350. switch (msg_type) {
  351. case PUCAN_MSG_CAN_RX:
  352. err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg);
  353. break;
  354. case PUCAN_MSG_ERROR:
  355. err = pucan_handle_error(priv, (struct pucan_error_msg *)msg);
  356. break;
  357. case PUCAN_MSG_STATUS:
  358. err = pucan_handle_status(priv, (struct pucan_status_msg *)msg);
  359. break;
  360. case PUCAN_MSG_CACHE_CRITICAL:
  361. err = pucan_handle_cache_critical(priv);
  362. break;
  363. default:
  364. err = 0;
  365. }
  366. if (err < 0)
  367. return err;
  368. exit:
  369. return msg_size;
  370. }
  371. /* handle a list of rx_count messages from rx_msg memory address */
  372. int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv,
  373. struct pucan_rx_msg *msg_list, int msg_count)
  374. {
  375. void *msg_ptr = msg_list;
  376. int i, msg_size = 0;
  377. for (i = 0; i < msg_count; i++) {
  378. msg_size = peak_canfd_handle_msg(priv, msg_ptr);
  379. /* a null packet can be found at the end of a list */
  380. if (msg_size <= 0)
  381. break;
  382. msg_ptr += ALIGN(msg_size, 4);
  383. }
  384. if (msg_size < 0)
  385. return msg_size;
  386. return i;
  387. }
  388. static int peak_canfd_start(struct peak_canfd_priv *priv)
  389. {
  390. int err;
  391. err = pucan_clr_err_counters(priv);
  392. if (err)
  393. goto err_exit;
  394. priv->echo_idx = 0;
  395. priv->bec.txerr = 0;
  396. priv->bec.rxerr = 0;
  397. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  398. err = pucan_set_listen_only_mode(priv);
  399. else
  400. err = pucan_set_normal_mode(priv);
  401. err_exit:
  402. return err;
  403. }
  404. static void peak_canfd_stop(struct peak_canfd_priv *priv)
  405. {
  406. int err;
  407. /* go back to RESET mode */
  408. err = pucan_set_reset_mode(priv);
  409. if (err) {
  410. netdev_err(priv->ndev, "channel %u reset failed\n",
  411. priv->index);
  412. } else {
  413. /* abort last Tx (MUST be done in RESET mode only!) */
  414. pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH);
  415. }
  416. }
  417. static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
  418. {
  419. struct peak_canfd_priv *priv = netdev_priv(ndev);
  420. switch (mode) {
  421. case CAN_MODE_START:
  422. peak_canfd_start(priv);
  423. netif_wake_queue(ndev);
  424. break;
  425. default:
  426. return -EOPNOTSUPP;
  427. }
  428. return 0;
  429. }
  430. static int peak_canfd_get_berr_counter(const struct net_device *ndev,
  431. struct can_berr_counter *bec)
  432. {
  433. struct peak_canfd_priv *priv = netdev_priv(ndev);
  434. *bec = priv->bec;
  435. return 0;
  436. }
  437. static int peak_canfd_open(struct net_device *ndev)
  438. {
  439. struct peak_canfd_priv *priv = netdev_priv(ndev);
  440. int i, err = 0;
  441. err = open_candev(ndev);
  442. if (err) {
  443. netdev_err(ndev, "open_candev() failed, error %d\n", err);
  444. goto err_exit;
  445. }
  446. err = pucan_set_reset_mode(priv);
  447. if (err)
  448. goto err_close;
  449. if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
  450. if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
  451. err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO);
  452. else
  453. err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO);
  454. if (err)
  455. goto err_close;
  456. }
  457. /* set option: get rx/tx error counters */
  458. err = pucan_set_options(priv, PUCAN_OPTION_ERROR);
  459. if (err)
  460. goto err_close;
  461. /* accept all standard CAN ID */
  462. for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++)
  463. pucan_set_std_filter(priv, i, 0xffffffff);
  464. err = peak_canfd_start(priv);
  465. if (err)
  466. goto err_close;
  467. /* receiving the RB status says when Tx path is ready */
  468. err = pucan_setup_rx_barrier(priv);
  469. if (!err)
  470. goto err_exit;
  471. err_close:
  472. close_candev(ndev);
  473. err_exit:
  474. return err;
  475. }
  476. static int peak_canfd_set_bittiming(struct net_device *ndev)
  477. {
  478. struct peak_canfd_priv *priv = netdev_priv(ndev);
  479. return pucan_set_timing_slow(priv, &priv->can.bittiming);
  480. }
  481. static int peak_canfd_set_data_bittiming(struct net_device *ndev)
  482. {
  483. struct peak_canfd_priv *priv = netdev_priv(ndev);
  484. return pucan_set_timing_fast(priv, &priv->can.data_bittiming);
  485. }
  486. static int peak_canfd_close(struct net_device *ndev)
  487. {
  488. struct peak_canfd_priv *priv = netdev_priv(ndev);
  489. netif_stop_queue(ndev);
  490. peak_canfd_stop(priv);
  491. close_candev(ndev);
  492. return 0;
  493. }
  494. static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb,
  495. struct net_device *ndev)
  496. {
  497. struct peak_canfd_priv *priv = netdev_priv(ndev);
  498. struct net_device_stats *stats = &ndev->stats;
  499. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  500. struct pucan_tx_msg *msg;
  501. u16 msg_size, msg_flags;
  502. unsigned long flags;
  503. bool should_stop_tx_queue;
  504. int room_left;
  505. u8 can_dlc;
  506. if (can_dropped_invalid_skb(ndev, skb))
  507. return NETDEV_TX_OK;
  508. msg_size = ALIGN(sizeof(*msg) + cf->len, 4);
  509. msg = priv->alloc_tx_msg(priv, msg_size, &room_left);
  510. /* should never happen except under bus-off condition and (auto-)restart
  511. * mechanism
  512. */
  513. if (!msg) {
  514. stats->tx_dropped++;
  515. netif_stop_queue(ndev);
  516. return NETDEV_TX_BUSY;
  517. }
  518. msg->size = cpu_to_le16(msg_size);
  519. msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
  520. msg_flags = 0;
  521. if (cf->can_id & CAN_EFF_FLAG) {
  522. msg_flags |= PUCAN_MSG_EXT_ID;
  523. msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK);
  524. } else {
  525. msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK);
  526. }
  527. if (can_is_canfd_skb(skb)) {
  528. /* CAN FD frame format */
  529. can_dlc = can_len2dlc(cf->len);
  530. msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
  531. if (cf->flags & CANFD_BRS)
  532. msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
  533. if (cf->flags & CANFD_ESI)
  534. msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
  535. } else {
  536. /* CAN 2.0 frame format */
  537. can_dlc = cf->len;
  538. if (cf->can_id & CAN_RTR_FLAG)
  539. msg_flags |= PUCAN_MSG_RTR;
  540. }
  541. /* always ask loopback for echo management */
  542. msg_flags |= PUCAN_MSG_LOOPED_BACK;
  543. /* set driver specific bit to differentiate with application loopback */
  544. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  545. msg_flags |= PUCAN_MSG_SELF_RECEIVE;
  546. msg->flags = cpu_to_le16(msg_flags);
  547. msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, can_dlc);
  548. memcpy(msg->d, cf->data, cf->len);
  549. /* struct msg client field is used as an index in the echo skbs ring */
  550. msg->client = priv->echo_idx;
  551. spin_lock_irqsave(&priv->echo_lock, flags);
  552. /* prepare and save echo skb in internal slot */
  553. can_put_echo_skb(skb, ndev, priv->echo_idx);
  554. /* move echo index to the next slot */
  555. priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max;
  556. /* if next slot is not free, stop network queue (no slot free in echo
  557. * skb ring means that the controller did not write these frames on
  558. * the bus: no need to continue).
  559. */
  560. should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]);
  561. /* stop network tx queue if not enough room to save one more msg too */
  562. if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
  563. should_stop_tx_queue |= (room_left <
  564. (sizeof(*msg) + CANFD_MAX_DLEN));
  565. else
  566. should_stop_tx_queue |= (room_left <
  567. (sizeof(*msg) + CAN_MAX_DLEN));
  568. if (should_stop_tx_queue)
  569. netif_stop_queue(ndev);
  570. spin_unlock_irqrestore(&priv->echo_lock, flags);
  571. /* write the skb on the interface */
  572. priv->write_tx_msg(priv, msg);
  573. return NETDEV_TX_OK;
  574. }
  575. static const struct net_device_ops peak_canfd_netdev_ops = {
  576. .ndo_open = peak_canfd_open,
  577. .ndo_stop = peak_canfd_close,
  578. .ndo_start_xmit = peak_canfd_start_xmit,
  579. .ndo_change_mtu = can_change_mtu,
  580. };
  581. struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index,
  582. int echo_skb_max)
  583. {
  584. struct net_device *ndev;
  585. struct peak_canfd_priv *priv;
  586. /* we DO support local echo */
  587. if (echo_skb_max < 0)
  588. echo_skb_max = PCANFD_ECHO_SKB_MAX;
  589. /* allocate the candev object */
  590. ndev = alloc_candev(sizeof_priv, echo_skb_max);
  591. if (!ndev)
  592. return NULL;
  593. priv = netdev_priv(ndev);
  594. /* complete now socket-can initialization side */
  595. priv->can.state = CAN_STATE_STOPPED;
  596. priv->can.bittiming_const = &peak_canfd_nominal_const;
  597. priv->can.data_bittiming_const = &peak_canfd_data_const;
  598. priv->can.do_set_mode = peak_canfd_set_mode;
  599. priv->can.do_get_berr_counter = peak_canfd_get_berr_counter;
  600. priv->can.do_set_bittiming = peak_canfd_set_bittiming;
  601. priv->can.do_set_data_bittiming = peak_canfd_set_data_bittiming;
  602. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  603. CAN_CTRLMODE_LISTENONLY |
  604. CAN_CTRLMODE_3_SAMPLES |
  605. CAN_CTRLMODE_FD |
  606. CAN_CTRLMODE_FD_NON_ISO |
  607. CAN_CTRLMODE_BERR_REPORTING;
  608. priv->ndev = ndev;
  609. priv->index = index;
  610. priv->cmd_len = 0;
  611. spin_lock_init(&priv->echo_lock);
  612. ndev->flags |= IFF_ECHO;
  613. ndev->netdev_ops = &peak_canfd_netdev_ops;
  614. ndev->dev_id = index;
  615. return ndev;
  616. }