mlx5: add device configure/start/stop
[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
34 /* DPDK headers don't like -pedantic. */
35 #ifdef PEDANTIC
36 #pragma GCC diagnostic ignored "-pedantic"
37 #endif
38 #include <rte_ether.h>
39 #include <rte_ethdev.h>
40 #ifdef PEDANTIC
41 #pragma GCC diagnostic error "-pedantic"
42 #endif
43
44 #include "mlx5.h"
45 #include "mlx5_rxtx.h"
46 #include "mlx5_utils.h"
47
48 /**
49  * DPDK callback to start the device.
50  *
51  * Simulate device start by attaching all configured flows.
52  *
53  * @param dev
54  *   Pointer to Ethernet device structure.
55  *
56  * @return
57  *   0 on success, negative errno value on failure.
58  */
59 int
60 mlx5_dev_start(struct rte_eth_dev *dev)
61 {
62         struct priv *priv = dev->data->dev_private;
63         unsigned int i = 0;
64         unsigned int r;
65         struct rxq *rxq;
66
67         priv_lock(priv);
68         if (priv->started) {
69                 priv_unlock(priv);
70                 return 0;
71         }
72         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
73         priv->started = 1;
74         if (priv->rss) {
75                 rxq = &priv->rxq_parent;
76                 r = 1;
77         } else {
78                 rxq = (*priv->rxqs)[0];
79                 r = priv->rxqs_n;
80         }
81         /* Iterate only once when RSS is enabled. */
82         do {
83                 int ret;
84
85                 /* Ignore nonexistent RX queues. */
86                 if (rxq == NULL)
87                         continue;
88                 ret = rxq_mac_addrs_add(rxq);
89                 if (!ret)
90                         continue;
91                 WARN("%p: QP flow attachment failed: %s",
92                      (void *)dev, strerror(ret));
93                 /* Rollback. */
94                 while (i != 0) {
95                         rxq = (*priv->rxqs)[--i];
96                         if (rxq != NULL) {
97                                 rxq_mac_addrs_del(rxq);
98                         }
99                 }
100                 priv->started = 0;
101                 priv_unlock(priv);
102                 return -ret;
103         } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
104         priv_unlock(priv);
105         return 0;
106 }
107
108 /**
109  * DPDK callback to stop the device.
110  *
111  * Simulate device stop by detaching all configured flows.
112  *
113  * @param dev
114  *   Pointer to Ethernet device structure.
115  */
116 void
117 mlx5_dev_stop(struct rte_eth_dev *dev)
118 {
119         struct priv *priv = dev->data->dev_private;
120         unsigned int i = 0;
121         unsigned int r;
122         struct rxq *rxq;
123
124         priv_lock(priv);
125         if (!priv->started) {
126                 priv_unlock(priv);
127                 return;
128         }
129         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
130         priv->started = 0;
131         if (priv->rss) {
132                 rxq = &priv->rxq_parent;
133                 r = 1;
134         } else {
135                 rxq = (*priv->rxqs)[0];
136                 r = priv->rxqs_n;
137         }
138         /* Iterate only once when RSS is enabled. */
139         do {
140                 /* Ignore nonexistent RX queues. */
141                 if (rxq == NULL)
142                         continue;
143                 rxq_mac_addrs_del(rxq);
144         } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
145         priv_unlock(priv);
146 }