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