iavf: new driver using new dev infra
[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
36 } iavf_device_t;
37
38 typedef struct
39 {
40   u32 flow_id;
41   u16 next_index;
42   i16 buffer_advance;
43 } iavf_flow_lookup_entry_t;
44
45 typedef struct
46 {
47   u8 admin_up : 1;
48   u8 flow_offload : 1;
49   iavf_flow_lookup_entry_t *flow_lookup_entries;
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 } iavf_port_t;
57
58 typedef struct
59 {
60   u32 *qtx_tail;
61   u32 *buffer_indices;
62   iavf_tx_desc_t *descs;
63   u16 next;
64   u16 n_enqueued;
65   u16 *rs_slots;
66   iavf_tx_desc_t *tmp_descs;
67   u32 *tmp_bufs;
68   u32 *ph_bufs;
69 } iavf_txq_t;
70
71 typedef struct
72 {
73   u32 *qrx_tail;
74   u32 *buffer_indices;
75   iavf_rx_desc_t *descs;
76   u16 next;
77   u16 n_enqueued;
78 } iavf_rxq_t;
79
80 typedef struct
81 {
82   u16 qid;
83   u16 next_index;
84   u32 hw_if_index;
85   u32 flow_id;
86   u64 qw1s[IAVF_RX_MAX_DESC_IN_CHAIN];
87 } iavf_rx_trace_t;
88
89 /* adminq.c */
90 vnet_dev_rv_t iavf_aq_alloc (vlib_main_t *, vnet_dev_t *);
91 void iavf_aq_init (vlib_main_t *, vnet_dev_t *);
92 void iavf_aq_poll_on (vlib_main_t *, vnet_dev_t *);
93 void iavf_aq_poll_off (vlib_main_t *, vnet_dev_t *);
94 void iavf_aq_deinit (vlib_main_t *, vnet_dev_t *);
95 void iavf_aq_free (vlib_main_t *, vnet_dev_t *);
96 vnet_dev_rv_t iavf_aq_atq_enq (vlib_main_t *, vnet_dev_t *, iavf_aq_desc_t *,
97                                const u8 *, u16, f64);
98 int iavf_aq_arq_next_acq (vlib_main_t *, vnet_dev_t *, iavf_aq_desc_t **,
99                           u8 **, f64);
100 void iavf_aq_arq_next_rel (vlib_main_t *, vnet_dev_t *);
101 format_function_t format_virtchnl_op_name;
102 format_function_t format_virtchnl_status;
103
104 /* format.c */
105 format_function_t format_iavf_vf_cap_flags;
106 format_function_t format_iavf_rx_trace;
107 format_function_t format_iavf_port_status;
108 format_function_t format_iavf_log;
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, 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, format_iavf_log,    \
161             (dev), __func__, ##__VA_ARGS__)
162 #define log_info(dev, f, ...)                                                 \
163   vlib_log (VLIB_LOG_LEVEL_INFO, iavf_log.class, "%U: " f,                    \
164             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
165 #define log_notice(dev, f, ...)                                               \
166   vlib_log (VLIB_LOG_LEVEL_NOTICE, iavf_log.class, "%U: " f,                  \
167             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
168 #define log_warn(dev, f, ...)                                                 \
169   vlib_log (VLIB_LOG_LEVEL_WARNING, iavf_log.class, "%U: " f,                 \
170             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
171 #define log_err(dev, f, ...)                                                  \
172   vlib_log (VLIB_LOG_LEVEL_ERR, iavf_log.class, "%U: " f,                     \
173             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
174
175 /* temp */
176 #define IAVF_RX_VECTOR_SZ VLIB_FRAME_SIZE
177
178 typedef struct
179 {
180   u64 qw1s[IAVF_RX_MAX_DESC_IN_CHAIN - 1];
181   u32 buffers[IAVF_RX_MAX_DESC_IN_CHAIN - 1];
182 } iavf_rx_tail_t;
183
184 typedef struct
185 {
186   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
187   vlib_buffer_t *bufs[IAVF_RX_VECTOR_SZ];
188   u16 next[IAVF_RX_VECTOR_SZ];
189   u64 qw1s[IAVF_RX_VECTOR_SZ];
190   u32 flow_ids[IAVF_RX_VECTOR_SZ];
191   iavf_rx_tail_t tails[IAVF_RX_VECTOR_SZ];
192 } iavf_rt_data_t;
193
194 #define foreach_iavf_tx_node_counter                                          \
195   _ (SEG_SZ_EXCEEDED, seg_sz_exceeded, ERROR, "segment size exceeded")        \
196   _ (NO_FREE_SLOTS, no_free_slots, ERROR, "no free tx slots")
197
198 typedef enum
199 {
200 #define _(f, n, s, d) IAVF_TX_NODE_CTR_##f,
201   foreach_iavf_tx_node_counter
202 #undef _
203 } iavf_tx_node_counter_t;
204
205 #define foreach_iavf_rx_node_counter                                          \
206   _ (BUFFER_ALLOC, buffer_alloc, ERROR, "buffer alloc error")
207
208 typedef enum
209 {
210 #define _(f, n, s, d) IAVF_RX_NODE_CTR_##f,
211   foreach_iavf_rx_node_counter
212 #undef _
213 } iavf_rx_node_counter_t;
214
215 #endif /* _IIAVF_H_ */