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