eth-uclass.c 13 KB

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