Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* bcm.c - implements for Broadcom eth switch
*
* Copyright (C) 2018 iopsys Software Solutions AB. All rights reserved.
*
* Author: anjan.chanda@iopsys.eu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <stdbool.h>
#include <linux/mii.h>
#include "easy.h"
#include "ethernet.h"
#include "bcmswapitypes.h"
#include "bcmnet.h"
#define PHYID_2_MII_IOCTL(phy_id, mii_ioctl_data) \
{mii_ioctl_data->val_out = (phy_id >> 24 ) & 0xff; \
mii_ioctl_data->phy_id = phy_id & 0x1f;}
static int bcm_eth_get_unit_port(const char *ifname, int *unit, int *port)
{
struct ethswctl_data data;
int ret;
int i;
memset(&data, 0, sizeof(struct ethswctl_data));
data.op = ETHSWUNITPORT;
data.type = TYPE_GET;
strncpy(data.ifname, ifname, 16);
ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data));
if (ret != 0) {
fprintf(stderr, "ioctl failed! ret = %d\n", ret);
return -1;
}
*unit = data.unit;
*port = data.port;
if (!data.port && data.port_map) {
unsigned int portmap = data.port_map;
for (i = 0; i < sizeof(portmap); i++) {
if (!!(portmap & (1 << i))) {
*port = i;
break;
}
}
}
/* fprintf(stderr, "[%s] unit = %d port = %d portmap = 0x%x "
"phyportmap = 0x%x\n",
ifname, *unit, *port, data.port_map,
data.phy_portmap); */
return 0;
}
int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
{
struct ethswctl_data data;
int unit = -1;
int port = -1;
int ret;
memset(&data, 0, sizeof(struct ethswctl_data));
ret = bcm_eth_get_unit_port(ifname, &unit, &port);
if (ret)
return -1;
data.op = ETHSWPHYMODE;
data.port = port;
data.unit = unit;
data.type = TYPE_GET;
ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data));
if (ret != 0) {
fprintf(stderr, "ioctl failed!\n");
return -1;
}
link->portid = port;
link->speed = data.speed;
link->fullduplex = data.duplex == 1 ? false : true;
/* fprintf(stderr, "port: %d speed = %d fullduplex = %d\n",
link->portid, link->speed, link->fullduplex); */
if (!!(data.phycfg & PHY_CFG_1000FD))
link->capability |= ETH_1000_Full;
if (!!(data.phycfg & PHY_CFG_1000HD))
link->capability |= ETH_1000_Half;
if (!!(data.phycfg & PHY_CFG_100FD))
link->capability |= ETH_100_Full;
if (!!(data.phycfg & PHY_CFG_100HD))
link->capability |= ETH_100_Half;
if (!!(data.phycfg & PHY_CFG_10FD))
link->capability |= ETH_10_Full;
if (!!(data.phycfg & PHY_CFG_10HD))
link->capability |= ETH_10_Half;
if (!!(data.phycfg & PHY_CFG_5000FD))
link->capability |= ETH_5000_Full;
if (!!(data.phycfg & PHY_CFG_10000FD))
link->capability |= ETH_10000_Full;
memset(&data, 0, sizeof(struct ethswctl_data));
data.op = ETHSWPHYAUTONEG;
data.port = port;
data.unit = unit;
data.type = TYPE_GET;
ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data));
if (ret != 0) {
fprintf(stderr, "ioctl failed! ret = %d\n", ret);
return -1;
}
link->autoneg = data.autoneg_info == 0 ? false : true;
/* fprintf(stderr, "autoneg = %d\n", link->autoneg); */
memset(&data, 0, sizeof(struct ethswctl_data));
data.op = ETHSWLINKSTATUS;
data.port = port;
data.unit = unit;
data.type = TYPE_GET;
ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data));
if (ret != 0) {
fprintf(stderr, "ioctl failed!\n");
return -1;
}
link->down = data.status == 0 ? true : false;
return 0;
}
int bcm_eth_set_link_settings(const char *name, struct eth_link link)
{
fprintf(stderr, "%s(): TODO\n", __func__);
return 0;
}
int bcm_eth_poweron_phy(const char *name, struct eth_phy p)
{
struct ethctl_data data;
memset(&data, 0, sizeof(struct ethctl_data));
data.op = ETHSETSPOWERUP;
if (eth_ioctl(name, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data)) < 0)
return -1;
return 0;
}
int bcm_eth_poweroff_phy(const char *name, struct eth_phy p)
{
struct ethctl_data data;
memset(&data, 0, sizeof(struct ethctl_data));
data.op = ETHSETSPOWERDOWN;
if (eth_ioctl(name, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data)) < 0)
return -1;
return 0;
}
int bcm_eth_reset_phy(const char *name, int phy_id)
{
return eth_mii_reset_phy(name, phy_id & 0x1f);
}
int bcm_eth_get_rmon_stats(const char *ifname, struct eth_rmon_stats *tx,
struct eth_rmon_stats *rx)
{
struct ethswctl_data data;
int ret;
int unit = -1;
int port = -1;
memset(&data, 0, sizeof(struct ethswctl_data));
ret = bcm_eth_get_unit_port(ifname, &unit, &port);
if (ret)
return -1;
//data.op = ETHSWDUMPMIB;
data.op = ETHSWEMACGET;
data.port = port;
data.unit = unit;
data.queue = -1; /* prio_q = -1 */
ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
sizeof(struct ethswctl_data));
if (ret != 0) {
fprintf(stderr, "ioctl failed! ret = %d\n", ret);
return -1;
}
tx->all_q = true;
tx->bytes = data.emac_stats_s.tx_byte;
tx->packets = data.emac_stats_s.tx_packet;
tx->bcast_packets = data.emac_stats_s.tx_broadcast_packet;
tx->mcast_packets = data.emac_stats_s.tx_multicast_packet;
tx->crc_err_packets = data.emac_stats_s.tx_fcs_error;
tx->under_sz_packets = data.emac_stats_s.tx_undersize_frame;
tx->over_sz_packets = data.emac_stats_s.tx_oversize_frame;
tx->packets_64bytes = data.emac_stats_s.tx_frame_64;
tx->packets_65to127bytes = data.emac_stats_s.tx_frame_65_127;
tx->packets_128to255bytes = data.emac_stats_s.tx_frame_128_255;
tx->packets_256to511bytes = data.emac_stats_s.tx_frame_256_511;
tx->packets_512to1023bytes = data.emac_stats_s.tx_frame_512_1023;
tx->packets_1024to1518bytes = data.emac_stats_s.tx_frame_1024_1518;
rx->all_q = true;
rx->bytes = data.emac_stats_s.rx_byte;
rx->packets = data.emac_stats_s.rx_packet;
rx->bcast_packets = data.emac_stats_s.rx_broadcast_packet;
rx->mcast_packets = data.emac_stats_s.rx_multicast_packet;
rx->crc_err_packets = data.emac_stats_s.rx_fcs_error;
rx->under_sz_packets = data.emac_stats_s.rx_undersize_packet;
rx->over_sz_packets = data.emac_stats_s.rx_oversize_packet;
rx->packets_64bytes = data.emac_stats_s.rx_frame_64;
rx->packets_65to127bytes = data.emac_stats_s.rx_frame_65_127;
rx->packets_128to255bytes = data.emac_stats_s.rx_frame_128_255;
rx->packets_256to511bytes = data.emac_stats_s.rx_frame_256_511;
rx->packets_512to1023bytes = data.emac_stats_s.rx_frame_512_1023;
rx->packets_1024to1518bytes = data.emac_stats_s.rx_frame_1024_1518;
return 0;
}
const struct eth_ops bcm_eth_ops = {
.ifname = "eth",
//.up = bcm_eth_up,
//.down = bcm_eth_down,
.set_link_settings = bcm_eth_set_link_settings,
.get_link_settings = bcm_eth_get_link_settings,
//.get_stats = bcm_eth_get_stats,
.get_rmon_stats = bcm_eth_get_rmon_stats,
.poweron_phy = bcm_eth_poweron_phy,
.poweroff_phy = bcm_eth_poweroff_phy,
.reset_phy = bcm_eth_reset_phy,
};