2 * Copyright 2015 Amazon.com, Inc. or its affiliates.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/bitops.h>
37 #include <linux/etherdevice.h>
38 #include <linux/inetdevice.h>
39 #include <linux/interrupt.h>
40 #include <linux/netdevice.h>
41 #include <linux/skbuff.h>
44 #include "ena_eth_com.h"
46 #define DRV_MODULE_VER_MAJOR 1
47 #define DRV_MODULE_VER_MINOR 2
48 #define DRV_MODULE_VER_SUBMINOR 0
50 #define DRV_MODULE_NAME "ena"
51 #ifndef DRV_MODULE_VERSION
52 #define DRV_MODULE_VERSION \
53 __stringify(DRV_MODULE_VER_MAJOR) "." \
54 __stringify(DRV_MODULE_VER_MINOR) "." \
55 __stringify(DRV_MODULE_VER_SUBMINOR) "k"
58 #define DEVICE_NAME "Elastic Network Adapter (ENA)"
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))
64 #define ENA_MIN_MSIX_VEC 2
68 #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
70 #define ENA_DEFAULT_RING_SIZE (1024)
72 #define ENA_TX_WAKEUP_THRESH (MAX_SKB_FRAGS + 2)
73 #define ENA_DEFAULT_RX_COPYBREAK (128 - NET_IP_ALIGN)
75 /* limit the buffer size to 600 bytes to handle MTU changes from very
76 * small to very large, in which case the number of buffers per packet
77 * could exceed ENA_PKT_MAX_BUFS
79 #define ENA_DEFAULT_MIN_RX_BUFF_ALLOC_SIZE 600
81 #define ENA_MIN_MTU 128
83 #define ENA_NAME_MAX_LEN 20
84 #define ENA_IRQNAME_SIZE 40
86 #define ENA_PKT_MAX_BUFS 19
88 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
89 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
91 #define ENA_HASH_KEY_SIZE 40
93 /* The number of tx packet completions that will be handled each NAPI poll
94 * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
96 #define ENA_TX_POLL_BUDGET_DIVIDER 4
98 /* Refill Rx queue when number of available descriptors is below
99 * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER
101 #define ENA_RX_REFILL_THRESH_DIVIDER 8
103 /* Number of queues to check for missing queues per timer service */
104 #define ENA_MONITORED_TX_QUEUES 4
105 /* Max timeout packets before device reset */
106 #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
108 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
110 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
111 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
112 (((idx) + (n)) & ((ring_size) - 1))
114 #define ENA_IO_TXQ_IDX(q) (2 * (q))
115 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
117 #define ENA_MGMNT_IRQ_IDX 0
118 #define ENA_IO_IRQ_FIRST_IDX 1
119 #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q))
121 /* ENA device should send keep alive msg every 1 sec.
122 * We wait for 6 sec just to be on the safe side.
124 #define ENA_DEVICE_KALIVE_TIMEOUT (6 * HZ)
126 #define ENA_MMIO_DISABLE_REG_READ BIT(0)
129 irq_handler_t handler
;
133 cpumask_t affinity_hint_mask
;
134 char name
[ENA_IRQNAME_SIZE
];
138 struct napi_struct napi ____cacheline_aligned
;
139 struct ena_ring
*tx_ring
;
140 struct ena_ring
*rx_ring
;
144 struct ena_tx_buffer
{
146 /* num of ena desc for this specific skb
147 * (includes data desc and metadata desc)
150 /* num of buffers used by this skb */
153 /* Used for detect missing tx packets to limit the number of prints */
155 /* Save the last jiffies to detect missing tx packets
157 * sets to non zero value on ena_start_xmit and set to zero on
158 * napi and timer_Service_routine.
160 * while this value is not protected by lock,
161 * a given packet is not expected to be handled by ena_start_xmit
162 * and by napi/timer_service at the same time.
164 unsigned long last_jiffies
;
165 struct ena_com_buf bufs
[ENA_PKT_MAX_BUFS
];
166 } ____cacheline_aligned
;
168 struct ena_rx_buffer
{
172 struct ena_com_buf ena_buf
;
173 } ____cacheline_aligned
;
175 struct ena_stats_tx
{
183 u64 linearize_failed
;
190 struct ena_stats_rx
{
199 u64 rx_copybreak_pkt
;
206 /* Holds the empty requests for TX/RX
207 * out of order completions
214 struct ena_tx_buffer
*tx_buffer_info
;
215 struct ena_rx_buffer
*rx_buffer_info
;
218 /* cache ptr to avoid using the adapter */
220 struct pci_dev
*pdev
;
221 struct napi_struct
*napi
;
222 struct net_device
*netdev
;
223 struct ena_com_dev
*ena_dev
;
224 struct ena_adapter
*adapter
;
225 struct ena_com_io_cq
*ena_com_io_cq
;
226 struct ena_com_io_sq
*ena_com_io_sq
;
235 /* The maximum header length the device can handle */
236 u8 tx_max_header_size
;
240 /* number of tx/rx_buffer_info's entries */
243 enum ena_admin_placement_policy_type tx_mem_queue_type
;
245 struct ena_com_rx_buf_info ena_bufs
[ENA_PKT_MAX_BUFS
];
246 u32 smoothed_interval
;
247 u32 per_napi_packets
;
249 enum ena_intr_moder_level moder_tbl_idx
;
250 struct u64_stats_sync syncp
;
252 struct ena_stats_tx tx_stats
;
253 struct ena_stats_rx rx_stats
;
256 } ____cacheline_aligned
;
258 struct ena_stats_dev
{
270 ENA_FLAG_DEVICE_RUNNING
,
273 ENA_FLAG_MSIX_ENABLED
,
274 ENA_FLAG_TRIGGER_RESET
277 /* adapter specific private data structure */
279 struct ena_com_dev
*ena_dev
;
280 /* OS defined structs */
281 struct net_device
*netdev
;
282 struct pci_dev
*pdev
;
284 /* rx packets that shorter that this len will be copied to the skb
294 u32 missing_tx_completion_threshold
;
296 u32 tx_usecs
, rx_usecs
; /* interrupt moderation */
297 u32 tx_frames
, rx_frames
; /* interrupt moderation */
307 u8 mac_addr
[ETH_ALEN
];
309 unsigned long keep_alive_timeout
;
310 unsigned long missing_tx_completion_to
;
312 char name
[ENA_NAME_MAX_LEN
];
316 struct ena_ring tx_ring
[ENA_MAX_NUM_IO_QUEUES
]
317 ____cacheline_aligned_in_smp
;
320 struct ena_ring rx_ring
[ENA_MAX_NUM_IO_QUEUES
]
321 ____cacheline_aligned_in_smp
;
323 struct ena_napi ena_napi
[ENA_MAX_NUM_IO_QUEUES
];
325 struct ena_irq irq_tbl
[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES
)];
328 struct work_struct reset_task
;
329 struct work_struct suspend_io_task
;
330 struct work_struct resume_io_task
;
331 struct timer_list timer_service
;
334 unsigned long last_keep_alive_jiffies
;
336 struct u64_stats_sync syncp
;
337 struct ena_stats_dev dev_stats
;
339 /* last queue index that was checked for uncompleted tx packets */
340 u32 last_monitored_tx_qid
;
342 enum ena_regs_reset_reason_types reset_reason
;
345 void ena_set_ethtool_ops(struct net_device
*netdev
);
347 void ena_dump_stats_to_dmesg(struct ena_adapter
*adapter
);
349 void ena_dump_stats_to_buf(struct ena_adapter
*adapter
, u8
*buf
);
351 int ena_get_sset_count(struct net_device
*netdev
, int sset
);
353 #endif /* !(ENA_H) */