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, u8 en)
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, u8 en)
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, u8 en)
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;
471 if ((new_mtu < MAC_MIN_MTU) || (new_frm > MAC_MAX_MTU) ||
472 (new_frm > HNS_RCB_RING_MAX_BD_PER_PKT * buf_size))
475 if (!drv->config_max_frame_length)
478 /* adjust max frame to be at least the size of a standard frame */
479 if (new_frm < (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN))
480 new_frm = (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN);
482 drv->config_max_frame_length(drv, new_frm);
484 mac_cb->max_frm = new_frm;
489 void hns_mac_start(struct hns_mac_cb *mac_cb)
491 struct mac_driver *mac_drv = hns_mac_get_drv(mac_cb);
494 if (mac_drv->mac_en_flg == MAC_EN_FLAG_V) {
495 /*plus 1 when the virtual mac has been enabled */
496 mac_drv->virt_dev_num += 1;
500 if (mac_drv->mac_enable) {
501 mac_drv->mac_enable(mac_cb->priv.mac, MAC_COMM_MODE_RX_AND_TX);
502 mac_drv->mac_en_flg = MAC_EN_FLAG_V;
506 void hns_mac_stop(struct hns_mac_cb *mac_cb)
508 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
510 /*modified for virtualization */
511 if (mac_ctrl_drv->virt_dev_num > 0) {
512 mac_ctrl_drv->virt_dev_num -= 1;
513 if (mac_ctrl_drv->virt_dev_num > 0)
517 if (mac_ctrl_drv->mac_disable)
518 mac_ctrl_drv->mac_disable(mac_cb->priv.mac,
519 MAC_COMM_MODE_RX_AND_TX);
521 mac_ctrl_drv->mac_en_flg = 0;
523 cpld_led_reset(mac_cb);
527 * hns_mac_get_autoneg - get auto autonegotiation
528 * @mac_cb: mac control block
529 * @enable: enable or not
530 * retuen 0 - success , negative --fail
532 void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg)
534 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
536 if (mac_ctrl_drv->autoneg_stat)
537 mac_ctrl_drv->autoneg_stat(mac_ctrl_drv, auto_neg);
543 * hns_mac_get_pauseparam - set rx & tx pause parameter
544 * @mac_cb: mac control block
545 * @rx_en: rx enable status
546 * @tx_en: tx enable status
547 * retuen 0 - success , negative --fail
549 void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en)
551 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
553 if (mac_ctrl_drv->get_pause_enable) {
554 mac_ctrl_drv->get_pause_enable(mac_ctrl_drv, rx_en, tx_en);
560 /* Due to the chip defect, the service mac's rx pause CAN'T be enabled.
561 * We set the rx pause frm always be true (1), because DSAF deals with
562 * the rx pause frm instead of service mac. After all, we still support
565 if (mac_cb->mac_type == HNAE_PORT_SERVICE)
570 * hns_mac_set_autoneg - set auto autonegotiation
571 * @mac_cb: mac control block
572 * @enable: enable or not
573 * retuen 0 - success , negative --fail
575 int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable)
577 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
579 if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII && enable) {
580 dev_err(mac_cb->dev, "enable autoneg is not allowed!");
584 if (mac_ctrl_drv->set_an_mode)
585 mac_ctrl_drv->set_an_mode(mac_ctrl_drv, enable);
591 * hns_mac_set_autoneg - set rx & tx pause parameter
592 * @mac_cb: mac control block
593 * @rx_en: rx enable or not
594 * @tx_en: tx enable or not
595 * return 0 - success , negative --fail
597 int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en)
599 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
601 if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
603 dev_err(mac_cb->dev, "disable rx_pause is not allowed!");
606 } else if (mac_cb->mac_type == HNAE_PORT_DEBUG) {
607 if (tx_en || rx_en) {
608 dev_err(mac_cb->dev, "enable tx_pause or enable rx_pause are not allowed!");
612 dev_err(mac_cb->dev, "Unsupport this operation!");
616 if (mac_ctrl_drv->mac_pausefrm_cfg)
617 mac_ctrl_drv->mac_pausefrm_cfg(mac_ctrl_drv, rx_en, tx_en);
623 * hns_mac_init_ex - mac init
624 * @mac_cb: mac control block
625 * retuen 0 - success , negative --fail
627 static int hns_mac_init_ex(struct hns_mac_cb *mac_cb)
630 struct mac_params param;
631 struct mac_driver *drv;
633 hns_dsaf_fix_mac_mode(mac_cb);
635 memset(¶m, 0, sizeof(struct mac_params));
636 hns_mac_param_get(¶m, mac_cb);
638 if (MAC_SPEED_FROM_MODE(param.mac_mode) < MAC_SPEED_10000)
639 drv = (struct mac_driver *)hns_gmac_config(mac_cb, ¶m);
641 drv = (struct mac_driver *)hns_xgmac_config(mac_cb, ¶m);
646 mac_cb->priv.mac = (void *)drv;
647 hns_mac_reset(mac_cb);
649 hns_mac_adjust_link(mac_cb, mac_cb->speed, !mac_cb->half_duplex);
651 ret = hns_mac_port_config_bc_en(mac_cb, mac_cb->mac_id, 0, ENABLE);
658 drv->mac_free(mac_cb->priv.mac);
659 mac_cb->priv.mac = NULL;
665 *mac_free_dev - get mac information from device node
668 *@mac_mode_idx:mac mode index
670 static void hns_mac_get_info(struct hns_mac_cb *mac_cb,
671 struct device_node *np, u32 mac_mode_idx)
673 mac_cb->link = false;
674 mac_cb->half_duplex = false;
675 mac_cb->speed = mac_phy_to_speed[mac_cb->phy_if];
676 mac_cb->max_speed = mac_cb->speed;
678 if (mac_cb->phy_if == PHY_INTERFACE_MODE_SGMII) {
679 mac_cb->if_support = MAC_GMAC_SUPPORTED;
680 mac_cb->if_support |= SUPPORTED_1000baseT_Full;
681 } else if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII) {
682 mac_cb->if_support = SUPPORTED_10000baseR_FEC;
683 mac_cb->if_support |= SUPPORTED_10000baseKR_Full;
686 mac_cb->max_frm = MAC_DEFAULT_MTU;
687 mac_cb->tx_pause_frm_time = MAC_DEFAULT_PAUSE_TIME;
689 /* Get the rest of the PHY information */
690 mac_cb->phy_node = of_parse_phandle(np, "phy-handle", mac_cb->mac_id);
691 if (mac_cb->phy_node)
692 dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n",
693 mac_cb->mac_id, mac_cb->phy_node->name);
697 * hns_mac_get_mode - get mac mode
698 * @phy_if: phy interface
699 * retuen 0 - gmac, 1 - xgmac , negative --fail
701 static int hns_mac_get_mode(phy_interface_t phy_if)
704 case PHY_INTERFACE_MODE_SGMII:
706 case PHY_INTERFACE_MODE_XGMII:
707 return MAC_XGMAC_IDX;
713 u8 __iomem *hns_mac_get_vaddr(struct dsaf_device *dsaf_dev,
714 struct hns_mac_cb *mac_cb, u32 mac_mode_idx)
716 u8 __iomem *base = dsaf_dev->io_base;
717 int mac_id = mac_cb->mac_id;
719 if (mac_cb->mac_type == HNAE_PORT_SERVICE)
720 return base + 0x40000 + mac_id * 0x4000 -
721 mac_mode_idx * 0x20000;
723 return mac_cb->serdes_vaddr + 0x1000
724 + (mac_id - DSAF_SERVICE_PORT_NUM_PER_DSAF) * 0x100000;
728 * hns_mac_get_cfg - get mac cfg from dtb or acpi table
729 * @dsaf_dev: dsa fabric device struct pointer
730 * @mac_idx: mac index
731 * retuen 0 - success , negative --fail
733 int hns_mac_get_cfg(struct dsaf_device *dsaf_dev, int mac_idx)
737 struct hns_mac_cb *mac_cb = &dsaf_dev->mac_cb[mac_idx];
739 mac_cb->dsaf_dev = dsaf_dev;
740 mac_cb->dev = dsaf_dev->dev;
741 mac_cb->mac_id = mac_idx;
743 mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base;
744 mac_cb->serdes_vaddr = dsaf_dev->sds_base;
746 if (dsaf_dev->cpld_base &&
747 mac_idx < DSAF_SERVICE_PORT_NUM_PER_DSAF)
748 mac_cb->cpld_vaddr = dsaf_dev->cpld_base +
749 mac_cb->mac_id * CPLD_ADDR_PORT_OFFSET;
750 mac_cb->sfp_prsnt = 0;
751 mac_cb->txpkt_for_led = 0;
752 mac_cb->rxpkt_for_led = 0;
754 if (mac_idx < DSAF_SERVICE_PORT_NUM_PER_DSAF)
755 mac_cb->mac_type = HNAE_PORT_SERVICE;
757 mac_cb->mac_type = HNAE_PORT_DEBUG;
759 mac_cb->phy_if = hns_mac_get_phy_if(mac_cb);
761 ret = hns_mac_get_mode(mac_cb->phy_if);
763 dev_err(dsaf_dev->dev,
764 "hns_mac_get_mode failed,mac%d ret = %#x!\n",
765 mac_cb->mac_id, ret);
768 mac_mode_idx = (u32)ret;
770 hns_mac_get_info(mac_cb, mac_cb->dev->of_node, mac_mode_idx);
772 mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx);
778 * hns_mac_init - init mac
779 * @dsaf_dev: dsa fabric device struct pointer
780 * retuen 0 - success , negative --fail
782 int hns_mac_init(struct dsaf_device *dsaf_dev)
787 struct hns_mac_cb *mac_cb;
789 size = sizeof(struct hns_mac_cb) * DSAF_MAX_PORT_NUM_PER_CHIP;
790 dsaf_dev->mac_cb = devm_kzalloc(dsaf_dev->dev, size, GFP_KERNEL);
791 if (!dsaf_dev->mac_cb)
794 for (i = 0; i < DSAF_MAX_PORT_NUM_PER_CHIP; i++) {
795 ret = hns_mac_get_cfg(dsaf_dev, i);
799 mac_cb = &dsaf_dev->mac_cb[i];
800 ret = hns_mac_init_ex(mac_cb);
808 dsaf_dev->mac_cb = NULL;
813 void hns_mac_uninit(struct dsaf_device *dsaf_dev)
815 cpld_led_reset(dsaf_dev->mac_cb);
816 dsaf_dev->mac_cb = NULL;
819 int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb,
820 enum hnae_loop loop, int en)
823 struct mac_driver *drv = hns_mac_get_drv(mac_cb);
825 if (drv->config_loopback)
826 ret = drv->config_loopback(drv, loop, en);
833 void hns_mac_update_stats(struct hns_mac_cb *mac_cb)
835 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
837 mac_ctrl_drv->update_stats(mac_ctrl_drv);
840 void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data)
842 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
844 mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data);
847 void hns_mac_get_strings(struct hns_mac_cb *mac_cb,
848 int stringset, u8 *data)
850 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
852 mac_ctrl_drv->get_strings(stringset, data);
855 int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset)
857 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
859 return mac_ctrl_drv->get_sset_count(stringset);
862 int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb)
864 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
866 return mac_ctrl_drv->get_regs_count();
869 void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data)
871 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
873 mac_ctrl_drv->get_regs(mac_ctrl_drv, data);
876 void hns_set_led_opt(struct hns_mac_cb *mac_cb)
881 txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts;
882 rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts;
883 if (txpkts || rxpkts)
887 mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts;
888 mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts;
889 hns_cpld_set_led(mac_cb, (int)mac_cb->link,
890 mac_cb->speed, nic_data);
893 int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb,
894 enum hnae_led_state status)
896 if (!mac_cb || !mac_cb->cpld_vaddr)
899 return cpld_set_led_id(mac_cb, status);