eth-uclass.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001-2015
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. * Joe Hershberger, National Instruments
  6. */
  7. #define LOG_CATEGORY UCLASS_ETH
  8. #include <common.h>
  9. #include <bootstage.h>
  10. #include <dm.h>
  11. #include <env.h>
  12. #include <log.h>
  13. #include <net.h>
  14. #include <asm/global_data.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/uclass-internal.h>
  17. #include <net/pcap.h>
  18. #include "eth_internal.h"
  19. #include <eth_phy.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /**
  22. * struct eth_device_priv - private structure for each Ethernet device
  23. *
  24. * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
  25. */
  26. struct eth_device_priv {
  27. enum eth_state_t state;
  28. bool running;
  29. };
  30. /**
  31. * struct eth_uclass_priv - The structure attached to the uclass itself
  32. *
  33. * @current: The Ethernet device that the network functions are using
  34. */
  35. struct eth_uclass_priv {
  36. struct udevice *current;
  37. };
  38. /* eth_errno - This stores the most recent failure code from DM functions */
  39. static int eth_errno;
  40. static struct eth_uclass_priv *eth_get_uclass_priv(void)
  41. {
  42. struct uclass *uc;
  43. int ret;
  44. ret = uclass_get(UCLASS_ETH, &uc);
  45. if (ret)
  46. return NULL;
  47. assert(uc);
  48. return uclass_get_priv(uc);
  49. }
  50. void eth_set_current_to_next(void)
  51. {
  52. struct eth_uclass_priv *uc_priv;
  53. uc_priv = eth_get_uclass_priv();
  54. if (uc_priv->current)
  55. uclass_next_device(&uc_priv->current);
  56. if (!uc_priv->current)
  57. uclass_first_device(UCLASS_ETH, &uc_priv->current);
  58. }
  59. /*
  60. * Typically this will simply return the active device.
  61. * In the case where the most recent active device was unset, this will attempt
  62. * to return the device with sequence id 0 (which can be configured by the
  63. * device tree). If this fails, fall back to just getting the first device.
  64. * The latter is non-deterministic and depends on the order of the probing.
  65. * If that device doesn't exist or fails to probe, this function will return
  66. * NULL.
  67. */
  68. struct udevice *eth_get_dev(void)
  69. {
  70. struct eth_uclass_priv *uc_priv;
  71. uc_priv = eth_get_uclass_priv();
  72. if (!uc_priv)
  73. return NULL;
  74. if (!uc_priv->current) {
  75. eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
  76. &uc_priv->current);
  77. if (eth_errno)
  78. eth_errno = uclass_first_device(UCLASS_ETH,
  79. &uc_priv->current);
  80. }
  81. return uc_priv->current;
  82. }
  83. /*
  84. * Typically this will just store a device pointer.
  85. * In case it was not probed, we will attempt to do so.
  86. * dev may be NULL to unset the active device.
  87. */
  88. void eth_set_dev(struct udevice *dev)
  89. {
  90. if (dev && !device_active(dev)) {
  91. eth_errno = device_probe(dev);
  92. if (eth_errno)
  93. dev = NULL;
  94. }
  95. eth_get_uclass_priv()->current = dev;
  96. }
  97. /*
  98. * Find the udevice that either has the name passed in as devname or has an
  99. * alias named devname.
  100. */
  101. struct udevice *eth_get_dev_by_name(const char *devname)
  102. {
  103. int seq = -1;
  104. char *endp = NULL;
  105. const char *startp = NULL;
  106. struct udevice *it;
  107. struct uclass *uc;
  108. int len = strlen("eth");
  109. int ret;
  110. /* Must be longer than 3 to be an alias */
  111. if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
  112. startp = devname + len;
  113. seq = dectoul(startp, &endp);
  114. }
  115. ret = uclass_get(UCLASS_ETH, &uc);
  116. if (ret)
  117. return NULL;
  118. uclass_foreach_dev(it, uc) {
  119. /*
  120. * We don't care about errors from probe here. Either they won't
  121. * match an alias or it will match a literal name and we'll pick
  122. * up the error when we try to probe again in eth_set_dev().
  123. */
  124. if (device_probe(it))
  125. continue;
  126. /* Check for the name or the sequence number to match */
  127. if (strcmp(it->name, devname) == 0 ||
  128. (endp > startp && dev_seq(it) == seq))
  129. return it;
  130. }
  131. return NULL;
  132. }
  133. unsigned char *eth_get_ethaddr(void)
  134. {
  135. struct eth_pdata *pdata;
  136. if (eth_get_dev()) {
  137. pdata = dev_get_plat(eth_get_dev());
  138. return pdata->enetaddr;
  139. }
  140. return NULL;
  141. }
  142. /* Set active state without calling start on the driver */
  143. int eth_init_state_only(void)
  144. {
  145. struct udevice *current;
  146. struct eth_device_priv *priv;
  147. current = eth_get_dev();
  148. if (!current || !device_active(current))
  149. return -EINVAL;
  150. priv = dev_get_uclass_priv(current);
  151. priv->state = ETH_STATE_ACTIVE;
  152. return 0;
  153. }
  154. /* Set passive state without calling stop on the driver */
  155. void eth_halt_state_only(void)
  156. {
  157. struct udevice *current;
  158. struct eth_device_priv *priv;
  159. current = eth_get_dev();
  160. if (!current || !device_active(current))
  161. return;
  162. priv = dev_get_uclass_priv(current);
  163. priv->state = ETH_STATE_PASSIVE;
  164. }
  165. int eth_get_dev_index(void)
  166. {
  167. if (eth_get_dev())
  168. return dev_seq(eth_get_dev());
  169. return -1;
  170. }
  171. static int eth_write_hwaddr(struct udevice *dev)
  172. {
  173. struct eth_pdata *pdata;
  174. int ret = 0;
  175. if (!dev || !device_active(dev))
  176. return -EINVAL;
  177. /* seq is valid since the device is active */
  178. if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
  179. pdata = dev_get_plat(dev);
  180. if (!is_valid_ethaddr(pdata->enetaddr)) {
  181. printf("\nError: %s address %pM illegal value\n",
  182. dev->name, pdata->enetaddr);
  183. return -EINVAL;
  184. }
  185. /*
  186. * Drivers are allowed to decide not to implement this at
  187. * run-time. E.g. Some devices may use it and some may not.
  188. */
  189. ret = eth_get_ops(dev)->write_hwaddr(dev);
  190. if (ret == -ENOSYS)
  191. ret = 0;
  192. if (ret)
  193. printf("\nWarning: %s failed to set MAC address\n",
  194. dev->name);
  195. }
  196. return ret;
  197. }
  198. static int on_ethaddr(const char *name, const char *value, enum env_op op,
  199. int flags)
  200. {
  201. int index;
  202. int retval;
  203. struct udevice *dev;
  204. /* look for an index after "eth" */
  205. index = dectoul(name + 3, NULL);
  206. retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
  207. if (!retval) {
  208. struct eth_pdata *pdata = dev_get_plat(dev);
  209. switch (op) {
  210. case env_op_create:
  211. case env_op_overwrite:
  212. string_to_enetaddr(value, pdata->enetaddr);
  213. eth_write_hwaddr(dev);
  214. break;
  215. case env_op_delete:
  216. memset(pdata->enetaddr, 0, ARP_HLEN);
  217. }
  218. }
  219. return 0;
  220. }
  221. U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  222. int eth_init(void)
  223. {
  224. char *ethact = env_get("ethact");
  225. char *ethrotate = env_get("ethrotate");
  226. struct udevice *current = NULL;
  227. struct udevice *old_current;
  228. int ret = -ENODEV;
  229. /*
  230. * When 'ethrotate' variable is set to 'no' and 'ethact' variable
  231. * is already set to an ethernet device, we should stick to 'ethact'.
  232. */
  233. if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
  234. if (ethact) {
  235. current = eth_get_dev_by_name(ethact);
  236. if (!current)
  237. return -EINVAL;
  238. }
  239. }
  240. if (!current) {
  241. current = eth_get_dev();
  242. if (!current) {
  243. log_err("No ethernet found.\n");
  244. return -ENODEV;
  245. }
  246. }
  247. old_current = current;
  248. do {
  249. if (current) {
  250. debug("Trying %s\n", current->name);
  251. if (device_active(current)) {
  252. ret = eth_get_ops(current)->start(current);
  253. if (ret >= 0) {
  254. struct eth_device_priv *priv =
  255. dev_get_uclass_priv(current);
  256. priv->state = ETH_STATE_ACTIVE;
  257. priv->running = true;
  258. return 0;
  259. }
  260. } else {
  261. ret = eth_errno;
  262. }
  263. debug("FAIL\n");
  264. } else {
  265. debug("PROBE FAIL\n");
  266. }
  267. /*
  268. * If ethrotate is enabled, this will change "current",
  269. * otherwise we will drop out of this while loop immediately
  270. */
  271. eth_try_another(0);
  272. /* This will ensure the new "current" attempted to probe */
  273. current = eth_get_dev();
  274. } while (old_current != current);
  275. return ret;
  276. }
  277. void eth_halt(void)
  278. {
  279. struct udevice *current;
  280. struct eth_device_priv *priv;
  281. current = eth_get_dev();
  282. if (!current)
  283. return;
  284. priv = dev_get_uclass_priv(current);
  285. if (!priv || !priv->running)
  286. return;
  287. eth_get_ops(current)->stop(current);
  288. priv->state = ETH_STATE_PASSIVE;
  289. priv->running = false;
  290. }
  291. int eth_is_active(struct udevice *dev)
  292. {
  293. struct eth_device_priv *priv;
  294. if (!dev || !device_active(dev))
  295. return 0;
  296. priv = dev_get_uclass_priv(dev);
  297. return priv->state == ETH_STATE_ACTIVE;
  298. }
  299. int eth_send(void *packet, int length)
  300. {
  301. struct udevice *current;
  302. int ret;
  303. current = eth_get_dev();
  304. if (!current)
  305. return -ENODEV;
  306. if (!eth_is_active(current))
  307. return -EINVAL;
  308. ret = eth_get_ops(current)->send(current, packet, length);
  309. if (ret < 0) {
  310. /* We cannot completely return the error at present */
  311. debug("%s: send() returned error %d\n", __func__, ret);
  312. }
  313. #if defined(CONFIG_CMD_PCAP)
  314. if (ret >= 0)
  315. pcap_post(packet, length, true);
  316. #endif
  317. return ret;
  318. }
  319. int eth_rx(void)
  320. {
  321. struct udevice *current;
  322. uchar *packet;
  323. int flags;
  324. int ret;
  325. int i;
  326. current = eth_get_dev();
  327. if (!current)
  328. return -ENODEV;
  329. if (!eth_is_active(current))
  330. return -EINVAL;
  331. /* Process up to 32 packets at one time */
  332. flags = ETH_RECV_CHECK_DEVICE;
  333. for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
  334. ret = eth_get_ops(current)->recv(current, flags, &packet);
  335. flags = 0;
  336. if (ret > 0)
  337. net_process_received_packet(packet, ret);
  338. if (ret >= 0 && eth_get_ops(current)->free_pkt)
  339. eth_get_ops(current)->free_pkt(current, packet, ret);
  340. if (ret <= 0)
  341. break;
  342. }
  343. if (ret == -EAGAIN)
  344. ret = 0;
  345. if (ret < 0) {
  346. /* We cannot completely return the error at present */
  347. debug("%s: recv() returned error %d\n", __func__, ret);
  348. }
  349. return ret;
  350. }
  351. int eth_initialize(void)
  352. {
  353. int num_devices = 0;
  354. struct udevice *dev;
  355. eth_common_init();
  356. /*
  357. * Devices need to write the hwaddr even if not started so that Linux
  358. * will have access to the hwaddr that u-boot stored for the device.
  359. * This is accomplished by attempting to probe each device and calling
  360. * their write_hwaddr() operation.
  361. */
  362. uclass_first_device_check(UCLASS_ETH, &dev);
  363. if (!dev) {
  364. log_err("No ethernet found.\n");
  365. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  366. } else {
  367. char *ethprime = env_get("ethprime");
  368. struct udevice *prime_dev = NULL;
  369. if (ethprime)
  370. prime_dev = eth_get_dev_by_name(ethprime);
  371. if (prime_dev) {
  372. eth_set_dev(prime_dev);
  373. eth_current_changed();
  374. } else {
  375. eth_set_dev(NULL);
  376. }
  377. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  378. do {
  379. if (device_active(dev)) {
  380. if (num_devices)
  381. printf(", ");
  382. printf("eth%d: %s", dev_seq(dev), dev->name);
  383. if (ethprime && dev == prime_dev)
  384. printf(" [PRIME]");
  385. }
  386. eth_write_hwaddr(dev);
  387. if (device_active(dev))
  388. num_devices++;
  389. uclass_next_device_check(&dev);
  390. } while (dev);
  391. if (!num_devices)
  392. log_err("No ethernet found.\n");
  393. putc('\n');
  394. }
  395. return num_devices;
  396. }
  397. static int eth_post_bind(struct udevice *dev)
  398. {
  399. if (strchr(dev->name, ' ')) {
  400. printf("\nError: eth device name \"%s\" has a space!\n",
  401. dev->name);
  402. return -EINVAL;
  403. }
  404. #ifdef CONFIG_DM_ETH_PHY
  405. eth_phy_binds_nodes(dev);
  406. #endif
  407. return 0;
  408. }
  409. static int eth_pre_unbind(struct udevice *dev)
  410. {
  411. /* Don't hang onto a pointer that is going away */
  412. if (dev == eth_get_uclass_priv()->current)
  413. eth_set_dev(NULL);
  414. return 0;
  415. }
  416. static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
  417. {
  418. #if CONFIG_IS_ENABLED(OF_CONTROL)
  419. const uint8_t *p;
  420. p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
  421. if (!p)
  422. p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
  423. if (!p)
  424. return false;
  425. memcpy(mac, p, ARP_HLEN);
  426. return true;
  427. #else
  428. return false;
  429. #endif
  430. }
  431. static int eth_post_probe(struct udevice *dev)
  432. {
  433. struct eth_device_priv *priv = dev_get_uclass_priv(dev);
  434. struct eth_pdata *pdata = dev_get_plat(dev);
  435. unsigned char env_enetaddr[ARP_HLEN];
  436. char *source = "DT";
  437. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  438. struct eth_ops *ops = eth_get_ops(dev);
  439. static int reloc_done;
  440. if (!reloc_done) {
  441. if (ops->start)
  442. ops->start += gd->reloc_off;
  443. if (ops->send)
  444. ops->send += gd->reloc_off;
  445. if (ops->recv)
  446. ops->recv += gd->reloc_off;
  447. if (ops->free_pkt)
  448. ops->free_pkt += gd->reloc_off;
  449. if (ops->stop)
  450. ops->stop += gd->reloc_off;
  451. if (ops->mcast)
  452. ops->mcast += gd->reloc_off;
  453. if (ops->write_hwaddr)
  454. ops->write_hwaddr += gd->reloc_off;
  455. if (ops->read_rom_hwaddr)
  456. ops->read_rom_hwaddr += gd->reloc_off;
  457. reloc_done++;
  458. }
  459. #endif
  460. priv->state = ETH_STATE_INIT;
  461. priv->running = false;
  462. /* Check if the device has a valid MAC address in device tree */
  463. if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
  464. !is_valid_ethaddr(pdata->enetaddr)) {
  465. source = "ROM";
  466. /* Check if the device has a MAC address in ROM */
  467. if (eth_get_ops(dev)->read_rom_hwaddr)
  468. eth_get_ops(dev)->read_rom_hwaddr(dev);
  469. }
  470. eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
  471. if (!is_zero_ethaddr(env_enetaddr)) {
  472. if (!is_zero_ethaddr(pdata->enetaddr) &&
  473. memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
  474. printf("\nWarning: %s MAC addresses don't match:\n",
  475. dev->name);
  476. printf("Address in %s is\t\t%pM\n",
  477. source, pdata->enetaddr);
  478. printf("Address in environment is\t%pM\n",
  479. env_enetaddr);
  480. }
  481. /* Override the ROM MAC address */
  482. memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
  483. } else if (is_valid_ethaddr(pdata->enetaddr)) {
  484. eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
  485. pdata->enetaddr);
  486. } else if (is_zero_ethaddr(pdata->enetaddr) ||
  487. !is_valid_ethaddr(pdata->enetaddr)) {
  488. #ifdef CONFIG_NET_RANDOM_ETHADDR
  489. net_random_ethaddr(pdata->enetaddr);
  490. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  491. dev->name, dev_seq(dev), pdata->enetaddr);
  492. #else
  493. printf("\nError: %s address not set.\n",
  494. dev->name);
  495. return -EINVAL;
  496. #endif
  497. }
  498. eth_write_hwaddr(dev);
  499. return 0;
  500. }
  501. static int eth_pre_remove(struct udevice *dev)
  502. {
  503. struct eth_pdata *pdata = dev_get_plat(dev);
  504. eth_get_ops(dev)->stop(dev);
  505. /* clear the MAC address */
  506. memset(pdata->enetaddr, 0, ARP_HLEN);
  507. return 0;
  508. }
  509. UCLASS_DRIVER(ethernet) = {
  510. .name = "ethernet",
  511. .id = UCLASS_ETH,
  512. .post_bind = eth_post_bind,
  513. .pre_unbind = eth_pre_unbind,
  514. .post_probe = eth_post_probe,
  515. .pre_remove = eth_pre_remove,
  516. .priv_auto = sizeof(struct eth_uclass_priv),
  517. .per_device_auto = sizeof(struct eth_device_priv),
  518. .flags = DM_UC_FLAG_SEQ_ALIAS,
  519. };