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