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