1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2014-2015 Hisilicon Limited.
6 #ifndef _HNS_DSAF_MAC_H
7 #define _HNS_DSAF_MAC_H
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"
17 #define MAC_GMAC_SUPPORTED \
18 (SUPPORTED_10baseT_Half \
19 | SUPPORTED_10baseT_Full \
20 | SUPPORTED_100baseT_Half \
21 | SUPPORTED_100baseT_Full \
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
30 #define MAC_DEFAULT_PAUSE_TIME 0xffff
32 #define MAC_GMAC_IDX 0
33 #define MAC_XGMAC_IDX 1
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))
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) && \
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))
52 /*check mac addr multicast*/
53 #define MAC_IS_MULTICAST(p) ((*((u8 *)((p) + 0)) & 0x01) ? (1) : (0))
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 */
67 /*mac interface keyword */
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 */
84 /**< Invalid Ethernet mode */
87 MAC_MODE_MII_10
= (MAC_IF_MII
| MAC_SPEED_10
),
89 MAC_MODE_MII_100
= (MAC_IF_MII
| MAC_SPEED_100
),
91 MAC_MODE_RMII_10
= (MAC_IF_RMII
| MAC_SPEED_10
),
93 MAC_MODE_RMII_100
= (MAC_IF_RMII
| MAC_SPEED_100
),
95 MAC_MODE_SMII_10
= (MAC_IF_SMII
| MAC_SPEED_10
),
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
)
122 /*mac communicate mode*/
123 enum 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 */
131 struct 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.*/
142 /* Total number of packets longer than valid maximum length octets */
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*/
155 u64 stat_rx_pause
; /**< Pause MAC Control received */
156 u64 stat_tx_pause
; /**< Pause MAC Control sent */
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. */
165 u64 in_errors
; /* Number of frames received with error: */
166 /* - FIFO Overflow 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!.*/
177 u64 out_errors
; /*Number of frames transmitted with error: */
178 /* - FIFO Overflow Error */
179 /* - FIFO Underflow Error */
183 /*mac para struct ,mac get param from nic or dsaf when initialize*/
186 u8 __iomem
*vaddr
; /*virtual address*/
189 /**< Ethernet operation mode (MAC-PHY interface and speed) */
190 enum mac_mode mac_mode
;
194 u16 speed
;/* The forced speed (lower bits) in */
195 /* *mbps. Please use */
196 /* * ethtool_cmd_speed()/_set() to */
198 u8 duplex
; /* Duplex, half or full */
199 u8 auto_neg
; /* Enable or disable autonegotiation */
200 enum hnae_loop loop_mode
;
206 u8 port_en
; /*port enable*/
209 struct mac_entry_idx
{
216 struct mac_hw_stats
{
217 u64 rx_good_pkts
; /* only for xgmac */
219 u64 rx_total_pkts
; /* only for xgmac */
220 u64 rx_total_bytes
; /* only for xgmac */
221 u64 rx_bad_bytes
; /* only for gmac */
225 u64 rx_fragment_err
; /* only for xgmac */
226 u64 rx_undersize
; /* only for xgmac */
228 u64 rx_minto64
; /* only for gmac */
236 u64 rx_1519tomax_good
; /* only for xgmac */
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 */
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 */
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 */
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 */
272 u64 tx_undersize
; /* only for xgmac */
273 u64 tx_fragment_err
; /* only for xgmac */
274 u64 tx_under_min_pkts
; /* only for gmac */
282 u64 tx_1519tomax_good
; /* only for xgmac */
283 u64 tx_oversize
; /* only for xgmac */
285 u64 tx_underrun_err
; /* only for gmac */
286 u64 tx_vlan
; /* only for gmac */
287 u64 tx_crc_err
; /* only for gmac */
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 */
305 struct dsaf_device
*dsaf_dev
;
306 struct mac_priv priv
;
307 struct fwnode_handle
*fw_port
;
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
];
317 struct mac_entry_idx addr_entry_idx
[DSAF_MAX_VM_NUM
];
327 u16 tx_pause_frm_time
;
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
;
336 struct phy_device
*phy_dev
;
338 struct mac_hw_stats hw_stats
;
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
, char *mac_addr
);
352 /*adjust mac mode of port,include speed and duplex*/
353 int (*adjust_link
)(void *mac_drv
, enum mac_speed speed
,
355 /* need adjust link */
356 bool (*need_adjust_link
)(void *mac_drv
, enum mac_speed speed
,
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
,
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 duplex mode*/
368 void (*config_half_duplex
)(void *mac_drv
, u8 newval
);
369 /*config tx pause time,if pause_time is zero,disable tx pause enable*/
370 void (*set_tx_auto_pause_frames
)(void *mac_drv
, u16 pause_time
);
371 /*config rx pause enable*/
372 void (*set_rx_ignore_pause_frames
)(void *mac_drv
, u32 enable
);
373 /* config rx mode for promiscuous*/
374 void (*set_promiscuous
)(void *mac_drv
, u8 enable
);
375 void (*mac_pausefrm_cfg
)(void *mac_drv
, u32 rx_en
, u32 tx_en
);
377 void (*autoneg_stat
)(void *mac_drv
, u32
*enable
);
378 int (*set_pause_enable
)(void *mac_drv
, u32 rx_en
, u32 tx_en
);
379 void (*get_pause_enable
)(void *mac_drv
, u32
*rx_en
, u32
*tx_en
);
380 void (*get_link_status
)(void *mac_drv
, u32
*link_stat
);
381 /* get the imporant regs*/
382 void (*get_regs
)(void *mac_drv
, void *data
);
383 int (*get_regs_count
)(void);
384 /* get strings name for ethtool statistic */
385 void (*get_strings
)(u32 stringset
, u8
*data
);
386 /* get the number of strings*/
387 int (*get_sset_count
)(int stringset
);
389 /* get the statistic by ethtools*/
390 void (*get_ethtool_stats
)(void *mac_drv
, u64
*data
);
392 /* get mac information */
393 void (*get_info
)(void *mac_drv
, struct mac_info
*mac_info
);
395 void (*update_stats
)(void *mac_drv
);
396 int (*wait_fifo_clean
)(void *mac_drv
);
398 enum mac_mode mac_mode
;
400 struct hns_mac_cb
*mac_cb
;
402 unsigned int mac_en_flg
;/*you'd better don't enable mac twice*/
403 unsigned int virt_dev_num
;
407 struct mac_stats_string
{
408 const char desc
[ETH_GSTRING_LEN
];
409 unsigned long offset
;
412 #define MAC_MAKE_MODE(interface, speed) (enum mac_mode)((interface) | (speed))
413 #define MAC_INTERFACE_FROM_MODE(mode) (enum mac_intf)((mode) & 0xFFFF0000)
414 #define MAC_SPEED_FROM_MODE(mode) (enum mac_speed)((mode) & 0x0000FFFF)
415 #define MAC_STATS_FIELD_OFF(field) (offsetof(struct mac_hw_stats, field))
417 static inline struct mac_driver
*hns_mac_get_drv(
418 const struct hns_mac_cb
*mac_cb
)
420 return (struct mac_driver
*)(mac_cb
->priv
.mac
);
423 void *hns_gmac_config(struct hns_mac_cb
*mac_cb
,
424 struct mac_params
*mac_param
);
425 void *hns_xgmac_config(struct hns_mac_cb
*mac_cb
,
426 struct mac_params
*mac_param
);
428 int hns_mac_init(struct dsaf_device
*dsaf_dev
);
429 void mac_adjust_link(struct net_device
*net_dev
);
430 bool hns_mac_need_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
);
431 void hns_mac_get_link_status(struct hns_mac_cb
*mac_cb
, u32
*link_status
);
432 int hns_mac_change_vf_addr(struct hns_mac_cb
*mac_cb
, u32 vmid
, char *addr
);
433 int hns_mac_set_multi(struct hns_mac_cb
*mac_cb
,
434 u32 port_num
, char *addr
, bool enable
);
435 int hns_mac_vm_config_bc_en(struct hns_mac_cb
*mac_cb
, u32 vm
, bool enable
);
436 void hns_mac_start(struct hns_mac_cb
*mac_cb
);
437 void hns_mac_stop(struct hns_mac_cb
*mac_cb
);
438 void hns_mac_uninit(struct dsaf_device
*dsaf_dev
);
439 void hns_mac_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
);
440 void hns_mac_reset(struct hns_mac_cb
*mac_cb
);
441 void hns_mac_get_autoneg(struct hns_mac_cb
*mac_cb
, u32
*auto_neg
);
442 void hns_mac_get_pauseparam(struct hns_mac_cb
*mac_cb
, u32
*rx_en
, u32
*tx_en
);
443 int hns_mac_set_autoneg(struct hns_mac_cb
*mac_cb
, u8 enable
);
444 int hns_mac_set_pauseparam(struct hns_mac_cb
*mac_cb
, u32 rx_en
, u32 tx_en
);
445 int hns_mac_set_mtu(struct hns_mac_cb
*mac_cb
, u32 new_mtu
, u32 buf_size
);
446 int hns_mac_get_port_info(struct hns_mac_cb
*mac_cb
,
447 u8
*auto_neg
, u16
*speed
, u8
*duplex
);
448 int hns_mac_config_mac_loopback(struct hns_mac_cb
*mac_cb
,
449 enum hnae_loop loop
, int en
);
450 void hns_mac_update_stats(struct hns_mac_cb
*mac_cb
);
451 void hns_mac_get_stats(struct hns_mac_cb
*mac_cb
, u64
*data
);
452 void hns_mac_get_strings(struct hns_mac_cb
*mac_cb
, int stringset
, u8
*data
);
453 int hns_mac_get_sset_count(struct hns_mac_cb
*mac_cb
, int stringset
);
454 void hns_mac_get_regs(struct hns_mac_cb
*mac_cb
, void *data
);
455 int hns_mac_get_regs_count(struct hns_mac_cb
*mac_cb
);
456 void hns_set_led_opt(struct hns_mac_cb
*mac_cb
);
457 int hns_cpld_led_set_id(struct hns_mac_cb
*mac_cb
,
458 enum hnae_led_state status
);
459 void hns_mac_set_promisc(struct hns_mac_cb
*mac_cb
, u8 en
);
460 int hns_mac_get_inner_port_num(struct hns_mac_cb
*mac_cb
,
461 u8 vmid
, u8
*port_num
);
462 int hns_mac_add_uc_addr(struct hns_mac_cb
*mac_cb
, u8 vf_id
,
463 const unsigned char *addr
);
464 int hns_mac_rm_uc_addr(struct hns_mac_cb
*mac_cb
, u8 vf_id
,
465 const unsigned char *addr
);
466 int hns_mac_clr_multicast(struct hns_mac_cb
*mac_cb
, int vfn
);
467 void hns_mac_enable(struct hns_mac_cb
*mac_cb
, enum mac_commom_mode mode
);
468 void hns_mac_disable(struct hns_mac_cb
*mac_cb
, enum mac_commom_mode mode
);
469 int hns_mac_wait_fifo_clean(struct hns_mac_cb
*mac_cb
);
471 #endif /* _HNS_DSAF_MAC_H */