net/i40e: fix Rx packet statistics
[dpdk.git] / drivers / net / mlx5 / mlx5_rss.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 <stdint.h>
8 #include <errno.h>
9 #include <string.h>
10
11 #include <rte_malloc.h>
12 #include <ethdev_driver.h>
13
14 #include <mlx5_malloc.h>
15
16 #include "mlx5_defs.h"
17 #include "mlx5.h"
18 #include "mlx5_rxtx.h"
19 #include "mlx5_rx.h"
20
21 /**
22  * DPDK callback to update the RSS hash configuration.
23  *
24  * @param dev
25  *   Pointer to Ethernet device structure.
26  * @param[in] rss_conf
27  *   RSS configuration data.
28  *
29  * @return
30  *   0 on success, a negative errno value otherwise and rte_errno is set.
31  */
32 int
33 mlx5_rss_hash_update(struct rte_eth_dev *dev,
34                      struct rte_eth_rss_conf *rss_conf)
35 {
36         struct mlx5_priv *priv = dev->data->dev_private;
37         unsigned int i;
38         unsigned int idx;
39
40         if (rss_conf->rss_hf & MLX5_RSS_HF_MASK) {
41                 rte_errno = EINVAL;
42                 return -rte_errno;
43         }
44         if (rss_conf->rss_key && rss_conf->rss_key_len) {
45                 if (rss_conf->rss_key_len != MLX5_RSS_HASH_KEY_LEN) {
46                         DRV_LOG(ERR,
47                                 "port %u RSS key len must be %s Bytes long",
48                                 dev->data->port_id,
49                                 RTE_STR(MLX5_RSS_HASH_KEY_LEN));
50                         rte_errno = EINVAL;
51                         return -rte_errno;
52                 }
53                 priv->rss_conf.rss_key = mlx5_realloc(priv->rss_conf.rss_key,
54                                                       MLX5_MEM_RTE,
55                                                       rss_conf->rss_key_len,
56                                                       0, SOCKET_ID_ANY);
57                 if (!priv->rss_conf.rss_key) {
58                         rte_errno = ENOMEM;
59                         return -rte_errno;
60                 }
61                 memcpy(priv->rss_conf.rss_key, rss_conf->rss_key,
62                        rss_conf->rss_key_len);
63                 priv->rss_conf.rss_key_len = rss_conf->rss_key_len;
64         }
65         priv->rss_conf.rss_hf = rss_conf->rss_hf;
66         /* Enable the RSS hash in all Rx queues. */
67         for (i = 0, idx = 0; idx != priv->rxqs_n; ++i) {
68                 if (!(*priv->rxqs)[i])
69                         continue;
70                 (*priv->rxqs)[i]->rss_hash = !!rss_conf->rss_hf &&
71                         !!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS);
72                 ++idx;
73         }
74         return 0;
75 }
76
77 /**
78  * DPDK callback to get the RSS hash configuration.
79  *
80  * @param dev
81  *   Pointer to Ethernet device structure.
82  * @param[in, out] rss_conf
83  *   RSS configuration data.
84  *
85  * @return
86  *   0 on success, a negative errno value otherwise and rte_errno is set.
87  */
88 int
89 mlx5_rss_hash_conf_get(struct rte_eth_dev *dev,
90                        struct rte_eth_rss_conf *rss_conf)
91 {
92         struct mlx5_priv *priv = dev->data->dev_private;
93
94         if (!rss_conf) {
95                 rte_errno = EINVAL;
96                 return -rte_errno;
97         }
98         if (rss_conf->rss_key &&
99             (rss_conf->rss_key_len >= priv->rss_conf.rss_key_len)) {
100                 memcpy(rss_conf->rss_key, priv->rss_conf.rss_key,
101                        priv->rss_conf.rss_key_len);
102         }
103         rss_conf->rss_key_len = priv->rss_conf.rss_key_len;
104         rss_conf->rss_hf = priv->rss_conf.rss_hf;
105         return 0;
106 }
107
108 /**
109  * Allocate/reallocate RETA index table.
110  *
111  * @param dev
112  *   Pointer to Ethernet device.
113  * @praram reta_size
114  *   The size of the array to allocate.
115  *
116  * @return
117  *   0 on success, a negative errno value otherwise and rte_errno is set.
118  */
119 int
120 mlx5_rss_reta_index_resize(struct rte_eth_dev *dev, unsigned int reta_size)
121 {
122         struct mlx5_priv *priv = dev->data->dev_private;
123         void *mem;
124         unsigned int old_size = priv->reta_idx_n;
125
126         if (priv->reta_idx_n == reta_size)
127                 return 0;
128
129         mem = mlx5_realloc(priv->reta_idx, MLX5_MEM_RTE,
130                            reta_size * sizeof((*priv->reta_idx)[0]), 0,
131                            SOCKET_ID_ANY);
132         if (!mem) {
133                 rte_errno = ENOMEM;
134                 return -rte_errno;
135         }
136         priv->reta_idx = mem;
137         priv->reta_idx_n = reta_size;
138         if (old_size < reta_size)
139                 memset(&(*priv->reta_idx)[old_size], 0,
140                        (reta_size - old_size) *
141                        sizeof((*priv->reta_idx)[0]));
142         return 0;
143 }
144
145 /**
146  * DPDK callback to get the RETA indirection table.
147  *
148  * @param dev
149  *   Pointer to Ethernet device structure.
150  * @param reta_conf
151  *   Pointer to RETA configuration structure array.
152  * @param reta_size
153  *   Size of the RETA table.
154  *
155  * @return
156  *   0 on success, a negative errno value otherwise and rte_errno is set.
157  */
158 int
159 mlx5_dev_rss_reta_query(struct rte_eth_dev *dev,
160                         struct rte_eth_rss_reta_entry64 *reta_conf,
161                         uint16_t reta_size)
162 {
163         struct mlx5_priv *priv = dev->data->dev_private;
164         unsigned int idx;
165         unsigned int i;
166
167         if (!reta_size || reta_size > priv->reta_idx_n) {
168                 rte_errno = EINVAL;
169                 return -rte_errno;
170         }
171         /* Fill each entry of the table even if its bit is not set. */
172         for (idx = 0, i = 0; (i != reta_size); ++i) {
173                 idx = i / RTE_RETA_GROUP_SIZE;
174                 reta_conf[idx].reta[i % RTE_RETA_GROUP_SIZE] =
175                         (*priv->reta_idx)[i];
176         }
177         return 0;
178 }
179
180 /**
181  * DPDK callback to update the RETA indirection table.
182  *
183  * @param dev
184  *   Pointer to Ethernet device structure.
185  * @param reta_conf
186  *   Pointer to RETA configuration structure array.
187  * @param reta_size
188  *   Size of the RETA table.
189  *
190  * @return
191  *   0 on success, a negative errno value otherwise and rte_errno is set.
192  */
193 int
194 mlx5_dev_rss_reta_update(struct rte_eth_dev *dev,
195                          struct rte_eth_rss_reta_entry64 *reta_conf,
196                          uint16_t reta_size)
197 {
198         int ret;
199         struct mlx5_priv *priv = dev->data->dev_private;
200         unsigned int idx;
201         unsigned int i;
202         unsigned int pos;
203
204         if (!reta_size) {
205                 rte_errno = EINVAL;
206                 return -rte_errno;
207         }
208         ret = mlx5_rss_reta_index_resize(dev, reta_size);
209         if (ret)
210                 return ret;
211         for (idx = 0, i = 0; (i != reta_size); ++i) {
212                 idx = i / RTE_RETA_GROUP_SIZE;
213                 pos = i % RTE_RETA_GROUP_SIZE;
214                 if (((reta_conf[idx].mask >> i) & 0x1) == 0)
215                         continue;
216                 MLX5_ASSERT(reta_conf[idx].reta[pos] < priv->rxqs_n);
217                 (*priv->reta_idx)[i] = reta_conf[idx].reta[pos];
218         }
219
220         priv->skip_default_rss_reta = 1;
221
222         if (dev->data->dev_started) {
223                 mlx5_dev_stop(dev);
224                 return mlx5_dev_start(dev);
225         }
226         return 0;
227 }