eth-uclass.c 13 KB

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