blob: 9d40f86ab6082d37367ced57b2a5975c21e466f6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05003 * (C) Copyright 2001-2015
wdenkc6097192002-11-03 00:24:07 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05005 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
wdenkc6097192002-11-03 00:24:07 +000010#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <net.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050014#include <phy.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090015#include <linux/errno.h>
Ramon Friedac598c12019-07-18 21:43:30 +030016#include <net/pcap.h>
Simon Glass0a9bde12016-01-17 14:51:57 -070017#include "eth_internal.h"
wdenkc6097192002-11-03 00:24:07 +000018
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassb6121f22015-04-05 16:07:37 -060021/*
22 * CPU and board-specific Ethernet initializations. Aliased function
23 * signals caller to move on
24 */
25static int __def_eth_init(bd_t *bis)
26{
27 return -1;
28}
29int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
30int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
31
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010032#ifdef CONFIG_API
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010033static struct {
34 uchar data[PKTSIZE];
35 int length;
36} eth_rcv_bufs[PKTBUFSRX];
37
Joe Hershberger5956ded2012-05-15 08:59:07 +000038static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010039#endif
40
Joe Hershberger9f374062012-08-03 10:59:08 +000041static struct eth_device *eth_devices;
42struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +000043
Simon Glassd9582c32016-01-17 14:51:59 -070044void eth_set_current_to_next(void)
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -050045{
46 eth_current = eth_current->next;
47}
48
Simon Glassd9582c32016-01-17 14:51:59 -070049void eth_set_dev(struct eth_device *dev)
Joe Hershberger279d2f62015-03-22 17:09:16 -050050{
51 eth_current = dev;
52}
53
Ben Warren97824d62010-07-29 12:56:11 -070054struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowiczaab8c492005-10-28 22:30:33 +020055{
56 struct eth_device *dev, *target_dev;
57
Helmut Raiger153bf3a2011-08-22 00:17:17 +000058 BUG_ON(devname == NULL);
59
Marian Balakowiczaab8c492005-10-28 22:30:33 +020060 if (!eth_devices)
61 return NULL;
62
63 dev = eth_devices;
64 target_dev = NULL;
65 do {
66 if (strcmp(devname, dev->name) == 0) {
67 target_dev = dev;
68 break;
69 }
70 dev = dev->next;
71 } while (dev != eth_devices);
72
73 return target_dev;
74}
75
Andy Fleminge7fd34b2009-02-11 15:07:24 -060076struct eth_device *eth_get_dev_by_index(int index)
77{
78 struct eth_device *dev, *target_dev;
Andy Fleminge7fd34b2009-02-11 15:07:24 -060079
80 if (!eth_devices)
81 return NULL;
82
83 dev = eth_devices;
84 target_dev = NULL;
85 do {
Michael Walle391da4c2011-10-27 11:31:35 +000086 if (dev->index == index) {
Andy Fleminge7fd34b2009-02-11 15:07:24 -060087 target_dev = dev;
88 break;
89 }
90 dev = dev->next;
Andy Fleminge7fd34b2009-02-11 15:07:24 -060091 } while (dev != eth_devices);
92
93 return target_dev;
94}
95
Joe Hershberger5956ded2012-05-15 08:59:07 +000096int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +000097{
Joe Hershberger5956ded2012-05-15 08:59:07 +000098 if (!eth_current)
Michael Walle391da4c2011-10-27 11:31:35 +000099 return -1;
wdenkc6097192002-11-03 00:24:07 +0000100
Michael Walle391da4c2011-10-27 11:31:35 +0000101 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000102}
103
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500104static int on_ethaddr(const char *name, const char *value, enum env_op op,
105 int flags)
106{
107 int index;
108 struct eth_device *dev;
109
110 if (!eth_devices)
111 return 0;
112
113 /* look for an index after "eth" */
114 index = simple_strtoul(name + 3, NULL, 10);
115
116 dev = eth_devices;
117 do {
118 if (dev->index == index) {
119 switch (op) {
120 case env_op_create:
121 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500122 string_to_enetaddr(value, dev->enetaddr);
Marek Vasut0ef21482016-11-12 16:28:40 +0100123 eth_write_hwaddr(dev, "eth", dev->index);
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500124 break;
125 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100126 memset(dev->enetaddr, 0, ARP_HLEN);
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500127 }
128 }
Gong Qianyua4f23972015-08-31 11:34:43 +0800129 dev = dev->next;
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500130 } while (dev != eth_devices);
131
132 return 0;
133}
134U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
135
Simon Glass62b36c92011-06-13 16:13:10 -0700136int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
137 int eth_number)
138{
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100139 unsigned char env_enetaddr[ARP_HLEN];
Simon Glass62b36c92011-06-13 16:13:10 -0700140 int ret = 0;
141
Simon Glass399a9ce2017-08-03 12:22:14 -0600142 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700143
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500144 if (!is_zero_ethaddr(env_enetaddr)) {
145 if (!is_zero_ethaddr(dev->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100146 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glass62b36c92011-06-13 16:13:10 -0700147 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500148 dev->name);
Simon Glass62b36c92011-06-13 16:13:10 -0700149 printf("Address in SROM is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500150 dev->enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700151 printf("Address in environment is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500152 env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700153 }
154
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100155 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500156 } else if (is_valid_ethaddr(dev->enetaddr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600157 eth_env_set_enetaddr_by_index(base_name, eth_number,
158 dev->enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500159 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500160#ifdef CONFIG_NET_RANDOM_ETHADDR
161 net_random_ethaddr(dev->enetaddr);
162 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
163 dev->name, eth_number, dev->enetaddr);
164#else
Pavel Machek222981f2014-07-13 10:27:02 +0200165 printf("\nError: %s address not set.\n",
166 dev->name);
167 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500168#endif
Simon Glass62b36c92011-06-13 16:13:10 -0700169 }
170
Pavel Machek222981f2014-07-13 10:27:02 +0200171 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500172 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek222981f2014-07-13 10:27:02 +0200173 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500174 dev->name, dev->enetaddr);
Pavel Machek222981f2014-07-13 10:27:02 +0200175 return -EINVAL;
176 }
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000177
Simon Glass62b36c92011-06-13 16:13:10 -0700178 ret = dev->write_hwaddr(dev);
Pavel Machek222981f2014-07-13 10:27:02 +0200179 if (ret)
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500180 printf("\nWarning: %s failed to set MAC address\n",
181 dev->name);
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000182 }
Simon Glass62b36c92011-06-13 16:13:10 -0700183
184 return ret;
185}
186
Simon Glass1e9961d2011-02-16 11:14:33 -0800187int eth_register(struct eth_device *dev)
188{
189 struct eth_device *d;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000190 static int index;
Michal Simek5b4945b2011-08-29 23:30:13 +0000191
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000192 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek5b4945b2011-08-29 23:30:13 +0000193
Simon Glass1e9961d2011-02-16 11:14:33 -0800194 if (!eth_devices) {
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500195 eth_devices = dev;
196 eth_current = dev;
Simon Glass1e9961d2011-02-16 11:14:33 -0800197 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000198 } else {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000199 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundeld46bcd32010-03-31 17:56:08 +0200200 ;
wdenkc6097192002-11-03 00:24:07 +0000201 d->next = dev;
202 }
203
204 dev->state = ETH_STATE_INIT;
205 dev->next = eth_devices;
Michael Walle391da4c2011-10-27 11:31:35 +0000206 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000207
208 return 0;
209}
210
Vincent Palatin451be502012-01-09 08:32:36 +0000211int eth_unregister(struct eth_device *dev)
212{
213 struct eth_device *cur;
214
215 /* No device */
216 if (!eth_devices)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500217 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000218
219 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
220 cur = cur->next)
221 ;
222
223 /* Device not found */
224 if (cur->next != dev)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500225 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000226
227 cur->next = dev->next;
228
229 if (eth_devices == dev)
230 eth_devices = dev->next == eth_devices ? NULL : dev->next;
231
232 if (eth_current == dev) {
233 eth_current = eth_devices;
234 eth_current_changed();
235 }
236
237 return 0;
238}
239
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500240int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000241{
Michael Walle391da4c2011-10-27 11:31:35 +0000242 int num_devices = 0;
Simon Glassb6121f22015-04-05 16:07:37 -0600243
wdenkc6097192002-11-03 00:24:07 +0000244 eth_devices = NULL;
245 eth_current = NULL;
Simon Glassb6121f22015-04-05 16:07:37 -0600246 eth_common_init();
Simon Glass0a9bde12016-01-17 14:51:57 -0700247 /*
248 * If board-specific initialization exists, call it.
249 * If not, call a CPU-specific one
250 */
251 if (board_eth_init != __def_eth_init) {
252 if (board_eth_init(gd->bd) < 0)
253 printf("Board Net Initialization Failed\n");
254 } else if (cpu_eth_init != __def_eth_init) {
255 if (cpu_eth_init(gd->bd) < 0)
256 printf("CPU Net Initialization Failed\n");
257 } else {
258 printf("Net Initialization Skipped\n");
259 }
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100260
wdenkc6097192002-11-03 00:24:07 +0000261 if (!eth_devices) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000262 puts("No ethernet found.\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000263 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000264 } else {
265 struct eth_device *dev = eth_devices;
Simon Glass64b723f2017-08-03 12:22:12 -0600266 char *ethprime = env_get("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000267
Simon Glass0169e6b2012-02-13 13:51:18 +0000268 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000269 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000270 if (dev->index)
Joe Hershberger5956ded2012-05-15 08:59:07 +0000271 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000272
273 printf("%s", dev->name);
274
Joe Hershberger5956ded2012-05-15 08:59:07 +0000275 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000276 eth_current = dev;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000277 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000278 }
279
Mike Frysinger2843ed92010-06-09 22:10:48 -0400280 if (strchr(dev->name, ' '))
Joe Hershberger5956ded2012-05-15 08:59:07 +0000281 puts("\nWarning: eth device name has a space!"
282 "\n");
Mike Frysinger2843ed92010-06-09 22:10:48 -0400283
Pavel Machek222981f2014-07-13 10:27:02 +0200284 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000285
wdenkc6097192002-11-03 00:24:07 +0000286 dev = dev->next;
Michael Walle391da4c2011-10-27 11:31:35 +0000287 num_devices++;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000288 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000289
Simon Glass1e9961d2011-02-16 11:14:33 -0800290 eth_current_changed();
Joe Hershberger5956ded2012-05-15 08:59:07 +0000291 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000292 }
293
Michael Walle391da4c2011-10-27 11:31:35 +0000294 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000295}
296
David Updegraff7280da72007-06-11 10:41:07 -0500297/* Multicast.
298 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200299 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500300 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500301int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff7280da72007-06-11 10:41:07 -0500302{
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100303 u8 mcast_mac[ARP_HLEN];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200304 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500305 return -1;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500306 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
307 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
308 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff7280da72007-06-11 10:41:07 -0500309 mcast_mac[2] = 0x5e;
310 mcast_mac[1] = 0x0;
311 mcast_mac[0] = 0x1;
312 return eth_current->mcast(eth_current, mcast_mac, join);
313}
314
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500315int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000316{
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500317 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000318
Stefan Roese11da2be2008-03-04 17:40:41 +0100319 if (!eth_current) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000320 puts("No ethernet found.\n");
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500321 return -ENODEV;
Stefan Roese11da2be2008-03-04 17:40:41 +0100322 }
wdenkc6097192002-11-03 00:24:07 +0000323
324 old_current = eth_current;
325 do {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400326 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000327
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500328 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000329 eth_current->state = ETH_STATE_ACTIVE;
330
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530331 return 0;
wdenkc6097192002-11-03 00:24:07 +0000332 }
Robin Getz9e0a4d62009-07-23 03:01:03 -0400333 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000334
335 eth_try_another(0);
336 } while (old_current != eth_current);
337
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500338 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000339}
340
341void eth_halt(void)
342{
343 if (!eth_current)
344 return;
345
346 eth_current->halt(eth_current);
347
348 eth_current->state = ETH_STATE_PASSIVE;
349}
350
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200351int eth_is_active(struct eth_device *dev)
352{
353 return dev && dev->state == ETH_STATE_ACTIVE;
354}
355
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000356int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000357{
Ramon Friedac598c12019-07-18 21:43:30 +0300358 int ret;
359
wdenkc6097192002-11-03 00:24:07 +0000360 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500361 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000362
Ramon Friedac598c12019-07-18 21:43:30 +0300363 ret = eth_current->send(eth_current, packet, length);
364#if defined(CONFIG_CMD_PCAP)
365 if (ret >= 0)
366 pcap_post(packet, lengeth, true);
367#endif
368 return ret;
wdenkc6097192002-11-03 00:24:07 +0000369}
370
371int eth_rx(void)
372{
373 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500374 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000375
376 return eth_current->recv(eth_current);
377}
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100378
379#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000380static void eth_save_packet(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100381{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000382 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100383 int i;
384
385 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
386 return;
387
388 if (PKTSIZE < length)
389 return;
390
391 for (i = 0; i < length; i++)
392 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
393
394 eth_rcv_bufs[eth_rcv_last].length = length;
395 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
396}
397
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000398int eth_receive(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100399{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000400 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100401 void *pp = push_packet;
402 int i;
403
404 if (eth_rcv_current == eth_rcv_last) {
405 push_packet = eth_save_packet;
406 eth_rx();
407 push_packet = pp;
408
409 if (eth_rcv_current == eth_rcv_last)
410 return -1;
411 }
412
Michael Walledf7c98a2012-06-22 11:24:28 +0000413 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100414
415 for (i = 0; i < length; i++)
416 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
417
418 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
419 return length;
420}
421#endif /* CONFIG_API */