xref: /freebsd/sys/dev/ena/ena.h (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015-2020 Amazon.com, Inc. or its affiliates.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  *
32  */
33 
34 #ifndef ENA_H
35 #define ENA_H
36 
37 #include <sys/types.h>
38 
39 #include "ena-com/ena_com.h"
40 #include "ena-com/ena_eth_com.h"
41 
42 #define DRV_MODULE_VER_MAJOR	2
43 #define DRV_MODULE_VER_MINOR	4
44 #define DRV_MODULE_VER_SUBMINOR 0
45 
46 #define DRV_MODULE_NAME		"ena"
47 
48 #ifndef DRV_MODULE_VERSION
49 #define DRV_MODULE_VERSION				\
50 	__XSTRING(DRV_MODULE_VER_MAJOR) "."		\
51 	__XSTRING(DRV_MODULE_VER_MINOR) "."		\
52 	__XSTRING(DRV_MODULE_VER_SUBMINOR)
53 #endif
54 #define DEVICE_NAME	"Elastic Network Adapter (ENA)"
55 #define DEVICE_DESC	"ENA adapter"
56 
57 /* Calculate DMA mask - width for ena cannot exceed 48, so it is safe */
58 #define ENA_DMA_BIT_MASK(x)		((1ULL << (x)) - 1ULL)
59 
60 /* 1 for AENQ + ADMIN */
61 #define	ENA_ADMIN_MSIX_VEC		1
62 #define	ENA_MAX_MSIX_VEC(io_queues)	(ENA_ADMIN_MSIX_VEC + (io_queues))
63 
64 #define	ENA_REG_BAR			0
65 #define	ENA_MEM_BAR			2
66 
67 #define	ENA_BUS_DMA_SEGS		32
68 
69 #define	ENA_DEFAULT_BUF_RING_SIZE	4096
70 
71 #define	ENA_DEFAULT_RING_SIZE		1024
72 #define	ENA_MIN_RING_SIZE		256
73 
74 /*
75  * Refill Rx queue when number of required descriptors is above
76  * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
77  */
78 #define	ENA_RX_REFILL_THRESH_DIVIDER	8
79 #define	ENA_RX_REFILL_THRESH_PACKET	256
80 
81 #define	ENA_IRQNAME_SIZE		40
82 
83 #define	ENA_PKT_MAX_BUFS 		19
84 
85 #define	ENA_RX_RSS_TABLE_LOG_SIZE	7
86 #define	ENA_RX_RSS_TABLE_SIZE		(1 << ENA_RX_RSS_TABLE_LOG_SIZE)
87 
88 #define	ENA_HASH_KEY_SIZE		40
89 
90 #define	ENA_MAX_FRAME_LEN		10000
91 #define	ENA_MIN_FRAME_LEN 		60
92 
93 #define ENA_TX_RESUME_THRESH		(ENA_PKT_MAX_BUFS + 2)
94 
95 #define DB_THRESHOLD	64
96 
97 #define TX_COMMIT	32
98  /*
99  * TX budget for cleaning. It should be half of the RX budget to reduce amount
100  *  of TCP retransmissions.
101  */
102 #define TX_BUDGET	128
103 /* RX cleanup budget. -1 stands for infinity. */
104 #define RX_BUDGET	256
105 /*
106  * How many times we can repeat cleanup in the io irq handling routine if the
107  * RX or TX budget was depleted.
108  */
109 #define CLEAN_BUDGET	8
110 
111 #define RX_IRQ_INTERVAL 20
112 #define TX_IRQ_INTERVAL 50
113 
114 #define	ENA_MIN_MTU		128
115 
116 #define	ENA_TSO_MAXSIZE		65536
117 
118 #define	ENA_MMIO_DISABLE_REG_READ	BIT(0)
119 
120 #define	ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
121 
122 #define	ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
123 
124 #define	ENA_IO_TXQ_IDX(q)		(2 * (q))
125 #define	ENA_IO_RXQ_IDX(q)		(2 * (q) + 1)
126 
127 #define	ENA_MGMNT_IRQ_IDX		0
128 #define	ENA_IO_IRQ_FIRST_IDX		1
129 #define	ENA_IO_IRQ_IDX(q)		(ENA_IO_IRQ_FIRST_IDX + (q))
130 
131 #define	ENA_MAX_NO_INTERRUPT_ITERATIONS	3
132 
133 /*
134  * ENA device should send keep alive msg every 1 sec.
135  * We wait for 6 sec just to be on the safe side.
136  */
137 #define DEFAULT_KEEP_ALIVE_TO		(SBT_1S * 6)
138 
139 /* Time in jiffies before concluding the transmitter is hung. */
140 #define DEFAULT_TX_CMP_TO		(SBT_1S * 5)
141 
142 /* Number of queues to check for missing queues per timer tick */
143 #define DEFAULT_TX_MONITORED_QUEUES	(4)
144 
145 /* Max number of timeouted packets before device reset */
146 #define DEFAULT_TX_CMP_THRESHOLD	(128)
147 
148 /*
149  * Supported PCI vendor and devices IDs
150  */
151 #define	PCI_VENDOR_ID_AMAZON	0x1d0f
152 
153 #define	PCI_DEV_ID_ENA_PF		0x0ec2
154 #define	PCI_DEV_ID_ENA_PF_RSERV0	0x1ec2
155 #define	PCI_DEV_ID_ENA_VF		0xec20
156 #define	PCI_DEV_ID_ENA_VF_RSERV0	0xec21
157 
158 /*
159  * Flags indicating current ENA driver state
160  */
161 enum ena_flags_t {
162 	ENA_FLAG_DEVICE_RUNNING,
163 	ENA_FLAG_DEV_UP,
164 	ENA_FLAG_LINK_UP,
165 	ENA_FLAG_MSIX_ENABLED,
166 	ENA_FLAG_TRIGGER_RESET,
167 	ENA_FLAG_ONGOING_RESET,
168 	ENA_FLAG_DEV_UP_BEFORE_RESET,
169 	ENA_FLAG_RSS_ACTIVE,
170 	ENA_FLAGS_NUMBER = ENA_FLAG_RSS_ACTIVE
171 };
172 
173 BITSET_DEFINE(_ena_state, ENA_FLAGS_NUMBER);
174 typedef struct _ena_state ena_state_t;
175 
176 #define ENA_FLAG_ZERO(adapter)		\
177 	BIT_ZERO(ENA_FLAGS_NUMBER, &(adapter)->flags)
178 #define ENA_FLAG_ISSET(bit, adapter)	\
179 	BIT_ISSET(ENA_FLAGS_NUMBER, (bit), &(adapter)->flags)
180 #define ENA_FLAG_SET_ATOMIC(bit, adapter)	\
181 	BIT_SET_ATOMIC(ENA_FLAGS_NUMBER, (bit), &(adapter)->flags)
182 #define ENA_FLAG_CLEAR_ATOMIC(bit, adapter)	\
183 	BIT_CLR_ATOMIC(ENA_FLAGS_NUMBER, (bit), &(adapter)->flags)
184 
185 struct msix_entry {
186 	int entry;
187 	int vector;
188 };
189 
190 typedef struct _ena_vendor_info_t {
191 	uint16_t vendor_id;
192 	uint16_t device_id;
193 	unsigned int index;
194 } ena_vendor_info_t;
195 
196 struct ena_irq {
197 	/* Interrupt resources */
198 	struct resource *res;
199 	driver_filter_t *handler;
200 	void *data;
201 	void *cookie;
202 	unsigned int vector;
203 	bool requested;
204 	int cpu;
205 	char name[ENA_IRQNAME_SIZE];
206 };
207 
208 struct ena_que {
209 	struct ena_adapter *adapter;
210 	struct ena_ring *tx_ring;
211 	struct ena_ring *rx_ring;
212 
213 	struct task cleanup_task;
214 	struct taskqueue *cleanup_tq;
215 
216 	uint32_t id;
217 	int cpu;
218 	struct sysctl_oid *oid;
219 };
220 
221 struct ena_calc_queue_size_ctx {
222 	struct ena_com_dev_get_features_ctx *get_feat_ctx;
223 	struct ena_com_dev *ena_dev;
224 	device_t pdev;
225 	uint32_t tx_queue_size;
226 	uint32_t rx_queue_size;
227 	uint32_t max_tx_queue_size;
228 	uint32_t max_rx_queue_size;
229 	uint16_t max_tx_sgl_size;
230 	uint16_t max_rx_sgl_size;
231 };
232 
233 #ifdef DEV_NETMAP
234 struct ena_netmap_tx_info {
235 	uint32_t socket_buf_idx[ENA_PKT_MAX_BUFS];
236 	bus_dmamap_t map_seg[ENA_PKT_MAX_BUFS];
237 	unsigned int sockets_used;
238 };
239 #endif
240 
241 struct ena_tx_buffer {
242 	struct mbuf *mbuf;
243 	/* # of ena desc for this specific mbuf
244 	 * (includes data desc and metadata desc) */
245 	unsigned int tx_descs;
246 	/* # of buffers used by this mbuf */
247 	unsigned int num_of_bufs;
248 
249 	bus_dmamap_t dmamap;
250 
251 	/* Used to detect missing tx packets */
252 	struct bintime timestamp;
253 	bool print_once;
254 
255 #ifdef DEV_NETMAP
256 	struct ena_netmap_tx_info nm_info;
257 #endif /* DEV_NETMAP */
258 
259 	struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
260 } __aligned(CACHE_LINE_SIZE);
261 
262 struct ena_rx_buffer {
263 	struct mbuf *mbuf;
264 	bus_dmamap_t map;
265 	struct ena_com_buf ena_buf;
266 #ifdef DEV_NETMAP
267 	uint32_t netmap_buf_idx;
268 #endif /* DEV_NETMAP */
269 } __aligned(CACHE_LINE_SIZE);
270 
271 struct ena_stats_tx {
272 	counter_u64_t cnt;
273 	counter_u64_t bytes;
274 	counter_u64_t prepare_ctx_err;
275 	counter_u64_t dma_mapping_err;
276 	counter_u64_t doorbells;
277 	counter_u64_t missing_tx_comp;
278 	counter_u64_t bad_req_id;
279 	counter_u64_t collapse;
280 	counter_u64_t collapse_err;
281 	counter_u64_t queue_wakeup;
282 	counter_u64_t queue_stop;
283 	counter_u64_t llq_buffer_copy;
284 };
285 
286 struct ena_stats_rx {
287 	counter_u64_t cnt;
288 	counter_u64_t bytes;
289 	counter_u64_t refil_partial;
290 	counter_u64_t bad_csum;
291 	counter_u64_t mjum_alloc_fail;
292 	counter_u64_t mbuf_alloc_fail;
293 	counter_u64_t dma_mapping_err;
294 	counter_u64_t bad_desc_num;
295 	counter_u64_t bad_req_id;
296 	counter_u64_t empty_rx_ring;
297 };
298 
299 struct ena_ring {
300 	/* Holds the empty requests for TX/RX out of order completions */
301 	union {
302 		uint16_t *free_tx_ids;
303 		uint16_t *free_rx_ids;
304 	};
305 	struct ena_com_dev *ena_dev;
306 	struct ena_adapter *adapter;
307 	struct ena_com_io_cq *ena_com_io_cq;
308 	struct ena_com_io_sq *ena_com_io_sq;
309 
310 	uint16_t qid;
311 
312 	/* Determines if device will use LLQ or normal mode for TX */
313 	enum ena_admin_placement_policy_type tx_mem_queue_type;
314 	union {
315 		/* The maximum length the driver can push to the device (For LLQ) */
316 		uint8_t tx_max_header_size;
317 		/* The maximum (and default) mbuf size for the Rx descriptor. */
318 		uint16_t rx_mbuf_sz;
319 
320 	};
321 
322 	bool first_interrupt;
323 	uint16_t no_interrupt_event_cnt;
324 
325 	struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
326 
327 	struct ena_que *que;
328 	struct lro_ctrl lro;
329 
330 	uint16_t next_to_use;
331 	uint16_t next_to_clean;
332 
333 	union {
334 		struct ena_tx_buffer *tx_buffer_info; /* contex of tx packet */
335 		struct ena_rx_buffer *rx_buffer_info; /* contex of rx packet */
336 	};
337 	int ring_size; /* number of tx/rx_buffer_info's entries */
338 
339 	struct buf_ring *br; /* only for TX */
340 	uint32_t buf_ring_size;
341 
342 	struct mtx ring_mtx;
343 	char mtx_name[16];
344 
345 	struct {
346 		struct task enqueue_task;
347 		struct taskqueue *enqueue_tq;
348 	};
349 
350 	union {
351 		struct ena_stats_tx tx_stats;
352 		struct ena_stats_rx rx_stats;
353 	};
354 
355 	union {
356 		int empty_rx_queue;
357 		/* For Tx ring to indicate if it's running or not */
358 		bool running;
359 	};
360 
361 	/* How many packets are sent in one Tx loop, used for doorbells */
362 	uint32_t acum_pkts;
363 
364 	/* Used for LLQ */
365 	uint8_t *push_buf_intermediate_buf;
366 
367 #ifdef DEV_NETMAP
368 	bool initialized;
369 #endif /* DEV_NETMAP */
370 } __aligned(CACHE_LINE_SIZE);
371 
372 struct ena_stats_dev {
373 	counter_u64_t wd_expired;
374 	counter_u64_t interface_up;
375 	counter_u64_t interface_down;
376 	counter_u64_t admin_q_pause;
377 };
378 
379 struct ena_hw_stats {
380 	counter_u64_t rx_packets;
381 	counter_u64_t tx_packets;
382 
383 	counter_u64_t rx_bytes;
384 	counter_u64_t tx_bytes;
385 
386 	counter_u64_t rx_drops;
387 	counter_u64_t tx_drops;
388 };
389 
390 /* Board specific private data structure */
391 struct ena_adapter {
392 	struct ena_com_dev *ena_dev;
393 
394 	/* OS defined structs */
395 	if_t ifp;
396 	device_t pdev;
397 	struct ifmedia	media;
398 
399 	/* OS resources */
400 	struct resource *memory;
401 	struct resource *registers;
402 	struct resource *msix;
403 	int msix_rid;
404 
405 	struct sx global_lock;
406 
407 	/* MSI-X */
408 	struct msix_entry *msix_entries;
409 	int msix_vecs;
410 
411 	/* DMA tags used throughout the driver adapter for Tx and Rx */
412 	bus_dma_tag_t tx_buf_tag;
413 	bus_dma_tag_t rx_buf_tag;
414 	int dma_width;
415 
416 	uint32_t max_mtu;
417 
418 	uint32_t num_io_queues;
419 	uint32_t max_num_io_queues;
420 
421 	uint32_t requested_tx_ring_size;
422 	uint32_t requested_rx_ring_size;
423 
424 	uint32_t max_tx_ring_size;
425 	uint32_t max_rx_ring_size;
426 
427 	uint16_t max_tx_sgl_size;
428 	uint16_t max_rx_sgl_size;
429 
430 	uint32_t tx_offload_cap;
431 
432 	uint32_t buf_ring_size;
433 
434 	/* RSS*/
435 	uint8_t	rss_ind_tbl[ENA_RX_RSS_TABLE_SIZE];
436 
437 	uint8_t mac_addr[ETHER_ADDR_LEN];
438 	/* mdio and phy*/
439 
440 	ena_state_t flags;
441 
442 	/* Queue will represent one TX and one RX ring */
443 	struct ena_que que[ENA_MAX_NUM_IO_QUEUES]
444 	    __aligned(CACHE_LINE_SIZE);
445 
446 	/* TX */
447 	struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
448 	    __aligned(CACHE_LINE_SIZE);
449 
450 	/* RX */
451 	struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
452 	    __aligned(CACHE_LINE_SIZE);
453 
454 	struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
455 
456 	/* Timer service */
457 	struct callout timer_service;
458 	sbintime_t keep_alive_timestamp;
459 	uint32_t next_monitored_tx_qid;
460 	struct task reset_task;
461 	struct taskqueue *reset_tq;
462 	int wd_active;
463 	sbintime_t keep_alive_timeout;
464 	sbintime_t missing_tx_timeout;
465 	uint32_t missing_tx_max_queues;
466 	uint32_t missing_tx_threshold;
467 	bool disable_meta_caching;
468 
469 	uint16_t eni_metrics_sample_interval;
470 	uint16_t eni_metrics_sample_interval_cnt;
471 
472 	/* Statistics */
473 	struct ena_stats_dev dev_stats;
474 	struct ena_hw_stats hw_stats;
475 	struct ena_admin_eni_stats eni_metrics;
476 
477 	enum ena_regs_reset_reason_types reset_reason;
478 };
479 
480 #define	ENA_RING_MTX_LOCK(_ring)		mtx_lock(&(_ring)->ring_mtx)
481 #define	ENA_RING_MTX_TRYLOCK(_ring)		mtx_trylock(&(_ring)->ring_mtx)
482 #define	ENA_RING_MTX_UNLOCK(_ring)		mtx_unlock(&(_ring)->ring_mtx)
483 
484 #define ENA_LOCK_INIT(adapter)			\
485 	sx_init(&(adapter)->global_lock, "ENA global lock")
486 #define ENA_LOCK_DESTROY(adapter)	sx_destroy(&(adapter)->global_lock)
487 #define ENA_LOCK_LOCK(adapter)		sx_xlock(&(adapter)->global_lock)
488 #define ENA_LOCK_UNLOCK(adapter)	sx_unlock(&(adapter)->global_lock)
489 
490 #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
491 #define clamp_val(val, lo, hi)		clamp_t(__typeof(val), val, lo, hi)
492 
493 static inline int ena_mbuf_count(struct mbuf *mbuf)
494 {
495 	int count = 1;
496 
497 	while ((mbuf = mbuf->m_next) != NULL)
498 		++count;
499 
500 	return count;
501 }
502 
503 int	ena_up(struct ena_adapter *adapter);
504 void	ena_down(struct ena_adapter *adapter);
505 int	ena_restore_device(struct ena_adapter *adapter);
506 void	ena_destroy_device(struct ena_adapter *adapter, bool graceful);
507 int	ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num);
508 int	ena_update_buf_ring_size(struct ena_adapter *adapter,
509     uint32_t new_buf_ring_size);
510 int	ena_update_queue_size(struct ena_adapter *adapter, uint32_t new_tx_size,
511     uint32_t new_rx_size);
512 int	ena_update_io_queue_nb(struct ena_adapter *adapter, uint32_t new_num);
513 
514 static inline void
515 ena_trigger_reset(struct ena_adapter *adapter,
516     enum ena_regs_reset_reason_types reset_reason)
517 {
518 	if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
519 		adapter->reset_reason = reset_reason;
520 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
521 	}
522 }
523 
524 #endif /* !(ENA_H) */
525