1aaa7cb26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23cc9358fSMarc Kleine-Budde /* Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
38ac8321eSStephane Grosjean * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
48ac8321eSStephane Grosjean *
58ac8321eSStephane Grosjean * Copyright (C) 2016 PEAK System-Technik GmbH
68ac8321eSStephane Grosjean */
78ac8321eSStephane Grosjean
88ac8321eSStephane Grosjean #include <linux/can.h>
98ac8321eSStephane Grosjean #include <linux/can/dev.h>
108ba09bfaSVincent Mailhol #include <linux/ethtool.h>
118ac8321eSStephane Grosjean
128ac8321eSStephane Grosjean #include "peak_canfd_user.h"
138ac8321eSStephane Grosjean
148ac8321eSStephane Grosjean /* internal IP core cache size (used as default echo skbs max number) */
158ac8321eSStephane Grosjean #define PCANFD_ECHO_SKB_MAX 24
168ac8321eSStephane Grosjean
178ac8321eSStephane Grosjean /* bittiming ranges of the PEAK-System PC CAN-FD interfaces */
188ac8321eSStephane Grosjean static const struct can_bittiming_const peak_canfd_nominal_const = {
198ac8321eSStephane Grosjean .name = "peak_canfd",
208ac8321eSStephane Grosjean .tseg1_min = 1,
218ac8321eSStephane Grosjean .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
228ac8321eSStephane Grosjean .tseg2_min = 1,
238ac8321eSStephane Grosjean .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
248ac8321eSStephane Grosjean .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
258ac8321eSStephane Grosjean .brp_min = 1,
268ac8321eSStephane Grosjean .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
278ac8321eSStephane Grosjean .brp_inc = 1,
288ac8321eSStephane Grosjean };
298ac8321eSStephane Grosjean
308ac8321eSStephane Grosjean static const struct can_bittiming_const peak_canfd_data_const = {
318ac8321eSStephane Grosjean .name = "peak_canfd",
328ac8321eSStephane Grosjean .tseg1_min = 1,
338ac8321eSStephane Grosjean .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
348ac8321eSStephane Grosjean .tseg2_min = 1,
358ac8321eSStephane Grosjean .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
368ac8321eSStephane Grosjean .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
378ac8321eSStephane Grosjean .brp_min = 1,
388ac8321eSStephane Grosjean .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
398ac8321eSStephane Grosjean .brp_inc = 1,
408ac8321eSStephane Grosjean };
418ac8321eSStephane Grosjean
pucan_init_cmd(struct peak_canfd_priv * priv)428ac8321eSStephane Grosjean static struct peak_canfd_priv *pucan_init_cmd(struct peak_canfd_priv *priv)
438ac8321eSStephane Grosjean {
448ac8321eSStephane Grosjean priv->cmd_len = 0;
458ac8321eSStephane Grosjean return priv;
468ac8321eSStephane Grosjean }
478ac8321eSStephane Grosjean
pucan_add_cmd(struct peak_canfd_priv * priv,int cmd_op)488ac8321eSStephane Grosjean static void *pucan_add_cmd(struct peak_canfd_priv *priv, int cmd_op)
498ac8321eSStephane Grosjean {
508ac8321eSStephane Grosjean struct pucan_command *cmd;
518ac8321eSStephane Grosjean
528ac8321eSStephane Grosjean if (priv->cmd_len + sizeof(*cmd) > priv->cmd_maxlen)
538ac8321eSStephane Grosjean return NULL;
548ac8321eSStephane Grosjean
558ac8321eSStephane Grosjean cmd = priv->cmd_buffer + priv->cmd_len;
568ac8321eSStephane Grosjean
578ac8321eSStephane Grosjean /* reset all unused bit to default */
588ac8321eSStephane Grosjean memset(cmd, 0, sizeof(*cmd));
598ac8321eSStephane Grosjean
608ac8321eSStephane Grosjean cmd->opcode_channel = pucan_cmd_opcode_channel(priv->index, cmd_op);
618ac8321eSStephane Grosjean priv->cmd_len += sizeof(*cmd);
628ac8321eSStephane Grosjean
638ac8321eSStephane Grosjean return cmd;
648ac8321eSStephane Grosjean }
658ac8321eSStephane Grosjean
pucan_write_cmd(struct peak_canfd_priv * priv)668ac8321eSStephane Grosjean static int pucan_write_cmd(struct peak_canfd_priv *priv)
678ac8321eSStephane Grosjean {
688ac8321eSStephane Grosjean int err;
698ac8321eSStephane Grosjean
708ac8321eSStephane Grosjean if (priv->pre_cmd) {
718ac8321eSStephane Grosjean err = priv->pre_cmd(priv);
728ac8321eSStephane Grosjean if (err)
738ac8321eSStephane Grosjean return err;
748ac8321eSStephane Grosjean }
758ac8321eSStephane Grosjean
768ac8321eSStephane Grosjean err = priv->write_cmd(priv);
778ac8321eSStephane Grosjean if (err)
788ac8321eSStephane Grosjean return err;
798ac8321eSStephane Grosjean
808ac8321eSStephane Grosjean if (priv->post_cmd)
818ac8321eSStephane Grosjean err = priv->post_cmd(priv);
828ac8321eSStephane Grosjean
838ac8321eSStephane Grosjean return err;
848ac8321eSStephane Grosjean }
858ac8321eSStephane Grosjean
868ac8321eSStephane Grosjean /* uCAN commands interface functions */
pucan_set_reset_mode(struct peak_canfd_priv * priv)878ac8321eSStephane Grosjean static int pucan_set_reset_mode(struct peak_canfd_priv *priv)
888ac8321eSStephane Grosjean {
898ac8321eSStephane Grosjean pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RESET_MODE);
908ac8321eSStephane Grosjean return pucan_write_cmd(priv);
918ac8321eSStephane Grosjean }
928ac8321eSStephane Grosjean
pucan_set_normal_mode(struct peak_canfd_priv * priv)938ac8321eSStephane Grosjean static int pucan_set_normal_mode(struct peak_canfd_priv *priv)
948ac8321eSStephane Grosjean {
958ac8321eSStephane Grosjean int err;
968ac8321eSStephane Grosjean
978ac8321eSStephane Grosjean pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_NORMAL_MODE);
988ac8321eSStephane Grosjean err = pucan_write_cmd(priv);
998ac8321eSStephane Grosjean if (!err)
1008ac8321eSStephane Grosjean priv->can.state = CAN_STATE_ERROR_ACTIVE;
1018ac8321eSStephane Grosjean
1028ac8321eSStephane Grosjean return err;
1038ac8321eSStephane Grosjean }
1048ac8321eSStephane Grosjean
pucan_set_listen_only_mode(struct peak_canfd_priv * priv)1058ac8321eSStephane Grosjean static int pucan_set_listen_only_mode(struct peak_canfd_priv *priv)
1068ac8321eSStephane Grosjean {
1078ac8321eSStephane Grosjean int err;
1088ac8321eSStephane Grosjean
1098ac8321eSStephane Grosjean pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_LISTEN_ONLY_MODE);
1108ac8321eSStephane Grosjean err = pucan_write_cmd(priv);
1118ac8321eSStephane Grosjean if (!err)
1128ac8321eSStephane Grosjean priv->can.state = CAN_STATE_ERROR_ACTIVE;
1138ac8321eSStephane Grosjean
1148ac8321eSStephane Grosjean return err;
1158ac8321eSStephane Grosjean }
1168ac8321eSStephane Grosjean
pucan_set_timing_slow(struct peak_canfd_priv * priv,const struct can_bittiming * pbt)1178ac8321eSStephane Grosjean static int pucan_set_timing_slow(struct peak_canfd_priv *priv,
1188ac8321eSStephane Grosjean const struct can_bittiming *pbt)
1198ac8321eSStephane Grosjean {
1208ac8321eSStephane Grosjean struct pucan_timing_slow *cmd;
1218ac8321eSStephane Grosjean
1228ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_SLOW);
1238ac8321eSStephane Grosjean
1248ac8321eSStephane Grosjean cmd->sjw_t = PUCAN_TSLOW_SJW_T(pbt->sjw - 1,
1253cc9358fSMarc Kleine-Budde priv->can.ctrlmode &
1263cc9358fSMarc Kleine-Budde CAN_CTRLMODE_3_SAMPLES);
1278ac8321eSStephane Grosjean cmd->tseg1 = PUCAN_TSLOW_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
1288ac8321eSStephane Grosjean cmd->tseg2 = PUCAN_TSLOW_TSEG2(pbt->phase_seg2 - 1);
1298ac8321eSStephane Grosjean cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(pbt->brp - 1));
1308ac8321eSStephane Grosjean
1318ac8321eSStephane Grosjean cmd->ewl = 96; /* default */
1328ac8321eSStephane Grosjean
1338ac8321eSStephane Grosjean netdev_dbg(priv->ndev,
1348ac8321eSStephane Grosjean "nominal: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
1358ac8321eSStephane Grosjean le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw_t);
1368ac8321eSStephane Grosjean
1378ac8321eSStephane Grosjean return pucan_write_cmd(priv);
1388ac8321eSStephane Grosjean }
1398ac8321eSStephane Grosjean
pucan_set_timing_fast(struct peak_canfd_priv * priv,const struct can_bittiming * pbt)1408ac8321eSStephane Grosjean static int pucan_set_timing_fast(struct peak_canfd_priv *priv,
1418ac8321eSStephane Grosjean const struct can_bittiming *pbt)
1428ac8321eSStephane Grosjean {
1438ac8321eSStephane Grosjean struct pucan_timing_fast *cmd;
1448ac8321eSStephane Grosjean
1458ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_FAST);
1468ac8321eSStephane Grosjean
1478ac8321eSStephane Grosjean cmd->sjw = PUCAN_TFAST_SJW(pbt->sjw - 1);
1488ac8321eSStephane Grosjean cmd->tseg1 = PUCAN_TFAST_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
1498ac8321eSStephane Grosjean cmd->tseg2 = PUCAN_TFAST_TSEG2(pbt->phase_seg2 - 1);
1508ac8321eSStephane Grosjean cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(pbt->brp - 1));
1518ac8321eSStephane Grosjean
1528ac8321eSStephane Grosjean netdev_dbg(priv->ndev,
1538ac8321eSStephane Grosjean "data: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
1548ac8321eSStephane Grosjean le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw);
1558ac8321eSStephane Grosjean
1568ac8321eSStephane Grosjean return pucan_write_cmd(priv);
1578ac8321eSStephane Grosjean }
1588ac8321eSStephane Grosjean
pucan_set_std_filter(struct peak_canfd_priv * priv,u8 row,u32 mask)1598ac8321eSStephane Grosjean static int pucan_set_std_filter(struct peak_canfd_priv *priv, u8 row, u32 mask)
1608ac8321eSStephane Grosjean {
1618ac8321eSStephane Grosjean struct pucan_std_filter *cmd;
1628ac8321eSStephane Grosjean
1638ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_STD_FILTER);
1648ac8321eSStephane Grosjean
1658ac8321eSStephane Grosjean /* all the 11-bits CAN ID values are represented by one bit in a
1668ac8321eSStephane Grosjean * 64 rows array of 32 bits: the upper 6 bits of the CAN ID select the
1678ac8321eSStephane Grosjean * row while the lowest 5 bits select the bit in that row.
1688ac8321eSStephane Grosjean *
1698ac8321eSStephane Grosjean * bit filter
1708ac8321eSStephane Grosjean * 1 passed
1718ac8321eSStephane Grosjean * 0 discarded
1728ac8321eSStephane Grosjean */
1738ac8321eSStephane Grosjean
1748ac8321eSStephane Grosjean /* select the row */
1758ac8321eSStephane Grosjean cmd->idx = row;
1768ac8321eSStephane Grosjean
1778ac8321eSStephane Grosjean /* set/unset bits in the row */
1788ac8321eSStephane Grosjean cmd->mask = cpu_to_le32(mask);
1798ac8321eSStephane Grosjean
1808ac8321eSStephane Grosjean return pucan_write_cmd(priv);
1818ac8321eSStephane Grosjean }
1828ac8321eSStephane Grosjean
pucan_tx_abort(struct peak_canfd_priv * priv,u16 flags)1838ac8321eSStephane Grosjean static int pucan_tx_abort(struct peak_canfd_priv *priv, u16 flags)
1848ac8321eSStephane Grosjean {
1858ac8321eSStephane Grosjean struct pucan_tx_abort *cmd;
1868ac8321eSStephane Grosjean
1878ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TX_ABORT);
1888ac8321eSStephane Grosjean
1898ac8321eSStephane Grosjean cmd->flags = cpu_to_le16(flags);
1908ac8321eSStephane Grosjean
1918ac8321eSStephane Grosjean return pucan_write_cmd(priv);
1928ac8321eSStephane Grosjean }
1938ac8321eSStephane Grosjean
pucan_clr_err_counters(struct peak_canfd_priv * priv)1948ac8321eSStephane Grosjean static int pucan_clr_err_counters(struct peak_canfd_priv *priv)
1958ac8321eSStephane Grosjean {
1968ac8321eSStephane Grosjean struct pucan_wr_err_cnt *cmd;
1978ac8321eSStephane Grosjean
1988ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_WR_ERR_CNT);
1998ac8321eSStephane Grosjean
2008ac8321eSStephane Grosjean cmd->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE | PUCAN_WRERRCNT_RE);
2018ac8321eSStephane Grosjean cmd->tx_counter = 0;
2028ac8321eSStephane Grosjean cmd->rx_counter = 0;
2038ac8321eSStephane Grosjean
2048ac8321eSStephane Grosjean return pucan_write_cmd(priv);
2058ac8321eSStephane Grosjean }
2068ac8321eSStephane Grosjean
pucan_set_options(struct peak_canfd_priv * priv,u16 opt_mask)2078ac8321eSStephane Grosjean static int pucan_set_options(struct peak_canfd_priv *priv, u16 opt_mask)
2088ac8321eSStephane Grosjean {
2098ac8321eSStephane Grosjean struct pucan_options *cmd;
2108ac8321eSStephane Grosjean
2118ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_EN_OPTION);
2128ac8321eSStephane Grosjean
2138ac8321eSStephane Grosjean cmd->options = cpu_to_le16(opt_mask);
2148ac8321eSStephane Grosjean
2158ac8321eSStephane Grosjean return pucan_write_cmd(priv);
2168ac8321eSStephane Grosjean }
2178ac8321eSStephane Grosjean
pucan_clr_options(struct peak_canfd_priv * priv,u16 opt_mask)2188ac8321eSStephane Grosjean static int pucan_clr_options(struct peak_canfd_priv *priv, u16 opt_mask)
2198ac8321eSStephane Grosjean {
2208ac8321eSStephane Grosjean struct pucan_options *cmd;
2218ac8321eSStephane Grosjean
2228ac8321eSStephane Grosjean cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_CLR_DIS_OPTION);
2238ac8321eSStephane Grosjean
2248ac8321eSStephane Grosjean cmd->options = cpu_to_le16(opt_mask);
2258ac8321eSStephane Grosjean
2268ac8321eSStephane Grosjean return pucan_write_cmd(priv);
2278ac8321eSStephane Grosjean }
2288ac8321eSStephane Grosjean
pucan_setup_rx_barrier(struct peak_canfd_priv * priv)2298ac8321eSStephane Grosjean static int pucan_setup_rx_barrier(struct peak_canfd_priv *priv)
2308ac8321eSStephane Grosjean {
2318ac8321eSStephane Grosjean pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RX_BARRIER);
2328ac8321eSStephane Grosjean
2338ac8321eSStephane Grosjean return pucan_write_cmd(priv);
2348ac8321eSStephane Grosjean }
2358ac8321eSStephane Grosjean
pucan_netif_rx(struct sk_buff * skb,__le32 ts_low,__le32 ts_high)2362b1a4547SStephane Grosjean static int pucan_netif_rx(struct sk_buff *skb, __le32 ts_low, __le32 ts_high)
2372b1a4547SStephane Grosjean {
2382b1a4547SStephane Grosjean struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
2392b1a4547SStephane Grosjean u64 ts_us;
2402b1a4547SStephane Grosjean
2412b1a4547SStephane Grosjean ts_us = (u64)le32_to_cpu(ts_high) << 32;
2422b1a4547SStephane Grosjean ts_us |= le32_to_cpu(ts_low);
2432b1a4547SStephane Grosjean
2442b1a4547SStephane Grosjean /* IP core timestamps are µs. */
2452b1a4547SStephane Grosjean hwts->hwtstamp = ns_to_ktime(ts_us * NSEC_PER_USEC);
2462b1a4547SStephane Grosjean
2472b1a4547SStephane Grosjean return netif_rx(skb);
2482b1a4547SStephane Grosjean }
2492b1a4547SStephane Grosjean
2508ac8321eSStephane Grosjean /* handle the reception of one CAN frame */
pucan_handle_can_rx(struct peak_canfd_priv * priv,struct pucan_rx_msg * msg)2518ac8321eSStephane Grosjean static int pucan_handle_can_rx(struct peak_canfd_priv *priv,
2528ac8321eSStephane Grosjean struct pucan_rx_msg *msg)
2538ac8321eSStephane Grosjean {
2548ac8321eSStephane Grosjean struct net_device_stats *stats = &priv->ndev->stats;
2558ac8321eSStephane Grosjean struct canfd_frame *cf;
2568ac8321eSStephane Grosjean struct sk_buff *skb;
2578ac8321eSStephane Grosjean const u16 rx_msg_flags = le16_to_cpu(msg->flags);
2588ac8321eSStephane Grosjean u8 cf_len;
2598ac8321eSStephane Grosjean
2608ac8321eSStephane Grosjean if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN)
2613ab4ce0dSOliver Hartkopp cf_len = can_fd_dlc2len(pucan_msg_get_dlc(msg));
2628ac8321eSStephane Grosjean else
26369d98969SOliver Hartkopp cf_len = can_cc_dlc2len(pucan_msg_get_dlc(msg));
2648ac8321eSStephane Grosjean
2658ac8321eSStephane Grosjean /* if this frame is an echo, */
26693ef65e5SStephane Grosjean if (rx_msg_flags & PUCAN_MSG_LOOPED_BACK) {
2678ac8321eSStephane Grosjean unsigned long flags;
2688ac8321eSStephane Grosjean
2698ac8321eSStephane Grosjean spin_lock_irqsave(&priv->echo_lock, flags);
2708ac8321eSStephane Grosjean
2718ac8321eSStephane Grosjean /* count bytes of the echo instead of skb */
272cc4b08c3SVincent Mailhol stats->tx_bytes += can_get_echo_skb(priv->ndev, msg->client, NULL);
2738ac8321eSStephane Grosjean stats->tx_packets++;
2748ac8321eSStephane Grosjean
27591785de6SStephane Grosjean /* restart tx queue (a slot is free) */
2768ac8321eSStephane Grosjean netif_wake_queue(priv->ndev);
2778ac8321eSStephane Grosjean
278e6048a00SStephane Grosjean spin_unlock_irqrestore(&priv->echo_lock, flags);
27993ef65e5SStephane Grosjean
28093ef65e5SStephane Grosjean /* if this frame is only an echo, stop here. Otherwise,
28193ef65e5SStephane Grosjean * continue to push this application self-received frame into
28293ef65e5SStephane Grosjean * its own rx queue.
28393ef65e5SStephane Grosjean */
28493ef65e5SStephane Grosjean if (!(rx_msg_flags & PUCAN_MSG_SELF_RECEIVE))
2858ac8321eSStephane Grosjean return 0;
2868ac8321eSStephane Grosjean }
2878ac8321eSStephane Grosjean
2888ac8321eSStephane Grosjean /* otherwise, it should be pushed into rx fifo */
2898ac8321eSStephane Grosjean if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
2908ac8321eSStephane Grosjean /* CANFD frame case */
2918ac8321eSStephane Grosjean skb = alloc_canfd_skb(priv->ndev, &cf);
2928ac8321eSStephane Grosjean if (!skb)
2938ac8321eSStephane Grosjean return -ENOMEM;
2948ac8321eSStephane Grosjean
2958ac8321eSStephane Grosjean if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
2968ac8321eSStephane Grosjean cf->flags |= CANFD_BRS;
2978ac8321eSStephane Grosjean
2988ac8321eSStephane Grosjean if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
2998ac8321eSStephane Grosjean cf->flags |= CANFD_ESI;
3008ac8321eSStephane Grosjean } else {
3018ac8321eSStephane Grosjean /* CAN 2.0 frame case */
3028ac8321eSStephane Grosjean skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
3038ac8321eSStephane Grosjean if (!skb)
3048ac8321eSStephane Grosjean return -ENOMEM;
3058ac8321eSStephane Grosjean }
3068ac8321eSStephane Grosjean
3078ac8321eSStephane Grosjean cf->can_id = le32_to_cpu(msg->can_id);
3088ac8321eSStephane Grosjean cf->len = cf_len;
3098ac8321eSStephane Grosjean
3108ac8321eSStephane Grosjean if (rx_msg_flags & PUCAN_MSG_EXT_ID)
3118ac8321eSStephane Grosjean cf->can_id |= CAN_EFF_FLAG;
3128ac8321eSStephane Grosjean
3138e674ca7SVincent Mailhol if (rx_msg_flags & PUCAN_MSG_RTR) {
3148ac8321eSStephane Grosjean cf->can_id |= CAN_RTR_FLAG;
3158e674ca7SVincent Mailhol } else {
3168ac8321eSStephane Grosjean memcpy(cf->data, msg->d, cf->len);
3178ac8321eSStephane Grosjean
3188ac8321eSStephane Grosjean stats->rx_bytes += cf->len;
3198e674ca7SVincent Mailhol }
3208ac8321eSStephane Grosjean stats->rx_packets++;
3218ac8321eSStephane Grosjean
3222b1a4547SStephane Grosjean pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
3238ac8321eSStephane Grosjean
3248ac8321eSStephane Grosjean return 0;
3258ac8321eSStephane Grosjean }
3268ac8321eSStephane Grosjean
3278ac8321eSStephane Grosjean /* handle rx/tx error counters notification */
pucan_handle_error(struct peak_canfd_priv * priv,struct pucan_error_msg * msg)3288ac8321eSStephane Grosjean static int pucan_handle_error(struct peak_canfd_priv *priv,
3298ac8321eSStephane Grosjean struct pucan_error_msg *msg)
3308ac8321eSStephane Grosjean {
3318ac8321eSStephane Grosjean priv->bec.txerr = msg->tx_err_cnt;
3328ac8321eSStephane Grosjean priv->bec.rxerr = msg->rx_err_cnt;
3338ac8321eSStephane Grosjean
3348ac8321eSStephane Grosjean return 0;
3358ac8321eSStephane Grosjean }
3368ac8321eSStephane Grosjean
3378ac8321eSStephane Grosjean /* handle status notification */
pucan_handle_status(struct peak_canfd_priv * priv,struct pucan_status_msg * msg)3388ac8321eSStephane Grosjean static int pucan_handle_status(struct peak_canfd_priv *priv,
3398ac8321eSStephane Grosjean struct pucan_status_msg *msg)
3408ac8321eSStephane Grosjean {
3418ac8321eSStephane Grosjean struct net_device *ndev = priv->ndev;
3428ac8321eSStephane Grosjean struct net_device_stats *stats = &ndev->stats;
3438ac8321eSStephane Grosjean struct can_frame *cf;
3448ac8321eSStephane Grosjean struct sk_buff *skb;
3458ac8321eSStephane Grosjean
3468ac8321eSStephane Grosjean /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */
3478ac8321eSStephane Grosjean if (pucan_status_is_rx_barrier(msg)) {
3488ac8321eSStephane Grosjean if (priv->enable_tx_path) {
3498ac8321eSStephane Grosjean int err = priv->enable_tx_path(priv);
3508ac8321eSStephane Grosjean
3518ac8321eSStephane Grosjean if (err)
3528ac8321eSStephane Grosjean return err;
3538ac8321eSStephane Grosjean }
3548ac8321eSStephane Grosjean
355b17233d3SStephane Grosjean /* wake network queue up (echo_skb array is empty) */
356b17233d3SStephane Grosjean netif_wake_queue(ndev);
3578ac8321eSStephane Grosjean
3588ac8321eSStephane Grosjean return 0;
3598ac8321eSStephane Grosjean }
3608ac8321eSStephane Grosjean
3618ac8321eSStephane Grosjean skb = alloc_can_err_skb(ndev, &cf);
3628ac8321eSStephane Grosjean
3638ac8321eSStephane Grosjean /* test state error bits according to their priority */
3648ac8321eSStephane Grosjean if (pucan_status_is_busoff(msg)) {
3658ac8321eSStephane Grosjean netdev_dbg(ndev, "Bus-off entry status\n");
3668ac8321eSStephane Grosjean priv->can.state = CAN_STATE_BUS_OFF;
3678ac8321eSStephane Grosjean priv->can.can_stats.bus_off++;
3688ac8321eSStephane Grosjean can_bus_off(ndev);
3698ac8321eSStephane Grosjean if (skb)
3708ac8321eSStephane Grosjean cf->can_id |= CAN_ERR_BUSOFF;
3718ac8321eSStephane Grosjean
3728ac8321eSStephane Grosjean } else if (pucan_status_is_passive(msg)) {
3738ac8321eSStephane Grosjean netdev_dbg(ndev, "Error passive status\n");
3748ac8321eSStephane Grosjean priv->can.state = CAN_STATE_ERROR_PASSIVE;
3758ac8321eSStephane Grosjean priv->can.can_stats.error_passive++;
3768ac8321eSStephane Grosjean if (skb) {
3773e5c291cSVincent Mailhol cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
3788ac8321eSStephane Grosjean cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
3798ac8321eSStephane Grosjean CAN_ERR_CRTL_TX_PASSIVE :
3808ac8321eSStephane Grosjean CAN_ERR_CRTL_RX_PASSIVE;
3818ac8321eSStephane Grosjean cf->data[6] = priv->bec.txerr;
3828ac8321eSStephane Grosjean cf->data[7] = priv->bec.rxerr;
3838ac8321eSStephane Grosjean }
3848ac8321eSStephane Grosjean
3858ac8321eSStephane Grosjean } else if (pucan_status_is_warning(msg)) {
3868ac8321eSStephane Grosjean netdev_dbg(ndev, "Error warning status\n");
3878ac8321eSStephane Grosjean priv->can.state = CAN_STATE_ERROR_WARNING;
3888ac8321eSStephane Grosjean priv->can.can_stats.error_warning++;
3898ac8321eSStephane Grosjean if (skb) {
3903e5c291cSVincent Mailhol cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
3918ac8321eSStephane Grosjean cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
3928ac8321eSStephane Grosjean CAN_ERR_CRTL_TX_WARNING :
3938ac8321eSStephane Grosjean CAN_ERR_CRTL_RX_WARNING;
3948ac8321eSStephane Grosjean cf->data[6] = priv->bec.txerr;
3958ac8321eSStephane Grosjean cf->data[7] = priv->bec.rxerr;
3968ac8321eSStephane Grosjean }
3978ac8321eSStephane Grosjean
3988ac8321eSStephane Grosjean } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) {
3998ac8321eSStephane Grosjean /* back to ERROR_ACTIVE */
4008ac8321eSStephane Grosjean netdev_dbg(ndev, "Error active status\n");
4018ac8321eSStephane Grosjean can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE,
4028ac8321eSStephane Grosjean CAN_STATE_ERROR_ACTIVE);
4038ac8321eSStephane Grosjean } else {
4048ac8321eSStephane Grosjean dev_kfree_skb(skb);
4058ac8321eSStephane Grosjean return 0;
4068ac8321eSStephane Grosjean }
4078ac8321eSStephane Grosjean
4088ac8321eSStephane Grosjean if (!skb) {
4098ac8321eSStephane Grosjean stats->rx_dropped++;
4108ac8321eSStephane Grosjean return -ENOMEM;
4118ac8321eSStephane Grosjean }
4128ac8321eSStephane Grosjean
4132b1a4547SStephane Grosjean pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
4148ac8321eSStephane Grosjean
4158ac8321eSStephane Grosjean return 0;
4168ac8321eSStephane Grosjean }
4178ac8321eSStephane Grosjean
4188ac8321eSStephane Grosjean /* handle uCAN Rx overflow notification */
pucan_handle_cache_critical(struct peak_canfd_priv * priv)4198ac8321eSStephane Grosjean static int pucan_handle_cache_critical(struct peak_canfd_priv *priv)
4208ac8321eSStephane Grosjean {
4218ac8321eSStephane Grosjean struct net_device_stats *stats = &priv->ndev->stats;
4228ac8321eSStephane Grosjean struct can_frame *cf;
4238ac8321eSStephane Grosjean struct sk_buff *skb;
4248ac8321eSStephane Grosjean
4258ac8321eSStephane Grosjean stats->rx_over_errors++;
4268ac8321eSStephane Grosjean stats->rx_errors++;
4278ac8321eSStephane Grosjean
4288ac8321eSStephane Grosjean skb = alloc_can_err_skb(priv->ndev, &cf);
4298ac8321eSStephane Grosjean if (!skb) {
4308ac8321eSStephane Grosjean stats->rx_dropped++;
4318ac8321eSStephane Grosjean return -ENOMEM;
4328ac8321eSStephane Grosjean }
4338ac8321eSStephane Grosjean
4343e5c291cSVincent Mailhol cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
4358ac8321eSStephane Grosjean cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
4368ac8321eSStephane Grosjean
4378ac8321eSStephane Grosjean cf->data[6] = priv->bec.txerr;
4388ac8321eSStephane Grosjean cf->data[7] = priv->bec.rxerr;
4398ac8321eSStephane Grosjean
4408ac8321eSStephane Grosjean netif_rx(skb);
4418ac8321eSStephane Grosjean
4428ac8321eSStephane Grosjean return 0;
4438ac8321eSStephane Grosjean }
4448ac8321eSStephane Grosjean
4458ac8321eSStephane Grosjean /* handle a single uCAN message */
peak_canfd_handle_msg(struct peak_canfd_priv * priv,struct pucan_rx_msg * msg)4468ac8321eSStephane Grosjean int peak_canfd_handle_msg(struct peak_canfd_priv *priv,
4478ac8321eSStephane Grosjean struct pucan_rx_msg *msg)
4488ac8321eSStephane Grosjean {
4498ac8321eSStephane Grosjean u16 msg_type = le16_to_cpu(msg->type);
4508ac8321eSStephane Grosjean int msg_size = le16_to_cpu(msg->size);
4518ac8321eSStephane Grosjean int err;
4528ac8321eSStephane Grosjean
4538ac8321eSStephane Grosjean if (!msg_size || !msg_type) {
4548ac8321eSStephane Grosjean /* null packet found: end of list */
4558ac8321eSStephane Grosjean goto exit;
4568ac8321eSStephane Grosjean }
4578ac8321eSStephane Grosjean
4588ac8321eSStephane Grosjean switch (msg_type) {
4598ac8321eSStephane Grosjean case PUCAN_MSG_CAN_RX:
4608ac8321eSStephane Grosjean err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg);
4618ac8321eSStephane Grosjean break;
4628ac8321eSStephane Grosjean case PUCAN_MSG_ERROR:
4638ac8321eSStephane Grosjean err = pucan_handle_error(priv, (struct pucan_error_msg *)msg);
4648ac8321eSStephane Grosjean break;
4658ac8321eSStephane Grosjean case PUCAN_MSG_STATUS:
4668ac8321eSStephane Grosjean err = pucan_handle_status(priv, (struct pucan_status_msg *)msg);
4678ac8321eSStephane Grosjean break;
4688ac8321eSStephane Grosjean case PUCAN_MSG_CACHE_CRITICAL:
4698ac8321eSStephane Grosjean err = pucan_handle_cache_critical(priv);
4708ac8321eSStephane Grosjean break;
4718ac8321eSStephane Grosjean default:
4728ac8321eSStephane Grosjean err = 0;
4738ac8321eSStephane Grosjean }
4748ac8321eSStephane Grosjean
4758ac8321eSStephane Grosjean if (err < 0)
4768ac8321eSStephane Grosjean return err;
4778ac8321eSStephane Grosjean
4788ac8321eSStephane Grosjean exit:
4798ac8321eSStephane Grosjean return msg_size;
4808ac8321eSStephane Grosjean }
4818ac8321eSStephane Grosjean
4828ac8321eSStephane Grosjean /* handle a list of rx_count messages from rx_msg memory address */
peak_canfd_handle_msgs_list(struct peak_canfd_priv * priv,struct pucan_rx_msg * msg_list,int msg_count)4838ac8321eSStephane Grosjean int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv,
4848ac8321eSStephane Grosjean struct pucan_rx_msg *msg_list, int msg_count)
4858ac8321eSStephane Grosjean {
4868ac8321eSStephane Grosjean void *msg_ptr = msg_list;
487f2a918b4SStephane Grosjean int i, msg_size = 0;
4888ac8321eSStephane Grosjean
4898ac8321eSStephane Grosjean for (i = 0; i < msg_count; i++) {
4908ac8321eSStephane Grosjean msg_size = peak_canfd_handle_msg(priv, msg_ptr);
4918ac8321eSStephane Grosjean
4928ac8321eSStephane Grosjean /* a null packet can be found at the end of a list */
4938ac8321eSStephane Grosjean if (msg_size <= 0)
4948ac8321eSStephane Grosjean break;
4958ac8321eSStephane Grosjean
4960cccf0abSStephane Grosjean msg_ptr += ALIGN(msg_size, 4);
4978ac8321eSStephane Grosjean }
4988ac8321eSStephane Grosjean
4998ac8321eSStephane Grosjean if (msg_size < 0)
5008ac8321eSStephane Grosjean return msg_size;
5018ac8321eSStephane Grosjean
5028ac8321eSStephane Grosjean return i;
5038ac8321eSStephane Grosjean }
5048ac8321eSStephane Grosjean
peak_canfd_start(struct peak_canfd_priv * priv)5058ac8321eSStephane Grosjean static int peak_canfd_start(struct peak_canfd_priv *priv)
5068ac8321eSStephane Grosjean {
5078ac8321eSStephane Grosjean int err;
5088ac8321eSStephane Grosjean
5098ac8321eSStephane Grosjean err = pucan_clr_err_counters(priv);
5108ac8321eSStephane Grosjean if (err)
5118ac8321eSStephane Grosjean goto err_exit;
5128ac8321eSStephane Grosjean
5138ac8321eSStephane Grosjean priv->echo_idx = 0;
5148ac8321eSStephane Grosjean
5158ac8321eSStephane Grosjean priv->bec.txerr = 0;
5168ac8321eSStephane Grosjean priv->bec.rxerr = 0;
5178ac8321eSStephane Grosjean
5188ac8321eSStephane Grosjean if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
5198ac8321eSStephane Grosjean err = pucan_set_listen_only_mode(priv);
5208ac8321eSStephane Grosjean else
5218ac8321eSStephane Grosjean err = pucan_set_normal_mode(priv);
5228ac8321eSStephane Grosjean
5238ac8321eSStephane Grosjean err_exit:
5248ac8321eSStephane Grosjean return err;
5258ac8321eSStephane Grosjean }
5268ac8321eSStephane Grosjean
peak_canfd_stop(struct peak_canfd_priv * priv)5278ac8321eSStephane Grosjean static void peak_canfd_stop(struct peak_canfd_priv *priv)
5288ac8321eSStephane Grosjean {
5298ac8321eSStephane Grosjean int err;
5308ac8321eSStephane Grosjean
5318ac8321eSStephane Grosjean /* go back to RESET mode */
5328ac8321eSStephane Grosjean err = pucan_set_reset_mode(priv);
5338ac8321eSStephane Grosjean if (err) {
5348ac8321eSStephane Grosjean netdev_err(priv->ndev, "channel %u reset failed\n",
5358ac8321eSStephane Grosjean priv->index);
5368ac8321eSStephane Grosjean } else {
5378ac8321eSStephane Grosjean /* abort last Tx (MUST be done in RESET mode only!) */
5388ac8321eSStephane Grosjean pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH);
5398ac8321eSStephane Grosjean }
5408ac8321eSStephane Grosjean }
5418ac8321eSStephane Grosjean
peak_canfd_set_mode(struct net_device * ndev,enum can_mode mode)5428ac8321eSStephane Grosjean static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
5438ac8321eSStephane Grosjean {
5448ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
5458ac8321eSStephane Grosjean
5468ac8321eSStephane Grosjean switch (mode) {
5478ac8321eSStephane Grosjean case CAN_MODE_START:
5488ac8321eSStephane Grosjean peak_canfd_start(priv);
5498ac8321eSStephane Grosjean netif_wake_queue(ndev);
5508ac8321eSStephane Grosjean break;
5518ac8321eSStephane Grosjean default:
5528ac8321eSStephane Grosjean return -EOPNOTSUPP;
5538ac8321eSStephane Grosjean }
5548ac8321eSStephane Grosjean
5558ac8321eSStephane Grosjean return 0;
5568ac8321eSStephane Grosjean }
5578ac8321eSStephane Grosjean
peak_canfd_get_berr_counter(const struct net_device * ndev,struct can_berr_counter * bec)5588ac8321eSStephane Grosjean static int peak_canfd_get_berr_counter(const struct net_device *ndev,
5598ac8321eSStephane Grosjean struct can_berr_counter *bec)
5608ac8321eSStephane Grosjean {
5618ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
5628ac8321eSStephane Grosjean
5638ac8321eSStephane Grosjean *bec = priv->bec;
5648ac8321eSStephane Grosjean return 0;
5658ac8321eSStephane Grosjean }
5668ac8321eSStephane Grosjean
peak_canfd_open(struct net_device * ndev)5678ac8321eSStephane Grosjean static int peak_canfd_open(struct net_device *ndev)
5688ac8321eSStephane Grosjean {
5698ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
5708ac8321eSStephane Grosjean int i, err = 0;
5718ac8321eSStephane Grosjean
5728ac8321eSStephane Grosjean err = open_candev(ndev);
5738ac8321eSStephane Grosjean if (err) {
5748ac8321eSStephane Grosjean netdev_err(ndev, "open_candev() failed, error %d\n", err);
5758ac8321eSStephane Grosjean goto err_exit;
5768ac8321eSStephane Grosjean }
5778ac8321eSStephane Grosjean
5788ac8321eSStephane Grosjean err = pucan_set_reset_mode(priv);
5798ac8321eSStephane Grosjean if (err)
5808ac8321eSStephane Grosjean goto err_close;
5818ac8321eSStephane Grosjean
5828ac8321eSStephane Grosjean if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
5838ac8321eSStephane Grosjean if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
5848ac8321eSStephane Grosjean err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO);
5858ac8321eSStephane Grosjean else
5868ac8321eSStephane Grosjean err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO);
5878ac8321eSStephane Grosjean
5888ac8321eSStephane Grosjean if (err)
5898ac8321eSStephane Grosjean goto err_close;
5908ac8321eSStephane Grosjean }
5918ac8321eSStephane Grosjean
5928ac8321eSStephane Grosjean /* set option: get rx/tx error counters */
5938ac8321eSStephane Grosjean err = pucan_set_options(priv, PUCAN_OPTION_ERROR);
5948ac8321eSStephane Grosjean if (err)
5958ac8321eSStephane Grosjean goto err_close;
5968ac8321eSStephane Grosjean
5978ac8321eSStephane Grosjean /* accept all standard CAN ID */
5988ac8321eSStephane Grosjean for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++)
5998ac8321eSStephane Grosjean pucan_set_std_filter(priv, i, 0xffffffff);
6008ac8321eSStephane Grosjean
6018ac8321eSStephane Grosjean err = peak_canfd_start(priv);
6028ac8321eSStephane Grosjean if (err)
6038ac8321eSStephane Grosjean goto err_close;
6048ac8321eSStephane Grosjean
6058ac8321eSStephane Grosjean /* receiving the RB status says when Tx path is ready */
6068ac8321eSStephane Grosjean err = pucan_setup_rx_barrier(priv);
6078ac8321eSStephane Grosjean if (!err)
6088ac8321eSStephane Grosjean goto err_exit;
6098ac8321eSStephane Grosjean
6108ac8321eSStephane Grosjean err_close:
6118ac8321eSStephane Grosjean close_candev(ndev);
6128ac8321eSStephane Grosjean err_exit:
6138ac8321eSStephane Grosjean return err;
6148ac8321eSStephane Grosjean }
6158ac8321eSStephane Grosjean
peak_canfd_set_bittiming(struct net_device * ndev)6168ac8321eSStephane Grosjean static int peak_canfd_set_bittiming(struct net_device *ndev)
6178ac8321eSStephane Grosjean {
6188ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
6198ac8321eSStephane Grosjean
6208ac8321eSStephane Grosjean return pucan_set_timing_slow(priv, &priv->can.bittiming);
6218ac8321eSStephane Grosjean }
6228ac8321eSStephane Grosjean
peak_canfd_set_data_bittiming(struct net_device * ndev)6238ac8321eSStephane Grosjean static int peak_canfd_set_data_bittiming(struct net_device *ndev)
6248ac8321eSStephane Grosjean {
6258ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
6268ac8321eSStephane Grosjean
6278ac8321eSStephane Grosjean return pucan_set_timing_fast(priv, &priv->can.data_bittiming);
6288ac8321eSStephane Grosjean }
6298ac8321eSStephane Grosjean
peak_canfd_close(struct net_device * ndev)6308ac8321eSStephane Grosjean static int peak_canfd_close(struct net_device *ndev)
6318ac8321eSStephane Grosjean {
6328ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
6338ac8321eSStephane Grosjean
6348ac8321eSStephane Grosjean netif_stop_queue(ndev);
6358ac8321eSStephane Grosjean peak_canfd_stop(priv);
6368ac8321eSStephane Grosjean close_candev(ndev);
6378ac8321eSStephane Grosjean
6388ac8321eSStephane Grosjean return 0;
6398ac8321eSStephane Grosjean }
6408ac8321eSStephane Grosjean
peak_canfd_start_xmit(struct sk_buff * skb,struct net_device * ndev)6418ac8321eSStephane Grosjean static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb,
6428ac8321eSStephane Grosjean struct net_device *ndev)
6438ac8321eSStephane Grosjean {
6448ac8321eSStephane Grosjean struct peak_canfd_priv *priv = netdev_priv(ndev);
6458ac8321eSStephane Grosjean struct net_device_stats *stats = &ndev->stats;
6468ac8321eSStephane Grosjean struct canfd_frame *cf = (struct canfd_frame *)skb->data;
6478ac8321eSStephane Grosjean struct pucan_tx_msg *msg;
6488ac8321eSStephane Grosjean u16 msg_size, msg_flags;
6498ac8321eSStephane Grosjean unsigned long flags;
6508ac8321eSStephane Grosjean bool should_stop_tx_queue;
6518ac8321eSStephane Grosjean int room_left;
652c7b74967SOliver Hartkopp u8 len;
6538ac8321eSStephane Grosjean
654*ae64438bSOliver Hartkopp if (can_dev_dropped_skb(ndev, skb))
6558ac8321eSStephane Grosjean return NETDEV_TX_OK;
6568ac8321eSStephane Grosjean
6578ac8321eSStephane Grosjean msg_size = ALIGN(sizeof(*msg) + cf->len, 4);
6588ac8321eSStephane Grosjean msg = priv->alloc_tx_msg(priv, msg_size, &room_left);
6598ac8321eSStephane Grosjean
6608ac8321eSStephane Grosjean /* should never happen except under bus-off condition and (auto-)restart
6618ac8321eSStephane Grosjean * mechanism
6628ac8321eSStephane Grosjean */
6638ac8321eSStephane Grosjean if (!msg) {
6648ac8321eSStephane Grosjean stats->tx_dropped++;
6658ac8321eSStephane Grosjean netif_stop_queue(ndev);
6668ac8321eSStephane Grosjean return NETDEV_TX_BUSY;
6678ac8321eSStephane Grosjean }
6688ac8321eSStephane Grosjean
6698ac8321eSStephane Grosjean msg->size = cpu_to_le16(msg_size);
6708ac8321eSStephane Grosjean msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
6718ac8321eSStephane Grosjean msg_flags = 0;
6728ac8321eSStephane Grosjean
6738ac8321eSStephane Grosjean if (cf->can_id & CAN_EFF_FLAG) {
6748ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_EXT_ID;
6758ac8321eSStephane Grosjean msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK);
6768ac8321eSStephane Grosjean } else {
6778ac8321eSStephane Grosjean msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK);
6788ac8321eSStephane Grosjean }
6798ac8321eSStephane Grosjean
6808ac8321eSStephane Grosjean if (can_is_canfd_skb(skb)) {
6818ac8321eSStephane Grosjean /* CAN FD frame format */
6823ab4ce0dSOliver Hartkopp len = can_fd_len2dlc(cf->len);
6838ac8321eSStephane Grosjean
6848ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
6858ac8321eSStephane Grosjean
6868ac8321eSStephane Grosjean if (cf->flags & CANFD_BRS)
6878ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
6888ac8321eSStephane Grosjean
6898ac8321eSStephane Grosjean if (cf->flags & CANFD_ESI)
6908ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
6918ac8321eSStephane Grosjean } else {
6928ac8321eSStephane Grosjean /* CAN 2.0 frame format */
693c7b74967SOliver Hartkopp len = cf->len;
6948ac8321eSStephane Grosjean
6958ac8321eSStephane Grosjean if (cf->can_id & CAN_RTR_FLAG)
6968ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_RTR;
6978ac8321eSStephane Grosjean }
6988ac8321eSStephane Grosjean
6998ac8321eSStephane Grosjean /* always ask loopback for echo management */
7008ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_LOOPED_BACK;
7018ac8321eSStephane Grosjean
7028ac8321eSStephane Grosjean /* set driver specific bit to differentiate with application loopback */
7038ac8321eSStephane Grosjean if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
7048ac8321eSStephane Grosjean msg_flags |= PUCAN_MSG_SELF_RECEIVE;
7058ac8321eSStephane Grosjean
7068ac8321eSStephane Grosjean msg->flags = cpu_to_le16(msg_flags);
707c7b74967SOliver Hartkopp msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, len);
7088ac8321eSStephane Grosjean memcpy(msg->d, cf->data, cf->len);
7098ac8321eSStephane Grosjean
7108ac8321eSStephane Grosjean /* struct msg client field is used as an index in the echo skbs ring */
7118ac8321eSStephane Grosjean msg->client = priv->echo_idx;
7128ac8321eSStephane Grosjean
7138ac8321eSStephane Grosjean spin_lock_irqsave(&priv->echo_lock, flags);
7148ac8321eSStephane Grosjean
7158ac8321eSStephane Grosjean /* prepare and save echo skb in internal slot */
7161dcb6e57SVincent Mailhol can_put_echo_skb(skb, ndev, priv->echo_idx, 0);
7178ac8321eSStephane Grosjean
7188ac8321eSStephane Grosjean /* move echo index to the next slot */
7198ac8321eSStephane Grosjean priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max;
7208ac8321eSStephane Grosjean
7218ac8321eSStephane Grosjean /* if next slot is not free, stop network queue (no slot free in echo
7228ac8321eSStephane Grosjean * skb ring means that the controller did not write these frames on
7238ac8321eSStephane Grosjean * the bus: no need to continue).
7248ac8321eSStephane Grosjean */
7258ac8321eSStephane Grosjean should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]);
7268ac8321eSStephane Grosjean
7278ac8321eSStephane Grosjean /* stop network tx queue if not enough room to save one more msg too */
7288ac8321eSStephane Grosjean if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
7298ac8321eSStephane Grosjean should_stop_tx_queue |= (room_left <
7308ac8321eSStephane Grosjean (sizeof(*msg) + CANFD_MAX_DLEN));
7318ac8321eSStephane Grosjean else
7328ac8321eSStephane Grosjean should_stop_tx_queue |= (room_left <
7338ac8321eSStephane Grosjean (sizeof(*msg) + CAN_MAX_DLEN));
7348ac8321eSStephane Grosjean
7358ac8321eSStephane Grosjean if (should_stop_tx_queue)
7368ac8321eSStephane Grosjean netif_stop_queue(ndev);
7378ac8321eSStephane Grosjean
738e6048a00SStephane Grosjean spin_unlock_irqrestore(&priv->echo_lock, flags);
739e6048a00SStephane Grosjean
740e6048a00SStephane Grosjean /* write the skb on the interface */
741e6048a00SStephane Grosjean priv->write_tx_msg(priv, msg);
742e6048a00SStephane Grosjean
7438ac8321eSStephane Grosjean return NETDEV_TX_OK;
7448ac8321eSStephane Grosjean }
7458ac8321eSStephane Grosjean
peak_eth_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)7468ba09bfaSVincent Mailhol static int peak_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7478ba09bfaSVincent Mailhol {
7488ba09bfaSVincent Mailhol struct hwtstamp_config hwts_cfg = { 0 };
7498ba09bfaSVincent Mailhol
7508ba09bfaSVincent Mailhol switch (cmd) {
7518ba09bfaSVincent Mailhol case SIOCSHWTSTAMP: /* set */
7528ba09bfaSVincent Mailhol if (copy_from_user(&hwts_cfg, ifr->ifr_data, sizeof(hwts_cfg)))
7538ba09bfaSVincent Mailhol return -EFAULT;
7548ba09bfaSVincent Mailhol if (hwts_cfg.tx_type == HWTSTAMP_TX_OFF &&
7558ba09bfaSVincent Mailhol hwts_cfg.rx_filter == HWTSTAMP_FILTER_ALL)
7568ba09bfaSVincent Mailhol return 0;
7578ba09bfaSVincent Mailhol return -ERANGE;
7588ba09bfaSVincent Mailhol
7598ba09bfaSVincent Mailhol case SIOCGHWTSTAMP: /* get */
7608ba09bfaSVincent Mailhol hwts_cfg.tx_type = HWTSTAMP_TX_OFF;
7618ba09bfaSVincent Mailhol hwts_cfg.rx_filter = HWTSTAMP_FILTER_ALL;
7628ba09bfaSVincent Mailhol if (copy_to_user(ifr->ifr_data, &hwts_cfg, sizeof(hwts_cfg)))
7638ba09bfaSVincent Mailhol return -EFAULT;
7648ba09bfaSVincent Mailhol return 0;
7658ba09bfaSVincent Mailhol
7668ba09bfaSVincent Mailhol default:
7678ba09bfaSVincent Mailhol return -EOPNOTSUPP;
7688ba09bfaSVincent Mailhol }
7698ba09bfaSVincent Mailhol }
7708ba09bfaSVincent Mailhol
7718ac8321eSStephane Grosjean static const struct net_device_ops peak_canfd_netdev_ops = {
7728ac8321eSStephane Grosjean .ndo_open = peak_canfd_open,
7738ac8321eSStephane Grosjean .ndo_stop = peak_canfd_close,
7748ba09bfaSVincent Mailhol .ndo_eth_ioctl = peak_eth_ioctl,
7758ac8321eSStephane Grosjean .ndo_start_xmit = peak_canfd_start_xmit,
7768ac8321eSStephane Grosjean .ndo_change_mtu = can_change_mtu,
7778ac8321eSStephane Grosjean };
7788ac8321eSStephane Grosjean
peak_get_ts_info(struct net_device * dev,struct ethtool_ts_info * info)7798ba09bfaSVincent Mailhol static int peak_get_ts_info(struct net_device *dev,
7808ba09bfaSVincent Mailhol struct ethtool_ts_info *info)
7818ba09bfaSVincent Mailhol {
7828ba09bfaSVincent Mailhol info->so_timestamping =
7838ba09bfaSVincent Mailhol SOF_TIMESTAMPING_TX_SOFTWARE |
7848ba09bfaSVincent Mailhol SOF_TIMESTAMPING_RX_SOFTWARE |
7858ba09bfaSVincent Mailhol SOF_TIMESTAMPING_SOFTWARE |
7868ba09bfaSVincent Mailhol SOF_TIMESTAMPING_RX_HARDWARE |
7878ba09bfaSVincent Mailhol SOF_TIMESTAMPING_RAW_HARDWARE;
7888ba09bfaSVincent Mailhol info->phc_index = -1;
7898ba09bfaSVincent Mailhol info->tx_types = BIT(HWTSTAMP_TX_OFF);
7908ba09bfaSVincent Mailhol info->rx_filters = BIT(HWTSTAMP_FILTER_ALL);
7918ba09bfaSVincent Mailhol
7928ba09bfaSVincent Mailhol return 0;
7938ba09bfaSVincent Mailhol }
7948ba09bfaSVincent Mailhol
7958ba09bfaSVincent Mailhol static const struct ethtool_ops peak_canfd_ethtool_ops = {
7968ba09bfaSVincent Mailhol .get_ts_info = peak_get_ts_info,
7978ba09bfaSVincent Mailhol };
7988ba09bfaSVincent Mailhol
alloc_peak_canfd_dev(int sizeof_priv,int index,int echo_skb_max)7998ac8321eSStephane Grosjean struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index,
8008ac8321eSStephane Grosjean int echo_skb_max)
8018ac8321eSStephane Grosjean {
8028ac8321eSStephane Grosjean struct net_device *ndev;
8038ac8321eSStephane Grosjean struct peak_canfd_priv *priv;
8048ac8321eSStephane Grosjean
8058ac8321eSStephane Grosjean /* we DO support local echo */
8068ac8321eSStephane Grosjean if (echo_skb_max < 0)
8078ac8321eSStephane Grosjean echo_skb_max = PCANFD_ECHO_SKB_MAX;
8088ac8321eSStephane Grosjean
8098ac8321eSStephane Grosjean /* allocate the candev object */
8108ac8321eSStephane Grosjean ndev = alloc_candev(sizeof_priv, echo_skb_max);
8118ac8321eSStephane Grosjean if (!ndev)
8128ac8321eSStephane Grosjean return NULL;
8138ac8321eSStephane Grosjean
8148ac8321eSStephane Grosjean priv = netdev_priv(ndev);
8158ac8321eSStephane Grosjean
8168ac8321eSStephane Grosjean /* complete now socket-can initialization side */
8178ac8321eSStephane Grosjean priv->can.state = CAN_STATE_STOPPED;
8188ac8321eSStephane Grosjean priv->can.bittiming_const = &peak_canfd_nominal_const;
8198ac8321eSStephane Grosjean priv->can.data_bittiming_const = &peak_canfd_data_const;
8208ac8321eSStephane Grosjean
8218ac8321eSStephane Grosjean priv->can.do_set_mode = peak_canfd_set_mode;
8228ac8321eSStephane Grosjean priv->can.do_get_berr_counter = peak_canfd_get_berr_counter;
8238ac8321eSStephane Grosjean priv->can.do_set_bittiming = peak_canfd_set_bittiming;
8248ac8321eSStephane Grosjean priv->can.do_set_data_bittiming = peak_canfd_set_data_bittiming;
8258ac8321eSStephane Grosjean priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
8268ac8321eSStephane Grosjean CAN_CTRLMODE_LISTENONLY |
8278ac8321eSStephane Grosjean CAN_CTRLMODE_3_SAMPLES |
8288ac8321eSStephane Grosjean CAN_CTRLMODE_FD |
8298ac8321eSStephane Grosjean CAN_CTRLMODE_FD_NON_ISO |
8308ac8321eSStephane Grosjean CAN_CTRLMODE_BERR_REPORTING;
8318ac8321eSStephane Grosjean
8328ac8321eSStephane Grosjean priv->ndev = ndev;
8338ac8321eSStephane Grosjean priv->index = index;
8348ac8321eSStephane Grosjean priv->cmd_len = 0;
8358ac8321eSStephane Grosjean spin_lock_init(&priv->echo_lock);
8368ac8321eSStephane Grosjean
8378ac8321eSStephane Grosjean ndev->flags |= IFF_ECHO;
8388ac8321eSStephane Grosjean ndev->netdev_ops = &peak_canfd_netdev_ops;
8398ba09bfaSVincent Mailhol ndev->ethtool_ops = &peak_canfd_ethtool_ops;
8408ac8321eSStephane Grosjean ndev->dev_id = index;
8418ac8321eSStephane Grosjean
8428ac8321eSStephane Grosjean return ndev;
8438ac8321eSStephane Grosjean }
844