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/*
40 * Not needed by this file; included to work around the lack of off_t
41 * definition for mlx5dv.h with unpatched rdma-core versions.
42 */
43#include <sys/types.h>
44
45/* DPDK headers don't like -pedantic. */
46#ifdef PEDANTIC
47#pragma GCC diagnostic ignored "-Wpedantic"
48#endif
49#include <rte_ethdev.h>
50#include <rte_common.h>
51#ifdef PEDANTIC
52#pragma GCC diagnostic error "-Wpedantic"
53#endif
54
55#include "mlx5_utils.h"
56#include "mlx5.h"
57#include "mlx5_autoconf.h"
58
59/**
60 * Configure a VLAN filter.
61 *
62 * @param dev
63 *   Pointer to Ethernet device structure.
64 * @param vlan_id
65 *   VLAN ID to filter.
66 * @param on
67 *   Toggle filter.
68 *
69 * @return
70 *   0 on success, errno value on failure.
71 */
72static int
73vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
74{
75	struct priv *priv = dev->data->dev_private;
76	unsigned int i;
77
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		return ENOMEM;
87	if (i < priv->vlan_filter_n) {
88		assert(priv->vlan_filter_n != 0);
89		/* Enabling an existing VLAN filter has no effect. */
90		if (on)
91			return 0;
92		/* Remove VLAN filter from list. */
93		--priv->vlan_filter_n;
94		memmove(&priv->vlan_filter[i],
95			&priv->vlan_filter[i + 1],
96			sizeof(priv->vlan_filter[i]) *
97			(priv->vlan_filter_n - i));
98		priv->vlan_filter[priv->vlan_filter_n] = 0;
99	} else {
100		assert(i == priv->vlan_filter_n);
101		/* Disabling an unknown VLAN filter has no effect. */
102		if (!on)
103			return 0;
104		/* Add new VLAN filter. */
105		priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
106		++priv->vlan_filter_n;
107	}
108	/* Rehash flows in all hash RX queues. */
109	priv_mac_addrs_disable(priv);
110	priv_special_flow_disable_all(priv);
111	return priv_rehash_flows(priv);
112}
113
114/**
115 * DPDK callback to configure a VLAN filter.
116 *
117 * @param dev
118 *   Pointer to Ethernet device structure.
119 * @param vlan_id
120 *   VLAN ID to filter.
121 * @param on
122 *   Toggle filter.
123 *
124 * @return
125 *   0 on success, negative errno value on failure.
126 */
127int
128mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
129{
130	struct priv *priv = dev->data->dev_private;
131	int ret;
132
133	priv_lock(priv);
134	ret = vlan_filter_set(dev, vlan_id, on);
135	priv_unlock(priv);
136	assert(ret >= 0);
137	return -ret;
138}
139
140/**
141 * Set/reset VLAN stripping for a specific queue.
142 *
143 * @param priv
144 *   Pointer to private structure.
145 * @param idx
146 *   RX queue index.
147 * @param on
148 *   Enable/disable VLAN stripping.
149 */
150static void
151priv_vlan_strip_queue_set(struct priv *priv, uint16_t idx, int on)
152{
153	struct rxq *rxq = (*priv->rxqs)[idx];
154	struct rxq_ctrl *rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
155	struct ibv_exp_wq_attr mod;
156	uint16_t vlan_offloads =
157		(on ? IBV_EXP_RECEIVE_WQ_CVLAN_STRIP : 0) |
158		0;
159	int err;
160
161	DEBUG("set VLAN offloads 0x%x for port %d queue %d",
162	      vlan_offloads, rxq->port_id, idx);
163	mod = (struct ibv_exp_wq_attr){
164		.attr_mask = IBV_EXP_WQ_ATTR_VLAN_OFFLOADS,
165		.vlan_offloads = vlan_offloads,
166	};
167
168	err = ibv_exp_modify_wq(rxq_ctrl->wq, &mod);
169	if (err) {
170		ERROR("%p: failed to modified stripping mode: %s",
171		      (void *)priv, strerror(err));
172		return;
173	}
174
175	/* Update related bits in RX queue. */
176	rxq->vlan_strip = !!on;
177}
178
179/**
180 * Callback to set/reset VLAN stripping for a specific queue.
181 *
182 * @param dev
183 *   Pointer to Ethernet device structure.
184 * @param queue
185 *   RX queue index.
186 * @param on
187 *   Enable/disable VLAN stripping.
188 */
189void
190mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
191{
192	struct priv *priv = dev->data->dev_private;
193
194	/* Validate hw support */
195	if (!priv->hw_vlan_strip) {
196		ERROR("VLAN stripping is not supported");
197		return;
198	}
199
200	/* Validate queue number */
201	if (queue >= priv->rxqs_n) {
202		ERROR("VLAN stripping, invalid queue number %d", queue);
203		return;
204	}
205
206	priv_lock(priv);
207	priv_vlan_strip_queue_set(priv, queue, on);
208	priv_unlock(priv);
209}
210
211/**
212 * Callback to set/reset VLAN offloads for a port.
213 *
214 * @param dev
215 *   Pointer to Ethernet device structure.
216 * @param mask
217 *   VLAN offload bit mask.
218 */
219void
220mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
221{
222	struct priv *priv = dev->data->dev_private;
223	unsigned int i;
224
225	if (mask & ETH_VLAN_STRIP_MASK) {
226		int hw_vlan_strip = !!dev->data->dev_conf.rxmode.hw_vlan_strip;
227
228		if (!priv->hw_vlan_strip) {
229			ERROR("VLAN stripping is not supported");
230			return;
231		}
232
233		/* Run on every RX queue and set/reset VLAN stripping. */
234		priv_lock(priv);
235		for (i = 0; (i != priv->rxqs_n); i++)
236			priv_vlan_strip_queue_set(priv, i, hw_vlan_strip);
237		priv_unlock(priv);
238	}
239}
240