net/mlx4: remove Rx QP initializer function
[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_malloc.h>
58 #include <rte_mbuf.h>
59 #include <rte_mempool.h>
60
61 #include "mlx4.h"
62 #include "mlx4_rxtx.h"
63 #include "mlx4_utils.h"
64
65 /**
66  * Allocate Rx queue elements.
67  *
68  * @param rxq
69  *   Pointer to Rx queue structure.
70  * @param elts_n
71  *   Number of elements to allocate.
72  *
73  * @return
74  *   0 on success, negative errno value otherwise and rte_errno is set.
75  */
76 static int
77 mlx4_rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
78 {
79         unsigned int i;
80         struct rxq_elt (*elts)[elts_n] =
81                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
82                                   rxq->socket);
83
84         if (elts == NULL) {
85                 rte_errno = ENOMEM;
86                 ERROR("%p: can't allocate packets array", (void *)rxq);
87                 goto error;
88         }
89         /* For each WR (packet). */
90         for (i = 0; (i != elts_n); ++i) {
91                 struct rxq_elt *elt = &(*elts)[i];
92                 struct ibv_recv_wr *wr = &elt->wr;
93                 struct ibv_sge *sge = &(*elts)[i].sge;
94                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
95
96                 if (buf == NULL) {
97                         rte_errno = ENOMEM;
98                         ERROR("%p: empty mbuf pool", (void *)rxq);
99                         goto error;
100                 }
101                 elt->buf = buf;
102                 wr->next = &(*elts)[(i + 1)].wr;
103                 wr->sg_list = sge;
104                 wr->num_sge = 1;
105                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
106                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
107                 /* Buffer is supposed to be empty. */
108                 assert(rte_pktmbuf_data_len(buf) == 0);
109                 assert(rte_pktmbuf_pkt_len(buf) == 0);
110                 /* sge->addr must be able to store a pointer. */
111                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
112                 /* SGE keeps its headroom. */
113                 sge->addr = (uintptr_t)
114                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
115                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
116                 sge->lkey = rxq->mr->lkey;
117                 /* Redundant check for tailroom. */
118                 assert(sge->length == rte_pktmbuf_tailroom(buf));
119         }
120         /* The last WR pointer must be NULL. */
121         (*elts)[(i - 1)].wr.next = NULL;
122         DEBUG("%p: allocated and configured %u single-segment WRs",
123               (void *)rxq, elts_n);
124         rxq->elts_n = elts_n;
125         rxq->elts_head = 0;
126         rxq->elts = elts;
127         return 0;
128 error:
129         if (elts != NULL) {
130                 for (i = 0; (i != RTE_DIM(*elts)); ++i)
131                         rte_pktmbuf_free_seg((*elts)[i].buf);
132                 rte_free(elts);
133         }
134         DEBUG("%p: failed, freed everything", (void *)rxq);
135         assert(rte_errno > 0);
136         return -rte_errno;
137 }
138
139 /**
140  * Free Rx queue elements.
141  *
142  * @param rxq
143  *   Pointer to Rx queue structure.
144  */
145 static void
146 mlx4_rxq_free_elts(struct rxq *rxq)
147 {
148         unsigned int i;
149         unsigned int elts_n = rxq->elts_n;
150         struct rxq_elt (*elts)[elts_n] = rxq->elts;
151
152         DEBUG("%p: freeing WRs", (void *)rxq);
153         rxq->elts_n = 0;
154         rxq->elts = NULL;
155         if (elts == NULL)
156                 return;
157         for (i = 0; (i != RTE_DIM(*elts)); ++i)
158                 rte_pktmbuf_free_seg((*elts)[i].buf);
159         rte_free(elts);
160 }
161
162 /**
163  * Clean up a Rx queue.
164  *
165  * Destroy objects, free allocated memory and reset the structure for reuse.
166  *
167  * @param rxq
168  *   Pointer to Rx queue structure.
169  */
170 void
171 mlx4_rxq_cleanup(struct rxq *rxq)
172 {
173         DEBUG("cleaning up %p", (void *)rxq);
174         mlx4_rxq_free_elts(rxq);
175         if (rxq->qp != NULL)
176                 claim_zero(ibv_destroy_qp(rxq->qp));
177         if (rxq->cq != NULL)
178                 claim_zero(ibv_destroy_cq(rxq->cq));
179         if (rxq->channel != NULL)
180                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
181         if (rxq->mr != NULL)
182                 claim_zero(ibv_dereg_mr(rxq->mr));
183         memset(rxq, 0, sizeof(*rxq));
184 }
185
186 /**
187  * Configure a Rx queue.
188  *
189  * @param dev
190  *   Pointer to Ethernet device structure.
191  * @param rxq
192  *   Pointer to Rx queue structure.
193  * @param desc
194  *   Number of descriptors to configure in queue.
195  * @param socket
196  *   NUMA socket on which memory must be allocated.
197  * @param[in] conf
198  *   Thresholds parameters.
199  * @param mp
200  *   Memory pool for buffer allocations.
201  *
202  * @return
203  *   0 on success, negative errno value otherwise and rte_errno is set.
204  */
205 static int
206 mlx4_rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
207                unsigned int socket, const struct rte_eth_rxconf *conf,
208                struct rte_mempool *mp)
209 {
210         struct priv *priv = dev->data->dev_private;
211         struct rxq tmpl = {
212                 .priv = priv,
213                 .mp = mp,
214                 .socket = socket
215         };
216         struct ibv_qp_attr mod;
217         struct ibv_qp_init_attr qp_init;
218         struct ibv_recv_wr *bad_wr;
219         unsigned int mb_len;
220         int ret;
221
222         (void)conf; /* Thresholds configuration (ignored). */
223         mb_len = rte_pktmbuf_data_room_size(mp);
224         if (desc == 0) {
225                 rte_errno = EINVAL;
226                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
227                 goto error;
228         }
229         /* Enable scattered packets support for this queue if necessary. */
230         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
231         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
232             (mb_len - RTE_PKTMBUF_HEADROOM)) {
233                 ;
234         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
235                 WARN("%p: scattered mode has been requested but is"
236                      " not supported, this may lead to packet loss",
237                      (void *)dev);
238         } else {
239                 WARN("%p: the requested maximum Rx packet size (%u) is"
240                      " larger than a single mbuf (%u) and scattered"
241                      " mode has not been requested",
242                      (void *)dev,
243                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
244                      mb_len - RTE_PKTMBUF_HEADROOM);
245         }
246         /* Use the entire Rx mempool as the memory region. */
247         tmpl.mr = mlx4_mp2mr(priv->pd, mp);
248         if (tmpl.mr == NULL) {
249                 rte_errno = EINVAL;
250                 ERROR("%p: MR creation failure: %s",
251                       (void *)dev, strerror(rte_errno));
252                 goto error;
253         }
254         if (dev->data->dev_conf.intr_conf.rxq) {
255                 tmpl.channel = ibv_create_comp_channel(priv->ctx);
256                 if (tmpl.channel == NULL) {
257                         rte_errno = ENOMEM;
258                         ERROR("%p: Rx interrupt completion channel creation"
259                               " failure: %s",
260                               (void *)dev, strerror(rte_errno));
261                         goto error;
262                 }
263                 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
264                         ERROR("%p: unable to make Rx interrupt completion"
265                               " channel non-blocking: %s",
266                               (void *)dev, strerror(rte_errno));
267                         goto error;
268                 }
269         }
270         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
271         if (tmpl.cq == NULL) {
272                 rte_errno = ENOMEM;
273                 ERROR("%p: CQ creation failure: %s",
274                       (void *)dev, strerror(rte_errno));
275                 goto error;
276         }
277         DEBUG("priv->device_attr.max_qp_wr is %d",
278               priv->device_attr.max_qp_wr);
279         DEBUG("priv->device_attr.max_sge is %d",
280               priv->device_attr.max_sge);
281         qp_init = (struct ibv_qp_init_attr){
282                 /* CQ to be associated with the send queue. */
283                 .send_cq = tmpl.cq,
284                 /* CQ to be associated with the receive queue. */
285                 .recv_cq = tmpl.cq,
286                 .cap = {
287                         /* Max number of outstanding WRs. */
288                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
289                                         priv->device_attr.max_qp_wr :
290                                         desc),
291                         /* Max number of scatter/gather elements in a WR. */
292                         .max_recv_sge = 1,
293                 },
294                 .qp_type = IBV_QPT_RAW_PACKET,
295         };
296         tmpl.qp = ibv_create_qp(priv->pd, &qp_init);
297         if (tmpl.qp == NULL) {
298                 rte_errno = errno ? errno : EINVAL;
299                 ERROR("%p: QP creation failure: %s",
300                       (void *)dev, strerror(rte_errno));
301                 goto error;
302         }
303         mod = (struct ibv_qp_attr){
304                 /* Move the QP to this state. */
305                 .qp_state = IBV_QPS_INIT,
306                 /* Primary port number. */
307                 .port_num = priv->port
308         };
309         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
310         if (ret) {
311                 rte_errno = ret;
312                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
313                       (void *)dev, strerror(rte_errno));
314                 goto error;
315         }
316         ret = mlx4_rxq_alloc_elts(&tmpl, desc);
317         if (ret) {
318                 ERROR("%p: RXQ allocation failed: %s",
319                       (void *)dev, strerror(rte_errno));
320                 goto error;
321         }
322         ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
323         if (ret) {
324                 rte_errno = ret;
325                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
326                       (void *)dev,
327                       (void *)bad_wr,
328                       strerror(rte_errno));
329                 goto error;
330         }
331         mod = (struct ibv_qp_attr){
332                 .qp_state = IBV_QPS_RTR
333         };
334         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
335         if (ret) {
336                 rte_errno = ret;
337                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
338                       (void *)dev, strerror(rte_errno));
339                 goto error;
340         }
341         /* Save port ID. */
342         tmpl.port_id = dev->data->port_id;
343         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
344         /* Clean up rxq in case we're reinitializing it. */
345         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
346         mlx4_rxq_cleanup(rxq);
347         *rxq = tmpl;
348         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
349         return 0;
350 error:
351         ret = rte_errno;
352         mlx4_rxq_cleanup(&tmpl);
353         rte_errno = ret;
354         assert(rte_errno > 0);
355         return -rte_errno;
356 }
357
358 /**
359  * DPDK callback to configure a Rx queue.
360  *
361  * @param dev
362  *   Pointer to Ethernet device structure.
363  * @param idx
364  *   Rx queue index.
365  * @param desc
366  *   Number of descriptors to configure in queue.
367  * @param socket
368  *   NUMA socket on which memory must be allocated.
369  * @param[in] conf
370  *   Thresholds parameters.
371  * @param mp
372  *   Memory pool for buffer allocations.
373  *
374  * @return
375  *   0 on success, negative errno value otherwise and rte_errno is set.
376  */
377 int
378 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
379                     unsigned int socket, const struct rte_eth_rxconf *conf,
380                     struct rte_mempool *mp)
381 {
382         struct priv *priv = dev->data->dev_private;
383         struct rxq *rxq = dev->data->rx_queues[idx];
384         int ret;
385
386         DEBUG("%p: configuring queue %u for %u descriptors",
387               (void *)dev, idx, desc);
388         if (idx >= dev->data->nb_rx_queues) {
389                 rte_errno = EOVERFLOW;
390                 ERROR("%p: queue index out of range (%u >= %u)",
391                       (void *)dev, idx, dev->data->nb_rx_queues);
392                 return -rte_errno;
393         }
394         if (rxq != NULL) {
395                 DEBUG("%p: reusing already allocated queue index %u (%p)",
396                       (void *)dev, idx, (void *)rxq);
397                 if (priv->started) {
398                         rte_errno = EEXIST;
399                         return -rte_errno;
400                 }
401                 dev->data->rx_queues[idx] = NULL;
402                 if (idx == 0)
403                         mlx4_mac_addr_del(priv);
404                 mlx4_rxq_cleanup(rxq);
405         } else {
406                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
407                 if (rxq == NULL) {
408                         rte_errno = ENOMEM;
409                         ERROR("%p: unable to allocate queue index %u",
410                               (void *)dev, idx);
411                         return -rte_errno;
412                 }
413         }
414         ret = mlx4_rxq_setup(dev, rxq, desc, socket, conf, mp);
415         if (ret) {
416                 rte_free(rxq);
417         } else {
418                 rxq->stats.idx = idx;
419                 DEBUG("%p: adding Rx queue %p to list",
420                       (void *)dev, (void *)rxq);
421                 dev->data->rx_queues[idx] = rxq;
422                 /* Update receive callback. */
423                 dev->rx_pkt_burst = mlx4_rx_burst;
424         }
425         return ret;
426 }
427
428 /**
429  * DPDK callback to release a Rx queue.
430  *
431  * @param dpdk_rxq
432  *   Generic Rx queue pointer.
433  */
434 void
435 mlx4_rx_queue_release(void *dpdk_rxq)
436 {
437         struct rxq *rxq = (struct rxq *)dpdk_rxq;
438         struct priv *priv;
439         unsigned int i;
440
441         if (rxq == NULL)
442                 return;
443         priv = rxq->priv;
444         for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
445                 if (priv->dev->data->rx_queues[i] == rxq) {
446                         DEBUG("%p: removing Rx queue %p from list",
447                               (void *)priv->dev, (void *)rxq);
448                         priv->dev->data->rx_queues[i] = NULL;
449                         if (i == 0)
450                                 mlx4_mac_addr_del(priv);
451                         break;
452                 }
453         mlx4_rxq_cleanup(rxq);
454         rte_free(rxq);
455 }
456
457 /**
458  * Unregister a MAC address.
459  *
460  * @param priv
461  *   Pointer to private structure.
462  */
463 void
464 mlx4_mac_addr_del(struct priv *priv)
465 {
466 #ifndef NDEBUG
467         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
468 #endif
469
470         if (!priv->mac_flow)
471                 return;
472         DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
473               (void *)priv,
474               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
475         claim_zero(ibv_destroy_flow(priv->mac_flow));
476         priv->mac_flow = NULL;
477 }
478
479 /**
480  * Register a MAC address.
481  *
482  * The MAC address is registered in queue 0.
483  *
484  * @param priv
485  *   Pointer to private structure.
486  *
487  * @return
488  *   0 on success, negative errno value otherwise and rte_errno is set.
489  */
490 int
491 mlx4_mac_addr_add(struct priv *priv)
492 {
493         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
494         struct rxq *rxq;
495         struct ibv_flow *flow;
496
497         /* If device isn't started, this is all we need to do. */
498         if (!priv->started)
499                 return 0;
500         if (priv->isolated)
501                 return 0;
502         if (priv->dev->data->rx_queues && priv->dev->data->rx_queues[0])
503                 rxq = priv->dev->data->rx_queues[0];
504         else
505                 return 0;
506
507         /* Allocate flow specification on the stack. */
508         struct __attribute__((packed)) {
509                 struct ibv_flow_attr attr;
510                 struct ibv_flow_spec_eth spec;
511         } data;
512         struct ibv_flow_attr *attr = &data.attr;
513         struct ibv_flow_spec_eth *spec = &data.spec;
514
515         if (priv->mac_flow)
516                 mlx4_mac_addr_del(priv);
517         /*
518          * No padding must be inserted by the compiler between attr and spec.
519          * This layout is expected by libibverbs.
520          */
521         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
522         *attr = (struct ibv_flow_attr){
523                 .type = IBV_FLOW_ATTR_NORMAL,
524                 .priority = 3,
525                 .num_of_specs = 1,
526                 .port = priv->port,
527                 .flags = 0
528         };
529         *spec = (struct ibv_flow_spec_eth){
530                 .type = IBV_FLOW_SPEC_ETH,
531                 .size = sizeof(*spec),
532                 .val = {
533                         .dst_mac = {
534                                 (*mac)[0], (*mac)[1], (*mac)[2],
535                                 (*mac)[3], (*mac)[4], (*mac)[5]
536                         },
537                 },
538                 .mask = {
539                         .dst_mac = "\xff\xff\xff\xff\xff\xff",
540                 }
541         };
542         DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
543               (void *)priv,
544               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
545         /* Create related flow. */
546         flow = ibv_create_flow(rxq->qp, attr);
547         if (flow == NULL) {
548                 rte_errno = errno ? errno : EINVAL;
549                 ERROR("%p: flow configuration failed, errno=%d: %s",
550                       (void *)rxq, rte_errno, strerror(errno));
551                 return -rte_errno;
552         }
553         assert(priv->mac_flow == NULL);
554         priv->mac_flow = flow;
555         return 0;
556 }