184ae2f4e69f830990e5f646f0d8e08a7b2ae5c4
[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, negative errno value on failure.
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         int ret = 0;
48
49         DEBUG("%p: %s VLAN filter ID %" PRIu16,
50               (void *)dev, (on ? "enable" : "disable"), vlan_id);
51         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
52         for (i = 0; (i != priv->vlan_filter_n); ++i)
53                 if (priv->vlan_filter[i] == vlan_id)
54                         break;
55         /* Check if there's room for another VLAN filter. */
56         if (i == RTE_DIM(priv->vlan_filter)) {
57                 ret = -ENOMEM;
58                 goto out;
59         }
60         if (i < priv->vlan_filter_n) {
61                 assert(priv->vlan_filter_n != 0);
62                 /* Enabling an existing VLAN filter has no effect. */
63                 if (on)
64                         goto out;
65                 /* Remove VLAN filter from list. */
66                 --priv->vlan_filter_n;
67                 memmove(&priv->vlan_filter[i],
68                         &priv->vlan_filter[i + 1],
69                         sizeof(priv->vlan_filter[i]) *
70                         (priv->vlan_filter_n - i));
71                 priv->vlan_filter[priv->vlan_filter_n] = 0;
72         } else {
73                 assert(i == priv->vlan_filter_n);
74                 /* Disabling an unknown VLAN filter has no effect. */
75                 if (!on)
76                         goto out;
77                 /* Add new VLAN filter. */
78                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
79                 ++priv->vlan_filter_n;
80         }
81         if (dev->data->dev_started)
82                 priv_dev_traffic_restart(priv, dev);
83 out:
84         return ret;
85 }
86
87 /**
88  * Set/reset VLAN stripping for a specific queue.
89  *
90  * @param priv
91  *   Pointer to private structure.
92  * @param idx
93  *   RX queue index.
94  * @param on
95  *   Enable/disable VLAN stripping.
96  */
97 static void
98 priv_vlan_strip_queue_set(struct priv *priv, uint16_t idx, int on)
99 {
100         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
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 err;
108
109         DEBUG("set VLAN offloads 0x%x for port %d queue %d",
110               vlan_offloads, rxq->port_id, idx);
111         if (!rxq_ctrl->ibv) {
112                 /* Update related bits in RX queue. */
113                 rxq->vlan_strip = !!on;
114                 return;
115         }
116         mod = (struct ibv_wq_attr){
117                 .attr_mask = IBV_WQ_ATTR_FLAGS,
118                 .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
119                 .flags = vlan_offloads,
120         };
121         err = mlx5_glue->modify_wq(rxq_ctrl->ibv->wq, &mod);
122         if (err) {
123                 ERROR("%p: failed to modified stripping mode: %s",
124                       (void *)priv, strerror(err));
125                 return;
126         }
127         /* Update related bits in RX queue. */
128         rxq->vlan_strip = !!on;
129 }
130
131 /**
132  * Callback to set/reset VLAN stripping for a specific queue.
133  *
134  * @param dev
135  *   Pointer to Ethernet device structure.
136  * @param queue
137  *   RX queue index.
138  * @param on
139  *   Enable/disable VLAN stripping.
140  */
141 void
142 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
143 {
144         struct priv *priv = dev->data->dev_private;
145
146         /* Validate hw support */
147         if (!priv->config.hw_vlan_strip) {
148                 ERROR("VLAN stripping is not supported");
149                 return;
150         }
151         /* Validate queue number */
152         if (queue >= priv->rxqs_n) {
153                 ERROR("VLAN stripping, invalid queue number %d", queue);
154                 return;
155         }
156         priv_vlan_strip_queue_set(priv, queue, on);
157 }
158
159 /**
160  * Callback to set/reset VLAN offloads for a port.
161  *
162  * @param dev
163  *   Pointer to Ethernet device structure.
164  * @param mask
165  *   VLAN offload bit mask.
166  */
167 int
168 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
169 {
170         struct priv *priv = dev->data->dev_private;
171         unsigned int i;
172
173         if (mask & ETH_VLAN_STRIP_MASK) {
174                 int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
175                                        DEV_RX_OFFLOAD_VLAN_STRIP);
176
177                 if (!priv->config.hw_vlan_strip) {
178                         ERROR("VLAN stripping is not supported");
179                         return 0;
180                 }
181                 /* Run on every RX queue and set/reset VLAN stripping. */
182                 for (i = 0; (i != priv->rxqs_n); i++)
183                         priv_vlan_strip_queue_set(priv, i, hw_vlan_strip);
184         }
185         return 0;
186 }