c8074ebf6c7232dd880da720c3cddb8d1d26d3ff
[vpp.git] / src / plugins / avf / avf.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #ifndef _AVF_H_
19 #define _AVF_H_
20
21 #include <avf/virtchnl.h>
22
23 #include <vlib/log.h>
24
25 #define foreach_avf_device_flags \
26   _(0, INITIALIZED, "initialized") \
27   _(1, ERROR, "error") \
28   _(2, ADMIN_UP, "admin-up") \
29   _(3, IOVA, "iova") \
30   _(4, LINK_UP, "link-up") \
31   _(5, SHARED_TXQ_LOCK, "shared-txq-lock") \
32   _(6, ELOG, "elog")
33
34 enum
35 {
36 #define _(a, b, c) AVF_DEVICE_F_##b = (1 << a),
37   foreach_avf_device_flags
38 #undef _
39 };
40
41 typedef struct
42 {
43   u64 qword[4];
44 } avf_rx_desc_t;
45
46 STATIC_ASSERT_SIZEOF (avf_rx_desc_t, 32);
47
48 typedef struct
49 {
50   union
51   {
52     u64 qword[2];
53     u64x2 as_u64x2;
54   };
55 } avf_tx_desc_t;
56
57 STATIC_ASSERT_SIZEOF (avf_tx_desc_t, 16);
58
59 typedef struct
60 {
61   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
62   volatile u32 *qrx_tail;
63   u16 next;
64   u16 size;
65   avf_rx_desc_t *descs;
66   u32 *bufs;
67   u16 n_bufs;
68 } avf_rxq_t;
69
70 typedef struct
71 {
72   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
73   volatile u32 *qtx_tail;
74   u16 next;
75   u16 size;
76   clib_spinlock_t lock;
77   avf_tx_desc_t *descs;
78   u32 *bufs;
79   u16 n_bufs;
80 } avf_txq_t;
81
82 typedef struct
83 {
84   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
85   u32 flags;
86   u32 per_interface_next_index;
87
88   u32 dev_instance;
89   u32 sw_if_index;
90   u32 hw_if_index;
91   vlib_pci_dev_handle_t pci_dev_handle;
92   void *bar0;
93
94   /* queues */
95   avf_rxq_t *rxqs;
96   avf_txq_t *txqs;
97
98   /* Admin queues */
99   avf_aq_desc_t *atq;
100   avf_aq_desc_t *arq;
101   void *atq_bufs;
102   void *arq_bufs;
103   u64 atq_bufs_pa;
104   u64 arq_bufs_pa;
105   u16 atq_next_slot;
106   u16 arq_next_slot;
107   virtchnl_pf_event_t *events;
108
109   u16 vsi_id;
110   u32 feature_bitmap;
111   u8 hwaddr[6];
112   u16 num_queue_pairs;
113   u16 max_vectors;
114   u16 max_mtu;
115   u32 rss_key_size;
116   u32 rss_lut_size;
117   virtchnl_link_speed_t link_speed;
118
119   /* stats */
120   virtchnl_eth_stats_t eth_stats;
121
122   /* error */
123   clib_error_t *error;
124 } avf_device_t;
125
126 typedef struct
127 {
128   u32 status;
129   u16 length;
130   u8 ptype;
131   u8 error;
132 } avf_rx_vector_entry_t;
133
134 STATIC_ASSERT_SIZEOF (avf_rx_vector_entry_t, 8);
135
136 #define AVF_RX_VECTOR_SZ VLIB_FRAME_SIZE
137
138 enum
139 {
140   AVF_PROCESS_EVENT_START = 1,
141   AVF_PROCESS_EVENT_STOP = 2,
142   AVF_PROCESS_EVENT_AQ_INT = 3,
143 } avf_process_event_t;
144
145 typedef struct
146 {
147   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
148   avf_rx_vector_entry_t rx_vector[AVF_RX_VECTOR_SZ];
149   u32 *to_free;
150   vlib_buffer_t buffer_template;
151 } avf_per_thread_data_t;
152
153 typedef struct
154 {
155   u8 next_node;
156   i8 buffer_advance;
157   u32 flags;
158 } avf_ptype_t;
159
160 STATIC_ASSERT (VNET_DEVICE_INPUT_N_NEXT_NODES < 256, "too many next nodes");
161
162 typedef struct
163 {
164   u16 msg_id_base;
165
166   avf_device_t *devices;
167   avf_per_thread_data_t *per_thread_data;
168   vlib_physmem_region_index_t physmem_region;
169   int physmem_region_alloc;
170
171   vlib_log_class_t log_class;
172
173   /* 256 element array for ptype based lookup */
174   avf_ptype_t *ptypes;
175 } avf_main_t;
176
177 extern avf_main_t avf_main;
178
179 typedef struct
180 {
181   vlib_pci_addr_t addr;
182   int enable_elog;
183   u16 rxq_size;
184   u16 txq_size;
185   /* return */
186   int rv;
187   clib_error_t *error;
188 } avf_create_if_args_t;
189
190 void avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args);
191 void avf_delete_if (vlib_main_t * vm, avf_device_t * ad);
192
193 extern vlib_node_registration_t avf_input_node;
194 extern vnet_device_class_t avf_device_class;
195 uword avf_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
196                         vlib_frame_t * frame);
197
198 /* format.c */
199 format_function_t format_avf_device;
200 format_function_t format_avf_device_name;
201 format_function_t format_avf_input_trace;
202
203 static inline u32
204 avf_get_u32 (void *start, int offset)
205 {
206   return *(u32 *) (((u8 *) start) + offset);
207 }
208
209 static inline u64
210 avf_get_u64 (void *start, int offset)
211 {
212   return *(u64 *) (((u8 *) start) + offset);
213 }
214
215 static inline u32
216 avf_get_u32_bits (void *start, int offset, int first, int last)
217 {
218   u32 value = avf_get_u32 (start, offset);
219   if ((last == 0) && (first == 31))
220     return value;
221   value >>= last;
222   value &= (1 << (first - last + 1)) - 1;
223   return value;
224 }
225
226 static inline u64
227 avf_get_u64_bits (void *start, int offset, int first, int last)
228 {
229   u64 value = avf_get_u64 (start, offset);
230   if ((last == 0) && (first == 63))
231     return value;
232   value >>= last;
233   value &= (1 << (first - last + 1)) - 1;
234   return value;
235 }
236
237 static inline void
238 avf_set_u32 (void *start, int offset, u32 value)
239 {
240   (*(u32 *) (((u8 *) start) + offset)) = value;
241 }
242
243 static inline void
244 avf_reg_write (avf_device_t * ad, u32 addr, u32 val)
245 {
246   *(volatile u32 *) ((u8 *) ad->bar0 + addr) = val;
247 }
248
249 static inline u32
250 avf_reg_read (avf_device_t * ad, u32 addr)
251 {
252   return *(volatile u32 *) (ad->bar0 + addr);
253 }
254
255 static inline void
256 avf_reg_flush (avf_device_t * ad)
257 {
258   avf_reg_read (ad, AVFGEN_RSTAT);
259   asm volatile ("":::"memory");
260 }
261
262 typedef struct
263 {
264   u32 next_index;
265   u32 hw_if_index;
266   avf_rx_vector_entry_t rxve;
267 } avf_input_trace_t;
268
269 #endif /* AVF_H */
270
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */