net/mlx5: add reference counter on DPDK Tx queues
[dpdk.git] / drivers / net / mlx5 / mlx5_trigger.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <unistd.h>
34
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_interrupts.h>
38 #include <rte_alarm.h>
39
40 #include "mlx5.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
43
44 static void
45 priv_txq_stop(struct priv *priv)
46 {
47         unsigned int i;
48
49         for (i = 0; i != priv->txqs_n; ++i)
50                 mlx5_priv_txq_release(priv, i);
51 }
52
53 static int
54 priv_txq_start(struct priv *priv)
55 {
56         unsigned int i;
57         int ret = 0;
58
59         /* Add memory regions to Tx queues. */
60         for (i = 0; i != priv->txqs_n; ++i) {
61                 unsigned int idx = 0;
62                 struct mlx5_mr *mr;
63                 struct mlx5_txq_ctrl *txq_ctrl = mlx5_priv_txq_get(priv, i);
64
65                 if (!txq_ctrl)
66                         continue;
67                 LIST_FOREACH(mr, &priv->mr, next)
68                         priv_txq_mp2mr_reg(priv, &txq_ctrl->txq, mr->mp, idx++);
69                 txq_alloc_elts(txq_ctrl);
70                 txq_ctrl->ibv = mlx5_priv_txq_ibv_new(priv, i);
71                 if (!txq_ctrl->ibv) {
72                         ret = ENOMEM;
73                         goto error;
74                 }
75         }
76         return -ret;
77 error:
78         priv_txq_stop(priv);
79         return -ret;
80 }
81
82 /**
83  * DPDK callback to start the device.
84  *
85  * Simulate device start by attaching all configured flows.
86  *
87  * @param dev
88  *   Pointer to Ethernet device structure.
89  *
90  * @return
91  *   0 on success, negative errno value on failure.
92  */
93 int
94 mlx5_dev_start(struct rte_eth_dev *dev)
95 {
96         struct priv *priv = dev->data->dev_private;
97         struct mlx5_mr *mr = NULL;
98         int err;
99
100         if (mlx5_is_secondary())
101                 return -E_RTE_SECONDARY;
102
103         priv_lock(priv);
104         /* Update Rx/Tx callback. */
105         priv_dev_select_rx_function(priv, dev);
106         DEBUG("%p: allocating and configuring hash RX queues", (void *)dev);
107         rte_mempool_walk(mlx5_mp2mr_iter, priv);
108         err = priv_txq_start(priv);
109         if (err) {
110                 ERROR("%p: TXQ allocation failed: %s",
111                       (void *)dev, strerror(err));
112                 goto error;
113         }
114         /* Update send callback. */
115         priv_dev_select_tx_function(priv, dev);
116         err = priv_create_hash_rxqs(priv);
117         if (!err)
118                 err = priv_rehash_flows(priv);
119         else {
120                 ERROR("%p: an error occurred while configuring hash RX queues:"
121                       " %s",
122                       (void *)priv, strerror(err));
123                 goto error;
124         }
125         err = priv_flow_start(priv);
126         if (err) {
127                 ERROR("%p: an error occurred while configuring flows:"
128                       " %s",
129                       (void *)priv, strerror(err));
130                 goto error;
131         }
132         err = priv_rx_intr_vec_enable(priv);
133         if (err) {
134                 ERROR("%p: RX interrupt vector creation failed",
135                       (void *)priv);
136                 goto error;
137         }
138         priv_dev_interrupt_handler_install(priv, dev);
139         priv_xstats_init(priv);
140         priv_unlock(priv);
141         return 0;
142 error:
143         /* Rollback. */
144         LIST_FOREACH(mr, &priv->mr, next)
145                 priv_mr_release(priv, mr);
146         priv_special_flow_disable_all(priv);
147         priv_mac_addrs_disable(priv);
148         priv_destroy_hash_rxqs(priv);
149         priv_flow_stop(priv);
150         priv_txq_stop(priv);
151         priv_unlock(priv);
152         return -err;
153 }
154
155 /**
156  * DPDK callback to stop the device.
157  *
158  * Simulate device stop by detaching all configured flows.
159  *
160  * @param dev
161  *   Pointer to Ethernet device structure.
162  */
163 void
164 mlx5_dev_stop(struct rte_eth_dev *dev)
165 {
166         struct priv *priv = dev->data->dev_private;
167         struct mlx5_mr *mr;
168
169         if (mlx5_is_secondary())
170                 return;
171
172         priv_lock(priv);
173         dev->data->dev_started = 0;
174         /* Prevent crashes when queues are still in use. */
175         dev->rx_pkt_burst = removed_rx_burst;
176         dev->tx_pkt_burst = removed_tx_burst;
177         rte_wmb();
178         usleep(1000 * priv->rxqs_n);
179         DEBUG("%p: cleaning up and destroying hash RX queues", (void *)dev);
180         priv_special_flow_disable_all(priv);
181         priv_mac_addrs_disable(priv);
182         priv_destroy_hash_rxqs(priv);
183         priv_flow_stop(priv);
184         priv_rx_intr_vec_disable(priv);
185         priv_txq_stop(priv);
186         LIST_FOREACH(mr, &priv->mr, next) {
187                 priv_mr_release(priv, mr);
188         }
189         priv_dev_interrupt_handler_uninstall(priv, dev);
190         priv_unlock(priv);
191 }