net_driver.h 32 KB

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