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