eth_legacy.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001-2015
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. * Joe Hershberger, National Instruments
  6. */
  7. #include <common.h>
  8. #include <bootstage.h>
  9. #include <command.h>
  10. #include <env.h>
  11. #include <net.h>
  12. #include <phy.h>
  13. #include <linux/errno.h>
  14. #include <net/pcap.h>
  15. #include "eth_internal.h"
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /*
  18. * CPU and board-specific Ethernet initializations. Aliased function
  19. * signals caller to move on
  20. */
  21. static int __def_eth_init(bd_t *bis)
  22. {
  23. return -1;
  24. }
  25. int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
  26. int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
  27. #ifdef CONFIG_API
  28. static struct {
  29. uchar data[PKTSIZE];
  30. int length;
  31. } eth_rcv_bufs[PKTBUFSRX];
  32. static unsigned int eth_rcv_current, eth_rcv_last;
  33. #endif
  34. static struct eth_device *eth_devices;
  35. struct eth_device *eth_current;
  36. void eth_set_current_to_next(void)
  37. {
  38. eth_current = eth_current->next;
  39. }
  40. void eth_set_dev(struct eth_device *dev)
  41. {
  42. eth_current = dev;
  43. }
  44. struct eth_device *eth_get_dev_by_name(const char *devname)
  45. {
  46. struct eth_device *dev, *target_dev;
  47. BUG_ON(devname == NULL);
  48. if (!eth_devices)
  49. return NULL;
  50. dev = eth_devices;
  51. target_dev = NULL;
  52. do {
  53. if (strcmp(devname, dev->name) == 0) {
  54. target_dev = dev;
  55. break;
  56. }
  57. dev = dev->next;
  58. } while (dev != eth_devices);
  59. return target_dev;
  60. }
  61. struct eth_device *eth_get_dev_by_index(int index)
  62. {
  63. struct eth_device *dev, *target_dev;
  64. if (!eth_devices)
  65. return NULL;
  66. dev = eth_devices;
  67. target_dev = NULL;
  68. do {
  69. if (dev->index == index) {
  70. target_dev = dev;
  71. break;
  72. }
  73. dev = dev->next;
  74. } while (dev != eth_devices);
  75. return target_dev;
  76. }
  77. int eth_get_dev_index(void)
  78. {
  79. if (!eth_current)
  80. return -1;
  81. return eth_current->index;
  82. }
  83. static int on_ethaddr(const char *name, const char *value, enum env_op op,
  84. int flags)
  85. {
  86. int index;
  87. struct eth_device *dev;
  88. if (!eth_devices)
  89. return 0;
  90. /* look for an index after "eth" */
  91. index = simple_strtoul(name + 3, NULL, 10);
  92. dev = eth_devices;
  93. do {
  94. if (dev->index == index) {
  95. switch (op) {
  96. case env_op_create:
  97. case env_op_overwrite:
  98. string_to_enetaddr(value, dev->enetaddr);
  99. eth_write_hwaddr(dev, "eth", dev->index);
  100. break;
  101. case env_op_delete:
  102. memset(dev->enetaddr, 0, ARP_HLEN);
  103. }
  104. }
  105. dev = dev->next;
  106. } while (dev != eth_devices);
  107. return 0;
  108. }
  109. U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  110. int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
  111. int eth_number)
  112. {
  113. unsigned char env_enetaddr[ARP_HLEN];
  114. int ret = 0;
  115. eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
  116. if (!is_zero_ethaddr(env_enetaddr)) {
  117. if (!is_zero_ethaddr(dev->enetaddr) &&
  118. memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
  119. printf("\nWarning: %s MAC addresses don't match:\n",
  120. dev->name);
  121. printf("Address in SROM is %pM\n",
  122. dev->enetaddr);
  123. printf("Address in environment is %pM\n",
  124. env_enetaddr);
  125. }
  126. memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
  127. } else if (is_valid_ethaddr(dev->enetaddr)) {
  128. eth_env_set_enetaddr_by_index(base_name, eth_number,
  129. dev->enetaddr);
  130. } else if (is_zero_ethaddr(dev->enetaddr)) {
  131. #ifdef CONFIG_NET_RANDOM_ETHADDR
  132. net_random_ethaddr(dev->enetaddr);
  133. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  134. dev->name, eth_number, dev->enetaddr);
  135. #else
  136. printf("\nError: %s address not set.\n",
  137. dev->name);
  138. return -EINVAL;
  139. #endif
  140. }
  141. if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
  142. if (!is_valid_ethaddr(dev->enetaddr)) {
  143. printf("\nError: %s address %pM illegal value\n",
  144. dev->name, dev->enetaddr);
  145. return -EINVAL;
  146. }
  147. ret = dev->write_hwaddr(dev);
  148. if (ret)
  149. printf("\nWarning: %s failed to set MAC address\n",
  150. dev->name);
  151. }
  152. return ret;
  153. }
  154. int eth_register(struct eth_device *dev)
  155. {
  156. struct eth_device *d;
  157. static int index;
  158. assert(strlen(dev->name) < sizeof(dev->name));
  159. if (!eth_devices) {
  160. eth_devices = dev;
  161. eth_current = dev;
  162. eth_current_changed();
  163. } else {
  164. for (d = eth_devices; d->next != eth_devices; d = d->next)
  165. ;
  166. d->next = dev;
  167. }
  168. dev->state = ETH_STATE_INIT;
  169. dev->next = eth_devices;
  170. dev->index = index++;
  171. return 0;
  172. }
  173. int eth_unregister(struct eth_device *dev)
  174. {
  175. struct eth_device *cur;
  176. /* No device */
  177. if (!eth_devices)
  178. return -ENODEV;
  179. for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
  180. cur = cur->next)
  181. ;
  182. /* Device not found */
  183. if (cur->next != dev)
  184. return -ENODEV;
  185. cur->next = dev->next;
  186. if (eth_devices == dev)
  187. eth_devices = dev->next == eth_devices ? NULL : dev->next;
  188. if (eth_current == dev) {
  189. eth_current = eth_devices;
  190. eth_current_changed();
  191. }
  192. return 0;
  193. }
  194. int eth_initialize(void)
  195. {
  196. int num_devices = 0;
  197. eth_devices = NULL;
  198. eth_current = NULL;
  199. eth_common_init();
  200. /*
  201. * If board-specific initialization exists, call it.
  202. * If not, call a CPU-specific one
  203. */
  204. if (board_eth_init != __def_eth_init) {
  205. if (board_eth_init(gd->bd) < 0)
  206. printf("Board Net Initialization Failed\n");
  207. } else if (cpu_eth_init != __def_eth_init) {
  208. if (cpu_eth_init(gd->bd) < 0)
  209. printf("CPU Net Initialization Failed\n");
  210. } else {
  211. printf("Net Initialization Skipped\n");
  212. }
  213. if (!eth_devices) {
  214. puts("No ethernet found.\n");
  215. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  216. } else {
  217. struct eth_device *dev = eth_devices;
  218. char *ethprime = env_get("ethprime");
  219. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  220. do {
  221. if (dev->index)
  222. puts(", ");
  223. printf("%s", dev->name);
  224. if (ethprime && strcmp(dev->name, ethprime) == 0) {
  225. eth_current = dev;
  226. puts(" [PRIME]");
  227. }
  228. if (strchr(dev->name, ' '))
  229. puts("\nWarning: eth device name has a space!"
  230. "\n");
  231. eth_write_hwaddr(dev, "eth", dev->index);
  232. dev = dev->next;
  233. num_devices++;
  234. } while (dev != eth_devices);
  235. eth_current_changed();
  236. putc('\n');
  237. }
  238. return num_devices;
  239. }
  240. /* Multicast.
  241. * mcast_addr: multicast ipaddr from which multicast Mac is made
  242. * join: 1=join, 0=leave.
  243. */
  244. int eth_mcast_join(struct in_addr mcast_ip, int join)
  245. {
  246. u8 mcast_mac[ARP_HLEN];
  247. if (!eth_current || !eth_current->mcast)
  248. return -1;
  249. mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
  250. mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
  251. mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
  252. mcast_mac[2] = 0x5e;
  253. mcast_mac[1] = 0x0;
  254. mcast_mac[0] = 0x1;
  255. return eth_current->mcast(eth_current, mcast_mac, join);
  256. }
  257. int eth_init(void)
  258. {
  259. struct eth_device *old_current;
  260. if (!eth_current) {
  261. puts("No ethernet found.\n");
  262. return -ENODEV;
  263. }
  264. old_current = eth_current;
  265. do {
  266. debug("Trying %s\n", eth_current->name);
  267. if (eth_current->init(eth_current, gd->bd) >= 0) {
  268. eth_current->state = ETH_STATE_ACTIVE;
  269. return 0;
  270. }
  271. debug("FAIL\n");
  272. eth_try_another(0);
  273. } while (old_current != eth_current);
  274. return -ETIMEDOUT;
  275. }
  276. void eth_halt(void)
  277. {
  278. if (!eth_current)
  279. return;
  280. eth_current->halt(eth_current);
  281. eth_current->state = ETH_STATE_PASSIVE;
  282. }
  283. int eth_is_active(struct eth_device *dev)
  284. {
  285. return dev && dev->state == ETH_STATE_ACTIVE;
  286. }
  287. int eth_send(void *packet, int length)
  288. {
  289. int ret;
  290. if (!eth_current)
  291. return -ENODEV;
  292. ret = eth_current->send(eth_current, packet, length);
  293. #if defined(CONFIG_CMD_PCAP)
  294. if (ret >= 0)
  295. pcap_post(packet, lengeth, true);
  296. #endif
  297. return ret;
  298. }
  299. int eth_rx(void)
  300. {
  301. if (!eth_current)
  302. return -ENODEV;
  303. return eth_current->recv(eth_current);
  304. }
  305. #ifdef CONFIG_API
  306. static void eth_save_packet(void *packet, int length)
  307. {
  308. char *p = packet;
  309. int i;
  310. if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
  311. return;
  312. if (PKTSIZE < length)
  313. return;
  314. for (i = 0; i < length; i++)
  315. eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
  316. eth_rcv_bufs[eth_rcv_last].length = length;
  317. eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
  318. }
  319. int eth_receive(void *packet, int length)
  320. {
  321. char *p = packet;
  322. void *pp = push_packet;
  323. int i;
  324. if (eth_rcv_current == eth_rcv_last) {
  325. push_packet = eth_save_packet;
  326. eth_rx();
  327. push_packet = pp;
  328. if (eth_rcv_current == eth_rcv_last)
  329. return -1;
  330. }
  331. length = min(eth_rcv_bufs[eth_rcv_current].length, length);
  332. for (i = 0; i < length; i++)
  333. p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
  334. eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
  335. return length;
  336. }
  337. #endif /* CONFIG_API */