1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (c) 2022 Amarula Solutions, Dario Binacchi <dario.binacchi@amarulasolutions.com>
3  * Copyright (c) 2022 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
4  *
5  */
6 
7 #include <linux/can/dev.h>
8 #include <linux/ethtool.h>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/platform_device.h>
12 
13 #include "flexcan.h"
14 
15 static const char flexcan_priv_flags_strings[][ETH_GSTRING_LEN] = {
16 #define FLEXCAN_PRIV_FLAGS_RX_RTR BIT(0)
17 	"rx-rtr",
18 };
19 
20 static void
21 flexcan_get_ringparam(struct net_device *ndev, struct ethtool_ringparam *ring,
22 		      struct kernel_ethtool_ringparam *kernel_ring,
23 		      struct netlink_ext_ack *ext_ack)
24 {
25 	const struct flexcan_priv *priv = netdev_priv(ndev);
26 
27 	ring->rx_max_pending = priv->mb_count;
28 	ring->tx_max_pending = priv->mb_count;
29 
30 	if (priv->devtype_data.quirks & FLEXCAN_QUIRK_USE_RX_MAILBOX)
31 		ring->rx_pending = priv->offload.mb_last -
32 			priv->offload.mb_first + 1;
33 	else
34 		ring->rx_pending = 6;	/* RX-FIFO depth is fixed */
35 
36 	/* the drive currently supports only on TX buffer */
37 	ring->tx_pending = 1;
38 }
39 
40 static void
41 flexcan_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
42 {
43 	switch (stringset) {
44 	case ETH_SS_PRIV_FLAGS:
45 		memcpy(data, flexcan_priv_flags_strings,
46 		       sizeof(flexcan_priv_flags_strings));
47 	}
48 }
49 
50 static u32 flexcan_get_priv_flags(struct net_device *ndev)
51 {
52 	const struct flexcan_priv *priv = netdev_priv(ndev);
53 	u32 priv_flags = 0;
54 
55 	if (flexcan_active_rx_rtr(priv))
56 		priv_flags |= FLEXCAN_PRIV_FLAGS_RX_RTR;
57 
58 	return priv_flags;
59 }
60 
61 static int flexcan_set_priv_flags(struct net_device *ndev, u32 priv_flags)
62 {
63 	struct flexcan_priv *priv = netdev_priv(ndev);
64 	u32 quirks = priv->devtype_data.quirks;
65 
66 	if (priv_flags & FLEXCAN_PRIV_FLAGS_RX_RTR) {
67 		if (flexcan_supports_rx_mailbox_rtr(priv))
68 			quirks |= FLEXCAN_QUIRK_USE_RX_MAILBOX;
69 		else if (flexcan_supports_rx_fifo(priv))
70 			quirks &= ~FLEXCAN_QUIRK_USE_RX_MAILBOX;
71 		else
72 			quirks |= FLEXCAN_QUIRK_USE_RX_MAILBOX;
73 	} else {
74 		if (flexcan_supports_rx_mailbox(priv))
75 			quirks |= FLEXCAN_QUIRK_USE_RX_MAILBOX;
76 		else
77 			quirks &= ~FLEXCAN_QUIRK_USE_RX_MAILBOX;
78 	}
79 
80 	if (quirks != priv->devtype_data.quirks && netif_running(ndev))
81 		return -EBUSY;
82 
83 	priv->devtype_data.quirks = quirks;
84 
85 	if (!(priv_flags & FLEXCAN_PRIV_FLAGS_RX_RTR) &&
86 	    !flexcan_active_rx_rtr(priv))
87 		netdev_info(ndev,
88 			    "Activating RX mailbox mode, cannot receive RTR frames.\n");
89 
90 	return 0;
91 }
92 
93 static int flexcan_get_sset_count(struct net_device *netdev, int sset)
94 {
95 	switch (sset) {
96 	case ETH_SS_PRIV_FLAGS:
97 		return ARRAY_SIZE(flexcan_priv_flags_strings);
98 	default:
99 		return -EOPNOTSUPP;
100 	}
101 }
102 
103 const struct ethtool_ops flexcan_ethtool_ops = {
104 	.get_ringparam = flexcan_get_ringparam,
105 	.get_strings = flexcan_get_strings,
106 	.get_priv_flags = flexcan_get_priv_flags,
107 	.set_priv_flags = flexcan_set_priv_flags,
108 	.get_sset_count = flexcan_get_sset_count,
109 	.get_ts_info = ethtool_op_get_ts_info,
110 };
111