net_driver.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2011 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. /* Common definitions for all Efx net driver code */
  11. #ifndef EFX_NET_DRIVER_H
  12. #define EFX_NET_DRIVER_H
  13. #if defined(EFX_ENABLE_DEBUG) && !defined(DEBUG)
  14. #define DEBUG
  15. #endif
  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/vmalloc.h>
  28. #include <linux/i2c.h>
  29. #include "enum.h"
  30. #include "bitfield.h"
  31. /**************************************************************************
  32. *
  33. * Build definitions
  34. *
  35. **************************************************************************/
  36. #define EFX_DRIVER_VERSION "3.1"
  37. #ifdef EFX_ENABLE_DEBUG
  38. #define EFX_BUG_ON_PARANOID(x) BUG_ON(x)
  39. #define EFX_WARN_ON_PARANOID(x) WARN_ON(x)
  40. #else
  41. #define EFX_BUG_ON_PARANOID(x) do {} while (0)
  42. #define EFX_WARN_ON_PARANOID(x) do {} while (0)
  43. #endif
  44. /**************************************************************************
  45. *
  46. * Efx data structures
  47. *
  48. **************************************************************************/
  49. #define EFX_MAX_CHANNELS 32
  50. #define EFX_MAX_RX_QUEUES EFX_MAX_CHANNELS
  51. /* Checksum generation is a per-queue option in hardware, so each
  52. * queue visible to the networking core is backed by two hardware TX
  53. * queues. */
  54. #define EFX_MAX_TX_TC 2
  55. #define EFX_MAX_CORE_TX_QUEUES (EFX_MAX_TX_TC * EFX_MAX_CHANNELS)
  56. #define EFX_TXQ_TYPE_OFFLOAD 1 /* flag */
  57. #define EFX_TXQ_TYPE_HIGHPRI 2 /* flag */
  58. #define EFX_TXQ_TYPES 4
  59. #define EFX_MAX_TX_QUEUES (EFX_TXQ_TYPES * EFX_MAX_CHANNELS)
  60. /**
  61. * struct efx_special_buffer - An Efx special buffer
  62. * @addr: CPU base address of the buffer
  63. * @dma_addr: DMA base address of the buffer
  64. * @len: Buffer length, in bytes
  65. * @index: Buffer index within controller;s buffer table
  66. * @entries: Number of buffer table entries
  67. *
  68. * Special buffers are used for the event queues and the TX and RX
  69. * descriptor queues for each channel. They are *not* used for the
  70. * actual transmit and receive buffers.
  71. */
  72. struct efx_special_buffer {
  73. void *addr;
  74. dma_addr_t dma_addr;
  75. unsigned int len;
  76. int index;
  77. int entries;
  78. };
  79. enum efx_flush_state {
  80. FLUSH_NONE,
  81. FLUSH_PENDING,
  82. FLUSH_FAILED,
  83. FLUSH_DONE,
  84. };
  85. /**
  86. * struct efx_tx_buffer - An Efx TX buffer
  87. * @skb: The associated socket buffer.
  88. * Set only on the final fragment of a packet; %NULL for all other
  89. * fragments. When this fragment completes, then we can free this
  90. * skb.
  91. * @tsoh: The associated TSO header structure, or %NULL if this
  92. * buffer is not a TSO header.
  93. * @dma_addr: DMA address of the fragment.
  94. * @len: Length of this fragment.
  95. * This field is zero when the queue slot is empty.
  96. * @continuation: True if this fragment is not the end of a packet.
  97. * @unmap_single: True if pci_unmap_single should be used.
  98. * @unmap_len: Length of this fragment to unmap
  99. */
  100. struct efx_tx_buffer {
  101. const struct sk_buff *skb;
  102. struct efx_tso_header *tsoh;
  103. dma_addr_t dma_addr;
  104. unsigned short len;
  105. bool continuation;
  106. bool unmap_single;
  107. unsigned short unmap_len;
  108. };
  109. /**
  110. * struct efx_tx_queue - An Efx TX queue
  111. *
  112. * This is a ring buffer of TX fragments.
  113. * Since the TX completion path always executes on the same
  114. * CPU and the xmit path can operate on different CPUs,
  115. * performance is increased by ensuring that the completion
  116. * path and the xmit path operate on different cache lines.
  117. * This is particularly important if the xmit path is always
  118. * executing on one CPU which is different from the completion
  119. * path. There is also a cache line for members which are
  120. * read but not written on the fast path.
  121. *
  122. * @efx: The associated Efx NIC
  123. * @queue: DMA queue number
  124. * @channel: The associated channel
  125. * @core_txq: The networking core TX queue structure
  126. * @buffer: The software buffer ring
  127. * @txd: The hardware descriptor ring
  128. * @ptr_mask: The size of the ring minus 1.
  129. * @initialised: Has hardware queue been initialised?
  130. * @flushed: Used when handling queue flushing
  131. * @read_count: Current read pointer.
  132. * This is the number of buffers that have been removed from both rings.
  133. * @old_write_count: The value of @write_count when last checked.
  134. * This is here for performance reasons. The xmit path will
  135. * only get the up-to-date value of @write_count if this
  136. * variable indicates that the queue is empty. This is to
  137. * avoid cache-line ping-pong between the xmit path and the
  138. * completion path.
  139. * @insert_count: Current insert pointer
  140. * This is the number of buffers that have been added to the
  141. * software ring.
  142. * @write_count: Current write pointer
  143. * This is the number of buffers that have been added to the
  144. * hardware ring.
  145. * @old_read_count: The value of read_count when last checked.
  146. * This is here for performance reasons. The xmit path will
  147. * only get the up-to-date value of read_count if this
  148. * variable indicates that the queue is full. This is to
  149. * avoid cache-line ping-pong between the xmit path and the
  150. * completion path.
  151. * @tso_headers_free: A list of TSO headers allocated for this TX queue
  152. * that are not in use, and so available for new TSO sends. The list
  153. * is protected by the TX queue lock.
  154. * @tso_bursts: Number of times TSO xmit invoked by kernel
  155. * @tso_long_headers: Number of packets with headers too long for standard
  156. * blocks
  157. * @tso_packets: Number of packets via the TSO xmit path
  158. * @pushes: Number of times the TX push feature has been used
  159. * @empty_read_count: If the completion path has seen the queue as empty
  160. * and the transmission path has not yet checked this, the value of
  161. * @read_count bitwise-added to %EFX_EMPTY_COUNT_VALID; otherwise 0.
  162. */
  163. struct efx_tx_queue {
  164. /* Members which don't change on the fast path */
  165. struct efx_nic *efx ____cacheline_aligned_in_smp;
  166. unsigned queue;
  167. struct efx_channel *channel;
  168. struct netdev_queue *core_txq;
  169. struct efx_tx_buffer *buffer;
  170. struct efx_special_buffer txd;
  171. unsigned int ptr_mask;
  172. bool initialised;
  173. enum efx_flush_state flushed;
  174. /* Members used mainly on the completion path */
  175. unsigned int read_count ____cacheline_aligned_in_smp;
  176. unsigned int old_write_count;
  177. /* Members used only on the xmit path */
  178. unsigned int insert_count ____cacheline_aligned_in_smp;
  179. unsigned int write_count;
  180. unsigned int old_read_count;
  181. struct efx_tso_header *tso_headers_free;
  182. unsigned int tso_bursts;
  183. unsigned int tso_long_headers;
  184. unsigned int tso_packets;
  185. unsigned int pushes;
  186. /* Members shared between paths and sometimes updated */
  187. unsigned int empty_read_count ____cacheline_aligned_in_smp;
  188. #define EFX_EMPTY_COUNT_VALID 0x80000000
  189. };
  190. /**
  191. * struct efx_rx_buffer - An Efx RX data buffer
  192. * @dma_addr: DMA base address of the buffer
  193. * @skb: The associated socket buffer, if any.
  194. * If both this and page are %NULL, the buffer slot is currently free.
  195. * @page: The associated page buffer, if any.
  196. * If both this and skb are %NULL, the buffer slot is currently free.
  197. * @len: Buffer length, in bytes.
  198. * @is_page: Indicates if @page is valid. If false, @skb is valid.
  199. */
  200. struct efx_rx_buffer {
  201. dma_addr_t dma_addr;
  202. union {
  203. struct sk_buff *skb;
  204. struct page *page;
  205. } u;
  206. unsigned int len;
  207. bool is_page;
  208. };
  209. /**
  210. * struct efx_rx_page_state - Page-based rx buffer state
  211. *
  212. * Inserted at the start of every page allocated for receive buffers.
  213. * Used to facilitate sharing dma mappings between recycled rx buffers
  214. * and those passed up to the kernel.
  215. *
  216. * @refcnt: Number of struct efx_rx_buffer's referencing this page.
  217. * When refcnt falls to zero, the page is unmapped for dma
  218. * @dma_addr: The dma address of this page.
  219. */
  220. struct efx_rx_page_state {
  221. unsigned refcnt;
  222. dma_addr_t dma_addr;
  223. unsigned int __pad[0] ____cacheline_aligned;
  224. };
  225. /**
  226. * struct efx_rx_queue - An Efx RX queue
  227. * @efx: The associated Efx NIC
  228. * @buffer: The software buffer ring
  229. * @rxd: The hardware descriptor ring
  230. * @ptr_mask: The size of the ring minus 1.
  231. * @added_count: Number of buffers added to the receive queue.
  232. * @notified_count: Number of buffers given to NIC (<= @added_count).
  233. * @removed_count: Number of buffers removed from the receive queue.
  234. * @max_fill: RX descriptor maximum fill level (<= ring size)
  235. * @fast_fill_trigger: RX descriptor fill level that will trigger a fast fill
  236. * (<= @max_fill)
  237. * @fast_fill_limit: The level to which a fast fill will fill
  238. * (@fast_fill_trigger <= @fast_fill_limit <= @max_fill)
  239. * @min_fill: RX descriptor minimum non-zero fill level.
  240. * This records the minimum fill level observed when a ring
  241. * refill was triggered.
  242. * @alloc_page_count: RX allocation strategy counter.
  243. * @alloc_skb_count: RX allocation strategy counter.
  244. * @slow_fill: Timer used to defer efx_nic_generate_fill_event().
  245. * @flushed: Use when handling queue flushing
  246. */
  247. struct efx_rx_queue {
  248. struct efx_nic *efx;
  249. struct efx_rx_buffer *buffer;
  250. struct efx_special_buffer rxd;
  251. unsigned int ptr_mask;
  252. int added_count;
  253. int notified_count;
  254. int removed_count;
  255. unsigned int max_fill;
  256. unsigned int fast_fill_trigger;
  257. unsigned int fast_fill_limit;
  258. unsigned int min_fill;
  259. unsigned int min_overfill;
  260. unsigned int alloc_page_count;
  261. unsigned int alloc_skb_count;
  262. struct timer_list slow_fill;
  263. unsigned int slow_fill_count;
  264. enum efx_flush_state flushed;
  265. };
  266. /**
  267. * struct efx_buffer - An Efx general-purpose buffer
  268. * @addr: host base address of the buffer
  269. * @dma_addr: DMA base address of the buffer
  270. * @len: Buffer length, in bytes
  271. *
  272. * The NIC uses these buffers for its interrupt status registers and
  273. * MAC stats dumps.
  274. */
  275. struct efx_buffer {
  276. void *addr;
  277. dma_addr_t dma_addr;
  278. unsigned int len;
  279. };
  280. enum efx_rx_alloc_method {
  281. RX_ALLOC_METHOD_AUTO = 0,
  282. RX_ALLOC_METHOD_SKB = 1,
  283. RX_ALLOC_METHOD_PAGE = 2,
  284. };
  285. /**
  286. * struct efx_channel - An Efx channel
  287. *
  288. * A channel comprises an event queue, at least one TX queue, at least
  289. * one RX queue, and an associated tasklet for processing the event
  290. * queue.
  291. *
  292. * @efx: Associated Efx NIC
  293. * @channel: Channel instance number
  294. * @enabled: Channel enabled indicator
  295. * @irq: IRQ number (MSI and MSI-X only)
  296. * @irq_moderation: IRQ moderation value (in hardware ticks)
  297. * @napi_dev: Net device used with NAPI
  298. * @napi_str: NAPI control structure
  299. * @work_pending: Is work pending via NAPI?
  300. * @eventq: Event queue buffer
  301. * @eventq_mask: Event queue pointer mask
  302. * @eventq_read_ptr: Event queue read pointer
  303. * @last_eventq_read_ptr: Last event queue read pointer value.
  304. * @irq_count: Number of IRQs since last adaptive moderation decision
  305. * @irq_mod_score: IRQ moderation score
  306. * @rx_alloc_level: Watermark based heuristic counter for pushing descriptors
  307. * and diagnostic counters
  308. * @rx_alloc_push_pages: RX allocation method currently in use for pushing
  309. * descriptors
  310. * @n_rx_tobe_disc: Count of RX_TOBE_DISC errors
  311. * @n_rx_ip_hdr_chksum_err: Count of RX IP header checksum errors
  312. * @n_rx_tcp_udp_chksum_err: Count of RX TCP and UDP checksum errors
  313. * @n_rx_mcast_mismatch: Count of unmatched multicast frames
  314. * @n_rx_frm_trunc: Count of RX_FRM_TRUNC errors
  315. * @n_rx_overlength: Count of RX_OVERLENGTH errors
  316. * @n_skbuff_leaks: Count of skbuffs leaked due to RX overrun
  317. * @rx_queue: RX queue for this channel
  318. * @tx_queue: TX queues for this channel
  319. */
  320. struct efx_channel {
  321. struct efx_nic *efx;
  322. int channel;
  323. bool enabled;
  324. int irq;
  325. unsigned int irq_moderation;
  326. struct net_device *napi_dev;
  327. struct napi_struct napi_str;
  328. bool work_pending;
  329. struct efx_special_buffer eventq;
  330. unsigned int eventq_mask;
  331. unsigned int eventq_read_ptr;
  332. unsigned int last_eventq_read_ptr;
  333. unsigned int irq_count;
  334. unsigned int irq_mod_score;
  335. #ifdef CONFIG_RFS_ACCEL
  336. unsigned int rfs_filters_added;
  337. #endif
  338. int rx_alloc_level;
  339. int rx_alloc_push_pages;
  340. unsigned n_rx_tobe_disc;
  341. unsigned n_rx_ip_hdr_chksum_err;
  342. unsigned n_rx_tcp_udp_chksum_err;
  343. unsigned n_rx_mcast_mismatch;
  344. unsigned n_rx_frm_trunc;
  345. unsigned n_rx_overlength;
  346. unsigned n_skbuff_leaks;
  347. /* Used to pipeline received packets in order to optimise memory
  348. * access with prefetches.
  349. */
  350. struct efx_rx_buffer *rx_pkt;
  351. bool rx_pkt_csummed;
  352. struct efx_rx_queue rx_queue;
  353. struct efx_tx_queue tx_queue[EFX_TXQ_TYPES];
  354. };
  355. enum efx_led_mode {
  356. EFX_LED_OFF = 0,
  357. EFX_LED_ON = 1,
  358. EFX_LED_DEFAULT = 2
  359. };
  360. #define STRING_TABLE_LOOKUP(val, member) \
  361. ((val) < member ## _max) ? member ## _names[val] : "(invalid)"
  362. extern const char *efx_loopback_mode_names[];
  363. extern const unsigned int efx_loopback_mode_max;
  364. #define LOOPBACK_MODE(efx) \
  365. STRING_TABLE_LOOKUP((efx)->loopback_mode, efx_loopback_mode)
  366. extern const char *efx_reset_type_names[];
  367. extern const unsigned int efx_reset_type_max;
  368. #define RESET_TYPE(type) \
  369. STRING_TABLE_LOOKUP(type, efx_reset_type)
  370. enum efx_int_mode {
  371. /* Be careful if altering to correct macro below */
  372. EFX_INT_MODE_MSIX = 0,
  373. EFX_INT_MODE_MSI = 1,
  374. EFX_INT_MODE_LEGACY = 2,
  375. EFX_INT_MODE_MAX /* Insert any new items before this */
  376. };
  377. #define EFX_INT_MODE_USE_MSI(x) (((x)->interrupt_mode) <= EFX_INT_MODE_MSI)
  378. enum nic_state {
  379. STATE_INIT = 0,
  380. STATE_RUNNING = 1,
  381. STATE_FINI = 2,
  382. STATE_DISABLED = 3,
  383. STATE_MAX,
  384. };
  385. /*
  386. * Alignment of page-allocated RX buffers
  387. *
  388. * Controls the number of bytes inserted at the start of an RX buffer.
  389. * This is the equivalent of NET_IP_ALIGN [which controls the alignment
  390. * of the skb->head for hardware DMA].
  391. */
  392. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  393. #define EFX_PAGE_IP_ALIGN 0
  394. #else
  395. #define EFX_PAGE_IP_ALIGN NET_IP_ALIGN
  396. #endif
  397. /*
  398. * Alignment of the skb->head which wraps a page-allocated RX buffer
  399. *
  400. * The skb allocated to wrap an rx_buffer can have this alignment. Since
  401. * the data is memcpy'd from the rx_buf, it does not need to be equal to
  402. * EFX_PAGE_IP_ALIGN.
  403. */
  404. #define EFX_PAGE_SKB_ALIGN 2
  405. /* Forward declaration */
  406. struct efx_nic;
  407. /* Pseudo bit-mask flow control field */
  408. #define EFX_FC_RX FLOW_CTRL_RX
  409. #define EFX_FC_TX FLOW_CTRL_TX
  410. #define EFX_FC_AUTO 4
  411. /**
  412. * struct efx_link_state - Current state of the link
  413. * @up: Link is up
  414. * @fd: Link is full-duplex
  415. * @fc: Actual flow control flags
  416. * @speed: Link speed (Mbps)
  417. */
  418. struct efx_link_state {
  419. bool up;
  420. bool fd;
  421. u8 fc;
  422. unsigned int speed;
  423. };
  424. static inline bool efx_link_state_equal(const struct efx_link_state *left,
  425. const struct efx_link_state *right)
  426. {
  427. return left->up == right->up && left->fd == right->fd &&
  428. left->fc == right->fc && left->speed == right->speed;
  429. }
  430. /**
  431. * struct efx_mac_operations - Efx MAC operations table
  432. * @reconfigure: Reconfigure MAC. Serialised by the mac_lock
  433. * @update_stats: Update statistics
  434. * @check_fault: Check fault state. True if fault present.
  435. */
  436. struct efx_mac_operations {
  437. int (*reconfigure) (struct efx_nic *efx);
  438. void (*update_stats) (struct efx_nic *efx);
  439. bool (*check_fault)(struct efx_nic *efx);
  440. };
  441. /**
  442. * struct efx_phy_operations - Efx PHY operations table
  443. * @probe: Probe PHY and initialise efx->mdio.mode_support, efx->mdio.mmds,
  444. * efx->loopback_modes.
  445. * @init: Initialise PHY
  446. * @fini: Shut down PHY
  447. * @reconfigure: Reconfigure PHY (e.g. for new link parameters)
  448. * @poll: Update @link_state and report whether it changed.
  449. * Serialised by the mac_lock.
  450. * @get_settings: Get ethtool settings. Serialised by the mac_lock.
  451. * @set_settings: Set ethtool settings. Serialised by the mac_lock.
  452. * @set_npage_adv: Set abilities advertised in (Extended) Next Page
  453. * (only needed where AN bit is set in mmds)
  454. * @test_alive: Test that PHY is 'alive' (online)
  455. * @test_name: Get the name of a PHY-specific test/result
  456. * @run_tests: Run tests and record results as appropriate (offline).
  457. * Flags are the ethtool tests flags.
  458. */
  459. struct efx_phy_operations {
  460. int (*probe) (struct efx_nic *efx);
  461. int (*init) (struct efx_nic *efx);
  462. void (*fini) (struct efx_nic *efx);
  463. void (*remove) (struct efx_nic *efx);
  464. int (*reconfigure) (struct efx_nic *efx);
  465. bool (*poll) (struct efx_nic *efx);
  466. void (*get_settings) (struct efx_nic *efx,
  467. struct ethtool_cmd *ecmd);
  468. int (*set_settings) (struct efx_nic *efx,
  469. struct ethtool_cmd *ecmd);
  470. void (*set_npage_adv) (struct efx_nic *efx, u32);
  471. int (*test_alive) (struct efx_nic *efx);
  472. const char *(*test_name) (struct efx_nic *efx, unsigned int index);
  473. int (*run_tests) (struct efx_nic *efx, int *results, unsigned flags);
  474. };
  475. /**
  476. * @enum efx_phy_mode - PHY operating mode flags
  477. * @PHY_MODE_NORMAL: on and should pass traffic
  478. * @PHY_MODE_TX_DISABLED: on with TX disabled
  479. * @PHY_MODE_LOW_POWER: set to low power through MDIO
  480. * @PHY_MODE_OFF: switched off through external control
  481. * @PHY_MODE_SPECIAL: on but will not pass traffic
  482. */
  483. enum efx_phy_mode {
  484. PHY_MODE_NORMAL = 0,
  485. PHY_MODE_TX_DISABLED = 1,
  486. PHY_MODE_LOW_POWER = 2,
  487. PHY_MODE_OFF = 4,
  488. PHY_MODE_SPECIAL = 8,
  489. };
  490. static inline bool efx_phy_mode_disabled(enum efx_phy_mode mode)
  491. {
  492. return !!(mode & ~PHY_MODE_TX_DISABLED);
  493. }
  494. /*
  495. * Efx extended statistics
  496. *
  497. * Not all statistics are provided by all supported MACs. The purpose
  498. * is this structure is to contain the raw statistics provided by each
  499. * MAC.
  500. */
  501. struct efx_mac_stats {
  502. u64 tx_bytes;
  503. u64 tx_good_bytes;
  504. u64 tx_bad_bytes;
  505. unsigned long tx_packets;
  506. unsigned long tx_bad;
  507. unsigned long tx_pause;
  508. unsigned long tx_control;
  509. unsigned long tx_unicast;
  510. unsigned long tx_multicast;
  511. unsigned long tx_broadcast;
  512. unsigned long tx_lt64;
  513. unsigned long tx_64;
  514. unsigned long tx_65_to_127;
  515. unsigned long tx_128_to_255;
  516. unsigned long tx_256_to_511;
  517. unsigned long tx_512_to_1023;
  518. unsigned long tx_1024_to_15xx;
  519. unsigned long tx_15xx_to_jumbo;
  520. unsigned long tx_gtjumbo;
  521. unsigned long tx_collision;
  522. unsigned long tx_single_collision;
  523. unsigned long tx_multiple_collision;
  524. unsigned long tx_excessive_collision;
  525. unsigned long tx_deferred;
  526. unsigned long tx_late_collision;
  527. unsigned long tx_excessive_deferred;
  528. unsigned long tx_non_tcpudp;
  529. unsigned long tx_mac_src_error;
  530. unsigned long tx_ip_src_error;
  531. u64 rx_bytes;
  532. u64 rx_good_bytes;
  533. u64 rx_bad_bytes;
  534. unsigned long rx_packets;
  535. unsigned long rx_good;
  536. unsigned long rx_bad;
  537. unsigned long rx_pause;
  538. unsigned long rx_control;
  539. unsigned long rx_unicast;
  540. unsigned long rx_multicast;
  541. unsigned long rx_broadcast;
  542. unsigned long rx_lt64;
  543. unsigned long rx_64;
  544. unsigned long rx_65_to_127;
  545. unsigned long rx_128_to_255;
  546. unsigned long rx_256_to_511;
  547. unsigned long rx_512_to_1023;
  548. unsigned long rx_1024_to_15xx;
  549. unsigned long rx_15xx_to_jumbo;
  550. unsigned long rx_gtjumbo;
  551. unsigned long rx_bad_lt64;
  552. unsigned long rx_bad_64_to_15xx;
  553. unsigned long rx_bad_15xx_to_jumbo;
  554. unsigned long rx_bad_gtjumbo;
  555. unsigned long rx_overflow;
  556. unsigned long rx_missed;
  557. unsigned long rx_false_carrier;
  558. unsigned long rx_symbol_error;
  559. unsigned long rx_align_error;
  560. unsigned long rx_length_error;
  561. unsigned long rx_internal_error;
  562. unsigned long rx_good_lt64;
  563. };
  564. /* Number of bits used in a multicast filter hash address */
  565. #define EFX_MCAST_HASH_BITS 8
  566. /* Number of (single-bit) entries in a multicast filter hash */
  567. #define EFX_MCAST_HASH_ENTRIES (1 << EFX_MCAST_HASH_BITS)
  568. /* An Efx multicast filter hash */
  569. union efx_multicast_hash {
  570. u8 byte[EFX_MCAST_HASH_ENTRIES / 8];
  571. efx_oword_t oword[EFX_MCAST_HASH_ENTRIES / sizeof(efx_oword_t) / 8];
  572. };
  573. struct efx_filter_state;
  574. /**
  575. * struct efx_nic - an Efx NIC
  576. * @name: Device name (net device name or bus id before net device registered)
  577. * @pci_dev: The PCI device
  578. * @type: Controller type attributes
  579. * @legacy_irq: IRQ number
  580. * @legacy_irq_enabled: Are IRQs enabled on NIC (INT_EN_KER register)?
  581. * @workqueue: Workqueue for port reconfigures and the HW monitor.
  582. * Work items do not hold and must not acquire RTNL.
  583. * @workqueue_name: Name of workqueue
  584. * @reset_work: Scheduled reset workitem
  585. * @membase_phys: Memory BAR value as physical address
  586. * @membase: Memory BAR value
  587. * @interrupt_mode: Interrupt mode
  588. * @irq_rx_adaptive: Adaptive IRQ moderation enabled for RX event queues
  589. * @irq_rx_moderation: IRQ moderation time for RX event queues
  590. * @msg_enable: Log message enable flags
  591. * @state: Device state flag. Serialised by the rtnl_lock.
  592. * @reset_pending: Bitmask for pending resets
  593. * @tx_queue: TX DMA queues
  594. * @rx_queue: RX DMA queues
  595. * @channel: Channels
  596. * @channel_name: Names for channels and their IRQs
  597. * @rxq_entries: Size of receive queues requested by user.
  598. * @txq_entries: Size of transmit queues requested by user.
  599. * @next_buffer_table: First available buffer table id
  600. * @n_channels: Number of channels in use
  601. * @n_rx_channels: Number of channels used for RX (= number of RX queues)
  602. * @n_tx_channels: Number of channels used for TX
  603. * @rx_buffer_len: RX buffer length
  604. * @rx_buffer_order: Order (log2) of number of pages for each RX buffer
  605. * @rx_hash_key: Toeplitz hash key for RSS
  606. * @rx_indir_table: Indirection table for RSS
  607. * @int_error_count: Number of internal errors seen recently
  608. * @int_error_expire: Time at which error count will be expired
  609. * @irq_status: Interrupt status buffer
  610. * @irq_zero_count: Number of legacy IRQs seen with queue flags == 0
  611. * @fatal_irq_level: IRQ level (bit number) used for serious errors
  612. * @mtd_list: List of MTDs attached to the NIC
  613. * @nic_data: Hardware dependent state
  614. * @mac_lock: MAC access lock. Protects @port_enabled, @phy_mode,
  615. * efx_monitor() and efx_reconfigure_port()
  616. * @port_enabled: Port enabled indicator.
  617. * Serialises efx_stop_all(), efx_start_all(), efx_monitor() and
  618. * efx_mac_work() with kernel interfaces. Safe to read under any
  619. * one of the rtnl_lock, mac_lock, or netif_tx_lock, but all three must
  620. * be held to modify it.
  621. * @port_initialized: Port initialized?
  622. * @net_dev: Operating system network device. Consider holding the rtnl lock
  623. * @stats_buffer: DMA buffer for statistics
  624. * @mac_op: MAC interface
  625. * @phy_type: PHY type
  626. * @phy_op: PHY interface
  627. * @phy_data: PHY private data (including PHY-specific stats)
  628. * @mdio: PHY MDIO interface
  629. * @mdio_bus: PHY MDIO bus ID (only used by Siena)
  630. * @phy_mode: PHY operating mode. Serialised by @mac_lock.
  631. * @link_advertising: Autonegotiation advertising flags
  632. * @link_state: Current state of the link
  633. * @n_link_state_changes: Number of times the link has changed state
  634. * @promiscuous: Promiscuous flag. Protected by netif_tx_lock.
  635. * @multicast_hash: Multicast hash table
  636. * @wanted_fc: Wanted flow control flags
  637. * @mac_work: Work item for changing MAC promiscuity and multicast hash
  638. * @loopback_mode: Loopback status
  639. * @loopback_modes: Supported loopback mode bitmask
  640. * @loopback_selftest: Offline self-test private state
  641. * @monitor_work: Hardware monitor workitem
  642. * @biu_lock: BIU (bus interface unit) lock
  643. * @last_irq_cpu: Last CPU to handle interrupt.
  644. * This register is written with the SMP processor ID whenever an
  645. * interrupt is handled. It is used by efx_nic_test_interrupt()
  646. * to verify that an interrupt has occurred.
  647. * @n_rx_nodesc_drop_cnt: RX no descriptor drop count
  648. * @mac_stats: MAC statistics. These include all statistics the MACs
  649. * can provide. Generic code converts these into a standard
  650. * &struct net_device_stats.
  651. * @stats_lock: Statistics update lock. Serialises statistics fetches
  652. *
  653. * This is stored in the private area of the &struct net_device.
  654. */
  655. struct efx_nic {
  656. /* The following fields should be written very rarely */
  657. char name[IFNAMSIZ];
  658. struct pci_dev *pci_dev;
  659. const struct efx_nic_type *type;
  660. int legacy_irq;
  661. bool legacy_irq_enabled;
  662. struct workqueue_struct *workqueue;
  663. char workqueue_name[16];
  664. struct work_struct reset_work;
  665. resource_size_t membase_phys;
  666. void __iomem *membase;
  667. enum efx_int_mode interrupt_mode;
  668. bool irq_rx_adaptive;
  669. unsigned int irq_rx_moderation;
  670. u32 msg_enable;
  671. enum nic_state state;
  672. unsigned long reset_pending;
  673. struct efx_channel *channel[EFX_MAX_CHANNELS];
  674. char channel_name[EFX_MAX_CHANNELS][IFNAMSIZ + 6];
  675. unsigned rxq_entries;
  676. unsigned txq_entries;
  677. unsigned next_buffer_table;
  678. unsigned n_channels;
  679. unsigned n_rx_channels;
  680. unsigned tx_channel_offset;
  681. unsigned n_tx_channels;
  682. unsigned int rx_buffer_len;
  683. unsigned int rx_buffer_order;
  684. u8 rx_hash_key[40];
  685. u32 rx_indir_table[128];
  686. unsigned int_error_count;
  687. unsigned long int_error_expire;
  688. struct efx_buffer irq_status;
  689. unsigned irq_zero_count;
  690. unsigned fatal_irq_level;
  691. #ifdef CONFIG_SFC_MTD
  692. struct list_head mtd_list;
  693. #endif
  694. void *nic_data;
  695. struct mutex mac_lock;
  696. struct work_struct mac_work;
  697. bool port_enabled;
  698. bool port_initialized;
  699. struct net_device *net_dev;
  700. struct efx_buffer stats_buffer;
  701. const struct efx_mac_operations *mac_op;
  702. unsigned int phy_type;
  703. const struct efx_phy_operations *phy_op;
  704. void *phy_data;
  705. struct mdio_if_info mdio;
  706. unsigned int mdio_bus;
  707. enum efx_phy_mode phy_mode;
  708. u32 link_advertising;
  709. struct efx_link_state link_state;
  710. unsigned int n_link_state_changes;
  711. bool promiscuous;
  712. union efx_multicast_hash multicast_hash;
  713. u8 wanted_fc;
  714. atomic_t rx_reset;
  715. enum efx_loopback_mode loopback_mode;
  716. u64 loopback_modes;
  717. void *loopback_selftest;
  718. struct efx_filter_state *filter_state;
  719. /* The following fields may be written more often */
  720. struct delayed_work monitor_work ____cacheline_aligned_in_smp;
  721. spinlock_t biu_lock;
  722. volatile signed int last_irq_cpu;
  723. unsigned n_rx_nodesc_drop_cnt;
  724. struct efx_mac_stats mac_stats;
  725. spinlock_t stats_lock;
  726. };
  727. static inline int efx_dev_registered(struct efx_nic *efx)
  728. {
  729. return efx->net_dev->reg_state == NETREG_REGISTERED;
  730. }
  731. /* Net device name, for inclusion in log messages if it has been registered.
  732. * Use efx->name not efx->net_dev->name so that races with (un)registration
  733. * are harmless.
  734. */
  735. static inline const char *efx_dev_name(struct efx_nic *efx)
  736. {
  737. return efx_dev_registered(efx) ? efx->name : "";
  738. }
  739. static inline unsigned int efx_port_num(struct efx_nic *efx)
  740. {
  741. return efx->net_dev->dev_id;
  742. }
  743. /**
  744. * struct efx_nic_type - Efx device type definition
  745. * @probe: Probe the controller
  746. * @remove: Free resources allocated by probe()
  747. * @init: Initialise the controller
  748. * @fini: Shut down the controller
  749. * @monitor: Periodic function for polling link state and hardware monitor
  750. * @map_reset_reason: Map ethtool reset reason to a reset method
  751. * @map_reset_flags: Map ethtool reset flags to a reset method, if possible
  752. * @reset: Reset the controller hardware and possibly the PHY. This will
  753. * be called while the controller is uninitialised.
  754. * @probe_port: Probe the MAC and PHY
  755. * @remove_port: Free resources allocated by probe_port()
  756. * @handle_global_event: Handle a "global" event (may be %NULL)
  757. * @prepare_flush: Prepare the hardware for flushing the DMA queues
  758. * @update_stats: Update statistics not provided by event handling
  759. * @start_stats: Start the regular fetching of statistics
  760. * @stop_stats: Stop the regular fetching of statistics
  761. * @set_id_led: Set state of identifying LED or revert to automatic function
  762. * @push_irq_moderation: Apply interrupt moderation value
  763. * @push_multicast_hash: Apply multicast hash table
  764. * @reconfigure_port: Push loopback/power/txdis changes to the MAC and PHY
  765. * @get_wol: Get WoL configuration from driver state
  766. * @set_wol: Push WoL configuration to the NIC
  767. * @resume_wol: Synchronise WoL state between driver and MC (e.g. after resume)
  768. * @test_registers: Test read/write functionality of control registers
  769. * @test_nvram: Test validity of NVRAM contents
  770. * @default_mac_ops: efx_mac_operations to set at startup
  771. * @revision: Hardware architecture revision
  772. * @mem_map_size: Memory BAR mapped size
  773. * @txd_ptr_tbl_base: TX descriptor ring base address
  774. * @rxd_ptr_tbl_base: RX descriptor ring base address
  775. * @buf_tbl_base: Buffer table base address
  776. * @evq_ptr_tbl_base: Event queue pointer table base address
  777. * @evq_rptr_tbl_base: Event queue read-pointer table base address
  778. * @max_dma_mask: Maximum possible DMA mask
  779. * @rx_buffer_hash_size: Size of hash at start of RX buffer
  780. * @rx_buffer_padding: Size of padding at end of RX buffer
  781. * @max_interrupt_mode: Highest capability interrupt mode supported
  782. * from &enum efx_init_mode.
  783. * @phys_addr_channels: Number of channels with physically addressed
  784. * descriptors
  785. * @tx_dc_base: Base address in SRAM of TX queue descriptor caches
  786. * @rx_dc_base: Base address in SRAM of RX queue descriptor caches
  787. * @offload_features: net_device feature flags for protocol offload
  788. * features implemented in hardware
  789. */
  790. struct efx_nic_type {
  791. int (*probe)(struct efx_nic *efx);
  792. void (*remove)(struct efx_nic *efx);
  793. int (*init)(struct efx_nic *efx);
  794. void (*fini)(struct efx_nic *efx);
  795. void (*monitor)(struct efx_nic *efx);
  796. enum reset_type (*map_reset_reason)(enum reset_type reason);
  797. int (*map_reset_flags)(u32 *flags);
  798. int (*reset)(struct efx_nic *efx, enum reset_type method);
  799. int (*probe_port)(struct efx_nic *efx);
  800. void (*remove_port)(struct efx_nic *efx);
  801. bool (*handle_global_event)(struct efx_channel *channel, efx_qword_t *);
  802. void (*prepare_flush)(struct efx_nic *efx);
  803. void (*update_stats)(struct efx_nic *efx);
  804. void (*start_stats)(struct efx_nic *efx);
  805. void (*stop_stats)(struct efx_nic *efx);
  806. void (*set_id_led)(struct efx_nic *efx, enum efx_led_mode mode);
  807. void (*push_irq_moderation)(struct efx_channel *channel);
  808. void (*push_multicast_hash)(struct efx_nic *efx);
  809. int (*reconfigure_port)(struct efx_nic *efx);
  810. void (*get_wol)(struct efx_nic *efx, struct ethtool_wolinfo *wol);
  811. int (*set_wol)(struct efx_nic *efx, u32 type);
  812. void (*resume_wol)(struct efx_nic *efx);
  813. int (*test_registers)(struct efx_nic *efx);
  814. int (*test_nvram)(struct efx_nic *efx);
  815. const struct efx_mac_operations *default_mac_ops;
  816. int revision;
  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. u64 max_dma_mask;
  824. unsigned int rx_buffer_hash_size;
  825. unsigned int rx_buffer_padding;
  826. unsigned int max_interrupt_mode;
  827. unsigned int phys_addr_channels;
  828. unsigned int tx_dc_base;
  829. unsigned int rx_dc_base;
  830. netdev_features_t offload_features;
  831. };
  832. /**************************************************************************
  833. *
  834. * Prototypes and inline functions
  835. *
  836. *************************************************************************/
  837. static inline struct efx_channel *
  838. efx_get_channel(struct efx_nic *efx, unsigned index)
  839. {
  840. EFX_BUG_ON_PARANOID(index >= efx->n_channels);
  841. return efx->channel[index];
  842. }
  843. /* Iterate over all used channels */
  844. #define efx_for_each_channel(_channel, _efx) \
  845. for (_channel = (_efx)->channel[0]; \
  846. _channel; \
  847. _channel = (_channel->channel + 1 < (_efx)->n_channels) ? \
  848. (_efx)->channel[_channel->channel + 1] : NULL)
  849. static inline struct efx_tx_queue *
  850. efx_get_tx_queue(struct efx_nic *efx, unsigned index, unsigned type)
  851. {
  852. EFX_BUG_ON_PARANOID(index >= efx->n_tx_channels ||
  853. type >= EFX_TXQ_TYPES);
  854. return &efx->channel[efx->tx_channel_offset + index]->tx_queue[type];
  855. }
  856. static inline bool efx_channel_has_tx_queues(struct efx_channel *channel)
  857. {
  858. return channel->channel - channel->efx->tx_channel_offset <
  859. channel->efx->n_tx_channels;
  860. }
  861. static inline struct efx_tx_queue *
  862. efx_channel_get_tx_queue(struct efx_channel *channel, unsigned type)
  863. {
  864. EFX_BUG_ON_PARANOID(!efx_channel_has_tx_queues(channel) ||
  865. type >= EFX_TXQ_TYPES);
  866. return &channel->tx_queue[type];
  867. }
  868. static inline bool efx_tx_queue_used(struct efx_tx_queue *tx_queue)
  869. {
  870. return !(tx_queue->efx->net_dev->num_tc < 2 &&
  871. tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI);
  872. }
  873. /* Iterate over all TX queues belonging to a channel */
  874. #define efx_for_each_channel_tx_queue(_tx_queue, _channel) \
  875. if (!efx_channel_has_tx_queues(_channel)) \
  876. ; \
  877. else \
  878. for (_tx_queue = (_channel)->tx_queue; \
  879. _tx_queue < (_channel)->tx_queue + EFX_TXQ_TYPES && \
  880. efx_tx_queue_used(_tx_queue); \
  881. _tx_queue++)
  882. /* Iterate over all possible TX queues belonging to a channel */
  883. #define efx_for_each_possible_channel_tx_queue(_tx_queue, _channel) \
  884. for (_tx_queue = (_channel)->tx_queue; \
  885. _tx_queue < (_channel)->tx_queue + EFX_TXQ_TYPES; \
  886. _tx_queue++)
  887. static inline struct efx_rx_queue *
  888. efx_get_rx_queue(struct efx_nic *efx, unsigned index)
  889. {
  890. EFX_BUG_ON_PARANOID(index >= efx->n_rx_channels);
  891. return &efx->channel[index]->rx_queue;
  892. }
  893. static inline bool efx_channel_has_rx_queue(struct efx_channel *channel)
  894. {
  895. return channel->channel < channel->efx->n_rx_channels;
  896. }
  897. static inline struct efx_rx_queue *
  898. efx_channel_get_rx_queue(struct efx_channel *channel)
  899. {
  900. EFX_BUG_ON_PARANOID(!efx_channel_has_rx_queue(channel));
  901. return &channel->rx_queue;
  902. }
  903. /* Iterate over all RX queues belonging to a channel */
  904. #define efx_for_each_channel_rx_queue(_rx_queue, _channel) \
  905. if (!efx_channel_has_rx_queue(_channel)) \
  906. ; \
  907. else \
  908. for (_rx_queue = &(_channel)->rx_queue; \
  909. _rx_queue; \
  910. _rx_queue = NULL)
  911. static inline struct efx_channel *
  912. efx_rx_queue_channel(struct efx_rx_queue *rx_queue)
  913. {
  914. return container_of(rx_queue, struct efx_channel, rx_queue);
  915. }
  916. static inline int efx_rx_queue_index(struct efx_rx_queue *rx_queue)
  917. {
  918. return efx_rx_queue_channel(rx_queue)->channel;
  919. }
  920. /* Returns a pointer to the specified receive buffer in the RX
  921. * descriptor queue.
  922. */
  923. static inline struct efx_rx_buffer *efx_rx_buffer(struct efx_rx_queue *rx_queue,
  924. unsigned int index)
  925. {
  926. return &rx_queue->buffer[index];
  927. }
  928. /* Set bit in a little-endian bitfield */
  929. static inline void set_bit_le(unsigned nr, unsigned char *addr)
  930. {
  931. addr[nr / 8] |= (1 << (nr % 8));
  932. }
  933. /* Clear bit in a little-endian bitfield */
  934. static inline void clear_bit_le(unsigned nr, unsigned char *addr)
  935. {
  936. addr[nr / 8] &= ~(1 << (nr % 8));
  937. }
  938. /**
  939. * EFX_MAX_FRAME_LEN - calculate maximum frame length
  940. *
  941. * This calculates the maximum frame length that will be used for a
  942. * given MTU. The frame length will be equal to the MTU plus a
  943. * constant amount of header space and padding. This is the quantity
  944. * that the net driver will program into the MAC as the maximum frame
  945. * length.
  946. *
  947. * The 10G MAC requires 8-byte alignment on the frame
  948. * length, so we round up to the nearest 8.
  949. *
  950. * Re-clocking by the XGXS on RX can reduce an IPG to 32 bits (half an
  951. * XGMII cycle). If the frame length reaches the maximum value in the
  952. * same cycle, the XMAC can miss the IPG altogether. We work around
  953. * this by adding a further 16 bytes.
  954. */
  955. #define EFX_MAX_FRAME_LEN(mtu) \
  956. ((((mtu) + ETH_HLEN + VLAN_HLEN + 4/* FCS */ + 7) & ~7) + 16)
  957. #endif /* EFX_NET_DRIVER_H */