xref: /linux/drivers/net/ethernet/ti/netcp.h (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1 /*
2  * NetCP driver local header
3  *
4  * Copyright (C) 2014 Texas Instruments Incorporated
5  * Authors:	Sandeep Nair <sandeep_n@ti.com>
6  *		Sandeep Paulraj <s-paulraj@ti.com>
7  *		Cyril Chemparathy <cyril@ti.com>
8  *		Santosh Shilimkar <santosh.shilimkar@ti.com>
9  *		Wingman Kwok <w-kwok2@ti.com>
10  *		Murali Karicheri <m-karicheri2@ti.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation version 2.
15  *
16  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
17  * kind, whether express or implied; without even the implied warranty
18  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  */
21 #ifndef __NETCP_H__
22 #define __NETCP_H__
23 
24 #include <linux/netdevice.h>
25 #include <linux/soc/ti/knav_dma.h>
26 #include <linux/u64_stats_sync.h>
27 
28 /* Maximum Ethernet frame size supported by Keystone switch */
29 #define NETCP_MAX_FRAME_SIZE		9504
30 
31 #define SGMII_LINK_MAC_MAC_AUTONEG	0
32 #define SGMII_LINK_MAC_PHY		1
33 #define SGMII_LINK_MAC_MAC_FORCED	2
34 #define SGMII_LINK_MAC_FIBER		3
35 #define SGMII_LINK_MAC_PHY_NO_MDIO	4
36 #define RGMII_LINK_MAC_PHY		5
37 #define RGMII_LINK_MAC_PHY_NO_MDIO	7
38 #define XGMII_LINK_MAC_PHY		10
39 #define XGMII_LINK_MAC_MAC_FORCED	11
40 
41 struct netcp_device;
42 
43 struct netcp_tx_pipe {
44 	struct netcp_device	*netcp_device;
45 	void			*dma_queue;
46 	unsigned int		dma_queue_id;
47 	/* To port for packet forwarded to switch. Used only by ethss */
48 	u8			switch_to_port;
49 #define	SWITCH_TO_PORT_IN_TAGINFO	BIT(0)
50 	u8			flags;
51 	void			*dma_channel;
52 	const char		*dma_chan_name;
53 };
54 
55 #define ADDR_NEW			BIT(0)
56 #define ADDR_VALID			BIT(1)
57 
58 enum netcp_addr_type {
59 	ADDR_ANY,
60 	ADDR_DEV,
61 	ADDR_UCAST,
62 	ADDR_MCAST,
63 	ADDR_BCAST
64 };
65 
66 struct netcp_addr {
67 	struct netcp_intf	*netcp;
68 	unsigned char		addr[ETH_ALEN];
69 	enum netcp_addr_type	type;
70 	unsigned int		flags;
71 	struct list_head	node;
72 };
73 
74 struct netcp_stats {
75 	struct u64_stats_sync   syncp_rx ____cacheline_aligned_in_smp;
76 	u64                     rx_packets;
77 	u64                     rx_bytes;
78 	u32                     rx_errors;
79 	u32                     rx_dropped;
80 
81 	struct u64_stats_sync   syncp_tx ____cacheline_aligned_in_smp;
82 	u64                     tx_packets;
83 	u64                     tx_bytes;
84 	u32                     tx_errors;
85 	u32                     tx_dropped;
86 };
87 
88 struct netcp_intf {
89 	struct device		*dev;
90 	struct device		*ndev_dev;
91 	struct net_device	*ndev;
92 	bool			big_endian;
93 	unsigned int		tx_compl_qid;
94 	void			*tx_pool;
95 	struct list_head	txhook_list_head;
96 	unsigned int		tx_pause_threshold;
97 	void			*tx_compl_q;
98 
99 	unsigned int		tx_resume_threshold;
100 	void			*rx_queue;
101 	void			*rx_pool;
102 	struct list_head	rxhook_list_head;
103 	unsigned int		rx_queue_id;
104 	void			*rx_fdq[KNAV_DMA_FDQ_PER_CHAN];
105 	struct napi_struct	rx_napi;
106 	struct napi_struct	tx_napi;
107 #define ETH_SW_CAN_REMOVE_ETH_FCS	BIT(0)
108 	u32			hw_cap;
109 
110 	/* 64-bit netcp stats */
111 	struct netcp_stats	stats;
112 
113 	void			*rx_channel;
114 	const char		*dma_chan_name;
115 	u32			rx_pool_size;
116 	u32			rx_pool_region_id;
117 	u32			tx_pool_size;
118 	u32			tx_pool_region_id;
119 	struct list_head	module_head;
120 	struct list_head	interface_list;
121 	struct list_head	addr_list;
122 	bool			netdev_registered;
123 	bool			primary_module_attached;
124 
125 	/* Lock used for protecting Rx/Tx hook list management */
126 	spinlock_t		lock;
127 	struct netcp_device	*netcp_device;
128 	struct device_node	*node_interface;
129 
130 	/* DMA configuration data */
131 	u32			msg_enable;
132 	u32			rx_queue_depths[KNAV_DMA_FDQ_PER_CHAN];
133 };
134 
135 #define	NETCP_PSDATA_LEN		KNAV_DMA_NUM_PS_WORDS
136 struct netcp_packet {
137 	struct sk_buff		*skb;
138 	__le32			*epib;
139 	u32			*psdata;
140 	u32			eflags;
141 	unsigned int		psdata_len;
142 	struct netcp_intf	*netcp;
143 	struct netcp_tx_pipe	*tx_pipe;
144 	bool			rxtstamp_complete;
145 	void			*ts_context;
146 
147 	void (*txtstamp)(void *ctx, struct sk_buff *skb);
148 };
149 
150 static inline u32 *netcp_push_psdata(struct netcp_packet *p_info,
151 				     unsigned int bytes)
152 {
153 	u32 *buf;
154 	unsigned int words;
155 
156 	if ((bytes & 0x03) != 0)
157 		return NULL;
158 	words = bytes >> 2;
159 
160 	if ((p_info->psdata_len + words) > NETCP_PSDATA_LEN)
161 		return NULL;
162 
163 	p_info->psdata_len += words;
164 	buf = &p_info->psdata[NETCP_PSDATA_LEN - p_info->psdata_len];
165 	return buf;
166 }
167 
168 static inline int netcp_align_psdata(struct netcp_packet *p_info,
169 				     unsigned int byte_align)
170 {
171 	int padding;
172 
173 	switch (byte_align) {
174 	case 0:
175 		padding = -EINVAL;
176 		break;
177 	case 1:
178 	case 2:
179 	case 4:
180 		padding = 0;
181 		break;
182 	case 8:
183 		padding = (p_info->psdata_len << 2) % 8;
184 		break;
185 	case 16:
186 		padding = (p_info->psdata_len << 2) % 16;
187 		break;
188 	default:
189 		padding = (p_info->psdata_len << 2) % byte_align;
190 		break;
191 	}
192 	return padding;
193 }
194 
195 struct netcp_module {
196 	const char		*name;
197 	struct module		*owner;
198 	bool			primary;
199 
200 	/* probe/remove: called once per NETCP instance */
201 	int	(*probe)(struct netcp_device *netcp_device,
202 			 struct device *device, struct device_node *node,
203 			 void **inst_priv);
204 	int	(*remove)(struct netcp_device *netcp_device, void *inst_priv);
205 
206 	/* attach/release: called once per network interface */
207 	int	(*attach)(void *inst_priv, struct net_device *ndev,
208 			  struct device_node *node, void **intf_priv);
209 	int	(*release)(void *intf_priv);
210 	int	(*open)(void *intf_priv, struct net_device *ndev);
211 	int	(*close)(void *intf_priv, struct net_device *ndev);
212 	int	(*add_addr)(void *intf_priv, struct netcp_addr *naddr);
213 	int	(*del_addr)(void *intf_priv, struct netcp_addr *naddr);
214 	int	(*add_vid)(void *intf_priv, int vid);
215 	int	(*del_vid)(void *intf_priv, int vid);
216 	int	(*ioctl)(void *intf_priv, struct ifreq *req, int cmd);
217 	int	(*set_rx_mode)(void *intf_priv, bool promisc);
218 
219 	/* used internally */
220 	struct list_head	module_list;
221 	struct list_head	interface_list;
222 };
223 
224 int netcp_register_module(struct netcp_module *module);
225 void netcp_unregister_module(struct netcp_module *module);
226 void *netcp_module_get_intf_data(struct netcp_module *module,
227 				 struct netcp_intf *intf);
228 
229 int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe,
230 		      struct netcp_device *netcp_device,
231 		      const char *dma_chan_name, unsigned int dma_queue_id);
232 int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe);
233 int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe);
234 
235 typedef int netcp_hook_rtn(int order, void *data, struct netcp_packet *packet);
236 int netcp_register_txhook(struct netcp_intf *netcp_priv, int order,
237 			  netcp_hook_rtn *hook_rtn, void *hook_data);
238 int netcp_unregister_txhook(struct netcp_intf *netcp_priv, int order,
239 			    netcp_hook_rtn *hook_rtn, void *hook_data);
240 int netcp_register_rxhook(struct netcp_intf *netcp_priv, int order,
241 			  netcp_hook_rtn *hook_rtn, void *hook_data);
242 int netcp_unregister_rxhook(struct netcp_intf *netcp_priv, int order,
243 			    netcp_hook_rtn *hook_rtn, void *hook_data);
244 void *netcp_device_find_module(struct netcp_device *netcp_device,
245 			       const char *name);
246 
247 /* SGMII functions */
248 int netcp_sgmii_reset(void __iomem *sgmii_ofs, int port);
249 bool netcp_sgmii_rtreset(void __iomem *sgmii_ofs, int port, bool set);
250 int netcp_sgmii_get_port_link(void __iomem *sgmii_ofs, int port);
251 int netcp_sgmii_config(void __iomem *sgmii_ofs, int port, u32 interface);
252 
253 /* XGBE SERDES init functions */
254 int netcp_xgbe_serdes_init(void __iomem *serdes_regs, void __iomem *xgbe_regs);
255 
256 #endif	/* __NETCP_H__ */
257