4 * Copyright (c) 2015-2019 Amazon.com, Inc. or its affiliates.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
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.
37 #include <sys/types.h>
39 #include "ena-com/ena_com.h"
40 #include "ena-com/ena_eth_com.h"
42 #define DRV_MODULE_VER_MAJOR 2
43 #define DRV_MODULE_VER_MINOR 0
44 #define DRV_MODULE_VER_SUBMINOR 0
46 #define DRV_MODULE_NAME "ena"
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)
54 #define DEVICE_NAME "Elastic Network Adapter (ENA)"
55 #define DEVICE_DESC "ENA adapter"
57 /* Calculate DMA mask - width for ena cannot exceed 48, so it is safe */
58 #define ENA_DMA_BIT_MASK(x) ((1ULL << (x)) - 1ULL)
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))
67 #define ENA_BUS_DMA_SEGS 32
69 #define ENA_DEFAULT_BUF_RING_SIZE 4096
71 #define ENA_DEFAULT_RING_SIZE 1024
74 * Refill Rx queue when number of required descriptors is above
75 * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
77 #define ENA_RX_REFILL_THRESH_DIVIDER 8
78 #define ENA_RX_REFILL_THRESH_PACKET 256
80 #define ENA_IRQNAME_SIZE 40
82 #define ENA_PKT_MAX_BUFS 19
84 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
85 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
87 #define ENA_HASH_KEY_SIZE 40
89 #define ENA_MAX_FRAME_LEN 10000
90 #define ENA_MIN_FRAME_LEN 60
92 #define ENA_TX_RESUME_THRESH (ENA_PKT_MAX_BUFS + 2)
94 #define DB_THRESHOLD 64
98 * TX budget for cleaning. It should be half of the RX budget to reduce amount
99 * of TCP retransmissions.
101 #define TX_BUDGET 128
102 /* RX cleanup budget. -1 stands for infinity. */
103 #define RX_BUDGET 256
105 * How many times we can repeat cleanup in the io irq handling routine if the
106 * RX or TX budget was depleted.
108 #define CLEAN_BUDGET 8
110 #define RX_IRQ_INTERVAL 20
111 #define TX_IRQ_INTERVAL 50
113 #define ENA_MIN_MTU 128
115 #define ENA_TSO_MAXSIZE 65536
117 #define ENA_MMIO_DISABLE_REG_READ BIT(0)
119 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
121 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
123 #define ENA_IO_TXQ_IDX(q) (2 * (q))
124 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
126 #define ENA_MGMNT_IRQ_IDX 0
127 #define ENA_IO_IRQ_FIRST_IDX 1
128 #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q))
130 #define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
133 * ENA device should send keep alive msg every 1 sec.
134 * We wait for 6 sec just to be on the safe side.
136 #define DEFAULT_KEEP_ALIVE_TO (SBT_1S * 6)
138 /* Time in jiffies before concluding the transmitter is hung. */
139 #define DEFAULT_TX_CMP_TO (SBT_1S * 5)
141 /* Number of queues to check for missing queues per timer tick */
142 #define DEFAULT_TX_MONITORED_QUEUES (4)
144 /* Max number of timeouted packets before device reset */
145 #define DEFAULT_TX_CMP_THRESHOLD (128)
148 * Supported PCI vendor and devices IDs
150 #define PCI_VENDOR_ID_AMAZON 0x1d0f
152 #define PCI_DEV_ID_ENA_PF 0x0ec2
153 #define PCI_DEV_ID_ENA_LLQ_PF 0x1ec2
154 #define PCI_DEV_ID_ENA_VF 0xec20
155 #define PCI_DEV_ID_ENA_LLQ_VF 0xec21
158 * Flags indicating current ENA driver state
161 ENA_FLAG_DEVICE_RUNNING,
164 ENA_FLAG_MSIX_ENABLED,
165 ENA_FLAG_TRIGGER_RESET,
166 ENA_FLAG_ONGOING_RESET,
167 ENA_FLAG_DEV_UP_BEFORE_RESET,
169 ENA_FLAGS_NUMBER = ENA_FLAG_RSS_ACTIVE
172 BITSET_DEFINE(_ena_state, ENA_FLAGS_NUMBER);
173 typedef struct _ena_state ena_state_t;
175 #define ENA_FLAG_ZERO(adapter) \
176 BIT_ZERO(ENA_FLAGS_NUMBER, &(adapter)->flags)
177 #define ENA_FLAG_ISSET(bit, adapter) \
178 BIT_ISSET(ENA_FLAGS_NUMBER, (bit), &(adapter)->flags)
179 #define ENA_FLAG_SET_ATOMIC(bit, adapter) \
180 BIT_SET_ATOMIC(ENA_FLAGS_NUMBER, (bit), &(adapter)->flags)
181 #define ENA_FLAG_CLEAR_ATOMIC(bit, adapter) \
182 BIT_CLR_ATOMIC(ENA_FLAGS_NUMBER, (bit), &(adapter)->flags)
189 typedef struct _ena_vendor_info_t {
196 /* Interrupt resources */
197 struct resource *res;
198 driver_filter_t *handler;
204 char name[ENA_IRQNAME_SIZE];
208 struct ena_adapter *adapter;
209 struct ena_ring *tx_ring;
210 struct ena_ring *rx_ring;
212 struct task cleanup_task;
213 struct taskqueue *cleanup_tq;
219 struct ena_calc_queue_size_ctx {
220 struct ena_com_dev_get_features_ctx *get_feat_ctx;
221 struct ena_com_dev *ena_dev;
223 uint16_t rx_queue_size;
224 uint16_t tx_queue_size;
225 uint16_t max_tx_sgl_size;
226 uint16_t max_rx_sgl_size;
229 struct ena_tx_buffer {
231 /* # of ena desc for this specific mbuf
232 * (includes data desc and metadata desc) */
233 unsigned int tx_descs;
234 /* # of buffers used by this mbuf */
235 unsigned int num_of_bufs;
236 bus_dmamap_t map_head;
237 bus_dmamap_t map_seg;
239 /* Indicate if segments of the mbuf were mapped */
241 /* Indicate if bufs[0] maps the linear data of the mbuf */
244 /* Used to detect missing tx packets */
245 struct bintime timestamp;
248 struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
249 } __aligned(CACHE_LINE_SIZE);
251 struct ena_rx_buffer {
254 struct ena_com_buf ena_buf;
255 } __aligned(CACHE_LINE_SIZE);
257 struct ena_stats_tx {
260 counter_u64_t prepare_ctx_err;
261 counter_u64_t dma_mapping_err;
262 counter_u64_t doorbells;
263 counter_u64_t missing_tx_comp;
264 counter_u64_t bad_req_id;
265 counter_u64_t collapse;
266 counter_u64_t collapse_err;
267 counter_u64_t queue_wakeup;
268 counter_u64_t queue_stop;
269 counter_u64_t llq_buffer_copy;
272 struct ena_stats_rx {
275 counter_u64_t refil_partial;
276 counter_u64_t bad_csum;
277 counter_u64_t mjum_alloc_fail;
278 counter_u64_t mbuf_alloc_fail;
279 counter_u64_t dma_mapping_err;
280 counter_u64_t bad_desc_num;
281 counter_u64_t bad_req_id;
282 counter_u64_t empty_rx_ring;
286 /* Holds the empty requests for TX/RX out of order completions */
288 uint16_t *free_tx_ids;
289 uint16_t *free_rx_ids;
291 struct ena_com_dev *ena_dev;
292 struct ena_adapter *adapter;
293 struct ena_com_io_cq *ena_com_io_cq;
294 struct ena_com_io_sq *ena_com_io_sq;
298 /* Determines if device will use LLQ or normal mode for TX */
299 enum ena_admin_placement_policy_type tx_mem_queue_type;
300 /* The maximum length the driver can push to the device (For LLQ) */
301 uint8_t tx_max_header_size;
303 bool first_interrupt;
304 uint16_t no_interrupt_event_cnt;
306 struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
309 * Fields used for Adaptive Interrupt Modulation - to be implemented in
310 * the future releases
312 uint32_t smoothed_interval;
313 enum ena_intr_moder_level moder_tbl_idx;
318 uint16_t next_to_use;
319 uint16_t next_to_clean;
322 struct ena_tx_buffer *tx_buffer_info; /* contex of tx packet */
323 struct ena_rx_buffer *rx_buffer_info; /* contex of rx packet */
325 int ring_size; /* number of tx/rx_buffer_info's entries */
327 struct buf_ring *br; /* only for TX */
328 uint32_t buf_ring_size;
334 struct task enqueue_task;
335 struct taskqueue *enqueue_tq;
339 struct ena_stats_tx tx_stats;
340 struct ena_stats_rx rx_stats;
345 /* For Tx ring to indicate if it's running or not */
349 /* How many packets are sent in one Tx loop, used for doorbells */
353 uint8_t *push_buf_intermediate_buf;
354 } __aligned(CACHE_LINE_SIZE);
356 struct ena_stats_dev {
357 counter_u64_t wd_expired;
358 counter_u64_t interface_up;
359 counter_u64_t interface_down;
360 counter_u64_t admin_q_pause;
363 struct ena_hw_stats {
364 counter_u64_t rx_packets;
365 counter_u64_t tx_packets;
367 counter_u64_t rx_bytes;
368 counter_u64_t tx_bytes;
370 counter_u64_t rx_drops;
373 /* Board specific private data structure */
375 struct ena_com_dev *ena_dev;
377 /* OS defined structs */
380 struct ifmedia media;
383 struct resource *memory;
384 struct resource *registers;
386 struct mtx global_mtx;
390 struct msix_entry *msix_entries;
393 /* DMA tags used throughout the driver adapter for Tx and Rx */
394 bus_dma_tag_t tx_buf_tag;
395 bus_dma_tag_t rx_buf_tag;
400 uint16_t max_tx_sgl_size;
401 uint16_t max_rx_sgl_size;
403 uint32_t tx_offload_cap;
405 /* Tx fast path data */
408 unsigned int tx_ring_size;
409 unsigned int rx_ring_size;
411 uint16_t buf_ring_size;
414 uint8_t rss_ind_tbl[ENA_RX_RSS_TABLE_SIZE];
416 uint8_t mac_addr[ETHER_ADDR_LEN];
421 /* Queue will represent one TX and one RX ring */
422 struct ena_que que[ENA_MAX_NUM_IO_QUEUES]
423 __aligned(CACHE_LINE_SIZE);
426 struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
427 __aligned(CACHE_LINE_SIZE);
430 struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
431 __aligned(CACHE_LINE_SIZE);
433 struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
436 struct callout timer_service;
437 sbintime_t keep_alive_timestamp;
438 uint32_t next_monitored_tx_qid;
439 struct task reset_task;
440 struct taskqueue *reset_tq;
442 sbintime_t keep_alive_timeout;
443 sbintime_t missing_tx_timeout;
444 uint32_t missing_tx_max_queues;
445 uint32_t missing_tx_threshold;
448 struct ena_stats_dev dev_stats;
449 struct ena_hw_stats hw_stats;
451 enum ena_regs_reset_reason_types reset_reason;
454 #define ENA_RING_MTX_LOCK(_ring) mtx_lock(&(_ring)->ring_mtx)
455 #define ENA_RING_MTX_TRYLOCK(_ring) mtx_trylock(&(_ring)->ring_mtx)
456 #define ENA_RING_MTX_UNLOCK(_ring) mtx_unlock(&(_ring)->ring_mtx)
458 static inline int ena_mbuf_count(struct mbuf *mbuf)
462 while ((mbuf = mbuf->m_next) != NULL)
468 #endif /* !(ENA_H) */