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/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy_fixed.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
18 #include <linux/of_address.h>
20 #include "hns_dsaf_misc.h"
21 #include "hns_dsaf_main.h"
22 #include "hns_dsaf_rcb.h"
24 #define MAC_EN_FLAG_V 0xada0328
26 static const u16 mac_phy_to_speed
[] = {
27 [PHY_INTERFACE_MODE_MII
] = MAC_SPEED_100
,
28 [PHY_INTERFACE_MODE_GMII
] = MAC_SPEED_1000
,
29 [PHY_INTERFACE_MODE_SGMII
] = MAC_SPEED_1000
,
30 [PHY_INTERFACE_MODE_TBI
] = MAC_SPEED_1000
,
31 [PHY_INTERFACE_MODE_RMII
] = MAC_SPEED_100
,
32 [PHY_INTERFACE_MODE_RGMII
] = MAC_SPEED_1000
,
33 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_SPEED_1000
,
34 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_SPEED_1000
,
35 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_SPEED_1000
,
36 [PHY_INTERFACE_MODE_RTBI
] = MAC_SPEED_1000
,
37 [PHY_INTERFACE_MODE_XGMII
] = MAC_SPEED_10000
40 static const enum mac_mode g_mac_mode_100
[] = {
41 [PHY_INTERFACE_MODE_MII
] = MAC_MODE_MII_100
,
42 [PHY_INTERFACE_MODE_RMII
] = MAC_MODE_RMII_100
45 static const enum mac_mode g_mac_mode_1000
[] = {
46 [PHY_INTERFACE_MODE_GMII
] = MAC_MODE_GMII_1000
,
47 [PHY_INTERFACE_MODE_SGMII
] = MAC_MODE_SGMII_1000
,
48 [PHY_INTERFACE_MODE_TBI
] = MAC_MODE_TBI_1000
,
49 [PHY_INTERFACE_MODE_RGMII
] = MAC_MODE_RGMII_1000
,
50 [PHY_INTERFACE_MODE_RGMII_ID
] = MAC_MODE_RGMII_1000
,
51 [PHY_INTERFACE_MODE_RGMII_RXID
] = MAC_MODE_RGMII_1000
,
52 [PHY_INTERFACE_MODE_RGMII_TXID
] = MAC_MODE_RGMII_1000
,
53 [PHY_INTERFACE_MODE_RTBI
] = MAC_MODE_RTBI_1000
56 static enum mac_mode
hns_mac_dev_to_enet_if(const struct hns_mac_cb
*mac_cb
)
58 switch (mac_cb
->max_speed
) {
60 return g_mac_mode_100
[mac_cb
->phy_if
];
62 return g_mac_mode_1000
[mac_cb
->phy_if
];
64 return MAC_MODE_XGMII_10000
;
66 return MAC_MODE_MII_100
;
70 static enum mac_mode
hns_get_enet_interface(const struct hns_mac_cb
*mac_cb
)
72 switch (mac_cb
->max_speed
) {
74 return g_mac_mode_100
[mac_cb
->phy_if
];
76 return g_mac_mode_1000
[mac_cb
->phy_if
];
78 return MAC_MODE_XGMII_10000
;
80 return MAC_MODE_MII_100
;
84 int hns_mac_get_sfp_prsnt(struct hns_mac_cb
*mac_cb
, int *sfp_prsnt
)
86 if (!mac_cb
->cpld_vaddr
)
89 *sfp_prsnt
= !dsaf_read_b((u8
*)mac_cb
->cpld_vaddr
90 + MAC_SFP_PORT_OFFSET
);
95 void hns_mac_get_link_status(struct hns_mac_cb
*mac_cb
, u32
*link_status
)
97 struct mac_driver
*mac_ctrl_drv
;
100 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
102 if (mac_ctrl_drv
->get_link_status
)
103 mac_ctrl_drv
->get_link_status(mac_ctrl_drv
, link_status
);
107 ret
= hns_mac_get_sfp_prsnt(mac_cb
, &sfp_prsnt
);
109 *link_status
= *link_status
&& sfp_prsnt
;
111 mac_cb
->link
= *link_status
;
114 int hns_mac_get_port_info(struct hns_mac_cb
*mac_cb
,
115 u8
*auto_neg
, u16
*speed
, u8
*duplex
)
117 struct mac_driver
*mac_ctrl_drv
;
118 struct mac_info info
;
120 mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
122 if (!mac_ctrl_drv
->get_info
)
125 mac_ctrl_drv
->get_info(mac_ctrl_drv
, &info
);
127 *auto_neg
= info
.auto_neg
;
131 *duplex
= info
.duplex
;
136 void hns_mac_adjust_link(struct hns_mac_cb
*mac_cb
, int speed
, int duplex
)
139 struct mac_driver
*mac_ctrl_drv
;
141 mac_ctrl_drv
= (struct mac_driver
*)(mac_cb
->priv
.mac
);
143 mac_cb
->speed
= speed
;
144 mac_cb
->half_duplex
= !duplex
;
145 mac_ctrl_drv
->mac_mode
= hns_mac_dev_to_enet_if(mac_cb
);
147 if (mac_ctrl_drv
->adjust_link
) {
148 ret
= mac_ctrl_drv
->adjust_link(mac_ctrl_drv
,
149 (enum mac_speed
)speed
, duplex
);
152 "adjust_link failed,%s mac%d ret = %#x!\n",
153 mac_cb
->dsaf_dev
->ae_dev
.name
,
154 mac_cb
->mac_id
, ret
);
161 *hns_mac_get_inner_port_num - get mac table inner port number
164 *@port_num:port number
167 static int hns_mac_get_inner_port_num(struct hns_mac_cb
*mac_cb
,
168 u8 vmid
, u8
*port_num
)
173 if (mac_cb
->dsaf_dev
->dsaf_mode
<= DSAF_MODE_ENABLE
) {
174 if (mac_cb
->mac_id
!= DSAF_MAX_PORT_NUM_PER_CHIP
) {
176 "input invalid,%s mac%d vmid%d !\n",
177 mac_cb
->dsaf_dev
->ae_dev
.name
,
178 mac_cb
->mac_id
, vmid
);
181 } else if (mac_cb
->dsaf_dev
->dsaf_mode
< DSAF_MODE_MAX
) {
182 if (mac_cb
->mac_id
>= DSAF_MAX_PORT_NUM_PER_CHIP
) {
184 "input invalid,%s mac%d vmid%d!\n",
185 mac_cb
->dsaf_dev
->ae_dev
.name
,
186 mac_cb
->mac_id
, vmid
);
190 dev_err(mac_cb
->dev
, "dsaf mode invalid,%s mac%d!\n",
191 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
);
195 comm_idx
= hns_dsaf_get_comm_idx_by_port(mac_cb
->mac_id
);
197 if (vmid
>= mac_cb
->dsaf_dev
->rcb_common
[comm_idx
]->max_vfn
) {
198 dev_err(mac_cb
->dev
, "input invalid,%s mac%d vmid%d !\n",
199 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vmid
);
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
:
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_get_inner_port_num - 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
];
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_set_multi(struct hns_mac_cb
*mac_cb
,
286 u32 port_num
, char *addr
, bool enable
)
289 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
290 struct dsaf_drv_mac_single_dest_entry mac_entry
;
292 if (dsaf_dev
&& addr
) {
293 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
294 mac_entry
.in_vlan_id
= 0;/*vlan_id;*/
295 mac_entry
.in_port_num
= mac_cb
->mac_id
;
296 mac_entry
.port_num
= port_num
;
299 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
301 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
303 dev_err(dsaf_dev
->dev
,
304 "set mac mc port failed,%s mac%d ret = %#x!\n",
305 mac_cb
->dsaf_dev
->ae_dev
.name
,
306 mac_cb
->mac_id
, ret
);
315 *hns_mac_del_mac - delete mac address into dsaf table,can't delete the same
317 *@net_dev: net device
322 int hns_mac_del_mac(struct hns_mac_cb
*mac_cb
, u32 vfn
, char *mac
)
324 struct mac_entry_idx
*old_mac
;
325 struct dsaf_device
*dsaf_dev
;
328 dsaf_dev
= mac_cb
->dsaf_dev
;
330 if (vfn
< DSAF_MAX_VM_NUM
) {
331 old_mac
= &mac_cb
->addr_entry_idx
[vfn
];
334 "vf queue is too large,%s mac%d queue = %#x!\n",
335 mac_cb
->dsaf_dev
->ae_dev
.name
, mac_cb
->mac_id
, vfn
);
340 ret
= hns_dsaf_del_mac_entry(dsaf_dev
, old_mac
->vlan_id
,
341 mac_cb
->mac_id
, old_mac
->addr
);
345 if (memcmp(old_mac
->addr
, mac
, sizeof(old_mac
->addr
)) == 0)
352 static void hns_mac_param_get(struct mac_params
*param
,
353 struct hns_mac_cb
*mac_cb
)
355 param
->vaddr
= (void *)mac_cb
->vaddr
;
356 param
->mac_mode
= hns_get_enet_interface(mac_cb
);
357 memcpy(param
->addr
, mac_cb
->addr_entry_idx
[0].addr
,
358 MAC_NUM_OCTETS_PER_ADDR
);
359 param
->mac_id
= mac_cb
->mac_id
;
360 param
->dev
= mac_cb
->dev
;
364 *hns_mac_queue_config_bc_en - set broadcast rx&tx enable
366 *@queue: queue number
368 *retuen 0 - success , negative --fail
370 static int hns_mac_port_config_bc_en(struct hns_mac_cb
*mac_cb
,
371 u32 port_num
, u16 vlan_id
, bool enable
)
374 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
375 u8 addr
[MAC_NUM_OCTETS_PER_ADDR
]
376 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
377 struct dsaf_drv_mac_single_dest_entry mac_entry
;
379 /* directy return ok in debug network mode */
380 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
384 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
385 mac_entry
.in_vlan_id
= vlan_id
;
386 mac_entry
.in_port_num
= mac_cb
->mac_id
;
387 mac_entry
.port_num
= port_num
;
390 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
392 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
400 *hns_mac_vm_config_bc_en - set broadcast rx&tx enable
404 *retuen 0 - success , negative --fail
406 int hns_mac_vm_config_bc_en(struct hns_mac_cb
*mac_cb
, u32 vmid
, bool enable
)
409 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
411 u8 addr
[MAC_NUM_OCTETS_PER_ADDR
]
412 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
413 struct mac_entry_idx
*uc_mac_entry
;
414 struct dsaf_drv_mac_single_dest_entry mac_entry
;
416 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
419 uc_mac_entry
= &mac_cb
->addr_entry_idx
[vmid
];
422 memcpy(mac_entry
.addr
, addr
, sizeof(mac_entry
.addr
));
423 mac_entry
.in_vlan_id
= uc_mac_entry
->vlan_id
;
424 mac_entry
.in_port_num
= mac_cb
->mac_id
;
425 ret
= hns_mac_get_inner_port_num(mac_cb
, vmid
, &port_num
);
428 mac_entry
.port_num
= port_num
;
431 ret
= hns_dsaf_del_mac_mc_port(dsaf_dev
, &mac_entry
);
433 ret
= hns_dsaf_add_mac_mc_port(dsaf_dev
, &mac_entry
);
440 void hns_mac_reset(struct hns_mac_cb
*mac_cb
)
442 struct mac_driver
*drv
;
444 drv
= hns_mac_get_drv(mac_cb
);
448 if (drv
->config_max_frame_length
)
449 drv
->config_max_frame_length(drv
, mac_cb
->max_frm
);
451 if (drv
->set_tx_auto_pause_frames
)
452 drv
->set_tx_auto_pause_frames(drv
, mac_cb
->tx_pause_frm_time
);
454 if (drv
->set_an_mode
)
455 drv
->set_an_mode(drv
, 1);
457 if (drv
->mac_pausefrm_cfg
) {
458 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
459 drv
->mac_pausefrm_cfg(drv
, 0, 0);
460 else /* mac rx must disable, dsaf pfc close instead of it*/
461 drv
->mac_pausefrm_cfg(drv
, 0, 1);
465 int hns_mac_set_mtu(struct hns_mac_cb
*mac_cb
, u32 new_mtu
)
467 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
468 u32 buf_size
= mac_cb
->dsaf_dev
->buf_size
;
469 u32 new_frm
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
470 u32 max_frm
= AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
) ?
471 MAC_MAX_MTU
: MAC_MAX_MTU_V2
;
473 if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
)
474 max_frm
= MAC_MAX_MTU_DBG
;
476 if ((new_mtu
< MAC_MIN_MTU
) || (new_frm
> max_frm
) ||
477 (new_frm
> HNS_RCB_RING_MAX_BD_PER_PKT
* buf_size
))
480 if (!drv
->config_max_frame_length
)
483 /* adjust max frame to be at least the size of a standard frame */
484 if (new_frm
< (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
))
485 new_frm
= (ETH_FRAME_LEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
487 drv
->config_max_frame_length(drv
, new_frm
);
489 mac_cb
->max_frm
= new_frm
;
494 void hns_mac_start(struct hns_mac_cb
*mac_cb
)
496 struct mac_driver
*mac_drv
= hns_mac_get_drv(mac_cb
);
499 if (mac_drv
->mac_en_flg
== MAC_EN_FLAG_V
) {
500 /*plus 1 when the virtual mac has been enabled */
501 mac_drv
->virt_dev_num
+= 1;
505 if (mac_drv
->mac_enable
) {
506 mac_drv
->mac_enable(mac_cb
->priv
.mac
, MAC_COMM_MODE_RX_AND_TX
);
507 mac_drv
->mac_en_flg
= MAC_EN_FLAG_V
;
511 void hns_mac_stop(struct hns_mac_cb
*mac_cb
)
513 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
515 /*modified for virtualization */
516 if (mac_ctrl_drv
->virt_dev_num
> 0) {
517 mac_ctrl_drv
->virt_dev_num
-= 1;
518 if (mac_ctrl_drv
->virt_dev_num
> 0)
522 if (mac_ctrl_drv
->mac_disable
)
523 mac_ctrl_drv
->mac_disable(mac_cb
->priv
.mac
,
524 MAC_COMM_MODE_RX_AND_TX
);
526 mac_ctrl_drv
->mac_en_flg
= 0;
528 cpld_led_reset(mac_cb
);
532 * hns_mac_get_autoneg - get auto autonegotiation
533 * @mac_cb: mac control block
534 * @enable: enable or not
535 * retuen 0 - success , negative --fail
537 void hns_mac_get_autoneg(struct hns_mac_cb
*mac_cb
, u32
*auto_neg
)
539 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
541 if (mac_ctrl_drv
->autoneg_stat
)
542 mac_ctrl_drv
->autoneg_stat(mac_ctrl_drv
, auto_neg
);
548 * hns_mac_get_pauseparam - set rx & tx pause parameter
549 * @mac_cb: mac control block
550 * @rx_en: rx enable status
551 * @tx_en: tx enable status
552 * retuen 0 - success , negative --fail
554 void hns_mac_get_pauseparam(struct hns_mac_cb
*mac_cb
, u32
*rx_en
, u32
*tx_en
)
556 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
558 if (mac_ctrl_drv
->get_pause_enable
) {
559 mac_ctrl_drv
->get_pause_enable(mac_ctrl_drv
, rx_en
, tx_en
);
565 /* Due to the chip defect, the service mac's rx pause CAN'T be enabled.
566 * We set the rx pause frm always be true (1), because DSAF deals with
567 * the rx pause frm instead of service mac. After all, we still support
570 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
575 * hns_mac_set_autoneg - set auto autonegotiation
576 * @mac_cb: mac control block
577 * @enable: enable or not
578 * retuen 0 - success , negative --fail
580 int hns_mac_set_autoneg(struct hns_mac_cb
*mac_cb
, u8 enable
)
582 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
584 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
&& enable
) {
585 dev_err(mac_cb
->dev
, "enable autoneg is not allowed!");
589 if (mac_ctrl_drv
->set_an_mode
)
590 mac_ctrl_drv
->set_an_mode(mac_ctrl_drv
, enable
);
596 * hns_mac_set_autoneg - set rx & tx pause parameter
597 * @mac_cb: mac control block
598 * @rx_en: rx enable or not
599 * @tx_en: tx enable or not
600 * return 0 - success , negative --fail
602 int hns_mac_set_pauseparam(struct hns_mac_cb
*mac_cb
, u32 rx_en
, u32 tx_en
)
604 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
606 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
) {
608 dev_err(mac_cb
->dev
, "disable rx_pause is not allowed!");
611 } else if (mac_cb
->mac_type
== HNAE_PORT_DEBUG
) {
612 if (tx_en
|| rx_en
) {
613 dev_err(mac_cb
->dev
, "enable tx_pause or enable rx_pause are not allowed!");
617 dev_err(mac_cb
->dev
, "Unsupport this operation!");
621 if (mac_ctrl_drv
->mac_pausefrm_cfg
)
622 mac_ctrl_drv
->mac_pausefrm_cfg(mac_ctrl_drv
, rx_en
, tx_en
);
628 * hns_mac_init_ex - mac init
629 * @mac_cb: mac control block
630 * retuen 0 - success , negative --fail
632 static int hns_mac_init_ex(struct hns_mac_cb
*mac_cb
)
635 struct mac_params param
;
636 struct mac_driver
*drv
;
638 hns_dsaf_fix_mac_mode(mac_cb
);
640 memset(¶m
, 0, sizeof(struct mac_params
));
641 hns_mac_param_get(¶m
, mac_cb
);
643 if (MAC_SPEED_FROM_MODE(param
.mac_mode
) < MAC_SPEED_10000
)
644 drv
= (struct mac_driver
*)hns_gmac_config(mac_cb
, ¶m
);
646 drv
= (struct mac_driver
*)hns_xgmac_config(mac_cb
, ¶m
);
651 mac_cb
->priv
.mac
= (void *)drv
;
652 hns_mac_reset(mac_cb
);
654 hns_mac_adjust_link(mac_cb
, mac_cb
->speed
, !mac_cb
->half_duplex
);
656 ret
= hns_mac_port_config_bc_en(mac_cb
, mac_cb
->mac_id
, 0, true);
663 drv
->mac_free(mac_cb
->priv
.mac
);
664 mac_cb
->priv
.mac
= NULL
;
670 *mac_free_dev - get mac information from device node
673 *@mac_mode_idx:mac mode index
675 static void hns_mac_get_info(struct hns_mac_cb
*mac_cb
,
676 struct device_node
*np
, u32 mac_mode_idx
)
678 mac_cb
->link
= false;
679 mac_cb
->half_duplex
= false;
680 mac_cb
->speed
= mac_phy_to_speed
[mac_cb
->phy_if
];
681 mac_cb
->max_speed
= mac_cb
->speed
;
683 if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
684 mac_cb
->if_support
= MAC_GMAC_SUPPORTED
;
685 mac_cb
->if_support
|= SUPPORTED_1000baseT_Full
;
686 } else if (mac_cb
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
687 mac_cb
->if_support
= SUPPORTED_10000baseR_FEC
;
688 mac_cb
->if_support
|= SUPPORTED_10000baseKR_Full
;
691 mac_cb
->max_frm
= MAC_DEFAULT_MTU
;
692 mac_cb
->tx_pause_frm_time
= MAC_DEFAULT_PAUSE_TIME
;
694 /* Get the rest of the PHY information */
695 mac_cb
->phy_node
= of_parse_phandle(np
, "phy-handle", mac_cb
->mac_id
);
696 if (mac_cb
->phy_node
)
697 dev_dbg(mac_cb
->dev
, "mac%d phy_node: %s\n",
698 mac_cb
->mac_id
, mac_cb
->phy_node
->name
);
702 * hns_mac_get_mode - get mac mode
703 * @phy_if: phy interface
704 * retuen 0 - gmac, 1 - xgmac , negative --fail
706 static int hns_mac_get_mode(phy_interface_t phy_if
)
709 case PHY_INTERFACE_MODE_SGMII
:
711 case PHY_INTERFACE_MODE_XGMII
:
712 return MAC_XGMAC_IDX
;
718 u8 __iomem
*hns_mac_get_vaddr(struct dsaf_device
*dsaf_dev
,
719 struct hns_mac_cb
*mac_cb
, u32 mac_mode_idx
)
721 u8 __iomem
*base
= dsaf_dev
->io_base
;
722 int mac_id
= mac_cb
->mac_id
;
724 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
725 return base
+ 0x40000 + mac_id
* 0x4000 -
726 mac_mode_idx
* 0x20000;
728 return mac_cb
->serdes_vaddr
+ 0x1000
729 + (mac_id
- DSAF_SERVICE_PORT_NUM_PER_DSAF
) * 0x100000;
733 * hns_mac_get_cfg - get mac cfg from dtb or acpi table
734 * @dsaf_dev: dsa fabric device struct pointer
735 * @mac_idx: mac index
736 * retuen 0 - success , negative --fail
738 int hns_mac_get_cfg(struct dsaf_device
*dsaf_dev
, int mac_idx
)
742 struct hns_mac_cb
*mac_cb
= &dsaf_dev
->mac_cb
[mac_idx
];
744 mac_cb
->dsaf_dev
= dsaf_dev
;
745 mac_cb
->dev
= dsaf_dev
->dev
;
746 mac_cb
->mac_id
= mac_idx
;
748 mac_cb
->sys_ctl_vaddr
= dsaf_dev
->sc_base
;
749 mac_cb
->serdes_vaddr
= dsaf_dev
->sds_base
;
751 if (dsaf_dev
->cpld_base
&&
752 mac_idx
< DSAF_SERVICE_PORT_NUM_PER_DSAF
) {
753 mac_cb
->cpld_vaddr
= dsaf_dev
->cpld_base
+
754 mac_cb
->mac_id
* CPLD_ADDR_PORT_OFFSET
;
755 cpld_led_reset(mac_cb
);
757 mac_cb
->sfp_prsnt
= 0;
758 mac_cb
->txpkt_for_led
= 0;
759 mac_cb
->rxpkt_for_led
= 0;
761 if (mac_idx
< DSAF_SERVICE_PORT_NUM_PER_DSAF
)
762 mac_cb
->mac_type
= HNAE_PORT_SERVICE
;
764 mac_cb
->mac_type
= HNAE_PORT_DEBUG
;
766 mac_cb
->phy_if
= hns_mac_get_phy_if(mac_cb
);
768 ret
= hns_mac_get_mode(mac_cb
->phy_if
);
770 dev_err(dsaf_dev
->dev
,
771 "hns_mac_get_mode failed,mac%d ret = %#x!\n",
772 mac_cb
->mac_id
, ret
);
775 mac_mode_idx
= (u32
)ret
;
777 hns_mac_get_info(mac_cb
, mac_cb
->dev
->of_node
, mac_mode_idx
);
779 mac_cb
->vaddr
= hns_mac_get_vaddr(dsaf_dev
, mac_cb
, mac_mode_idx
);
785 * hns_mac_init - init mac
786 * @dsaf_dev: dsa fabric device struct pointer
787 * retuen 0 - success , negative --fail
789 int hns_mac_init(struct dsaf_device
*dsaf_dev
)
794 struct hns_mac_cb
*mac_cb
;
796 size
= sizeof(struct hns_mac_cb
) * DSAF_MAX_PORT_NUM_PER_CHIP
;
797 dsaf_dev
->mac_cb
= devm_kzalloc(dsaf_dev
->dev
, size
, GFP_KERNEL
);
798 if (!dsaf_dev
->mac_cb
)
801 for (i
= 0; i
< DSAF_MAX_PORT_NUM_PER_CHIP
; i
++) {
802 ret
= hns_mac_get_cfg(dsaf_dev
, i
);
806 mac_cb
= &dsaf_dev
->mac_cb
[i
];
807 ret
= hns_mac_init_ex(mac_cb
);
815 dsaf_dev
->mac_cb
= NULL
;
820 void hns_mac_uninit(struct dsaf_device
*dsaf_dev
)
822 cpld_led_reset(dsaf_dev
->mac_cb
);
823 dsaf_dev
->mac_cb
= NULL
;
826 int hns_mac_config_mac_loopback(struct hns_mac_cb
*mac_cb
,
827 enum hnae_loop loop
, int en
)
830 struct mac_driver
*drv
= hns_mac_get_drv(mac_cb
);
832 if (drv
->config_loopback
)
833 ret
= drv
->config_loopback(drv
, loop
, en
);
840 void hns_mac_update_stats(struct hns_mac_cb
*mac_cb
)
842 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
844 mac_ctrl_drv
->update_stats(mac_ctrl_drv
);
847 void hns_mac_get_stats(struct hns_mac_cb
*mac_cb
, u64
*data
)
849 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
851 mac_ctrl_drv
->get_ethtool_stats(mac_ctrl_drv
, data
);
854 void hns_mac_get_strings(struct hns_mac_cb
*mac_cb
,
855 int stringset
, u8
*data
)
857 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
859 mac_ctrl_drv
->get_strings(stringset
, data
);
862 int hns_mac_get_sset_count(struct hns_mac_cb
*mac_cb
, int stringset
)
864 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
866 return mac_ctrl_drv
->get_sset_count(stringset
);
869 void hns_mac_set_promisc(struct hns_mac_cb
*mac_cb
, u8 en
)
871 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
873 if (mac_ctrl_drv
->set_promiscuous
)
874 mac_ctrl_drv
->set_promiscuous(mac_ctrl_drv
, en
);
877 int hns_mac_get_regs_count(struct hns_mac_cb
*mac_cb
)
879 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
881 return mac_ctrl_drv
->get_regs_count();
884 void hns_mac_get_regs(struct hns_mac_cb
*mac_cb
, void *data
)
886 struct mac_driver
*mac_ctrl_drv
= hns_mac_get_drv(mac_cb
);
888 mac_ctrl_drv
->get_regs(mac_ctrl_drv
, data
);
891 void hns_set_led_opt(struct hns_mac_cb
*mac_cb
)
896 txpkts
= mac_cb
->txpkt_for_led
- mac_cb
->hw_stats
.tx_good_pkts
;
897 rxpkts
= mac_cb
->rxpkt_for_led
- mac_cb
->hw_stats
.rx_good_pkts
;
898 if (txpkts
|| rxpkts
)
902 mac_cb
->txpkt_for_led
= mac_cb
->hw_stats
.tx_good_pkts
;
903 mac_cb
->rxpkt_for_led
= mac_cb
->hw_stats
.rx_good_pkts
;
904 hns_cpld_set_led(mac_cb
, (int)mac_cb
->link
,
905 mac_cb
->speed
, nic_data
);
908 int hns_cpld_led_set_id(struct hns_mac_cb
*mac_cb
,
909 enum hnae_led_state status
)
911 if (!mac_cb
|| !mac_cb
->cpld_vaddr
)
914 return cpld_set_led_id(mac_cb
, status
);