New upstream version 17.11.1
[deb_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
43 #include "efx.h"
44
45 #include "sfc_filter.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #if EFSYS_OPT_RX_SCALE
52 /** RSS hash offloads mask */
53 #define SFC_RSS_OFFLOADS        (ETH_RSS_IP | ETH_RSS_TCP)
54 #endif
55
56 /*
57  * +---------------+
58  * | UNINITIALIZED |<-----------+
59  * +---------------+            |
60  *      |.eth_dev_init          |.eth_dev_uninit
61  *      V                       |
62  * +---------------+------------+
63  * |  INITIALIZED  |
64  * +---------------+<-----------<---------------+
65  *      |.dev_configure         |               |
66  *      V                       |failed         |
67  * +---------------+------------+               |
68  * |  CONFIGURING  |                            |
69  * +---------------+----+                       |
70  *      |success        |                       |
71  *      |               |               +---------------+
72  *      |               |               |    CLOSING    |
73  *      |               |               +---------------+
74  *      |               |                       ^
75  *      V               |.dev_configure         |
76  * +---------------+----+                       |.dev_close
77  * |  CONFIGURED   |----------------------------+
78  * +---------------+<-----------+
79  *      |.dev_start             |
80  *      V                       |
81  * +---------------+            |
82  * |   STARTING    |------------^
83  * +---------------+ failed     |
84  *      |success                |
85  *      |               +---------------+
86  *      |               |   STOPPING    |
87  *      |               +---------------+
88  *      |                       ^
89  *      V                       |.dev_stop
90  * +---------------+------------+
91  * |    STARTED    |
92  * +---------------+
93  */
94 enum sfc_adapter_state {
95         SFC_ADAPTER_UNINITIALIZED = 0,
96         SFC_ADAPTER_INITIALIZED,
97         SFC_ADAPTER_CONFIGURING,
98         SFC_ADAPTER_CONFIGURED,
99         SFC_ADAPTER_CLOSING,
100         SFC_ADAPTER_STARTING,
101         SFC_ADAPTER_STARTED,
102         SFC_ADAPTER_STOPPING,
103
104         SFC_ADAPTER_NSTATES
105 };
106
107 enum sfc_dev_filter_mode {
108         SFC_DEV_FILTER_MODE_PROMISC = 0,
109         SFC_DEV_FILTER_MODE_ALLMULTI,
110
111         SFC_DEV_FILTER_NMODES
112 };
113
114 enum sfc_mcdi_state {
115         SFC_MCDI_UNINITIALIZED = 0,
116         SFC_MCDI_INITIALIZED,
117         SFC_MCDI_BUSY,
118         SFC_MCDI_COMPLETED,
119
120         SFC_MCDI_NSTATES
121 };
122
123 struct sfc_mcdi {
124         rte_spinlock_t                  lock;
125         efsys_mem_t                     mem;
126         enum sfc_mcdi_state             state;
127         efx_mcdi_transport_t            transport;
128         bool                            logging;
129         uint32_t                        proxy_handle;
130         efx_rc_t                        proxy_result;
131 };
132
133 struct sfc_intr {
134         efx_intr_type_t                 type;
135         rte_intr_callback_fn            handler;
136         boolean_t                       lsc_intr;
137 };
138
139 struct sfc_rxq_info;
140 struct sfc_txq_info;
141 struct sfc_dp_rx;
142
143 struct sfc_port {
144         unsigned int                    lsc_seq;
145
146         uint32_t                        phy_adv_cap_mask;
147         uint32_t                        phy_adv_cap;
148
149         unsigned int                    flow_ctrl;
150         boolean_t                       flow_ctrl_autoneg;
151         size_t                          pdu;
152
153         /*
154          * Flow API isolated mode overrides promisc and allmulti settings;
155          * they won't be applied if isolated mode is active
156          */
157         boolean_t                       isolated;
158         boolean_t                       promisc;
159         boolean_t                       allmulti;
160
161         struct ether_addr               default_mac_addr;
162
163         unsigned int                    max_mcast_addrs;
164         unsigned int                    nb_mcast_addrs;
165         uint8_t                         *mcast_addrs;
166
167         rte_spinlock_t                  mac_stats_lock;
168         uint64_t                        *mac_stats_buf;
169         unsigned int                    mac_stats_nb_supported;
170         efsys_mem_t                     mac_stats_dma_mem;
171         boolean_t                       mac_stats_reset_pending;
172         uint16_t                        mac_stats_update_period_ms;
173         uint32_t                        mac_stats_update_generation;
174         boolean_t                       mac_stats_periodic_dma_supported;
175         uint64_t                        mac_stats_last_request_timestamp;
176
177         uint32_t                mac_stats_mask[EFX_MAC_STATS_MASK_NPAGES];
178 };
179
180 /* Adapter private data */
181 struct sfc_adapter {
182         /*
183          * PMD setup and configuration is not thread safe. Since it is not
184          * performance sensitive, it is better to guarantee thread-safety
185          * and add device level lock. Adapter control operations which
186          * change its state should acquire the lock.
187          */
188         rte_spinlock_t                  lock;
189         enum sfc_adapter_state          state;
190         struct rte_pci_addr             pci_addr;
191         uint16_t                        port_id;
192         struct rte_eth_dev              *eth_dev;
193         struct rte_kvargs               *kvargs;
194         bool                            debug_init;
195         int                             socket_id;
196         efsys_bar_t                     mem_bar;
197         efx_family_t                    family;
198         efx_nic_t                       *nic;
199         rte_spinlock_t                  nic_lock;
200
201         struct sfc_mcdi                 mcdi;
202         struct sfc_intr                 intr;
203         struct sfc_port                 port;
204         struct sfc_filter               filter;
205
206         unsigned int                    rxq_max;
207         unsigned int                    txq_max;
208
209         unsigned int                    txq_max_entries;
210
211         uint32_t                        evq_flags;
212         unsigned int                    evq_count;
213
214         unsigned int                    mgmt_evq_index;
215         /*
216          * The lock is used to serialise management event queue polling
217          * which can be done from different context. Also the lock
218          * guarantees that mgmt_evq_running is preserved while the lock
219          * is held. It is used to serialise polling and start/stop
220          * operations.
221          *
222          * Locks which may be held when the lock is acquired:
223          *  - adapter lock, when:
224          *    - device start/stop to change mgmt_evq_running
225          *    - any control operations in client side MCDI proxy handling to
226          *      poll management event queue waiting for proxy response
227          *  - MCDI lock, when:
228          *    - any control operations in client side MCDI proxy handling to
229          *      poll management event queue waiting for proxy response
230          *
231          * Locks which are acquired with the lock held:
232          *  - nic_lock, when:
233          *    - MC event processing on management event queue polling
234          *      (e.g. MC REBOOT or BADASSERT events)
235          */
236         rte_spinlock_t                  mgmt_evq_lock;
237         bool                            mgmt_evq_running;
238         struct sfc_evq                  *mgmt_evq;
239
240         unsigned int                    rxq_count;
241         struct sfc_rxq_info             *rxq_info;
242
243         unsigned int                    txq_count;
244         struct sfc_txq_info             *txq_info;
245
246         boolean_t                       tso;
247
248         unsigned int                    rss_channels;
249
250 #if EFSYS_OPT_RX_SCALE
251         efx_rx_scale_context_type_t     rss_support;
252         efx_rx_hash_support_t           hash_support;
253         efx_rx_hash_type_t              rss_hash_types;
254         unsigned int                    rss_tbl[EFX_RSS_TBL_SIZE];
255         uint8_t                         rss_key[EFX_RSS_KEY_SIZE];
256 #endif
257
258         /*
259          * Shared memory copy of the Rx datapath name to be used by
260          * the secondary process to find Rx datapath to be used.
261          */
262         char                            *dp_rx_name;
263         const struct sfc_dp_rx          *dp_rx;
264
265         /*
266          * Shared memory copy of the Tx datapath name to be used by
267          * the secondary process to find Rx datapath to be used.
268          */
269         char                            *dp_tx_name;
270         const struct sfc_dp_tx          *dp_tx;
271 };
272
273 /*
274  * Add wrapper functions to acquire/release lock to be able to remove or
275  * change the lock in one place.
276  */
277
278 static inline void
279 sfc_adapter_lock_init(struct sfc_adapter *sa)
280 {
281         rte_spinlock_init(&sa->lock);
282 }
283
284 static inline int
285 sfc_adapter_is_locked(struct sfc_adapter *sa)
286 {
287         return rte_spinlock_is_locked(&sa->lock);
288 }
289
290 static inline void
291 sfc_adapter_lock(struct sfc_adapter *sa)
292 {
293         rte_spinlock_lock(&sa->lock);
294 }
295
296 static inline int
297 sfc_adapter_trylock(struct sfc_adapter *sa)
298 {
299         return rte_spinlock_trylock(&sa->lock);
300 }
301
302 static inline void
303 sfc_adapter_unlock(struct sfc_adapter *sa)
304 {
305         rte_spinlock_unlock(&sa->lock);
306 }
307
308 static inline void
309 sfc_adapter_lock_fini(__rte_unused struct sfc_adapter *sa)
310 {
311         /* Just for symmetry of the API */
312 }
313
314 /** Get the number of milliseconds since boot from the default timer */
315 static inline uint64_t
316 sfc_get_system_msecs(void)
317 {
318         return rte_get_timer_cycles() * MS_PER_S / rte_get_timer_hz();
319 }
320
321 int sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
322                   size_t len, int socket_id, efsys_mem_t *esmp);
323 void sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp);
324
325 int sfc_probe(struct sfc_adapter *sa);
326 void sfc_unprobe(struct sfc_adapter *sa);
327 int sfc_attach(struct sfc_adapter *sa);
328 void sfc_detach(struct sfc_adapter *sa);
329 int sfc_start(struct sfc_adapter *sa);
330 void sfc_stop(struct sfc_adapter *sa);
331
332 int sfc_mcdi_init(struct sfc_adapter *sa);
333 void sfc_mcdi_fini(struct sfc_adapter *sa);
334
335 int sfc_configure(struct sfc_adapter *sa);
336 void sfc_close(struct sfc_adapter *sa);
337
338 int sfc_intr_attach(struct sfc_adapter *sa);
339 void sfc_intr_detach(struct sfc_adapter *sa);
340 int sfc_intr_configure(struct sfc_adapter *sa);
341 void sfc_intr_close(struct sfc_adapter *sa);
342 int sfc_intr_start(struct sfc_adapter *sa);
343 void sfc_intr_stop(struct sfc_adapter *sa);
344
345 int sfc_port_attach(struct sfc_adapter *sa);
346 void sfc_port_detach(struct sfc_adapter *sa);
347 int sfc_port_configure(struct sfc_adapter *sa);
348 void sfc_port_close(struct sfc_adapter *sa);
349 int sfc_port_start(struct sfc_adapter *sa);
350 void sfc_port_stop(struct sfc_adapter *sa);
351 void sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
352                                 struct rte_eth_link *link_info);
353 int sfc_port_update_mac_stats(struct sfc_adapter *sa);
354 int sfc_port_reset_mac_stats(struct sfc_adapter *sa);
355 int sfc_set_rx_mode(struct sfc_adapter *sa);
356
357
358 #ifdef __cplusplus
359 }
360 #endif
361
362 #endif  /* _SFC_H */