xref: /linux/drivers/net/ethernet/engleder/tsnep.h (revision 24aeeb107f0724fa15e16d5f28b39f3c3ecfc746)
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 #define TSNEP_QUEUES 1
25 
26 struct tsnep_gcl {
27 	void __iomem *addr;
28 
29 	u64 base_time;
30 	u64 cycle_time;
31 	u64 cycle_time_extension;
32 
33 	struct tsnep_gcl_operation operation[TSNEP_GCL_COUNT];
34 	int count;
35 
36 	u64 change_limit;
37 
38 	u64 start_time;
39 	bool change;
40 };
41 
42 struct tsnep_tx_entry {
43 	struct tsnep_tx_desc *desc;
44 	struct tsnep_tx_desc_wb *desc_wb;
45 	dma_addr_t desc_dma;
46 	bool owner_user_flag;
47 
48 	u32 properties;
49 
50 	struct sk_buff *skb;
51 	size_t len;
52 	DEFINE_DMA_UNMAP_ADDR(dma);
53 };
54 
55 struct tsnep_tx {
56 	struct tsnep_adapter *adapter;
57 	void __iomem *addr;
58 
59 	void *page[TSNEP_RING_PAGE_COUNT];
60 	dma_addr_t page_dma[TSNEP_RING_PAGE_COUNT];
61 
62 	/* TX ring lock */
63 	spinlock_t lock;
64 	struct tsnep_tx_entry entry[TSNEP_RING_SIZE];
65 	int write;
66 	int read;
67 	u32 owner_counter;
68 	int increment_owner_counter;
69 
70 	u32 packets;
71 	u32 bytes;
72 	u32 dropped;
73 };
74 
75 struct tsnep_rx_entry {
76 	struct tsnep_rx_desc *desc;
77 	struct tsnep_rx_desc_wb *desc_wb;
78 	dma_addr_t desc_dma;
79 
80 	u32 properties;
81 
82 	struct sk_buff *skb;
83 	size_t len;
84 	DEFINE_DMA_UNMAP_ADDR(dma);
85 };
86 
87 struct tsnep_rx {
88 	struct tsnep_adapter *adapter;
89 	void __iomem *addr;
90 	int queue_index;
91 
92 	void *page[TSNEP_RING_PAGE_COUNT];
93 	dma_addr_t page_dma[TSNEP_RING_PAGE_COUNT];
94 
95 	struct tsnep_rx_entry entry[TSNEP_RING_SIZE];
96 	int read;
97 	u32 owner_counter;
98 	int increment_owner_counter;
99 
100 	u32 packets;
101 	u32 bytes;
102 	u32 dropped;
103 	u32 multicast;
104 };
105 
106 struct tsnep_queue {
107 	struct tsnep_adapter *adapter;
108 
109 	struct tsnep_tx *tx;
110 	struct tsnep_rx *rx;
111 
112 	struct napi_struct napi;
113 
114 	u32 irq_mask;
115 };
116 
117 struct tsnep_adapter {
118 	struct net_device *netdev;
119 	u8 mac_address[ETH_ALEN];
120 	struct mii_bus *mdiobus;
121 	bool suppress_preamble;
122 	phy_interface_t phy_mode;
123 	struct phy_device *phydev;
124 	int msg_enable;
125 
126 	struct platform_device *pdev;
127 	struct device *dmadev;
128 	void __iomem *addr;
129 	int irq;
130 
131 	bool gate_control;
132 	/* gate control lock */
133 	struct mutex gate_control_lock;
134 	bool gate_control_active;
135 	struct tsnep_gcl gcl[2];
136 	int next_gcl;
137 
138 	struct hwtstamp_config hwtstamp_config;
139 	struct ptp_clock *ptp_clock;
140 	struct ptp_clock_info ptp_clock_info;
141 	/* ptp clock lock */
142 	spinlock_t ptp_lock;
143 
144 	int num_tx_queues;
145 	struct tsnep_tx tx[TSNEP_MAX_QUEUES];
146 	int num_rx_queues;
147 	struct tsnep_rx rx[TSNEP_MAX_QUEUES];
148 
149 	int num_queues;
150 	struct tsnep_queue queue[TSNEP_MAX_QUEUES];
151 };
152 
153 extern const struct ethtool_ops tsnep_ethtool_ops;
154 
155 int tsnep_ptp_init(struct tsnep_adapter *adapter);
156 void tsnep_ptp_cleanup(struct tsnep_adapter *adapter);
157 int tsnep_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
158 
159 int tsnep_tc_init(struct tsnep_adapter *adapter);
160 void tsnep_tc_cleanup(struct tsnep_adapter *adapter);
161 int tsnep_tc_setup(struct net_device *netdev, enum tc_setup_type type,
162 		   void *type_data);
163 
164 #if IS_ENABLED(CONFIG_TSNEP_SELFTESTS)
165 int tsnep_ethtool_get_test_count(void);
166 void tsnep_ethtool_get_test_strings(u8 *data);
167 void tsnep_ethtool_self_test(struct net_device *netdev,
168 			     struct ethtool_test *eth_test, u64 *data);
169 #else
170 static inline int tsnep_ethtool_get_test_count(void)
171 {
172 	return -EOPNOTSUPP;
173 }
174 
175 static inline void tsnep_ethtool_get_test_strings(u8 *data)
176 {
177 	/* not enabled */
178 }
179 
180 static inline void tsnep_ethtool_self_test(struct net_device *dev,
181 					   struct ethtool_test *eth_test,
182 					   u64 *data)
183 {
184 	/* not enabled */
185 }
186 #endif /* CONFIG_TSNEP_SELFTESTS */
187 
188 void tsnep_get_system_time(struct tsnep_adapter *adapter, u64 *time);
189 
190 #endif /* _TSNEP_H */
191