eth_legacy.c 8.3 KB

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