7721f13ed21df3685473820297124f7bda1afa8d
[dpdk.git] / drivers / net / mlx4 / mlx4_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox
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 6WIND S.A. 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
34 /**
35  * @file
36  * Miscellaneous control operations for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <dirent.h>
41 #include <errno.h>
42 #include <linux/ethtool.h>
43 #include <linux/sockios.h>
44 #include <net/if.h>
45 #include <netinet/ip.h>
46 #include <stddef.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
53 #include <unistd.h>
54
55 /* Verbs headers do not support -pedantic. */
56 #ifdef PEDANTIC
57 #pragma GCC diagnostic ignored "-Wpedantic"
58 #endif
59 #include <infiniband/verbs.h>
60 #ifdef PEDANTIC
61 #pragma GCC diagnostic error "-Wpedantic"
62 #endif
63
64 #include <rte_errno.h>
65 #include <rte_ethdev.h>
66 #include <rte_ether.h>
67 #include <rte_flow.h>
68 #include <rte_pci.h>
69
70 #include "mlx4.h"
71 #include "mlx4_flow.h"
72 #include "mlx4_rxtx.h"
73 #include "mlx4_utils.h"
74
75 /**
76  * Get interface name from private structure.
77  *
78  * @param[in] priv
79  *   Pointer to private structure.
80  * @param[out] ifname
81  *   Interface name output buffer.
82  *
83  * @return
84  *   0 on success, negative errno value otherwise and rte_errno is set.
85  */
86 int
87 mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
88 {
89         DIR *dir;
90         struct dirent *dent;
91         unsigned int dev_type = 0;
92         unsigned int dev_port_prev = ~0u;
93         char match[IF_NAMESIZE] = "";
94
95         {
96                 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
97
98                 dir = opendir(path);
99                 if (dir == NULL) {
100                         rte_errno = errno;
101                         return -rte_errno;
102                 }
103         }
104         while ((dent = readdir(dir)) != NULL) {
105                 char *name = dent->d_name;
106                 FILE *file;
107                 unsigned int dev_port;
108                 int r;
109
110                 if ((name[0] == '.') &&
111                     ((name[1] == '\0') ||
112                      ((name[1] == '.') && (name[2] == '\0'))))
113                         continue;
114
115                 MKSTR(path, "%s/device/net/%s/%s",
116                       priv->ctx->device->ibdev_path, name,
117                       (dev_type ? "dev_id" : "dev_port"));
118
119                 file = fopen(path, "rb");
120                 if (file == NULL) {
121                         if (errno != ENOENT)
122                                 continue;
123                         /*
124                          * Switch to dev_id when dev_port does not exist as
125                          * is the case with Linux kernel versions < 3.15.
126                          */
127 try_dev_id:
128                         match[0] = '\0';
129                         if (dev_type)
130                                 break;
131                         dev_type = 1;
132                         dev_port_prev = ~0u;
133                         rewinddir(dir);
134                         continue;
135                 }
136                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
137                 fclose(file);
138                 if (r != 1)
139                         continue;
140                 /*
141                  * Switch to dev_id when dev_port returns the same value for
142                  * all ports. May happen when using a MOFED release older than
143                  * 3.0 with a Linux kernel >= 3.15.
144                  */
145                 if (dev_port == dev_port_prev)
146                         goto try_dev_id;
147                 dev_port_prev = dev_port;
148                 if (dev_port == (priv->port - 1u))
149                         snprintf(match, sizeof(match), "%s", name);
150         }
151         closedir(dir);
152         if (match[0] == '\0') {
153                 rte_errno = ENODEV;
154                 return -rte_errno;
155         }
156         strncpy(*ifname, match, sizeof(*ifname));
157         return 0;
158 }
159
160 /**
161  * Read from sysfs entry.
162  *
163  * @param[in] priv
164  *   Pointer to private structure.
165  * @param[in] entry
166  *   Entry name relative to sysfs path.
167  * @param[out] buf
168  *   Data output buffer.
169  * @param size
170  *   Buffer size.
171  *
172  * @return
173  *   Number of bytes read on success, negative errno value otherwise and
174  *   rte_errno is set.
175  */
176 static int
177 mlx4_sysfs_read(const struct priv *priv, const char *entry,
178                 char *buf, size_t size)
179 {
180         char ifname[IF_NAMESIZE];
181         FILE *file;
182         int ret;
183
184         ret = mlx4_get_ifname(priv, &ifname);
185         if (ret)
186                 return ret;
187
188         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
189               ifname, entry);
190
191         file = fopen(path, "rb");
192         if (file == NULL) {
193                 rte_errno = errno;
194                 return -rte_errno;
195         }
196         ret = fread(buf, 1, size, file);
197         if ((size_t)ret < size && ferror(file)) {
198                 rte_errno = EIO;
199                 ret = -rte_errno;
200         } else {
201                 ret = size;
202         }
203         fclose(file);
204         return ret;
205 }
206
207 /**
208  * Write to sysfs entry.
209  *
210  * @param[in] priv
211  *   Pointer to private structure.
212  * @param[in] entry
213  *   Entry name relative to sysfs path.
214  * @param[in] buf
215  *   Data buffer.
216  * @param size
217  *   Buffer size.
218  *
219  * @return
220  *   Number of bytes written on success, negative errno value otherwise and
221  *   rte_errno is set.
222  */
223 static int
224 mlx4_sysfs_write(const struct priv *priv, const char *entry,
225                  char *buf, size_t size)
226 {
227         char ifname[IF_NAMESIZE];
228         FILE *file;
229         int ret;
230
231         ret = mlx4_get_ifname(priv, &ifname);
232         if (ret)
233                 return ret;
234
235         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
236               ifname, entry);
237
238         file = fopen(path, "wb");
239         if (file == NULL) {
240                 rte_errno = errno;
241                 return -rte_errno;
242         }
243         ret = fwrite(buf, 1, size, file);
244         if ((size_t)ret < size || ferror(file)) {
245                 rte_errno = EIO;
246                 ret = -rte_errno;
247         } else {
248                 ret = size;
249         }
250         fclose(file);
251         return ret;
252 }
253
254 /**
255  * Get unsigned long sysfs property.
256  *
257  * @param priv
258  *   Pointer to private structure.
259  * @param[in] name
260  *   Entry name relative to sysfs path.
261  * @param[out] value
262  *   Value output buffer.
263  *
264  * @return
265  *   0 on success, negative errno value otherwise and rte_errno is set.
266  */
267 static int
268 mlx4_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
269 {
270         int ret;
271         unsigned long value_ret;
272         char value_str[32];
273
274         ret = mlx4_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
275         if (ret < 0) {
276                 DEBUG("cannot read %s value from sysfs: %s",
277                       name, strerror(rte_errno));
278                 return ret;
279         }
280         value_str[ret] = '\0';
281         errno = 0;
282         value_ret = strtoul(value_str, NULL, 0);
283         if (errno) {
284                 rte_errno = errno;
285                 DEBUG("invalid %s value `%s': %s", name, value_str,
286                       strerror(rte_errno));
287                 return -rte_errno;
288         }
289         *value = value_ret;
290         return 0;
291 }
292
293 /**
294  * Set unsigned long sysfs property.
295  *
296  * @param priv
297  *   Pointer to private structure.
298  * @param[in] name
299  *   Entry name relative to sysfs path.
300  * @param value
301  *   Value to set.
302  *
303  * @return
304  *   0 on success, negative errno value otherwise and rte_errno is set.
305  */
306 static int
307 mlx4_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
308 {
309         int ret;
310         MKSTR(value_str, "%lu", value);
311
312         ret = mlx4_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
313         if (ret < 0) {
314                 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
315                       name, value_str, value, strerror(rte_errno));
316                 return ret;
317         }
318         return 0;
319 }
320
321 /**
322  * Perform ifreq ioctl() on associated Ethernet device.
323  *
324  * @param[in] priv
325  *   Pointer to private structure.
326  * @param req
327  *   Request number to pass to ioctl().
328  * @param[out] ifr
329  *   Interface request structure output buffer.
330  *
331  * @return
332  *   0 on success, negative errno value otherwise and rte_errno is set.
333  */
334 static int
335 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
336 {
337         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
338         int ret;
339
340         if (sock == -1) {
341                 rte_errno = errno;
342                 return -rte_errno;
343         }
344         ret = mlx4_get_ifname(priv, &ifr->ifr_name);
345         if (!ret && ioctl(sock, req, ifr) == -1) {
346                 rte_errno = errno;
347                 ret = -rte_errno;
348         }
349         close(sock);
350         return ret;
351 }
352
353 /**
354  * Get MAC address by querying netdevice.
355  *
356  * @param[in] priv
357  *   Pointer to private structure.
358  * @param[out] mac
359  *   MAC address output buffer.
360  *
361  * @return
362  *   0 on success, negative errno value otherwise and rte_errno is set.
363  */
364 int
365 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
366 {
367         struct ifreq request;
368         int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
369
370         if (ret)
371                 return ret;
372         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
373         return 0;
374 }
375
376 /**
377  * Get device MTU.
378  *
379  * @param priv
380  *   Pointer to private structure.
381  * @param[out] mtu
382  *   MTU value output buffer.
383  *
384  * @return
385  *   0 on success, negative errno value otherwise and rte_errno is set.
386  */
387 int
388 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
389 {
390         unsigned long ulong_mtu = 0;
391         int ret = mlx4_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
392
393         if (ret)
394                 return ret;
395         *mtu = ulong_mtu;
396         return 0;
397 }
398
399 /**
400  * DPDK callback to change the MTU.
401  *
402  * @param priv
403  *   Pointer to Ethernet device structure.
404  * @param mtu
405  *   MTU value to set.
406  *
407  * @return
408  *   0 on success, negative errno value otherwise and rte_errno is set.
409  */
410 int
411 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
412 {
413         struct priv *priv = dev->data->dev_private;
414         uint16_t new_mtu;
415         int ret = mlx4_set_sysfs_ulong(priv, "mtu", mtu);
416
417         if (ret)
418                 return ret;
419         ret = mlx4_mtu_get(priv, &new_mtu);
420         if (ret)
421                 return ret;
422         if (new_mtu == mtu) {
423                 priv->mtu = mtu;
424                 return 0;
425         }
426         rte_errno = EINVAL;
427         return -rte_errno;
428 }
429
430 /**
431  * Set device flags.
432  *
433  * @param priv
434  *   Pointer to private structure.
435  * @param keep
436  *   Bitmask for flags that must remain untouched.
437  * @param flags
438  *   Bitmask for flags to modify.
439  *
440  * @return
441  *   0 on success, negative errno value otherwise and rte_errno is set.
442  */
443 static int
444 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
445 {
446         unsigned long tmp = 0;
447         int ret = mlx4_get_sysfs_ulong(priv, "flags", &tmp);
448
449         if (ret)
450                 return ret;
451         tmp &= keep;
452         tmp |= (flags & (~keep));
453         return mlx4_set_sysfs_ulong(priv, "flags", tmp);
454 }
455
456 /**
457  * Change the link state (UP / DOWN).
458  *
459  * @param priv
460  *   Pointer to Ethernet device private data.
461  * @param up
462  *   Nonzero for link up, otherwise link down.
463  *
464  * @return
465  *   0 on success, negative errno value otherwise and rte_errno is set.
466  */
467 static int
468 mlx4_dev_set_link(struct priv *priv, int up)
469 {
470         struct rte_eth_dev *dev = priv->dev;
471         int err;
472
473         if (up) {
474                 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
475                 if (err)
476                         return err;
477                 dev->rx_pkt_burst = mlx4_rx_burst;
478         } else {
479                 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
480                 if (err)
481                         return err;
482                 dev->rx_pkt_burst = mlx4_rx_burst_removed;
483                 dev->tx_pkt_burst = mlx4_tx_burst_removed;
484         }
485         return 0;
486 }
487
488 /**
489  * DPDK callback to bring the link DOWN.
490  *
491  * @param dev
492  *   Pointer to Ethernet device structure.
493  *
494  * @return
495  *   0 on success, negative errno value otherwise and rte_errno is set.
496  */
497 int
498 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
499 {
500         struct priv *priv = dev->data->dev_private;
501
502         return mlx4_dev_set_link(priv, 0);
503 }
504
505 /**
506  * DPDK callback to bring the link UP.
507  *
508  * @param dev
509  *   Pointer to Ethernet device structure.
510  *
511  * @return
512  *   0 on success, negative errno value otherwise and rte_errno is set.
513  */
514 int
515 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
516 {
517         struct priv *priv = dev->data->dev_private;
518
519         return mlx4_dev_set_link(priv, 1);
520 }
521
522 /**
523  * DPDK callback to remove a MAC address.
524  *
525  * @param dev
526  *   Pointer to Ethernet device structure.
527  * @param index
528  *   MAC address index.
529  */
530 void
531 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
532 {
533         struct priv *priv = dev->data->dev_private;
534         struct rte_flow_error error;
535
536         if (index >= RTE_DIM(priv->mac)) {
537                 rte_errno = EINVAL;
538                 return;
539         }
540         memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
541         if (!mlx4_flow_sync(priv, &error))
542                 return;
543         ERROR("failed to synchronize flow rules after removing MAC address"
544               " at index %d (code %d, \"%s\"),"
545               " flow error type %d, cause %p, message: %s",
546               index, rte_errno, strerror(rte_errno), error.type, error.cause,
547               error.message ? error.message : "(unspecified)");
548 }
549
550 /**
551  * DPDK callback to add a MAC address.
552  *
553  * @param dev
554  *   Pointer to Ethernet device structure.
555  * @param mac_addr
556  *   MAC address to register.
557  * @param index
558  *   MAC address index.
559  * @param vmdq
560  *   VMDq pool index to associate address with (ignored).
561  *
562  * @return
563  *   0 on success, negative errno value otherwise and rte_errno is set.
564  */
565 int
566 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
567                   uint32_t index, uint32_t vmdq)
568 {
569         struct priv *priv = dev->data->dev_private;
570         struct rte_flow_error error;
571         int ret;
572
573         (void)vmdq;
574         if (index >= RTE_DIM(priv->mac)) {
575                 rte_errno = EINVAL;
576                 return -rte_errno;
577         }
578         memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
579         ret = mlx4_flow_sync(priv, &error);
580         if (!ret)
581                 return 0;
582         ERROR("failed to synchronize flow rules after adding MAC address"
583               " at index %d (code %d, \"%s\"),"
584               " flow error type %d, cause %p, message: %s",
585               index, rte_errno, strerror(rte_errno), error.type, error.cause,
586               error.message ? error.message : "(unspecified)");
587         return ret;
588 }
589
590 /**
591  * DPDK callback to configure a VLAN filter.
592  *
593  * @param dev
594  *   Pointer to Ethernet device structure.
595  * @param vlan_id
596  *   VLAN ID to filter.
597  * @param on
598  *   Toggle filter.
599  *
600  * @return
601  *   0 on success, negative errno value otherwise and rte_errno is set.
602  */
603 int
604 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
605 {
606         struct priv *priv = dev->data->dev_private;
607         struct rte_flow_error error;
608         unsigned int vidx = vlan_id / 64;
609         unsigned int vbit = vlan_id % 64;
610         uint64_t *v;
611         int ret;
612
613         if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
614                 rte_errno = EINVAL;
615                 return -rte_errno;
616         }
617         v = &dev->data->vlan_filter_conf.ids[vidx];
618         *v &= ~(UINT64_C(1) << vbit);
619         *v |= (uint64_t)!!on << vbit;
620         ret = mlx4_flow_sync(priv, &error);
621         if (!ret)
622                 return 0;
623         ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
624               " (code %d, \"%s\"), "
625               " flow error type %d, cause %p, message: %s",
626               on ? "enabling" : "disabling", vlan_id,
627               rte_errno, strerror(rte_errno), error.type, error.cause,
628               error.message ? error.message : "(unspecified)");
629         return ret;
630 }
631
632 /**
633  * DPDK callback to set the primary MAC address.
634  *
635  * @param dev
636  *   Pointer to Ethernet device structure.
637  * @param mac_addr
638  *   MAC address to register.
639  */
640 void
641 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
642 {
643         mlx4_mac_addr_add(dev, mac_addr, 0, 0);
644 }
645
646 /**
647  * DPDK callback to get information about the device.
648  *
649  * @param dev
650  *   Pointer to Ethernet device structure.
651  * @param[out] info
652  *   Info structure output buffer.
653  */
654 void
655 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
656 {
657         struct priv *priv = dev->data->dev_private;
658         unsigned int max;
659         char ifname[IF_NAMESIZE];
660
661         info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
662         if (priv == NULL)
663                 return;
664         /* FIXME: we should ask the device for these values. */
665         info->min_rx_bufsize = 32;
666         info->max_rx_pktlen = 65536;
667         /*
668          * Since we need one CQ per QP, the limit is the minimum number
669          * between the two values.
670          */
671         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
672                priv->device_attr.max_qp : priv->device_attr.max_cq);
673         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
674         if (max >= 65535)
675                 max = 65535;
676         info->max_rx_queues = max;
677         info->max_tx_queues = max;
678         info->max_mac_addrs = RTE_DIM(priv->mac);
679         info->rx_offload_capa = 0;
680         info->tx_offload_capa = 0;
681         if (mlx4_get_ifname(priv, &ifname) == 0)
682                 info->if_index = if_nametoindex(ifname);
683         info->speed_capa =
684                         ETH_LINK_SPEED_1G |
685                         ETH_LINK_SPEED_10G |
686                         ETH_LINK_SPEED_20G |
687                         ETH_LINK_SPEED_40G |
688                         ETH_LINK_SPEED_56G;
689 }
690
691 /**
692  * DPDK callback to get device statistics.
693  *
694  * @param dev
695  *   Pointer to Ethernet device structure.
696  * @param[out] stats
697  *   Stats structure output buffer.
698  */
699 int
700 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
701 {
702         struct rte_eth_stats tmp;
703         unsigned int i;
704         unsigned int idx;
705
706         memset(&tmp, 0, sizeof(tmp));
707         /* Add software counters. */
708         for (i = 0; i != dev->data->nb_rx_queues; ++i) {
709                 struct rxq *rxq = dev->data->rx_queues[i];
710
711                 if (rxq == NULL)
712                         continue;
713                 idx = rxq->stats.idx;
714                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
715                         tmp.q_ipackets[idx] += rxq->stats.ipackets;
716                         tmp.q_ibytes[idx] += rxq->stats.ibytes;
717                         tmp.q_errors[idx] += (rxq->stats.idropped +
718                                               rxq->stats.rx_nombuf);
719                 }
720                 tmp.ipackets += rxq->stats.ipackets;
721                 tmp.ibytes += rxq->stats.ibytes;
722                 tmp.ierrors += rxq->stats.idropped;
723                 tmp.rx_nombuf += rxq->stats.rx_nombuf;
724         }
725         for (i = 0; i != dev->data->nb_tx_queues; ++i) {
726                 struct txq *txq = dev->data->tx_queues[i];
727
728                 if (txq == NULL)
729                         continue;
730                 idx = txq->stats.idx;
731                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
732                         tmp.q_opackets[idx] += txq->stats.opackets;
733                         tmp.q_obytes[idx] += txq->stats.obytes;
734                         tmp.q_errors[idx] += txq->stats.odropped;
735                 }
736                 tmp.opackets += txq->stats.opackets;
737                 tmp.obytes += txq->stats.obytes;
738                 tmp.oerrors += txq->stats.odropped;
739         }
740         *stats = tmp;
741         return 0;
742 }
743
744 /**
745  * DPDK callback to clear device statistics.
746  *
747  * @param dev
748  *   Pointer to Ethernet device structure.
749  */
750 void
751 mlx4_stats_reset(struct rte_eth_dev *dev)
752 {
753         unsigned int i;
754
755         for (i = 0; i != dev->data->nb_rx_queues; ++i) {
756                 struct rxq *rxq = dev->data->rx_queues[i];
757
758                 if (rxq)
759                         rxq->stats = (struct mlx4_rxq_stats){
760                                 .idx = rxq->stats.idx,
761                         };
762         }
763         for (i = 0; i != dev->data->nb_tx_queues; ++i) {
764                 struct txq *txq = dev->data->tx_queues[i];
765
766                 if (txq)
767                         txq->stats = (struct mlx4_txq_stats){
768                                 .idx = txq->stats.idx,
769                         };
770         }
771 }
772
773 /**
774  * DPDK callback to retrieve physical link information.
775  *
776  * @param dev
777  *   Pointer to Ethernet device structure.
778  * @param wait_to_complete
779  *   Wait for request completion (ignored).
780  *
781  * @return
782  *   0 on success, negative errno value otherwise and rte_errno is set.
783  */
784 int
785 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
786 {
787         const struct priv *priv = dev->data->dev_private;
788         struct ethtool_cmd edata = {
789                 .cmd = ETHTOOL_GSET,
790         };
791         struct ifreq ifr;
792         struct rte_eth_link dev_link;
793         int link_speed = 0;
794
795         if (priv == NULL) {
796                 rte_errno = EINVAL;
797                 return -rte_errno;
798         }
799         (void)wait_to_complete;
800         if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
801                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
802                 return -rte_errno;
803         }
804         memset(&dev_link, 0, sizeof(dev_link));
805         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
806                                 (ifr.ifr_flags & IFF_RUNNING));
807         ifr.ifr_data = (void *)&edata;
808         if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
809                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
810                      strerror(rte_errno));
811                 return -rte_errno;
812         }
813         link_speed = ethtool_cmd_speed(&edata);
814         if (link_speed == -1)
815                 dev_link.link_speed = 0;
816         else
817                 dev_link.link_speed = link_speed;
818         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
819                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
820         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
821                                   ETH_LINK_SPEED_FIXED);
822         dev->data->dev_link = dev_link;
823         return 0;
824 }
825
826 /**
827  * DPDK callback to get flow control status.
828  *
829  * @param dev
830  *   Pointer to Ethernet device structure.
831  * @param[out] fc_conf
832  *   Flow control output buffer.
833  *
834  * @return
835  *   0 on success, negative errno value otherwise and rte_errno is set.
836  */
837 int
838 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
839 {
840         struct priv *priv = dev->data->dev_private;
841         struct ifreq ifr;
842         struct ethtool_pauseparam ethpause = {
843                 .cmd = ETHTOOL_GPAUSEPARAM,
844         };
845         int ret;
846
847         ifr.ifr_data = (void *)&ethpause;
848         if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
849                 ret = rte_errno;
850                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
851                      " failed: %s",
852                      strerror(rte_errno));
853                 goto out;
854         }
855         fc_conf->autoneg = ethpause.autoneg;
856         if (ethpause.rx_pause && ethpause.tx_pause)
857                 fc_conf->mode = RTE_FC_FULL;
858         else if (ethpause.rx_pause)
859                 fc_conf->mode = RTE_FC_RX_PAUSE;
860         else if (ethpause.tx_pause)
861                 fc_conf->mode = RTE_FC_TX_PAUSE;
862         else
863                 fc_conf->mode = RTE_FC_NONE;
864         ret = 0;
865 out:
866         assert(ret >= 0);
867         return -ret;
868 }
869
870 /**
871  * DPDK callback to modify flow control parameters.
872  *
873  * @param dev
874  *   Pointer to Ethernet device structure.
875  * @param[in] fc_conf
876  *   Flow control parameters.
877  *
878  * @return
879  *   0 on success, negative errno value otherwise and rte_errno is set.
880  */
881 int
882 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
883 {
884         struct priv *priv = dev->data->dev_private;
885         struct ifreq ifr;
886         struct ethtool_pauseparam ethpause = {
887                 .cmd = ETHTOOL_SPAUSEPARAM,
888         };
889         int ret;
890
891         ifr.ifr_data = (void *)&ethpause;
892         ethpause.autoneg = fc_conf->autoneg;
893         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
894             (fc_conf->mode & RTE_FC_RX_PAUSE))
895                 ethpause.rx_pause = 1;
896         else
897                 ethpause.rx_pause = 0;
898         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
899             (fc_conf->mode & RTE_FC_TX_PAUSE))
900                 ethpause.tx_pause = 1;
901         else
902                 ethpause.tx_pause = 0;
903         if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
904                 ret = rte_errno;
905                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
906                      " failed: %s",
907                      strerror(rte_errno));
908                 goto out;
909         }
910         ret = 0;
911 out:
912         assert(ret >= 0);
913         return -ret;
914 }