eth-uclass.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 <environment.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 || !device_active(current))
  263. return;
  264. eth_get_ops(current)->stop(current);
  265. priv = current->uclass_priv;
  266. priv->state = ETH_STATE_PASSIVE;
  267. }
  268. int eth_is_active(struct udevice *dev)
  269. {
  270. struct eth_device_priv *priv;
  271. if (!dev || !device_active(dev))
  272. return 0;
  273. priv = dev_get_uclass_priv(dev);
  274. return priv->state == ETH_STATE_ACTIVE;
  275. }
  276. int eth_send(void *packet, int length)
  277. {
  278. struct udevice *current;
  279. int ret;
  280. current = eth_get_dev();
  281. if (!current)
  282. return -ENODEV;
  283. if (!eth_is_active(current))
  284. return -EINVAL;
  285. ret = eth_get_ops(current)->send(current, packet, length);
  286. if (ret < 0) {
  287. /* We cannot completely return the error at present */
  288. debug("%s: send() returned error %d\n", __func__, ret);
  289. }
  290. return ret;
  291. }
  292. int eth_rx(void)
  293. {
  294. struct udevice *current;
  295. uchar *packet;
  296. int flags;
  297. int ret;
  298. int i;
  299. current = eth_get_dev();
  300. if (!current)
  301. return -ENODEV;
  302. if (!eth_is_active(current))
  303. return -EINVAL;
  304. /* Process up to 32 packets at one time */
  305. flags = ETH_RECV_CHECK_DEVICE;
  306. for (i = 0; i < 32; i++) {
  307. ret = eth_get_ops(current)->recv(current, flags, &packet);
  308. flags = 0;
  309. if (ret > 0)
  310. net_process_received_packet(packet, ret);
  311. if (ret >= 0 && eth_get_ops(current)->free_pkt)
  312. eth_get_ops(current)->free_pkt(current, packet, ret);
  313. if (ret <= 0)
  314. break;
  315. }
  316. if (ret == -EAGAIN)
  317. ret = 0;
  318. if (ret < 0) {
  319. /* We cannot completely return the error at present */
  320. debug("%s: recv() returned error %d\n", __func__, ret);
  321. }
  322. return ret;
  323. }
  324. int eth_initialize(void)
  325. {
  326. int num_devices = 0;
  327. struct udevice *dev;
  328. eth_common_init();
  329. /*
  330. * Devices need to write the hwaddr even if not started so that Linux
  331. * will have access to the hwaddr that u-boot stored for the device.
  332. * This is accomplished by attempting to probe each device and calling
  333. * their write_hwaddr() operation.
  334. */
  335. uclass_first_device(UCLASS_ETH, &dev);
  336. if (!dev) {
  337. printf("No ethernet found.\n");
  338. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  339. } else {
  340. char *ethprime = env_get("ethprime");
  341. struct udevice *prime_dev = NULL;
  342. if (ethprime)
  343. prime_dev = eth_get_dev_by_name(ethprime);
  344. if (prime_dev) {
  345. eth_set_dev(prime_dev);
  346. eth_current_changed();
  347. } else {
  348. eth_set_dev(NULL);
  349. }
  350. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  351. do {
  352. if (num_devices)
  353. printf(", ");
  354. printf("eth%d: %s", dev->seq, dev->name);
  355. if (ethprime && dev == prime_dev)
  356. printf(" [PRIME]");
  357. eth_write_hwaddr(dev);
  358. uclass_next_device(&dev);
  359. num_devices++;
  360. } while (dev);
  361. putc('\n');
  362. }
  363. return num_devices;
  364. }
  365. static int eth_post_bind(struct udevice *dev)
  366. {
  367. if (strchr(dev->name, ' ')) {
  368. printf("\nError: eth device name \"%s\" has a space!\n",
  369. dev->name);
  370. return -EINVAL;
  371. }
  372. return 0;
  373. }
  374. static int eth_pre_unbind(struct udevice *dev)
  375. {
  376. /* Don't hang onto a pointer that is going away */
  377. if (dev == eth_get_uclass_priv()->current)
  378. eth_set_dev(NULL);
  379. return 0;
  380. }
  381. static int eth_post_probe(struct udevice *dev)
  382. {
  383. struct eth_device_priv *priv = dev->uclass_priv;
  384. struct eth_pdata *pdata = dev->platdata;
  385. unsigned char env_enetaddr[ARP_HLEN];
  386. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  387. struct eth_ops *ops = eth_get_ops(dev);
  388. static int reloc_done;
  389. if (!reloc_done) {
  390. if (ops->start)
  391. ops->start += gd->reloc_off;
  392. if (ops->send)
  393. ops->send += gd->reloc_off;
  394. if (ops->recv)
  395. ops->recv += gd->reloc_off;
  396. if (ops->free_pkt)
  397. ops->free_pkt += gd->reloc_off;
  398. if (ops->stop)
  399. ops->stop += gd->reloc_off;
  400. #ifdef CONFIG_MCAST_TFTP
  401. if (ops->mcast)
  402. ops->mcast += gd->reloc_off;
  403. #endif
  404. if (ops->write_hwaddr)
  405. ops->write_hwaddr += gd->reloc_off;
  406. if (ops->read_rom_hwaddr)
  407. ops->read_rom_hwaddr += gd->reloc_off;
  408. reloc_done++;
  409. }
  410. #endif
  411. priv->state = ETH_STATE_INIT;
  412. /* Check if the device has a MAC address in ROM */
  413. if (eth_get_ops(dev)->read_rom_hwaddr)
  414. eth_get_ops(dev)->read_rom_hwaddr(dev);
  415. eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
  416. if (!is_zero_ethaddr(env_enetaddr)) {
  417. if (!is_zero_ethaddr(pdata->enetaddr) &&
  418. memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
  419. printf("\nWarning: %s MAC addresses don't match:\n",
  420. dev->name);
  421. printf("Address in ROM is %pM\n",
  422. pdata->enetaddr);
  423. printf("Address in environment is %pM\n",
  424. env_enetaddr);
  425. }
  426. /* Override the ROM MAC address */
  427. memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
  428. } else if (is_valid_ethaddr(pdata->enetaddr)) {
  429. eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
  430. printf("\nWarning: %s using MAC address from ROM\n",
  431. dev->name);
  432. } else if (is_zero_ethaddr(pdata->enetaddr) ||
  433. !is_valid_ethaddr(pdata->enetaddr)) {
  434. #ifdef CONFIG_NET_RANDOM_ETHADDR
  435. net_random_ethaddr(pdata->enetaddr);
  436. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  437. dev->name, dev->seq, pdata->enetaddr);
  438. #else
  439. printf("\nError: %s address not set.\n",
  440. dev->name);
  441. return -EINVAL;
  442. #endif
  443. }
  444. return 0;
  445. }
  446. static int eth_pre_remove(struct udevice *dev)
  447. {
  448. struct eth_pdata *pdata = dev->platdata;
  449. eth_get_ops(dev)->stop(dev);
  450. /* clear the MAC address */
  451. memset(pdata->enetaddr, 0, ARP_HLEN);
  452. return 0;
  453. }
  454. UCLASS_DRIVER(eth) = {
  455. .name = "eth",
  456. .id = UCLASS_ETH,
  457. .post_bind = eth_post_bind,
  458. .pre_unbind = eth_pre_unbind,
  459. .post_probe = eth_post_probe,
  460. .pre_remove = eth_pre_remove,
  461. .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
  462. .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
  463. .flags = DM_UC_FLAG_SEQ_ALIAS,
  464. };