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