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