net_driver.h 34 KB

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