xref: /linux/drivers/net/ethernet/ti/icssg/icssg_prueth.h (revision 04508d20b017326e116c6e8ef953839507c73b6d)
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 #define ICSSG_NUM_PA_STATS	4
54 #define ICSSG_NUM_MIIG_STATS	60
55 /* Number of ICSSG related stats */
56 #define ICSSG_NUM_STATS (ICSSG_NUM_MIIG_STATS + ICSSG_NUM_PA_STATS)
57 #define ICSSG_NUM_STANDARD_STATS 31
58 #define ICSSG_NUM_ETHTOOL_STATS (ICSSG_NUM_STATS - ICSSG_NUM_STANDARD_STATS)
59 
60 #define IEP_DEFAULT_CYCLE_TIME_NS	1000000	/* 1 ms */
61 
62 #define PRUETH_UNDIRECTED_PKT_DST_TAG	0
63 #define PRUETH_UNDIRECTED_PKT_TAG_INS	BIT(30)
64 
65 /* Firmware status codes */
66 #define ICSS_HS_FW_READY 0x55555555
67 #define ICSS_HS_FW_DEAD 0xDEAD0000	/* lower 16 bits contain error code */
68 
69 /* Firmware command codes */
70 #define ICSS_HS_CMD_BUSY 0x40000000
71 #define ICSS_HS_CMD_DONE 0x80000000
72 #define ICSS_HS_CMD_CANCEL 0x10000000
73 
74 /* Firmware commands */
75 #define ICSS_CMD_SPAD 0x20
76 #define ICSS_CMD_RXTX 0x10
77 #define ICSS_CMD_ADD_FDB 0x1
78 #define ICSS_CMD_DEL_FDB 0x2
79 #define ICSS_CMD_SET_RUN 0x4
80 #define ICSS_CMD_GET_FDB_SLOT 0x5
81 #define ICSS_CMD_ENABLE_VLAN 0x5
82 #define ICSS_CMD_DISABLE_VLAN 0x6
83 #define ICSS_CMD_ADD_FILTER 0x7
84 #define ICSS_CMD_ADD_MAC 0x8
85 
86 /* VLAN Filtering Related MACROs */
87 #define PRUETH_DFLT_VLAN_MAC	0
88 #define MAX_VLAN_ID		256
89 
90 /* In switch mode there are 3 real ports i.e. 3 mac addrs.
91  * however Linux sees only the host side port. The other 2 ports
92  * are the switch ports.
93  * In emac mode there are 2 real ports i.e. 2 mac addrs.
94  * Linux sees both the ports.
95  */
96 enum prueth_port {
97 	PRUETH_PORT_HOST = 0,	/* host side port */
98 	PRUETH_PORT_MII0,	/* physical port RG/SG MII 0 */
99 	PRUETH_PORT_MII1,	/* physical port RG/SG MII 1 */
100 	PRUETH_PORT_INVALID,	/* Invalid prueth port */
101 };
102 
103 enum prueth_mac {
104 	PRUETH_MAC0 = 0,
105 	PRUETH_MAC1,
106 	PRUETH_NUM_MACS,
107 	PRUETH_MAC_INVALID,
108 };
109 
110 struct prueth_tx_chn {
111 	struct device *dma_dev;
112 	struct napi_struct napi_tx;
113 	struct k3_cppi_desc_pool *desc_pool;
114 	struct k3_udma_glue_tx_channel *tx_chn;
115 	struct prueth_emac *emac;
116 	u32 id;
117 	u32 descs_num;
118 	unsigned int irq;
119 	char name[32];
120 	struct hrtimer tx_hrtimer;
121 	unsigned long tx_pace_timeout_ns;
122 };
123 
124 struct prueth_rx_chn {
125 	struct device *dev;
126 	struct device *dma_dev;
127 	struct k3_cppi_desc_pool *desc_pool;
128 	struct k3_udma_glue_rx_channel *rx_chn;
129 	u32 descs_num;
130 	unsigned int irq[ICSSG_MAX_RFLOWS];	/* separate irq per flow */
131 	char name[32];
132 };
133 
134 /* There are 4 Tx DMA channels, but the highest priority is CH3 (thread 3)
135  * and lower three are lower priority channels or threads.
136  */
137 #define PRUETH_MAX_TX_QUEUES	4
138 
139 #define PRUETH_MAX_TX_TS_REQUESTS	50 /* Max simultaneous TX_TS requests */
140 
141 /* Minimum coalesce time in usecs for both Tx and Rx */
142 #define ICSSG_MIN_COALESCE_USECS 20
143 
144 /* data for each emac port */
145 struct prueth_emac {
146 	bool is_sr1;
147 	struct prueth *prueth;
148 	struct net_device *ndev;
149 	u8 mac_addr[6];
150 	struct napi_struct napi_rx;
151 	u32 msg_enable;
152 
153 	int link;
154 	int speed;
155 	int duplex;
156 
157 	const char *phy_id;
158 	struct device_node *phy_node;
159 	phy_interface_t phy_if;
160 	enum prueth_port port_id;
161 	struct icss_iep *iep;
162 	unsigned int rx_ts_enabled : 1;
163 	unsigned int tx_ts_enabled : 1;
164 	unsigned int half_duplex : 1;
165 
166 	/* DMA related */
167 	struct prueth_tx_chn tx_chns[PRUETH_MAX_TX_QUEUES];
168 	struct completion tdown_complete;
169 	atomic_t tdown_cnt;
170 	struct prueth_rx_chn rx_chns;
171 	int rx_flow_id_base;
172 	int tx_ch_num;
173 
174 	/* SR1.0 Management channel */
175 	struct prueth_rx_chn rx_mgm_chn;
176 	int rx_mgm_flow_id_base;
177 
178 	spinlock_t lock;	/* serialize access */
179 
180 	/* TX HW Timestamping */
181 	/* TX TS cookie will be index to the tx_ts_skb array */
182 	struct sk_buff *tx_ts_skb[PRUETH_MAX_TX_TS_REQUESTS];
183 	atomic_t tx_ts_pending;
184 	int tx_ts_irq;
185 
186 	u8 cmd_seq;
187 	/* shutdown related */
188 	__le32 cmd_data[4];
189 	struct completion cmd_complete;
190 	/* Mutex to serialize access to firmware command interface */
191 	struct mutex cmd_lock;
192 	struct work_struct rx_mode_work;
193 	struct workqueue_struct	*cmd_wq;
194 
195 	struct pruss_mem_region dram;
196 
197 	bool offload_fwd_mark;
198 	int port_vlan;
199 
200 	struct delayed_work stats_work;
201 	u64 stats[ICSSG_NUM_MIIG_STATS];
202 	u64 pa_stats[ICSSG_NUM_PA_STATS];
203 
204 	/* RX IRQ Coalescing Related */
205 	struct hrtimer rx_hrtimer;
206 	unsigned long rx_pace_timeout_ns;
207 
208 	struct netdev_hw_addr_list vlan_mcast_list[MAX_VLAN_ID];
209 };
210 
211 /**
212  * struct prueth_pdata - PRUeth platform data
213  * @fdqring_mode: Free desc queue mode
214  * @quirk_10m_link_issue: 10M link detect errata
215  * @switch_mode: switch firmware support
216  */
217 struct prueth_pdata {
218 	enum k3_ring_mode fdqring_mode;
219 	u32	quirk_10m_link_issue:1;
220 	u32	switch_mode:1;
221 };
222 
223 struct icssg_firmwares {
224 	char *pru;
225 	char *rtu;
226 	char *txpru;
227 };
228 
229 /**
230  * struct prueth - PRUeth structure
231  * @dev: device
232  * @pruss: pruss handle
233  * @pru: rproc instances of PRUs
234  * @rtu: rproc instances of RTUs
235  * @txpru: rproc instances of TX_PRUs
236  * @shram: PRUSS shared RAM region
237  * @sram_pool: MSMC RAM pool for buffers
238  * @msmcram: MSMC RAM region
239  * @eth_node: DT node for the port
240  * @emac: private EMAC data structure
241  * @registered_netdevs: list of registered netdevs
242  * @miig_rt: regmap to mii_g_rt block
243  * @mii_rt: regmap to mii_rt block
244  * @pa_stats: regmap to pa_stats block
245  * @pru_id: ID for each of the PRUs
246  * @pdev: pointer to ICSSG platform device
247  * @pdata: pointer to platform data for ICSSG driver
248  * @icssg_hwcmdseq: seq counter or HWQ messages
249  * @emacs_initialized: num of EMACs/ext ports that are up/running
250  * @iep0: pointer to IEP0 device
251  * @iep1: pointer to IEP1 device
252  * @vlan_tbl: VLAN-FID table pointer
253  * @hw_bridge_dev: pointer to HW bridge net device
254  * @hsr_dev: pointer to the HSR net device
255  * @br_members: bitmask of bridge member ports
256  * @hsr_members: bitmask of hsr member ports
257  * @prueth_netdevice_nb: netdevice notifier block
258  * @prueth_switchdev_nb: switchdev notifier block
259  * @prueth_switchdev_bl_nb: switchdev blocking notifier block
260  * @is_switch_mode: flag to indicate if device is in Switch mode
261  * @is_hsr_offload_mode: flag to indicate if device is in hsr offload mode
262  * @is_switchmode_supported: indicates platform support for switch mode
263  * @switch_id: ID for mapping switch ports to bridge
264  * @default_vlan: Default VLAN for host
265  */
266 struct prueth {
267 	struct device *dev;
268 	struct pruss *pruss;
269 	struct rproc *pru[PRUSS_NUM_PRUS];
270 	struct rproc *rtu[PRUSS_NUM_PRUS];
271 	struct rproc *txpru[PRUSS_NUM_PRUS];
272 	struct pruss_mem_region shram;
273 	struct gen_pool *sram_pool;
274 	struct pruss_mem_region msmcram;
275 
276 	struct device_node *eth_node[PRUETH_NUM_MACS];
277 	struct prueth_emac *emac[PRUETH_NUM_MACS];
278 	struct net_device *registered_netdevs[PRUETH_NUM_MACS];
279 	struct regmap *miig_rt;
280 	struct regmap *mii_rt;
281 	struct regmap *pa_stats;
282 
283 	enum pruss_pru_id pru_id[PRUSS_NUM_PRUS];
284 	struct platform_device *pdev;
285 	struct prueth_pdata pdata;
286 	u8 icssg_hwcmdseq;
287 	int emacs_initialized;
288 	struct icss_iep *iep0;
289 	struct icss_iep *iep1;
290 	struct prueth_vlan_tbl *vlan_tbl;
291 
292 	struct net_device *hw_bridge_dev;
293 	struct net_device *hsr_dev;
294 	u8 br_members;
295 	u8 hsr_members;
296 	struct notifier_block prueth_netdevice_nb;
297 	struct notifier_block prueth_switchdev_nb;
298 	struct notifier_block prueth_switchdev_bl_nb;
299 	bool is_switch_mode;
300 	bool is_hsr_offload_mode;
301 	bool is_switchmode_supported;
302 	unsigned char switch_id[MAX_PHYS_ITEM_ID_LEN];
303 	int default_vlan;
304 	/** @vtbl_lock: Lock for vtbl in shared memory */
305 	spinlock_t vtbl_lock;
306 };
307 
308 struct emac_tx_ts_response {
309 	u32 reserved[2];
310 	u32 cookie;
311 	u32 lo_ts;
312 	u32 hi_ts;
313 };
314 
315 struct emac_tx_ts_response_sr1 {
316 	__le32 lo_ts;
317 	__le32 hi_ts;
318 	__le32 reserved;
319 	__le32 cookie;
320 };
321 
322 /* get PRUSS SLICE number from prueth_emac */
323 static inline int prueth_emac_slice(struct prueth_emac *emac)
324 {
325 	switch (emac->port_id) {
326 	case PRUETH_PORT_MII0:
327 		return ICSS_SLICE0;
328 	case PRUETH_PORT_MII1:
329 		return ICSS_SLICE1;
330 	default:
331 		return -EINVAL;
332 	}
333 }
334 
335 extern const struct ethtool_ops icssg_ethtool_ops;
336 extern const struct dev_pm_ops prueth_dev_pm_ops;
337 
338 static inline u64 icssg_read_time(const void __iomem *addr)
339 {
340 	u32 low, high;
341 
342 	do {
343 		high = readl(addr + 4);
344 		low = readl(addr);
345 	} while (high != readl(addr + 4));
346 
347 	return low + ((u64)high << 32);
348 }
349 
350 /* Classifier helpers */
351 void icssg_class_set_mac_addr(struct regmap *miig_rt, int slice, u8 *mac);
352 void icssg_class_set_host_mac_addr(struct regmap *miig_rt, const u8 *mac);
353 void icssg_class_disable(struct regmap *miig_rt, int slice);
354 void icssg_class_default(struct regmap *miig_rt, int slice, bool allmulti,
355 			 bool is_sr1);
356 void icssg_class_promiscuous_sr1(struct regmap *miig_rt, int slice);
357 void icssg_class_add_mcast_sr1(struct regmap *miig_rt, int slice,
358 			       struct net_device *ndev);
359 void icssg_ft1_set_mac_addr(struct regmap *miig_rt, int slice, u8 *mac_addr);
360 
361 /* config helpers */
362 void icssg_config_ipg(struct prueth_emac *emac);
363 int icssg_config(struct prueth *prueth, struct prueth_emac *emac,
364 		 int slice);
365 int icssg_set_port_state(struct prueth_emac *emac,
366 			 enum icssg_port_state_cmd state);
367 void icssg_config_set_speed(struct prueth_emac *emac);
368 void icssg_config_half_duplex(struct prueth_emac *emac);
369 void icssg_init_emac_mode(struct prueth *prueth);
370 void icssg_init_fw_offload_mode(struct prueth *prueth);
371 
372 /* Buffer queue helpers */
373 int icssg_queue_pop(struct prueth *prueth, u8 queue);
374 void icssg_queue_push(struct prueth *prueth, int queue, u16 addr);
375 u32 icssg_queue_level(struct prueth *prueth, int queue);
376 
377 int icssg_send_fdb_msg(struct prueth_emac *emac, struct mgmt_cmd *cmd,
378 		       struct mgmt_cmd_rsp *rsp);
379 int icssg_fdb_add_del(struct prueth_emac *emac,  const unsigned char *addr,
380 		      u8 vid, u8 fid_c2, bool add);
381 int icssg_fdb_lookup(struct prueth_emac *emac, const unsigned char *addr,
382 		     u8 vid);
383 void icssg_vtbl_modify(struct prueth_emac *emac, u8 vid, u8 port_mask,
384 		       u8 untag_mask, bool add);
385 u16 icssg_get_pvid(struct prueth_emac *emac);
386 void icssg_set_pvid(struct prueth *prueth, u8 vid, u8 port);
387 int emac_fdb_flow_id_updated(struct prueth_emac *emac);
388 #define prueth_napi_to_tx_chn(pnapi) \
389 	container_of(pnapi, struct prueth_tx_chn, napi_tx)
390 
391 void icssg_stats_work_handler(struct work_struct *work);
392 void emac_update_hardware_stats(struct prueth_emac *emac);
393 int emac_get_stat_by_name(struct prueth_emac *emac, char *stat_name);
394 
395 /* Common functions */
396 void prueth_cleanup_rx_chns(struct prueth_emac *emac,
397 			    struct prueth_rx_chn *rx_chn,
398 			    int max_rflows);
399 void prueth_cleanup_tx_chns(struct prueth_emac *emac);
400 void prueth_ndev_del_tx_napi(struct prueth_emac *emac, int num);
401 void prueth_xmit_free(struct prueth_tx_chn *tx_chn,
402 		      struct cppi5_host_desc_t *desc);
403 int emac_tx_complete_packets(struct prueth_emac *emac, int chn,
404 			     int budget, bool *tdown);
405 int prueth_ndev_add_tx_napi(struct prueth_emac *emac);
406 int prueth_init_tx_chns(struct prueth_emac *emac);
407 int prueth_init_rx_chns(struct prueth_emac *emac,
408 			struct prueth_rx_chn *rx_chn,
409 			char *name, u32 max_rflows,
410 			u32 max_desc_num);
411 int prueth_dma_rx_push(struct prueth_emac *emac,
412 		       struct sk_buff *skb,
413 		       struct prueth_rx_chn *rx_chn);
414 void emac_rx_timestamp(struct prueth_emac *emac,
415 		       struct sk_buff *skb, u32 *psdata);
416 enum netdev_tx icssg_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev);
417 irqreturn_t prueth_rx_irq(int irq, void *dev_id);
418 void prueth_cleanup_tx_ts(struct prueth_emac *emac);
419 int icssg_napi_rx_poll(struct napi_struct *napi_rx, int budget);
420 int prueth_prepare_rx_chan(struct prueth_emac *emac,
421 			   struct prueth_rx_chn *chn,
422 			   int buf_size);
423 void prueth_reset_tx_chan(struct prueth_emac *emac, int ch_num,
424 			  bool free_skb);
425 void prueth_reset_rx_chan(struct prueth_rx_chn *chn,
426 			  int num_flows, bool disable);
427 void icssg_ndo_tx_timeout(struct net_device *ndev, unsigned int txqueue);
428 int icssg_ndo_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd);
429 void icssg_ndo_get_stats64(struct net_device *ndev,
430 			   struct rtnl_link_stats64 *stats);
431 int icssg_ndo_get_phys_port_name(struct net_device *ndev, char *name,
432 				 size_t len);
433 int prueth_node_port(struct device_node *eth_node);
434 int prueth_node_mac(struct device_node *eth_node);
435 void prueth_netdev_exit(struct prueth *prueth,
436 			struct device_node *eth_node);
437 int prueth_get_cores(struct prueth *prueth, int slice, bool is_sr1);
438 void prueth_put_cores(struct prueth *prueth, int slice);
439 
440 /* Revision specific helper */
441 u64 icssg_ts_to_ns(u32 hi_sw, u32 hi, u32 lo, u32 cycle_time_ns);
442 
443 #endif /* __NET_TI_ICSSG_PRUETH_H */
444