net/sfc: fix reading adapter state without locking
[dpdk.git] / drivers / net / sfc / sfc.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2 *
3  * Copyright(c) 2019-2021 Xilinx, Inc.
4  * Copyright(c) 2016-2019 Solarflare Communications Inc.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #ifndef _SFC_H
11 #define _SFC_H
12
13 #include <stdbool.h>
14
15 #include <rte_pci.h>
16 #include <rte_bus_pci.h>
17 #include <ethdev_driver.h>
18 #include <rte_kvargs.h>
19 #include <rte_spinlock.h>
20 #include <rte_atomic.h>
21
22 #include "efx.h"
23
24 #include "sfc_efx_mcdi.h"
25 #include "sfc_efx.h"
26
27 #include "sfc_debug.h"
28 #include "sfc_log.h"
29 #include "sfc_filter.h"
30 #include "sfc_sriov.h"
31 #include "sfc_mae.h"
32 #include "sfc_dp.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /*
39  * +---------------+
40  * | UNINITIALIZED |<-----------+
41  * +---------------+            |
42  *      |.eth_dev_init          |.eth_dev_uninit
43  *      V                       |
44  * +---------------+------------+
45  * |  INITIALIZED  |
46  * +---------------+<-----------<---------------+
47  *      |.dev_configure         |               |
48  *      V                       |failed         |
49  * +---------------+------------+               |
50  * |  CONFIGURING  |                            |
51  * +---------------+----+                       |
52  *      |success        |                       |
53  *      |               |               +---------------+
54  *      |               |               |    CLOSING    |
55  *      |               |               +---------------+
56  *      |               |                       ^
57  *      V               |.dev_configure         |
58  * +---------------+----+                       |.dev_close
59  * |  CONFIGURED   |----------------------------+
60  * +---------------+<-----------+
61  *      |.dev_start             |
62  *      V                       |
63  * +---------------+            |
64  * |   STARTING    |------------^
65  * +---------------+ failed     |
66  *      |success                |
67  *      |               +---------------+
68  *      |               |   STOPPING    |
69  *      |               +---------------+
70  *      |                       ^
71  *      V                       |.dev_stop
72  * +---------------+------------+
73  * |    STARTED    |
74  * +---------------+
75  */
76 enum sfc_adapter_state {
77         SFC_ADAPTER_UNINITIALIZED = 0,
78         SFC_ADAPTER_INITIALIZED,
79         SFC_ADAPTER_CONFIGURING,
80         SFC_ADAPTER_CONFIGURED,
81         SFC_ADAPTER_CLOSING,
82         SFC_ADAPTER_STARTING,
83         SFC_ADAPTER_STARTED,
84         SFC_ADAPTER_STOPPING,
85
86         SFC_ADAPTER_NSTATES
87 };
88
89 enum sfc_dev_filter_mode {
90         SFC_DEV_FILTER_MODE_PROMISC = 0,
91         SFC_DEV_FILTER_MODE_ALLMULTI,
92
93         SFC_DEV_FILTER_NMODES
94 };
95
96 struct sfc_intr {
97         efx_intr_type_t                 type;
98         rte_intr_callback_fn            handler;
99         boolean_t                       lsc_intr;
100         boolean_t                       rxq_intr;
101 };
102
103 struct sfc_rxq;
104 struct sfc_txq;
105
106 struct sfc_rxq_info;
107 struct sfc_txq_info;
108 struct sfc_dp_rx;
109
110 struct sfc_port {
111         unsigned int                    lsc_seq;
112
113         uint32_t                        phy_adv_cap_mask;
114         uint32_t                        phy_adv_cap;
115
116         unsigned int                    flow_ctrl;
117         boolean_t                       flow_ctrl_autoneg;
118         size_t                          pdu;
119
120         /*
121          * Flow API isolated mode overrides promisc and allmulti settings;
122          * they won't be applied if isolated mode is active
123          */
124         boolean_t                       promisc;
125         boolean_t                       allmulti;
126
127         struct rte_ether_addr           default_mac_addr;
128
129         unsigned int                    max_mcast_addrs;
130         unsigned int                    nb_mcast_addrs;
131         uint8_t                         *mcast_addrs;
132
133         uint64_t                        *mac_stats_buf;
134         unsigned int                    mac_stats_nb_supported;
135         efsys_mem_t                     mac_stats_dma_mem;
136         boolean_t                       mac_stats_reset_pending;
137         uint16_t                        mac_stats_update_period_ms;
138         uint32_t                        mac_stats_update_generation;
139         boolean_t                       mac_stats_periodic_dma_supported;
140         uint64_t                        mac_stats_last_request_timestamp;
141
142         uint32_t                mac_stats_mask[EFX_MAC_STATS_MASK_NPAGES];
143
144         uint64_t                        ipackets;
145 };
146
147 struct sfc_rss_hf_rte_to_efx {
148         uint64_t                        rte;
149         efx_rx_hash_type_t              efx;
150 };
151
152 struct sfc_rss {
153         unsigned int                    channels;
154         efx_rx_scale_context_type_t     context_type;
155         efx_rx_hash_support_t           hash_support;
156         efx_rx_hash_alg_t               hash_alg;
157         unsigned int                    hf_map_nb_entries;
158         struct sfc_rss_hf_rte_to_efx    *hf_map;
159
160         efx_rx_hash_type_t              hash_types;
161         unsigned int                    tbl[EFX_RSS_TBL_SIZE];
162         uint8_t                         key[EFX_RSS_KEY_SIZE];
163
164         uint32_t                        dummy_rss_context;
165 };
166
167 /* Adapter private data shared by primary and secondary processes */
168 struct sfc_adapter_shared {
169         unsigned int                    rxq_count;
170         struct sfc_rxq_info             *rxq_info;
171         unsigned int                    ethdev_rxq_count;
172
173         unsigned int                    txq_count;
174         struct sfc_txq_info             *txq_info;
175         unsigned int                    ethdev_txq_count;
176
177         struct sfc_rss                  rss;
178
179         boolean_t                       isolated;
180         uint32_t                        tunnel_encaps;
181
182         char                            log_prefix[SFC_LOG_PREFIX_MAX];
183         struct rte_pci_addr             pci_addr;
184         uint16_t                        port_id;
185
186         char                            *dp_rx_name;
187         char                            *dp_tx_name;
188
189         bool                            counters_rxq_allocated;
190 };
191
192 /* Adapter process private data */
193 struct sfc_adapter_priv {
194         struct sfc_adapter_shared       *shared;
195         const struct sfc_dp_rx          *dp_rx;
196         const struct sfc_dp_tx          *dp_tx;
197         uint32_t                        logtype_main;
198 };
199
200 static inline struct sfc_adapter_priv *
201 sfc_adapter_priv_by_eth_dev(struct rte_eth_dev *eth_dev)
202 {
203         struct sfc_adapter_priv *sap = eth_dev->process_private;
204
205         SFC_ASSERT(sap != NULL);
206         return sap;
207 }
208
209 /* RxQ dedicated for counters (counter only RxQ) data */
210 struct sfc_counter_rxq {
211         unsigned int                    state;
212 #define SFC_COUNTER_RXQ_ATTACHED                0x1
213 #define SFC_COUNTER_RXQ_INITIALIZED             0x2
214         sfc_sw_index_t                  sw_index;
215         struct rte_mempool              *mp;
216 };
217
218 /* Adapter private data */
219 struct sfc_adapter {
220         /*
221          * It must be the first field of the sfc_adapter structure since
222          * sfc_adapter is the primary process private data (i.e.  process
223          * private data plus additional primary process specific data).
224          */
225         struct sfc_adapter_priv         priv;
226
227         /*
228          * PMD setup and configuration is not thread safe. Since it is not
229          * performance sensitive, it is better to guarantee thread-safety
230          * and add device level lock. Adapter control operations which
231          * change its state should acquire the lock.
232          */
233         rte_spinlock_t                  lock;
234         enum sfc_adapter_state          state;
235         struct rte_eth_dev              *eth_dev;
236         struct rte_kvargs               *kvargs;
237         int                             socket_id;
238         efsys_bar_t                     mem_bar;
239         /* Function control window offset */
240         efsys_dma_addr_t                fcw_offset;
241         efx_family_t                    family;
242         efx_nic_t                       *nic;
243         rte_spinlock_t                  nic_lock;
244         rte_atomic32_t                  restart_required;
245
246         struct sfc_efx_mcdi             mcdi;
247         struct sfc_sriov                sriov;
248         struct sfc_intr                 intr;
249         struct sfc_port                 port;
250         struct sfc_filter               filter;
251         struct sfc_mae                  mae;
252
253         struct sfc_flow_list            flow_list;
254
255         unsigned int                    rxq_max;
256         unsigned int                    txq_max;
257
258         unsigned int                    rxq_max_entries;
259         unsigned int                    rxq_min_entries;
260
261         unsigned int                    txq_max_entries;
262         unsigned int                    txq_min_entries;
263
264         unsigned int                    evq_max_entries;
265         unsigned int                    evq_min_entries;
266
267         uint32_t                        evq_flags;
268         unsigned int                    evq_count;
269
270         unsigned int                    mgmt_evq_index;
271         /*
272          * The lock is used to serialise management event queue polling
273          * which can be done from different context. Also the lock
274          * guarantees that mgmt_evq_running is preserved while the lock
275          * is held. It is used to serialise polling and start/stop
276          * operations.
277          *
278          * Locks which may be held when the lock is acquired:
279          *  - adapter lock, when:
280          *    - device start/stop to change mgmt_evq_running
281          *    - any control operations in client side MCDI proxy handling to
282          *      poll management event queue waiting for proxy response
283          *  - MCDI lock, when:
284          *    - any control operations in client side MCDI proxy handling to
285          *      poll management event queue waiting for proxy response
286          *
287          * Locks which are acquired with the lock held:
288          *  - nic_lock, when:
289          *    - MC event processing on management event queue polling
290          *      (e.g. MC REBOOT or BADASSERT events)
291          */
292         rte_spinlock_t                  mgmt_evq_lock;
293         bool                            mgmt_evq_running;
294         struct sfc_evq                  *mgmt_evq;
295
296         struct sfc_counter_rxq          counter_rxq;
297
298         struct sfc_rxq                  *rxq_ctrl;
299         struct sfc_txq                  *txq_ctrl;
300
301         boolean_t                       tso;
302         boolean_t                       tso_encap;
303
304         uint32_t                        rxd_wait_timeout_ns;
305 };
306
307 static inline struct sfc_adapter_shared *
308 sfc_adapter_shared_by_eth_dev(struct rte_eth_dev *eth_dev)
309 {
310         struct sfc_adapter_shared *sas = eth_dev->data->dev_private;
311
312         return sas;
313 }
314
315 static inline struct sfc_adapter *
316 sfc_adapter_by_eth_dev(struct rte_eth_dev *eth_dev)
317 {
318         struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(eth_dev);
319
320         SFC_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
321
322         return container_of(sap, struct sfc_adapter, priv);
323 }
324
325 static inline struct sfc_adapter_shared *
326 sfc_sa2shared(struct sfc_adapter *sa)
327 {
328         return sa->priv.shared;
329 }
330
331 /*
332  * Add wrapper functions to acquire/release lock to be able to remove or
333  * change the lock in one place.
334  */
335
336 static inline void
337 sfc_adapter_lock_init(struct sfc_adapter *sa)
338 {
339         rte_spinlock_init(&sa->lock);
340 }
341
342 static inline int
343 sfc_adapter_is_locked(struct sfc_adapter *sa)
344 {
345         return rte_spinlock_is_locked(&sa->lock);
346 }
347
348 static inline void
349 sfc_adapter_lock(struct sfc_adapter *sa)
350 {
351         rte_spinlock_lock(&sa->lock);
352 }
353
354 static inline int
355 sfc_adapter_trylock(struct sfc_adapter *sa)
356 {
357         return rte_spinlock_trylock(&sa->lock);
358 }
359
360 static inline void
361 sfc_adapter_unlock(struct sfc_adapter *sa)
362 {
363         rte_spinlock_unlock(&sa->lock);
364 }
365
366 static inline void
367 sfc_adapter_lock_fini(__rte_unused struct sfc_adapter *sa)
368 {
369         /* Just for symmetry of the API */
370 }
371
372 static inline unsigned int
373 sfc_nb_counter_rxq(const struct sfc_adapter_shared *sas)
374 {
375         return sas->counters_rxq_allocated ? 1 : 0;
376 }
377
378 /** Get the number of milliseconds since boot from the default timer */
379 static inline uint64_t
380 sfc_get_system_msecs(void)
381 {
382         return rte_get_timer_cycles() * MS_PER_S / rte_get_timer_hz();
383 }
384
385 int sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
386                   size_t len, int socket_id, efsys_mem_t *esmp);
387 void sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp);
388
389 uint32_t sfc_register_logtype(const struct rte_pci_addr *pci_addr,
390                               const char *lt_prefix_str,
391                               uint32_t ll_default);
392
393 int sfc_probe(struct sfc_adapter *sa);
394 void sfc_unprobe(struct sfc_adapter *sa);
395 int sfc_attach(struct sfc_adapter *sa);
396 void sfc_detach(struct sfc_adapter *sa);
397 int sfc_start(struct sfc_adapter *sa);
398 void sfc_stop(struct sfc_adapter *sa);
399
400 void sfc_schedule_restart(struct sfc_adapter *sa);
401
402 int sfc_mcdi_init(struct sfc_adapter *sa);
403 void sfc_mcdi_fini(struct sfc_adapter *sa);
404
405 int sfc_configure(struct sfc_adapter *sa);
406 void sfc_close(struct sfc_adapter *sa);
407
408 int sfc_intr_attach(struct sfc_adapter *sa);
409 void sfc_intr_detach(struct sfc_adapter *sa);
410 int sfc_intr_configure(struct sfc_adapter *sa);
411 void sfc_intr_close(struct sfc_adapter *sa);
412 int sfc_intr_start(struct sfc_adapter *sa);
413 void sfc_intr_stop(struct sfc_adapter *sa);
414
415 int sfc_port_attach(struct sfc_adapter *sa);
416 void sfc_port_detach(struct sfc_adapter *sa);
417 int sfc_port_configure(struct sfc_adapter *sa);
418 void sfc_port_close(struct sfc_adapter *sa);
419 int sfc_port_start(struct sfc_adapter *sa);
420 void sfc_port_stop(struct sfc_adapter *sa);
421 void sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
422                                 struct rte_eth_link *link_info);
423 int sfc_port_update_mac_stats(struct sfc_adapter *sa);
424 int sfc_port_reset_mac_stats(struct sfc_adapter *sa);
425 int sfc_set_rx_mode(struct sfc_adapter *sa);
426 int sfc_set_rx_mode_unchecked(struct sfc_adapter *sa);
427
428 struct sfc_hw_switch_id;
429
430 int sfc_hw_switch_id_init(struct sfc_adapter *sa,
431                           struct sfc_hw_switch_id **idp);
432 void sfc_hw_switch_id_fini(struct sfc_adapter *sa,
433                            struct sfc_hw_switch_id *idp);
434 bool sfc_hw_switch_ids_equal(const struct sfc_hw_switch_id *left,
435                              const struct sfc_hw_switch_id *right);
436
437 #ifdef __cplusplus
438 }
439 #endif
440
441 #endif  /* _SFC_H */