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