eth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * (C) Copyright 2001-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <net.h>
  10. #include <miiphy.h>
  11. #include <phy.h>
  12. void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
  13. {
  14. char *end;
  15. int i;
  16. for (i = 0; i < 6; ++i) {
  17. enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
  18. if (addr)
  19. addr = (*end) ? end + 1 : end;
  20. }
  21. }
  22. int eth_getenv_enetaddr(char *name, uchar *enetaddr)
  23. {
  24. eth_parse_enetaddr(getenv(name), enetaddr);
  25. return is_valid_ether_addr(enetaddr);
  26. }
  27. int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
  28. {
  29. char buf[20];
  30. sprintf(buf, "%pM", enetaddr);
  31. return setenv(name, buf);
  32. }
  33. int eth_getenv_enetaddr_by_index(const char *base_name, int index,
  34. uchar *enetaddr)
  35. {
  36. char enetvar[32];
  37. sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
  38. return eth_getenv_enetaddr(enetvar, enetaddr);
  39. }
  40. static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
  41. uchar *enetaddr)
  42. {
  43. char enetvar[32];
  44. sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
  45. return eth_setenv_enetaddr(enetvar, enetaddr);
  46. }
  47. static int eth_mac_skip(int index)
  48. {
  49. char enetvar[15];
  50. char *skip_state;
  51. sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
  52. return ((skip_state = getenv(enetvar)) != NULL);
  53. }
  54. #ifdef CONFIG_RANDOM_MACADDR
  55. void eth_random_enetaddr(uchar *enetaddr)
  56. {
  57. uint32_t rval;
  58. srand(get_timer(0));
  59. rval = rand();
  60. enetaddr[0] = rval & 0xff;
  61. enetaddr[1] = (rval >> 8) & 0xff;
  62. enetaddr[2] = (rval >> 16) & 0xff;
  63. rval = rand();
  64. enetaddr[3] = rval & 0xff;
  65. enetaddr[4] = (rval >> 8) & 0xff;
  66. enetaddr[5] = (rval >> 16) & 0xff;
  67. /* make sure it's local and unicast */
  68. enetaddr[0] = (enetaddr[0] | 0x02) & ~0x01;
  69. }
  70. #endif
  71. /*
  72. * CPU and board-specific Ethernet initializations. Aliased function
  73. * signals caller to move on
  74. */
  75. static int __def_eth_init(bd_t *bis)
  76. {
  77. return -1;
  78. }
  79. int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
  80. int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
  81. #ifdef CONFIG_API
  82. static struct {
  83. uchar data[PKTSIZE];
  84. int length;
  85. } eth_rcv_bufs[PKTBUFSRX];
  86. static unsigned int eth_rcv_current, eth_rcv_last;
  87. #endif
  88. static struct eth_device *eth_devices;
  89. struct eth_device *eth_current;
  90. struct eth_device *eth_get_dev_by_name(const char *devname)
  91. {
  92. struct eth_device *dev, *target_dev;
  93. BUG_ON(devname == NULL);
  94. if (!eth_devices)
  95. return NULL;
  96. dev = eth_devices;
  97. target_dev = NULL;
  98. do {
  99. if (strcmp(devname, dev->name) == 0) {
  100. target_dev = dev;
  101. break;
  102. }
  103. dev = dev->next;
  104. } while (dev != eth_devices);
  105. return target_dev;
  106. }
  107. struct eth_device *eth_get_dev_by_index(int index)
  108. {
  109. struct eth_device *dev, *target_dev;
  110. if (!eth_devices)
  111. return NULL;
  112. dev = eth_devices;
  113. target_dev = NULL;
  114. do {
  115. if (dev->index == index) {
  116. target_dev = dev;
  117. break;
  118. }
  119. dev = dev->next;
  120. } while (dev != eth_devices);
  121. return target_dev;
  122. }
  123. int eth_get_dev_index(void)
  124. {
  125. if (!eth_current)
  126. return -1;
  127. return eth_current->index;
  128. }
  129. static void eth_current_changed(void)
  130. {
  131. char *act = getenv("ethact");
  132. /* update current ethernet name */
  133. if (eth_current) {
  134. if (act == NULL || strcmp(act, eth_current->name) != 0)
  135. setenv("ethact", eth_current->name);
  136. }
  137. /*
  138. * remove the variable completely if there is no active
  139. * interface
  140. */
  141. else if (act != NULL)
  142. setenv("ethact", NULL);
  143. }
  144. int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
  145. int eth_number)
  146. {
  147. unsigned char env_enetaddr[6];
  148. int ret = 0;
  149. eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
  150. if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
  151. if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
  152. memcmp(dev->enetaddr, env_enetaddr, 6)) {
  153. printf("\nWarning: %s MAC addresses don't match:\n",
  154. dev->name);
  155. printf("Address in SROM is %pM\n",
  156. dev->enetaddr);
  157. printf("Address in environment is %pM\n",
  158. env_enetaddr);
  159. }
  160. memcpy(dev->enetaddr, env_enetaddr, 6);
  161. } else if (is_valid_ether_addr(dev->enetaddr)) {
  162. eth_setenv_enetaddr_by_index(base_name, eth_number,
  163. dev->enetaddr);
  164. printf("\nWarning: %s using MAC address from net device\n",
  165. dev->name);
  166. }
  167. if (dev->write_hwaddr &&
  168. !eth_mac_skip(eth_number)) {
  169. if (!is_valid_ether_addr(dev->enetaddr))
  170. return -1;
  171. ret = dev->write_hwaddr(dev);
  172. }
  173. return ret;
  174. }
  175. int eth_register(struct eth_device *dev)
  176. {
  177. struct eth_device *d;
  178. static int index;
  179. assert(strlen(dev->name) < sizeof(dev->name));
  180. if (!eth_devices) {
  181. eth_current = eth_devices = dev;
  182. eth_current_changed();
  183. } else {
  184. for (d = eth_devices; d->next != eth_devices; d = d->next)
  185. ;
  186. d->next = dev;
  187. }
  188. dev->state = ETH_STATE_INIT;
  189. dev->next = eth_devices;
  190. dev->index = index++;
  191. return 0;
  192. }
  193. int eth_unregister(struct eth_device *dev)
  194. {
  195. struct eth_device *cur;
  196. /* No device */
  197. if (!eth_devices)
  198. return -1;
  199. for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
  200. cur = cur->next)
  201. ;
  202. /* Device not found */
  203. if (cur->next != dev)
  204. return -1;
  205. cur->next = dev->next;
  206. if (eth_devices == dev)
  207. eth_devices = dev->next == eth_devices ? NULL : dev->next;
  208. if (eth_current == dev) {
  209. eth_current = eth_devices;
  210. eth_current_changed();
  211. }
  212. return 0;
  213. }
  214. static void eth_env_init(bd_t *bis)
  215. {
  216. const char *s;
  217. if ((s = getenv("bootfile")) != NULL)
  218. copy_filename(BootFile, s, sizeof(BootFile));
  219. }
  220. int eth_initialize(bd_t *bis)
  221. {
  222. int num_devices = 0;
  223. eth_devices = NULL;
  224. eth_current = NULL;
  225. bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
  226. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  227. miiphy_init();
  228. #endif
  229. #ifdef CONFIG_PHYLIB
  230. phy_init();
  231. #endif
  232. eth_env_init(bis);
  233. /*
  234. * If board-specific initialization exists, call it.
  235. * If not, call a CPU-specific one
  236. */
  237. if (board_eth_init != __def_eth_init) {
  238. if (board_eth_init(bis) < 0)
  239. printf("Board Net Initialization Failed\n");
  240. } else if (cpu_eth_init != __def_eth_init) {
  241. if (cpu_eth_init(bis) < 0)
  242. printf("CPU Net Initialization Failed\n");
  243. } else
  244. printf("Net Initialization Skipped\n");
  245. if (!eth_devices) {
  246. puts("No ethernet found.\n");
  247. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  248. } else {
  249. struct eth_device *dev = eth_devices;
  250. char *ethprime = getenv("ethprime");
  251. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  252. do {
  253. if (dev->index)
  254. puts(", ");
  255. printf("%s", dev->name);
  256. if (ethprime && strcmp(dev->name, ethprime) == 0) {
  257. eth_current = dev;
  258. puts(" [PRIME]");
  259. }
  260. if (strchr(dev->name, ' '))
  261. puts("\nWarning: eth device name has a space!"
  262. "\n");
  263. if (eth_write_hwaddr(dev, "eth", dev->index))
  264. puts("\nWarning: failed to set MAC address\n");
  265. dev = dev->next;
  266. num_devices++;
  267. } while (dev != eth_devices);
  268. eth_current_changed();
  269. putc('\n');
  270. }
  271. return num_devices;
  272. }
  273. #ifdef CONFIG_MCAST_TFTP
  274. /* Multicast.
  275. * mcast_addr: multicast ipaddr from which multicast Mac is made
  276. * join: 1=join, 0=leave.
  277. */
  278. int eth_mcast_join(IPaddr_t mcast_ip, u8 join)
  279. {
  280. u8 mcast_mac[6];
  281. if (!eth_current || !eth_current->mcast)
  282. return -1;
  283. mcast_mac[5] = htonl(mcast_ip) & 0xff;
  284. mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
  285. mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
  286. mcast_mac[2] = 0x5e;
  287. mcast_mac[1] = 0x0;
  288. mcast_mac[0] = 0x1;
  289. return eth_current->mcast(eth_current, mcast_mac, join);
  290. }
  291. /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
  292. * and this is the ethernet-crc method needed for TSEC -- and perhaps
  293. * some other adapter -- hash tables
  294. */
  295. #define CRCPOLY_LE 0xedb88320
  296. u32 ether_crc(size_t len, unsigned char const *p)
  297. {
  298. int i;
  299. u32 crc;
  300. crc = ~0;
  301. while (len--) {
  302. crc ^= *p++;
  303. for (i = 0; i < 8; i++)
  304. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  305. }
  306. /* an reverse the bits, cuz of way they arrive -- last-first */
  307. crc = (crc >> 16) | (crc << 16);
  308. crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
  309. crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
  310. crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
  311. crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
  312. return crc;
  313. }
  314. #endif
  315. int eth_init(bd_t *bis)
  316. {
  317. struct eth_device *old_current, *dev;
  318. if (!eth_current) {
  319. puts("No ethernet found.\n");
  320. return -1;
  321. }
  322. /* Sync environment with network devices */
  323. dev = eth_devices;
  324. do {
  325. uchar env_enetaddr[6];
  326. if (eth_getenv_enetaddr_by_index("eth", dev->index,
  327. env_enetaddr))
  328. memcpy(dev->enetaddr, env_enetaddr, 6);
  329. dev = dev->next;
  330. } while (dev != eth_devices);
  331. old_current = eth_current;
  332. do {
  333. debug("Trying %s\n", eth_current->name);
  334. if (eth_current->init(eth_current, bis) >= 0) {
  335. eth_current->state = ETH_STATE_ACTIVE;
  336. return 0;
  337. }
  338. debug("FAIL\n");
  339. eth_try_another(0);
  340. } while (old_current != eth_current);
  341. return -1;
  342. }
  343. void eth_halt(void)
  344. {
  345. if (!eth_current)
  346. return;
  347. eth_current->halt(eth_current);
  348. eth_current->state = ETH_STATE_PASSIVE;
  349. }
  350. int eth_send(void *packet, int length)
  351. {
  352. if (!eth_current)
  353. return -1;
  354. return eth_current->send(eth_current, packet, length);
  355. }
  356. int eth_rx(void)
  357. {
  358. if (!eth_current)
  359. return -1;
  360. return eth_current->recv(eth_current);
  361. }
  362. #ifdef CONFIG_API
  363. static void eth_save_packet(void *packet, int length)
  364. {
  365. char *p = packet;
  366. int i;
  367. if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
  368. return;
  369. if (PKTSIZE < length)
  370. return;
  371. for (i = 0; i < length; i++)
  372. eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
  373. eth_rcv_bufs[eth_rcv_last].length = length;
  374. eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
  375. }
  376. int eth_receive(void *packet, int length)
  377. {
  378. char *p = packet;
  379. void *pp = push_packet;
  380. int i;
  381. if (eth_rcv_current == eth_rcv_last) {
  382. push_packet = eth_save_packet;
  383. eth_rx();
  384. push_packet = pp;
  385. if (eth_rcv_current == eth_rcv_last)
  386. return -1;
  387. }
  388. length = min(eth_rcv_bufs[eth_rcv_current].length, length);
  389. for (i = 0; i < length; i++)
  390. p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
  391. eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
  392. return length;
  393. }
  394. #endif /* CONFIG_API */
  395. void eth_try_another(int first_restart)
  396. {
  397. static struct eth_device *first_failed;
  398. char *ethrotate;
  399. /*
  400. * Do not rotate between network interfaces when
  401. * 'ethrotate' variable is set to 'no'.
  402. */
  403. ethrotate = getenv("ethrotate");
  404. if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
  405. return;
  406. if (!eth_current)
  407. return;
  408. if (first_restart)
  409. first_failed = eth_current;
  410. eth_current = eth_current->next;
  411. eth_current_changed();
  412. if (first_failed == eth_current)
  413. NetRestartWrap = 1;
  414. }
  415. void eth_set_current(void)
  416. {
  417. static char *act;
  418. static int env_changed_id;
  419. struct eth_device *old_current;
  420. int env_id;
  421. if (!eth_current) /* XXX no current */
  422. return;
  423. env_id = get_env_id();
  424. if ((act == NULL) || (env_changed_id != env_id)) {
  425. act = getenv("ethact");
  426. env_changed_id = env_id;
  427. }
  428. if (act != NULL) {
  429. old_current = eth_current;
  430. do {
  431. if (strcmp(eth_current->name, act) == 0)
  432. return;
  433. eth_current = eth_current->next;
  434. } while (old_current != eth_current);
  435. }
  436. eth_current_changed();
  437. }
  438. char *eth_get_name(void)
  439. {
  440. return eth_current ? eth_current->name : "unknown";
  441. }