Merge tag 'docs-5.18-2' of git://git.lwn.net/linux
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / microsoft / mana / mana_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
3
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
7
8 #include "mana.h"
9
10 static const struct {
11         char name[ETH_GSTRING_LEN];
12         u16 offset;
13 } mana_eth_stats[] = {
14         {"stop_queue", offsetof(struct mana_ethtool_stats, stop_queue)},
15         {"wake_queue", offsetof(struct mana_ethtool_stats, wake_queue)},
16 };
17
18 static int mana_get_sset_count(struct net_device *ndev, int stringset)
19 {
20         struct mana_port_context *apc = netdev_priv(ndev);
21         unsigned int num_queues = apc->num_queues;
22
23         if (stringset != ETH_SS_STATS)
24                 return -EINVAL;
25
26         return ARRAY_SIZE(mana_eth_stats) + num_queues * 6;
27 }
28
29 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
30 {
31         struct mana_port_context *apc = netdev_priv(ndev);
32         unsigned int num_queues = apc->num_queues;
33         u8 *p = data;
34         int i;
35
36         if (stringset != ETH_SS_STATS)
37                 return;
38
39         for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++) {
40                 memcpy(p, mana_eth_stats[i].name, ETH_GSTRING_LEN);
41                 p += ETH_GSTRING_LEN;
42         }
43
44         for (i = 0; i < num_queues; i++) {
45                 sprintf(p, "rx_%d_packets", i);
46                 p += ETH_GSTRING_LEN;
47                 sprintf(p, "rx_%d_bytes", i);
48                 p += ETH_GSTRING_LEN;
49                 sprintf(p, "rx_%d_xdp_drop", i);
50                 p += ETH_GSTRING_LEN;
51                 sprintf(p, "rx_%d_xdp_tx", i);
52                 p += ETH_GSTRING_LEN;
53         }
54
55         for (i = 0; i < num_queues; i++) {
56                 sprintf(p, "tx_%d_packets", i);
57                 p += ETH_GSTRING_LEN;
58                 sprintf(p, "tx_%d_bytes", i);
59                 p += ETH_GSTRING_LEN;
60         }
61 }
62
63 static void mana_get_ethtool_stats(struct net_device *ndev,
64                                    struct ethtool_stats *e_stats, u64 *data)
65 {
66         struct mana_port_context *apc = netdev_priv(ndev);
67         unsigned int num_queues = apc->num_queues;
68         void *eth_stats = &apc->eth_stats;
69         struct mana_stats_rx *rx_stats;
70         struct mana_stats_tx *tx_stats;
71         unsigned int start;
72         u64 packets, bytes;
73         u64 xdp_drop;
74         u64 xdp_tx;
75         int q, i = 0;
76
77         if (!apc->port_is_up)
78                 return;
79
80         for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
81                 data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
82
83         for (q = 0; q < num_queues; q++) {
84                 rx_stats = &apc->rxqs[q]->stats;
85
86                 do {
87                         start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
88                         packets = rx_stats->packets;
89                         bytes = rx_stats->bytes;
90                         xdp_drop = rx_stats->xdp_drop;
91                         xdp_tx = rx_stats->xdp_tx;
92                 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
93
94                 data[i++] = packets;
95                 data[i++] = bytes;
96                 data[i++] = xdp_drop;
97                 data[i++] = xdp_tx;
98         }
99
100         for (q = 0; q < num_queues; q++) {
101                 tx_stats = &apc->tx_qp[q].txq.stats;
102
103                 do {
104                         start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
105                         packets = tx_stats->packets;
106                         bytes = tx_stats->bytes;
107                 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
108
109                 data[i++] = packets;
110                 data[i++] = bytes;
111         }
112 }
113
114 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd,
115                           u32 *rules)
116 {
117         struct mana_port_context *apc = netdev_priv(ndev);
118
119         switch (cmd->cmd) {
120         case ETHTOOL_GRXRINGS:
121                 cmd->data = apc->num_queues;
122                 return 0;
123         }
124
125         return -EOPNOTSUPP;
126 }
127
128 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
129 {
130         return MANA_HASH_KEY_SIZE;
131 }
132
133 static u32 mana_rss_indir_size(struct net_device *ndev)
134 {
135         return MANA_INDIRECT_TABLE_SIZE;
136 }
137
138 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
139                          u8 *hfunc)
140 {
141         struct mana_port_context *apc = netdev_priv(ndev);
142         int i;
143
144         if (hfunc)
145                 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
146
147         if (indir) {
148                 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
149                         indir[i] = apc->indir_table[i];
150         }
151
152         if (key)
153                 memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE);
154
155         return 0;
156 }
157
158 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir,
159                          const u8 *key, const u8 hfunc)
160 {
161         struct mana_port_context *apc = netdev_priv(ndev);
162         bool update_hash = false, update_table = false;
163         u32 save_table[MANA_INDIRECT_TABLE_SIZE];
164         u8 save_key[MANA_HASH_KEY_SIZE];
165         int i, err;
166
167         if (!apc->port_is_up)
168                 return -EOPNOTSUPP;
169
170         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
171                 return -EOPNOTSUPP;
172
173         if (indir) {
174                 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
175                         if (indir[i] >= apc->num_queues)
176                                 return -EINVAL;
177
178                 update_table = true;
179                 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
180                         save_table[i] = apc->indir_table[i];
181                         apc->indir_table[i] = indir[i];
182                 }
183         }
184
185         if (key) {
186                 update_hash = true;
187                 memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
188                 memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE);
189         }
190
191         err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
192
193         if (err) { /* recover to original values */
194                 if (update_table) {
195                         for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
196                                 apc->indir_table[i] = save_table[i];
197                 }
198
199                 if (update_hash)
200                         memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
201
202                 mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
203         }
204
205         return err;
206 }
207
208 static void mana_get_channels(struct net_device *ndev,
209                               struct ethtool_channels *channel)
210 {
211         struct mana_port_context *apc = netdev_priv(ndev);
212
213         channel->max_combined = apc->max_queues;
214         channel->combined_count = apc->num_queues;
215 }
216
217 static int mana_set_channels(struct net_device *ndev,
218                              struct ethtool_channels *channels)
219 {
220         struct mana_port_context *apc = netdev_priv(ndev);
221         unsigned int new_count = channels->combined_count;
222         unsigned int old_count = apc->num_queues;
223         int err, err2;
224
225         err = mana_detach(ndev, false);
226         if (err) {
227                 netdev_err(ndev, "mana_detach failed: %d\n", err);
228                 return err;
229         }
230
231         apc->num_queues = new_count;
232         err = mana_attach(ndev);
233         if (!err)
234                 return 0;
235
236         netdev_err(ndev, "mana_attach failed: %d\n", err);
237
238         /* Try to roll it back to the old configuration. */
239         apc->num_queues = old_count;
240         err2 = mana_attach(ndev);
241         if (err2)
242                 netdev_err(ndev, "mana re-attach failed: %d\n", err2);
243
244         return err;
245 }
246
247 const struct ethtool_ops mana_ethtool_ops = {
248         .get_ethtool_stats      = mana_get_ethtool_stats,
249         .get_sset_count         = mana_get_sset_count,
250         .get_strings            = mana_get_strings,
251         .get_rxnfc              = mana_get_rxnfc,
252         .get_rxfh_key_size      = mana_get_rxfh_key_size,
253         .get_rxfh_indir_size    = mana_rss_indir_size,
254         .get_rxfh               = mana_get_rxfh,
255         .set_rxfh               = mana_set_rxfh,
256         .get_channels           = mana_get_channels,
257         .set_channels           = mana_set_channels,
258 };