net/mlx5: use SPDX tags in 6WIND copyrighted files
[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
124         err = mlx5_glue->modify_wq(rxq_ctrl->ibv->wq, &mod);
125         if (err) {
126                 ERROR("%p: failed to modified stripping mode: %s",
127                       (void *)priv, strerror(err));
128                 return;
129         }
130
131         /* Update related bits in RX queue. */
132         rxq->vlan_strip = !!on;
133 }
134
135 /**
136  * Callback to set/reset VLAN stripping for a specific queue.
137  *
138  * @param dev
139  *   Pointer to Ethernet device structure.
140  * @param queue
141  *   RX queue index.
142  * @param on
143  *   Enable/disable VLAN stripping.
144  */
145 void
146 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
147 {
148         struct priv *priv = dev->data->dev_private;
149
150         /* Validate hw support */
151         if (!priv->config.hw_vlan_strip) {
152                 ERROR("VLAN stripping is not supported");
153                 return;
154         }
155
156         /* Validate queue number */
157         if (queue >= priv->rxqs_n) {
158                 ERROR("VLAN stripping, invalid queue number %d", queue);
159                 return;
160         }
161
162         priv_lock(priv);
163         priv_vlan_strip_queue_set(priv, queue, on);
164         priv_unlock(priv);
165 }
166
167 /**
168  * Callback to set/reset VLAN offloads for a port.
169  *
170  * @param dev
171  *   Pointer to Ethernet device structure.
172  * @param mask
173  *   VLAN offload bit mask.
174  */
175 int
176 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
177 {
178         struct priv *priv = dev->data->dev_private;
179         unsigned int i;
180
181         if (mask & ETH_VLAN_STRIP_MASK) {
182                 int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
183                                        DEV_RX_OFFLOAD_VLAN_STRIP);
184
185                 if (!priv->config.hw_vlan_strip) {
186                         ERROR("VLAN stripping is not supported");
187                         return 0;
188                 }
189
190                 /* Run on every RX queue and set/reset VLAN stripping. */
191                 priv_lock(priv);
192                 for (i = 0; (i != priv->rxqs_n); i++)
193                         priv_vlan_strip_queue_set(priv, i, hw_vlan_strip);
194                 priv_unlock(priv);
195         }
196
197         return 0;
198 }