Merge branch 'dccp' of git://eden-feed.erg.abdn.ac.uk/dccp_exp
[pandora-kernel.git] / drivers / net / sfc / net_driver.h
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2005-2008 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/version.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/if_vlan.h>
21 #include <linux/timer.h>
22 #include <linux/mii.h>
23 #include <linux/list.h>
24 #include <linux/pci.h>
25 #include <linux/device.h>
26 #include <linux/highmem.h>
27 #include <linux/workqueue.h>
28 #include <linux/inet_lro.h>
29 #include <linux/i2c.h>
30
31 #include "enum.h"
32 #include "bitfield.h"
33
34 #define EFX_MAX_LRO_DESCRIPTORS 8
35 #define EFX_MAX_LRO_AGGR MAX_SKB_FRAGS
36
37 /**************************************************************************
38  *
39  * Build definitions
40  *
41  **************************************************************************/
42 #ifndef EFX_DRIVER_NAME
43 #define EFX_DRIVER_NAME "sfc"
44 #endif
45 #define EFX_DRIVER_VERSION      "2.2"
46
47 #ifdef EFX_ENABLE_DEBUG
48 #define EFX_BUG_ON_PARANOID(x) BUG_ON(x)
49 #define EFX_WARN_ON_PARANOID(x) WARN_ON(x)
50 #else
51 #define EFX_BUG_ON_PARANOID(x) do {} while (0)
52 #define EFX_WARN_ON_PARANOID(x) do {} while (0)
53 #endif
54
55 /* Un-rate-limited logging */
56 #define EFX_ERR(efx, fmt, args...) \
57 dev_err(&((efx)->pci_dev->dev), "ERR: %s " fmt, efx_dev_name(efx), ##args)
58
59 #define EFX_INFO(efx, fmt, args...) \
60 dev_info(&((efx)->pci_dev->dev), "INFO: %s " fmt, efx_dev_name(efx), ##args)
61
62 #ifdef EFX_ENABLE_DEBUG
63 #define EFX_LOG(efx, fmt, args...) \
64 dev_info(&((efx)->pci_dev->dev), "DBG: %s " fmt, efx_dev_name(efx), ##args)
65 #else
66 #define EFX_LOG(efx, fmt, args...) \
67 dev_dbg(&((efx)->pci_dev->dev), "DBG: %s " fmt, efx_dev_name(efx), ##args)
68 #endif
69
70 #define EFX_TRACE(efx, fmt, args...) do {} while (0)
71
72 #define EFX_REGDUMP(efx, fmt, args...) do {} while (0)
73
74 /* Rate-limited logging */
75 #define EFX_ERR_RL(efx, fmt, args...) \
76 do {if (net_ratelimit()) EFX_ERR(efx, fmt, ##args); } while (0)
77
78 #define EFX_INFO_RL(efx, fmt, args...) \
79 do {if (net_ratelimit()) EFX_INFO(efx, fmt, ##args); } while (0)
80
81 #define EFX_LOG_RL(efx, fmt, args...) \
82 do {if (net_ratelimit()) EFX_LOG(efx, fmt, ##args); } while (0)
83
84 /**************************************************************************
85  *
86  * Efx data structures
87  *
88  **************************************************************************/
89
90 #define EFX_MAX_CHANNELS 32
91 #define EFX_MAX_RX_QUEUES EFX_MAX_CHANNELS
92
93 #define EFX_TX_QUEUE_OFFLOAD_CSUM       0
94 #define EFX_TX_QUEUE_NO_CSUM            1
95 #define EFX_TX_QUEUE_COUNT              2
96
97 /**
98  * struct efx_special_buffer - An Efx special buffer
99  * @addr: CPU base address of the buffer
100  * @dma_addr: DMA base address of the buffer
101  * @len: Buffer length, in bytes
102  * @index: Buffer index within controller;s buffer table
103  * @entries: Number of buffer table entries
104  *
105  * Special buffers are used for the event queues and the TX and RX
106  * descriptor queues for each channel.  They are *not* used for the
107  * actual transmit and receive buffers.
108  *
109  * Note that for Falcon, TX and RX descriptor queues live in host memory.
110  * Allocation and freeing procedures must take this into account.
111  */
112 struct efx_special_buffer {
113         void *addr;
114         dma_addr_t dma_addr;
115         unsigned int len;
116         int index;
117         int entries;
118 };
119
120 /**
121  * struct efx_tx_buffer - An Efx TX buffer
122  * @skb: The associated socket buffer.
123  *      Set only on the final fragment of a packet; %NULL for all other
124  *      fragments.  When this fragment completes, then we can free this
125  *      skb.
126  * @tsoh: The associated TSO header structure, or %NULL if this
127  *      buffer is not a TSO header.
128  * @dma_addr: DMA address of the fragment.
129  * @len: Length of this fragment.
130  *      This field is zero when the queue slot is empty.
131  * @continuation: True if this fragment is not the end of a packet.
132  * @unmap_single: True if pci_unmap_single should be used.
133  * @unmap_len: Length of this fragment to unmap
134  */
135 struct efx_tx_buffer {
136         const struct sk_buff *skb;
137         struct efx_tso_header *tsoh;
138         dma_addr_t dma_addr;
139         unsigned short len;
140         bool continuation;
141         bool unmap_single;
142         unsigned short unmap_len;
143 };
144
145 /**
146  * struct efx_tx_queue - An Efx TX queue
147  *
148  * This is a ring buffer of TX fragments.
149  * Since the TX completion path always executes on the same
150  * CPU and the xmit path can operate on different CPUs,
151  * performance is increased by ensuring that the completion
152  * path and the xmit path operate on different cache lines.
153  * This is particularly important if the xmit path is always
154  * executing on one CPU which is different from the completion
155  * path.  There is also a cache line for members which are
156  * read but not written on the fast path.
157  *
158  * @efx: The associated Efx NIC
159  * @queue: DMA queue number
160  * @channel: The associated channel
161  * @buffer: The software buffer ring
162  * @txd: The hardware descriptor ring
163  * @read_count: Current read pointer.
164  *      This is the number of buffers that have been removed from both rings.
165  * @stopped: Stopped count.
166  *      Set if this TX queue is currently stopping its port.
167  * @insert_count: Current insert pointer
168  *      This is the number of buffers that have been added to the
169  *      software ring.
170  * @write_count: Current write pointer
171  *      This is the number of buffers that have been added to the
172  *      hardware ring.
173  * @old_read_count: The value of read_count when last checked.
174  *      This is here for performance reasons.  The xmit path will
175  *      only get the up-to-date value of read_count if this
176  *      variable indicates that the queue is full.  This is to
177  *      avoid cache-line ping-pong between the xmit path and the
178  *      completion path.
179  * @tso_headers_free: A list of TSO headers allocated for this TX queue
180  *      that are not in use, and so available for new TSO sends. The list
181  *      is protected by the TX queue lock.
182  * @tso_bursts: Number of times TSO xmit invoked by kernel
183  * @tso_long_headers: Number of packets with headers too long for standard
184  *      blocks
185  * @tso_packets: Number of packets via the TSO xmit path
186  */
187 struct efx_tx_queue {
188         /* Members which don't change on the fast path */
189         struct efx_nic *efx ____cacheline_aligned_in_smp;
190         int queue;
191         struct efx_channel *channel;
192         struct efx_nic *nic;
193         struct efx_tx_buffer *buffer;
194         struct efx_special_buffer txd;
195
196         /* Members used mainly on the completion path */
197         unsigned int read_count ____cacheline_aligned_in_smp;
198         int stopped;
199
200         /* Members used only on the xmit path */
201         unsigned int insert_count ____cacheline_aligned_in_smp;
202         unsigned int write_count;
203         unsigned int old_read_count;
204         struct efx_tso_header *tso_headers_free;
205         unsigned int tso_bursts;
206         unsigned int tso_long_headers;
207         unsigned int tso_packets;
208 };
209
210 /**
211  * struct efx_rx_buffer - An Efx RX data buffer
212  * @dma_addr: DMA base address of the buffer
213  * @skb: The associated socket buffer, if any.
214  *      If both this and page are %NULL, the buffer slot is currently free.
215  * @page: The associated page buffer, if any.
216  *      If both this and skb are %NULL, the buffer slot is currently free.
217  * @data: Pointer to ethernet header
218  * @len: Buffer length, in bytes.
219  * @unmap_addr: DMA address to unmap
220  */
221 struct efx_rx_buffer {
222         dma_addr_t dma_addr;
223         struct sk_buff *skb;
224         struct page *page;
225         char *data;
226         unsigned int len;
227         dma_addr_t unmap_addr;
228 };
229
230 /**
231  * struct efx_rx_queue - An Efx RX queue
232  * @efx: The associated Efx NIC
233  * @queue: DMA queue number
234  * @channel: The associated channel
235  * @buffer: The software buffer ring
236  * @rxd: The hardware descriptor ring
237  * @added_count: Number of buffers added to the receive queue.
238  * @notified_count: Number of buffers given to NIC (<= @added_count).
239  * @removed_count: Number of buffers removed from the receive queue.
240  * @add_lock: Receive queue descriptor add spin lock.
241  *      This lock must be held in order to add buffers to the RX
242  *      descriptor ring (rxd and buffer) and to update added_count (but
243  *      not removed_count).
244  * @max_fill: RX descriptor maximum fill level (<= ring size)
245  * @fast_fill_trigger: RX descriptor fill level that will trigger a fast fill
246  *      (<= @max_fill)
247  * @fast_fill_limit: The level to which a fast fill will fill
248  *      (@fast_fill_trigger <= @fast_fill_limit <= @max_fill)
249  * @min_fill: RX descriptor minimum non-zero fill level.
250  *      This records the minimum fill level observed when a ring
251  *      refill was triggered.
252  * @min_overfill: RX descriptor minimum overflow fill level.
253  *      This records the minimum fill level at which RX queue
254  *      overflow was observed.  It should never be set.
255  * @alloc_page_count: RX allocation strategy counter.
256  * @alloc_skb_count: RX allocation strategy counter.
257  * @work: Descriptor push work thread
258  * @buf_page: Page for next RX buffer.
259  *      We can use a single page for multiple RX buffers. This tracks
260  *      the remaining space in the allocation.
261  * @buf_dma_addr: Page's DMA address.
262  * @buf_data: Page's host address.
263  */
264 struct efx_rx_queue {
265         struct efx_nic *efx;
266         int queue;
267         struct efx_channel *channel;
268         struct efx_rx_buffer *buffer;
269         struct efx_special_buffer rxd;
270
271         int added_count;
272         int notified_count;
273         int removed_count;
274         spinlock_t add_lock;
275         unsigned int max_fill;
276         unsigned int fast_fill_trigger;
277         unsigned int fast_fill_limit;
278         unsigned int min_fill;
279         unsigned int min_overfill;
280         unsigned int alloc_page_count;
281         unsigned int alloc_skb_count;
282         struct delayed_work work;
283         unsigned int slow_fill_count;
284
285         struct page *buf_page;
286         dma_addr_t buf_dma_addr;
287         char *buf_data;
288 };
289
290 /**
291  * struct efx_buffer - An Efx general-purpose buffer
292  * @addr: host base address of the buffer
293  * @dma_addr: DMA base address of the buffer
294  * @len: Buffer length, in bytes
295  *
296  * Falcon uses these buffers for its interrupt status registers and
297  * MAC stats dumps.
298  */
299 struct efx_buffer {
300         void *addr;
301         dma_addr_t dma_addr;
302         unsigned int len;
303 };
304
305
306 /* Flags for channel->used_flags */
307 #define EFX_USED_BY_RX 1
308 #define EFX_USED_BY_TX 2
309 #define EFX_USED_BY_RX_TX (EFX_USED_BY_RX | EFX_USED_BY_TX)
310
311 enum efx_rx_alloc_method {
312         RX_ALLOC_METHOD_AUTO = 0,
313         RX_ALLOC_METHOD_SKB = 1,
314         RX_ALLOC_METHOD_PAGE = 2,
315 };
316
317 /**
318  * struct efx_channel - An Efx channel
319  *
320  * A channel comprises an event queue, at least one TX queue, at least
321  * one RX queue, and an associated tasklet for processing the event
322  * queue.
323  *
324  * @efx: Associated Efx NIC
325  * @channel: Channel instance number
326  * @used_flags: Channel is used by net driver
327  * @enabled: Channel enabled indicator
328  * @irq: IRQ number (MSI and MSI-X only)
329  * @irq_moderation: IRQ moderation value (in us)
330  * @napi_dev: Net device used with NAPI
331  * @napi_str: NAPI control structure
332  * @reset_work: Scheduled reset work thread
333  * @work_pending: Is work pending via NAPI?
334  * @eventq: Event queue buffer
335  * @eventq_read_ptr: Event queue read pointer
336  * @last_eventq_read_ptr: Last event queue read pointer value.
337  * @eventq_magic: Event queue magic value for driver-generated test events
338  * @lro_mgr: LRO state
339  * @rx_alloc_level: Watermark based heuristic counter for pushing descriptors
340  *      and diagnostic counters
341  * @rx_alloc_push_pages: RX allocation method currently in use for pushing
342  *      descriptors
343  * @rx_alloc_pop_pages: RX allocation method currently in use for popping
344  *      descriptors
345  * @n_rx_tobe_disc: Count of RX_TOBE_DISC errors
346  * @n_rx_ip_frag_err: Count of RX IP fragment errors
347  * @n_rx_ip_hdr_chksum_err: Count of RX IP header checksum errors
348  * @n_rx_tcp_udp_chksum_err: Count of RX TCP and UDP checksum errors
349  * @n_rx_frm_trunc: Count of RX_FRM_TRUNC errors
350  * @n_rx_overlength: Count of RX_OVERLENGTH errors
351  * @n_skbuff_leaks: Count of skbuffs leaked due to RX overrun
352  */
353 struct efx_channel {
354         struct efx_nic *efx;
355         int channel;
356         int used_flags;
357         bool enabled;
358         int irq;
359         unsigned int irq_moderation;
360         struct net_device *napi_dev;
361         struct napi_struct napi_str;
362         bool work_pending;
363         struct efx_special_buffer eventq;
364         unsigned int eventq_read_ptr;
365         unsigned int last_eventq_read_ptr;
366         unsigned int eventq_magic;
367
368         struct net_lro_mgr lro_mgr;
369         int rx_alloc_level;
370         int rx_alloc_push_pages;
371         int rx_alloc_pop_pages;
372
373         unsigned n_rx_tobe_disc;
374         unsigned n_rx_ip_frag_err;
375         unsigned n_rx_ip_hdr_chksum_err;
376         unsigned n_rx_tcp_udp_chksum_err;
377         unsigned n_rx_frm_trunc;
378         unsigned n_rx_overlength;
379         unsigned n_skbuff_leaks;
380
381         /* Used to pipeline received packets in order to optimise memory
382          * access with prefetches.
383          */
384         struct efx_rx_buffer *rx_pkt;
385         bool rx_pkt_csummed;
386
387 };
388
389 /**
390  * struct efx_blinker - S/W LED blinking context
391  * @led_num: LED ID (board-specific meaning)
392  * @state: Current state - on or off
393  * @resubmit: Timer resubmission flag
394  * @timer: Control timer for blinking
395  */
396 struct efx_blinker {
397         int led_num;
398         bool state;
399         bool resubmit;
400         struct timer_list timer;
401 };
402
403
404 /**
405  * struct efx_board - board information
406  * @type: Board model type
407  * @major: Major rev. ('A', 'B' ...)
408  * @minor: Minor rev. (0, 1, ...)
409  * @init: Initialisation function
410  * @init_leds: Sets up board LEDs
411  * @set_fault_led: Turns the fault LED on or off
412  * @blink: Starts/stops blinking
413  * @fini: Cleanup function
414  * @blinker: used to blink LEDs in software
415  * @hwmon_client: I2C client for hardware monitor
416  * @ioexp_client: I2C client for power/port control
417  */
418 struct efx_board {
419         int type;
420         int major;
421         int minor;
422         int (*init) (struct efx_nic *nic);
423         /* As the LEDs are typically attached to the PHY, LEDs
424          * have a separate init callback that happens later than
425          * board init. */
426         int (*init_leds)(struct efx_nic *efx);
427         void (*set_fault_led) (struct efx_nic *efx, bool state);
428         void (*blink) (struct efx_nic *efx, bool start);
429         void (*fini) (struct efx_nic *nic);
430         struct efx_blinker blinker;
431         struct i2c_client *hwmon_client, *ioexp_client;
432 };
433
434 #define STRING_TABLE_LOOKUP(val, member)        \
435         member ## _names[val]
436
437 enum efx_int_mode {
438         /* Be careful if altering to correct macro below */
439         EFX_INT_MODE_MSIX = 0,
440         EFX_INT_MODE_MSI = 1,
441         EFX_INT_MODE_LEGACY = 2,
442         EFX_INT_MODE_MAX        /* Insert any new items before this */
443 };
444 #define EFX_INT_MODE_USE_MSI(x) (((x)->interrupt_mode) <= EFX_INT_MODE_MSI)
445
446 enum phy_type {
447         PHY_TYPE_NONE = 0,
448         PHY_TYPE_CX4_RTMR = 1,
449         PHY_TYPE_1G_ALASKA = 2,
450         PHY_TYPE_10XPRESS = 3,
451         PHY_TYPE_XFP = 4,
452         PHY_TYPE_PM8358 = 6,
453         PHY_TYPE_MAX    /* Insert any new items before this */
454 };
455
456 #define PHY_ADDR_INVALID 0xff
457
458 enum nic_state {
459         STATE_INIT = 0,
460         STATE_RUNNING = 1,
461         STATE_FINI = 2,
462         STATE_DISABLED = 3,
463         STATE_MAX,
464 };
465
466 /*
467  * Alignment of page-allocated RX buffers
468  *
469  * Controls the number of bytes inserted at the start of an RX buffer.
470  * This is the equivalent of NET_IP_ALIGN [which controls the alignment
471  * of the skb->head for hardware DMA].
472  */
473 #if defined(__i386__) || defined(__x86_64__)
474 #define EFX_PAGE_IP_ALIGN 0
475 #else
476 #define EFX_PAGE_IP_ALIGN NET_IP_ALIGN
477 #endif
478
479 /*
480  * Alignment of the skb->head which wraps a page-allocated RX buffer
481  *
482  * The skb allocated to wrap an rx_buffer can have this alignment. Since
483  * the data is memcpy'd from the rx_buf, it does not need to be equal to
484  * EFX_PAGE_IP_ALIGN.
485  */
486 #define EFX_PAGE_SKB_ALIGN 2
487
488 /* Forward declaration */
489 struct efx_nic;
490
491 /* Pseudo bit-mask flow control field */
492 enum efx_fc_type {
493         EFX_FC_RX = 1,
494         EFX_FC_TX = 2,
495         EFX_FC_AUTO = 4,
496 };
497
498 /**
499  * struct efx_phy_operations - Efx PHY operations table
500  * @init: Initialise PHY
501  * @fini: Shut down PHY
502  * @reconfigure: Reconfigure PHY (e.g. for new link parameters)
503  * @clear_interrupt: Clear down interrupt
504  * @blink: Blink LEDs
505  * @check_hw: Check hardware
506  * @reset_xaui: Reset XAUI side of PHY for (software sequenced reset)
507  * @mmds: MMD presence mask
508  * @loopbacks: Supported loopback modes mask
509  */
510 struct efx_phy_operations {
511         int (*init) (struct efx_nic *efx);
512         void (*fini) (struct efx_nic *efx);
513         void (*reconfigure) (struct efx_nic *efx);
514         void (*clear_interrupt) (struct efx_nic *efx);
515         int (*check_hw) (struct efx_nic *efx);
516         void (*reset_xaui) (struct efx_nic *efx);
517         int (*test) (struct efx_nic *efx);
518         int mmds;
519         unsigned loopbacks;
520 };
521
522 /**
523  * @enum efx_phy_mode - PHY operating mode flags
524  * @PHY_MODE_NORMAL: on and should pass traffic
525  * @PHY_MODE_TX_DISABLED: on with TX disabled
526  * @PHY_MODE_SPECIAL: on but will not pass traffic
527  */
528 enum efx_phy_mode {
529         PHY_MODE_NORMAL         = 0,
530         PHY_MODE_TX_DISABLED    = 1,
531         PHY_MODE_SPECIAL        = 8,
532 };
533
534 static inline bool efx_phy_mode_disabled(enum efx_phy_mode mode)
535 {
536         return !!(mode & ~PHY_MODE_TX_DISABLED);
537 }
538
539 /*
540  * Efx extended statistics
541  *
542  * Not all statistics are provided by all supported MACs.  The purpose
543  * is this structure is to contain the raw statistics provided by each
544  * MAC.
545  */
546 struct efx_mac_stats {
547         u64 tx_bytes;
548         u64 tx_good_bytes;
549         u64 tx_bad_bytes;
550         unsigned long tx_packets;
551         unsigned long tx_bad;
552         unsigned long tx_pause;
553         unsigned long tx_control;
554         unsigned long tx_unicast;
555         unsigned long tx_multicast;
556         unsigned long tx_broadcast;
557         unsigned long tx_lt64;
558         unsigned long tx_64;
559         unsigned long tx_65_to_127;
560         unsigned long tx_128_to_255;
561         unsigned long tx_256_to_511;
562         unsigned long tx_512_to_1023;
563         unsigned long tx_1024_to_15xx;
564         unsigned long tx_15xx_to_jumbo;
565         unsigned long tx_gtjumbo;
566         unsigned long tx_collision;
567         unsigned long tx_single_collision;
568         unsigned long tx_multiple_collision;
569         unsigned long tx_excessive_collision;
570         unsigned long tx_deferred;
571         unsigned long tx_late_collision;
572         unsigned long tx_excessive_deferred;
573         unsigned long tx_non_tcpudp;
574         unsigned long tx_mac_src_error;
575         unsigned long tx_ip_src_error;
576         u64 rx_bytes;
577         u64 rx_good_bytes;
578         u64 rx_bad_bytes;
579         unsigned long rx_packets;
580         unsigned long rx_good;
581         unsigned long rx_bad;
582         unsigned long rx_pause;
583         unsigned long rx_control;
584         unsigned long rx_unicast;
585         unsigned long rx_multicast;
586         unsigned long rx_broadcast;
587         unsigned long rx_lt64;
588         unsigned long rx_64;
589         unsigned long rx_65_to_127;
590         unsigned long rx_128_to_255;
591         unsigned long rx_256_to_511;
592         unsigned long rx_512_to_1023;
593         unsigned long rx_1024_to_15xx;
594         unsigned long rx_15xx_to_jumbo;
595         unsigned long rx_gtjumbo;
596         unsigned long rx_bad_lt64;
597         unsigned long rx_bad_64_to_15xx;
598         unsigned long rx_bad_15xx_to_jumbo;
599         unsigned long rx_bad_gtjumbo;
600         unsigned long rx_overflow;
601         unsigned long rx_missed;
602         unsigned long rx_false_carrier;
603         unsigned long rx_symbol_error;
604         unsigned long rx_align_error;
605         unsigned long rx_length_error;
606         unsigned long rx_internal_error;
607         unsigned long rx_good_lt64;
608 };
609
610 /* Number of bits used in a multicast filter hash address */
611 #define EFX_MCAST_HASH_BITS 8
612
613 /* Number of (single-bit) entries in a multicast filter hash */
614 #define EFX_MCAST_HASH_ENTRIES (1 << EFX_MCAST_HASH_BITS)
615
616 /* An Efx multicast filter hash */
617 union efx_multicast_hash {
618         u8 byte[EFX_MCAST_HASH_ENTRIES / 8];
619         efx_oword_t oword[EFX_MCAST_HASH_ENTRIES / sizeof(efx_oword_t) / 8];
620 };
621
622 /**
623  * struct efx_nic - an Efx NIC
624  * @name: Device name (net device name or bus id before net device registered)
625  * @pci_dev: The PCI device
626  * @type: Controller type attributes
627  * @legacy_irq: IRQ number
628  * @workqueue: Workqueue for port reconfigures and the HW monitor.
629  *      Work items do not hold and must not acquire RTNL.
630  * @reset_workqueue: Workqueue for resets.  Work item will acquire RTNL.
631  * @reset_work: Scheduled reset workitem
632  * @monitor_work: Hardware monitor workitem
633  * @membase_phys: Memory BAR value as physical address
634  * @membase: Memory BAR value
635  * @biu_lock: BIU (bus interface unit) lock
636  * @interrupt_mode: Interrupt mode
637  * @i2c_adap: I2C adapter
638  * @board_info: Board-level information
639  * @state: Device state flag. Serialised by the rtnl_lock.
640  * @reset_pending: Pending reset method (normally RESET_TYPE_NONE)
641  * @tx_queue: TX DMA queues
642  * @rx_queue: RX DMA queues
643  * @channel: Channels
644  * @n_rx_queues: Number of RX queues
645  * @rx_buffer_len: RX buffer length
646  * @rx_buffer_order: Order (log2) of number of pages for each RX buffer
647  * @irq_status: Interrupt status buffer
648  * @last_irq_cpu: Last CPU to handle interrupt.
649  *      This register is written with the SMP processor ID whenever an
650  *      interrupt is handled.  It is used by falcon_test_interrupt()
651  *      to verify that an interrupt has occurred.
652  * @spi_flash: SPI flash device
653  *      This field will be %NULL if no flash device is present.
654  * @spi_eeprom: SPI EEPROM device
655  *      This field will be %NULL if no EEPROM device is present.
656  * @n_rx_nodesc_drop_cnt: RX no descriptor drop count
657  * @nic_data: Hardware dependant state
658  * @mac_lock: MAC access lock. Protects @port_enabled, @phy_mode,
659  *      @port_inhibited, efx_monitor() and efx_reconfigure_port()
660  * @port_enabled: Port enabled indicator.
661  *      Serialises efx_stop_all(), efx_start_all() and efx_monitor() and
662  *      efx_reconfigure_work with kernel interfaces. Safe to read under any
663  *      one of the rtnl_lock, mac_lock, or netif_tx_lock, but all three must
664  *      be held to modify it.
665  * @port_inhibited: If set, the netif_carrier is always off. Hold the mac_lock
666  * @port_initialized: Port initialized?
667  * @net_dev: Operating system network device. Consider holding the rtnl lock
668  * @rx_checksum_enabled: RX checksumming enabled
669  * @netif_stop_count: Port stop count
670  * @netif_stop_lock: Port stop lock
671  * @mac_stats: MAC statistics. These include all statistics the MACs
672  *      can provide.  Generic code converts these into a standard
673  *      &struct net_device_stats.
674  * @stats_buffer: DMA buffer for statistics
675  * @stats_lock: Statistics update lock. Serialises statistics fetches
676  * @stats_enabled: Temporarily disable statistics fetches.
677  *      Serialised by @stats_lock
678  * @mac_address: Permanent MAC address
679  * @phy_type: PHY type
680  * @phy_lock: PHY access lock
681  * @phy_op: PHY interface
682  * @phy_data: PHY private data (including PHY-specific stats)
683  * @mii: PHY interface
684  * @phy_mode: PHY operating mode. Serialised by @mac_lock.
685  * @link_up: Link status
686  * @link_options: Link options (MII/GMII format)
687  * @n_link_state_changes: Number of times the link has changed state
688  * @promiscuous: Promiscuous flag. Protected by netif_tx_lock.
689  * @multicast_hash: Multicast hash table
690  * @flow_control: Flow control flags - separate RX/TX so can't use link_options
691  * @reconfigure_work: work item for dealing with PHY events
692  * @loopback_mode: Loopback status
693  * @loopback_modes: Supported loopback mode bitmask
694  * @loopback_selftest: Offline self-test private state
695  *
696  * The @priv field of the corresponding &struct net_device points to
697  * this.
698  */
699 struct efx_nic {
700         char name[IFNAMSIZ];
701         struct pci_dev *pci_dev;
702         const struct efx_nic_type *type;
703         int legacy_irq;
704         struct workqueue_struct *workqueue;
705         struct workqueue_struct *reset_workqueue;
706         struct work_struct reset_work;
707         struct delayed_work monitor_work;
708         resource_size_t membase_phys;
709         void __iomem *membase;
710         spinlock_t biu_lock;
711         enum efx_int_mode interrupt_mode;
712
713         struct i2c_adapter i2c_adap;
714         struct efx_board board_info;
715
716         enum nic_state state;
717         enum reset_type reset_pending;
718
719         struct efx_tx_queue tx_queue[EFX_TX_QUEUE_COUNT];
720         struct efx_rx_queue rx_queue[EFX_MAX_RX_QUEUES];
721         struct efx_channel channel[EFX_MAX_CHANNELS];
722
723         int n_rx_queues;
724         unsigned int rx_buffer_len;
725         unsigned int rx_buffer_order;
726
727         struct efx_buffer irq_status;
728         volatile signed int last_irq_cpu;
729
730         struct efx_spi_device *spi_flash;
731         struct efx_spi_device *spi_eeprom;
732
733         unsigned n_rx_nodesc_drop_cnt;
734
735         struct falcon_nic_data *nic_data;
736
737         struct mutex mac_lock;
738         bool port_enabled;
739         bool port_inhibited;
740
741         bool port_initialized;
742         struct net_device *net_dev;
743         bool rx_checksum_enabled;
744
745         atomic_t netif_stop_count;
746         spinlock_t netif_stop_lock;
747
748         struct efx_mac_stats mac_stats;
749         struct efx_buffer stats_buffer;
750         spinlock_t stats_lock;
751         bool stats_enabled;
752
753         unsigned char mac_address[ETH_ALEN];
754
755         enum phy_type phy_type;
756         spinlock_t phy_lock;
757         struct efx_phy_operations *phy_op;
758         void *phy_data;
759         struct mii_if_info mii;
760         enum efx_phy_mode phy_mode;
761
762         bool link_up;
763         unsigned int link_options;
764         unsigned int n_link_state_changes;
765
766         bool promiscuous;
767         union efx_multicast_hash multicast_hash;
768         enum efx_fc_type flow_control;
769         struct work_struct reconfigure_work;
770
771         atomic_t rx_reset;
772         enum efx_loopback_mode loopback_mode;
773         unsigned int loopback_modes;
774
775         void *loopback_selftest;
776 };
777
778 static inline int efx_dev_registered(struct efx_nic *efx)
779 {
780         return efx->net_dev->reg_state == NETREG_REGISTERED;
781 }
782
783 /* Net device name, for inclusion in log messages if it has been registered.
784  * Use efx->name not efx->net_dev->name so that races with (un)registration
785  * are harmless.
786  */
787 static inline const char *efx_dev_name(struct efx_nic *efx)
788 {
789         return efx_dev_registered(efx) ? efx->name : "";
790 }
791
792 /**
793  * struct efx_nic_type - Efx device type definition
794  * @mem_bar: Memory BAR number
795  * @mem_map_size: Memory BAR mapped size
796  * @txd_ptr_tbl_base: TX descriptor ring base address
797  * @rxd_ptr_tbl_base: RX descriptor ring base address
798  * @buf_tbl_base: Buffer table base address
799  * @evq_ptr_tbl_base: Event queue pointer table base address
800  * @evq_rptr_tbl_base: Event queue read-pointer table base address
801  * @txd_ring_mask: TX descriptor ring size - 1 (must be a power of two - 1)
802  * @rxd_ring_mask: RX descriptor ring size - 1 (must be a power of two - 1)
803  * @evq_size: Event queue size (must be a power of two)
804  * @max_dma_mask: Maximum possible DMA mask
805  * @tx_dma_mask: TX DMA mask
806  * @bug5391_mask: Address mask for bug 5391 workaround
807  * @rx_xoff_thresh: RX FIFO XOFF watermark (bytes)
808  * @rx_xon_thresh: RX FIFO XON watermark (bytes)
809  * @rx_buffer_padding: Padding added to each RX buffer
810  * @max_interrupt_mode: Highest capability interrupt mode supported
811  *      from &enum efx_init_mode.
812  * @phys_addr_channels: Number of channels with physically addressed
813  *      descriptors
814  */
815 struct efx_nic_type {
816         unsigned int mem_bar;
817         unsigned int mem_map_size;
818         unsigned int txd_ptr_tbl_base;
819         unsigned int rxd_ptr_tbl_base;
820         unsigned int buf_tbl_base;
821         unsigned int evq_ptr_tbl_base;
822         unsigned int evq_rptr_tbl_base;
823
824         unsigned int txd_ring_mask;
825         unsigned int rxd_ring_mask;
826         unsigned int evq_size;
827         u64 max_dma_mask;
828         unsigned int tx_dma_mask;
829         unsigned bug5391_mask;
830
831         int rx_xoff_thresh;
832         int rx_xon_thresh;
833         unsigned int rx_buffer_padding;
834         unsigned int max_interrupt_mode;
835         unsigned int phys_addr_channels;
836 };
837
838 /**************************************************************************
839  *
840  * Prototypes and inline functions
841  *
842  *************************************************************************/
843
844 /* Iterate over all used channels */
845 #define efx_for_each_channel(_channel, _efx)                            \
846         for (_channel = &_efx->channel[0];                              \
847              _channel < &_efx->channel[EFX_MAX_CHANNELS];               \
848              _channel++)                                                \
849                 if (!_channel->used_flags)                              \
850                         continue;                                       \
851                 else
852
853 /* Iterate over all used TX queues */
854 #define efx_for_each_tx_queue(_tx_queue, _efx)                          \
855         for (_tx_queue = &_efx->tx_queue[0];                            \
856              _tx_queue < &_efx->tx_queue[EFX_TX_QUEUE_COUNT];           \
857              _tx_queue++)
858
859 /* Iterate over all TX queues belonging to a channel */
860 #define efx_for_each_channel_tx_queue(_tx_queue, _channel)              \
861         for (_tx_queue = &_channel->efx->tx_queue[0];                   \
862              _tx_queue < &_channel->efx->tx_queue[EFX_TX_QUEUE_COUNT];  \
863              _tx_queue++)                                               \
864                 if (_tx_queue->channel != _channel)                     \
865                         continue;                                       \
866                 else
867
868 /* Iterate over all used RX queues */
869 #define efx_for_each_rx_queue(_rx_queue, _efx)                          \
870         for (_rx_queue = &_efx->rx_queue[0];                            \
871              _rx_queue < &_efx->rx_queue[_efx->n_rx_queues];            \
872              _rx_queue++)
873
874 /* Iterate over all RX queues belonging to a channel */
875 #define efx_for_each_channel_rx_queue(_rx_queue, _channel)              \
876         for (_rx_queue = &_channel->efx->rx_queue[_channel->channel];   \
877              _rx_queue;                                                 \
878              _rx_queue = NULL)                                          \
879                 if (_rx_queue->channel != _channel)                     \
880                         continue;                                       \
881                 else
882
883 /* Returns a pointer to the specified receive buffer in the RX
884  * descriptor queue.
885  */
886 static inline struct efx_rx_buffer *efx_rx_buffer(struct efx_rx_queue *rx_queue,
887                                                   unsigned int index)
888 {
889         return (&rx_queue->buffer[index]);
890 }
891
892 /* Set bit in a little-endian bitfield */
893 static inline void set_bit_le(unsigned nr, unsigned char *addr)
894 {
895         addr[nr / 8] |= (1 << (nr % 8));
896 }
897
898 /* Clear bit in a little-endian bitfield */
899 static inline void clear_bit_le(unsigned nr, unsigned char *addr)
900 {
901         addr[nr / 8] &= ~(1 << (nr % 8));
902 }
903
904
905 /**
906  * EFX_MAX_FRAME_LEN - calculate maximum frame length
907  *
908  * This calculates the maximum frame length that will be used for a
909  * given MTU.  The frame length will be equal to the MTU plus a
910  * constant amount of header space and padding.  This is the quantity
911  * that the net driver will program into the MAC as the maximum frame
912  * length.
913  *
914  * The 10G MAC used in Falcon requires 8-byte alignment on the frame
915  * length, so we round up to the nearest 8.
916  */
917 #define EFX_MAX_FRAME_LEN(mtu) \
918         ((((mtu) + ETH_HLEN + VLAN_HLEN + 4/* FCS */) + 7) & ~7)
919
920
921 #endif /* EFX_NET_DRIVER_H */