1 /*
2 * BSD LICENSE
3 *
4 * Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Cavium, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /* \file lio_network.h
35 * \brief Host NIC Driver: Structure and Macro definitions used by NIC Module.
36 */
37
38 #ifndef __LIO_NETWORK_H__
39 #define __LIO_NETWORK_H__
40
41 #include "lio_rss.h"
42
43 #define LIO_MIN_MTU_SIZE 72
44 #define LIO_MAX_MTU_SIZE (LIO_MAX_FRM_SIZE - LIO_FRM_HEADER_SIZE)
45
46 #define LIO_MAX_SG 64
47 #define LIO_MAX_FRAME_SIZE 60000
48
49 struct lio_fw_stats_resp {
50 uint64_t rh;
51 struct octeon_link_stats stats;
52 uint64_t status;
53 };
54
55 /* LiquidIO per-interface network private data */
56 struct lio {
57 /* State of the interface. Rx/Tx happens only in the RUNNING state. */
58 int ifstate;
59
60 /*
61 * Octeon Interface index number. This device will be represented as
62 * oct<ifidx> in the system.
63 */
64 int ifidx;
65
66 /* Octeon Input queue to use to transmit for this network interface. */
67 int txq;
68
69 /*
70 * Octeon Output queue from which pkts arrive
71 * for this network interface.
72 */
73 int rxq;
74
75 /* Guards each glist */
76 struct mtx *glist_lock;
77
78 #define LIO_DEFAULT_STATS_INTERVAL 10000
79 /* callout timer for stats */
80 struct callout stats_timer;
81
82 /* Stats Update Interval in milli Seconds */
83 uint16_t stats_interval;
84
85 /* IRQ coalescing driver stats */
86 struct octeon_intrmod_cfg intrmod_cfg;
87
88 /* Array of gather component linked lists */
89 struct lio_stailq_head *ghead;
90 void **glists_virt_base;
91 vm_paddr_t *glists_dma_base;
92 uint32_t glist_entry_size;
93
94 /* Pointer to the octeon device structure. */
95 struct octeon_device *oct_dev;
96
97 if_t ifp;
98 struct ifmedia ifmedia;
99 int if_flags;
100
101 /* Link information sent by the core application for this interface. */
102 struct octeon_link_info linfo;
103
104 /* counter of link changes */
105 uint64_t link_changes;
106
107 /* Size of Tx queue for this octeon device. */
108 uint32_t tx_qsize;
109
110 /* Size of Rx queue for this octeon device. */
111 uint32_t rx_qsize;
112
113 /* Size of MTU this octeon device. */
114 uint32_t mtu;
115
116 /* msg level flag per interface. */
117 uint32_t msg_enable;
118
119 /* Interface info */
120 uint32_t intf_open;
121
122 /* task queue for rx oom status */
123 struct lio_tq rx_status_tq;
124
125 /* VLAN Filtering related */
126 eventhandler_tag vlan_attach;
127 eventhandler_tag vlan_detach;
128 struct lio_rss_params_set rss_set;
129 };
130
131 #define LIO_MAX_CORES 12
132
133 /*
134 * \brief Enable or disable feature
135 * @param ifp pointer to network device
136 * @param cmd Command that just requires acknowledgment
137 * @param param1 Parameter to command
138 */
139 int lio_set_feature(if_t ifp, int cmd, uint16_t param1);
140
141 /*
142 * \brief Link control command completion callback
143 * @param nctrl_ptr pointer to control packet structure
144 *
145 * This routine is called by the callback function when a ctrl pkt sent to
146 * core app completes. The nctrl_ptr contains a copy of the command type
147 * and data sent to the core app. This routine is only called if the ctrl
148 * pkt was sent successfully to the core app.
149 */
150 void lio_ctrl_cmd_completion(void *nctrl_ptr);
151
152 int lio_setup_io_queues(struct octeon_device *octeon_dev, int ifidx,
153 uint32_t num_iqs, uint32_t num_oqs);
154
155 int lio_setup_interrupt(struct octeon_device *oct, uint32_t num_ioqs);
156
157 static inline void *
lio_recv_buffer_alloc(uint32_t size)158 lio_recv_buffer_alloc(uint32_t size)
159 {
160 struct mbuf *mb = NULL;
161
162 mb = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
163 if (mb != NULL)
164 mb->m_pkthdr.len = mb->m_len = size;
165
166 return ((void *)mb);
167 }
168
169 static inline void
lio_recv_buffer_free(void * buffer)170 lio_recv_buffer_free(void *buffer)
171 {
172
173 m_freem((struct mbuf *)buffer);
174 }
175
176 static inline int
lio_get_order(unsigned long size)177 lio_get_order(unsigned long size)
178 {
179 int order;
180
181 size = (size - 1) >> PAGE_SHIFT;
182 order = 0;
183 while (size) {
184 order++;
185 size >>= 1;
186 }
187
188 return (order);
189 }
190
191 static inline void *
lio_dma_alloc(size_t size,vm_paddr_t * dma_handle)192 lio_dma_alloc(size_t size, vm_paddr_t *dma_handle)
193 {
194 size_t align;
195 void *mem;
196
197 align = PAGE_SIZE << lio_get_order(size);
198 mem = kmem_alloc_contig(size, M_WAITOK, 0, ~0ul, align, 0,
199 VM_MEMATTR_DEFAULT);
200 if (mem != NULL)
201 *dma_handle = vtophys(mem);
202 else
203 *dma_handle = 0;
204
205 return (mem);
206 }
207
208 static inline void
lio_dma_free(size_t size,void * cpu_addr)209 lio_dma_free(size_t size, void *cpu_addr)
210 {
211
212 kmem_free(cpu_addr, size);
213 }
214
215 static inline uint64_t
lio_map_ring(device_t dev,void * buf,uint32_t size)216 lio_map_ring(device_t dev, void *buf, uint32_t size)
217 {
218 vm_paddr_t dma_addr;
219
220 dma_addr = vtophys(((struct mbuf *)buf)->m_data);
221 return ((uint64_t)dma_addr);
222 }
223
224 /*
225 * \brief check interface state
226 * @param lio per-network private data
227 * @param state_flag flag state to check
228 */
229 static inline int
lio_ifstate_check(struct lio * lio,int state_flag)230 lio_ifstate_check(struct lio *lio, int state_flag)
231 {
232
233 return (atomic_load_acq_int(&lio->ifstate) & state_flag);
234 }
235
236 /*
237 * \brief set interface state
238 * @param lio per-network private data
239 * @param state_flag flag state to set
240 */
241 static inline void
lio_ifstate_set(struct lio * lio,int state_flag)242 lio_ifstate_set(struct lio *lio, int state_flag)
243 {
244
245 atomic_store_rel_int(&lio->ifstate,
246 (atomic_load_acq_int(&lio->ifstate) | state_flag));
247 }
248
249 /*
250 * \brief clear interface state
251 * @param lio per-network private data
252 * @param state_flag flag state to clear
253 */
254 static inline void
lio_ifstate_reset(struct lio * lio,int state_flag)255 lio_ifstate_reset(struct lio *lio, int state_flag)
256 {
257
258 atomic_store_rel_int(&lio->ifstate,
259 (atomic_load_acq_int(&lio->ifstate) &
260 ~(state_flag)));
261 }
262
263 /*
264 * \brief wait for all pending requests to complete
265 * @param oct Pointer to Octeon device
266 *
267 * Called during shutdown sequence
268 */
269 static inline int
lio_wait_for_pending_requests(struct octeon_device * oct)270 lio_wait_for_pending_requests(struct octeon_device *oct)
271 {
272 int i, pcount = 0;
273
274 for (i = 0; i < 100; i++) {
275 pcount = atomic_load_acq_int(
276 &oct->response_list[LIO_ORDERED_SC_LIST].
277 pending_req_count);
278 if (pcount)
279 lio_sleep_timeout(100);
280 else
281 break;
282 }
283
284 if (pcount)
285 return (1);
286
287 return (0);
288 }
289
290 #endif /* __LIO_NETWORK_H__ */
291