net_driver.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2013 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 EF4_NET_DRIVER_H
  12. #define EF4_NET_DRIVER_H
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/timer.h>
  18. #include <linux/mdio.h>
  19. #include <linux/list.h>
  20. #include <linux/pci.h>
  21. #include <linux/device.h>
  22. #include <linux/highmem.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/mutex.h>
  25. #include <linux/rwsem.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/i2c.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <net/busy_poll.h>
  30. #include "enum.h"
  31. #include "bitfield.h"
  32. #include "filter.h"
  33. /**************************************************************************
  34. *
  35. * Build definitions
  36. *
  37. **************************************************************************/
  38. #define EF4_DRIVER_VERSION "4.1"
  39. #ifdef DEBUG
  40. #define EF4_BUG_ON_PARANOID(x) BUG_ON(x)
  41. #define EF4_WARN_ON_PARANOID(x) WARN_ON(x)
  42. #else
  43. #define EF4_BUG_ON_PARANOID(x) do {} while (0)
  44. #define EF4_WARN_ON_PARANOID(x) do {} while (0)
  45. #endif
  46. /**************************************************************************
  47. *
  48. * Efx data structures
  49. *
  50. **************************************************************************/
  51. #define EF4_MAX_CHANNELS 32U
  52. #define EF4_MAX_RX_QUEUES EF4_MAX_CHANNELS
  53. #define EF4_EXTRA_CHANNEL_IOV 0
  54. #define EF4_EXTRA_CHANNEL_PTP 1
  55. #define EF4_MAX_EXTRA_CHANNELS 2U
  56. /* Checksum generation is a per-queue option in hardware, so each
  57. * queue visible to the networking core is backed by two hardware TX
  58. * queues. */
  59. #define EF4_MAX_TX_TC 2
  60. #define EF4_MAX_CORE_TX_QUEUES (EF4_MAX_TX_TC * EF4_MAX_CHANNELS)
  61. #define EF4_TXQ_TYPE_OFFLOAD 1 /* flag */
  62. #define EF4_TXQ_TYPE_HIGHPRI 2 /* flag */
  63. #define EF4_TXQ_TYPES 4
  64. #define EF4_MAX_TX_QUEUES (EF4_TXQ_TYPES * EF4_MAX_CHANNELS)
  65. /* Maximum possible MTU the driver supports */
  66. #define EF4_MAX_MTU (9 * 1024)
  67. /* Minimum MTU, from RFC791 (IP) */
  68. #define EF4_MIN_MTU 68
  69. /* Size of an RX scatter buffer. Small enough to pack 2 into a 4K page,
  70. * and should be a multiple of the cache line size.
  71. */
  72. #define EF4_RX_USR_BUF_SIZE (2048 - 256)
  73. /* If possible, we should ensure cache line alignment at start and end
  74. * of every buffer. Otherwise, we just need to ensure 4-byte
  75. * alignment of the network header.
  76. */
  77. #if NET_IP_ALIGN == 0
  78. #define EF4_RX_BUF_ALIGNMENT L1_CACHE_BYTES
  79. #else
  80. #define EF4_RX_BUF_ALIGNMENT 4
  81. #endif
  82. struct ef4_self_tests;
  83. /**
  84. * struct ef4_buffer - A general-purpose DMA buffer
  85. * @addr: host base address of the buffer
  86. * @dma_addr: DMA base address of the buffer
  87. * @len: Buffer length, in bytes
  88. *
  89. * The NIC uses these buffers for its interrupt status registers and
  90. * MAC stats dumps.
  91. */
  92. struct ef4_buffer {
  93. void *addr;
  94. dma_addr_t dma_addr;
  95. unsigned int len;
  96. };
  97. /**
  98. * struct ef4_special_buffer - DMA buffer entered into buffer table
  99. * @buf: Standard &struct ef4_buffer
  100. * @index: Buffer index within controller;s buffer table
  101. * @entries: Number of buffer table entries
  102. *
  103. * The NIC has a buffer table that maps buffers of size %EF4_BUF_SIZE.
  104. * Event and descriptor rings are addressed via one or more buffer
  105. * table entries (and so can be physically non-contiguous, although we
  106. * currently do not take advantage of that). On Falcon and Siena we
  107. * have to take care of allocating and initialising the entries
  108. * ourselves. On later hardware this is managed by the firmware and
  109. * @index and @entries are left as 0.
  110. */
  111. struct ef4_special_buffer {
  112. struct ef4_buffer buf;
  113. unsigned int index;
  114. unsigned int entries;
  115. };
  116. /**
  117. * struct ef4_tx_buffer - buffer state for a TX descriptor
  118. * @skb: When @flags & %EF4_TX_BUF_SKB, the associated socket buffer to be
  119. * freed when descriptor completes
  120. * @option: When @flags & %EF4_TX_BUF_OPTION, a NIC-specific option descriptor.
  121. * @dma_addr: DMA address of the fragment.
  122. * @flags: Flags for allocation and DMA mapping type
  123. * @len: Length of this fragment.
  124. * This field is zero when the queue slot is empty.
  125. * @unmap_len: Length of this fragment to unmap
  126. * @dma_offset: Offset of @dma_addr from the address of the backing DMA mapping.
  127. * Only valid if @unmap_len != 0.
  128. */
  129. struct ef4_tx_buffer {
  130. const struct sk_buff *skb;
  131. union {
  132. ef4_qword_t option;
  133. dma_addr_t dma_addr;
  134. };
  135. unsigned short flags;
  136. unsigned short len;
  137. unsigned short unmap_len;
  138. unsigned short dma_offset;
  139. };
  140. #define EF4_TX_BUF_CONT 1 /* not last descriptor of packet */
  141. #define EF4_TX_BUF_SKB 2 /* buffer is last part of skb */
  142. #define EF4_TX_BUF_MAP_SINGLE 8 /* buffer was mapped with dma_map_single() */
  143. #define EF4_TX_BUF_OPTION 0x10 /* empty buffer for option descriptor */
  144. /**
  145. * struct ef4_tx_queue - An Efx TX queue
  146. *
  147. * This is a ring buffer of TX fragments.
  148. * Since the TX completion path always executes on the same
  149. * CPU and the xmit path can operate on different CPUs,
  150. * performance is increased by ensuring that the completion
  151. * path and the xmit path operate on different cache lines.
  152. * This is particularly important if the xmit path is always
  153. * executing on one CPU which is different from the completion
  154. * path. There is also a cache line for members which are
  155. * read but not written on the fast path.
  156. *
  157. * @efx: The associated Efx NIC
  158. * @queue: DMA queue number
  159. * @channel: The associated channel
  160. * @core_txq: The networking core TX queue structure
  161. * @buffer: The software buffer ring
  162. * @cb_page: Array of pages of copy buffers. Carved up according to
  163. * %EF4_TX_CB_ORDER into %EF4_TX_CB_SIZE-sized chunks.
  164. * @txd: The hardware descriptor ring
  165. * @ptr_mask: The size of the ring minus 1.
  166. * @initialised: Has hardware queue been initialised?
  167. * @tx_min_size: Minimum transmit size for this queue. Depends on HW.
  168. * @read_count: Current read pointer.
  169. * This is the number of buffers that have been removed from both rings.
  170. * @old_write_count: The value of @write_count when last checked.
  171. * This is here for performance reasons. The xmit path will
  172. * only get the up-to-date value of @write_count if this
  173. * variable indicates that the queue is empty. This is to
  174. * avoid cache-line ping-pong between the xmit path and the
  175. * completion path.
  176. * @merge_events: Number of TX merged completion events
  177. * @insert_count: Current insert pointer
  178. * This is the number of buffers that have been added to the
  179. * software ring.
  180. * @write_count: Current write pointer
  181. * This is the number of buffers that have been added to the
  182. * hardware ring.
  183. * @old_read_count: The value of read_count when last checked.
  184. * This is here for performance reasons. The xmit path will
  185. * only get the up-to-date value of read_count if this
  186. * variable indicates that the queue is full. This is to
  187. * avoid cache-line ping-pong between the xmit path and the
  188. * completion path.
  189. * @pushes: Number of times the TX push feature has been used
  190. * @xmit_more_available: Are any packets waiting to be pushed to the NIC
  191. * @cb_packets: Number of times the TX copybreak feature has been used
  192. * @empty_read_count: If the completion path has seen the queue as empty
  193. * and the transmission path has not yet checked this, the value of
  194. * @read_count bitwise-added to %EF4_EMPTY_COUNT_VALID; otherwise 0.
  195. */
  196. struct ef4_tx_queue {
  197. /* Members which don't change on the fast path */
  198. struct ef4_nic *efx ____cacheline_aligned_in_smp;
  199. unsigned queue;
  200. struct ef4_channel *channel;
  201. struct netdev_queue *core_txq;
  202. struct ef4_tx_buffer *buffer;
  203. struct ef4_buffer *cb_page;
  204. struct ef4_special_buffer txd;
  205. unsigned int ptr_mask;
  206. bool initialised;
  207. unsigned int tx_min_size;
  208. /* Function pointers used in the fast path. */
  209. int (*handle_tso)(struct ef4_tx_queue*, struct sk_buff*, bool *);
  210. /* Members used mainly on the completion path */
  211. unsigned int read_count ____cacheline_aligned_in_smp;
  212. unsigned int old_write_count;
  213. unsigned int merge_events;
  214. unsigned int bytes_compl;
  215. unsigned int pkts_compl;
  216. /* Members used only on the xmit path */
  217. unsigned int insert_count ____cacheline_aligned_in_smp;
  218. unsigned int write_count;
  219. unsigned int old_read_count;
  220. unsigned int pushes;
  221. bool xmit_more_available;
  222. unsigned int cb_packets;
  223. /* Statistics to supplement MAC stats */
  224. unsigned long tx_packets;
  225. /* Members shared between paths and sometimes updated */
  226. unsigned int empty_read_count ____cacheline_aligned_in_smp;
  227. #define EF4_EMPTY_COUNT_VALID 0x80000000
  228. atomic_t flush_outstanding;
  229. };
  230. #define EF4_TX_CB_ORDER 7
  231. #define EF4_TX_CB_SIZE (1 << EF4_TX_CB_ORDER) - NET_IP_ALIGN
  232. /**
  233. * struct ef4_rx_buffer - An Efx RX data buffer
  234. * @dma_addr: DMA base address of the buffer
  235. * @page: The associated page buffer.
  236. * Will be %NULL if the buffer slot is currently free.
  237. * @page_offset: If pending: offset in @page of DMA base address.
  238. * If completed: offset in @page of Ethernet header.
  239. * @len: If pending: length for DMA descriptor.
  240. * If completed: received length, excluding hash prefix.
  241. * @flags: Flags for buffer and packet state. These are only set on the
  242. * first buffer of a scattered packet.
  243. */
  244. struct ef4_rx_buffer {
  245. dma_addr_t dma_addr;
  246. struct page *page;
  247. u16 page_offset;
  248. u16 len;
  249. u16 flags;
  250. };
  251. #define EF4_RX_BUF_LAST_IN_PAGE 0x0001
  252. #define EF4_RX_PKT_CSUMMED 0x0002
  253. #define EF4_RX_PKT_DISCARD 0x0004
  254. #define EF4_RX_PKT_TCP 0x0040
  255. #define EF4_RX_PKT_PREFIX_LEN 0x0080 /* length is in prefix only */
  256. /**
  257. * struct ef4_rx_page_state - Page-based rx buffer state
  258. *
  259. * Inserted at the start of every page allocated for receive buffers.
  260. * Used to facilitate sharing dma mappings between recycled rx buffers
  261. * and those passed up to the kernel.
  262. *
  263. * @dma_addr: The dma address of this page.
  264. */
  265. struct ef4_rx_page_state {
  266. dma_addr_t dma_addr;
  267. unsigned int __pad[0] ____cacheline_aligned;
  268. };
  269. /**
  270. * struct ef4_rx_queue - An Efx RX queue
  271. * @efx: The associated Efx NIC
  272. * @core_index: Index of network core RX queue. Will be >= 0 iff this
  273. * is associated with a real RX queue.
  274. * @buffer: The software buffer ring
  275. * @rxd: The hardware descriptor ring
  276. * @ptr_mask: The size of the ring minus 1.
  277. * @refill_enabled: Enable refill whenever fill level is low
  278. * @flush_pending: Set when a RX flush is pending. Has the same lifetime as
  279. * @rxq_flush_pending.
  280. * @added_count: Number of buffers added to the receive queue.
  281. * @notified_count: Number of buffers given to NIC (<= @added_count).
  282. * @removed_count: Number of buffers removed from the receive queue.
  283. * @scatter_n: Used by NIC specific receive code.
  284. * @scatter_len: Used by NIC specific receive code.
  285. * @page_ring: The ring to store DMA mapped pages for reuse.
  286. * @page_add: Counter to calculate the write pointer for the recycle ring.
  287. * @page_remove: Counter to calculate the read pointer for the recycle ring.
  288. * @page_recycle_count: The number of pages that have been recycled.
  289. * @page_recycle_failed: The number of pages that couldn't be recycled because
  290. * the kernel still held a reference to them.
  291. * @page_recycle_full: The number of pages that were released because the
  292. * recycle ring was full.
  293. * @page_ptr_mask: The number of pages in the RX recycle ring minus 1.
  294. * @max_fill: RX descriptor maximum fill level (<= ring size)
  295. * @fast_fill_trigger: RX descriptor fill level that will trigger a fast fill
  296. * (<= @max_fill)
  297. * @min_fill: RX descriptor minimum non-zero fill level.
  298. * This records the minimum fill level observed when a ring
  299. * refill was triggered.
  300. * @recycle_count: RX buffer recycle counter.
  301. * @slow_fill: Timer used to defer ef4_nic_generate_fill_event().
  302. */
  303. struct ef4_rx_queue {
  304. struct ef4_nic *efx;
  305. int core_index;
  306. struct ef4_rx_buffer *buffer;
  307. struct ef4_special_buffer rxd;
  308. unsigned int ptr_mask;
  309. bool refill_enabled;
  310. bool flush_pending;
  311. unsigned int added_count;
  312. unsigned int notified_count;
  313. unsigned int removed_count;
  314. unsigned int scatter_n;
  315. unsigned int scatter_len;
  316. struct page **page_ring;
  317. unsigned int page_add;
  318. unsigned int page_remove;
  319. unsigned int page_recycle_count;
  320. unsigned int page_recycle_failed;
  321. unsigned int page_recycle_full;
  322. unsigned int page_ptr_mask;
  323. unsigned int max_fill;
  324. unsigned int fast_fill_trigger;
  325. unsigned int min_fill;
  326. unsigned int min_overfill;
  327. unsigned int recycle_count;
  328. struct timer_list slow_fill;
  329. unsigned int slow_fill_count;
  330. /* Statistics to supplement MAC stats */
  331. unsigned long rx_packets;
  332. };
  333. /**
  334. * struct ef4_channel - An Efx channel
  335. *
  336. * A channel comprises an event queue, at least one TX queue, at least
  337. * one RX queue, and an associated tasklet for processing the event
  338. * queue.
  339. *
  340. * @efx: Associated Efx NIC
  341. * @channel: Channel instance number
  342. * @type: Channel type definition
  343. * @eventq_init: Event queue initialised flag
  344. * @enabled: Channel enabled indicator
  345. * @irq: IRQ number (MSI and MSI-X only)
  346. * @irq_moderation_us: IRQ moderation value (in microseconds)
  347. * @napi_dev: Net device used with NAPI
  348. * @napi_str: NAPI control structure
  349. * @state: state for NAPI vs busy polling
  350. * @state_lock: lock protecting @state
  351. * @eventq: Event queue buffer
  352. * @eventq_mask: Event queue pointer mask
  353. * @eventq_read_ptr: Event queue read pointer
  354. * @event_test_cpu: Last CPU to handle interrupt or test event for this channel
  355. * @irq_count: Number of IRQs since last adaptive moderation decision
  356. * @irq_mod_score: IRQ moderation score
  357. * @rps_flow_id: Flow IDs of filters allocated for accelerated RFS,
  358. * indexed by filter ID
  359. * @n_rx_tobe_disc: Count of RX_TOBE_DISC errors
  360. * @n_rx_ip_hdr_chksum_err: Count of RX IP header checksum errors
  361. * @n_rx_tcp_udp_chksum_err: Count of RX TCP and UDP checksum errors
  362. * @n_rx_mcast_mismatch: Count of unmatched multicast frames
  363. * @n_rx_frm_trunc: Count of RX_FRM_TRUNC errors
  364. * @n_rx_overlength: Count of RX_OVERLENGTH errors
  365. * @n_skbuff_leaks: Count of skbuffs leaked due to RX overrun
  366. * @n_rx_nodesc_trunc: Number of RX packets truncated and then dropped due to
  367. * lack of descriptors
  368. * @n_rx_merge_events: Number of RX merged completion events
  369. * @n_rx_merge_packets: Number of RX packets completed by merged events
  370. * @rx_pkt_n_frags: Number of fragments in next packet to be delivered by
  371. * __ef4_rx_packet(), or zero if there is none
  372. * @rx_pkt_index: Ring index of first buffer for next packet to be delivered
  373. * by __ef4_rx_packet(), if @rx_pkt_n_frags != 0
  374. * @rx_queue: RX queue for this channel
  375. * @tx_queue: TX queues for this channel
  376. */
  377. struct ef4_channel {
  378. struct ef4_nic *efx;
  379. int channel;
  380. const struct ef4_channel_type *type;
  381. bool eventq_init;
  382. bool enabled;
  383. int irq;
  384. unsigned int irq_moderation_us;
  385. struct net_device *napi_dev;
  386. struct napi_struct napi_str;
  387. #ifdef CONFIG_NET_RX_BUSY_POLL
  388. unsigned long busy_poll_state;
  389. #endif
  390. struct ef4_special_buffer eventq;
  391. unsigned int eventq_mask;
  392. unsigned int eventq_read_ptr;
  393. int event_test_cpu;
  394. unsigned int irq_count;
  395. unsigned int irq_mod_score;
  396. #ifdef CONFIG_RFS_ACCEL
  397. unsigned int rfs_filters_added;
  398. #define RPS_FLOW_ID_INVALID 0xFFFFFFFF
  399. u32 *rps_flow_id;
  400. #endif
  401. unsigned n_rx_tobe_disc;
  402. unsigned n_rx_ip_hdr_chksum_err;
  403. unsigned n_rx_tcp_udp_chksum_err;
  404. unsigned n_rx_mcast_mismatch;
  405. unsigned n_rx_frm_trunc;
  406. unsigned n_rx_overlength;
  407. unsigned n_skbuff_leaks;
  408. unsigned int n_rx_nodesc_trunc;
  409. unsigned int n_rx_merge_events;
  410. unsigned int n_rx_merge_packets;
  411. unsigned int rx_pkt_n_frags;
  412. unsigned int rx_pkt_index;
  413. struct ef4_rx_queue rx_queue;
  414. struct ef4_tx_queue tx_queue[EF4_TXQ_TYPES];
  415. };
  416. #ifdef CONFIG_NET_RX_BUSY_POLL
  417. enum ef4_channel_busy_poll_state {
  418. EF4_CHANNEL_STATE_IDLE = 0,
  419. EF4_CHANNEL_STATE_NAPI = BIT(0),
  420. EF4_CHANNEL_STATE_NAPI_REQ_BIT = 1,
  421. EF4_CHANNEL_STATE_NAPI_REQ = BIT(1),
  422. EF4_CHANNEL_STATE_POLL_BIT = 2,
  423. EF4_CHANNEL_STATE_POLL = BIT(2),
  424. EF4_CHANNEL_STATE_DISABLE_BIT = 3,
  425. };
  426. static inline void ef4_channel_busy_poll_init(struct ef4_channel *channel)
  427. {
  428. WRITE_ONCE(channel->busy_poll_state, EF4_CHANNEL_STATE_IDLE);
  429. }
  430. /* Called from the device poll routine to get ownership of a channel. */
  431. static inline bool ef4_channel_lock_napi(struct ef4_channel *channel)
  432. {
  433. unsigned long prev, old = READ_ONCE(channel->busy_poll_state);
  434. while (1) {
  435. switch (old) {
  436. case EF4_CHANNEL_STATE_POLL:
  437. /* Ensure ef4_channel_try_lock_poll() wont starve us */
  438. set_bit(EF4_CHANNEL_STATE_NAPI_REQ_BIT,
  439. &channel->busy_poll_state);
  440. /* fallthrough */
  441. case EF4_CHANNEL_STATE_POLL | EF4_CHANNEL_STATE_NAPI_REQ:
  442. return false;
  443. default:
  444. break;
  445. }
  446. prev = cmpxchg(&channel->busy_poll_state, old,
  447. EF4_CHANNEL_STATE_NAPI);
  448. if (unlikely(prev != old)) {
  449. /* This is likely to mean we've just entered polling
  450. * state. Go back round to set the REQ bit.
  451. */
  452. old = prev;
  453. continue;
  454. }
  455. return true;
  456. }
  457. }
  458. static inline void ef4_channel_unlock_napi(struct ef4_channel *channel)
  459. {
  460. /* Make sure write has completed from ef4_channel_lock_napi() */
  461. smp_wmb();
  462. WRITE_ONCE(channel->busy_poll_state, EF4_CHANNEL_STATE_IDLE);
  463. }
  464. /* Called from ef4_busy_poll(). */
  465. static inline bool ef4_channel_try_lock_poll(struct ef4_channel *channel)
  466. {
  467. return cmpxchg(&channel->busy_poll_state, EF4_CHANNEL_STATE_IDLE,
  468. EF4_CHANNEL_STATE_POLL) == EF4_CHANNEL_STATE_IDLE;
  469. }
  470. static inline void ef4_channel_unlock_poll(struct ef4_channel *channel)
  471. {
  472. clear_bit_unlock(EF4_CHANNEL_STATE_POLL_BIT, &channel->busy_poll_state);
  473. }
  474. static inline bool ef4_channel_busy_polling(struct ef4_channel *channel)
  475. {
  476. return test_bit(EF4_CHANNEL_STATE_POLL_BIT, &channel->busy_poll_state);
  477. }
  478. static inline void ef4_channel_enable(struct ef4_channel *channel)
  479. {
  480. clear_bit_unlock(EF4_CHANNEL_STATE_DISABLE_BIT,
  481. &channel->busy_poll_state);
  482. }
  483. /* Stop further polling or napi access.
  484. * Returns false if the channel is currently busy polling.
  485. */
  486. static inline bool ef4_channel_disable(struct ef4_channel *channel)
  487. {
  488. set_bit(EF4_CHANNEL_STATE_DISABLE_BIT, &channel->busy_poll_state);
  489. /* Implicit barrier in ef4_channel_busy_polling() */
  490. return !ef4_channel_busy_polling(channel);
  491. }
  492. #else /* CONFIG_NET_RX_BUSY_POLL */
  493. static inline void ef4_channel_busy_poll_init(struct ef4_channel *channel)
  494. {
  495. }
  496. static inline bool ef4_channel_lock_napi(struct ef4_channel *channel)
  497. {
  498. return true;
  499. }
  500. static inline void ef4_channel_unlock_napi(struct ef4_channel *channel)
  501. {
  502. }
  503. static inline bool ef4_channel_try_lock_poll(struct ef4_channel *channel)
  504. {
  505. return false;
  506. }
  507. static inline void ef4_channel_unlock_poll(struct ef4_channel *channel)
  508. {
  509. }
  510. static inline bool ef4_channel_busy_polling(struct ef4_channel *channel)
  511. {
  512. return false;
  513. }
  514. static inline void ef4_channel_enable(struct ef4_channel *channel)
  515. {
  516. }
  517. static inline bool ef4_channel_disable(struct ef4_channel *channel)
  518. {
  519. return true;
  520. }
  521. #endif /* CONFIG_NET_RX_BUSY_POLL */
  522. /**
  523. * struct ef4_msi_context - Context for each MSI
  524. * @efx: The associated NIC
  525. * @index: Index of the channel/IRQ
  526. * @name: Name of the channel/IRQ
  527. *
  528. * Unlike &struct ef4_channel, this is never reallocated and is always
  529. * safe for the IRQ handler to access.
  530. */
  531. struct ef4_msi_context {
  532. struct ef4_nic *efx;
  533. unsigned int index;
  534. char name[IFNAMSIZ + 6];
  535. };
  536. /**
  537. * struct ef4_channel_type - distinguishes traffic and extra channels
  538. * @handle_no_channel: Handle failure to allocate an extra channel
  539. * @pre_probe: Set up extra state prior to initialisation
  540. * @post_remove: Tear down extra state after finalisation, if allocated.
  541. * May be called on channels that have not been probed.
  542. * @get_name: Generate the channel's name (used for its IRQ handler)
  543. * @copy: Copy the channel state prior to reallocation. May be %NULL if
  544. * reallocation is not supported.
  545. * @receive_skb: Handle an skb ready to be passed to netif_receive_skb()
  546. * @keep_eventq: Flag for whether event queue should be kept initialised
  547. * while the device is stopped
  548. */
  549. struct ef4_channel_type {
  550. void (*handle_no_channel)(struct ef4_nic *);
  551. int (*pre_probe)(struct ef4_channel *);
  552. void (*post_remove)(struct ef4_channel *);
  553. void (*get_name)(struct ef4_channel *, char *buf, size_t len);
  554. struct ef4_channel *(*copy)(const struct ef4_channel *);
  555. bool (*receive_skb)(struct ef4_channel *, struct sk_buff *);
  556. bool keep_eventq;
  557. };
  558. enum ef4_led_mode {
  559. EF4_LED_OFF = 0,
  560. EF4_LED_ON = 1,
  561. EF4_LED_DEFAULT = 2
  562. };
  563. #define STRING_TABLE_LOOKUP(val, member) \
  564. ((val) < member ## _max) ? member ## _names[val] : "(invalid)"
  565. extern const char *const ef4_loopback_mode_names[];
  566. extern const unsigned int ef4_loopback_mode_max;
  567. #define LOOPBACK_MODE(efx) \
  568. STRING_TABLE_LOOKUP((efx)->loopback_mode, ef4_loopback_mode)
  569. extern const char *const ef4_reset_type_names[];
  570. extern const unsigned int ef4_reset_type_max;
  571. #define RESET_TYPE(type) \
  572. STRING_TABLE_LOOKUP(type, ef4_reset_type)
  573. enum ef4_int_mode {
  574. /* Be careful if altering to correct macro below */
  575. EF4_INT_MODE_MSIX = 0,
  576. EF4_INT_MODE_MSI = 1,
  577. EF4_INT_MODE_LEGACY = 2,
  578. EF4_INT_MODE_MAX /* Insert any new items before this */
  579. };
  580. #define EF4_INT_MODE_USE_MSI(x) (((x)->interrupt_mode) <= EF4_INT_MODE_MSI)
  581. enum nic_state {
  582. STATE_UNINIT = 0, /* device being probed/removed or is frozen */
  583. STATE_READY = 1, /* hardware ready and netdev registered */
  584. STATE_DISABLED = 2, /* device disabled due to hardware errors */
  585. STATE_RECOVERY = 3, /* device recovering from PCI error */
  586. };
  587. /* Forward declaration */
  588. struct ef4_nic;
  589. /* Pseudo bit-mask flow control field */
  590. #define EF4_FC_RX FLOW_CTRL_RX
  591. #define EF4_FC_TX FLOW_CTRL_TX
  592. #define EF4_FC_AUTO 4
  593. /**
  594. * struct ef4_link_state - Current state of the link
  595. * @up: Link is up
  596. * @fd: Link is full-duplex
  597. * @fc: Actual flow control flags
  598. * @speed: Link speed (Mbps)
  599. */
  600. struct ef4_link_state {
  601. bool up;
  602. bool fd;
  603. u8 fc;
  604. unsigned int speed;
  605. };
  606. static inline bool ef4_link_state_equal(const struct ef4_link_state *left,
  607. const struct ef4_link_state *right)
  608. {
  609. return left->up == right->up && left->fd == right->fd &&
  610. left->fc == right->fc && left->speed == right->speed;
  611. }
  612. /**
  613. * struct ef4_phy_operations - Efx PHY operations table
  614. * @probe: Probe PHY and initialise efx->mdio.mode_support, efx->mdio.mmds,
  615. * efx->loopback_modes.
  616. * @init: Initialise PHY
  617. * @fini: Shut down PHY
  618. * @reconfigure: Reconfigure PHY (e.g. for new link parameters)
  619. * @poll: Update @link_state and report whether it changed.
  620. * Serialised by the mac_lock.
  621. * @get_settings: Get ethtool settings. Serialised by the mac_lock.
  622. * @set_settings: Set ethtool settings. Serialised by the mac_lock.
  623. * @set_npage_adv: Set abilities advertised in (Extended) Next Page
  624. * (only needed where AN bit is set in mmds)
  625. * @test_alive: Test that PHY is 'alive' (online)
  626. * @test_name: Get the name of a PHY-specific test/result
  627. * @run_tests: Run tests and record results as appropriate (offline).
  628. * Flags are the ethtool tests flags.
  629. */
  630. struct ef4_phy_operations {
  631. int (*probe) (struct ef4_nic *efx);
  632. int (*init) (struct ef4_nic *efx);
  633. void (*fini) (struct ef4_nic *efx);
  634. void (*remove) (struct ef4_nic *efx);
  635. int (*reconfigure) (struct ef4_nic *efx);
  636. bool (*poll) (struct ef4_nic *efx);
  637. void (*get_settings) (struct ef4_nic *efx,
  638. struct ethtool_cmd *ecmd);
  639. int (*set_settings) (struct ef4_nic *efx,
  640. struct ethtool_cmd *ecmd);
  641. void (*set_npage_adv) (struct ef4_nic *efx, u32);
  642. int (*test_alive) (struct ef4_nic *efx);
  643. const char *(*test_name) (struct ef4_nic *efx, unsigned int index);
  644. int (*run_tests) (struct ef4_nic *efx, int *results, unsigned flags);
  645. int (*get_module_eeprom) (struct ef4_nic *efx,
  646. struct ethtool_eeprom *ee,
  647. u8 *data);
  648. int (*get_module_info) (struct ef4_nic *efx,
  649. struct ethtool_modinfo *modinfo);
  650. };
  651. /**
  652. * enum ef4_phy_mode - PHY operating mode flags
  653. * @PHY_MODE_NORMAL: on and should pass traffic
  654. * @PHY_MODE_TX_DISABLED: on with TX disabled
  655. * @PHY_MODE_LOW_POWER: set to low power through MDIO
  656. * @PHY_MODE_OFF: switched off through external control
  657. * @PHY_MODE_SPECIAL: on but will not pass traffic
  658. */
  659. enum ef4_phy_mode {
  660. PHY_MODE_NORMAL = 0,
  661. PHY_MODE_TX_DISABLED = 1,
  662. PHY_MODE_LOW_POWER = 2,
  663. PHY_MODE_OFF = 4,
  664. PHY_MODE_SPECIAL = 8,
  665. };
  666. static inline bool ef4_phy_mode_disabled(enum ef4_phy_mode mode)
  667. {
  668. return !!(mode & ~PHY_MODE_TX_DISABLED);
  669. }
  670. /**
  671. * struct ef4_hw_stat_desc - Description of a hardware statistic
  672. * @name: Name of the statistic as visible through ethtool, or %NULL if
  673. * it should not be exposed
  674. * @dma_width: Width in bits (0 for non-DMA statistics)
  675. * @offset: Offset within stats (ignored for non-DMA statistics)
  676. */
  677. struct ef4_hw_stat_desc {
  678. const char *name;
  679. u16 dma_width;
  680. u16 offset;
  681. };
  682. /* Number of bits used in a multicast filter hash address */
  683. #define EF4_MCAST_HASH_BITS 8
  684. /* Number of (single-bit) entries in a multicast filter hash */
  685. #define EF4_MCAST_HASH_ENTRIES (1 << EF4_MCAST_HASH_BITS)
  686. /* An Efx multicast filter hash */
  687. union ef4_multicast_hash {
  688. u8 byte[EF4_MCAST_HASH_ENTRIES / 8];
  689. ef4_oword_t oword[EF4_MCAST_HASH_ENTRIES / sizeof(ef4_oword_t) / 8];
  690. };
  691. /**
  692. * struct ef4_nic - an Efx NIC
  693. * @name: Device name (net device name or bus id before net device registered)
  694. * @pci_dev: The PCI device
  695. * @node: List node for maintaning primary/secondary function lists
  696. * @primary: &struct ef4_nic instance for the primary function of this
  697. * controller. May be the same structure, and may be %NULL if no
  698. * primary function is bound. Serialised by rtnl_lock.
  699. * @secondary_list: List of &struct ef4_nic instances for the secondary PCI
  700. * functions of the controller, if this is for the primary function.
  701. * Serialised by rtnl_lock.
  702. * @type: Controller type attributes
  703. * @legacy_irq: IRQ number
  704. * @workqueue: Workqueue for port reconfigures and the HW monitor.
  705. * Work items do not hold and must not acquire RTNL.
  706. * @workqueue_name: Name of workqueue
  707. * @reset_work: Scheduled reset workitem
  708. * @membase_phys: Memory BAR value as physical address
  709. * @membase: Memory BAR value
  710. * @interrupt_mode: Interrupt mode
  711. * @timer_quantum_ns: Interrupt timer quantum, in nanoseconds
  712. * @timer_max_ns: Interrupt timer maximum value, in nanoseconds
  713. * @irq_rx_adaptive: Adaptive IRQ moderation enabled for RX event queues
  714. * @irq_rx_mod_step_us: Step size for IRQ moderation for RX event queues
  715. * @irq_rx_moderation_us: IRQ moderation time for RX event queues
  716. * @msg_enable: Log message enable flags
  717. * @state: Device state number (%STATE_*). Serialised by the rtnl_lock.
  718. * @reset_pending: Bitmask for pending resets
  719. * @tx_queue: TX DMA queues
  720. * @rx_queue: RX DMA queues
  721. * @channel: Channels
  722. * @msi_context: Context for each MSI
  723. * @extra_channel_types: Types of extra (non-traffic) channels that
  724. * should be allocated for this NIC
  725. * @rxq_entries: Size of receive queues requested by user.
  726. * @txq_entries: Size of transmit queues requested by user.
  727. * @txq_stop_thresh: TX queue fill level at or above which we stop it.
  728. * @txq_wake_thresh: TX queue fill level at or below which we wake it.
  729. * @tx_dc_base: Base qword address in SRAM of TX queue descriptor caches
  730. * @rx_dc_base: Base qword address in SRAM of RX queue descriptor caches
  731. * @sram_lim_qw: Qword address limit of SRAM
  732. * @next_buffer_table: First available buffer table id
  733. * @n_channels: Number of channels in use
  734. * @n_rx_channels: Number of channels used for RX (= number of RX queues)
  735. * @n_tx_channels: Number of channels used for TX
  736. * @rx_ip_align: RX DMA address offset to have IP header aligned in
  737. * in accordance with NET_IP_ALIGN
  738. * @rx_dma_len: Current maximum RX DMA length
  739. * @rx_buffer_order: Order (log2) of number of pages for each RX buffer
  740. * @rx_buffer_truesize: Amortised allocation size of an RX buffer,
  741. * for use in sk_buff::truesize
  742. * @rx_prefix_size: Size of RX prefix before packet data
  743. * @rx_packet_hash_offset: Offset of RX flow hash from start of packet data
  744. * (valid only if @rx_prefix_size != 0; always negative)
  745. * @rx_packet_len_offset: Offset of RX packet length from start of packet data
  746. * (valid only for NICs that set %EF4_RX_PKT_PREFIX_LEN; always negative)
  747. * @rx_packet_ts_offset: Offset of timestamp from start of packet data
  748. * (valid only if channel->sync_timestamps_enabled; always negative)
  749. * @rx_hash_key: Toeplitz hash key for RSS
  750. * @rx_indir_table: Indirection table for RSS
  751. * @rx_scatter: Scatter mode enabled for receives
  752. * @int_error_count: Number of internal errors seen recently
  753. * @int_error_expire: Time at which error count will be expired
  754. * @irq_soft_enabled: Are IRQs soft-enabled? If not, IRQ handler will
  755. * acknowledge but do nothing else.
  756. * @irq_status: Interrupt status buffer
  757. * @irq_zero_count: Number of legacy IRQs seen with queue flags == 0
  758. * @irq_level: IRQ level/index for IRQs not triggered by an event queue
  759. * @selftest_work: Work item for asynchronous self-test
  760. * @mtd_list: List of MTDs attached to the NIC
  761. * @nic_data: Hardware dependent state
  762. * @mac_lock: MAC access lock. Protects @port_enabled, @phy_mode,
  763. * ef4_monitor() and ef4_reconfigure_port()
  764. * @port_enabled: Port enabled indicator.
  765. * Serialises ef4_stop_all(), ef4_start_all(), ef4_monitor() and
  766. * ef4_mac_work() with kernel interfaces. Safe to read under any
  767. * one of the rtnl_lock, mac_lock, or netif_tx_lock, but all three must
  768. * be held to modify it.
  769. * @port_initialized: Port initialized?
  770. * @net_dev: Operating system network device. Consider holding the rtnl lock
  771. * @fixed_features: Features which cannot be turned off
  772. * @stats_buffer: DMA buffer for statistics
  773. * @phy_type: PHY type
  774. * @phy_op: PHY interface
  775. * @phy_data: PHY private data (including PHY-specific stats)
  776. * @mdio: PHY MDIO interface
  777. * @phy_mode: PHY operating mode. Serialised by @mac_lock.
  778. * @link_advertising: Autonegotiation advertising flags
  779. * @link_state: Current state of the link
  780. * @n_link_state_changes: Number of times the link has changed state
  781. * @unicast_filter: Flag for Falcon-arch simple unicast filter.
  782. * Protected by @mac_lock.
  783. * @multicast_hash: Multicast hash table for Falcon-arch.
  784. * Protected by @mac_lock.
  785. * @wanted_fc: Wanted flow control flags
  786. * @fc_disable: When non-zero flow control is disabled. Typically used to
  787. * ensure that network back pressure doesn't delay dma queue flushes.
  788. * Serialised by the rtnl lock.
  789. * @mac_work: Work item for changing MAC promiscuity and multicast hash
  790. * @loopback_mode: Loopback status
  791. * @loopback_modes: Supported loopback mode bitmask
  792. * @loopback_selftest: Offline self-test private state
  793. * @filter_sem: Filter table rw_semaphore, for freeing the table
  794. * @filter_lock: Filter table lock, for mere content changes
  795. * @filter_state: Architecture-dependent filter table state
  796. * @rps_expire_channel: Next channel to check for expiry
  797. * @rps_expire_index: Next index to check for expiry in
  798. * @rps_expire_channel's @rps_flow_id
  799. * @active_queues: Count of RX and TX queues that haven't been flushed and drained.
  800. * @rxq_flush_pending: Count of number of receive queues that need to be flushed.
  801. * Decremented when the ef4_flush_rx_queue() is called.
  802. * @rxq_flush_outstanding: Count of number of RX flushes started but not yet
  803. * completed (either success or failure). Not used when MCDI is used to
  804. * flush receive queues.
  805. * @flush_wq: wait queue used by ef4_nic_flush_queues() to wait for flush completions.
  806. * @vpd_sn: Serial number read from VPD
  807. * @monitor_work: Hardware monitor workitem
  808. * @biu_lock: BIU (bus interface unit) lock
  809. * @last_irq_cpu: Last CPU to handle a possible test interrupt. This
  810. * field is used by ef4_test_interrupts() to verify that an
  811. * interrupt has occurred.
  812. * @stats_lock: Statistics update lock. Must be held when calling
  813. * ef4_nic_type::{update,start,stop}_stats.
  814. * @n_rx_noskb_drops: Count of RX packets dropped due to failure to allocate an skb
  815. *
  816. * This is stored in the private area of the &struct net_device.
  817. */
  818. struct ef4_nic {
  819. /* The following fields should be written very rarely */
  820. char name[IFNAMSIZ];
  821. struct list_head node;
  822. struct ef4_nic *primary;
  823. struct list_head secondary_list;
  824. struct pci_dev *pci_dev;
  825. unsigned int port_num;
  826. const struct ef4_nic_type *type;
  827. int legacy_irq;
  828. bool eeh_disabled_legacy_irq;
  829. struct workqueue_struct *workqueue;
  830. char workqueue_name[16];
  831. struct work_struct reset_work;
  832. resource_size_t membase_phys;
  833. void __iomem *membase;
  834. enum ef4_int_mode interrupt_mode;
  835. unsigned int timer_quantum_ns;
  836. unsigned int timer_max_ns;
  837. bool irq_rx_adaptive;
  838. unsigned int irq_mod_step_us;
  839. unsigned int irq_rx_moderation_us;
  840. u32 msg_enable;
  841. enum nic_state state;
  842. unsigned long reset_pending;
  843. struct ef4_channel *channel[EF4_MAX_CHANNELS];
  844. struct ef4_msi_context msi_context[EF4_MAX_CHANNELS];
  845. const struct ef4_channel_type *
  846. extra_channel_type[EF4_MAX_EXTRA_CHANNELS];
  847. unsigned rxq_entries;
  848. unsigned txq_entries;
  849. unsigned int txq_stop_thresh;
  850. unsigned int txq_wake_thresh;
  851. unsigned tx_dc_base;
  852. unsigned rx_dc_base;
  853. unsigned sram_lim_qw;
  854. unsigned next_buffer_table;
  855. unsigned int max_channels;
  856. unsigned int max_tx_channels;
  857. unsigned n_channels;
  858. unsigned n_rx_channels;
  859. unsigned rss_spread;
  860. unsigned tx_channel_offset;
  861. unsigned n_tx_channels;
  862. unsigned int rx_ip_align;
  863. unsigned int rx_dma_len;
  864. unsigned int rx_buffer_order;
  865. unsigned int rx_buffer_truesize;
  866. unsigned int rx_page_buf_step;
  867. unsigned int rx_bufs_per_page;
  868. unsigned int rx_pages_per_batch;
  869. unsigned int rx_prefix_size;
  870. int rx_packet_hash_offset;
  871. int rx_packet_len_offset;
  872. int rx_packet_ts_offset;
  873. u8 rx_hash_key[40];
  874. u32 rx_indir_table[128];
  875. bool rx_scatter;
  876. unsigned int_error_count;
  877. unsigned long int_error_expire;
  878. bool irq_soft_enabled;
  879. struct ef4_buffer irq_status;
  880. unsigned irq_zero_count;
  881. unsigned irq_level;
  882. struct delayed_work selftest_work;
  883. #ifdef CONFIG_SFC_FALCON_MTD
  884. struct list_head mtd_list;
  885. #endif
  886. void *nic_data;
  887. struct mutex mac_lock;
  888. struct work_struct mac_work;
  889. bool port_enabled;
  890. bool mc_bist_for_other_fn;
  891. bool port_initialized;
  892. struct net_device *net_dev;
  893. netdev_features_t fixed_features;
  894. struct ef4_buffer stats_buffer;
  895. u64 rx_nodesc_drops_total;
  896. u64 rx_nodesc_drops_while_down;
  897. bool rx_nodesc_drops_prev_state;
  898. unsigned int phy_type;
  899. const struct ef4_phy_operations *phy_op;
  900. void *phy_data;
  901. struct mdio_if_info mdio;
  902. enum ef4_phy_mode phy_mode;
  903. u32 link_advertising;
  904. struct ef4_link_state link_state;
  905. unsigned int n_link_state_changes;
  906. bool unicast_filter;
  907. union ef4_multicast_hash multicast_hash;
  908. u8 wanted_fc;
  909. unsigned fc_disable;
  910. atomic_t rx_reset;
  911. enum ef4_loopback_mode loopback_mode;
  912. u64 loopback_modes;
  913. void *loopback_selftest;
  914. struct rw_semaphore filter_sem;
  915. spinlock_t filter_lock;
  916. void *filter_state;
  917. #ifdef CONFIG_RFS_ACCEL
  918. unsigned int rps_expire_channel;
  919. unsigned int rps_expire_index;
  920. #endif
  921. atomic_t active_queues;
  922. atomic_t rxq_flush_pending;
  923. atomic_t rxq_flush_outstanding;
  924. wait_queue_head_t flush_wq;
  925. char *vpd_sn;
  926. /* The following fields may be written more often */
  927. struct delayed_work monitor_work ____cacheline_aligned_in_smp;
  928. spinlock_t biu_lock;
  929. int last_irq_cpu;
  930. spinlock_t stats_lock;
  931. atomic_t n_rx_noskb_drops;
  932. };
  933. static inline int ef4_dev_registered(struct ef4_nic *efx)
  934. {
  935. return efx->net_dev->reg_state == NETREG_REGISTERED;
  936. }
  937. static inline unsigned int ef4_port_num(struct ef4_nic *efx)
  938. {
  939. return efx->port_num;
  940. }
  941. struct ef4_mtd_partition {
  942. struct list_head node;
  943. struct mtd_info mtd;
  944. const char *dev_type_name;
  945. const char *type_name;
  946. char name[IFNAMSIZ + 20];
  947. };
  948. /**
  949. * struct ef4_nic_type - Efx device type definition
  950. * @mem_bar: Get the memory BAR
  951. * @mem_map_size: Get memory BAR mapped size
  952. * @probe: Probe the controller
  953. * @remove: Free resources allocated by probe()
  954. * @init: Initialise the controller
  955. * @dimension_resources: Dimension controller resources (buffer table,
  956. * and VIs once the available interrupt resources are clear)
  957. * @fini: Shut down the controller
  958. * @monitor: Periodic function for polling link state and hardware monitor
  959. * @map_reset_reason: Map ethtool reset reason to a reset method
  960. * @map_reset_flags: Map ethtool reset flags to a reset method, if possible
  961. * @reset: Reset the controller hardware and possibly the PHY. This will
  962. * be called while the controller is uninitialised.
  963. * @probe_port: Probe the MAC and PHY
  964. * @remove_port: Free resources allocated by probe_port()
  965. * @handle_global_event: Handle a "global" event (may be %NULL)
  966. * @fini_dmaq: Flush and finalise DMA queues (RX and TX queues)
  967. * @prepare_flush: Prepare the hardware for flushing the DMA queues
  968. * (for Falcon architecture)
  969. * @finish_flush: Clean up after flushing the DMA queues (for Falcon
  970. * architecture)
  971. * @prepare_flr: Prepare for an FLR
  972. * @finish_flr: Clean up after an FLR
  973. * @describe_stats: Describe statistics for ethtool
  974. * @update_stats: Update statistics not provided by event handling.
  975. * Either argument may be %NULL.
  976. * @start_stats: Start the regular fetching of statistics
  977. * @pull_stats: Pull stats from the NIC and wait until they arrive.
  978. * @stop_stats: Stop the regular fetching of statistics
  979. * @set_id_led: Set state of identifying LED or revert to automatic function
  980. * @push_irq_moderation: Apply interrupt moderation value
  981. * @reconfigure_port: Push loopback/power/txdis changes to the MAC and PHY
  982. * @prepare_enable_fc_tx: Prepare MAC to enable pause frame TX (may be %NULL)
  983. * @reconfigure_mac: Push MAC address, MTU, flow control and filter settings
  984. * to the hardware. Serialised by the mac_lock.
  985. * @check_mac_fault: Check MAC fault state. True if fault present.
  986. * @get_wol: Get WoL configuration from driver state
  987. * @set_wol: Push WoL configuration to the NIC
  988. * @resume_wol: Synchronise WoL state between driver and MC (e.g. after resume)
  989. * @test_chip: Test registers. May use ef4_farch_test_registers(), and is
  990. * expected to reset the NIC.
  991. * @test_nvram: Test validity of NVRAM contents
  992. * @irq_enable_master: Enable IRQs on the NIC. Each event queue must
  993. * be separately enabled after this.
  994. * @irq_test_generate: Generate a test IRQ
  995. * @irq_disable_non_ev: Disable non-event IRQs on the NIC. Each event
  996. * queue must be separately disabled before this.
  997. * @irq_handle_msi: Handle MSI for a channel. The @dev_id argument is
  998. * a pointer to the &struct ef4_msi_context for the channel.
  999. * @irq_handle_legacy: Handle legacy interrupt. The @dev_id argument
  1000. * is a pointer to the &struct ef4_nic.
  1001. * @tx_probe: Allocate resources for TX queue
  1002. * @tx_init: Initialise TX queue on the NIC
  1003. * @tx_remove: Free resources for TX queue
  1004. * @tx_write: Write TX descriptors and doorbell
  1005. * @rx_push_rss_config: Write RSS hash key and indirection table to the NIC
  1006. * @rx_probe: Allocate resources for RX queue
  1007. * @rx_init: Initialise RX queue on the NIC
  1008. * @rx_remove: Free resources for RX queue
  1009. * @rx_write: Write RX descriptors and doorbell
  1010. * @rx_defer_refill: Generate a refill reminder event
  1011. * @ev_probe: Allocate resources for event queue
  1012. * @ev_init: Initialise event queue on the NIC
  1013. * @ev_fini: Deinitialise event queue on the NIC
  1014. * @ev_remove: Free resources for event queue
  1015. * @ev_process: Process events for a queue, up to the given NAPI quota
  1016. * @ev_read_ack: Acknowledge read events on a queue, rearming its IRQ
  1017. * @ev_test_generate: Generate a test event
  1018. * @filter_table_probe: Probe filter capabilities and set up filter software state
  1019. * @filter_table_restore: Restore filters removed from hardware
  1020. * @filter_table_remove: Remove filters from hardware and tear down software state
  1021. * @filter_update_rx_scatter: Update filters after change to rx scatter setting
  1022. * @filter_insert: add or replace a filter
  1023. * @filter_remove_safe: remove a filter by ID, carefully
  1024. * @filter_get_safe: retrieve a filter by ID, carefully
  1025. * @filter_clear_rx: Remove all RX filters whose priority is less than or
  1026. * equal to the given priority and is not %EF4_FILTER_PRI_AUTO
  1027. * @filter_count_rx_used: Get the number of filters in use at a given priority
  1028. * @filter_get_rx_id_limit: Get maximum value of a filter id, plus 1
  1029. * @filter_get_rx_ids: Get list of RX filters at a given priority
  1030. * @filter_rfs_insert: Add or replace a filter for RFS. This must be
  1031. * atomic. The hardware change may be asynchronous but should
  1032. * not be delayed for long. It may fail if this can't be done
  1033. * atomically.
  1034. * @filter_rfs_expire_one: Consider expiring a filter inserted for RFS.
  1035. * This must check whether the specified table entry is used by RFS
  1036. * and that rps_may_expire_flow() returns true for it.
  1037. * @mtd_probe: Probe and add MTD partitions associated with this net device,
  1038. * using ef4_mtd_add()
  1039. * @mtd_rename: Set an MTD partition name using the net device name
  1040. * @mtd_read: Read from an MTD partition
  1041. * @mtd_erase: Erase part of an MTD partition
  1042. * @mtd_write: Write to an MTD partition
  1043. * @mtd_sync: Wait for write-back to complete on MTD partition. This
  1044. * also notifies the driver that a writer has finished using this
  1045. * partition.
  1046. * @set_mac_address: Set the MAC address of the device
  1047. * @revision: Hardware architecture revision
  1048. * @txd_ptr_tbl_base: TX descriptor ring base address
  1049. * @rxd_ptr_tbl_base: RX descriptor ring base address
  1050. * @buf_tbl_base: Buffer table base address
  1051. * @evq_ptr_tbl_base: Event queue pointer table base address
  1052. * @evq_rptr_tbl_base: Event queue read-pointer table base address
  1053. * @max_dma_mask: Maximum possible DMA mask
  1054. * @rx_prefix_size: Size of RX prefix before packet data
  1055. * @rx_hash_offset: Offset of RX flow hash within prefix
  1056. * @rx_ts_offset: Offset of timestamp within prefix
  1057. * @rx_buffer_padding: Size of padding at end of RX packet
  1058. * @can_rx_scatter: NIC is able to scatter packets to multiple buffers
  1059. * @always_rx_scatter: NIC will always scatter packets to multiple buffers
  1060. * @max_interrupt_mode: Highest capability interrupt mode supported
  1061. * from &enum ef4_init_mode.
  1062. * @timer_period_max: Maximum period of interrupt timer (in ticks)
  1063. * @offload_features: net_device feature flags for protocol offload
  1064. * features implemented in hardware
  1065. */
  1066. struct ef4_nic_type {
  1067. unsigned int mem_bar;
  1068. unsigned int (*mem_map_size)(struct ef4_nic *efx);
  1069. int (*probe)(struct ef4_nic *efx);
  1070. void (*remove)(struct ef4_nic *efx);
  1071. int (*init)(struct ef4_nic *efx);
  1072. int (*dimension_resources)(struct ef4_nic *efx);
  1073. void (*fini)(struct ef4_nic *efx);
  1074. void (*monitor)(struct ef4_nic *efx);
  1075. enum reset_type (*map_reset_reason)(enum reset_type reason);
  1076. int (*map_reset_flags)(u32 *flags);
  1077. int (*reset)(struct ef4_nic *efx, enum reset_type method);
  1078. int (*probe_port)(struct ef4_nic *efx);
  1079. void (*remove_port)(struct ef4_nic *efx);
  1080. bool (*handle_global_event)(struct ef4_channel *channel, ef4_qword_t *);
  1081. int (*fini_dmaq)(struct ef4_nic *efx);
  1082. void (*prepare_flush)(struct ef4_nic *efx);
  1083. void (*finish_flush)(struct ef4_nic *efx);
  1084. void (*prepare_flr)(struct ef4_nic *efx);
  1085. void (*finish_flr)(struct ef4_nic *efx);
  1086. size_t (*describe_stats)(struct ef4_nic *efx, u8 *names);
  1087. size_t (*update_stats)(struct ef4_nic *efx, u64 *full_stats,
  1088. struct rtnl_link_stats64 *core_stats);
  1089. void (*start_stats)(struct ef4_nic *efx);
  1090. void (*pull_stats)(struct ef4_nic *efx);
  1091. void (*stop_stats)(struct ef4_nic *efx);
  1092. void (*set_id_led)(struct ef4_nic *efx, enum ef4_led_mode mode);
  1093. void (*push_irq_moderation)(struct ef4_channel *channel);
  1094. int (*reconfigure_port)(struct ef4_nic *efx);
  1095. void (*prepare_enable_fc_tx)(struct ef4_nic *efx);
  1096. int (*reconfigure_mac)(struct ef4_nic *efx);
  1097. bool (*check_mac_fault)(struct ef4_nic *efx);
  1098. void (*get_wol)(struct ef4_nic *efx, struct ethtool_wolinfo *wol);
  1099. int (*set_wol)(struct ef4_nic *efx, u32 type);
  1100. void (*resume_wol)(struct ef4_nic *efx);
  1101. int (*test_chip)(struct ef4_nic *efx, struct ef4_self_tests *tests);
  1102. int (*test_nvram)(struct ef4_nic *efx);
  1103. void (*irq_enable_master)(struct ef4_nic *efx);
  1104. int (*irq_test_generate)(struct ef4_nic *efx);
  1105. void (*irq_disable_non_ev)(struct ef4_nic *efx);
  1106. irqreturn_t (*irq_handle_msi)(int irq, void *dev_id);
  1107. irqreturn_t (*irq_handle_legacy)(int irq, void *dev_id);
  1108. int (*tx_probe)(struct ef4_tx_queue *tx_queue);
  1109. void (*tx_init)(struct ef4_tx_queue *tx_queue);
  1110. void (*tx_remove)(struct ef4_tx_queue *tx_queue);
  1111. void (*tx_write)(struct ef4_tx_queue *tx_queue);
  1112. unsigned int (*tx_limit_len)(struct ef4_tx_queue *tx_queue,
  1113. dma_addr_t dma_addr, unsigned int len);
  1114. int (*rx_push_rss_config)(struct ef4_nic *efx, bool user,
  1115. const u32 *rx_indir_table);
  1116. int (*rx_probe)(struct ef4_rx_queue *rx_queue);
  1117. void (*rx_init)(struct ef4_rx_queue *rx_queue);
  1118. void (*rx_remove)(struct ef4_rx_queue *rx_queue);
  1119. void (*rx_write)(struct ef4_rx_queue *rx_queue);
  1120. void (*rx_defer_refill)(struct ef4_rx_queue *rx_queue);
  1121. int (*ev_probe)(struct ef4_channel *channel);
  1122. int (*ev_init)(struct ef4_channel *channel);
  1123. void (*ev_fini)(struct ef4_channel *channel);
  1124. void (*ev_remove)(struct ef4_channel *channel);
  1125. int (*ev_process)(struct ef4_channel *channel, int quota);
  1126. void (*ev_read_ack)(struct ef4_channel *channel);
  1127. void (*ev_test_generate)(struct ef4_channel *channel);
  1128. int (*filter_table_probe)(struct ef4_nic *efx);
  1129. void (*filter_table_restore)(struct ef4_nic *efx);
  1130. void (*filter_table_remove)(struct ef4_nic *efx);
  1131. void (*filter_update_rx_scatter)(struct ef4_nic *efx);
  1132. s32 (*filter_insert)(struct ef4_nic *efx,
  1133. struct ef4_filter_spec *spec, bool replace);
  1134. int (*filter_remove_safe)(struct ef4_nic *efx,
  1135. enum ef4_filter_priority priority,
  1136. u32 filter_id);
  1137. int (*filter_get_safe)(struct ef4_nic *efx,
  1138. enum ef4_filter_priority priority,
  1139. u32 filter_id, struct ef4_filter_spec *);
  1140. int (*filter_clear_rx)(struct ef4_nic *efx,
  1141. enum ef4_filter_priority priority);
  1142. u32 (*filter_count_rx_used)(struct ef4_nic *efx,
  1143. enum ef4_filter_priority priority);
  1144. u32 (*filter_get_rx_id_limit)(struct ef4_nic *efx);
  1145. s32 (*filter_get_rx_ids)(struct ef4_nic *efx,
  1146. enum ef4_filter_priority priority,
  1147. u32 *buf, u32 size);
  1148. #ifdef CONFIG_RFS_ACCEL
  1149. s32 (*filter_rfs_insert)(struct ef4_nic *efx,
  1150. struct ef4_filter_spec *spec);
  1151. bool (*filter_rfs_expire_one)(struct ef4_nic *efx, u32 flow_id,
  1152. unsigned int index);
  1153. #endif
  1154. #ifdef CONFIG_SFC_FALCON_MTD
  1155. int (*mtd_probe)(struct ef4_nic *efx);
  1156. void (*mtd_rename)(struct ef4_mtd_partition *part);
  1157. int (*mtd_read)(struct mtd_info *mtd, loff_t start, size_t len,
  1158. size_t *retlen, u8 *buffer);
  1159. int (*mtd_erase)(struct mtd_info *mtd, loff_t start, size_t len);
  1160. int (*mtd_write)(struct mtd_info *mtd, loff_t start, size_t len,
  1161. size_t *retlen, const u8 *buffer);
  1162. int (*mtd_sync)(struct mtd_info *mtd);
  1163. #endif
  1164. int (*get_mac_address)(struct ef4_nic *efx, unsigned char *perm_addr);
  1165. int (*set_mac_address)(struct ef4_nic *efx);
  1166. int revision;
  1167. unsigned int txd_ptr_tbl_base;
  1168. unsigned int rxd_ptr_tbl_base;
  1169. unsigned int buf_tbl_base;
  1170. unsigned int evq_ptr_tbl_base;
  1171. unsigned int evq_rptr_tbl_base;
  1172. u64 max_dma_mask;
  1173. unsigned int rx_prefix_size;
  1174. unsigned int rx_hash_offset;
  1175. unsigned int rx_ts_offset;
  1176. unsigned int rx_buffer_padding;
  1177. bool can_rx_scatter;
  1178. bool always_rx_scatter;
  1179. unsigned int max_interrupt_mode;
  1180. unsigned int timer_period_max;
  1181. netdev_features_t offload_features;
  1182. unsigned int max_rx_ip_filters;
  1183. };
  1184. /**************************************************************************
  1185. *
  1186. * Prototypes and inline functions
  1187. *
  1188. *************************************************************************/
  1189. static inline struct ef4_channel *
  1190. ef4_get_channel(struct ef4_nic *efx, unsigned index)
  1191. {
  1192. EF4_BUG_ON_PARANOID(index >= efx->n_channels);
  1193. return efx->channel[index];
  1194. }
  1195. /* Iterate over all used channels */
  1196. #define ef4_for_each_channel(_channel, _efx) \
  1197. for (_channel = (_efx)->channel[0]; \
  1198. _channel; \
  1199. _channel = (_channel->channel + 1 < (_efx)->n_channels) ? \
  1200. (_efx)->channel[_channel->channel + 1] : NULL)
  1201. /* Iterate over all used channels in reverse */
  1202. #define ef4_for_each_channel_rev(_channel, _efx) \
  1203. for (_channel = (_efx)->channel[(_efx)->n_channels - 1]; \
  1204. _channel; \
  1205. _channel = _channel->channel ? \
  1206. (_efx)->channel[_channel->channel - 1] : NULL)
  1207. static inline struct ef4_tx_queue *
  1208. ef4_get_tx_queue(struct ef4_nic *efx, unsigned index, unsigned type)
  1209. {
  1210. EF4_BUG_ON_PARANOID(index >= efx->n_tx_channels ||
  1211. type >= EF4_TXQ_TYPES);
  1212. return &efx->channel[efx->tx_channel_offset + index]->tx_queue[type];
  1213. }
  1214. static inline bool ef4_channel_has_tx_queues(struct ef4_channel *channel)
  1215. {
  1216. return channel->channel - channel->efx->tx_channel_offset <
  1217. channel->efx->n_tx_channels;
  1218. }
  1219. static inline struct ef4_tx_queue *
  1220. ef4_channel_get_tx_queue(struct ef4_channel *channel, unsigned type)
  1221. {
  1222. EF4_BUG_ON_PARANOID(!ef4_channel_has_tx_queues(channel) ||
  1223. type >= EF4_TXQ_TYPES);
  1224. return &channel->tx_queue[type];
  1225. }
  1226. static inline bool ef4_tx_queue_used(struct ef4_tx_queue *tx_queue)
  1227. {
  1228. return !(tx_queue->efx->net_dev->num_tc < 2 &&
  1229. tx_queue->queue & EF4_TXQ_TYPE_HIGHPRI);
  1230. }
  1231. /* Iterate over all TX queues belonging to a channel */
  1232. #define ef4_for_each_channel_tx_queue(_tx_queue, _channel) \
  1233. if (!ef4_channel_has_tx_queues(_channel)) \
  1234. ; \
  1235. else \
  1236. for (_tx_queue = (_channel)->tx_queue; \
  1237. _tx_queue < (_channel)->tx_queue + EF4_TXQ_TYPES && \
  1238. ef4_tx_queue_used(_tx_queue); \
  1239. _tx_queue++)
  1240. /* Iterate over all possible TX queues belonging to a channel */
  1241. #define ef4_for_each_possible_channel_tx_queue(_tx_queue, _channel) \
  1242. if (!ef4_channel_has_tx_queues(_channel)) \
  1243. ; \
  1244. else \
  1245. for (_tx_queue = (_channel)->tx_queue; \
  1246. _tx_queue < (_channel)->tx_queue + EF4_TXQ_TYPES; \
  1247. _tx_queue++)
  1248. static inline bool ef4_channel_has_rx_queue(struct ef4_channel *channel)
  1249. {
  1250. return channel->rx_queue.core_index >= 0;
  1251. }
  1252. static inline struct ef4_rx_queue *
  1253. ef4_channel_get_rx_queue(struct ef4_channel *channel)
  1254. {
  1255. EF4_BUG_ON_PARANOID(!ef4_channel_has_rx_queue(channel));
  1256. return &channel->rx_queue;
  1257. }
  1258. /* Iterate over all RX queues belonging to a channel */
  1259. #define ef4_for_each_channel_rx_queue(_rx_queue, _channel) \
  1260. if (!ef4_channel_has_rx_queue(_channel)) \
  1261. ; \
  1262. else \
  1263. for (_rx_queue = &(_channel)->rx_queue; \
  1264. _rx_queue; \
  1265. _rx_queue = NULL)
  1266. static inline struct ef4_channel *
  1267. ef4_rx_queue_channel(struct ef4_rx_queue *rx_queue)
  1268. {
  1269. return container_of(rx_queue, struct ef4_channel, rx_queue);
  1270. }
  1271. static inline int ef4_rx_queue_index(struct ef4_rx_queue *rx_queue)
  1272. {
  1273. return ef4_rx_queue_channel(rx_queue)->channel;
  1274. }
  1275. /* Returns a pointer to the specified receive buffer in the RX
  1276. * descriptor queue.
  1277. */
  1278. static inline struct ef4_rx_buffer *ef4_rx_buffer(struct ef4_rx_queue *rx_queue,
  1279. unsigned int index)
  1280. {
  1281. return &rx_queue->buffer[index];
  1282. }
  1283. /**
  1284. * EF4_MAX_FRAME_LEN - calculate maximum frame length
  1285. *
  1286. * This calculates the maximum frame length that will be used for a
  1287. * given MTU. The frame length will be equal to the MTU plus a
  1288. * constant amount of header space and padding. This is the quantity
  1289. * that the net driver will program into the MAC as the maximum frame
  1290. * length.
  1291. *
  1292. * The 10G MAC requires 8-byte alignment on the frame
  1293. * length, so we round up to the nearest 8.
  1294. *
  1295. * Re-clocking by the XGXS on RX can reduce an IPG to 32 bits (half an
  1296. * XGMII cycle). If the frame length reaches the maximum value in the
  1297. * same cycle, the XMAC can miss the IPG altogether. We work around
  1298. * this by adding a further 16 bytes.
  1299. */
  1300. #define EF4_FRAME_PAD 16
  1301. #define EF4_MAX_FRAME_LEN(mtu) \
  1302. (ALIGN(((mtu) + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN + EF4_FRAME_PAD), 8))
  1303. /* Get all supported features.
  1304. * If a feature is not fixed, it is present in hw_features.
  1305. * If a feature is fixed, it does not present in hw_features, but
  1306. * always in features.
  1307. */
  1308. static inline netdev_features_t ef4_supported_features(const struct ef4_nic *efx)
  1309. {
  1310. const struct net_device *net_dev = efx->net_dev;
  1311. return net_dev->features | net_dev->hw_features;
  1312. }
  1313. /* Get the current TX queue insert index. */
  1314. static inline unsigned int
  1315. ef4_tx_queue_get_insert_index(const struct ef4_tx_queue *tx_queue)
  1316. {
  1317. return tx_queue->insert_count & tx_queue->ptr_mask;
  1318. }
  1319. /* Get a TX buffer. */
  1320. static inline struct ef4_tx_buffer *
  1321. __ef4_tx_queue_get_insert_buffer(const struct ef4_tx_queue *tx_queue)
  1322. {
  1323. return &tx_queue->buffer[ef4_tx_queue_get_insert_index(tx_queue)];
  1324. }
  1325. /* Get a TX buffer, checking it's not currently in use. */
  1326. static inline struct ef4_tx_buffer *
  1327. ef4_tx_queue_get_insert_buffer(const struct ef4_tx_queue *tx_queue)
  1328. {
  1329. struct ef4_tx_buffer *buffer =
  1330. __ef4_tx_queue_get_insert_buffer(tx_queue);
  1331. EF4_BUG_ON_PARANOID(buffer->len);
  1332. EF4_BUG_ON_PARANOID(buffer->flags);
  1333. EF4_BUG_ON_PARANOID(buffer->unmap_len);
  1334. return buffer;
  1335. }
  1336. #endif /* EF4_NET_DRIVER_H */