eth-uclass.c 13 KB

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