eth.c 11 KB

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