net/failsafe: register as Rx interrupt mode
[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 <sys/queue.h>
38
39 #include <rte_atomic.h>
40 #include <rte_dev.h>
41 #include <rte_ethdev_driver.h>
42 #include <rte_devargs.h>
43 #include <rte_interrupts.h>
44
45 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
46
47 #define PMD_FAILSAFE_MAC_KVARG "mac"
48 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
49 #define PMD_FAILSAFE_PARAM_STRING       \
50         "dev(<ifc>),"                   \
51         "exec(<shell command>),"        \
52         "fd(<fd number>),"              \
53         "mac=mac_addr,"                 \
54         "hotplug_poll=u64"              \
55         ""
56
57 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
58
59 #define FAILSAFE_MAX_ETHPORTS 2
60 #define FAILSAFE_MAX_ETHADDR 128
61
62 #define DEVARGS_MAXLEN 4096
63
64 /* TYPES */
65
66 struct rxq {
67         struct fs_priv *priv;
68         uint16_t qid;
69         /* next sub_device to poll */
70         struct sub_device *sdev;
71         unsigned int socket_id;
72         int event_fd;
73         unsigned int enable_events:1;
74         struct rte_eth_rxq_info info;
75         rte_atomic64_t refcnt[];
76 };
77
78 struct txq {
79         struct fs_priv *priv;
80         uint16_t qid;
81         unsigned int socket_id;
82         struct rte_eth_txq_info info;
83         rte_atomic64_t refcnt[];
84 };
85
86 struct rte_flow {
87         TAILQ_ENTRY(rte_flow) next;
88         /* sub_flows */
89         struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
90         /* flow description for synchronization */
91         struct rte_flow_desc *fd;
92 };
93
94 enum dev_state {
95         DEV_UNDEFINED,
96         DEV_PARSED,
97         DEV_PROBED,
98         DEV_ACTIVE,
99         DEV_STARTED,
100 };
101
102 struct fs_stats {
103         struct rte_eth_stats stats;
104         uint64_t timestamp;
105 };
106
107 struct sub_device {
108         /* Exhaustive DPDK device description */
109         struct sub_device *next;
110         struct rte_devargs devargs;
111         struct rte_bus *bus;
112         struct rte_device *dev;
113         struct rte_eth_dev *edev;
114         uint8_t sid;
115         /* Device state machine */
116         enum dev_state state;
117         /* Last stats snapshot passed to user */
118         struct fs_stats stats_snapshot;
119         /* Some device are defined as a command line */
120         char *cmdline;
121         /* Others are retrieved through a file descriptor */
122         char *fd_str;
123         /* fail-safe device backreference */
124         struct rte_eth_dev *fs_dev;
125         /* flag calling for recollection */
126         volatile unsigned int remove:1;
127         /* flow isolation state */
128         int flow_isolated:1;
129 };
130
131 struct fs_priv {
132         struct rte_eth_dev *dev;
133         /*
134          * Set of sub_devices.
135          * subs[0] is the preferred device
136          * any other is just another slave
137          */
138         struct sub_device *subs;
139         uint8_t subs_head; /* if head == tail, no subs */
140         uint8_t subs_tail; /* first invalid */
141         uint8_t subs_tx; /* current emitting device */
142         uint8_t current_probed;
143         /* flow mapping */
144         TAILQ_HEAD(sub_flows, rte_flow) flow_list;
145         /* current number of mac_addr slots allocated. */
146         uint32_t nb_mac_addr;
147         struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
148         uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
149         /* current capabilities */
150         struct rte_eth_dev_info infos;
151         struct rte_intr_handle intr_handle; /* Port interrupt handle. */
152         /*
153          * Fail-safe state machine.
154          * This level will be tracking state of the EAL and eth
155          * layer at large as defined by the user application.
156          * It will then steer the sub_devices toward the same
157          * synchronized state.
158          */
159         enum dev_state state;
160         struct rte_eth_stats stats_accumulator;
161         unsigned int pending_alarm:1; /* An alarm is pending */
162         /* flow isolation state */
163         int flow_isolated:1;
164 };
165
166 /* FAILSAFE_INTR */
167
168 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
169 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
170
171 /* MISC */
172
173 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
174 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
175
176 /* RX / TX */
177
178 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
179
180 uint16_t failsafe_rx_burst(void *rxq,
181                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
182 uint16_t failsafe_tx_burst(void *txq,
183                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
184
185 uint16_t failsafe_rx_burst_fast(void *rxq,
186                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
187 uint16_t failsafe_tx_burst_fast(void *txq,
188                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
189
190 /* ARGS */
191
192 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
193 void failsafe_args_free(struct rte_eth_dev *dev);
194 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
195 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
196
197 /* EAL */
198
199 int failsafe_eal_init(struct rte_eth_dev *dev);
200 int failsafe_eal_uninit(struct rte_eth_dev *dev);
201
202 /* ETH_DEV */
203
204 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
205 void failsafe_dev_remove(struct rte_eth_dev *dev);
206 void failsafe_stats_increment(struct rte_eth_stats *to,
207                                 struct rte_eth_stats *from);
208 int failsafe_eth_rmv_event_callback(uint16_t port_id,
209                                     enum rte_eth_event_type type,
210                                     void *arg, void *out);
211 int failsafe_eth_lsc_event_callback(uint16_t port_id,
212                                     enum rte_eth_event_type event,
213                                     void *cb_arg, void *out);
214
215 /* GLOBALS */
216
217 extern const char pmd_failsafe_driver_name[];
218 extern const struct eth_dev_ops failsafe_ops;
219 extern const struct rte_flow_ops fs_flow_ops;
220 extern uint64_t hotplug_poll;
221 extern int mac_from_arg;
222
223 /* HELPERS */
224
225 /* dev: (struct rte_eth_dev *) fail-safe device */
226 #define PRIV(dev) \
227         ((struct fs_priv *)(dev)->data->dev_private)
228
229 /* sdev: (struct sub_device *) */
230 #define ETH(sdev) \
231         ((sdev)->edev)
232
233 /* sdev: (struct sub_device *) */
234 #define PORT_ID(sdev) \
235         (ETH(sdev)->data->port_id)
236
237 /* sdev: (struct sub_device *) */
238 #define SUB_ID(sdev) \
239         ((sdev)->sid)
240
241 /**
242  * Stateful iterator construct over fail-safe sub-devices:
243  * s:     (struct sub_device *), iterator
244  * i:     (uint8_t), increment
245  * dev:   (struct rte_eth_dev *), fail-safe ethdev
246  * state: (enum dev_state), minimum acceptable device state
247  */
248 #define FOREACH_SUBDEV_STATE(s, i, dev, state)          \
249         for (s = fs_find_next((dev), 0, state, &i);     \
250              s != NULL;                                 \
251              s = fs_find_next((dev), i + 1, state, &i))
252
253 /**
254  * Iterator construct over fail-safe sub-devices:
255  * s:   (struct sub_device *), iterator
256  * i:   (uint8_t), increment
257  * dev: (struct rte_eth_dev *), fail-safe ethdev
258  */
259 #define FOREACH_SUBDEV(s, i, dev)                       \
260         FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
261
262 /* dev: (struct rte_eth_dev *) fail-safe device */
263 #define PREFERRED_SUBDEV(dev) \
264         (&PRIV(dev)->subs[0])
265
266 /* dev: (struct rte_eth_dev *) fail-safe device */
267 #define TX_SUBDEV(dev)                                                    \
268         (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail                ? NULL \
269          : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
270          : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
271
272 /**
273  * s:   (struct sub_device *)
274  * ops: (struct eth_dev_ops) member
275  */
276 #define SUBOPS(s, ops) \
277         (ETH(s)->dev_ops->ops)
278
279 /**
280  * Atomic guard
281  */
282
283 /**
284  * a: (rte_atomic64_t)
285  */
286 #define FS_ATOMIC_P(a) \
287         rte_atomic64_set(&(a), 1)
288
289 /**
290  * a: (rte_atomic64_t)
291  */
292 #define FS_ATOMIC_V(a) \
293         rte_atomic64_set(&(a), 0)
294
295 /**
296  * s: (struct sub_device *)
297  * i: uint16_t qid
298  */
299 #define FS_ATOMIC_RX(s, i) \
300         rte_atomic64_read( \
301          &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
302         )
303 /**
304  * s: (struct sub_device *)
305  * i: uint16_t qid
306  */
307 #define FS_ATOMIC_TX(s, i) \
308         rte_atomic64_read( \
309          &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
310         )
311
312 #define LOG__(level, m, ...) \
313         RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
314 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
315 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
316 #define INFO(...) LOG_(INFO, __VA_ARGS__)
317 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
318 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
319
320 /* inlined functions */
321
322 static inline struct sub_device *
323 fs_find_next(struct rte_eth_dev *dev,
324              uint8_t sid,
325              enum dev_state min_state,
326              uint8_t *sid_out)
327 {
328         struct sub_device *subs;
329         uint8_t tail;
330
331         subs = PRIV(dev)->subs;
332         tail = PRIV(dev)->subs_tail;
333         while (sid < tail) {
334                 if (subs[sid].state >= min_state)
335                         break;
336                 sid++;
337         }
338         *sid_out = sid;
339         if (sid >= tail)
340                 return NULL;
341         return &subs[sid];
342 }
343
344 /*
345  * Switch emitting device.
346  * If banned is set, banned must not be considered for
347  * the role of emitting device.
348  */
349 static inline void
350 fs_switch_dev(struct rte_eth_dev *dev,
351               struct sub_device *banned)
352 {
353         struct sub_device *txd;
354         enum dev_state req_state;
355
356         req_state = PRIV(dev)->state;
357         txd = TX_SUBDEV(dev);
358         if (PREFERRED_SUBDEV(dev)->state >= req_state &&
359             PREFERRED_SUBDEV(dev) != banned) {
360                 if (txd != PREFERRED_SUBDEV(dev) &&
361                     (txd == NULL ||
362                      (req_state == DEV_STARTED) ||
363                      (txd && txd->state < DEV_STARTED))) {
364                         DEBUG("Switching tx_dev to preferred sub_device");
365                         PRIV(dev)->subs_tx = 0;
366                 }
367         } else if ((txd && txd->state < req_state) ||
368                    txd == NULL ||
369                    txd == banned) {
370                 struct sub_device *sdev = NULL;
371                 uint8_t i;
372
373                 /* Using acceptable device */
374                 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
375                         if (sdev == banned)
376                                 continue;
377                         DEBUG("Switching tx_dev to sub_device %d",
378                               i);
379                         PRIV(dev)->subs_tx = i;
380                         break;
381                 }
382                 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
383                         DEBUG("No device ready, deactivating tx_dev");
384                         PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
385                 }
386         } else {
387                 return;
388         }
389         set_burst_fn(dev, 0);
390         rte_wmb();
391 }
392
393 /*
394  * Adjust error value and rte_errno to the fail-safe actual error value.
395  */
396 static inline int
397 fs_err(struct sub_device *sdev, int err)
398 {
399         /* A device removal shouldn't be reported as an error. */
400         if (sdev->remove == 1 || err == -EIO)
401                 return rte_errno = 0;
402         return err;
403 }
404 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */