1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdint.h>
7 #include <rte_string_fns.h>
8 #include <rte_version.h>
9 #include <rte_ethdev.h>
10 #include <rte_ether.h>
11 #include <rte_bus_pci.h>
12 #ifdef RTE_NET_IXGBE
13 #include <rte_pmd_ixgbe.h>
14 #endif
15 #include "rte_ethtool.h"
16
17 #define PKTPOOL_SIZE 512
18 #define PKTPOOL_CACHE 32
19
20
21 int
rte_ethtool_get_drvinfo(uint16_t port_id,struct ethtool_drvinfo * drvinfo)22 rte_ethtool_get_drvinfo(uint16_t port_id, struct ethtool_drvinfo *drvinfo)
23 {
24 struct rte_eth_dev_info dev_info;
25 struct rte_dev_reg_info reg_info;
26 const struct rte_pci_device *pci_dev;
27 const struct rte_bus *bus = NULL;
28 int n;
29 int ret;
30
31 if (drvinfo == NULL)
32 return -EINVAL;
33
34 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
35
36 ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
37 sizeof(drvinfo->fw_version));
38 if (ret < 0)
39 printf("firmware version get error: (%s)\n", strerror(-ret));
40 else if (ret > 0)
41 printf("Insufficient fw version buffer size, "
42 "the minimum size should be %d\n", ret);
43
44 ret = rte_eth_dev_info_get(port_id, &dev_info);
45 if (ret != 0) {
46 printf("Error during getting device (port %u) info: %s\n",
47 port_id, strerror(-ret));
48
49 return ret;
50 }
51
52 strlcpy(drvinfo->driver, dev_info.driver_name,
53 sizeof(drvinfo->driver));
54 strlcpy(drvinfo->version, rte_version(), sizeof(drvinfo->version));
55 /* TODO: replace bus_info by rte_devargs.name */
56 if (dev_info.device)
57 bus = rte_bus_find_by_device(dev_info.device);
58 if (bus && !strcmp(bus->name, "pci")) {
59 pci_dev = RTE_DEV_TO_PCI(dev_info.device);
60 snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info),
61 "%04x:%02x:%02x.%x",
62 pci_dev->addr.domain, pci_dev->addr.bus,
63 pci_dev->addr.devid, pci_dev->addr.function);
64 } else {
65 snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info), "N/A");
66 }
67
68 memset(®_info, 0, sizeof(reg_info));
69 rte_eth_dev_get_reg_info(port_id, ®_info);
70 n = reg_info.length;
71 if (n > 0)
72 drvinfo->regdump_len = n;
73 else
74 drvinfo->regdump_len = 0;
75
76 n = rte_eth_dev_get_eeprom_length(port_id);
77 if (n > 0)
78 drvinfo->eedump_len = n;
79 else
80 drvinfo->eedump_len = 0;
81
82 drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
83 drvinfo->testinfo_len = 0;
84
85 return 0;
86 }
87
88 int
rte_ethtool_get_regs_len(uint16_t port_id)89 rte_ethtool_get_regs_len(uint16_t port_id)
90 {
91 struct rte_dev_reg_info reg_info;
92 int ret;
93
94 memset(®_info, 0, sizeof(reg_info));
95
96 ret = rte_eth_dev_get_reg_info(port_id, ®_info);
97 if (ret)
98 return ret;
99
100 return reg_info.length * reg_info.width;
101 }
102
103 int
rte_ethtool_get_regs(uint16_t port_id,struct ethtool_regs * regs,void * data)104 rte_ethtool_get_regs(uint16_t port_id, struct ethtool_regs *regs, void *data)
105 {
106 struct rte_dev_reg_info reg_info;
107 int status;
108
109 if (regs == NULL || data == NULL)
110 return -EINVAL;
111
112 reg_info.data = data;
113 reg_info.length = 0;
114
115 status = rte_eth_dev_get_reg_info(port_id, ®_info);
116 if (status)
117 return status;
118 regs->version = reg_info.version;
119
120 return 0;
121 }
122
123 int
rte_ethtool_get_link(uint16_t port_id)124 rte_ethtool_get_link(uint16_t port_id)
125 {
126 struct rte_eth_link link;
127 int ret;
128
129 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
130 ret = rte_eth_link_get(port_id, &link);
131 if (ret < 0)
132 return ret;
133
134 return link.link_status;
135 }
136
137 int
rte_ethtool_get_eeprom_len(uint16_t port_id)138 rte_ethtool_get_eeprom_len(uint16_t port_id)
139 {
140 return rte_eth_dev_get_eeprom_length(port_id);
141 }
142
143 int
rte_ethtool_get_eeprom(uint16_t port_id,struct ethtool_eeprom * eeprom,void * words)144 rte_ethtool_get_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
145 void *words)
146 {
147 struct rte_dev_eeprom_info eeprom_info;
148 int status;
149
150 if (eeprom == NULL || words == NULL)
151 return -EINVAL;
152
153 eeprom_info.offset = eeprom->offset;
154 eeprom_info.length = eeprom->len;
155 eeprom_info.data = words;
156
157 status = rte_eth_dev_get_eeprom(port_id, &eeprom_info);
158 if (status)
159 return status;
160
161 eeprom->magic = eeprom_info.magic;
162
163 return 0;
164 }
165
166 int
rte_ethtool_set_eeprom(uint16_t port_id,struct ethtool_eeprom * eeprom,void * words)167 rte_ethtool_set_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
168 void *words)
169 {
170 struct rte_dev_eeprom_info eeprom_info;
171 int status;
172
173 if (eeprom == NULL || words == NULL || eeprom->offset >= eeprom->len)
174 return -EINVAL;
175
176 eeprom_info.offset = eeprom->offset;
177 eeprom_info.length = eeprom->len;
178 eeprom_info.data = words;
179
180 status = rte_eth_dev_set_eeprom(port_id, &eeprom_info);
181 if (status)
182 return status;
183
184 eeprom->magic = eeprom_info.magic;
185
186 return 0;
187 }
188
189 int
rte_ethtool_get_module_info(uint16_t port_id,uint32_t * modinfo)190 rte_ethtool_get_module_info(uint16_t port_id, uint32_t *modinfo)
191 {
192 struct rte_eth_dev_module_info *info;
193
194 info = (struct rte_eth_dev_module_info *)modinfo;
195 return rte_eth_dev_get_module_info(port_id, info);
196 }
197
198 int
rte_ethtool_get_module_eeprom(uint16_t port_id,struct ethtool_eeprom * eeprom,void * words)199 rte_ethtool_get_module_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
200 void *words)
201 {
202 struct rte_dev_eeprom_info eeprom_info;
203 int status;
204
205 if (eeprom == NULL || words == NULL)
206 return -EINVAL;
207
208 eeprom_info.offset = eeprom->offset;
209 eeprom_info.length = eeprom->len;
210 eeprom_info.data = words;
211
212 status = rte_eth_dev_get_module_eeprom(port_id, &eeprom_info);
213 if (status)
214 return status;
215
216 return 0;
217 }
218
219 int
rte_ethtool_get_pauseparam(uint16_t port_id,struct ethtool_pauseparam * pause_param)220 rte_ethtool_get_pauseparam(uint16_t port_id,
221 struct ethtool_pauseparam *pause_param)
222 {
223 struct rte_eth_fc_conf fc_conf;
224 int status;
225
226 if (pause_param == NULL)
227 return -EINVAL;
228
229 status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
230 if (status)
231 return status;
232
233 pause_param->tx_pause = 0;
234 pause_param->rx_pause = 0;
235 switch (fc_conf.mode) {
236 case RTE_FC_RX_PAUSE:
237 pause_param->rx_pause = 1;
238 break;
239 case RTE_FC_TX_PAUSE:
240 pause_param->tx_pause = 1;
241 break;
242 case RTE_FC_FULL:
243 pause_param->rx_pause = 1;
244 pause_param->tx_pause = 1;
245 default:
246 /* dummy block to avoid compiler warning */
247 break;
248 }
249 pause_param->autoneg = (uint32_t)fc_conf.autoneg;
250
251 return 0;
252 }
253
254 int
rte_ethtool_set_pauseparam(uint16_t port_id,struct ethtool_pauseparam * pause_param)255 rte_ethtool_set_pauseparam(uint16_t port_id,
256 struct ethtool_pauseparam *pause_param)
257 {
258 struct rte_eth_fc_conf fc_conf;
259 int status;
260
261 if (pause_param == NULL)
262 return -EINVAL;
263
264 /*
265 * Read device flow control parameter first since
266 * ethtool set_pauseparam op doesn't have all the information.
267 * as defined in struct rte_eth_fc_conf.
268 * This API requires the device to support both
269 * rte_eth_dev_flow_ctrl_get and rte_eth_dev_flow_ctrl_set, otherwise
270 * return -ENOTSUP
271 */
272 status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
273 if (status)
274 return status;
275
276 fc_conf.autoneg = (uint8_t)pause_param->autoneg;
277
278 if (pause_param->tx_pause) {
279 if (pause_param->rx_pause)
280 fc_conf.mode = RTE_FC_FULL;
281 else
282 fc_conf.mode = RTE_FC_TX_PAUSE;
283 } else {
284 if (pause_param->rx_pause)
285 fc_conf.mode = RTE_FC_RX_PAUSE;
286 else
287 fc_conf.mode = RTE_FC_NONE;
288 }
289
290 status = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
291 if (status)
292 return status;
293
294 return 0;
295 }
296
297 int
rte_ethtool_net_open(uint16_t port_id)298 rte_ethtool_net_open(uint16_t port_id)
299 {
300 int ret;
301
302 ret = rte_eth_dev_stop(port_id);
303 if (ret != 0)
304 return ret;
305
306 return rte_eth_dev_start(port_id);
307 }
308
309 int
rte_ethtool_net_stop(uint16_t port_id)310 rte_ethtool_net_stop(uint16_t port_id)
311 {
312 return rte_eth_dev_stop(port_id);
313 }
314
315 int
rte_ethtool_net_get_mac_addr(uint16_t port_id,struct rte_ether_addr * addr)316 rte_ethtool_net_get_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
317 {
318 int ret;
319
320 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
321 if (addr == NULL)
322 return -EINVAL;
323
324 ret = rte_eth_macaddr_get(port_id, addr);
325 if (ret != 0)
326 return ret;
327
328 return 0;
329 }
330
331 int
rte_ethtool_net_set_mac_addr(uint16_t port_id,struct rte_ether_addr * addr)332 rte_ethtool_net_set_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
333 {
334 if (addr == NULL)
335 return -EINVAL;
336 return rte_eth_dev_default_mac_addr_set(port_id, addr);
337 }
338
339 int
rte_ethtool_net_validate_addr(uint16_t port_id __rte_unused,struct rte_ether_addr * addr)340 rte_ethtool_net_validate_addr(uint16_t port_id __rte_unused,
341 struct rte_ether_addr *addr)
342 {
343 if (addr == NULL)
344 return -EINVAL;
345 return rte_is_valid_assigned_ether_addr(addr);
346 }
347
348 int
rte_ethtool_net_change_mtu(uint16_t port_id,int mtu)349 rte_ethtool_net_change_mtu(uint16_t port_id, int mtu)
350 {
351 if (mtu < 0 || mtu > UINT16_MAX)
352 return -EINVAL;
353 return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
354 }
355
356 int
rte_ethtool_net_get_stats64(uint16_t port_id,struct rte_eth_stats * stats)357 rte_ethtool_net_get_stats64(uint16_t port_id, struct rte_eth_stats *stats)
358 {
359 if (stats == NULL)
360 return -EINVAL;
361 return rte_eth_stats_get(port_id, stats);
362 }
363
364 int
rte_ethtool_net_vlan_rx_add_vid(uint16_t port_id,uint16_t vid)365 rte_ethtool_net_vlan_rx_add_vid(uint16_t port_id, uint16_t vid)
366 {
367 return rte_eth_dev_vlan_filter(port_id, vid, 1);
368 }
369
370 int
rte_ethtool_net_vlan_rx_kill_vid(uint16_t port_id,uint16_t vid)371 rte_ethtool_net_vlan_rx_kill_vid(uint16_t port_id, uint16_t vid)
372 {
373 return rte_eth_dev_vlan_filter(port_id, vid, 0);
374 }
375
376 /*
377 * The set_rx_mode provides driver-specific rx mode setting.
378 * This implementation implements rx mode setting based upon
379 * ixgbe/igb drivers. Further improvement is to provide a
380 * callback op field over struct rte_eth_dev::dev_ops so each
381 * driver can register device-specific implementation
382 */
383 int
rte_ethtool_net_set_rx_mode(uint16_t port_id)384 rte_ethtool_net_set_rx_mode(uint16_t port_id)
385 {
386 uint16_t num_vfs;
387 struct rte_eth_dev_info dev_info;
388 uint16_t vf;
389 int ret;
390
391 ret = rte_eth_dev_info_get(port_id, &dev_info);
392 if (ret != 0)
393 return ret;
394
395 num_vfs = dev_info.max_vfs;
396
397 /* Set VF vf_rx_mode, VF unsupport status is discard */
398 for (vf = 0; vf < num_vfs; vf++) {
399 #ifdef RTE_NET_IXGBE
400 rte_pmd_ixgbe_set_vf_rxmode(port_id, vf,
401 ETH_VMDQ_ACCEPT_UNTAG, 0);
402 #endif
403 }
404
405 /* Enable Rx vlan filter, VF unspport status is discard */
406 ret = rte_eth_dev_set_vlan_offload(port_id, ETH_VLAN_FILTER_MASK);
407 if (ret != 0)
408 return ret;
409
410 return 0;
411 }
412
413
414 int
rte_ethtool_get_ringparam(uint16_t port_id,struct ethtool_ringparam * ring_param)415 rte_ethtool_get_ringparam(uint16_t port_id,
416 struct ethtool_ringparam *ring_param)
417 {
418 struct rte_eth_dev_info dev_info;
419 struct rte_eth_rxq_info rx_qinfo;
420 struct rte_eth_txq_info tx_qinfo;
421 int stat;
422 int ret;
423
424 if (ring_param == NULL)
425 return -EINVAL;
426
427 ret = rte_eth_dev_info_get(port_id, &dev_info);
428 if (ret != 0)
429 return ret;
430
431 stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
432 if (stat != 0)
433 return stat;
434
435 stat = rte_eth_tx_queue_info_get(port_id, 0, &tx_qinfo);
436 if (stat != 0)
437 return stat;
438
439 memset(ring_param, 0, sizeof(*ring_param));
440 ring_param->rx_pending = rx_qinfo.nb_desc;
441 ring_param->rx_max_pending = dev_info.rx_desc_lim.nb_max;
442 ring_param->tx_pending = tx_qinfo.nb_desc;
443 ring_param->tx_max_pending = dev_info.tx_desc_lim.nb_max;
444
445 return 0;
446 }
447
448
449 int
rte_ethtool_set_ringparam(uint16_t port_id,struct ethtool_ringparam * ring_param)450 rte_ethtool_set_ringparam(uint16_t port_id,
451 struct ethtool_ringparam *ring_param)
452 {
453 struct rte_eth_rxq_info rx_qinfo;
454 int stat;
455
456 if (ring_param == NULL)
457 return -EINVAL;
458
459 stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
460 if (stat != 0)
461 return stat;
462
463 stat = rte_eth_dev_stop(port_id);
464 if (stat != 0)
465 return stat;
466
467 stat = rte_eth_tx_queue_setup(port_id, 0, ring_param->tx_pending,
468 rte_socket_id(), NULL);
469 if (stat != 0)
470 return stat;
471
472 stat = rte_eth_rx_queue_setup(port_id, 0, ring_param->rx_pending,
473 rte_socket_id(), NULL, rx_qinfo.mp);
474 if (stat != 0)
475 return stat;
476
477 return rte_eth_dev_start(port_id);
478 }
479