nic.h 16 KB

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