eth_legacy.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. eth_env_set_enetaddr_by_index("eth", eth_number,
  140. dev->enetaddr);
  141. #else
  142. printf("\nError: %s address not set.\n",
  143. dev->name);
  144. return -EINVAL;
  145. #endif
  146. }
  147. if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
  148. if (!is_valid_ethaddr(dev->enetaddr)) {
  149. printf("\nError: %s address %pM illegal value\n",
  150. dev->name, dev->enetaddr);
  151. return -EINVAL;
  152. }
  153. ret = dev->write_hwaddr(dev);
  154. if (ret)
  155. printf("\nWarning: %s failed to set MAC address\n",
  156. dev->name);
  157. }
  158. return ret;
  159. }
  160. int eth_register(struct eth_device *dev)
  161. {
  162. struct eth_device *d;
  163. static int index;
  164. assert(strlen(dev->name) < sizeof(dev->name));
  165. if (!eth_devices) {
  166. eth_devices = dev;
  167. eth_current = dev;
  168. eth_current_changed();
  169. } else {
  170. for (d = eth_devices; d->next != eth_devices; d = d->next)
  171. ;
  172. d->next = dev;
  173. }
  174. dev->state = ETH_STATE_INIT;
  175. dev->next = eth_devices;
  176. dev->index = index++;
  177. return 0;
  178. }
  179. int eth_unregister(struct eth_device *dev)
  180. {
  181. struct eth_device *cur;
  182. /* No device */
  183. if (!eth_devices)
  184. return -ENODEV;
  185. for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
  186. cur = cur->next)
  187. ;
  188. /* Device not found */
  189. if (cur->next != dev)
  190. return -ENODEV;
  191. cur->next = dev->next;
  192. if (eth_devices == dev)
  193. eth_devices = dev->next == eth_devices ? NULL : dev->next;
  194. if (eth_current == dev) {
  195. eth_current = eth_devices;
  196. eth_current_changed();
  197. }
  198. return 0;
  199. }
  200. int eth_initialize(void)
  201. {
  202. int num_devices = 0;
  203. eth_devices = NULL;
  204. eth_current = NULL;
  205. eth_common_init();
  206. /*
  207. * If board-specific initialization exists, call it.
  208. * If not, call a CPU-specific one
  209. */
  210. if (board_eth_init != __def_eth_init) {
  211. if (board_eth_init(gd->bd) < 0)
  212. printf("Board Net Initialization Failed\n");
  213. } else if (cpu_eth_init != __def_eth_init) {
  214. if (cpu_eth_init(gd->bd) < 0)
  215. printf("CPU Net Initialization Failed\n");
  216. } else {
  217. printf("Net Initialization Skipped\n");
  218. }
  219. if (!eth_devices) {
  220. log_err("No ethernet found.\n");
  221. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  222. } else {
  223. struct eth_device *dev = eth_devices;
  224. char *ethprime = env_get("ethprime");
  225. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  226. do {
  227. if (dev->index)
  228. puts(", ");
  229. printf("%s", dev->name);
  230. if (ethprime && strcmp(dev->name, ethprime) == 0) {
  231. eth_current = dev;
  232. puts(" [PRIME]");
  233. }
  234. if (strchr(dev->name, ' '))
  235. puts("\nWarning: eth device name has a space!"
  236. "\n");
  237. eth_write_hwaddr(dev, "eth", dev->index);
  238. dev = dev->next;
  239. num_devices++;
  240. } while (dev != eth_devices);
  241. eth_current_changed();
  242. putc('\n');
  243. }
  244. return num_devices;
  245. }
  246. /* Multicast.
  247. * mcast_addr: multicast ipaddr from which multicast Mac is made
  248. * join: 1=join, 0=leave.
  249. */
  250. int eth_mcast_join(struct in_addr mcast_ip, int join)
  251. {
  252. u8 mcast_mac[ARP_HLEN];
  253. if (!eth_current || !eth_current->mcast)
  254. return -1;
  255. mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
  256. mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
  257. mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
  258. mcast_mac[2] = 0x5e;
  259. mcast_mac[1] = 0x0;
  260. mcast_mac[0] = 0x1;
  261. return eth_current->mcast(eth_current, mcast_mac, join);
  262. }
  263. int eth_init(void)
  264. {
  265. struct eth_device *old_current;
  266. if (!eth_current) {
  267. log_err("No ethernet found.\n");
  268. return -ENODEV;
  269. }
  270. old_current = eth_current;
  271. do {
  272. debug("Trying %s\n", eth_current->name);
  273. if (eth_current->init(eth_current, gd->bd) >= 0) {
  274. eth_current->state = ETH_STATE_ACTIVE;
  275. return 0;
  276. }
  277. debug("FAIL\n");
  278. eth_try_another(0);
  279. } while (old_current != eth_current);
  280. return -ETIMEDOUT;
  281. }
  282. void eth_halt(void)
  283. {
  284. if (!eth_current)
  285. return;
  286. eth_current->halt(eth_current);
  287. eth_current->state = ETH_STATE_PASSIVE;
  288. }
  289. int eth_is_active(struct eth_device *dev)
  290. {
  291. return dev && dev->state == ETH_STATE_ACTIVE;
  292. }
  293. int eth_send(void *packet, int length)
  294. {
  295. int ret;
  296. if (!eth_current)
  297. return -ENODEV;
  298. ret = eth_current->send(eth_current, packet, length);
  299. #if defined(CONFIG_CMD_PCAP)
  300. if (ret >= 0)
  301. pcap_post(packet, length, true);
  302. #endif
  303. return ret;
  304. }
  305. int eth_rx(void)
  306. {
  307. if (!eth_current)
  308. return -ENODEV;
  309. return eth_current->recv(eth_current);
  310. }
  311. #ifdef CONFIG_API
  312. static void eth_save_packet(void *packet, int length)
  313. {
  314. char *p = packet;
  315. int i;
  316. if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
  317. return;
  318. if (PKTSIZE < length)
  319. return;
  320. for (i = 0; i < length; i++)
  321. eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
  322. eth_rcv_bufs[eth_rcv_last].length = length;
  323. eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
  324. }
  325. int eth_receive(void *packet, int length)
  326. {
  327. char *p = packet;
  328. void *pp = push_packet;
  329. int i;
  330. if (eth_rcv_current == eth_rcv_last) {
  331. push_packet = eth_save_packet;
  332. eth_rx();
  333. push_packet = pp;
  334. if (eth_rcv_current == eth_rcv_last)
  335. return -1;
  336. }
  337. length = min(eth_rcv_bufs[eth_rcv_current].length, length);
  338. for (i = 0; i < length; i++)
  339. p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
  340. eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
  341. return length;
  342. }
  343. #endif /* CONFIG_API */