net/failsafe: add fail-safe PMD
[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_PARAM_STRING       \
45         "dev(<ifc>),"                   \
46         "mac=mac_addr"                  \
47         ""
48
49 #define FAILSAFE_MAX_ETHPORTS 2
50 #define FAILSAFE_MAX_ETHADDR 128
51
52 /* TYPES */
53
54 struct rxq {
55         struct fs_priv *priv;
56         uint16_t qid;
57         /* id of last sub_device polled */
58         uint8_t last_polled;
59         unsigned int socket_id;
60         struct rte_eth_rxq_info info;
61 };
62
63 struct txq {
64         struct fs_priv *priv;
65         uint16_t qid;
66         unsigned int socket_id;
67         struct rte_eth_txq_info info;
68 };
69
70 enum dev_state {
71         DEV_UNDEFINED,
72         DEV_PARSED,
73         DEV_PROBED,
74         DEV_ACTIVE,
75         DEV_STARTED,
76 };
77
78 struct sub_device {
79         /* Exhaustive DPDK device description */
80         struct rte_devargs devargs;
81         struct rte_bus *bus;
82         struct rte_device *dev;
83         struct rte_eth_dev *edev;
84         /* Device state machine */
85         enum dev_state state;
86 };
87
88 struct fs_priv {
89         struct rte_eth_dev *dev;
90         /*
91          * Set of sub_devices.
92          * subs[0] is the preferred device
93          * any other is just another slave
94          */
95         struct sub_device *subs;
96         uint8_t subs_head; /* if head == tail, no subs */
97         uint8_t subs_tail; /* first invalid */
98         uint8_t subs_tx; /* current emitting device */
99         uint8_t current_probed;
100         /* current number of mac_addr slots allocated. */
101         uint32_t nb_mac_addr;
102         struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
103         uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
104         /* current capabilities */
105         struct rte_eth_dev_info infos;
106 };
107
108 /* RX / TX */
109
110 uint16_t failsafe_rx_burst(void *rxq,
111                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
112 uint16_t failsafe_tx_burst(void *txq,
113                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
114
115 /* ARGS */
116
117 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
118 void failsafe_args_free(struct rte_eth_dev *dev);
119 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
120
121 /* EAL */
122
123 int failsafe_eal_init(struct rte_eth_dev *dev);
124 int failsafe_eal_uninit(struct rte_eth_dev *dev);
125
126 /* GLOBALS */
127
128 extern const char pmd_failsafe_driver_name[];
129 extern const struct eth_dev_ops failsafe_ops;
130 extern int mac_from_arg;
131
132 /* HELPERS */
133
134 /* dev: (struct rte_eth_dev *) fail-safe device */
135 #define PRIV(dev) \
136         ((struct fs_priv *)(dev)->data->dev_private)
137
138 /* sdev: (struct sub_device *) */
139 #define ETH(sdev) \
140         ((sdev)->edev)
141
142 /* sdev: (struct sub_device *) */
143 #define PORT_ID(sdev) \
144         (ETH(sdev)->data->port_id)
145
146 /**
147  * Stateful iterator construct over fail-safe sub-devices:
148  * s:     (struct sub_device *), iterator
149  * i:     (uint8_t), increment
150  * dev:   (struct rte_eth_dev *), fail-safe ethdev
151  * state: (enum dev_state), minimum acceptable device state
152  */
153 #define FOREACH_SUBDEV_STATE(s, i, dev, state)                          \
154         for (i = fs_find_next((dev), 0, state);                         \
155              i < PRIV(dev)->subs_tail && (s = &PRIV(dev)->subs[i]);     \
156              i = fs_find_next((dev), i + 1, state))
157
158 /**
159  * Iterator construct over fail-safe sub-devices:
160  * s:   (struct sub_device *), iterator
161  * i:   (uint8_t), increment
162  * dev: (struct rte_eth_dev *), fail-safe ethdev
163  */
164 #define FOREACH_SUBDEV(s, i, dev)                       \
165         FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
166
167 /* dev: (struct rte_eth_dev *) fail-safe device */
168 #define PREFERRED_SUBDEV(dev) \
169         (&PRIV(dev)->subs[0])
170
171 /* dev: (struct rte_eth_dev *) fail-safe device */
172 #define TX_SUBDEV(dev)                                                    \
173         (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail                ? NULL \
174          : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
175          : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
176
177 /**
178  * s:   (struct sub_device *)
179  * ops: (struct eth_dev_ops) member
180  */
181 #define SUBOPS(s, ops) \
182         (ETH(s)->dev_ops->ops)
183
184 #define LOG__(level, m, ...) \
185         RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
186 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
187 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
188 #define INFO(...) LOG_(INFO, __VA_ARGS__)
189 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
190 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
191
192 /* inlined functions */
193
194 static inline uint8_t
195 fs_find_next(struct rte_eth_dev *dev, uint8_t sid,
196                 enum dev_state min_state)
197 {
198         while (sid < PRIV(dev)->subs_tail) {
199                 if (PRIV(dev)->subs[sid].state >= min_state)
200                         break;
201                 sid++;
202         }
203         if (sid >= PRIV(dev)->subs_tail)
204                 return PRIV(dev)->subs_tail;
205         return sid;
206 }
207
208 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */