]>
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 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/netdevice.h> | |
14 | #include <linux/phy_fixed.h> | |
15 | #include <linux/interrupt.h> | |
16 | #include <linux/platform_device.h> | |
17 | #include <linux/of.h> | |
18 | #include <linux/of_address.h> | |
19 | ||
20 | #include "hns_dsaf_misc.h" | |
21 | #include "hns_dsaf_main.h" | |
22 | #include "hns_dsaf_rcb.h" | |
23 | ||
24 | #define MAC_EN_FLAG_V 0xada0328 | |
25 | ||
26 | static const u16 mac_phy_to_speed[] = { | |
27 | [PHY_INTERFACE_MODE_MII] = MAC_SPEED_100, | |
28 | [PHY_INTERFACE_MODE_GMII] = MAC_SPEED_1000, | |
29 | [PHY_INTERFACE_MODE_SGMII] = MAC_SPEED_1000, | |
30 | [PHY_INTERFACE_MODE_TBI] = MAC_SPEED_1000, | |
31 | [PHY_INTERFACE_MODE_RMII] = MAC_SPEED_100, | |
32 | [PHY_INTERFACE_MODE_RGMII] = MAC_SPEED_1000, | |
33 | [PHY_INTERFACE_MODE_RGMII_ID] = MAC_SPEED_1000, | |
34 | [PHY_INTERFACE_MODE_RGMII_RXID] = MAC_SPEED_1000, | |
35 | [PHY_INTERFACE_MODE_RGMII_TXID] = MAC_SPEED_1000, | |
36 | [PHY_INTERFACE_MODE_RTBI] = MAC_SPEED_1000, | |
37 | [PHY_INTERFACE_MODE_XGMII] = MAC_SPEED_10000 | |
38 | }; | |
39 | ||
40 | static const enum mac_mode g_mac_mode_100[] = { | |
41 | [PHY_INTERFACE_MODE_MII] = MAC_MODE_MII_100, | |
42 | [PHY_INTERFACE_MODE_RMII] = MAC_MODE_RMII_100 | |
43 | }; | |
44 | ||
45 | static const enum mac_mode g_mac_mode_1000[] = { | |
46 | [PHY_INTERFACE_MODE_GMII] = MAC_MODE_GMII_1000, | |
47 | [PHY_INTERFACE_MODE_SGMII] = MAC_MODE_SGMII_1000, | |
48 | [PHY_INTERFACE_MODE_TBI] = MAC_MODE_TBI_1000, | |
49 | [PHY_INTERFACE_MODE_RGMII] = MAC_MODE_RGMII_1000, | |
50 | [PHY_INTERFACE_MODE_RGMII_ID] = MAC_MODE_RGMII_1000, | |
51 | [PHY_INTERFACE_MODE_RGMII_RXID] = MAC_MODE_RGMII_1000, | |
52 | [PHY_INTERFACE_MODE_RGMII_TXID] = MAC_MODE_RGMII_1000, | |
53 | [PHY_INTERFACE_MODE_RTBI] = MAC_MODE_RTBI_1000 | |
54 | }; | |
55 | ||
56 | static enum mac_mode hns_mac_dev_to_enet_if(const struct hns_mac_cb *mac_cb) | |
57 | { | |
58 | switch (mac_cb->max_speed) { | |
59 | case MAC_SPEED_100: | |
60 | return g_mac_mode_100[mac_cb->phy_if]; | |
61 | case MAC_SPEED_1000: | |
62 | return g_mac_mode_1000[mac_cb->phy_if]; | |
63 | case MAC_SPEED_10000: | |
64 | return MAC_MODE_XGMII_10000; | |
65 | default: | |
66 | return MAC_MODE_MII_100; | |
67 | } | |
68 | } | |
69 | ||
70 | static enum mac_mode hns_get_enet_interface(const struct hns_mac_cb *mac_cb) | |
71 | { | |
72 | switch (mac_cb->max_speed) { | |
73 | case MAC_SPEED_100: | |
74 | return g_mac_mode_100[mac_cb->phy_if]; | |
75 | case MAC_SPEED_1000: | |
76 | return g_mac_mode_1000[mac_cb->phy_if]; | |
77 | case MAC_SPEED_10000: | |
78 | return MAC_MODE_XGMII_10000; | |
79 | default: | |
80 | return MAC_MODE_MII_100; | |
81 | } | |
82 | } | |
83 | ||
84 | int hns_mac_get_sfp_prsnt(struct hns_mac_cb *mac_cb, int *sfp_prsnt) | |
85 | { | |
86 | if (!mac_cb->cpld_vaddr) | |
87 | return -ENODEV; | |
88 | ||
e4600d69 | 89 | *sfp_prsnt = !dsaf_read_b((u8 *)mac_cb->cpld_vaddr |
511e6bc0 | 90 | + MAC_SFP_PORT_OFFSET); |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | void hns_mac_get_link_status(struct hns_mac_cb *mac_cb, u32 *link_status) | |
96 | { | |
97 | struct mac_driver *mac_ctrl_drv; | |
98 | int ret, sfp_prsnt; | |
99 | ||
100 | mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
101 | ||
102 | if (mac_ctrl_drv->get_link_status) | |
103 | mac_ctrl_drv->get_link_status(mac_ctrl_drv, link_status); | |
104 | else | |
105 | *link_status = 0; | |
106 | ||
107 | ret = hns_mac_get_sfp_prsnt(mac_cb, &sfp_prsnt); | |
108 | if (!ret) | |
109 | *link_status = *link_status && sfp_prsnt; | |
110 | ||
111 | mac_cb->link = *link_status; | |
112 | } | |
113 | ||
114 | int hns_mac_get_port_info(struct hns_mac_cb *mac_cb, | |
115 | u8 *auto_neg, u16 *speed, u8 *duplex) | |
116 | { | |
117 | struct mac_driver *mac_ctrl_drv; | |
118 | struct mac_info info; | |
119 | ||
120 | mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
121 | ||
122 | if (!mac_ctrl_drv->get_info) | |
123 | return -ENODEV; | |
124 | ||
125 | mac_ctrl_drv->get_info(mac_ctrl_drv, &info); | |
126 | if (auto_neg) | |
127 | *auto_neg = info.auto_neg; | |
128 | if (speed) | |
129 | *speed = info.speed; | |
130 | if (duplex) | |
131 | *duplex = info.duplex; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | void hns_mac_adjust_link(struct hns_mac_cb *mac_cb, int speed, int duplex) | |
137 | { | |
138 | int ret; | |
139 | struct mac_driver *mac_ctrl_drv; | |
140 | ||
141 | mac_ctrl_drv = (struct mac_driver *)(mac_cb->priv.mac); | |
142 | ||
143 | mac_cb->speed = speed; | |
144 | mac_cb->half_duplex = !duplex; | |
145 | mac_ctrl_drv->mac_mode = hns_mac_dev_to_enet_if(mac_cb); | |
146 | ||
147 | if (mac_ctrl_drv->adjust_link) { | |
148 | ret = mac_ctrl_drv->adjust_link(mac_ctrl_drv, | |
149 | (enum mac_speed)speed, duplex); | |
150 | if (ret) { | |
151 | dev_err(mac_cb->dev, | |
152 | "adjust_link failed,%s mac%d ret = %#x!\n", | |
153 | mac_cb->dsaf_dev->ae_dev.name, | |
154 | mac_cb->mac_id, ret); | |
155 | return; | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | /** | |
161 | *hns_mac_get_inner_port_num - get mac table inner port number | |
162 | *@mac_cb: mac device | |
163 | *@vmid: vm id | |
164 | *@port_num:port number | |
165 | * | |
166 | */ | |
167 | static int hns_mac_get_inner_port_num(struct hns_mac_cb *mac_cb, | |
168 | u8 vmid, u8 *port_num) | |
169 | { | |
170 | u8 tmp_port; | |
171 | u32 comm_idx; | |
172 | ||
173 | if (mac_cb->dsaf_dev->dsaf_mode <= DSAF_MODE_ENABLE) { | |
174 | if (mac_cb->mac_id != DSAF_MAX_PORT_NUM_PER_CHIP) { | |
175 | dev_err(mac_cb->dev, | |
176 | "input invalid,%s mac%d vmid%d !\n", | |
177 | mac_cb->dsaf_dev->ae_dev.name, | |
178 | mac_cb->mac_id, vmid); | |
179 | return -EINVAL; | |
180 | } | |
181 | } else if (mac_cb->dsaf_dev->dsaf_mode < DSAF_MODE_MAX) { | |
abc2b10e | 182 | if (mac_cb->mac_id >= DSAF_MAX_PORT_NUM_PER_CHIP) { |
511e6bc0 | 183 | dev_err(mac_cb->dev, |
184 | "input invalid,%s mac%d vmid%d!\n", | |
185 | mac_cb->dsaf_dev->ae_dev.name, | |
186 | mac_cb->mac_id, vmid); | |
187 | return -EINVAL; | |
188 | } | |
189 | } else { | |
190 | dev_err(mac_cb->dev, "dsaf mode invalid,%s mac%d!\n", | |
191 | mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id); | |
192 | return -EINVAL; | |
193 | } | |
194 | ||
195 | comm_idx = hns_dsaf_get_comm_idx_by_port(mac_cb->mac_id); | |
196 | ||
197 | if (vmid >= mac_cb->dsaf_dev->rcb_common[comm_idx]->max_vfn) { | |
198 | dev_err(mac_cb->dev, "input invalid,%s mac%d vmid%d !\n", | |
199 | mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id, vmid); | |
200 | return -EINVAL; | |
201 | } | |
202 | ||
203 | switch (mac_cb->dsaf_dev->dsaf_mode) { | |
204 | case DSAF_MODE_ENABLE_FIX: | |
205 | tmp_port = 0; | |
206 | break; | |
207 | case DSAF_MODE_DISABLE_FIX: | |
208 | tmp_port = 0; | |
209 | break; | |
210 | case DSAF_MODE_ENABLE_0VM: | |
211 | case DSAF_MODE_ENABLE_8VM: | |
212 | case DSAF_MODE_ENABLE_16VM: | |
213 | case DSAF_MODE_ENABLE_32VM: | |
214 | case DSAF_MODE_ENABLE_128VM: | |
215 | case DSAF_MODE_DISABLE_2PORT_8VM: | |
216 | case DSAF_MODE_DISABLE_2PORT_16VM: | |
217 | case DSAF_MODE_DISABLE_2PORT_64VM: | |
218 | case DSAF_MODE_DISABLE_6PORT_0VM: | |
219 | case DSAF_MODE_DISABLE_6PORT_2VM: | |
220 | case DSAF_MODE_DISABLE_6PORT_4VM: | |
221 | case DSAF_MODE_DISABLE_6PORT_16VM: | |
222 | tmp_port = vmid; | |
223 | break; | |
224 | default: | |
225 | dev_err(mac_cb->dev, "dsaf mode invalid,%s mac%d!\n", | |
226 | mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id); | |
227 | return -EINVAL; | |
228 | } | |
229 | tmp_port += DSAF_BASE_INNER_PORT_NUM; | |
230 | ||
231 | *port_num = tmp_port; | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
236 | /** | |
237 | *hns_mac_get_inner_port_num - change vf mac address | |
238 | *@mac_cb: mac device | |
239 | *@vmid: vmid | |
240 | *@addr:mac address | |
241 | */ | |
242 | int hns_mac_change_vf_addr(struct hns_mac_cb *mac_cb, | |
243 | u32 vmid, char *addr) | |
244 | { | |
245 | int ret; | |
246 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
247 | struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; | |
248 | struct dsaf_drv_mac_single_dest_entry mac_entry; | |
249 | struct mac_entry_idx *old_entry; | |
250 | ||
251 | old_entry = &mac_cb->addr_entry_idx[vmid]; | |
89a44093 | 252 | if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) { |
511e6bc0 | 253 | memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); |
254 | mac_entry.in_vlan_id = old_entry->vlan_id; | |
255 | mac_entry.in_port_num = mac_cb->mac_id; | |
256 | ret = hns_mac_get_inner_port_num(mac_cb, (u8)vmid, | |
257 | &mac_entry.port_num); | |
258 | if (ret) | |
259 | return ret; | |
260 | ||
261 | if ((old_entry->valid != 0) && | |
262 | (memcmp(old_entry->addr, | |
263 | addr, sizeof(mac_entry.addr)) != 0)) { | |
264 | ret = hns_dsaf_del_mac_entry(dsaf_dev, | |
265 | old_entry->vlan_id, | |
266 | mac_cb->mac_id, | |
267 | old_entry->addr); | |
268 | if (ret) | |
269 | return ret; | |
270 | } | |
271 | ||
272 | ret = hns_dsaf_set_mac_uc_entry(dsaf_dev, &mac_entry); | |
273 | if (ret) | |
274 | return ret; | |
275 | } | |
276 | ||
277 | if ((mac_ctrl_drv->set_mac_addr) && (vmid == 0)) | |
278 | mac_ctrl_drv->set_mac_addr(mac_cb->priv.mac, addr); | |
279 | ||
280 | memcpy(old_entry->addr, addr, sizeof(old_entry->addr)); | |
281 | old_entry->valid = 1; | |
282 | return 0; | |
283 | } | |
284 | ||
285 | int hns_mac_set_multi(struct hns_mac_cb *mac_cb, | |
13ac695e | 286 | u32 port_num, char *addr, bool enable) |
511e6bc0 | 287 | { |
288 | int ret; | |
289 | struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; | |
290 | struct dsaf_drv_mac_single_dest_entry mac_entry; | |
291 | ||
89a44093 | 292 | if (!HNS_DSAF_IS_DEBUG(dsaf_dev) && addr) { |
511e6bc0 | 293 | memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); |
294 | mac_entry.in_vlan_id = 0;/*vlan_id;*/ | |
295 | mac_entry.in_port_num = mac_cb->mac_id; | |
296 | mac_entry.port_num = port_num; | |
297 | ||
13ac695e | 298 | if (!enable) |
511e6bc0 | 299 | ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry); |
300 | else | |
301 | ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry); | |
302 | if (ret) { | |
303 | dev_err(dsaf_dev->dev, | |
304 | "set mac mc port failed,%s mac%d ret = %#x!\n", | |
305 | mac_cb->dsaf_dev->ae_dev.name, | |
306 | mac_cb->mac_id, ret); | |
307 | return ret; | |
308 | } | |
309 | } | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
314 | /** | |
315 | *hns_mac_del_mac - delete mac address into dsaf table,can't delete the same | |
316 | * address twice | |
317 | *@net_dev: net device | |
318 | *@vfn : vf lan | |
319 | *@mac : mac address | |
320 | *return status | |
321 | */ | |
322 | int hns_mac_del_mac(struct hns_mac_cb *mac_cb, u32 vfn, char *mac) | |
323 | { | |
324 | struct mac_entry_idx *old_mac; | |
325 | struct dsaf_device *dsaf_dev; | |
326 | u32 ret; | |
327 | ||
328 | dsaf_dev = mac_cb->dsaf_dev; | |
329 | ||
330 | if (vfn < DSAF_MAX_VM_NUM) { | |
331 | old_mac = &mac_cb->addr_entry_idx[vfn]; | |
332 | } else { | |
333 | dev_err(mac_cb->dev, | |
334 | "vf queue is too large,%s mac%d queue = %#x!\n", | |
335 | mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id, vfn); | |
336 | return -EINVAL; | |
337 | } | |
338 | ||
339 | if (dsaf_dev) { | |
340 | ret = hns_dsaf_del_mac_entry(dsaf_dev, old_mac->vlan_id, | |
341 | mac_cb->mac_id, old_mac->addr); | |
342 | if (ret) | |
343 | return ret; | |
344 | ||
345 | if (memcmp(old_mac->addr, mac, sizeof(old_mac->addr)) == 0) | |
346 | old_mac->valid = 0; | |
347 | } | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | static void hns_mac_param_get(struct mac_params *param, | |
353 | struct hns_mac_cb *mac_cb) | |
354 | { | |
355 | param->vaddr = (void *)mac_cb->vaddr; | |
356 | param->mac_mode = hns_get_enet_interface(mac_cb); | |
357 | memcpy(param->addr, mac_cb->addr_entry_idx[0].addr, | |
358 | MAC_NUM_OCTETS_PER_ADDR); | |
359 | param->mac_id = mac_cb->mac_id; | |
360 | param->dev = mac_cb->dev; | |
361 | } | |
362 | ||
363 | /** | |
364 | *hns_mac_queue_config_bc_en - set broadcast rx&tx enable | |
365 | *@mac_cb: mac device | |
366 | *@queue: queue number | |
367 | *@en:enable | |
368 | *retuen 0 - success , negative --fail | |
369 | */ | |
370 | static int hns_mac_port_config_bc_en(struct hns_mac_cb *mac_cb, | |
13ac695e | 371 | u32 port_num, u16 vlan_id, bool enable) |
511e6bc0 | 372 | { |
373 | int ret; | |
374 | struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; | |
375 | u8 addr[MAC_NUM_OCTETS_PER_ADDR] | |
376 | = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
377 | struct dsaf_drv_mac_single_dest_entry mac_entry; | |
378 | ||
379 | /* directy return ok in debug network mode */ | |
380 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) | |
381 | return 0; | |
382 | ||
89a44093 | 383 | if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) { |
511e6bc0 | 384 | memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); |
385 | mac_entry.in_vlan_id = vlan_id; | |
386 | mac_entry.in_port_num = mac_cb->mac_id; | |
387 | mac_entry.port_num = port_num; | |
388 | ||
13ac695e | 389 | if (!enable) |
511e6bc0 | 390 | ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry); |
391 | else | |
392 | ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry); | |
393 | return ret; | |
394 | } | |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
399 | /** | |
400 | *hns_mac_vm_config_bc_en - set broadcast rx&tx enable | |
401 | *@mac_cb: mac device | |
402 | *@vmid: vm id | |
403 | *@en:enable | |
404 | *retuen 0 - success , negative --fail | |
405 | */ | |
13ac695e | 406 | int hns_mac_vm_config_bc_en(struct hns_mac_cb *mac_cb, u32 vmid, bool enable) |
511e6bc0 | 407 | { |
408 | int ret; | |
409 | struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; | |
410 | u8 port_num; | |
411 | u8 addr[MAC_NUM_OCTETS_PER_ADDR] | |
412 | = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
413 | struct mac_entry_idx *uc_mac_entry; | |
414 | struct dsaf_drv_mac_single_dest_entry mac_entry; | |
415 | ||
416 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) | |
417 | return 0; | |
418 | ||
419 | uc_mac_entry = &mac_cb->addr_entry_idx[vmid]; | |
420 | ||
89a44093 | 421 | if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) { |
511e6bc0 | 422 | memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); |
423 | mac_entry.in_vlan_id = uc_mac_entry->vlan_id; | |
424 | mac_entry.in_port_num = mac_cb->mac_id; | |
425 | ret = hns_mac_get_inner_port_num(mac_cb, vmid, &port_num); | |
426 | if (ret) | |
427 | return ret; | |
428 | mac_entry.port_num = port_num; | |
429 | ||
13ac695e | 430 | if (!enable) |
511e6bc0 | 431 | ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry); |
432 | else | |
433 | ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry); | |
434 | return ret; | |
435 | } | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | void hns_mac_reset(struct hns_mac_cb *mac_cb) | |
441 | { | |
5ada37b5 L |
442 | struct mac_driver *drv = hns_mac_get_drv(mac_cb); |
443 | bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver); | |
511e6bc0 | 444 | |
445 | drv->mac_init(drv); | |
446 | ||
447 | if (drv->config_max_frame_length) | |
448 | drv->config_max_frame_length(drv, mac_cb->max_frm); | |
449 | ||
450 | if (drv->set_tx_auto_pause_frames) | |
451 | drv->set_tx_auto_pause_frames(drv, mac_cb->tx_pause_frm_time); | |
452 | ||
453 | if (drv->set_an_mode) | |
454 | drv->set_an_mode(drv, 1); | |
455 | ||
456 | if (drv->mac_pausefrm_cfg) { | |
457 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) | |
5ada37b5 | 458 | drv->mac_pausefrm_cfg(drv, !is_ver1, !is_ver1); |
511e6bc0 | 459 | else /* mac rx must disable, dsaf pfc close instead of it*/ |
460 | drv->mac_pausefrm_cfg(drv, 0, 1); | |
461 | } | |
462 | } | |
463 | ||
464 | int hns_mac_set_mtu(struct hns_mac_cb *mac_cb, u32 new_mtu) | |
465 | { | |
466 | struct mac_driver *drv = hns_mac_get_drv(mac_cb); | |
467 | u32 buf_size = mac_cb->dsaf_dev->buf_size; | |
468 | u32 new_frm = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; | |
da3488bb KY |
469 | u32 max_frm = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver) ? |
470 | MAC_MAX_MTU : MAC_MAX_MTU_V2; | |
511e6bc0 | 471 | |
211b1384 KY |
472 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) |
473 | max_frm = MAC_MAX_MTU_DBG; | |
474 | ||
da3488bb | 475 | if ((new_mtu < MAC_MIN_MTU) || (new_frm > max_frm) || |
511e6bc0 | 476 | (new_frm > HNS_RCB_RING_MAX_BD_PER_PKT * buf_size)) |
477 | return -EINVAL; | |
478 | ||
479 | if (!drv->config_max_frame_length) | |
480 | return -ECHILD; | |
481 | ||
482 | /* adjust max frame to be at least the size of a standard frame */ | |
483 | if (new_frm < (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)) | |
484 | new_frm = (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN); | |
485 | ||
486 | drv->config_max_frame_length(drv, new_frm); | |
487 | ||
488 | mac_cb->max_frm = new_frm; | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
493 | void hns_mac_start(struct hns_mac_cb *mac_cb) | |
494 | { | |
495 | struct mac_driver *mac_drv = hns_mac_get_drv(mac_cb); | |
496 | ||
497 | /* for virt */ | |
498 | if (mac_drv->mac_en_flg == MAC_EN_FLAG_V) { | |
499 | /*plus 1 when the virtual mac has been enabled */ | |
500 | mac_drv->virt_dev_num += 1; | |
501 | return; | |
502 | } | |
503 | ||
504 | if (mac_drv->mac_enable) { | |
505 | mac_drv->mac_enable(mac_cb->priv.mac, MAC_COMM_MODE_RX_AND_TX); | |
506 | mac_drv->mac_en_flg = MAC_EN_FLAG_V; | |
507 | } | |
508 | } | |
509 | ||
510 | void hns_mac_stop(struct hns_mac_cb *mac_cb) | |
511 | { | |
512 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
513 | ||
514 | /*modified for virtualization */ | |
515 | if (mac_ctrl_drv->virt_dev_num > 0) { | |
516 | mac_ctrl_drv->virt_dev_num -= 1; | |
517 | if (mac_ctrl_drv->virt_dev_num > 0) | |
518 | return; | |
519 | } | |
520 | ||
521 | if (mac_ctrl_drv->mac_disable) | |
522 | mac_ctrl_drv->mac_disable(mac_cb->priv.mac, | |
523 | MAC_COMM_MODE_RX_AND_TX); | |
524 | ||
525 | mac_ctrl_drv->mac_en_flg = 0; | |
526 | mac_cb->link = 0; | |
527 | cpld_led_reset(mac_cb); | |
528 | } | |
529 | ||
530 | /** | |
531 | * hns_mac_get_autoneg - get auto autonegotiation | |
532 | * @mac_cb: mac control block | |
533 | * @enable: enable or not | |
534 | * retuen 0 - success , negative --fail | |
535 | */ | |
536 | void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg) | |
537 | { | |
538 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
539 | ||
540 | if (mac_ctrl_drv->autoneg_stat) | |
541 | mac_ctrl_drv->autoneg_stat(mac_ctrl_drv, auto_neg); | |
542 | else | |
543 | *auto_neg = 0; | |
544 | } | |
545 | ||
546 | /** | |
547 | * hns_mac_get_pauseparam - set rx & tx pause parameter | |
548 | * @mac_cb: mac control block | |
549 | * @rx_en: rx enable status | |
550 | * @tx_en: tx enable status | |
551 | * retuen 0 - success , negative --fail | |
552 | */ | |
553 | void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en) | |
554 | { | |
555 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
556 | ||
557 | if (mac_ctrl_drv->get_pause_enable) { | |
558 | mac_ctrl_drv->get_pause_enable(mac_ctrl_drv, rx_en, tx_en); | |
559 | } else { | |
560 | *rx_en = 0; | |
561 | *tx_en = 0; | |
562 | } | |
511e6bc0 | 563 | } |
564 | ||
565 | /** | |
566 | * hns_mac_set_autoneg - set auto autonegotiation | |
567 | * @mac_cb: mac control block | |
568 | * @enable: enable or not | |
569 | * retuen 0 - success , negative --fail | |
570 | */ | |
571 | int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable) | |
572 | { | |
573 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
574 | ||
575 | if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII && enable) { | |
576 | dev_err(mac_cb->dev, "enable autoneg is not allowed!"); | |
577 | return -ENOTSUPP; | |
578 | } | |
579 | ||
580 | if (mac_ctrl_drv->set_an_mode) | |
581 | mac_ctrl_drv->set_an_mode(mac_ctrl_drv, enable); | |
582 | ||
583 | return 0; | |
584 | } | |
585 | ||
586 | /** | |
587 | * hns_mac_set_autoneg - set rx & tx pause parameter | |
588 | * @mac_cb: mac control block | |
589 | * @rx_en: rx enable or not | |
590 | * @tx_en: tx enable or not | |
591 | * return 0 - success , negative --fail | |
592 | */ | |
593 | int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en) | |
594 | { | |
595 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
5ada37b5 | 596 | bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver); |
511e6bc0 | 597 | |
5ada37b5 L |
598 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) { |
599 | if (is_ver1 && (tx_en || rx_en)) { | |
600 | dev_err(mac_cb->dev, "macv1 cann't enable tx/rx_pause!"); | |
511e6bc0 | 601 | return -EINVAL; |
602 | } | |
511e6bc0 | 603 | } |
604 | ||
605 | if (mac_ctrl_drv->mac_pausefrm_cfg) | |
606 | mac_ctrl_drv->mac_pausefrm_cfg(mac_ctrl_drv, rx_en, tx_en); | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
611 | /** | |
612 | * hns_mac_init_ex - mac init | |
613 | * @mac_cb: mac control block | |
614 | * retuen 0 - success , negative --fail | |
615 | */ | |
616 | static int hns_mac_init_ex(struct hns_mac_cb *mac_cb) | |
617 | { | |
618 | int ret; | |
619 | struct mac_params param; | |
620 | struct mac_driver *drv; | |
621 | ||
622 | hns_dsaf_fix_mac_mode(mac_cb); | |
623 | ||
624 | memset(¶m, 0, sizeof(struct mac_params)); | |
625 | hns_mac_param_get(¶m, mac_cb); | |
626 | ||
627 | if (MAC_SPEED_FROM_MODE(param.mac_mode) < MAC_SPEED_10000) | |
628 | drv = (struct mac_driver *)hns_gmac_config(mac_cb, ¶m); | |
629 | else | |
630 | drv = (struct mac_driver *)hns_xgmac_config(mac_cb, ¶m); | |
631 | ||
632 | if (!drv) | |
633 | return -ENOMEM; | |
634 | ||
635 | mac_cb->priv.mac = (void *)drv; | |
636 | hns_mac_reset(mac_cb); | |
637 | ||
638 | hns_mac_adjust_link(mac_cb, mac_cb->speed, !mac_cb->half_duplex); | |
639 | ||
13ac695e | 640 | ret = hns_mac_port_config_bc_en(mac_cb, mac_cb->mac_id, 0, true); |
511e6bc0 | 641 | if (ret) |
642 | goto free_mac_drv; | |
643 | ||
644 | return 0; | |
645 | ||
646 | free_mac_drv: | |
647 | drv->mac_free(mac_cb->priv.mac); | |
648 | mac_cb->priv.mac = NULL; | |
649 | ||
650 | return ret; | |
651 | } | |
652 | ||
653 | /** | |
654 | *mac_free_dev - get mac information from device node | |
655 | *@mac_cb: mac device | |
656 | *@np:device node | |
657 | *@mac_mode_idx:mac mode index | |
658 | */ | |
659 | static void hns_mac_get_info(struct hns_mac_cb *mac_cb, | |
660 | struct device_node *np, u32 mac_mode_idx) | |
661 | { | |
662 | mac_cb->link = false; | |
663 | mac_cb->half_duplex = false; | |
664 | mac_cb->speed = mac_phy_to_speed[mac_cb->phy_if]; | |
665 | mac_cb->max_speed = mac_cb->speed; | |
666 | ||
667 | if (mac_cb->phy_if == PHY_INTERFACE_MODE_SGMII) { | |
668 | mac_cb->if_support = MAC_GMAC_SUPPORTED; | |
669 | mac_cb->if_support |= SUPPORTED_1000baseT_Full; | |
670 | } else if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII) { | |
671 | mac_cb->if_support = SUPPORTED_10000baseR_FEC; | |
672 | mac_cb->if_support |= SUPPORTED_10000baseKR_Full; | |
673 | } | |
674 | ||
675 | mac_cb->max_frm = MAC_DEFAULT_MTU; | |
676 | mac_cb->tx_pause_frm_time = MAC_DEFAULT_PAUSE_TIME; | |
677 | ||
678 | /* Get the rest of the PHY information */ | |
679 | mac_cb->phy_node = of_parse_phandle(np, "phy-handle", mac_cb->mac_id); | |
680 | if (mac_cb->phy_node) | |
681 | dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n", | |
682 | mac_cb->mac_id, mac_cb->phy_node->name); | |
683 | } | |
684 | ||
685 | /** | |
686 | * hns_mac_get_mode - get mac mode | |
687 | * @phy_if: phy interface | |
688 | * retuen 0 - gmac, 1 - xgmac , negative --fail | |
689 | */ | |
690 | static int hns_mac_get_mode(phy_interface_t phy_if) | |
691 | { | |
692 | switch (phy_if) { | |
693 | case PHY_INTERFACE_MODE_SGMII: | |
694 | return MAC_GMAC_IDX; | |
695 | case PHY_INTERFACE_MODE_XGMII: | |
696 | return MAC_XGMAC_IDX; | |
697 | default: | |
698 | return -EINVAL; | |
699 | } | |
700 | } | |
701 | ||
702 | u8 __iomem *hns_mac_get_vaddr(struct dsaf_device *dsaf_dev, | |
703 | struct hns_mac_cb *mac_cb, u32 mac_mode_idx) | |
704 | { | |
705 | u8 __iomem *base = dsaf_dev->io_base; | |
706 | int mac_id = mac_cb->mac_id; | |
707 | ||
708 | if (mac_cb->mac_type == HNAE_PORT_SERVICE) | |
709 | return base + 0x40000 + mac_id * 0x4000 - | |
710 | mac_mode_idx * 0x20000; | |
711 | else | |
712 | return mac_cb->serdes_vaddr + 0x1000 | |
713 | + (mac_id - DSAF_SERVICE_PORT_NUM_PER_DSAF) * 0x100000; | |
714 | } | |
715 | ||
716 | /** | |
717 | * hns_mac_get_cfg - get mac cfg from dtb or acpi table | |
718 | * @dsaf_dev: dsa fabric device struct pointer | |
719 | * @mac_idx: mac index | |
720 | * retuen 0 - success , negative --fail | |
721 | */ | |
722 | int hns_mac_get_cfg(struct dsaf_device *dsaf_dev, int mac_idx) | |
723 | { | |
724 | int ret; | |
725 | u32 mac_mode_idx; | |
726 | struct hns_mac_cb *mac_cb = &dsaf_dev->mac_cb[mac_idx]; | |
727 | ||
728 | mac_cb->dsaf_dev = dsaf_dev; | |
729 | mac_cb->dev = dsaf_dev->dev; | |
730 | mac_cb->mac_id = mac_idx; | |
731 | ||
732 | mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base; | |
733 | mac_cb->serdes_vaddr = dsaf_dev->sds_base; | |
734 | ||
735 | if (dsaf_dev->cpld_base && | |
7f0abb1f | 736 | mac_idx < DSAF_SERVICE_PORT_NUM_PER_DSAF) { |
511e6bc0 | 737 | mac_cb->cpld_vaddr = dsaf_dev->cpld_base + |
738 | mac_cb->mac_id * CPLD_ADDR_PORT_OFFSET; | |
7f0abb1f | 739 | cpld_led_reset(mac_cb); |
740 | } | |
511e6bc0 | 741 | mac_cb->sfp_prsnt = 0; |
742 | mac_cb->txpkt_for_led = 0; | |
743 | mac_cb->rxpkt_for_led = 0; | |
744 | ||
745 | if (mac_idx < DSAF_SERVICE_PORT_NUM_PER_DSAF) | |
746 | mac_cb->mac_type = HNAE_PORT_SERVICE; | |
747 | else | |
748 | mac_cb->mac_type = HNAE_PORT_DEBUG; | |
749 | ||
750 | mac_cb->phy_if = hns_mac_get_phy_if(mac_cb); | |
751 | ||
752 | ret = hns_mac_get_mode(mac_cb->phy_if); | |
753 | if (ret < 0) { | |
754 | dev_err(dsaf_dev->dev, | |
755 | "hns_mac_get_mode failed,mac%d ret = %#x!\n", | |
756 | mac_cb->mac_id, ret); | |
757 | return ret; | |
758 | } | |
759 | mac_mode_idx = (u32)ret; | |
760 | ||
761 | hns_mac_get_info(mac_cb, mac_cb->dev->of_node, mac_mode_idx); | |
762 | ||
763 | mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx); | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
768 | /** | |
769 | * hns_mac_init - init mac | |
770 | * @dsaf_dev: dsa fabric device struct pointer | |
771 | * retuen 0 - success , negative --fail | |
772 | */ | |
773 | int hns_mac_init(struct dsaf_device *dsaf_dev) | |
774 | { | |
775 | int i; | |
776 | int ret; | |
777 | size_t size; | |
778 | struct hns_mac_cb *mac_cb; | |
779 | ||
780 | size = sizeof(struct hns_mac_cb) * DSAF_MAX_PORT_NUM_PER_CHIP; | |
781 | dsaf_dev->mac_cb = devm_kzalloc(dsaf_dev->dev, size, GFP_KERNEL); | |
782 | if (!dsaf_dev->mac_cb) | |
783 | return -ENOMEM; | |
784 | ||
785 | for (i = 0; i < DSAF_MAX_PORT_NUM_PER_CHIP; i++) { | |
786 | ret = hns_mac_get_cfg(dsaf_dev, i); | |
787 | if (ret) | |
788 | goto free_mac_cb; | |
789 | ||
790 | mac_cb = &dsaf_dev->mac_cb[i]; | |
791 | ret = hns_mac_init_ex(mac_cb); | |
792 | if (ret) | |
793 | goto free_mac_cb; | |
794 | } | |
795 | ||
796 | return 0; | |
797 | ||
798 | free_mac_cb: | |
799 | dsaf_dev->mac_cb = NULL; | |
800 | ||
801 | return ret; | |
802 | } | |
803 | ||
804 | void hns_mac_uninit(struct dsaf_device *dsaf_dev) | |
805 | { | |
806 | cpld_led_reset(dsaf_dev->mac_cb); | |
807 | dsaf_dev->mac_cb = NULL; | |
808 | } | |
809 | ||
810 | int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb, | |
811 | enum hnae_loop loop, int en) | |
812 | { | |
813 | int ret; | |
814 | struct mac_driver *drv = hns_mac_get_drv(mac_cb); | |
815 | ||
816 | if (drv->config_loopback) | |
817 | ret = drv->config_loopback(drv, loop, en); | |
818 | else | |
819 | ret = -ENOTSUPP; | |
820 | ||
821 | return ret; | |
822 | } | |
823 | ||
824 | void hns_mac_update_stats(struct hns_mac_cb *mac_cb) | |
825 | { | |
826 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
827 | ||
828 | mac_ctrl_drv->update_stats(mac_ctrl_drv); | |
829 | } | |
830 | ||
831 | void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data) | |
832 | { | |
833 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
834 | ||
835 | mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data); | |
836 | } | |
837 | ||
838 | void hns_mac_get_strings(struct hns_mac_cb *mac_cb, | |
839 | int stringset, u8 *data) | |
840 | { | |
841 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
842 | ||
843 | mac_ctrl_drv->get_strings(stringset, data); | |
844 | } | |
845 | ||
846 | int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset) | |
847 | { | |
848 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
849 | ||
850 | return mac_ctrl_drv->get_sset_count(stringset); | |
851 | } | |
852 | ||
d5679849 KY |
853 | void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en) |
854 | { | |
855 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
856 | ||
857 | if (mac_ctrl_drv->set_promiscuous) | |
858 | mac_ctrl_drv->set_promiscuous(mac_ctrl_drv, en); | |
859 | } | |
860 | ||
511e6bc0 | 861 | int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb) |
862 | { | |
863 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
864 | ||
865 | return mac_ctrl_drv->get_regs_count(); | |
866 | } | |
867 | ||
868 | void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data) | |
869 | { | |
870 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
871 | ||
872 | mac_ctrl_drv->get_regs(mac_ctrl_drv, data); | |
873 | } | |
874 | ||
875 | void hns_set_led_opt(struct hns_mac_cb *mac_cb) | |
876 | { | |
877 | int nic_data = 0; | |
878 | int txpkts, rxpkts; | |
879 | ||
880 | txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts; | |
881 | rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts; | |
882 | if (txpkts || rxpkts) | |
883 | nic_data = 1; | |
884 | else | |
885 | nic_data = 0; | |
886 | mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts; | |
887 | mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts; | |
888 | hns_cpld_set_led(mac_cb, (int)mac_cb->link, | |
889 | mac_cb->speed, nic_data); | |
890 | } | |
891 | ||
892 | int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb, | |
893 | enum hnae_led_state status) | |
894 | { | |
895 | if (!mac_cb || !mac_cb->cpld_vaddr) | |
896 | return 0; | |
897 | ||
898 | return cpld_set_led_id(mac_cb, status); | |
899 | } |