net_driver.h 32 KB

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