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