eth-uclass.c 13 KB

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