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