New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_rss.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stddef.h>
35 #include <stdint.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <assert.h>
39
40 /* Verbs header. */
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #ifdef PEDANTIC
43 #pragma GCC diagnostic ignored "-Wpedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic error "-Wpedantic"
48 #endif
49
50 #include <rte_malloc.h>
51 #include <rte_ethdev.h>
52
53 #include "mlx5.h"
54 #include "mlx5_rxtx.h"
55
56 /**
57  * DPDK callback to update the RSS hash configuration.
58  *
59  * @param dev
60  *   Pointer to Ethernet device structure.
61  * @param[in] rss_conf
62  *   RSS configuration data.
63  *
64  * @return
65  *   0 on success, negative errno value on failure.
66  */
67 int
68 mlx5_rss_hash_update(struct rte_eth_dev *dev,
69                      struct rte_eth_rss_conf *rss_conf)
70 {
71         struct priv *priv = dev->data->dev_private;
72         int ret = 0;
73
74         priv_lock(priv);
75         if (rss_conf->rss_key && rss_conf->rss_key_len) {
76                 priv->rss_conf.rss_key = rte_realloc(priv->rss_conf.rss_key,
77                                                      rss_conf->rss_key_len, 0);
78                 if (!priv->rss_conf.rss_key) {
79                         ret = -ENOMEM;
80                         goto out;
81                 }
82                 memcpy(priv->rss_conf.rss_key, rss_conf->rss_key,
83                        rss_conf->rss_key_len);
84                 priv->rss_conf.rss_key_len = rss_conf->rss_key_len;
85         }
86         priv->rss_conf.rss_hf = rss_conf->rss_hf;
87 out:
88         priv_unlock(priv);
89         return ret;
90 }
91
92 /**
93  * DPDK callback to get the RSS hash configuration.
94  *
95  * @param dev
96  *   Pointer to Ethernet device structure.
97  * @param[in, out] rss_conf
98  *   RSS configuration data.
99  *
100  * @return
101  *   0 on success, negative errno value on failure.
102  */
103 int
104 mlx5_rss_hash_conf_get(struct rte_eth_dev *dev,
105                        struct rte_eth_rss_conf *rss_conf)
106 {
107         struct priv *priv = dev->data->dev_private;
108
109         if (!rss_conf)
110                 return -EINVAL;
111         priv_lock(priv);
112         if (rss_conf->rss_key &&
113             (rss_conf->rss_key_len >= priv->rss_conf.rss_key_len)) {
114                 memcpy(rss_conf->rss_key, priv->rss_conf.rss_key,
115                        priv->rss_conf.rss_key_len);
116         }
117         rss_conf->rss_key_len = priv->rss_conf.rss_key_len;
118         rss_conf->rss_hf = priv->rss_conf.rss_hf;
119         priv_unlock(priv);
120         return 0;
121 }
122
123 /**
124  * Allocate/reallocate RETA index table.
125  *
126  * @param priv
127  *   Pointer to private structure.
128  * @praram reta_size
129  *   The size of the array to allocate.
130  *
131  * @return
132  *   0 on success, errno value on failure.
133  */
134 int
135 priv_rss_reta_index_resize(struct priv *priv, unsigned int reta_size)
136 {
137         void *mem;
138         unsigned int old_size = priv->reta_idx_n;
139
140         if (priv->reta_idx_n == reta_size)
141                 return 0;
142
143         mem = rte_realloc(priv->reta_idx,
144                           reta_size * sizeof((*priv->reta_idx)[0]), 0);
145         if (!mem)
146                 return ENOMEM;
147         priv->reta_idx = mem;
148         priv->reta_idx_n = reta_size;
149
150         if (old_size < reta_size)
151                 memset(&(*priv->reta_idx)[old_size], 0,
152                        (reta_size - old_size) *
153                        sizeof((*priv->reta_idx)[0]));
154         return 0;
155 }
156
157 /**
158  * Query RETA table.
159  *
160  * @param priv
161  *   Pointer to private structure.
162  * @param[in, out] reta_conf
163  *   Pointer to the first RETA configuration structure.
164  * @param reta_size
165  *   Number of entries.
166  *
167  * @return
168  *   0 on success, errno value on failure.
169  */
170 static int
171 priv_dev_rss_reta_query(struct priv *priv,
172                         struct rte_eth_rss_reta_entry64 *reta_conf,
173                         unsigned int reta_size)
174 {
175         unsigned int idx;
176         unsigned int i;
177
178         if (!reta_size || reta_size > priv->reta_idx_n)
179                 return EINVAL;
180         /* Fill each entry of the table even if its bit is not set. */
181         for (idx = 0, i = 0; (i != reta_size); ++i) {
182                 idx = i / RTE_RETA_GROUP_SIZE;
183                 reta_conf[idx].reta[i % RTE_RETA_GROUP_SIZE] =
184                         (*priv->reta_idx)[i];
185         }
186         return 0;
187 }
188
189 /**
190  * Update RETA table.
191  *
192  * @param priv
193  *   Pointer to private structure.
194  * @param[in] reta_conf
195  *   Pointer to the first RETA configuration structure.
196  * @param reta_size
197  *   Number of entries.
198  *
199  * @return
200  *   0 on success, errno value on failure.
201  */
202 static int
203 priv_dev_rss_reta_update(struct priv *priv,
204                          struct rte_eth_rss_reta_entry64 *reta_conf,
205                          unsigned int reta_size)
206 {
207         unsigned int idx;
208         unsigned int i;
209         unsigned int pos;
210         int ret;
211
212         if (!reta_size)
213                 return EINVAL;
214         ret = priv_rss_reta_index_resize(priv, reta_size);
215         if (ret)
216                 return ret;
217
218         for (idx = 0, i = 0; (i != reta_size); ++i) {
219                 idx = i / RTE_RETA_GROUP_SIZE;
220                 pos = i % RTE_RETA_GROUP_SIZE;
221                 if (((reta_conf[idx].mask >> i) & 0x1) == 0)
222                         continue;
223                 assert(reta_conf[idx].reta[pos] < priv->rxqs_n);
224                 (*priv->reta_idx)[i] = reta_conf[idx].reta[pos];
225         }
226         return 0;
227 }
228
229 /**
230  * DPDK callback to get the RETA indirection table.
231  *
232  * @param dev
233  *   Pointer to Ethernet device structure.
234  * @param reta_conf
235  *   Pointer to RETA configuration structure array.
236  * @param reta_size
237  *   Size of the RETA table.
238  *
239  * @return
240  *   0 on success, negative errno value on failure.
241  */
242 int
243 mlx5_dev_rss_reta_query(struct rte_eth_dev *dev,
244                         struct rte_eth_rss_reta_entry64 *reta_conf,
245                         uint16_t reta_size)
246 {
247         int ret;
248         struct priv *priv = dev->data->dev_private;
249
250         priv_lock(priv);
251         ret = priv_dev_rss_reta_query(priv, reta_conf, reta_size);
252         priv_unlock(priv);
253         return -ret;
254 }
255
256 /**
257  * DPDK callback to update the RETA indirection table.
258  *
259  * @param dev
260  *   Pointer to Ethernet device structure.
261  * @param reta_conf
262  *   Pointer to RETA configuration structure array.
263  * @param reta_size
264  *   Size of the RETA table.
265  *
266  * @return
267  *   0 on success, negative errno value on failure.
268  */
269 int
270 mlx5_dev_rss_reta_update(struct rte_eth_dev *dev,
271                          struct rte_eth_rss_reta_entry64 *reta_conf,
272                          uint16_t reta_size)
273 {
274         int ret;
275         struct priv *priv = dev->data->dev_private;
276
277         assert(!mlx5_is_secondary());
278         priv_lock(priv);
279         ret = priv_dev_rss_reta_update(priv, reta_conf, reta_size);
280         priv_unlock(priv);
281         if (dev->data->dev_started) {
282                 mlx5_dev_stop(dev);
283                 mlx5_dev_start(dev);
284         }
285         return -ret;
286 }