net_driver.h 33 KB

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