1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014-2015 Hisilicon Limited.
6 #include <linux/acpi.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
14 #include <linux/of_address.h>
15 #include <linux/of_mdio.h>
16 #include <linux/phy.h>
17 #include <linux/platform_device.h>
19 #include "hns_dsaf_main.h"
20 #include "hns_dsaf_misc.h"
21 #include "hns_dsaf_rcb.h"
23 #define MAC_EN_FLAG_V 0xada0328
25 static const u16 mac_phy_to_speed
[] = {
26 [PHY_INTERFACE_MODE_MII
] = MAC_SPEED_100
,
27 [PHY_INTERFACE_MODE_GMII
] = MAC_SPEED_1000
,
28 [PHY_INTERFACE_MODE_SGMII
] = MAC_SPEED_1000
,
29 [PHY_INTERFACE_MODE_TBI
] = MAC_SPEED_1000
,
30 [PHY_INTERFACE_MODE_RMII
] = MAC_SPEED_100
,
31 [PHY_INTERFACE_MODE_RGMII
] = MAC_SPEED_1000
,
32 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_SPEED_1000
,
33 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_SPEED_1000
,
34 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_SPEED_1000
,
35 [PHY_INTERFACE_MODE_RTBI
] = MAC_SPEED_1000
,
36 [PHY_INTERFACE_MODE_XGMII
] = MAC_SPEED_10000
39 static const enum mac_mode g_mac_mode_100
[] = {
40 [PHY_INTERFACE_MODE_MII
] = MAC_MODE_MII_100
,
41 [PHY_INTERFACE_MODE_RMII
] = MAC_MODE_RMII_100
44 static const enum mac_mode g_mac_mode_1000
[] = {
45 [PHY_INTERFACE_MODE_GMII
] = MAC_MODE_GMII_1000
,
46 [PHY_INTERFACE_MODE_SGMII
] = MAC_MODE_SGMII_1000
,
47 [PHY_INTERFACE_MODE_TBI
] = MAC_MODE_TBI_1000
,
48 [PHY_INTERFACE_MODE_RGMII
] = MAC_MODE_RGMII_1000
,
49 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_MODE_RGMII_1000
,
50 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_MODE_RGMII_1000
,
51 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_MODE_RGMII_1000
,
52 [PHY_INTERFACE_MODE_RTBI
] = MAC_MODE_RTBI_1000
55 static enum mac_mode
hns_get_enet_interface(const struct hns_mac_cb
*mac_cb
)
57 switch (mac_cb
->max_speed
) {
59 return g_mac_mode_100
[mac_cb
->phy_if
];
61 return g_mac_mode_1000
[mac_cb
->phy_if
];
63 return MAC_MODE_XGMII_10000
;
65 return MAC_MODE_MII_100
;
69 void hns_mac_get_link_status(struct hns_mac_cb
*mac_cb
, u32
*link_status
)
71 struct mac_driver
*mac_ctrl_drv
;
74 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
76 if (mac_ctrl_drv
->get_link_status
)
77 mac_ctrl_drv
->get_link_status(mac_ctrl_drv
, link_status
);
81 if (mac_cb
->media_type
== HNAE_MEDIA_TYPE_FIBER
) {
82 ret
= mac_cb
->dsaf_dev
->misc_op
->get_sfp_prsnt(mac_cb
,
85 *link_status
= *link_status
&& sfp_prsnt
;
88 mac_cb
->link
= *link_status
;
91 int hns_mac_get_port_info(struct hns_mac_cb
*mac_cb
,
92 u8
*auto_neg
, u16
*speed
, u8
*duplex
)
94 struct mac_driver
*mac_ctrl_drv
;
97 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
99 if (!mac_ctrl_drv
->get_info
)
102 mac_ctrl_drv
->get_info(mac_ctrl_drv
, &info
);
104 *auto_neg
= info
.auto_neg
;
108 *duplex
= info
.duplex
;
114 *hns_mac_is_adjust_link - check is need change mac speed and duplex register
116 *@speed: phy device speed
117 *@duplex:phy device duplex
120 bool hns_mac_need_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
)
122 struct mac_driver
*mac_ctrl_drv
;
124 mac_ctrl_drv
= (struct mac_driver
*)(mac_cb
->priv
.mac
);
126 if (mac_ctrl_drv
->need_adjust_link
)
127 return mac_ctrl_drv
->need_adjust_link(mac_ctrl_drv
,
128 (enum mac_speed
)speed
, duplex
);
133 void hns_mac_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
)
136 struct mac_driver
*mac_ctrl_drv
;
138 mac_ctrl_drv
= (struct mac_driver
*)(mac_cb
->priv
.mac
);
140 mac_cb
->speed
= speed
;
141 mac_cb
->half_duplex
= !duplex
;
143 if (mac_ctrl_drv
->adjust_link
) {
144 ret
= mac_ctrl_drv
->adjust_link(mac_ctrl_drv
,
145 (enum mac_speed
)speed
, duplex
);
148 "adjust_link failed, %s mac%d ret = %#x!\n",
149 mac_cb
->dsaf_dev
->ae_dev
.name
,
150 mac_cb
->mac_id
, ret
);
157 *hns_mac_get_inner_port_num - get mac table inner port number
160 *@port_num:port number
163 int hns_mac_get_inner_port_num(struct hns_mac_cb
*mac_cb
, u8 vmid
, u8
*port_num
)
165 int q_num_per_vf
, vf_num_per_port
;
169 if (mac_cb
->dsaf_dev
->dsaf_mode
<= DSAF_MODE_ENABLE
) {
170 if (mac_cb
->mac_id
!= DSAF_MAX_PORT_NUM
) {
172 "input invalid, %s mac%d vmid%d !\n",
173 mac_cb
->dsaf_dev
->ae_dev
.name
,
174 mac_cb
->mac_id
, vmid
);
177 } else if (mac_cb
->dsaf_dev
->dsaf_mode
< DSAF_MODE_MAX
) {
178 if (mac_cb
->mac_id
>= DSAF_MAX_PORT_NUM
) {
180 "input invalid, %s mac%d vmid%d!\n",
181 mac_cb
->dsaf_dev
->ae_dev
.name
,
182 mac_cb
->mac_id
, vmid
);
186 dev_err(mac_cb
->dev
, "dsaf mode invalid, %s mac%d!\n",
187 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
191 if (vmid
>= mac_cb
->dsaf_dev
->rcb_common
[0]->max_vfn
) {
192 dev_err(mac_cb
->dev
, "input invalid, %s mac%d vmid%d !\n",
193 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vmid
);
197 q_num_per_vf
= mac_cb
->dsaf_dev
->rcb_common
[0]->max_q_per_vf
;
198 vf_num_per_port
= mac_cb
->dsaf_dev
->rcb_common
[0]->max_vfn
;
200 vm_queue_id
= vmid
* q_num_per_vf
+
201 vf_num_per_port
* q_num_per_vf
* mac_cb
->mac_id
;
203 switch (mac_cb
->dsaf_dev
->dsaf_mode
) {
204 case DSAF_MODE_ENABLE_FIX
:
207 case DSAF_MODE_DISABLE_FIX
:
210 case DSAF_MODE_ENABLE_0VM
:
211 case DSAF_MODE_ENABLE_8VM
:
212 case DSAF_MODE_ENABLE_16VM
:
213 case DSAF_MODE_ENABLE_32VM
:
214 case DSAF_MODE_ENABLE_128VM
:
215 case DSAF_MODE_DISABLE_2PORT_8VM
:
216 case DSAF_MODE_DISABLE_2PORT_16VM
:
217 case DSAF_MODE_DISABLE_2PORT_64VM
:
218 case DSAF_MODE_DISABLE_6PORT_0VM
:
219 case DSAF_MODE_DISABLE_6PORT_2VM
:
220 case DSAF_MODE_DISABLE_6PORT_4VM
:
221 case DSAF_MODE_DISABLE_6PORT_16VM
:
222 tmp_port
= vm_queue_id
;
225 dev_err(mac_cb
->dev
, "dsaf mode invalid, %s mac%d!\n",
226 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
229 tmp_port
+= DSAF_BASE_INNER_PORT_NUM
;
231 *port_num
= tmp_port
;
237 *hns_mac_change_vf_addr - change vf mac address
242 int hns_mac_change_vf_addr(struct hns_mac_cb
*mac_cb
,
243 u32 vmid
, char *addr
)
246 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
247 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
248 struct dsaf_drv_mac_single_dest_entry mac_entry
;
249 struct mac_entry_idx
*old_entry
;
251 old_entry
= &mac_cb
->addr_entry_idx
[vmid
];
252 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
253 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
254 mac_entry
.in_vlan_id
= old_entry
->vlan_id
;
255 mac_entry
.in_port_num
= mac_cb
->mac_id
;
256 ret
= hns_mac_get_inner_port_num(mac_cb
, (u8
)vmid
,
257 &mac_entry
.port_num
);
261 if ((old_entry
->valid
!= 0) &&
262 (memcmp(old_entry
->addr
,
263 addr
, sizeof(mac_entry
.addr
)) != 0)) {
264 ret
= hns_dsaf_del_mac_entry(dsaf_dev
,
272 ret
= hns_dsaf_set_mac_uc_entry(dsaf_dev
, &mac_entry
);
277 if ((mac_ctrl_drv
->set_mac_addr
) && (vmid
== 0))
278 mac_ctrl_drv
->set_mac_addr(mac_cb
->priv
.mac
, addr
);
280 memcpy(old_entry
->addr
, addr
, sizeof(old_entry
->addr
));
281 old_entry
->valid
= 1;
285 int hns_mac_add_uc_addr(struct hns_mac_cb
*mac_cb
, u8 vf_id
,
286 const unsigned char *addr
)
288 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
289 struct dsaf_drv_mac_single_dest_entry mac_entry
;
292 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
295 memset(&mac_entry
, 0, sizeof(mac_entry
));
296 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
297 mac_entry
.in_port_num
= mac_cb
->mac_id
;
298 ret
= hns_mac_get_inner_port_num(mac_cb
, vf_id
, &mac_entry
.port_num
);
302 return hns_dsaf_set_mac_uc_entry(dsaf_dev
, &mac_entry
);
305 int hns_mac_rm_uc_addr(struct hns_mac_cb
*mac_cb
, u8 vf_id
,
306 const unsigned char *addr
)
308 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
309 struct dsaf_drv_mac_single_dest_entry mac_entry
;
312 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
315 memset(&mac_entry
, 0, sizeof(mac_entry
));
316 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
317 mac_entry
.in_port_num
= mac_cb
->mac_id
;
318 ret
= hns_mac_get_inner_port_num(mac_cb
, vf_id
, &mac_entry
.port_num
);
322 return hns_dsaf_rm_mac_addr(dsaf_dev
, &mac_entry
);
325 int hns_mac_set_multi(struct hns_mac_cb
*mac_cb
,
326 u32 port_num
, char *addr
, bool enable
)
329 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
330 struct dsaf_drv_mac_single_dest_entry mac_entry
;
332 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
) && addr
) {
333 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
334 mac_entry
.in_vlan_id
= 0;/*vlan_id;*/
335 mac_entry
.in_port_num
= mac_cb
->mac_id
;
336 mac_entry
.port_num
= port_num
;
339 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
341 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
343 dev_err(dsaf_dev
->dev
,
344 "set mac mc port failed, %s mac%d ret = %#x!\n",
345 mac_cb
->dsaf_dev
->ae_dev
.name
,
346 mac_cb
->mac_id
, ret
);
354 int hns_mac_clr_multicast(struct hns_mac_cb
*mac_cb
, int vfn
)
356 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
358 int ret
= hns_mac_get_inner_port_num(mac_cb
, vfn
, &port_num
);
363 return hns_dsaf_clr_mac_mc_port(dsaf_dev
, mac_cb
->mac_id
, port_num
);
366 static void hns_mac_param_get(struct mac_params
*param
,
367 struct hns_mac_cb
*mac_cb
)
369 param
->vaddr
= mac_cb
->vaddr
;
370 param
->mac_mode
= hns_get_enet_interface(mac_cb
);
371 ether_addr_copy(param
->addr
, mac_cb
->addr_entry_idx
[0].addr
);
372 param
->mac_id
= mac_cb
->mac_id
;
373 param
->dev
= mac_cb
->dev
;
377 *hns_mac_queue_config_bc_en - set broadcast rx&tx enable
379 *@queue: queue number
381 *retuen 0 - success , negative --fail
383 static int hns_mac_port_config_bc_en(struct hns_mac_cb
*mac_cb
,
384 u32 port_num
, u16 vlan_id
, bool enable
)
387 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
388 struct dsaf_drv_mac_single_dest_entry mac_entry
;
390 /* directy return ok in debug network mode */
391 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
394 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
395 eth_broadcast_addr(mac_entry
.addr
);
396 mac_entry
.in_vlan_id
= vlan_id
;
397 mac_entry
.in_port_num
= mac_cb
->mac_id
;
398 mac_entry
.port_num
= port_num
;
401 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
403 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
411 *hns_mac_vm_config_bc_en - set broadcast rx&tx enable
415 *retuen 0 - success , negative --fail
417 int hns_mac_vm_config_bc_en(struct hns_mac_cb
*mac_cb
, u32 vmid
, bool enable
)
420 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
422 struct mac_entry_idx
*uc_mac_entry
;
423 struct dsaf_drv_mac_single_dest_entry mac_entry
;
425 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
428 uc_mac_entry
= &mac_cb
->addr_entry_idx
[vmid
];
430 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
)) {
431 eth_broadcast_addr(mac_entry
.addr
);
432 mac_entry
.in_vlan_id
= uc_mac_entry
->vlan_id
;
433 mac_entry
.in_port_num
= mac_cb
->mac_id
;
434 ret
= hns_mac_get_inner_port_num(mac_cb
, vmid
, &port_num
);
437 mac_entry
.port_num
= port_num
;
440 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
442 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
449 int hns_mac_wait_fifo_clean(struct hns_mac_cb
*mac_cb
)
451 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
453 if (drv
->wait_fifo_clean
)
454 return drv
->wait_fifo_clean(drv
);
459 void hns_mac_reset(struct hns_mac_cb
*mac_cb
)
461 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
462 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
466 if (drv
->config_max_frame_length
)
467 drv
->config_max_frame_length(drv
, mac_cb
->max_frm
);
469 if (drv
->set_tx_auto_pause_frames
)
470 drv
->set_tx_auto_pause_frames(drv
, mac_cb
->tx_pause_frm_time
);
472 if (drv
->set_an_mode
)
473 drv
->set_an_mode(drv
, 1);
475 if (drv
->mac_pausefrm_cfg
) {
476 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
477 drv
->mac_pausefrm_cfg(drv
, !is_ver1
, !is_ver1
);
478 else /* mac rx must disable, dsaf pfc close instead of it*/
479 drv
->mac_pausefrm_cfg(drv
, 0, 1);
483 int hns_mac_set_mtu(struct hns_mac_cb
*mac_cb
, u32 new_mtu
, u32 buf_size
)
485 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
486 u32 new_frm
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
488 if (new_frm
> HNS_RCB_RING_MAX_BD_PER_PKT
* buf_size
)
491 if (!drv
->config_max_frame_length
)
494 /* adjust max frame to be at least the size of a standard frame */
495 if (new_frm
< (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
))
496 new_frm
= (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
498 drv
->config_max_frame_length(drv
, new_frm
);
500 mac_cb
->max_frm
= new_frm
;
505 void hns_mac_start(struct hns_mac_cb
*mac_cb
)
507 struct mac_driver
*mac_drv
= hns_mac_get_drv(mac_cb
);
510 if (mac_drv
->mac_en_flg
== MAC_EN_FLAG_V
) {
511 /*plus 1 when the virtual mac has been enabled */
512 mac_drv
->virt_dev_num
+= 1;
516 if (mac_drv
->mac_enable
) {
517 mac_drv
->mac_enable(mac_cb
->priv
.mac
, MAC_COMM_MODE_RX_AND_TX
);
518 mac_drv
->mac_en_flg
= MAC_EN_FLAG_V
;
522 void hns_mac_stop(struct hns_mac_cb
*mac_cb
)
524 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
526 /*modified for virtualization */
527 if (mac_ctrl_drv
->virt_dev_num
> 0) {
528 mac_ctrl_drv
->virt_dev_num
-= 1;
529 if (mac_ctrl_drv
->virt_dev_num
> 0)
533 if (mac_ctrl_drv
->mac_disable
)
534 mac_ctrl_drv
->mac_disable(mac_cb
->priv
.mac
,
535 MAC_COMM_MODE_RX_AND_TX
);
537 mac_ctrl_drv
->mac_en_flg
= 0;
539 mac_cb
->dsaf_dev
->misc_op
->cpld_reset_led(mac_cb
);
543 * hns_mac_get_autoneg - get auto autonegotiation
544 * @mac_cb: mac control block
545 * @enable: enable or not
546 * retuen 0 - success , negative --fail
548 void hns_mac_get_autoneg(struct hns_mac_cb
*mac_cb
, u32
*auto_neg
)
550 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
552 if (mac_ctrl_drv
->autoneg_stat
)
553 mac_ctrl_drv
->autoneg_stat(mac_ctrl_drv
, auto_neg
);
559 * hns_mac_get_pauseparam - set rx & tx pause parameter
560 * @mac_cb: mac control block
561 * @rx_en: rx enable status
562 * @tx_en: tx enable status
563 * retuen 0 - success , negative --fail
565 void hns_mac_get_pauseparam(struct hns_mac_cb
*mac_cb
, u32
*rx_en
, u32
*tx_en
)
567 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
569 if (mac_ctrl_drv
->get_pause_enable
) {
570 mac_ctrl_drv
->get_pause_enable(mac_ctrl_drv
, rx_en
, tx_en
);
578 * hns_mac_set_autoneg - set auto autonegotiation
579 * @mac_cb: mac control block
580 * @enable: enable or not
581 * retuen 0 - success , negative --fail
583 int hns_mac_set_autoneg(struct hns_mac_cb
*mac_cb
, u8 enable
)
585 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
587 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
&& enable
) {
588 dev_err(mac_cb
->dev
, "enabling autoneg is not allowed!\n");
592 if (mac_ctrl_drv
->set_an_mode
)
593 mac_ctrl_drv
->set_an_mode(mac_ctrl_drv
, enable
);
599 * hns_mac_set_autoneg - set rx & tx pause parameter
600 * @mac_cb: mac control block
601 * @rx_en: rx enable or not
602 * @tx_en: tx enable or not
603 * return 0 - success , negative --fail
605 int hns_mac_set_pauseparam(struct hns_mac_cb
*mac_cb
, u32 rx_en
, u32 tx_en
)
607 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
608 bool is_ver1
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
);
610 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
) {
611 if (is_ver1
&& (tx_en
|| rx_en
)) {
612 dev_err(mac_cb
->dev
, "macv1 can't enable tx/rx_pause!\n");
617 if (mac_ctrl_drv
->mac_pausefrm_cfg
)
618 mac_ctrl_drv
->mac_pausefrm_cfg(mac_ctrl_drv
, rx_en
, tx_en
);
624 * hns_mac_init_ex - mac init
625 * @mac_cb: mac control block
626 * retuen 0 - success , negative --fail
628 static int hns_mac_init_ex(struct hns_mac_cb
*mac_cb
)
631 struct mac_params param
;
632 struct mac_driver
*drv
;
634 hns_dsaf_fix_mac_mode(mac_cb
);
636 memset(¶m
, 0, sizeof(struct mac_params
));
637 hns_mac_param_get(¶m
, mac_cb
);
639 if (MAC_SPEED_FROM_MODE(param
.mac_mode
) < MAC_SPEED_10000
)
640 drv
= (struct mac_driver
*)hns_gmac_config(mac_cb
, ¶m
);
642 drv
= (struct mac_driver
*)hns_xgmac_config(mac_cb
, ¶m
);
647 mac_cb
->priv
.mac
= (void *)drv
;
648 hns_mac_reset(mac_cb
);
650 hns_mac_adjust_link(mac_cb
, mac_cb
->speed
, !mac_cb
->half_duplex
);
652 ret
= hns_mac_port_config_bc_en(mac_cb
, mac_cb
->mac_id
, 0, true);
659 drv
->mac_free(mac_cb
->priv
.mac
);
660 mac_cb
->priv
.mac
= NULL
;
666 hns_mac_phy_parse_addr(struct device
*dev
, struct fwnode_handle
*fwnode
)
671 ret
= fwnode_property_read_u32(fwnode
, "phy-addr", &addr
);
673 dev_err(dev
, "has invalid PHY address ret:%d\n", ret
);
677 if (addr
>= PHY_MAX_ADDR
) {
678 dev_err(dev
, "PHY address %i is too large\n", addr
);
686 hns_mac_register_phydev(struct mii_bus
*mdio
, struct hns_mac_cb
*mac_cb
,
689 struct phy_device
*phy
;
690 const char *phy_type
;
694 rc
= fwnode_property_read_string(mac_cb
->fw_port
,
695 "phy-mode", &phy_type
);
699 if (!strcmp(phy_type
, phy_modes(PHY_INTERFACE_MODE_XGMII
)))
701 else if (!strcmp(phy_type
, phy_modes(PHY_INTERFACE_MODE_SGMII
)))
706 phy
= get_phy_device(mdio
, addr
, is_c45
);
707 if (!phy
|| IS_ERR(phy
))
710 phy
->irq
= mdio
->irq
[addr
];
712 /* All data is now stored in the phy struct;
715 rc
= phy_device_register(phy
);
717 phy_device_free(phy
);
718 dev_err(&mdio
->dev
, "registered phy fail at address %i\n",
723 mac_cb
->phy_dev
= phy
;
725 dev_dbg(&mdio
->dev
, "registered phy at address %i\n", addr
);
730 static int hns_mac_register_phy(struct hns_mac_cb
*mac_cb
)
732 struct fwnode_reference_args args
;
733 struct platform_device
*pdev
;
734 struct mii_bus
*mii_bus
;
738 /* Loop over the child nodes and register a phy_device for each one */
739 if (!to_acpi_device_node(mac_cb
->fw_port
))
742 rc
= acpi_node_get_property_reference(
743 mac_cb
->fw_port
, "mdio-node", 0, &args
);
746 if (!is_acpi_device_node(args
.fwnode
))
749 addr
= hns_mac_phy_parse_addr(mac_cb
->dev
, mac_cb
->fw_port
);
753 /* dev address in adev */
754 pdev
= hns_dsaf_find_platform_device(args
.fwnode
);
756 dev_err(mac_cb
->dev
, "mac%d mdio pdev is NULL\n",
761 mii_bus
= platform_get_drvdata(pdev
);
764 "mac%d mdio is NULL, dsaf will probe again later\n",
766 return -EPROBE_DEFER
;
769 rc
= hns_mac_register_phydev(mii_bus
, mac_cb
, addr
);
771 dev_dbg(mac_cb
->dev
, "mac%d register phy addr:%d\n",
772 mac_cb
->mac_id
, addr
);
777 static void hns_mac_remove_phydev(struct hns_mac_cb
*mac_cb
)
779 if (!to_acpi_device_node(mac_cb
->fw_port
) || !mac_cb
->phy_dev
)
782 phy_device_remove(mac_cb
->phy_dev
);
783 phy_device_free(mac_cb
->phy_dev
);
785 mac_cb
->phy_dev
= NULL
;
788 #define MAC_MEDIA_TYPE_MAX_LEN 16
790 static const struct {
791 enum hnae_media_type value
;
793 } media_type_defs
[] = {
794 {HNAE_MEDIA_TYPE_UNKNOWN
, "unknown" },
795 {HNAE_MEDIA_TYPE_FIBER
, "fiber" },
796 {HNAE_MEDIA_TYPE_COPPER
, "copper" },
797 {HNAE_MEDIA_TYPE_BACKPLANE
, "backplane" },
801 *hns_mac_get_info - get mac information from device node
804 * return: 0 --success, negative --fail
806 static int hns_mac_get_info(struct hns_mac_cb
*mac_cb
)
808 struct device_node
*np
;
809 struct regmap
*syscon
;
810 struct of_phandle_args cpld_args
;
811 const char *media_type
;
815 mac_cb
->link
= false;
816 mac_cb
->half_duplex
= false;
817 mac_cb
->media_type
= HNAE_MEDIA_TYPE_UNKNOWN
;
818 mac_cb
->speed
= mac_phy_to_speed
[mac_cb
->phy_if
];
819 mac_cb
->max_speed
= mac_cb
->speed
;
821 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
822 mac_cb
->if_support
= MAC_GMAC_SUPPORTED
;
823 mac_cb
->if_support
|= SUPPORTED_1000baseT_Full
;
824 } else if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
825 mac_cb
->if_support
= SUPPORTED_10000baseR_FEC
;
826 mac_cb
->if_support
|= SUPPORTED_10000baseKR_Full
;
829 mac_cb
->max_frm
= MAC_DEFAULT_MTU
;
830 mac_cb
->tx_pause_frm_time
= MAC_DEFAULT_PAUSE_TIME
;
831 mac_cb
->port_rst_off
= mac_cb
->mac_id
;
832 mac_cb
->port_mode_off
= 0;
834 /* if the dsaf node doesn't contain a port subnode, get phy-handle
837 if (!mac_cb
->fw_port
) {
838 np
= of_parse_phandle(mac_cb
->dev
->of_node
, "phy-handle",
840 mac_cb
->phy_dev
= of_phy_find_device(np
);
841 if (mac_cb
->phy_dev
) {
842 /* refcount is held by of_phy_find_device()
843 * if the phy_dev is found
845 put_device(&mac_cb
->phy_dev
->mdio
.dev
);
847 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %pOFn\n",
855 if (is_of_node(mac_cb
->fw_port
)) {
856 /* parse property from port subnode in dsaf */
857 np
= of_parse_phandle(to_of_node(mac_cb
->fw_port
),
859 mac_cb
->phy_dev
= of_phy_find_device(np
);
860 if (mac_cb
->phy_dev
) {
861 /* refcount is held by of_phy_find_device()
862 * if the phy_dev is found
864 put_device(&mac_cb
->phy_dev
->mdio
.dev
);
865 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %pOFn\n",
870 np
= of_parse_phandle(to_of_node(mac_cb
->fw_port
),
872 syscon
= syscon_node_to_regmap(np
);
874 if (IS_ERR_OR_NULL(syscon
)) {
875 dev_err(mac_cb
->dev
, "serdes-syscon is needed!\n");
878 mac_cb
->serdes_ctrl
= syscon
;
880 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
882 &mac_cb
->port_rst_off
);
885 "mac%d port-rst-offset not found, use default value.\n",
889 ret
= fwnode_property_read_u32(mac_cb
->fw_port
,
891 &mac_cb
->port_mode_off
);
894 "mac%d port-mode-offset not found, use default value.\n",
898 ret
= of_parse_phandle_with_fixed_args(
899 to_of_node(mac_cb
->fw_port
), "cpld-syscon", 1, 0,
902 dev_dbg(mac_cb
->dev
, "mac%d no cpld-syscon found.\n",
904 mac_cb
->cpld_ctrl
= NULL
;
906 syscon
= syscon_node_to_regmap(cpld_args
.np
);
907 if (IS_ERR_OR_NULL(syscon
)) {
908 dev_dbg(mac_cb
->dev
, "no cpld-syscon found!\n");
909 mac_cb
->cpld_ctrl
= NULL
;
911 mac_cb
->cpld_ctrl
= syscon
;
912 mac_cb
->cpld_ctrl_reg
= cpld_args
.args
[0];
915 } else if (is_acpi_node(mac_cb
->fw_port
)) {
916 ret
= hns_mac_register_phy(mac_cb
);
918 * Mac can work well if there is phy or not.If the port don't
919 * connect with phy, the return value will be ignored. Only
920 * when there is phy but can't find mdio bus, the return value
923 if (ret
== -EPROBE_DEFER
)
926 dev_err(mac_cb
->dev
, "mac%d cannot find phy node\n",
930 if (!fwnode_property_read_string(mac_cb
->fw_port
, "media-type",
932 for (i
= 0; i
< ARRAY_SIZE(media_type_defs
); i
++) {
933 if (!strncmp(media_type_defs
[i
].name
, media_type
,
934 MAC_MEDIA_TYPE_MAX_LEN
)) {
935 mac_cb
->media_type
= media_type_defs
[i
].value
;
941 if (fwnode_property_read_u8_array(mac_cb
->fw_port
, "mc-mac-mask",
942 mac_cb
->mc_mask
, ETH_ALEN
)) {
943 dev_warn(mac_cb
->dev
,
944 "no mc-mac-mask property, set to default value.\n");
945 eth_broadcast_addr(mac_cb
->mc_mask
);
952 * hns_mac_get_mode - get mac mode
953 * @phy_if: phy interface
954 * retuen 0 - gmac, 1 - xgmac , negative --fail
956 static int hns_mac_get_mode(phy_interface_t phy_if
)
959 case PHY_INTERFACE_MODE_SGMII
:
961 case PHY_INTERFACE_MODE_XGMII
:
962 return MAC_XGMAC_IDX
;
969 hns_mac_get_vaddr(struct dsaf_device
*dsaf_dev
,
970 struct hns_mac_cb
*mac_cb
, u32 mac_mode_idx
)
972 u8 __iomem
*base
= dsaf_dev
->io_base
;
973 int mac_id
= mac_cb
->mac_id
;
975 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
976 return base
+ 0x40000 + mac_id
* 0x4000 -
977 mac_mode_idx
* 0x20000;
979 return dsaf_dev
->ppe_base
+ 0x1000;
983 * hns_mac_get_cfg - get mac cfg from dtb or acpi table
984 * @dsaf_dev: dsa fabric device struct pointer
985 * @mac_cb: mac control block
986 * return 0 - success , negative --fail
989 hns_mac_get_cfg(struct dsaf_device
*dsaf_dev
, struct hns_mac_cb
*mac_cb
)
994 mac_cb
->dsaf_dev
= dsaf_dev
;
995 mac_cb
->dev
= dsaf_dev
->dev
;
997 mac_cb
->sys_ctl_vaddr
= dsaf_dev
->sc_base
;
998 mac_cb
->serdes_vaddr
= dsaf_dev
->sds_base
;
1000 mac_cb
->sfp_prsnt
= 0;
1001 mac_cb
->txpkt_for_led
= 0;
1002 mac_cb
->rxpkt_for_led
= 0;
1004 if (!HNS_DSAF_IS_DEBUG(dsaf_dev
))
1005 mac_cb
->mac_type
= HNAE_PORT_SERVICE
;
1007 mac_cb
->mac_type
= HNAE_PORT_DEBUG
;
1009 mac_cb
->phy_if
= dsaf_dev
->misc_op
->get_phy_if(mac_cb
);
1011 ret
= hns_mac_get_mode(mac_cb
->phy_if
);
1013 dev_err(dsaf_dev
->dev
,
1014 "hns_mac_get_mode failed, mac%d ret = %#x!\n",
1015 mac_cb
->mac_id
, ret
);
1018 mac_mode_idx
= (u32
)ret
;
1020 ret
= hns_mac_get_info(mac_cb
);
1024 mac_cb
->dsaf_dev
->misc_op
->cpld_reset_led(mac_cb
);
1025 mac_cb
->vaddr
= hns_mac_get_vaddr(dsaf_dev
, mac_cb
, mac_mode_idx
);
1030 static int hns_mac_get_max_port_num(struct dsaf_device
*dsaf_dev
)
1032 if (HNS_DSAF_IS_DEBUG(dsaf_dev
))
1035 return DSAF_MAX_PORT_NUM
;
1038 void hns_mac_enable(struct hns_mac_cb
*mac_cb
, enum mac_commom_mode mode
)
1040 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1042 mac_ctrl_drv
->mac_enable(mac_cb
->priv
.mac
, mode
);
1045 void hns_mac_disable(struct hns_mac_cb
*mac_cb
, enum mac_commom_mode mode
)
1047 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1049 mac_ctrl_drv
->mac_disable(mac_cb
->priv
.mac
, mode
);
1053 * hns_mac_init - init mac
1054 * @dsaf_dev: dsa fabric device struct pointer
1055 * return 0 - success , negative --fail
1057 int hns_mac_init(struct dsaf_device
*dsaf_dev
)
1062 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
1063 struct hns_mac_cb
*mac_cb
;
1064 struct fwnode_handle
*child
;
1066 device_for_each_child_node(dsaf_dev
->dev
, child
) {
1067 ret
= fwnode_property_read_u32(child
, "reg", &port_id
);
1069 dev_err(dsaf_dev
->dev
,
1070 "get reg fail, ret=%d!\n", ret
);
1073 if (port_id
>= max_port_num
) {
1074 dev_err(dsaf_dev
->dev
,
1075 "reg(%u) out of range!\n", port_id
);
1078 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
1082 mac_cb
->fw_port
= child
;
1083 mac_cb
->mac_id
= (u8
)port_id
;
1084 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
1088 /* if don't get any port subnode from dsaf node
1089 * will init all port then, this is compatible with the old dts
1092 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
1093 mac_cb
= devm_kzalloc(dsaf_dev
->dev
, sizeof(*mac_cb
),
1098 mac_cb
->mac_id
= port_id
;
1099 dsaf_dev
->mac_cb
[port_id
] = mac_cb
;
1103 /* init mac_cb for all port */
1104 for (port_id
= 0; port_id
< max_port_num
; port_id
++) {
1105 mac_cb
= dsaf_dev
->mac_cb
[port_id
];
1109 ret
= hns_mac_get_cfg(dsaf_dev
, mac_cb
);
1113 ret
= hns_mac_init_ex(mac_cb
);
1121 void hns_mac_uninit(struct dsaf_device
*dsaf_dev
)
1124 int max_port_num
= hns_mac_get_max_port_num(dsaf_dev
);
1126 for (i
= 0; i
< max_port_num
; i
++) {
1127 if (!dsaf_dev
->mac_cb
[i
])
1130 dsaf_dev
->misc_op
->cpld_reset_led(dsaf_dev
->mac_cb
[i
]);
1131 hns_mac_remove_phydev(dsaf_dev
->mac_cb
[i
]);
1132 dsaf_dev
->mac_cb
[i
] = NULL
;
1136 int hns_mac_config_mac_loopback(struct hns_mac_cb
*mac_cb
,
1137 enum hnae_loop loop
, int en
)
1140 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
1142 if (drv
->config_loopback
)
1143 ret
= drv
->config_loopback(drv
, loop
, en
);
1150 void hns_mac_update_stats(struct hns_mac_cb
*mac_cb
)
1152 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1154 mac_ctrl_drv
->update_stats(mac_ctrl_drv
);
1157 void hns_mac_get_stats(struct hns_mac_cb
*mac_cb
, u64
*data
)
1159 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1161 mac_ctrl_drv
->get_ethtool_stats(mac_ctrl_drv
, data
);
1164 void hns_mac_get_strings(struct hns_mac_cb
*mac_cb
,
1165 int stringset
, u8
*data
)
1167 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1169 mac_ctrl_drv
->get_strings(stringset
, data
);
1172 int hns_mac_get_sset_count(struct hns_mac_cb
*mac_cb
, int stringset
)
1174 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1176 return mac_ctrl_drv
->get_sset_count(stringset
);
1179 void hns_mac_set_promisc(struct hns_mac_cb
*mac_cb
, u8 en
)
1181 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1183 hns_dsaf_set_promisc_tcam(mac_cb
->dsaf_dev
, mac_cb
->mac_id
, !!en
);
1185 if (mac_ctrl_drv
->set_promiscuous
)
1186 mac_ctrl_drv
->set_promiscuous(mac_ctrl_drv
, en
);
1189 int hns_mac_get_regs_count(struct hns_mac_cb
*mac_cb
)
1191 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1193 return mac_ctrl_drv
->get_regs_count();
1196 void hns_mac_get_regs(struct hns_mac_cb
*mac_cb
, void *data
)
1198 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
1200 mac_ctrl_drv
->get_regs(mac_ctrl_drv
, data
);
1203 void hns_set_led_opt(struct hns_mac_cb
*mac_cb
)
1208 txpkts
= mac_cb
->txpkt_for_led
- mac_cb
->hw_stats
.tx_good_pkts
;
1209 rxpkts
= mac_cb
->rxpkt_for_led
- mac_cb
->hw_stats
.rx_good_pkts
;
1210 if (txpkts
|| rxpkts
)
1214 mac_cb
->txpkt_for_led
= mac_cb
->hw_stats
.tx_good_pkts
;
1215 mac_cb
->rxpkt_for_led
= mac_cb
->hw_stats
.rx_good_pkts
;
1216 mac_cb
->dsaf_dev
->misc_op
->cpld_set_led(mac_cb
, (int)mac_cb
->link
,
1217 mac_cb
->speed
, nic_data
);
1220 int hns_cpld_led_set_id(struct hns_mac_cb
*mac_cb
,
1221 enum hnae_led_state status
)
1226 return mac_cb
->dsaf_dev
->misc_op
->cpld_set_led_id(mac_cb
, status
);