net/mlx: control netdevices through ioctl only
[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  * Perform ifreq ioctl() on associated Ethernet device.
136  *
137  * @param[in] priv
138  *   Pointer to private structure.
139  * @param req
140  *   Request number to pass to ioctl().
141  * @param[out] ifr
142  *   Interface request structure output buffer.
143  *
144  * @return
145  *   0 on success, negative errno value otherwise and rte_errno is set.
146  */
147 static int
148 mlx4_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
149 {
150         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
151         int ret;
152
153         if (sock == -1) {
154                 rte_errno = errno;
155                 return -rte_errno;
156         }
157         ret = mlx4_get_ifname(priv, &ifr->ifr_name);
158         if (!ret && ioctl(sock, req, ifr) == -1) {
159                 rte_errno = errno;
160                 ret = -rte_errno;
161         }
162         close(sock);
163         return ret;
164 }
165
166 /**
167  * Get MAC address by querying netdevice.
168  *
169  * @param[in] priv
170  *   Pointer to private structure.
171  * @param[out] mac
172  *   MAC address output buffer.
173  *
174  * @return
175  *   0 on success, negative errno value otherwise and rte_errno is set.
176  */
177 int
178 mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
179 {
180         struct ifreq request;
181         int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
182
183         if (ret)
184                 return ret;
185         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
186         return 0;
187 }
188
189 /**
190  * Get device MTU.
191  *
192  * @param priv
193  *   Pointer to private structure.
194  * @param[out] mtu
195  *   MTU value output buffer.
196  *
197  * @return
198  *   0 on success, negative errno value otherwise and rte_errno is set.
199  */
200 int
201 mlx4_mtu_get(struct priv *priv, uint16_t *mtu)
202 {
203         struct ifreq request;
204         int ret = mlx4_ifreq(priv, SIOCGIFMTU, &request);
205
206         if (ret)
207                 return ret;
208         *mtu = request.ifr_mtu;
209         return 0;
210 }
211
212 /**
213  * DPDK callback to change the MTU.
214  *
215  * @param priv
216  *   Pointer to Ethernet device structure.
217  * @param mtu
218  *   MTU value to set.
219  *
220  * @return
221  *   0 on success, negative errno value otherwise and rte_errno is set.
222  */
223 int
224 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
225 {
226         struct priv *priv = dev->data->dev_private;
227         struct ifreq request = { .ifr_mtu = mtu, };
228         int ret = mlx4_ifreq(priv, SIOCSIFMTU, &request);
229
230         if (ret)
231                 return ret;
232         priv->mtu = mtu;
233         return 0;
234 }
235
236 /**
237  * Set device flags.
238  *
239  * @param priv
240  *   Pointer to private structure.
241  * @param keep
242  *   Bitmask for flags that must remain untouched.
243  * @param flags
244  *   Bitmask for flags to modify.
245  *
246  * @return
247  *   0 on success, negative errno value otherwise and rte_errno is set.
248  */
249 static int
250 mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
251 {
252         struct ifreq request;
253         int ret = mlx4_ifreq(priv, SIOCGIFFLAGS, &request);
254
255         if (ret)
256                 return ret;
257         request.ifr_flags &= keep;
258         request.ifr_flags |= flags & ~keep;
259         return mlx4_ifreq(priv, SIOCSIFFLAGS, &request);
260 }
261
262 /**
263  * Change the link state (UP / DOWN).
264  *
265  * @param priv
266  *   Pointer to Ethernet device private data.
267  * @param up
268  *   Nonzero for link up, otherwise link down.
269  *
270  * @return
271  *   0 on success, negative errno value otherwise and rte_errno is set.
272  */
273 static int
274 mlx4_dev_set_link(struct priv *priv, int up)
275 {
276         int err;
277
278         if (up) {
279                 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
280                 if (err)
281                         return err;
282         } else {
283                 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
284                 if (err)
285                         return err;
286         }
287         return 0;
288 }
289
290 /**
291  * DPDK callback to bring the link DOWN.
292  *
293  * @param dev
294  *   Pointer to Ethernet device structure.
295  *
296  * @return
297  *   0 on success, negative errno value otherwise and rte_errno is set.
298  */
299 int
300 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
301 {
302         struct priv *priv = dev->data->dev_private;
303
304         return mlx4_dev_set_link(priv, 0);
305 }
306
307 /**
308  * DPDK callback to bring the link UP.
309  *
310  * @param dev
311  *   Pointer to Ethernet device structure.
312  *
313  * @return
314  *   0 on success, negative errno value otherwise and rte_errno is set.
315  */
316 int
317 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
318 {
319         struct priv *priv = dev->data->dev_private;
320
321         return mlx4_dev_set_link(priv, 1);
322 }
323
324 /**
325  * Supported Rx mode toggles.
326  *
327  * Even and odd values respectively stand for off and on.
328  */
329 enum rxmode_toggle {
330         RXMODE_TOGGLE_PROMISC_OFF,
331         RXMODE_TOGGLE_PROMISC_ON,
332         RXMODE_TOGGLE_ALLMULTI_OFF,
333         RXMODE_TOGGLE_ALLMULTI_ON,
334 };
335
336 /**
337  * Helper function to toggle promiscuous and all multicast modes.
338  *
339  * @param dev
340  *   Pointer to Ethernet device structure.
341  * @param toggle
342  *   Toggle to set.
343  */
344 static void
345 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
346 {
347         struct priv *priv = dev->data->dev_private;
348         const char *mode;
349         struct rte_flow_error error;
350
351         switch (toggle) {
352         case RXMODE_TOGGLE_PROMISC_OFF:
353         case RXMODE_TOGGLE_PROMISC_ON:
354                 mode = "promiscuous";
355                 dev->data->promiscuous = toggle & 1;
356                 break;
357         case RXMODE_TOGGLE_ALLMULTI_OFF:
358         case RXMODE_TOGGLE_ALLMULTI_ON:
359                 mode = "all multicast";
360                 dev->data->all_multicast = toggle & 1;
361                 break;
362         }
363         if (!mlx4_flow_sync(priv, &error))
364                 return;
365         ERROR("cannot toggle %s mode (code %d, \"%s\"),"
366               " flow error type %d, cause %p, message: %s",
367               mode, rte_errno, strerror(rte_errno), error.type, error.cause,
368               error.message ? error.message : "(unspecified)");
369 }
370
371 /**
372  * DPDK callback to enable promiscuous mode.
373  *
374  * @param dev
375  *   Pointer to Ethernet device structure.
376  */
377 void
378 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
379 {
380         mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
381 }
382
383 /**
384  * DPDK callback to disable promiscuous mode.
385  *
386  * @param dev
387  *   Pointer to Ethernet device structure.
388  */
389 void
390 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
391 {
392         mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
393 }
394
395 /**
396  * DPDK callback to enable all multicast mode.
397  *
398  * @param dev
399  *   Pointer to Ethernet device structure.
400  */
401 void
402 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
403 {
404         mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
405 }
406
407 /**
408  * DPDK callback to disable all multicast mode.
409  *
410  * @param dev
411  *   Pointer to Ethernet device structure.
412  */
413 void
414 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
415 {
416         mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
417 }
418
419 /**
420  * DPDK callback to remove a MAC address.
421  *
422  * @param dev
423  *   Pointer to Ethernet device structure.
424  * @param index
425  *   MAC address index.
426  */
427 void
428 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
429 {
430         struct priv *priv = dev->data->dev_private;
431         struct rte_flow_error error;
432
433         if (index >= RTE_DIM(priv->mac)) {
434                 rte_errno = EINVAL;
435                 return;
436         }
437         memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
438         if (!mlx4_flow_sync(priv, &error))
439                 return;
440         ERROR("failed to synchronize flow rules after removing MAC address"
441               " at index %d (code %d, \"%s\"),"
442               " flow error type %d, cause %p, message: %s",
443               index, rte_errno, strerror(rte_errno), error.type, error.cause,
444               error.message ? error.message : "(unspecified)");
445 }
446
447 /**
448  * DPDK callback to add a MAC address.
449  *
450  * @param dev
451  *   Pointer to Ethernet device structure.
452  * @param mac_addr
453  *   MAC address to register.
454  * @param index
455  *   MAC address index.
456  * @param vmdq
457  *   VMDq pool index to associate address with (ignored).
458  *
459  * @return
460  *   0 on success, negative errno value otherwise and rte_errno is set.
461  */
462 int
463 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
464                   uint32_t index, uint32_t vmdq)
465 {
466         struct priv *priv = dev->data->dev_private;
467         struct rte_flow_error error;
468         int ret;
469
470         (void)vmdq;
471         if (index >= RTE_DIM(priv->mac)) {
472                 rte_errno = EINVAL;
473                 return -rte_errno;
474         }
475         memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
476         ret = mlx4_flow_sync(priv, &error);
477         if (!ret)
478                 return 0;
479         ERROR("failed to synchronize flow rules after adding MAC address"
480               " at index %d (code %d, \"%s\"),"
481               " flow error type %d, cause %p, message: %s",
482               index, rte_errno, strerror(rte_errno), error.type, error.cause,
483               error.message ? error.message : "(unspecified)");
484         return ret;
485 }
486
487 /**
488  * DPDK callback to configure a VLAN filter.
489  *
490  * @param dev
491  *   Pointer to Ethernet device structure.
492  * @param vlan_id
493  *   VLAN ID to filter.
494  * @param on
495  *   Toggle filter.
496  *
497  * @return
498  *   0 on success, negative errno value otherwise and rte_errno is set.
499  */
500 int
501 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
502 {
503         struct priv *priv = dev->data->dev_private;
504         struct rte_flow_error error;
505         unsigned int vidx = vlan_id / 64;
506         unsigned int vbit = vlan_id % 64;
507         uint64_t *v;
508         int ret;
509
510         if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
511                 rte_errno = EINVAL;
512                 return -rte_errno;
513         }
514         v = &dev->data->vlan_filter_conf.ids[vidx];
515         *v &= ~(UINT64_C(1) << vbit);
516         *v |= (uint64_t)!!on << vbit;
517         ret = mlx4_flow_sync(priv, &error);
518         if (!ret)
519                 return 0;
520         ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
521               " (code %d, \"%s\"), "
522               " flow error type %d, cause %p, message: %s",
523               on ? "enabling" : "disabling", vlan_id,
524               rte_errno, strerror(rte_errno), error.type, error.cause,
525               error.message ? error.message : "(unspecified)");
526         return ret;
527 }
528
529 /**
530  * DPDK callback to set the primary MAC address.
531  *
532  * @param dev
533  *   Pointer to Ethernet device structure.
534  * @param mac_addr
535  *   MAC address to register.
536  */
537 void
538 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
539 {
540         mlx4_mac_addr_add(dev, mac_addr, 0, 0);
541 }
542
543 /**
544  * DPDK callback to get information about the device.
545  *
546  * @param dev
547  *   Pointer to Ethernet device structure.
548  * @param[out] info
549  *   Info structure output buffer.
550  */
551 void
552 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
553 {
554         struct priv *priv = dev->data->dev_private;
555         unsigned int max;
556         char ifname[IF_NAMESIZE];
557
558         info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
559         /* FIXME: we should ask the device for these values. */
560         info->min_rx_bufsize = 32;
561         info->max_rx_pktlen = 65536;
562         /*
563          * Since we need one CQ per QP, the limit is the minimum number
564          * between the two values.
565          */
566         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
567                priv->device_attr.max_qp : priv->device_attr.max_cq);
568         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
569         if (max >= 65535)
570                 max = 65535;
571         info->max_rx_queues = max;
572         info->max_tx_queues = max;
573         info->max_mac_addrs = RTE_DIM(priv->mac);
574         info->tx_offload_capa = mlx4_get_tx_port_offloads(priv);
575         info->rx_queue_offload_capa = mlx4_get_rx_queue_offloads(priv);
576         info->rx_offload_capa = (mlx4_get_rx_port_offloads(priv) |
577                                  info->rx_queue_offload_capa);
578         if (mlx4_get_ifname(priv, &ifname) == 0)
579                 info->if_index = if_nametoindex(ifname);
580         info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
581         info->speed_capa =
582                         ETH_LINK_SPEED_1G |
583                         ETH_LINK_SPEED_10G |
584                         ETH_LINK_SPEED_20G |
585                         ETH_LINK_SPEED_40G |
586                         ETH_LINK_SPEED_56G;
587 }
588
589 /**
590  * DPDK callback to get device statistics.
591  *
592  * @param dev
593  *   Pointer to Ethernet device structure.
594  * @param[out] stats
595  *   Stats structure output buffer.
596  */
597 int
598 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
599 {
600         struct rte_eth_stats tmp;
601         unsigned int i;
602         unsigned int idx;
603
604         memset(&tmp, 0, sizeof(tmp));
605         /* Add software counters. */
606         for (i = 0; i != dev->data->nb_rx_queues; ++i) {
607                 struct rxq *rxq = dev->data->rx_queues[i];
608
609                 if (rxq == NULL)
610                         continue;
611                 idx = rxq->stats.idx;
612                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
613                         tmp.q_ipackets[idx] += rxq->stats.ipackets;
614                         tmp.q_ibytes[idx] += rxq->stats.ibytes;
615                         tmp.q_errors[idx] += (rxq->stats.idropped +
616                                               rxq->stats.rx_nombuf);
617                 }
618                 tmp.ipackets += rxq->stats.ipackets;
619                 tmp.ibytes += rxq->stats.ibytes;
620                 tmp.ierrors += rxq->stats.idropped;
621                 tmp.rx_nombuf += rxq->stats.rx_nombuf;
622         }
623         for (i = 0; i != dev->data->nb_tx_queues; ++i) {
624                 struct txq *txq = dev->data->tx_queues[i];
625
626                 if (txq == NULL)
627                         continue;
628                 idx = txq->stats.idx;
629                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
630                         tmp.q_opackets[idx] += txq->stats.opackets;
631                         tmp.q_obytes[idx] += txq->stats.obytes;
632                         tmp.q_errors[idx] += txq->stats.odropped;
633                 }
634                 tmp.opackets += txq->stats.opackets;
635                 tmp.obytes += txq->stats.obytes;
636                 tmp.oerrors += txq->stats.odropped;
637         }
638         *stats = tmp;
639         return 0;
640 }
641
642 /**
643  * DPDK callback to clear device statistics.
644  *
645  * @param dev
646  *   Pointer to Ethernet device structure.
647  */
648 void
649 mlx4_stats_reset(struct rte_eth_dev *dev)
650 {
651         unsigned int i;
652
653         for (i = 0; i != dev->data->nb_rx_queues; ++i) {
654                 struct rxq *rxq = dev->data->rx_queues[i];
655
656                 if (rxq)
657                         rxq->stats = (struct mlx4_rxq_stats){
658                                 .idx = rxq->stats.idx,
659                         };
660         }
661         for (i = 0; i != dev->data->nb_tx_queues; ++i) {
662                 struct txq *txq = dev->data->tx_queues[i];
663
664                 if (txq)
665                         txq->stats = (struct mlx4_txq_stats){
666                                 .idx = txq->stats.idx,
667                         };
668         }
669 }
670
671 /**
672  * DPDK callback to retrieve physical link information.
673  *
674  * @param dev
675  *   Pointer to Ethernet device structure.
676  * @param wait_to_complete
677  *   Wait for request completion (ignored).
678  *
679  * @return
680  *   0 on success, negative errno value otherwise and rte_errno is set.
681  */
682 int
683 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
684 {
685         const struct priv *priv = dev->data->dev_private;
686         struct ethtool_cmd edata = {
687                 .cmd = ETHTOOL_GSET,
688         };
689         struct ifreq ifr;
690         struct rte_eth_link dev_link;
691         int link_speed = 0;
692
693         if (priv == NULL) {
694                 rte_errno = EINVAL;
695                 return -rte_errno;
696         }
697         (void)wait_to_complete;
698         if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
699                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
700                 return -rte_errno;
701         }
702         memset(&dev_link, 0, sizeof(dev_link));
703         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
704                                 (ifr.ifr_flags & IFF_RUNNING));
705         ifr.ifr_data = (void *)&edata;
706         if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
707                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
708                      strerror(rte_errno));
709                 return -rte_errno;
710         }
711         link_speed = ethtool_cmd_speed(&edata);
712         if (link_speed == -1)
713                 dev_link.link_speed = 0;
714         else
715                 dev_link.link_speed = link_speed;
716         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
717                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
718         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
719                                   ETH_LINK_SPEED_FIXED);
720         dev->data->dev_link = dev_link;
721         return 0;
722 }
723
724 /**
725  * DPDK callback to get flow control status.
726  *
727  * @param dev
728  *   Pointer to Ethernet device structure.
729  * @param[out] fc_conf
730  *   Flow control output buffer.
731  *
732  * @return
733  *   0 on success, negative errno value otherwise and rte_errno is set.
734  */
735 int
736 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
737 {
738         struct priv *priv = dev->data->dev_private;
739         struct ifreq ifr;
740         struct ethtool_pauseparam ethpause = {
741                 .cmd = ETHTOOL_GPAUSEPARAM,
742         };
743         int ret;
744
745         ifr.ifr_data = (void *)&ethpause;
746         if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
747                 ret = rte_errno;
748                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
749                      " failed: %s",
750                      strerror(rte_errno));
751                 goto out;
752         }
753         fc_conf->autoneg = ethpause.autoneg;
754         if (ethpause.rx_pause && ethpause.tx_pause)
755                 fc_conf->mode = RTE_FC_FULL;
756         else if (ethpause.rx_pause)
757                 fc_conf->mode = RTE_FC_RX_PAUSE;
758         else if (ethpause.tx_pause)
759                 fc_conf->mode = RTE_FC_TX_PAUSE;
760         else
761                 fc_conf->mode = RTE_FC_NONE;
762         ret = 0;
763 out:
764         assert(ret >= 0);
765         return -ret;
766 }
767
768 /**
769  * DPDK callback to modify flow control parameters.
770  *
771  * @param dev
772  *   Pointer to Ethernet device structure.
773  * @param[in] fc_conf
774  *   Flow control parameters.
775  *
776  * @return
777  *   0 on success, negative errno value otherwise and rte_errno is set.
778  */
779 int
780 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
781 {
782         struct priv *priv = dev->data->dev_private;
783         struct ifreq ifr;
784         struct ethtool_pauseparam ethpause = {
785                 .cmd = ETHTOOL_SPAUSEPARAM,
786         };
787         int ret;
788
789         ifr.ifr_data = (void *)&ethpause;
790         ethpause.autoneg = fc_conf->autoneg;
791         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
792             (fc_conf->mode & RTE_FC_RX_PAUSE))
793                 ethpause.rx_pause = 1;
794         else
795                 ethpause.rx_pause = 0;
796         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
797             (fc_conf->mode & RTE_FC_TX_PAUSE))
798                 ethpause.tx_pause = 1;
799         else
800                 ethpause.tx_pause = 0;
801         if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
802                 ret = rte_errno;
803                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
804                      " failed: %s",
805                      strerror(rte_errno));
806                 goto out;
807         }
808         ret = 0;
809 out:
810         assert(ret >= 0);
811         return -ret;
812 }
813
814 /**
815  * DPDK callback to retrieve the received packet types that are recognized
816  * by the device.
817  *
818  * @param dev
819  *   Pointer to Ethernet device structure.
820  *
821  * @return
822  *   Pointer to an array of recognized packet types if in Rx burst mode,
823  *   NULL otherwise.
824  */
825 const uint32_t *
826 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
827 {
828         static const uint32_t ptypes[] = {
829                 /* refers to rxq_cq_to_pkt_type() */
830                 RTE_PTYPE_L2_ETHER,
831                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
832                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
833                 RTE_PTYPE_L4_FRAG,
834                 RTE_PTYPE_L4_TCP,
835                 RTE_PTYPE_L4_UDP,
836                 RTE_PTYPE_UNKNOWN
837         };
838         static const uint32_t ptypes_l2tun[] = {
839                 /* refers to rxq_cq_to_pkt_type() */
840                 RTE_PTYPE_L2_ETHER,
841                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
842                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
843                 RTE_PTYPE_L4_FRAG,
844                 RTE_PTYPE_L4_TCP,
845                 RTE_PTYPE_L4_UDP,
846                 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
847                 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
848                 RTE_PTYPE_UNKNOWN
849         };
850         struct priv *priv = dev->data->dev_private;
851
852         if (dev->rx_pkt_burst == mlx4_rx_burst) {
853                 if (priv->hw_csum_l2tun)
854                         return ptypes_l2tun;
855                 else
856                         return ptypes;
857         }
858         return NULL;
859 }
860
861 /**
862  * Check if mlx4 device was removed.
863  *
864  * @param dev
865  *   Pointer to Ethernet device structure.
866  *
867  * @return
868  *   1 when device is removed, otherwise 0.
869  */
870 int
871 mlx4_is_removed(struct rte_eth_dev *dev)
872 {
873         struct ibv_device_attr device_attr;
874         struct priv *priv = dev->data->dev_private;
875
876         if (mlx4_glue->query_device(priv->ctx, &device_attr) == EIO)
877                 return 1;
878         return 0;
879 }