3246c0a3872dc113ca91c6d903c85b374c75e295
[dpdk.git] / drivers / net / mlx5 / mlx5_vlan.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox.
4  */
5
6 #include <stddef.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <stdint.h>
10
11 /* Verbs headers do not support -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/mlx5dv.h>
16 #include <infiniband/verbs.h>
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic error "-Wpedantic"
19 #endif
20
21 #include <rte_ethdev_driver.h>
22 #include <rte_common.h>
23
24 #include "mlx5_utils.h"
25 #include "mlx5.h"
26 #include "mlx5_autoconf.h"
27 #include "mlx5_glue.h"
28
29 /**
30  * DPDK callback to configure a VLAN filter.
31  *
32  * @param dev
33  *   Pointer to Ethernet device structure.
34  * @param vlan_id
35  *   VLAN ID to filter.
36  * @param on
37  *   Toggle filter.
38  *
39  * @return
40  *   0 on success, a negative errno value otherwise and rte_errno is set.
41  */
42 int
43 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
44 {
45         struct priv *priv = dev->data->dev_private;
46         unsigned int i;
47
48         DEBUG("%p: %s VLAN filter ID %" PRIu16,
49               (void *)dev, (on ? "enable" : "disable"), vlan_id);
50         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
51         for (i = 0; (i != priv->vlan_filter_n); ++i)
52                 if (priv->vlan_filter[i] == vlan_id)
53                         break;
54         /* Check if there's room for another VLAN filter. */
55         if (i == RTE_DIM(priv->vlan_filter)) {
56                 rte_errno = ENOMEM;
57                 return -rte_errno;
58         }
59         if (i < priv->vlan_filter_n) {
60                 assert(priv->vlan_filter_n != 0);
61                 /* Enabling an existing VLAN filter has no effect. */
62                 if (on)
63                         goto out;
64                 /* Remove VLAN filter from list. */
65                 --priv->vlan_filter_n;
66                 memmove(&priv->vlan_filter[i],
67                         &priv->vlan_filter[i + 1],
68                         sizeof(priv->vlan_filter[i]) *
69                         (priv->vlan_filter_n - i));
70                 priv->vlan_filter[priv->vlan_filter_n] = 0;
71         } else {
72                 assert(i == priv->vlan_filter_n);
73                 /* Disabling an unknown VLAN filter has no effect. */
74                 if (!on)
75                         goto out;
76                 /* Add new VLAN filter. */
77                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
78                 ++priv->vlan_filter_n;
79         }
80 out:
81         if (dev->data->dev_started)
82                 return mlx5_traffic_restart(dev);
83         return 0;
84 }
85
86 /**
87  * Callback to set/reset VLAN stripping for a specific queue.
88  *
89  * @param dev
90  *   Pointer to Ethernet device structure.
91  * @param queue
92  *   RX queue index.
93  * @param on
94  *   Enable/disable VLAN stripping.
95  */
96 void
97 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
98 {
99         struct priv *priv = dev->data->dev_private;
100         struct mlx5_rxq_data *rxq = (*priv->rxqs)[queue];
101         struct mlx5_rxq_ctrl *rxq_ctrl =
102                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
103         struct ibv_wq_attr mod;
104         uint16_t vlan_offloads =
105                 (on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
106                 0;
107         int ret;
108
109         /* Validate hw support */
110         if (!priv->config.hw_vlan_strip) {
111                 ERROR("VLAN stripping is not supported");
112                 return;
113         }
114         /* Validate queue number */
115         if (queue >= priv->rxqs_n) {
116                 ERROR("VLAN stripping, invalid queue number %d", queue);
117                 return;
118         }
119         DEBUG("set VLAN offloads 0x%x for port %d queue %d",
120               vlan_offloads, rxq->port_id, queue);
121         if (!rxq_ctrl->ibv) {
122                 /* Update related bits in RX queue. */
123                 rxq->vlan_strip = !!on;
124                 return;
125         }
126         mod = (struct ibv_wq_attr){
127                 .attr_mask = IBV_WQ_ATTR_FLAGS,
128                 .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
129                 .flags = vlan_offloads,
130         };
131         ret = mlx5_glue->modify_wq(rxq_ctrl->ibv->wq, &mod);
132         if (ret) {
133                 ERROR("%p: failed to modified stripping mode: %s",
134                       (void *)dev, strerror(rte_errno));
135                 return;
136         }
137         /* Update related bits in RX queue. */
138         rxq->vlan_strip = !!on;
139 }
140
141 /**
142  * Callback to set/reset VLAN offloads for a port.
143  *
144  * @param dev
145  *   Pointer to Ethernet device structure.
146  * @param mask
147  *   VLAN offload bit mask.
148  *
149  * @return
150  *   0 on success, a negative errno value otherwise and rte_errno is set.
151  */
152 int
153 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
154 {
155         struct priv *priv = dev->data->dev_private;
156         unsigned int i;
157
158         if (mask & ETH_VLAN_STRIP_MASK) {
159                 int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
160                                        DEV_RX_OFFLOAD_VLAN_STRIP);
161
162                 if (!priv->config.hw_vlan_strip) {
163                         ERROR("VLAN stripping is not supported");
164                         return 0;
165                 }
166                 /* Run on every RX queue and set/reset VLAN stripping. */
167                 for (i = 0; (i != priv->rxqs_n); i++)
168                         mlx5_vlan_strip_queue_set(dev, i, hw_vlan_strip);
169         }
170         return 0;
171 }