New upstream version 17.11.3
[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_defs.h"
55 #include "mlx5_rxtx.h"
56
57 /**
58  * DPDK callback to update the RSS hash configuration.
59  *
60  * @param dev
61  *   Pointer to Ethernet device structure.
62  * @param[in] rss_conf
63  *   RSS configuration data.
64  *
65  * @return
66  *   0 on success, a negative errno value otherwise and rte_errno is set.
67  */
68 int
69 mlx5_rss_hash_update(struct rte_eth_dev *dev,
70                      struct rte_eth_rss_conf *rss_conf)
71 {
72         struct priv *priv = dev->data->dev_private;
73         unsigned int i;
74         unsigned int idx;
75
76         if (rss_conf->rss_hf & MLX5_RSS_HF_MASK) {
77                 rte_errno = EINVAL;
78                 return -rte_errno;
79         }
80         if (rss_conf->rss_key && rss_conf->rss_key_len) {
81                 if (rss_conf->rss_key_len != rss_hash_default_key_len) {
82                         DRV_LOG(ERR,
83                                 "port %u RSS key len must be %zu Bytes long",
84                                 dev->data->port_id, rss_hash_default_key_len);
85                         rte_errno = EINVAL;
86                         return -rte_errno;
87                 }
88                 priv->rss_conf.rss_key = rte_realloc(priv->rss_conf.rss_key,
89                                                      rss_conf->rss_key_len, 0);
90                 if (!priv->rss_conf.rss_key) {
91                         rte_errno = ENOMEM;
92                         return -rte_errno;
93                 }
94                 memcpy(priv->rss_conf.rss_key, rss_conf->rss_key,
95                        rss_conf->rss_key_len);
96                 priv->rss_conf.rss_key_len = rss_conf->rss_key_len;
97         }
98         priv->rss_conf.rss_hf = rss_conf->rss_hf;
99         /* Enable the RSS hash in all Rx queues. */
100         for (i = 0, idx = 0; idx != priv->rxqs_n; ++i) {
101                 if (!(*priv->rxqs)[i])
102                         continue;
103                 (*priv->rxqs)[i]->rss_hash = !!rss_conf->rss_hf &&
104                         !!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS);
105                 ++idx;
106         }
107         return 0;
108 }
109
110 /**
111  * DPDK callback to get the RSS hash configuration.
112  *
113  * @param dev
114  *   Pointer to Ethernet device structure.
115  * @param[in, out] rss_conf
116  *   RSS configuration data.
117  *
118  * @return
119  *   0 on success, a negative errno value otherwise and rte_errno is set.
120  */
121 int
122 mlx5_rss_hash_conf_get(struct rte_eth_dev *dev,
123                        struct rte_eth_rss_conf *rss_conf)
124 {
125         struct priv *priv = dev->data->dev_private;
126
127         if (!rss_conf) {
128                 rte_errno = EINVAL;
129                 return -rte_errno;
130         }
131         if (rss_conf->rss_key &&
132             (rss_conf->rss_key_len >= priv->rss_conf.rss_key_len)) {
133                 memcpy(rss_conf->rss_key, priv->rss_conf.rss_key,
134                        priv->rss_conf.rss_key_len);
135         }
136         rss_conf->rss_key_len = priv->rss_conf.rss_key_len;
137         rss_conf->rss_hf = priv->rss_conf.rss_hf;
138         return 0;
139 }
140
141 /**
142  * Allocate/reallocate RETA index table.
143  *
144  * @param dev
145  *   Pointer to Ethernet device.
146  * @praram reta_size
147  *   The size of the array to allocate.
148  *
149  * @return
150  *   0 on success, a negative errno value otherwise and rte_errno is set.
151  */
152 int
153 mlx5_rss_reta_index_resize(struct rte_eth_dev *dev, unsigned int reta_size)
154 {
155         struct priv *priv = dev->data->dev_private;
156         void *mem;
157         unsigned int old_size = priv->reta_idx_n;
158
159         if (priv->reta_idx_n == reta_size)
160                 return 0;
161
162         mem = rte_realloc(priv->reta_idx,
163                           reta_size * sizeof((*priv->reta_idx)[0]), 0);
164         if (!mem) {
165                 rte_errno = ENOMEM;
166                 return -rte_errno;
167         }
168         priv->reta_idx = mem;
169         priv->reta_idx_n = reta_size;
170         if (old_size < reta_size)
171                 memset(&(*priv->reta_idx)[old_size], 0,
172                        (reta_size - old_size) *
173                        sizeof((*priv->reta_idx)[0]));
174         return 0;
175 }
176
177 /**
178  * DPDK callback to get 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_query(struct rte_eth_dev *dev,
192                         struct rte_eth_rss_reta_entry64 *reta_conf,
193                         uint16_t reta_size)
194 {
195         struct priv *priv = dev->data->dev_private;
196         unsigned int idx;
197         unsigned int i;
198
199         if (!reta_size || reta_size > priv->reta_idx_n) {
200                 rte_errno = EINVAL;
201                 return -rte_errno;
202         }
203         /* Fill each entry of the table even if its bit is not set. */
204         for (idx = 0, i = 0; (i != reta_size); ++i) {
205                 idx = i / RTE_RETA_GROUP_SIZE;
206                 reta_conf[idx].reta[i % RTE_RETA_GROUP_SIZE] =
207                         (*priv->reta_idx)[i];
208         }
209         return 0;
210 }
211
212 /**
213  * DPDK callback to update the RETA indirection table.
214  *
215  * @param dev
216  *   Pointer to Ethernet device structure.
217  * @param reta_conf
218  *   Pointer to RETA configuration structure array.
219  * @param reta_size
220  *   Size of the RETA table.
221  *
222  * @return
223  *   0 on success, a negative errno value otherwise and rte_errno is set.
224  */
225 int
226 mlx5_dev_rss_reta_update(struct rte_eth_dev *dev,
227                          struct rte_eth_rss_reta_entry64 *reta_conf,
228                          uint16_t reta_size)
229 {
230         int ret;
231         struct priv *priv = dev->data->dev_private;
232         unsigned int idx;
233         unsigned int i;
234         unsigned int pos;
235
236         if (!reta_size) {
237                 rte_errno = EINVAL;
238                 return -rte_errno;
239         }
240         ret = mlx5_rss_reta_index_resize(dev, reta_size);
241         if (ret)
242                 return ret;
243         for (idx = 0, i = 0; (i != reta_size); ++i) {
244                 idx = i / RTE_RETA_GROUP_SIZE;
245                 pos = i % RTE_RETA_GROUP_SIZE;
246                 if (((reta_conf[idx].mask >> i) & 0x1) == 0)
247                         continue;
248                 assert(reta_conf[idx].reta[pos] < priv->rxqs_n);
249                 (*priv->reta_idx)[i] = reta_conf[idx].reta[pos];
250         }
251         if (dev->data->dev_started) {
252                 mlx5_dev_stop(dev);
253                 return mlx5_dev_start(dev);
254         }
255         return 0;
256 }