ethdev: change promiscuous callbacks to return status
[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 Technologies, Ltd
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 #include <rte_string_fns.h>
43
44 #include "mlx4.h"
45 #include "mlx4_flow.h"
46 #include "mlx4_glue.h"
47 #include "mlx4_rxtx.h"
48 #include "mlx4_utils.h"
49
50 /**
51  * Get interface name from private structure.
52  *
53  * @param[in] priv
54  *   Pointer to private structure.
55  * @param[out] ifname
56  *   Interface name output buffer.
57  *
58  * @return
59  *   0 on success, negative errno value otherwise and rte_errno is set.
60  */
61 int
62 mlx4_get_ifname(const struct mlx4_priv *priv, char (*ifname)[IF_NAMESIZE])
63 {
64         DIR *dir;
65         struct dirent *dent;
66         unsigned int dev_type = 0;
67         unsigned int dev_port_prev = ~0u;
68         char match[IF_NAMESIZE] = "";
69
70         {
71                 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
72
73                 dir = opendir(path);
74                 if (dir == NULL) {
75                         rte_errno = errno;
76                         return -rte_errno;
77                 }
78         }
79         while ((dent = readdir(dir)) != NULL) {
80                 char *name = dent->d_name;
81                 FILE *file;
82                 unsigned int dev_port;
83                 int r;
84
85                 if ((name[0] == '.') &&
86                     ((name[1] == '\0') ||
87                      ((name[1] == '.') && (name[2] == '\0'))))
88                         continue;
89
90                 MKSTR(path, "%s/device/net/%s/%s",
91                       priv->ctx->device->ibdev_path, name,
92                       (dev_type ? "dev_id" : "dev_port"));
93
94                 file = fopen(path, "rb");
95                 if (file == NULL) {
96                         if (errno != ENOENT)
97                                 continue;
98                         /*
99                          * Switch to dev_id when dev_port does not exist as
100                          * is the case with Linux kernel versions < 3.15.
101                          */
102 try_dev_id:
103                         match[0] = '\0';
104                         if (dev_type)
105                                 break;
106                         dev_type = 1;
107                         dev_port_prev = ~0u;
108                         rewinddir(dir);
109                         continue;
110                 }
111                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
112                 fclose(file);
113                 if (r != 1)
114                         continue;
115                 /*
116                  * Switch to dev_id when dev_port returns the same value for
117                  * all ports. May happen when using a MOFED release older than
118                  * 3.0 with a Linux kernel >= 3.15.
119                  */
120                 if (dev_port == dev_port_prev)
121                         goto try_dev_id;
122                 dev_port_prev = dev_port;
123                 if (dev_port == (priv->port - 1u))
124                         strlcpy(match, name, sizeof(match));
125         }
126         closedir(dir);
127         if (match[0] == '\0') {
128                 rte_errno = ENODEV;
129                 return -rte_errno;
130         }
131         strncpy(*ifname, match, sizeof(*ifname));
132         return 0;
133 }
134
135 /**
136  * Perform ifreq ioctl() on associated Ethernet device.
137  *
138  * @param[in] priv
139  *   Pointer to private structure.
140  * @param req
141  *   Request number to pass to ioctl().
142  * @param[out] ifr
143  *   Interface request structure output buffer.
144  *
145  * @return
146  *   0 on success, negative errno value otherwise and rte_errno is set.
147  */
148 static int
149 mlx4_ifreq(const struct mlx4_priv *priv, int req, struct ifreq *ifr)
150 {
151         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
152         int ret;
153
154         if (sock == -1) {
155                 rte_errno = errno;
156                 return -rte_errno;
157         }
158         ret = mlx4_get_ifname(priv, &ifr->ifr_name);
159         if (!ret && ioctl(sock, req, ifr) == -1) {
160                 rte_errno = errno;
161                 ret = -rte_errno;
162         }
163         close(sock);
164         return ret;
165 }
166
167 /**
168  * Get MAC address by querying netdevice.
169  *
170  * @param[in] priv
171  *   Pointer to private structure.
172  * @param[out] mac
173  *   MAC address output buffer.
174  *
175  * @return
176  *   0 on success, negative errno value otherwise and rte_errno is set.
177  */
178 int
179 mlx4_get_mac(struct mlx4_priv *priv, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
180 {
181         struct ifreq request;
182         int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
183
184         if (ret)
185                 return ret;
186         memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
187         return 0;
188 }
189
190 /**
191  * Get device MTU.
192  *
193  * @param priv
194  *   Pointer to private structure.
195  * @param[out] mtu
196  *   MTU value output buffer.
197  *
198  * @return
199  *   0 on success, negative errno value otherwise and rte_errno is set.
200  */
201 int
202 mlx4_mtu_get(struct mlx4_priv *priv, uint16_t *mtu)
203 {
204         struct ifreq request;
205         int ret = mlx4_ifreq(priv, SIOCGIFMTU, &request);
206
207         if (ret)
208                 return ret;
209         *mtu = request.ifr_mtu;
210         return 0;
211 }
212
213 /**
214  * DPDK callback to change the MTU.
215  *
216  * @param priv
217  *   Pointer to Ethernet device structure.
218  * @param mtu
219  *   MTU value to set.
220  *
221  * @return
222  *   0 on success, negative errno value otherwise and rte_errno is set.
223  */
224 int
225 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
226 {
227         struct mlx4_priv *priv = dev->data->dev_private;
228         struct ifreq request = { .ifr_mtu = mtu, };
229         int ret = mlx4_ifreq(priv, SIOCSIFMTU, &request);
230
231         if (ret)
232                 return ret;
233         priv->mtu = mtu;
234         return 0;
235 }
236
237 /**
238  * Set device flags.
239  *
240  * @param priv
241  *   Pointer to private structure.
242  * @param keep
243  *   Bitmask for flags that must remain untouched.
244  * @param flags
245  *   Bitmask for flags to modify.
246  *
247  * @return
248  *   0 on success, negative errno value otherwise and rte_errno is set.
249  */
250 static int
251 mlx4_set_flags(struct mlx4_priv *priv, unsigned int keep, unsigned int flags)
252 {
253         struct ifreq request;
254         int ret = mlx4_ifreq(priv, SIOCGIFFLAGS, &request);
255
256         if (ret)
257                 return ret;
258         request.ifr_flags &= keep;
259         request.ifr_flags |= flags & ~keep;
260         return mlx4_ifreq(priv, SIOCSIFFLAGS, &request);
261 }
262
263 /**
264  * Change the link state (UP / DOWN).
265  *
266  * @param priv
267  *   Pointer to Ethernet device private data.
268  * @param up
269  *   Nonzero for link up, otherwise link down.
270  *
271  * @return
272  *   0 on success, negative errno value otherwise and rte_errno is set.
273  */
274 static int
275 mlx4_dev_set_link(struct mlx4_priv *priv, int up)
276 {
277         int err;
278
279         if (up) {
280                 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
281                 if (err)
282                         return err;
283         } else {
284                 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
285                 if (err)
286                         return err;
287         }
288         return 0;
289 }
290
291 /**
292  * DPDK callback to bring the link DOWN.
293  *
294  * @param dev
295  *   Pointer to Ethernet device structure.
296  *
297  * @return
298  *   0 on success, negative errno value otherwise and rte_errno is set.
299  */
300 int
301 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
302 {
303         struct mlx4_priv *priv = dev->data->dev_private;
304
305         return mlx4_dev_set_link(priv, 0);
306 }
307
308 /**
309  * DPDK callback to bring the link UP.
310  *
311  * @param dev
312  *   Pointer to Ethernet device structure.
313  *
314  * @return
315  *   0 on success, negative errno value otherwise and rte_errno is set.
316  */
317 int
318 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
319 {
320         struct mlx4_priv *priv = dev->data->dev_private;
321
322         return mlx4_dev_set_link(priv, 1);
323 }
324
325 /**
326  * Supported Rx mode toggles.
327  *
328  * Even and odd values respectively stand for off and on.
329  */
330 enum rxmode_toggle {
331         RXMODE_TOGGLE_PROMISC_OFF,
332         RXMODE_TOGGLE_PROMISC_ON,
333         RXMODE_TOGGLE_ALLMULTI_OFF,
334         RXMODE_TOGGLE_ALLMULTI_ON,
335 };
336
337 /**
338  * Helper function to toggle promiscuous and all multicast modes.
339  *
340  * @param dev
341  *   Pointer to Ethernet device structure.
342  * @param toggle
343  *   Toggle to set.
344  *
345  * @return
346  *   0 on success, a negative errno value otherwise and rte_errno is set.
347  */
348 static int
349 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
350 {
351         struct mlx4_priv *priv = dev->data->dev_private;
352         const char *mode;
353         struct rte_flow_error error;
354         int ret;
355
356         switch (toggle) {
357         case RXMODE_TOGGLE_PROMISC_OFF:
358         case RXMODE_TOGGLE_PROMISC_ON:
359                 mode = "promiscuous";
360                 dev->data->promiscuous = toggle & 1;
361                 break;
362         case RXMODE_TOGGLE_ALLMULTI_OFF:
363         case RXMODE_TOGGLE_ALLMULTI_ON:
364                 mode = "all multicast";
365                 dev->data->all_multicast = toggle & 1;
366                 break;
367         default:
368                 mode = "undefined";
369         }
370
371         ret = mlx4_flow_sync(priv, &error);
372         if (!ret)
373                 return 0;
374
375         ERROR("cannot toggle %s mode (code %d, \"%s\"),"
376               " flow error type %d, cause %p, message: %s",
377               mode, rte_errno, strerror(rte_errno), error.type, error.cause,
378               error.message ? error.message : "(unspecified)");
379         return ret;
380 }
381
382 /**
383  * DPDK callback to enable promiscuous mode.
384  *
385  * @param dev
386  *   Pointer to Ethernet device structure.
387  *
388  * @return
389  *   0 on success, a negative errno value otherwise and rte_errno is set.
390  */
391 int
392 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
393 {
394         return mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
395 }
396
397 /**
398  * DPDK callback to disable promiscuous mode.
399  *
400  * @param dev
401  *   Pointer to Ethernet device structure.
402  *
403  * @return
404  *   0 on success, a negative errno value otherwise and rte_errno is set.
405  */
406 int
407 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
408 {
409         return mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
410 }
411
412 /**
413  * DPDK callback to enable all multicast mode.
414  *
415  * @param dev
416  *   Pointer to Ethernet device structure.
417  */
418 void
419 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
420 {
421         mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
422 }
423
424 /**
425  * DPDK callback to disable all multicast mode.
426  *
427  * @param dev
428  *   Pointer to Ethernet device structure.
429  */
430 void
431 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
432 {
433         mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
434 }
435
436 /**
437  * DPDK callback to remove a MAC address.
438  *
439  * @param dev
440  *   Pointer to Ethernet device structure.
441  * @param index
442  *   MAC address index.
443  */
444 void
445 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
446 {
447         struct mlx4_priv *priv = dev->data->dev_private;
448         struct rte_flow_error error;
449
450         if (index >= RTE_DIM(priv->mac) - priv->mac_mc) {
451                 rte_errno = EINVAL;
452                 return;
453         }
454         memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
455         if (!mlx4_flow_sync(priv, &error))
456                 return;
457         ERROR("failed to synchronize flow rules after removing MAC address"
458               " at index %d (code %d, \"%s\"),"
459               " flow error type %d, cause %p, message: %s",
460               index, rte_errno, strerror(rte_errno), error.type, error.cause,
461               error.message ? error.message : "(unspecified)");
462 }
463
464 /**
465  * DPDK callback to add a MAC address.
466  *
467  * @param dev
468  *   Pointer to Ethernet device structure.
469  * @param mac_addr
470  *   MAC address to register.
471  * @param index
472  *   MAC address index.
473  * @param vmdq
474  *   VMDq pool index to associate address with (ignored).
475  *
476  * @return
477  *   0 on success, negative errno value otherwise and rte_errno is set.
478  */
479 int
480 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
481                   uint32_t index, uint32_t vmdq)
482 {
483         struct mlx4_priv *priv = dev->data->dev_private;
484         struct rte_flow_error error;
485         int ret;
486
487         (void)vmdq;
488         if (index >= RTE_DIM(priv->mac) - priv->mac_mc) {
489                 rte_errno = EINVAL;
490                 return -rte_errno;
491         }
492         memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
493         ret = mlx4_flow_sync(priv, &error);
494         if (!ret)
495                 return 0;
496         ERROR("failed to synchronize flow rules after adding MAC address"
497               " at index %d (code %d, \"%s\"),"
498               " flow error type %d, cause %p, message: %s",
499               index, rte_errno, strerror(rte_errno), error.type, error.cause,
500               error.message ? error.message : "(unspecified)");
501         return ret;
502 }
503
504 /**
505  * DPDK callback to configure multicast addresses.
506  *
507  * @param dev
508  *   Pointer to Ethernet device structure.
509  * @param list
510  *   List of MAC addresses to register.
511  * @param num
512  *   Number of entries in list.
513  *
514  * @return
515  *   0 on success, negative errno value otherwise and rte_errno is set.
516  */
517 int
518 mlx4_set_mc_addr_list(struct rte_eth_dev *dev, struct rte_ether_addr *list,
519                       uint32_t num)
520 {
521         struct mlx4_priv *priv = dev->data->dev_private;
522         struct rte_flow_error error;
523         int ret;
524
525         if (num > RTE_DIM(priv->mac)) {
526                 rte_errno = EINVAL;
527                 return -rte_errno;
528         }
529         /*
530          * Make sure there is enough room to increase the number of
531          * multicast entries without overwriting standard entries.
532          */
533         if (num > priv->mac_mc) {
534                 unsigned int i;
535
536                 for (i = RTE_DIM(priv->mac) - num;
537                      i != RTE_DIM(priv->mac) - priv->mac_mc;
538                      ++i)
539                         if (!rte_is_zero_ether_addr(&priv->mac[i])) {
540                                 rte_errno = EBUSY;
541                                 return -rte_errno;
542                         }
543         } else if (num < priv->mac_mc) {
544                 /* Clear unused entries. */
545                 memset(priv->mac + RTE_DIM(priv->mac) - priv->mac_mc,
546                        0,
547                        sizeof(priv->mac[0]) * (priv->mac_mc - num));
548         }
549         memcpy(priv->mac + RTE_DIM(priv->mac) - num, list, sizeof(*list) * num);
550         priv->mac_mc = num;
551         ret = mlx4_flow_sync(priv, &error);
552         if (!ret)
553                 return 0;
554         ERROR("failed to synchronize flow rules after modifying MC list,"
555               " (code %d, \"%s\"), flow error type %d, cause %p, message: %s",
556               rte_errno, strerror(rte_errno), error.type, error.cause,
557               error.message ? error.message : "(unspecified)");
558         return ret;
559 }
560
561 /**
562  * DPDK callback to configure a VLAN filter.
563  *
564  * @param dev
565  *   Pointer to Ethernet device structure.
566  * @param vlan_id
567  *   VLAN ID to filter.
568  * @param on
569  *   Toggle filter.
570  *
571  * @return
572  *   0 on success, negative errno value otherwise and rte_errno is set.
573  */
574 int
575 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
576 {
577         struct mlx4_priv *priv = dev->data->dev_private;
578         struct rte_flow_error error;
579         unsigned int vidx = vlan_id / 64;
580         unsigned int vbit = vlan_id % 64;
581         uint64_t *v;
582         int ret;
583
584         if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
585                 rte_errno = EINVAL;
586                 return -rte_errno;
587         }
588         v = &dev->data->vlan_filter_conf.ids[vidx];
589         *v &= ~(UINT64_C(1) << vbit);
590         *v |= (uint64_t)!!on << vbit;
591         ret = mlx4_flow_sync(priv, &error);
592         if (!ret)
593                 return 0;
594         ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
595               " (code %d, \"%s\"), "
596               " flow error type %d, cause %p, message: %s",
597               on ? "enabling" : "disabling", vlan_id,
598               rte_errno, strerror(rte_errno), error.type, error.cause,
599               error.message ? error.message : "(unspecified)");
600         return ret;
601 }
602
603 /**
604  * DPDK callback to set the primary MAC address.
605  *
606  * @param dev
607  *   Pointer to Ethernet device structure.
608  * @param mac_addr
609  *   MAC address to register.
610  *
611  * @return
612  *   0 on success, negative errno value otherwise and rte_errno is set.
613  */
614 int
615 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
616 {
617         return mlx4_mac_addr_add(dev, mac_addr, 0, 0);
618 }
619
620 /**
621  * DPDK callback to get information about the device.
622  *
623  * @param dev
624  *   Pointer to Ethernet device structure.
625  * @param[out] info
626  *   Info structure output buffer.
627  */
628 int
629 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
630 {
631         struct mlx4_priv *priv = dev->data->dev_private;
632         unsigned int max;
633
634         /* FIXME: we should ask the device for these values. */
635         info->min_rx_bufsize = 32;
636         info->max_rx_pktlen = 65536;
637         /*
638          * Since we need one CQ per QP, the limit is the minimum number
639          * between the two values.
640          */
641         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
642                priv->device_attr.max_qp : priv->device_attr.max_cq);
643         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
644         if (max >= 65535)
645                 max = 65535;
646         info->max_rx_queues = max;
647         info->max_tx_queues = max;
648         info->max_mac_addrs = RTE_DIM(priv->mac);
649         info->tx_offload_capa = mlx4_get_tx_port_offloads(priv);
650         info->rx_queue_offload_capa = mlx4_get_rx_queue_offloads(priv);
651         info->rx_offload_capa = (mlx4_get_rx_port_offloads(priv) |
652                                  info->rx_queue_offload_capa);
653         info->if_index = priv->if_index;
654         info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
655         info->speed_capa =
656                         ETH_LINK_SPEED_1G |
657                         ETH_LINK_SPEED_10G |
658                         ETH_LINK_SPEED_20G |
659                         ETH_LINK_SPEED_40G |
660                         ETH_LINK_SPEED_56G;
661         info->flow_type_rss_offloads = mlx4_conv_rss_types(priv, 0, 1);
662
663         return 0;
664 }
665
666 /**
667  * Get firmware version of a device.
668  *
669  * @param dev
670  *   Ethernet device port.
671  * @param fw_ver
672  *   String output allocated by caller.
673  * @param fw_size
674  *   Size of the output string, including terminating null byte.
675  *
676  * @return
677  *   0 on success, or the size of the non truncated string if too big.
678  */
679 int mlx4_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
680 {
681         struct mlx4_priv *priv = dev->data->dev_private;
682         struct ibv_device_attr *attr = &priv->device_attr;
683         size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
684
685         if (fw_size < size)
686                 return size;
687         if (fw_ver != NULL)
688                 strlcpy(fw_ver, attr->fw_ver, fw_size);
689         return 0;
690 }
691
692 /**
693  * DPDK callback to get device statistics.
694  *
695  * @param dev
696  *   Pointer to Ethernet device structure.
697  * @param[out] stats
698  *   Stats structure output buffer.
699  */
700 int
701 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
702 {
703         struct rte_eth_stats tmp;
704         unsigned int i;
705         unsigned int idx;
706
707         memset(&tmp, 0, sizeof(tmp));
708         /* Add software counters. */
709         for (i = 0; i != dev->data->nb_rx_queues; ++i) {
710                 struct rxq *rxq = dev->data->rx_queues[i];
711
712                 if (rxq == NULL)
713                         continue;
714                 idx = rxq->stats.idx;
715                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
716                         tmp.q_ipackets[idx] += rxq->stats.ipackets;
717                         tmp.q_ibytes[idx] += rxq->stats.ibytes;
718                         tmp.q_errors[idx] += (rxq->stats.idropped +
719                                               rxq->stats.rx_nombuf);
720                 }
721                 tmp.ipackets += rxq->stats.ipackets;
722                 tmp.ibytes += rxq->stats.ibytes;
723                 tmp.ierrors += rxq->stats.idropped;
724                 tmp.rx_nombuf += rxq->stats.rx_nombuf;
725         }
726         for (i = 0; i != dev->data->nb_tx_queues; ++i) {
727                 struct txq *txq = dev->data->tx_queues[i];
728
729                 if (txq == NULL)
730                         continue;
731                 idx = txq->stats.idx;
732                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
733                         tmp.q_opackets[idx] += txq->stats.opackets;
734                         tmp.q_obytes[idx] += txq->stats.obytes;
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 mlx4_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 = ETH_SPEED_NUM_NONE;
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 mlx4_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 mlx4_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 }
915
916 /**
917  * DPDK callback to retrieve the received packet types that are recognized
918  * by the device.
919  *
920  * @param dev
921  *   Pointer to Ethernet device structure.
922  *
923  * @return
924  *   Pointer to an array of recognized packet types if in Rx burst mode,
925  *   NULL otherwise.
926  */
927 const uint32_t *
928 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
929 {
930         static const uint32_t ptypes[] = {
931                 /* refers to rxq_cq_to_pkt_type() */
932                 RTE_PTYPE_L2_ETHER,
933                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
934                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
935                 RTE_PTYPE_L4_FRAG,
936                 RTE_PTYPE_L4_TCP,
937                 RTE_PTYPE_L4_UDP,
938                 RTE_PTYPE_UNKNOWN
939         };
940         static const uint32_t ptypes_l2tun[] = {
941                 /* refers to rxq_cq_to_pkt_type() */
942                 RTE_PTYPE_L2_ETHER,
943                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
944                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
945                 RTE_PTYPE_L4_FRAG,
946                 RTE_PTYPE_L4_TCP,
947                 RTE_PTYPE_L4_UDP,
948                 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
949                 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
950                 RTE_PTYPE_UNKNOWN
951         };
952         struct mlx4_priv *priv = dev->data->dev_private;
953
954         if (dev->rx_pkt_burst == mlx4_rx_burst) {
955                 if (priv->hw_csum_l2tun)
956                         return ptypes_l2tun;
957                 else
958                         return ptypes;
959         }
960         return NULL;
961 }
962
963 /**
964  * Check if mlx4 device was removed.
965  *
966  * @param dev
967  *   Pointer to Ethernet device structure.
968  *
969  * @return
970  *   1 when device is removed, otherwise 0.
971  */
972 int
973 mlx4_is_removed(struct rte_eth_dev *dev)
974 {
975         struct ibv_device_attr device_attr;
976         struct mlx4_priv *priv = dev->data->dev_private;
977
978         if (mlx4_glue->query_device(priv->ctx, &device_attr) == EIO)
979                 return 1;
980         return 0;
981 }