eth_legacy.c 8.8 KB

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