Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /****************************************************************************
0003  * Driver for Solarflare network controllers and boards
0004  * Copyright 2005-2006 Fen Systems Ltd.
0005  * Copyright 2005-2013 Solarflare Communications Inc.
0006  */
0007 
0008 /* Common definitions for all Efx net driver code */
0009 
0010 #ifndef EF4_NET_DRIVER_H
0011 #define EF4_NET_DRIVER_H
0012 
0013 #include <linux/netdevice.h>
0014 #include <linux/etherdevice.h>
0015 #include <linux/ethtool.h>
0016 #include <linux/if_vlan.h>
0017 #include <linux/timer.h>
0018 #include <linux/mdio.h>
0019 #include <linux/list.h>
0020 #include <linux/pci.h>
0021 #include <linux/device.h>
0022 #include <linux/highmem.h>
0023 #include <linux/workqueue.h>
0024 #include <linux/mutex.h>
0025 #include <linux/rwsem.h>
0026 #include <linux/vmalloc.h>
0027 #include <linux/i2c.h>
0028 #include <linux/mtd/mtd.h>
0029 #include <net/busy_poll.h>
0030 
0031 #include "enum.h"
0032 #include "bitfield.h"
0033 #include "filter.h"
0034 
0035 /**************************************************************************
0036  *
0037  * Build definitions
0038  *
0039  **************************************************************************/
0040 
0041 #define EF4_DRIVER_VERSION  "4.1"
0042 
0043 #ifdef DEBUG
0044 #define EF4_BUG_ON_PARANOID(x) BUG_ON(x)
0045 #define EF4_WARN_ON_PARANOID(x) WARN_ON(x)
0046 #else
0047 #define EF4_BUG_ON_PARANOID(x) do {} while (0)
0048 #define EF4_WARN_ON_PARANOID(x) do {} while (0)
0049 #endif
0050 
0051 /**************************************************************************
0052  *
0053  * Efx data structures
0054  *
0055  **************************************************************************/
0056 
0057 #define EF4_MAX_CHANNELS 32U
0058 #define EF4_MAX_RX_QUEUES EF4_MAX_CHANNELS
0059 #define EF4_EXTRA_CHANNEL_IOV   0
0060 #define EF4_EXTRA_CHANNEL_PTP   1
0061 #define EF4_MAX_EXTRA_CHANNELS  2U
0062 
0063 /* Checksum generation is a per-queue option in hardware, so each
0064  * queue visible to the networking core is backed by two hardware TX
0065  * queues. */
0066 #define EF4_MAX_TX_TC       2
0067 #define EF4_MAX_CORE_TX_QUEUES  (EF4_MAX_TX_TC * EF4_MAX_CHANNELS)
0068 #define EF4_TXQ_TYPE_OFFLOAD    1   /* flag */
0069 #define EF4_TXQ_TYPE_HIGHPRI    2   /* flag */
0070 #define EF4_TXQ_TYPES       4
0071 #define EF4_MAX_TX_QUEUES   (EF4_TXQ_TYPES * EF4_MAX_CHANNELS)
0072 
0073 /* Maximum possible MTU the driver supports */
0074 #define EF4_MAX_MTU (9 * 1024)
0075 
0076 /* Minimum MTU, from RFC791 (IP) */
0077 #define EF4_MIN_MTU 68
0078 
0079 /* Size of an RX scatter buffer.  Small enough to pack 2 into a 4K page,
0080  * and should be a multiple of the cache line size.
0081  */
0082 #define EF4_RX_USR_BUF_SIZE (2048 - 256)
0083 
0084 /* If possible, we should ensure cache line alignment at start and end
0085  * of every buffer.  Otherwise, we just need to ensure 4-byte
0086  * alignment of the network header.
0087  */
0088 #if NET_IP_ALIGN == 0
0089 #define EF4_RX_BUF_ALIGNMENT    L1_CACHE_BYTES
0090 #else
0091 #define EF4_RX_BUF_ALIGNMENT    4
0092 #endif
0093 
0094 struct ef4_self_tests;
0095 
0096 /**
0097  * struct ef4_buffer - A general-purpose DMA buffer
0098  * @addr: host base address of the buffer
0099  * @dma_addr: DMA base address of the buffer
0100  * @len: Buffer length, in bytes
0101  *
0102  * The NIC uses these buffers for its interrupt status registers and
0103  * MAC stats dumps.
0104  */
0105 struct ef4_buffer {
0106     void *addr;
0107     dma_addr_t dma_addr;
0108     unsigned int len;
0109 };
0110 
0111 /**
0112  * struct ef4_special_buffer - DMA buffer entered into buffer table
0113  * @buf: Standard &struct ef4_buffer
0114  * @index: Buffer index within controller;s buffer table
0115  * @entries: Number of buffer table entries
0116  *
0117  * The NIC has a buffer table that maps buffers of size %EF4_BUF_SIZE.
0118  * Event and descriptor rings are addressed via one or more buffer
0119  * table entries (and so can be physically non-contiguous, although we
0120  * currently do not take advantage of that).  On Falcon and Siena we
0121  * have to take care of allocating and initialising the entries
0122  * ourselves.  On later hardware this is managed by the firmware and
0123  * @index and @entries are left as 0.
0124  */
0125 struct ef4_special_buffer {
0126     struct ef4_buffer buf;
0127     unsigned int index;
0128     unsigned int entries;
0129 };
0130 
0131 /**
0132  * struct ef4_tx_buffer - buffer state for a TX descriptor
0133  * @skb: When @flags & %EF4_TX_BUF_SKB, the associated socket buffer to be
0134  *  freed when descriptor completes
0135  * @option: When @flags & %EF4_TX_BUF_OPTION, a NIC-specific option descriptor.
0136  * @dma_addr: DMA address of the fragment.
0137  * @flags: Flags for allocation and DMA mapping type
0138  * @len: Length of this fragment.
0139  *  This field is zero when the queue slot is empty.
0140  * @unmap_len: Length of this fragment to unmap
0141  * @dma_offset: Offset of @dma_addr from the address of the backing DMA mapping.
0142  * Only valid if @unmap_len != 0.
0143  */
0144 struct ef4_tx_buffer {
0145     const struct sk_buff *skb;
0146     union {
0147         ef4_qword_t option;
0148         dma_addr_t dma_addr;
0149     };
0150     unsigned short flags;
0151     unsigned short len;
0152     unsigned short unmap_len;
0153     unsigned short dma_offset;
0154 };
0155 #define EF4_TX_BUF_CONT     1   /* not last descriptor of packet */
0156 #define EF4_TX_BUF_SKB      2   /* buffer is last part of skb */
0157 #define EF4_TX_BUF_MAP_SINGLE   8   /* buffer was mapped with dma_map_single() */
0158 #define EF4_TX_BUF_OPTION   0x10    /* empty buffer for option descriptor */
0159 
0160 /**
0161  * struct ef4_tx_queue - An Efx TX queue
0162  *
0163  * This is a ring buffer of TX fragments.
0164  * Since the TX completion path always executes on the same
0165  * CPU and the xmit path can operate on different CPUs,
0166  * performance is increased by ensuring that the completion
0167  * path and the xmit path operate on different cache lines.
0168  * This is particularly important if the xmit path is always
0169  * executing on one CPU which is different from the completion
0170  * path.  There is also a cache line for members which are
0171  * read but not written on the fast path.
0172  *
0173  * @efx: The associated Efx NIC
0174  * @queue: DMA queue number
0175  * @channel: The associated channel
0176  * @core_txq: The networking core TX queue structure
0177  * @buffer: The software buffer ring
0178  * @cb_page: Array of pages of copy buffers.  Carved up according to
0179  *  %EF4_TX_CB_ORDER into %EF4_TX_CB_SIZE-sized chunks.
0180  * @txd: The hardware descriptor ring
0181  * @ptr_mask: The size of the ring minus 1.
0182  * @initialised: Has hardware queue been initialised?
0183  * @tx_min_size: Minimum transmit size for this queue. Depends on HW.
0184  * @read_count: Current read pointer.
0185  *  This is the number of buffers that have been removed from both rings.
0186  * @old_write_count: The value of @write_count when last checked.
0187  *  This is here for performance reasons.  The xmit path will
0188  *  only get the up-to-date value of @write_count if this
0189  *  variable indicates that the queue is empty.  This is to
0190  *  avoid cache-line ping-pong between the xmit path and the
0191  *  completion path.
0192  * @merge_events: Number of TX merged completion events
0193  * @insert_count: Current insert pointer
0194  *  This is the number of buffers that have been added to the
0195  *  software ring.
0196  * @write_count: Current write pointer
0197  *  This is the number of buffers that have been added to the
0198  *  hardware ring.
0199  * @old_read_count: The value of read_count when last checked.
0200  *  This is here for performance reasons.  The xmit path will
0201  *  only get the up-to-date value of read_count if this
0202  *  variable indicates that the queue is full.  This is to
0203  *  avoid cache-line ping-pong between the xmit path and the
0204  *  completion path.
0205  * @pushes: Number of times the TX push feature has been used
0206  * @xmit_more_available: Are any packets waiting to be pushed to the NIC
0207  * @cb_packets: Number of times the TX copybreak feature has been used
0208  * @empty_read_count: If the completion path has seen the queue as empty
0209  *  and the transmission path has not yet checked this, the value of
0210  *  @read_count bitwise-added to %EF4_EMPTY_COUNT_VALID; otherwise 0.
0211  */
0212 struct ef4_tx_queue {
0213     /* Members which don't change on the fast path */
0214     struct ef4_nic *efx ____cacheline_aligned_in_smp;
0215     unsigned queue;
0216     struct ef4_channel *channel;
0217     struct netdev_queue *core_txq;
0218     struct ef4_tx_buffer *buffer;
0219     struct ef4_buffer *cb_page;
0220     struct ef4_special_buffer txd;
0221     unsigned int ptr_mask;
0222     bool initialised;
0223     unsigned int tx_min_size;
0224 
0225     /* Function pointers used in the fast path. */
0226     int (*handle_tso)(struct ef4_tx_queue*, struct sk_buff*, bool *);
0227 
0228     /* Members used mainly on the completion path */
0229     unsigned int read_count ____cacheline_aligned_in_smp;
0230     unsigned int old_write_count;
0231     unsigned int merge_events;
0232     unsigned int bytes_compl;
0233     unsigned int pkts_compl;
0234 
0235     /* Members used only on the xmit path */
0236     unsigned int insert_count ____cacheline_aligned_in_smp;
0237     unsigned int write_count;
0238     unsigned int old_read_count;
0239     unsigned int pushes;
0240     bool xmit_more_available;
0241     unsigned int cb_packets;
0242     /* Statistics to supplement MAC stats */
0243     unsigned long tx_packets;
0244 
0245     /* Members shared between paths and sometimes updated */
0246     unsigned int empty_read_count ____cacheline_aligned_in_smp;
0247 #define EF4_EMPTY_COUNT_VALID 0x80000000
0248     atomic_t flush_outstanding;
0249 };
0250 
0251 #define EF4_TX_CB_ORDER 7
0252 #define EF4_TX_CB_SIZE  (1 << EF4_TX_CB_ORDER) - NET_IP_ALIGN
0253 
0254 /**
0255  * struct ef4_rx_buffer - An Efx RX data buffer
0256  * @dma_addr: DMA base address of the buffer
0257  * @page: The associated page buffer.
0258  *  Will be %NULL if the buffer slot is currently free.
0259  * @page_offset: If pending: offset in @page of DMA base address.
0260  *  If completed: offset in @page of Ethernet header.
0261  * @len: If pending: length for DMA descriptor.
0262  *  If completed: received length, excluding hash prefix.
0263  * @flags: Flags for buffer and packet state.  These are only set on the
0264  *  first buffer of a scattered packet.
0265  */
0266 struct ef4_rx_buffer {
0267     dma_addr_t dma_addr;
0268     struct page *page;
0269     u16 page_offset;
0270     u16 len;
0271     u16 flags;
0272 };
0273 #define EF4_RX_BUF_LAST_IN_PAGE 0x0001
0274 #define EF4_RX_PKT_CSUMMED  0x0002
0275 #define EF4_RX_PKT_DISCARD  0x0004
0276 #define EF4_RX_PKT_TCP      0x0040
0277 #define EF4_RX_PKT_PREFIX_LEN   0x0080  /* length is in prefix only */
0278 
0279 /**
0280  * struct ef4_rx_page_state - Page-based rx buffer state
0281  *
0282  * Inserted at the start of every page allocated for receive buffers.
0283  * Used to facilitate sharing dma mappings between recycled rx buffers
0284  * and those passed up to the kernel.
0285  *
0286  * @dma_addr: The dma address of this page.
0287  */
0288 struct ef4_rx_page_state {
0289     dma_addr_t dma_addr;
0290 
0291     unsigned int __pad[] ____cacheline_aligned;
0292 };
0293 
0294 /**
0295  * struct ef4_rx_queue - An Efx RX queue
0296  * @efx: The associated Efx NIC
0297  * @core_index:  Index of network core RX queue.  Will be >= 0 iff this
0298  *  is associated with a real RX queue.
0299  * @buffer: The software buffer ring
0300  * @rxd: The hardware descriptor ring
0301  * @ptr_mask: The size of the ring minus 1.
0302  * @refill_enabled: Enable refill whenever fill level is low
0303  * @flush_pending: Set when a RX flush is pending. Has the same lifetime as
0304  *  @rxq_flush_pending.
0305  * @added_count: Number of buffers added to the receive queue.
0306  * @notified_count: Number of buffers given to NIC (<= @added_count).
0307  * @removed_count: Number of buffers removed from the receive queue.
0308  * @scatter_n: Used by NIC specific receive code.
0309  * @scatter_len: Used by NIC specific receive code.
0310  * @page_ring: The ring to store DMA mapped pages for reuse.
0311  * @page_add: Counter to calculate the write pointer for the recycle ring.
0312  * @page_remove: Counter to calculate the read pointer for the recycle ring.
0313  * @page_recycle_count: The number of pages that have been recycled.
0314  * @page_recycle_failed: The number of pages that couldn't be recycled because
0315  *      the kernel still held a reference to them.
0316  * @page_recycle_full: The number of pages that were released because the
0317  *      recycle ring was full.
0318  * @page_ptr_mask: The number of pages in the RX recycle ring minus 1.
0319  * @max_fill: RX descriptor maximum fill level (<= ring size)
0320  * @fast_fill_trigger: RX descriptor fill level that will trigger a fast fill
0321  *  (<= @max_fill)
0322  * @min_fill: RX descriptor minimum non-zero fill level.
0323  *  This records the minimum fill level observed when a ring
0324  *  refill was triggered.
0325  * @recycle_count: RX buffer recycle counter.
0326  * @slow_fill: Timer used to defer ef4_nic_generate_fill_event().
0327  */
0328 struct ef4_rx_queue {
0329     struct ef4_nic *efx;
0330     int core_index;
0331     struct ef4_rx_buffer *buffer;
0332     struct ef4_special_buffer rxd;
0333     unsigned int ptr_mask;
0334     bool refill_enabled;
0335     bool flush_pending;
0336 
0337     unsigned int added_count;
0338     unsigned int notified_count;
0339     unsigned int removed_count;
0340     unsigned int scatter_n;
0341     unsigned int scatter_len;
0342     struct page **page_ring;
0343     unsigned int page_add;
0344     unsigned int page_remove;
0345     unsigned int page_recycle_count;
0346     unsigned int page_recycle_failed;
0347     unsigned int page_recycle_full;
0348     unsigned int page_ptr_mask;
0349     unsigned int max_fill;
0350     unsigned int fast_fill_trigger;
0351     unsigned int min_fill;
0352     unsigned int min_overfill;
0353     unsigned int recycle_count;
0354     struct timer_list slow_fill;
0355     unsigned int slow_fill_count;
0356     /* Statistics to supplement MAC stats */
0357     unsigned long rx_packets;
0358 };
0359 
0360 /**
0361  * struct ef4_channel - An Efx channel
0362  *
0363  * A channel comprises an event queue, at least one TX queue, at least
0364  * one RX queue, and an associated tasklet for processing the event
0365  * queue.
0366  *
0367  * @efx: Associated Efx NIC
0368  * @channel: Channel instance number
0369  * @type: Channel type definition
0370  * @eventq_init: Event queue initialised flag
0371  * @enabled: Channel enabled indicator
0372  * @irq: IRQ number (MSI and MSI-X only)
0373  * @irq_moderation_us: IRQ moderation value (in microseconds)
0374  * @napi_dev: Net device used with NAPI
0375  * @napi_str: NAPI control structure
0376  * @state: state for NAPI vs busy polling
0377  * @state_lock: lock protecting @state
0378  * @eventq: Event queue buffer
0379  * @eventq_mask: Event queue pointer mask
0380  * @eventq_read_ptr: Event queue read pointer
0381  * @event_test_cpu: Last CPU to handle interrupt or test event for this channel
0382  * @irq_count: Number of IRQs since last adaptive moderation decision
0383  * @irq_mod_score: IRQ moderation score
0384  * @rps_flow_id: Flow IDs of filters allocated for accelerated RFS,
0385  *      indexed by filter ID
0386  * @n_rx_tobe_disc: Count of RX_TOBE_DISC errors
0387  * @n_rx_ip_hdr_chksum_err: Count of RX IP header checksum errors
0388  * @n_rx_tcp_udp_chksum_err: Count of RX TCP and UDP checksum errors
0389  * @n_rx_mcast_mismatch: Count of unmatched multicast frames
0390  * @n_rx_frm_trunc: Count of RX_FRM_TRUNC errors
0391  * @n_rx_overlength: Count of RX_OVERLENGTH errors
0392  * @n_skbuff_leaks: Count of skbuffs leaked due to RX overrun
0393  * @n_rx_nodesc_trunc: Number of RX packets truncated and then dropped due to
0394  *  lack of descriptors
0395  * @n_rx_merge_events: Number of RX merged completion events
0396  * @n_rx_merge_packets: Number of RX packets completed by merged events
0397  * @rx_pkt_n_frags: Number of fragments in next packet to be delivered by
0398  *  __ef4_rx_packet(), or zero if there is none
0399  * @rx_pkt_index: Ring index of first buffer for next packet to be delivered
0400  *  by __ef4_rx_packet(), if @rx_pkt_n_frags != 0
0401  * @rx_queue: RX queue for this channel
0402  * @tx_queue: TX queues for this channel
0403  */
0404 struct ef4_channel {
0405     struct ef4_nic *efx;
0406     int channel;
0407     const struct ef4_channel_type *type;
0408     bool eventq_init;
0409     bool enabled;
0410     int irq;
0411     unsigned int irq_moderation_us;
0412     struct net_device *napi_dev;
0413     struct napi_struct napi_str;
0414 #ifdef CONFIG_NET_RX_BUSY_POLL
0415     unsigned long busy_poll_state;
0416 #endif
0417     struct ef4_special_buffer eventq;
0418     unsigned int eventq_mask;
0419     unsigned int eventq_read_ptr;
0420     int event_test_cpu;
0421 
0422     unsigned int irq_count;
0423     unsigned int irq_mod_score;
0424 #ifdef CONFIG_RFS_ACCEL
0425     unsigned int rfs_filters_added;
0426 #define RPS_FLOW_ID_INVALID 0xFFFFFFFF
0427     u32 *rps_flow_id;
0428 #endif
0429 
0430     unsigned n_rx_tobe_disc;
0431     unsigned n_rx_ip_hdr_chksum_err;
0432     unsigned n_rx_tcp_udp_chksum_err;
0433     unsigned n_rx_mcast_mismatch;
0434     unsigned n_rx_frm_trunc;
0435     unsigned n_rx_overlength;
0436     unsigned n_skbuff_leaks;
0437     unsigned int n_rx_nodesc_trunc;
0438     unsigned int n_rx_merge_events;
0439     unsigned int n_rx_merge_packets;
0440 
0441     unsigned int rx_pkt_n_frags;
0442     unsigned int rx_pkt_index;
0443 
0444     struct ef4_rx_queue rx_queue;
0445     struct ef4_tx_queue tx_queue[EF4_TXQ_TYPES];
0446 };
0447 
0448 /**
0449  * struct ef4_msi_context - Context for each MSI
0450  * @efx: The associated NIC
0451  * @index: Index of the channel/IRQ
0452  * @name: Name of the channel/IRQ
0453  *
0454  * Unlike &struct ef4_channel, this is never reallocated and is always
0455  * safe for the IRQ handler to access.
0456  */
0457 struct ef4_msi_context {
0458     struct ef4_nic *efx;
0459     unsigned int index;
0460     char name[IFNAMSIZ + 6];
0461 };
0462 
0463 /**
0464  * struct ef4_channel_type - distinguishes traffic and extra channels
0465  * @handle_no_channel: Handle failure to allocate an extra channel
0466  * @pre_probe: Set up extra state prior to initialisation
0467  * @post_remove: Tear down extra state after finalisation, if allocated.
0468  *  May be called on channels that have not been probed.
0469  * @get_name: Generate the channel's name (used for its IRQ handler)
0470  * @copy: Copy the channel state prior to reallocation.  May be %NULL if
0471  *  reallocation is not supported.
0472  * @receive_skb: Handle an skb ready to be passed to netif_receive_skb()
0473  * @keep_eventq: Flag for whether event queue should be kept initialised
0474  *  while the device is stopped
0475  */
0476 struct ef4_channel_type {
0477     void (*handle_no_channel)(struct ef4_nic *);
0478     int (*pre_probe)(struct ef4_channel *);
0479     void (*post_remove)(struct ef4_channel *);
0480     void (*get_name)(struct ef4_channel *, char *buf, size_t len);
0481     struct ef4_channel *(*copy)(const struct ef4_channel *);
0482     bool (*receive_skb)(struct ef4_channel *, struct sk_buff *);
0483     bool keep_eventq;
0484 };
0485 
0486 enum ef4_led_mode {
0487     EF4_LED_OFF = 0,
0488     EF4_LED_ON  = 1,
0489     EF4_LED_DEFAULT = 2
0490 };
0491 
0492 #define STRING_TABLE_LOOKUP(val, member) \
0493     ((val) < member ## _max) ? member ## _names[val] : "(invalid)"
0494 
0495 extern const char *const ef4_loopback_mode_names[];
0496 extern const unsigned int ef4_loopback_mode_max;
0497 #define LOOPBACK_MODE(efx) \
0498     STRING_TABLE_LOOKUP((efx)->loopback_mode, ef4_loopback_mode)
0499 
0500 extern const char *const ef4_reset_type_names[];
0501 extern const unsigned int ef4_reset_type_max;
0502 #define RESET_TYPE(type) \
0503     STRING_TABLE_LOOKUP(type, ef4_reset_type)
0504 
0505 enum ef4_int_mode {
0506     /* Be careful if altering to correct macro below */
0507     EF4_INT_MODE_MSIX = 0,
0508     EF4_INT_MODE_MSI = 1,
0509     EF4_INT_MODE_LEGACY = 2,
0510     EF4_INT_MODE_MAX    /* Insert any new items before this */
0511 };
0512 #define EF4_INT_MODE_USE_MSI(x) (((x)->interrupt_mode) <= EF4_INT_MODE_MSI)
0513 
0514 enum nic_state {
0515     STATE_UNINIT = 0,   /* device being probed/removed or is frozen */
0516     STATE_READY = 1,    /* hardware ready and netdev registered */
0517     STATE_DISABLED = 2, /* device disabled due to hardware errors */
0518     STATE_RECOVERY = 3, /* device recovering from PCI error */
0519 };
0520 
0521 /* Forward declaration */
0522 struct ef4_nic;
0523 
0524 /* Pseudo bit-mask flow control field */
0525 #define EF4_FC_RX   FLOW_CTRL_RX
0526 #define EF4_FC_TX   FLOW_CTRL_TX
0527 #define EF4_FC_AUTO 4
0528 
0529 /**
0530  * struct ef4_link_state - Current state of the link
0531  * @up: Link is up
0532  * @fd: Link is full-duplex
0533  * @fc: Actual flow control flags
0534  * @speed: Link speed (Mbps)
0535  */
0536 struct ef4_link_state {
0537     bool up;
0538     bool fd;
0539     u8 fc;
0540     unsigned int speed;
0541 };
0542 
0543 static inline bool ef4_link_state_equal(const struct ef4_link_state *left,
0544                     const struct ef4_link_state *right)
0545 {
0546     return left->up == right->up && left->fd == right->fd &&
0547         left->fc == right->fc && left->speed == right->speed;
0548 }
0549 
0550 /**
0551  * struct ef4_phy_operations - Efx PHY operations table
0552  * @probe: Probe PHY and initialise efx->mdio.mode_support, efx->mdio.mmds,
0553  *  efx->loopback_modes.
0554  * @init: Initialise PHY
0555  * @fini: Shut down PHY
0556  * @reconfigure: Reconfigure PHY (e.g. for new link parameters)
0557  * @poll: Update @link_state and report whether it changed.
0558  *  Serialised by the mac_lock.
0559  * @get_link_ksettings: Get ethtool settings. Serialised by the mac_lock.
0560  * @set_link_ksettings: Set ethtool settings. Serialised by the mac_lock.
0561  * @set_npage_adv: Set abilities advertised in (Extended) Next Page
0562  *  (only needed where AN bit is set in mmds)
0563  * @test_alive: Test that PHY is 'alive' (online)
0564  * @test_name: Get the name of a PHY-specific test/result
0565  * @run_tests: Run tests and record results as appropriate (offline).
0566  *  Flags are the ethtool tests flags.
0567  */
0568 struct ef4_phy_operations {
0569     int (*probe) (struct ef4_nic *efx);
0570     int (*init) (struct ef4_nic *efx);
0571     void (*fini) (struct ef4_nic *efx);
0572     void (*remove) (struct ef4_nic *efx);
0573     int (*reconfigure) (struct ef4_nic *efx);
0574     bool (*poll) (struct ef4_nic *efx);
0575     void (*get_link_ksettings)(struct ef4_nic *efx,
0576                    struct ethtool_link_ksettings *cmd);
0577     int (*set_link_ksettings)(struct ef4_nic *efx,
0578                   const struct ethtool_link_ksettings *cmd);
0579     void (*set_npage_adv) (struct ef4_nic *efx, u32);
0580     int (*test_alive) (struct ef4_nic *efx);
0581     const char *(*test_name) (struct ef4_nic *efx, unsigned int index);
0582     int (*run_tests) (struct ef4_nic *efx, int *results, unsigned flags);
0583     int (*get_module_eeprom) (struct ef4_nic *efx,
0584                    struct ethtool_eeprom *ee,
0585                    u8 *data);
0586     int (*get_module_info) (struct ef4_nic *efx,
0587                 struct ethtool_modinfo *modinfo);
0588 };
0589 
0590 /**
0591  * enum ef4_phy_mode - PHY operating mode flags
0592  * @PHY_MODE_NORMAL: on and should pass traffic
0593  * @PHY_MODE_TX_DISABLED: on with TX disabled
0594  * @PHY_MODE_LOW_POWER: set to low power through MDIO
0595  * @PHY_MODE_OFF: switched off through external control
0596  * @PHY_MODE_SPECIAL: on but will not pass traffic
0597  */
0598 enum ef4_phy_mode {
0599     PHY_MODE_NORMAL     = 0,
0600     PHY_MODE_TX_DISABLED    = 1,
0601     PHY_MODE_LOW_POWER  = 2,
0602     PHY_MODE_OFF        = 4,
0603     PHY_MODE_SPECIAL    = 8,
0604 };
0605 
0606 static inline bool ef4_phy_mode_disabled(enum ef4_phy_mode mode)
0607 {
0608     return !!(mode & ~PHY_MODE_TX_DISABLED);
0609 }
0610 
0611 /**
0612  * struct ef4_hw_stat_desc - Description of a hardware statistic
0613  * @name: Name of the statistic as visible through ethtool, or %NULL if
0614  *  it should not be exposed
0615  * @dma_width: Width in bits (0 for non-DMA statistics)
0616  * @offset: Offset within stats (ignored for non-DMA statistics)
0617  */
0618 struct ef4_hw_stat_desc {
0619     const char *name;
0620     u16 dma_width;
0621     u16 offset;
0622 };
0623 
0624 /* Number of bits used in a multicast filter hash address */
0625 #define EF4_MCAST_HASH_BITS 8
0626 
0627 /* Number of (single-bit) entries in a multicast filter hash */
0628 #define EF4_MCAST_HASH_ENTRIES (1 << EF4_MCAST_HASH_BITS)
0629 
0630 /* An Efx multicast filter hash */
0631 union ef4_multicast_hash {
0632     u8 byte[EF4_MCAST_HASH_ENTRIES / 8];
0633     ef4_oword_t oword[EF4_MCAST_HASH_ENTRIES / sizeof(ef4_oword_t) / 8];
0634 };
0635 
0636 /**
0637  * struct ef4_nic - an Efx NIC
0638  * @name: Device name (net device name or bus id before net device registered)
0639  * @pci_dev: The PCI device
0640  * @node: List node for maintaining primary/secondary function lists
0641  * @primary: &struct ef4_nic instance for the primary function of this
0642  *  controller.  May be the same structure, and may be %NULL if no
0643  *  primary function is bound.  Serialised by rtnl_lock.
0644  * @secondary_list: List of &struct ef4_nic instances for the secondary PCI
0645  *  functions of the controller, if this is for the primary function.
0646  *  Serialised by rtnl_lock.
0647  * @type: Controller type attributes
0648  * @legacy_irq: IRQ number
0649  * @workqueue: Workqueue for port reconfigures and the HW monitor.
0650  *  Work items do not hold and must not acquire RTNL.
0651  * @workqueue_name: Name of workqueue
0652  * @reset_work: Scheduled reset workitem
0653  * @membase_phys: Memory BAR value as physical address
0654  * @membase: Memory BAR value
0655  * @interrupt_mode: Interrupt mode
0656  * @timer_quantum_ns: Interrupt timer quantum, in nanoseconds
0657  * @timer_max_ns: Interrupt timer maximum value, in nanoseconds
0658  * @irq_rx_adaptive: Adaptive IRQ moderation enabled for RX event queues
0659  * @irq_rx_mod_step_us: Step size for IRQ moderation for RX event queues
0660  * @irq_rx_moderation_us: IRQ moderation time for RX event queues
0661  * @msg_enable: Log message enable flags
0662  * @state: Device state number (%STATE_*). Serialised by the rtnl_lock.
0663  * @reset_pending: Bitmask for pending resets
0664  * @tx_queue: TX DMA queues
0665  * @rx_queue: RX DMA queues
0666  * @channel: Channels
0667  * @msi_context: Context for each MSI
0668  * @extra_channel_types: Types of extra (non-traffic) channels that
0669  *  should be allocated for this NIC
0670  * @rxq_entries: Size of receive queues requested by user.
0671  * @txq_entries: Size of transmit queues requested by user.
0672  * @txq_stop_thresh: TX queue fill level at or above which we stop it.
0673  * @txq_wake_thresh: TX queue fill level at or below which we wake it.
0674  * @tx_dc_base: Base qword address in SRAM of TX queue descriptor caches
0675  * @rx_dc_base: Base qword address in SRAM of RX queue descriptor caches
0676  * @sram_lim_qw: Qword address limit of SRAM
0677  * @next_buffer_table: First available buffer table id
0678  * @n_channels: Number of channels in use
0679  * @n_rx_channels: Number of channels used for RX (= number of RX queues)
0680  * @n_tx_channels: Number of channels used for TX
0681  * @rx_ip_align: RX DMA address offset to have IP header aligned in
0682  *  accordance with NET_IP_ALIGN
0683  * @rx_dma_len: Current maximum RX DMA length
0684  * @rx_buffer_order: Order (log2) of number of pages for each RX buffer
0685  * @rx_buffer_truesize: Amortised allocation size of an RX buffer,
0686  *  for use in sk_buff::truesize
0687  * @rx_prefix_size: Size of RX prefix before packet data
0688  * @rx_packet_hash_offset: Offset of RX flow hash from start of packet data
0689  *  (valid only if @rx_prefix_size != 0; always negative)
0690  * @rx_packet_len_offset: Offset of RX packet length from start of packet data
0691  *  (valid only for NICs that set %EF4_RX_PKT_PREFIX_LEN; always negative)
0692  * @rx_packet_ts_offset: Offset of timestamp from start of packet data
0693  *  (valid only if channel->sync_timestamps_enabled; always negative)
0694  * @rx_hash_key: Toeplitz hash key for RSS
0695  * @rx_indir_table: Indirection table for RSS
0696  * @rx_scatter: Scatter mode enabled for receives
0697  * @int_error_count: Number of internal errors seen recently
0698  * @int_error_expire: Time at which error count will be expired
0699  * @irq_soft_enabled: Are IRQs soft-enabled? If not, IRQ handler will
0700  *  acknowledge but do nothing else.
0701  * @irq_status: Interrupt status buffer
0702  * @irq_zero_count: Number of legacy IRQs seen with queue flags == 0
0703  * @irq_level: IRQ level/index for IRQs not triggered by an event queue
0704  * @selftest_work: Work item for asynchronous self-test
0705  * @mtd_list: List of MTDs attached to the NIC
0706  * @nic_data: Hardware dependent state
0707  * @mac_lock: MAC access lock. Protects @port_enabled, @phy_mode,
0708  *  ef4_monitor() and ef4_reconfigure_port()
0709  * @port_enabled: Port enabled indicator.
0710  *  Serialises ef4_stop_all(), ef4_start_all(), ef4_monitor() and
0711  *  ef4_mac_work() with kernel interfaces. Safe to read under any
0712  *  one of the rtnl_lock, mac_lock, or netif_tx_lock, but all three must
0713  *  be held to modify it.
0714  * @port_initialized: Port initialized?
0715  * @net_dev: Operating system network device. Consider holding the rtnl lock
0716  * @fixed_features: Features which cannot be turned off
0717  * @stats_buffer: DMA buffer for statistics
0718  * @phy_type: PHY type
0719  * @phy_op: PHY interface
0720  * @phy_data: PHY private data (including PHY-specific stats)
0721  * @mdio: PHY MDIO interface
0722  * @phy_mode: PHY operating mode. Serialised by @mac_lock.
0723  * @link_advertising: Autonegotiation advertising flags
0724  * @link_state: Current state of the link
0725  * @n_link_state_changes: Number of times the link has changed state
0726  * @unicast_filter: Flag for Falcon-arch simple unicast filter.
0727  *  Protected by @mac_lock.
0728  * @multicast_hash: Multicast hash table for Falcon-arch.
0729  *  Protected by @mac_lock.
0730  * @wanted_fc: Wanted flow control flags
0731  * @fc_disable: When non-zero flow control is disabled. Typically used to
0732  *  ensure that network back pressure doesn't delay dma queue flushes.
0733  *  Serialised by the rtnl lock.
0734  * @mac_work: Work item for changing MAC promiscuity and multicast hash
0735  * @loopback_mode: Loopback status
0736  * @loopback_modes: Supported loopback mode bitmask
0737  * @loopback_selftest: Offline self-test private state
0738  * @filter_sem: Filter table rw_semaphore, for freeing the table
0739  * @filter_lock: Filter table lock, for mere content changes
0740  * @filter_state: Architecture-dependent filter table state
0741  * @rps_expire_channel: Next channel to check for expiry
0742  * @rps_expire_index: Next index to check for expiry in
0743  *  @rps_expire_channel's @rps_flow_id
0744  * @active_queues: Count of RX and TX queues that haven't been flushed and drained.
0745  * @rxq_flush_pending: Count of number of receive queues that need to be flushed.
0746  *  Decremented when the ef4_flush_rx_queue() is called.
0747  * @rxq_flush_outstanding: Count of number of RX flushes started but not yet
0748  *  completed (either success or failure). Not used when MCDI is used to
0749  *  flush receive queues.
0750  * @flush_wq: wait queue used by ef4_nic_flush_queues() to wait for flush completions.
0751  * @vpd_sn: Serial number read from VPD
0752  * @monitor_work: Hardware monitor workitem
0753  * @biu_lock: BIU (bus interface unit) lock
0754  * @last_irq_cpu: Last CPU to handle a possible test interrupt.  This
0755  *  field is used by ef4_test_interrupts() to verify that an
0756  *  interrupt has occurred.
0757  * @stats_lock: Statistics update lock. Must be held when calling
0758  *  ef4_nic_type::{update,start,stop}_stats.
0759  * @n_rx_noskb_drops: Count of RX packets dropped due to failure to allocate an skb
0760  *
0761  * This is stored in the private area of the &struct net_device.
0762  */
0763 struct ef4_nic {
0764     /* The following fields should be written very rarely */
0765 
0766     char name[IFNAMSIZ];
0767     struct list_head node;
0768     struct ef4_nic *primary;
0769     struct list_head secondary_list;
0770     struct pci_dev *pci_dev;
0771     unsigned int port_num;
0772     const struct ef4_nic_type *type;
0773     int legacy_irq;
0774     bool eeh_disabled_legacy_irq;
0775     struct workqueue_struct *workqueue;
0776     char workqueue_name[16];
0777     struct work_struct reset_work;
0778     resource_size_t membase_phys;
0779     void __iomem *membase;
0780 
0781     enum ef4_int_mode interrupt_mode;
0782     unsigned int timer_quantum_ns;
0783     unsigned int timer_max_ns;
0784     bool irq_rx_adaptive;
0785     unsigned int irq_mod_step_us;
0786     unsigned int irq_rx_moderation_us;
0787     u32 msg_enable;
0788 
0789     enum nic_state state;
0790     unsigned long reset_pending;
0791 
0792     struct ef4_channel *channel[EF4_MAX_CHANNELS];
0793     struct ef4_msi_context msi_context[EF4_MAX_CHANNELS];
0794     const struct ef4_channel_type *
0795     extra_channel_type[EF4_MAX_EXTRA_CHANNELS];
0796 
0797     unsigned rxq_entries;
0798     unsigned txq_entries;
0799     unsigned int txq_stop_thresh;
0800     unsigned int txq_wake_thresh;
0801 
0802     unsigned tx_dc_base;
0803     unsigned rx_dc_base;
0804     unsigned sram_lim_qw;
0805     unsigned next_buffer_table;
0806 
0807     unsigned int max_channels;
0808     unsigned int max_tx_channels;
0809     unsigned n_channels;
0810     unsigned n_rx_channels;
0811     unsigned rss_spread;
0812     unsigned tx_channel_offset;
0813     unsigned n_tx_channels;
0814     unsigned int rx_ip_align;
0815     unsigned int rx_dma_len;
0816     unsigned int rx_buffer_order;
0817     unsigned int rx_buffer_truesize;
0818     unsigned int rx_page_buf_step;
0819     unsigned int rx_bufs_per_page;
0820     unsigned int rx_pages_per_batch;
0821     unsigned int rx_prefix_size;
0822     int rx_packet_hash_offset;
0823     int rx_packet_len_offset;
0824     int rx_packet_ts_offset;
0825     u8 rx_hash_key[40];
0826     u32 rx_indir_table[128];
0827     bool rx_scatter;
0828 
0829     unsigned int_error_count;
0830     unsigned long int_error_expire;
0831 
0832     bool irq_soft_enabled;
0833     struct ef4_buffer irq_status;
0834     unsigned irq_zero_count;
0835     unsigned irq_level;
0836     struct delayed_work selftest_work;
0837 
0838 #ifdef CONFIG_SFC_FALCON_MTD
0839     struct list_head mtd_list;
0840 #endif
0841 
0842     void *nic_data;
0843 
0844     struct mutex mac_lock;
0845     struct work_struct mac_work;
0846     bool port_enabled;
0847 
0848     bool mc_bist_for_other_fn;
0849     bool port_initialized;
0850     struct net_device *net_dev;
0851 
0852     netdev_features_t fixed_features;
0853 
0854     struct ef4_buffer stats_buffer;
0855     u64 rx_nodesc_drops_total;
0856     u64 rx_nodesc_drops_while_down;
0857     bool rx_nodesc_drops_prev_state;
0858 
0859     unsigned int phy_type;
0860     const struct ef4_phy_operations *phy_op;
0861     void *phy_data;
0862     struct mdio_if_info mdio;
0863     enum ef4_phy_mode phy_mode;
0864 
0865     u32 link_advertising;
0866     struct ef4_link_state link_state;
0867     unsigned int n_link_state_changes;
0868 
0869     bool unicast_filter;
0870     union ef4_multicast_hash multicast_hash;
0871     u8 wanted_fc;
0872     unsigned fc_disable;
0873 
0874     atomic_t rx_reset;
0875     enum ef4_loopback_mode loopback_mode;
0876     u64 loopback_modes;
0877 
0878     void *loopback_selftest;
0879 
0880     struct rw_semaphore filter_sem;
0881     spinlock_t filter_lock;
0882     void *filter_state;
0883 #ifdef CONFIG_RFS_ACCEL
0884     unsigned int rps_expire_channel;
0885     unsigned int rps_expire_index;
0886 #endif
0887 
0888     atomic_t active_queues;
0889     atomic_t rxq_flush_pending;
0890     atomic_t rxq_flush_outstanding;
0891     wait_queue_head_t flush_wq;
0892 
0893     char *vpd_sn;
0894 
0895     /* The following fields may be written more often */
0896 
0897     struct delayed_work monitor_work ____cacheline_aligned_in_smp;
0898     spinlock_t biu_lock;
0899     int last_irq_cpu;
0900     spinlock_t stats_lock;
0901     atomic_t n_rx_noskb_drops;
0902 };
0903 
0904 static inline int ef4_dev_registered(struct ef4_nic *efx)
0905 {
0906     return efx->net_dev->reg_state == NETREG_REGISTERED;
0907 }
0908 
0909 static inline unsigned int ef4_port_num(struct ef4_nic *efx)
0910 {
0911     return efx->port_num;
0912 }
0913 
0914 struct ef4_mtd_partition {
0915     struct list_head node;
0916     struct mtd_info mtd;
0917     const char *dev_type_name;
0918     const char *type_name;
0919     char name[IFNAMSIZ + 20];
0920 };
0921 
0922 /**
0923  * struct ef4_nic_type - Efx device type definition
0924  * @mem_bar: Get the memory BAR
0925  * @mem_map_size: Get memory BAR mapped size
0926  * @probe: Probe the controller
0927  * @remove: Free resources allocated by probe()
0928  * @init: Initialise the controller
0929  * @dimension_resources: Dimension controller resources (buffer table,
0930  *  and VIs once the available interrupt resources are clear)
0931  * @fini: Shut down the controller
0932  * @monitor: Periodic function for polling link state and hardware monitor
0933  * @map_reset_reason: Map ethtool reset reason to a reset method
0934  * @map_reset_flags: Map ethtool reset flags to a reset method, if possible
0935  * @reset: Reset the controller hardware and possibly the PHY.  This will
0936  *  be called while the controller is uninitialised.
0937  * @probe_port: Probe the MAC and PHY
0938  * @remove_port: Free resources allocated by probe_port()
0939  * @handle_global_event: Handle a "global" event (may be %NULL)
0940  * @fini_dmaq: Flush and finalise DMA queues (RX and TX queues)
0941  * @prepare_flush: Prepare the hardware for flushing the DMA queues
0942  *  (for Falcon architecture)
0943  * @finish_flush: Clean up after flushing the DMA queues (for Falcon
0944  *  architecture)
0945  * @prepare_flr: Prepare for an FLR
0946  * @finish_flr: Clean up after an FLR
0947  * @describe_stats: Describe statistics for ethtool
0948  * @update_stats: Update statistics not provided by event handling.
0949  *  Either argument may be %NULL.
0950  * @start_stats: Start the regular fetching of statistics
0951  * @pull_stats: Pull stats from the NIC and wait until they arrive.
0952  * @stop_stats: Stop the regular fetching of statistics
0953  * @set_id_led: Set state of identifying LED or revert to automatic function
0954  * @push_irq_moderation: Apply interrupt moderation value
0955  * @reconfigure_port: Push loopback/power/txdis changes to the MAC and PHY
0956  * @prepare_enable_fc_tx: Prepare MAC to enable pause frame TX (may be %NULL)
0957  * @reconfigure_mac: Push MAC address, MTU, flow control and filter settings
0958  *  to the hardware.  Serialised by the mac_lock.
0959  * @check_mac_fault: Check MAC fault state. True if fault present.
0960  * @get_wol: Get WoL configuration from driver state
0961  * @set_wol: Push WoL configuration to the NIC
0962  * @resume_wol: Synchronise WoL state between driver and MC (e.g. after resume)
0963  * @test_chip: Test registers.  May use ef4_farch_test_registers(), and is
0964  *  expected to reset the NIC.
0965  * @test_nvram: Test validity of NVRAM contents
0966  * @irq_enable_master: Enable IRQs on the NIC.  Each event queue must
0967  *  be separately enabled after this.
0968  * @irq_test_generate: Generate a test IRQ
0969  * @irq_disable_non_ev: Disable non-event IRQs on the NIC.  Each event
0970  *  queue must be separately disabled before this.
0971  * @irq_handle_msi: Handle MSI for a channel.  The @dev_id argument is
0972  *  a pointer to the &struct ef4_msi_context for the channel.
0973  * @irq_handle_legacy: Handle legacy interrupt.  The @dev_id argument
0974  *  is a pointer to the &struct ef4_nic.
0975  * @tx_probe: Allocate resources for TX queue
0976  * @tx_init: Initialise TX queue on the NIC
0977  * @tx_remove: Free resources for TX queue
0978  * @tx_write: Write TX descriptors and doorbell
0979  * @rx_push_rss_config: Write RSS hash key and indirection table to the NIC
0980  * @rx_probe: Allocate resources for RX queue
0981  * @rx_init: Initialise RX queue on the NIC
0982  * @rx_remove: Free resources for RX queue
0983  * @rx_write: Write RX descriptors and doorbell
0984  * @rx_defer_refill: Generate a refill reminder event
0985  * @ev_probe: Allocate resources for event queue
0986  * @ev_init: Initialise event queue on the NIC
0987  * @ev_fini: Deinitialise event queue on the NIC
0988  * @ev_remove: Free resources for event queue
0989  * @ev_process: Process events for a queue, up to the given NAPI quota
0990  * @ev_read_ack: Acknowledge read events on a queue, rearming its IRQ
0991  * @ev_test_generate: Generate a test event
0992  * @filter_table_probe: Probe filter capabilities and set up filter software state
0993  * @filter_table_restore: Restore filters removed from hardware
0994  * @filter_table_remove: Remove filters from hardware and tear down software state
0995  * @filter_update_rx_scatter: Update filters after change to rx scatter setting
0996  * @filter_insert: add or replace a filter
0997  * @filter_remove_safe: remove a filter by ID, carefully
0998  * @filter_get_safe: retrieve a filter by ID, carefully
0999  * @filter_clear_rx: Remove all RX filters whose priority is less than or
1000  *  equal to the given priority and is not %EF4_FILTER_PRI_AUTO
1001  * @filter_count_rx_used: Get the number of filters in use at a given priority
1002  * @filter_get_rx_id_limit: Get maximum value of a filter id, plus 1
1003  * @filter_get_rx_ids: Get list of RX filters at a given priority
1004  * @filter_rfs_insert: Add or replace a filter for RFS.  This must be
1005  *  atomic.  The hardware change may be asynchronous but should
1006  *  not be delayed for long.  It may fail if this can't be done
1007  *  atomically.
1008  * @filter_rfs_expire_one: Consider expiring a filter inserted for RFS.
1009  *  This must check whether the specified table entry is used by RFS
1010  *  and that rps_may_expire_flow() returns true for it.
1011  * @mtd_probe: Probe and add MTD partitions associated with this net device,
1012  *   using ef4_mtd_add()
1013  * @mtd_rename: Set an MTD partition name using the net device name
1014  * @mtd_read: Read from an MTD partition
1015  * @mtd_erase: Erase part of an MTD partition
1016  * @mtd_write: Write to an MTD partition
1017  * @mtd_sync: Wait for write-back to complete on MTD partition.  This
1018  *  also notifies the driver that a writer has finished using this
1019  *  partition.
1020  * @set_mac_address: Set the MAC address of the device
1021  * @revision: Hardware architecture revision
1022  * @txd_ptr_tbl_base: TX descriptor ring base address
1023  * @rxd_ptr_tbl_base: RX descriptor ring base address
1024  * @buf_tbl_base: Buffer table base address
1025  * @evq_ptr_tbl_base: Event queue pointer table base address
1026  * @evq_rptr_tbl_base: Event queue read-pointer table base address
1027  * @max_dma_mask: Maximum possible DMA mask
1028  * @rx_prefix_size: Size of RX prefix before packet data
1029  * @rx_hash_offset: Offset of RX flow hash within prefix
1030  * @rx_ts_offset: Offset of timestamp within prefix
1031  * @rx_buffer_padding: Size of padding at end of RX packet
1032  * @can_rx_scatter: NIC is able to scatter packets to multiple buffers
1033  * @always_rx_scatter: NIC will always scatter packets to multiple buffers
1034  * @max_interrupt_mode: Highest capability interrupt mode supported
1035  *  from &enum ef4_init_mode.
1036  * @timer_period_max: Maximum period of interrupt timer (in ticks)
1037  * @offload_features: net_device feature flags for protocol offload
1038  *  features implemented in hardware
1039  */
1040 struct ef4_nic_type {
1041     unsigned int mem_bar;
1042     unsigned int (*mem_map_size)(struct ef4_nic *efx);
1043     int (*probe)(struct ef4_nic *efx);
1044     void (*remove)(struct ef4_nic *efx);
1045     int (*init)(struct ef4_nic *efx);
1046     int (*dimension_resources)(struct ef4_nic *efx);
1047     void (*fini)(struct ef4_nic *efx);
1048     void (*monitor)(struct ef4_nic *efx);
1049     enum reset_type (*map_reset_reason)(enum reset_type reason);
1050     int (*map_reset_flags)(u32 *flags);
1051     int (*reset)(struct ef4_nic *efx, enum reset_type method);
1052     int (*probe_port)(struct ef4_nic *efx);
1053     void (*remove_port)(struct ef4_nic *efx);
1054     bool (*handle_global_event)(struct ef4_channel *channel, ef4_qword_t *);
1055     int (*fini_dmaq)(struct ef4_nic *efx);
1056     void (*prepare_flush)(struct ef4_nic *efx);
1057     void (*finish_flush)(struct ef4_nic *efx);
1058     void (*prepare_flr)(struct ef4_nic *efx);
1059     void (*finish_flr)(struct ef4_nic *efx);
1060     size_t (*describe_stats)(struct ef4_nic *efx, u8 *names);
1061     size_t (*update_stats)(struct ef4_nic *efx, u64 *full_stats,
1062                    struct rtnl_link_stats64 *core_stats);
1063     void (*start_stats)(struct ef4_nic *efx);
1064     void (*pull_stats)(struct ef4_nic *efx);
1065     void (*stop_stats)(struct ef4_nic *efx);
1066     void (*set_id_led)(struct ef4_nic *efx, enum ef4_led_mode mode);
1067     void (*push_irq_moderation)(struct ef4_channel *channel);
1068     int (*reconfigure_port)(struct ef4_nic *efx);
1069     void (*prepare_enable_fc_tx)(struct ef4_nic *efx);
1070     int (*reconfigure_mac)(struct ef4_nic *efx);
1071     bool (*check_mac_fault)(struct ef4_nic *efx);
1072     void (*get_wol)(struct ef4_nic *efx, struct ethtool_wolinfo *wol);
1073     int (*set_wol)(struct ef4_nic *efx, u32 type);
1074     void (*resume_wol)(struct ef4_nic *efx);
1075     int (*test_chip)(struct ef4_nic *efx, struct ef4_self_tests *tests);
1076     int (*test_nvram)(struct ef4_nic *efx);
1077     void (*irq_enable_master)(struct ef4_nic *efx);
1078     int (*irq_test_generate)(struct ef4_nic *efx);
1079     void (*irq_disable_non_ev)(struct ef4_nic *efx);
1080     irqreturn_t (*irq_handle_msi)(int irq, void *dev_id);
1081     irqreturn_t (*irq_handle_legacy)(int irq, void *dev_id);
1082     int (*tx_probe)(struct ef4_tx_queue *tx_queue);
1083     void (*tx_init)(struct ef4_tx_queue *tx_queue);
1084     void (*tx_remove)(struct ef4_tx_queue *tx_queue);
1085     void (*tx_write)(struct ef4_tx_queue *tx_queue);
1086     unsigned int (*tx_limit_len)(struct ef4_tx_queue *tx_queue,
1087                      dma_addr_t dma_addr, unsigned int len);
1088     int (*rx_push_rss_config)(struct ef4_nic *efx, bool user,
1089                   const u32 *rx_indir_table);
1090     int (*rx_probe)(struct ef4_rx_queue *rx_queue);
1091     void (*rx_init)(struct ef4_rx_queue *rx_queue);
1092     void (*rx_remove)(struct ef4_rx_queue *rx_queue);
1093     void (*rx_write)(struct ef4_rx_queue *rx_queue);
1094     void (*rx_defer_refill)(struct ef4_rx_queue *rx_queue);
1095     int (*ev_probe)(struct ef4_channel *channel);
1096     int (*ev_init)(struct ef4_channel *channel);
1097     void (*ev_fini)(struct ef4_channel *channel);
1098     void (*ev_remove)(struct ef4_channel *channel);
1099     int (*ev_process)(struct ef4_channel *channel, int quota);
1100     void (*ev_read_ack)(struct ef4_channel *channel);
1101     void (*ev_test_generate)(struct ef4_channel *channel);
1102     int (*filter_table_probe)(struct ef4_nic *efx);
1103     void (*filter_table_restore)(struct ef4_nic *efx);
1104     void (*filter_table_remove)(struct ef4_nic *efx);
1105     void (*filter_update_rx_scatter)(struct ef4_nic *efx);
1106     s32 (*filter_insert)(struct ef4_nic *efx,
1107                  struct ef4_filter_spec *spec, bool replace);
1108     int (*filter_remove_safe)(struct ef4_nic *efx,
1109                   enum ef4_filter_priority priority,
1110                   u32 filter_id);
1111     int (*filter_get_safe)(struct ef4_nic *efx,
1112                    enum ef4_filter_priority priority,
1113                    u32 filter_id, struct ef4_filter_spec *);
1114     int (*filter_clear_rx)(struct ef4_nic *efx,
1115                    enum ef4_filter_priority priority);
1116     u32 (*filter_count_rx_used)(struct ef4_nic *efx,
1117                     enum ef4_filter_priority priority);
1118     u32 (*filter_get_rx_id_limit)(struct ef4_nic *efx);
1119     s32 (*filter_get_rx_ids)(struct ef4_nic *efx,
1120                  enum ef4_filter_priority priority,
1121                  u32 *buf, u32 size);
1122 #ifdef CONFIG_RFS_ACCEL
1123     s32 (*filter_rfs_insert)(struct ef4_nic *efx,
1124                  struct ef4_filter_spec *spec);
1125     bool (*filter_rfs_expire_one)(struct ef4_nic *efx, u32 flow_id,
1126                       unsigned int index);
1127 #endif
1128 #ifdef CONFIG_SFC_FALCON_MTD
1129     int (*mtd_probe)(struct ef4_nic *efx);
1130     void (*mtd_rename)(struct ef4_mtd_partition *part);
1131     int (*mtd_read)(struct mtd_info *mtd, loff_t start, size_t len,
1132             size_t *retlen, u8 *buffer);
1133     int (*mtd_erase)(struct mtd_info *mtd, loff_t start, size_t len);
1134     int (*mtd_write)(struct mtd_info *mtd, loff_t start, size_t len,
1135              size_t *retlen, const u8 *buffer);
1136     int (*mtd_sync)(struct mtd_info *mtd);
1137 #endif
1138     int (*get_mac_address)(struct ef4_nic *efx, unsigned char *perm_addr);
1139     int (*set_mac_address)(struct ef4_nic *efx);
1140 
1141     int revision;
1142     unsigned int txd_ptr_tbl_base;
1143     unsigned int rxd_ptr_tbl_base;
1144     unsigned int buf_tbl_base;
1145     unsigned int evq_ptr_tbl_base;
1146     unsigned int evq_rptr_tbl_base;
1147     u64 max_dma_mask;
1148     unsigned int rx_prefix_size;
1149     unsigned int rx_hash_offset;
1150     unsigned int rx_ts_offset;
1151     unsigned int rx_buffer_padding;
1152     bool can_rx_scatter;
1153     bool always_rx_scatter;
1154     unsigned int max_interrupt_mode;
1155     unsigned int timer_period_max;
1156     netdev_features_t offload_features;
1157     unsigned int max_rx_ip_filters;
1158 };
1159 
1160 /**************************************************************************
1161  *
1162  * Prototypes and inline functions
1163  *
1164  *************************************************************************/
1165 
1166 static inline struct ef4_channel *
1167 ef4_get_channel(struct ef4_nic *efx, unsigned index)
1168 {
1169     EF4_BUG_ON_PARANOID(index >= efx->n_channels);
1170     return efx->channel[index];
1171 }
1172 
1173 /* Iterate over all used channels */
1174 #define ef4_for_each_channel(_channel, _efx)                \
1175     for (_channel = (_efx)->channel[0];             \
1176          _channel;                          \
1177          _channel = (_channel->channel + 1 < (_efx)->n_channels) ?  \
1178              (_efx)->channel[_channel->channel + 1] : NULL)
1179 
1180 /* Iterate over all used channels in reverse */
1181 #define ef4_for_each_channel_rev(_channel, _efx)            \
1182     for (_channel = (_efx)->channel[(_efx)->n_channels - 1];    \
1183          _channel;                          \
1184          _channel = _channel->channel ?             \
1185              (_efx)->channel[_channel->channel - 1] : NULL)
1186 
1187 static inline struct ef4_tx_queue *
1188 ef4_get_tx_queue(struct ef4_nic *efx, unsigned index, unsigned type)
1189 {
1190     EF4_BUG_ON_PARANOID(index >= efx->n_tx_channels ||
1191                 type >= EF4_TXQ_TYPES);
1192     return &efx->channel[efx->tx_channel_offset + index]->tx_queue[type];
1193 }
1194 
1195 static inline bool ef4_channel_has_tx_queues(struct ef4_channel *channel)
1196 {
1197     return channel->channel - channel->efx->tx_channel_offset <
1198         channel->efx->n_tx_channels;
1199 }
1200 
1201 static inline struct ef4_tx_queue *
1202 ef4_channel_get_tx_queue(struct ef4_channel *channel, unsigned type)
1203 {
1204     EF4_BUG_ON_PARANOID(!ef4_channel_has_tx_queues(channel) ||
1205                 type >= EF4_TXQ_TYPES);
1206     return &channel->tx_queue[type];
1207 }
1208 
1209 static inline bool ef4_tx_queue_used(struct ef4_tx_queue *tx_queue)
1210 {
1211     return !(tx_queue->efx->net_dev->num_tc < 2 &&
1212          tx_queue->queue & EF4_TXQ_TYPE_HIGHPRI);
1213 }
1214 
1215 /* Iterate over all TX queues belonging to a channel */
1216 #define ef4_for_each_channel_tx_queue(_tx_queue, _channel)      \
1217     if (!ef4_channel_has_tx_queues(_channel))           \
1218         ;                           \
1219     else                                \
1220         for (_tx_queue = (_channel)->tx_queue;          \
1221              _tx_queue < (_channel)->tx_queue + EF4_TXQ_TYPES && \
1222                  ef4_tx_queue_used(_tx_queue);      \
1223              _tx_queue++)
1224 
1225 /* Iterate over all possible TX queues belonging to a channel */
1226 #define ef4_for_each_possible_channel_tx_queue(_tx_queue, _channel) \
1227     if (!ef4_channel_has_tx_queues(_channel))           \
1228         ;                           \
1229     else                                \
1230         for (_tx_queue = (_channel)->tx_queue;          \
1231              _tx_queue < (_channel)->tx_queue + EF4_TXQ_TYPES;  \
1232              _tx_queue++)
1233 
1234 static inline bool ef4_channel_has_rx_queue(struct ef4_channel *channel)
1235 {
1236     return channel->rx_queue.core_index >= 0;
1237 }
1238 
1239 static inline struct ef4_rx_queue *
1240 ef4_channel_get_rx_queue(struct ef4_channel *channel)
1241 {
1242     EF4_BUG_ON_PARANOID(!ef4_channel_has_rx_queue(channel));
1243     return &channel->rx_queue;
1244 }
1245 
1246 /* Iterate over all RX queues belonging to a channel */
1247 #define ef4_for_each_channel_rx_queue(_rx_queue, _channel)      \
1248     if (!ef4_channel_has_rx_queue(_channel))            \
1249         ;                           \
1250     else                                \
1251         for (_rx_queue = &(_channel)->rx_queue;         \
1252              _rx_queue;                     \
1253              _rx_queue = NULL)
1254 
1255 static inline struct ef4_channel *
1256 ef4_rx_queue_channel(struct ef4_rx_queue *rx_queue)
1257 {
1258     return container_of(rx_queue, struct ef4_channel, rx_queue);
1259 }
1260 
1261 static inline int ef4_rx_queue_index(struct ef4_rx_queue *rx_queue)
1262 {
1263     return ef4_rx_queue_channel(rx_queue)->channel;
1264 }
1265 
1266 /* Returns a pointer to the specified receive buffer in the RX
1267  * descriptor queue.
1268  */
1269 static inline struct ef4_rx_buffer *ef4_rx_buffer(struct ef4_rx_queue *rx_queue,
1270                           unsigned int index)
1271 {
1272     return &rx_queue->buffer[index];
1273 }
1274 
1275 /**
1276  * EF4_MAX_FRAME_LEN - calculate maximum frame length
1277  *
1278  * This calculates the maximum frame length that will be used for a
1279  * given MTU.  The frame length will be equal to the MTU plus a
1280  * constant amount of header space and padding.  This is the quantity
1281  * that the net driver will program into the MAC as the maximum frame
1282  * length.
1283  *
1284  * The 10G MAC requires 8-byte alignment on the frame
1285  * length, so we round up to the nearest 8.
1286  *
1287  * Re-clocking by the XGXS on RX can reduce an IPG to 32 bits (half an
1288  * XGMII cycle).  If the frame length reaches the maximum value in the
1289  * same cycle, the XMAC can miss the IPG altogether.  We work around
1290  * this by adding a further 16 bytes.
1291  */
1292 #define EF4_FRAME_PAD   16
1293 #define EF4_MAX_FRAME_LEN(mtu) \
1294     (ALIGN(((mtu) + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN + EF4_FRAME_PAD), 8))
1295 
1296 /* Get all supported features.
1297  * If a feature is not fixed, it is present in hw_features.
1298  * If a feature is fixed, it does not present in hw_features, but
1299  * always in features.
1300  */
1301 static inline netdev_features_t ef4_supported_features(const struct ef4_nic *efx)
1302 {
1303     const struct net_device *net_dev = efx->net_dev;
1304 
1305     return net_dev->features | net_dev->hw_features;
1306 }
1307 
1308 /* Get the current TX queue insert index. */
1309 static inline unsigned int
1310 ef4_tx_queue_get_insert_index(const struct ef4_tx_queue *tx_queue)
1311 {
1312     return tx_queue->insert_count & tx_queue->ptr_mask;
1313 }
1314 
1315 /* Get a TX buffer. */
1316 static inline struct ef4_tx_buffer *
1317 __ef4_tx_queue_get_insert_buffer(const struct ef4_tx_queue *tx_queue)
1318 {
1319     return &tx_queue->buffer[ef4_tx_queue_get_insert_index(tx_queue)];
1320 }
1321 
1322 /* Get a TX buffer, checking it's not currently in use. */
1323 static inline struct ef4_tx_buffer *
1324 ef4_tx_queue_get_insert_buffer(const struct ef4_tx_queue *tx_queue)
1325 {
1326     struct ef4_tx_buffer *buffer =
1327         __ef4_tx_queue_get_insert_buffer(tx_queue);
1328 
1329     EF4_BUG_ON_PARANOID(buffer->len);
1330     EF4_BUG_ON_PARANOID(buffer->flags);
1331     EF4_BUG_ON_PARANOID(buffer->unmap_len);
1332 
1333     return buffer;
1334 }
1335 
1336 #endif /* EF4_NET_DRIVER_H */