f55e5e02096d3219e6830ad048c7f6a8df8a877e
[dpdk.git] / drivers / net / mlx5 / mlx5_vlan.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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 #include <stddef.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <stdint.h>
38
39 /* Verbs headers do not support -pedantic. */
40 #ifdef PEDANTIC
41 #pragma GCC diagnostic ignored "-Wpedantic"
42 #endif
43 #include <infiniband/mlx5dv.h>
44 #include <infiniband/verbs.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-Wpedantic"
47 #endif
48
49 #include <rte_ethdev_driver.h>
50 #include <rte_common.h>
51
52 #include "mlx5_utils.h"
53 #include "mlx5.h"
54 #include "mlx5_autoconf.h"
55 #include "mlx5_glue.h"
56
57 /**
58  * DPDK callback to configure a VLAN filter.
59  *
60  * @param dev
61  *   Pointer to Ethernet device structure.
62  * @param vlan_id
63  *   VLAN ID to filter.
64  * @param on
65  *   Toggle filter.
66  *
67  * @return
68  *   0 on success, negative errno value on failure.
69  */
70 int
71 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
72 {
73         struct priv *priv = dev->data->dev_private;
74         unsigned int i;
75         int ret = 0;
76
77         priv_lock(priv);
78         DEBUG("%p: %s VLAN filter ID %" PRIu16,
79               (void *)dev, (on ? "enable" : "disable"), vlan_id);
80         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
81         for (i = 0; (i != priv->vlan_filter_n); ++i)
82                 if (priv->vlan_filter[i] == vlan_id)
83                         break;
84         /* Check if there's room for another VLAN filter. */
85         if (i == RTE_DIM(priv->vlan_filter)) {
86                 ret = -ENOMEM;
87                 goto out;
88         }
89         if (i < priv->vlan_filter_n) {
90                 assert(priv->vlan_filter_n != 0);
91                 /* Enabling an existing VLAN filter has no effect. */
92                 if (on)
93                         goto out;
94                 /* Remove VLAN filter from list. */
95                 --priv->vlan_filter_n;
96                 memmove(&priv->vlan_filter[i],
97                         &priv->vlan_filter[i + 1],
98                         sizeof(priv->vlan_filter[i]) *
99                         (priv->vlan_filter_n - i));
100                 priv->vlan_filter[priv->vlan_filter_n] = 0;
101         } else {
102                 assert(i == priv->vlan_filter_n);
103                 /* Disabling an unknown VLAN filter has no effect. */
104                 if (!on)
105                         goto out;
106                 /* Add new VLAN filter. */
107                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
108                 ++priv->vlan_filter_n;
109         }
110         if (dev->data->dev_started)
111                 priv_dev_traffic_restart(priv, dev);
112 out:
113         priv_unlock(priv);
114         return ret;
115 }
116
117 /**
118  * Set/reset VLAN stripping for a specific queue.
119  *
120  * @param priv
121  *   Pointer to private structure.
122  * @param idx
123  *   RX queue index.
124  * @param on
125  *   Enable/disable VLAN stripping.
126  */
127 static void
128 priv_vlan_strip_queue_set(struct priv *priv, uint16_t idx, int on)
129 {
130         struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
131         struct mlx5_rxq_ctrl *rxq_ctrl =
132                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
133         struct ibv_wq_attr mod;
134         uint16_t vlan_offloads =
135                 (on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
136                 0;
137         int err;
138
139         DEBUG("set VLAN offloads 0x%x for port %d queue %d",
140               vlan_offloads, rxq->port_id, idx);
141         if (!rxq_ctrl->ibv) {
142                 /* Update related bits in RX queue. */
143                 rxq->vlan_strip = !!on;
144                 return;
145         }
146         mod = (struct ibv_wq_attr){
147                 .attr_mask = IBV_WQ_ATTR_FLAGS,
148                 .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
149                 .flags = vlan_offloads,
150         };
151
152         err = mlx5_glue->modify_wq(rxq_ctrl->ibv->wq, &mod);
153         if (err) {
154                 ERROR("%p: failed to modified stripping mode: %s",
155                       (void *)priv, strerror(err));
156                 return;
157         }
158
159         /* Update related bits in RX queue. */
160         rxq->vlan_strip = !!on;
161 }
162
163 /**
164  * Callback to set/reset VLAN stripping for a specific queue.
165  *
166  * @param dev
167  *   Pointer to Ethernet device structure.
168  * @param queue
169  *   RX queue index.
170  * @param on
171  *   Enable/disable VLAN stripping.
172  */
173 void
174 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
175 {
176         struct priv *priv = dev->data->dev_private;
177
178         /* Validate hw support */
179         if (!priv->config.hw_vlan_strip) {
180                 ERROR("VLAN stripping is not supported");
181                 return;
182         }
183
184         /* Validate queue number */
185         if (queue >= priv->rxqs_n) {
186                 ERROR("VLAN stripping, invalid queue number %d", queue);
187                 return;
188         }
189
190         priv_lock(priv);
191         priv_vlan_strip_queue_set(priv, queue, on);
192         priv_unlock(priv);
193 }
194
195 /**
196  * Callback to set/reset VLAN offloads for a port.
197  *
198  * @param dev
199  *   Pointer to Ethernet device structure.
200  * @param mask
201  *   VLAN offload bit mask.
202  */
203 int
204 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
205 {
206         struct priv *priv = dev->data->dev_private;
207         unsigned int i;
208
209         if (mask & ETH_VLAN_STRIP_MASK) {
210                 int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
211                                        DEV_RX_OFFLOAD_VLAN_STRIP);
212
213                 if (!priv->config.hw_vlan_strip) {
214                         ERROR("VLAN stripping is not supported");
215                         return 0;
216                 }
217
218                 /* Run on every RX queue and set/reset VLAN stripping. */
219                 priv_lock(priv);
220                 for (i = 0; (i != priv->rxqs_n); i++)
221                         priv_vlan_strip_queue_set(priv, i, hw_vlan_strip);
222                 priv_unlock(priv);
223         }
224
225         return 0;
226 }