tsnep.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */
  3. #ifndef _TSNEP_H
  4. #define _TSNEP_H
  5. #include "tsnep_hw.h"
  6. #include <linux/platform_device.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/phy.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/net_tstamp.h>
  12. #include <linux/ptp_clock_kernel.h>
  13. #include <linux/miscdevice.h>
  14. #include <net/xdp.h>
  15. #define TSNEP "tsnep"
  16. #define TSNEP_RING_SIZE 256
  17. #define TSNEP_RING_MASK (TSNEP_RING_SIZE - 1)
  18. #define TSNEP_RING_RX_REFILL 16
  19. #define TSNEP_RING_RX_REUSE (TSNEP_RING_SIZE - TSNEP_RING_SIZE / 4)
  20. #define TSNEP_RING_ENTRIES_PER_PAGE (PAGE_SIZE / TSNEP_DESC_SIZE)
  21. #define TSNEP_RING_PAGE_COUNT (TSNEP_RING_SIZE / TSNEP_RING_ENTRIES_PER_PAGE)
  22. struct tsnep_gcl {
  23. void __iomem *addr;
  24. u64 base_time;
  25. u64 cycle_time;
  26. u64 cycle_time_extension;
  27. struct tsnep_gcl_operation operation[TSNEP_GCL_COUNT];
  28. int count;
  29. u64 change_limit;
  30. u64 start_time;
  31. bool change;
  32. };
  33. enum tsnep_rxnfc_filter_type {
  34. TSNEP_RXNFC_ETHER_TYPE,
  35. };
  36. struct tsnep_rxnfc_filter {
  37. enum tsnep_rxnfc_filter_type type;
  38. union {
  39. u16 ether_type;
  40. };
  41. };
  42. struct tsnep_rxnfc_rule {
  43. struct list_head list;
  44. struct tsnep_rxnfc_filter filter;
  45. int queue_index;
  46. int location;
  47. };
  48. struct tsnep_tx_entry {
  49. struct tsnep_tx_desc *desc;
  50. struct tsnep_tx_desc_wb *desc_wb;
  51. dma_addr_t desc_dma;
  52. bool owner_user_flag;
  53. u32 properties;
  54. u32 type;
  55. union {
  56. struct sk_buff *skb;
  57. struct xdp_frame *xdpf;
  58. bool zc;
  59. };
  60. size_t len;
  61. DEFINE_DMA_UNMAP_ADDR(dma);
  62. };
  63. struct tsnep_tx {
  64. struct tsnep_adapter *adapter;
  65. void __iomem *addr;
  66. int queue_index;
  67. void *page[TSNEP_RING_PAGE_COUNT];
  68. dma_addr_t page_dma[TSNEP_RING_PAGE_COUNT];
  69. struct tsnep_tx_entry entry[TSNEP_RING_SIZE];
  70. int write;
  71. int read;
  72. u32 owner_counter;
  73. int increment_owner_counter;
  74. struct xsk_buff_pool *xsk_pool;
  75. u32 packets;
  76. u32 bytes;
  77. u32 dropped;
  78. };
  79. struct tsnep_rx_entry {
  80. struct tsnep_rx_desc *desc;
  81. struct tsnep_rx_desc_wb *desc_wb;
  82. dma_addr_t desc_dma;
  83. u32 properties;
  84. union {
  85. struct page *page;
  86. struct xdp_buff *xdp;
  87. };
  88. size_t len;
  89. dma_addr_t dma;
  90. };
  91. struct tsnep_rx {
  92. struct tsnep_adapter *adapter;
  93. void __iomem *addr;
  94. int queue_index;
  95. int tx_queue_index;
  96. void *page[TSNEP_RING_PAGE_COUNT];
  97. dma_addr_t page_dma[TSNEP_RING_PAGE_COUNT];
  98. struct tsnep_rx_entry entry[TSNEP_RING_SIZE];
  99. int write;
  100. int read;
  101. u32 owner_counter;
  102. int increment_owner_counter;
  103. struct page_pool *page_pool;
  104. struct page **page_buffer;
  105. struct xsk_buff_pool *xsk_pool;
  106. struct xdp_buff **xdp_batch;
  107. u32 packets;
  108. u32 bytes;
  109. u32 dropped;
  110. u32 multicast;
  111. u32 alloc_failed;
  112. struct xdp_rxq_info xdp_rxq;
  113. struct xdp_rxq_info xdp_rxq_zc;
  114. };
  115. struct tsnep_queue {
  116. struct tsnep_adapter *adapter;
  117. char name[IFNAMSIZ + 16];
  118. struct tsnep_tx *tx;
  119. struct tsnep_rx *rx;
  120. struct napi_struct napi;
  121. int irq;
  122. u32 irq_mask;
  123. void __iomem *irq_delay_addr;
  124. u8 irq_delay;
  125. };
  126. struct tsnep_adapter {
  127. struct net_device *netdev;
  128. u8 mac_address[ETH_ALEN];
  129. struct mii_bus *mdiobus;
  130. bool suppress_preamble;
  131. phy_interface_t phy_mode;
  132. struct phy_device *phydev;
  133. int msg_enable;
  134. struct platform_device *pdev;
  135. struct device *dmadev;
  136. void __iomem *addr;
  137. bool gate_control;
  138. /* gate control lock */
  139. struct mutex gate_control_lock;
  140. bool gate_control_active;
  141. struct tsnep_gcl gcl[2];
  142. int next_gcl;
  143. struct kernel_hwtstamp_config hwtstamp_config;
  144. struct ptp_clock *ptp_clock;
  145. struct ptp_clock_info ptp_clock_info;
  146. /* ptp clock lock */
  147. spinlock_t ptp_lock;
  148. /* RX flow classification rules lock */
  149. struct mutex rxnfc_lock;
  150. struct list_head rxnfc_rules;
  151. int rxnfc_count;
  152. int rxnfc_max;
  153. struct bpf_prog *xdp_prog;
  154. int num_tx_queues;
  155. struct tsnep_tx tx[TSNEP_MAX_QUEUES];
  156. int num_rx_queues;
  157. struct tsnep_rx rx[TSNEP_MAX_QUEUES];
  158. int num_queues;
  159. struct tsnep_queue queue[TSNEP_MAX_QUEUES];
  160. };
  161. extern const struct ethtool_ops tsnep_ethtool_ops;
  162. int tsnep_ptp_init(struct tsnep_adapter *adapter);
  163. void tsnep_ptp_cleanup(struct tsnep_adapter *adapter);
  164. int tsnep_ptp_hwtstamp_get(struct net_device *netdev,
  165. struct kernel_hwtstamp_config *config);
  166. int tsnep_ptp_hwtstamp_set(struct net_device *netdev,
  167. struct kernel_hwtstamp_config *config,
  168. struct netlink_ext_ack *extack);
  169. int tsnep_tc_init(struct tsnep_adapter *adapter);
  170. void tsnep_tc_cleanup(struct tsnep_adapter *adapter);
  171. int tsnep_tc_setup(struct net_device *netdev, enum tc_setup_type type,
  172. void *type_data);
  173. int tsnep_rxnfc_init(struct tsnep_adapter *adapter);
  174. void tsnep_rxnfc_cleanup(struct tsnep_adapter *adapter);
  175. int tsnep_rxnfc_get_rule(struct tsnep_adapter *adapter,
  176. struct ethtool_rxnfc *cmd);
  177. int tsnep_rxnfc_get_all(struct tsnep_adapter *adapter,
  178. struct ethtool_rxnfc *cmd,
  179. u32 *rule_locs);
  180. int tsnep_rxnfc_add_rule(struct tsnep_adapter *adapter,
  181. struct ethtool_rxnfc *cmd);
  182. int tsnep_rxnfc_del_rule(struct tsnep_adapter *adapter,
  183. struct ethtool_rxnfc *cmd);
  184. int tsnep_xdp_setup_prog(struct tsnep_adapter *adapter, struct bpf_prog *prog,
  185. struct netlink_ext_ack *extack);
  186. int tsnep_xdp_setup_pool(struct tsnep_adapter *adapter,
  187. struct xsk_buff_pool *pool, u16 queue_id);
  188. #if IS_ENABLED(CONFIG_TSNEP_SELFTESTS)
  189. int tsnep_ethtool_get_test_count(void);
  190. void tsnep_ethtool_get_test_strings(u8 *data);
  191. void tsnep_ethtool_self_test(struct net_device *netdev,
  192. struct ethtool_test *eth_test, u64 *data);
  193. #else
  194. static inline int tsnep_ethtool_get_test_count(void)
  195. {
  196. return -EOPNOTSUPP;
  197. }
  198. static inline void tsnep_ethtool_get_test_strings(u8 *data)
  199. {
  200. /* not enabled */
  201. }
  202. static inline void tsnep_ethtool_self_test(struct net_device *dev,
  203. struct ethtool_test *eth_test,
  204. u64 *data)
  205. {
  206. /* not enabled */
  207. }
  208. #endif /* CONFIG_TSNEP_SELFTESTS */
  209. void tsnep_get_system_time(struct tsnep_adapter *adapter, u64 *time);
  210. int tsnep_set_irq_coalesce(struct tsnep_queue *queue, u32 usecs);
  211. u32 tsnep_get_irq_coalesce(struct tsnep_queue *queue);
  212. int tsnep_enable_xsk(struct tsnep_queue *queue, struct xsk_buff_pool *pool);
  213. void tsnep_disable_xsk(struct tsnep_queue *queue);
  214. #endif /* _TSNEP_H */