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/of_mdio.h>
19 #include <linux/phy.h>
20 #include <linux/platform_device.h>
22 #include "hns_dsaf_main.h"
23 #include "hns_dsaf_misc.h"
24 #include "hns_dsaf_rcb.h"
26 #define MAC_EN_FLAG_V 0xada0328
28 static const u16 mac_phy_to_speed
[] = {
29 [PHY_INTERFACE_MODE_MII
] = MAC_SPEED_100
,
30 [PHY_INTERFACE_MODE_GMII
] = MAC_SPEED_1000
,
31 [PHY_INTERFACE_MODE_SGMII
] = MAC_SPEED_1000
,
32 [PHY_INTERFACE_MODE_TBI
] = MAC_SPEED_1000
,
33 [PHY_INTERFACE_MODE_RMII
] = MAC_SPEED_100
,
34 [PHY_INTERFACE_MODE_RGMII
] = MAC_SPEED_1000
,
35 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_SPEED_1000
,
36 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_SPEED_1000
,
37 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_SPEED_1000
,
38 [PHY_INTERFACE_MODE_RTBI
] = MAC_SPEED_1000
,
39 [PHY_INTERFACE_MODE_XGMII
] = MAC_SPEED_10000
42 static const enum mac_mode g_mac_mode_100
[] = {
43 [PHY_INTERFACE_MODE_MII
] = MAC_MODE_MII_100
,
44 [PHY_INTERFACE_MODE_RMII
] = MAC_MODE_RMII_100
47 static const enum mac_mode g_mac_mode_1000
[] = {
48 [PHY_INTERFACE_MODE_GMII
] = MAC_MODE_GMII_1000
,
49 [PHY_INTERFACE_MODE_SGMII
] = MAC_MODE_SGMII_1000
,
50 [PHY_INTERFACE_MODE_TBI
] = MAC_MODE_TBI_1000
,
51 [PHY_INTERFACE_MODE_RGMII
] = MAC_MODE_RGMII_1000
,
52 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_MODE_RGMII_1000
,
53 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_MODE_RGMII_1000
,
54 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_MODE_RGMII_1000
,
55 [PHY_INTERFACE_MODE_RTBI
] = MAC_MODE_RTBI_1000
58 static enum mac_mode
hns_mac_dev_to_enet_if(const struct hns_mac_cb
*mac_cb
)
60 switch (mac_cb
->max_speed
) {
62 return g_mac_mode_100
[mac_cb
->phy_if
];
64 return g_mac_mode_1000
[mac_cb
->phy_if
];
66 return MAC_MODE_XGMII_10000
;
68 return MAC_MODE_MII_100
;
72 static enum mac_mode
hns_get_enet_interface(const struct hns_mac_cb
*mac_cb
)
74 switch (mac_cb
->max_speed
) {
76 return g_mac_mode_100
[mac_cb
->phy_if
];
78 return g_mac_mode_1000
[mac_cb
->phy_if
];
80 return MAC_MODE_XGMII_10000
;
82 return MAC_MODE_MII_100
;
86 void hns_mac_get_link_status(struct hns_mac_cb
*mac_cb
, u32
*link_status
)
88 struct mac_driver
*mac_ctrl_drv
;
91 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
93 if (mac_ctrl_drv
->get_link_status
)
94 mac_ctrl_drv
->get_link_status(mac_ctrl_drv
, link_status
);
98 ret
= mac_cb
->dsaf_dev
->misc_op
->get_sfp_prsnt(mac_cb
, &sfp_prsnt
);
100 *link_status
= *link_status
&& sfp_prsnt
;
102 mac_cb
->link
= *link_status
;
105 int hns_mac_get_port_info(struct hns_mac_cb
*mac_cb
,
106 u8
*auto_neg
, u16
*speed
, u8
*duplex
)
108 struct mac_driver
*mac_ctrl_drv
;
109 struct mac_info info
;
111 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
113 if (!mac_ctrl_drv
->get_info
)
116 mac_ctrl_drv
->get_info(mac_ctrl_drv
, &info
);
118 *auto_neg
= info
.auto_neg
;
122 *duplex
= info
.duplex
;
127 void hns_mac_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
)
130 struct mac_driver
*mac_ctrl_drv
;
132 mac_ctrl_drv
= (struct mac_driver
*)(mac_cb
->priv
.mac
);
134 mac_cb
->speed
= speed
;
135 mac_cb
->half_duplex
= !duplex
;
136 mac_ctrl_drv
->mac_mode
= hns_mac_dev_to_enet_if(mac_cb
);
138 if (mac_ctrl_drv
->adjust_link
) {
139 ret
= mac_ctrl_drv
->adjust_link(mac_ctrl_drv
,
140 (enum mac_speed
)speed
, duplex
);
143 "adjust_link failed,%s mac%d ret = %#x!\n",
144 mac_cb
->dsaf_dev
->ae_dev
.name
,
145 mac_cb
->mac_id
, ret
);
152 *hns_mac_get_inner_port_num - get mac table inner port number
155 *@port_num:port number
158 static int hns_mac_get_inner_port_num(struct hns_mac_cb
*mac_cb
,
159 u8 vmid
, u8
*port_num
)
163 if (mac_cb
->dsaf_dev
->dsaf_mode
<= DSAF_MODE_ENABLE
) {
164 if (mac_cb
->mac_id
!= DSAF_MAX_PORT_NUM
) {
166 "input invalid,%s mac%d vmid%d !\n",
167 mac_cb
->dsaf_dev
->ae_dev
.name
,
168 mac_cb
->mac_id
, vmid
);
171 } else if (mac_cb
->dsaf_dev
->dsaf_mode
< DSAF_MODE_MAX
) {
172 if (mac_cb
->mac_id
>= DSAF_MAX_PORT_NUM
) {
174 "input invalid,%s mac%d vmid%d!\n",
175 mac_cb
->dsaf_dev
->ae_dev
.name
,
176 mac_cb
->mac_id
, vmid
);
180 dev_err(mac_cb
->dev
, "dsaf mode invalid,%s mac%d!\n",
181 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
185 if (vmid
>= mac_cb
->dsaf_dev
->rcb_common
[0]->max_vfn
) {
186 dev_err(mac_cb
->dev
, "input invalid,%s mac%d vmid%d !\n",
187 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vmid
);
191 switch (mac_cb
->dsaf_dev
->dsaf_mode
) {
192 case DSAF_MODE_ENABLE_FIX
:
195 case DSAF_MODE_DISABLE_FIX
:
198 case DSAF_MODE_ENABLE_0VM
:
199 case DSAF_MODE_ENABLE_8VM
:
200 case DSAF_MODE_ENABLE_16VM
:
201 case DSAF_MODE_ENABLE_32VM
:
202 case DSAF_MODE_ENABLE_128VM
:
203 case DSAF_MODE_DISABLE_2PORT_8VM
:
204 case DSAF_MODE_DISABLE_2PORT_16VM
:
205 case DSAF_MODE_DISABLE_2PORT_64VM
:
206 case DSAF_MODE_DISABLE_6PORT_0VM
:
207 case DSAF_MODE_DISABLE_6PORT_2VM
:
208 case DSAF_MODE_DISABLE_6PORT_4VM
:
209 case DSAF_MODE_DISABLE_6PORT_16VM
:
213 dev_err(mac_cb
->dev
, "dsaf mode invalid,%s mac%d!\n",
214 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
217 tmp_port
+= DSAF_BASE_INNER_PORT_NUM
;
219 *port_num
= tmp_port
;
225 *hns_mac_change_vf_addr - change vf mac address
230 int hns_mac_change_vf_addr(struct hns_mac_cb
*mac_cb
,
231 u32 vmid
, char *addr
)
234 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
235 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
236 struct dsaf_drv_mac_single_dest_entry mac_entry
;
237 struct mac_entry_idx
*old_entry
;
239 old_entry
= &mac_cb
->addr_entry_idx
[vmid
];
240 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
241 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
242 mac_entry
.in_vlan_id
= old_entry
->vlan_id
;
243 mac_entry
.in_port_num
= mac_cb
->mac_id
;
244 ret
= hns_mac_get_inner_port_num(mac_cb
, (u8
)vmid
,
245 &mac_entry
.port_num
);
249 if ((old_entry
->valid
!= 0) &&
250 (memcmp(old_entry
->addr
,
251 addr
, sizeof(mac_entry
.addr
)) != 0)) {
252 ret
= hns_dsaf_del_mac_entry(dsaf_dev
,
260 ret
= hns_dsaf_set_mac_uc_entry(dsaf_dev
, &mac_entry
);
265 if ((mac_ctrl_drv
->set_mac_addr
) && (vmid
== 0))
266 mac_ctrl_drv
->set_mac_addr(mac_cb
->priv
.mac
, addr
);
268 memcpy(old_entry
->addr
, addr
, sizeof(old_entry
->addr
));
269 old_entry
->valid
= 1;
273 int hns_mac_set_multi(struct hns_mac_cb
*mac_cb
,
274 u32 port_num
, char *addr
, bool enable
)
277 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
278 struct dsaf_drv_mac_single_dest_entry mac_entry
;
280 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
) && addr
) {
281 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
282 mac_entry
.in_vlan_id
= 0;/*vlan_id;*/
283 mac_entry
.in_port_num
= mac_cb
->mac_id
;
284 mac_entry
.port_num
= port_num
;
287 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
289 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
291 dev_err(dsaf_dev
->dev
,
292 "set mac mc port failed,%s mac%d ret = %#x!\n",
293 mac_cb
->dsaf_dev
->ae_dev
.name
,
294 mac_cb
->mac_id
, ret
);
303 *hns_mac_del_mac - delete mac address into dsaf table,can't delete the same
305 *@net_dev: net device
310 int hns_mac_del_mac(struct hns_mac_cb
*mac_cb
, u32 vfn
, char *mac
)
312 struct mac_entry_idx
*old_mac
;
313 struct dsaf_device
*dsaf_dev
;
316 dsaf_dev
= mac_cb
->dsaf_dev
;
318 if (vfn
< DSAF_MAX_VM_NUM
) {
319 old_mac
= &mac_cb
->addr_entry_idx
[vfn
];
322 "vf queue is too large,%s mac%d queue = %#x!\n",
323 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vfn
);
328 ret
= hns_dsaf_del_mac_entry(dsaf_dev
, old_mac
->vlan_id
,
329 mac_cb
->mac_id
, old_mac
->addr
);
333 if (memcmp(old_mac
->addr
, mac
, sizeof(old_mac
->addr
)) == 0)
340 static void hns_mac_param_get(struct mac_params
*param
,
341 struct hns_mac_cb
*mac_cb
)
343 param
->vaddr
= (void *)mac_cb
->vaddr
;
344 param
->mac_mode
= hns_get_enet_interface(mac_cb
);
345 memcpy(param
->addr
, mac_cb
->addr_entry_idx
[0].addr
,
346 MAC_NUM_OCTETS_PER_ADDR
);
347 param
->mac_id
= mac_cb
->mac_id
;
348 param
->dev
= mac_cb
->dev
;
352 *hns_mac_queue_config_bc_en - set broadcast rx&tx enable
354 *@queue: queue number
356 *retuen 0 - success , negative --fail
358 static int hns_mac_port_config_bc_en(struct hns_mac_cb
*mac_cb
,
359 u32 port_num
, u16 vlan_id
, bool enable
)
362 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
363 u8 addr
[MAC_NUM_OCTETS_PER_ADDR
]
364 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
365 struct dsaf_drv_mac_single_dest_entry mac_entry
;
367 /* directy return ok in debug network mode */
368 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
371 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
372 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
373 mac_entry
.in_vlan_id
= vlan_id
;
374 mac_entry
.in_port_num
= mac_cb
->mac_id
;
375 mac_entry
.port_num
= port_num
;
378 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
380 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
388 *hns_mac_vm_config_bc_en - set broadcast rx&tx enable
392 *retuen 0 - success , negative --fail
394 int hns_mac_vm_config_bc_en(struct hns_mac_cb
*mac_cb
, u32 vmid
, bool enable
)
397 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
399 u8 addr
[MAC_NUM_OCTETS_PER_ADDR
]
400 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
401 struct mac_entry_idx
*uc_mac_entry
;
402 struct dsaf_drv_mac_single_dest_entry mac_entry
;
404 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
407 uc_mac_entry
= &mac_cb
->addr_entry_idx
[vmid
];
409 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
410 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
411 mac_entry
.in_vlan_id
= uc_mac_entry
->vlan_id
;
412 mac_entry
.in_port_num
= mac_cb
->mac_id
;
413 ret
= hns_mac_get_inner_port_num(mac_cb
, vmid
, &port_num
);
416 mac_entry
.port_num
= port_num
;
419 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
421 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
428 void hns_mac_reset(struct hns_mac_cb
*mac_cb
)
430 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
431 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
435 if (drv
->config_max_frame_length
)
436 drv
->config_max_frame_length(drv
, mac_cb
->max_frm
);
438 if (drv
->set_tx_auto_pause_frames
)
439 drv
->set_tx_auto_pause_frames(drv
, mac_cb
->tx_pause_frm_time
);
441 if (drv
->set_an_mode
)
442 drv
->set_an_mode(drv
, 1);
444 if (drv
->mac_pausefrm_cfg
) {
445 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
446 drv
->mac_pausefrm_cfg(drv
, !is_ver1
, !is_ver1
);
447 else /* mac rx must disable, dsaf pfc close instead of it*/
448 drv
->mac_pausefrm_cfg(drv
, 0, 1);
452 int hns_mac_set_mtu(struct hns_mac_cb
*mac_cb
, u32 new_mtu
)
454 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
455 u32 buf_size
= mac_cb
->dsaf_dev
->buf_size
;
456 u32 new_frm
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
457 u32 max_frm
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
) ?
458 MAC_MAX_MTU
: MAC_MAX_MTU_V2
;
460 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
461 max_frm
= MAC_MAX_MTU_DBG
;
463 if ((new_mtu
< MAC_MIN_MTU
) || (new_frm
> max_frm
) ||
464 (new_frm
> HNS_RCB_RING_MAX_BD_PER_PKT
* buf_size
))
467 if (!drv
->config_max_frame_length
)
470 /* adjust max frame to be at least the size of a standard frame */
471 if (new_frm
< (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
))
472 new_frm
= (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
474 drv
->config_max_frame_length(drv
, new_frm
);
476 mac_cb
->max_frm
= new_frm
;
481 void hns_mac_start(struct hns_mac_cb
*mac_cb
)
483 struct mac_driver
*mac_drv
= hns_mac_get_drv(mac_cb
);
486 if (mac_drv
->mac_en_flg
== MAC_EN_FLAG_V
) {
487 /*plus 1 when the virtual mac has been enabled */
488 mac_drv
->virt_dev_num
+= 1;
492 if (mac_drv
->mac_enable
) {
493 mac_drv
->mac_enable(mac_cb
->priv
.mac
, MAC_COMM_MODE_RX_AND_TX
);
494 mac_drv
->mac_en_flg
= MAC_EN_FLAG_V
;
498 void hns_mac_stop(struct hns_mac_cb
*mac_cb
)
500 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
502 /*modified for virtualization */
503 if (mac_ctrl_drv
->virt_dev_num
> 0) {
504 mac_ctrl_drv
->virt_dev_num
-= 1;
505 if (mac_ctrl_drv
->virt_dev_num
> 0)
509 if (mac_ctrl_drv
->mac_disable
)
510 mac_ctrl_drv
->mac_disable(mac_cb
->priv
.mac
,
511 MAC_COMM_MODE_RX_AND_TX
);
513 mac_ctrl_drv
->mac_en_flg
= 0;
515 mac_cb
->dsaf_dev
->misc_op
->cpld_reset_led(mac_cb
);
519 * hns_mac_get_autoneg - get auto autonegotiation
520 * @mac_cb: mac control block
521 * @enable: enable or not
522 * retuen 0 - success , negative --fail
524 void hns_mac_get_autoneg(struct hns_mac_cb
*mac_cb
, u32
*auto_neg
)
526 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
528 if (mac_ctrl_drv
->autoneg_stat
)
529 mac_ctrl_drv
->autoneg_stat(mac_ctrl_drv
, auto_neg
);
535 * hns_mac_get_pauseparam - set rx & tx pause parameter
536 * @mac_cb: mac control block
537 * @rx_en: rx enable status
538 * @tx_en: tx enable status
539 * retuen 0 - success , negative --fail
541 void hns_mac_get_pauseparam(struct hns_mac_cb
*mac_cb
, u32
*rx_en
, u32
*tx_en
)
543 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
545 if (mac_ctrl_drv
->get_pause_enable
) {
546 mac_ctrl_drv
->get_pause_enable(mac_ctrl_drv
, rx_en
, tx_en
);
554 * hns_mac_set_autoneg - set auto autonegotiation
555 * @mac_cb: mac control block
556 * @enable: enable or not
557 * retuen 0 - success , negative --fail
559 int hns_mac_set_autoneg(struct hns_mac_cb
*mac_cb
, u8 enable
)
561 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
563 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
&& enable
) {
564 dev_err(mac_cb
->dev
, "enable autoneg is not allowed!");
568 if (mac_ctrl_drv
->set_an_mode
)
569 mac_ctrl_drv
->set_an_mode(mac_ctrl_drv
, enable
);
575 * hns_mac_set_autoneg - set rx & tx pause parameter
576 * @mac_cb: mac control block
577 * @rx_en: rx enable or not
578 * @tx_en: tx enable or not
579 * return 0 - success , negative --fail
581 int hns_mac_set_pauseparam(struct hns_mac_cb
*mac_cb
, u32 rx_en
, u32 tx_en
)
583 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
584 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
586 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
) {
587 if (is_ver1
&& (tx_en
|| rx_en
)) {
588 dev_err(mac_cb
->dev
, "macv1 cann't enable tx/rx_pause!");
593 if (mac_ctrl_drv
->mac_pausefrm_cfg
)
594 mac_ctrl_drv
->mac_pausefrm_cfg(mac_ctrl_drv
, rx_en
, tx_en
);
600 * hns_mac_init_ex - mac init
601 * @mac_cb: mac control block
602 * retuen 0 - success , negative --fail
604 static int hns_mac_init_ex(struct hns_mac_cb
*mac_cb
)
607 struct mac_params param
;
608 struct mac_driver
*drv
;
610 hns_dsaf_fix_mac_mode(mac_cb
);
612 memset(¶m
, 0, sizeof(struct mac_params
));
613 hns_mac_param_get(¶m
, mac_cb
);
615 if (MAC_SPEED_FROM_MODE(param
.mac_mode
) < MAC_SPEED_10000
)
616 drv
= (struct mac_driver
*)hns_gmac_config(mac_cb
, ¶m
);
618 drv
= (struct mac_driver
*)hns_xgmac_config(mac_cb
, ¶m
);
623 mac_cb
->priv
.mac
= (void *)drv
;
624 hns_mac_reset(mac_cb
);
626 hns_mac_adjust_link(mac_cb
, mac_cb
->speed
, !mac_cb
->half_duplex
);
628 ret
= hns_mac_port_config_bc_en(mac_cb
, mac_cb
->mac_id
, 0, true);
635 drv
->mac_free(mac_cb
->priv
.mac
);
636 mac_cb
->priv
.mac
= NULL
;
642 *hns_mac_get_info - get mac information from device node
645 * return: 0 --success, negative --fail
647 static int hns_mac_get_info(struct hns_mac_cb
*mac_cb
)
649 struct device_node
*np
;
650 struct regmap
*syscon
;
651 struct of_phandle_args cpld_args
;
654 mac_cb
->link
= false;
655 mac_cb
->half_duplex
= false;
656 mac_cb
->speed
= mac_phy_to_speed
[mac_cb
->phy_if
];
657 mac_cb
->max_speed
= mac_cb
->speed
;
659 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
660 mac_cb
->if_support
= MAC_GMAC_SUPPORTED
;
661 mac_cb
->if_support
|= SUPPORTED_1000baseT_Full
;
662 } else if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
663 mac_cb
->if_support
= SUPPORTED_10000baseR_FEC
;
664 mac_cb
->if_support
|= SUPPORTED_10000baseKR_Full
;
667 mac_cb
->max_frm
= MAC_DEFAULT_MTU
;
668 mac_cb
->tx_pause_frm_time
= MAC_DEFAULT_PAUSE_TIME
;
669 mac_cb
->port_rst_off
= mac_cb
->mac_id
;
670 mac_cb
->port_mode_off
= 0;
672 /* if the dsaf node doesn't contain a port subnode, get phy-handle
675 if (!mac_cb
->fw_port
) {
676 np
= of_parse_phandle(mac_cb
->dev
->of_node
, "phy-handle",
678 mac_cb
->phy_dev
= of_phy_find_device(np
);
679 if (mac_cb
->phy_dev
) {
680 /* refcount is held by of_phy_find_device()
681 * if the phy_dev is found
683 put_device(&mac_cb
->phy_dev
->mdio
.dev
);
685 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
686 mac_cb
->mac_id
, np
->name
);
692 if (!is_of_node(mac_cb
->fw_port
))
695 /* parse property from port subnode in dsaf */
696 np
= of_parse_phandle(to_of_node(mac_cb
->fw_port
), "phy-handle", 0);
697 mac_cb
->phy_dev
= of_phy_find_device(np
);
698 if (mac_cb
->phy_dev
) {
699 put_device(&mac_cb
->phy_dev
->mdio
.dev
);
700 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
701 mac_cb
->mac_id
, np
->name
);
704 syscon
= syscon_node_to_regmap(
705 of_parse_phandle(to_of_node(mac_cb
->fw_port
),
706 "serdes-syscon", 0));
707 if (IS_ERR_OR_NULL(syscon
)) {
708 dev_err(mac_cb
->dev
, "serdes-syscon is needed!\n");
711 mac_cb
->serdes_ctrl
= syscon
;
713 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
715 &mac_cb
->port_rst_off
);
718 "mac%d port-rst-offset not found, use default value.\n",
722 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
724 &mac_cb
->port_mode_off
);
727 "mac%d port-mode-offset not found, use default value.\n",
731 ret
= of_parse_phandle_with_fixed_args(to_of_node(mac_cb
->fw_port
),
732 "cpld-syscon", 1, 0, &cpld_args
);
734 dev_dbg(mac_cb
->dev
, "mac%d no cpld-syscon found.\n",
736 mac_cb
->cpld_ctrl
= NULL
;
738 syscon
= syscon_node_to_regmap(cpld_args
.np
);
739 if (IS_ERR_OR_NULL(syscon
)) {
740 dev_dbg(mac_cb
->dev
, "no cpld-syscon found!\n");
741 mac_cb
->cpld_ctrl
= NULL
;
743 mac_cb
->cpld_ctrl
= syscon
;
744 mac_cb
->cpld_ctrl_reg
= cpld_args
.args
[0];
752 * hns_mac_get_mode - get mac mode
753 * @phy_if: phy interface
754 * retuen 0 - gmac, 1 - xgmac , negative --fail
756 static int hns_mac_get_mode(phy_interface_t phy_if
)
759 case PHY_INTERFACE_MODE_SGMII
:
761 case PHY_INTERFACE_MODE_XGMII
:
762 return MAC_XGMAC_IDX
;
768 u8 __iomem
*hns_mac_get_vaddr(struct dsaf_device
*dsaf_dev
,
769 struct hns_mac_cb
*mac_cb
, u32 mac_mode_idx
)
771 u8 __iomem
*base
= dsaf_dev
->io_base
;
772 int mac_id
= mac_cb
->mac_id
;
774 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
775 return base
+ 0x40000 + mac_id
* 0x4000 -
776 mac_mode_idx
* 0x20000;
778 return dsaf_dev
->ppe_base
+ 0x1000;
782 * hns_mac_get_cfg - get mac cfg from dtb or acpi table
783 * @dsaf_dev: dsa fabric device struct pointer
784 * @mac_cb: mac control block
785 * return 0 - success , negative --fail
787 int hns_mac_get_cfg(struct dsaf_device
*dsaf_dev
, struct hns_mac_cb
*mac_cb
)
792 mac_cb
->dsaf_dev
= dsaf_dev
;
793 mac_cb
->dev
= dsaf_dev
->dev
;
795 mac_cb
->sys_ctl_vaddr
= dsaf_dev
->sc_base
;
796 mac_cb
->serdes_vaddr
= dsaf_dev
->sds_base
;
798 mac_cb
->sfp_prsnt
= 0;
799 mac_cb
->txpkt_for_led
= 0;
800 mac_cb
->rxpkt_for_led
= 0;
802 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
))
803 mac_cb
->mac_type
= HNAE_PORT_SERVICE
;
805 mac_cb
->mac_type
= HNAE_PORT_DEBUG
;
807 mac_cb
->phy_if
= dsaf_dev
->misc_op
->get_phy_if(mac_cb
);
809 ret
= hns_mac_get_mode(mac_cb
->phy_if
);
811 dev_err(dsaf_dev
->dev
,
812 "hns_mac_get_mode failed,mac%d ret = %#x!\n",
813 mac_cb
->mac_id
, ret
);
816 mac_mode_idx
= (u32
)ret
;
818 ret
= hns_mac_get_info(mac_cb
);
822 mac_cb
->dsaf_dev
->misc_op
->cpld_reset_led(mac_cb
);
823 mac_cb
->vaddr
= hns_mac_get_vaddr(dsaf_dev
, mac_cb
, mac_mode_idx
);
828 static int hns_mac_get_max_port_num(struct dsaf_device
*dsaf_dev
)
830 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
833 return DSAF_MAX_PORT_NUM
;
837 * hns_mac_init - init mac
838 * @dsaf_dev: dsa fabric device struct pointer
839 * return 0 - success , negative --fail
841 int hns_mac_init(struct dsaf_device
*dsaf_dev
)
846 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
847 struct hns_mac_cb
*mac_cb
;
848 struct fwnode_handle
*child
;
850 device_for_each_child_node(dsaf_dev
->dev
, child
) {
851 ret
= fwnode_property_read_u32(child
, "reg", &port_id
);
853 dev_err(dsaf_dev
->dev
,
854 "get reg fail, ret=%d!\n", ret
);
857 if (port_id
>= max_port_num
) {
858 dev_err(dsaf_dev
->dev
,
859 "reg(%u) out of range!\n", port_id
);
862 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
866 mac_cb
->fw_port
= child
;
867 mac_cb
->mac_id
= (u8
)port_id
;
868 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
872 /* if don't get any port subnode from dsaf node
873 * will init all port then, this is compatible with the old dts
876 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
877 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
882 mac_cb
->mac_id
= port_id
;
883 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
886 /* init mac_cb for all port */
887 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
888 mac_cb
= dsaf_dev
->mac_cb
[port_id
];
892 ret
= hns_mac_get_cfg(dsaf_dev
, mac_cb
);
895 ret
= hns_mac_init_ex(mac_cb
);
903 void hns_mac_uninit(struct dsaf_device
*dsaf_dev
)
906 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
908 for (i
= 0; i
< max_port_num
; i
++) {
909 dsaf_dev
->misc_op
->cpld_reset_led(dsaf_dev
->mac_cb
[i
]);
910 dsaf_dev
->mac_cb
[i
] = NULL
;
914 int hns_mac_config_mac_loopback(struct hns_mac_cb
*mac_cb
,
915 enum hnae_loop loop
, int en
)
918 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
920 if (drv
->config_loopback
)
921 ret
= drv
->config_loopback(drv
, loop
, en
);
928 void hns_mac_update_stats(struct hns_mac_cb
*mac_cb
)
930 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
932 mac_ctrl_drv
->update_stats(mac_ctrl_drv
);
935 void hns_mac_get_stats(struct hns_mac_cb
*mac_cb
, u64
*data
)
937 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
939 mac_ctrl_drv
->get_ethtool_stats(mac_ctrl_drv
, data
);
942 void hns_mac_get_strings(struct hns_mac_cb
*mac_cb
,
943 int stringset
, u8
*data
)
945 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
947 mac_ctrl_drv
->get_strings(stringset
, data
);
950 int hns_mac_get_sset_count(struct hns_mac_cb
*mac_cb
, int stringset
)
952 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
954 return mac_ctrl_drv
->get_sset_count(stringset
);
957 void hns_mac_set_promisc(struct hns_mac_cb
*mac_cb
, u8 en
)
959 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
961 if (mac_ctrl_drv
->set_promiscuous
)
962 mac_ctrl_drv
->set_promiscuous(mac_ctrl_drv
, en
);
965 int hns_mac_get_regs_count(struct hns_mac_cb
*mac_cb
)
967 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
969 return mac_ctrl_drv
->get_regs_count();
972 void hns_mac_get_regs(struct hns_mac_cb
*mac_cb
, void *data
)
974 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
976 mac_ctrl_drv
->get_regs(mac_ctrl_drv
, data
);
979 void hns_set_led_opt(struct hns_mac_cb
*mac_cb
)
984 txpkts
= mac_cb
->txpkt_for_led
- mac_cb
->hw_stats
.tx_good_pkts
;
985 rxpkts
= mac_cb
->rxpkt_for_led
- mac_cb
->hw_stats
.rx_good_pkts
;
986 if (txpkts
|| rxpkts
)
990 mac_cb
->txpkt_for_led
= mac_cb
->hw_stats
.tx_good_pkts
;
991 mac_cb
->rxpkt_for_led
= mac_cb
->hw_stats
.rx_good_pkts
;
992 mac_cb
->dsaf_dev
->misc_op
->cpld_set_led(mac_cb
, (int)mac_cb
->link
,
993 mac_cb
->speed
, nic_data
);
996 int hns_cpld_led_set_id(struct hns_mac_cb
*mac_cb
,
997 enum hnae_led_state status
)
999 if (!mac_cb
|| !mac_cb
->cpld_ctrl
)
1002 return mac_cb
->dsaf_dev
->misc_op
->cpld_set_led_id(mac_cb
, status
);