eth-uclass.c 15 KB

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