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/acpi.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
18 #include <linux/of_address.h>
19 #include <linux/of_mdio.h>
20 #include <linux/phy.h>
21 #include <linux/platform_device.h>
23 #include "hns_dsaf_main.h"
24 #include "hns_dsaf_misc.h"
25 #include "hns_dsaf_rcb.h"
27 #define MAC_EN_FLAG_V 0xada0328
29 static const u16 mac_phy_to_speed
[] = {
30 [PHY_INTERFACE_MODE_MII
] = MAC_SPEED_100
,
31 [PHY_INTERFACE_MODE_GMII
] = MAC_SPEED_1000
,
32 [PHY_INTERFACE_MODE_SGMII
] = MAC_SPEED_1000
,
33 [PHY_INTERFACE_MODE_TBI
] = MAC_SPEED_1000
,
34 [PHY_INTERFACE_MODE_RMII
] = MAC_SPEED_100
,
35 [PHY_INTERFACE_MODE_RGMII
] = MAC_SPEED_1000
,
36 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_SPEED_1000
,
37 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_SPEED_1000
,
38 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_SPEED_1000
,
39 [PHY_INTERFACE_MODE_RTBI
] = MAC_SPEED_1000
,
40 [PHY_INTERFACE_MODE_XGMII
] = MAC_SPEED_10000
43 static const enum mac_mode g_mac_mode_100
[] = {
44 [PHY_INTERFACE_MODE_MII
] = MAC_MODE_MII_100
,
45 [PHY_INTERFACE_MODE_RMII
] = MAC_MODE_RMII_100
48 static const enum mac_mode g_mac_mode_1000
[] = {
49 [PHY_INTERFACE_MODE_GMII
] = MAC_MODE_GMII_1000
,
50 [PHY_INTERFACE_MODE_SGMII
] = MAC_MODE_SGMII_1000
,
51 [PHY_INTERFACE_MODE_TBI
] = MAC_MODE_TBI_1000
,
52 [PHY_INTERFACE_MODE_RGMII
] = MAC_MODE_RGMII_1000
,
53 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_MODE_RGMII_1000
,
54 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_MODE_RGMII_1000
,
55 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_MODE_RGMII_1000
,
56 [PHY_INTERFACE_MODE_RTBI
] = MAC_MODE_RTBI_1000
59 static enum mac_mode
hns_get_enet_interface(const struct hns_mac_cb
*mac_cb
)
61 switch (mac_cb
->max_speed
) {
63 return g_mac_mode_100
[mac_cb
->phy_if
];
65 return g_mac_mode_1000
[mac_cb
->phy_if
];
67 return MAC_MODE_XGMII_10000
;
69 return MAC_MODE_MII_100
;
73 void hns_mac_get_link_status(struct hns_mac_cb
*mac_cb
, u32
*link_status
)
75 struct mac_driver
*mac_ctrl_drv
;
78 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
80 if (mac_ctrl_drv
->get_link_status
)
81 mac_ctrl_drv
->get_link_status(mac_ctrl_drv
, link_status
);
85 if (mac_cb
->media_type
== HNAE_MEDIA_TYPE_FIBER
) {
86 ret
= mac_cb
->dsaf_dev
->misc_op
->get_sfp_prsnt(mac_cb
,
89 *link_status
= *link_status
&& sfp_prsnt
;
92 mac_cb
->link
= *link_status
;
95 int hns_mac_get_port_info(struct hns_mac_cb
*mac_cb
,
96 u8
*auto_neg
, u16
*speed
, u8
*duplex
)
98 struct mac_driver
*mac_ctrl_drv
;
101 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
103 if (!mac_ctrl_drv
->get_info
)
106 mac_ctrl_drv
->get_info(mac_ctrl_drv
, &info
);
108 *auto_neg
= info
.auto_neg
;
112 *duplex
= info
.duplex
;
117 void hns_mac_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
)
120 struct mac_driver
*mac_ctrl_drv
;
122 mac_ctrl_drv
= (struct mac_driver
*)(mac_cb
->priv
.mac
);
124 mac_cb
->speed
= speed
;
125 mac_cb
->half_duplex
= !duplex
;
127 if (mac_ctrl_drv
->adjust_link
) {
128 ret
= mac_ctrl_drv
->adjust_link(mac_ctrl_drv
,
129 (enum mac_speed
)speed
, duplex
);
132 "adjust_link failed, %s mac%d ret = %#x!\n",
133 mac_cb
->dsaf_dev
->ae_dev
.name
,
134 mac_cb
->mac_id
, ret
);
141 *hns_mac_get_inner_port_num - get mac table inner port number
144 *@port_num:port number
147 int hns_mac_get_inner_port_num(struct hns_mac_cb
*mac_cb
, u8 vmid
, u8
*port_num
)
149 int q_num_per_vf
, vf_num_per_port
;
153 if (mac_cb
->dsaf_dev
->dsaf_mode
<= DSAF_MODE_ENABLE
) {
154 if (mac_cb
->mac_id
!= DSAF_MAX_PORT_NUM
) {
156 "input invalid, %s mac%d vmid%d !\n",
157 mac_cb
->dsaf_dev
->ae_dev
.name
,
158 mac_cb
->mac_id
, vmid
);
161 } else if (mac_cb
->dsaf_dev
->dsaf_mode
< DSAF_MODE_MAX
) {
162 if (mac_cb
->mac_id
>= DSAF_MAX_PORT_NUM
) {
164 "input invalid, %s mac%d vmid%d!\n",
165 mac_cb
->dsaf_dev
->ae_dev
.name
,
166 mac_cb
->mac_id
, vmid
);
170 dev_err(mac_cb
->dev
, "dsaf mode invalid, %s mac%d!\n",
171 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
175 if (vmid
>= mac_cb
->dsaf_dev
->rcb_common
[0]->max_vfn
) {
176 dev_err(mac_cb
->dev
, "input invalid, %s mac%d vmid%d !\n",
177 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vmid
);
181 q_num_per_vf
= mac_cb
->dsaf_dev
->rcb_common
[0]->max_q_per_vf
;
182 vf_num_per_port
= mac_cb
->dsaf_dev
->rcb_common
[0]->max_vfn
;
184 vm_queue_id
= vmid
* q_num_per_vf
+
185 vf_num_per_port
* q_num_per_vf
* mac_cb
->mac_id
;
187 switch (mac_cb
->dsaf_dev
->dsaf_mode
) {
188 case DSAF_MODE_ENABLE_FIX
:
191 case DSAF_MODE_DISABLE_FIX
:
194 case DSAF_MODE_ENABLE_0VM
:
195 case DSAF_MODE_ENABLE_8VM
:
196 case DSAF_MODE_ENABLE_16VM
:
197 case DSAF_MODE_ENABLE_32VM
:
198 case DSAF_MODE_ENABLE_128VM
:
199 case DSAF_MODE_DISABLE_2PORT_8VM
:
200 case DSAF_MODE_DISABLE_2PORT_16VM
:
201 case DSAF_MODE_DISABLE_2PORT_64VM
:
202 case DSAF_MODE_DISABLE_6PORT_0VM
:
203 case DSAF_MODE_DISABLE_6PORT_2VM
:
204 case DSAF_MODE_DISABLE_6PORT_4VM
:
205 case DSAF_MODE_DISABLE_6PORT_16VM
:
206 tmp_port
= vm_queue_id
;
209 dev_err(mac_cb
->dev
, "dsaf mode invalid, %s mac%d!\n",
210 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
213 tmp_port
+= DSAF_BASE_INNER_PORT_NUM
;
215 *port_num
= tmp_port
;
221 *hns_mac_change_vf_addr - change vf mac address
226 int hns_mac_change_vf_addr(struct hns_mac_cb
*mac_cb
,
227 u32 vmid
, char *addr
)
230 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
231 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
232 struct dsaf_drv_mac_single_dest_entry mac_entry
;
233 struct mac_entry_idx
*old_entry
;
235 old_entry
= &mac_cb
->addr_entry_idx
[vmid
];
236 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
237 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
238 mac_entry
.in_vlan_id
= old_entry
->vlan_id
;
239 mac_entry
.in_port_num
= mac_cb
->mac_id
;
240 ret
= hns_mac_get_inner_port_num(mac_cb
, (u8
)vmid
,
241 &mac_entry
.port_num
);
245 if ((old_entry
->valid
!= 0) &&
246 (memcmp(old_entry
->addr
,
247 addr
, sizeof(mac_entry
.addr
)) != 0)) {
248 ret
= hns_dsaf_del_mac_entry(dsaf_dev
,
256 ret
= hns_dsaf_set_mac_uc_entry(dsaf_dev
, &mac_entry
);
261 if ((mac_ctrl_drv
->set_mac_addr
) && (vmid
== 0))
262 mac_ctrl_drv
->set_mac_addr(mac_cb
->priv
.mac
, addr
);
264 memcpy(old_entry
->addr
, addr
, sizeof(old_entry
->addr
));
265 old_entry
->valid
= 1;
269 int hns_mac_add_uc_addr(struct hns_mac_cb
*mac_cb
, u8 vf_id
,
270 const unsigned char *addr
)
272 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
273 struct dsaf_drv_mac_single_dest_entry mac_entry
;
276 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
279 memset(&mac_entry
, 0, sizeof(mac_entry
));
280 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
281 mac_entry
.in_port_num
= mac_cb
->mac_id
;
282 ret
= hns_mac_get_inner_port_num(mac_cb
, vf_id
, &mac_entry
.port_num
);
286 return hns_dsaf_set_mac_uc_entry(dsaf_dev
, &mac_entry
);
289 int hns_mac_rm_uc_addr(struct hns_mac_cb
*mac_cb
, u8 vf_id
,
290 const unsigned char *addr
)
292 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
293 struct dsaf_drv_mac_single_dest_entry mac_entry
;
296 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
299 memset(&mac_entry
, 0, sizeof(mac_entry
));
300 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
301 mac_entry
.in_port_num
= mac_cb
->mac_id
;
302 ret
= hns_mac_get_inner_port_num(mac_cb
, vf_id
, &mac_entry
.port_num
);
306 return hns_dsaf_rm_mac_addr(dsaf_dev
, &mac_entry
);
309 int hns_mac_set_multi(struct hns_mac_cb
*mac_cb
,
310 u32 port_num
, char *addr
, bool enable
)
313 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
314 struct dsaf_drv_mac_single_dest_entry mac_entry
;
316 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
) && addr
) {
317 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
318 mac_entry
.in_vlan_id
= 0;/*vlan_id;*/
319 mac_entry
.in_port_num
= mac_cb
->mac_id
;
320 mac_entry
.port_num
= port_num
;
323 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
325 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
327 dev_err(dsaf_dev
->dev
,
328 "set mac mc port failed, %s mac%d ret = %#x!\n",
329 mac_cb
->dsaf_dev
->ae_dev
.name
,
330 mac_cb
->mac_id
, ret
);
339 *hns_mac_del_mac - delete mac address into dsaf table,can't delete the same
341 *@net_dev: net device
346 int hns_mac_del_mac(struct hns_mac_cb
*mac_cb
, u32 vfn
, char *mac
)
348 struct mac_entry_idx
*old_mac
;
349 struct dsaf_device
*dsaf_dev
;
352 dsaf_dev
= mac_cb
->dsaf_dev
;
354 if (vfn
< DSAF_MAX_VM_NUM
) {
355 old_mac
= &mac_cb
->addr_entry_idx
[vfn
];
358 "vf queue is too large, %s mac%d queue = %#x!\n",
359 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vfn
);
364 ret
= hns_dsaf_del_mac_entry(dsaf_dev
, old_mac
->vlan_id
,
365 mac_cb
->mac_id
, old_mac
->addr
);
369 if (memcmp(old_mac
->addr
, mac
, sizeof(old_mac
->addr
)) == 0)
376 int hns_mac_clr_multicast(struct hns_mac_cb
*mac_cb
, int vfn
)
378 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
380 int ret
= hns_mac_get_inner_port_num(mac_cb
, vfn
, &port_num
);
385 return hns_dsaf_clr_mac_mc_port(dsaf_dev
, mac_cb
->mac_id
, port_num
);
388 static void hns_mac_param_get(struct mac_params
*param
,
389 struct hns_mac_cb
*mac_cb
)
391 param
->vaddr
= (void *)mac_cb
->vaddr
;
392 param
->mac_mode
= hns_get_enet_interface(mac_cb
);
393 ether_addr_copy(param
->addr
, mac_cb
->addr_entry_idx
[0].addr
);
394 param
->mac_id
= mac_cb
->mac_id
;
395 param
->dev
= mac_cb
->dev
;
399 *hns_mac_queue_config_bc_en - set broadcast rx&tx enable
401 *@queue: queue number
403 *retuen 0 - success , negative --fail
405 static int hns_mac_port_config_bc_en(struct hns_mac_cb
*mac_cb
,
406 u32 port_num
, u16 vlan_id
, bool enable
)
409 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
410 u8 addr
[ETH_ALEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
411 struct dsaf_drv_mac_single_dest_entry mac_entry
;
413 /* directy return ok in debug network mode */
414 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
417 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
418 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
419 mac_entry
.in_vlan_id
= vlan_id
;
420 mac_entry
.in_port_num
= mac_cb
->mac_id
;
421 mac_entry
.port_num
= port_num
;
424 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
426 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
434 *hns_mac_vm_config_bc_en - set broadcast rx&tx enable
438 *retuen 0 - success , negative --fail
440 int hns_mac_vm_config_bc_en(struct hns_mac_cb
*mac_cb
, u32 vmid
, bool enable
)
443 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
445 u8 addr
[ETH_ALEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
446 struct mac_entry_idx
*uc_mac_entry
;
447 struct dsaf_drv_mac_single_dest_entry mac_entry
;
449 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
452 uc_mac_entry
= &mac_cb
->addr_entry_idx
[vmid
];
454 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
455 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
456 mac_entry
.in_vlan_id
= uc_mac_entry
->vlan_id
;
457 mac_entry
.in_port_num
= mac_cb
->mac_id
;
458 ret
= hns_mac_get_inner_port_num(mac_cb
, vmid
, &port_num
);
461 mac_entry
.port_num
= port_num
;
464 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
466 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
473 void hns_mac_reset(struct hns_mac_cb
*mac_cb
)
475 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
476 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
480 if (drv
->config_max_frame_length
)
481 drv
->config_max_frame_length(drv
, mac_cb
->max_frm
);
483 if (drv
->set_tx_auto_pause_frames
)
484 drv
->set_tx_auto_pause_frames(drv
, mac_cb
->tx_pause_frm_time
);
486 if (drv
->set_an_mode
)
487 drv
->set_an_mode(drv
, 1);
489 if (drv
->mac_pausefrm_cfg
) {
490 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
491 drv
->mac_pausefrm_cfg(drv
, !is_ver1
, !is_ver1
);
492 else /* mac rx must disable, dsaf pfc close instead of it*/
493 drv
->mac_pausefrm_cfg(drv
, 0, 1);
497 int hns_mac_set_mtu(struct hns_mac_cb
*mac_cb
, u32 new_mtu
, u32 buf_size
)
499 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
500 u32 new_frm
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
501 u32 max_frm
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
) ?
502 MAC_MAX_MTU
: MAC_MAX_MTU_V2
;
504 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
505 max_frm
= MAC_MAX_MTU_DBG
;
507 if (new_frm
> HNS_RCB_RING_MAX_BD_PER_PKT
* buf_size
)
510 if (!drv
->config_max_frame_length
)
513 /* adjust max frame to be at least the size of a standard frame */
514 if (new_frm
< (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
))
515 new_frm
= (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
517 drv
->config_max_frame_length(drv
, new_frm
);
519 mac_cb
->max_frm
= new_frm
;
524 void hns_mac_start(struct hns_mac_cb
*mac_cb
)
526 struct mac_driver
*mac_drv
= hns_mac_get_drv(mac_cb
);
529 if (mac_drv
->mac_en_flg
== MAC_EN_FLAG_V
) {
530 /*plus 1 when the virtual mac has been enabled */
531 mac_drv
->virt_dev_num
+= 1;
535 if (mac_drv
->mac_enable
) {
536 mac_drv
->mac_enable(mac_cb
->priv
.mac
, MAC_COMM_MODE_RX_AND_TX
);
537 mac_drv
->mac_en_flg
= MAC_EN_FLAG_V
;
541 void hns_mac_stop(struct hns_mac_cb
*mac_cb
)
543 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
545 /*modified for virtualization */
546 if (mac_ctrl_drv
->virt_dev_num
> 0) {
547 mac_ctrl_drv
->virt_dev_num
-= 1;
548 if (mac_ctrl_drv
->virt_dev_num
> 0)
552 if (mac_ctrl_drv
->mac_disable
)
553 mac_ctrl_drv
->mac_disable(mac_cb
->priv
.mac
,
554 MAC_COMM_MODE_RX_AND_TX
);
556 mac_ctrl_drv
->mac_en_flg
= 0;
558 mac_cb
->dsaf_dev
->misc_op
->cpld_reset_led(mac_cb
);
562 * hns_mac_get_autoneg - get auto autonegotiation
563 * @mac_cb: mac control block
564 * @enable: enable or not
565 * retuen 0 - success , negative --fail
567 void hns_mac_get_autoneg(struct hns_mac_cb
*mac_cb
, u32
*auto_neg
)
569 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
571 if (mac_ctrl_drv
->autoneg_stat
)
572 mac_ctrl_drv
->autoneg_stat(mac_ctrl_drv
, auto_neg
);
578 * hns_mac_get_pauseparam - set rx & tx pause parameter
579 * @mac_cb: mac control block
580 * @rx_en: rx enable status
581 * @tx_en: tx enable status
582 * retuen 0 - success , negative --fail
584 void hns_mac_get_pauseparam(struct hns_mac_cb
*mac_cb
, u32
*rx_en
, u32
*tx_en
)
586 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
588 if (mac_ctrl_drv
->get_pause_enable
) {
589 mac_ctrl_drv
->get_pause_enable(mac_ctrl_drv
, rx_en
, tx_en
);
597 * hns_mac_set_autoneg - set auto autonegotiation
598 * @mac_cb: mac control block
599 * @enable: enable or not
600 * retuen 0 - success , negative --fail
602 int hns_mac_set_autoneg(struct hns_mac_cb
*mac_cb
, u8 enable
)
604 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
606 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
&& enable
) {
607 dev_err(mac_cb
->dev
, "enabling autoneg is not allowed!\n");
611 if (mac_ctrl_drv
->set_an_mode
)
612 mac_ctrl_drv
->set_an_mode(mac_ctrl_drv
, enable
);
618 * hns_mac_set_autoneg - set rx & tx pause parameter
619 * @mac_cb: mac control block
620 * @rx_en: rx enable or not
621 * @tx_en: tx enable or not
622 * return 0 - success , negative --fail
624 int hns_mac_set_pauseparam(struct hns_mac_cb
*mac_cb
, u32 rx_en
, u32 tx_en
)
626 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
627 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
629 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
) {
630 if (is_ver1
&& (tx_en
|| rx_en
)) {
631 dev_err(mac_cb
->dev
, "macv1 can't enable tx/rx_pause!\n");
636 if (mac_ctrl_drv
->mac_pausefrm_cfg
)
637 mac_ctrl_drv
->mac_pausefrm_cfg(mac_ctrl_drv
, rx_en
, tx_en
);
643 * hns_mac_init_ex - mac init
644 * @mac_cb: mac control block
645 * retuen 0 - success , negative --fail
647 static int hns_mac_init_ex(struct hns_mac_cb
*mac_cb
)
650 struct mac_params param
;
651 struct mac_driver
*drv
;
653 hns_dsaf_fix_mac_mode(mac_cb
);
655 memset(¶m
, 0, sizeof(struct mac_params
));
656 hns_mac_param_get(¶m
, mac_cb
);
658 if (MAC_SPEED_FROM_MODE(param
.mac_mode
) < MAC_SPEED_10000
)
659 drv
= (struct mac_driver
*)hns_gmac_config(mac_cb
, ¶m
);
661 drv
= (struct mac_driver
*)hns_xgmac_config(mac_cb
, ¶m
);
666 mac_cb
->priv
.mac
= (void *)drv
;
667 hns_mac_reset(mac_cb
);
669 hns_mac_adjust_link(mac_cb
, mac_cb
->speed
, !mac_cb
->half_duplex
);
671 ret
= hns_mac_port_config_bc_en(mac_cb
, mac_cb
->mac_id
, 0, true);
678 drv
->mac_free(mac_cb
->priv
.mac
);
679 mac_cb
->priv
.mac
= NULL
;
685 hns_mac_phy_parse_addr(struct device
*dev
, struct fwnode_handle
*fwnode
)
690 ret
= fwnode_property_read_u32(fwnode
, "phy-addr", &addr
);
692 dev_err(dev
, "has invalid PHY address ret:%d\n", ret
);
696 if (addr
>= PHY_MAX_ADDR
) {
697 dev_err(dev
, "PHY address %i is too large\n", addr
);
705 hns_mac_register_phydev(struct mii_bus
*mdio
, struct hns_mac_cb
*mac_cb
,
708 struct phy_device
*phy
;
709 const char *phy_type
;
713 rc
= fwnode_property_read_string(mac_cb
->fw_port
,
714 "phy-mode", &phy_type
);
718 if (!strcmp(phy_type
, phy_modes(PHY_INTERFACE_MODE_XGMII
)))
720 else if (!strcmp(phy_type
, phy_modes(PHY_INTERFACE_MODE_SGMII
)))
725 phy
= get_phy_device(mdio
, addr
, is_c45
);
726 if (!phy
|| IS_ERR(phy
))
729 phy
->irq
= mdio
->irq
[addr
];
731 /* All data is now stored in the phy struct;
734 rc
= phy_device_register(phy
);
736 phy_device_free(phy
);
740 mac_cb
->phy_dev
= phy
;
742 dev_dbg(&mdio
->dev
, "registered phy at address %i\n", addr
);
747 static void hns_mac_register_phy(struct hns_mac_cb
*mac_cb
)
749 struct acpi_reference_args args
;
750 struct platform_device
*pdev
;
751 struct mii_bus
*mii_bus
;
755 /* Loop over the child nodes and register a phy_device for each one */
756 if (!to_acpi_device_node(mac_cb
->fw_port
))
759 rc
= acpi_node_get_property_reference(
760 mac_cb
->fw_port
, "mdio-node", 0, &args
);
764 addr
= hns_mac_phy_parse_addr(mac_cb
->dev
, mac_cb
->fw_port
);
768 /* dev address in adev */
769 pdev
= hns_dsaf_find_platform_device(acpi_fwnode_handle(args
.adev
));
770 mii_bus
= platform_get_drvdata(pdev
);
771 rc
= hns_mac_register_phydev(mii_bus
, mac_cb
, addr
);
773 dev_dbg(mac_cb
->dev
, "mac%d register phy addr:%d\n",
774 mac_cb
->mac_id
, addr
);
777 #define MAC_MEDIA_TYPE_MAX_LEN 16
779 static const struct {
780 enum hnae_media_type value
;
782 } media_type_defs
[] = {
783 {HNAE_MEDIA_TYPE_UNKNOWN
, "unknown" },
784 {HNAE_MEDIA_TYPE_FIBER
, "fiber" },
785 {HNAE_MEDIA_TYPE_COPPER
, "copper" },
786 {HNAE_MEDIA_TYPE_BACKPLANE
, "backplane" },
790 *hns_mac_get_info - get mac information from device node
793 * return: 0 --success, negative --fail
795 static int hns_mac_get_info(struct hns_mac_cb
*mac_cb
)
797 struct device_node
*np
;
798 struct regmap
*syscon
;
799 struct of_phandle_args cpld_args
;
800 const char *media_type
;
804 mac_cb
->link
= false;
805 mac_cb
->half_duplex
= false;
806 mac_cb
->media_type
= HNAE_MEDIA_TYPE_UNKNOWN
;
807 mac_cb
->speed
= mac_phy_to_speed
[mac_cb
->phy_if
];
808 mac_cb
->max_speed
= mac_cb
->speed
;
810 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
811 mac_cb
->if_support
= MAC_GMAC_SUPPORTED
;
812 mac_cb
->if_support
|= SUPPORTED_1000baseT_Full
;
813 } else if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
814 mac_cb
->if_support
= SUPPORTED_10000baseR_FEC
;
815 mac_cb
->if_support
|= SUPPORTED_10000baseKR_Full
;
818 mac_cb
->max_frm
= MAC_DEFAULT_MTU
;
819 mac_cb
->tx_pause_frm_time
= MAC_DEFAULT_PAUSE_TIME
;
820 mac_cb
->port_rst_off
= mac_cb
->mac_id
;
821 mac_cb
->port_mode_off
= 0;
823 /* if the dsaf node doesn't contain a port subnode, get phy-handle
826 if (!mac_cb
->fw_port
) {
827 np
= of_parse_phandle(mac_cb
->dev
->of_node
, "phy-handle",
829 mac_cb
->phy_dev
= of_phy_find_device(np
);
830 if (mac_cb
->phy_dev
) {
831 /* refcount is held by of_phy_find_device()
832 * if the phy_dev is found
834 put_device(&mac_cb
->phy_dev
->mdio
.dev
);
836 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
837 mac_cb
->mac_id
, np
->name
);
844 if (is_of_node(mac_cb
->fw_port
)) {
845 /* parse property from port subnode in dsaf */
846 np
= of_parse_phandle(to_of_node(mac_cb
->fw_port
),
848 mac_cb
->phy_dev
= of_phy_find_device(np
);
849 if (mac_cb
->phy_dev
) {
850 /* refcount is held by of_phy_find_device()
851 * if the phy_dev is found
853 put_device(&mac_cb
->phy_dev
->mdio
.dev
);
854 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
855 mac_cb
->mac_id
, np
->name
);
859 np
= of_parse_phandle(to_of_node(mac_cb
->fw_port
),
861 syscon
= syscon_node_to_regmap(np
);
863 if (IS_ERR_OR_NULL(syscon
)) {
864 dev_err(mac_cb
->dev
, "serdes-syscon is needed!\n");
867 mac_cb
->serdes_ctrl
= syscon
;
869 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
871 &mac_cb
->port_rst_off
);
874 "mac%d port-rst-offset not found, use default value.\n",
878 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
880 &mac_cb
->port_mode_off
);
883 "mac%d port-mode-offset not found, use default value.\n",
887 ret
= of_parse_phandle_with_fixed_args(
888 to_of_node(mac_cb
->fw_port
), "cpld-syscon", 1, 0,
891 dev_dbg(mac_cb
->dev
, "mac%d no cpld-syscon found.\n",
893 mac_cb
->cpld_ctrl
= NULL
;
895 syscon
= syscon_node_to_regmap(cpld_args
.np
);
896 if (IS_ERR_OR_NULL(syscon
)) {
897 dev_dbg(mac_cb
->dev
, "no cpld-syscon found!\n");
898 mac_cb
->cpld_ctrl
= NULL
;
900 mac_cb
->cpld_ctrl
= syscon
;
901 mac_cb
->cpld_ctrl_reg
= cpld_args
.args
[0];
904 } else if (is_acpi_node(mac_cb
->fw_port
)) {
905 hns_mac_register_phy(mac_cb
);
907 dev_err(mac_cb
->dev
, "mac%d cannot find phy node\n",
911 if (!fwnode_property_read_string(mac_cb
->fw_port
, "media-type",
913 for (i
= 0; i
< ARRAY_SIZE(media_type_defs
); i
++) {
914 if (!strncmp(media_type_defs
[i
].name
, media_type
,
915 MAC_MEDIA_TYPE_MAX_LEN
)) {
916 mac_cb
->media_type
= media_type_defs
[i
].value
;
922 if (fwnode_property_read_u8_array(mac_cb
->fw_port
, "mc-mac-mask",
923 mac_cb
->mc_mask
, ETH_ALEN
)) {
924 dev_warn(mac_cb
->dev
,
925 "no mc-mac-mask property, set to default value.\n");
926 eth_broadcast_addr(mac_cb
->mc_mask
);
933 * hns_mac_get_mode - get mac mode
934 * @phy_if: phy interface
935 * retuen 0 - gmac, 1 - xgmac , negative --fail
937 static int hns_mac_get_mode(phy_interface_t phy_if
)
940 case PHY_INTERFACE_MODE_SGMII
:
942 case PHY_INTERFACE_MODE_XGMII
:
943 return MAC_XGMAC_IDX
;
949 u8 __iomem
*hns_mac_get_vaddr(struct dsaf_device
*dsaf_dev
,
950 struct hns_mac_cb
*mac_cb
, u32 mac_mode_idx
)
952 u8 __iomem
*base
= dsaf_dev
->io_base
;
953 int mac_id
= mac_cb
->mac_id
;
955 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
956 return base
+ 0x40000 + mac_id
* 0x4000 -
957 mac_mode_idx
* 0x20000;
959 return dsaf_dev
->ppe_base
+ 0x1000;
963 * hns_mac_get_cfg - get mac cfg from dtb or acpi table
964 * @dsaf_dev: dsa fabric device struct pointer
965 * @mac_cb: mac control block
966 * return 0 - success , negative --fail
968 int hns_mac_get_cfg(struct dsaf_device
*dsaf_dev
, struct hns_mac_cb
*mac_cb
)
973 mac_cb
->dsaf_dev
= dsaf_dev
;
974 mac_cb
->dev
= dsaf_dev
->dev
;
976 mac_cb
->sys_ctl_vaddr
= dsaf_dev
->sc_base
;
977 mac_cb
->serdes_vaddr
= dsaf_dev
->sds_base
;
979 mac_cb
->sfp_prsnt
= 0;
980 mac_cb
->txpkt_for_led
= 0;
981 mac_cb
->rxpkt_for_led
= 0;
983 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
))
984 mac_cb
->mac_type
= HNAE_PORT_SERVICE
;
986 mac_cb
->mac_type
= HNAE_PORT_DEBUG
;
988 mac_cb
->phy_if
= dsaf_dev
->misc_op
->get_phy_if(mac_cb
);
990 ret
= hns_mac_get_mode(mac_cb
->phy_if
);
992 dev_err(dsaf_dev
->dev
,
993 "hns_mac_get_mode failed, mac%d ret = %#x!\n",
994 mac_cb
->mac_id
, ret
);
997 mac_mode_idx
= (u32
)ret
;
999 ret
= hns_mac_get_info(mac_cb
);
1003 mac_cb
->dsaf_dev
->misc_op
->cpld_reset_led(mac_cb
);
1004 mac_cb
->vaddr
= hns_mac_get_vaddr(dsaf_dev
, mac_cb
, mac_mode_idx
);
1009 static int hns_mac_get_max_port_num(struct dsaf_device
*dsaf_dev
)
1011 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
1014 return DSAF_MAX_PORT_NUM
;
1018 * hns_mac_init - init mac
1019 * @dsaf_dev: dsa fabric device struct pointer
1020 * return 0 - success , negative --fail
1022 int hns_mac_init(struct dsaf_device
*dsaf_dev
)
1027 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
1028 struct hns_mac_cb
*mac_cb
;
1029 struct fwnode_handle
*child
;
1031 device_for_each_child_node(dsaf_dev
->dev
, child
) {
1032 ret
= fwnode_property_read_u32(child
, "reg", &port_id
);
1034 dev_err(dsaf_dev
->dev
,
1035 "get reg fail, ret=%d!\n", ret
);
1038 if (port_id
>= max_port_num
) {
1039 dev_err(dsaf_dev
->dev
,
1040 "reg(%u) out of range!\n", port_id
);
1043 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
1047 mac_cb
->fw_port
= child
;
1048 mac_cb
->mac_id
= (u8
)port_id
;
1049 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
1053 /* if don't get any port subnode from dsaf node
1054 * will init all port then, this is compatible with the old dts
1057 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
1058 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
1063 mac_cb
->mac_id
= port_id
;
1064 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
1067 /* init mac_cb for all port */
1068 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
1069 mac_cb
= dsaf_dev
->mac_cb
[port_id
];
1073 ret
= hns_mac_get_cfg(dsaf_dev
, mac_cb
);
1076 ret
= hns_mac_init_ex(mac_cb
);
1084 void hns_mac_uninit(struct dsaf_device
*dsaf_dev
)
1087 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
1089 for (i
= 0; i
< max_port_num
; i
++) {
1090 dsaf_dev
->misc_op
->cpld_reset_led(dsaf_dev
->mac_cb
[i
]);
1091 dsaf_dev
->mac_cb
[i
] = NULL
;
1095 int hns_mac_config_mac_loopback(struct hns_mac_cb
*mac_cb
,
1096 enum hnae_loop loop
, int en
)
1099 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
1101 if (drv
->config_loopback
)
1102 ret
= drv
->config_loopback(drv
, loop
, en
);
1109 void hns_mac_update_stats(struct hns_mac_cb
*mac_cb
)
1111 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1113 mac_ctrl_drv
->update_stats(mac_ctrl_drv
);
1116 void hns_mac_get_stats(struct hns_mac_cb
*mac_cb
, u64
*data
)
1118 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1120 mac_ctrl_drv
->get_ethtool_stats(mac_ctrl_drv
, data
);
1123 void hns_mac_get_strings(struct hns_mac_cb
*mac_cb
,
1124 int stringset
, u8
*data
)
1126 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1128 mac_ctrl_drv
->get_strings(stringset
, data
);
1131 int hns_mac_get_sset_count(struct hns_mac_cb
*mac_cb
, int stringset
)
1133 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1135 return mac_ctrl_drv
->get_sset_count(stringset
);
1138 void hns_mac_set_promisc(struct hns_mac_cb
*mac_cb
, u8 en
)
1140 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1142 hns_dsaf_set_promisc_tcam(mac_cb
->dsaf_dev
, mac_cb
->mac_id
, !!en
);
1144 if (mac_ctrl_drv
->set_promiscuous
)
1145 mac_ctrl_drv
->set_promiscuous(mac_ctrl_drv
, en
);
1148 int hns_mac_get_regs_count(struct hns_mac_cb
*mac_cb
)
1150 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1152 return mac_ctrl_drv
->get_regs_count();
1155 void hns_mac_get_regs(struct hns_mac_cb
*mac_cb
, void *data
)
1157 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1159 mac_ctrl_drv
->get_regs(mac_ctrl_drv
, data
);
1162 void hns_set_led_opt(struct hns_mac_cb
*mac_cb
)
1167 txpkts
= mac_cb
->txpkt_for_led
- mac_cb
->hw_stats
.tx_good_pkts
;
1168 rxpkts
= mac_cb
->rxpkt_for_led
- mac_cb
->hw_stats
.rx_good_pkts
;
1169 if (txpkts
|| rxpkts
)
1173 mac_cb
->txpkt_for_led
= mac_cb
->hw_stats
.tx_good_pkts
;
1174 mac_cb
->rxpkt_for_led
= mac_cb
->hw_stats
.rx_good_pkts
;
1175 mac_cb
->dsaf_dev
->misc_op
->cpld_set_led(mac_cb
, (int)mac_cb
->link
,
1176 mac_cb
->speed
, nic_data
);
1179 int hns_cpld_led_set_id(struct hns_mac_cb
*mac_cb
,
1180 enum hnae_led_state status
)
1182 if (!mac_cb
|| !mac_cb
->cpld_ctrl
)
1185 return mac_cb
->dsaf_dev
->misc_op
->cpld_set_led_id(mac_cb
, status
);