net_driver.h 32 KB

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