eth.c 11 KB

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