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