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