1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2014-2015 Hisilicon Limited.
4 */
5
6#ifndef _HNS_DSAF_MAC_H
7#define _HNS_DSAF_MAC_H
8
9#include <linux/if_vlan.h>
10#include <linux/kernel.h>
11#include <linux/phy.h>
12#include <linux/regmap.h>
13#include "hns_dsaf_main.h"
14
15struct dsaf_device;
16
17#define MAC_GMAC_SUPPORTED \
18	(SUPPORTED_10baseT_Half \
19	| SUPPORTED_10baseT_Full \
20	| SUPPORTED_100baseT_Half \
21	| SUPPORTED_100baseT_Full \
22	| SUPPORTED_Autoneg)
23
24#define MAC_DEFAULT_MTU	(ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN + ETH_DATA_LEN)
25#define MAC_MAX_MTU		9600
26#define MAC_MAX_MTU_V2		9728
27#define MAC_MIN_MTU		68
28#define MAC_MAX_MTU_DBG		MAC_DEFAULT_MTU
29
30#define MAC_DEFAULT_PAUSE_TIME 0xffff
31
32#define MAC_GMAC_IDX 0
33#define MAC_XGMAC_IDX 1
34
35#define ETH_STATIC_REG	 1
36#define ETH_DUMP_REG	 5
37/* check mac addr broadcast */
38#define MAC_IS_BROADCAST(p)	((*(p) == 0xff) && (*((p) + 1) == 0xff) && \
39		(*((p) + 2) == 0xff) &&  (*((p) + 3) == 0xff)  && \
40		(*((p) + 4) == 0xff) && (*((p) + 5) == 0xff))
41
42/* check mac addr is 01-00-5e-xx-xx-xx*/
43#define MAC_IS_L3_MULTICAST(p) ((*((p) + 0) == 0x01) && \
44			(*((p) + 1) == 0x00)   && \
45			(*((p) + 2) == 0x5e))
46
47/*check the mac addr is 0 in all bit*/
48#define MAC_IS_ALL_ZEROS(p)   ((*(p) == 0) && (*((p) + 1) == 0) && \
49	(*((p) + 2) == 0) && (*((p) + 3) == 0) && \
50	(*((p) + 4) == 0) && (*((p) + 5) == 0))
51
52/*check mac addr multicast*/
53#define MAC_IS_MULTICAST(p)	((*((u8 *)((p) + 0)) & 0x01) ? (1) : (0))
54
55struct mac_priv {
56	void *mac;
57};
58
59/* net speed */
60enum mac_speed {
61	MAC_SPEED_10	= 10,	   /**< 10 Mbps */
62	MAC_SPEED_100	= 100,	  /**< 100 Mbps */
63	MAC_SPEED_1000  = 1000,	 /**< 1000 Mbps = 1 Gbps */
64	MAC_SPEED_10000 = 10000	 /**< 10000 Mbps = 10 Gbps */
65};
66
67/*mac interface keyword	*/
68enum mac_intf {
69	MAC_IF_NONE  = 0x00000000,   /**< interface not invalid */
70	MAC_IF_MII   = 0x00010000,   /**< MII interface */
71	MAC_IF_RMII  = 0x00020000,   /**< RMII interface */
72	MAC_IF_SMII  = 0x00030000,   /**< SMII interface */
73	MAC_IF_GMII  = 0x00040000,   /**< GMII interface */
74	MAC_IF_RGMII = 0x00050000,   /**< RGMII interface */
75	MAC_IF_TBI   = 0x00060000,   /**< TBI interface */
76	MAC_IF_RTBI  = 0x00070000,   /**< RTBI interface */
77	MAC_IF_SGMII = 0x00080000,   /**< SGMII interface */
78	MAC_IF_XGMII = 0x00090000,   /**< XGMII interface */
79	MAC_IF_QSGMII = 0x000a0000	/**< QSGMII interface */
80};
81
82/*mac mode */
83enum mac_mode {
84	/**< Invalid Ethernet mode */
85	MAC_MODE_INVALID	 = 0,
86	/**<	10 Mbps MII   */
87	MAC_MODE_MII_10	  = (MAC_IF_MII   | MAC_SPEED_10),
88	/**<   100 Mbps MII   */
89	MAC_MODE_MII_100	 = (MAC_IF_MII   | MAC_SPEED_100),
90	/**<	10 Mbps RMII  */
91	MAC_MODE_RMII_10	 = (MAC_IF_RMII  | MAC_SPEED_10),
92	/**<   100 Mbps RMII  */
93	MAC_MODE_RMII_100	= (MAC_IF_RMII  | MAC_SPEED_100),
94	/**<	10 Mbps SMII  */
95	MAC_MODE_SMII_10	 = (MAC_IF_SMII  | MAC_SPEED_10),
96	/**<   100 Mbps SMII  */
97	MAC_MODE_SMII_100	= (MAC_IF_SMII  | MAC_SPEED_100),
98	/**<  1000 Mbps GMII  */
99	MAC_MODE_GMII_1000   = (MAC_IF_GMII  | MAC_SPEED_1000),
100	/**<	10 Mbps RGMII */
101	MAC_MODE_RGMII_10	= (MAC_IF_RGMII | MAC_SPEED_10),
102	/**<   100 Mbps RGMII */
103	MAC_MODE_RGMII_100   = (MAC_IF_RGMII | MAC_SPEED_100),
104	/**<  1000 Mbps RGMII */
105	MAC_MODE_RGMII_1000  = (MAC_IF_RGMII | MAC_SPEED_1000),
106	/**<  1000 Mbps TBI   */
107	MAC_MODE_TBI_1000	= (MAC_IF_TBI   | MAC_SPEED_1000),
108	/**<  1000 Mbps RTBI  */
109	MAC_MODE_RTBI_1000   = (MAC_IF_RTBI  | MAC_SPEED_1000),
110	/**<	10 Mbps SGMII */
111	MAC_MODE_SGMII_10	= (MAC_IF_SGMII | MAC_SPEED_10),
112	/**<   100 Mbps SGMII */
113	MAC_MODE_SGMII_100   = (MAC_IF_SGMII | MAC_SPEED_100),
114	/**<  1000 Mbps SGMII */
115	MAC_MODE_SGMII_1000  = (MAC_IF_SGMII | MAC_SPEED_1000),
116	/**< 10000 Mbps XGMII */
117	MAC_MODE_XGMII_10000 = (MAC_IF_XGMII | MAC_SPEED_10000),
118	/**<  1000 Mbps QSGMII */
119	MAC_MODE_QSGMII_1000 = (MAC_IF_QSGMII | MAC_SPEED_1000)
120};
121
122/*mac communicate mode*/
123enum mac_commom_mode {
124	MAC_COMM_MODE_NONE	  = 0, /**< No transmit/receive communication */
125	MAC_COMM_MODE_RX		= 1, /**< Only receive communication */
126	MAC_COMM_MODE_TX		= 2, /**< Only transmit communication */
127	MAC_COMM_MODE_RX_AND_TX = 3  /**< Both tx and rx communication */
128};
129
130/*mac statistics */
131struct mac_statistics {
132	u64  stat_pkts64; /* r-10G tr-DT 64 byte frame counter */
133	u64  stat_pkts65to127; /* r-10G 65 to 127 byte frame counter */
134	u64  stat_pkts128to255; /* r-10G 128 to 255 byte frame counter */
135	u64  stat_pkts256to511; /*r-10G 256 to 511 byte frame counter */
136	u64  stat_pkts512to1023;/* r-10G 512 to 1023 byte frame counter */
137	u64  stat_pkts1024to1518; /* r-10G 1024 to 1518 byte frame counter */
138	u64  stat_pkts1519to1522; /* r-10G 1519 to 1522 byte good frame count*/
139	/* Total number of packets that were less than 64 octets */
140	/*			long with a wrong CRC.*/
141	u64  stat_fragments;
142	/* Total number of packets longer than valid maximum length octets */
143	u64  stat_jabbers;
144	/* number of dropped packets due to internal errors of */
145	/*			the MAC Client. */
146	u64  stat_drop_events;
147	/* Incremented when frames of correct length but with */
148	/*			CRC error are received.*/
149	u64  stat_crc_align_errors;
150	/* Total number of packets that were less than 64 octets */
151	/*			long with a good CRC.*/
152	u64  stat_undersize_pkts;
153	u64  stat_oversize_pkts;  /**< T,B.D*/
154
155	u64  stat_rx_pause;		   /**< Pause MAC Control received */
156	u64  stat_tx_pause;		   /**< Pause MAC Control sent */
157
158	u64  in_octets;		/**< Total number of byte received. */
159	u64  in_pkts;		/* Total number of packets received.*/
160	u64  in_mcast_pkts;	/* Total number of multicast frame received */
161	u64  in_bcast_pkts;	/* Total number of broadcast frame received */
162				/* Frames received, but discarded due to */
163				/* problems within the MAC RX. */
164	u64  in_discards;
165	u64  in_errors;		/* Number of frames received with error: */
166				/*	- FIFO Overflow Error */
167				/*	- CRC Error */
168				/*	- Frame Too Long Error */
169				/*	- Alignment Error */
170	u64  out_octets; /*Total number of byte sent. */
171	u64  out_pkts;	/**< Total number of packets sent .*/
172	u64  out_mcast_pkts; /* Total number of multicast frame sent */
173	u64  out_bcast_pkts; /* Total number of multicast frame sent */
174	/* Frames received, but discarded due to problems within */
175	/*			the MAC TX N/A!.*/
176	u64  out_discards;
177	u64  out_errors;	/*Number of frames transmitted with error: */
178			/*	- FIFO Overflow Error */
179			/*	- FIFO Underflow Error */
180			/*	 - Other */
181};
182
183/*mac para struct ,mac get param from nic or dsaf when initialize*/
184struct mac_params {
185	char addr[ETH_ALEN];
186	u8 __iomem *vaddr; /*virtual address*/
187	struct device *dev;
188	u8 mac_id;
189	/**< Ethernet operation mode (MAC-PHY interface and speed) */
190	enum mac_mode mac_mode;
191};
192
193struct mac_info {
194	u16 speed;/* The forced speed (lower bits) in */
195		/*		 *mbps. Please use */
196		/*		 * ethtool_cmd_speed()/_set() to */
197		/*		 * access it */
198	u8 duplex;		/* Duplex, half or full */
199	u8 auto_neg;	/* Enable or disable autonegotiation */
200	enum hnae_loop loop_mode;
201	u8 tx_pause_en;
202	u8 tx_pause_time;
203	u8 rx_pause_en;
204	u8 pad_and_crc_en;
205	u8 promiscuous_en;
206	u8 port_en;	 /*port enable*/
207};
208
209struct mac_entry_idx {
210	u8 addr[ETH_ALEN];
211	u16 vlan_id:12;
212	u16 valid:1;
213	u16 qos:3;
214};
215
216struct mac_hw_stats {
217	u64 rx_good_pkts;	/* only for xgmac */
218	u64 rx_good_bytes;
219	u64 rx_total_pkts;	/* only for xgmac */
220	u64 rx_total_bytes;	/* only for xgmac */
221	u64 rx_bad_bytes;	/* only for gmac */
222	u64 rx_uc_pkts;
223	u64 rx_mc_pkts;
224	u64 rx_bc_pkts;
225	u64 rx_fragment_err;	/* only for xgmac */
226	u64 rx_undersize;	/* only for xgmac */
227	u64 rx_under_min;
228	u64 rx_minto64;		/* only for gmac */
229	u64 rx_64bytes;
230	u64 rx_65to127;
231	u64 rx_128to255;
232	u64 rx_256to511;
233	u64 rx_512to1023;
234	u64 rx_1024to1518;
235	u64 rx_1519tomax;
236	u64 rx_1519tomax_good;	/* only for xgmac */
237	u64 rx_oversize;
238	u64 rx_jabber_err;
239	u64 rx_fcs_err;
240	u64 rx_vlan_pkts;	/* only for gmac */
241	u64 rx_data_err;	/* only for gmac */
242	u64 rx_align_err;	/* only for gmac */
243	u64 rx_long_err;	/* only for gmac */
244	u64 rx_pfc_tc0;
245	u64 rx_pfc_tc1;		/* only for xgmac */
246	u64 rx_pfc_tc2;		/* only for xgmac */
247	u64 rx_pfc_tc3;		/* only for xgmac */
248	u64 rx_pfc_tc4;		/* only for xgmac */
249	u64 rx_pfc_tc5;		/* only for xgmac */
250	u64 rx_pfc_tc6;		/* only for xgmac */
251	u64 rx_pfc_tc7;		/* only for xgmac */
252	u64 rx_unknown_ctrl;
253	u64 rx_filter_pkts;	/* only for gmac */
254	u64 rx_filter_bytes;	/* only for gmac */
255	u64 rx_fifo_overrun_err;/* only for gmac */
256	u64 rx_len_err;		/* only for gmac */
257	u64 rx_comma_err;	/* only for gmac */
258	u64 rx_symbol_err;	/* only for xgmac */
259	u64 tx_good_to_sw;	/* only for xgmac */
260	u64 tx_bad_to_sw;	/* only for xgmac */
261	u64 rx_1731_pkts;	/* only for xgmac */
262
263	u64 tx_good_bytes;
264	u64 tx_good_pkts;	/* only for xgmac */
265	u64 tx_total_bytes;	/* only for xgmac */
266	u64 tx_total_pkts;	/* only for xgmac */
267	u64 tx_bad_bytes;	/* only for gmac */
268	u64 tx_bad_pkts;	/* only for xgmac */
269	u64 tx_uc_pkts;
270	u64 tx_mc_pkts;
271	u64 tx_bc_pkts;
272	u64 tx_undersize;	/* only for xgmac */
273	u64 tx_fragment_err;	/* only for xgmac */
274	u64 tx_under_min_pkts;	/* only for gmac */
275	u64 tx_64bytes;
276	u64 tx_65to127;
277	u64 tx_128to255;
278	u64 tx_256to511;
279	u64 tx_512to1023;
280	u64 tx_1024to1518;
281	u64 tx_1519tomax;
282	u64 tx_1519tomax_good;	/* only for xgmac */
283	u64 tx_oversize;	/* only for xgmac */
284	u64 tx_jabber_err;
285	u64 tx_underrun_err;	/* only for gmac */
286	u64 tx_vlan;		/* only for gmac */
287	u64 tx_crc_err;		/* only for gmac */
288	u64 tx_pfc_tc0;
289	u64 tx_pfc_tc1;		/* only for xgmac */
290	u64 tx_pfc_tc2;		/* only for xgmac */
291	u64 tx_pfc_tc3;		/* only for xgmac */
292	u64 tx_pfc_tc4;		/* only for xgmac */
293	u64 tx_pfc_tc5;		/* only for xgmac */
294	u64 tx_pfc_tc6;		/* only for xgmac */
295	u64 tx_pfc_tc7;		/* only for xgmac */
296	u64 tx_ctrl;		/* only for xgmac */
297	u64 tx_1731_pkts;	/* only for xgmac */
298	u64 tx_1588_pkts;	/* only for xgmac */
299	u64 rx_good_from_sw;	/* only for xgmac */
300	u64 rx_bad_from_sw;	/* only for xgmac */
301};
302
303struct hns_mac_cb {
304	struct device *dev;
305	struct dsaf_device *dsaf_dev;
306	struct mac_priv priv;
307	struct fwnode_handle *fw_port;
308	u8 __iomem *vaddr;
309	u8 __iomem *sys_ctl_vaddr;
310	u8 __iomem *serdes_vaddr;
311	struct regmap *serdes_ctrl;
312	struct regmap *cpld_ctrl;
313	char mc_mask[ETH_ALEN];
314	u32 cpld_ctrl_reg;
315	u32 port_rst_off;
316	u32 port_mode_off;
317	struct mac_entry_idx addr_entry_idx[DSAF_MAX_VM_NUM];
318	u8 sfp_prsnt;
319	u8 cpld_led_value;
320	u8 mac_id;
321
322	u8 link;
323	u8 half_duplex;
324	u16 speed;
325	u16 max_speed;
326	u16 max_frm;
327	u16 tx_pause_frm_time;
328	u32 if_support;
329	u64 txpkt_for_led;
330	u64 rxpkt_for_led;
331	enum hnae_port_type mac_type;
332	enum hnae_media_type media_type;
333	phy_interface_t phy_if;
334	enum hnae_loop loop_mode;
335
336	struct phy_device *phy_dev;
337
338	struct mac_hw_stats hw_stats;
339};
340
341struct mac_driver {
342	/*init Mac when init nic or dsaf*/
343	void (*mac_init)(void *mac_drv);
344	/*remove mac when remove nic or dsaf*/
345	void (*mac_free)(void *mac_drv);
346	/*enable mac when enable nic or dsaf*/
347	void (*mac_enable)(void *mac_drv, enum mac_commom_mode mode);
348	/*disable mac when disable nic or dsaf*/
349	void (*mac_disable)(void *mac_drv, enum mac_commom_mode mode);
350	/* config mac address*/
351	void (*set_mac_addr)(void *mac_drv,	const char *mac_addr);
352	/*adjust mac mode of port,include speed and duplex*/
353	int (*adjust_link)(void *mac_drv, enum mac_speed speed,
354			   u32 full_duplex);
355	/* need adjust link */
356	bool (*need_adjust_link)(void *mac_drv, enum mac_speed speed,
357				 int duplex);
358	/* config autoegotaite mode of port*/
359	void (*set_an_mode)(void *mac_drv, u8 enable);
360	/* config loopbank mode */
361	int (*config_loopback)(void *mac_drv, enum hnae_loop loop_mode,
362			       u8 enable);
363	/* config mtu*/
364	void (*config_max_frame_length)(void *mac_drv, u16 newval);
365	/*config PAD and CRC enable */
366	void (*config_pad_and_crc)(void *mac_drv, u8 newval);
367	/*config tx pause time,if pause_time is zero,disable tx pause enable*/
368	void (*set_tx_auto_pause_frames)(void *mac_drv, u16 pause_time);
369	/* config rx mode for promiscuous*/
370	void (*set_promiscuous)(void *mac_drv, u8 enable);
371	void (*mac_pausefrm_cfg)(void *mac_drv, u32 rx_en, u32 tx_en);
372
373	void (*autoneg_stat)(void *mac_drv, u32 *enable);
374	int (*set_pause_enable)(void *mac_drv, u32 rx_en, u32 tx_en);
375	void (*get_pause_enable)(void *mac_drv, u32 *rx_en, u32 *tx_en);
376	void (*get_link_status)(void *mac_drv, u32 *link_stat);
377	/* get the imporant regs*/
378	void (*get_regs)(void *mac_drv, void *data);
379	int (*get_regs_count)(void);
380	/* get strings name for ethtool statistic */
381	void (*get_strings)(u32 stringset, u8 *data);
382	/* get the number of strings*/
383	int (*get_sset_count)(int stringset);
384
385	/* get the statistic by ethtools*/
386	void (*get_ethtool_stats)(void *mac_drv, u64 *data);
387
388	/* get mac information */
389	void (*get_info)(void *mac_drv, struct mac_info *mac_info);
390
391	void (*update_stats)(void *mac_drv);
392	int (*wait_fifo_clean)(void *mac_drv);
393
394	enum mac_mode mac_mode;
395	u8 mac_id;
396	struct hns_mac_cb *mac_cb;
397	u8 __iomem *io_base;
398	unsigned int mac_en_flg;/*you'd better don't enable mac twice*/
399	unsigned int virt_dev_num;
400	struct device *dev;
401};
402
403struct mac_stats_string {
404	const char desc[ETH_GSTRING_LEN];
405	unsigned long offset;
406};
407
408#define MAC_MAKE_MODE(interface, speed) (enum mac_mode)((interface) | (speed))
409#define MAC_INTERFACE_FROM_MODE(mode) (enum mac_intf)((mode) & 0xFFFF0000)
410#define MAC_SPEED_FROM_MODE(mode) (enum mac_speed)((mode) & 0x0000FFFF)
411#define MAC_STATS_FIELD_OFF(field) (offsetof(struct mac_hw_stats, field))
412
413static inline struct mac_driver *hns_mac_get_drv(
414	const struct hns_mac_cb *mac_cb)
415{
416	return (struct mac_driver *)(mac_cb->priv.mac);
417}
418
419void *hns_gmac_config(struct hns_mac_cb *mac_cb,
420		      struct mac_params *mac_param);
421void *hns_xgmac_config(struct hns_mac_cb *mac_cb,
422		       struct mac_params *mac_param);
423
424int hns_mac_init(struct dsaf_device *dsaf_dev);
425bool hns_mac_need_adjust_link(struct hns_mac_cb *mac_cb, int speed, int duplex);
426void hns_mac_get_link_status(struct hns_mac_cb *mac_cb,	u32 *link_status);
427int hns_mac_change_vf_addr(struct hns_mac_cb *mac_cb, u32 vmid,
428			   const char *addr);
429int hns_mac_set_multi(struct hns_mac_cb *mac_cb,
430		      u32 port_num, char *addr, bool enable);
431int hns_mac_vm_config_bc_en(struct hns_mac_cb *mac_cb, u32 vm, bool enable);
432void hns_mac_start(struct hns_mac_cb *mac_cb);
433void hns_mac_stop(struct hns_mac_cb *mac_cb);
434void hns_mac_uninit(struct dsaf_device *dsaf_dev);
435void hns_mac_adjust_link(struct hns_mac_cb *mac_cb, int speed, int duplex);
436void hns_mac_reset(struct hns_mac_cb *mac_cb);
437void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg);
438void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en);
439int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable);
440int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en);
441int hns_mac_set_mtu(struct hns_mac_cb *mac_cb, u32 new_mtu, u32 buf_size);
442int hns_mac_get_port_info(struct hns_mac_cb *mac_cb,
443			  u8 *auto_neg, u16 *speed, u8 *duplex);
444int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb,
445				enum hnae_loop loop, int en);
446void hns_mac_update_stats(struct hns_mac_cb *mac_cb);
447void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data);
448void hns_mac_get_strings(struct hns_mac_cb *mac_cb, int stringset, u8 *data);
449int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset);
450void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data);
451int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb);
452void hns_set_led_opt(struct hns_mac_cb *mac_cb);
453int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb,
454			enum hnae_led_state status);
455void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en);
456int hns_mac_get_inner_port_num(struct hns_mac_cb *mac_cb,
457			       u8 vmid, u8 *port_num);
458int hns_mac_add_uc_addr(struct hns_mac_cb *mac_cb, u8 vf_id,
459			const unsigned char *addr);
460int hns_mac_rm_uc_addr(struct hns_mac_cb *mac_cb, u8 vf_id,
461		       const unsigned char *addr);
462int hns_mac_clr_multicast(struct hns_mac_cb *mac_cb, int vfn);
463void hns_mac_enable(struct hns_mac_cb *mac_cb, enum mac_commom_mode mode);
464void hns_mac_disable(struct hns_mac_cb *mac_cb, enum mac_commom_mode mode);
465int hns_mac_wait_fifo_clean(struct hns_mac_cb *mac_cb);
466
467#endif /* _HNS_DSAF_MAC_H */
468