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 #ifdef RSS
129 struct lio_rss_params_set rss_set;
130 #endif /* RSS */
131 };
132
133 #define LIO_MAX_CORES 12
134
135 /*
136 * \brief Enable or disable feature
137 * @param ifp pointer to network device
138 * @param cmd Command that just requires acknowledgment
139 * @param param1 Parameter to command
140 */
141 int lio_set_feature(if_t ifp, int cmd, uint16_t param1);
142
143 /*
144 * \brief Link control command completion callback
145 * @param nctrl_ptr pointer to control packet structure
146 *
147 * This routine is called by the callback function when a ctrl pkt sent to
148 * core app completes. The nctrl_ptr contains a copy of the command type
149 * and data sent to the core app. This routine is only called if the ctrl
150 * pkt was sent successfully to the core app.
151 */
152 void lio_ctrl_cmd_completion(void *nctrl_ptr);
153
154 int lio_setup_io_queues(struct octeon_device *octeon_dev, int ifidx,
155 uint32_t num_iqs, uint32_t num_oqs);
156
157 int lio_setup_interrupt(struct octeon_device *oct, uint32_t num_ioqs);
158
159 static inline void *
lio_recv_buffer_alloc(uint32_t size)160 lio_recv_buffer_alloc(uint32_t size)
161 {
162 struct mbuf *mb = NULL;
163
164 mb = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
165 if (mb != NULL)
166 mb->m_pkthdr.len = mb->m_len = size;
167
168 return ((void *)mb);
169 }
170
171 static inline void
lio_recv_buffer_free(void * buffer)172 lio_recv_buffer_free(void *buffer)
173 {
174
175 m_freem((struct mbuf *)buffer);
176 }
177
178 static inline int
lio_get_order(unsigned long size)179 lio_get_order(unsigned long size)
180 {
181 int order;
182
183 size = (size - 1) >> PAGE_SHIFT;
184 order = 0;
185 while (size) {
186 order++;
187 size >>= 1;
188 }
189
190 return (order);
191 }
192
193 static inline void *
lio_dma_alloc(size_t size,vm_paddr_t * dma_handle)194 lio_dma_alloc(size_t size, vm_paddr_t *dma_handle)
195 {
196 size_t align;
197 void *mem;
198
199 align = PAGE_SIZE << lio_get_order(size);
200 mem = kmem_alloc_contig(size, M_WAITOK, 0, ~0ul, align, 0,
201 VM_MEMATTR_DEFAULT);
202 if (mem != NULL)
203 *dma_handle = vtophys(mem);
204 else
205 *dma_handle = 0;
206
207 return (mem);
208 }
209
210 static inline void
lio_dma_free(size_t size,void * cpu_addr)211 lio_dma_free(size_t size, void *cpu_addr)
212 {
213
214 kmem_free(cpu_addr, size);
215 }
216
217 static inline uint64_t
lio_map_ring(device_t dev,void * buf,uint32_t size)218 lio_map_ring(device_t dev, void *buf, uint32_t size)
219 {
220 vm_paddr_t dma_addr;
221
222 dma_addr = vtophys(((struct mbuf *)buf)->m_data);
223 return ((uint64_t)dma_addr);
224 }
225
226 /*
227 * \brief check interface state
228 * @param lio per-network private data
229 * @param state_flag flag state to check
230 */
231 static inline int
lio_ifstate_check(struct lio * lio,int state_flag)232 lio_ifstate_check(struct lio *lio, int state_flag)
233 {
234
235 return (atomic_load_acq_int(&lio->ifstate) & state_flag);
236 }
237
238 /*
239 * \brief set interface state
240 * @param lio per-network private data
241 * @param state_flag flag state to set
242 */
243 static inline void
lio_ifstate_set(struct lio * lio,int state_flag)244 lio_ifstate_set(struct lio *lio, int state_flag)
245 {
246
247 atomic_store_rel_int(&lio->ifstate,
248 (atomic_load_acq_int(&lio->ifstate) | state_flag));
249 }
250
251 /*
252 * \brief clear interface state
253 * @param lio per-network private data
254 * @param state_flag flag state to clear
255 */
256 static inline void
lio_ifstate_reset(struct lio * lio,int state_flag)257 lio_ifstate_reset(struct lio *lio, int state_flag)
258 {
259
260 atomic_store_rel_int(&lio->ifstate,
261 (atomic_load_acq_int(&lio->ifstate) &
262 ~(state_flag)));
263 }
264
265 /*
266 * \brief wait for all pending requests to complete
267 * @param oct Pointer to Octeon device
268 *
269 * Called during shutdown sequence
270 */
271 static inline int
lio_wait_for_pending_requests(struct octeon_device * oct)272 lio_wait_for_pending_requests(struct octeon_device *oct)
273 {
274 int i, pcount = 0;
275
276 for (i = 0; i < 100; i++) {
277 pcount = atomic_load_acq_int(
278 &oct->response_list[LIO_ORDERED_SC_LIST].
279 pending_req_count);
280 if (pcount)
281 lio_sleep_timeout(100);
282 else
283 break;
284 }
285
286 if (pcount)
287 return (1);
288
289 return (0);
290 }
291
292 #endif /* __LIO_NETWORK_H__ */
293