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
nsim_get_pause_stats(struct net_device * dev,struct ethtool_pause_stats * pause_stats)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
nsim_get_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)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
nsim_set_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)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
nsim_get_coalesce(struct net_device * dev,struct ethtool_coalesce * coal,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)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
nsim_set_coalesce(struct net_device * dev,struct ethtool_coalesce * coal,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)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
nsim_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)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->hds_thresh_max = NSIM_HDS_THRESHOLD_MAX;
76
77 if (dev->cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN)
78 kernel_ring->tcp_data_split = ETHTOOL_TCP_DATA_SPLIT_ENABLED;
79 }
80
nsim_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)81 static int nsim_set_ringparam(struct net_device *dev,
82 struct ethtool_ringparam *ring,
83 struct kernel_ethtool_ringparam *kernel_ring,
84 struct netlink_ext_ack *extack)
85 {
86 struct netdevsim *ns = netdev_priv(dev);
87
88 ns->ethtool.ring.rx_pending = ring->rx_pending;
89 ns->ethtool.ring.rx_jumbo_pending = ring->rx_jumbo_pending;
90 ns->ethtool.ring.rx_mini_pending = ring->rx_mini_pending;
91 ns->ethtool.ring.tx_pending = ring->tx_pending;
92 return 0;
93 }
94
95 static void
nsim_get_channels(struct net_device * dev,struct ethtool_channels * ch)96 nsim_get_channels(struct net_device *dev, struct ethtool_channels *ch)
97 {
98 struct netdevsim *ns = netdev_priv(dev);
99
100 ch->max_combined = ns->nsim_bus_dev->num_queues;
101 ch->combined_count = ns->ethtool.channels;
102 }
103
104 static void
nsim_wake_queues(struct net_device * dev)105 nsim_wake_queues(struct net_device *dev)
106 {
107 struct netdevsim *ns = netdev_priv(dev);
108 struct netdevsim *peer;
109
110 synchronize_net();
111 netif_tx_wake_all_queues(dev);
112
113 rcu_read_lock();
114 peer = rcu_dereference(ns->peer);
115 if (peer)
116 netif_tx_wake_all_queues(peer->netdev);
117 rcu_read_unlock();
118 }
119
120 static int
nsim_set_channels(struct net_device * dev,struct ethtool_channels * ch)121 nsim_set_channels(struct net_device *dev, struct ethtool_channels *ch)
122 {
123 struct netdevsim *ns = netdev_priv(dev);
124 int err;
125
126 err = netif_set_real_num_queues(dev, ch->combined_count,
127 ch->combined_count);
128 if (err)
129 return err;
130
131 ns->ethtool.channels = ch->combined_count;
132
133 /* Only wake up queues if devices are linked */
134 if (rcu_access_pointer(ns->peer))
135 nsim_wake_queues(dev);
136
137 return 0;
138 }
139
140 static int
nsim_get_fecparam(struct net_device * dev,struct ethtool_fecparam * fecparam)141 nsim_get_fecparam(struct net_device *dev, struct ethtool_fecparam *fecparam)
142 {
143 struct netdevsim *ns = netdev_priv(dev);
144
145 if (ns->ethtool.get_err)
146 return -ns->ethtool.get_err;
147 memcpy(fecparam, &ns->ethtool.fec, sizeof(ns->ethtool.fec));
148 return 0;
149 }
150
151 static int
nsim_set_fecparam(struct net_device * dev,struct ethtool_fecparam * fecparam)152 nsim_set_fecparam(struct net_device *dev, struct ethtool_fecparam *fecparam)
153 {
154 struct netdevsim *ns = netdev_priv(dev);
155 u32 fec;
156
157 if (ns->ethtool.set_err)
158 return -ns->ethtool.set_err;
159 memcpy(&ns->ethtool.fec, fecparam, sizeof(ns->ethtool.fec));
160 fec = fecparam->fec;
161 if (fec == ETHTOOL_FEC_AUTO)
162 fec |= ETHTOOL_FEC_OFF;
163 fec |= ETHTOOL_FEC_NONE;
164 ns->ethtool.fec.active_fec = 1 << (fls(fec) - 1);
165 return 0;
166 }
167
168 static void
nsim_get_fec_stats(struct net_device * dev,struct ethtool_fec_stats * fec_stats)169 nsim_get_fec_stats(struct net_device *dev, struct ethtool_fec_stats *fec_stats)
170 {
171 fec_stats->corrected_blocks.total = 123;
172 fec_stats->uncorrectable_blocks.total = 4;
173 }
174
nsim_get_ts_info(struct net_device * dev,struct kernel_ethtool_ts_info * info)175 static int nsim_get_ts_info(struct net_device *dev,
176 struct kernel_ethtool_ts_info *info)
177 {
178 struct netdevsim *ns = netdev_priv(dev);
179
180 info->phc_index = mock_phc_index(ns->phc);
181
182 return 0;
183 }
184
185 static const struct ethtool_ops nsim_ethtool_ops = {
186 .supported_coalesce_params = ETHTOOL_COALESCE_ALL_PARAMS,
187 .supported_ring_params = ETHTOOL_RING_USE_TCP_DATA_SPLIT |
188 ETHTOOL_RING_USE_HDS_THRS,
189 .get_pause_stats = nsim_get_pause_stats,
190 .get_pauseparam = nsim_get_pauseparam,
191 .set_pauseparam = nsim_set_pauseparam,
192 .set_coalesce = nsim_set_coalesce,
193 .get_coalesce = nsim_get_coalesce,
194 .get_ringparam = nsim_get_ringparam,
195 .set_ringparam = nsim_set_ringparam,
196 .get_channels = nsim_get_channels,
197 .set_channels = nsim_set_channels,
198 .get_fecparam = nsim_get_fecparam,
199 .set_fecparam = nsim_set_fecparam,
200 .get_fec_stats = nsim_get_fec_stats,
201 .get_ts_info = nsim_get_ts_info,
202 };
203
nsim_ethtool_ring_init(struct netdevsim * ns)204 static void nsim_ethtool_ring_init(struct netdevsim *ns)
205 {
206 ns->ethtool.ring.rx_pending = 512;
207 ns->ethtool.ring.rx_max_pending = 4096;
208 ns->ethtool.ring.rx_jumbo_max_pending = 4096;
209 ns->ethtool.ring.rx_mini_max_pending = 4096;
210 ns->ethtool.ring.tx_pending = 512;
211 ns->ethtool.ring.tx_max_pending = 4096;
212 }
213
nsim_ethtool_init(struct netdevsim * ns)214 void nsim_ethtool_init(struct netdevsim *ns)
215 {
216 struct dentry *ethtool, *dir;
217
218 ns->netdev->ethtool_ops = &nsim_ethtool_ops;
219
220 nsim_ethtool_ring_init(ns);
221
222 ns->ethtool.pauseparam.report_stats_rx = true;
223 ns->ethtool.pauseparam.report_stats_tx = true;
224
225 ns->ethtool.fec.fec = ETHTOOL_FEC_NONE;
226 ns->ethtool.fec.active_fec = ETHTOOL_FEC_NONE;
227
228 ns->ethtool.channels = ns->nsim_bus_dev->num_queues;
229
230 ethtool = debugfs_create_dir("ethtool", ns->nsim_dev_port->ddir);
231
232 debugfs_create_u32("get_err", 0600, ethtool, &ns->ethtool.get_err);
233 debugfs_create_u32("set_err", 0600, ethtool, &ns->ethtool.set_err);
234
235 dir = debugfs_create_dir("pause", ethtool);
236 debugfs_create_bool("report_stats_rx", 0600, dir,
237 &ns->ethtool.pauseparam.report_stats_rx);
238 debugfs_create_bool("report_stats_tx", 0600, dir,
239 &ns->ethtool.pauseparam.report_stats_tx);
240
241 dir = debugfs_create_dir("ring", ethtool);
242 debugfs_create_u32("rx_max_pending", 0600, dir,
243 &ns->ethtool.ring.rx_max_pending);
244 debugfs_create_u32("rx_jumbo_max_pending", 0600, dir,
245 &ns->ethtool.ring.rx_jumbo_max_pending);
246 debugfs_create_u32("rx_mini_max_pending", 0600, dir,
247 &ns->ethtool.ring.rx_mini_max_pending);
248 debugfs_create_u32("tx_max_pending", 0600, dir,
249 &ns->ethtool.ring.tx_max_pending);
250 }
251