feac0f150fdb75f814a3daf42084d062c687c7ac
[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 Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <stdint.h>
10
11 /*
12  * Not needed by this file; included to work around the lack of off_t
13  * definition for mlx5dv.h with unpatched rdma-core versions.
14  */
15 #include <sys/types.h>
16
17 /* Verbs headers do not support -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/mlx5dv.h>
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29
30 #include "mlx5.h"
31 #include "mlx5_autoconf.h"
32 #include "mlx5_glue.h"
33 #include "mlx5_devx_cmds.h"
34 #include "mlx5_rxtx.h"
35 #include "mlx5_utils.h"
36
37 /**
38  * DPDK callback to configure a VLAN filter.
39  *
40  * @param dev
41  *   Pointer to Ethernet device structure.
42  * @param vlan_id
43  *   VLAN ID to filter.
44  * @param on
45  *   Toggle filter.
46  *
47  * @return
48  *   0 on success, a negative errno value otherwise and rte_errno is set.
49  */
50 int
51 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
52 {
53         struct mlx5_priv *priv = dev->data->dev_private;
54         unsigned int i;
55
56         DRV_LOG(DEBUG, "port %u %s VLAN filter ID %" PRIu16,
57                 dev->data->port_id, (on ? "enable" : "disable"), vlan_id);
58         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
59         for (i = 0; (i != priv->vlan_filter_n); ++i)
60                 if (priv->vlan_filter[i] == vlan_id)
61                         break;
62         /* Check if there's room for another VLAN filter. */
63         if (i == RTE_DIM(priv->vlan_filter)) {
64                 rte_errno = ENOMEM;
65                 return -rte_errno;
66         }
67         if (i < priv->vlan_filter_n) {
68                 assert(priv->vlan_filter_n != 0);
69                 /* Enabling an existing VLAN filter has no effect. */
70                 if (on)
71                         goto out;
72                 /* Remove VLAN filter from list. */
73                 --priv->vlan_filter_n;
74                 memmove(&priv->vlan_filter[i],
75                         &priv->vlan_filter[i + 1],
76                         sizeof(priv->vlan_filter[i]) *
77                         (priv->vlan_filter_n - i));
78                 priv->vlan_filter[priv->vlan_filter_n] = 0;
79         } else {
80                 assert(i == priv->vlan_filter_n);
81                 /* Disabling an unknown VLAN filter has no effect. */
82                 if (!on)
83                         goto out;
84                 /* Add new VLAN filter. */
85                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
86                 ++priv->vlan_filter_n;
87         }
88 out:
89         if (dev->data->dev_started)
90                 return mlx5_traffic_restart(dev);
91         return 0;
92 }
93
94 /**
95  * Callback to set/reset VLAN stripping for a specific queue.
96  *
97  * @param dev
98  *   Pointer to Ethernet device structure.
99  * @param queue
100  *   RX queue index.
101  * @param on
102  *   Enable/disable VLAN stripping.
103  */
104 void
105 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
106 {
107         struct mlx5_priv *priv = dev->data->dev_private;
108         struct mlx5_rxq_data *rxq = (*priv->rxqs)[queue];
109         struct mlx5_rxq_ctrl *rxq_ctrl =
110                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
111         struct ibv_wq_attr mod;
112         uint16_t vlan_offloads =
113                 (on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
114                 0;
115         int ret = 0;
116
117         /* Validate hw support */
118         if (!priv->config.hw_vlan_strip) {
119                 DRV_LOG(ERR, "port %u VLAN stripping is not supported",
120                         dev->data->port_id);
121                 return;
122         }
123         /* Validate queue number */
124         if (queue >= priv->rxqs_n) {
125                 DRV_LOG(ERR, "port %u VLAN stripping, invalid queue number %d",
126                         dev->data->port_id, queue);
127                 return;
128         }
129         DRV_LOG(DEBUG, "port %u set VLAN offloads 0x%x for port %uqueue %d",
130                 dev->data->port_id, vlan_offloads, rxq->port_id, queue);
131         if (!rxq_ctrl->obj) {
132                 /* Update related bits in RX queue. */
133                 rxq->vlan_strip = !!on;
134                 return;
135         }
136         if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
137                 mod = (struct ibv_wq_attr){
138                         .attr_mask = IBV_WQ_ATTR_FLAGS,
139                         .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
140                         .flags = vlan_offloads,
141                 };
142                 ret = mlx5_glue->modify_wq(rxq_ctrl->obj->wq, &mod);
143         } else if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
144                 struct mlx5_devx_modify_rq_attr rq_attr;
145
146                 memset(&rq_attr, 0, sizeof(rq_attr));
147                 rq_attr.rq_state = MLX5_RQC_STATE_RDY;
148                 rq_attr.state = MLX5_RQC_STATE_RDY;
149                 rq_attr.vsd = (on ? 0 : 1);
150                 rq_attr.modify_bitmask = MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD;
151                 ret = mlx5_devx_cmd_modify_rq(rxq_ctrl->obj->rq, &rq_attr);
152         }
153         if (ret) {
154                 DRV_LOG(ERR, "port %u failed to modify object %d stripping "
155                         "mode: %s", dev->data->port_id,
156                         rxq_ctrl->obj->type, strerror(rte_errno));
157                 return;
158         }
159         /* Update related bits in RX queue. */
160         rxq->vlan_strip = !!on;
161 }
162
163 /**
164  * Callback to set/reset VLAN offloads for a port.
165  *
166  * @param dev
167  *   Pointer to Ethernet device structure.
168  * @param mask
169  *   VLAN offload bit mask.
170  *
171  * @return
172  *   0 on success, a negative errno value otherwise and rte_errno is set.
173  */
174 int
175 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
176 {
177         struct mlx5_priv *priv = dev->data->dev_private;
178         unsigned int i;
179
180         if (mask & ETH_VLAN_STRIP_MASK) {
181                 int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
182                                        DEV_RX_OFFLOAD_VLAN_STRIP);
183
184                 if (!priv->config.hw_vlan_strip) {
185                         DRV_LOG(ERR, "port %u VLAN stripping is not supported",
186                                 dev->data->port_id);
187                         return 0;
188                 }
189                 /* Run on every RX queue and set/reset VLAN stripping. */
190                 for (i = 0; (i != priv->rxqs_n); i++)
191                         mlx5_vlan_strip_queue_set(dev, i, hw_vlan_strip);
192         }
193         return 0;
194 }