eth-uclass.c 13 KB

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