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_ENTRIES_PER_PAGE (PAGE_SIZE / TSNEP_DESC_SIZE)
22 #define TSNEP_RING_PAGE_COUNT (TSNEP_RING_SIZE / TSNEP_RING_ENTRIES_PER_PAGE)
23
24 struct tsnep_gcl {
25 void __iomem *addr;
26
27 u64 base_time;
28 u64 cycle_time;
29 u64 cycle_time_extension;
30
31 struct tsnep_gcl_operation operation[TSNEP_GCL_COUNT];
32 int count;
33
34 u64 change_limit;
35
36 u64 start_time;
37 bool change;
38 };
39
40 enum tsnep_rxnfc_filter_type {
41 TSNEP_RXNFC_ETHER_TYPE,
42 };
43
44 struct tsnep_rxnfc_filter {
45 enum tsnep_rxnfc_filter_type type;
46 union {
47 u16 ether_type;
48 };
49 };
50
51 struct tsnep_rxnfc_rule {
52 struct list_head list;
53 struct tsnep_rxnfc_filter filter;
54 int queue_index;
55 int location;
56 };
57
58 struct tsnep_tx_entry {
59 struct tsnep_tx_desc *desc;
60 struct tsnep_tx_desc_wb *desc_wb;
61 dma_addr_t desc_dma;
62 bool owner_user_flag;
63
64 u32 properties;
65
66 struct sk_buff *skb;
67 size_t len;
68 DEFINE_DMA_UNMAP_ADDR(dma);
69 };
70
71 struct tsnep_tx {
72 struct tsnep_adapter *adapter;
73 void __iomem *addr;
74 int queue_index;
75
76 void *page[TSNEP_RING_PAGE_COUNT];
77 dma_addr_t page_dma[TSNEP_RING_PAGE_COUNT];
78
79 /* TX ring lock */
80 spinlock_t lock;
81 struct tsnep_tx_entry entry[TSNEP_RING_SIZE];
82 int write;
83 int read;
84 u32 owner_counter;
85 int increment_owner_counter;
86
87 u32 packets;
88 u32 bytes;
89 u32 dropped;
90 };
91
92 struct tsnep_rx_entry {
93 struct tsnep_rx_desc *desc;
94 struct tsnep_rx_desc_wb *desc_wb;
95 dma_addr_t desc_dma;
96
97 u32 properties;
98
99 struct page *page;
100 size_t len;
101 dma_addr_t dma;
102 };
103
104 struct tsnep_rx {
105 struct tsnep_adapter *adapter;
106 void __iomem *addr;
107 int queue_index;
108
109 void *page[TSNEP_RING_PAGE_COUNT];
110 dma_addr_t page_dma[TSNEP_RING_PAGE_COUNT];
111
112 struct tsnep_rx_entry entry[TSNEP_RING_SIZE];
113 int read;
114 u32 owner_counter;
115 int increment_owner_counter;
116 struct page_pool *page_pool;
117
118 u32 packets;
119 u32 bytes;
120 u32 dropped;
121 u32 multicast;
122 };
123
124 struct tsnep_queue {
125 struct tsnep_adapter *adapter;
126 char name[IFNAMSIZ + 9];
127
128 struct tsnep_tx *tx;
129 struct tsnep_rx *rx;
130
131 struct napi_struct napi;
132
133 int irq;
134 u32 irq_mask;
135 };
136
137 struct tsnep_adapter {
138 struct net_device *netdev;
139 u8 mac_address[ETH_ALEN];
140 struct mii_bus *mdiobus;
141 bool suppress_preamble;
142 phy_interface_t phy_mode;
143 struct phy_device *phydev;
144 int msg_enable;
145
146 struct platform_device *pdev;
147 struct device *dmadev;
148 void __iomem *addr;
149
150 bool gate_control;
151 /* gate control lock */
152 struct mutex gate_control_lock;
153 bool gate_control_active;
154 struct tsnep_gcl gcl[2];
155 int next_gcl;
156
157 struct hwtstamp_config hwtstamp_config;
158 struct ptp_clock *ptp_clock;
159 struct ptp_clock_info ptp_clock_info;
160 /* ptp clock lock */
161 spinlock_t ptp_lock;
162
163 /* RX flow classification rules lock */
164 struct mutex rxnfc_lock;
165 struct list_head rxnfc_rules;
166 int rxnfc_count;
167 int rxnfc_max;
168
169 int num_tx_queues;
170 struct tsnep_tx tx[TSNEP_MAX_QUEUES];
171 int num_rx_queues;
172 struct tsnep_rx rx[TSNEP_MAX_QUEUES];
173
174 int num_queues;
175 struct tsnep_queue queue[TSNEP_MAX_QUEUES];
176 };
177
178 extern const struct ethtool_ops tsnep_ethtool_ops;
179
180 int tsnep_ptp_init(struct tsnep_adapter *adapter);
181 void tsnep_ptp_cleanup(struct tsnep_adapter *adapter);
182 int tsnep_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
183
184 int tsnep_tc_init(struct tsnep_adapter *adapter);
185 void tsnep_tc_cleanup(struct tsnep_adapter *adapter);
186 int tsnep_tc_setup(struct net_device *netdev, enum tc_setup_type type,
187 void *type_data);
188
189 int tsnep_rxnfc_init(struct tsnep_adapter *adapter);
190 void tsnep_rxnfc_cleanup(struct tsnep_adapter *adapter);
191 int tsnep_rxnfc_get_rule(struct tsnep_adapter *adapter,
192 struct ethtool_rxnfc *cmd);
193 int tsnep_rxnfc_get_all(struct tsnep_adapter *adapter,
194 struct ethtool_rxnfc *cmd,
195 u32 *rule_locs);
196 int tsnep_rxnfc_add_rule(struct tsnep_adapter *adapter,
197 struct ethtool_rxnfc *cmd);
198 int tsnep_rxnfc_del_rule(struct tsnep_adapter *adapter,
199 struct ethtool_rxnfc *cmd);
200
201 #if IS_ENABLED(CONFIG_TSNEP_SELFTESTS)
202 int tsnep_ethtool_get_test_count(void);
203 void tsnep_ethtool_get_test_strings(u8 *data);
204 void tsnep_ethtool_self_test(struct net_device *netdev,
205 struct ethtool_test *eth_test, u64 *data);
206 #else
tsnep_ethtool_get_test_count(void)207 static inline int tsnep_ethtool_get_test_count(void)
208 {
209 return -EOPNOTSUPP;
210 }
211
tsnep_ethtool_get_test_strings(u8 * data)212 static inline void tsnep_ethtool_get_test_strings(u8 *data)
213 {
214 /* not enabled */
215 }
216
tsnep_ethtool_self_test(struct net_device * dev,struct ethtool_test * eth_test,u64 * data)217 static inline void tsnep_ethtool_self_test(struct net_device *dev,
218 struct ethtool_test *eth_test,
219 u64 *data)
220 {
221 /* not enabled */
222 }
223 #endif /* CONFIG_TSNEP_SELFTESTS */
224
225 void tsnep_get_system_time(struct tsnep_adapter *adapter, u64 *time);
226
227 #endif /* _TSNEP_H */
228