030f05ec934f49ebaefddc6be7aee2fc2d8870f8
[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 AVF_RXD_STATUS(x)               (1ULL << x)
26 #define AVF_RXD_STATUS_DD               AVF_RXD_STATUS(0)
27 #define AVF_RXD_STATUS_EOP              AVF_RXD_STATUS(1)
28 #define AVF_RXD_ERROR_SHIFT             19
29 #define AVF_RXD_PTYPE_SHIFT             30
30 #define AVF_RXD_LEN_SHIFT               38
31
32 #define AVF_RXD_ERROR_IPE               (1ULL << (AVF_RXD_ERROR_SHIFT + 3))
33 #define AVF_RXD_ERROR_L4E               (1ULL << (AVF_RXD_ERROR_SHIFT + 4))
34
35
36 #define foreach_avf_device_flags \
37   _(0, INITIALIZED, "initialized") \
38   _(1, ERROR, "error") \
39   _(2, ADMIN_UP, "admin-up") \
40   _(3, VA_DMA, "vaddr-dma") \
41   _(4, LINK_UP, "link-up") \
42   _(5, SHARED_TXQ_LOCK, "shared-txq-lock") \
43   _(6, ELOG, "elog")
44
45 enum
46 {
47 #define _(a, b, c) AVF_DEVICE_F_##b = (1 << a),
48   foreach_avf_device_flags
49 #undef _
50 };
51
52 typedef volatile struct
53 {
54   union
55   {
56     struct
57     {
58       u64 mirr:13;
59       u64 rsv1:3;
60       u64 l2tag1:16;
61       u64 filter_status:32;
62       u64 status:19;
63       u64 error:8;
64       u64 rsv2:3;
65       u64 ptype:8;
66       u64 length:26;
67     };
68     u64 qword[4];
69 #ifdef CLIB_HAVE_VEC256
70     u64x4 as_u64x4;
71 #endif
72   };
73 } avf_rx_desc_t;
74
75 STATIC_ASSERT_SIZEOF (avf_rx_desc_t, 32);
76
77 typedef volatile struct
78 {
79   union
80   {
81     u64 qword[2];
82 #ifdef CLIB_HAVE_VEC128
83     u64x2 as_u64x2;
84 #endif
85   };
86 } avf_tx_desc_t;
87
88 STATIC_ASSERT_SIZEOF (avf_tx_desc_t, 16);
89
90 typedef struct
91 {
92   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
93   volatile u32 *qrx_tail;
94   u16 next;
95   u16 size;
96   avf_rx_desc_t *descs;
97   u32 *bufs;
98   u16 n_enqueued;
99   u8 int_mode;
100 } avf_rxq_t;
101
102 typedef struct
103 {
104   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
105   volatile u32 *qtx_tail;
106   u16 next;
107   u16 size;
108   clib_spinlock_t lock;
109   avf_tx_desc_t *descs;
110   u32 *bufs;
111   u16 n_enqueued;
112 } avf_txq_t;
113
114 typedef struct
115 {
116   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
117   u32 flags;
118   u32 per_interface_next_index;
119
120   u32 dev_instance;
121   u32 sw_if_index;
122   u32 hw_if_index;
123   vlib_pci_dev_handle_t pci_dev_handle;
124   void *bar0;
125   u8 *name;
126
127   /* queues */
128   avf_rxq_t *rxqs;
129   avf_txq_t *txqs;
130   u16 n_tx_queues;
131   u16 n_rx_queues;
132
133   /* Admin queues */
134   avf_aq_desc_t *atq;
135   avf_aq_desc_t *arq;
136   void *atq_bufs;
137   void *arq_bufs;
138   u64 atq_bufs_pa;
139   u64 arq_bufs_pa;
140   u16 atq_next_slot;
141   u16 arq_next_slot;
142   virtchnl_pf_event_t *events;
143
144   u16 vsi_id;
145   u32 feature_bitmap;
146   u8 hwaddr[6];
147   u16 num_queue_pairs;
148   u16 max_vectors;
149   u16 max_mtu;
150   u32 rss_key_size;
151   u32 rss_lut_size;
152   virtchnl_link_speed_t link_speed;
153
154   /* stats */
155   virtchnl_eth_stats_t eth_stats;
156
157   /* error */
158   clib_error_t *error;
159 } avf_device_t;
160
161 #define AVF_RX_VECTOR_SZ VLIB_FRAME_SIZE
162
163 enum
164 {
165   AVF_PROCESS_EVENT_START = 1,
166   AVF_PROCESS_EVENT_STOP = 2,
167   AVF_PROCESS_EVENT_AQ_INT = 3,
168 } avf_process_event_t;
169
170 typedef struct
171 {
172   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
173   u32 *to_free;
174   vlib_buffer_t buffer_template;
175 } avf_per_thread_data_t;
176
177 typedef struct
178 {
179   u16 msg_id_base;
180
181   avf_device_t *devices;
182   avf_per_thread_data_t *per_thread_data;
183
184   vlib_log_class_t log_class;
185 } avf_main_t;
186
187 extern avf_main_t avf_main;
188
189 typedef struct
190 {
191   vlib_pci_addr_t addr;
192   u8 *name;
193   int enable_elog;
194   u16 rxq_num;
195   u16 rxq_size;
196   u16 txq_size;
197   /* return */
198   int rv;
199   u32 sw_if_index;
200   clib_error_t *error;
201 } avf_create_if_args_t;
202
203 void avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args);
204 void avf_delete_if (vlib_main_t * vm, avf_device_t * ad);
205
206 extern vlib_node_registration_t avf_input_node;
207 extern vnet_device_class_t avf_device_class;
208
209 /* format.c */
210 format_function_t format_avf_device;
211 format_function_t format_avf_device_name;
212 format_function_t format_avf_input_trace;
213
214 static inline u32
215 avf_get_u32 (void *start, int offset)
216 {
217   return *(u32 *) (((u8 *) start) + offset);
218 }
219
220 static inline u64
221 avf_get_u64 (void *start, int offset)
222 {
223   return *(u64 *) (((u8 *) start) + offset);
224 }
225
226 static inline u32
227 avf_get_u32_bits (void *start, int offset, int first, int last)
228 {
229   u32 value = avf_get_u32 (start, offset);
230   if ((last == 0) && (first == 31))
231     return value;
232   value >>= last;
233   value &= (1 << (first - last + 1)) - 1;
234   return value;
235 }
236
237 static inline u64
238 avf_get_u64_bits (void *start, int offset, int first, int last)
239 {
240   u64 value = avf_get_u64 (start, offset);
241   if ((last == 0) && (first == 63))
242     return value;
243   value >>= last;
244   value &= (1 << (first - last + 1)) - 1;
245   return value;
246 }
247
248 static inline void
249 avf_set_u32 (void *start, int offset, u32 value)
250 {
251   (*(u32 *) (((u8 *) start) + offset)) = value;
252 }
253
254 static inline void
255 avf_reg_write (avf_device_t * ad, u32 addr, u32 val)
256 {
257   *(volatile u32 *) ((u8 *) ad->bar0 + addr) = val;
258 }
259
260 static inline u32
261 avf_reg_read (avf_device_t * ad, u32 addr)
262 {
263   return *(volatile u32 *) (ad->bar0 + addr);
264 }
265
266 static inline void
267 avf_reg_flush (avf_device_t * ad)
268 {
269   avf_reg_read (ad, AVFGEN_RSTAT);
270   asm volatile ("":::"memory");
271 }
272
273 typedef struct
274 {
275   u32 next_index;
276   u32 hw_if_index;
277   u64 qw1;
278 } avf_input_trace_t;
279
280 #define foreach_avf_tx_func_error              \
281 _(NO_FREE_SLOTS, "no free tx slots")
282
283 typedef enum
284 {
285 #define _(f,s) AVF_TX_ERROR_##f,
286   foreach_avf_tx_func_error
287 #undef _
288     AVF_TX_N_ERROR,
289 } avf_tx_func_error_t;
290
291 #endif /* AVF_H */
292
293 /*
294  * fd.io coding-style-patch-verification: ON
295  *
296  * Local Variables:
297  * eval: (c-set-style "gnu")
298  * End:
299  */