eth_legacy.c 8.2 KB

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