Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
0002 /*
0003  * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
0004  */
0005 
0006 #ifndef ENA_H
0007 #define ENA_H
0008 
0009 #include <linux/bitops.h>
0010 #include <linux/dim.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/if_vlan.h>
0013 #include <linux/inetdevice.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/netdevice.h>
0016 #include <linux/skbuff.h>
0017 #include <uapi/linux/bpf.h>
0018 
0019 #include "ena_com.h"
0020 #include "ena_eth_com.h"
0021 
0022 #define DRV_MODULE_GEN_MAJOR    2
0023 #define DRV_MODULE_GEN_MINOR    1
0024 #define DRV_MODULE_GEN_SUBMINOR 0
0025 
0026 #define DRV_MODULE_NAME     "ena"
0027 
0028 #define DEVICE_NAME "Elastic Network Adapter (ENA)"
0029 
0030 /* 1 for AENQ + ADMIN */
0031 #define ENA_ADMIN_MSIX_VEC      1
0032 #define ENA_MAX_MSIX_VEC(io_queues) (ENA_ADMIN_MSIX_VEC + (io_queues))
0033 
0034 /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
0035  * driver passes 0.
0036  * Since the max packet size the ENA handles is ~9kB limit the buffer length to
0037  * 16kB.
0038  */
0039 #if PAGE_SIZE > SZ_16K
0040 #define ENA_PAGE_SIZE (_AC(SZ_16K, UL))
0041 #else
0042 #define ENA_PAGE_SIZE PAGE_SIZE
0043 #endif
0044 
0045 #define ENA_MIN_MSIX_VEC        2
0046 
0047 #define ENA_REG_BAR         0
0048 #define ENA_MEM_BAR         2
0049 #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
0050 
0051 #define ENA_DEFAULT_RING_SIZE   (1024)
0052 #define ENA_MIN_RING_SIZE   (256)
0053 
0054 #define ENA_MIN_NUM_IO_QUEUES   (1)
0055 
0056 #define ENA_TX_WAKEUP_THRESH        (MAX_SKB_FRAGS + 2)
0057 #define ENA_DEFAULT_RX_COPYBREAK    (256 - NET_IP_ALIGN)
0058 
0059 #define ENA_MIN_MTU     128
0060 
0061 #define ENA_NAME_MAX_LEN    20
0062 #define ENA_IRQNAME_SIZE    40
0063 
0064 #define ENA_PKT_MAX_BUFS    19
0065 
0066 #define ENA_RX_RSS_TABLE_LOG_SIZE  7
0067 #define ENA_RX_RSS_TABLE_SIZE   (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
0068 
0069 /* The number of tx packet completions that will be handled each NAPI poll
0070  * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
0071  */
0072 #define ENA_TX_POLL_BUDGET_DIVIDER  4
0073 
0074 /* Refill Rx queue when number of required descriptors is above
0075  * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
0076  */
0077 #define ENA_RX_REFILL_THRESH_DIVIDER    8
0078 #define ENA_RX_REFILL_THRESH_PACKET 256
0079 
0080 /* Number of queues to check for missing queues per timer service */
0081 #define ENA_MONITORED_TX_QUEUES 4
0082 /* Max timeout packets before device reset */
0083 #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
0084 
0085 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
0086 
0087 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
0088 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
0089     (((idx) + (n)) & ((ring_size) - 1))
0090 
0091 #define ENA_IO_TXQ_IDX(q)   (2 * (q))
0092 #define ENA_IO_RXQ_IDX(q)   (2 * (q) + 1)
0093 #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q)   ((q) / 2)
0094 #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q)   (((q) - 1) / 2)
0095 
0096 #define ENA_MGMNT_IRQ_IDX       0
0097 #define ENA_IO_IRQ_FIRST_IDX        1
0098 #define ENA_IO_IRQ_IDX(q)       (ENA_IO_IRQ_FIRST_IDX + (q))
0099 
0100 #define ENA_ADMIN_POLL_DELAY_US 100
0101 
0102 /* ENA device should send keep alive msg every 1 sec.
0103  * We wait for 6 sec just to be on the safe side.
0104  */
0105 #define ENA_DEVICE_KALIVE_TIMEOUT   (6 * HZ)
0106 #define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
0107 
0108 #define ENA_MMIO_DISABLE_REG_READ   BIT(0)
0109 
0110 /* The max MTU size is configured to be the ethernet frame size without
0111  * the overhead of the ethernet header, which can have a VLAN header, and
0112  * a frame check sequence (FCS).
0113  * The buffer size we share with the device is defined to be ENA_PAGE_SIZE
0114  */
0115 
0116 #define ENA_XDP_MAX_MTU (ENA_PAGE_SIZE - ETH_HLEN - ETH_FCS_LEN -   \
0117              VLAN_HLEN - XDP_PACKET_HEADROOM -      \
0118              SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
0119 
0120 #define ENA_IS_XDP_INDEX(adapter, index) (((index) >= (adapter)->xdp_first_ring) && \
0121     ((index) < (adapter)->xdp_first_ring + (adapter)->xdp_num_queues))
0122 
0123 struct ena_irq {
0124     irq_handler_t handler;
0125     void *data;
0126     int cpu;
0127     u32 vector;
0128     cpumask_t affinity_hint_mask;
0129     char name[ENA_IRQNAME_SIZE];
0130 };
0131 
0132 struct ena_napi {
0133     u8 first_interrupt ____cacheline_aligned;
0134     u8 interrupts_masked;
0135     struct napi_struct napi;
0136     struct ena_ring *tx_ring;
0137     struct ena_ring *rx_ring;
0138     struct ena_ring *xdp_ring;
0139     u32 qid;
0140     struct dim dim;
0141 };
0142 
0143 struct ena_tx_buffer {
0144     struct sk_buff *skb;
0145     /* num of ena desc for this specific skb
0146      * (includes data desc and metadata desc)
0147      */
0148     u32 tx_descs;
0149     /* num of buffers used by this skb */
0150     u32 num_of_bufs;
0151 
0152     /* XDP buffer structure which is used for sending packets in
0153      * the xdp queues
0154      */
0155     struct xdp_frame *xdpf;
0156 
0157     /* Indicate if bufs[0] map the linear data of the skb. */
0158     u8 map_linear_data;
0159 
0160     /* Used for detect missing tx packets to limit the number of prints */
0161     u32 print_once;
0162     /* Save the last jiffies to detect missing tx packets
0163      *
0164      * sets to non zero value on ena_start_xmit and set to zero on
0165      * napi and timer_Service_routine.
0166      *
0167      * while this value is not protected by lock,
0168      * a given packet is not expected to be handled by ena_start_xmit
0169      * and by napi/timer_service at the same time.
0170      */
0171     unsigned long last_jiffies;
0172     struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
0173 } ____cacheline_aligned;
0174 
0175 struct ena_rx_buffer {
0176     struct sk_buff *skb;
0177     struct page *page;
0178     u32 page_offset;
0179     struct ena_com_buf ena_buf;
0180 } ____cacheline_aligned;
0181 
0182 struct ena_stats_tx {
0183     u64 cnt;
0184     u64 bytes;
0185     u64 queue_stop;
0186     u64 prepare_ctx_err;
0187     u64 queue_wakeup;
0188     u64 dma_mapping_err;
0189     u64 linearize;
0190     u64 linearize_failed;
0191     u64 napi_comp;
0192     u64 tx_poll;
0193     u64 doorbells;
0194     u64 bad_req_id;
0195     u64 llq_buffer_copy;
0196     u64 missed_tx;
0197     u64 unmask_interrupt;
0198     u64 last_napi_jiffies;
0199 };
0200 
0201 struct ena_stats_rx {
0202     u64 cnt;
0203     u64 bytes;
0204     u64 rx_copybreak_pkt;
0205     u64 csum_good;
0206     u64 refil_partial;
0207     u64 csum_bad;
0208     u64 page_alloc_fail;
0209     u64 skb_alloc_fail;
0210     u64 dma_mapping_err;
0211     u64 bad_desc_num;
0212     u64 bad_req_id;
0213     u64 empty_rx_ring;
0214     u64 csum_unchecked;
0215     u64 xdp_aborted;
0216     u64 xdp_drop;
0217     u64 xdp_pass;
0218     u64 xdp_tx;
0219     u64 xdp_invalid;
0220     u64 xdp_redirect;
0221 };
0222 
0223 struct ena_ring {
0224     /* Holds the empty requests for TX/RX
0225      * out of order completions
0226      */
0227     u16 *free_ids;
0228 
0229     union {
0230         struct ena_tx_buffer *tx_buffer_info;
0231         struct ena_rx_buffer *rx_buffer_info;
0232     };
0233 
0234     /* cache ptr to avoid using the adapter */
0235     struct device *dev;
0236     struct pci_dev *pdev;
0237     struct napi_struct *napi;
0238     struct net_device *netdev;
0239     struct ena_com_dev *ena_dev;
0240     struct ena_adapter *adapter;
0241     struct ena_com_io_cq *ena_com_io_cq;
0242     struct ena_com_io_sq *ena_com_io_sq;
0243     struct bpf_prog *xdp_bpf_prog;
0244     struct xdp_rxq_info xdp_rxq;
0245     spinlock_t xdp_tx_lock; /* synchronize XDP TX/Redirect traffic */
0246     /* Used for rx queues only to point to the xdp tx ring, to
0247      * which traffic should be redirected from this rx ring.
0248      */
0249     struct ena_ring *xdp_ring;
0250 
0251     u16 next_to_use;
0252     u16 next_to_clean;
0253     u16 rx_copybreak;
0254     u16 rx_headroom;
0255     u16 qid;
0256     u16 mtu;
0257     u16 sgl_size;
0258 
0259     /* The maximum header length the device can handle */
0260     u8 tx_max_header_size;
0261 
0262     bool disable_meta_caching;
0263     u16 no_interrupt_event_cnt;
0264 
0265     /* cpu for TPH */
0266     int cpu;
0267      /* number of tx/rx_buffer_info's entries */
0268     int ring_size;
0269 
0270     enum ena_admin_placement_policy_type tx_mem_queue_type;
0271 
0272     struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
0273     u32  smoothed_interval;
0274     u32  per_napi_packets;
0275     u16 non_empty_napi_events;
0276     struct u64_stats_sync syncp;
0277     union {
0278         struct ena_stats_tx tx_stats;
0279         struct ena_stats_rx rx_stats;
0280     };
0281 
0282     u8 *push_buf_intermediate_buf;
0283     int empty_rx_queue;
0284 } ____cacheline_aligned;
0285 
0286 struct ena_stats_dev {
0287     u64 tx_timeout;
0288     u64 suspend;
0289     u64 resume;
0290     u64 wd_expired;
0291     u64 interface_up;
0292     u64 interface_down;
0293     u64 admin_q_pause;
0294     u64 rx_drops;
0295     u64 tx_drops;
0296 };
0297 
0298 enum ena_flags_t {
0299     ENA_FLAG_DEVICE_RUNNING,
0300     ENA_FLAG_DEV_UP,
0301     ENA_FLAG_LINK_UP,
0302     ENA_FLAG_MSIX_ENABLED,
0303     ENA_FLAG_TRIGGER_RESET,
0304     ENA_FLAG_ONGOING_RESET
0305 };
0306 
0307 /* adapter specific private data structure */
0308 struct ena_adapter {
0309     struct ena_com_dev *ena_dev;
0310     /* OS defined structs */
0311     struct net_device *netdev;
0312     struct pci_dev *pdev;
0313 
0314     /* rx packets that shorter that this len will be copied to the skb
0315      * header
0316      */
0317     u32 rx_copybreak;
0318     u32 max_mtu;
0319 
0320     u32 num_io_queues;
0321     u32 max_num_io_queues;
0322 
0323     int msix_vecs;
0324 
0325     u32 missing_tx_completion_threshold;
0326 
0327     u32 requested_tx_ring_size;
0328     u32 requested_rx_ring_size;
0329 
0330     u32 max_tx_ring_size;
0331     u32 max_rx_ring_size;
0332 
0333     u32 msg_enable;
0334 
0335     u16 max_tx_sgl_size;
0336     u16 max_rx_sgl_size;
0337 
0338     u8 mac_addr[ETH_ALEN];
0339 
0340     unsigned long keep_alive_timeout;
0341     unsigned long missing_tx_completion_to;
0342 
0343     char name[ENA_NAME_MAX_LEN];
0344 
0345     unsigned long flags;
0346     /* TX */
0347     struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
0348         ____cacheline_aligned_in_smp;
0349 
0350     /* RX */
0351     struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
0352         ____cacheline_aligned_in_smp;
0353 
0354     struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];
0355 
0356     struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
0357 
0358     /* timer service */
0359     struct work_struct reset_task;
0360     struct timer_list timer_service;
0361 
0362     bool wd_state;
0363     bool dev_up_before_reset;
0364     bool disable_meta_caching;
0365     unsigned long last_keep_alive_jiffies;
0366 
0367     struct u64_stats_sync syncp;
0368     struct ena_stats_dev dev_stats;
0369     struct ena_admin_eni_stats eni_stats;
0370 
0371     /* last queue index that was checked for uncompleted tx packets */
0372     u32 last_monitored_tx_qid;
0373 
0374     enum ena_regs_reset_reason_types reset_reason;
0375 
0376     struct bpf_prog *xdp_bpf_prog;
0377     u32 xdp_first_ring;
0378     u32 xdp_num_queues;
0379 };
0380 
0381 void ena_set_ethtool_ops(struct net_device *netdev);
0382 
0383 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
0384 
0385 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);
0386 
0387 int ena_update_hw_stats(struct ena_adapter *adapter);
0388 
0389 int ena_update_queue_sizes(struct ena_adapter *adapter,
0390                u32 new_tx_size,
0391                u32 new_rx_size);
0392 
0393 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count);
0394 
0395 int ena_get_sset_count(struct net_device *netdev, int sset);
0396 
0397 static inline void ena_reset_device(struct ena_adapter *adapter,
0398                     enum ena_regs_reset_reason_types reset_reason)
0399 {
0400     adapter->reset_reason = reset_reason;
0401     /* Make sure reset reason is set before triggering the reset */
0402     smp_mb__before_atomic();
0403     set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
0404 }
0405 
0406 enum ena_xdp_errors_t {
0407     ENA_XDP_ALLOWED = 0,
0408     ENA_XDP_CURRENT_MTU_TOO_LARGE,
0409     ENA_XDP_NO_ENOUGH_QUEUES,
0410 };
0411 
0412 static inline bool ena_xdp_present(struct ena_adapter *adapter)
0413 {
0414     return !!adapter->xdp_bpf_prog;
0415 }
0416 
0417 static inline bool ena_xdp_present_ring(struct ena_ring *ring)
0418 {
0419     return !!ring->xdp_bpf_prog;
0420 }
0421 
0422 static inline bool ena_xdp_legal_queue_count(struct ena_adapter *adapter,
0423                          u32 queues)
0424 {
0425     return 2 * queues <= adapter->max_num_io_queues;
0426 }
0427 
0428 static inline enum ena_xdp_errors_t ena_xdp_allowed(struct ena_adapter *adapter)
0429 {
0430     enum ena_xdp_errors_t rc = ENA_XDP_ALLOWED;
0431 
0432     if (adapter->netdev->mtu > ENA_XDP_MAX_MTU)
0433         rc = ENA_XDP_CURRENT_MTU_TOO_LARGE;
0434     else if (!ena_xdp_legal_queue_count(adapter, adapter->num_io_queues))
0435         rc = ENA_XDP_NO_ENOUGH_QUEUES;
0436 
0437     return rc;
0438 }
0439 
0440 #endif /* !(ENA_H) */