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