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