1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Ethernet test commands			File: ui_test_ether.c
5    *
6    *  User interface commands to test Ethernet devices
7    *
8    *  Author:  Mitch Lichtenberg
9    *
10    *********************************************************************
11    *
12    *  Copyright 2000,2001,2002,2003
13    *  Broadcom Corporation. All rights reserved.
14    *
15    *  This software is furnished under license and may be used and
16    *  copied only in accordance with the following terms and
17    *  conditions.  Subject to these conditions, you may download,
18    *  copy, install, use, modify and distribute modified or unmodified
19    *  copies of this software in source and/or binary form.  No title
20    *  or ownership is transferred hereby.
21    *
22    *  1) Any source code used, modified or distributed must reproduce
23    *     and retain this copyright notice and list of conditions
24    *     as they appear in the source file.
25    *
26    *  2) No right is granted to use any trade name, trademark, or
27    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
28    *     name may not be used to endorse or promote products derived
29    *     from this software without the prior written permission of
30    *     Broadcom Corporation.
31    *
32    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44    *     THE POSSIBILITY OF SUCH DAMAGE.
45    ********************************************************************* */
46
47
48#include "cfe.h"
49#include "ui_command.h"
50
51typedef struct netparam_s {
52    const char *str;
53    int num;
54} netparam_t;
55
56const static netparam_t speedtypes[] = {
57    {"auto",ETHER_SPEED_AUTO},
58    {"10hdx",ETHER_SPEED_10HDX},
59    {"10fdx",ETHER_SPEED_10FDX},
60    {"100hdx",ETHER_SPEED_100HDX},
61    {"100fdx",ETHER_SPEED_100FDX},
62    {"1000hdx",ETHER_SPEED_1000HDX},
63    {"1000fdx",ETHER_SPEED_1000FDX},
64    {0,NULL}};
65
66
67int ui_init_ethertestcmds(void);
68
69static int ui_cmd_ethertest(ui_cmdline_t *cmd,int argc,char *argv[]);
70
71int ui_init_ethertestcmds(void)
72{
73    cmd_addcmd("test ether",
74	       ui_cmd_ethertest,
75	       NULL,
76	       "Do an ethernet test, reading packets from the net",
77	       "test ether device-name",
78	       "-speed=*;Specify speed|"
79               "-q;Be quiet|"
80	       "-send=*;Transmit packets"
81	);
82
83    return 0;
84}
85
86
87static int ui_ifconfig_lookup(char *name,char *val,const netparam_t *list)
88{
89    const netparam_t *p = list;
90
91    while (p->str) {
92	if (strcmp(p->str,val) == 0) return p->num;
93	p++;
94	}
95
96    xprintf("Invalid parameter for %s: Valid options are: ");
97
98    p = list;
99    while (p->str) {
100	xprintf("%s ",p->str);
101	p++;
102	}
103
104    xprintf("\n");
105    return -1;
106}
107
108static int ui_cmd_ethertest(ui_cmdline_t *cmd,int argc,char *argv[])
109{
110    char *tok;
111    int fh;
112    uint8_t packet[2048];
113    int res;
114    int idx;
115    int speed = ETHER_SPEED_AUTO;
116    char *x;
117    int count = 0;
118    int quiet;
119
120    tok = cmd_getarg(cmd,0);
121    if (!tok) return -1;
122
123    if (cmd_sw_value(cmd,"-speed",&x)) {
124	speed = ui_ifconfig_lookup("-speed",x,speedtypes);
125	if (speed < 0) return CFE_ERR_INV_PARAM;
126	}
127
128    quiet = cmd_sw_isset(cmd,"-q");
129
130
131    fh = cfe_open(tok);
132    if (fh < 0) {
133	xprintf("Could not open device: %s\n",cfe_errortext(fh));
134	return fh;
135	}
136
137    if (speed != ETHER_SPEED_AUTO) {
138	xprintf("Setting speed to %d...\n",speed);
139	cfe_ioctl(fh,IOCTL_ETHER_SETSPEED,(uint8_t *) &speed,sizeof(speed),&idx,0);
140	}
141
142
143    if (cmd_sw_value(cmd,"-send",&x)) {
144	count = atoi(x);
145	memset(packet,0xEE,sizeof(packet));
146	memcpy(packet,"\xFF\xFF\xFF\xFF\xFF\xFF\x40\x00\x00\x10\x00\x00\x12\x34",16);
147	res = 0;
148	for (idx = 0; idx < count; idx++) {
149	    res = cfe_write(fh,PTR2HSADDR(packet),128);
150	    if (res < 0) break;
151	    }
152	if (res) {
153	    ui_showerror(res,"Could not transmit packet");
154	    }
155	cfe_close(fh);
156	return 0;
157	}
158
159    xprintf("Receiving... press enter to stop\n");
160    while (!console_status()) {
161	res = cfe_read(fh,PTR2HSADDR(packet),sizeof(packet));
162	if (res == 0) continue;
163	if (res < 0) {
164	    xprintf("Read error: %s\n",cfe_errortext(res));
165	    break;
166	    }
167
168	if (!quiet) {
169	    xprintf("%4d ",res);
170	    if (res > 32) res = 32;
171
172	    for (idx = 0; idx < res; idx++) {
173		xprintf("%02X",packet[idx]);
174		if ((idx == 5) || (idx == 11) || (idx == 13)) xprintf(" ");
175		}
176
177	    xprintf("\n");
178	    }
179
180	count++;
181	if (quiet && !(count % 1000)) printf(".");
182	}
183
184    printf("Total packets received: %d\n",count);
185
186    cfe_close(fh);
187
188    return 0;
189}
190
191