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