xref: /linux/drivers/net/ethernet/ti/icssg/icssg_prueth.h (revision 860a9bed265146b10311bcadbbcef59c3af4454d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Texas Instruments ICSSG Ethernet driver
3  *
4  * Copyright (C) 2018-2022 Texas Instruments Incorporated - https://www.ti.com/
5  *
6  */
7 
8 #ifndef __NET_TI_ICSSG_PRUETH_H
9 #define __NET_TI_ICSSG_PRUETH_H
10 
11 #include <linux/etherdevice.h>
12 #include <linux/genalloc.h>
13 #include <linux/if_vlan.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/net_tstamp.h>
20 #include <linux/of.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_mdio.h>
23 #include <linux/of_net.h>
24 #include <linux/of_platform.h>
25 #include <linux/phy.h>
26 #include <linux/remoteproc/pruss.h>
27 #include <linux/pruss_driver.h>
28 #include <linux/ptp_clock_kernel.h>
29 #include <linux/remoteproc.h>
30 
31 #include <linux/dma-mapping.h>
32 #include <linux/dma/ti-cppi5.h>
33 #include <linux/dma/k3-udma-glue.h>
34 
35 #include <net/devlink.h>
36 
37 #include "icssg_config.h"
38 #include "icss_iep.h"
39 #include "icssg_switch_map.h"
40 
41 #define PRUETH_MAX_MTU          (2000 - ETH_HLEN - ETH_FCS_LEN)
42 #define PRUETH_MIN_PKT_SIZE     (VLAN_ETH_ZLEN)
43 #define PRUETH_MAX_PKT_SIZE     (PRUETH_MAX_MTU + ETH_HLEN + ETH_FCS_LEN)
44 
45 #define ICSS_SLICE0	0
46 #define ICSS_SLICE1	1
47 
48 #define ICSS_FW_PRU	0
49 #define ICSS_FW_RTU	1
50 
51 #define ICSSG_MAX_RFLOWS	8	/* per slice */
52 
53 /* Number of ICSSG related stats */
54 #define ICSSG_NUM_STATS 60
55 #define ICSSG_NUM_STANDARD_STATS 31
56 #define ICSSG_NUM_ETHTOOL_STATS (ICSSG_NUM_STATS - ICSSG_NUM_STANDARD_STATS)
57 
58 #define IEP_DEFAULT_CYCLE_TIME_NS	1000000	/* 1 ms */
59 
60 /* Firmware status codes */
61 #define ICSS_HS_FW_READY 0x55555555
62 #define ICSS_HS_FW_DEAD 0xDEAD0000	/* lower 16 bits contain error code */
63 
64 /* Firmware command codes */
65 #define ICSS_HS_CMD_BUSY 0x40000000
66 #define ICSS_HS_CMD_DONE 0x80000000
67 #define ICSS_HS_CMD_CANCEL 0x10000000
68 
69 /* Firmware commands */
70 #define ICSS_CMD_SPAD 0x20
71 #define ICSS_CMD_RXTX 0x10
72 #define ICSS_CMD_ADD_FDB 0x1
73 #define ICSS_CMD_DEL_FDB 0x2
74 #define ICSS_CMD_SET_RUN 0x4
75 #define ICSS_CMD_GET_FDB_SLOT 0x5
76 #define ICSS_CMD_ENABLE_VLAN 0x5
77 #define ICSS_CMD_DISABLE_VLAN 0x6
78 #define ICSS_CMD_ADD_FILTER 0x7
79 #define ICSS_CMD_ADD_MAC 0x8
80 
81 /* In switch mode there are 3 real ports i.e. 3 mac addrs.
82  * however Linux sees only the host side port. The other 2 ports
83  * are the switch ports.
84  * In emac mode there are 2 real ports i.e. 2 mac addrs.
85  * Linux sees both the ports.
86  */
87 enum prueth_port {
88 	PRUETH_PORT_HOST = 0,	/* host side port */
89 	PRUETH_PORT_MII0,	/* physical port RG/SG MII 0 */
90 	PRUETH_PORT_MII1,	/* physical port RG/SG MII 1 */
91 	PRUETH_PORT_INVALID,	/* Invalid prueth port */
92 };
93 
94 enum prueth_mac {
95 	PRUETH_MAC0 = 0,
96 	PRUETH_MAC1,
97 	PRUETH_NUM_MACS,
98 	PRUETH_MAC_INVALID,
99 };
100 
101 struct prueth_tx_chn {
102 	struct device *dma_dev;
103 	struct napi_struct napi_tx;
104 	struct k3_cppi_desc_pool *desc_pool;
105 	struct k3_udma_glue_tx_channel *tx_chn;
106 	struct prueth_emac *emac;
107 	u32 id;
108 	u32 descs_num;
109 	unsigned int irq;
110 	char name[32];
111 };
112 
113 struct prueth_rx_chn {
114 	struct device *dev;
115 	struct device *dma_dev;
116 	struct k3_cppi_desc_pool *desc_pool;
117 	struct k3_udma_glue_rx_channel *rx_chn;
118 	u32 descs_num;
119 	unsigned int irq[ICSSG_MAX_RFLOWS];	/* separate irq per flow */
120 	char name[32];
121 };
122 
123 /* There are 4 Tx DMA channels, but the highest priority is CH3 (thread 3)
124  * and lower three are lower priority channels or threads.
125  */
126 #define PRUETH_MAX_TX_QUEUES	4
127 
128 #define PRUETH_MAX_TX_TS_REQUESTS	50 /* Max simultaneous TX_TS requests */
129 
130 /* data for each emac port */
131 struct prueth_emac {
132 	bool is_sr1;
133 	bool fw_running;
134 	struct prueth *prueth;
135 	struct net_device *ndev;
136 	u8 mac_addr[6];
137 	struct napi_struct napi_rx;
138 	u32 msg_enable;
139 
140 	int link;
141 	int speed;
142 	int duplex;
143 
144 	const char *phy_id;
145 	struct device_node *phy_node;
146 	phy_interface_t phy_if;
147 	enum prueth_port port_id;
148 	struct icss_iep *iep;
149 	unsigned int rx_ts_enabled : 1;
150 	unsigned int tx_ts_enabled : 1;
151 	unsigned int half_duplex : 1;
152 
153 	/* DMA related */
154 	struct prueth_tx_chn tx_chns[PRUETH_MAX_TX_QUEUES];
155 	struct completion tdown_complete;
156 	atomic_t tdown_cnt;
157 	struct prueth_rx_chn rx_chns;
158 	int rx_flow_id_base;
159 	int tx_ch_num;
160 
161 	/* SR1.0 Management channel */
162 	struct prueth_rx_chn rx_mgm_chn;
163 	int rx_mgm_flow_id_base;
164 
165 	spinlock_t lock;	/* serialize access */
166 
167 	/* TX HW Timestamping */
168 	/* TX TS cookie will be index to the tx_ts_skb array */
169 	struct sk_buff *tx_ts_skb[PRUETH_MAX_TX_TS_REQUESTS];
170 	atomic_t tx_ts_pending;
171 	int tx_ts_irq;
172 
173 	u8 cmd_seq;
174 	/* shutdown related */
175 	__le32 cmd_data[4];
176 	struct completion cmd_complete;
177 	/* Mutex to serialize access to firmware command interface */
178 	struct mutex cmd_lock;
179 	struct work_struct rx_mode_work;
180 	struct workqueue_struct	*cmd_wq;
181 
182 	struct pruss_mem_region dram;
183 
184 	struct delayed_work stats_work;
185 	u64 stats[ICSSG_NUM_STATS];
186 };
187 
188 /**
189  * struct prueth_pdata - PRUeth platform data
190  * @fdqring_mode: Free desc queue mode
191  * @quirk_10m_link_issue: 10M link detect errata
192  */
193 struct prueth_pdata {
194 	enum k3_ring_mode fdqring_mode;
195 	u32	quirk_10m_link_issue:1;
196 };
197 
198 struct icssg_firmwares {
199 	char *pru;
200 	char *rtu;
201 	char *txpru;
202 };
203 
204 /**
205  * struct prueth - PRUeth structure
206  * @dev: device
207  * @pruss: pruss handle
208  * @pru: rproc instances of PRUs
209  * @rtu: rproc instances of RTUs
210  * @txpru: rproc instances of TX_PRUs
211  * @shram: PRUSS shared RAM region
212  * @sram_pool: MSMC RAM pool for buffers
213  * @msmcram: MSMC RAM region
214  * @eth_node: DT node for the port
215  * @emac: private EMAC data structure
216  * @registered_netdevs: list of registered netdevs
217  * @miig_rt: regmap to mii_g_rt block
218  * @mii_rt: regmap to mii_rt block
219  * @pru_id: ID for each of the PRUs
220  * @pdev: pointer to ICSSG platform device
221  * @pdata: pointer to platform data for ICSSG driver
222  * @icssg_hwcmdseq: seq counter or HWQ messages
223  * @emacs_initialized: num of EMACs/ext ports that are up/running
224  * @iep0: pointer to IEP0 device
225  * @iep1: pointer to IEP1 device
226  */
227 struct prueth {
228 	struct device *dev;
229 	struct pruss *pruss;
230 	struct rproc *pru[PRUSS_NUM_PRUS];
231 	struct rproc *rtu[PRUSS_NUM_PRUS];
232 	struct rproc *txpru[PRUSS_NUM_PRUS];
233 	struct pruss_mem_region shram;
234 	struct gen_pool *sram_pool;
235 	struct pruss_mem_region msmcram;
236 
237 	struct device_node *eth_node[PRUETH_NUM_MACS];
238 	struct prueth_emac *emac[PRUETH_NUM_MACS];
239 	struct net_device *registered_netdevs[PRUETH_NUM_MACS];
240 	struct regmap *miig_rt;
241 	struct regmap *mii_rt;
242 
243 	enum pruss_pru_id pru_id[PRUSS_NUM_PRUS];
244 	struct platform_device *pdev;
245 	struct prueth_pdata pdata;
246 	u8 icssg_hwcmdseq;
247 	int emacs_initialized;
248 	struct icss_iep *iep0;
249 	struct icss_iep *iep1;
250 };
251 
252 struct emac_tx_ts_response {
253 	u32 reserved[2];
254 	u32 cookie;
255 	u32 lo_ts;
256 	u32 hi_ts;
257 };
258 
259 struct emac_tx_ts_response_sr1 {
260 	__le32 lo_ts;
261 	__le32 hi_ts;
262 	__le32 reserved;
263 	__le32 cookie;
264 };
265 
266 /* get PRUSS SLICE number from prueth_emac */
267 static inline int prueth_emac_slice(struct prueth_emac *emac)
268 {
269 	switch (emac->port_id) {
270 	case PRUETH_PORT_MII0:
271 		return ICSS_SLICE0;
272 	case PRUETH_PORT_MII1:
273 		return ICSS_SLICE1;
274 	default:
275 		return -EINVAL;
276 	}
277 }
278 
279 extern const struct ethtool_ops icssg_ethtool_ops;
280 extern const struct dev_pm_ops prueth_dev_pm_ops;
281 
282 /* Classifier helpers */
283 void icssg_class_set_mac_addr(struct regmap *miig_rt, int slice, u8 *mac);
284 void icssg_class_set_host_mac_addr(struct regmap *miig_rt, const u8 *mac);
285 void icssg_class_disable(struct regmap *miig_rt, int slice);
286 void icssg_class_default(struct regmap *miig_rt, int slice, bool allmulti,
287 			 bool is_sr1);
288 void icssg_class_promiscuous_sr1(struct regmap *miig_rt, int slice);
289 void icssg_class_add_mcast_sr1(struct regmap *miig_rt, int slice,
290 			       struct net_device *ndev);
291 void icssg_ft1_set_mac_addr(struct regmap *miig_rt, int slice, u8 *mac_addr);
292 
293 /* config helpers */
294 void icssg_config_ipg(struct prueth_emac *emac);
295 int icssg_config(struct prueth *prueth, struct prueth_emac *emac,
296 		 int slice);
297 int emac_set_port_state(struct prueth_emac *emac,
298 			enum icssg_port_state_cmd state);
299 void icssg_config_set_speed(struct prueth_emac *emac);
300 void icssg_config_half_duplex(struct prueth_emac *emac);
301 
302 /* Buffer queue helpers */
303 int icssg_queue_pop(struct prueth *prueth, u8 queue);
304 void icssg_queue_push(struct prueth *prueth, int queue, u16 addr);
305 u32 icssg_queue_level(struct prueth *prueth, int queue);
306 
307 #define prueth_napi_to_tx_chn(pnapi) \
308 	container_of(pnapi, struct prueth_tx_chn, napi_tx)
309 
310 void emac_stats_work_handler(struct work_struct *work);
311 void emac_update_hardware_stats(struct prueth_emac *emac);
312 int emac_get_stat_by_name(struct prueth_emac *emac, char *stat_name);
313 
314 /* Common functions */
315 void prueth_cleanup_rx_chns(struct prueth_emac *emac,
316 			    struct prueth_rx_chn *rx_chn,
317 			    int max_rflows);
318 void prueth_cleanup_tx_chns(struct prueth_emac *emac);
319 void prueth_ndev_del_tx_napi(struct prueth_emac *emac, int num);
320 void prueth_xmit_free(struct prueth_tx_chn *tx_chn,
321 		      struct cppi5_host_desc_t *desc);
322 int emac_tx_complete_packets(struct prueth_emac *emac, int chn,
323 			     int budget);
324 int prueth_ndev_add_tx_napi(struct prueth_emac *emac);
325 int prueth_init_tx_chns(struct prueth_emac *emac);
326 int prueth_init_rx_chns(struct prueth_emac *emac,
327 			struct prueth_rx_chn *rx_chn,
328 			char *name, u32 max_rflows,
329 			u32 max_desc_num);
330 int prueth_dma_rx_push(struct prueth_emac *emac,
331 		       struct sk_buff *skb,
332 		       struct prueth_rx_chn *rx_chn);
333 void emac_rx_timestamp(struct prueth_emac *emac,
334 		       struct sk_buff *skb, u32 *psdata);
335 enum netdev_tx emac_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev);
336 irqreturn_t prueth_rx_irq(int irq, void *dev_id);
337 void prueth_emac_stop(struct prueth_emac *emac);
338 void prueth_cleanup_tx_ts(struct prueth_emac *emac);
339 int emac_napi_rx_poll(struct napi_struct *napi_rx, int budget);
340 int prueth_prepare_rx_chan(struct prueth_emac *emac,
341 			   struct prueth_rx_chn *chn,
342 			   int buf_size);
343 void prueth_reset_tx_chan(struct prueth_emac *emac, int ch_num,
344 			  bool free_skb);
345 void prueth_reset_rx_chan(struct prueth_rx_chn *chn,
346 			  int num_flows, bool disable);
347 void emac_ndo_tx_timeout(struct net_device *ndev, unsigned int txqueue);
348 int emac_ndo_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd);
349 void emac_ndo_get_stats64(struct net_device *ndev,
350 			  struct rtnl_link_stats64 *stats);
351 int emac_ndo_get_phys_port_name(struct net_device *ndev, char *name,
352 				size_t len);
353 int prueth_node_port(struct device_node *eth_node);
354 int prueth_node_mac(struct device_node *eth_node);
355 void prueth_netdev_exit(struct prueth *prueth,
356 			struct device_node *eth_node);
357 int prueth_get_cores(struct prueth *prueth, int slice, bool is_sr1);
358 void prueth_put_cores(struct prueth *prueth, int slice);
359 
360 /* Revision specific helper */
361 u64 icssg_ts_to_ns(u32 hi_sw, u32 hi, u32 lo, u32 cycle_time_ns);
362 
363 #endif /* __NET_TI_ICSSG_PRUETH_H */
364