examples/ethtool: display firmware version
[dpdk.git] / examples / ethtool / lib / rte_ethtool.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <rte_version.h>
37 #include <rte_ethdev.h>
38 #include <rte_ether.h>
39 #include "rte_ethtool.h"
40
41 #define PKTPOOL_SIZE 512
42 #define PKTPOOL_CACHE 32
43
44
45 int
46 rte_ethtool_get_drvinfo(uint8_t port_id, struct ethtool_drvinfo *drvinfo)
47 {
48         struct rte_eth_dev_info dev_info;
49         struct rte_dev_reg_info reg_info;
50         int n;
51         int ret;
52
53         if (drvinfo == NULL)
54                 return -EINVAL;
55
56         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
57
58         ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
59                               sizeof(drvinfo->fw_version));
60         if (ret < 0)
61                 printf("firmware version get error: (%s)\n", strerror(-ret));
62         else if (ret > 0)
63                 printf("Insufficient fw version buffer size, "
64                        "the minimun size should be %d\n", ret);
65
66         memset(&dev_info, 0, sizeof(dev_info));
67         rte_eth_dev_info_get(port_id, &dev_info);
68
69         snprintf(drvinfo->driver, sizeof(drvinfo->driver), "%s",
70                 dev_info.driver_name);
71         snprintf(drvinfo->version, sizeof(drvinfo->version), "%s",
72                 rte_version());
73         if (dev_info.pci_dev)
74                 snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info),
75                         "%04x:%02x:%02x.%x",
76                         dev_info.pci_dev->addr.domain,
77                         dev_info.pci_dev->addr.bus,
78                         dev_info.pci_dev->addr.devid,
79                         dev_info.pci_dev->addr.function);
80         else
81                 snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info), "N/A");
82
83         memset(&reg_info, 0, sizeof(reg_info));
84         rte_eth_dev_get_reg_info(port_id, &reg_info);
85         n = reg_info.length;
86         if (n > 0)
87                 drvinfo->regdump_len = n;
88         else
89                 drvinfo->regdump_len = 0;
90
91         n = rte_eth_dev_get_eeprom_length(port_id);
92         if (n > 0)
93                 drvinfo->eedump_len = n;
94         else
95                 drvinfo->eedump_len = 0;
96
97         drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
98         drvinfo->testinfo_len = 0;
99
100         return 0;
101 }
102
103 int
104 rte_ethtool_get_regs_len(uint8_t port_id)
105 {
106         struct rte_dev_reg_info reg_info;
107         int ret;
108
109         memset(&reg_info, 0, sizeof(reg_info));
110
111         ret = rte_eth_dev_get_reg_info(port_id, &reg_info);
112         if (ret)
113                 return ret;
114
115         return reg_info.length * reg_info.width;
116 }
117
118 int
119 rte_ethtool_get_regs(uint8_t port_id, struct ethtool_regs *regs, void *data)
120 {
121         struct rte_dev_reg_info reg_info;
122         int status;
123
124         if (regs == NULL || data == NULL)
125                 return -EINVAL;
126
127         reg_info.data = data;
128         reg_info.length = 0;
129
130         status = rte_eth_dev_get_reg_info(port_id, &reg_info);
131         if (status)
132                 return status;
133         regs->version = reg_info.version;
134
135         return 0;
136 }
137
138 int
139 rte_ethtool_get_link(uint8_t port_id)
140 {
141         struct rte_eth_link link;
142
143         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
144         rte_eth_link_get(port_id, &link);
145         return link.link_status;
146 }
147
148 int
149 rte_ethtool_get_eeprom_len(uint8_t port_id)
150 {
151         return rte_eth_dev_get_eeprom_length(port_id);
152 }
153
154 int
155 rte_ethtool_get_eeprom(uint8_t port_id, struct ethtool_eeprom *eeprom,
156         void *words)
157 {
158         struct rte_dev_eeprom_info eeprom_info;
159         int status;
160
161         if (eeprom == NULL || words == NULL)
162                 return -EINVAL;
163
164         eeprom_info.offset = eeprom->offset;
165         eeprom_info.length = eeprom->len;
166         eeprom_info.data = words;
167
168         status = rte_eth_dev_get_eeprom(port_id, &eeprom_info);
169         if (status)
170                 return status;
171
172         eeprom->magic = eeprom_info.magic;
173
174         return 0;
175 }
176
177 int
178 rte_ethtool_set_eeprom(uint8_t port_id, struct ethtool_eeprom *eeprom,
179         void *words)
180 {
181         struct rte_dev_eeprom_info eeprom_info;
182         int status;
183
184         if (eeprom == NULL || words == NULL || eeprom->offset >= eeprom->len)
185                 return -EINVAL;
186
187         eeprom_info.offset = eeprom->offset;
188         eeprom_info.length = eeprom->len;
189         eeprom_info.data = words;
190
191         status = rte_eth_dev_set_eeprom(port_id, &eeprom_info);
192         if (status)
193                 return status;
194
195         eeprom->magic = eeprom_info.magic;
196
197         return 0;
198 }
199
200 int
201 rte_ethtool_get_pauseparam(uint8_t port_id,
202         struct ethtool_pauseparam *pause_param)
203 {
204         struct rte_eth_fc_conf fc_conf;
205         int status;
206
207         if (pause_param == NULL)
208                 return -EINVAL;
209
210         status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
211         if (status)
212                 return status;
213
214         pause_param->tx_pause = 0;
215         pause_param->rx_pause = 0;
216         switch (fc_conf.mode) {
217         case RTE_FC_RX_PAUSE:
218                 pause_param->rx_pause = 1;
219                 break;
220         case RTE_FC_TX_PAUSE:
221                 pause_param->tx_pause = 1;
222                 break;
223         case RTE_FC_FULL:
224                 pause_param->rx_pause = 1;
225                 pause_param->tx_pause = 1;
226         default:
227                 /* dummy block to avoid compiler warning */
228                 break;
229         }
230         pause_param->autoneg = (uint32_t)fc_conf.autoneg;
231
232         return 0;
233 }
234
235 int
236 rte_ethtool_set_pauseparam(uint8_t port_id,
237         struct ethtool_pauseparam *pause_param)
238 {
239         struct rte_eth_fc_conf fc_conf;
240         int status;
241
242         if (pause_param == NULL)
243                 return -EINVAL;
244
245         /*
246          * Read device flow control parameter first since
247          * ethtool set_pauseparam op doesn't have all the information.
248          * as defined in struct rte_eth_fc_conf.
249          * This API requires the device to support both
250          * rte_eth_dev_flow_ctrl_get and rte_eth_dev_flow_ctrl_set, otherwise
251          * return -ENOTSUP
252          */
253         status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
254         if (status)
255                 return status;
256
257         fc_conf.autoneg = (uint8_t)pause_param->autoneg;
258
259         if (pause_param->tx_pause) {
260                 if (pause_param->rx_pause)
261                         fc_conf.mode = RTE_FC_FULL;
262                 else
263                         fc_conf.mode = RTE_FC_TX_PAUSE;
264         } else {
265                 if (pause_param->rx_pause)
266                         fc_conf.mode = RTE_FC_RX_PAUSE;
267                 else
268                         fc_conf.mode = RTE_FC_NONE;
269         }
270
271         status = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
272         if (status)
273                 return status;
274
275         return 0;
276 }
277
278 int
279 rte_ethtool_net_open(uint8_t port_id)
280 {
281         rte_eth_dev_stop(port_id);
282
283         return rte_eth_dev_start(port_id);
284 }
285
286 int
287 rte_ethtool_net_stop(uint8_t port_id)
288 {
289         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
290         rte_eth_dev_stop(port_id);
291
292         return 0;
293 }
294
295 int
296 rte_ethtool_net_get_mac_addr(uint8_t port_id, struct ether_addr *addr)
297 {
298         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
299         if (addr == NULL)
300                 return -EINVAL;
301         rte_eth_macaddr_get(port_id, addr);
302
303         return 0;
304 }
305
306 int
307 rte_ethtool_net_set_mac_addr(uint8_t port_id, struct ether_addr *addr)
308 {
309         if (addr == NULL)
310                 return -EINVAL;
311         return rte_eth_dev_default_mac_addr_set(port_id, addr);
312 }
313
314 int
315 rte_ethtool_net_validate_addr(uint8_t port_id __rte_unused,
316         struct ether_addr *addr)
317 {
318         if (addr == NULL)
319                 return -EINVAL;
320         return is_valid_assigned_ether_addr(addr);
321 }
322
323 int
324 rte_ethtool_net_change_mtu(uint8_t port_id, int mtu)
325 {
326         if (mtu < 0 || mtu > UINT16_MAX)
327                 return -EINVAL;
328         return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
329 }
330
331 int
332 rte_ethtool_net_get_stats64(uint8_t port_id, struct rte_eth_stats *stats)
333 {
334         if (stats == NULL)
335                 return -EINVAL;
336         return rte_eth_stats_get(port_id, stats);
337 }
338
339 int
340 rte_ethtool_net_vlan_rx_add_vid(uint8_t port_id, uint16_t vid)
341 {
342         return rte_eth_dev_vlan_filter(port_id, vid, 1);
343 }
344
345 int
346 rte_ethtool_net_vlan_rx_kill_vid(uint8_t port_id, uint16_t vid)
347 {
348         return rte_eth_dev_vlan_filter(port_id, vid, 0);
349 }
350
351 /*
352  * The set_rx_mode provides driver-specific rx mode setting.
353  * This implementation implements rx mode setting based upon
354  * ixgbe/igb drivers. Further improvement is to provide a
355  * callback op field over struct rte_eth_dev::dev_ops so each
356  * driver can register device-specific implementation
357  */
358 int
359 rte_ethtool_net_set_rx_mode(uint8_t port_id)
360 {
361         uint16_t num_vfs;
362         struct rte_eth_dev_info dev_info;
363         uint16_t vf;
364
365         memset(&dev_info, 0, sizeof(dev_info));
366         rte_eth_dev_info_get(port_id, &dev_info);
367         num_vfs = dev_info.max_vfs;
368
369         /* Set VF vf_rx_mode, VF unsupport status is discard */
370         for (vf = 0; vf < num_vfs; vf++)
371                 rte_eth_dev_set_vf_rxmode(port_id, vf,
372                         ETH_VMDQ_ACCEPT_UNTAG, 0);
373
374         /* Enable Rx vlan filter, VF unspport status is discard */
375         rte_eth_dev_set_vlan_offload(port_id, ETH_VLAN_FILTER_MASK);
376
377         return 0;
378 }
379
380
381 int
382 rte_ethtool_get_ringparam(uint8_t port_id,
383         struct ethtool_ringparam *ring_param)
384 {
385         struct rte_eth_dev_info dev_info;
386         struct rte_eth_rxq_info rx_qinfo;
387         struct rte_eth_txq_info tx_qinfo;
388         int stat;
389
390         if (ring_param == NULL)
391                 return -EINVAL;
392
393         rte_eth_dev_info_get(port_id, &dev_info);
394
395         stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
396         if (stat != 0)
397                 return stat;
398
399         stat = rte_eth_tx_queue_info_get(port_id, 0, &tx_qinfo);
400         if (stat != 0)
401                 return stat;
402
403         memset(ring_param, 0, sizeof(*ring_param));
404         ring_param->rx_pending = rx_qinfo.nb_desc;
405         ring_param->rx_max_pending = dev_info.rx_desc_lim.nb_max;
406         ring_param->tx_pending = tx_qinfo.nb_desc;
407         ring_param->tx_max_pending = dev_info.tx_desc_lim.nb_max;
408
409         return 0;
410 }
411
412
413 int
414 rte_ethtool_set_ringparam(uint8_t port_id,
415         struct ethtool_ringparam *ring_param)
416 {
417         struct rte_eth_rxq_info rx_qinfo;
418         int stat;
419
420         if (ring_param == NULL)
421                 return -EINVAL;
422
423         stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
424         if (stat != 0)
425                 return stat;
426
427         rte_eth_dev_stop(port_id);
428
429         stat = rte_eth_tx_queue_setup(port_id, 0, ring_param->tx_pending,
430                 rte_socket_id(), NULL);
431         if (stat != 0)
432                 return stat;
433
434         stat = rte_eth_rx_queue_setup(port_id, 0, ring_param->rx_pending,
435                 rte_socket_id(), NULL, rx_qinfo.mp);
436         if (stat != 0)
437                 return stat;
438
439         return rte_eth_dev_start(port_id);
440 }