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