net/mlx5: use flow to enable all multi mode
[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 static void
83 priv_rxq_stop(struct priv *priv)
84 {
85         unsigned int i;
86
87         for (i = 0; i != priv->rxqs_n; ++i)
88                 mlx5_priv_rxq_release(priv, i);
89 }
90
91 static int
92 priv_rxq_start(struct priv *priv)
93 {
94         unsigned int i;
95         int ret = 0;
96
97         for (i = 0; i != priv->rxqs_n; ++i) {
98                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_priv_rxq_get(priv, i);
99
100                 if (!rxq_ctrl)
101                         continue;
102                 ret = rxq_alloc_elts(rxq_ctrl);
103                 if (ret)
104                         goto error;
105                 rxq_ctrl->ibv = mlx5_priv_rxq_ibv_new(priv, i);
106                 if (!rxq_ctrl->ibv) {
107                         ret = ENOMEM;
108                         goto error;
109                 }
110         }
111         return -ret;
112 error:
113         priv_rxq_stop(priv);
114         return -ret;
115 }
116
117 /**
118  * DPDK callback to start the device.
119  *
120  * Simulate device start by attaching all configured flows.
121  *
122  * @param dev
123  *   Pointer to Ethernet device structure.
124  *
125  * @return
126  *   0 on success, negative errno value on failure.
127  */
128 int
129 mlx5_dev_start(struct rte_eth_dev *dev)
130 {
131         struct priv *priv = dev->data->dev_private;
132         struct mlx5_mr *mr = NULL;
133         int err;
134
135         if (mlx5_is_secondary())
136                 return -E_RTE_SECONDARY;
137
138         priv_lock(priv);
139         DEBUG("%p: allocating and configuring hash RX queues", (void *)dev);
140         rte_mempool_walk(mlx5_mp2mr_iter, priv);
141         err = priv_txq_start(priv);
142         if (err) {
143                 ERROR("%p: TXQ allocation failed: %s",
144                       (void *)dev, strerror(err));
145                 goto error;
146         }
147         /* Update send callback. */
148         priv_dev_select_tx_function(priv, dev);
149         err = priv_rxq_start(priv);
150         if (err) {
151                 ERROR("%p: RXQ allocation failed: %s",
152                       (void *)dev, strerror(err));
153                 goto error;
154         }
155         /* Update receive callback. */
156         priv_dev_select_rx_function(priv, dev);
157         err = priv_create_hash_rxqs(priv);
158         if (!err)
159                 err = priv_rehash_flows(priv);
160         else {
161                 ERROR("%p: an error occurred while configuring hash RX queues:"
162                       " %s",
163                       (void *)priv, strerror(err));
164                 goto error;
165         }
166         if (dev->data->promiscuous)
167                 mlx5_promiscuous_enable(dev);
168         else if (dev->data->all_multicast)
169                 mlx5_allmulticast_enable(dev);
170         err = priv_flow_start(priv, &priv->ctrl_flows);
171         if (err) {
172                 ERROR("%p: an error occurred while configuring control flows:"
173                       " %s",
174                       (void *)priv, strerror(err));
175                 goto error;
176         }
177         err = priv_flow_start(priv, &priv->flows);
178         if (err) {
179                 ERROR("%p: an error occurred while configuring flows:"
180                       " %s",
181                       (void *)priv, strerror(err));
182                 goto error;
183         }
184         err = priv_rx_intr_vec_enable(priv);
185         if (err) {
186                 ERROR("%p: RX interrupt vector creation failed",
187                       (void *)priv);
188                 goto error;
189         }
190         priv_dev_interrupt_handler_install(priv, dev);
191         priv_xstats_init(priv);
192         priv_unlock(priv);
193         return 0;
194 error:
195         /* Rollback. */
196         LIST_FOREACH(mr, &priv->mr, next)
197                 priv_mr_release(priv, mr);
198         priv_special_flow_disable_all(priv);
199         priv_mac_addrs_disable(priv);
200         priv_destroy_hash_rxqs(priv);
201         priv_flow_stop(priv, &priv->flows);
202         priv_flow_flush(priv, &priv->ctrl_flows);
203         priv_rxq_stop(priv);
204         priv_txq_stop(priv);
205         priv_unlock(priv);
206         return -err;
207 }
208
209 /**
210  * DPDK callback to stop the device.
211  *
212  * Simulate device stop by detaching all configured flows.
213  *
214  * @param dev
215  *   Pointer to Ethernet device structure.
216  */
217 void
218 mlx5_dev_stop(struct rte_eth_dev *dev)
219 {
220         struct priv *priv = dev->data->dev_private;
221         struct mlx5_mr *mr;
222
223         if (mlx5_is_secondary())
224                 return;
225
226         priv_lock(priv);
227         dev->data->dev_started = 0;
228         /* Prevent crashes when queues are still in use. */
229         dev->rx_pkt_burst = removed_rx_burst;
230         dev->tx_pkt_burst = removed_tx_burst;
231         rte_wmb();
232         usleep(1000 * priv->rxqs_n);
233         DEBUG("%p: cleaning up and destroying hash RX queues", (void *)dev);
234         priv_special_flow_disable_all(priv);
235         priv_mac_addrs_disable(priv);
236         priv_destroy_hash_rxqs(priv);
237         priv_flow_stop(priv, &priv->flows);
238         priv_flow_flush(priv, &priv->ctrl_flows);
239         priv_rx_intr_vec_disable(priv);
240         priv_dev_interrupt_handler_uninstall(priv, dev);
241         priv_txq_stop(priv);
242         priv_rxq_stop(priv);
243         LIST_FOREACH(mr, &priv->mr, next) {
244                 priv_mr_release(priv, mr);
245         }
246         priv_unlock(priv);
247 }