eth_legacy.c 8.3 KB

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