1
2
3
4
5
6
7
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
29static 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
43static 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
48static 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
59static 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
73void 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
95int 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
119
120
121
122
123
124bool 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
137void 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
162
163
164
165
166
167int 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
242
243
244
245
246int 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
289int 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
309int 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
329int 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;
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
358int 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
370static void hns_mac_param_get(struct mac_params *param,
371 struct hns_mac_cb *mac_cb)
372{
373 param->vaddr = 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
382
383
384
385
386
387static 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 struct dsaf_drv_mac_single_dest_entry mac_entry;
393
394
395 if (mac_cb->mac_type == HNAE_PORT_DEBUG)
396 return 0;
397
398 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) {
399 eth_broadcast_addr(mac_entry.addr);
400 mac_entry.in_vlan_id = vlan_id;
401 mac_entry.in_port_num = mac_cb->mac_id;
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
415
416
417
418
419
420
421int hns_mac_vm_config_bc_en(struct hns_mac_cb *mac_cb, u32 vmid, bool enable)
422{
423 int ret;
424 struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
425 u8 port_num;
426 struct mac_entry_idx *uc_mac_entry;
427 struct dsaf_drv_mac_single_dest_entry mac_entry;
428
429 if (mac_cb->mac_type == HNAE_PORT_DEBUG)
430 return 0;
431
432 uc_mac_entry = &mac_cb->addr_entry_idx[vmid];
433
434 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) {
435 eth_broadcast_addr(mac_entry.addr);
436 mac_entry.in_vlan_id = uc_mac_entry->vlan_id;
437 mac_entry.in_port_num = mac_cb->mac_id;
438 ret = hns_mac_get_inner_port_num(mac_cb, vmid, &port_num);
439 if (ret)
440 return ret;
441 mac_entry.port_num = port_num;
442
443 if (!enable)
444 ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry);
445 else
446 ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry);
447 return ret;
448 }
449
450 return 0;
451}
452
453int hns_mac_wait_fifo_clean(struct hns_mac_cb *mac_cb)
454{
455 struct mac_driver *drv = hns_mac_get_drv(mac_cb);
456
457 if (drv->wait_fifo_clean)
458 return drv->wait_fifo_clean(drv);
459
460 return 0;
461}
462
463void hns_mac_reset(struct hns_mac_cb *mac_cb)
464{
465 struct mac_driver *drv = hns_mac_get_drv(mac_cb);
466 bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver);
467
468 drv->mac_init(drv);
469
470 if (drv->config_max_frame_length)
471 drv->config_max_frame_length(drv, mac_cb->max_frm);
472
473 if (drv->set_tx_auto_pause_frames)
474 drv->set_tx_auto_pause_frames(drv, mac_cb->tx_pause_frm_time);
475
476 if (drv->set_an_mode)
477 drv->set_an_mode(drv, 1);
478
479 if (drv->mac_pausefrm_cfg) {
480 if (mac_cb->mac_type == HNAE_PORT_DEBUG)
481 drv->mac_pausefrm_cfg(drv, !is_ver1, !is_ver1);
482 else
483 drv->mac_pausefrm_cfg(drv, 0, 1);
484 }
485}
486
487int hns_mac_set_mtu(struct hns_mac_cb *mac_cb, u32 new_mtu, u32 buf_size)
488{
489 struct mac_driver *drv = hns_mac_get_drv(mac_cb);
490 u32 new_frm = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
491
492 if (new_frm > HNS_RCB_RING_MAX_BD_PER_PKT * buf_size)
493 return -EINVAL;
494
495 if (!drv->config_max_frame_length)
496 return -ECHILD;
497
498
499 if (new_frm < (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN))
500 new_frm = (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN);
501
502 drv->config_max_frame_length(drv, new_frm);
503
504 mac_cb->max_frm = new_frm;
505
506 return 0;
507}
508
509void hns_mac_start(struct hns_mac_cb *mac_cb)
510{
511 struct mac_driver *mac_drv = hns_mac_get_drv(mac_cb);
512
513
514 if (mac_drv->mac_en_flg == MAC_EN_FLAG_V) {
515
516 mac_drv->virt_dev_num += 1;
517 return;
518 }
519
520 if (mac_drv->mac_enable) {
521 mac_drv->mac_enable(mac_cb->priv.mac, MAC_COMM_MODE_RX_AND_TX);
522 mac_drv->mac_en_flg = MAC_EN_FLAG_V;
523 }
524}
525
526void hns_mac_stop(struct hns_mac_cb *mac_cb)
527{
528 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
529
530
531 if (mac_ctrl_drv->virt_dev_num > 0) {
532 mac_ctrl_drv->virt_dev_num -= 1;
533 if (mac_ctrl_drv->virt_dev_num > 0)
534 return;
535 }
536
537 if (mac_ctrl_drv->mac_disable)
538 mac_ctrl_drv->mac_disable(mac_cb->priv.mac,
539 MAC_COMM_MODE_RX_AND_TX);
540
541 mac_ctrl_drv->mac_en_flg = 0;
542 mac_cb->link = 0;
543 mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb);
544}
545
546
547
548
549
550
551
552void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg)
553{
554 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
555
556 if (mac_ctrl_drv->autoneg_stat)
557 mac_ctrl_drv->autoneg_stat(mac_ctrl_drv, auto_neg);
558 else
559 *auto_neg = 0;
560}
561
562
563
564
565
566
567
568
569void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en)
570{
571 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
572
573 if (mac_ctrl_drv->get_pause_enable) {
574 mac_ctrl_drv->get_pause_enable(mac_ctrl_drv, rx_en, tx_en);
575 } else {
576 *rx_en = 0;
577 *tx_en = 0;
578 }
579}
580
581
582
583
584
585
586
587int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable)
588{
589 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
590
591 if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII && enable) {
592 dev_err(mac_cb->dev, "enabling autoneg is not allowed!\n");
593 return -ENOTSUPP;
594 }
595
596 if (mac_ctrl_drv->set_an_mode)
597 mac_ctrl_drv->set_an_mode(mac_ctrl_drv, enable);
598
599 return 0;
600}
601
602
603
604
605
606
607
608
609int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en)
610{
611 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
612 bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver);
613
614 if (mac_cb->mac_type == HNAE_PORT_DEBUG) {
615 if (is_ver1 && (tx_en || rx_en)) {
616 dev_err(mac_cb->dev, "macv1 can't enable tx/rx_pause!\n");
617 return -EINVAL;
618 }
619 }
620
621 if (mac_ctrl_drv->mac_pausefrm_cfg)
622 mac_ctrl_drv->mac_pausefrm_cfg(mac_ctrl_drv, rx_en, tx_en);
623
624 return 0;
625}
626
627
628
629
630
631
632static int hns_mac_init_ex(struct hns_mac_cb *mac_cb)
633{
634 int ret;
635 struct mac_params param;
636 struct mac_driver *drv;
637
638 hns_dsaf_fix_mac_mode(mac_cb);
639
640 memset(¶m, 0, sizeof(struct mac_params));
641 hns_mac_param_get(¶m, mac_cb);
642
643 if (MAC_SPEED_FROM_MODE(param.mac_mode) < MAC_SPEED_10000)
644 drv = (struct mac_driver *)hns_gmac_config(mac_cb, ¶m);
645 else
646 drv = (struct mac_driver *)hns_xgmac_config(mac_cb, ¶m);
647
648 if (!drv)
649 return -ENOMEM;
650
651 mac_cb->priv.mac = (void *)drv;
652 hns_mac_reset(mac_cb);
653
654 hns_mac_adjust_link(mac_cb, mac_cb->speed, !mac_cb->half_duplex);
655
656 ret = hns_mac_port_config_bc_en(mac_cb, mac_cb->mac_id, 0, true);
657 if (ret)
658 goto free_mac_drv;
659
660 return 0;
661
662free_mac_drv:
663 drv->mac_free(mac_cb->priv.mac);
664 mac_cb->priv.mac = NULL;
665
666 return ret;
667}
668
669static int
670hns_mac_phy_parse_addr(struct device *dev, struct fwnode_handle *fwnode)
671{
672 u32 addr;
673 int ret;
674
675 ret = fwnode_property_read_u32(fwnode, "phy-addr", &addr);
676 if (ret) {
677 dev_err(dev, "has invalid PHY address ret:%d\n", ret);
678 return ret;
679 }
680
681 if (addr >= PHY_MAX_ADDR) {
682 dev_err(dev, "PHY address %i is too large\n", addr);
683 return -EINVAL;
684 }
685
686 return addr;
687}
688
689static int
690hns_mac_register_phydev(struct mii_bus *mdio, struct hns_mac_cb *mac_cb,
691 u32 addr)
692{
693 struct phy_device *phy;
694 const char *phy_type;
695 bool is_c45;
696 int rc;
697
698 rc = fwnode_property_read_string(mac_cb->fw_port,
699 "phy-mode", &phy_type);
700 if (rc < 0)
701 return rc;
702
703 if (!strcmp(phy_type, phy_modes(PHY_INTERFACE_MODE_XGMII)))
704 is_c45 = 1;
705 else if (!strcmp(phy_type, phy_modes(PHY_INTERFACE_MODE_SGMII)))
706 is_c45 = 0;
707 else
708 return -ENODATA;
709
710 phy = get_phy_device(mdio, addr, is_c45);
711 if (!phy || IS_ERR(phy))
712 return -EIO;
713
714 phy->irq = mdio->irq[addr];
715
716
717
718
719 rc = phy_device_register(phy);
720 if (rc) {
721 phy_device_free(phy);
722 dev_err(&mdio->dev, "registered phy fail at address %i\n",
723 addr);
724 return -ENODEV;
725 }
726
727 mac_cb->phy_dev = phy;
728
729 dev_dbg(&mdio->dev, "registered phy at address %i\n", addr);
730
731 return 0;
732}
733
734static int hns_mac_register_phy(struct hns_mac_cb *mac_cb)
735{
736 struct fwnode_reference_args args;
737 struct platform_device *pdev;
738 struct mii_bus *mii_bus;
739 int rc;
740 int addr;
741
742
743 if (!to_acpi_device_node(mac_cb->fw_port))
744 return -ENODEV;
745
746 rc = acpi_node_get_property_reference(
747 mac_cb->fw_port, "mdio-node", 0, &args);
748 if (rc)
749 return rc;
750 if (!is_acpi_device_node(args.fwnode))
751 return -EINVAL;
752
753 addr = hns_mac_phy_parse_addr(mac_cb->dev, mac_cb->fw_port);
754 if (addr < 0)
755 return addr;
756
757
758 pdev = hns_dsaf_find_platform_device(args.fwnode);
759 if (!pdev) {
760 dev_err(mac_cb->dev, "mac%d mdio pdev is NULL\n",
761 mac_cb->mac_id);
762 return -EINVAL;
763 }
764
765 mii_bus = platform_get_drvdata(pdev);
766 if (!mii_bus) {
767 dev_err(mac_cb->dev,
768 "mac%d mdio is NULL, dsaf will probe again later\n",
769 mac_cb->mac_id);
770 return -EPROBE_DEFER;
771 }
772
773 rc = hns_mac_register_phydev(mii_bus, mac_cb, addr);
774 if (!rc)
775 dev_dbg(mac_cb->dev, "mac%d register phy addr:%d\n",
776 mac_cb->mac_id, addr);
777
778 return rc;
779}
780
781static void hns_mac_remove_phydev(struct hns_mac_cb *mac_cb)
782{
783 if (!to_acpi_device_node(mac_cb->fw_port) || !mac_cb->phy_dev)
784 return;
785
786 phy_device_remove(mac_cb->phy_dev);
787 phy_device_free(mac_cb->phy_dev);
788
789 mac_cb->phy_dev = NULL;
790}
791
792#define MAC_MEDIA_TYPE_MAX_LEN 16
793
794static const struct {
795 enum hnae_media_type value;
796 const char *name;
797} media_type_defs[] = {
798 {HNAE_MEDIA_TYPE_UNKNOWN, "unknown" },
799 {HNAE_MEDIA_TYPE_FIBER, "fiber" },
800 {HNAE_MEDIA_TYPE_COPPER, "copper" },
801 {HNAE_MEDIA_TYPE_BACKPLANE, "backplane" },
802};
803
804
805
806
807
808
809
810static int hns_mac_get_info(struct hns_mac_cb *mac_cb)
811{
812 struct device_node *np;
813 struct regmap *syscon;
814 struct of_phandle_args cpld_args;
815 const char *media_type;
816 u32 i;
817 u32 ret;
818
819 mac_cb->link = false;
820 mac_cb->half_duplex = false;
821 mac_cb->media_type = HNAE_MEDIA_TYPE_UNKNOWN;
822 mac_cb->speed = mac_phy_to_speed[mac_cb->phy_if];
823 mac_cb->max_speed = mac_cb->speed;
824
825 if (mac_cb->phy_if == PHY_INTERFACE_MODE_SGMII) {
826 mac_cb->if_support = MAC_GMAC_SUPPORTED;
827 mac_cb->if_support |= SUPPORTED_1000baseT_Full;
828 } else if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII) {
829 mac_cb->if_support = SUPPORTED_10000baseR_FEC;
830 mac_cb->if_support |= SUPPORTED_10000baseKR_Full;
831 }
832
833 mac_cb->max_frm = MAC_DEFAULT_MTU;
834 mac_cb->tx_pause_frm_time = MAC_DEFAULT_PAUSE_TIME;
835 mac_cb->port_rst_off = mac_cb->mac_id;
836 mac_cb->port_mode_off = 0;
837
838
839
840
841 if (!mac_cb->fw_port) {
842 np = of_parse_phandle(mac_cb->dev->of_node, "phy-handle",
843 mac_cb->mac_id);
844 mac_cb->phy_dev = of_phy_find_device(np);
845 if (mac_cb->phy_dev) {
846
847
848
849 put_device(&mac_cb->phy_dev->mdio.dev);
850
851 dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n",
852 mac_cb->mac_id, np->name);
853 }
854 of_node_put(np);
855
856 return 0;
857 }
858
859 if (is_of_node(mac_cb->fw_port)) {
860
861 np = of_parse_phandle(to_of_node(mac_cb->fw_port),
862 "phy-handle", 0);
863 mac_cb->phy_dev = of_phy_find_device(np);
864 if (mac_cb->phy_dev) {
865
866
867
868 put_device(&mac_cb->phy_dev->mdio.dev);
869 dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n",
870 mac_cb->mac_id, np->name);
871 }
872 of_node_put(np);
873
874 np = of_parse_phandle(to_of_node(mac_cb->fw_port),
875 "serdes-syscon", 0);
876 syscon = syscon_node_to_regmap(np);
877 of_node_put(np);
878 if (IS_ERR_OR_NULL(syscon)) {
879 dev_err(mac_cb->dev, "serdes-syscon is needed!\n");
880 return -EINVAL;
881 }
882 mac_cb->serdes_ctrl = syscon;
883
884 ret = fwnode_property_read_u32(mac_cb->fw_port,
885 "port-rst-offset",
886 &mac_cb->port_rst_off);
887 if (ret) {
888 dev_dbg(mac_cb->dev,
889 "mac%d port-rst-offset not found, use default value.\n",
890 mac_cb->mac_id);
891 }
892
893 ret = fwnode_property_read_u32(mac_cb->fw_port,
894 "port-mode-offset",
895 &mac_cb->port_mode_off);
896 if (ret) {
897 dev_dbg(mac_cb->dev,
898 "mac%d port-mode-offset not found, use default value.\n",
899 mac_cb->mac_id);
900 }
901
902 ret = of_parse_phandle_with_fixed_args(
903 to_of_node(mac_cb->fw_port), "cpld-syscon", 1, 0,
904 &cpld_args);
905 if (ret) {
906 dev_dbg(mac_cb->dev, "mac%d no cpld-syscon found.\n",
907 mac_cb->mac_id);
908 mac_cb->cpld_ctrl = NULL;
909 } else {
910 syscon = syscon_node_to_regmap(cpld_args.np);
911 if (IS_ERR_OR_NULL(syscon)) {
912 dev_dbg(mac_cb->dev, "no cpld-syscon found!\n");
913 mac_cb->cpld_ctrl = NULL;
914 } else {
915 mac_cb->cpld_ctrl = syscon;
916 mac_cb->cpld_ctrl_reg = cpld_args.args[0];
917 }
918 }
919 } else if (is_acpi_node(mac_cb->fw_port)) {
920 ret = hns_mac_register_phy(mac_cb);
921
922
923
924
925
926
927 if (ret == -EPROBE_DEFER)
928 return ret;
929 } else {
930 dev_err(mac_cb->dev, "mac%d cannot find phy node\n",
931 mac_cb->mac_id);
932 }
933
934 if (!fwnode_property_read_string(mac_cb->fw_port, "media-type",
935 &media_type)) {
936 for (i = 0; i < ARRAY_SIZE(media_type_defs); i++) {
937 if (!strncmp(media_type_defs[i].name, media_type,
938 MAC_MEDIA_TYPE_MAX_LEN)) {
939 mac_cb->media_type = media_type_defs[i].value;
940 break;
941 }
942 }
943 }
944
945 if (fwnode_property_read_u8_array(mac_cb->fw_port, "mc-mac-mask",
946 mac_cb->mc_mask, ETH_ALEN)) {
947 dev_warn(mac_cb->dev,
948 "no mc-mac-mask property, set to default value.\n");
949 eth_broadcast_addr(mac_cb->mc_mask);
950 }
951
952 return 0;
953}
954
955
956
957
958
959
960static int hns_mac_get_mode(phy_interface_t phy_if)
961{
962 switch (phy_if) {
963 case PHY_INTERFACE_MODE_SGMII:
964 return MAC_GMAC_IDX;
965 case PHY_INTERFACE_MODE_XGMII:
966 return MAC_XGMAC_IDX;
967 default:
968 return -EINVAL;
969 }
970}
971
972static u8 __iomem *
973hns_mac_get_vaddr(struct dsaf_device *dsaf_dev,
974 struct hns_mac_cb *mac_cb, u32 mac_mode_idx)
975{
976 u8 __iomem *base = dsaf_dev->io_base;
977 int mac_id = mac_cb->mac_id;
978
979 if (mac_cb->mac_type == HNAE_PORT_SERVICE)
980 return base + 0x40000 + mac_id * 0x4000 -
981 mac_mode_idx * 0x20000;
982 else
983 return dsaf_dev->ppe_base + 0x1000;
984}
985
986
987
988
989
990
991
992static int
993hns_mac_get_cfg(struct dsaf_device *dsaf_dev, struct hns_mac_cb *mac_cb)
994{
995 int ret;
996 u32 mac_mode_idx;
997
998 mac_cb->dsaf_dev = dsaf_dev;
999 mac_cb->dev = dsaf_dev->dev;
1000
1001 mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base;
1002 mac_cb->serdes_vaddr = dsaf_dev->sds_base;
1003
1004 mac_cb->sfp_prsnt = 0;
1005 mac_cb->txpkt_for_led = 0;
1006 mac_cb->rxpkt_for_led = 0;
1007
1008 if (!HNS_DSAF_IS_DEBUG(dsaf_dev))
1009 mac_cb->mac_type = HNAE_PORT_SERVICE;
1010 else
1011 mac_cb->mac_type = HNAE_PORT_DEBUG;
1012
1013 mac_cb->phy_if = dsaf_dev->misc_op->get_phy_if(mac_cb);
1014
1015 ret = hns_mac_get_mode(mac_cb->phy_if);
1016 if (ret < 0) {
1017 dev_err(dsaf_dev->dev,
1018 "hns_mac_get_mode failed, mac%d ret = %#x!\n",
1019 mac_cb->mac_id, ret);
1020 return ret;
1021 }
1022 mac_mode_idx = (u32)ret;
1023
1024 ret = hns_mac_get_info(mac_cb);
1025 if (ret)
1026 return ret;
1027
1028 mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb);
1029 mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx);
1030
1031 return 0;
1032}
1033
1034static int hns_mac_get_max_port_num(struct dsaf_device *dsaf_dev)
1035{
1036 if (HNS_DSAF_IS_DEBUG(dsaf_dev))
1037 return 1;
1038 else
1039 return DSAF_MAX_PORT_NUM;
1040}
1041
1042void hns_mac_enable(struct hns_mac_cb *mac_cb, enum mac_commom_mode mode)
1043{
1044 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1045
1046 mac_ctrl_drv->mac_enable(mac_cb->priv.mac, mode);
1047}
1048
1049void hns_mac_disable(struct hns_mac_cb *mac_cb, enum mac_commom_mode mode)
1050{
1051 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1052
1053 mac_ctrl_drv->mac_disable(mac_cb->priv.mac, mode);
1054}
1055
1056
1057
1058
1059
1060
1061int hns_mac_init(struct dsaf_device *dsaf_dev)
1062{
1063 bool found = false;
1064 int ret;
1065 u32 port_id;
1066 int max_port_num = hns_mac_get_max_port_num(dsaf_dev);
1067 struct hns_mac_cb *mac_cb;
1068 struct fwnode_handle *child;
1069
1070 device_for_each_child_node(dsaf_dev->dev, child) {
1071 ret = fwnode_property_read_u32(child, "reg", &port_id);
1072 if (ret) {
1073 dev_err(dsaf_dev->dev,
1074 "get reg fail, ret=%d!\n", ret);
1075 return ret;
1076 }
1077 if (port_id >= max_port_num) {
1078 dev_err(dsaf_dev->dev,
1079 "reg(%u) out of range!\n", port_id);
1080 return -EINVAL;
1081 }
1082 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb),
1083 GFP_KERNEL);
1084 if (!mac_cb)
1085 return -ENOMEM;
1086 mac_cb->fw_port = child;
1087 mac_cb->mac_id = (u8)port_id;
1088 dsaf_dev->mac_cb[port_id] = mac_cb;
1089 found = true;
1090 }
1091
1092
1093
1094
1095 if (!found) {
1096 for (port_id = 0; port_id < max_port_num; port_id++) {
1097 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb),
1098 GFP_KERNEL);
1099 if (!mac_cb)
1100 return -ENOMEM;
1101
1102 mac_cb->mac_id = port_id;
1103 dsaf_dev->mac_cb[port_id] = mac_cb;
1104 }
1105 }
1106
1107
1108 for (port_id = 0; port_id < max_port_num; port_id++) {
1109 mac_cb = dsaf_dev->mac_cb[port_id];
1110 if (!mac_cb)
1111 continue;
1112
1113 ret = hns_mac_get_cfg(dsaf_dev, mac_cb);
1114 if (ret)
1115 return ret;
1116
1117 ret = hns_mac_init_ex(mac_cb);
1118 if (ret)
1119 return ret;
1120 }
1121
1122 return 0;
1123}
1124
1125void hns_mac_uninit(struct dsaf_device *dsaf_dev)
1126{
1127 int i;
1128 int max_port_num = hns_mac_get_max_port_num(dsaf_dev);
1129
1130 for (i = 0; i < max_port_num; i++) {
1131 if (!dsaf_dev->mac_cb[i])
1132 continue;
1133
1134 dsaf_dev->misc_op->cpld_reset_led(dsaf_dev->mac_cb[i]);
1135 hns_mac_remove_phydev(dsaf_dev->mac_cb[i]);
1136 dsaf_dev->mac_cb[i] = NULL;
1137 }
1138}
1139
1140int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb,
1141 enum hnae_loop loop, int en)
1142{
1143 int ret;
1144 struct mac_driver *drv = hns_mac_get_drv(mac_cb);
1145
1146 if (drv->config_loopback)
1147 ret = drv->config_loopback(drv, loop, en);
1148 else
1149 ret = -ENOTSUPP;
1150
1151 return ret;
1152}
1153
1154void hns_mac_update_stats(struct hns_mac_cb *mac_cb)
1155{
1156 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1157
1158 mac_ctrl_drv->update_stats(mac_ctrl_drv);
1159}
1160
1161void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data)
1162{
1163 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1164
1165 mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data);
1166}
1167
1168void hns_mac_get_strings(struct hns_mac_cb *mac_cb,
1169 int stringset, u8 *data)
1170{
1171 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1172
1173 mac_ctrl_drv->get_strings(stringset, data);
1174}
1175
1176int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset)
1177{
1178 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1179
1180 return mac_ctrl_drv->get_sset_count(stringset);
1181}
1182
1183void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en)
1184{
1185 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1186
1187 hns_dsaf_set_promisc_tcam(mac_cb->dsaf_dev, mac_cb->mac_id, !!en);
1188
1189 if (mac_ctrl_drv->set_promiscuous)
1190 mac_ctrl_drv->set_promiscuous(mac_ctrl_drv, en);
1191}
1192
1193int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb)
1194{
1195 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1196
1197 return mac_ctrl_drv->get_regs_count();
1198}
1199
1200void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data)
1201{
1202 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb);
1203
1204 mac_ctrl_drv->get_regs(mac_ctrl_drv, data);
1205}
1206
1207void hns_set_led_opt(struct hns_mac_cb *mac_cb)
1208{
1209 int nic_data = 0;
1210 int txpkts, rxpkts;
1211
1212 txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts;
1213 rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts;
1214 if (txpkts || rxpkts)
1215 nic_data = 1;
1216 else
1217 nic_data = 0;
1218 mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts;
1219 mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts;
1220 mac_cb->dsaf_dev->misc_op->cpld_set_led(mac_cb, (int)mac_cb->link,
1221 mac_cb->speed, nic_data);
1222}
1223
1224int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb,
1225 enum hnae_led_state status)
1226{
1227 if (!mac_cb)
1228 return 0;
1229
1230 return mac_cb->dsaf_dev->misc_op->cpld_set_led_id(mac_cb, status);
1231}
1232