cff57c2f6e7af64381a031e7d8978d96e7afbfaa
[dpdk.git] / drivers / net / mlx4 / mlx4.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2012 6WIND S.A.
5  *   Copyright 2012 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 /* System headers. */
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <assert.h>
44
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_ethdev_pci.h>
48 #include <rte_dev.h>
49 #include <rte_mbuf.h>
50 #include <rte_errno.h>
51 #include <rte_mempool.h>
52 #include <rte_malloc.h>
53 #include <rte_memory.h>
54 #include <rte_flow.h>
55 #include <rte_kvargs.h>
56 #include <rte_interrupts.h>
57 #include <rte_common.h>
58
59 /* Generated configuration header. */
60 #include "mlx4_autoconf.h"
61
62 /* PMD headers. */
63 #include "mlx4.h"
64 #include "mlx4_flow.h"
65 #include "mlx4_rxtx.h"
66 #include "mlx4_utils.h"
67
68 /** Configuration structure for device arguments. */
69 struct mlx4_conf {
70         struct {
71                 uint32_t present; /**< Bit-field for existing ports. */
72                 uint32_t enabled; /**< Bit-field for user-enabled ports. */
73         } ports;
74 };
75
76 /* Available parameters list. */
77 const char *pmd_mlx4_init_params[] = {
78         MLX4_PMD_PORT_KVARG,
79         NULL,
80 };
81
82 /* Device configuration. */
83
84 static int
85 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
86           unsigned int socket, const struct rte_eth_txconf *conf);
87
88 static void
89 txq_cleanup(struct txq *txq);
90
91 static int
92 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
93           unsigned int socket, const struct rte_eth_rxconf *conf,
94           struct rte_mempool *mp);
95
96 static void
97 rxq_cleanup(struct rxq *rxq);
98
99 static void
100 priv_mac_addr_del(struct priv *priv);
101
102 /**
103  * DPDK callback for Ethernet device configuration.
104  *
105  * Prepare the driver for a given number of TX and RX queues.
106  *
107  * @param dev
108  *   Pointer to Ethernet device structure.
109  *
110  * @return
111  *   0 on success, negative errno value otherwise and rte_errno is set.
112  */
113 static int
114 mlx4_dev_configure(struct rte_eth_dev *dev)
115 {
116         struct priv *priv = dev->data->dev_private;
117         unsigned int rxqs_n = dev->data->nb_rx_queues;
118         unsigned int txqs_n = dev->data->nb_tx_queues;
119
120         priv->rxqs = (void *)dev->data->rx_queues;
121         priv->txqs = (void *)dev->data->tx_queues;
122         if (txqs_n != priv->txqs_n) {
123                 INFO("%p: TX queues number update: %u -> %u",
124                      (void *)dev, priv->txqs_n, txqs_n);
125                 priv->txqs_n = txqs_n;
126         }
127         if (rxqs_n != priv->rxqs_n) {
128                 INFO("%p: Rx queues number update: %u -> %u",
129                      (void *)dev, priv->rxqs_n, rxqs_n);
130                 priv->rxqs_n = rxqs_n;
131         }
132         return 0;
133 }
134
135 /* TX queues handling. */
136
137 /**
138  * Allocate TX queue elements.
139  *
140  * @param txq
141  *   Pointer to TX queue structure.
142  * @param elts_n
143  *   Number of elements to allocate.
144  *
145  * @return
146  *   0 on success, negative errno value otherwise and rte_errno is set.
147  */
148 static int
149 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
150 {
151         unsigned int i;
152         struct txq_elt (*elts)[elts_n] =
153                 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
154         int ret = 0;
155
156         if (elts == NULL) {
157                 ERROR("%p: can't allocate packets array", (void *)txq);
158                 ret = ENOMEM;
159                 goto error;
160         }
161         for (i = 0; (i != elts_n); ++i) {
162                 struct txq_elt *elt = &(*elts)[i];
163
164                 elt->buf = NULL;
165         }
166         DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
167         txq->elts_n = elts_n;
168         txq->elts = elts;
169         txq->elts_head = 0;
170         txq->elts_tail = 0;
171         txq->elts_comp = 0;
172         /*
173          * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
174          * at least 4 times per ring.
175          */
176         txq->elts_comp_cd_init =
177                 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
178                  MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
179         txq->elts_comp_cd = txq->elts_comp_cd_init;
180         assert(ret == 0);
181         return 0;
182 error:
183         rte_free(elts);
184         DEBUG("%p: failed, freed everything", (void *)txq);
185         assert(ret > 0);
186         rte_errno = ret;
187         return -rte_errno;
188 }
189
190 /**
191  * Free TX queue elements.
192  *
193  * @param txq
194  *   Pointer to TX queue structure.
195  */
196 static void
197 txq_free_elts(struct txq *txq)
198 {
199         unsigned int elts_n = txq->elts_n;
200         unsigned int elts_head = txq->elts_head;
201         unsigned int elts_tail = txq->elts_tail;
202         struct txq_elt (*elts)[elts_n] = txq->elts;
203
204         DEBUG("%p: freeing WRs", (void *)txq);
205         txq->elts_n = 0;
206         txq->elts_head = 0;
207         txq->elts_tail = 0;
208         txq->elts_comp = 0;
209         txq->elts_comp_cd = 0;
210         txq->elts_comp_cd_init = 0;
211         txq->elts = NULL;
212         if (elts == NULL)
213                 return;
214         while (elts_tail != elts_head) {
215                 struct txq_elt *elt = &(*elts)[elts_tail];
216
217                 assert(elt->buf != NULL);
218                 rte_pktmbuf_free(elt->buf);
219 #ifndef NDEBUG
220                 /* Poisoning. */
221                 memset(elt, 0x77, sizeof(*elt));
222 #endif
223                 if (++elts_tail == elts_n)
224                         elts_tail = 0;
225         }
226         rte_free(elts);
227 }
228
229 /**
230  * Clean up a TX queue.
231  *
232  * Destroy objects, free allocated memory and reset the structure for reuse.
233  *
234  * @param txq
235  *   Pointer to TX queue structure.
236  */
237 static void
238 txq_cleanup(struct txq *txq)
239 {
240         size_t i;
241
242         DEBUG("cleaning up %p", (void *)txq);
243         txq_free_elts(txq);
244         if (txq->qp != NULL)
245                 claim_zero(ibv_destroy_qp(txq->qp));
246         if (txq->cq != NULL)
247                 claim_zero(ibv_destroy_cq(txq->cq));
248         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
249                 if (txq->mp2mr[i].mp == NULL)
250                         break;
251                 assert(txq->mp2mr[i].mr != NULL);
252                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
253         }
254         memset(txq, 0, sizeof(*txq));
255 }
256
257 struct mlx4_check_mempool_data {
258         int ret;
259         char *start;
260         char *end;
261 };
262
263 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
264 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
265         void *opaque, struct rte_mempool_memhdr *memhdr,
266         unsigned mem_idx)
267 {
268         struct mlx4_check_mempool_data *data = opaque;
269
270         (void)mp;
271         (void)mem_idx;
272         /* It already failed, skip the next chunks. */
273         if (data->ret != 0)
274                 return;
275         /* It is the first chunk. */
276         if (data->start == NULL && data->end == NULL) {
277                 data->start = memhdr->addr;
278                 data->end = data->start + memhdr->len;
279                 return;
280         }
281         if (data->end == memhdr->addr) {
282                 data->end += memhdr->len;
283                 return;
284         }
285         if (data->start == (char *)memhdr->addr + memhdr->len) {
286                 data->start -= memhdr->len;
287                 return;
288         }
289         /* Error, mempool is not virtually contigous. */
290         data->ret = -1;
291 }
292
293 /**
294  * Check if a mempool can be used: it must be virtually contiguous.
295  *
296  * @param[in] mp
297  *   Pointer to memory pool.
298  * @param[out] start
299  *   Pointer to the start address of the mempool virtual memory area
300  * @param[out] end
301  *   Pointer to the end address of the mempool virtual memory area
302  *
303  * @return
304  *   0 on success (mempool is virtually contiguous), -1 on error.
305  */
306 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
307         uintptr_t *end)
308 {
309         struct mlx4_check_mempool_data data;
310
311         memset(&data, 0, sizeof(data));
312         rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
313         *start = (uintptr_t)data.start;
314         *end = (uintptr_t)data.end;
315         return data.ret;
316 }
317
318 /**
319  * Register mempool as a memory region.
320  *
321  * @param pd
322  *   Pointer to protection domain.
323  * @param mp
324  *   Pointer to memory pool.
325  *
326  * @return
327  *   Memory region pointer, NULL in case of error and rte_errno is set.
328  */
329 struct ibv_mr *
330 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
331 {
332         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
333         uintptr_t start;
334         uintptr_t end;
335         unsigned int i;
336         struct ibv_mr *mr;
337
338         if (mlx4_check_mempool(mp, &start, &end) != 0) {
339                 rte_errno = EINVAL;
340                 ERROR("mempool %p: not virtually contiguous",
341                         (void *)mp);
342                 return NULL;
343         }
344         DEBUG("mempool %p area start=%p end=%p size=%zu",
345               (void *)mp, (void *)start, (void *)end,
346               (size_t)(end - start));
347         /* Round start and end to page boundary if found in memory segments. */
348         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
349                 uintptr_t addr = (uintptr_t)ms[i].addr;
350                 size_t len = ms[i].len;
351                 unsigned int align = ms[i].hugepage_sz;
352
353                 if ((start > addr) && (start < addr + len))
354                         start = RTE_ALIGN_FLOOR(start, align);
355                 if ((end > addr) && (end < addr + len))
356                         end = RTE_ALIGN_CEIL(end, align);
357         }
358         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
359               (void *)mp, (void *)start, (void *)end,
360               (size_t)(end - start));
361         mr = ibv_reg_mr(pd,
362                         (void *)start,
363                         end - start,
364                         IBV_ACCESS_LOCAL_WRITE);
365         if (!mr)
366                 rte_errno = errno ? errno : EINVAL;
367         return mr;
368 }
369
370 struct txq_mp2mr_mbuf_check_data {
371         int ret;
372 };
373
374 /**
375  * Callback function for rte_mempool_obj_iter() to check whether a given
376  * mempool object looks like a mbuf.
377  *
378  * @param[in] mp
379  *   The mempool pointer
380  * @param[in] arg
381  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
382  *   return value.
383  * @param[in] obj
384  *   Object address.
385  * @param index
386  *   Object index, unused.
387  */
388 static void
389 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
390         uint32_t index __rte_unused)
391 {
392         struct txq_mp2mr_mbuf_check_data *data = arg;
393         struct rte_mbuf *buf = obj;
394
395         /*
396          * Check whether mbuf structure fits element size and whether mempool
397          * pointer is valid.
398          */
399         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
400                 data->ret = -1;
401 }
402
403 /**
404  * Iterator function for rte_mempool_walk() to register existing mempools and
405  * fill the MP to MR cache of a TX queue.
406  *
407  * @param[in] mp
408  *   Memory Pool to register.
409  * @param *arg
410  *   Pointer to TX queue structure.
411  */
412 static void
413 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
414 {
415         struct txq *txq = arg;
416         struct txq_mp2mr_mbuf_check_data data = {
417                 .ret = 0,
418         };
419
420         /* Register mempool only if the first element looks like a mbuf. */
421         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
422                         data.ret == -1)
423                 return;
424         mlx4_txq_mp2mr(txq, mp);
425 }
426
427 /**
428  * Configure a TX queue.
429  *
430  * @param dev
431  *   Pointer to Ethernet device structure.
432  * @param txq
433  *   Pointer to TX queue structure.
434  * @param desc
435  *   Number of descriptors to configure in queue.
436  * @param socket
437  *   NUMA socket on which memory must be allocated.
438  * @param[in] conf
439  *   Thresholds parameters.
440  *
441  * @return
442  *   0 on success, negative errno value otherwise and rte_errno is set.
443  */
444 static int
445 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
446           unsigned int socket, const struct rte_eth_txconf *conf)
447 {
448         struct priv *priv = dev->data->dev_private;
449         struct txq tmpl = {
450                 .priv = priv,
451                 .socket = socket
452         };
453         union {
454                 struct ibv_qp_init_attr init;
455                 struct ibv_qp_attr mod;
456         } attr;
457         int ret;
458
459         (void)conf; /* Thresholds configuration (ignored). */
460         if (priv == NULL) {
461                 rte_errno = EINVAL;
462                 goto error;
463         }
464         if (desc == 0) {
465                 rte_errno = EINVAL;
466                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
467                 goto error;
468         }
469         /* MRs will be registered in mp2mr[] later. */
470         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
471         if (tmpl.cq == NULL) {
472                 rte_errno = ENOMEM;
473                 ERROR("%p: CQ creation failure: %s",
474                       (void *)dev, strerror(rte_errno));
475                 goto error;
476         }
477         DEBUG("priv->device_attr.max_qp_wr is %d",
478               priv->device_attr.max_qp_wr);
479         DEBUG("priv->device_attr.max_sge is %d",
480               priv->device_attr.max_sge);
481         attr.init = (struct ibv_qp_init_attr){
482                 /* CQ to be associated with the send queue. */
483                 .send_cq = tmpl.cq,
484                 /* CQ to be associated with the receive queue. */
485                 .recv_cq = tmpl.cq,
486                 .cap = {
487                         /* Max number of outstanding WRs. */
488                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
489                                         priv->device_attr.max_qp_wr :
490                                         desc),
491                         /* Max number of scatter/gather elements in a WR. */
492                         .max_send_sge = 1,
493                         .max_inline_data = MLX4_PMD_MAX_INLINE,
494                 },
495                 .qp_type = IBV_QPT_RAW_PACKET,
496                 /*
497                  * Do *NOT* enable this, completions events are managed per
498                  * Tx burst.
499                  */
500                 .sq_sig_all = 0,
501         };
502         tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
503         if (tmpl.qp == NULL) {
504                 rte_errno = errno ? errno : EINVAL;
505                 ERROR("%p: QP creation failure: %s",
506                       (void *)dev, strerror(rte_errno));
507                 goto error;
508         }
509         /* ibv_create_qp() updates this value. */
510         tmpl.max_inline = attr.init.cap.max_inline_data;
511         attr.mod = (struct ibv_qp_attr){
512                 /* Move the QP to this state. */
513                 .qp_state = IBV_QPS_INIT,
514                 /* Primary port number. */
515                 .port_num = priv->port
516         };
517         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
518         if (ret) {
519                 rte_errno = ret;
520                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
521                       (void *)dev, strerror(rte_errno));
522                 goto error;
523         }
524         ret = txq_alloc_elts(&tmpl, desc);
525         if (ret) {
526                 rte_errno = ret;
527                 ERROR("%p: TXQ allocation failed: %s",
528                       (void *)dev, strerror(rte_errno));
529                 goto error;
530         }
531         attr.mod = (struct ibv_qp_attr){
532                 .qp_state = IBV_QPS_RTR
533         };
534         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
535         if (ret) {
536                 rte_errno = ret;
537                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
538                       (void *)dev, strerror(rte_errno));
539                 goto error;
540         }
541         attr.mod.qp_state = IBV_QPS_RTS;
542         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
543         if (ret) {
544                 rte_errno = ret;
545                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
546                       (void *)dev, strerror(rte_errno));
547                 goto error;
548         }
549         /* Clean up txq in case we're reinitializing it. */
550         DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
551         txq_cleanup(txq);
552         *txq = tmpl;
553         DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
554         /* Pre-register known mempools. */
555         rte_mempool_walk(txq_mp2mr_iter, txq);
556         return 0;
557 error:
558         ret = rte_errno;
559         txq_cleanup(&tmpl);
560         rte_errno = ret;
561         assert(rte_errno > 0);
562         return -rte_errno;
563 }
564
565 /**
566  * DPDK callback to configure a TX queue.
567  *
568  * @param dev
569  *   Pointer to Ethernet device structure.
570  * @param idx
571  *   TX queue index.
572  * @param desc
573  *   Number of descriptors to configure in queue.
574  * @param socket
575  *   NUMA socket on which memory must be allocated.
576  * @param[in] conf
577  *   Thresholds parameters.
578  *
579  * @return
580  *   0 on success, negative errno value otherwise and rte_errno is set.
581  */
582 static int
583 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
584                     unsigned int socket, const struct rte_eth_txconf *conf)
585 {
586         struct priv *priv = dev->data->dev_private;
587         struct txq *txq = (*priv->txqs)[idx];
588         int ret;
589
590         DEBUG("%p: configuring queue %u for %u descriptors",
591               (void *)dev, idx, desc);
592         if (idx >= priv->txqs_n) {
593                 rte_errno = EOVERFLOW;
594                 ERROR("%p: queue index out of range (%u >= %u)",
595                       (void *)dev, idx, priv->txqs_n);
596                 return -rte_errno;
597         }
598         if (txq != NULL) {
599                 DEBUG("%p: reusing already allocated queue index %u (%p)",
600                       (void *)dev, idx, (void *)txq);
601                 if (priv->started) {
602                         rte_errno = EEXIST;
603                         return -rte_errno;
604                 }
605                 (*priv->txqs)[idx] = NULL;
606                 txq_cleanup(txq);
607         } else {
608                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
609                 if (txq == NULL) {
610                         rte_errno = ENOMEM;
611                         ERROR("%p: unable to allocate queue index %u",
612                               (void *)dev, idx);
613                         return -rte_errno;
614                 }
615         }
616         ret = txq_setup(dev, txq, desc, socket, conf);
617         if (ret)
618                 rte_free(txq);
619         else {
620                 txq->stats.idx = idx;
621                 DEBUG("%p: adding TX queue %p to list",
622                       (void *)dev, (void *)txq);
623                 (*priv->txqs)[idx] = txq;
624                 /* Update send callback. */
625                 dev->tx_pkt_burst = mlx4_tx_burst;
626         }
627         return ret;
628 }
629
630 /**
631  * DPDK callback to release a TX queue.
632  *
633  * @param dpdk_txq
634  *   Generic TX queue pointer.
635  */
636 static void
637 mlx4_tx_queue_release(void *dpdk_txq)
638 {
639         struct txq *txq = (struct txq *)dpdk_txq;
640         struct priv *priv;
641         unsigned int i;
642
643         if (txq == NULL)
644                 return;
645         priv = txq->priv;
646         for (i = 0; (i != priv->txqs_n); ++i)
647                 if ((*priv->txqs)[i] == txq) {
648                         DEBUG("%p: removing TX queue %p from list",
649                               (void *)priv->dev, (void *)txq);
650                         (*priv->txqs)[i] = NULL;
651                         break;
652                 }
653         txq_cleanup(txq);
654         rte_free(txq);
655 }
656
657 /* RX queues handling. */
658
659 /**
660  * Allocate RX queue elements.
661  *
662  * @param rxq
663  *   Pointer to RX queue structure.
664  * @param elts_n
665  *   Number of elements to allocate.
666  *
667  * @return
668  *   0 on success, negative errno value otherwise and rte_errno is set.
669  */
670 static int
671 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
672 {
673         unsigned int i;
674         struct rxq_elt (*elts)[elts_n] =
675                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
676                                   rxq->socket);
677
678         if (elts == NULL) {
679                 rte_errno = ENOMEM;
680                 ERROR("%p: can't allocate packets array", (void *)rxq);
681                 goto error;
682         }
683         /* For each WR (packet). */
684         for (i = 0; (i != elts_n); ++i) {
685                 struct rxq_elt *elt = &(*elts)[i];
686                 struct ibv_recv_wr *wr = &elt->wr;
687                 struct ibv_sge *sge = &(*elts)[i].sge;
688                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
689
690                 if (buf == NULL) {
691                         rte_errno = ENOMEM;
692                         ERROR("%p: empty mbuf pool", (void *)rxq);
693                         goto error;
694                 }
695                 elt->buf = buf;
696                 wr->next = &(*elts)[(i + 1)].wr;
697                 wr->sg_list = sge;
698                 wr->num_sge = 1;
699                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
700                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
701                 /* Buffer is supposed to be empty. */
702                 assert(rte_pktmbuf_data_len(buf) == 0);
703                 assert(rte_pktmbuf_pkt_len(buf) == 0);
704                 /* sge->addr must be able to store a pointer. */
705                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
706                 /* SGE keeps its headroom. */
707                 sge->addr = (uintptr_t)
708                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
709                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
710                 sge->lkey = rxq->mr->lkey;
711                 /* Redundant check for tailroom. */
712                 assert(sge->length == rte_pktmbuf_tailroom(buf));
713         }
714         /* The last WR pointer must be NULL. */
715         (*elts)[(i - 1)].wr.next = NULL;
716         DEBUG("%p: allocated and configured %u single-segment WRs",
717               (void *)rxq, elts_n);
718         rxq->elts_n = elts_n;
719         rxq->elts_head = 0;
720         rxq->elts = elts;
721         return 0;
722 error:
723         if (elts != NULL) {
724                 for (i = 0; (i != RTE_DIM(*elts)); ++i)
725                         rte_pktmbuf_free_seg((*elts)[i].buf);
726                 rte_free(elts);
727         }
728         DEBUG("%p: failed, freed everything", (void *)rxq);
729         assert(rte_errno > 0);
730         return -rte_errno;
731 }
732
733 /**
734  * Free RX queue elements.
735  *
736  * @param rxq
737  *   Pointer to RX queue structure.
738  */
739 static void
740 rxq_free_elts(struct rxq *rxq)
741 {
742         unsigned int i;
743         unsigned int elts_n = rxq->elts_n;
744         struct rxq_elt (*elts)[elts_n] = rxq->elts;
745
746         DEBUG("%p: freeing WRs", (void *)rxq);
747         rxq->elts_n = 0;
748         rxq->elts = NULL;
749         if (elts == NULL)
750                 return;
751         for (i = 0; (i != RTE_DIM(*elts)); ++i)
752                 rte_pktmbuf_free_seg((*elts)[i].buf);
753         rte_free(elts);
754 }
755
756 /**
757  * Unregister a MAC address.
758  *
759  * @param priv
760  *   Pointer to private structure.
761  */
762 static void
763 priv_mac_addr_del(struct priv *priv)
764 {
765 #ifndef NDEBUG
766         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
767 #endif
768
769         if (!priv->mac_flow)
770                 return;
771         DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
772               (void *)priv,
773               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
774         claim_zero(ibv_destroy_flow(priv->mac_flow));
775         priv->mac_flow = NULL;
776 }
777
778 /**
779  * Register a MAC address.
780  *
781  * The MAC address is registered in queue 0.
782  *
783  * @param priv
784  *   Pointer to private structure.
785  *
786  * @return
787  *   0 on success, negative errno value otherwise and rte_errno is set.
788  */
789 static int
790 priv_mac_addr_add(struct priv *priv)
791 {
792         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
793         struct rxq *rxq;
794         struct ibv_flow *flow;
795
796         /* If device isn't started, this is all we need to do. */
797         if (!priv->started)
798                 return 0;
799         if (priv->isolated)
800                 return 0;
801         if (*priv->rxqs && (*priv->rxqs)[0])
802                 rxq = (*priv->rxqs)[0];
803         else
804                 return 0;
805
806         /* Allocate flow specification on the stack. */
807         struct __attribute__((packed)) {
808                 struct ibv_flow_attr attr;
809                 struct ibv_flow_spec_eth spec;
810         } data;
811         struct ibv_flow_attr *attr = &data.attr;
812         struct ibv_flow_spec_eth *spec = &data.spec;
813
814         if (priv->mac_flow)
815                 priv_mac_addr_del(priv);
816         /*
817          * No padding must be inserted by the compiler between attr and spec.
818          * This layout is expected by libibverbs.
819          */
820         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
821         *attr = (struct ibv_flow_attr){
822                 .type = IBV_FLOW_ATTR_NORMAL,
823                 .priority = 3,
824                 .num_of_specs = 1,
825                 .port = priv->port,
826                 .flags = 0
827         };
828         *spec = (struct ibv_flow_spec_eth){
829                 .type = IBV_FLOW_SPEC_ETH,
830                 .size = sizeof(*spec),
831                 .val = {
832                         .dst_mac = {
833                                 (*mac)[0], (*mac)[1], (*mac)[2],
834                                 (*mac)[3], (*mac)[4], (*mac)[5]
835                         },
836                 },
837                 .mask = {
838                         .dst_mac = "\xff\xff\xff\xff\xff\xff",
839                 }
840         };
841         DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
842               (void *)priv,
843               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
844         /* Create related flow. */
845         flow = ibv_create_flow(rxq->qp, attr);
846         if (flow == NULL) {
847                 rte_errno = errno ? errno : EINVAL;
848                 ERROR("%p: flow configuration failed, errno=%d: %s",
849                       (void *)rxq, rte_errno, strerror(errno));
850                 return -rte_errno;
851         }
852         assert(priv->mac_flow == NULL);
853         priv->mac_flow = flow;
854         return 0;
855 }
856
857 /**
858  * Clean up a RX queue.
859  *
860  * Destroy objects, free allocated memory and reset the structure for reuse.
861  *
862  * @param rxq
863  *   Pointer to RX queue structure.
864  */
865 static void
866 rxq_cleanup(struct rxq *rxq)
867 {
868         DEBUG("cleaning up %p", (void *)rxq);
869         rxq_free_elts(rxq);
870         if (rxq->qp != NULL)
871                 claim_zero(ibv_destroy_qp(rxq->qp));
872         if (rxq->cq != NULL)
873                 claim_zero(ibv_destroy_cq(rxq->cq));
874         if (rxq->channel != NULL)
875                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
876         if (rxq->mr != NULL)
877                 claim_zero(ibv_dereg_mr(rxq->mr));
878         memset(rxq, 0, sizeof(*rxq));
879 }
880
881 /**
882  * Allocate a Queue Pair.
883  * Optionally setup inline receive if supported.
884  *
885  * @param priv
886  *   Pointer to private structure.
887  * @param cq
888  *   Completion queue to associate with QP.
889  * @param desc
890  *   Number of descriptors in QP (hint only).
891  *
892  * @return
893  *   QP pointer or NULL in case of error and rte_errno is set.
894  */
895 static struct ibv_qp *
896 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
897 {
898         struct ibv_qp *qp;
899         struct ibv_qp_init_attr attr = {
900                 /* CQ to be associated with the send queue. */
901                 .send_cq = cq,
902                 /* CQ to be associated with the receive queue. */
903                 .recv_cq = cq,
904                 .cap = {
905                         /* Max number of outstanding WRs. */
906                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
907                                         priv->device_attr.max_qp_wr :
908                                         desc),
909                         /* Max number of scatter/gather elements in a WR. */
910                         .max_recv_sge = 1,
911                 },
912                 .qp_type = IBV_QPT_RAW_PACKET,
913         };
914
915         qp = ibv_create_qp(priv->pd, &attr);
916         if (!qp)
917                 rte_errno = errno ? errno : EINVAL;
918         return qp;
919 }
920
921 /**
922  * Configure a RX queue.
923  *
924  * @param dev
925  *   Pointer to Ethernet device structure.
926  * @param rxq
927  *   Pointer to RX queue structure.
928  * @param desc
929  *   Number of descriptors to configure in queue.
930  * @param socket
931  *   NUMA socket on which memory must be allocated.
932  * @param[in] conf
933  *   Thresholds parameters.
934  * @param mp
935  *   Memory pool for buffer allocations.
936  *
937  * @return
938  *   0 on success, negative errno value otherwise and rte_errno is set.
939  */
940 static int
941 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
942           unsigned int socket, const struct rte_eth_rxconf *conf,
943           struct rte_mempool *mp)
944 {
945         struct priv *priv = dev->data->dev_private;
946         struct rxq tmpl = {
947                 .priv = priv,
948                 .mp = mp,
949                 .socket = socket
950         };
951         struct ibv_qp_attr mod;
952         struct ibv_recv_wr *bad_wr;
953         unsigned int mb_len;
954         int ret;
955
956         (void)conf; /* Thresholds configuration (ignored). */
957         mb_len = rte_pktmbuf_data_room_size(mp);
958         if (desc == 0) {
959                 rte_errno = EINVAL;
960                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
961                 goto error;
962         }
963         /* Enable scattered packets support for this queue if necessary. */
964         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
965         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
966             (mb_len - RTE_PKTMBUF_HEADROOM)) {
967                 ;
968         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
969                 WARN("%p: scattered mode has been requested but is"
970                      " not supported, this may lead to packet loss",
971                      (void *)dev);
972         } else {
973                 WARN("%p: the requested maximum Rx packet size (%u) is"
974                      " larger than a single mbuf (%u) and scattered"
975                      " mode has not been requested",
976                      (void *)dev,
977                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
978                      mb_len - RTE_PKTMBUF_HEADROOM);
979         }
980         /* Use the entire RX mempool as the memory region. */
981         tmpl.mr = mlx4_mp2mr(priv->pd, mp);
982         if (tmpl.mr == NULL) {
983                 rte_errno = EINVAL;
984                 ERROR("%p: MR creation failure: %s",
985                       (void *)dev, strerror(rte_errno));
986                 goto error;
987         }
988         if (dev->data->dev_conf.intr_conf.rxq) {
989                 tmpl.channel = ibv_create_comp_channel(priv->ctx);
990                 if (tmpl.channel == NULL) {
991                         rte_errno = ENOMEM;
992                         ERROR("%p: Rx interrupt completion channel creation"
993                               " failure: %s",
994                               (void *)dev, strerror(rte_errno));
995                         goto error;
996                 }
997                 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
998                         ERROR("%p: unable to make Rx interrupt completion"
999                               " channel non-blocking: %s",
1000                               (void *)dev, strerror(rte_errno));
1001                         goto error;
1002                 }
1003         }
1004         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1005         if (tmpl.cq == NULL) {
1006                 rte_errno = ENOMEM;
1007                 ERROR("%p: CQ creation failure: %s",
1008                       (void *)dev, strerror(rte_errno));
1009                 goto error;
1010         }
1011         DEBUG("priv->device_attr.max_qp_wr is %d",
1012               priv->device_attr.max_qp_wr);
1013         DEBUG("priv->device_attr.max_sge is %d",
1014               priv->device_attr.max_sge);
1015         tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1016         if (tmpl.qp == NULL) {
1017                 ERROR("%p: QP creation failure: %s",
1018                       (void *)dev, strerror(rte_errno));
1019                 goto error;
1020         }
1021         mod = (struct ibv_qp_attr){
1022                 /* Move the QP to this state. */
1023                 .qp_state = IBV_QPS_INIT,
1024                 /* Primary port number. */
1025                 .port_num = priv->port
1026         };
1027         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1028         if (ret) {
1029                 rte_errno = ret;
1030                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1031                       (void *)dev, strerror(rte_errno));
1032                 goto error;
1033         }
1034         ret = rxq_alloc_elts(&tmpl, desc);
1035         if (ret) {
1036                 ERROR("%p: RXQ allocation failed: %s",
1037                       (void *)dev, strerror(rte_errno));
1038                 goto error;
1039         }
1040         ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1041         if (ret) {
1042                 rte_errno = ret;
1043                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1044                       (void *)dev,
1045                       (void *)bad_wr,
1046                       strerror(rte_errno));
1047                 goto error;
1048         }
1049         mod = (struct ibv_qp_attr){
1050                 .qp_state = IBV_QPS_RTR
1051         };
1052         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1053         if (ret) {
1054                 rte_errno = ret;
1055                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1056                       (void *)dev, strerror(rte_errno));
1057                 goto error;
1058         }
1059         /* Save port ID. */
1060         tmpl.port_id = dev->data->port_id;
1061         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1062         /* Clean up rxq in case we're reinitializing it. */
1063         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1064         rxq_cleanup(rxq);
1065         *rxq = tmpl;
1066         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1067         return 0;
1068 error:
1069         ret = rte_errno;
1070         rxq_cleanup(&tmpl);
1071         rte_errno = ret;
1072         assert(rte_errno > 0);
1073         return -rte_errno;
1074 }
1075
1076 /**
1077  * DPDK callback to configure a RX queue.
1078  *
1079  * @param dev
1080  *   Pointer to Ethernet device structure.
1081  * @param idx
1082  *   RX queue index.
1083  * @param desc
1084  *   Number of descriptors to configure in queue.
1085  * @param socket
1086  *   NUMA socket on which memory must be allocated.
1087  * @param[in] conf
1088  *   Thresholds parameters.
1089  * @param mp
1090  *   Memory pool for buffer allocations.
1091  *
1092  * @return
1093  *   0 on success, negative errno value otherwise and rte_errno is set.
1094  */
1095 static int
1096 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1097                     unsigned int socket, const struct rte_eth_rxconf *conf,
1098                     struct rte_mempool *mp)
1099 {
1100         struct priv *priv = dev->data->dev_private;
1101         struct rxq *rxq = (*priv->rxqs)[idx];
1102         int ret;
1103
1104         DEBUG("%p: configuring queue %u for %u descriptors",
1105               (void *)dev, idx, desc);
1106         if (idx >= priv->rxqs_n) {
1107                 rte_errno = EOVERFLOW;
1108                 ERROR("%p: queue index out of range (%u >= %u)",
1109                       (void *)dev, idx, priv->rxqs_n);
1110                 return -rte_errno;
1111         }
1112         if (rxq != NULL) {
1113                 DEBUG("%p: reusing already allocated queue index %u (%p)",
1114                       (void *)dev, idx, (void *)rxq);
1115                 if (priv->started) {
1116                         rte_errno = EEXIST;
1117                         return -rte_errno;
1118                 }
1119                 (*priv->rxqs)[idx] = NULL;
1120                 if (idx == 0)
1121                         priv_mac_addr_del(priv);
1122                 rxq_cleanup(rxq);
1123         } else {
1124                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
1125                 if (rxq == NULL) {
1126                         rte_errno = ENOMEM;
1127                         ERROR("%p: unable to allocate queue index %u",
1128                               (void *)dev, idx);
1129                         return -rte_errno;
1130                 }
1131         }
1132         ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
1133         if (ret)
1134                 rte_free(rxq);
1135         else {
1136                 rxq->stats.idx = idx;
1137                 DEBUG("%p: adding RX queue %p to list",
1138                       (void *)dev, (void *)rxq);
1139                 (*priv->rxqs)[idx] = rxq;
1140                 /* Update receive callback. */
1141                 dev->rx_pkt_burst = mlx4_rx_burst;
1142         }
1143         return ret;
1144 }
1145
1146 /**
1147  * DPDK callback to release a RX queue.
1148  *
1149  * @param dpdk_rxq
1150  *   Generic RX queue pointer.
1151  */
1152 static void
1153 mlx4_rx_queue_release(void *dpdk_rxq)
1154 {
1155         struct rxq *rxq = (struct rxq *)dpdk_rxq;
1156         struct priv *priv;
1157         unsigned int i;
1158
1159         if (rxq == NULL)
1160                 return;
1161         priv = rxq->priv;
1162         for (i = 0; (i != priv->rxqs_n); ++i)
1163                 if ((*priv->rxqs)[i] == rxq) {
1164                         DEBUG("%p: removing RX queue %p from list",
1165                               (void *)priv->dev, (void *)rxq);
1166                         (*priv->rxqs)[i] = NULL;
1167                         if (i == 0)
1168                                 priv_mac_addr_del(priv);
1169                         break;
1170                 }
1171         rxq_cleanup(rxq);
1172         rte_free(rxq);
1173 }
1174
1175 /**
1176  * DPDK callback to start the device.
1177  *
1178  * Simulate device start by attaching all configured flows.
1179  *
1180  * @param dev
1181  *   Pointer to Ethernet device structure.
1182  *
1183  * @return
1184  *   0 on success, negative errno value otherwise and rte_errno is set.
1185  */
1186 static int
1187 mlx4_dev_start(struct rte_eth_dev *dev)
1188 {
1189         struct priv *priv = dev->data->dev_private;
1190         int ret;
1191
1192         if (priv->started)
1193                 return 0;
1194         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
1195         priv->started = 1;
1196         ret = priv_mac_addr_add(priv);
1197         if (ret)
1198                 goto err;
1199         ret = mlx4_intr_install(priv);
1200         if (ret) {
1201                 ERROR("%p: interrupt handler installation failed",
1202                      (void *)dev);
1203                 goto err;
1204         }
1205         ret = mlx4_priv_flow_start(priv);
1206         if (ret) {
1207                 ERROR("%p: flow start failed: %s",
1208                       (void *)dev, strerror(ret));
1209                 goto err;
1210         }
1211         return 0;
1212 err:
1213         /* Rollback. */
1214         priv_mac_addr_del(priv);
1215         priv->started = 0;
1216         return ret;
1217 }
1218
1219 /**
1220  * DPDK callback to stop the device.
1221  *
1222  * Simulate device stop by detaching all configured flows.
1223  *
1224  * @param dev
1225  *   Pointer to Ethernet device structure.
1226  */
1227 static void
1228 mlx4_dev_stop(struct rte_eth_dev *dev)
1229 {
1230         struct priv *priv = dev->data->dev_private;
1231
1232         if (!priv->started)
1233                 return;
1234         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
1235         priv->started = 0;
1236         mlx4_priv_flow_stop(priv);
1237         mlx4_intr_uninstall(priv);
1238         priv_mac_addr_del(priv);
1239 }
1240
1241 /**
1242  * DPDK callback to close the device.
1243  *
1244  * Destroy all queues and objects, free memory.
1245  *
1246  * @param dev
1247  *   Pointer to Ethernet device structure.
1248  */
1249 static void
1250 mlx4_dev_close(struct rte_eth_dev *dev)
1251 {
1252         struct priv *priv = dev->data->dev_private;
1253         void *tmp;
1254         unsigned int i;
1255
1256         if (priv == NULL)
1257                 return;
1258         DEBUG("%p: closing device \"%s\"",
1259               (void *)dev,
1260               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
1261         priv_mac_addr_del(priv);
1262         /*
1263          * Prevent crashes when queues are still in use. This is unfortunately
1264          * still required for DPDK 1.3 because some programs (such as testpmd)
1265          * never release them before closing the device.
1266          */
1267         dev->rx_pkt_burst = mlx4_rx_burst_removed;
1268         dev->tx_pkt_burst = mlx4_tx_burst_removed;
1269         if (priv->rxqs != NULL) {
1270                 /* XXX race condition if mlx4_rx_burst() is still running. */
1271                 usleep(1000);
1272                 for (i = 0; (i != priv->rxqs_n); ++i) {
1273                         tmp = (*priv->rxqs)[i];
1274                         if (tmp == NULL)
1275                                 continue;
1276                         (*priv->rxqs)[i] = NULL;
1277                         rxq_cleanup(tmp);
1278                         rte_free(tmp);
1279                 }
1280                 priv->rxqs_n = 0;
1281                 priv->rxqs = NULL;
1282         }
1283         if (priv->txqs != NULL) {
1284                 /* XXX race condition if mlx4_tx_burst() is still running. */
1285                 usleep(1000);
1286                 for (i = 0; (i != priv->txqs_n); ++i) {
1287                         tmp = (*priv->txqs)[i];
1288                         if (tmp == NULL)
1289                                 continue;
1290                         (*priv->txqs)[i] = NULL;
1291                         txq_cleanup(tmp);
1292                         rte_free(tmp);
1293                 }
1294                 priv->txqs_n = 0;
1295                 priv->txqs = NULL;
1296         }
1297         if (priv->pd != NULL) {
1298                 assert(priv->ctx != NULL);
1299                 claim_zero(ibv_dealloc_pd(priv->pd));
1300                 claim_zero(ibv_close_device(priv->ctx));
1301         } else
1302                 assert(priv->ctx == NULL);
1303         mlx4_intr_uninstall(priv);
1304         memset(priv, 0, sizeof(*priv));
1305 }
1306
1307 const struct rte_flow_ops mlx4_flow_ops = {
1308         .validate = mlx4_flow_validate,
1309         .create = mlx4_flow_create,
1310         .destroy = mlx4_flow_destroy,
1311         .flush = mlx4_flow_flush,
1312         .query = NULL,
1313         .isolate = mlx4_flow_isolate,
1314 };
1315
1316 /**
1317  * Manage filter operations.
1318  *
1319  * @param dev
1320  *   Pointer to Ethernet device structure.
1321  * @param filter_type
1322  *   Filter type.
1323  * @param filter_op
1324  *   Operation to perform.
1325  * @param arg
1326  *   Pointer to operation-specific structure.
1327  *
1328  * @return
1329  *   0 on success, negative errno value otherwise and rte_errno is set.
1330  */
1331 static int
1332 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
1333                      enum rte_filter_type filter_type,
1334                      enum rte_filter_op filter_op,
1335                      void *arg)
1336 {
1337         switch (filter_type) {
1338         case RTE_ETH_FILTER_GENERIC:
1339                 if (filter_op != RTE_ETH_FILTER_GET)
1340                         break;
1341                 *(const void **)arg = &mlx4_flow_ops;
1342                 return 0;
1343         default:
1344                 ERROR("%p: filter type (%d) not supported",
1345                       (void *)dev, filter_type);
1346                 break;
1347         }
1348         rte_errno = ENOTSUP;
1349         return -rte_errno;
1350 }
1351
1352 static const struct eth_dev_ops mlx4_dev_ops = {
1353         .dev_configure = mlx4_dev_configure,
1354         .dev_start = mlx4_dev_start,
1355         .dev_stop = mlx4_dev_stop,
1356         .dev_set_link_down = mlx4_dev_set_link_down,
1357         .dev_set_link_up = mlx4_dev_set_link_up,
1358         .dev_close = mlx4_dev_close,
1359         .link_update = mlx4_link_update,
1360         .stats_get = mlx4_stats_get,
1361         .stats_reset = mlx4_stats_reset,
1362         .dev_infos_get = mlx4_dev_infos_get,
1363         .rx_queue_setup = mlx4_rx_queue_setup,
1364         .tx_queue_setup = mlx4_tx_queue_setup,
1365         .rx_queue_release = mlx4_rx_queue_release,
1366         .tx_queue_release = mlx4_tx_queue_release,
1367         .flow_ctrl_get = mlx4_flow_ctrl_get,
1368         .flow_ctrl_set = mlx4_flow_ctrl_set,
1369         .mtu_set = mlx4_mtu_set,
1370         .filter_ctrl = mlx4_dev_filter_ctrl,
1371         .rx_queue_intr_enable = mlx4_rx_intr_enable,
1372         .rx_queue_intr_disable = mlx4_rx_intr_disable,
1373 };
1374
1375 /**
1376  * Get PCI information from struct ibv_device.
1377  *
1378  * @param device
1379  *   Pointer to Ethernet device structure.
1380  * @param[out] pci_addr
1381  *   PCI bus address output buffer.
1382  *
1383  * @return
1384  *   0 on success, negative errno value otherwise and rte_errno is set.
1385  */
1386 static int
1387 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
1388                             struct rte_pci_addr *pci_addr)
1389 {
1390         FILE *file;
1391         char line[32];
1392         MKSTR(path, "%s/device/uevent", device->ibdev_path);
1393
1394         file = fopen(path, "rb");
1395         if (file == NULL) {
1396                 rte_errno = errno;
1397                 return -rte_errno;
1398         }
1399         while (fgets(line, sizeof(line), file) == line) {
1400                 size_t len = strlen(line);
1401                 int ret;
1402
1403                 /* Truncate long lines. */
1404                 if (len == (sizeof(line) - 1))
1405                         while (line[(len - 1)] != '\n') {
1406                                 ret = fgetc(file);
1407                                 if (ret == EOF)
1408                                         break;
1409                                 line[(len - 1)] = ret;
1410                         }
1411                 /* Extract information. */
1412                 if (sscanf(line,
1413                            "PCI_SLOT_NAME="
1414                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1415                            &pci_addr->domain,
1416                            &pci_addr->bus,
1417                            &pci_addr->devid,
1418                            &pci_addr->function) == 4) {
1419                         ret = 0;
1420                         break;
1421                 }
1422         }
1423         fclose(file);
1424         return 0;
1425 }
1426
1427 /**
1428  * Verify and store value for device argument.
1429  *
1430  * @param[in] key
1431  *   Key argument to verify.
1432  * @param[in] val
1433  *   Value associated with key.
1434  * @param[in, out] conf
1435  *   Shared configuration data.
1436  *
1437  * @return
1438  *   0 on success, negative errno value otherwise and rte_errno is set.
1439  */
1440 static int
1441 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
1442 {
1443         unsigned long tmp;
1444
1445         errno = 0;
1446         tmp = strtoul(val, NULL, 0);
1447         if (errno) {
1448                 rte_errno = errno;
1449                 WARN("%s: \"%s\" is not a valid integer", key, val);
1450                 return -rte_errno;
1451         }
1452         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
1453                 uint32_t ports = rte_log2_u32(conf->ports.present);
1454
1455                 if (tmp >= ports) {
1456                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
1457                               tmp, ports);
1458                         return -EINVAL;
1459                 }
1460                 if (!(conf->ports.present & (1 << tmp))) {
1461                         rte_errno = EINVAL;
1462                         ERROR("invalid port index %lu", tmp);
1463                         return -rte_errno;
1464                 }
1465                 conf->ports.enabled |= 1 << tmp;
1466         } else {
1467                 rte_errno = EINVAL;
1468                 WARN("%s: unknown parameter", key);
1469                 return -rte_errno;
1470         }
1471         return 0;
1472 }
1473
1474 /**
1475  * Parse device parameters.
1476  *
1477  * @param devargs
1478  *   Device arguments structure.
1479  *
1480  * @return
1481  *   0 on success, negative errno value otherwise and rte_errno is set.
1482  */
1483 static int
1484 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
1485 {
1486         struct rte_kvargs *kvlist;
1487         unsigned int arg_count;
1488         int ret = 0;
1489         int i;
1490
1491         if (devargs == NULL)
1492                 return 0;
1493         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
1494         if (kvlist == NULL) {
1495                 rte_errno = EINVAL;
1496                 ERROR("failed to parse kvargs");
1497                 return -rte_errno;
1498         }
1499         /* Process parameters. */
1500         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
1501                 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
1502                 while (arg_count-- > 0) {
1503                         ret = rte_kvargs_process(kvlist,
1504                                                  MLX4_PMD_PORT_KVARG,
1505                                                  (int (*)(const char *,
1506                                                           const char *,
1507                                                           void *))
1508                                                  mlx4_arg_parse,
1509                                                  conf);
1510                         if (ret != 0)
1511                                 goto free_kvlist;
1512                 }
1513         }
1514 free_kvlist:
1515         rte_kvargs_free(kvlist);
1516         return ret;
1517 }
1518
1519 static struct rte_pci_driver mlx4_driver;
1520
1521 /**
1522  * DPDK callback to register a PCI device.
1523  *
1524  * This function creates an Ethernet device for each port of a given
1525  * PCI device.
1526  *
1527  * @param[in] pci_drv
1528  *   PCI driver structure (mlx4_driver).
1529  * @param[in] pci_dev
1530  *   PCI device information.
1531  *
1532  * @return
1533  *   0 on success, negative errno value otherwise and rte_errno is set.
1534  */
1535 static int
1536 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
1537 {
1538         struct ibv_device **list;
1539         struct ibv_device *ibv_dev;
1540         int err = 0;
1541         struct ibv_context *attr_ctx = NULL;
1542         struct ibv_device_attr device_attr;
1543         struct mlx4_conf conf = {
1544                 .ports.present = 0,
1545         };
1546         unsigned int vf;
1547         int i;
1548
1549         (void)pci_drv;
1550         assert(pci_drv == &mlx4_driver);
1551         list = ibv_get_device_list(&i);
1552         if (list == NULL) {
1553                 rte_errno = errno;
1554                 assert(rte_errno);
1555                 if (rte_errno == ENOSYS)
1556                         ERROR("cannot list devices, is ib_uverbs loaded?");
1557                 return -rte_errno;
1558         }
1559         assert(i >= 0);
1560         /*
1561          * For each listed device, check related sysfs entry against
1562          * the provided PCI ID.
1563          */
1564         while (i != 0) {
1565                 struct rte_pci_addr pci_addr;
1566
1567                 --i;
1568                 DEBUG("checking device \"%s\"", list[i]->name);
1569                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
1570                         continue;
1571                 if ((pci_dev->addr.domain != pci_addr.domain) ||
1572                     (pci_dev->addr.bus != pci_addr.bus) ||
1573                     (pci_dev->addr.devid != pci_addr.devid) ||
1574                     (pci_dev->addr.function != pci_addr.function))
1575                         continue;
1576                 vf = (pci_dev->id.device_id ==
1577                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
1578                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
1579                      list[i]->name, (vf ? "true" : "false"));
1580                 attr_ctx = ibv_open_device(list[i]);
1581                 err = errno;
1582                 break;
1583         }
1584         if (attr_ctx == NULL) {
1585                 ibv_free_device_list(list);
1586                 switch (err) {
1587                 case 0:
1588                         rte_errno = ENODEV;
1589                         ERROR("cannot access device, is mlx4_ib loaded?");
1590                         return -rte_errno;
1591                 case EINVAL:
1592                         rte_errno = EINVAL;
1593                         ERROR("cannot use device, are drivers up to date?");
1594                         return -rte_errno;
1595                 }
1596                 assert(err > 0);
1597                 rte_errno = err;
1598                 return -rte_errno;
1599         }
1600         ibv_dev = list[i];
1601         DEBUG("device opened");
1602         if (ibv_query_device(attr_ctx, &device_attr)) {
1603                 rte_errno = ENODEV;
1604                 goto error;
1605         }
1606         INFO("%u port(s) detected", device_attr.phys_port_cnt);
1607         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
1608         if (mlx4_args(pci_dev->device.devargs, &conf)) {
1609                 ERROR("failed to process device arguments");
1610                 rte_errno = EINVAL;
1611                 goto error;
1612         }
1613         /* Use all ports when none are defined */
1614         if (!conf.ports.enabled)
1615                 conf.ports.enabled = conf.ports.present;
1616         for (i = 0; i < device_attr.phys_port_cnt; i++) {
1617                 uint32_t port = i + 1; /* ports are indexed from one */
1618                 struct ibv_context *ctx = NULL;
1619                 struct ibv_port_attr port_attr;
1620                 struct ibv_pd *pd = NULL;
1621                 struct priv *priv = NULL;
1622                 struct rte_eth_dev *eth_dev = NULL;
1623                 struct ether_addr mac;
1624
1625                 /* If port is not enabled, skip. */
1626                 if (!(conf.ports.enabled & (1 << i)))
1627                         continue;
1628                 DEBUG("using port %u", port);
1629                 ctx = ibv_open_device(ibv_dev);
1630                 if (ctx == NULL) {
1631                         rte_errno = ENODEV;
1632                         goto port_error;
1633                 }
1634                 /* Check port status. */
1635                 err = ibv_query_port(ctx, port, &port_attr);
1636                 if (err) {
1637                         rte_errno = err;
1638                         ERROR("port query failed: %s", strerror(rte_errno));
1639                         goto port_error;
1640                 }
1641                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
1642                         rte_errno = ENOTSUP;
1643                         ERROR("port %d is not configured in Ethernet mode",
1644                               port);
1645                         goto port_error;
1646                 }
1647                 if (port_attr.state != IBV_PORT_ACTIVE)
1648                         DEBUG("port %d is not active: \"%s\" (%d)",
1649                               port, ibv_port_state_str(port_attr.state),
1650                               port_attr.state);
1651                 /* Make asynchronous FD non-blocking to handle interrupts. */
1652                 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
1653                         ERROR("cannot make asynchronous FD non-blocking: %s",
1654                               strerror(rte_errno));
1655                         goto port_error;
1656                 }
1657                 /* Allocate protection domain. */
1658                 pd = ibv_alloc_pd(ctx);
1659                 if (pd == NULL) {
1660                         rte_errno = ENOMEM;
1661                         ERROR("PD allocation failure");
1662                         goto port_error;
1663                 }
1664                 /* from rte_ethdev.c */
1665                 priv = rte_zmalloc("ethdev private structure",
1666                                    sizeof(*priv),
1667                                    RTE_CACHE_LINE_SIZE);
1668                 if (priv == NULL) {
1669                         rte_errno = ENOMEM;
1670                         ERROR("priv allocation failure");
1671                         goto port_error;
1672                 }
1673                 priv->ctx = ctx;
1674                 priv->device_attr = device_attr;
1675                 priv->port = port;
1676                 priv->pd = pd;
1677                 priv->mtu = ETHER_MTU;
1678                 priv->vf = vf;
1679                 /* Configure the first MAC address by default. */
1680                 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
1681                         ERROR("cannot get MAC address, is mlx4_en loaded?"
1682                               " (rte_errno: %s)", strerror(rte_errno));
1683                         goto port_error;
1684                 }
1685                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1686                      priv->port,
1687                      mac.addr_bytes[0], mac.addr_bytes[1],
1688                      mac.addr_bytes[2], mac.addr_bytes[3],
1689                      mac.addr_bytes[4], mac.addr_bytes[5]);
1690                 /* Register MAC address. */
1691                 priv->mac = mac;
1692                 if (priv_mac_addr_add(priv))
1693                         goto port_error;
1694 #ifndef NDEBUG
1695                 {
1696                         char ifname[IF_NAMESIZE];
1697
1698                         if (mlx4_get_ifname(priv, &ifname) == 0)
1699                                 DEBUG("port %u ifname is \"%s\"",
1700                                       priv->port, ifname);
1701                         else
1702                                 DEBUG("port %u ifname is unknown", priv->port);
1703                 }
1704 #endif
1705                 /* Get actual MTU if possible. */
1706                 mlx4_mtu_get(priv, &priv->mtu);
1707                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
1708                 /* from rte_ethdev.c */
1709                 {
1710                         char name[RTE_ETH_NAME_MAX_LEN];
1711
1712                         snprintf(name, sizeof(name), "%s port %u",
1713                                  ibv_get_device_name(ibv_dev), port);
1714                         eth_dev = rte_eth_dev_allocate(name);
1715                 }
1716                 if (eth_dev == NULL) {
1717                         ERROR("can not allocate rte ethdev");
1718                         rte_errno = ENOMEM;
1719                         goto port_error;
1720                 }
1721                 eth_dev->data->dev_private = priv;
1722                 eth_dev->data->mac_addrs = &priv->mac;
1723                 eth_dev->device = &pci_dev->device;
1724                 rte_eth_copy_pci_info(eth_dev, pci_dev);
1725                 eth_dev->device->driver = &mlx4_driver.driver;
1726                 /* Initialize local interrupt handle for current port. */
1727                 priv->intr_handle = (struct rte_intr_handle){
1728                         .fd = -1,
1729                         .type = RTE_INTR_HANDLE_EXT,
1730                 };
1731                 /*
1732                  * Override ethdev interrupt handle pointer with private
1733                  * handle instead of that of the parent PCI device used by
1734                  * default. This prevents it from being shared between all
1735                  * ports of the same PCI device since each of them is
1736                  * associated its own Verbs context.
1737                  *
1738                  * Rx interrupts in particular require this as the PMD has
1739                  * no control over the registration of queue interrupts
1740                  * besides setting up eth_dev->intr_handle, the rest is
1741                  * handled by rte_intr_rx_ctl().
1742                  */
1743                 eth_dev->intr_handle = &priv->intr_handle;
1744                 priv->dev = eth_dev;
1745                 eth_dev->dev_ops = &mlx4_dev_ops;
1746                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1747                 /* Bring Ethernet device up. */
1748                 DEBUG("forcing Ethernet interface up");
1749                 mlx4_dev_set_link_up(priv->dev);
1750                 /* Update link status once if waiting for LSC. */
1751                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1752                         mlx4_link_update(eth_dev, 0);
1753                 continue;
1754 port_error:
1755                 rte_free(priv);
1756                 if (pd)
1757                         claim_zero(ibv_dealloc_pd(pd));
1758                 if (ctx)
1759                         claim_zero(ibv_close_device(ctx));
1760                 if (eth_dev)
1761                         rte_eth_dev_release_port(eth_dev);
1762                 break;
1763         }
1764         if (i == device_attr.phys_port_cnt)
1765                 return 0;
1766         /*
1767          * XXX if something went wrong in the loop above, there is a resource
1768          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
1769          * long as the dpdk does not provide a way to deallocate a ethdev and a
1770          * way to enumerate the registered ethdevs to free the previous ones.
1771          */
1772 error:
1773         if (attr_ctx)
1774                 claim_zero(ibv_close_device(attr_ctx));
1775         if (list)
1776                 ibv_free_device_list(list);
1777         assert(rte_errno >= 0);
1778         return -rte_errno;
1779 }
1780
1781 static const struct rte_pci_id mlx4_pci_id_map[] = {
1782         {
1783                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1784                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
1785         },
1786         {
1787                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1788                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
1789         },
1790         {
1791                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1792                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
1793         },
1794         {
1795                 .vendor_id = 0
1796         }
1797 };
1798
1799 static struct rte_pci_driver mlx4_driver = {
1800         .driver = {
1801                 .name = MLX4_DRIVER_NAME
1802         },
1803         .id_table = mlx4_pci_id_map,
1804         .probe = mlx4_pci_probe,
1805         .drv_flags = RTE_PCI_DRV_INTR_LSC |
1806                      RTE_PCI_DRV_INTR_RMV,
1807 };
1808
1809 /**
1810  * Driver initialization routine.
1811  */
1812 RTE_INIT(rte_mlx4_pmd_init);
1813 static void
1814 rte_mlx4_pmd_init(void)
1815 {
1816         /*
1817          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1818          * huge pages. Calling ibv_fork_init() during init allows
1819          * applications to use fork() safely for purposes other than
1820          * using this PMD, which is not supported in forked processes.
1821          */
1822         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1823         ibv_fork_init();
1824         rte_pci_register(&mlx4_driver);
1825 }
1826
1827 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
1828 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
1829 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
1830         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");