]>
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]; | |
252 | if (dsaf_dev) { | |
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 | ||
292 | if (dsaf_dev && addr) { | |
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 | ||
383 | if (dsaf_dev) { | |
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 | ||
421 | if (dsaf_dev) { | |
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 | { | |
442 | struct mac_driver *drv; | |
443 | ||
444 | drv = hns_mac_get_drv(mac_cb); | |
445 | ||
446 | drv->mac_init(drv); | |
447 | ||
448 | if (drv->config_max_frame_length) | |
449 | drv->config_max_frame_length(drv, mac_cb->max_frm); | |
450 | ||
451 | if (drv->set_tx_auto_pause_frames) | |
452 | drv->set_tx_auto_pause_frames(drv, mac_cb->tx_pause_frm_time); | |
453 | ||
454 | if (drv->set_an_mode) | |
455 | drv->set_an_mode(drv, 1); | |
456 | ||
457 | if (drv->mac_pausefrm_cfg) { | |
458 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) | |
459 | drv->mac_pausefrm_cfg(drv, 0, 0); | |
460 | else /* mac rx must disable, dsaf pfc close instead of it*/ | |
461 | drv->mac_pausefrm_cfg(drv, 0, 1); | |
462 | } | |
463 | } | |
464 | ||
465 | int hns_mac_set_mtu(struct hns_mac_cb *mac_cb, u32 new_mtu) | |
466 | { | |
467 | struct mac_driver *drv = hns_mac_get_drv(mac_cb); | |
468 | u32 buf_size = mac_cb->dsaf_dev->buf_size; | |
469 | u32 new_frm = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; | |
da3488bb KY |
470 | u32 max_frm = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver) ? |
471 | MAC_MAX_MTU : MAC_MAX_MTU_V2; | |
511e6bc0 | 472 | |
211b1384 KY |
473 | if (mac_cb->mac_type == HNAE_PORT_DEBUG) |
474 | max_frm = MAC_MAX_MTU_DBG; | |
475 | ||
da3488bb | 476 | if ((new_mtu < MAC_MIN_MTU) || (new_frm > max_frm) || |
511e6bc0 | 477 | (new_frm > HNS_RCB_RING_MAX_BD_PER_PKT * buf_size)) |
478 | return -EINVAL; | |
479 | ||
480 | if (!drv->config_max_frame_length) | |
481 | return -ECHILD; | |
482 | ||
483 | /* adjust max frame to be at least the size of a standard frame */ | |
484 | if (new_frm < (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)) | |
485 | new_frm = (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN); | |
486 | ||
487 | drv->config_max_frame_length(drv, new_frm); | |
488 | ||
489 | mac_cb->max_frm = new_frm; | |
490 | ||
491 | return 0; | |
492 | } | |
493 | ||
494 | void hns_mac_start(struct hns_mac_cb *mac_cb) | |
495 | { | |
496 | struct mac_driver *mac_drv = hns_mac_get_drv(mac_cb); | |
497 | ||
498 | /* for virt */ | |
499 | if (mac_drv->mac_en_flg == MAC_EN_FLAG_V) { | |
500 | /*plus 1 when the virtual mac has been enabled */ | |
501 | mac_drv->virt_dev_num += 1; | |
502 | return; | |
503 | } | |
504 | ||
505 | if (mac_drv->mac_enable) { | |
506 | mac_drv->mac_enable(mac_cb->priv.mac, MAC_COMM_MODE_RX_AND_TX); | |
507 | mac_drv->mac_en_flg = MAC_EN_FLAG_V; | |
508 | } | |
509 | } | |
510 | ||
511 | void hns_mac_stop(struct hns_mac_cb *mac_cb) | |
512 | { | |
513 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
514 | ||
515 | /*modified for virtualization */ | |
516 | if (mac_ctrl_drv->virt_dev_num > 0) { | |
517 | mac_ctrl_drv->virt_dev_num -= 1; | |
518 | if (mac_ctrl_drv->virt_dev_num > 0) | |
519 | return; | |
520 | } | |
521 | ||
522 | if (mac_ctrl_drv->mac_disable) | |
523 | mac_ctrl_drv->mac_disable(mac_cb->priv.mac, | |
524 | MAC_COMM_MODE_RX_AND_TX); | |
525 | ||
526 | mac_ctrl_drv->mac_en_flg = 0; | |
527 | mac_cb->link = 0; | |
528 | cpld_led_reset(mac_cb); | |
529 | } | |
530 | ||
531 | /** | |
532 | * hns_mac_get_autoneg - get auto autonegotiation | |
533 | * @mac_cb: mac control block | |
534 | * @enable: enable or not | |
535 | * retuen 0 - success , negative --fail | |
536 | */ | |
537 | void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg) | |
538 | { | |
539 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
540 | ||
541 | if (mac_ctrl_drv->autoneg_stat) | |
542 | mac_ctrl_drv->autoneg_stat(mac_ctrl_drv, auto_neg); | |
543 | else | |
544 | *auto_neg = 0; | |
545 | } | |
546 | ||
547 | /** | |
548 | * hns_mac_get_pauseparam - set rx & tx pause parameter | |
549 | * @mac_cb: mac control block | |
550 | * @rx_en: rx enable status | |
551 | * @tx_en: tx enable status | |
552 | * retuen 0 - success , negative --fail | |
553 | */ | |
554 | void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en) | |
555 | { | |
556 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
557 | ||
558 | if (mac_ctrl_drv->get_pause_enable) { | |
559 | mac_ctrl_drv->get_pause_enable(mac_ctrl_drv, rx_en, tx_en); | |
560 | } else { | |
561 | *rx_en = 0; | |
562 | *tx_en = 0; | |
563 | } | |
564 | ||
565 | /* Due to the chip defect, the service mac's rx pause CAN'T be enabled. | |
566 | * We set the rx pause frm always be true (1), because DSAF deals with | |
567 | * the rx pause frm instead of service mac. After all, we still support | |
568 | * rx pause frm. | |
569 | */ | |
570 | if (mac_cb->mac_type == HNAE_PORT_SERVICE) | |
571 | *rx_en = 1; | |
572 | } | |
573 | ||
574 | /** | |
575 | * hns_mac_set_autoneg - set auto autonegotiation | |
576 | * @mac_cb: mac control block | |
577 | * @enable: enable or not | |
578 | * retuen 0 - success , negative --fail | |
579 | */ | |
580 | int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable) | |
581 | { | |
582 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
583 | ||
584 | if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII && enable) { | |
585 | dev_err(mac_cb->dev, "enable autoneg is not allowed!"); | |
586 | return -ENOTSUPP; | |
587 | } | |
588 | ||
589 | if (mac_ctrl_drv->set_an_mode) | |
590 | mac_ctrl_drv->set_an_mode(mac_ctrl_drv, enable); | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
595 | /** | |
596 | * hns_mac_set_autoneg - set rx & tx pause parameter | |
597 | * @mac_cb: mac control block | |
598 | * @rx_en: rx enable or not | |
599 | * @tx_en: tx enable or not | |
600 | * return 0 - success , negative --fail | |
601 | */ | |
602 | int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en) | |
603 | { | |
604 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
605 | ||
606 | if (mac_cb->mac_type == HNAE_PORT_SERVICE) { | |
607 | if (!rx_en) { | |
608 | dev_err(mac_cb->dev, "disable rx_pause is not allowed!"); | |
609 | return -EINVAL; | |
610 | } | |
611 | } else if (mac_cb->mac_type == HNAE_PORT_DEBUG) { | |
612 | if (tx_en || rx_en) { | |
613 | dev_err(mac_cb->dev, "enable tx_pause or enable rx_pause are not allowed!"); | |
614 | return -EINVAL; | |
615 | } | |
616 | } else { | |
617 | dev_err(mac_cb->dev, "Unsupport this operation!"); | |
618 | return -EINVAL; | |
619 | } | |
620 | ||
621 | if (mac_ctrl_drv->mac_pausefrm_cfg) | |
622 | mac_ctrl_drv->mac_pausefrm_cfg(mac_ctrl_drv, rx_en, tx_en); | |
623 | ||
624 | return 0; | |
625 | } | |
626 | ||
627 | /** | |
628 | * hns_mac_init_ex - mac init | |
629 | * @mac_cb: mac control block | |
630 | * retuen 0 - success , negative --fail | |
631 | */ | |
632 | static int hns_mac_init_ex(struct hns_mac_cb *mac_cb) | |
633 | { | |
634 | int ret; | |
635 | struct mac_params param; | |
636 | struct mac_driver *drv; | |
637 | ||
638 | hns_dsaf_fix_mac_mode(mac_cb); | |
639 | ||
640 | memset(¶m, 0, sizeof(struct mac_params)); | |
641 | hns_mac_param_get(¶m, mac_cb); | |
642 | ||
643 | if (MAC_SPEED_FROM_MODE(param.mac_mode) < MAC_SPEED_10000) | |
644 | drv = (struct mac_driver *)hns_gmac_config(mac_cb, ¶m); | |
645 | else | |
646 | drv = (struct mac_driver *)hns_xgmac_config(mac_cb, ¶m); | |
647 | ||
648 | if (!drv) | |
649 | return -ENOMEM; | |
650 | ||
651 | mac_cb->priv.mac = (void *)drv; | |
652 | hns_mac_reset(mac_cb); | |
653 | ||
654 | hns_mac_adjust_link(mac_cb, mac_cb->speed, !mac_cb->half_duplex); | |
655 | ||
13ac695e | 656 | ret = hns_mac_port_config_bc_en(mac_cb, mac_cb->mac_id, 0, true); |
511e6bc0 | 657 | if (ret) |
658 | goto free_mac_drv; | |
659 | ||
660 | return 0; | |
661 | ||
662 | free_mac_drv: | |
663 | drv->mac_free(mac_cb->priv.mac); | |
664 | mac_cb->priv.mac = NULL; | |
665 | ||
666 | return ret; | |
667 | } | |
668 | ||
669 | /** | |
670 | *mac_free_dev - get mac information from device node | |
671 | *@mac_cb: mac device | |
672 | *@np:device node | |
673 | *@mac_mode_idx:mac mode index | |
674 | */ | |
675 | static void hns_mac_get_info(struct hns_mac_cb *mac_cb, | |
676 | struct device_node *np, u32 mac_mode_idx) | |
677 | { | |
678 | mac_cb->link = false; | |
679 | mac_cb->half_duplex = false; | |
680 | mac_cb->speed = mac_phy_to_speed[mac_cb->phy_if]; | |
681 | mac_cb->max_speed = mac_cb->speed; | |
682 | ||
683 | if (mac_cb->phy_if == PHY_INTERFACE_MODE_SGMII) { | |
684 | mac_cb->if_support = MAC_GMAC_SUPPORTED; | |
685 | mac_cb->if_support |= SUPPORTED_1000baseT_Full; | |
686 | } else if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII) { | |
687 | mac_cb->if_support = SUPPORTED_10000baseR_FEC; | |
688 | mac_cb->if_support |= SUPPORTED_10000baseKR_Full; | |
689 | } | |
690 | ||
691 | mac_cb->max_frm = MAC_DEFAULT_MTU; | |
692 | mac_cb->tx_pause_frm_time = MAC_DEFAULT_PAUSE_TIME; | |
693 | ||
694 | /* Get the rest of the PHY information */ | |
695 | mac_cb->phy_node = of_parse_phandle(np, "phy-handle", mac_cb->mac_id); | |
696 | if (mac_cb->phy_node) | |
697 | dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n", | |
698 | mac_cb->mac_id, mac_cb->phy_node->name); | |
699 | } | |
700 | ||
701 | /** | |
702 | * hns_mac_get_mode - get mac mode | |
703 | * @phy_if: phy interface | |
704 | * retuen 0 - gmac, 1 - xgmac , negative --fail | |
705 | */ | |
706 | static int hns_mac_get_mode(phy_interface_t phy_if) | |
707 | { | |
708 | switch (phy_if) { | |
709 | case PHY_INTERFACE_MODE_SGMII: | |
710 | return MAC_GMAC_IDX; | |
711 | case PHY_INTERFACE_MODE_XGMII: | |
712 | return MAC_XGMAC_IDX; | |
713 | default: | |
714 | return -EINVAL; | |
715 | } | |
716 | } | |
717 | ||
718 | u8 __iomem *hns_mac_get_vaddr(struct dsaf_device *dsaf_dev, | |
719 | struct hns_mac_cb *mac_cb, u32 mac_mode_idx) | |
720 | { | |
721 | u8 __iomem *base = dsaf_dev->io_base; | |
722 | int mac_id = mac_cb->mac_id; | |
723 | ||
724 | if (mac_cb->mac_type == HNAE_PORT_SERVICE) | |
725 | return base + 0x40000 + mac_id * 0x4000 - | |
726 | mac_mode_idx * 0x20000; | |
727 | else | |
728 | return mac_cb->serdes_vaddr + 0x1000 | |
729 | + (mac_id - DSAF_SERVICE_PORT_NUM_PER_DSAF) * 0x100000; | |
730 | } | |
731 | ||
732 | /** | |
733 | * hns_mac_get_cfg - get mac cfg from dtb or acpi table | |
734 | * @dsaf_dev: dsa fabric device struct pointer | |
735 | * @mac_idx: mac index | |
736 | * retuen 0 - success , negative --fail | |
737 | */ | |
738 | int hns_mac_get_cfg(struct dsaf_device *dsaf_dev, int mac_idx) | |
739 | { | |
740 | int ret; | |
741 | u32 mac_mode_idx; | |
742 | struct hns_mac_cb *mac_cb = &dsaf_dev->mac_cb[mac_idx]; | |
743 | ||
744 | mac_cb->dsaf_dev = dsaf_dev; | |
745 | mac_cb->dev = dsaf_dev->dev; | |
746 | mac_cb->mac_id = mac_idx; | |
747 | ||
748 | mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base; | |
749 | mac_cb->serdes_vaddr = dsaf_dev->sds_base; | |
750 | ||
751 | if (dsaf_dev->cpld_base && | |
7f0abb1f | 752 | mac_idx < DSAF_SERVICE_PORT_NUM_PER_DSAF) { |
511e6bc0 | 753 | mac_cb->cpld_vaddr = dsaf_dev->cpld_base + |
754 | mac_cb->mac_id * CPLD_ADDR_PORT_OFFSET; | |
7f0abb1f | 755 | cpld_led_reset(mac_cb); |
756 | } | |
511e6bc0 | 757 | mac_cb->sfp_prsnt = 0; |
758 | mac_cb->txpkt_for_led = 0; | |
759 | mac_cb->rxpkt_for_led = 0; | |
760 | ||
761 | if (mac_idx < DSAF_SERVICE_PORT_NUM_PER_DSAF) | |
762 | mac_cb->mac_type = HNAE_PORT_SERVICE; | |
763 | else | |
764 | mac_cb->mac_type = HNAE_PORT_DEBUG; | |
765 | ||
766 | mac_cb->phy_if = hns_mac_get_phy_if(mac_cb); | |
767 | ||
768 | ret = hns_mac_get_mode(mac_cb->phy_if); | |
769 | if (ret < 0) { | |
770 | dev_err(dsaf_dev->dev, | |
771 | "hns_mac_get_mode failed,mac%d ret = %#x!\n", | |
772 | mac_cb->mac_id, ret); | |
773 | return ret; | |
774 | } | |
775 | mac_mode_idx = (u32)ret; | |
776 | ||
777 | hns_mac_get_info(mac_cb, mac_cb->dev->of_node, mac_mode_idx); | |
778 | ||
779 | mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx); | |
780 | ||
781 | return 0; | |
782 | } | |
783 | ||
784 | /** | |
785 | * hns_mac_init - init mac | |
786 | * @dsaf_dev: dsa fabric device struct pointer | |
787 | * retuen 0 - success , negative --fail | |
788 | */ | |
789 | int hns_mac_init(struct dsaf_device *dsaf_dev) | |
790 | { | |
791 | int i; | |
792 | int ret; | |
793 | size_t size; | |
794 | struct hns_mac_cb *mac_cb; | |
795 | ||
796 | size = sizeof(struct hns_mac_cb) * DSAF_MAX_PORT_NUM_PER_CHIP; | |
797 | dsaf_dev->mac_cb = devm_kzalloc(dsaf_dev->dev, size, GFP_KERNEL); | |
798 | if (!dsaf_dev->mac_cb) | |
799 | return -ENOMEM; | |
800 | ||
801 | for (i = 0; i < DSAF_MAX_PORT_NUM_PER_CHIP; i++) { | |
802 | ret = hns_mac_get_cfg(dsaf_dev, i); | |
803 | if (ret) | |
804 | goto free_mac_cb; | |
805 | ||
806 | mac_cb = &dsaf_dev->mac_cb[i]; | |
807 | ret = hns_mac_init_ex(mac_cb); | |
808 | if (ret) | |
809 | goto free_mac_cb; | |
810 | } | |
811 | ||
812 | return 0; | |
813 | ||
814 | free_mac_cb: | |
815 | dsaf_dev->mac_cb = NULL; | |
816 | ||
817 | return ret; | |
818 | } | |
819 | ||
820 | void hns_mac_uninit(struct dsaf_device *dsaf_dev) | |
821 | { | |
822 | cpld_led_reset(dsaf_dev->mac_cb); | |
823 | dsaf_dev->mac_cb = NULL; | |
824 | } | |
825 | ||
826 | int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb, | |
827 | enum hnae_loop loop, int en) | |
828 | { | |
829 | int ret; | |
830 | struct mac_driver *drv = hns_mac_get_drv(mac_cb); | |
831 | ||
832 | if (drv->config_loopback) | |
833 | ret = drv->config_loopback(drv, loop, en); | |
834 | else | |
835 | ret = -ENOTSUPP; | |
836 | ||
837 | return ret; | |
838 | } | |
839 | ||
840 | void hns_mac_update_stats(struct hns_mac_cb *mac_cb) | |
841 | { | |
842 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
843 | ||
844 | mac_ctrl_drv->update_stats(mac_ctrl_drv); | |
845 | } | |
846 | ||
847 | void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data) | |
848 | { | |
849 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
850 | ||
851 | mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data); | |
852 | } | |
853 | ||
854 | void hns_mac_get_strings(struct hns_mac_cb *mac_cb, | |
855 | int stringset, u8 *data) | |
856 | { | |
857 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
858 | ||
859 | mac_ctrl_drv->get_strings(stringset, data); | |
860 | } | |
861 | ||
862 | int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset) | |
863 | { | |
864 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
865 | ||
866 | return mac_ctrl_drv->get_sset_count(stringset); | |
867 | } | |
868 | ||
d5679849 KY |
869 | void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en) |
870 | { | |
871 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
872 | ||
873 | if (mac_ctrl_drv->set_promiscuous) | |
874 | mac_ctrl_drv->set_promiscuous(mac_ctrl_drv, en); | |
875 | } | |
876 | ||
511e6bc0 | 877 | int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb) |
878 | { | |
879 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
880 | ||
881 | return mac_ctrl_drv->get_regs_count(); | |
882 | } | |
883 | ||
884 | void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data) | |
885 | { | |
886 | struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); | |
887 | ||
888 | mac_ctrl_drv->get_regs(mac_ctrl_drv, data); | |
889 | } | |
890 | ||
891 | void hns_set_led_opt(struct hns_mac_cb *mac_cb) | |
892 | { | |
893 | int nic_data = 0; | |
894 | int txpkts, rxpkts; | |
895 | ||
896 | txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts; | |
897 | rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts; | |
898 | if (txpkts || rxpkts) | |
899 | nic_data = 1; | |
900 | else | |
901 | nic_data = 0; | |
902 | mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts; | |
903 | mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts; | |
904 | hns_cpld_set_led(mac_cb, (int)mac_cb->link, | |
905 | mac_cb->speed, nic_data); | |
906 | } | |
907 | ||
908 | int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb, | |
909 | enum hnae_led_state status) | |
910 | { | |
911 | if (!mac_cb || !mac_cb->cpld_vaddr) | |
912 | return 0; | |
913 | ||
914 | return cpld_set_led_id(mac_cb, status); | |
915 | } |