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