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