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