eth-uclass.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * (C) Copyright 2001-2015
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. * Joe Hershberger, National Instruments
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <environment.h>
  11. #include <net.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/uclass-internal.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 = dev->platdata;
  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. 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. break;
  195. case env_op_delete:
  196. memset(pdata->enetaddr, 0, 6);
  197. }
  198. }
  199. return 0;
  200. }
  201. U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  202. int eth_init(void)
  203. {
  204. char *ethact = getenv("ethact");
  205. char *ethrotate = getenv("ethrotate");
  206. struct udevice *current = NULL;
  207. struct udevice *old_current;
  208. int ret = -ENODEV;
  209. /*
  210. * When 'ethrotate' variable is set to 'no' and 'ethact' variable
  211. * is already set to an ethernet device, we should stick to 'ethact'.
  212. */
  213. if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
  214. if (ethact) {
  215. current = eth_get_dev_by_name(ethact);
  216. if (!current)
  217. return -EINVAL;
  218. }
  219. }
  220. if (!current) {
  221. current = eth_get_dev();
  222. if (!current) {
  223. printf("No ethernet found.\n");
  224. return -ENODEV;
  225. }
  226. }
  227. old_current = current;
  228. do {
  229. if (current) {
  230. debug("Trying %s\n", current->name);
  231. if (device_active(current)) {
  232. ret = eth_get_ops(current)->start(current);
  233. if (ret >= 0) {
  234. struct eth_device_priv *priv =
  235. current->uclass_priv;
  236. priv->state = ETH_STATE_ACTIVE;
  237. return 0;
  238. }
  239. } else {
  240. ret = eth_errno;
  241. }
  242. debug("FAIL\n");
  243. } else {
  244. debug("PROBE FAIL\n");
  245. }
  246. /*
  247. * If ethrotate is enabled, this will change "current",
  248. * otherwise we will drop out of this while loop immediately
  249. */
  250. eth_try_another(0);
  251. /* This will ensure the new "current" attempted to probe */
  252. current = eth_get_dev();
  253. } while (old_current != current);
  254. return ret;
  255. }
  256. void eth_halt(void)
  257. {
  258. struct udevice *current;
  259. struct eth_device_priv *priv;
  260. current = eth_get_dev();
  261. if (!current || !device_active(current))
  262. return;
  263. eth_get_ops(current)->stop(current);
  264. priv = current->uclass_priv;
  265. priv->state = ETH_STATE_PASSIVE;
  266. }
  267. int eth_is_active(struct udevice *dev)
  268. {
  269. struct eth_device_priv *priv;
  270. if (!dev || !device_active(dev))
  271. return 0;
  272. priv = dev_get_uclass_priv(dev);
  273. return priv->state == ETH_STATE_ACTIVE;
  274. }
  275. int eth_send(void *packet, int length)
  276. {
  277. struct udevice *current;
  278. int ret;
  279. current = eth_get_dev();
  280. if (!current)
  281. return -ENODEV;
  282. if (!device_active(current))
  283. return -EINVAL;
  284. ret = eth_get_ops(current)->send(current, packet, length);
  285. if (ret < 0) {
  286. /* We cannot completely return the error at present */
  287. debug("%s: send() returned error %d\n", __func__, ret);
  288. }
  289. return ret;
  290. }
  291. int eth_rx(void)
  292. {
  293. struct udevice *current;
  294. uchar *packet;
  295. int flags;
  296. int ret;
  297. int i;
  298. current = eth_get_dev();
  299. if (!current)
  300. return -ENODEV;
  301. if (!device_active(current))
  302. return -EINVAL;
  303. /* Process up to 32 packets at one time */
  304. flags = ETH_RECV_CHECK_DEVICE;
  305. for (i = 0; i < 32; i++) {
  306. ret = eth_get_ops(current)->recv(current, flags, &packet);
  307. flags = 0;
  308. if (ret > 0)
  309. net_process_received_packet(packet, ret);
  310. if (ret >= 0 && eth_get_ops(current)->free_pkt)
  311. eth_get_ops(current)->free_pkt(current, packet, ret);
  312. if (ret <= 0)
  313. break;
  314. }
  315. if (ret == -EAGAIN)
  316. ret = 0;
  317. if (ret < 0) {
  318. /* We cannot completely return the error at present */
  319. debug("%s: recv() returned error %d\n", __func__, ret);
  320. }
  321. return ret;
  322. }
  323. int eth_initialize(void)
  324. {
  325. int num_devices = 0;
  326. struct udevice *dev;
  327. eth_common_init();
  328. /*
  329. * Devices need to write the hwaddr even if not started so that Linux
  330. * will have access to the hwaddr that u-boot stored for the device.
  331. * This is accomplished by attempting to probe each device and calling
  332. * their write_hwaddr() operation.
  333. */
  334. uclass_first_device(UCLASS_ETH, &dev);
  335. if (!dev) {
  336. printf("No ethernet found.\n");
  337. bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  338. } else {
  339. char *ethprime = getenv("ethprime");
  340. struct udevice *prime_dev = NULL;
  341. if (ethprime)
  342. prime_dev = eth_get_dev_by_name(ethprime);
  343. if (prime_dev) {
  344. eth_set_dev(prime_dev);
  345. eth_current_changed();
  346. } else {
  347. eth_set_dev(NULL);
  348. }
  349. bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  350. do {
  351. if (num_devices)
  352. printf(", ");
  353. printf("eth%d: %s", dev->seq, dev->name);
  354. if (ethprime && dev == prime_dev)
  355. printf(" [PRIME]");
  356. eth_write_hwaddr(dev);
  357. uclass_next_device(&dev);
  358. num_devices++;
  359. } while (dev);
  360. putc('\n');
  361. }
  362. return num_devices;
  363. }
  364. static int eth_post_bind(struct udevice *dev)
  365. {
  366. if (strchr(dev->name, ' ')) {
  367. printf("\nError: eth device name \"%s\" has a space!\n",
  368. dev->name);
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. static int eth_pre_unbind(struct udevice *dev)
  374. {
  375. /* Don't hang onto a pointer that is going away */
  376. if (dev == eth_get_uclass_priv()->current)
  377. eth_set_dev(NULL);
  378. return 0;
  379. }
  380. static int eth_post_probe(struct udevice *dev)
  381. {
  382. struct eth_device_priv *priv = dev->uclass_priv;
  383. struct eth_pdata *pdata = dev->platdata;
  384. unsigned char env_enetaddr[6];
  385. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  386. struct eth_ops *ops = eth_get_ops(dev);
  387. static int reloc_done;
  388. if (!reloc_done) {
  389. if (ops->start)
  390. ops->start += gd->reloc_off;
  391. if (ops->send)
  392. ops->send += gd->reloc_off;
  393. if (ops->recv)
  394. ops->recv += gd->reloc_off;
  395. if (ops->free_pkt)
  396. ops->free_pkt += gd->reloc_off;
  397. if (ops->stop)
  398. ops->stop += gd->reloc_off;
  399. #ifdef CONFIG_MCAST_TFTP
  400. if (ops->mcast)
  401. ops->mcast += gd->reloc_off;
  402. #endif
  403. if (ops->write_hwaddr)
  404. ops->write_hwaddr += gd->reloc_off;
  405. if (ops->read_rom_hwaddr)
  406. ops->read_rom_hwaddr += gd->reloc_off;
  407. reloc_done++;
  408. }
  409. #endif
  410. priv->state = ETH_STATE_INIT;
  411. /* Check if the device has a MAC address in ROM */
  412. if (eth_get_ops(dev)->read_rom_hwaddr)
  413. eth_get_ops(dev)->read_rom_hwaddr(dev);
  414. eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
  415. if (!is_zero_ethaddr(env_enetaddr)) {
  416. if (!is_zero_ethaddr(pdata->enetaddr) &&
  417. memcmp(pdata->enetaddr, env_enetaddr, 6)) {
  418. printf("\nWarning: %s MAC addresses don't match:\n",
  419. dev->name);
  420. printf("Address in SROM is %pM\n",
  421. pdata->enetaddr);
  422. printf("Address in environment is %pM\n",
  423. env_enetaddr);
  424. }
  425. /* Override the ROM MAC address */
  426. memcpy(pdata->enetaddr, env_enetaddr, 6);
  427. } else if (is_valid_ethaddr(pdata->enetaddr)) {
  428. eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
  429. printf("\nWarning: %s using MAC address from ROM\n",
  430. dev->name);
  431. } else if (is_zero_ethaddr(pdata->enetaddr)) {
  432. #ifdef CONFIG_NET_RANDOM_ETHADDR
  433. net_random_ethaddr(pdata->enetaddr);
  434. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  435. dev->name, dev->seq, pdata->enetaddr);
  436. #else
  437. printf("\nError: %s address not set.\n",
  438. dev->name);
  439. return -EINVAL;
  440. #endif
  441. }
  442. return 0;
  443. }
  444. static int eth_pre_remove(struct udevice *dev)
  445. {
  446. struct eth_pdata *pdata = dev->platdata;
  447. eth_get_ops(dev)->stop(dev);
  448. /* clear the MAC address */
  449. memset(pdata->enetaddr, 0, 6);
  450. return 0;
  451. }
  452. UCLASS_DRIVER(eth) = {
  453. .name = "eth",
  454. .id = UCLASS_ETH,
  455. .post_bind = eth_post_bind,
  456. .pre_unbind = eth_pre_unbind,
  457. .post_probe = eth_post_probe,
  458. .pre_remove = eth_pre_remove,
  459. .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
  460. .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
  461. .flags = DM_UC_FLAG_SEQ_ALIAS,
  462. };