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>
18 #include <uapi/linux/bpf.h>
21 #include "ena_eth_com.h"
23 #define DRV_MODULE_GEN_MAJOR 2
24 #define DRV_MODULE_GEN_MINOR 1
25 #define DRV_MODULE_GEN_SUBMINOR 0
27 #define DRV_MODULE_NAME "ena"
29 #define DEVICE_NAME "Elastic Network Adapter (ENA)"
31 /* 1 for AENQ + ADMIN */
32 #define ENA_ADMIN_MSIX_VEC 1
33 #define ENA_MAX_MSIX_VEC(io_queues) (ENA_ADMIN_MSIX_VEC + (io_queues))
35 /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
37 * Since the max packet size the ENA handles is ~9kB limit the buffer length to
40 #if PAGE_SIZE > SZ_16K
41 #define ENA_PAGE_SIZE (_AC(SZ_16K, UL))
43 #define ENA_PAGE_SIZE PAGE_SIZE
46 #define ENA_MIN_MSIX_VEC 2
50 #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
52 #define ENA_DEFAULT_RING_SIZE (1024)
53 #define ENA_MIN_RING_SIZE (256)
55 #define ENA_MIN_RX_BUF_SIZE (2048)
57 #define ENA_MIN_NUM_IO_QUEUES (1)
59 #define ENA_TX_WAKEUP_THRESH (MAX_SKB_FRAGS + 2)
60 #define ENA_DEFAULT_RX_COPYBREAK (256 - NET_IP_ALIGN)
62 #define ENA_MIN_MTU 128
64 #define ENA_NAME_MAX_LEN 20
65 #define ENA_IRQNAME_SIZE 40
67 #define ENA_PKT_MAX_BUFS 19
69 #define ENA_RX_RSS_TABLE_LOG_SIZE 7
70 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
72 /* The number of tx packet completions that will be handled each NAPI poll
73 * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
75 #define ENA_TX_POLL_BUDGET_DIVIDER 4
77 /* Refill Rx queue when number of required descriptors is above
78 * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
80 #define ENA_RX_REFILL_THRESH_DIVIDER 8
81 #define ENA_RX_REFILL_THRESH_PACKET 256
83 /* Number of queues to check for missing queues per timer service */
84 #define ENA_MONITORED_TX_QUEUES 4
85 /* Max timeout packets before device reset */
86 #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
88 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
90 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
91 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
92 (((idx) + (n)) & ((ring_size) - 1))
94 #define ENA_IO_TXQ_IDX(q) (2 * (q))
95 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
96 #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q) ((q) / 2)
97 #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q) (((q) - 1) / 2)
99 #define ENA_MGMNT_IRQ_IDX 0
100 #define ENA_IO_IRQ_FIRST_IDX 1
101 #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q))
103 #define ENA_ADMIN_POLL_DELAY_US 100
105 /* ENA device should send keep alive msg every 1 sec.
106 * We wait for 6 sec just to be on the safe side.
108 #define ENA_DEVICE_KALIVE_TIMEOUT (6 * HZ)
109 #define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
111 #define ENA_MMIO_DISABLE_REG_READ BIT(0)
114 irq_handler_t handler;
118 cpumask_t affinity_hint_mask;
119 char name[ENA_IRQNAME_SIZE];
123 u8 first_interrupt ____cacheline_aligned;
124 u8 interrupts_masked;
125 struct napi_struct napi;
126 struct ena_ring *tx_ring;
127 struct ena_ring *rx_ring;
132 struct ena_tx_buffer {
135 /* XDP buffer structure which is used for sending packets in
138 struct xdp_frame *xdpf;
140 /* num of ena desc for this specific skb
141 * (includes data desc and metadata desc)
144 /* num of buffers used by this skb */
147 /* Total size of all buffers in bytes */
150 /* Indicate if bufs[0] map the linear data of the 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 {
174 struct ena_com_buf ena_buf;
175 } ____cacheline_aligned;
177 struct ena_stats_tx {
185 u64 linearize_failed;
192 u64 unmask_interrupt;
193 u64 last_napi_jiffies;
196 struct ena_stats_rx {
199 u64 rx_copybreak_pkt;
219 /* Holds the empty requests for TX/RX
220 * out of order completions
225 struct ena_tx_buffer *tx_buffer_info;
226 struct ena_rx_buffer *rx_buffer_info;
229 /* cache ptr to avoid using the adapter */
231 struct pci_dev *pdev;
232 struct napi_struct *napi;
233 struct net_device *netdev;
234 struct ena_com_dev *ena_dev;
235 struct ena_adapter *adapter;
236 struct ena_com_io_cq *ena_com_io_cq;
237 struct ena_com_io_sq *ena_com_io_sq;
238 struct bpf_prog *xdp_bpf_prog;
239 struct xdp_rxq_info xdp_rxq;
240 spinlock_t xdp_tx_lock; /* synchronize XDP TX/Redirect traffic */
241 /* Used for rx queues only to point to the xdp tx ring, to
242 * which traffic should be redirected from this rx ring.
244 struct ena_ring *xdp_ring;
254 /* The maximum header length the device can handle */
255 u8 tx_max_header_size;
257 bool disable_meta_caching;
258 u16 no_interrupt_event_cnt;
260 /* cpu and NUMA for TPH */
264 /* number of tx/rx_buffer_info's entries */
267 enum ena_admin_placement_policy_type tx_mem_queue_type;
269 struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
270 u32 smoothed_interval;
271 u32 per_napi_packets;
272 u16 non_empty_napi_events;
273 struct u64_stats_sync syncp;
275 struct ena_stats_tx tx_stats;
276 struct ena_stats_rx rx_stats;
279 u8 *push_buf_intermediate_buf;
281 } ____cacheline_aligned;
283 struct ena_stats_dev {
297 ENA_FLAG_DEVICE_RUNNING,
300 ENA_FLAG_MSIX_ENABLED,
301 ENA_FLAG_TRIGGER_RESET,
302 ENA_FLAG_ONGOING_RESET
305 /* adapter specific private data structure */
307 struct ena_com_dev *ena_dev;
308 /* OS defined structs */
309 struct net_device *netdev;
310 struct pci_dev *pdev;
312 /* rx packets that shorter that this len will be copied to the skb
319 u32 max_num_io_queues;
323 u32 missing_tx_completion_threshold;
325 u32 requested_tx_ring_size;
326 u32 requested_rx_ring_size;
328 u32 max_tx_ring_size;
329 u32 max_rx_ring_size;
333 /* large_llq_header_enabled is used for two purposes:
334 * 1. Indicates that large LLQ has been requested.
335 * 2. Indicates whether large LLQ is set or not after device
336 * initialization / configuration.
338 bool large_llq_header_enabled;
339 bool large_llq_header_supported;
344 u8 mac_addr[ETH_ALEN];
346 unsigned long keep_alive_timeout;
347 unsigned long missing_tx_completion_to;
349 char name[ENA_NAME_MAX_LEN];
353 struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
354 ____cacheline_aligned_in_smp;
357 struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
358 ____cacheline_aligned_in_smp;
360 struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];
362 struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
365 struct work_struct reset_task;
366 struct timer_list timer_service;
369 bool dev_up_before_reset;
370 bool disable_meta_caching;
371 unsigned long last_keep_alive_jiffies;
373 struct u64_stats_sync syncp;
374 struct ena_stats_dev dev_stats;
375 struct ena_admin_eni_stats eni_stats;
376 struct ena_admin_ena_srd_info ena_srd_info;
378 /* last queue index that was checked for uncompleted tx packets */
379 u32 last_monitored_tx_qid;
381 enum ena_regs_reset_reason_types reset_reason;
383 struct bpf_prog *xdp_bpf_prog;
388 void ena_set_ethtool_ops(struct net_device *netdev);
390 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
392 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);
395 int ena_update_queue_params(struct ena_adapter *adapter,
398 u32 new_llq_header_len);
400 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count);
402 int ena_set_rx_copybreak(struct ena_adapter *adapter, u32 rx_copybreak);
404 int ena_get_sset_count(struct net_device *netdev, int sset);
406 static inline void ena_reset_device(struct ena_adapter *adapter,
407 enum ena_regs_reset_reason_types reset_reason)
409 adapter->reset_reason = reset_reason;
410 /* Make sure reset reason is set before triggering the reset */
411 smp_mb__before_atomic();
412 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
415 int handle_invalid_req_id(struct ena_ring *ring, u16 req_id,
416 struct ena_tx_buffer *tx_info, bool is_xdp);
418 /* Increase a stat by cnt while holding syncp seqlock on 32bit machines */
419 static inline void ena_increase_stat(u64 *statp, u64 cnt,
420 struct u64_stats_sync *syncp)
422 u64_stats_update_begin(syncp);
424 u64_stats_update_end(syncp);
427 static inline void ena_ring_tx_doorbell(struct ena_ring *tx_ring)
429 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
430 ena_increase_stat(&tx_ring->tx_stats.doorbells, 1, &tx_ring->syncp);
433 int ena_xmit_common(struct ena_adapter *adapter,
434 struct ena_ring *ring,
435 struct ena_tx_buffer *tx_info,
436 struct ena_com_tx_ctx *ena_tx_ctx,
439 void ena_unmap_tx_buff(struct ena_ring *tx_ring,
440 struct ena_tx_buffer *tx_info);
441 void ena_init_io_rings(struct ena_adapter *adapter,
442 int first_index, int count);
443 int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter,
444 int first_index, int count);
445 int ena_setup_tx_resources_in_range(struct ena_adapter *adapter,
446 int first_index, int count);
447 void ena_free_all_io_tx_resources_in_range(struct ena_adapter *adapter,
448 int first_index, int count);
449 void ena_free_all_io_tx_resources(struct ena_adapter *adapter);
450 void ena_down(struct ena_adapter *adapter);
451 int ena_up(struct ena_adapter *adapter);
452 void ena_unmask_interrupt(struct ena_ring *tx_ring, struct ena_ring *rx_ring);
453 void ena_update_ring_numa_node(struct ena_ring *tx_ring,
454 struct ena_ring *rx_ring);
455 #endif /* !(ENA_H) */