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