2 * Copyright (c) 2014-2015 Hisilicon Limited.
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.
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
17 #include <linux/of_address.h>
18 #include <linux/phy_fixed.h>
19 #include <linux/platform_device.h>
21 #include "hns_dsaf_main.h"
22 #include "hns_dsaf_misc.h"
23 #include "hns_dsaf_rcb.h"
25 #define MAC_EN_FLAG_V 0xada0328
27 static const u16 mac_phy_to_speed
[] = {
28 [PHY_INTERFACE_MODE_MII
] = MAC_SPEED_100
,
29 [PHY_INTERFACE_MODE_GMII
] = MAC_SPEED_1000
,
30 [PHY_INTERFACE_MODE_SGMII
] = MAC_SPEED_1000
,
31 [PHY_INTERFACE_MODE_TBI
] = MAC_SPEED_1000
,
32 [PHY_INTERFACE_MODE_RMII
] = MAC_SPEED_100
,
33 [PHY_INTERFACE_MODE_RGMII
] = MAC_SPEED_1000
,
34 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_SPEED_1000
,
35 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_SPEED_1000
,
36 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_SPEED_1000
,
37 [PHY_INTERFACE_MODE_RTBI
] = MAC_SPEED_1000
,
38 [PHY_INTERFACE_MODE_XGMII
] = MAC_SPEED_10000
41 static const enum mac_mode g_mac_mode_100
[] = {
42 [PHY_INTERFACE_MODE_MII
] = MAC_MODE_MII_100
,
43 [PHY_INTERFACE_MODE_RMII
] = MAC_MODE_RMII_100
46 static const enum mac_mode g_mac_mode_1000
[] = {
47 [PHY_INTERFACE_MODE_GMII
] = MAC_MODE_GMII_1000
,
48 [PHY_INTERFACE_MODE_SGMII
] = MAC_MODE_SGMII_1000
,
49 [PHY_INTERFACE_MODE_TBI
] = MAC_MODE_TBI_1000
,
50 [PHY_INTERFACE_MODE_RGMII
] = MAC_MODE_RGMII_1000
,
51 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_MODE_RGMII_1000
,
52 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_MODE_RGMII_1000
,
53 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_MODE_RGMII_1000
,
54 [PHY_INTERFACE_MODE_RTBI
] = MAC_MODE_RTBI_1000
57 static enum mac_mode
hns_mac_dev_to_enet_if(const struct hns_mac_cb
*mac_cb
)
59 switch (mac_cb
->max_speed
) {
61 return g_mac_mode_100
[mac_cb
->phy_if
];
63 return g_mac_mode_1000
[mac_cb
->phy_if
];
65 return MAC_MODE_XGMII_10000
;
67 return MAC_MODE_MII_100
;
71 static enum mac_mode
hns_get_enet_interface(const struct hns_mac_cb
*mac_cb
)
73 switch (mac_cb
->max_speed
) {
75 return g_mac_mode_100
[mac_cb
->phy_if
];
77 return g_mac_mode_1000
[mac_cb
->phy_if
];
79 return MAC_MODE_XGMII_10000
;
81 return MAC_MODE_MII_100
;
85 void hns_mac_get_link_status(struct hns_mac_cb
*mac_cb
, u32
*link_status
)
87 struct mac_driver
*mac_ctrl_drv
;
90 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
92 if (mac_ctrl_drv
->get_link_status
)
93 mac_ctrl_drv
->get_link_status(mac_ctrl_drv
, link_status
);
97 ret
= hns_mac_get_sfp_prsnt(mac_cb
, &sfp_prsnt
);
99 *link_status
= *link_status
&& sfp_prsnt
;
101 mac_cb
->link
= *link_status
;
104 int hns_mac_get_port_info(struct hns_mac_cb
*mac_cb
,
105 u8
*auto_neg
, u16
*speed
, u8
*duplex
)
107 struct mac_driver
*mac_ctrl_drv
;
108 struct mac_info info
;
110 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
112 if (!mac_ctrl_drv
->get_info
)
115 mac_ctrl_drv
->get_info(mac_ctrl_drv
, &info
);
117 *auto_neg
= info
.auto_neg
;
121 *duplex
= info
.duplex
;
126 void hns_mac_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
)
129 struct mac_driver
*mac_ctrl_drv
;
131 mac_ctrl_drv
= (struct mac_driver
*)(mac_cb
->priv
.mac
);
133 mac_cb
->speed
= speed
;
134 mac_cb
->half_duplex
= !duplex
;
135 mac_ctrl_drv
->mac_mode
= hns_mac_dev_to_enet_if(mac_cb
);
137 if (mac_ctrl_drv
->adjust_link
) {
138 ret
= mac_ctrl_drv
->adjust_link(mac_ctrl_drv
,
139 (enum mac_speed
)speed
, duplex
);
142 "adjust_link failed,%s mac%d ret = %#x!\n",
143 mac_cb
->dsaf_dev
->ae_dev
.name
,
144 mac_cb
->mac_id
, ret
);
151 *hns_mac_get_inner_port_num - get mac table inner port number
154 *@port_num:port number
157 static int hns_mac_get_inner_port_num(struct hns_mac_cb
*mac_cb
,
158 u8 vmid
, u8
*port_num
)
162 if (mac_cb
->dsaf_dev
->dsaf_mode
<= DSAF_MODE_ENABLE
) {
163 if (mac_cb
->mac_id
!= DSAF_MAX_PORT_NUM
) {
165 "input invalid,%s mac%d vmid%d !\n",
166 mac_cb
->dsaf_dev
->ae_dev
.name
,
167 mac_cb
->mac_id
, vmid
);
170 } else if (mac_cb
->dsaf_dev
->dsaf_mode
< DSAF_MODE_MAX
) {
171 if (mac_cb
->mac_id
>= DSAF_MAX_PORT_NUM
) {
173 "input invalid,%s mac%d vmid%d!\n",
174 mac_cb
->dsaf_dev
->ae_dev
.name
,
175 mac_cb
->mac_id
, vmid
);
179 dev_err(mac_cb
->dev
, "dsaf mode invalid,%s mac%d!\n",
180 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
184 if (vmid
>= mac_cb
->dsaf_dev
->rcb_common
[0]->max_vfn
) {
185 dev_err(mac_cb
->dev
, "input invalid,%s mac%d vmid%d !\n",
186 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vmid
);
190 switch (mac_cb
->dsaf_dev
->dsaf_mode
) {
191 case DSAF_MODE_ENABLE_FIX
:
194 case DSAF_MODE_DISABLE_FIX
:
197 case DSAF_MODE_ENABLE_0VM
:
198 case DSAF_MODE_ENABLE_8VM
:
199 case DSAF_MODE_ENABLE_16VM
:
200 case DSAF_MODE_ENABLE_32VM
:
201 case DSAF_MODE_ENABLE_128VM
:
202 case DSAF_MODE_DISABLE_2PORT_8VM
:
203 case DSAF_MODE_DISABLE_2PORT_16VM
:
204 case DSAF_MODE_DISABLE_2PORT_64VM
:
205 case DSAF_MODE_DISABLE_6PORT_0VM
:
206 case DSAF_MODE_DISABLE_6PORT_2VM
:
207 case DSAF_MODE_DISABLE_6PORT_4VM
:
208 case DSAF_MODE_DISABLE_6PORT_16VM
:
212 dev_err(mac_cb
->dev
, "dsaf mode invalid,%s mac%d!\n",
213 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
216 tmp_port
+= DSAF_BASE_INNER_PORT_NUM
;
218 *port_num
= tmp_port
;
224 *hns_mac_change_vf_addr - change vf mac address
229 int hns_mac_change_vf_addr(struct hns_mac_cb
*mac_cb
,
230 u32 vmid
, char *addr
)
233 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
234 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
235 struct dsaf_drv_mac_single_dest_entry mac_entry
;
236 struct mac_entry_idx
*old_entry
;
238 old_entry
= &mac_cb
->addr_entry_idx
[vmid
];
239 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
240 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
241 mac_entry
.in_vlan_id
= old_entry
->vlan_id
;
242 mac_entry
.in_port_num
= mac_cb
->mac_id
;
243 ret
= hns_mac_get_inner_port_num(mac_cb
, (u8
)vmid
,
244 &mac_entry
.port_num
);
248 if ((old_entry
->valid
!= 0) &&
249 (memcmp(old_entry
->addr
,
250 addr
, sizeof(mac_entry
.addr
)) != 0)) {
251 ret
= hns_dsaf_del_mac_entry(dsaf_dev
,
259 ret
= hns_dsaf_set_mac_uc_entry(dsaf_dev
, &mac_entry
);
264 if ((mac_ctrl_drv
->set_mac_addr
) && (vmid
== 0))
265 mac_ctrl_drv
->set_mac_addr(mac_cb
->priv
.mac
, addr
);
267 memcpy(old_entry
->addr
, addr
, sizeof(old_entry
->addr
));
268 old_entry
->valid
= 1;
272 int hns_mac_set_multi(struct hns_mac_cb
*mac_cb
,
273 u32 port_num
, char *addr
, bool enable
)
276 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
277 struct dsaf_drv_mac_single_dest_entry mac_entry
;
279 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
) && addr
) {
280 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
281 mac_entry
.in_vlan_id
= 0;/*vlan_id;*/
282 mac_entry
.in_port_num
= mac_cb
->mac_id
;
283 mac_entry
.port_num
= port_num
;
286 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
288 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
290 dev_err(dsaf_dev
->dev
,
291 "set mac mc port failed,%s mac%d ret = %#x!\n",
292 mac_cb
->dsaf_dev
->ae_dev
.name
,
293 mac_cb
->mac_id
, ret
);
302 *hns_mac_del_mac - delete mac address into dsaf table,can't delete the same
304 *@net_dev: net device
309 int hns_mac_del_mac(struct hns_mac_cb
*mac_cb
, u32 vfn
, char *mac
)
311 struct mac_entry_idx
*old_mac
;
312 struct dsaf_device
*dsaf_dev
;
315 dsaf_dev
= mac_cb
->dsaf_dev
;
317 if (vfn
< DSAF_MAX_VM_NUM
) {
318 old_mac
= &mac_cb
->addr_entry_idx
[vfn
];
321 "vf queue is too large,%s mac%d queue = %#x!\n",
322 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vfn
);
327 ret
= hns_dsaf_del_mac_entry(dsaf_dev
, old_mac
->vlan_id
,
328 mac_cb
->mac_id
, old_mac
->addr
);
332 if (memcmp(old_mac
->addr
, mac
, sizeof(old_mac
->addr
)) == 0)
339 static void hns_mac_param_get(struct mac_params
*param
,
340 struct hns_mac_cb
*mac_cb
)
342 param
->vaddr
= (void *)mac_cb
->vaddr
;
343 param
->mac_mode
= hns_get_enet_interface(mac_cb
);
344 memcpy(param
->addr
, mac_cb
->addr_entry_idx
[0].addr
,
345 MAC_NUM_OCTETS_PER_ADDR
);
346 param
->mac_id
= mac_cb
->mac_id
;
347 param
->dev
= mac_cb
->dev
;
351 *hns_mac_queue_config_bc_en - set broadcast rx&tx enable
353 *@queue: queue number
355 *retuen 0 - success , negative --fail
357 static int hns_mac_port_config_bc_en(struct hns_mac_cb
*mac_cb
,
358 u32 port_num
, u16 vlan_id
, bool enable
)
361 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
362 u8 addr
[MAC_NUM_OCTETS_PER_ADDR
]
363 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
364 struct dsaf_drv_mac_single_dest_entry mac_entry
;
366 /* directy return ok in debug network mode */
367 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
370 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
371 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
372 mac_entry
.in_vlan_id
= vlan_id
;
373 mac_entry
.in_port_num
= mac_cb
->mac_id
;
374 mac_entry
.port_num
= port_num
;
377 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
379 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
387 *hns_mac_vm_config_bc_en - set broadcast rx&tx enable
391 *retuen 0 - success , negative --fail
393 int hns_mac_vm_config_bc_en(struct hns_mac_cb
*mac_cb
, u32 vmid
, bool enable
)
396 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
398 u8 addr
[MAC_NUM_OCTETS_PER_ADDR
]
399 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
400 struct mac_entry_idx
*uc_mac_entry
;
401 struct dsaf_drv_mac_single_dest_entry mac_entry
;
403 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
406 uc_mac_entry
= &mac_cb
->addr_entry_idx
[vmid
];
408 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
409 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
410 mac_entry
.in_vlan_id
= uc_mac_entry
->vlan_id
;
411 mac_entry
.in_port_num
= mac_cb
->mac_id
;
412 ret
= hns_mac_get_inner_port_num(mac_cb
, vmid
, &port_num
);
415 mac_entry
.port_num
= port_num
;
418 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
420 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
427 void hns_mac_reset(struct hns_mac_cb
*mac_cb
)
429 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
430 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
434 if (drv
->config_max_frame_length
)
435 drv
->config_max_frame_length(drv
, mac_cb
->max_frm
);
437 if (drv
->set_tx_auto_pause_frames
)
438 drv
->set_tx_auto_pause_frames(drv
, mac_cb
->tx_pause_frm_time
);
440 if (drv
->set_an_mode
)
441 drv
->set_an_mode(drv
, 1);
443 if (drv
->mac_pausefrm_cfg
) {
444 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
445 drv
->mac_pausefrm_cfg(drv
, !is_ver1
, !is_ver1
);
446 else /* mac rx must disable, dsaf pfc close instead of it*/
447 drv
->mac_pausefrm_cfg(drv
, 0, 1);
451 int hns_mac_set_mtu(struct hns_mac_cb
*mac_cb
, u32 new_mtu
)
453 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
454 u32 buf_size
= mac_cb
->dsaf_dev
->buf_size
;
455 u32 new_frm
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
456 u32 max_frm
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
) ?
457 MAC_MAX_MTU
: MAC_MAX_MTU_V2
;
459 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
460 max_frm
= MAC_MAX_MTU_DBG
;
462 if ((new_mtu
< MAC_MIN_MTU
) || (new_frm
> max_frm
) ||
463 (new_frm
> HNS_RCB_RING_MAX_BD_PER_PKT
* buf_size
))
466 if (!drv
->config_max_frame_length
)
469 /* adjust max frame to be at least the size of a standard frame */
470 if (new_frm
< (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
))
471 new_frm
= (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
473 drv
->config_max_frame_length(drv
, new_frm
);
475 mac_cb
->max_frm
= new_frm
;
480 void hns_mac_start(struct hns_mac_cb
*mac_cb
)
482 struct mac_driver
*mac_drv
= hns_mac_get_drv(mac_cb
);
485 if (mac_drv
->mac_en_flg
== MAC_EN_FLAG_V
) {
486 /*plus 1 when the virtual mac has been enabled */
487 mac_drv
->virt_dev_num
+= 1;
491 if (mac_drv
->mac_enable
) {
492 mac_drv
->mac_enable(mac_cb
->priv
.mac
, MAC_COMM_MODE_RX_AND_TX
);
493 mac_drv
->mac_en_flg
= MAC_EN_FLAG_V
;
497 void hns_mac_stop(struct hns_mac_cb
*mac_cb
)
499 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
501 /*modified for virtualization */
502 if (mac_ctrl_drv
->virt_dev_num
> 0) {
503 mac_ctrl_drv
->virt_dev_num
-= 1;
504 if (mac_ctrl_drv
->virt_dev_num
> 0)
508 if (mac_ctrl_drv
->mac_disable
)
509 mac_ctrl_drv
->mac_disable(mac_cb
->priv
.mac
,
510 MAC_COMM_MODE_RX_AND_TX
);
512 mac_ctrl_drv
->mac_en_flg
= 0;
514 cpld_led_reset(mac_cb
);
518 * hns_mac_get_autoneg - get auto autonegotiation
519 * @mac_cb: mac control block
520 * @enable: enable or not
521 * retuen 0 - success , negative --fail
523 void hns_mac_get_autoneg(struct hns_mac_cb
*mac_cb
, u32
*auto_neg
)
525 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
527 if (mac_ctrl_drv
->autoneg_stat
)
528 mac_ctrl_drv
->autoneg_stat(mac_ctrl_drv
, auto_neg
);
534 * hns_mac_get_pauseparam - set rx & tx pause parameter
535 * @mac_cb: mac control block
536 * @rx_en: rx enable status
537 * @tx_en: tx enable status
538 * retuen 0 - success , negative --fail
540 void hns_mac_get_pauseparam(struct hns_mac_cb
*mac_cb
, u32
*rx_en
, u32
*tx_en
)
542 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
544 if (mac_ctrl_drv
->get_pause_enable
) {
545 mac_ctrl_drv
->get_pause_enable(mac_ctrl_drv
, rx_en
, tx_en
);
553 * hns_mac_set_autoneg - set auto autonegotiation
554 * @mac_cb: mac control block
555 * @enable: enable or not
556 * retuen 0 - success , negative --fail
558 int hns_mac_set_autoneg(struct hns_mac_cb
*mac_cb
, u8 enable
)
560 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
562 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
&& enable
) {
563 dev_err(mac_cb
->dev
, "enable autoneg is not allowed!");
567 if (mac_ctrl_drv
->set_an_mode
)
568 mac_ctrl_drv
->set_an_mode(mac_ctrl_drv
, enable
);
574 * hns_mac_set_autoneg - set rx & tx pause parameter
575 * @mac_cb: mac control block
576 * @rx_en: rx enable or not
577 * @tx_en: tx enable or not
578 * return 0 - success , negative --fail
580 int hns_mac_set_pauseparam(struct hns_mac_cb
*mac_cb
, u32 rx_en
, u32 tx_en
)
582 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
583 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
585 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
) {
586 if (is_ver1
&& (tx_en
|| rx_en
)) {
587 dev_err(mac_cb
->dev
, "macv1 cann't enable tx/rx_pause!");
592 if (mac_ctrl_drv
->mac_pausefrm_cfg
)
593 mac_ctrl_drv
->mac_pausefrm_cfg(mac_ctrl_drv
, rx_en
, tx_en
);
599 * hns_mac_init_ex - mac init
600 * @mac_cb: mac control block
601 * retuen 0 - success , negative --fail
603 static int hns_mac_init_ex(struct hns_mac_cb
*mac_cb
)
606 struct mac_params param
;
607 struct mac_driver
*drv
;
609 hns_dsaf_fix_mac_mode(mac_cb
);
611 memset(¶m
, 0, sizeof(struct mac_params
));
612 hns_mac_param_get(¶m
, mac_cb
);
614 if (MAC_SPEED_FROM_MODE(param
.mac_mode
) < MAC_SPEED_10000
)
615 drv
= (struct mac_driver
*)hns_gmac_config(mac_cb
, ¶m
);
617 drv
= (struct mac_driver
*)hns_xgmac_config(mac_cb
, ¶m
);
622 mac_cb
->priv
.mac
= (void *)drv
;
623 hns_mac_reset(mac_cb
);
625 hns_mac_adjust_link(mac_cb
, mac_cb
->speed
, !mac_cb
->half_duplex
);
627 ret
= hns_mac_port_config_bc_en(mac_cb
, mac_cb
->mac_id
, 0, true);
634 drv
->mac_free(mac_cb
->priv
.mac
);
635 mac_cb
->priv
.mac
= NULL
;
641 *hns_mac_get_info - get mac information from device node
644 * return: 0 --success, negative --fail
646 static int hns_mac_get_info(struct hns_mac_cb
*mac_cb
)
648 struct device_node
*np
= mac_cb
->dev
->of_node
;
649 struct regmap
*syscon
;
652 mac_cb
->link
= false;
653 mac_cb
->half_duplex
= false;
654 mac_cb
->speed
= mac_phy_to_speed
[mac_cb
->phy_if
];
655 mac_cb
->max_speed
= mac_cb
->speed
;
657 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
658 mac_cb
->if_support
= MAC_GMAC_SUPPORTED
;
659 mac_cb
->if_support
|= SUPPORTED_1000baseT_Full
;
660 } else if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
661 mac_cb
->if_support
= SUPPORTED_10000baseR_FEC
;
662 mac_cb
->if_support
|= SUPPORTED_10000baseKR_Full
;
665 mac_cb
->max_frm
= MAC_DEFAULT_MTU
;
666 mac_cb
->tx_pause_frm_time
= MAC_DEFAULT_PAUSE_TIME
;
667 mac_cb
->port_rst_off
= mac_cb
->mac_id
;
668 mac_cb
->port_mode_off
= 0;
670 /* if the dsaf node doesn't contain a port subnode, get phy-handle
673 if (!mac_cb
->fw_port
) {
674 mac_cb
->phy_node
= of_parse_phandle(np
, "phy-handle",
676 if (mac_cb
->phy_node
)
677 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
678 mac_cb
->mac_id
, mac_cb
->phy_node
->name
);
681 if (!is_of_node(mac_cb
->fw_port
))
683 /* parse property from port subnode in dsaf */
684 mac_cb
->phy_node
= of_parse_phandle(to_of_node(mac_cb
->fw_port
),
686 if (mac_cb
->phy_node
)
687 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
688 mac_cb
->mac_id
, mac_cb
->phy_node
->name
);
689 syscon
= syscon_node_to_regmap(
690 of_parse_phandle(to_of_node(mac_cb
->fw_port
),
691 "serdes-syscon", 0));
692 if (IS_ERR_OR_NULL(syscon
)) {
693 dev_err(mac_cb
->dev
, "serdes-syscon is needed!\n");
696 mac_cb
->serdes_ctrl
= syscon
;
698 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
700 &mac_cb
->port_rst_off
);
703 "mac%d port-rst-offset not found, use default value.\n",
707 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
709 &mac_cb
->port_mode_off
);
712 "mac%d port-mode-offset not found, use default value.\n",
716 syscon
= syscon_node_to_regmap(
717 of_parse_phandle(to_of_node(mac_cb
->fw_port
),
719 if (IS_ERR_OR_NULL(syscon
)) {
720 dev_dbg(mac_cb
->dev
, "no cpld-syscon found!\n");
721 mac_cb
->cpld_ctrl
= NULL
;
723 mac_cb
->cpld_ctrl
= syscon
;
724 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
726 &mac_cb
->cpld_ctrl_reg
);
728 dev_err(mac_cb
->dev
, "get cpld-ctrl-reg fail!\n");
736 * hns_mac_get_mode - get mac mode
737 * @phy_if: phy interface
738 * retuen 0 - gmac, 1 - xgmac , negative --fail
740 static int hns_mac_get_mode(phy_interface_t phy_if
)
743 case PHY_INTERFACE_MODE_SGMII
:
745 case PHY_INTERFACE_MODE_XGMII
:
746 return MAC_XGMAC_IDX
;
752 u8 __iomem
*hns_mac_get_vaddr(struct dsaf_device
*dsaf_dev
,
753 struct hns_mac_cb
*mac_cb
, u32 mac_mode_idx
)
755 u8 __iomem
*base
= dsaf_dev
->io_base
;
756 int mac_id
= mac_cb
->mac_id
;
758 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
759 return base
+ 0x40000 + mac_id
* 0x4000 -
760 mac_mode_idx
* 0x20000;
762 return dsaf_dev
->ppe_base
+ 0x1000;
766 * hns_mac_get_cfg - get mac cfg from dtb or acpi table
767 * @dsaf_dev: dsa fabric device struct pointer
768 * @mac_cb: mac control block
769 * return 0 - success , negative --fail
771 int hns_mac_get_cfg(struct dsaf_device
*dsaf_dev
, struct hns_mac_cb
*mac_cb
)
776 mac_cb
->dsaf_dev
= dsaf_dev
;
777 mac_cb
->dev
= dsaf_dev
->dev
;
779 mac_cb
->sys_ctl_vaddr
= dsaf_dev
->sc_base
;
780 mac_cb
->serdes_vaddr
= dsaf_dev
->sds_base
;
782 mac_cb
->sfp_prsnt
= 0;
783 mac_cb
->txpkt_for_led
= 0;
784 mac_cb
->rxpkt_for_led
= 0;
786 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
))
787 mac_cb
->mac_type
= HNAE_PORT_SERVICE
;
789 mac_cb
->mac_type
= HNAE_PORT_DEBUG
;
791 mac_cb
->phy_if
= hns_mac_get_phy_if(mac_cb
);
793 ret
= hns_mac_get_mode(mac_cb
->phy_if
);
795 dev_err(dsaf_dev
->dev
,
796 "hns_mac_get_mode failed,mac%d ret = %#x!\n",
797 mac_cb
->mac_id
, ret
);
800 mac_mode_idx
= (u32
)ret
;
802 ret
= hns_mac_get_info(mac_cb
);
806 cpld_led_reset(mac_cb
);
807 mac_cb
->vaddr
= hns_mac_get_vaddr(dsaf_dev
, mac_cb
, mac_mode_idx
);
812 static int hns_mac_get_max_port_num(struct dsaf_device
*dsaf_dev
)
814 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
817 return DSAF_MAX_PORT_NUM
;
821 * hns_mac_init - init mac
822 * @dsaf_dev: dsa fabric device struct pointer
823 * return 0 - success , negative --fail
825 int hns_mac_init(struct dsaf_device
*dsaf_dev
)
830 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
831 struct hns_mac_cb
*mac_cb
;
832 struct fwnode_handle
*child
;
834 device_for_each_child_node(dsaf_dev
->dev
, child
) {
835 ret
= fwnode_property_read_u32(child
, "port-id", &port_id
);
837 dev_err(dsaf_dev
->dev
,
838 "get port-id fail, ret=%d!\n", ret
);
841 if (port_id
>= max_port_num
) {
842 dev_err(dsaf_dev
->dev
,
843 "port-id(%u) out of range!\n", port_id
);
846 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
850 mac_cb
->fw_port
= child
;
851 mac_cb
->mac_id
= (u8
)port_id
;
852 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
856 /* if don't get any port subnode from dsaf node
857 * will init all port then, this is compatible with the old dts
860 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
861 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
866 mac_cb
->mac_id
= port_id
;
867 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
870 /* init mac_cb for all port */
871 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
872 mac_cb
= dsaf_dev
->mac_cb
[port_id
];
876 ret
= hns_mac_get_cfg(dsaf_dev
, mac_cb
);
879 ret
= hns_mac_init_ex(mac_cb
);
887 void hns_mac_uninit(struct dsaf_device
*dsaf_dev
)
890 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
892 for (i
= 0; i
< max_port_num
; i
++) {
893 cpld_led_reset(dsaf_dev
->mac_cb
[i
]);
894 dsaf_dev
->mac_cb
[i
] = NULL
;
898 int hns_mac_config_mac_loopback(struct hns_mac_cb
*mac_cb
,
899 enum hnae_loop loop
, int en
)
902 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
904 if (drv
->config_loopback
)
905 ret
= drv
->config_loopback(drv
, loop
, en
);
912 void hns_mac_update_stats(struct hns_mac_cb
*mac_cb
)
914 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
916 mac_ctrl_drv
->update_stats(mac_ctrl_drv
);
919 void hns_mac_get_stats(struct hns_mac_cb
*mac_cb
, u64
*data
)
921 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
923 mac_ctrl_drv
->get_ethtool_stats(mac_ctrl_drv
, data
);
926 void hns_mac_get_strings(struct hns_mac_cb
*mac_cb
,
927 int stringset
, u8
*data
)
929 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
931 mac_ctrl_drv
->get_strings(stringset
, data
);
934 int hns_mac_get_sset_count(struct hns_mac_cb
*mac_cb
, int stringset
)
936 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
938 return mac_ctrl_drv
->get_sset_count(stringset
);
941 void hns_mac_set_promisc(struct hns_mac_cb
*mac_cb
, u8 en
)
943 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
945 if (mac_ctrl_drv
->set_promiscuous
)
946 mac_ctrl_drv
->set_promiscuous(mac_ctrl_drv
, en
);
949 int hns_mac_get_regs_count(struct hns_mac_cb
*mac_cb
)
951 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
953 return mac_ctrl_drv
->get_regs_count();
956 void hns_mac_get_regs(struct hns_mac_cb
*mac_cb
, void *data
)
958 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
960 mac_ctrl_drv
->get_regs(mac_ctrl_drv
, data
);
963 void hns_set_led_opt(struct hns_mac_cb
*mac_cb
)
968 txpkts
= mac_cb
->txpkt_for_led
- mac_cb
->hw_stats
.tx_good_pkts
;
969 rxpkts
= mac_cb
->rxpkt_for_led
- mac_cb
->hw_stats
.rx_good_pkts
;
970 if (txpkts
|| rxpkts
)
974 mac_cb
->txpkt_for_led
= mac_cb
->hw_stats
.tx_good_pkts
;
975 mac_cb
->rxpkt_for_led
= mac_cb
->hw_stats
.rx_good_pkts
;
976 hns_cpld_set_led(mac_cb
, (int)mac_cb
->link
,
977 mac_cb
->speed
, nic_data
);
980 int hns_cpld_led_set_id(struct hns_mac_cb
*mac_cb
,
981 enum hnae_led_state status
)
983 if (!mac_cb
|| !mac_cb
->cpld_ctrl
)
986 return cpld_set_led_id(mac_cb
, status
);