1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3
4 #include <linux/debugfs.h>
5 #include <linux/ethtool.h>
6 #include <linux/random.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)67 static void nsim_get_ringparam(struct net_device *dev,
68 struct ethtool_ringparam *ring)
69 {
70 struct netdevsim *ns = netdev_priv(dev);
71
72 memcpy(ring, &ns->ethtool.ring, sizeof(ns->ethtool.ring));
73 }
74
nsim_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)75 static int nsim_set_ringparam(struct net_device *dev,
76 struct ethtool_ringparam *ring)
77 {
78 struct netdevsim *ns = netdev_priv(dev);
79
80 ns->ethtool.ring.rx_pending = ring->rx_pending;
81 ns->ethtool.ring.rx_jumbo_pending = ring->rx_jumbo_pending;
82 ns->ethtool.ring.rx_mini_pending = ring->rx_mini_pending;
83 ns->ethtool.ring.tx_pending = ring->tx_pending;
84 return 0;
85 }
86
87 static void
nsim_get_channels(struct net_device * dev,struct ethtool_channels * ch)88 nsim_get_channels(struct net_device *dev, struct ethtool_channels *ch)
89 {
90 struct netdevsim *ns = netdev_priv(dev);
91
92 ch->max_combined = ns->nsim_bus_dev->num_queues;
93 ch->combined_count = ns->ethtool.channels;
94 }
95
96 static int
nsim_set_channels(struct net_device * dev,struct ethtool_channels * ch)97 nsim_set_channels(struct net_device *dev, struct ethtool_channels *ch)
98 {
99 struct netdevsim *ns = netdev_priv(dev);
100 int err;
101
102 err = netif_set_real_num_queues(dev, ch->combined_count,
103 ch->combined_count);
104 if (err)
105 return err;
106
107 ns->ethtool.channels = ch->combined_count;
108 return 0;
109 }
110
111 static int
nsim_get_fecparam(struct net_device * dev,struct ethtool_fecparam * fecparam)112 nsim_get_fecparam(struct net_device *dev, struct ethtool_fecparam *fecparam)
113 {
114 struct netdevsim *ns = netdev_priv(dev);
115
116 if (ns->ethtool.get_err)
117 return -ns->ethtool.get_err;
118 memcpy(fecparam, &ns->ethtool.fec, sizeof(ns->ethtool.fec));
119 return 0;
120 }
121
122 static int
nsim_set_fecparam(struct net_device * dev,struct ethtool_fecparam * fecparam)123 nsim_set_fecparam(struct net_device *dev, struct ethtool_fecparam *fecparam)
124 {
125 struct netdevsim *ns = netdev_priv(dev);
126 u32 fec;
127
128 if (ns->ethtool.set_err)
129 return -ns->ethtool.set_err;
130 memcpy(&ns->ethtool.fec, fecparam, sizeof(ns->ethtool.fec));
131 fec = fecparam->fec;
132 if (fec == ETHTOOL_FEC_AUTO)
133 fec |= ETHTOOL_FEC_OFF;
134 fec |= ETHTOOL_FEC_NONE;
135 ns->ethtool.fec.active_fec = 1 << (fls(fec) - 1);
136 return 0;
137 }
138
139 static const struct ethtool_ops nsim_ethtool_ops = {
140 .supported_coalesce_params = ETHTOOL_COALESCE_ALL_PARAMS,
141 .get_pause_stats = nsim_get_pause_stats,
142 .get_pauseparam = nsim_get_pauseparam,
143 .set_pauseparam = nsim_set_pauseparam,
144 .set_coalesce = nsim_set_coalesce,
145 .get_coalesce = nsim_get_coalesce,
146 .get_ringparam = nsim_get_ringparam,
147 .set_ringparam = nsim_set_ringparam,
148 .get_channels = nsim_get_channels,
149 .set_channels = nsim_set_channels,
150 .get_fecparam = nsim_get_fecparam,
151 .set_fecparam = nsim_set_fecparam,
152 };
153
nsim_ethtool_ring_init(struct netdevsim * ns)154 static void nsim_ethtool_ring_init(struct netdevsim *ns)
155 {
156 ns->ethtool.ring.rx_max_pending = 4096;
157 ns->ethtool.ring.rx_jumbo_max_pending = 4096;
158 ns->ethtool.ring.rx_mini_max_pending = 4096;
159 ns->ethtool.ring.tx_max_pending = 4096;
160 }
161
nsim_ethtool_init(struct netdevsim * ns)162 void nsim_ethtool_init(struct netdevsim *ns)
163 {
164 struct dentry *ethtool, *dir;
165
166 ns->netdev->ethtool_ops = &nsim_ethtool_ops;
167
168 nsim_ethtool_ring_init(ns);
169
170 ns->ethtool.fec.fec = ETHTOOL_FEC_NONE;
171 ns->ethtool.fec.active_fec = ETHTOOL_FEC_NONE;
172
173 ns->ethtool.channels = ns->nsim_bus_dev->num_queues;
174
175 ethtool = debugfs_create_dir("ethtool", ns->nsim_dev_port->ddir);
176
177 debugfs_create_u32("get_err", 0600, ethtool, &ns->ethtool.get_err);
178 debugfs_create_u32("set_err", 0600, ethtool, &ns->ethtool.set_err);
179
180 dir = debugfs_create_dir("pause", ethtool);
181 debugfs_create_bool("report_stats_rx", 0600, dir,
182 &ns->ethtool.pauseparam.report_stats_rx);
183 debugfs_create_bool("report_stats_tx", 0600, dir,
184 &ns->ethtool.pauseparam.report_stats_tx);
185
186 dir = debugfs_create_dir("ring", ethtool);
187 debugfs_create_u32("rx_max_pending", 0600, dir,
188 &ns->ethtool.ring.rx_max_pending);
189 debugfs_create_u32("rx_jumbo_max_pending", 0600, dir,
190 &ns->ethtool.ring.rx_jumbo_max_pending);
191 debugfs_create_u32("rx_mini_max_pending", 0600, dir,
192 &ns->ethtool.ring.rx_mini_max_pending);
193 debugfs_create_u32("tx_max_pending", 0600, dir,
194 &ns->ethtool.ring.tx_max_pending);
195 }
196