net/failsafe: add plug-in support
[dpdk.git] / drivers / net / failsafe / failsafe_private.h
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 #ifndef _RTE_ETH_FAILSAFE_PRIVATE_H_
35 #define _RTE_ETH_FAILSAFE_PRIVATE_H_
36
37 #include <rte_dev.h>
38 #include <rte_ethdev.h>
39 #include <rte_devargs.h>
40
41 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
42
43 #define PMD_FAILSAFE_MAC_KVARG "mac"
44 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
45 #define PMD_FAILSAFE_PARAM_STRING       \
46         "dev(<ifc>),"                   \
47         "mac=mac_addr,"                 \
48         "hotplug_poll=u64"              \
49         ""
50
51 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
52
53 #define FAILSAFE_MAX_ETHPORTS 2
54 #define FAILSAFE_MAX_ETHADDR 128
55
56 /* TYPES */
57
58 struct rxq {
59         struct fs_priv *priv;
60         uint16_t qid;
61         /* id of last sub_device polled */
62         uint8_t last_polled;
63         unsigned int socket_id;
64         struct rte_eth_rxq_info info;
65 };
66
67 struct txq {
68         struct fs_priv *priv;
69         uint16_t qid;
70         unsigned int socket_id;
71         struct rte_eth_txq_info info;
72 };
73
74 enum dev_state {
75         DEV_UNDEFINED,
76         DEV_PARSED,
77         DEV_PROBED,
78         DEV_ACTIVE,
79         DEV_STARTED,
80 };
81
82 struct sub_device {
83         /* Exhaustive DPDK device description */
84         struct rte_devargs devargs;
85         struct rte_bus *bus;
86         struct rte_device *dev;
87         struct rte_eth_dev *edev;
88         /* Device state machine */
89         enum dev_state state;
90 };
91
92 struct fs_priv {
93         struct rte_eth_dev *dev;
94         /*
95          * Set of sub_devices.
96          * subs[0] is the preferred device
97          * any other is just another slave
98          */
99         struct sub_device *subs;
100         uint8_t subs_head; /* if head == tail, no subs */
101         uint8_t subs_tail; /* first invalid */
102         uint8_t subs_tx; /* current emitting device */
103         uint8_t current_probed;
104         /* current number of mac_addr slots allocated. */
105         uint32_t nb_mac_addr;
106         struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
107         uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
108         /* current capabilities */
109         struct rte_eth_dev_info infos;
110         /*
111          * Fail-safe state machine.
112          * This level will be tracking state of the EAL and eth
113          * layer at large as defined by the user application.
114          * It will then steer the sub_devices toward the same
115          * synchronized state.
116          */
117         enum dev_state state;
118         unsigned int pending_alarm:1; /* An alarm is pending */
119 };
120
121 /* MISC */
122
123 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
124 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
125
126 /* RX / TX */
127
128 uint16_t failsafe_rx_burst(void *rxq,
129                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
130 uint16_t failsafe_tx_burst(void *txq,
131                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
132
133 /* ARGS */
134
135 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
136 void failsafe_args_free(struct rte_eth_dev *dev);
137 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
138
139 /* EAL */
140
141 int failsafe_eal_init(struct rte_eth_dev *dev);
142 int failsafe_eal_uninit(struct rte_eth_dev *dev);
143
144 /* ETH_DEV */
145
146 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
147
148 /* GLOBALS */
149
150 extern const char pmd_failsafe_driver_name[];
151 extern const struct eth_dev_ops failsafe_ops;
152 extern uint64_t hotplug_poll;
153 extern int mac_from_arg;
154
155 /* HELPERS */
156
157 /* dev: (struct rte_eth_dev *) fail-safe device */
158 #define PRIV(dev) \
159         ((struct fs_priv *)(dev)->data->dev_private)
160
161 /* sdev: (struct sub_device *) */
162 #define ETH(sdev) \
163         ((sdev)->edev)
164
165 /* sdev: (struct sub_device *) */
166 #define PORT_ID(sdev) \
167         (ETH(sdev)->data->port_id)
168
169 /**
170  * Stateful iterator construct over fail-safe sub-devices:
171  * s:     (struct sub_device *), iterator
172  * i:     (uint8_t), increment
173  * dev:   (struct rte_eth_dev *), fail-safe ethdev
174  * state: (enum dev_state), minimum acceptable device state
175  */
176 #define FOREACH_SUBDEV_STATE(s, i, dev, state)                          \
177         for (i = fs_find_next((dev), 0, state);                         \
178              i < PRIV(dev)->subs_tail && (s = &PRIV(dev)->subs[i]);     \
179              i = fs_find_next((dev), i + 1, state))
180
181 /**
182  * Iterator construct over fail-safe sub-devices:
183  * s:   (struct sub_device *), iterator
184  * i:   (uint8_t), increment
185  * dev: (struct rte_eth_dev *), fail-safe ethdev
186  */
187 #define FOREACH_SUBDEV(s, i, dev)                       \
188         FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
189
190 /* dev: (struct rte_eth_dev *) fail-safe device */
191 #define PREFERRED_SUBDEV(dev) \
192         (&PRIV(dev)->subs[0])
193
194 /* dev: (struct rte_eth_dev *) fail-safe device */
195 #define TX_SUBDEV(dev)                                                    \
196         (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail                ? NULL \
197          : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
198          : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
199
200 /**
201  * s:   (struct sub_device *)
202  * ops: (struct eth_dev_ops) member
203  */
204 #define SUBOPS(s, ops) \
205         (ETH(s)->dev_ops->ops)
206
207 #define LOG__(level, m, ...) \
208         RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
209 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
210 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
211 #define INFO(...) LOG_(INFO, __VA_ARGS__)
212 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
213 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
214
215 /* inlined functions */
216
217 static inline uint8_t
218 fs_find_next(struct rte_eth_dev *dev, uint8_t sid,
219                 enum dev_state min_state)
220 {
221         while (sid < PRIV(dev)->subs_tail) {
222                 if (PRIV(dev)->subs[sid].state >= min_state)
223                         break;
224                 sid++;
225         }
226         if (sid >= PRIV(dev)->subs_tail)
227                 return PRIV(dev)->subs_tail;
228         return sid;
229 }
230
231 static inline void
232 fs_switch_dev(struct rte_eth_dev *dev)
233 {
234         enum dev_state req_state;
235
236         req_state = PRIV(dev)->state;
237         if (PREFERRED_SUBDEV(dev)->state >= req_state) {
238                 if (TX_SUBDEV(dev) != PREFERRED_SUBDEV(dev) &&
239                     (TX_SUBDEV(dev) == NULL ||
240                      (req_state == DEV_STARTED) ||
241                      (TX_SUBDEV(dev) && TX_SUBDEV(dev)->state < DEV_STARTED))) {
242                         DEBUG("Switching tx_dev to preferred sub_device");
243                         PRIV(dev)->subs_tx = 0;
244                 }
245         } else if ((TX_SUBDEV(dev) && TX_SUBDEV(dev)->state < req_state) ||
246                    TX_SUBDEV(dev) == NULL) {
247                 struct sub_device *sdev;
248                 uint8_t i;
249
250                 /* Using acceptable device */
251                 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
252                         DEBUG("Switching tx_dev to sub_device %d",
253                               i);
254                         PRIV(dev)->subs_tx = i;
255                         break;
256                 }
257         } else if (TX_SUBDEV(dev) && TX_SUBDEV(dev)->state < req_state) {
258                 DEBUG("No device ready, deactivating tx_dev");
259                 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
260         } else {
261                 return;
262         }
263         rte_wmb();
264 }
265
266 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */