GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / ethernet / hisilicon / hns / hns_dsaf_mac.c
1 /*
2  * Copyright (c) 2014-2015 Hisilicon Limited.
3  *
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.
8  */
9
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>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_mdio.h>
20 #include <linux/phy.h>
21 #include <linux/platform_device.h>
22
23 #include "hns_dsaf_main.h"
24 #include "hns_dsaf_misc.h"
25 #include "hns_dsaf_rcb.h"
26
27 #define MAC_EN_FLAG_V           0xada0328
28
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
41 };
42
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
46 };
47
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
57 };
58
59 static enum mac_mode hns_get_enet_interface(const struct hns_mac_cb *mac_cb)
60 {
61         switch (mac_cb->max_speed) {
62         case MAC_SPEED_100:
63                 return g_mac_mode_100[mac_cb->phy_if];
64         case MAC_SPEED_1000:
65                 return g_mac_mode_1000[mac_cb->phy_if];
66         case MAC_SPEED_10000:
67                 return MAC_MODE_XGMII_10000;
68         default:
69                 return MAC_MODE_MII_100;
70         }
71 }
72
73 void hns_mac_get_link_status(struct hns_mac_cb *mac_cb, u32 *link_status)
74 {
75         struct mac_driver *mac_ctrl_drv;
76         int ret, sfp_prsnt;
77
78         mac_ctrl_drv = hns_mac_get_drv(mac_cb);
79
80         if (mac_ctrl_drv->get_link_status)
81                 mac_ctrl_drv->get_link_status(mac_ctrl_drv, link_status);
82         else
83                 *link_status = 0;
84
85         if (mac_cb->media_type == HNAE_MEDIA_TYPE_FIBER) {
86                 ret = mac_cb->dsaf_dev->misc_op->get_sfp_prsnt(mac_cb,
87                                                                &sfp_prsnt);
88                 if (!ret)
89                         *link_status = *link_status && sfp_prsnt;
90         }
91
92         mac_cb->link = *link_status;
93 }
94
95 int hns_mac_get_port_info(struct hns_mac_cb *mac_cb,
96                           u8 *auto_neg, u16 *speed, u8 *duplex)
97 {
98         struct mac_driver *mac_ctrl_drv;
99         struct mac_info    info;
100
101         mac_ctrl_drv = hns_mac_get_drv(mac_cb);
102
103         if (!mac_ctrl_drv->get_info)
104                 return -ENODEV;
105
106         mac_ctrl_drv->get_info(mac_ctrl_drv, &info);
107         if (auto_neg)
108                 *auto_neg = info.auto_neg;
109         if (speed)
110                 *speed = info.speed;
111         if (duplex)
112                 *duplex = info.duplex;
113
114         return 0;
115 }
116
117 /**
118  *hns_mac_is_adjust_link - check is need change mac speed and duplex register
119  *@mac_cb: mac device
120  *@speed: phy device speed
121  *@duplex:phy device duplex
122  *
123  */
124 bool hns_mac_need_adjust_link(struct hns_mac_cb *mac_cb, int speed, int duplex)
125 {
126         struct mac_driver *mac_ctrl_drv;
127
128         mac_ctrl_drv = (struct mac_driver *)(mac_cb->priv.mac);
129
130         if (mac_ctrl_drv->need_adjust_link)
131                 return mac_ctrl_drv->need_adjust_link(mac_ctrl_drv,
132                         (enum mac_speed)speed, duplex);
133         else
134                 return true;
135 }
136
137 void hns_mac_adjust_link(struct hns_mac_cb *mac_cb, int speed, int duplex)
138 {
139         int ret;
140         struct mac_driver *mac_ctrl_drv;
141
142         mac_ctrl_drv = (struct mac_driver *)(mac_cb->priv.mac);
143
144         mac_cb->speed = speed;
145         mac_cb->half_duplex = !duplex;
146
147         if (mac_ctrl_drv->adjust_link) {
148                 ret = mac_ctrl_drv->adjust_link(mac_ctrl_drv,
149                         (enum mac_speed)speed, duplex);
150                 if (ret) {
151                         dev_err(mac_cb->dev,
152                                 "adjust_link failed, %s mac%d ret = %#x!\n",
153                                 mac_cb->dsaf_dev->ae_dev.name,
154                                 mac_cb->mac_id, ret);
155                         return;
156                 }
157         }
158 }
159
160 /**
161  *hns_mac_get_inner_port_num - get mac table inner port number
162  *@mac_cb: mac device
163  *@vmid: vm id
164  *@port_num:port number
165  *
166  */
167 int hns_mac_get_inner_port_num(struct hns_mac_cb *mac_cb, u8 vmid, u8 *port_num)
168 {
169         int q_num_per_vf, vf_num_per_port;
170         int vm_queue_id;
171         u8 tmp_port;
172
173         if (mac_cb->dsaf_dev->dsaf_mode <= DSAF_MODE_ENABLE) {
174                 if (mac_cb->mac_id != DSAF_MAX_PORT_NUM) {
175                         dev_err(mac_cb->dev,
176                                 "input invalid, %s mac%d vmid%d !\n",
177                                 mac_cb->dsaf_dev->ae_dev.name,
178                                 mac_cb->mac_id, vmid);
179                         return -EINVAL;
180                 }
181         } else if (mac_cb->dsaf_dev->dsaf_mode < DSAF_MODE_MAX) {
182                 if (mac_cb->mac_id >= DSAF_MAX_PORT_NUM) {
183                         dev_err(mac_cb->dev,
184                                 "input invalid, %s mac%d vmid%d!\n",
185                                 mac_cb->dsaf_dev->ae_dev.name,
186                                 mac_cb->mac_id, vmid);
187                         return -EINVAL;
188                 }
189         } else {
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);
192                 return -EINVAL;
193         }
194
195         if (vmid >= mac_cb->dsaf_dev->rcb_common[0]->max_vfn) {
196                 dev_err(mac_cb->dev, "input invalid, %s mac%d vmid%d !\n",
197                         mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id, vmid);
198                 return -EINVAL;
199         }
200
201         q_num_per_vf = mac_cb->dsaf_dev->rcb_common[0]->max_q_per_vf;
202         vf_num_per_port = mac_cb->dsaf_dev->rcb_common[0]->max_vfn;
203
204         vm_queue_id = vmid * q_num_per_vf +
205                         vf_num_per_port * q_num_per_vf * mac_cb->mac_id;
206
207         switch (mac_cb->dsaf_dev->dsaf_mode) {
208         case DSAF_MODE_ENABLE_FIX:
209                 tmp_port = 0;
210                 break;
211         case DSAF_MODE_DISABLE_FIX:
212                 tmp_port = 0;
213                 break;
214         case DSAF_MODE_ENABLE_0VM:
215         case DSAF_MODE_ENABLE_8VM:
216         case DSAF_MODE_ENABLE_16VM:
217         case DSAF_MODE_ENABLE_32VM:
218         case DSAF_MODE_ENABLE_128VM:
219         case DSAF_MODE_DISABLE_2PORT_8VM:
220         case DSAF_MODE_DISABLE_2PORT_16VM:
221         case DSAF_MODE_DISABLE_2PORT_64VM:
222         case DSAF_MODE_DISABLE_6PORT_0VM:
223         case DSAF_MODE_DISABLE_6PORT_2VM:
224         case DSAF_MODE_DISABLE_6PORT_4VM:
225         case DSAF_MODE_DISABLE_6PORT_16VM:
226                 tmp_port = vm_queue_id;
227                 break;
228         default:
229                 dev_err(mac_cb->dev, "dsaf mode invalid, %s mac%d!\n",
230                         mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id);
231                 return -EINVAL;
232         }
233         tmp_port += DSAF_BASE_INNER_PORT_NUM;
234
235         *port_num = tmp_port;
236
237         return 0;
238 }
239
240 /**
241  *hns_mac_change_vf_addr - change vf mac address
242  *@mac_cb: mac device
243  *@vmid: vmid
244  *@addr:mac address
245  */
246 int hns_mac_change_vf_addr(struct hns_mac_cb *mac_cb,
247                            u32 vmid, char *addr)
248 {
249         int ret;
250         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
251         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
252         struct dsaf_drv_mac_single_dest_entry mac_entry;
253         struct mac_entry_idx *old_entry;
254
255         old_entry = &mac_cb->addr_entry_idx[vmid];
256         if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) {
257                 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr));
258                 mac_entry.in_vlan_id = old_entry->vlan_id;
259                 mac_entry.in_port_num = mac_cb->mac_id;
260                 ret = hns_mac_get_inner_port_num(mac_cb, (u8)vmid,
261                                                  &mac_entry.port_num);
262                 if (ret)
263                         return ret;
264
265                 if ((old_entry->valid != 0) &&
266                     (memcmp(old_entry->addr,
267                     addr, sizeof(mac_entry.addr)) != 0)) {
268                         ret = hns_dsaf_del_mac_entry(dsaf_dev,
269                                                      old_entry->vlan_id,
270                                                      mac_cb->mac_id,
271                                                      old_entry->addr);
272                         if (ret)
273                                 return ret;
274                 }
275
276                 ret = hns_dsaf_set_mac_uc_entry(dsaf_dev, &mac_entry);
277                 if (ret)
278                         return ret;
279         }
280
281         if ((mac_ctrl_drv->set_mac_addr) && (vmid == 0))
282                 mac_ctrl_drv->set_mac_addr(mac_cb->priv.mac, addr);
283
284         memcpy(old_entry->addr, addr, sizeof(old_entry->addr));
285         old_entry->valid = 1;
286         return 0;
287 }
288
289 int hns_mac_add_uc_addr(struct hns_mac_cb *mac_cb, u8 vf_id,
290                         const unsigned char *addr)
291 {
292         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
293         struct dsaf_drv_mac_single_dest_entry mac_entry;
294         int ret;
295
296         if (HNS_DSAF_IS_DEBUG(dsaf_dev))
297                 return -ENOSPC;
298
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);
303         if (ret)
304                 return ret;
305
306         return hns_dsaf_set_mac_uc_entry(dsaf_dev, &mac_entry);
307 }
308
309 int hns_mac_rm_uc_addr(struct hns_mac_cb *mac_cb, u8 vf_id,
310                        const unsigned char *addr)
311 {
312         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
313         struct dsaf_drv_mac_single_dest_entry mac_entry;
314         int ret;
315
316         if (HNS_DSAF_IS_DEBUG(dsaf_dev))
317                 return -ENOSPC;
318
319         memset(&mac_entry, 0, sizeof(mac_entry));
320         memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr));
321         mac_entry.in_port_num = mac_cb->mac_id;
322         ret = hns_mac_get_inner_port_num(mac_cb, vf_id, &mac_entry.port_num);
323         if (ret)
324                 return ret;
325
326         return hns_dsaf_rm_mac_addr(dsaf_dev, &mac_entry);
327 }
328
329 int hns_mac_set_multi(struct hns_mac_cb *mac_cb,
330                       u32 port_num, char *addr, bool enable)
331 {
332         int ret;
333         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
334         struct dsaf_drv_mac_single_dest_entry mac_entry;
335
336         if (!HNS_DSAF_IS_DEBUG(dsaf_dev) && addr) {
337                 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr));
338                 mac_entry.in_vlan_id = 0;/*vlan_id;*/
339                 mac_entry.in_port_num = mac_cb->mac_id;
340                 mac_entry.port_num = port_num;
341
342                 if (!enable)
343                         ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry);
344                 else
345                         ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry);
346                 if (ret) {
347                         dev_err(dsaf_dev->dev,
348                                 "set mac mc port failed, %s mac%d ret = %#x!\n",
349                                 mac_cb->dsaf_dev->ae_dev.name,
350                                 mac_cb->mac_id, ret);
351                         return ret;
352                 }
353         }
354
355         return 0;
356 }
357
358 int hns_mac_clr_multicast(struct hns_mac_cb *mac_cb, int vfn)
359 {
360         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
361         u8 port_num;
362         int ret = hns_mac_get_inner_port_num(mac_cb, vfn, &port_num);
363
364         if (ret)
365                 return ret;
366
367         return hns_dsaf_clr_mac_mc_port(dsaf_dev, mac_cb->mac_id, port_num);
368 }
369
370 static void hns_mac_param_get(struct mac_params *param,
371                               struct hns_mac_cb *mac_cb)
372 {
373         param->vaddr = (void *)mac_cb->vaddr;
374         param->mac_mode = hns_get_enet_interface(mac_cb);
375         ether_addr_copy(param->addr, mac_cb->addr_entry_idx[0].addr);
376         param->mac_id = mac_cb->mac_id;
377         param->dev = mac_cb->dev;
378 }
379
380 /**
381  *hns_mac_queue_config_bc_en - set broadcast rx&tx enable
382  *@mac_cb: mac device
383  *@queue: queue number
384  *@en:enable
385  *retuen 0 - success , negative --fail
386  */
387 static int hns_mac_port_config_bc_en(struct hns_mac_cb *mac_cb,
388                                      u32 port_num, u16 vlan_id, bool enable)
389 {
390         int ret;
391         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
392         u8 addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
393         struct dsaf_drv_mac_single_dest_entry mac_entry;
394
395         /* directy return ok in debug network mode */
396         if (mac_cb->mac_type == HNAE_PORT_DEBUG)
397                 return 0;
398
399         if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) {
400                 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr));
401                 mac_entry.in_vlan_id = vlan_id;
402                 mac_entry.in_port_num = mac_cb->mac_id;
403                 mac_entry.port_num = port_num;
404
405                 if (!enable)
406                         ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry);
407                 else
408                         ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry);
409                 return ret;
410         }
411
412         return 0;
413 }
414
415 /**
416  *hns_mac_vm_config_bc_en - set broadcast rx&tx enable
417  *@mac_cb: mac device
418  *@vmid: vm id
419  *@en:enable
420  *retuen 0 - success , negative --fail
421  */
422 int hns_mac_vm_config_bc_en(struct hns_mac_cb *mac_cb, u32 vmid, bool enable)
423 {
424         int ret;
425         struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
426         u8 port_num;
427         u8 addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
428         struct mac_entry_idx *uc_mac_entry;
429         struct dsaf_drv_mac_single_dest_entry mac_entry;
430
431         if (mac_cb->mac_type == HNAE_PORT_DEBUG)
432                 return 0;
433
434         uc_mac_entry = &mac_cb->addr_entry_idx[vmid];
435
436         if (!HNS_DSAF_IS_DEBUG(dsaf_dev))  {
437                 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr));
438                 mac_entry.in_vlan_id = uc_mac_entry->vlan_id;
439                 mac_entry.in_port_num = mac_cb->mac_id;
440                 ret = hns_mac_get_inner_port_num(mac_cb, vmid, &port_num);
441                 if (ret)
442                         return ret;
443                 mac_entry.port_num = port_num;
444
445                 if (!enable)
446                         ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry);
447                 else
448                         ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry);
449                 return ret;
450         }
451
452         return 0;
453 }
454
455 int hns_mac_wait_fifo_clean(struct hns_mac_cb *mac_cb)
456 {
457         struct mac_driver *drv = hns_mac_get_drv(mac_cb);
458
459         if (drv->wait_fifo_clean)
460                 return drv->wait_fifo_clean(drv);
461
462         return 0;
463 }
464
465 void hns_mac_reset(struct hns_mac_cb *mac_cb)
466 {
467         struct mac_driver *drv = hns_mac_get_drv(mac_cb);
468         bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver);
469
470         drv->mac_init(drv);
471
472         if (drv->config_max_frame_length)
473                 drv->config_max_frame_length(drv, mac_cb->max_frm);
474
475         if (drv->set_tx_auto_pause_frames)
476                 drv->set_tx_auto_pause_frames(drv, mac_cb->tx_pause_frm_time);
477
478         if (drv->set_an_mode)
479                 drv->set_an_mode(drv, 1);
480
481         if (drv->mac_pausefrm_cfg) {
482                 if (mac_cb->mac_type == HNAE_PORT_DEBUG)
483                         drv->mac_pausefrm_cfg(drv, !is_ver1, !is_ver1);
484                 else /* mac rx must disable, dsaf pfc close instead of it*/
485                         drv->mac_pausefrm_cfg(drv, 0, 1);
486         }
487 }
488
489 int hns_mac_set_mtu(struct hns_mac_cb *mac_cb, u32 new_mtu, u32 buf_size)
490 {
491         struct mac_driver *drv = hns_mac_get_drv(mac_cb);
492         u32 new_frm = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
493         u32 max_frm = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver) ?
494                         MAC_MAX_MTU : MAC_MAX_MTU_V2;
495
496         if (mac_cb->mac_type == HNAE_PORT_DEBUG)
497                 max_frm = MAC_MAX_MTU_DBG;
498
499         if (new_frm > HNS_RCB_RING_MAX_BD_PER_PKT * buf_size)
500                 return -EINVAL;
501
502         if (!drv->config_max_frame_length)
503                 return -ECHILD;
504
505         /* adjust max frame to be at least the size of a standard frame */
506         if (new_frm < (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN))
507                 new_frm = (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN);
508
509         drv->config_max_frame_length(drv, new_frm);
510
511         mac_cb->max_frm = new_frm;
512
513         return 0;
514 }
515
516 void hns_mac_start(struct hns_mac_cb *mac_cb)
517 {
518         struct mac_driver *mac_drv = hns_mac_get_drv(mac_cb);
519
520         /* for virt */
521         if (mac_drv->mac_en_flg == MAC_EN_FLAG_V) {
522                 /*plus 1 when the virtual mac has been enabled */
523                 mac_drv->virt_dev_num += 1;
524                 return;
525         }
526
527         if (mac_drv->mac_enable) {
528                 mac_drv->mac_enable(mac_cb->priv.mac, MAC_COMM_MODE_RX_AND_TX);
529                 mac_drv->mac_en_flg = MAC_EN_FLAG_V;
530         }
531 }
532
533 void hns_mac_stop(struct hns_mac_cb *mac_cb)
534 {
535         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
536
537         /*modified for virtualization */
538         if (mac_ctrl_drv->virt_dev_num > 0) {
539                 mac_ctrl_drv->virt_dev_num -= 1;
540                 if (mac_ctrl_drv->virt_dev_num > 0)
541                         return;
542         }
543
544         if (mac_ctrl_drv->mac_disable)
545                 mac_ctrl_drv->mac_disable(mac_cb->priv.mac,
546                         MAC_COMM_MODE_RX_AND_TX);
547
548         mac_ctrl_drv->mac_en_flg = 0;
549         mac_cb->link = 0;
550         mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb);
551 }
552
553 /**
554  * hns_mac_get_autoneg - get auto autonegotiation
555  * @mac_cb: mac control block
556  * @enable: enable or not
557  * retuen 0 - success , negative --fail
558  */
559 void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg)
560 {
561         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
562
563         if (mac_ctrl_drv->autoneg_stat)
564                 mac_ctrl_drv->autoneg_stat(mac_ctrl_drv, auto_neg);
565         else
566                 *auto_neg = 0;
567 }
568
569 /**
570  * hns_mac_get_pauseparam - set rx & tx pause parameter
571  * @mac_cb: mac control block
572  * @rx_en: rx enable status
573  * @tx_en: tx enable status
574  * retuen 0 - success , negative --fail
575  */
576 void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en)
577 {
578         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
579
580         if (mac_ctrl_drv->get_pause_enable) {
581                 mac_ctrl_drv->get_pause_enable(mac_ctrl_drv, rx_en, tx_en);
582         } else {
583                 *rx_en = 0;
584                 *tx_en = 0;
585         }
586 }
587
588 /**
589  * hns_mac_set_autoneg - set auto autonegotiation
590  * @mac_cb: mac control block
591  * @enable: enable or not
592  * retuen 0 - success , negative --fail
593  */
594 int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable)
595 {
596         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
597
598         if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII && enable) {
599                 dev_err(mac_cb->dev, "enabling autoneg is not allowed!\n");
600                 return -ENOTSUPP;
601         }
602
603         if (mac_ctrl_drv->set_an_mode)
604                 mac_ctrl_drv->set_an_mode(mac_ctrl_drv, enable);
605
606         return 0;
607 }
608
609 /**
610  * hns_mac_set_autoneg - set rx & tx pause parameter
611  * @mac_cb: mac control block
612  * @rx_en: rx enable or not
613  * @tx_en: tx enable or not
614  * return 0 - success , negative --fail
615  */
616 int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en)
617 {
618         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
619         bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver);
620
621         if (mac_cb->mac_type == HNAE_PORT_DEBUG) {
622                 if (is_ver1 && (tx_en || rx_en)) {
623                         dev_err(mac_cb->dev, "macv1 can't enable tx/rx_pause!\n");
624                         return -EINVAL;
625                 }
626         }
627
628         if (mac_ctrl_drv->mac_pausefrm_cfg)
629                 mac_ctrl_drv->mac_pausefrm_cfg(mac_ctrl_drv, rx_en, tx_en);
630
631         return 0;
632 }
633
634 /**
635  * hns_mac_init_ex - mac init
636  * @mac_cb: mac control block
637  * retuen 0 - success , negative --fail
638  */
639 static int hns_mac_init_ex(struct hns_mac_cb *mac_cb)
640 {
641         int ret;
642         struct mac_params param;
643         struct mac_driver *drv;
644
645         hns_dsaf_fix_mac_mode(mac_cb);
646
647         memset(&param, 0, sizeof(struct mac_params));
648         hns_mac_param_get(&param, mac_cb);
649
650         if (MAC_SPEED_FROM_MODE(param.mac_mode) < MAC_SPEED_10000)
651                 drv = (struct mac_driver *)hns_gmac_config(mac_cb, &param);
652         else
653                 drv = (struct mac_driver *)hns_xgmac_config(mac_cb, &param);
654
655         if (!drv)
656                 return -ENOMEM;
657
658         mac_cb->priv.mac = (void *)drv;
659         hns_mac_reset(mac_cb);
660
661         hns_mac_adjust_link(mac_cb, mac_cb->speed, !mac_cb->half_duplex);
662
663         ret = hns_mac_port_config_bc_en(mac_cb, mac_cb->mac_id, 0, true);
664         if (ret)
665                 goto free_mac_drv;
666
667         return 0;
668
669 free_mac_drv:
670         drv->mac_free(mac_cb->priv.mac);
671         mac_cb->priv.mac = NULL;
672
673         return ret;
674 }
675
676 static int
677 hns_mac_phy_parse_addr(struct device *dev, struct fwnode_handle *fwnode)
678 {
679         u32 addr;
680         int ret;
681
682         ret = fwnode_property_read_u32(fwnode, "phy-addr", &addr);
683         if (ret) {
684                 dev_err(dev, "has invalid PHY address ret:%d\n", ret);
685                 return ret;
686         }
687
688         if (addr >= PHY_MAX_ADDR) {
689                 dev_err(dev, "PHY address %i is too large\n", addr);
690                 return -EINVAL;
691         }
692
693         return addr;
694 }
695
696 static int
697 hns_mac_register_phydev(struct mii_bus *mdio, struct hns_mac_cb *mac_cb,
698                         u32 addr)
699 {
700         struct phy_device *phy;
701         const char *phy_type;
702         bool is_c45;
703         int rc;
704
705         rc = fwnode_property_read_string(mac_cb->fw_port,
706                                          "phy-mode", &phy_type);
707         if (rc < 0)
708                 return rc;
709
710         if (!strcmp(phy_type, phy_modes(PHY_INTERFACE_MODE_XGMII)))
711                 is_c45 = 1;
712         else if (!strcmp(phy_type, phy_modes(PHY_INTERFACE_MODE_SGMII)))
713                 is_c45 = 0;
714         else
715                 return -ENODATA;
716
717         phy = get_phy_device(mdio, addr, is_c45);
718         if (!phy || IS_ERR(phy))
719                 return -EIO;
720
721         phy->irq = mdio->irq[addr];
722
723         /* All data is now stored in the phy struct;
724          * register it
725          */
726         rc = phy_device_register(phy);
727         if (rc) {
728                 phy_device_free(phy);
729                 dev_err(&mdio->dev, "registered phy fail at address %i\n",
730                         addr);
731                 return -ENODEV;
732         }
733
734         mac_cb->phy_dev = phy;
735
736         dev_dbg(&mdio->dev, "registered phy at address %i\n", addr);
737
738         return 0;
739 }
740
741 static int hns_mac_register_phy(struct hns_mac_cb *mac_cb)
742 {
743         struct acpi_reference_args args;
744         struct platform_device *pdev;
745         struct mii_bus *mii_bus;
746         int rc;
747         int addr;
748
749         /* Loop over the child nodes and register a phy_device for each one */
750         if (!to_acpi_device_node(mac_cb->fw_port))
751                 return -ENODEV;
752
753         rc = acpi_node_get_property_reference(
754                         mac_cb->fw_port, "mdio-node", 0, &args);
755         if (rc)
756                 return rc;
757
758         addr = hns_mac_phy_parse_addr(mac_cb->dev, mac_cb->fw_port);
759         if (addr < 0)
760                 return addr;
761
762         /* dev address in adev */
763         pdev = hns_dsaf_find_platform_device(acpi_fwnode_handle(args.adev));
764         if (!pdev) {
765                 dev_err(mac_cb->dev, "mac%d mdio pdev is NULL\n",
766                         mac_cb->mac_id);
767                 return  -EINVAL;
768         }
769
770         mii_bus = platform_get_drvdata(pdev);
771         if (!mii_bus) {
772                 dev_err(mac_cb->dev,
773                         "mac%d mdio is NULL, dsaf will probe again later\n",
774                         mac_cb->mac_id);
775                 return -EPROBE_DEFER;
776         }
777
778         rc = hns_mac_register_phydev(mii_bus, mac_cb, addr);
779         if (!rc)
780                 dev_dbg(mac_cb->dev, "mac%d register phy addr:%d\n",
781                         mac_cb->mac_id, addr);
782
783         return rc;
784 }
785
786 static void hns_mac_remove_phydev(struct hns_mac_cb *mac_cb)
787 {
788         if (!to_acpi_device_node(mac_cb->fw_port) || !mac_cb->phy_dev)
789                 return;
790
791         phy_device_remove(mac_cb->phy_dev);
792         phy_device_free(mac_cb->phy_dev);
793
794         mac_cb->phy_dev = NULL;
795 }
796
797 #define MAC_MEDIA_TYPE_MAX_LEN          16
798
799 static const struct {
800         enum hnae_media_type value;
801         const char *name;
802 } media_type_defs[] = {
803         {HNAE_MEDIA_TYPE_UNKNOWN,       "unknown" },
804         {HNAE_MEDIA_TYPE_FIBER,         "fiber" },
805         {HNAE_MEDIA_TYPE_COPPER,        "copper" },
806         {HNAE_MEDIA_TYPE_BACKPLANE,     "backplane" },
807 };
808
809 /**
810  *hns_mac_get_info  - get mac information from device node
811  *@mac_cb: mac device
812  *@np:device node
813  * return: 0 --success, negative --fail
814  */
815 static int hns_mac_get_info(struct hns_mac_cb *mac_cb)
816 {
817         struct device_node *np;
818         struct regmap *syscon;
819         struct of_phandle_args cpld_args;
820         const char *media_type;
821         u32 i;
822         u32 ret;
823
824         mac_cb->link = false;
825         mac_cb->half_duplex = false;
826         mac_cb->media_type = HNAE_MEDIA_TYPE_UNKNOWN;
827         mac_cb->speed = mac_phy_to_speed[mac_cb->phy_if];
828         mac_cb->max_speed = mac_cb->speed;
829
830         if (mac_cb->phy_if == PHY_INTERFACE_MODE_SGMII) {
831                 mac_cb->if_support = MAC_GMAC_SUPPORTED;
832                 mac_cb->if_support |= SUPPORTED_1000baseT_Full;
833         } else if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII) {
834                 mac_cb->if_support = SUPPORTED_10000baseR_FEC;
835                 mac_cb->if_support |= SUPPORTED_10000baseKR_Full;
836         }
837
838         mac_cb->max_frm = MAC_DEFAULT_MTU;
839         mac_cb->tx_pause_frm_time = MAC_DEFAULT_PAUSE_TIME;
840         mac_cb->port_rst_off = mac_cb->mac_id;
841         mac_cb->port_mode_off = 0;
842
843         /* if the dsaf node doesn't contain a port subnode, get phy-handle
844          * from dsaf node
845          */
846         if (!mac_cb->fw_port) {
847                 np = of_parse_phandle(mac_cb->dev->of_node, "phy-handle",
848                                       mac_cb->mac_id);
849                 mac_cb->phy_dev = of_phy_find_device(np);
850                 if (mac_cb->phy_dev) {
851                         /* refcount is held by of_phy_find_device()
852                          * if the phy_dev is found
853                          */
854                         put_device(&mac_cb->phy_dev->mdio.dev);
855
856                         dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n",
857                                 mac_cb->mac_id, np->name);
858                 }
859                 of_node_put(np);
860
861                 return 0;
862         }
863
864         if (is_of_node(mac_cb->fw_port)) {
865                 /* parse property from port subnode in dsaf */
866                 np = of_parse_phandle(to_of_node(mac_cb->fw_port),
867                                       "phy-handle", 0);
868                 mac_cb->phy_dev = of_phy_find_device(np);
869                 if (mac_cb->phy_dev) {
870                         /* refcount is held by of_phy_find_device()
871                          * if the phy_dev is found
872                          */
873                         put_device(&mac_cb->phy_dev->mdio.dev);
874                         dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n",
875                                 mac_cb->mac_id, np->name);
876                 }
877                 of_node_put(np);
878
879                 np = of_parse_phandle(to_of_node(mac_cb->fw_port),
880                                       "serdes-syscon", 0);
881                 syscon = syscon_node_to_regmap(np);
882                 of_node_put(np);
883                 if (IS_ERR_OR_NULL(syscon)) {
884                         dev_err(mac_cb->dev, "serdes-syscon is needed!\n");
885                         return -EINVAL;
886                 }
887                 mac_cb->serdes_ctrl = syscon;
888
889                 ret = fwnode_property_read_u32(mac_cb->fw_port,
890                                                "port-rst-offset",
891                                                &mac_cb->port_rst_off);
892                 if (ret) {
893                         dev_dbg(mac_cb->dev,
894                                 "mac%d port-rst-offset not found, use default value.\n",
895                                 mac_cb->mac_id);
896                 }
897
898                 ret = fwnode_property_read_u32(mac_cb->fw_port,
899                                                "port-mode-offset",
900                                                &mac_cb->port_mode_off);
901                 if (ret) {
902                         dev_dbg(mac_cb->dev,
903                                 "mac%d port-mode-offset not found, use default value.\n",
904                                 mac_cb->mac_id);
905                 }
906
907                 ret = of_parse_phandle_with_fixed_args(
908                         to_of_node(mac_cb->fw_port), "cpld-syscon", 1, 0,
909                         &cpld_args);
910                 if (ret) {
911                         dev_dbg(mac_cb->dev, "mac%d no cpld-syscon found.\n",
912                                 mac_cb->mac_id);
913                         mac_cb->cpld_ctrl = NULL;
914                 } else {
915                         syscon = syscon_node_to_regmap(cpld_args.np);
916                         if (IS_ERR_OR_NULL(syscon)) {
917                                 dev_dbg(mac_cb->dev, "no cpld-syscon found!\n");
918                                 mac_cb->cpld_ctrl = NULL;
919                         } else {
920                                 mac_cb->cpld_ctrl = syscon;
921                                 mac_cb->cpld_ctrl_reg = cpld_args.args[0];
922                         }
923                 }
924         } else if (is_acpi_node(mac_cb->fw_port)) {
925                 ret = hns_mac_register_phy(mac_cb);
926                 /*
927                  * Mac can work well if there is phy or not.If the port don't
928                  * connect with phy, the return value will be ignored. Only
929                  * when there is phy but can't find mdio bus, the return value
930                  * will be handled.
931                  */
932                 if (ret == -EPROBE_DEFER)
933                         return ret;
934         } else {
935                 dev_err(mac_cb->dev, "mac%d cannot find phy node\n",
936                         mac_cb->mac_id);
937         }
938
939         if (!fwnode_property_read_string(mac_cb->fw_port, "media-type",
940                                          &media_type)) {
941                 for (i = 0; i < ARRAY_SIZE(media_type_defs); i++) {
942                         if (!strncmp(media_type_defs[i].name, media_type,
943                                      MAC_MEDIA_TYPE_MAX_LEN)) {
944                                 mac_cb->media_type = media_type_defs[i].value;
945                                 break;
946                         }
947                 }
948         }
949
950         if (fwnode_property_read_u8_array(mac_cb->fw_port, "mc-mac-mask",
951                                           mac_cb->mc_mask, ETH_ALEN)) {
952                 dev_warn(mac_cb->dev,
953                          "no mc-mac-mask property, set to default value.\n");
954                 eth_broadcast_addr(mac_cb->mc_mask);
955         }
956
957         return 0;
958 }
959
960 /**
961  * hns_mac_get_mode - get mac mode
962  * @phy_if: phy interface
963  * retuen 0 - gmac, 1 - xgmac , negative --fail
964  */
965 static int hns_mac_get_mode(phy_interface_t phy_if)
966 {
967         switch (phy_if) {
968         case PHY_INTERFACE_MODE_SGMII:
969                 return MAC_GMAC_IDX;
970         case PHY_INTERFACE_MODE_XGMII:
971                 return MAC_XGMAC_IDX;
972         default:
973                 return -EINVAL;
974         }
975 }
976
977 u8 __iomem *hns_mac_get_vaddr(struct dsaf_device *dsaf_dev,
978                               struct hns_mac_cb *mac_cb, u32 mac_mode_idx)
979 {
980         u8 __iomem *base = dsaf_dev->io_base;
981         int mac_id = mac_cb->mac_id;
982
983         if (mac_cb->mac_type == HNAE_PORT_SERVICE)
984                 return base + 0x40000 + mac_id * 0x4000 -
985                                 mac_mode_idx * 0x20000;
986         else
987                 return dsaf_dev->ppe_base + 0x1000;
988 }
989
990 /**
991  * hns_mac_get_cfg - get mac cfg from dtb or acpi table
992  * @dsaf_dev: dsa fabric device struct pointer
993  * @mac_cb: mac control block
994  * return 0 - success , negative --fail
995  */
996 int hns_mac_get_cfg(struct dsaf_device *dsaf_dev, struct hns_mac_cb *mac_cb)
997 {
998         int ret;
999         u32 mac_mode_idx;
1000
1001         mac_cb->dsaf_dev = dsaf_dev;
1002         mac_cb->dev = dsaf_dev->dev;
1003
1004         mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base;
1005         mac_cb->serdes_vaddr = dsaf_dev->sds_base;
1006
1007         mac_cb->sfp_prsnt = 0;
1008         mac_cb->txpkt_for_led = 0;
1009         mac_cb->rxpkt_for_led = 0;
1010
1011         if (!HNS_DSAF_IS_DEBUG(dsaf_dev))
1012                 mac_cb->mac_type = HNAE_PORT_SERVICE;
1013         else
1014                 mac_cb->mac_type = HNAE_PORT_DEBUG;
1015
1016         mac_cb->phy_if = dsaf_dev->misc_op->get_phy_if(mac_cb);
1017
1018         ret = hns_mac_get_mode(mac_cb->phy_if);
1019         if (ret < 0) {
1020                 dev_err(dsaf_dev->dev,
1021                         "hns_mac_get_mode failed, mac%d ret = %#x!\n",
1022                         mac_cb->mac_id, ret);
1023                 return ret;
1024         }
1025         mac_mode_idx = (u32)ret;
1026
1027         ret  = hns_mac_get_info(mac_cb);
1028         if (ret)
1029                 return ret;
1030
1031         mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb);
1032         mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx);
1033
1034         return 0;
1035 }
1036
1037 static int hns_mac_get_max_port_num(struct dsaf_device *dsaf_dev)
1038 {
1039         if (HNS_DSAF_IS_DEBUG(dsaf_dev))
1040                 return 1;
1041         else
1042                 return  DSAF_MAX_PORT_NUM;
1043 }
1044
1045 void hns_mac_enable(struct hns_mac_cb *mac_cb, enum mac_commom_mode mode)
1046 {
1047         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1048
1049         mac_ctrl_drv->mac_enable(mac_cb->priv.mac, mode);
1050 }
1051
1052 void hns_mac_disable(struct hns_mac_cb *mac_cb, enum mac_commom_mode mode)
1053 {
1054         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1055
1056         mac_ctrl_drv->mac_disable(mac_cb->priv.mac, mode);
1057 }
1058
1059 /**
1060  * hns_mac_init - init mac
1061  * @dsaf_dev: dsa fabric device struct pointer
1062  * return 0 - success , negative --fail
1063  */
1064 int hns_mac_init(struct dsaf_device *dsaf_dev)
1065 {
1066         bool found = false;
1067         int ret;
1068         u32 port_id;
1069         int max_port_num = hns_mac_get_max_port_num(dsaf_dev);
1070         struct hns_mac_cb *mac_cb;
1071         struct fwnode_handle *child;
1072
1073         device_for_each_child_node(dsaf_dev->dev, child) {
1074                 ret = fwnode_property_read_u32(child, "reg", &port_id);
1075                 if (ret) {
1076                         dev_err(dsaf_dev->dev,
1077                                 "get reg fail, ret=%d!\n", ret);
1078                         return ret;
1079                 }
1080                 if (port_id >= max_port_num) {
1081                         dev_err(dsaf_dev->dev,
1082                                 "reg(%u) out of range!\n", port_id);
1083                         return -EINVAL;
1084                 }
1085                 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb),
1086                                       GFP_KERNEL);
1087                 if (!mac_cb)
1088                         return -ENOMEM;
1089                 mac_cb->fw_port = child;
1090                 mac_cb->mac_id = (u8)port_id;
1091                 dsaf_dev->mac_cb[port_id] = mac_cb;
1092                 found = true;
1093         }
1094
1095         /* if don't get any port subnode from dsaf node
1096          * will init all port then, this is compatible with the old dts
1097          */
1098         if (!found) {
1099                 for (port_id = 0; port_id < max_port_num; port_id++) {
1100                         mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb),
1101                                               GFP_KERNEL);
1102                         if (!mac_cb)
1103                                 return -ENOMEM;
1104
1105                         mac_cb->mac_id = port_id;
1106                         dsaf_dev->mac_cb[port_id] = mac_cb;
1107                 }
1108         }
1109
1110         /* init mac_cb for all port */
1111         for (port_id = 0; port_id < max_port_num; port_id++) {
1112                 mac_cb = dsaf_dev->mac_cb[port_id];
1113                 if (!mac_cb)
1114                         continue;
1115
1116                 ret = hns_mac_get_cfg(dsaf_dev, mac_cb);
1117                 if (ret)
1118                         return ret;
1119
1120                 ret = hns_mac_init_ex(mac_cb);
1121                 if (ret)
1122                         return ret;
1123         }
1124
1125         return 0;
1126 }
1127
1128 void hns_mac_uninit(struct dsaf_device *dsaf_dev)
1129 {
1130         int i;
1131         int max_port_num = hns_mac_get_max_port_num(dsaf_dev);
1132
1133         for (i = 0; i < max_port_num; i++) {
1134                 if (!dsaf_dev->mac_cb[i])
1135                         continue;
1136
1137                 dsaf_dev->misc_op->cpld_reset_led(dsaf_dev->mac_cb[i]);
1138                 hns_mac_remove_phydev(dsaf_dev->mac_cb[i]);
1139                 dsaf_dev->mac_cb[i] = NULL;
1140         }
1141 }
1142
1143 int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb,
1144                                 enum hnae_loop loop, int en)
1145 {
1146         int ret;
1147         struct mac_driver *drv = hns_mac_get_drv(mac_cb);
1148
1149         if (drv->config_loopback)
1150                 ret = drv->config_loopback(drv, loop, en);
1151         else
1152                 ret = -ENOTSUPP;
1153
1154         return ret;
1155 }
1156
1157 void hns_mac_update_stats(struct hns_mac_cb *mac_cb)
1158 {
1159         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1160
1161         mac_ctrl_drv->update_stats(mac_ctrl_drv);
1162 }
1163
1164 void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data)
1165 {
1166         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1167
1168         mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data);
1169 }
1170
1171 void hns_mac_get_strings(struct hns_mac_cb *mac_cb,
1172                          int stringset, u8 *data)
1173 {
1174         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1175
1176         mac_ctrl_drv->get_strings(stringset, data);
1177 }
1178
1179 int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset)
1180 {
1181         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1182
1183         return mac_ctrl_drv->get_sset_count(stringset);
1184 }
1185
1186 void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en)
1187 {
1188         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1189
1190         hns_dsaf_set_promisc_tcam(mac_cb->dsaf_dev, mac_cb->mac_id, !!en);
1191
1192         if (mac_ctrl_drv->set_promiscuous)
1193                 mac_ctrl_drv->set_promiscuous(mac_ctrl_drv, en);
1194 }
1195
1196 int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb)
1197 {
1198         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1199
1200         return mac_ctrl_drv->get_regs_count();
1201 }
1202
1203 void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data)
1204 {
1205         struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1206
1207         mac_ctrl_drv->get_regs(mac_ctrl_drv, data);
1208 }
1209
1210 void hns_set_led_opt(struct hns_mac_cb *mac_cb)
1211 {
1212         int nic_data = 0;
1213         int txpkts, rxpkts;
1214
1215         txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts;
1216         rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts;
1217         if (txpkts || rxpkts)
1218                 nic_data = 1;
1219         else
1220                 nic_data = 0;
1221         mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts;
1222         mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts;
1223         mac_cb->dsaf_dev->misc_op->cpld_set_led(mac_cb, (int)mac_cb->link,
1224                          mac_cb->speed, nic_data);
1225 }
1226
1227 int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb,
1228                         enum hnae_led_state status)
1229 {
1230         if (!mac_cb || !mac_cb->cpld_ctrl)
1231                 return 0;
1232
1233         return mac_cb->dsaf_dev->misc_op->cpld_set_led_id(mac_cb, status);
1234 }