nic.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2005-2006 Fen Systems Ltd.
  5. * Copyright 2006-2013 Solarflare Communications Inc.
  6. */
  7. #ifndef EF4_NIC_H
  8. #define EF4_NIC_H
  9. #include <linux/net_tstamp.h>
  10. #include <linux/i2c-algo-bit.h>
  11. #include "net_driver.h"
  12. #include "efx.h"
  13. enum {
  14. EF4_REV_FALCON_A0 = 0,
  15. EF4_REV_FALCON_A1 = 1,
  16. EF4_REV_FALCON_B0 = 2,
  17. };
  18. static inline int ef4_nic_rev(struct ef4_nic *efx)
  19. {
  20. return efx->type->revision;
  21. }
  22. u32 ef4_farch_fpga_ver(struct ef4_nic *efx);
  23. /* NIC has two interlinked PCI functions for the same port. */
  24. static inline bool ef4_nic_is_dual_func(struct ef4_nic *efx)
  25. {
  26. return ef4_nic_rev(efx) < EF4_REV_FALCON_B0;
  27. }
  28. /* Read the current event from the event queue */
  29. static inline ef4_qword_t *ef4_event(struct ef4_channel *channel,
  30. unsigned int index)
  31. {
  32. return ((ef4_qword_t *) (channel->eventq.buf.addr)) +
  33. (index & channel->eventq_mask);
  34. }
  35. /* See if an event is present
  36. *
  37. * We check both the high and low dword of the event for all ones. We
  38. * wrote all ones when we cleared the event, and no valid event can
  39. * have all ones in either its high or low dwords. This approach is
  40. * robust against reordering.
  41. *
  42. * Note that using a single 64-bit comparison is incorrect; even
  43. * though the CPU read will be atomic, the DMA write may not be.
  44. */
  45. static inline int ef4_event_present(ef4_qword_t *event)
  46. {
  47. return !(EF4_DWORD_IS_ALL_ONES(event->dword[0]) |
  48. EF4_DWORD_IS_ALL_ONES(event->dword[1]));
  49. }
  50. /* Returns a pointer to the specified transmit descriptor in the TX
  51. * descriptor queue belonging to the specified channel.
  52. */
  53. static inline ef4_qword_t *
  54. ef4_tx_desc(struct ef4_tx_queue *tx_queue, unsigned int index)
  55. {
  56. return ((ef4_qword_t *) (tx_queue->txd.buf.addr)) + index;
  57. }
  58. /* Get partner of a TX queue, seen as part of the same net core queue */
  59. static inline struct ef4_tx_queue *ef4_tx_queue_partner(struct ef4_tx_queue *tx_queue)
  60. {
  61. if (tx_queue->queue & EF4_TXQ_TYPE_OFFLOAD)
  62. return tx_queue - EF4_TXQ_TYPE_OFFLOAD;
  63. else
  64. return tx_queue + EF4_TXQ_TYPE_OFFLOAD;
  65. }
  66. /* Report whether this TX queue would be empty for the given write_count.
  67. * May return false negative.
  68. */
  69. static inline bool __ef4_nic_tx_is_empty(struct ef4_tx_queue *tx_queue,
  70. unsigned int write_count)
  71. {
  72. unsigned int empty_read_count = READ_ONCE(tx_queue->empty_read_count);
  73. if (empty_read_count == 0)
  74. return false;
  75. return ((empty_read_count ^ write_count) & ~EF4_EMPTY_COUNT_VALID) == 0;
  76. }
  77. /* Decide whether to push a TX descriptor to the NIC vs merely writing
  78. * the doorbell. This can reduce latency when we are adding a single
  79. * descriptor to an empty queue, but is otherwise pointless. Further,
  80. * Falcon and Siena have hardware bugs (SF bug 33851) that may be
  81. * triggered if we don't check this.
  82. * We use the write_count used for the last doorbell push, to get the
  83. * NIC's view of the tx queue.
  84. */
  85. static inline bool ef4_nic_may_push_tx_desc(struct ef4_tx_queue *tx_queue,
  86. unsigned int write_count)
  87. {
  88. bool was_empty = __ef4_nic_tx_is_empty(tx_queue, write_count);
  89. tx_queue->empty_read_count = 0;
  90. return was_empty && tx_queue->write_count - write_count == 1;
  91. }
  92. /* Returns a pointer to the specified descriptor in the RX descriptor queue */
  93. static inline ef4_qword_t *
  94. ef4_rx_desc(struct ef4_rx_queue *rx_queue, unsigned int index)
  95. {
  96. return ((ef4_qword_t *) (rx_queue->rxd.buf.addr)) + index;
  97. }
  98. enum {
  99. PHY_TYPE_NONE = 0,
  100. PHY_TYPE_TXC43128 = 1,
  101. PHY_TYPE_88E1111 = 2,
  102. PHY_TYPE_SFX7101 = 3,
  103. PHY_TYPE_QT2022C2 = 4,
  104. PHY_TYPE_PM8358 = 6,
  105. PHY_TYPE_SFT9001A = 8,
  106. PHY_TYPE_QT2025C = 9,
  107. PHY_TYPE_SFT9001B = 10,
  108. };
  109. #define FALCON_XMAC_LOOPBACKS \
  110. ((1 << LOOPBACK_XGMII) | \
  111. (1 << LOOPBACK_XGXS) | \
  112. (1 << LOOPBACK_XAUI))
  113. /* Alignment of PCIe DMA boundaries (4KB) */
  114. #define EF4_PAGE_SIZE 4096
  115. /* Size and alignment of buffer table entries (same) */
  116. #define EF4_BUF_SIZE EF4_PAGE_SIZE
  117. /* NIC-generic software stats */
  118. enum {
  119. GENERIC_STAT_rx_noskb_drops,
  120. GENERIC_STAT_rx_nodesc_trunc,
  121. GENERIC_STAT_COUNT
  122. };
  123. /**
  124. * struct falcon_board_type - board operations and type information
  125. * @id: Board type id, as found in NVRAM
  126. * @init: Allocate resources and initialise peripheral hardware
  127. * @init_phy: Do board-specific PHY initialisation
  128. * @fini: Shut down hardware and free resources
  129. * @set_id_led: Set state of identifying LED or revert to automatic function
  130. * @monitor: Board-specific health check function
  131. */
  132. struct falcon_board_type {
  133. u8 id;
  134. int (*init) (struct ef4_nic *nic);
  135. void (*init_phy) (struct ef4_nic *efx);
  136. void (*fini) (struct ef4_nic *nic);
  137. void (*set_id_led) (struct ef4_nic *efx, enum ef4_led_mode mode);
  138. int (*monitor) (struct ef4_nic *nic);
  139. };
  140. /**
  141. * struct falcon_board - board information
  142. * @type: Type of board
  143. * @major: Major rev. ('A', 'B' ...)
  144. * @minor: Minor rev. (0, 1, ...)
  145. * @i2c_adap: I2C adapter for on-board peripherals
  146. * @i2c_data: Data for bit-banging algorithm
  147. * @hwmon_client: I2C client for hardware monitor
  148. * @ioexp_client: I2C client for power/port control
  149. */
  150. struct falcon_board {
  151. const struct falcon_board_type *type;
  152. int major;
  153. int minor;
  154. struct i2c_adapter i2c_adap;
  155. struct i2c_algo_bit_data i2c_data;
  156. struct i2c_client *hwmon_client, *ioexp_client;
  157. };
  158. /**
  159. * struct falcon_spi_device - a Falcon SPI (Serial Peripheral Interface) device
  160. * @device_id: Controller's id for the device
  161. * @size: Size (in bytes)
  162. * @addr_len: Number of address bytes in read/write commands
  163. * @munge_address: Flag whether addresses should be munged.
  164. * Some devices with 9-bit addresses (e.g. AT25040A EEPROM)
  165. * use bit 3 of the command byte as address bit A8, rather
  166. * than having a two-byte address. If this flag is set, then
  167. * commands should be munged in this way.
  168. * @erase_command: Erase command (or 0 if sector erase not needed).
  169. * @erase_size: Erase sector size (in bytes)
  170. * Erase commands affect sectors with this size and alignment.
  171. * This must be a power of two.
  172. * @block_size: Write block size (in bytes).
  173. * Write commands are limited to blocks with this size and alignment.
  174. */
  175. struct falcon_spi_device {
  176. int device_id;
  177. unsigned int size;
  178. unsigned int addr_len;
  179. unsigned int munge_address:1;
  180. u8 erase_command;
  181. unsigned int erase_size;
  182. unsigned int block_size;
  183. };
  184. static inline bool falcon_spi_present(const struct falcon_spi_device *spi)
  185. {
  186. return spi->size != 0;
  187. }
  188. enum {
  189. FALCON_STAT_tx_bytes = GENERIC_STAT_COUNT,
  190. FALCON_STAT_tx_packets,
  191. FALCON_STAT_tx_pause,
  192. FALCON_STAT_tx_control,
  193. FALCON_STAT_tx_unicast,
  194. FALCON_STAT_tx_multicast,
  195. FALCON_STAT_tx_broadcast,
  196. FALCON_STAT_tx_lt64,
  197. FALCON_STAT_tx_64,
  198. FALCON_STAT_tx_65_to_127,
  199. FALCON_STAT_tx_128_to_255,
  200. FALCON_STAT_tx_256_to_511,
  201. FALCON_STAT_tx_512_to_1023,
  202. FALCON_STAT_tx_1024_to_15xx,
  203. FALCON_STAT_tx_15xx_to_jumbo,
  204. FALCON_STAT_tx_gtjumbo,
  205. FALCON_STAT_tx_non_tcpudp,
  206. FALCON_STAT_tx_mac_src_error,
  207. FALCON_STAT_tx_ip_src_error,
  208. FALCON_STAT_rx_bytes,
  209. FALCON_STAT_rx_good_bytes,
  210. FALCON_STAT_rx_bad_bytes,
  211. FALCON_STAT_rx_packets,
  212. FALCON_STAT_rx_good,
  213. FALCON_STAT_rx_bad,
  214. FALCON_STAT_rx_pause,
  215. FALCON_STAT_rx_control,
  216. FALCON_STAT_rx_unicast,
  217. FALCON_STAT_rx_multicast,
  218. FALCON_STAT_rx_broadcast,
  219. FALCON_STAT_rx_lt64,
  220. FALCON_STAT_rx_64,
  221. FALCON_STAT_rx_65_to_127,
  222. FALCON_STAT_rx_128_to_255,
  223. FALCON_STAT_rx_256_to_511,
  224. FALCON_STAT_rx_512_to_1023,
  225. FALCON_STAT_rx_1024_to_15xx,
  226. FALCON_STAT_rx_15xx_to_jumbo,
  227. FALCON_STAT_rx_gtjumbo,
  228. FALCON_STAT_rx_bad_lt64,
  229. FALCON_STAT_rx_bad_gtjumbo,
  230. FALCON_STAT_rx_overflow,
  231. FALCON_STAT_rx_symbol_error,
  232. FALCON_STAT_rx_align_error,
  233. FALCON_STAT_rx_length_error,
  234. FALCON_STAT_rx_internal_error,
  235. FALCON_STAT_rx_nodesc_drop_cnt,
  236. FALCON_STAT_COUNT
  237. };
  238. /**
  239. * struct falcon_nic_data - Falcon NIC state
  240. * @pci_dev2: Secondary function of Falcon A
  241. * @efx: ef4_nic pointer
  242. * @board: Board state and functions
  243. * @stats: Hardware statistics
  244. * @stats_disable_count: Nest count for disabling statistics fetches
  245. * @stats_pending: Is there a pending DMA of MAC statistics.
  246. * @stats_timer: A timer for regularly fetching MAC statistics.
  247. * @spi_flash: SPI flash device
  248. * @spi_eeprom: SPI EEPROM device
  249. * @spi_lock: SPI bus lock
  250. * @mdio_lock: MDIO bus lock
  251. * @xmac_poll_required: XMAC link state needs polling
  252. */
  253. struct falcon_nic_data {
  254. struct pci_dev *pci_dev2;
  255. struct ef4_nic *efx;
  256. struct falcon_board board;
  257. u64 stats[FALCON_STAT_COUNT];
  258. unsigned int stats_disable_count;
  259. bool stats_pending;
  260. struct timer_list stats_timer;
  261. struct falcon_spi_device spi_flash;
  262. struct falcon_spi_device spi_eeprom;
  263. struct mutex spi_lock;
  264. struct mutex mdio_lock;
  265. bool xmac_poll_required;
  266. };
  267. static inline struct falcon_board *falcon_board(struct ef4_nic *efx)
  268. {
  269. struct falcon_nic_data *data = efx->nic_data;
  270. return &data->board;
  271. }
  272. struct ethtool_ts_info;
  273. extern const struct ef4_nic_type falcon_a1_nic_type;
  274. extern const struct ef4_nic_type falcon_b0_nic_type;
  275. /**************************************************************************
  276. *
  277. * Externs
  278. *
  279. **************************************************************************
  280. */
  281. int falcon_probe_board(struct ef4_nic *efx, u16 revision_info);
  282. /* TX data path */
  283. static inline int ef4_nic_probe_tx(struct ef4_tx_queue *tx_queue)
  284. {
  285. return tx_queue->efx->type->tx_probe(tx_queue);
  286. }
  287. static inline void ef4_nic_init_tx(struct ef4_tx_queue *tx_queue)
  288. {
  289. tx_queue->efx->type->tx_init(tx_queue);
  290. }
  291. static inline void ef4_nic_remove_tx(struct ef4_tx_queue *tx_queue)
  292. {
  293. tx_queue->efx->type->tx_remove(tx_queue);
  294. }
  295. static inline void ef4_nic_push_buffers(struct ef4_tx_queue *tx_queue)
  296. {
  297. tx_queue->efx->type->tx_write(tx_queue);
  298. }
  299. /* RX data path */
  300. static inline int ef4_nic_probe_rx(struct ef4_rx_queue *rx_queue)
  301. {
  302. return rx_queue->efx->type->rx_probe(rx_queue);
  303. }
  304. static inline void ef4_nic_init_rx(struct ef4_rx_queue *rx_queue)
  305. {
  306. rx_queue->efx->type->rx_init(rx_queue);
  307. }
  308. static inline void ef4_nic_remove_rx(struct ef4_rx_queue *rx_queue)
  309. {
  310. rx_queue->efx->type->rx_remove(rx_queue);
  311. }
  312. static inline void ef4_nic_notify_rx_desc(struct ef4_rx_queue *rx_queue)
  313. {
  314. rx_queue->efx->type->rx_write(rx_queue);
  315. }
  316. static inline void ef4_nic_generate_fill_event(struct ef4_rx_queue *rx_queue)
  317. {
  318. rx_queue->efx->type->rx_defer_refill(rx_queue);
  319. }
  320. /* Event data path */
  321. static inline int ef4_nic_probe_eventq(struct ef4_channel *channel)
  322. {
  323. return channel->efx->type->ev_probe(channel);
  324. }
  325. static inline int ef4_nic_init_eventq(struct ef4_channel *channel)
  326. {
  327. return channel->efx->type->ev_init(channel);
  328. }
  329. static inline void ef4_nic_fini_eventq(struct ef4_channel *channel)
  330. {
  331. channel->efx->type->ev_fini(channel);
  332. }
  333. static inline void ef4_nic_remove_eventq(struct ef4_channel *channel)
  334. {
  335. channel->efx->type->ev_remove(channel);
  336. }
  337. static inline int
  338. ef4_nic_process_eventq(struct ef4_channel *channel, int quota)
  339. {
  340. return channel->efx->type->ev_process(channel, quota);
  341. }
  342. static inline void ef4_nic_eventq_read_ack(struct ef4_channel *channel)
  343. {
  344. channel->efx->type->ev_read_ack(channel);
  345. }
  346. void ef4_nic_event_test_start(struct ef4_channel *channel);
  347. /* queue operations */
  348. int ef4_farch_tx_probe(struct ef4_tx_queue *tx_queue);
  349. void ef4_farch_tx_init(struct ef4_tx_queue *tx_queue);
  350. void ef4_farch_tx_fini(struct ef4_tx_queue *tx_queue);
  351. void ef4_farch_tx_remove(struct ef4_tx_queue *tx_queue);
  352. void ef4_farch_tx_write(struct ef4_tx_queue *tx_queue);
  353. unsigned int ef4_farch_tx_limit_len(struct ef4_tx_queue *tx_queue,
  354. dma_addr_t dma_addr, unsigned int len);
  355. int ef4_farch_rx_probe(struct ef4_rx_queue *rx_queue);
  356. void ef4_farch_rx_init(struct ef4_rx_queue *rx_queue);
  357. void ef4_farch_rx_fini(struct ef4_rx_queue *rx_queue);
  358. void ef4_farch_rx_remove(struct ef4_rx_queue *rx_queue);
  359. void ef4_farch_rx_write(struct ef4_rx_queue *rx_queue);
  360. void ef4_farch_rx_defer_refill(struct ef4_rx_queue *rx_queue);
  361. int ef4_farch_ev_probe(struct ef4_channel *channel);
  362. int ef4_farch_ev_init(struct ef4_channel *channel);
  363. void ef4_farch_ev_fini(struct ef4_channel *channel);
  364. void ef4_farch_ev_remove(struct ef4_channel *channel);
  365. int ef4_farch_ev_process(struct ef4_channel *channel, int quota);
  366. void ef4_farch_ev_read_ack(struct ef4_channel *channel);
  367. void ef4_farch_ev_test_generate(struct ef4_channel *channel);
  368. /* filter operations */
  369. int ef4_farch_filter_table_probe(struct ef4_nic *efx);
  370. void ef4_farch_filter_table_restore(struct ef4_nic *efx);
  371. void ef4_farch_filter_table_remove(struct ef4_nic *efx);
  372. void ef4_farch_filter_update_rx_scatter(struct ef4_nic *efx);
  373. s32 ef4_farch_filter_insert(struct ef4_nic *efx, struct ef4_filter_spec *spec,
  374. bool replace);
  375. int ef4_farch_filter_remove_safe(struct ef4_nic *efx,
  376. enum ef4_filter_priority priority,
  377. u32 filter_id);
  378. int ef4_farch_filter_get_safe(struct ef4_nic *efx,
  379. enum ef4_filter_priority priority, u32 filter_id,
  380. struct ef4_filter_spec *);
  381. int ef4_farch_filter_clear_rx(struct ef4_nic *efx,
  382. enum ef4_filter_priority priority);
  383. u32 ef4_farch_filter_count_rx_used(struct ef4_nic *efx,
  384. enum ef4_filter_priority priority);
  385. u32 ef4_farch_filter_get_rx_id_limit(struct ef4_nic *efx);
  386. s32 ef4_farch_filter_get_rx_ids(struct ef4_nic *efx,
  387. enum ef4_filter_priority priority, u32 *buf,
  388. u32 size);
  389. #ifdef CONFIG_RFS_ACCEL
  390. s32 ef4_farch_filter_rfs_insert(struct ef4_nic *efx,
  391. struct ef4_filter_spec *spec);
  392. bool ef4_farch_filter_rfs_expire_one(struct ef4_nic *efx, u32 flow_id,
  393. unsigned int index);
  394. #endif
  395. void ef4_farch_filter_sync_rx_mode(struct ef4_nic *efx);
  396. bool ef4_nic_event_present(struct ef4_channel *channel);
  397. /* Some statistics are computed as A - B where A and B each increase
  398. * linearly with some hardware counter(s) and the counters are read
  399. * asynchronously. If the counters contributing to B are always read
  400. * after those contributing to A, the computed value may be lower than
  401. * the true value by some variable amount, and may decrease between
  402. * subsequent computations.
  403. *
  404. * We should never allow statistics to decrease or to exceed the true
  405. * value. Since the computed value will never be greater than the
  406. * true value, we can achieve this by only storing the computed value
  407. * when it increases.
  408. */
  409. static inline void ef4_update_diff_stat(u64 *stat, u64 diff)
  410. {
  411. if ((s64)(diff - *stat) > 0)
  412. *stat = diff;
  413. }
  414. /* Interrupts */
  415. int ef4_nic_init_interrupt(struct ef4_nic *efx);
  416. int ef4_nic_irq_test_start(struct ef4_nic *efx);
  417. void ef4_nic_fini_interrupt(struct ef4_nic *efx);
  418. void ef4_farch_irq_enable_master(struct ef4_nic *efx);
  419. int ef4_farch_irq_test_generate(struct ef4_nic *efx);
  420. void ef4_farch_irq_disable_master(struct ef4_nic *efx);
  421. irqreturn_t ef4_farch_msi_interrupt(int irq, void *dev_id);
  422. irqreturn_t ef4_farch_legacy_interrupt(int irq, void *dev_id);
  423. irqreturn_t ef4_farch_fatal_interrupt(struct ef4_nic *efx);
  424. static inline int ef4_nic_event_test_irq_cpu(struct ef4_channel *channel)
  425. {
  426. return READ_ONCE(channel->event_test_cpu);
  427. }
  428. static inline int ef4_nic_irq_test_irq_cpu(struct ef4_nic *efx)
  429. {
  430. return READ_ONCE(efx->last_irq_cpu);
  431. }
  432. /* Global Resources */
  433. int ef4_nic_flush_queues(struct ef4_nic *efx);
  434. int ef4_farch_fini_dmaq(struct ef4_nic *efx);
  435. void ef4_farch_finish_flr(struct ef4_nic *efx);
  436. void falcon_start_nic_stats(struct ef4_nic *efx);
  437. void falcon_stop_nic_stats(struct ef4_nic *efx);
  438. int falcon_reset_xaui(struct ef4_nic *efx);
  439. void ef4_farch_dimension_resources(struct ef4_nic *efx, unsigned sram_lim_qw);
  440. void ef4_farch_init_common(struct ef4_nic *efx);
  441. void ef4_farch_rx_push_indir_table(struct ef4_nic *efx);
  442. int ef4_nic_alloc_buffer(struct ef4_nic *efx, struct ef4_buffer *buffer,
  443. unsigned int len, gfp_t gfp_flags);
  444. void ef4_nic_free_buffer(struct ef4_nic *efx, struct ef4_buffer *buffer);
  445. /* Tests */
  446. struct ef4_farch_register_test {
  447. unsigned address;
  448. ef4_oword_t mask;
  449. };
  450. int ef4_farch_test_registers(struct ef4_nic *efx,
  451. const struct ef4_farch_register_test *regs,
  452. size_t n_regs);
  453. size_t ef4_nic_get_regs_len(struct ef4_nic *efx);
  454. void ef4_nic_get_regs(struct ef4_nic *efx, void *buf);
  455. size_t ef4_nic_describe_stats(const struct ef4_hw_stat_desc *desc, size_t count,
  456. const unsigned long *mask, u8 *names);
  457. void ef4_nic_update_stats(const struct ef4_hw_stat_desc *desc, size_t count,
  458. const unsigned long *mask, u64 *stats,
  459. const void *dma_buf, bool accumulate);
  460. void ef4_nic_fix_nodesc_drop_stat(struct ef4_nic *efx, u64 *stat);
  461. #define EF4_MAX_FLUSH_TIME 5000
  462. void ef4_farch_generate_event(struct ef4_nic *efx, unsigned int evq,
  463. ef4_qword_t *event);
  464. #endif /* EF4_NIC_H */