blob: 6e0c0587616f61d1d8ef5953c6856d6b4a663077 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Joe Hershberger05c3e682015-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 Hershberger05c3e682015-03-22 17:09:10 -05005 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -06009#include <bootstage.h>
wdenkc6097192002-11-03 00:24:07 +000010#include <command.h>
Simon Glass2a64ada2020-07-19 10:15:39 -060011#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060012#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <net.h>
Andy Fleming5f184712011-04-08 02:10:27 -050015#include <phy.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060016#include <linux/bug.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090017#include <linux/errno.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030018#include <net/pcap.h>
Simon Glass818f91e2016-01-17 14:51:57 -070019#include "eth_internal.h"
wdenkc6097192002-11-03 00:24:07 +000020
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass3bc42702015-04-05 16:07:37 -060023/*
24 * CPU and board-specific Ethernet initializations. Aliased function
25 * signals caller to move on
26 */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +090027static int __def_eth_init(struct bd_info *bis)
Simon Glass3bc42702015-04-05 16:07:37 -060028{
29 return -1;
30}
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +090031int cpu_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
32int board_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
Simon Glass3bc42702015-04-05 16:07:37 -060033
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010034#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010035static struct {
36 uchar data[PKTSIZE];
37 int length;
38} eth_rcv_bufs[PKTBUFSRX];
39
Joe Hershberger66c73852012-05-15 08:59:07 +000040static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010041#endif
42
Joe Hershbergerf8be7d62012-08-03 10:59:08 +000043static struct eth_device *eth_devices;
44struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +000045
Simon Glass8607a6b2016-01-17 14:51:59 -070046void eth_set_current_to_next(void)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050047{
48 eth_current = eth_current->next;
49}
50
Simon Glass8607a6b2016-01-17 14:51:59 -070051void eth_set_dev(struct eth_device *dev)
Joe Hershbergere58780d2015-03-22 17:09:16 -050052{
53 eth_current = dev;
54}
55
Ben Warrend7fb9bc2010-07-29 12:56:11 -070056struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +020057{
58 struct eth_device *dev, *target_dev;
59
Helmut Raiger7e7f9032011-08-22 00:17:17 +000060 BUG_ON(devname == NULL);
61
Marian Balakowicz63ff0042005-10-28 22:30:33 +020062 if (!eth_devices)
63 return NULL;
64
65 dev = eth_devices;
66 target_dev = NULL;
67 do {
68 if (strcmp(devname, dev->name) == 0) {
69 target_dev = dev;
70 break;
71 }
72 dev = dev->next;
73 } while (dev != eth_devices);
74
75 return target_dev;
76}
77
Andy Fleming9e569862009-02-11 15:07:24 -060078struct eth_device *eth_get_dev_by_index(int index)
79{
80 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -060081
82 if (!eth_devices)
83 return NULL;
84
85 dev = eth_devices;
86 target_dev = NULL;
87 do {
Michael Wallefea7dca2011-10-27 11:31:35 +000088 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -060089 target_dev = dev;
90 break;
91 }
92 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -060093 } while (dev != eth_devices);
94
95 return target_dev;
96}
97
Joe Hershberger66c73852012-05-15 08:59:07 +000098int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +000099{
Joe Hershberger66c73852012-05-15 08:59:07 +0000100 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000101 return -1;
wdenkc6097192002-11-03 00:24:07 +0000102
Michael Wallefea7dca2011-10-27 11:31:35 +0000103 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000104}
105
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500106static int on_ethaddr(const char *name, const char *value, enum env_op op,
107 int flags)
108{
109 int index;
110 struct eth_device *dev;
111
112 if (!eth_devices)
113 return 0;
114
115 /* look for an index after "eth" */
116 index = simple_strtoul(name + 3, NULL, 10);
117
118 dev = eth_devices;
119 do {
120 if (dev->index == index) {
121 switch (op) {
122 case env_op_create:
123 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500124 string_to_enetaddr(value, dev->enetaddr);
Marek Vasut73d570a72016-11-12 16:28:40 +0100125 eth_write_hwaddr(dev, "eth", dev->index);
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500126 break;
127 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100128 memset(dev->enetaddr, 0, ARP_HLEN);
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500129 }
130 }
Gong Qianyu7aba0f22015-08-31 11:34:43 +0800131 dev = dev->next;
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500132 } while (dev != eth_devices);
133
134 return 0;
135}
136U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
137
Simon Glass7616e782011-06-13 16:13:10 -0700138int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
139 int eth_number)
140{
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100141 unsigned char env_enetaddr[ARP_HLEN];
Simon Glass7616e782011-06-13 16:13:10 -0700142 int ret = 0;
143
Simon Glass35affd72017-08-03 12:22:14 -0600144 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700145
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500146 if (!is_zero_ethaddr(env_enetaddr)) {
147 if (!is_zero_ethaddr(dev->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100148 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glass7616e782011-06-13 16:13:10 -0700149 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500150 dev->name);
Simon Glass7616e782011-06-13 16:13:10 -0700151 printf("Address in SROM is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500152 dev->enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700153 printf("Address in environment is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500154 env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700155 }
156
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100157 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500158 } else if (is_valid_ethaddr(dev->enetaddr)) {
Simon Glassfd1e9592017-08-03 12:22:11 -0600159 eth_env_set_enetaddr_by_index(base_name, eth_number,
160 dev->enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500161 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershbergerbef10142015-05-04 14:55:13 -0500162#ifdef CONFIG_NET_RANDOM_ETHADDR
163 net_random_ethaddr(dev->enetaddr);
164 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
165 dev->name, eth_number, dev->enetaddr);
166#else
Pavel Machek75d9a452014-07-13 10:27:02 +0200167 printf("\nError: %s address not set.\n",
168 dev->name);
169 return -EINVAL;
Joe Hershbergerbef10142015-05-04 14:55:13 -0500170#endif
Simon Glass7616e782011-06-13 16:13:10 -0700171 }
172
Pavel Machek75d9a452014-07-13 10:27:02 +0200173 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500174 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200175 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500176 dev->name, dev->enetaddr);
Pavel Machek75d9a452014-07-13 10:27:02 +0200177 return -EINVAL;
178 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000179
Simon Glass7616e782011-06-13 16:13:10 -0700180 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200181 if (ret)
Joe Hershbergerff819a32015-04-08 01:41:19 -0500182 printf("\nWarning: %s failed to set MAC address\n",
183 dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000184 }
Simon Glass7616e782011-06-13 16:13:10 -0700185
186 return ret;
187}
188
Simon Glass89d48362011-02-16 11:14:33 -0800189int eth_register(struct eth_device *dev)
190{
191 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000192 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000193
Mike Frysingerf6add132011-11-10 14:11:04 +0000194 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000195
Simon Glass89d48362011-02-16 11:14:33 -0800196 if (!eth_devices) {
Joe Hershbergerff819a32015-04-08 01:41:19 -0500197 eth_devices = dev;
198 eth_current = dev;
Simon Glass89d48362011-02-16 11:14:33 -0800199 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000200 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000201 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200202 ;
wdenkc6097192002-11-03 00:24:07 +0000203 d->next = dev;
204 }
205
206 dev->state = ETH_STATE_INIT;
207 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000208 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000209
210 return 0;
211}
212
Vincent Palatine7e982d2012-01-09 08:32:36 +0000213int eth_unregister(struct eth_device *dev)
214{
215 struct eth_device *cur;
216
217 /* No device */
218 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500219 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000220
221 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
222 cur = cur->next)
223 ;
224
225 /* Device not found */
226 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500227 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000228
229 cur->next = dev->next;
230
231 if (eth_devices == dev)
232 eth_devices = dev->next == eth_devices ? NULL : dev->next;
233
234 if (eth_current == dev) {
235 eth_current = eth_devices;
236 eth_current_changed();
237 }
238
239 return 0;
240}
241
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500242int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000243{
Michael Wallefea7dca2011-10-27 11:31:35 +0000244 int num_devices = 0;
Simon Glass3bc42702015-04-05 16:07:37 -0600245
wdenkc6097192002-11-03 00:24:07 +0000246 eth_devices = NULL;
247 eth_current = NULL;
Simon Glass3bc42702015-04-05 16:07:37 -0600248 eth_common_init();
Simon Glass818f91e2016-01-17 14:51:57 -0700249 /*
250 * If board-specific initialization exists, call it.
251 * If not, call a CPU-specific one
252 */
253 if (board_eth_init != __def_eth_init) {
254 if (board_eth_init(gd->bd) < 0)
255 printf("Board Net Initialization Failed\n");
256 } else if (cpu_eth_init != __def_eth_init) {
257 if (cpu_eth_init(gd->bd) < 0)
258 printf("CPU Net Initialization Failed\n");
259 } else {
260 printf("Net Initialization Skipped\n");
261 }
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100262
wdenkc6097192002-11-03 00:24:07 +0000263 if (!eth_devices) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200264 log_err("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000265 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000266 } else {
267 struct eth_device *dev = eth_devices;
Simon Glass00caae62017-08-03 12:22:12 -0600268 char *ethprime = env_get("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000269
Simon Glass770605e2012-02-13 13:51:18 +0000270 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000271 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000272 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000273 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000274
275 printf("%s", dev->name);
276
Joe Hershberger66c73852012-05-15 08:59:07 +0000277 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000278 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000279 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000280 }
281
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400282 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000283 puts("\nWarning: eth device name has a space!"
284 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400285
Pavel Machek75d9a452014-07-13 10:27:02 +0200286 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000287
wdenkc6097192002-11-03 00:24:07 +0000288 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000289 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000290 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000291
Simon Glass89d48362011-02-16 11:14:33 -0800292 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000293 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000294 }
295
Michael Wallefea7dca2011-10-27 11:31:35 +0000296 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000297}
298
David Updegraff53a5c422007-06-11 10:41:07 -0500299/* Multicast.
300 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200301 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500302 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500303int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500304{
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100305 u8 mcast_mac[ARP_HLEN];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200306 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500307 return -1;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500308 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
309 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
310 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff53a5c422007-06-11 10:41:07 -0500311 mcast_mac[2] = 0x5e;
312 mcast_mac[1] = 0x0;
313 mcast_mac[0] = 0x1;
314 return eth_current->mcast(eth_current, mcast_mac, join);
315}
316
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500317int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000318{
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500319 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000320
Stefan Roese6bc11382008-03-04 17:40:41 +0100321 if (!eth_current) {
Heinrich Schuchardtad895912020-09-14 11:00:18 +0200322 log_err("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500323 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100324 }
wdenkc6097192002-11-03 00:24:07 +0000325
326 old_current = eth_current;
327 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400328 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000329
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500330 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000331 eth_current->state = ETH_STATE_ACTIVE;
332
Upakul Barkakaty505be872007-11-29 12:16:13 +0530333 return 0;
wdenkc6097192002-11-03 00:24:07 +0000334 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400335 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000336
337 eth_try_another(0);
338 } while (old_current != eth_current);
339
Joe Hershberger05324a42015-03-22 17:09:04 -0500340 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000341}
342
343void eth_halt(void)
344{
345 if (!eth_current)
346 return;
347
348 eth_current->halt(eth_current);
349
350 eth_current->state = ETH_STATE_PASSIVE;
351}
352
Bernhard Nortmanneaa8a192015-09-14 15:29:43 +0200353int eth_is_active(struct eth_device *dev)
354{
355 return dev && dev->state == ETH_STATE_ACTIVE;
356}
357
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000358int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000359{
Ramon Fried3eaac632019-07-18 21:43:30 +0300360 int ret;
361
wdenkc6097192002-11-03 00:24:07 +0000362 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500363 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000364
Ramon Fried3eaac632019-07-18 21:43:30 +0300365 ret = eth_current->send(eth_current, packet, length);
366#if defined(CONFIG_CMD_PCAP)
367 if (ret >= 0)
368 pcap_post(packet, lengeth, true);
369#endif
370 return ret;
wdenkc6097192002-11-03 00:24:07 +0000371}
372
373int eth_rx(void)
374{
375 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500376 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000377
378 return eth_current->recv(eth_current);
379}
380
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100381#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000382static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100383{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000384 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100385 int i;
386
387 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
388 return;
389
390 if (PKTSIZE < length)
391 return;
392
393 for (i = 0; i < length; i++)
394 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
395
396 eth_rcv_bufs[eth_rcv_last].length = length;
397 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
398}
399
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000400int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100401{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000402 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100403 void *pp = push_packet;
404 int i;
405
406 if (eth_rcv_current == eth_rcv_last) {
407 push_packet = eth_save_packet;
408 eth_rx();
409 push_packet = pp;
410
411 if (eth_rcv_current == eth_rcv_last)
412 return -1;
413 }
414
Michael Walle46c07bc2012-06-22 11:24:28 +0000415 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100416
417 for (i = 0; i < length; i++)
418 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
419
420 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
421 return length;
422}
423#endif /* CONFIG_API */