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