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