net/sfc: add init on attach
[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  */
58 enum sfc_adapter_state {
59         SFC_ADAPTER_UNINITIALIZED = 0,
60         SFC_ADAPTER_INITIALIZED,
61
62         SFC_ADAPTER_NSTATES
63 };
64
65 enum sfc_mcdi_state {
66         SFC_MCDI_UNINITIALIZED = 0,
67         SFC_MCDI_INITIALIZED,
68         SFC_MCDI_BUSY,
69         SFC_MCDI_COMPLETED,
70
71         SFC_MCDI_NSTATES
72 };
73
74 struct sfc_mcdi {
75         rte_spinlock_t                  lock;
76         efsys_mem_t                     mem;
77         enum sfc_mcdi_state             state;
78         efx_mcdi_transport_t            transport;
79 };
80
81 /* Adapter private data */
82 struct sfc_adapter {
83         /*
84          * PMD setup and configuration is not thread safe.
85          * Since it is not performance sensitive, it is better to guarantee
86          * thread-safety and add device level lock.
87          * Adapter control operations which change its state should
88          * acquire the lock.
89          */
90         rte_spinlock_t                  lock;
91         enum sfc_adapter_state          state;
92         struct rte_eth_dev              *eth_dev;
93         struct rte_kvargs               *kvargs;
94         bool                            debug_init;
95         int                             socket_id;
96         efsys_bar_t                     mem_bar;
97         efx_family_t                    family;
98         efx_nic_t                       *nic;
99         rte_spinlock_t                  nic_lock;
100
101         struct sfc_mcdi                 mcdi;
102
103         unsigned int                    rxq_max;
104         unsigned int                    txq_max;
105 };
106
107 /*
108  * Add wrapper functions to acquire/release lock to be able to remove or
109  * change the lock in one place.
110  */
111
112 static inline void
113 sfc_adapter_lock_init(struct sfc_adapter *sa)
114 {
115         rte_spinlock_init(&sa->lock);
116 }
117
118 static inline int
119 sfc_adapter_is_locked(struct sfc_adapter *sa)
120 {
121         return rte_spinlock_is_locked(&sa->lock);
122 }
123
124 static inline void
125 sfc_adapter_lock(struct sfc_adapter *sa)
126 {
127         rte_spinlock_lock(&sa->lock);
128 }
129
130 static inline void
131 sfc_adapter_unlock(struct sfc_adapter *sa)
132 {
133         rte_spinlock_unlock(&sa->lock);
134 }
135
136 static inline void
137 sfc_adapter_lock_fini(__rte_unused struct sfc_adapter *sa)
138 {
139         /* Just for symmetry of the API */
140 }
141
142 int sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
143                   size_t len, int socket_id, efsys_mem_t *esmp);
144 void sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp);
145
146 int sfc_attach(struct sfc_adapter *sa);
147 void sfc_detach(struct sfc_adapter *sa);
148
149 int sfc_mcdi_init(struct sfc_adapter *sa);
150 void sfc_mcdi_fini(struct sfc_adapter *sa);
151
152 #ifdef __cplusplus
153 }
154 #endif
155
156 #endif  /* _SFC_H */