net/mlx4: separate Rx configuration functions
[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  * Allocate a Queue Pair.
188  * Optionally setup inline receive if supported.
189  *
190  * @param priv
191  *   Pointer to private structure.
192  * @param cq
193  *   Completion queue to associate with QP.
194  * @param desc
195  *   Number of descriptors in QP (hint only).
196  *
197  * @return
198  *   QP pointer or NULL in case of error and rte_errno is set.
199  */
200 static struct ibv_qp *
201 mlx4_rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
202 {
203         struct ibv_qp *qp;
204         struct ibv_qp_init_attr attr = {
205                 /* CQ to be associated with the send queue. */
206                 .send_cq = cq,
207                 /* CQ to be associated with the receive queue. */
208                 .recv_cq = cq,
209                 .cap = {
210                         /* Max number of outstanding WRs. */
211                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
212                                         priv->device_attr.max_qp_wr :
213                                         desc),
214                         /* Max number of scatter/gather elements in a WR. */
215                         .max_recv_sge = 1,
216                 },
217                 .qp_type = IBV_QPT_RAW_PACKET,
218         };
219
220         qp = ibv_create_qp(priv->pd, &attr);
221         if (!qp)
222                 rte_errno = errno ? errno : EINVAL;
223         return qp;
224 }
225
226 /**
227  * Configure a Rx queue.
228  *
229  * @param dev
230  *   Pointer to Ethernet device structure.
231  * @param rxq
232  *   Pointer to Rx queue structure.
233  * @param desc
234  *   Number of descriptors to configure in queue.
235  * @param socket
236  *   NUMA socket on which memory must be allocated.
237  * @param[in] conf
238  *   Thresholds parameters.
239  * @param mp
240  *   Memory pool for buffer allocations.
241  *
242  * @return
243  *   0 on success, negative errno value otherwise and rte_errno is set.
244  */
245 static int
246 mlx4_rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
247                unsigned int socket, const struct rte_eth_rxconf *conf,
248                struct rte_mempool *mp)
249 {
250         struct priv *priv = dev->data->dev_private;
251         struct rxq tmpl = {
252                 .priv = priv,
253                 .mp = mp,
254                 .socket = socket
255         };
256         struct ibv_qp_attr mod;
257         struct ibv_recv_wr *bad_wr;
258         unsigned int mb_len;
259         int ret;
260
261         (void)conf; /* Thresholds configuration (ignored). */
262         mb_len = rte_pktmbuf_data_room_size(mp);
263         if (desc == 0) {
264                 rte_errno = EINVAL;
265                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
266                 goto error;
267         }
268         /* Enable scattered packets support for this queue if necessary. */
269         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
270         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
271             (mb_len - RTE_PKTMBUF_HEADROOM)) {
272                 ;
273         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
274                 WARN("%p: scattered mode has been requested but is"
275                      " not supported, this may lead to packet loss",
276                      (void *)dev);
277         } else {
278                 WARN("%p: the requested maximum Rx packet size (%u) is"
279                      " larger than a single mbuf (%u) and scattered"
280                      " mode has not been requested",
281                      (void *)dev,
282                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
283                      mb_len - RTE_PKTMBUF_HEADROOM);
284         }
285         /* Use the entire Rx mempool as the memory region. */
286         tmpl.mr = mlx4_mp2mr(priv->pd, mp);
287         if (tmpl.mr == NULL) {
288                 rte_errno = EINVAL;
289                 ERROR("%p: MR creation failure: %s",
290                       (void *)dev, strerror(rte_errno));
291                 goto error;
292         }
293         if (dev->data->dev_conf.intr_conf.rxq) {
294                 tmpl.channel = ibv_create_comp_channel(priv->ctx);
295                 if (tmpl.channel == NULL) {
296                         rte_errno = ENOMEM;
297                         ERROR("%p: Rx interrupt completion channel creation"
298                               " failure: %s",
299                               (void *)dev, strerror(rte_errno));
300                         goto error;
301                 }
302                 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
303                         ERROR("%p: unable to make Rx interrupt completion"
304                               " channel non-blocking: %s",
305                               (void *)dev, strerror(rte_errno));
306                         goto error;
307                 }
308         }
309         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
310         if (tmpl.cq == NULL) {
311                 rte_errno = ENOMEM;
312                 ERROR("%p: CQ creation failure: %s",
313                       (void *)dev, strerror(rte_errno));
314                 goto error;
315         }
316         DEBUG("priv->device_attr.max_qp_wr is %d",
317               priv->device_attr.max_qp_wr);
318         DEBUG("priv->device_attr.max_sge is %d",
319               priv->device_attr.max_sge);
320         tmpl.qp = mlx4_rxq_setup_qp(priv, tmpl.cq, desc);
321         if (tmpl.qp == NULL) {
322                 ERROR("%p: QP creation failure: %s",
323                       (void *)dev, strerror(rte_errno));
324                 goto error;
325         }
326         mod = (struct ibv_qp_attr){
327                 /* Move the QP to this state. */
328                 .qp_state = IBV_QPS_INIT,
329                 /* Primary port number. */
330                 .port_num = priv->port
331         };
332         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
333         if (ret) {
334                 rte_errno = ret;
335                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
336                       (void *)dev, strerror(rte_errno));
337                 goto error;
338         }
339         ret = mlx4_rxq_alloc_elts(&tmpl, desc);
340         if (ret) {
341                 ERROR("%p: RXQ allocation failed: %s",
342                       (void *)dev, strerror(rte_errno));
343                 goto error;
344         }
345         ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
346         if (ret) {
347                 rte_errno = ret;
348                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
349                       (void *)dev,
350                       (void *)bad_wr,
351                       strerror(rte_errno));
352                 goto error;
353         }
354         mod = (struct ibv_qp_attr){
355                 .qp_state = IBV_QPS_RTR
356         };
357         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
358         if (ret) {
359                 rte_errno = ret;
360                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
361                       (void *)dev, strerror(rte_errno));
362                 goto error;
363         }
364         /* Save port ID. */
365         tmpl.port_id = dev->data->port_id;
366         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
367         /* Clean up rxq in case we're reinitializing it. */
368         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
369         mlx4_rxq_cleanup(rxq);
370         *rxq = tmpl;
371         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
372         return 0;
373 error:
374         ret = rte_errno;
375         mlx4_rxq_cleanup(&tmpl);
376         rte_errno = ret;
377         assert(rte_errno > 0);
378         return -rte_errno;
379 }
380
381 /**
382  * DPDK callback to configure a Rx queue.
383  *
384  * @param dev
385  *   Pointer to Ethernet device structure.
386  * @param idx
387  *   Rx queue index.
388  * @param desc
389  *   Number of descriptors to configure in queue.
390  * @param socket
391  *   NUMA socket on which memory must be allocated.
392  * @param[in] conf
393  *   Thresholds parameters.
394  * @param mp
395  *   Memory pool for buffer allocations.
396  *
397  * @return
398  *   0 on success, negative errno value otherwise and rte_errno is set.
399  */
400 int
401 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
402                     unsigned int socket, const struct rte_eth_rxconf *conf,
403                     struct rte_mempool *mp)
404 {
405         struct priv *priv = dev->data->dev_private;
406         struct rxq *rxq = (*priv->rxqs)[idx];
407         int ret;
408
409         DEBUG("%p: configuring queue %u for %u descriptors",
410               (void *)dev, idx, desc);
411         if (idx >= priv->rxqs_n) {
412                 rte_errno = EOVERFLOW;
413                 ERROR("%p: queue index out of range (%u >= %u)",
414                       (void *)dev, idx, priv->rxqs_n);
415                 return -rte_errno;
416         }
417         if (rxq != NULL) {
418                 DEBUG("%p: reusing already allocated queue index %u (%p)",
419                       (void *)dev, idx, (void *)rxq);
420                 if (priv->started) {
421                         rte_errno = EEXIST;
422                         return -rte_errno;
423                 }
424                 (*priv->rxqs)[idx] = NULL;
425                 if (idx == 0)
426                         mlx4_mac_addr_del(priv);
427                 mlx4_rxq_cleanup(rxq);
428         } else {
429                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
430                 if (rxq == NULL) {
431                         rte_errno = ENOMEM;
432                         ERROR("%p: unable to allocate queue index %u",
433                               (void *)dev, idx);
434                         return -rte_errno;
435                 }
436         }
437         ret = mlx4_rxq_setup(dev, rxq, desc, socket, conf, mp);
438         if (ret) {
439                 rte_free(rxq);
440         } else {
441                 rxq->stats.idx = idx;
442                 DEBUG("%p: adding Rx queue %p to list",
443                       (void *)dev, (void *)rxq);
444                 (*priv->rxqs)[idx] = rxq;
445                 /* Update receive callback. */
446                 dev->rx_pkt_burst = mlx4_rx_burst;
447         }
448         return ret;
449 }
450
451 /**
452  * DPDK callback to release a Rx queue.
453  *
454  * @param dpdk_rxq
455  *   Generic Rx queue pointer.
456  */
457 void
458 mlx4_rx_queue_release(void *dpdk_rxq)
459 {
460         struct rxq *rxq = (struct rxq *)dpdk_rxq;
461         struct priv *priv;
462         unsigned int i;
463
464         if (rxq == NULL)
465                 return;
466         priv = rxq->priv;
467         for (i = 0; (i != priv->rxqs_n); ++i)
468                 if ((*priv->rxqs)[i] == rxq) {
469                         DEBUG("%p: removing Rx queue %p from list",
470                               (void *)priv->dev, (void *)rxq);
471                         (*priv->rxqs)[i] = NULL;
472                         if (i == 0)
473                                 mlx4_mac_addr_del(priv);
474                         break;
475                 }
476         mlx4_rxq_cleanup(rxq);
477         rte_free(rxq);
478 }
479
480 /**
481  * Unregister a MAC address.
482  *
483  * @param priv
484  *   Pointer to private structure.
485  */
486 void
487 mlx4_mac_addr_del(struct priv *priv)
488 {
489 #ifndef NDEBUG
490         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
491 #endif
492
493         if (!priv->mac_flow)
494                 return;
495         DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
496               (void *)priv,
497               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
498         claim_zero(ibv_destroy_flow(priv->mac_flow));
499         priv->mac_flow = NULL;
500 }
501
502 /**
503  * Register a MAC address.
504  *
505  * The MAC address is registered in queue 0.
506  *
507  * @param priv
508  *   Pointer to private structure.
509  *
510  * @return
511  *   0 on success, negative errno value otherwise and rte_errno is set.
512  */
513 int
514 mlx4_mac_addr_add(struct priv *priv)
515 {
516         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
517         struct rxq *rxq;
518         struct ibv_flow *flow;
519
520         /* If device isn't started, this is all we need to do. */
521         if (!priv->started)
522                 return 0;
523         if (priv->isolated)
524                 return 0;
525         if (*priv->rxqs && (*priv->rxqs)[0])
526                 rxq = (*priv->rxqs)[0];
527         else
528                 return 0;
529
530         /* Allocate flow specification on the stack. */
531         struct __attribute__((packed)) {
532                 struct ibv_flow_attr attr;
533                 struct ibv_flow_spec_eth spec;
534         } data;
535         struct ibv_flow_attr *attr = &data.attr;
536         struct ibv_flow_spec_eth *spec = &data.spec;
537
538         if (priv->mac_flow)
539                 mlx4_mac_addr_del(priv);
540         /*
541          * No padding must be inserted by the compiler between attr and spec.
542          * This layout is expected by libibverbs.
543          */
544         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
545         *attr = (struct ibv_flow_attr){
546                 .type = IBV_FLOW_ATTR_NORMAL,
547                 .priority = 3,
548                 .num_of_specs = 1,
549                 .port = priv->port,
550                 .flags = 0
551         };
552         *spec = (struct ibv_flow_spec_eth){
553                 .type = IBV_FLOW_SPEC_ETH,
554                 .size = sizeof(*spec),
555                 .val = {
556                         .dst_mac = {
557                                 (*mac)[0], (*mac)[1], (*mac)[2],
558                                 (*mac)[3], (*mac)[4], (*mac)[5]
559                         },
560                 },
561                 .mask = {
562                         .dst_mac = "\xff\xff\xff\xff\xff\xff",
563                 }
564         };
565         DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
566               (void *)priv,
567               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
568         /* Create related flow. */
569         flow = ibv_create_flow(rxq->qp, attr);
570         if (flow == NULL) {
571                 rte_errno = errno ? errno : EINVAL;
572                 ERROR("%p: flow configuration failed, errno=%d: %s",
573                       (void *)rxq, rte_errno, strerror(errno));
574                 return -rte_errno;
575         }
576         assert(priv->mac_flow == NULL);
577         priv->mac_flow = flow;
578         return 0;
579 }