eth.c 22 KB

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