avf: print stats in separate log class
[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 <vppinfra/types.h>
24 #include <vppinfra/error_bootstrap.h>
25 #include <vppinfra/lock.h>
26
27 #include <vlib/log.h>
28 #include <vlib/pci/pci.h>
29
30 #include <vnet/interface.h>
31
32 #include <vnet/devices/devices.h>
33 #include <vnet/flow/flow.h>
34
35 #define AVF_QUEUE_SZ_MAX                4096
36 #define AVF_QUEUE_SZ_MIN                64
37
38 #define AVF_AQ_ENQ_SUSPEND_TIME         50e-6
39 #define AVF_AQ_ENQ_MAX_WAIT_TIME        250e-3
40
41 #define AVF_RESET_SUSPEND_TIME          20e-3
42 #define AVF_RESET_MAX_WAIT_TIME         1
43
44 #define AVF_SEND_TO_PF_SUSPEND_TIME     10e-3
45 #define AVF_SEND_TO_PF_MAX_WAIT_TIME    1
46
47 #define AVF_RXD_STATUS(x)               (1ULL << x)
48 #define AVF_RXD_STATUS_DD               AVF_RXD_STATUS(0)
49 #define AVF_RXD_STATUS_EOP              AVF_RXD_STATUS(1)
50 #define AVF_RXD_STATUS_FLM              AVF_RXD_STATUS (11)
51 #define AVF_RXD_ERROR_SHIFT             19
52 #define AVF_RXD_PTYPE_SHIFT             30
53 #define AVF_RXD_LEN_SHIFT               38
54 #define AVF_RX_MAX_DESC_IN_CHAIN        5
55
56 #define AVF_RXD_ERROR_IPE               (1ULL << (AVF_RXD_ERROR_SHIFT + 3))
57 #define AVF_RXD_ERROR_L4E               (1ULL << (AVF_RXD_ERROR_SHIFT + 4))
58
59 #define AVF_TXD_CMD(x)                  (1 << (x + 4))
60 #define AVF_TXD_CMD_EXT(x, val)         ((u64)val << (x + 4))
61 #define AVF_TXD_CMD_EOP                 AVF_TXD_CMD(0)
62 #define AVF_TXD_CMD_RS                  AVF_TXD_CMD(1)
63 #define AVF_TXD_CMD_RSV                 AVF_TXD_CMD(2)
64
65 #define AVF_TXD_CMD_IIPT_NONE           AVF_TXD_CMD_EXT(5, 0)
66 #define AVF_TXD_CMD_IIPT_IPV6           AVF_TXD_CMD_EXT(5, 1)
67 #define AVF_TXD_CMD_IIPT_IPV4_NO_CSUM   AVF_TXD_CMD_EXT(5, 2)
68 #define AVF_TXD_CMD_IIPT_IPV4           AVF_TXD_CMD_EXT(5, 3)
69
70 #define AVF_TXD_CMD_L4T_UNKNOWN         AVF_TXD_CMD_EXT(8, 0)
71 #define AVF_TXD_CMD_L4T_TCP             AVF_TXD_CMD_EXT(8, 1)
72 #define AVF_TXD_CMD_L4T_SCTP            AVF_TXD_CMD_EXT(8, 2)
73 #define AVF_TXD_CMD_L4T_UDP             AVF_TXD_CMD_EXT(8, 3)
74
75 #define AVF_TXD_OFFSET(x,factor,val)    (((u64)val/(u64)factor) << (16 + x))
76 #define AVF_TXD_OFFSET_MACLEN(val)      AVF_TXD_OFFSET( 0, 2, val)
77 #define AVF_TXD_OFFSET_IPLEN(val)       AVF_TXD_OFFSET( 7, 4, val)
78 #define AVF_TXD_OFFSET_L4LEN(val)       AVF_TXD_OFFSET(14, 4, val)
79
80 #define AVF_TXD_DTYP_CTX                0x1ULL
81 #define AVF_TXD_CTX_CMD_TSO             AVF_TXD_CMD(0)
82 #define AVF_TXD_CTX_SEG(val,x)          (((u64)val) << (30 + x))
83 #define AVF_TXD_CTX_SEG_TLEN(val)       AVF_TXD_CTX_SEG(val,0)
84 #define AVF_TXD_CTX_SEG_MSS(val)        AVF_TXD_CTX_SEG(val,20)
85
86
87 extern vlib_log_class_registration_t avf_log;
88 extern vlib_log_class_registration_t avf_stats_log;
89
90 #define avf_log_err(dev, f, ...)                        \
91   vlib_log (VLIB_LOG_LEVEL_ERR, avf_log.class, "%U: " f, \
92             format_vlib_pci_addr, &dev->pci_addr, \
93             ## __VA_ARGS__)
94
95 #define avf_log_warn(dev, f, ...)                        \
96   vlib_log (VLIB_LOG_LEVEL_WARNING, avf_log.class, "%U: " f, \
97             format_vlib_pci_addr, &dev->pci_addr, \
98             ## __VA_ARGS__)
99
100 #define avf_log_debug(dev, f, ...)                        \
101   vlib_log (VLIB_LOG_LEVEL_DEBUG, avf_log.class, "%U: " f, \
102             format_vlib_pci_addr, &dev->pci_addr, \
103             ## __VA_ARGS__)
104
105 #define avf_stats_log_debug(dev, f, ...)                                      \
106   vlib_log (VLIB_LOG_LEVEL_DEBUG, avf_stats_log.class, "%U: " f,              \
107             format_vlib_pci_addr, &dev->pci_addr, ##__VA_ARGS__)
108
109 #define foreach_avf_device_flags                                              \
110   _ (0, INITIALIZED, "initialized")                                           \
111   _ (1, ERROR, "error")                                                       \
112   _ (2, ADMIN_UP, "admin-up")                                                 \
113   _ (3, VA_DMA, "vaddr-dma")                                                  \
114   _ (4, LINK_UP, "link-up")                                                   \
115   _ (5, SHARED_TXQ_LOCK, "shared-txq-lock")                                   \
116   _ (6, ELOG, "elog")                                                         \
117   _ (7, PROMISC, "promisc")                                                   \
118   _ (8, RX_INT, "rx-interrupts")                                              \
119   _ (9, RX_FLOW_OFFLOAD, "rx-flow-offload")
120
121 enum
122 {
123 #define _(a, b, c) AVF_DEVICE_F_##b = (1 << a),
124   foreach_avf_device_flags
125 #undef _
126 };
127
128 typedef volatile struct
129 {
130   union
131   {
132     struct
133     {
134       u64 mirr:13;
135       u64 rsv1:3;
136       u64 l2tag1:16;
137       u64 filter_status:32;
138       u64 status:19;
139       u64 error:8;
140       u64 rsv2:3;
141       u64 ptype:8;
142       u64 length:26;
143
144       u64 rsv3 : 64;
145       u32 flex_lo;
146       u32 fdid_flex_hi;
147     };
148     u64 qword[4];
149 #ifdef CLIB_HAVE_VEC256
150     u64x4 as_u64x4;
151 #endif
152   };
153 } avf_rx_desc_t;
154
155 STATIC_ASSERT_SIZEOF (avf_rx_desc_t, 32);
156
157 typedef struct
158 {
159   union
160   {
161     u64 qword[2];
162 #ifdef CLIB_HAVE_VEC128
163     u64x2 as_u64x2;
164 #endif
165   };
166 } avf_tx_desc_t;
167
168 STATIC_ASSERT_SIZEOF (avf_tx_desc_t, 16);
169
170 typedef struct
171 {
172   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
173   volatile u32 *qrx_tail;
174   u16 next;
175   u16 size;
176   avf_rx_desc_t *descs;
177   u32 *bufs;
178   u16 n_enqueued;
179   u8 int_mode;
180   u8 buffer_pool_index;
181   u32 queue_index;
182 } avf_rxq_t;
183
184 typedef struct
185 {
186   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
187   volatile u32 *qtx_tail;
188   u16 next;
189   u16 size;
190   u32 *ph_bufs;
191   clib_spinlock_t lock;
192   avf_tx_desc_t *descs;
193   u32 *bufs;
194   u16 n_enqueued;
195   u16 *rs_slots;
196
197   avf_tx_desc_t *tmp_descs;
198   u32 *tmp_bufs;
199   u32 queue_index;
200 } avf_txq_t;
201
202 typedef struct
203 {
204   u32 flow_index;
205   u32 mark;
206   struct avf_fdir_conf *rcfg;
207 } avf_flow_entry_t;
208
209 typedef struct
210 {
211   u32 flow_id;
212   u16 next_index;
213   i16 buffer_advance;
214 } avf_flow_lookup_entry_t;
215
216 typedef struct
217 {
218   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
219   u32 flags;
220   u32 per_interface_next_index;
221
222   u32 dev_instance;
223   u32 sw_if_index;
224   u32 hw_if_index;
225   vlib_pci_dev_handle_t pci_dev_handle;
226   u32 numa_node;
227   void *bar0;
228   u8 *name;
229
230   /* queues */
231   avf_rxq_t *rxqs;
232   avf_txq_t *txqs;
233   u16 n_tx_queues;
234   u16 n_rx_queues;
235
236   /* Admin queues */
237   avf_aq_desc_t *atq;
238   avf_aq_desc_t *arq;
239   void *atq_bufs;
240   void *arq_bufs;
241   u64 atq_bufs_pa;
242   u64 arq_bufs_pa;
243   u16 atq_next_slot;
244   u16 arq_next_slot;
245   virtchnl_pf_event_t *events;
246
247   u16 vsi_id;
248   u32 cap_flags;
249   u8 hwaddr[6];
250   u16 num_queue_pairs;
251   u16 max_vectors;
252   u16 n_rx_irqs;
253   u16 max_mtu;
254   u32 rss_key_size;
255   u32 rss_lut_size;
256   virtchnl_link_speed_t link_speed;
257   vlib_pci_addr_t pci_addr;
258
259   /* flow */
260   avf_flow_entry_t *flow_entries;               /* pool */
261   avf_flow_lookup_entry_t *flow_lookup_entries; /* pool */
262
263   /* stats */
264   virtchnl_eth_stats_t eth_stats;
265   virtchnl_eth_stats_t last_cleared_eth_stats;
266
267   /* error */
268   clib_error_t *error;
269 } avf_device_t;
270
271 #define AVF_RX_VECTOR_SZ VLIB_FRAME_SIZE
272
273 typedef enum
274 {
275   AVF_PROCESS_EVENT_START = 1,
276   AVF_PROCESS_EVENT_DELETE_IF = 2,
277   AVF_PROCESS_EVENT_AQ_INT = 3,
278   AVF_PROCESS_EVENT_REQ = 4,
279 } avf_process_event_t;
280
281 typedef enum
282 {
283   AVF_PROCESS_REQ_ADD_DEL_ETH_ADDR = 1,
284   AVF_PROCESS_REQ_CONFIG_PROMISC_MDDE = 2,
285   AVF_PROCESS_REQ_PROGRAM_FLOW = 3,
286 } avf_process_req_type_t;
287
288 typedef struct
289 {
290   avf_process_req_type_t type;
291   u32 dev_instance;
292   u32 calling_process_index;
293   u8 eth_addr[6];
294   int is_add, is_enable;
295
296   /* below parameters are used for 'program flow' event */
297   u8 *rule;
298   u32 rule_len;
299   u8 *program_status;
300   u32 status_len;
301
302   clib_error_t *error;
303 } avf_process_req_t;
304
305 typedef struct
306 {
307   u64 qw1s[AVF_RX_MAX_DESC_IN_CHAIN - 1];
308   u32 buffers[AVF_RX_MAX_DESC_IN_CHAIN - 1];
309 } avf_rx_tail_t;
310
311 typedef struct
312 {
313   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
314   vlib_buffer_t *bufs[AVF_RX_VECTOR_SZ];
315   u16 next[AVF_RX_VECTOR_SZ];
316   u64 qw1s[AVF_RX_VECTOR_SZ];
317   u32 flow_ids[AVF_RX_VECTOR_SZ];
318   avf_rx_tail_t tails[AVF_RX_VECTOR_SZ];
319   vlib_buffer_t buffer_template;
320 } avf_per_thread_data_t;
321
322 typedef struct
323 {
324   u16 msg_id_base;
325
326   avf_device_t **devices;
327   avf_per_thread_data_t *per_thread_data;
328 } avf_main_t;
329
330 extern avf_main_t avf_main;
331
332 typedef struct
333 {
334   vlib_pci_addr_t addr;
335   u8 *name;
336   int enable_elog;
337   u16 rxq_num;
338   u16 rxq_size;
339   u16 txq_size;
340   /* return */
341   int rv;
342   u32 sw_if_index;
343   clib_error_t *error;
344 } avf_create_if_args_t;
345
346 void avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args);
347
348 extern vlib_node_registration_t avf_input_node;
349 extern vlib_node_registration_t avf_process_node;
350 extern vnet_device_class_t avf_device_class;
351
352 clib_error_t *avf_program_flow (u32 dev_instance, int is_add, u8 *rule,
353                                 u32 rule_len, u8 *program_status,
354                                 u32 status_len);
355
356 /* format.c */
357 format_function_t format_avf_device;
358 format_function_t format_avf_device_name;
359 format_function_t format_avf_input_trace;
360 format_function_t format_avf_vf_cap_flags;
361 format_function_t format_avf_vlan_supported_caps;
362 format_function_t format_avf_vlan_caps;
363 format_function_t format_avf_vlan_support;
364 format_function_t format_avf_eth_stats;
365 vnet_flow_dev_ops_function_t avf_flow_ops_fn;
366
367 static_always_inline avf_device_t *
368 avf_get_device (u32 dev_instance)
369 {
370   return pool_elt_at_index (avf_main.devices, dev_instance)[0];
371 }
372
373 static inline u32
374 avf_get_u32 (void *start, int offset)
375 {
376   return *(u32 *) (((u8 *) start) + offset);
377 }
378
379 static inline u64
380 avf_get_u64 (void *start, int offset)
381 {
382   return *(u64 *) (((u8 *) start) + offset);
383 }
384
385 static inline u32
386 avf_get_u32_bits (void *start, int offset, int first, int last)
387 {
388   u32 value = avf_get_u32 (start, offset);
389   if ((last == 0) && (first == 31))
390     return value;
391   value >>= last;
392   value &= (1 << (first - last + 1)) - 1;
393   return value;
394 }
395
396 static inline u64
397 avf_get_u64_bits (void *start, int offset, int first, int last)
398 {
399   u64 value = avf_get_u64 (start, offset);
400   if ((last == 0) && (first == 63))
401     return value;
402   value >>= last;
403   value &= (1 << (first - last + 1)) - 1;
404   return value;
405 }
406
407 static inline void
408 avf_set_u32 (void *start, int offset, u32 value)
409 {
410   (*(u32 *) (((u8 *) start) + offset)) = value;
411 }
412
413 static inline void
414 avf_reg_write (avf_device_t * ad, u32 addr, u32 val)
415 {
416   *(volatile u32 *) ((u8 *) ad->bar0 + addr) = val;
417 }
418
419 static inline u32
420 avf_reg_read (avf_device_t * ad, u32 addr)
421 {
422   return *(volatile u32 *) (ad->bar0 + addr);
423 }
424
425 static inline void
426 avf_reg_flush (avf_device_t * ad)
427 {
428   avf_reg_read (ad, AVFGEN_RSTAT);
429   asm volatile ("":::"memory");
430 }
431
432 static inline void
433 avf_tail_write (volatile u32 *addr, u32 val)
434 {
435 #ifdef __MOVDIRI__
436   _mm_sfence ();
437   _directstoreu_u32 ((void *) addr, val);
438 #else
439   clib_atomic_store_rel_n (addr, val);
440 #endif
441 }
442
443 static_always_inline int
444 avf_rxd_is_not_eop (avf_rx_desc_t * d)
445 {
446   return (d->qword[1] & AVF_RXD_STATUS_EOP) == 0;
447 }
448
449 static_always_inline int
450 avf_rxd_is_not_dd (avf_rx_desc_t * d)
451 {
452   return (d->qword[1] & AVF_RXD_STATUS_DD) == 0;
453 }
454
455 typedef struct
456 {
457   u16 qid;
458   u16 next_index;
459   u32 hw_if_index;
460   u32 flow_id;
461   u64 qw1s[AVF_RX_MAX_DESC_IN_CHAIN];
462 } avf_input_trace_t;
463
464 #define foreach_avf_tx_func_error              \
465   _(SEGMENT_SIZE_EXCEEDED, "segment size exceeded")     \
466   _(NO_FREE_SLOTS, "no free tx slots")
467
468 typedef enum
469 {
470 #define _(f,s) AVF_TX_ERROR_##f,
471   foreach_avf_tx_func_error
472 #undef _
473     AVF_TX_N_ERROR,
474 } avf_tx_func_error_t;
475
476 #endif /* AVF_H */
477
478 /*
479  * fd.io coding-style-patch-verification: ON
480  *
481  * Local Variables:
482  * eval: (c-set-style "gnu")
483  * End:
484  */