1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
9 #include <linux/bitops.h>
10 #include <linux/dim.h>
11 #include <linux/etherdevice.h>
12 #include <linux/if_vlan.h>
13 #include <linux/inetdevice.h>
14 #include <linux/interrupt.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <uapi/linux/bpf.h>
20 #include "ena_eth_com.h"
22 #define DRV_MODULE_GEN_MAJOR 2
23 #define DRV_MODULE_GEN_MINOR 1
24 #define DRV_MODULE_GEN_SUBMINOR 0
26 #define DRV_MODULE_NAME "ena"
28 #define DEVICE_NAME "Elastic Network Adapter (ENA)"
30 /* 1 for AENQ + ADMIN */
31 #define ENA_ADMIN_MSIX_VEC 1
32 #define ENA_MAX_MSIX_VEC(io_queues) (ENA_ADMIN_MSIX_VEC + (io_queues))
34 /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
36 * Since the max packet size the ENA handles is ~9kB limit the buffer length to
39 #if PAGE_SIZE > SZ_16K
40 #define ENA_PAGE_SIZE (_AC(SZ_16K, UL))
42 #define ENA_PAGE_SIZE PAGE_SIZE
45 #define ENA_MIN_MSIX_VEC 2
49 #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
51 #define ENA_DEFAULT_RING_SIZE (1024)
52 #define ENA_MIN_RING_SIZE (256)
54 #define ENA_MIN_NUM_IO_QUEUES (1)
56 #define ENA_TX_WAKEUP_THRESH (MAX_SKB_FRAGS + 2)
57 #define ENA_DEFAULT_RX_COPYBREAK (256 - NET_IP_ALIGN)
59 #define ENA_MIN_MTU 128
61 #define ENA_NAME_MAX_LEN 20
62 #define ENA_IRQNAME_SIZE 40
64 #define ENA_PKT_MAX_BUFS 19
66 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
67 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
69 /* The number of tx packet completions that will be handled each NAPI poll
70 * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
72 #define ENA_TX_POLL_BUDGET_DIVIDER 4
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 /* Number of queues to check for missing queues per timer service */
81 #define ENA_MONITORED_TX_QUEUES 4
82 /* Max timeout packets before device reset */
83 #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
85 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
87 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
88 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
89 (((idx) + (n)) & ((ring_size) - 1))
91 #define ENA_IO_TXQ_IDX(q) (2 * (q))
92 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
93 #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q) ((q) / 2)
94 #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q) (((q) - 1) / 2)
96 #define ENA_MGMNT_IRQ_IDX 0
97 #define ENA_IO_IRQ_FIRST_IDX 1
98 #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q))
100 #define ENA_ADMIN_POLL_DELAY_US 100
102 /* ENA device should send keep alive msg every 1 sec.
103 * We wait for 6 sec just to be on the safe side.
105 #define ENA_DEVICE_KALIVE_TIMEOUT (6 * HZ)
106 #define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
108 #define ENA_MMIO_DISABLE_REG_READ BIT(0)
110 /* The max MTU size is configured to be the ethernet frame size without
111 * the overhead of the ethernet header, which can have a VLAN header, and
112 * a frame check sequence (FCS).
113 * The buffer size we share with the device is defined to be ENA_PAGE_SIZE
116 #define ENA_XDP_MAX_MTU (ENA_PAGE_SIZE - ETH_HLEN - ETH_FCS_LEN - \
117 VLAN_HLEN - XDP_PACKET_HEADROOM - \
118 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
120 #define ENA_IS_XDP_INDEX(adapter, index) (((index) >= (adapter)->xdp_first_ring) && \
121 ((index) < (adapter)->xdp_first_ring + (adapter)->xdp_num_queues))
124 irq_handler_t handler;
128 cpumask_t affinity_hint_mask;
129 char name[ENA_IRQNAME_SIZE];
133 u8 first_interrupt ____cacheline_aligned;
134 u8 interrupts_masked;
135 struct napi_struct napi;
136 struct ena_ring *tx_ring;
137 struct ena_ring *rx_ring;
138 struct ena_ring *xdp_ring;
143 struct ena_tx_buffer {
145 /* num of ena desc for this specific skb
146 * (includes data desc and metadata desc)
149 /* num of buffers used by this skb */
152 /* XDP buffer structure which is used for sending packets in
155 struct xdp_frame *xdpf;
157 /* Indicate if bufs[0] map the linear data of the skb. */
160 /* Used for detect missing tx packets to limit the number of prints */
162 /* Save the last jiffies to detect missing tx packets
164 * sets to non zero value on ena_start_xmit and set to zero on
165 * napi and timer_Service_routine.
167 * while this value is not protected by lock,
168 * a given packet is not expected to be handled by ena_start_xmit
169 * and by napi/timer_service at the same time.
171 unsigned long last_jiffies;
172 struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
173 } ____cacheline_aligned;
175 struct ena_rx_buffer {
179 struct ena_com_buf ena_buf;
180 } ____cacheline_aligned;
182 struct ena_stats_tx {
190 u64 linearize_failed;
197 u64 unmask_interrupt;
198 u64 last_napi_jiffies;
201 struct ena_stats_rx {
204 u64 rx_copybreak_pkt;
224 /* Holds the empty requests for TX/RX
225 * out of order completions
230 struct ena_tx_buffer *tx_buffer_info;
231 struct ena_rx_buffer *rx_buffer_info;
234 /* cache ptr to avoid using the adapter */
236 struct pci_dev *pdev;
237 struct napi_struct *napi;
238 struct net_device *netdev;
239 struct ena_com_dev *ena_dev;
240 struct ena_adapter *adapter;
241 struct ena_com_io_cq *ena_com_io_cq;
242 struct ena_com_io_sq *ena_com_io_sq;
243 struct bpf_prog *xdp_bpf_prog;
244 struct xdp_rxq_info xdp_rxq;
245 spinlock_t xdp_tx_lock; /* synchronize XDP TX/Redirect traffic */
246 /* Used for rx queues only to point to the xdp tx ring, to
247 * which traffic should be redirected from this rx ring.
249 struct ena_ring *xdp_ring;
259 /* The maximum header length the device can handle */
260 u8 tx_max_header_size;
262 bool disable_meta_caching;
263 u16 no_interrupt_event_cnt;
267 /* number of tx/rx_buffer_info's entries */
270 enum ena_admin_placement_policy_type tx_mem_queue_type;
272 struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
273 u32 smoothed_interval;
274 u32 per_napi_packets;
275 u16 non_empty_napi_events;
276 struct u64_stats_sync syncp;
278 struct ena_stats_tx tx_stats;
279 struct ena_stats_rx rx_stats;
282 u8 *push_buf_intermediate_buf;
284 } ____cacheline_aligned;
286 struct ena_stats_dev {
299 ENA_FLAG_DEVICE_RUNNING,
302 ENA_FLAG_MSIX_ENABLED,
303 ENA_FLAG_TRIGGER_RESET,
304 ENA_FLAG_ONGOING_RESET
307 /* adapter specific private data structure */
309 struct ena_com_dev *ena_dev;
310 /* OS defined structs */
311 struct net_device *netdev;
312 struct pci_dev *pdev;
314 /* rx packets that shorter that this len will be copied to the skb
321 u32 max_num_io_queues;
325 u32 missing_tx_completion_threshold;
327 u32 requested_tx_ring_size;
328 u32 requested_rx_ring_size;
330 u32 max_tx_ring_size;
331 u32 max_rx_ring_size;
338 u8 mac_addr[ETH_ALEN];
340 unsigned long keep_alive_timeout;
341 unsigned long missing_tx_completion_to;
343 char name[ENA_NAME_MAX_LEN];
347 struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
348 ____cacheline_aligned_in_smp;
351 struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
352 ____cacheline_aligned_in_smp;
354 struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];
356 struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
359 struct work_struct reset_task;
360 struct timer_list timer_service;
363 bool dev_up_before_reset;
364 bool disable_meta_caching;
365 unsigned long last_keep_alive_jiffies;
367 struct u64_stats_sync syncp;
368 struct ena_stats_dev dev_stats;
369 struct ena_admin_eni_stats eni_stats;
371 /* last queue index that was checked for uncompleted tx packets */
372 u32 last_monitored_tx_qid;
374 enum ena_regs_reset_reason_types reset_reason;
376 struct bpf_prog *xdp_bpf_prog;
381 void ena_set_ethtool_ops(struct net_device *netdev);
383 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
385 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);
387 int ena_update_hw_stats(struct ena_adapter *adapter);
389 int ena_update_queue_sizes(struct ena_adapter *adapter,
393 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count);
395 int ena_get_sset_count(struct net_device *netdev, int sset);
397 static inline void ena_reset_device(struct ena_adapter *adapter,
398 enum ena_regs_reset_reason_types reset_reason)
400 adapter->reset_reason = reset_reason;
401 /* Make sure reset reason is set before triggering the reset */
402 smp_mb__before_atomic();
403 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
406 enum ena_xdp_errors_t {
408 ENA_XDP_CURRENT_MTU_TOO_LARGE,
409 ENA_XDP_NO_ENOUGH_QUEUES,
412 static inline bool ena_xdp_present(struct ena_adapter *adapter)
414 return !!adapter->xdp_bpf_prog;
417 static inline bool ena_xdp_present_ring(struct ena_ring *ring)
419 return !!ring->xdp_bpf_prog;
422 static inline bool ena_xdp_legal_queue_count(struct ena_adapter *adapter,
425 return 2 * queues <= adapter->max_num_io_queues;
428 static inline enum ena_xdp_errors_t ena_xdp_allowed(struct ena_adapter *adapter)
430 enum ena_xdp_errors_t rc = ENA_XDP_ALLOWED;
432 if (adapter->netdev->mtu > ENA_XDP_MAX_MTU)
433 rc = ENA_XDP_CURRENT_MTU_TOO_LARGE;
434 else if (!ena_xdp_legal_queue_count(adapter, adapter->num_io_queues))
435 rc = ENA_XDP_NO_ENOUGH_QUEUES;
440 #endif /* !(ENA_H) */