net/sfc: handle MC reboot event
[dpdk.git] / drivers / net / sfc / sfc.h
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2016-2017 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was jointly developed between OKTET Labs (under contract
8  * for Solarflare) and Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef _SFC_H
33 #define _SFC_H
34
35 #include <stdbool.h>
36
37 #include <rte_pci.h>
38 #include <rte_bus_pci.h>
39 #include <rte_ethdev.h>
40 #include <rte_kvargs.h>
41 #include <rte_spinlock.h>
42 #include <rte_atomic.h>
43
44 #include "efx.h"
45
46 #include "sfc_filter.h"
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 #if EFSYS_OPT_RX_SCALE
53 /** RSS hash offloads mask */
54 #define SFC_RSS_OFFLOADS        (ETH_RSS_IP | ETH_RSS_TCP)
55 #endif
56
57 /*
58  * +---------------+
59  * | UNINITIALIZED |<-----------+
60  * +---------------+            |
61  *      |.eth_dev_init          |.eth_dev_uninit
62  *      V                       |
63  * +---------------+------------+
64  * |  INITIALIZED  |
65  * +---------------+<-----------<---------------+
66  *      |.dev_configure         |               |
67  *      V                       |failed         |
68  * +---------------+------------+               |
69  * |  CONFIGURING  |                            |
70  * +---------------+----+                       |
71  *      |success        |                       |
72  *      |               |               +---------------+
73  *      |               |               |    CLOSING    |
74  *      |               |               +---------------+
75  *      |               |                       ^
76  *      V               |.dev_configure         |
77  * +---------------+----+                       |.dev_close
78  * |  CONFIGURED   |----------------------------+
79  * +---------------+<-----------+
80  *      |.dev_start             |
81  *      V                       |
82  * +---------------+            |
83  * |   STARTING    |------------^
84  * +---------------+ failed     |
85  *      |success                |
86  *      |               +---------------+
87  *      |               |   STOPPING    |
88  *      |               +---------------+
89  *      |                       ^
90  *      V                       |.dev_stop
91  * +---------------+------------+
92  * |    STARTED    |
93  * +---------------+
94  */
95 enum sfc_adapter_state {
96         SFC_ADAPTER_UNINITIALIZED = 0,
97         SFC_ADAPTER_INITIALIZED,
98         SFC_ADAPTER_CONFIGURING,
99         SFC_ADAPTER_CONFIGURED,
100         SFC_ADAPTER_CLOSING,
101         SFC_ADAPTER_STARTING,
102         SFC_ADAPTER_STARTED,
103         SFC_ADAPTER_STOPPING,
104
105         SFC_ADAPTER_NSTATES
106 };
107
108 enum sfc_dev_filter_mode {
109         SFC_DEV_FILTER_MODE_PROMISC = 0,
110         SFC_DEV_FILTER_MODE_ALLMULTI,
111
112         SFC_DEV_FILTER_NMODES
113 };
114
115 enum sfc_mcdi_state {
116         SFC_MCDI_UNINITIALIZED = 0,
117         SFC_MCDI_INITIALIZED,
118         SFC_MCDI_BUSY,
119         SFC_MCDI_COMPLETED,
120
121         SFC_MCDI_NSTATES
122 };
123
124 struct sfc_mcdi {
125         rte_spinlock_t                  lock;
126         efsys_mem_t                     mem;
127         enum sfc_mcdi_state             state;
128         efx_mcdi_transport_t            transport;
129         bool                            logging;
130         uint32_t                        proxy_handle;
131         efx_rc_t                        proxy_result;
132 };
133
134 struct sfc_intr {
135         efx_intr_type_t                 type;
136         rte_intr_callback_fn            handler;
137         boolean_t                       lsc_intr;
138 };
139
140 struct sfc_rxq_info;
141 struct sfc_txq_info;
142 struct sfc_dp_rx;
143
144 struct sfc_port {
145         unsigned int                    lsc_seq;
146
147         uint32_t                        phy_adv_cap_mask;
148         uint32_t                        phy_adv_cap;
149
150         unsigned int                    flow_ctrl;
151         boolean_t                       flow_ctrl_autoneg;
152         size_t                          pdu;
153
154         /*
155          * Flow API isolated mode overrides promisc and allmulti settings;
156          * they won't be applied if isolated mode is active
157          */
158         boolean_t                       isolated;
159         boolean_t                       promisc;
160         boolean_t                       allmulti;
161
162         struct ether_addr               default_mac_addr;
163
164         unsigned int                    max_mcast_addrs;
165         unsigned int                    nb_mcast_addrs;
166         uint8_t                         *mcast_addrs;
167
168         rte_spinlock_t                  mac_stats_lock;
169         uint64_t                        *mac_stats_buf;
170         unsigned int                    mac_stats_nb_supported;
171         efsys_mem_t                     mac_stats_dma_mem;
172         boolean_t                       mac_stats_reset_pending;
173         uint16_t                        mac_stats_update_period_ms;
174         uint32_t                        mac_stats_update_generation;
175         boolean_t                       mac_stats_periodic_dma_supported;
176         uint64_t                        mac_stats_last_request_timestamp;
177
178         uint32_t                mac_stats_mask[EFX_MAC_STATS_MASK_NPAGES];
179 };
180
181 /* Adapter private data */
182 struct sfc_adapter {
183         /*
184          * PMD setup and configuration is not thread safe. Since it is not
185          * performance sensitive, it is better to guarantee thread-safety
186          * and add device level lock. Adapter control operations which
187          * change its state should acquire the lock.
188          */
189         rte_spinlock_t                  lock;
190         enum sfc_adapter_state          state;
191         struct rte_pci_addr             pci_addr;
192         uint16_t                        port_id;
193         struct rte_eth_dev              *eth_dev;
194         struct rte_kvargs               *kvargs;
195         bool                            debug_init;
196         int                             socket_id;
197         efsys_bar_t                     mem_bar;
198         efx_family_t                    family;
199         efx_nic_t                       *nic;
200         rte_spinlock_t                  nic_lock;
201         rte_atomic32_t                  restart_required;
202
203         struct sfc_mcdi                 mcdi;
204         struct sfc_intr                 intr;
205         struct sfc_port                 port;
206         struct sfc_filter               filter;
207
208         unsigned int                    rxq_max;
209         unsigned int                    txq_max;
210
211         unsigned int                    txq_max_entries;
212
213         uint32_t                        evq_flags;
214         unsigned int                    evq_count;
215
216         unsigned int                    mgmt_evq_index;
217         /*
218          * The lock is used to serialise management event queue polling
219          * which can be done from different context. Also the lock
220          * guarantees that mgmt_evq_running is preserved while the lock
221          * is held. It is used to serialise polling and start/stop
222          * operations.
223          *
224          * Locks which may be held when the lock is acquired:
225          *  - adapter lock, when:
226          *    - device start/stop to change mgmt_evq_running
227          *    - any control operations in client side MCDI proxy handling to
228          *      poll management event queue waiting for proxy response
229          *  - MCDI lock, when:
230          *    - any control operations in client side MCDI proxy handling to
231          *      poll management event queue waiting for proxy response
232          *
233          * Locks which are acquired with the lock held:
234          *  - nic_lock, when:
235          *    - MC event processing on management event queue polling
236          *      (e.g. MC REBOOT or BADASSERT events)
237          */
238         rte_spinlock_t                  mgmt_evq_lock;
239         bool                            mgmt_evq_running;
240         struct sfc_evq                  *mgmt_evq;
241
242         unsigned int                    rxq_count;
243         struct sfc_rxq_info             *rxq_info;
244
245         unsigned int                    txq_count;
246         struct sfc_txq_info             *txq_info;
247
248         boolean_t                       tso;
249
250         unsigned int                    rss_channels;
251
252 #if EFSYS_OPT_RX_SCALE
253         efx_rx_scale_context_type_t     rss_support;
254         efx_rx_hash_support_t           hash_support;
255         efx_rx_hash_type_t              rss_hash_types;
256         unsigned int                    rss_tbl[EFX_RSS_TBL_SIZE];
257         uint8_t                         rss_key[EFX_RSS_KEY_SIZE];
258 #endif
259
260         /*
261          * Shared memory copy of the Rx datapath name to be used by
262          * the secondary process to find Rx datapath to be used.
263          */
264         char                            *dp_rx_name;
265         const struct sfc_dp_rx          *dp_rx;
266
267         /*
268          * Shared memory copy of the Tx datapath name to be used by
269          * the secondary process to find Rx datapath to be used.
270          */
271         char                            *dp_tx_name;
272         const struct sfc_dp_tx          *dp_tx;
273 };
274
275 /*
276  * Add wrapper functions to acquire/release lock to be able to remove or
277  * change the lock in one place.
278  */
279
280 static inline void
281 sfc_adapter_lock_init(struct sfc_adapter *sa)
282 {
283         rte_spinlock_init(&sa->lock);
284 }
285
286 static inline int
287 sfc_adapter_is_locked(struct sfc_adapter *sa)
288 {
289         return rte_spinlock_is_locked(&sa->lock);
290 }
291
292 static inline void
293 sfc_adapter_lock(struct sfc_adapter *sa)
294 {
295         rte_spinlock_lock(&sa->lock);
296 }
297
298 static inline int
299 sfc_adapter_trylock(struct sfc_adapter *sa)
300 {
301         return rte_spinlock_trylock(&sa->lock);
302 }
303
304 static inline void
305 sfc_adapter_unlock(struct sfc_adapter *sa)
306 {
307         rte_spinlock_unlock(&sa->lock);
308 }
309
310 static inline void
311 sfc_adapter_lock_fini(__rte_unused struct sfc_adapter *sa)
312 {
313         /* Just for symmetry of the API */
314 }
315
316 /** Get the number of milliseconds since boot from the default timer */
317 static inline uint64_t
318 sfc_get_system_msecs(void)
319 {
320         return rte_get_timer_cycles() * MS_PER_S / rte_get_timer_hz();
321 }
322
323 int sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
324                   size_t len, int socket_id, efsys_mem_t *esmp);
325 void sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp);
326
327 int sfc_probe(struct sfc_adapter *sa);
328 void sfc_unprobe(struct sfc_adapter *sa);
329 int sfc_attach(struct sfc_adapter *sa);
330 void sfc_detach(struct sfc_adapter *sa);
331 int sfc_start(struct sfc_adapter *sa);
332 void sfc_stop(struct sfc_adapter *sa);
333
334 void sfc_schedule_restart(struct sfc_adapter *sa);
335
336 int sfc_mcdi_init(struct sfc_adapter *sa);
337 void sfc_mcdi_fini(struct sfc_adapter *sa);
338
339 int sfc_configure(struct sfc_adapter *sa);
340 void sfc_close(struct sfc_adapter *sa);
341
342 int sfc_intr_attach(struct sfc_adapter *sa);
343 void sfc_intr_detach(struct sfc_adapter *sa);
344 int sfc_intr_configure(struct sfc_adapter *sa);
345 void sfc_intr_close(struct sfc_adapter *sa);
346 int sfc_intr_start(struct sfc_adapter *sa);
347 void sfc_intr_stop(struct sfc_adapter *sa);
348
349 int sfc_port_attach(struct sfc_adapter *sa);
350 void sfc_port_detach(struct sfc_adapter *sa);
351 int sfc_port_configure(struct sfc_adapter *sa);
352 void sfc_port_close(struct sfc_adapter *sa);
353 int sfc_port_start(struct sfc_adapter *sa);
354 void sfc_port_stop(struct sfc_adapter *sa);
355 void sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
356                                 struct rte_eth_link *link_info);
357 int sfc_port_update_mac_stats(struct sfc_adapter *sa);
358 int sfc_port_reset_mac_stats(struct sfc_adapter *sa);
359 int sfc_set_rx_mode(struct sfc_adapter *sa);
360
361
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif  /* _SFC_H */