3df962a9046e0165b28c184d4e88c9f92754daa8
[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         DEBUG("%p: %s VLAN filter ID %" PRIu16,
50               (void *)dev, (on ? "enable" : "disable"), vlan_id);
51         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
52         for (i = 0; (i != priv->vlan_filter_n); ++i)
53                 if (priv->vlan_filter[i] == vlan_id)
54                         break;
55         /* Check if there's room for another VLAN filter. */
56         if (i == RTE_DIM(priv->vlan_filter)) {
57                 ret = -ENOMEM;
58                 goto out;
59         }
60         if (i < priv->vlan_filter_n) {
61                 assert(priv->vlan_filter_n != 0);
62                 /* Enabling an existing VLAN filter has no effect. */
63                 if (on)
64                         goto out;
65                 /* Remove VLAN filter from list. */
66                 --priv->vlan_filter_n;
67                 memmove(&priv->vlan_filter[i],
68                         &priv->vlan_filter[i + 1],
69                         sizeof(priv->vlan_filter[i]) *
70                         (priv->vlan_filter_n - i));
71                 priv->vlan_filter[priv->vlan_filter_n] = 0;
72         } else {
73                 assert(i == priv->vlan_filter_n);
74                 /* Disabling an unknown VLAN filter has no effect. */
75                 if (!on)
76                         goto out;
77                 /* Add new VLAN filter. */
78                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
79                 ++priv->vlan_filter_n;
80         }
81         if (dev->data->dev_started)
82                 mlx5_traffic_restart(dev);
83 out:
84         return ret;
85 }
86
87 /**
88  * Callback to set/reset VLAN stripping for a specific queue.
89  *
90  * @param dev
91  *   Pointer to Ethernet device structure.
92  * @param queue
93  *   RX queue index.
94  * @param on
95  *   Enable/disable VLAN stripping.
96  */
97 void
98 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
99 {
100         struct priv *priv = dev->data->dev_private;
101         struct mlx5_rxq_data *rxq = (*priv->rxqs)[queue];
102         struct mlx5_rxq_ctrl *rxq_ctrl =
103                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
104         struct ibv_wq_attr mod;
105         uint16_t vlan_offloads =
106                 (on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
107                 0;
108         int err;
109
110         /* Validate hw support */
111         if (!priv->config.hw_vlan_strip) {
112                 ERROR("VLAN stripping is not supported");
113                 return;
114         }
115         /* Validate queue number */
116         if (queue >= priv->rxqs_n) {
117                 ERROR("VLAN stripping, invalid queue number %d", queue);
118                 return;
119         }
120         DEBUG("set VLAN offloads 0x%x for port %d queue %d",
121               vlan_offloads, rxq->port_id, queue);
122         if (!rxq_ctrl->ibv) {
123                 /* Update related bits in RX queue. */
124                 rxq->vlan_strip = !!on;
125                 return;
126         }
127         mod = (struct ibv_wq_attr){
128                 .attr_mask = IBV_WQ_ATTR_FLAGS,
129                 .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
130                 .flags = vlan_offloads,
131         };
132         err = mlx5_glue->modify_wq(rxq_ctrl->ibv->wq, &mod);
133         if (err) {
134                 ERROR("%p: failed to modified stripping mode: %s",
135                       (void *)dev, strerror(err));
136                 return;
137         }
138         /* Update related bits in RX queue. */
139         rxq->vlan_strip = !!on;
140 }
141
142 /**
143  * Callback to set/reset VLAN offloads for a port.
144  *
145  * @param dev
146  *   Pointer to Ethernet device structure.
147  * @param mask
148  *   VLAN offload bit mask.
149  */
150 int
151 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
152 {
153         struct priv *priv = dev->data->dev_private;
154         unsigned int i;
155
156         if (mask & ETH_VLAN_STRIP_MASK) {
157                 int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
158                                        DEV_RX_OFFLOAD_VLAN_STRIP);
159
160                 if (!priv->config.hw_vlan_strip) {
161                         ERROR("VLAN stripping is not supported");
162                         return 0;
163                 }
164                 /* Run on every RX queue and set/reset VLAN stripping. */
165                 for (i = 0; (i != priv->rxqs_n); i++)
166                         mlx5_vlan_strip_queue_set(dev, i, hw_vlan_strip);
167         }
168         return 0;
169 }