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