eth.c 12 KB

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