47e86e14f4321a134ea61f32367c7da929d42a95
[vpp.git] / src / plugins / dev_iavf / iavf.h
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright (c) 2023 Cisco Systems, Inc.
3  */
4
5 #ifndef _IIAVF_H_
6 #define _IIAVF_H_
7
8 #include <vppinfra/clib.h>
9 #include <vppinfra/error_bootstrap.h>
10 #include <vppinfra/format.h>
11 #include <vnet/vnet.h>
12 #include <vnet/dev/dev.h>
13 #include <dev_iavf/iavf_desc.h>
14 #include <dev_iavf/virtchnl.h>
15
16 #define IAVF_ITR_INT              250
17 #define IAVF_RX_MAX_DESC_IN_CHAIN 5
18 #define IAVF_MAX_RSS_KEY_SIZE     52
19 #define IAVF_MAX_RSS_LUT_SIZE     64
20 #define IIAVF_AQ_POLL_INTERVAL    0.2
21 #define IIAVF_AQ_BUF_SIZE         4096
22
23 typedef struct iavf_adminq_dma_mem iavf_adminq_dma_mem_t;
24
25 typedef struct
26 {
27   u8 adminq_active : 1;
28   void *bar0;
29
30   /* Admin queues */
31   iavf_adminq_dma_mem_t *aq_mem;
32   u16 atq_next_slot;
33   u16 arq_next_slot;
34   virtchnl_pf_event_t *events;
35 } iavf_device_t;
36
37 typedef struct
38 {
39   u32 flow_id;
40   u16 next_index;
41   i16 buffer_advance;
42 } iavf_flow_lookup_entry_t;
43
44 typedef struct
45 {
46   u8 admin_up : 1;
47   u8 flow_offload : 1;
48   iavf_flow_lookup_entry_t *flow_lookup_entries;
49   u64 intr_mode_per_rxq_bitmap;
50   u32 vf_cap_flags;
51   u16 vsi_id;
52   u16 rss_key_size;
53   u16 rss_lut_size;
54   u16 num_qp;
55   u16 max_vectors;
56   u16 n_rx_vectors;
57 } iavf_port_t;
58
59 typedef struct
60 {
61   u32 *qtx_tail;
62   u32 *buffer_indices;
63   iavf_tx_desc_t *descs;
64   u16 next;
65   u16 n_enqueued;
66   u16 *rs_slots;
67   iavf_tx_desc_t *tmp_descs;
68   u32 *tmp_bufs;
69   u32 *ph_bufs;
70 } iavf_txq_t;
71
72 typedef struct
73 {
74   u32 *qrx_tail;
75   u32 *buffer_indices;
76   iavf_rx_desc_t *descs;
77   u16 next;
78   u16 n_enqueued;
79 } iavf_rxq_t;
80
81 typedef struct
82 {
83   u16 qid;
84   u16 next_index;
85   u32 hw_if_index;
86   u32 flow_id;
87   u64 qw1s[IAVF_RX_MAX_DESC_IN_CHAIN];
88 } iavf_rx_trace_t;
89
90 /* adminq.c */
91 vnet_dev_rv_t iavf_aq_alloc (vlib_main_t *, vnet_dev_t *);
92 void iavf_aq_init (vlib_main_t *, vnet_dev_t *);
93 void iavf_aq_poll_on (vlib_main_t *, vnet_dev_t *);
94 void iavf_aq_poll_off (vlib_main_t *, vnet_dev_t *);
95 void iavf_aq_deinit (vlib_main_t *, vnet_dev_t *);
96 void iavf_aq_free (vlib_main_t *, vnet_dev_t *);
97 vnet_dev_rv_t iavf_aq_atq_enq (vlib_main_t *, vnet_dev_t *, iavf_aq_desc_t *,
98                                const u8 *, u16, f64);
99 int iavf_aq_arq_next_acq (vlib_main_t *, vnet_dev_t *, iavf_aq_desc_t **,
100                           u8 **, f64);
101 void iavf_aq_arq_next_rel (vlib_main_t *, vnet_dev_t *);
102 format_function_t format_virtchnl_op_name;
103 format_function_t format_virtchnl_status;
104
105 /* format.c */
106 format_function_t format_iavf_vf_cap_flags;
107 format_function_t format_iavf_rx_trace;
108 format_function_t format_iavf_port_status;
109
110 /* port.c */
111 vnet_dev_rv_t iavf_port_init (vlib_main_t *, vnet_dev_port_t *);
112 vnet_dev_rv_t iavf_port_start (vlib_main_t *, vnet_dev_port_t *);
113 void iavf_port_stop (vlib_main_t *, vnet_dev_port_t *);
114 vnet_dev_rv_t iavf_port_cfg_change (vlib_main_t *, vnet_dev_port_t *,
115                                     vnet_dev_port_cfg_change_req_t *);
116
117 /* queue.c */
118 vnet_dev_rv_t iavf_rx_queue_alloc (vlib_main_t *, vnet_dev_rx_queue_t *);
119 vnet_dev_rv_t iavf_tx_queue_alloc (vlib_main_t *, vnet_dev_tx_queue_t *);
120 vnet_dev_rv_t iavf_rx_queue_start (vlib_main_t *, vnet_dev_rx_queue_t *);
121 vnet_dev_rv_t iavf_tx_queue_start (vlib_main_t *, vnet_dev_tx_queue_t *);
122 void iavf_rx_queue_stop (vlib_main_t *, vnet_dev_rx_queue_t *);
123 void iavf_tx_queue_stop (vlib_main_t *, vnet_dev_tx_queue_t *);
124 void iavf_rx_queue_free (vlib_main_t *, vnet_dev_rx_queue_t *);
125 void iavf_tx_queue_free (vlib_main_t *, vnet_dev_tx_queue_t *);
126
127 /* counter.c */
128 void iavf_port_poll_stats (vlib_main_t *, vnet_dev_port_t *);
129 void iavf_port_add_counters (vlib_main_t *, vnet_dev_port_t *);
130
131 /* inline funcs */
132
133 static inline u32
134 iavf_get_u32 (void *start, int offset)
135 {
136   return *(u32 *) (((u8 *) start) + offset);
137 }
138
139 static inline void
140 iavf_reg_write (iavf_device_t *ad, u32 addr, u32 val)
141 {
142   __atomic_store_n ((u32 *) ((u8 *) ad->bar0 + addr), val, __ATOMIC_RELEASE);
143 }
144
145 static inline u32
146 iavf_reg_read (iavf_device_t *ad, u32 addr)
147 {
148   return __atomic_load_n ((u32 *) (ad->bar0 + addr), __ATOMIC_RELAXED);
149   ;
150 }
151
152 static inline void
153 iavf_reg_flush (iavf_device_t *ad)
154 {
155   iavf_reg_read (ad, IAVF_VFGEN_RSTAT);
156   asm volatile("" ::: "memory");
157 }
158
159 #define log_debug(dev, f, ...)                                                \
160   vlib_log (VLIB_LOG_LEVEL_DEBUG, iavf_log.class, "%U" f,                     \
161             format_vnet_dev_log, (dev),                                       \
162             clib_string_skip_prefix (__func__, "iavf_"), ##__VA_ARGS__)
163 #define log_info(dev, f, ...)                                                 \
164   vlib_log (VLIB_LOG_LEVEL_INFO, iavf_log.class, "%U: " f,                    \
165             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
166 #define log_notice(dev, f, ...)                                               \
167   vlib_log (VLIB_LOG_LEVEL_NOTICE, iavf_log.class, "%U: " f,                  \
168             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
169 #define log_warn(dev, f, ...)                                                 \
170   vlib_log (VLIB_LOG_LEVEL_WARNING, iavf_log.class, "%U: " f,                 \
171             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
172 #define log_err(dev, f, ...)                                                  \
173   vlib_log (VLIB_LOG_LEVEL_ERR, iavf_log.class, "%U: " f,                     \
174             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
175
176 /* temp */
177 #define IAVF_RX_VECTOR_SZ VLIB_FRAME_SIZE
178
179 typedef struct
180 {
181   u64 qw1s[IAVF_RX_MAX_DESC_IN_CHAIN - 1];
182   u32 buffers[IAVF_RX_MAX_DESC_IN_CHAIN - 1];
183 } iavf_rx_tail_t;
184
185 typedef struct
186 {
187   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
188   vlib_buffer_t *bufs[IAVF_RX_VECTOR_SZ];
189   u16 next[IAVF_RX_VECTOR_SZ];
190   u64 qw1s[IAVF_RX_VECTOR_SZ];
191   u32 flow_ids[IAVF_RX_VECTOR_SZ];
192   iavf_rx_tail_t tails[IAVF_RX_VECTOR_SZ];
193 } iavf_rt_data_t;
194
195 #define foreach_iavf_tx_node_counter                                          \
196   _ (SEG_SZ_EXCEEDED, seg_sz_exceeded, ERROR, "segment size exceeded")        \
197   _ (NO_FREE_SLOTS, no_free_slots, ERROR, "no free tx slots")
198
199 typedef enum
200 {
201 #define _(f, n, s, d) IAVF_TX_NODE_CTR_##f,
202   foreach_iavf_tx_node_counter
203 #undef _
204 } iavf_tx_node_counter_t;
205
206 #define foreach_iavf_rx_node_counter                                          \
207   _ (BUFFER_ALLOC, buffer_alloc, ERROR, "buffer alloc error")
208
209 typedef enum
210 {
211 #define _(f, n, s, d) IAVF_RX_NODE_CTR_##f,
212   foreach_iavf_rx_node_counter
213 #undef _
214 } iavf_rx_node_counter_t;
215
216 #endif /* _IIAVF_H_ */