net/sfc: support MCDI proxy
[dpdk.git] / drivers / net / sfc / sfc.h
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef _SFC_H
31 #define _SFC_H
32
33 #include <stdbool.h>
34
35 #include <rte_ethdev.h>
36 #include <rte_kvargs.h>
37 #include <rte_spinlock.h>
38
39 #include "efx.h"
40
41 #include "sfc_filter.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #define SFC_DEV_TO_PCI(eth_dev) \
48         RTE_DEV_TO_PCI((eth_dev)->device)
49
50 #if EFSYS_OPT_RX_SCALE
51 /** RSS key length (bytes) */
52 #define SFC_RSS_KEY_SIZE        40
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                       |
68  * +---------------+            |
69  * |  CONFIGURING  |------------^
70  * +---------------+ failed     |
71  *      |success                |
72  *      |               +---------------+
73  *      |               |    CLOSING    |
74  *      |               +---------------+
75  *      |                       ^
76  *      V                       |.dev_close
77  * +---------------+------------+
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_evq_info;
141 struct sfc_rxq_info;
142 struct sfc_txq_info;
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         boolean_t                       promisc;
155         boolean_t                       allmulti;
156
157         unsigned int                    max_mcast_addrs;
158         unsigned int                    nb_mcast_addrs;
159         uint8_t                         *mcast_addrs;
160
161         rte_spinlock_t                  mac_stats_lock;
162         uint64_t                        *mac_stats_buf;
163         efsys_mem_t                     mac_stats_dma_mem;
164         boolean_t                       mac_stats_reset_pending;
165         uint16_t                        mac_stats_update_period_ms;
166         uint32_t                        mac_stats_update_generation;
167         boolean_t                       mac_stats_periodic_dma_supported;
168         uint64_t                        mac_stats_last_request_timestamp;
169
170         uint32_t                mac_stats_mask[EFX_MAC_STATS_MASK_NPAGES];
171 };
172
173 /* Adapter private data */
174 struct sfc_adapter {
175         /*
176          * PMD setup and configuration is not thread safe. Since it is not
177          * performance sensitive, it is better to guarantee thread-safety
178          * and add device level lock. Adapter control operations which
179          * change its state should acquire the lock.
180          */
181         rte_spinlock_t                  lock;
182         enum sfc_adapter_state          state;
183         struct rte_eth_dev              *eth_dev;
184         struct rte_kvargs               *kvargs;
185         bool                            debug_init;
186         int                             socket_id;
187         efsys_bar_t                     mem_bar;
188         efx_family_t                    family;
189         efx_nic_t                       *nic;
190         rte_spinlock_t                  nic_lock;
191
192         struct sfc_mcdi                 mcdi;
193         struct sfc_intr                 intr;
194         struct sfc_port                 port;
195         struct sfc_filter               filter;
196
197         unsigned int                    rxq_max;
198         unsigned int                    txq_max;
199
200         unsigned int                    txq_max_entries;
201
202         uint32_t                        evq_flags;
203         unsigned int                    evq_count;
204         struct sfc_evq_info             *evq_info;
205
206         unsigned int                    mgmt_evq_index;
207         rte_spinlock_t                  mgmt_evq_lock;
208
209         unsigned int                    rxq_count;
210         struct sfc_rxq_info             *rxq_info;
211
212         unsigned int                    txq_count;
213         struct sfc_txq_info             *txq_info;
214
215         boolean_t                       tso;
216
217         unsigned int                    rss_channels;
218
219 #if EFSYS_OPT_RX_SCALE
220         efx_rx_scale_support_t          rss_support;
221         efx_rx_hash_support_t           hash_support;
222         efx_rx_hash_type_t              rss_hash_types;
223         unsigned int                    rss_tbl[EFX_RSS_TBL_SIZE];
224         uint8_t                         rss_key[SFC_RSS_KEY_SIZE];
225 #endif
226 };
227
228 /*
229  * Add wrapper functions to acquire/release lock to be able to remove or
230  * change the lock in one place.
231  */
232
233 static inline void
234 sfc_adapter_lock_init(struct sfc_adapter *sa)
235 {
236         rte_spinlock_init(&sa->lock);
237 }
238
239 static inline int
240 sfc_adapter_is_locked(struct sfc_adapter *sa)
241 {
242         return rte_spinlock_is_locked(&sa->lock);
243 }
244
245 static inline void
246 sfc_adapter_lock(struct sfc_adapter *sa)
247 {
248         rte_spinlock_lock(&sa->lock);
249 }
250
251 static inline int
252 sfc_adapter_trylock(struct sfc_adapter *sa)
253 {
254         return rte_spinlock_trylock(&sa->lock);
255 }
256
257 static inline void
258 sfc_adapter_unlock(struct sfc_adapter *sa)
259 {
260         rte_spinlock_unlock(&sa->lock);
261 }
262
263 static inline void
264 sfc_adapter_lock_fini(__rte_unused struct sfc_adapter *sa)
265 {
266         /* Just for symmetry of the API */
267 }
268
269 /** Get the number of milliseconds since boot from the default timer */
270 static inline uint64_t
271 sfc_get_system_msecs(void)
272 {
273         return rte_get_timer_cycles() * MS_PER_S / rte_get_timer_hz();
274 }
275
276 int sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
277                   size_t len, int socket_id, efsys_mem_t *esmp);
278 void sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp);
279
280 int sfc_attach(struct sfc_adapter *sa);
281 void sfc_detach(struct sfc_adapter *sa);
282 int sfc_start(struct sfc_adapter *sa);
283 void sfc_stop(struct sfc_adapter *sa);
284
285 int sfc_mcdi_init(struct sfc_adapter *sa);
286 void sfc_mcdi_fini(struct sfc_adapter *sa);
287
288 int sfc_configure(struct sfc_adapter *sa);
289 void sfc_close(struct sfc_adapter *sa);
290
291 int sfc_intr_attach(struct sfc_adapter *sa);
292 void sfc_intr_detach(struct sfc_adapter *sa);
293 int sfc_intr_init(struct sfc_adapter *sa);
294 void sfc_intr_fini(struct sfc_adapter *sa);
295 int sfc_intr_start(struct sfc_adapter *sa);
296 void sfc_intr_stop(struct sfc_adapter *sa);
297
298 int sfc_port_init(struct sfc_adapter *sa);
299 void sfc_port_fini(struct sfc_adapter *sa);
300 int sfc_port_start(struct sfc_adapter *sa);
301 void sfc_port_stop(struct sfc_adapter *sa);
302 void sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
303                                 struct rte_eth_link *link_info);
304 int sfc_port_update_mac_stats(struct sfc_adapter *sa);
305 int sfc_port_reset_mac_stats(struct sfc_adapter *sa);
306 int sfc_set_rx_mode(struct sfc_adapter *sa);
307
308
309 #ifdef __cplusplus
310 }
311 #endif
312
313 #endif  /* _SFC_H */