c2961ea11ab861d8a5217d4007552043b0c7aae5
[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_ethdev.h>
38 #include <rte_kvargs.h>
39 #include <rte_spinlock.h>
40
41 #include "efx.h"
42
43 #include "sfc_filter.h"
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #define SFC_DEV_TO_PCI(eth_dev) \
50         RTE_DEV_TO_PCI((eth_dev)->device)
51
52 #if EFSYS_OPT_RX_SCALE
53 /** RSS key length (bytes) */
54 #define SFC_RSS_KEY_SIZE        40
55 /** RSS hash offloads mask */
56 #define SFC_RSS_OFFLOADS        (ETH_RSS_IP | ETH_RSS_TCP)
57 #endif
58
59 /*
60  * +---------------+
61  * | UNINITIALIZED |<-----------+
62  * +---------------+            |
63  *      |.eth_dev_init          |.eth_dev_uninit
64  *      V                       |
65  * +---------------+------------+
66  * |  INITIALIZED  |
67  * +---------------+<-----------+
68  *      |.dev_configure         |
69  *      V                       |
70  * +---------------+            |
71  * |  CONFIGURING  |------------^
72  * +---------------+ failed     |
73  *      |success                |
74  *      |               +---------------+
75  *      |               |    CLOSING    |
76  *      |               +---------------+
77  *      |                       ^
78  *      V                       |.dev_close
79  * +---------------+------------+
80  * |  CONFIGURED   |
81  * +---------------+<-----------+
82  *      |.dev_start             |
83  *      V                       |
84  * +---------------+            |
85  * |   STARTING    |------------^
86  * +---------------+ failed     |
87  *      |success                |
88  *      |               +---------------+
89  *      |               |   STOPPING    |
90  *      |               +---------------+
91  *      |                       ^
92  *      V                       |.dev_stop
93  * +---------------+------------+
94  * |    STARTED    |
95  * +---------------+
96  */
97 enum sfc_adapter_state {
98         SFC_ADAPTER_UNINITIALIZED = 0,
99         SFC_ADAPTER_INITIALIZED,
100         SFC_ADAPTER_CONFIGURING,
101         SFC_ADAPTER_CONFIGURED,
102         SFC_ADAPTER_CLOSING,
103         SFC_ADAPTER_STARTING,
104         SFC_ADAPTER_STARTED,
105         SFC_ADAPTER_STOPPING,
106
107         SFC_ADAPTER_NSTATES
108 };
109
110 enum sfc_dev_filter_mode {
111         SFC_DEV_FILTER_MODE_PROMISC = 0,
112         SFC_DEV_FILTER_MODE_ALLMULTI,
113
114         SFC_DEV_FILTER_NMODES
115 };
116
117 enum sfc_mcdi_state {
118         SFC_MCDI_UNINITIALIZED = 0,
119         SFC_MCDI_INITIALIZED,
120         SFC_MCDI_BUSY,
121         SFC_MCDI_COMPLETED,
122
123         SFC_MCDI_NSTATES
124 };
125
126 struct sfc_mcdi {
127         rte_spinlock_t                  lock;
128         efsys_mem_t                     mem;
129         enum sfc_mcdi_state             state;
130         efx_mcdi_transport_t            transport;
131         bool                            logging;
132         uint32_t                        proxy_handle;
133         efx_rc_t                        proxy_result;
134 };
135
136 struct sfc_intr {
137         efx_intr_type_t                 type;
138         rte_intr_callback_fn            handler;
139         boolean_t                       lsc_intr;
140 };
141
142 struct sfc_evq_info;
143 struct sfc_rxq_info;
144 struct sfc_txq_info;
145 struct sfc_dp_rx;
146
147 struct sfc_port {
148         unsigned int                    lsc_seq;
149
150         uint32_t                        phy_adv_cap_mask;
151         uint32_t                        phy_adv_cap;
152
153         unsigned int                    flow_ctrl;
154         boolean_t                       flow_ctrl_autoneg;
155         size_t                          pdu;
156
157         boolean_t                       promisc;
158         boolean_t                       allmulti;
159
160         unsigned int                    max_mcast_addrs;
161         unsigned int                    nb_mcast_addrs;
162         uint8_t                         *mcast_addrs;
163
164         rte_spinlock_t                  mac_stats_lock;
165         uint64_t                        *mac_stats_buf;
166         efsys_mem_t                     mac_stats_dma_mem;
167         boolean_t                       mac_stats_reset_pending;
168         uint16_t                        mac_stats_update_period_ms;
169         uint32_t                        mac_stats_update_generation;
170         boolean_t                       mac_stats_periodic_dma_supported;
171         uint64_t                        mac_stats_last_request_timestamp;
172
173         uint32_t                mac_stats_mask[EFX_MAC_STATS_MASK_NPAGES];
174 };
175
176 /* Adapter private data */
177 struct sfc_adapter {
178         /*
179          * PMD setup and configuration is not thread safe. Since it is not
180          * performance sensitive, it is better to guarantee thread-safety
181          * and add device level lock. Adapter control operations which
182          * change its state should acquire the lock.
183          */
184         rte_spinlock_t                  lock;
185         enum sfc_adapter_state          state;
186         struct rte_eth_dev              *eth_dev;
187         struct rte_kvargs               *kvargs;
188         bool                            debug_init;
189         int                             socket_id;
190         efsys_bar_t                     mem_bar;
191         efx_family_t                    family;
192         efx_nic_t                       *nic;
193         rte_spinlock_t                  nic_lock;
194
195         struct sfc_mcdi                 mcdi;
196         struct sfc_intr                 intr;
197         struct sfc_port                 port;
198         struct sfc_filter               filter;
199
200         unsigned int                    rxq_max;
201         unsigned int                    txq_max;
202
203         unsigned int                    txq_max_entries;
204
205         uint32_t                        evq_flags;
206         unsigned int                    evq_count;
207         struct sfc_evq_info             *evq_info;
208
209         unsigned int                    mgmt_evq_index;
210         rte_spinlock_t                  mgmt_evq_lock;
211
212         unsigned int                    rxq_count;
213         struct sfc_rxq_info             *rxq_info;
214
215         unsigned int                    txq_count;
216         struct sfc_txq_info             *txq_info;
217
218         boolean_t                       tso;
219
220         unsigned int                    rss_channels;
221
222 #if EFSYS_OPT_RX_SCALE
223         efx_rx_scale_support_t          rss_support;
224         efx_rx_hash_support_t           hash_support;
225         efx_rx_hash_type_t              rss_hash_types;
226         unsigned int                    rss_tbl[EFX_RSS_TBL_SIZE];
227         uint8_t                         rss_key[SFC_RSS_KEY_SIZE];
228 #endif
229
230         const struct sfc_dp_rx          *dp_rx;
231         const struct sfc_dp_tx          *dp_tx;
232 };
233
234 /*
235  * Add wrapper functions to acquire/release lock to be able to remove or
236  * change the lock in one place.
237  */
238
239 static inline void
240 sfc_adapter_lock_init(struct sfc_adapter *sa)
241 {
242         rte_spinlock_init(&sa->lock);
243 }
244
245 static inline int
246 sfc_adapter_is_locked(struct sfc_adapter *sa)
247 {
248         return rte_spinlock_is_locked(&sa->lock);
249 }
250
251 static inline void
252 sfc_adapter_lock(struct sfc_adapter *sa)
253 {
254         rte_spinlock_lock(&sa->lock);
255 }
256
257 static inline int
258 sfc_adapter_trylock(struct sfc_adapter *sa)
259 {
260         return rte_spinlock_trylock(&sa->lock);
261 }
262
263 static inline void
264 sfc_adapter_unlock(struct sfc_adapter *sa)
265 {
266         rte_spinlock_unlock(&sa->lock);
267 }
268
269 static inline void
270 sfc_adapter_lock_fini(__rte_unused struct sfc_adapter *sa)
271 {
272         /* Just for symmetry of the API */
273 }
274
275 /** Get the number of milliseconds since boot from the default timer */
276 static inline uint64_t
277 sfc_get_system_msecs(void)
278 {
279         return rte_get_timer_cycles() * MS_PER_S / rte_get_timer_hz();
280 }
281
282 int sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
283                   size_t len, int socket_id, efsys_mem_t *esmp);
284 void sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp);
285
286 int sfc_attach(struct sfc_adapter *sa);
287 void sfc_detach(struct sfc_adapter *sa);
288 int sfc_start(struct sfc_adapter *sa);
289 void sfc_stop(struct sfc_adapter *sa);
290
291 int sfc_mcdi_init(struct sfc_adapter *sa);
292 void sfc_mcdi_fini(struct sfc_adapter *sa);
293
294 int sfc_configure(struct sfc_adapter *sa);
295 void sfc_close(struct sfc_adapter *sa);
296
297 int sfc_intr_attach(struct sfc_adapter *sa);
298 void sfc_intr_detach(struct sfc_adapter *sa);
299 int sfc_intr_init(struct sfc_adapter *sa);
300 void sfc_intr_fini(struct sfc_adapter *sa);
301 int sfc_intr_start(struct sfc_adapter *sa);
302 void sfc_intr_stop(struct sfc_adapter *sa);
303
304 int sfc_port_init(struct sfc_adapter *sa);
305 void sfc_port_fini(struct sfc_adapter *sa);
306 int sfc_port_start(struct sfc_adapter *sa);
307 void sfc_port_stop(struct sfc_adapter *sa);
308 void sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
309                                 struct rte_eth_link *link_info);
310 int sfc_port_update_mac_stats(struct sfc_adapter *sa);
311 int sfc_port_reset_mac_stats(struct sfc_adapter *sa);
312 int sfc_set_rx_mode(struct sfc_adapter *sa);
313
314
315 #ifdef __cplusplus
316 }
317 #endif
318
319 #endif  /* _SFC_H */