]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ena/ena.h
Remove deprecated and unused counters in ENA driver
[FreeBSD/FreeBSD.git] / sys / dev / ena / ena.h
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright (c) 2015-2017 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    0
43 #define DRV_MODULE_VER_MINOR    8
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_MAX_MSIX_VEC(io_queues)     (1 + (io_queues))
62
63 #define ENA_REG_BAR                     0
64 #define ENA_MEM_BAR                     2
65
66 #define ENA_BUS_DMA_SEGS                32
67
68 #define ENA_DEFAULT_RING_SIZE           1024
69 #define ENA_DEFAULT_SMALL_PACKET_LEN    128
70 #define ENA_DEFAULT_MAX_RX_BUFF_ALLOC_SIZE      1536
71
72 #define ENA_RX_REFILL_THRESH_DEVIDER    8
73
74 #define ENA_MAX_PUSH_PKT_SIZE           128
75
76 #define ENA_NAME_MAX_LEN                20
77 #define ENA_IRQNAME_SIZE                40
78
79 #define ENA_PKT_MAX_BUFS                19
80 #define ENA_STALL_TIMEOUT               100
81
82 #define ENA_RX_RSS_TABLE_LOG_SIZE       7
83 #define ENA_RX_RSS_TABLE_SIZE           (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
84
85 #define ENA_HASH_KEY_SIZE               40
86
87 #define ENA_DMA_BITS_MASK               40
88 #define ENA_MAX_FRAME_LEN               10000
89 #define ENA_MIN_FRAME_LEN               60
90 #define ENA_RX_HASH_KEY_NUM             10
91 #define ENA_RX_THASH_TABLE_SIZE         (1 << 8)
92
93 #define ENA_TX_CLEANUP_THRESHOLD        128
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_MAX_MTU             9216
115 #define ENA_TSO_MAXSIZE         65536
116 #define ENA_TSO_NSEGS           ENA_PKT_MAX_BUFS
117 #define ENA_RX_OFFSET           NET_SKB_PAD + NET_IP_ALIGN
118
119 #define ENA_MMIO_DISABLE_REG_READ       BIT(0)
120
121 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
122
123 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
124 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size)  \
125         (((idx) + (n)) & ((ring_size) - 1))
126
127 #define ENA_IO_TXQ_IDX(q)               (2 * (q))
128 #define ENA_IO_RXQ_IDX(q)               (2 * (q) + 1)
129
130 #define ENA_MGMNT_IRQ_IDX               0
131 #define ENA_IO_IRQ_FIRST_IDX            1
132 #define ENA_IO_IRQ_IDX(q)               (ENA_IO_IRQ_FIRST_IDX + (q))
133
134 /*
135  * ENA device should send keep alive msg every 1 sec.
136  * We wait for 6 sec just to be on the safe side.
137  */
138 #define DEFAULT_KEEP_ALIVE_TO           (SBT_1S * 6)
139
140 /* Time in jiffies before concluding the transmitter is hung. */
141 #define DEFAULT_TX_CMP_TO               (SBT_1S * 5)
142
143 /* Number of queues to check for missing queues per timer tick */
144 #define DEFAULT_TX_MONITORED_QUEUES     (4)
145
146 /* Max number of timeouted packets before device reset */
147 #define DEFAULT_TX_CMP_THRESHOLD        (128)
148
149 /*
150  * Supported PCI vendor and devices IDs
151  */
152 #define PCI_VENDOR_ID_AMAZON    0x1d0f
153
154 #define PCI_DEV_ID_ENA_PF       0x0ec2
155 #define PCI_DEV_ID_ENA_LLQ_PF   0x1ec2
156 #define PCI_DEV_ID_ENA_VF       0xec20
157 #define PCI_DEV_ID_ENA_LLQ_VF   0xec21
158
159 struct msix_entry {
160         int entry;
161         int vector;
162 };
163
164 typedef struct _ena_vendor_info_t {
165         unsigned int vendor_id;
166         unsigned int device_id;
167         unsigned int index;
168 } ena_vendor_info_t;
169
170 struct ena_irq {
171         /* Interrupt resources */
172         struct resource *res;
173         driver_intr_t *handler;
174         void *data;
175         void *cookie;
176         unsigned int vector;
177         bool requested;
178         int cpu;
179         char name[ENA_IRQNAME_SIZE];
180 };
181
182 struct ena_que {
183         struct ena_adapter *adapter;
184         struct ena_ring *tx_ring;
185         struct ena_ring *rx_ring;
186         uint32_t id;
187         int cpu;
188 };
189
190 struct ena_tx_buffer {
191         struct mbuf *mbuf;
192         /* # of ena desc for this specific mbuf
193          * (includes data desc and metadata desc) */
194         unsigned int tx_descs;
195         /* # of buffers used by this mbuf */
196         unsigned int num_of_bufs;
197         bus_dmamap_t map;
198
199         /* Used to detect missing tx packets */
200         struct bintime timestamp;
201         bool print_once;
202
203         struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
204 } __aligned(CACHE_LINE_SIZE);
205
206 struct ena_rx_buffer {
207         struct mbuf *mbuf;
208         bus_dmamap_t map;
209         struct ena_com_buf ena_buf;
210 } __aligned(CACHE_LINE_SIZE);
211
212
213 struct ena_stats_tx {
214         counter_u64_t cnt;
215         counter_u64_t bytes;
216         counter_u64_t prepare_ctx_err;
217         counter_u64_t dma_mapping_err;
218         counter_u64_t doorbells;
219         counter_u64_t missing_tx_comp;
220         counter_u64_t bad_req_id;
221         counter_u64_t collapse;
222         counter_u64_t collapse_err;
223 };
224
225 struct ena_stats_rx {
226         counter_u64_t cnt;
227         counter_u64_t bytes;
228         counter_u64_t refil_partial;
229         counter_u64_t bad_csum;
230         counter_u64_t mbuf_alloc_fail;
231         counter_u64_t dma_mapping_err;
232         counter_u64_t bad_desc_num;
233         counter_u64_t bad_req_id;
234         counter_u64_t empty_rx_ring;
235 };
236
237 struct ena_ring {
238         /* Holds the empty requests for TX/RX out of order completions */
239         union {
240                 uint16_t *free_tx_ids;
241                 uint16_t *free_rx_ids;
242         };
243         struct ena_com_dev *ena_dev;
244         struct ena_adapter *adapter;
245         struct ena_com_io_cq *ena_com_io_cq;
246         struct ena_com_io_sq *ena_com_io_sq;
247
248         /* The maximum length the driver can push to the device (For LLQ) */
249         enum ena_admin_placement_policy_type tx_mem_queue_type;
250         uint16_t rx_small_copy_len;
251         uint16_t qid;
252         uint16_t mtu;
253         uint8_t tx_max_header_size;
254
255         struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
256         uint32_t  smoothed_interval;
257         enum ena_intr_moder_level moder_tbl_idx;
258
259         struct ena_que *que;
260         struct lro_ctrl lro;
261
262         uint16_t next_to_use;
263         uint16_t next_to_clean;
264
265         union {
266                 struct ena_tx_buffer *tx_buffer_info; /* contex of tx packet */
267                 struct ena_rx_buffer *rx_buffer_info; /* contex of rx packet */
268         };
269         int ring_size; /* number of tx/rx_buffer_info's entries */
270
271         struct buf_ring *br; /* only for TX */
272         struct mtx ring_mtx;
273         char mtx_name[16];
274         union {
275                 struct {
276                         struct task enqueue_task;
277                         struct taskqueue *enqueue_tq;
278                 };
279                 struct {
280                         struct task cmpl_task;
281                         struct taskqueue *cmpl_tq;
282                 };
283         };
284
285         union {
286                 struct ena_stats_tx tx_stats;
287                 struct ena_stats_rx rx_stats;
288         };
289
290         int empty_rx_queue;
291
292 } __aligned(CACHE_LINE_SIZE);
293
294 struct ena_stats_dev {
295         counter_u64_t wd_expired;
296         counter_u64_t interface_up;
297         counter_u64_t interface_down;
298         counter_u64_t admin_q_pause;
299 };
300
301 struct ena_hw_stats {
302         counter_u64_t rx_packets;
303         counter_u64_t tx_packets;
304
305         counter_u64_t rx_bytes;
306         counter_u64_t tx_bytes;
307
308         counter_u64_t rx_drops;
309 };
310
311 /* Board specific private data structure */
312 struct ena_adapter {
313         struct ena_com_dev *ena_dev;
314
315         /* OS defined structs */
316         if_t ifp;
317         device_t pdev;
318         struct ifmedia  media;
319
320         /* OS resources */
321         struct resource * memory;
322         struct resource * registers;
323
324         struct mtx global_mtx;
325         struct sx ioctl_sx;
326
327         /* MSI-X */
328         uint32_t msix_enabled;
329         struct msix_entry *msix_entries;
330         int msix_vecs;
331
332         /* DMA tags used throughout the driver adapter for Tx and Rx */
333         bus_dma_tag_t tx_buf_tag;
334         bus_dma_tag_t rx_buf_tag;
335         int dma_width;
336         /*
337          * RX packets that shorter that this len will be copied to the skb
338          * header
339          */
340         unsigned int small_copy_len;
341
342         uint16_t max_tx_sgl_size;
343         uint16_t max_rx_sgl_size;
344
345         uint32_t tx_offload_cap;
346
347         /* Tx fast path data */
348         int num_queues;
349
350         unsigned int tx_usecs, rx_usecs; /* Interrupt coalescing */
351
352         unsigned int tx_ring_size;
353         unsigned int rx_ring_size;
354
355         /* RSS*/
356         uint8_t  rss_ind_tbl[ENA_RX_RSS_TABLE_SIZE];
357         bool rss_support;
358
359         uint32_t msg_enable;
360
361         uint8_t mac_addr[ETHER_ADDR_LEN];
362         /* mdio and phy*/
363
364         char name[ENA_NAME_MAX_LEN];
365         bool link_status;
366         bool trigger_reset;
367         bool up;
368         bool running;
369
370         uint32_t wol;
371
372         /* Queue will represent one TX and one RX ring */
373         struct ena_que que[ENA_MAX_NUM_IO_QUEUES]
374             __aligned(CACHE_LINE_SIZE);
375
376         /* TX */
377         struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
378             __aligned(CACHE_LINE_SIZE);
379
380         /* RX */
381         struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
382             __aligned(CACHE_LINE_SIZE);
383
384         struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
385
386         /* Timer service */
387         struct callout timer_service;
388         sbintime_t keep_alive_timestamp;
389         uint32_t next_monitored_tx_qid;
390         struct task reset_task;
391         struct taskqueue *reset_tq;
392         int wd_active;
393         sbintime_t keep_alive_timeout;
394         sbintime_t missing_tx_timeout;
395         uint32_t missing_tx_max_queues;
396         uint32_t missing_tx_threshold;
397
398         /* Statistics */
399         struct ena_stats_dev dev_stats;
400         struct ena_hw_stats hw_stats;
401
402         enum ena_regs_reset_reason_types reset_reason;
403 };
404
405 #define ENA_DEV_LOCK                    mtx_lock(&adapter->global_mtx)
406 #define ENA_DEV_UNLOCK                  mtx_unlock(&adapter->global_mtx)
407
408 #define ENA_RING_MTX_LOCK(_ring)                mtx_lock(&(_ring)->ring_mtx)
409 #define ENA_RING_MTX_TRYLOCK(_ring)             mtx_trylock(&(_ring)->ring_mtx)
410 #define ENA_RING_MTX_UNLOCK(_ring)              mtx_unlock(&(_ring)->ring_mtx)
411
412 struct ena_dev *ena_efa_enadev_get(device_t pdev);
413
414 int ena_register_adapter(struct ena_adapter *adapter);
415 void ena_unregister_adapter(struct ena_adapter *adapter);
416
417 static inline int ena_mbuf_count(struct mbuf *mbuf)
418 {
419         int count = 1;
420
421         while ((mbuf = mbuf->m_next) != NULL)
422                 ++count;
423
424         return count;
425 }
426
427 #endif /* !(ENA_H) */