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