xref: /linux-6.15/drivers/net/netdevsim/ethtool.c (revision 3c836451)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3 
4 #include <linux/debugfs.h>
5 #include <linux/random.h>
6 #include <net/netdev_queues.h>
7 
8 #include "netdevsim.h"
9 
10 static void
11 nsim_get_pause_stats(struct net_device *dev,
12 		     struct ethtool_pause_stats *pause_stats)
13 {
14 	struct netdevsim *ns = netdev_priv(dev);
15 
16 	if (ns->ethtool.pauseparam.report_stats_rx)
17 		pause_stats->rx_pause_frames = 1;
18 	if (ns->ethtool.pauseparam.report_stats_tx)
19 		pause_stats->tx_pause_frames = 2;
20 }
21 
22 static void
23 nsim_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *pause)
24 {
25 	struct netdevsim *ns = netdev_priv(dev);
26 
27 	pause->autoneg = 0; /* We don't support ksettings, so can't pretend */
28 	pause->rx_pause = ns->ethtool.pauseparam.rx;
29 	pause->tx_pause = ns->ethtool.pauseparam.tx;
30 }
31 
32 static int
33 nsim_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *pause)
34 {
35 	struct netdevsim *ns = netdev_priv(dev);
36 
37 	if (pause->autoneg)
38 		return -EINVAL;
39 
40 	ns->ethtool.pauseparam.rx = pause->rx_pause;
41 	ns->ethtool.pauseparam.tx = pause->tx_pause;
42 	return 0;
43 }
44 
45 static int nsim_get_coalesce(struct net_device *dev,
46 			     struct ethtool_coalesce *coal,
47 			     struct kernel_ethtool_coalesce *kernel_coal,
48 			     struct netlink_ext_ack *extack)
49 {
50 	struct netdevsim *ns = netdev_priv(dev);
51 
52 	memcpy(coal, &ns->ethtool.coalesce, sizeof(ns->ethtool.coalesce));
53 	return 0;
54 }
55 
56 static int nsim_set_coalesce(struct net_device *dev,
57 			     struct ethtool_coalesce *coal,
58 			     struct kernel_ethtool_coalesce *kernel_coal,
59 			     struct netlink_ext_ack *extack)
60 {
61 	struct netdevsim *ns = netdev_priv(dev);
62 
63 	memcpy(&ns->ethtool.coalesce, coal, sizeof(ns->ethtool.coalesce));
64 	return 0;
65 }
66 
67 static void nsim_get_ringparam(struct net_device *dev,
68 			       struct ethtool_ringparam *ring,
69 			       struct kernel_ethtool_ringparam *kernel_ring,
70 			       struct netlink_ext_ack *extack)
71 {
72 	struct netdevsim *ns = netdev_priv(dev);
73 
74 	memcpy(ring, &ns->ethtool.ring, sizeof(ns->ethtool.ring));
75 	kernel_ring->tcp_data_split = dev->cfg->hds_config;
76 	kernel_ring->hds_thresh = dev->cfg->hds_thresh;
77 	kernel_ring->hds_thresh_max = NSIM_HDS_THRESHOLD_MAX;
78 
79 	if (kernel_ring->tcp_data_split == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN)
80 		kernel_ring->tcp_data_split = ETHTOOL_TCP_DATA_SPLIT_ENABLED;
81 }
82 
83 static int nsim_set_ringparam(struct net_device *dev,
84 			      struct ethtool_ringparam *ring,
85 			      struct kernel_ethtool_ringparam *kernel_ring,
86 			      struct netlink_ext_ack *extack)
87 {
88 	struct netdevsim *ns = netdev_priv(dev);
89 
90 	ns->ethtool.ring.rx_pending = ring->rx_pending;
91 	ns->ethtool.ring.rx_jumbo_pending = ring->rx_jumbo_pending;
92 	ns->ethtool.ring.rx_mini_pending = ring->rx_mini_pending;
93 	ns->ethtool.ring.tx_pending = ring->tx_pending;
94 	return 0;
95 }
96 
97 static void
98 nsim_get_channels(struct net_device *dev, struct ethtool_channels *ch)
99 {
100 	struct netdevsim *ns = netdev_priv(dev);
101 
102 	ch->max_combined = ns->nsim_bus_dev->num_queues;
103 	ch->combined_count = ns->ethtool.channels;
104 }
105 
106 static int
107 nsim_set_channels(struct net_device *dev, struct ethtool_channels *ch)
108 {
109 	struct netdevsim *ns = netdev_priv(dev);
110 	int err;
111 
112 	netdev_lock(dev);
113 	err = netif_set_real_num_queues(dev, ch->combined_count,
114 					ch->combined_count);
115 	netdev_unlock(dev);
116 	if (err)
117 		return err;
118 
119 	ns->ethtool.channels = ch->combined_count;
120 	return 0;
121 }
122 
123 static int
124 nsim_get_fecparam(struct net_device *dev, struct ethtool_fecparam *fecparam)
125 {
126 	struct netdevsim *ns = netdev_priv(dev);
127 
128 	if (ns->ethtool.get_err)
129 		return -ns->ethtool.get_err;
130 	memcpy(fecparam, &ns->ethtool.fec, sizeof(ns->ethtool.fec));
131 	return 0;
132 }
133 
134 static int
135 nsim_set_fecparam(struct net_device *dev, struct ethtool_fecparam *fecparam)
136 {
137 	struct netdevsim *ns = netdev_priv(dev);
138 	u32 fec;
139 
140 	if (ns->ethtool.set_err)
141 		return -ns->ethtool.set_err;
142 	memcpy(&ns->ethtool.fec, fecparam, sizeof(ns->ethtool.fec));
143 	fec = fecparam->fec;
144 	if (fec == ETHTOOL_FEC_AUTO)
145 		fec |= ETHTOOL_FEC_OFF;
146 	fec |= ETHTOOL_FEC_NONE;
147 	ns->ethtool.fec.active_fec = 1 << (fls(fec) - 1);
148 	return 0;
149 }
150 
151 static void
152 nsim_get_fec_stats(struct net_device *dev, struct ethtool_fec_stats *fec_stats)
153 {
154 	fec_stats->corrected_blocks.total = 123;
155 	fec_stats->uncorrectable_blocks.total = 4;
156 }
157 
158 static int nsim_get_ts_info(struct net_device *dev,
159 			    struct kernel_ethtool_ts_info *info)
160 {
161 	struct netdevsim *ns = netdev_priv(dev);
162 
163 	info->phc_index = mock_phc_index(ns->phc);
164 
165 	return 0;
166 }
167 
168 static const struct ethtool_ops nsim_ethtool_ops = {
169 	.supported_coalesce_params	= ETHTOOL_COALESCE_ALL_PARAMS,
170 	.supported_ring_params		= ETHTOOL_RING_USE_TCP_DATA_SPLIT |
171 					  ETHTOOL_RING_USE_HDS_THRS,
172 	.get_pause_stats	        = nsim_get_pause_stats,
173 	.get_pauseparam		        = nsim_get_pauseparam,
174 	.set_pauseparam		        = nsim_set_pauseparam,
175 	.set_coalesce			= nsim_set_coalesce,
176 	.get_coalesce			= nsim_get_coalesce,
177 	.get_ringparam			= nsim_get_ringparam,
178 	.set_ringparam			= nsim_set_ringparam,
179 	.get_channels			= nsim_get_channels,
180 	.set_channels			= nsim_set_channels,
181 	.get_fecparam			= nsim_get_fecparam,
182 	.set_fecparam			= nsim_set_fecparam,
183 	.get_fec_stats			= nsim_get_fec_stats,
184 	.get_ts_info			= nsim_get_ts_info,
185 };
186 
187 static void nsim_ethtool_ring_init(struct netdevsim *ns)
188 {
189 	ns->ethtool.ring.rx_max_pending = 4096;
190 	ns->ethtool.ring.rx_jumbo_max_pending = 4096;
191 	ns->ethtool.ring.rx_mini_max_pending = 4096;
192 	ns->ethtool.ring.tx_max_pending = 4096;
193 
194 	ns->netdev->cfg->hds_config = ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
195 	ns->netdev->cfg->hds_thresh = 0;
196 }
197 
198 void nsim_ethtool_init(struct netdevsim *ns)
199 {
200 	struct dentry *ethtool, *dir;
201 
202 	ns->netdev->ethtool_ops = &nsim_ethtool_ops;
203 
204 	nsim_ethtool_ring_init(ns);
205 
206 	ns->ethtool.pauseparam.report_stats_rx = true;
207 	ns->ethtool.pauseparam.report_stats_tx = true;
208 
209 	ns->ethtool.fec.fec = ETHTOOL_FEC_NONE;
210 	ns->ethtool.fec.active_fec = ETHTOOL_FEC_NONE;
211 
212 	ns->ethtool.channels = ns->nsim_bus_dev->num_queues;
213 
214 	ethtool = debugfs_create_dir("ethtool", ns->nsim_dev_port->ddir);
215 
216 	debugfs_create_u32("get_err", 0600, ethtool, &ns->ethtool.get_err);
217 	debugfs_create_u32("set_err", 0600, ethtool, &ns->ethtool.set_err);
218 
219 	dir = debugfs_create_dir("pause", ethtool);
220 	debugfs_create_bool("report_stats_rx", 0600, dir,
221 			    &ns->ethtool.pauseparam.report_stats_rx);
222 	debugfs_create_bool("report_stats_tx", 0600, dir,
223 			    &ns->ethtool.pauseparam.report_stats_tx);
224 
225 	dir = debugfs_create_dir("ring", ethtool);
226 	debugfs_create_u32("rx_max_pending", 0600, dir,
227 			   &ns->ethtool.ring.rx_max_pending);
228 	debugfs_create_u32("rx_jumbo_max_pending", 0600, dir,
229 			   &ns->ethtool.ring.rx_jumbo_max_pending);
230 	debugfs_create_u32("rx_mini_max_pending", 0600, dir,
231 			   &ns->ethtool.ring.rx_mini_max_pending);
232 	debugfs_create_u32("tx_max_pending", 0600, dir,
233 			   &ns->ethtool.ring.tx_max_pending);
234 }
235