eth_legacy.c 8.3 KB

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