]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/sfc/net_driver.h
Merge tag 'wireless-drivers-next-for-davem-2015-10-27' of git://git.kernel.org/pub...
[karo-tx-linux.git] / drivers / net / ethernet / sfc / net_driver.h
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2005-2013 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10
11 /* Common definitions for all Efx net driver code */
12
13 #ifndef EFX_NET_DRIVER_H
14 #define EFX_NET_DRIVER_H
15
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/if_vlan.h>
20 #include <linux/timer.h>
21 #include <linux/mdio.h>
22 #include <linux/list.h>
23 #include <linux/pci.h>
24 #include <linux/device.h>
25 #include <linux/highmem.h>
26 #include <linux/workqueue.h>
27 #include <linux/mutex.h>
28 #include <linux/rwsem.h>
29 #include <linux/vmalloc.h>
30 #include <linux/i2c.h>
31 #include <linux/mtd/mtd.h>
32 #include <net/busy_poll.h>
33
34 #include "enum.h"
35 #include "bitfield.h"
36 #include "filter.h"
37
38 /**************************************************************************
39  *
40  * Build definitions
41  *
42  **************************************************************************/
43
44 #define EFX_DRIVER_VERSION      "4.0"
45
46 #ifdef DEBUG
47 #define EFX_BUG_ON_PARANOID(x) BUG_ON(x)
48 #define EFX_WARN_ON_PARANOID(x) WARN_ON(x)
49 #else
50 #define EFX_BUG_ON_PARANOID(x) do {} while (0)
51 #define EFX_WARN_ON_PARANOID(x) do {} while (0)
52 #endif
53
54 /**************************************************************************
55  *
56  * Efx data structures
57  *
58  **************************************************************************/
59
60 #define EFX_MAX_CHANNELS 32U
61 #define EFX_MAX_RX_QUEUES EFX_MAX_CHANNELS
62 #define EFX_EXTRA_CHANNEL_IOV   0
63 #define EFX_EXTRA_CHANNEL_PTP   1
64 #define EFX_MAX_EXTRA_CHANNELS  2U
65
66 /* Checksum generation is a per-queue option in hardware, so each
67  * queue visible to the networking core is backed by two hardware TX
68  * queues. */
69 #define EFX_MAX_TX_TC           2
70 #define EFX_MAX_CORE_TX_QUEUES  (EFX_MAX_TX_TC * EFX_MAX_CHANNELS)
71 #define EFX_TXQ_TYPE_OFFLOAD    1       /* flag */
72 #define EFX_TXQ_TYPE_HIGHPRI    2       /* flag */
73 #define EFX_TXQ_TYPES           4
74 #define EFX_MAX_TX_QUEUES       (EFX_TXQ_TYPES * EFX_MAX_CHANNELS)
75
76 /* Maximum possible MTU the driver supports */
77 #define EFX_MAX_MTU (9 * 1024)
78
79 /* Size of an RX scatter buffer.  Small enough to pack 2 into a 4K page,
80  * and should be a multiple of the cache line size.
81  */
82 #define EFX_RX_USR_BUF_SIZE     (2048 - 256)
83
84 /* If possible, we should ensure cache line alignment at start and end
85  * of every buffer.  Otherwise, we just need to ensure 4-byte
86  * alignment of the network header.
87  */
88 #if NET_IP_ALIGN == 0
89 #define EFX_RX_BUF_ALIGNMENT    L1_CACHE_BYTES
90 #else
91 #define EFX_RX_BUF_ALIGNMENT    4
92 #endif
93
94 /* Forward declare Precision Time Protocol (PTP) support structure. */
95 struct efx_ptp_data;
96 struct hwtstamp_config;
97
98 struct efx_self_tests;
99
100 /**
101  * struct efx_buffer - A general-purpose DMA buffer
102  * @addr: host base address of the buffer
103  * @dma_addr: DMA base address of the buffer
104  * @len: Buffer length, in bytes
105  *
106  * The NIC uses these buffers for its interrupt status registers and
107  * MAC stats dumps.
108  */
109 struct efx_buffer {
110         void *addr;
111         dma_addr_t dma_addr;
112         unsigned int len;
113 };
114
115 /**
116  * struct efx_special_buffer - DMA buffer entered into buffer table
117  * @buf: Standard &struct efx_buffer
118  * @index: Buffer index within controller;s buffer table
119  * @entries: Number of buffer table entries
120  *
121  * The NIC has a buffer table that maps buffers of size %EFX_BUF_SIZE.
122  * Event and descriptor rings are addressed via one or more buffer
123  * table entries (and so can be physically non-contiguous, although we
124  * currently do not take advantage of that).  On Falcon and Siena we
125  * have to take care of allocating and initialising the entries
126  * ourselves.  On later hardware this is managed by the firmware and
127  * @index and @entries are left as 0.
128  */
129 struct efx_special_buffer {
130         struct efx_buffer buf;
131         unsigned int index;
132         unsigned int entries;
133 };
134
135 /**
136  * struct efx_tx_buffer - buffer state for a TX descriptor
137  * @skb: When @flags & %EFX_TX_BUF_SKB, the associated socket buffer to be
138  *      freed when descriptor completes
139  * @heap_buf: When @flags & %EFX_TX_BUF_HEAP, the associated heap buffer to be
140  *      freed when descriptor completes.
141  * @option: When @flags & %EFX_TX_BUF_OPTION, a NIC-specific option descriptor.
142  * @dma_addr: DMA address of the fragment.
143  * @flags: Flags for allocation and DMA mapping type
144  * @len: Length of this fragment.
145  *      This field is zero when the queue slot is empty.
146  * @unmap_len: Length of this fragment to unmap
147  * @dma_offset: Offset of @dma_addr from the address of the backing DMA mapping.
148  * Only valid if @unmap_len != 0.
149  */
150 struct efx_tx_buffer {
151         union {
152                 const struct sk_buff *skb;
153                 void *heap_buf;
154         };
155         union {
156                 efx_qword_t option;
157                 dma_addr_t dma_addr;
158         };
159         unsigned short flags;
160         unsigned short len;
161         unsigned short unmap_len;
162         unsigned short dma_offset;
163 };
164 #define EFX_TX_BUF_CONT         1       /* not last descriptor of packet */
165 #define EFX_TX_BUF_SKB          2       /* buffer is last part of skb */
166 #define EFX_TX_BUF_HEAP         4       /* buffer was allocated with kmalloc() */
167 #define EFX_TX_BUF_MAP_SINGLE   8       /* buffer was mapped with dma_map_single() */
168 #define EFX_TX_BUF_OPTION       0x10    /* empty buffer for option descriptor */
169
170 /**
171  * struct efx_tx_queue - An Efx TX queue
172  *
173  * This is a ring buffer of TX fragments.
174  * Since the TX completion path always executes on the same
175  * CPU and the xmit path can operate on different CPUs,
176  * performance is increased by ensuring that the completion
177  * path and the xmit path operate on different cache lines.
178  * This is particularly important if the xmit path is always
179  * executing on one CPU which is different from the completion
180  * path.  There is also a cache line for members which are
181  * read but not written on the fast path.
182  *
183  * @efx: The associated Efx NIC
184  * @queue: DMA queue number
185  * @channel: The associated channel
186  * @core_txq: The networking core TX queue structure
187  * @buffer: The software buffer ring
188  * @tsoh_page: Array of pages of TSO header buffers
189  * @txd: The hardware descriptor ring
190  * @ptr_mask: The size of the ring minus 1.
191  * @piobuf: PIO buffer region for this TX queue (shared with its partner).
192  *      Size of the region is efx_piobuf_size.
193  * @piobuf_offset: Buffer offset to be specified in PIO descriptors
194  * @initialised: Has hardware queue been initialised?
195  * @read_count: Current read pointer.
196  *      This is the number of buffers that have been removed from both rings.
197  * @old_write_count: The value of @write_count when last checked.
198  *      This is here for performance reasons.  The xmit path will
199  *      only get the up-to-date value of @write_count if this
200  *      variable indicates that the queue is empty.  This is to
201  *      avoid cache-line ping-pong between the xmit path and the
202  *      completion path.
203  * @merge_events: Number of TX merged completion events
204  * @insert_count: Current insert pointer
205  *      This is the number of buffers that have been added to the
206  *      software ring.
207  * @write_count: Current write pointer
208  *      This is the number of buffers that have been added to the
209  *      hardware ring.
210  * @old_read_count: The value of read_count when last checked.
211  *      This is here for performance reasons.  The xmit path will
212  *      only get the up-to-date value of read_count if this
213  *      variable indicates that the queue is full.  This is to
214  *      avoid cache-line ping-pong between the xmit path and the
215  *      completion path.
216  * @tso_bursts: Number of times TSO xmit invoked by kernel
217  * @tso_long_headers: Number of packets with headers too long for standard
218  *      blocks
219  * @tso_packets: Number of packets via the TSO xmit path
220  * @pushes: Number of times the TX push feature has been used
221  * @pio_packets: Number of times the TX PIO feature has been used
222  * @empty_read_count: If the completion path has seen the queue as empty
223  *      and the transmission path has not yet checked this, the value of
224  *      @read_count bitwise-added to %EFX_EMPTY_COUNT_VALID; otherwise 0.
225  */
226 struct efx_tx_queue {
227         /* Members which don't change on the fast path */
228         struct efx_nic *efx ____cacheline_aligned_in_smp;
229         unsigned queue;
230         struct efx_channel *channel;
231         struct netdev_queue *core_txq;
232         struct efx_tx_buffer *buffer;
233         struct efx_buffer *tsoh_page;
234         struct efx_special_buffer txd;
235         unsigned int ptr_mask;
236         void __iomem *piobuf;
237         unsigned int piobuf_offset;
238         bool initialised;
239
240         /* Members used mainly on the completion path */
241         unsigned int read_count ____cacheline_aligned_in_smp;
242         unsigned int old_write_count;
243         unsigned int merge_events;
244         unsigned int bytes_compl;
245         unsigned int pkts_compl;
246
247         /* Members used only on the xmit path */
248         unsigned int insert_count ____cacheline_aligned_in_smp;
249         unsigned int write_count;
250         unsigned int old_read_count;
251         unsigned int tso_bursts;
252         unsigned int tso_long_headers;
253         unsigned int tso_packets;
254         unsigned int pushes;
255         unsigned int pio_packets;
256         /* Statistics to supplement MAC stats */
257         unsigned long tx_packets;
258
259         /* Members shared between paths and sometimes updated */
260         unsigned int empty_read_count ____cacheline_aligned_in_smp;
261 #define EFX_EMPTY_COUNT_VALID 0x80000000
262         atomic_t flush_outstanding;
263 };
264
265 /**
266  * struct efx_rx_buffer - An Efx RX data buffer
267  * @dma_addr: DMA base address of the buffer
268  * @page: The associated page buffer.
269  *      Will be %NULL if the buffer slot is currently free.
270  * @page_offset: If pending: offset in @page of DMA base address.
271  *      If completed: offset in @page of Ethernet header.
272  * @len: If pending: length for DMA descriptor.
273  *      If completed: received length, excluding hash prefix.
274  * @flags: Flags for buffer and packet state.  These are only set on the
275  *      first buffer of a scattered packet.
276  */
277 struct efx_rx_buffer {
278         dma_addr_t dma_addr;
279         struct page *page;
280         u16 page_offset;
281         u16 len;
282         u16 flags;
283 };
284 #define EFX_RX_BUF_LAST_IN_PAGE 0x0001
285 #define EFX_RX_PKT_CSUMMED      0x0002
286 #define EFX_RX_PKT_DISCARD      0x0004
287 #define EFX_RX_PKT_TCP          0x0040
288 #define EFX_RX_PKT_PREFIX_LEN   0x0080  /* length is in prefix only */
289
290 /**
291  * struct efx_rx_page_state - Page-based rx buffer state
292  *
293  * Inserted at the start of every page allocated for receive buffers.
294  * Used to facilitate sharing dma mappings between recycled rx buffers
295  * and those passed up to the kernel.
296  *
297  * @dma_addr: The dma address of this page.
298  */
299 struct efx_rx_page_state {
300         dma_addr_t dma_addr;
301
302         unsigned int __pad[0] ____cacheline_aligned;
303 };
304
305 /**
306  * struct efx_rx_queue - An Efx RX queue
307  * @efx: The associated Efx NIC
308  * @core_index:  Index of network core RX queue.  Will be >= 0 iff this
309  *      is associated with a real RX queue.
310  * @buffer: The software buffer ring
311  * @rxd: The hardware descriptor ring
312  * @ptr_mask: The size of the ring minus 1.
313  * @refill_enabled: Enable refill whenever fill level is low
314  * @flush_pending: Set when a RX flush is pending. Has the same lifetime as
315  *      @rxq_flush_pending.
316  * @added_count: Number of buffers added to the receive queue.
317  * @notified_count: Number of buffers given to NIC (<= @added_count).
318  * @removed_count: Number of buffers removed from the receive queue.
319  * @scatter_n: Used by NIC specific receive code.
320  * @scatter_len: Used by NIC specific receive code.
321  * @page_ring: The ring to store DMA mapped pages for reuse.
322  * @page_add: Counter to calculate the write pointer for the recycle ring.
323  * @page_remove: Counter to calculate the read pointer for the recycle ring.
324  * @page_recycle_count: The number of pages that have been recycled.
325  * @page_recycle_failed: The number of pages that couldn't be recycled because
326  *      the kernel still held a reference to them.
327  * @page_recycle_full: The number of pages that were released because the
328  *      recycle ring was full.
329  * @page_ptr_mask: The number of pages in the RX recycle ring minus 1.
330  * @max_fill: RX descriptor maximum fill level (<= ring size)
331  * @fast_fill_trigger: RX descriptor fill level that will trigger a fast fill
332  *      (<= @max_fill)
333  * @min_fill: RX descriptor minimum non-zero fill level.
334  *      This records the minimum fill level observed when a ring
335  *      refill was triggered.
336  * @recycle_count: RX buffer recycle counter.
337  * @slow_fill: Timer used to defer efx_nic_generate_fill_event().
338  */
339 struct efx_rx_queue {
340         struct efx_nic *efx;
341         int core_index;
342         struct efx_rx_buffer *buffer;
343         struct efx_special_buffer rxd;
344         unsigned int ptr_mask;
345         bool refill_enabled;
346         bool flush_pending;
347
348         unsigned int added_count;
349         unsigned int notified_count;
350         unsigned int removed_count;
351         unsigned int scatter_n;
352         unsigned int scatter_len;
353         struct page **page_ring;
354         unsigned int page_add;
355         unsigned int page_remove;
356         unsigned int page_recycle_count;
357         unsigned int page_recycle_failed;
358         unsigned int page_recycle_full;
359         unsigned int page_ptr_mask;
360         unsigned int max_fill;
361         unsigned int fast_fill_trigger;
362         unsigned int min_fill;
363         unsigned int min_overfill;
364         unsigned int recycle_count;
365         struct timer_list slow_fill;
366         unsigned int slow_fill_count;
367         /* Statistics to supplement MAC stats */
368         unsigned long rx_packets;
369 };
370
371 enum efx_sync_events_state {
372         SYNC_EVENTS_DISABLED = 0,
373         SYNC_EVENTS_QUIESCENT,
374         SYNC_EVENTS_REQUESTED,
375         SYNC_EVENTS_VALID,
376 };
377
378 /**
379  * struct efx_channel - An Efx channel
380  *
381  * A channel comprises an event queue, at least one TX queue, at least
382  * one RX queue, and an associated tasklet for processing the event
383  * queue.
384  *
385  * @efx: Associated Efx NIC
386  * @channel: Channel instance number
387  * @type: Channel type definition
388  * @eventq_init: Event queue initialised flag
389  * @enabled: Channel enabled indicator
390  * @irq: IRQ number (MSI and MSI-X only)
391  * @irq_moderation: IRQ moderation value (in hardware ticks)
392  * @napi_dev: Net device used with NAPI
393  * @napi_str: NAPI control structure
394  * @state: state for NAPI vs busy polling
395  * @state_lock: lock protecting @state
396  * @eventq: Event queue buffer
397  * @eventq_mask: Event queue pointer mask
398  * @eventq_read_ptr: Event queue read pointer
399  * @event_test_cpu: Last CPU to handle interrupt or test event for this channel
400  * @irq_count: Number of IRQs since last adaptive moderation decision
401  * @irq_mod_score: IRQ moderation score
402  * @n_rx_tobe_disc: Count of RX_TOBE_DISC errors
403  * @n_rx_ip_hdr_chksum_err: Count of RX IP header checksum errors
404  * @n_rx_tcp_udp_chksum_err: Count of RX TCP and UDP checksum errors
405  * @n_rx_mcast_mismatch: Count of unmatched multicast frames
406  * @n_rx_frm_trunc: Count of RX_FRM_TRUNC errors
407  * @n_rx_overlength: Count of RX_OVERLENGTH errors
408  * @n_skbuff_leaks: Count of skbuffs leaked due to RX overrun
409  * @n_rx_nodesc_trunc: Number of RX packets truncated and then dropped due to
410  *      lack of descriptors
411  * @n_rx_merge_events: Number of RX merged completion events
412  * @n_rx_merge_packets: Number of RX packets completed by merged events
413  * @rx_pkt_n_frags: Number of fragments in next packet to be delivered by
414  *      __efx_rx_packet(), or zero if there is none
415  * @rx_pkt_index: Ring index of first buffer for next packet to be delivered
416  *      by __efx_rx_packet(), if @rx_pkt_n_frags != 0
417  * @rx_queue: RX queue for this channel
418  * @tx_queue: TX queues for this channel
419  * @sync_events_state: Current state of sync events on this channel
420  * @sync_timestamp_major: Major part of the last ptp sync event
421  * @sync_timestamp_minor: Minor part of the last ptp sync event
422  */
423 struct efx_channel {
424         struct efx_nic *efx;
425         int channel;
426         const struct efx_channel_type *type;
427         bool eventq_init;
428         bool enabled;
429         int irq;
430         unsigned int irq_moderation;
431         struct net_device *napi_dev;
432         struct napi_struct napi_str;
433 #ifdef CONFIG_NET_RX_BUSY_POLL
434         unsigned long busy_poll_state;
435 #endif
436         struct efx_special_buffer eventq;
437         unsigned int eventq_mask;
438         unsigned int eventq_read_ptr;
439         int event_test_cpu;
440
441         unsigned int irq_count;
442         unsigned int irq_mod_score;
443 #ifdef CONFIG_RFS_ACCEL
444         unsigned int rfs_filters_added;
445 #endif
446
447         unsigned n_rx_tobe_disc;
448         unsigned n_rx_ip_hdr_chksum_err;
449         unsigned n_rx_tcp_udp_chksum_err;
450         unsigned n_rx_mcast_mismatch;
451         unsigned n_rx_frm_trunc;
452         unsigned n_rx_overlength;
453         unsigned n_skbuff_leaks;
454         unsigned int n_rx_nodesc_trunc;
455         unsigned int n_rx_merge_events;
456         unsigned int n_rx_merge_packets;
457
458         unsigned int rx_pkt_n_frags;
459         unsigned int rx_pkt_index;
460
461         struct efx_rx_queue rx_queue;
462         struct efx_tx_queue tx_queue[EFX_TXQ_TYPES];
463
464         enum efx_sync_events_state sync_events_state;
465         u32 sync_timestamp_major;
466         u32 sync_timestamp_minor;
467 };
468
469 #ifdef CONFIG_NET_RX_BUSY_POLL
470 enum efx_channel_busy_poll_state {
471         EFX_CHANNEL_STATE_IDLE = 0,
472         EFX_CHANNEL_STATE_NAPI = BIT(0),
473         EFX_CHANNEL_STATE_NAPI_REQ_BIT = 1,
474         EFX_CHANNEL_STATE_NAPI_REQ = BIT(1),
475         EFX_CHANNEL_STATE_POLL_BIT = 2,
476         EFX_CHANNEL_STATE_POLL = BIT(2),
477         EFX_CHANNEL_STATE_DISABLE_BIT = 3,
478 };
479
480 static inline void efx_channel_busy_poll_init(struct efx_channel *channel)
481 {
482         WRITE_ONCE(channel->busy_poll_state, EFX_CHANNEL_STATE_IDLE);
483 }
484
485 /* Called from the device poll routine to get ownership of a channel. */
486 static inline bool efx_channel_lock_napi(struct efx_channel *channel)
487 {
488         unsigned long prev, old = READ_ONCE(channel->busy_poll_state);
489
490         while (1) {
491                 switch (old) {
492                 case EFX_CHANNEL_STATE_POLL:
493                         /* Ensure efx_channel_try_lock_poll() wont starve us */
494                         set_bit(EFX_CHANNEL_STATE_NAPI_REQ_BIT,
495                                 &channel->busy_poll_state);
496                         /* fallthrough */
497                 case EFX_CHANNEL_STATE_POLL | EFX_CHANNEL_STATE_NAPI_REQ:
498                         return false;
499                 default:
500                         break;
501                 }
502                 prev = cmpxchg(&channel->busy_poll_state, old,
503                                EFX_CHANNEL_STATE_NAPI);
504                 if (unlikely(prev != old)) {
505                         /* This is likely to mean we've just entered polling
506                          * state. Go back round to set the REQ bit.
507                          */
508                         old = prev;
509                         continue;
510                 }
511                 return true;
512         }
513 }
514
515 static inline void efx_channel_unlock_napi(struct efx_channel *channel)
516 {
517         /* Make sure write has completed from efx_channel_lock_napi() */
518         smp_wmb();
519         WRITE_ONCE(channel->busy_poll_state, EFX_CHANNEL_STATE_IDLE);
520 }
521
522 /* Called from efx_busy_poll(). */
523 static inline bool efx_channel_try_lock_poll(struct efx_channel *channel)
524 {
525         return cmpxchg(&channel->busy_poll_state, EFX_CHANNEL_STATE_IDLE,
526                         EFX_CHANNEL_STATE_POLL) == EFX_CHANNEL_STATE_IDLE;
527 }
528
529 static inline void efx_channel_unlock_poll(struct efx_channel *channel)
530 {
531         clear_bit_unlock(EFX_CHANNEL_STATE_POLL_BIT, &channel->busy_poll_state);
532 }
533
534 static inline bool efx_channel_busy_polling(struct efx_channel *channel)
535 {
536         return test_bit(EFX_CHANNEL_STATE_POLL_BIT, &channel->busy_poll_state);
537 }
538
539 static inline void efx_channel_enable(struct efx_channel *channel)
540 {
541         clear_bit_unlock(EFX_CHANNEL_STATE_DISABLE_BIT,
542                          &channel->busy_poll_state);
543 }
544
545 /* Stop further polling or napi access.
546  * Returns false if the channel is currently busy polling.
547  */
548 static inline bool efx_channel_disable(struct efx_channel *channel)
549 {
550         set_bit(EFX_CHANNEL_STATE_DISABLE_BIT, &channel->busy_poll_state);
551         /* Implicit barrier in efx_channel_busy_polling() */
552         return !efx_channel_busy_polling(channel);
553 }
554
555 #else /* CONFIG_NET_RX_BUSY_POLL */
556
557 static inline void efx_channel_busy_poll_init(struct efx_channel *channel)
558 {
559 }
560
561 static inline bool efx_channel_lock_napi(struct efx_channel *channel)
562 {
563         return true;
564 }
565
566 static inline void efx_channel_unlock_napi(struct efx_channel *channel)
567 {
568 }
569
570 static inline bool efx_channel_try_lock_poll(struct efx_channel *channel)
571 {
572         return false;
573 }
574
575 static inline void efx_channel_unlock_poll(struct efx_channel *channel)
576 {
577 }
578
579 static inline bool efx_channel_busy_polling(struct efx_channel *channel)
580 {
581         return false;
582 }
583
584 static inline void efx_channel_enable(struct efx_channel *channel)
585 {
586 }
587
588 static inline bool efx_channel_disable(struct efx_channel *channel)
589 {
590         return true;
591 }
592 #endif /* CONFIG_NET_RX_BUSY_POLL */
593
594 /**
595  * struct efx_msi_context - Context for each MSI
596  * @efx: The associated NIC
597  * @index: Index of the channel/IRQ
598  * @name: Name of the channel/IRQ
599  *
600  * Unlike &struct efx_channel, this is never reallocated and is always
601  * safe for the IRQ handler to access.
602  */
603 struct efx_msi_context {
604         struct efx_nic *efx;
605         unsigned int index;
606         char name[IFNAMSIZ + 6];
607 };
608
609 /**
610  * struct efx_channel_type - distinguishes traffic and extra channels
611  * @handle_no_channel: Handle failure to allocate an extra channel
612  * @pre_probe: Set up extra state prior to initialisation
613  * @post_remove: Tear down extra state after finalisation, if allocated.
614  *      May be called on channels that have not been probed.
615  * @get_name: Generate the channel's name (used for its IRQ handler)
616  * @copy: Copy the channel state prior to reallocation.  May be %NULL if
617  *      reallocation is not supported.
618  * @receive_skb: Handle an skb ready to be passed to netif_receive_skb()
619  * @keep_eventq: Flag for whether event queue should be kept initialised
620  *      while the device is stopped
621  */
622 struct efx_channel_type {
623         void (*handle_no_channel)(struct efx_nic *);
624         int (*pre_probe)(struct efx_channel *);
625         void (*post_remove)(struct efx_channel *);
626         void (*get_name)(struct efx_channel *, char *buf, size_t len);
627         struct efx_channel *(*copy)(const struct efx_channel *);
628         bool (*receive_skb)(struct efx_channel *, struct sk_buff *);
629         bool keep_eventq;
630 };
631
632 enum efx_led_mode {
633         EFX_LED_OFF     = 0,
634         EFX_LED_ON      = 1,
635         EFX_LED_DEFAULT = 2
636 };
637
638 #define STRING_TABLE_LOOKUP(val, member) \
639         ((val) < member ## _max) ? member ## _names[val] : "(invalid)"
640
641 extern const char *const efx_loopback_mode_names[];
642 extern const unsigned int efx_loopback_mode_max;
643 #define LOOPBACK_MODE(efx) \
644         STRING_TABLE_LOOKUP((efx)->loopback_mode, efx_loopback_mode)
645
646 extern const char *const efx_reset_type_names[];
647 extern const unsigned int efx_reset_type_max;
648 #define RESET_TYPE(type) \
649         STRING_TABLE_LOOKUP(type, efx_reset_type)
650
651 enum efx_int_mode {
652         /* Be careful if altering to correct macro below */
653         EFX_INT_MODE_MSIX = 0,
654         EFX_INT_MODE_MSI = 1,
655         EFX_INT_MODE_LEGACY = 2,
656         EFX_INT_MODE_MAX        /* Insert any new items before this */
657 };
658 #define EFX_INT_MODE_USE_MSI(x) (((x)->interrupt_mode) <= EFX_INT_MODE_MSI)
659
660 enum nic_state {
661         STATE_UNINIT = 0,       /* device being probed/removed or is frozen */
662         STATE_READY = 1,        /* hardware ready and netdev registered */
663         STATE_DISABLED = 2,     /* device disabled due to hardware errors */
664         STATE_RECOVERY = 3,     /* device recovering from PCI error */
665 };
666
667 /* Forward declaration */
668 struct efx_nic;
669
670 /* Pseudo bit-mask flow control field */
671 #define EFX_FC_RX       FLOW_CTRL_RX
672 #define EFX_FC_TX       FLOW_CTRL_TX
673 #define EFX_FC_AUTO     4
674
675 /**
676  * struct efx_link_state - Current state of the link
677  * @up: Link is up
678  * @fd: Link is full-duplex
679  * @fc: Actual flow control flags
680  * @speed: Link speed (Mbps)
681  */
682 struct efx_link_state {
683         bool up;
684         bool fd;
685         u8 fc;
686         unsigned int speed;
687 };
688
689 static inline bool efx_link_state_equal(const struct efx_link_state *left,
690                                         const struct efx_link_state *right)
691 {
692         return left->up == right->up && left->fd == right->fd &&
693                 left->fc == right->fc && left->speed == right->speed;
694 }
695
696 /**
697  * struct efx_phy_operations - Efx PHY operations table
698  * @probe: Probe PHY and initialise efx->mdio.mode_support, efx->mdio.mmds,
699  *      efx->loopback_modes.
700  * @init: Initialise PHY
701  * @fini: Shut down PHY
702  * @reconfigure: Reconfigure PHY (e.g. for new link parameters)
703  * @poll: Update @link_state and report whether it changed.
704  *      Serialised by the mac_lock.
705  * @get_settings: Get ethtool settings. Serialised by the mac_lock.
706  * @set_settings: Set ethtool settings. Serialised by the mac_lock.
707  * @set_npage_adv: Set abilities advertised in (Extended) Next Page
708  *      (only needed where AN bit is set in mmds)
709  * @test_alive: Test that PHY is 'alive' (online)
710  * @test_name: Get the name of a PHY-specific test/result
711  * @run_tests: Run tests and record results as appropriate (offline).
712  *      Flags are the ethtool tests flags.
713  */
714 struct efx_phy_operations {
715         int (*probe) (struct efx_nic *efx);
716         int (*init) (struct efx_nic *efx);
717         void (*fini) (struct efx_nic *efx);
718         void (*remove) (struct efx_nic *efx);
719         int (*reconfigure) (struct efx_nic *efx);
720         bool (*poll) (struct efx_nic *efx);
721         void (*get_settings) (struct efx_nic *efx,
722                               struct ethtool_cmd *ecmd);
723         int (*set_settings) (struct efx_nic *efx,
724                              struct ethtool_cmd *ecmd);
725         void (*set_npage_adv) (struct efx_nic *efx, u32);
726         int (*test_alive) (struct efx_nic *efx);
727         const char *(*test_name) (struct efx_nic *efx, unsigned int index);
728         int (*run_tests) (struct efx_nic *efx, int *results, unsigned flags);
729         int (*get_module_eeprom) (struct efx_nic *efx,
730                                struct ethtool_eeprom *ee,
731                                u8 *data);
732         int (*get_module_info) (struct efx_nic *efx,
733                                 struct ethtool_modinfo *modinfo);
734 };
735
736 /**
737  * enum efx_phy_mode - PHY operating mode flags
738  * @PHY_MODE_NORMAL: on and should pass traffic
739  * @PHY_MODE_TX_DISABLED: on with TX disabled
740  * @PHY_MODE_LOW_POWER: set to low power through MDIO
741  * @PHY_MODE_OFF: switched off through external control
742  * @PHY_MODE_SPECIAL: on but will not pass traffic
743  */
744 enum efx_phy_mode {
745         PHY_MODE_NORMAL         = 0,
746         PHY_MODE_TX_DISABLED    = 1,
747         PHY_MODE_LOW_POWER      = 2,
748         PHY_MODE_OFF            = 4,
749         PHY_MODE_SPECIAL        = 8,
750 };
751
752 static inline bool efx_phy_mode_disabled(enum efx_phy_mode mode)
753 {
754         return !!(mode & ~PHY_MODE_TX_DISABLED);
755 }
756
757 /**
758  * struct efx_hw_stat_desc - Description of a hardware statistic
759  * @name: Name of the statistic as visible through ethtool, or %NULL if
760  *      it should not be exposed
761  * @dma_width: Width in bits (0 for non-DMA statistics)
762  * @offset: Offset within stats (ignored for non-DMA statistics)
763  */
764 struct efx_hw_stat_desc {
765         const char *name;
766         u16 dma_width;
767         u16 offset;
768 };
769
770 /* Number of bits used in a multicast filter hash address */
771 #define EFX_MCAST_HASH_BITS 8
772
773 /* Number of (single-bit) entries in a multicast filter hash */
774 #define EFX_MCAST_HASH_ENTRIES (1 << EFX_MCAST_HASH_BITS)
775
776 /* An Efx multicast filter hash */
777 union efx_multicast_hash {
778         u8 byte[EFX_MCAST_HASH_ENTRIES / 8];
779         efx_oword_t oword[EFX_MCAST_HASH_ENTRIES / sizeof(efx_oword_t) / 8];
780 };
781
782 struct vfdi_status;
783
784 /**
785  * struct efx_nic - an Efx NIC
786  * @name: Device name (net device name or bus id before net device registered)
787  * @pci_dev: The PCI device
788  * @node: List node for maintaning primary/secondary function lists
789  * @primary: &struct efx_nic instance for the primary function of this
790  *      controller.  May be the same structure, and may be %NULL if no
791  *      primary function is bound.  Serialised by rtnl_lock.
792  * @secondary_list: List of &struct efx_nic instances for the secondary PCI
793  *      functions of the controller, if this is for the primary function.
794  *      Serialised by rtnl_lock.
795  * @type: Controller type attributes
796  * @legacy_irq: IRQ number
797  * @workqueue: Workqueue for port reconfigures and the HW monitor.
798  *      Work items do not hold and must not acquire RTNL.
799  * @workqueue_name: Name of workqueue
800  * @reset_work: Scheduled reset workitem
801  * @membase_phys: Memory BAR value as physical address
802  * @membase: Memory BAR value
803  * @interrupt_mode: Interrupt mode
804  * @timer_quantum_ns: Interrupt timer quantum, in nanoseconds
805  * @irq_rx_adaptive: Adaptive IRQ moderation enabled for RX event queues
806  * @irq_rx_moderation: IRQ moderation time for RX event queues
807  * @msg_enable: Log message enable flags
808  * @state: Device state number (%STATE_*). Serialised by the rtnl_lock.
809  * @reset_pending: Bitmask for pending resets
810  * @tx_queue: TX DMA queues
811  * @rx_queue: RX DMA queues
812  * @channel: Channels
813  * @msi_context: Context for each MSI
814  * @extra_channel_types: Types of extra (non-traffic) channels that
815  *      should be allocated for this NIC
816  * @rxq_entries: Size of receive queues requested by user.
817  * @txq_entries: Size of transmit queues requested by user.
818  * @txq_stop_thresh: TX queue fill level at or above which we stop it.
819  * @txq_wake_thresh: TX queue fill level at or below which we wake it.
820  * @tx_dc_base: Base qword address in SRAM of TX queue descriptor caches
821  * @rx_dc_base: Base qword address in SRAM of RX queue descriptor caches
822  * @sram_lim_qw: Qword address limit of SRAM
823  * @next_buffer_table: First available buffer table id
824  * @n_channels: Number of channels in use
825  * @n_rx_channels: Number of channels used for RX (= number of RX queues)
826  * @n_tx_channels: Number of channels used for TX
827  * @rx_ip_align: RX DMA address offset to have IP header aligned in
828  *      in accordance with NET_IP_ALIGN
829  * @rx_dma_len: Current maximum RX DMA length
830  * @rx_buffer_order: Order (log2) of number of pages for each RX buffer
831  * @rx_buffer_truesize: Amortised allocation size of an RX buffer,
832  *      for use in sk_buff::truesize
833  * @rx_prefix_size: Size of RX prefix before packet data
834  * @rx_packet_hash_offset: Offset of RX flow hash from start of packet data
835  *      (valid only if @rx_prefix_size != 0; always negative)
836  * @rx_packet_len_offset: Offset of RX packet length from start of packet data
837  *      (valid only for NICs that set %EFX_RX_PKT_PREFIX_LEN; always negative)
838  * @rx_packet_ts_offset: Offset of timestamp from start of packet data
839  *      (valid only if channel->sync_timestamps_enabled; always negative)
840  * @rx_hash_key: Toeplitz hash key for RSS
841  * @rx_indir_table: Indirection table for RSS
842  * @rx_scatter: Scatter mode enabled for receives
843  * @int_error_count: Number of internal errors seen recently
844  * @int_error_expire: Time at which error count will be expired
845  * @irq_soft_enabled: Are IRQs soft-enabled? If not, IRQ handler will
846  *      acknowledge but do nothing else.
847  * @irq_status: Interrupt status buffer
848  * @irq_zero_count: Number of legacy IRQs seen with queue flags == 0
849  * @irq_level: IRQ level/index for IRQs not triggered by an event queue
850  * @selftest_work: Work item for asynchronous self-test
851  * @mtd_list: List of MTDs attached to the NIC
852  * @nic_data: Hardware dependent state
853  * @mcdi: Management-Controller-to-Driver Interface state
854  * @mac_lock: MAC access lock. Protects @port_enabled, @phy_mode,
855  *      efx_monitor() and efx_reconfigure_port()
856  * @port_enabled: Port enabled indicator.
857  *      Serialises efx_stop_all(), efx_start_all(), efx_monitor() and
858  *      efx_mac_work() with kernel interfaces. Safe to read under any
859  *      one of the rtnl_lock, mac_lock, or netif_tx_lock, but all three must
860  *      be held to modify it.
861  * @port_initialized: Port initialized?
862  * @net_dev: Operating system network device. Consider holding the rtnl lock
863  * @stats_buffer: DMA buffer for statistics
864  * @phy_type: PHY type
865  * @phy_op: PHY interface
866  * @phy_data: PHY private data (including PHY-specific stats)
867  * @mdio: PHY MDIO interface
868  * @mdio_bus: PHY MDIO bus ID (only used by Siena)
869  * @phy_mode: PHY operating mode. Serialised by @mac_lock.
870  * @link_advertising: Autonegotiation advertising flags
871  * @link_state: Current state of the link
872  * @n_link_state_changes: Number of times the link has changed state
873  * @unicast_filter: Flag for Falcon-arch simple unicast filter.
874  *      Protected by @mac_lock.
875  * @multicast_hash: Multicast hash table for Falcon-arch.
876  *      Protected by @mac_lock.
877  * @wanted_fc: Wanted flow control flags
878  * @fc_disable: When non-zero flow control is disabled. Typically used to
879  *      ensure that network back pressure doesn't delay dma queue flushes.
880  *      Serialised by the rtnl lock.
881  * @mac_work: Work item for changing MAC promiscuity and multicast hash
882  * @loopback_mode: Loopback status
883  * @loopback_modes: Supported loopback mode bitmask
884  * @loopback_selftest: Offline self-test private state
885  * @filter_sem: Filter table rw_semaphore, for freeing the table
886  * @filter_lock: Filter table lock, for mere content changes
887  * @filter_state: Architecture-dependent filter table state
888  * @rps_flow_id: Flow IDs of filters allocated for accelerated RFS,
889  *      indexed by filter ID
890  * @rps_expire_index: Next index to check for expiry in @rps_flow_id
891  * @active_queues: Count of RX and TX queues that haven't been flushed and drained.
892  * @rxq_flush_pending: Count of number of receive queues that need to be flushed.
893  *      Decremented when the efx_flush_rx_queue() is called.
894  * @rxq_flush_outstanding: Count of number of RX flushes started but not yet
895  *      completed (either success or failure). Not used when MCDI is used to
896  *      flush receive queues.
897  * @flush_wq: wait queue used by efx_nic_flush_queues() to wait for flush completions.
898  * @vf_count: Number of VFs intended to be enabled.
899  * @vf_init_count: Number of VFs that have been fully initialised.
900  * @vi_scale: log2 number of vnics per VF.
901  * @ptp_data: PTP state data
902  * @vpd_sn: Serial number read from VPD
903  * @monitor_work: Hardware monitor workitem
904  * @biu_lock: BIU (bus interface unit) lock
905  * @last_irq_cpu: Last CPU to handle a possible test interrupt.  This
906  *      field is used by efx_test_interrupts() to verify that an
907  *      interrupt has occurred.
908  * @stats_lock: Statistics update lock. Must be held when calling
909  *      efx_nic_type::{update,start,stop}_stats.
910  * @n_rx_noskb_drops: Count of RX packets dropped due to failure to allocate an skb
911  * @mc_promisc: Whether in multicast promiscuous mode when last changed
912  *
913  * This is stored in the private area of the &struct net_device.
914  */
915 struct efx_nic {
916         /* The following fields should be written very rarely */
917
918         char name[IFNAMSIZ];
919         struct list_head node;
920         struct efx_nic *primary;
921         struct list_head secondary_list;
922         struct pci_dev *pci_dev;
923         unsigned int port_num;
924         const struct efx_nic_type *type;
925         int legacy_irq;
926         bool eeh_disabled_legacy_irq;
927         struct workqueue_struct *workqueue;
928         char workqueue_name[16];
929         struct work_struct reset_work;
930         resource_size_t membase_phys;
931         void __iomem *membase;
932
933         enum efx_int_mode interrupt_mode;
934         unsigned int timer_quantum_ns;
935         bool irq_rx_adaptive;
936         unsigned int irq_rx_moderation;
937         u32 msg_enable;
938
939         enum nic_state state;
940         unsigned long reset_pending;
941
942         struct efx_channel *channel[EFX_MAX_CHANNELS];
943         struct efx_msi_context msi_context[EFX_MAX_CHANNELS];
944         const struct efx_channel_type *
945         extra_channel_type[EFX_MAX_EXTRA_CHANNELS];
946
947         unsigned rxq_entries;
948         unsigned txq_entries;
949         unsigned int txq_stop_thresh;
950         unsigned int txq_wake_thresh;
951
952         unsigned tx_dc_base;
953         unsigned rx_dc_base;
954         unsigned sram_lim_qw;
955         unsigned next_buffer_table;
956
957         unsigned int max_channels;
958         unsigned int max_tx_channels;
959         unsigned n_channels;
960         unsigned n_rx_channels;
961         unsigned rss_spread;
962         unsigned tx_channel_offset;
963         unsigned n_tx_channels;
964         unsigned int rx_ip_align;
965         unsigned int rx_dma_len;
966         unsigned int rx_buffer_order;
967         unsigned int rx_buffer_truesize;
968         unsigned int rx_page_buf_step;
969         unsigned int rx_bufs_per_page;
970         unsigned int rx_pages_per_batch;
971         unsigned int rx_prefix_size;
972         int rx_packet_hash_offset;
973         int rx_packet_len_offset;
974         int rx_packet_ts_offset;
975         u8 rx_hash_key[40];
976         u32 rx_indir_table[128];
977         bool rx_scatter;
978
979         unsigned int_error_count;
980         unsigned long int_error_expire;
981
982         bool irq_soft_enabled;
983         struct efx_buffer irq_status;
984         unsigned irq_zero_count;
985         unsigned irq_level;
986         struct delayed_work selftest_work;
987
988 #ifdef CONFIG_SFC_MTD
989         struct list_head mtd_list;
990 #endif
991
992         void *nic_data;
993         struct efx_mcdi_data *mcdi;
994
995         struct mutex mac_lock;
996         struct work_struct mac_work;
997         bool port_enabled;
998
999         bool mc_bist_for_other_fn;
1000         bool port_initialized;
1001         struct net_device *net_dev;
1002
1003         struct efx_buffer stats_buffer;
1004         u64 rx_nodesc_drops_total;
1005         u64 rx_nodesc_drops_while_down;
1006         bool rx_nodesc_drops_prev_state;
1007
1008         unsigned int phy_type;
1009         const struct efx_phy_operations *phy_op;
1010         void *phy_data;
1011         struct mdio_if_info mdio;
1012         unsigned int mdio_bus;
1013         enum efx_phy_mode phy_mode;
1014
1015         u32 link_advertising;
1016         struct efx_link_state link_state;
1017         unsigned int n_link_state_changes;
1018
1019         bool unicast_filter;
1020         union efx_multicast_hash multicast_hash;
1021         u8 wanted_fc;
1022         unsigned fc_disable;
1023
1024         atomic_t rx_reset;
1025         enum efx_loopback_mode loopback_mode;
1026         u64 loopback_modes;
1027
1028         void *loopback_selftest;
1029
1030         struct rw_semaphore filter_sem;
1031         spinlock_t filter_lock;
1032         void *filter_state;
1033 #ifdef CONFIG_RFS_ACCEL
1034         u32 *rps_flow_id;
1035         unsigned int rps_expire_index;
1036 #endif
1037
1038         atomic_t active_queues;
1039         atomic_t rxq_flush_pending;
1040         atomic_t rxq_flush_outstanding;
1041         wait_queue_head_t flush_wq;
1042
1043 #ifdef CONFIG_SFC_SRIOV
1044         unsigned vf_count;
1045         unsigned vf_init_count;
1046         unsigned vi_scale;
1047 #endif
1048
1049         struct efx_ptp_data *ptp_data;
1050
1051         char *vpd_sn;
1052
1053         /* The following fields may be written more often */
1054
1055         struct delayed_work monitor_work ____cacheline_aligned_in_smp;
1056         spinlock_t biu_lock;
1057         int last_irq_cpu;
1058         spinlock_t stats_lock;
1059         atomic_t n_rx_noskb_drops;
1060         bool mc_promisc;
1061 };
1062
1063 static inline int efx_dev_registered(struct efx_nic *efx)
1064 {
1065         return efx->net_dev->reg_state == NETREG_REGISTERED;
1066 }
1067
1068 static inline unsigned int efx_port_num(struct efx_nic *efx)
1069 {
1070         return efx->port_num;
1071 }
1072
1073 struct efx_mtd_partition {
1074         struct list_head node;
1075         struct mtd_info mtd;
1076         const char *dev_type_name;
1077         const char *type_name;
1078         char name[IFNAMSIZ + 20];
1079 };
1080
1081 /**
1082  * struct efx_nic_type - Efx device type definition
1083  * @mem_bar: Get the memory BAR
1084  * @mem_map_size: Get memory BAR mapped size
1085  * @probe: Probe the controller
1086  * @remove: Free resources allocated by probe()
1087  * @init: Initialise the controller
1088  * @dimension_resources: Dimension controller resources (buffer table,
1089  *      and VIs once the available interrupt resources are clear)
1090  * @fini: Shut down the controller
1091  * @monitor: Periodic function for polling link state and hardware monitor
1092  * @map_reset_reason: Map ethtool reset reason to a reset method
1093  * @map_reset_flags: Map ethtool reset flags to a reset method, if possible
1094  * @reset: Reset the controller hardware and possibly the PHY.  This will
1095  *      be called while the controller is uninitialised.
1096  * @probe_port: Probe the MAC and PHY
1097  * @remove_port: Free resources allocated by probe_port()
1098  * @handle_global_event: Handle a "global" event (may be %NULL)
1099  * @fini_dmaq: Flush and finalise DMA queues (RX and TX queues)
1100  * @prepare_flush: Prepare the hardware for flushing the DMA queues
1101  *      (for Falcon architecture)
1102  * @finish_flush: Clean up after flushing the DMA queues (for Falcon
1103  *      architecture)
1104  * @prepare_flr: Prepare for an FLR
1105  * @finish_flr: Clean up after an FLR
1106  * @describe_stats: Describe statistics for ethtool
1107  * @update_stats: Update statistics not provided by event handling.
1108  *      Either argument may be %NULL.
1109  * @start_stats: Start the regular fetching of statistics
1110  * @pull_stats: Pull stats from the NIC and wait until they arrive.
1111  * @stop_stats: Stop the regular fetching of statistics
1112  * @set_id_led: Set state of identifying LED or revert to automatic function
1113  * @push_irq_moderation: Apply interrupt moderation value
1114  * @reconfigure_port: Push loopback/power/txdis changes to the MAC and PHY
1115  * @prepare_enable_fc_tx: Prepare MAC to enable pause frame TX (may be %NULL)
1116  * @reconfigure_mac: Push MAC address, MTU, flow control and filter settings
1117  *      to the hardware.  Serialised by the mac_lock.
1118  * @check_mac_fault: Check MAC fault state. True if fault present.
1119  * @get_wol: Get WoL configuration from driver state
1120  * @set_wol: Push WoL configuration to the NIC
1121  * @resume_wol: Synchronise WoL state between driver and MC (e.g. after resume)
1122  * @test_chip: Test registers.  May use efx_farch_test_registers(), and is
1123  *      expected to reset the NIC.
1124  * @test_nvram: Test validity of NVRAM contents
1125  * @mcdi_request: Send an MCDI request with the given header and SDU.
1126  *      The SDU length may be any value from 0 up to the protocol-
1127  *      defined maximum, but its buffer will be padded to a multiple
1128  *      of 4 bytes.
1129  * @mcdi_poll_response: Test whether an MCDI response is available.
1130  * @mcdi_read_response: Read the MCDI response PDU.  The offset will
1131  *      be a multiple of 4.  The length may not be, but the buffer
1132  *      will be padded so it is safe to round up.
1133  * @mcdi_poll_reboot: Test whether the MCDI has rebooted.  If so,
1134  *      return an appropriate error code for aborting any current
1135  *      request; otherwise return 0.
1136  * @irq_enable_master: Enable IRQs on the NIC.  Each event queue must
1137  *      be separately enabled after this.
1138  * @irq_test_generate: Generate a test IRQ
1139  * @irq_disable_non_ev: Disable non-event IRQs on the NIC.  Each event
1140  *      queue must be separately disabled before this.
1141  * @irq_handle_msi: Handle MSI for a channel.  The @dev_id argument is
1142  *      a pointer to the &struct efx_msi_context for the channel.
1143  * @irq_handle_legacy: Handle legacy interrupt.  The @dev_id argument
1144  *      is a pointer to the &struct efx_nic.
1145  * @tx_probe: Allocate resources for TX queue
1146  * @tx_init: Initialise TX queue on the NIC
1147  * @tx_remove: Free resources for TX queue
1148  * @tx_write: Write TX descriptors and doorbell
1149  * @rx_push_rss_config: Write RSS hash key and indirection table to the NIC
1150  * @rx_probe: Allocate resources for RX queue
1151  * @rx_init: Initialise RX queue on the NIC
1152  * @rx_remove: Free resources for RX queue
1153  * @rx_write: Write RX descriptors and doorbell
1154  * @rx_defer_refill: Generate a refill reminder event
1155  * @ev_probe: Allocate resources for event queue
1156  * @ev_init: Initialise event queue on the NIC
1157  * @ev_fini: Deinitialise event queue on the NIC
1158  * @ev_remove: Free resources for event queue
1159  * @ev_process: Process events for a queue, up to the given NAPI quota
1160  * @ev_read_ack: Acknowledge read events on a queue, rearming its IRQ
1161  * @ev_test_generate: Generate a test event
1162  * @filter_table_probe: Probe filter capabilities and set up filter software state
1163  * @filter_table_restore: Restore filters removed from hardware
1164  * @filter_table_remove: Remove filters from hardware and tear down software state
1165  * @filter_update_rx_scatter: Update filters after change to rx scatter setting
1166  * @filter_insert: add or replace a filter
1167  * @filter_remove_safe: remove a filter by ID, carefully
1168  * @filter_get_safe: retrieve a filter by ID, carefully
1169  * @filter_clear_rx: Remove all RX filters whose priority is less than or
1170  *      equal to the given priority and is not %EFX_FILTER_PRI_AUTO
1171  * @filter_count_rx_used: Get the number of filters in use at a given priority
1172  * @filter_get_rx_id_limit: Get maximum value of a filter id, plus 1
1173  * @filter_get_rx_ids: Get list of RX filters at a given priority
1174  * @filter_rfs_insert: Add or replace a filter for RFS.  This must be
1175  *      atomic.  The hardware change may be asynchronous but should
1176  *      not be delayed for long.  It may fail if this can't be done
1177  *      atomically.
1178  * @filter_rfs_expire_one: Consider expiring a filter inserted for RFS.
1179  *      This must check whether the specified table entry is used by RFS
1180  *      and that rps_may_expire_flow() returns true for it.
1181  * @mtd_probe: Probe and add MTD partitions associated with this net device,
1182  *       using efx_mtd_add()
1183  * @mtd_rename: Set an MTD partition name using the net device name
1184  * @mtd_read: Read from an MTD partition
1185  * @mtd_erase: Erase part of an MTD partition
1186  * @mtd_write: Write to an MTD partition
1187  * @mtd_sync: Wait for write-back to complete on MTD partition.  This
1188  *      also notifies the driver that a writer has finished using this
1189  *      partition.
1190  * @ptp_write_host_time: Send host time to MC as part of sync protocol
1191  * @ptp_set_ts_sync_events: Enable or disable sync events for inline RX
1192  *      timestamping, possibly only temporarily for the purposes of a reset.
1193  * @ptp_set_ts_config: Set hardware timestamp configuration.  The flags
1194  *      and tx_type will already have been validated but this operation
1195  *      must validate and update rx_filter.
1196  * @set_mac_address: Set the MAC address of the device
1197  * @revision: Hardware architecture revision
1198  * @txd_ptr_tbl_base: TX descriptor ring base address
1199  * @rxd_ptr_tbl_base: RX descriptor ring base address
1200  * @buf_tbl_base: Buffer table base address
1201  * @evq_ptr_tbl_base: Event queue pointer table base address
1202  * @evq_rptr_tbl_base: Event queue read-pointer table base address
1203  * @max_dma_mask: Maximum possible DMA mask
1204  * @rx_prefix_size: Size of RX prefix before packet data
1205  * @rx_hash_offset: Offset of RX flow hash within prefix
1206  * @rx_ts_offset: Offset of timestamp within prefix
1207  * @rx_buffer_padding: Size of padding at end of RX packet
1208  * @can_rx_scatter: NIC is able to scatter packets to multiple buffers
1209  * @always_rx_scatter: NIC will always scatter packets to multiple buffers
1210  * @max_interrupt_mode: Highest capability interrupt mode supported
1211  *      from &enum efx_init_mode.
1212  * @timer_period_max: Maximum period of interrupt timer (in ticks)
1213  * @offload_features: net_device feature flags for protocol offload
1214  *      features implemented in hardware
1215  * @mcdi_max_ver: Maximum MCDI version supported
1216  * @hwtstamp_filters: Mask of hardware timestamp filter types supported
1217  */
1218 struct efx_nic_type {
1219         bool is_vf;
1220         unsigned int mem_bar;
1221         unsigned int (*mem_map_size)(struct efx_nic *efx);
1222         int (*probe)(struct efx_nic *efx);
1223         void (*remove)(struct efx_nic *efx);
1224         int (*init)(struct efx_nic *efx);
1225         int (*dimension_resources)(struct efx_nic *efx);
1226         void (*fini)(struct efx_nic *efx);
1227         void (*monitor)(struct efx_nic *efx);
1228         enum reset_type (*map_reset_reason)(enum reset_type reason);
1229         int (*map_reset_flags)(u32 *flags);
1230         int (*reset)(struct efx_nic *efx, enum reset_type method);
1231         int (*probe_port)(struct efx_nic *efx);
1232         void (*remove_port)(struct efx_nic *efx);
1233         bool (*handle_global_event)(struct efx_channel *channel, efx_qword_t *);
1234         int (*fini_dmaq)(struct efx_nic *efx);
1235         void (*prepare_flush)(struct efx_nic *efx);
1236         void (*finish_flush)(struct efx_nic *efx);
1237         void (*prepare_flr)(struct efx_nic *efx);
1238         void (*finish_flr)(struct efx_nic *efx);
1239         size_t (*describe_stats)(struct efx_nic *efx, u8 *names);
1240         size_t (*update_stats)(struct efx_nic *efx, u64 *full_stats,
1241                                struct rtnl_link_stats64 *core_stats);
1242         void (*start_stats)(struct efx_nic *efx);
1243         void (*pull_stats)(struct efx_nic *efx);
1244         void (*stop_stats)(struct efx_nic *efx);
1245         void (*set_id_led)(struct efx_nic *efx, enum efx_led_mode mode);
1246         void (*push_irq_moderation)(struct efx_channel *channel);
1247         int (*reconfigure_port)(struct efx_nic *efx);
1248         void (*prepare_enable_fc_tx)(struct efx_nic *efx);
1249         int (*reconfigure_mac)(struct efx_nic *efx);
1250         bool (*check_mac_fault)(struct efx_nic *efx);
1251         void (*get_wol)(struct efx_nic *efx, struct ethtool_wolinfo *wol);
1252         int (*set_wol)(struct efx_nic *efx, u32 type);
1253         void (*resume_wol)(struct efx_nic *efx);
1254         int (*test_chip)(struct efx_nic *efx, struct efx_self_tests *tests);
1255         int (*test_nvram)(struct efx_nic *efx);
1256         void (*mcdi_request)(struct efx_nic *efx,
1257                              const efx_dword_t *hdr, size_t hdr_len,
1258                              const efx_dword_t *sdu, size_t sdu_len);
1259         bool (*mcdi_poll_response)(struct efx_nic *efx);
1260         void (*mcdi_read_response)(struct efx_nic *efx, efx_dword_t *pdu,
1261                                    size_t pdu_offset, size_t pdu_len);
1262         int (*mcdi_poll_reboot)(struct efx_nic *efx);
1263         void (*mcdi_reboot_detected)(struct efx_nic *efx);
1264         void (*irq_enable_master)(struct efx_nic *efx);
1265         void (*irq_test_generate)(struct efx_nic *efx);
1266         void (*irq_disable_non_ev)(struct efx_nic *efx);
1267         irqreturn_t (*irq_handle_msi)(int irq, void *dev_id);
1268         irqreturn_t (*irq_handle_legacy)(int irq, void *dev_id);
1269         int (*tx_probe)(struct efx_tx_queue *tx_queue);
1270         void (*tx_init)(struct efx_tx_queue *tx_queue);
1271         void (*tx_remove)(struct efx_tx_queue *tx_queue);
1272         void (*tx_write)(struct efx_tx_queue *tx_queue);
1273         int (*rx_push_rss_config)(struct efx_nic *efx, bool user,
1274                                   const u32 *rx_indir_table);
1275         int (*rx_probe)(struct efx_rx_queue *rx_queue);
1276         void (*rx_init)(struct efx_rx_queue *rx_queue);
1277         void (*rx_remove)(struct efx_rx_queue *rx_queue);
1278         void (*rx_write)(struct efx_rx_queue *rx_queue);
1279         void (*rx_defer_refill)(struct efx_rx_queue *rx_queue);
1280         int (*ev_probe)(struct efx_channel *channel);
1281         int (*ev_init)(struct efx_channel *channel);
1282         void (*ev_fini)(struct efx_channel *channel);
1283         void (*ev_remove)(struct efx_channel *channel);
1284         int (*ev_process)(struct efx_channel *channel, int quota);
1285         void (*ev_read_ack)(struct efx_channel *channel);
1286         void (*ev_test_generate)(struct efx_channel *channel);
1287         int (*filter_table_probe)(struct efx_nic *efx);
1288         void (*filter_table_restore)(struct efx_nic *efx);
1289         void (*filter_table_remove)(struct efx_nic *efx);
1290         void (*filter_update_rx_scatter)(struct efx_nic *efx);
1291         s32 (*filter_insert)(struct efx_nic *efx,
1292                              struct efx_filter_spec *spec, bool replace);
1293         int (*filter_remove_safe)(struct efx_nic *efx,
1294                                   enum efx_filter_priority priority,
1295                                   u32 filter_id);
1296         int (*filter_get_safe)(struct efx_nic *efx,
1297                                enum efx_filter_priority priority,
1298                                u32 filter_id, struct efx_filter_spec *);
1299         int (*filter_clear_rx)(struct efx_nic *efx,
1300                                enum efx_filter_priority priority);
1301         u32 (*filter_count_rx_used)(struct efx_nic *efx,
1302                                     enum efx_filter_priority priority);
1303         u32 (*filter_get_rx_id_limit)(struct efx_nic *efx);
1304         s32 (*filter_get_rx_ids)(struct efx_nic *efx,
1305                                  enum efx_filter_priority priority,
1306                                  u32 *buf, u32 size);
1307 #ifdef CONFIG_RFS_ACCEL
1308         s32 (*filter_rfs_insert)(struct efx_nic *efx,
1309                                  struct efx_filter_spec *spec);
1310         bool (*filter_rfs_expire_one)(struct efx_nic *efx, u32 flow_id,
1311                                       unsigned int index);
1312 #endif
1313 #ifdef CONFIG_SFC_MTD
1314         int (*mtd_probe)(struct efx_nic *efx);
1315         void (*mtd_rename)(struct efx_mtd_partition *part);
1316         int (*mtd_read)(struct mtd_info *mtd, loff_t start, size_t len,
1317                         size_t *retlen, u8 *buffer);
1318         int (*mtd_erase)(struct mtd_info *mtd, loff_t start, size_t len);
1319         int (*mtd_write)(struct mtd_info *mtd, loff_t start, size_t len,
1320                          size_t *retlen, const u8 *buffer);
1321         int (*mtd_sync)(struct mtd_info *mtd);
1322 #endif
1323         void (*ptp_write_host_time)(struct efx_nic *efx, u32 host_time);
1324         int (*ptp_set_ts_sync_events)(struct efx_nic *efx, bool en, bool temp);
1325         int (*ptp_set_ts_config)(struct efx_nic *efx,
1326                                  struct hwtstamp_config *init);
1327         int (*sriov_configure)(struct efx_nic *efx, int num_vfs);
1328         int (*sriov_init)(struct efx_nic *efx);
1329         void (*sriov_fini)(struct efx_nic *efx);
1330         bool (*sriov_wanted)(struct efx_nic *efx);
1331         void (*sriov_reset)(struct efx_nic *efx);
1332         void (*sriov_flr)(struct efx_nic *efx, unsigned vf_i);
1333         int (*sriov_set_vf_mac)(struct efx_nic *efx, int vf_i, u8 *mac);
1334         int (*sriov_set_vf_vlan)(struct efx_nic *efx, int vf_i, u16 vlan,
1335                                  u8 qos);
1336         int (*sriov_set_vf_spoofchk)(struct efx_nic *efx, int vf_i,
1337                                      bool spoofchk);
1338         int (*sriov_get_vf_config)(struct efx_nic *efx, int vf_i,
1339                                    struct ifla_vf_info *ivi);
1340         int (*sriov_set_vf_link_state)(struct efx_nic *efx, int vf_i,
1341                                        int link_state);
1342         int (*sriov_get_phys_port_id)(struct efx_nic *efx,
1343                                       struct netdev_phys_item_id *ppid);
1344         int (*vswitching_probe)(struct efx_nic *efx);
1345         int (*vswitching_restore)(struct efx_nic *efx);
1346         void (*vswitching_remove)(struct efx_nic *efx);
1347         int (*get_mac_address)(struct efx_nic *efx, unsigned char *perm_addr);
1348         int (*set_mac_address)(struct efx_nic *efx);
1349
1350         int revision;
1351         unsigned int txd_ptr_tbl_base;
1352         unsigned int rxd_ptr_tbl_base;
1353         unsigned int buf_tbl_base;
1354         unsigned int evq_ptr_tbl_base;
1355         unsigned int evq_rptr_tbl_base;
1356         u64 max_dma_mask;
1357         unsigned int rx_prefix_size;
1358         unsigned int rx_hash_offset;
1359         unsigned int rx_ts_offset;
1360         unsigned int rx_buffer_padding;
1361         bool can_rx_scatter;
1362         bool always_rx_scatter;
1363         unsigned int max_interrupt_mode;
1364         unsigned int timer_period_max;
1365         netdev_features_t offload_features;
1366         int mcdi_max_ver;
1367         unsigned int max_rx_ip_filters;
1368         u32 hwtstamp_filters;
1369 };
1370
1371 /**************************************************************************
1372  *
1373  * Prototypes and inline functions
1374  *
1375  *************************************************************************/
1376
1377 static inline struct efx_channel *
1378 efx_get_channel(struct efx_nic *efx, unsigned index)
1379 {
1380         EFX_BUG_ON_PARANOID(index >= efx->n_channels);
1381         return efx->channel[index];
1382 }
1383
1384 /* Iterate over all used channels */
1385 #define efx_for_each_channel(_channel, _efx)                            \
1386         for (_channel = (_efx)->channel[0];                             \
1387              _channel;                                                  \
1388              _channel = (_channel->channel + 1 < (_efx)->n_channels) ?  \
1389                      (_efx)->channel[_channel->channel + 1] : NULL)
1390
1391 /* Iterate over all used channels in reverse */
1392 #define efx_for_each_channel_rev(_channel, _efx)                        \
1393         for (_channel = (_efx)->channel[(_efx)->n_channels - 1];        \
1394              _channel;                                                  \
1395              _channel = _channel->channel ?                             \
1396                      (_efx)->channel[_channel->channel - 1] : NULL)
1397
1398 static inline struct efx_tx_queue *
1399 efx_get_tx_queue(struct efx_nic *efx, unsigned index, unsigned type)
1400 {
1401         EFX_BUG_ON_PARANOID(index >= efx->n_tx_channels ||
1402                             type >= EFX_TXQ_TYPES);
1403         return &efx->channel[efx->tx_channel_offset + index]->tx_queue[type];
1404 }
1405
1406 static inline bool efx_channel_has_tx_queues(struct efx_channel *channel)
1407 {
1408         return channel->channel - channel->efx->tx_channel_offset <
1409                 channel->efx->n_tx_channels;
1410 }
1411
1412 static inline struct efx_tx_queue *
1413 efx_channel_get_tx_queue(struct efx_channel *channel, unsigned type)
1414 {
1415         EFX_BUG_ON_PARANOID(!efx_channel_has_tx_queues(channel) ||
1416                             type >= EFX_TXQ_TYPES);
1417         return &channel->tx_queue[type];
1418 }
1419
1420 static inline bool efx_tx_queue_used(struct efx_tx_queue *tx_queue)
1421 {
1422         return !(tx_queue->efx->net_dev->num_tc < 2 &&
1423                  tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI);
1424 }
1425
1426 /* Iterate over all TX queues belonging to a channel */
1427 #define efx_for_each_channel_tx_queue(_tx_queue, _channel)              \
1428         if (!efx_channel_has_tx_queues(_channel))                       \
1429                 ;                                                       \
1430         else                                                            \
1431                 for (_tx_queue = (_channel)->tx_queue;                  \
1432                      _tx_queue < (_channel)->tx_queue + EFX_TXQ_TYPES && \
1433                              efx_tx_queue_used(_tx_queue);              \
1434                      _tx_queue++)
1435
1436 /* Iterate over all possible TX queues belonging to a channel */
1437 #define efx_for_each_possible_channel_tx_queue(_tx_queue, _channel)     \
1438         if (!efx_channel_has_tx_queues(_channel))                       \
1439                 ;                                                       \
1440         else                                                            \
1441                 for (_tx_queue = (_channel)->tx_queue;                  \
1442                      _tx_queue < (_channel)->tx_queue + EFX_TXQ_TYPES;  \
1443                      _tx_queue++)
1444
1445 static inline bool efx_channel_has_rx_queue(struct efx_channel *channel)
1446 {
1447         return channel->rx_queue.core_index >= 0;
1448 }
1449
1450 static inline struct efx_rx_queue *
1451 efx_channel_get_rx_queue(struct efx_channel *channel)
1452 {
1453         EFX_BUG_ON_PARANOID(!efx_channel_has_rx_queue(channel));
1454         return &channel->rx_queue;
1455 }
1456
1457 /* Iterate over all RX queues belonging to a channel */
1458 #define efx_for_each_channel_rx_queue(_rx_queue, _channel)              \
1459         if (!efx_channel_has_rx_queue(_channel))                        \
1460                 ;                                                       \
1461         else                                                            \
1462                 for (_rx_queue = &(_channel)->rx_queue;                 \
1463                      _rx_queue;                                         \
1464                      _rx_queue = NULL)
1465
1466 static inline struct efx_channel *
1467 efx_rx_queue_channel(struct efx_rx_queue *rx_queue)
1468 {
1469         return container_of(rx_queue, struct efx_channel, rx_queue);
1470 }
1471
1472 static inline int efx_rx_queue_index(struct efx_rx_queue *rx_queue)
1473 {
1474         return efx_rx_queue_channel(rx_queue)->channel;
1475 }
1476
1477 /* Returns a pointer to the specified receive buffer in the RX
1478  * descriptor queue.
1479  */
1480 static inline struct efx_rx_buffer *efx_rx_buffer(struct efx_rx_queue *rx_queue,
1481                                                   unsigned int index)
1482 {
1483         return &rx_queue->buffer[index];
1484 }
1485
1486 /**
1487  * EFX_MAX_FRAME_LEN - calculate maximum frame length
1488  *
1489  * This calculates the maximum frame length that will be used for a
1490  * given MTU.  The frame length will be equal to the MTU plus a
1491  * constant amount of header space and padding.  This is the quantity
1492  * that the net driver will program into the MAC as the maximum frame
1493  * length.
1494  *
1495  * The 10G MAC requires 8-byte alignment on the frame
1496  * length, so we round up to the nearest 8.
1497  *
1498  * Re-clocking by the XGXS on RX can reduce an IPG to 32 bits (half an
1499  * XGMII cycle).  If the frame length reaches the maximum value in the
1500  * same cycle, the XMAC can miss the IPG altogether.  We work around
1501  * this by adding a further 16 bytes.
1502  */
1503 #define EFX_MAX_FRAME_LEN(mtu) \
1504         ((((mtu) + ETH_HLEN + VLAN_HLEN + 4/* FCS */ + 7) & ~7) + 16)
1505
1506 static inline bool efx_xmit_with_hwtstamp(struct sk_buff *skb)
1507 {
1508         return skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP;
1509 }
1510 static inline void efx_xmit_hwtstamp_pending(struct sk_buff *skb)
1511 {
1512         skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1513 }
1514
1515 #endif /* EFX_NET_DRIVER_H */