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