eth-uclass.c 13 KB

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