eth.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * (C) Copyright 2001-2015
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. * Joe Hershberger, National Instruments
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <environment.h>
  12. #include <net.h>
  13. #include <miiphy.h>
  14. #include <phy.h>
  15. #include <asm/errno.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/uclass-internal.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
  20. {
  21. char *end;
  22. int i;
  23. for (i = 0; i < 6; ++i) {
  24. enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
  25. if (addr)
  26. addr = (*end) ? end + 1 : end;
  27. }
  28. }
  29. int eth_getenv_enetaddr(char *name, uchar *enetaddr)
  30. {
  31. eth_parse_enetaddr(getenv(name), enetaddr);
  32. return is_valid_ethaddr(enetaddr);
  33. }
  34. int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
  35. {
  36. char buf[20];
  37. sprintf(buf, "%pM", enetaddr);
  38. return setenv(name, buf);
  39. }
  40. int eth_getenv_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_getenv_enetaddr(enetvar, enetaddr);
  46. }
  47. static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
  48. uchar *enetaddr)
  49. {
  50. char enetvar[32];
  51. sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
  52. return eth_setenv_enetaddr(enetvar, enetaddr);
  53. }
  54. static int eth_mac_skip(int index)
  55. {
  56. char enetvar[15];
  57. char *skip_state;
  58. sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
  59. skip_state = getenv(enetvar);
  60. return skip_state != 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. static void eth_common_init(void)
  74. {
  75. bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
  76. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  77. miiphy_init();
  78. #endif
  79. #ifdef CONFIG_PHYLIB
  80. phy_init();
  81. #endif
  82. /*
  83. * If board-specific initialization exists, call it.
  84. * If not, call a CPU-specific one
  85. */
  86. if (board_eth_init != __def_eth_init) {
  87. if (board_eth_init(gd->bd) < 0)
  88. printf("Board Net Initialization Failed\n");
  89. } else if (cpu_eth_init != __def_eth_init) {
  90. if (cpu_eth_init(gd->bd) < 0)
  91. printf("CPU Net Initialization Failed\n");
  92. } else {
  93. printf("Net Initialization Skipped\n");
  94. }
  95. }
  96. #ifdef CONFIG_DM_ETH
  97. /**
  98. * struct eth_device_priv - private structure for each Ethernet device
  99. *
  100. * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
  101. */
  102. struct eth_device_priv {
  103. enum eth_state_t state;
  104. };
  105. /**
  106. * struct eth_uclass_priv - The structure attached to the uclass itself
  107. *
  108. * @current: The Ethernet device that the network functions are using
  109. */
  110. struct eth_uclass_priv {
  111. struct udevice *current;
  112. };
  113. /* eth_errno - This stores the most recent failure code from DM functions */
  114. static int eth_errno;
  115. static struct eth_uclass_priv *eth_get_uclass_priv(void)
  116. {
  117. struct uclass *uc;
  118. uclass_get(UCLASS_ETH, &uc);
  119. assert(uc);
  120. return uc->priv;
  121. }
  122. static void eth_set_current_to_next(void)
  123. {
  124. struct eth_uclass_priv *uc_priv;
  125. uc_priv = eth_get_uclass_priv();
  126. if (uc_priv->current)
  127. uclass_next_device(&uc_priv->current);
  128. if (!uc_priv->current)
  129. uclass_first_device(UCLASS_ETH, &uc_priv->current);
  130. }
  131. /*
  132. * Typically this will simply return the active device.
  133. * In the case where the most recent active device was unset, this will attempt
  134. * to return the first device. If that device doesn't exist or fails to probe,
  135. * this function will return NULL.
  136. */
  137. struct udevice *eth_get_dev(void)
  138. {
  139. struct eth_uclass_priv *uc_priv;
  140. uc_priv = eth_get_uclass_priv();
  141. if (!uc_priv->current)
  142. eth_errno = uclass_first_device(UCLASS_ETH,
  143. &uc_priv->current);
  144. return uc_priv->current;
  145. }
  146. /*
  147. * Typically this will just store a device pointer.
  148. * In case it was not probed, we will attempt to do so.
  149. * dev may be NULL to unset the active device.
  150. */
  151. static void eth_set_dev(struct udevice *dev)
  152. {
  153. if (dev && !device_active(dev))
  154. eth_errno = device_probe(dev);
  155. eth_get_uclass_priv()->current = dev;
  156. }
  157. /*
  158. * Find the udevice that either has the name passed in as devname or has an
  159. * alias named devname.
  160. */
  161. struct udevice *eth_get_dev_by_name(const char *devname)
  162. {
  163. int seq = -1;
  164. char *endp = NULL;
  165. const char *startp = NULL;
  166. struct udevice *it;
  167. struct uclass *uc;
  168. /* Must be longer than 3 to be an alias */
  169. if (strlen(devname) > strlen("eth")) {
  170. startp = devname + strlen("eth");
  171. seq = simple_strtoul(startp, &endp, 10);
  172. }
  173. uclass_get(UCLASS_ETH, &uc);
  174. uclass_foreach_dev(it, uc) {
  175. /*
  176. * We need the seq to be valid, so try to probe it.
  177. * If the probe fails, the seq will not match since it will be
  178. * -1 instead of what we are looking for.
  179. * We don't care about errors from probe here. Either they won't
  180. * match an alias or it will match a literal name and we'll pick
  181. * up the error when we try to probe again in eth_set_dev().
  182. */
  183. device_probe(it);
  184. /*
  185. * Check for the name or the sequence number to match
  186. */
  187. if (strcmp(it->name, devname) == 0 ||
  188. (endp > startp && it->seq == seq))
  189. return it;
  190. }
  191. return NULL;
  192. }
  193. unsigned char *eth_get_ethaddr(void)
  194. {
  195. struct eth_pdata *pdata;
  196. if (eth_get_dev()) {
  197. pdata = eth_get_dev()->platdata;
  198. return pdata->enetaddr;
  199. }
  200. return NULL;
  201. }
  202. /* Set active state without calling start on the driver */
  203. int eth_init_state_only(void)
  204. {
  205. struct udevice *current;
  206. struct eth_device_priv *priv;
  207. current = eth_get_dev();
  208. if (!current || !device_active(current))
  209. return -EINVAL;
  210. priv = current->uclass_priv;
  211. priv->state = ETH_STATE_ACTIVE;
  212. return 0;
  213. }
  214. /* Set passive state without calling stop on the driver */
  215. void eth_halt_state_only(void)
  216. {
  217. struct udevice *current;
  218. struct eth_device_priv *priv;
  219. current = eth_get_dev();
  220. if (!current || !device_active(current))
  221. return;
  222. priv = current->uclass_priv;
  223. priv->state = ETH_STATE_PASSIVE;
  224. }
  225. int eth_get_dev_index(void)
  226. {
  227. if (eth_get_dev())
  228. return eth_get_dev()->seq;
  229. return -1;
  230. }
  231. static int eth_write_hwaddr(struct udevice *dev)
  232. {
  233. struct eth_pdata *pdata = dev->platdata;
  234. int ret = 0;
  235. if (!dev || !device_active(dev))
  236. return -EINVAL;
  237. /* seq is valid since the device is active */
  238. if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
  239. if (!is_valid_ethaddr(pdata->enetaddr)) {
  240. printf("\nError: %s address %pM illegal value\n",
  241. dev->name, pdata->enetaddr);
  242. return -EINVAL;
  243. }
  244. /*
  245. * Drivers are allowed to decide not to implement this at
  246. * run-time. E.g. Some devices may use it and some may not.
  247. */
  248. ret = eth_get_ops(dev)->write_hwaddr(dev);
  249. if (ret == -ENOSYS)
  250. ret = 0;
  251. if (ret)
  252. printf("\nWarning: %s failed to set MAC address\n",
  253. dev->name);
  254. }
  255. return ret;
  256. }
  257. static int on_ethaddr(const char *name, const char *value, enum env_op op,
  258. int flags)
  259. {
  260. int index;
  261. int retval;
  262. struct udevice *dev;
  263. /* look for an index after "eth" */
  264. index = simple_strtoul(name + 3, NULL, 10);
  265. retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
  266. if (!retval) {
  267. struct eth_pdata *pdata = dev->platdata;
  268. switch (op) {
  269. case env_op_create:
  270. case env_op_overwrite:
  271. eth_parse_enetaddr(value, pdata->enetaddr);
  272. break;
  273. case env_op_delete:
  274. memset(pdata->enetaddr, 0, 6);
  275. }
  276. }
  277. return 0;
  278. }
  279. U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  280. int eth_init(void)
  281. {
  282. struct udevice *current;
  283. struct udevice *old_current;
  284. int ret = -ENODEV;
  285. current = eth_get_dev();
  286. if (!current) {
  287. printf("No ethernet found.\n");
  288. return -ENODEV;
  289. }
  290. old_current = current;
  291. do {
  292. debug("Trying %s\n", current->name);
  293. if (device_active(current)) {
  294. ret = eth_get_ops(current)->start(current);
  295. if (ret >= 0) {
  296. struct eth_device_priv *priv =
  297. current->uclass_priv;
  298. priv->state = ETH_STATE_ACTIVE;
  299. return 0;
  300. }
  301. } else {
  302. ret = eth_errno;
  303. }
  304. debug("FAIL\n");
  305. /*
  306. * If ethrotate is enabled, this will change "current",
  307. * otherwise we will drop out of this while loop immediately
  308. */
  309. eth_try_another(0);
  310. /* This will ensure the new "current" attempted to probe */
  311. current = eth_get_dev();
  312. } while (old_current != current);
  313. return ret;
  314. }
  315. void eth_halt(void)
  316. {
  317. struct udevice *current;
  318. struct eth_device_priv *priv;
  319. current = eth_get_dev();
  320. if (!current || !device_active(current))
  321. return;
  322. eth_get_ops(current)->stop(current);
  323. priv = current->uclass_priv;
  324. priv->state = ETH_STATE_PASSIVE;
  325. }
  326. int eth_send(void *packet, int length)
  327. {
  328. struct udevice *current;
  329. int ret;
  330. current = eth_get_dev();
  331. if (!current)
  332. return -ENODEV;
  333. if (!device_active(current))
  334. return -EINVAL;
  335. ret = eth_get_ops(current)->send(current, packet, length);
  336. if (ret < 0) {
  337. /* We cannot completely return the error at present */
  338. debug("%s: send() returned error %d\n", __func__, ret);
  339. }
  340. return ret;
  341. }
  342. int eth_rx(void)
  343. {
  344. struct udevice *current;
  345. uchar *packet;
  346. int flags;
  347. int ret;
  348. int i;
  349. current = eth_get_dev();
  350. if (!current)
  351. return -ENODEV;
  352. if (!device_active(current))
  353. return -EINVAL;
  354. /* Process up to 32 packets at one time */
  355. flags = ETH_RECV_CHECK_DEVICE;
  356. for (i = 0; i < 32; i++) {
  357. ret = eth_get_ops(current)->recv(current, flags, &packet);
  358. flags = 0;
  359. if (ret > 0)
  360. net_process_received_packet(packet, ret);
  361. if (ret >= 0 && eth_get_ops(current)->free_pkt)
  362. eth_get_ops(current)->free_pkt(current, packet, ret);
  363. if (ret <= 0)
  364. break;
  365. }
  366. if (ret == -EAGAIN)
  367. ret = 0;
  368. if (ret < 0) {
  369. /* We cannot completely return the error at present */
  370. debug("%s: recv() returned error %d\n", __func__, ret);
  371. }
  372. return ret;
  373. }
  374. int eth_initialize(void)
  375. {
  376. int num_devices = 0;
  377. struct udevice *dev;
  378. eth_common_init();
  379. /*
  380. * Devices need to write the hwaddr even if not started so that Linux
  381. * will have access to the hwaddr that u-boot stored for the device.
  382. * This is accomplished by attempting to probe each device and calling
  383. * their write_hwaddr() operation.
  384. */
  385. uclass_first_device(UCLASS_ETH, &dev);
  386. if (!dev) {
  387. printf("No ethernet found.\n");
  388. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  389. } else {
  390. char *ethprime = getenv("ethprime");
  391. struct udevice *prime_dev = NULL;
  392. if (ethprime)
  393. prime_dev = eth_get_dev_by_name(ethprime);
  394. if (prime_dev) {
  395. eth_set_dev(prime_dev);
  396. eth_current_changed();
  397. } else {
  398. eth_set_dev(NULL);
  399. }
  400. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  401. do {
  402. if (num_devices)
  403. printf(", ");
  404. printf("eth%d: %s", dev->seq, dev->name);
  405. if (ethprime && dev == prime_dev)
  406. printf(" [PRIME]");
  407. eth_write_hwaddr(dev);
  408. uclass_next_device(&dev);
  409. num_devices++;
  410. } while (dev);
  411. putc('\n');
  412. }
  413. return num_devices;
  414. }
  415. static int eth_post_bind(struct udevice *dev)
  416. {
  417. if (strchr(dev->name, ' ')) {
  418. printf("\nError: eth device name \"%s\" has a space!\n",
  419. dev->name);
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. static int eth_pre_unbind(struct udevice *dev)
  425. {
  426. /* Don't hang onto a pointer that is going away */
  427. if (dev == eth_get_uclass_priv()->current)
  428. eth_set_dev(NULL);
  429. return 0;
  430. }
  431. static int eth_post_probe(struct udevice *dev)
  432. {
  433. struct eth_device_priv *priv = dev->uclass_priv;
  434. struct eth_pdata *pdata = dev->platdata;
  435. unsigned char env_enetaddr[6];
  436. priv->state = ETH_STATE_INIT;
  437. /* Check if the device has a MAC address in ROM */
  438. if (eth_get_ops(dev)->read_rom_hwaddr)
  439. eth_get_ops(dev)->read_rom_hwaddr(dev);
  440. eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
  441. if (!is_zero_ethaddr(env_enetaddr)) {
  442. if (!is_zero_ethaddr(pdata->enetaddr) &&
  443. memcmp(pdata->enetaddr, env_enetaddr, 6)) {
  444. printf("\nWarning: %s MAC addresses don't match:\n",
  445. dev->name);
  446. printf("Address in SROM is %pM\n",
  447. pdata->enetaddr);
  448. printf("Address in environment is %pM\n",
  449. env_enetaddr);
  450. }
  451. /* Override the ROM MAC address */
  452. memcpy(pdata->enetaddr, env_enetaddr, 6);
  453. } else if (is_valid_ethaddr(pdata->enetaddr)) {
  454. eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
  455. printf("\nWarning: %s using MAC address from ROM\n",
  456. dev->name);
  457. } else if (is_zero_ethaddr(pdata->enetaddr)) {
  458. #ifdef CONFIG_NET_RANDOM_ETHADDR
  459. net_random_ethaddr(pdata->enetaddr);
  460. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  461. dev->name, dev->seq, pdata->enetaddr);
  462. #else
  463. printf("\nError: %s address not set.\n",
  464. dev->name);
  465. return -EINVAL;
  466. #endif
  467. }
  468. return 0;
  469. }
  470. static int eth_pre_remove(struct udevice *dev)
  471. {
  472. eth_get_ops(dev)->stop(dev);
  473. return 0;
  474. }
  475. UCLASS_DRIVER(eth) = {
  476. .name = "eth",
  477. .id = UCLASS_ETH,
  478. .post_bind = eth_post_bind,
  479. .pre_unbind = eth_pre_unbind,
  480. .post_probe = eth_post_probe,
  481. .pre_remove = eth_pre_remove,
  482. .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
  483. .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
  484. .flags = DM_UC_FLAG_SEQ_ALIAS,
  485. };
  486. #endif
  487. #ifndef CONFIG_DM_ETH
  488. #ifdef CONFIG_API
  489. static struct {
  490. uchar data[PKTSIZE];
  491. int length;
  492. } eth_rcv_bufs[PKTBUFSRX];
  493. static unsigned int eth_rcv_current, eth_rcv_last;
  494. #endif
  495. static struct eth_device *eth_devices;
  496. struct eth_device *eth_current;
  497. static void eth_set_current_to_next(void)
  498. {
  499. eth_current = eth_current->next;
  500. }
  501. static void eth_set_dev(struct eth_device *dev)
  502. {
  503. eth_current = dev;
  504. }
  505. struct eth_device *eth_get_dev_by_name(const char *devname)
  506. {
  507. struct eth_device *dev, *target_dev;
  508. BUG_ON(devname == NULL);
  509. if (!eth_devices)
  510. return NULL;
  511. dev = eth_devices;
  512. target_dev = NULL;
  513. do {
  514. if (strcmp(devname, dev->name) == 0) {
  515. target_dev = dev;
  516. break;
  517. }
  518. dev = dev->next;
  519. } while (dev != eth_devices);
  520. return target_dev;
  521. }
  522. struct eth_device *eth_get_dev_by_index(int index)
  523. {
  524. struct eth_device *dev, *target_dev;
  525. if (!eth_devices)
  526. return NULL;
  527. dev = eth_devices;
  528. target_dev = NULL;
  529. do {
  530. if (dev->index == index) {
  531. target_dev = dev;
  532. break;
  533. }
  534. dev = dev->next;
  535. } while (dev != eth_devices);
  536. return target_dev;
  537. }
  538. int eth_get_dev_index(void)
  539. {
  540. if (!eth_current)
  541. return -1;
  542. return eth_current->index;
  543. }
  544. static int on_ethaddr(const char *name, const char *value, enum env_op op,
  545. int flags)
  546. {
  547. int index;
  548. struct eth_device *dev;
  549. if (!eth_devices)
  550. return 0;
  551. /* look for an index after "eth" */
  552. index = simple_strtoul(name + 3, NULL, 10);
  553. dev = eth_devices;
  554. do {
  555. if (dev->index == index) {
  556. switch (op) {
  557. case env_op_create:
  558. case env_op_overwrite:
  559. eth_parse_enetaddr(value, dev->enetaddr);
  560. break;
  561. case env_op_delete:
  562. memset(dev->enetaddr, 0, 6);
  563. }
  564. }
  565. } while (dev != eth_devices);
  566. return 0;
  567. }
  568. U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  569. int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
  570. int eth_number)
  571. {
  572. unsigned char env_enetaddr[6];
  573. int ret = 0;
  574. eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
  575. if (!is_zero_ethaddr(env_enetaddr)) {
  576. if (!is_zero_ethaddr(dev->enetaddr) &&
  577. memcmp(dev->enetaddr, env_enetaddr, 6)) {
  578. printf("\nWarning: %s MAC addresses don't match:\n",
  579. dev->name);
  580. printf("Address in SROM is %pM\n",
  581. dev->enetaddr);
  582. printf("Address in environment is %pM\n",
  583. env_enetaddr);
  584. }
  585. memcpy(dev->enetaddr, env_enetaddr, 6);
  586. } else if (is_valid_ethaddr(dev->enetaddr)) {
  587. eth_setenv_enetaddr_by_index(base_name, eth_number,
  588. dev->enetaddr);
  589. printf("\nWarning: %s using MAC address from net device\n",
  590. dev->name);
  591. } else if (is_zero_ethaddr(dev->enetaddr)) {
  592. #ifdef CONFIG_NET_RANDOM_ETHADDR
  593. net_random_ethaddr(dev->enetaddr);
  594. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  595. dev->name, eth_number, dev->enetaddr);
  596. #else
  597. printf("\nError: %s address not set.\n",
  598. dev->name);
  599. return -EINVAL;
  600. #endif
  601. }
  602. if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
  603. if (!is_valid_ethaddr(dev->enetaddr)) {
  604. printf("\nError: %s address %pM illegal value\n",
  605. dev->name, dev->enetaddr);
  606. return -EINVAL;
  607. }
  608. ret = dev->write_hwaddr(dev);
  609. if (ret)
  610. printf("\nWarning: %s failed to set MAC address\n",
  611. dev->name);
  612. }
  613. return ret;
  614. }
  615. int eth_register(struct eth_device *dev)
  616. {
  617. struct eth_device *d;
  618. static int index;
  619. assert(strlen(dev->name) < sizeof(dev->name));
  620. if (!eth_devices) {
  621. eth_devices = dev;
  622. eth_current = dev;
  623. eth_current_changed();
  624. } else {
  625. for (d = eth_devices; d->next != eth_devices; d = d->next)
  626. ;
  627. d->next = dev;
  628. }
  629. dev->state = ETH_STATE_INIT;
  630. dev->next = eth_devices;
  631. dev->index = index++;
  632. return 0;
  633. }
  634. int eth_unregister(struct eth_device *dev)
  635. {
  636. struct eth_device *cur;
  637. /* No device */
  638. if (!eth_devices)
  639. return -ENODEV;
  640. for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
  641. cur = cur->next)
  642. ;
  643. /* Device not found */
  644. if (cur->next != dev)
  645. return -ENODEV;
  646. cur->next = dev->next;
  647. if (eth_devices == dev)
  648. eth_devices = dev->next == eth_devices ? NULL : dev->next;
  649. if (eth_current == dev) {
  650. eth_current = eth_devices;
  651. eth_current_changed();
  652. }
  653. return 0;
  654. }
  655. int eth_initialize(void)
  656. {
  657. int num_devices = 0;
  658. eth_devices = NULL;
  659. eth_current = NULL;
  660. eth_common_init();
  661. if (!eth_devices) {
  662. puts("No ethernet found.\n");
  663. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  664. } else {
  665. struct eth_device *dev = eth_devices;
  666. char *ethprime = getenv("ethprime");
  667. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  668. do {
  669. if (dev->index)
  670. puts(", ");
  671. printf("%s", dev->name);
  672. if (ethprime && strcmp(dev->name, ethprime) == 0) {
  673. eth_current = dev;
  674. puts(" [PRIME]");
  675. }
  676. if (strchr(dev->name, ' '))
  677. puts("\nWarning: eth device name has a space!"
  678. "\n");
  679. eth_write_hwaddr(dev, "eth", dev->index);
  680. dev = dev->next;
  681. num_devices++;
  682. } while (dev != eth_devices);
  683. eth_current_changed();
  684. putc('\n');
  685. }
  686. return num_devices;
  687. }
  688. #ifdef CONFIG_MCAST_TFTP
  689. /* Multicast.
  690. * mcast_addr: multicast ipaddr from which multicast Mac is made
  691. * join: 1=join, 0=leave.
  692. */
  693. int eth_mcast_join(struct in_addr mcast_ip, int join)
  694. {
  695. u8 mcast_mac[6];
  696. if (!eth_current || !eth_current->mcast)
  697. return -1;
  698. mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
  699. mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
  700. mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
  701. mcast_mac[2] = 0x5e;
  702. mcast_mac[1] = 0x0;
  703. mcast_mac[0] = 0x1;
  704. return eth_current->mcast(eth_current, mcast_mac, join);
  705. }
  706. /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
  707. * and this is the ethernet-crc method needed for TSEC -- and perhaps
  708. * some other adapter -- hash tables
  709. */
  710. #define CRCPOLY_LE 0xedb88320
  711. u32 ether_crc(size_t len, unsigned char const *p)
  712. {
  713. int i;
  714. u32 crc;
  715. crc = ~0;
  716. while (len--) {
  717. crc ^= *p++;
  718. for (i = 0; i < 8; i++)
  719. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  720. }
  721. /* an reverse the bits, cuz of way they arrive -- last-first */
  722. crc = (crc >> 16) | (crc << 16);
  723. crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
  724. crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
  725. crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
  726. crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
  727. return crc;
  728. }
  729. #endif
  730. int eth_init(void)
  731. {
  732. struct eth_device *old_current;
  733. if (!eth_current) {
  734. puts("No ethernet found.\n");
  735. return -ENODEV;
  736. }
  737. old_current = eth_current;
  738. do {
  739. debug("Trying %s\n", eth_current->name);
  740. if (eth_current->init(eth_current, gd->bd) >= 0) {
  741. eth_current->state = ETH_STATE_ACTIVE;
  742. return 0;
  743. }
  744. debug("FAIL\n");
  745. eth_try_another(0);
  746. } while (old_current != eth_current);
  747. return -ETIMEDOUT;
  748. }
  749. void eth_halt(void)
  750. {
  751. if (!eth_current)
  752. return;
  753. eth_current->halt(eth_current);
  754. eth_current->state = ETH_STATE_PASSIVE;
  755. }
  756. int eth_send(void *packet, int length)
  757. {
  758. if (!eth_current)
  759. return -ENODEV;
  760. return eth_current->send(eth_current, packet, length);
  761. }
  762. int eth_rx(void)
  763. {
  764. if (!eth_current)
  765. return -ENODEV;
  766. return eth_current->recv(eth_current);
  767. }
  768. #endif /* ifndef CONFIG_DM_ETH */
  769. #ifdef CONFIG_API
  770. static void eth_save_packet(void *packet, int length)
  771. {
  772. char *p = packet;
  773. int i;
  774. if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
  775. return;
  776. if (PKTSIZE < length)
  777. return;
  778. for (i = 0; i < length; i++)
  779. eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
  780. eth_rcv_bufs[eth_rcv_last].length = length;
  781. eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
  782. }
  783. int eth_receive(void *packet, int length)
  784. {
  785. char *p = packet;
  786. void *pp = push_packet;
  787. int i;
  788. if (eth_rcv_current == eth_rcv_last) {
  789. push_packet = eth_save_packet;
  790. eth_rx();
  791. push_packet = pp;
  792. if (eth_rcv_current == eth_rcv_last)
  793. return -1;
  794. }
  795. length = min(eth_rcv_bufs[eth_rcv_current].length, length);
  796. for (i = 0; i < length; i++)
  797. p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
  798. eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
  799. return length;
  800. }
  801. #endif /* CONFIG_API */
  802. static void eth_current_changed(void)
  803. {
  804. char *act = getenv("ethact");
  805. /* update current ethernet name */
  806. if (eth_get_dev()) {
  807. if (act == NULL || strcmp(act, eth_get_name()) != 0)
  808. setenv("ethact", eth_get_name());
  809. }
  810. /*
  811. * remove the variable completely if there is no active
  812. * interface
  813. */
  814. else if (act != NULL)
  815. setenv("ethact", NULL);
  816. }
  817. void eth_try_another(int first_restart)
  818. {
  819. static void *first_failed;
  820. char *ethrotate;
  821. /*
  822. * Do not rotate between network interfaces when
  823. * 'ethrotate' variable is set to 'no'.
  824. */
  825. ethrotate = getenv("ethrotate");
  826. if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
  827. return;
  828. if (!eth_get_dev())
  829. return;
  830. if (first_restart)
  831. first_failed = eth_get_dev();
  832. eth_set_current_to_next();
  833. eth_current_changed();
  834. if (first_failed == eth_get_dev())
  835. net_restart_wrap = 1;
  836. }
  837. void eth_set_current(void)
  838. {
  839. static char *act;
  840. static int env_changed_id;
  841. int env_id;
  842. env_id = get_env_id();
  843. if ((act == NULL) || (env_changed_id != env_id)) {
  844. act = getenv("ethact");
  845. env_changed_id = env_id;
  846. }
  847. if (act == NULL) {
  848. char *ethprime = getenv("ethprime");
  849. void *dev = NULL;
  850. if (ethprime)
  851. dev = eth_get_dev_by_name(ethprime);
  852. if (dev)
  853. eth_set_dev(dev);
  854. else
  855. eth_set_dev(NULL);
  856. } else {
  857. eth_set_dev(eth_get_dev_by_name(act));
  858. }
  859. eth_current_changed();
  860. }
  861. const char *eth_get_name(void)
  862. {
  863. return eth_get_dev() ? eth_get_dev()->name : "unknown";
  864. }