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