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