net/mlx4: drop live queue reconfiguration support
[dpdk.git] / drivers / net / mlx4 / mlx4_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 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 /**
35  * @file
36  * Rx queues configuration for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <string.h>
44
45 /* Verbs headers do not support -pedantic. */
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic ignored "-Wpedantic"
48 #endif
49 #include <infiniband/verbs.h>
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic error "-Wpedantic"
52 #endif
53
54 #include <rte_common.h>
55 #include <rte_errno.h>
56 #include <rte_ethdev.h>
57 #include <rte_flow.h>
58 #include <rte_malloc.h>
59 #include <rte_mbuf.h>
60 #include <rte_mempool.h>
61
62 #include "mlx4.h"
63 #include "mlx4_flow.h"
64 #include "mlx4_rxtx.h"
65 #include "mlx4_utils.h"
66
67 /**
68  * Allocate Rx queue elements.
69  *
70  * @param rxq
71  *   Pointer to Rx queue structure.
72  * @param elts_n
73  *   Number of elements to allocate.
74  *
75  * @return
76  *   0 on success, negative errno value otherwise and rte_errno is set.
77  */
78 static int
79 mlx4_rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
80 {
81         unsigned int i;
82         struct rxq_elt (*elts)[elts_n] =
83                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
84                                   rxq->socket);
85
86         if (elts == NULL) {
87                 rte_errno = ENOMEM;
88                 ERROR("%p: can't allocate packets array", (void *)rxq);
89                 goto error;
90         }
91         /* For each WR (packet). */
92         for (i = 0; (i != elts_n); ++i) {
93                 struct rxq_elt *elt = &(*elts)[i];
94                 struct ibv_recv_wr *wr = &elt->wr;
95                 struct ibv_sge *sge = &(*elts)[i].sge;
96                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
97
98                 if (buf == NULL) {
99                         rte_errno = ENOMEM;
100                         ERROR("%p: empty mbuf pool", (void *)rxq);
101                         goto error;
102                 }
103                 elt->buf = buf;
104                 wr->next = &(*elts)[(i + 1)].wr;
105                 wr->sg_list = sge;
106                 wr->num_sge = 1;
107                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
108                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
109                 /* Buffer is supposed to be empty. */
110                 assert(rte_pktmbuf_data_len(buf) == 0);
111                 assert(rte_pktmbuf_pkt_len(buf) == 0);
112                 /* sge->addr must be able to store a pointer. */
113                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
114                 /* SGE keeps its headroom. */
115                 sge->addr = (uintptr_t)
116                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
117                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
118                 sge->lkey = rxq->mr->lkey;
119                 /* Redundant check for tailroom. */
120                 assert(sge->length == rte_pktmbuf_tailroom(buf));
121         }
122         /* The last WR pointer must be NULL. */
123         (*elts)[(i - 1)].wr.next = NULL;
124         DEBUG("%p: allocated and configured %u single-segment WRs",
125               (void *)rxq, elts_n);
126         rxq->elts_n = elts_n;
127         rxq->elts_head = 0;
128         rxq->elts = elts;
129         return 0;
130 error:
131         if (elts != NULL) {
132                 for (i = 0; (i != RTE_DIM(*elts)); ++i)
133                         rte_pktmbuf_free_seg((*elts)[i].buf);
134                 rte_free(elts);
135         }
136         DEBUG("%p: failed, freed everything", (void *)rxq);
137         assert(rte_errno > 0);
138         return -rte_errno;
139 }
140
141 /**
142  * Free Rx queue elements.
143  *
144  * @param rxq
145  *   Pointer to Rx queue structure.
146  */
147 static void
148 mlx4_rxq_free_elts(struct rxq *rxq)
149 {
150         unsigned int i;
151         unsigned int elts_n = rxq->elts_n;
152         struct rxq_elt (*elts)[elts_n] = rxq->elts;
153
154         DEBUG("%p: freeing WRs", (void *)rxq);
155         rxq->elts_n = 0;
156         rxq->elts = NULL;
157         if (elts == NULL)
158                 return;
159         for (i = 0; (i != RTE_DIM(*elts)); ++i)
160                 rte_pktmbuf_free_seg((*elts)[i].buf);
161         rte_free(elts);
162 }
163
164 /**
165  * DPDK callback to configure a Rx queue.
166  *
167  * @param dev
168  *   Pointer to Ethernet device structure.
169  * @param idx
170  *   Rx queue index.
171  * @param desc
172  *   Number of descriptors to configure in queue.
173  * @param socket
174  *   NUMA socket on which memory must be allocated.
175  * @param[in] conf
176  *   Thresholds parameters.
177  * @param mp
178  *   Memory pool for buffer allocations.
179  *
180  * @return
181  *   0 on success, negative errno value otherwise and rte_errno is set.
182  */
183 int
184 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
185                     unsigned int socket, const struct rte_eth_rxconf *conf,
186                     struct rte_mempool *mp)
187 {
188         struct priv *priv = dev->data->dev_private;
189         uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
190         struct rte_flow_error error;
191         struct rxq *rxq;
192         int ret;
193
194         (void)conf; /* Thresholds configuration (ignored). */
195         DEBUG("%p: configuring queue %u for %u descriptors",
196               (void *)dev, idx, desc);
197         if (idx >= dev->data->nb_rx_queues) {
198                 rte_errno = EOVERFLOW;
199                 ERROR("%p: queue index out of range (%u >= %u)",
200                       (void *)dev, idx, dev->data->nb_rx_queues);
201                 return -rte_errno;
202         }
203         rxq = dev->data->rx_queues[idx];
204         if (rxq) {
205                 rte_errno = EEXIST;
206                 ERROR("%p: Rx queue %u already configured, release it first",
207                       (void *)dev, idx);
208                 return -rte_errno;
209         }
210         if (!desc) {
211                 rte_errno = EINVAL;
212                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
213                 return -rte_errno;
214         }
215         /* Allocate and initialize Rx queue. */
216         rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
217         if (!rxq) {
218                 rte_errno = ENOMEM;
219                 ERROR("%p: unable to allocate queue index %u",
220                       (void *)dev, idx);
221                 return -rte_errno;
222         }
223         *rxq = (struct rxq){
224                 .priv = priv,
225                 .mp = mp,
226                 .port_id = dev->data->port_id,
227                 .stats.idx = idx,
228                 .socket = socket,
229         };
230         /* Enable scattered packets support for this queue if necessary. */
231         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
232         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
233             (mb_len - RTE_PKTMBUF_HEADROOM)) {
234                 ;
235         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
236                 WARN("%p: scattered mode has been requested but is"
237                      " not supported, this may lead to packet loss",
238                      (void *)dev);
239         } else {
240                 WARN("%p: the requested maximum Rx packet size (%u) is"
241                      " larger than a single mbuf (%u) and scattered"
242                      " mode has not been requested",
243                      (void *)dev,
244                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
245                      mb_len - RTE_PKTMBUF_HEADROOM);
246         }
247         /* Use the entire Rx mempool as the memory region. */
248         rxq->mr = mlx4_mp2mr(priv->pd, mp);
249         if (!rxq->mr) {
250                 rte_errno = EINVAL;
251                 ERROR("%p: MR creation failure: %s",
252                       (void *)dev, strerror(rte_errno));
253                 goto error;
254         }
255         if (dev->data->dev_conf.intr_conf.rxq) {
256                 rxq->channel = ibv_create_comp_channel(priv->ctx);
257                 if (rxq->channel == NULL) {
258                         rte_errno = ENOMEM;
259                         ERROR("%p: Rx interrupt completion channel creation"
260                               " failure: %s",
261                               (void *)dev, strerror(rte_errno));
262                         goto error;
263                 }
264                 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
265                         ERROR("%p: unable to make Rx interrupt completion"
266                               " channel non-blocking: %s",
267                               (void *)dev, strerror(rte_errno));
268                         goto error;
269                 }
270         }
271         rxq->cq = ibv_create_cq(priv->ctx, desc, NULL, rxq->channel, 0);
272         if (!rxq->cq) {
273                 rte_errno = ENOMEM;
274                 ERROR("%p: CQ creation failure: %s",
275                       (void *)dev, strerror(rte_errno));
276                 goto error;
277         }
278         rxq->qp = ibv_create_qp
279                 (priv->pd,
280                  &(struct ibv_qp_init_attr){
281                         .send_cq = rxq->cq,
282                         .recv_cq = rxq->cq,
283                         .cap = {
284                                 .max_recv_wr =
285                                         RTE_MIN(priv->device_attr.max_qp_wr,
286                                                 desc),
287                                 .max_recv_sge = 1,
288                         },
289                         .qp_type = IBV_QPT_RAW_PACKET,
290                  });
291         if (!rxq->qp) {
292                 rte_errno = errno ? errno : EINVAL;
293                 ERROR("%p: QP creation failure: %s",
294                       (void *)dev, strerror(rte_errno));
295                 goto error;
296         }
297         ret = ibv_modify_qp
298                 (rxq->qp,
299                  &(struct ibv_qp_attr){
300                         .qp_state = IBV_QPS_INIT,
301                         .port_num = priv->port,
302                  },
303                  IBV_QP_STATE | IBV_QP_PORT);
304         if (ret) {
305                 rte_errno = ret;
306                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
307                       (void *)dev, strerror(rte_errno));
308                 goto error;
309         }
310         ret = mlx4_rxq_alloc_elts(rxq, desc);
311         if (ret) {
312                 ERROR("%p: RXQ allocation failed: %s",
313                       (void *)dev, strerror(rte_errno));
314                 goto error;
315         }
316         ret = ibv_post_recv(rxq->qp, &(*rxq->elts)[0].wr,
317                             &(struct ibv_recv_wr *){ NULL });
318         if (ret) {
319                 rte_errno = ret;
320                 ERROR("%p: ibv_post_recv() failed: %s",
321                       (void *)dev,
322                       strerror(rte_errno));
323                 goto error;
324         }
325         ret = ibv_modify_qp
326                 (rxq->qp,
327                  &(struct ibv_qp_attr){
328                         .qp_state = IBV_QPS_RTR,
329                  },
330                  IBV_QP_STATE);
331         if (ret) {
332                 rte_errno = ret;
333                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
334                       (void *)dev, strerror(rte_errno));
335                 goto error;
336         }
337         DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
338         dev->data->rx_queues[idx] = rxq;
339         /* Enable associated flows. */
340         ret = mlx4_flow_sync(priv, &error);
341         if (!ret)
342                 return 0;
343         ERROR("cannot re-attach flow rules to queue %u"
344               " (code %d, \"%s\"), flow error type %d, cause %p, message: %s",
345               idx, -ret, strerror(-ret), error.type, error.cause,
346               error.message ? error.message : "(unspecified)");
347 error:
348         dev->data->rx_queues[idx] = NULL;
349         ret = rte_errno;
350         mlx4_rx_queue_release(rxq);
351         rte_errno = ret;
352         assert(rte_errno > 0);
353         return -rte_errno;
354 }
355
356 /**
357  * DPDK callback to release a Rx queue.
358  *
359  * @param dpdk_rxq
360  *   Generic Rx queue pointer.
361  */
362 void
363 mlx4_rx_queue_release(void *dpdk_rxq)
364 {
365         struct rxq *rxq = (struct rxq *)dpdk_rxq;
366         struct priv *priv;
367         unsigned int i;
368
369         if (rxq == NULL)
370                 return;
371         priv = rxq->priv;
372         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
373                 if (priv->dev->data->rx_queues[i] == rxq) {
374                         DEBUG("%p: removing Rx queue %p from list",
375                               (void *)priv->dev, (void *)rxq);
376                         priv->dev->data->rx_queues[i] = NULL;
377                         break;
378                 }
379         mlx4_flow_sync(priv, NULL);
380         mlx4_rxq_free_elts(rxq);
381         if (rxq->qp)
382                 claim_zero(ibv_destroy_qp(rxq->qp));
383         if (rxq->cq)
384                 claim_zero(ibv_destroy_cq(rxq->cq));
385         if (rxq->channel)
386                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
387         if (rxq->mr)
388                 claim_zero(ibv_dereg_mr(rxq->mr));
389         rte_free(rxq);
390 }