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