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