net_driver.h 34 KB

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