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