eth-uclass.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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;
  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. eth_parse_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 = getenv("ethact");
  207. char *ethrotate = getenv("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 || !device_active(current))
  264. return;
  265. eth_get_ops(current)->stop(current);
  266. priv = current->uclass_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 (!device_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 (!device_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(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 = getenv("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(&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 int eth_post_probe(struct udevice *dev)
  383. {
  384. struct eth_device_priv *priv = dev->uclass_priv;
  385. struct eth_pdata *pdata = dev->platdata;
  386. unsigned char env_enetaddr[ARP_HLEN];
  387. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  388. struct eth_ops *ops = eth_get_ops(dev);
  389. static int reloc_done;
  390. if (!reloc_done) {
  391. if (ops->start)
  392. ops->start += gd->reloc_off;
  393. if (ops->send)
  394. ops->send += gd->reloc_off;
  395. if (ops->recv)
  396. ops->recv += gd->reloc_off;
  397. if (ops->free_pkt)
  398. ops->free_pkt += gd->reloc_off;
  399. if (ops->stop)
  400. ops->stop += gd->reloc_off;
  401. #ifdef CONFIG_MCAST_TFTP
  402. if (ops->mcast)
  403. ops->mcast += gd->reloc_off;
  404. #endif
  405. if (ops->write_hwaddr)
  406. ops->write_hwaddr += gd->reloc_off;
  407. if (ops->read_rom_hwaddr)
  408. ops->read_rom_hwaddr += gd->reloc_off;
  409. reloc_done++;
  410. }
  411. #endif
  412. priv->state = ETH_STATE_INIT;
  413. /* Check if the device has a MAC address in ROM */
  414. if (eth_get_ops(dev)->read_rom_hwaddr)
  415. eth_get_ops(dev)->read_rom_hwaddr(dev);
  416. eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
  417. if (!is_zero_ethaddr(env_enetaddr)) {
  418. if (!is_zero_ethaddr(pdata->enetaddr) &&
  419. memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
  420. printf("\nWarning: %s MAC addresses don't match:\n",
  421. dev->name);
  422. printf("Address in ROM is %pM\n",
  423. pdata->enetaddr);
  424. printf("Address in environment is %pM\n",
  425. env_enetaddr);
  426. }
  427. /* Override the ROM MAC address */
  428. memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
  429. } else if (is_valid_ethaddr(pdata->enetaddr)) {
  430. eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
  431. printf("\nWarning: %s using MAC address from ROM\n",
  432. dev->name);
  433. } else if (is_zero_ethaddr(pdata->enetaddr) ||
  434. !is_valid_ethaddr(pdata->enetaddr)) {
  435. #ifdef CONFIG_NET_RANDOM_ETHADDR
  436. net_random_ethaddr(pdata->enetaddr);
  437. printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
  438. dev->name, dev->seq, pdata->enetaddr);
  439. #else
  440. printf("\nError: %s address not set.\n",
  441. dev->name);
  442. return -EINVAL;
  443. #endif
  444. }
  445. return 0;
  446. }
  447. static int eth_pre_remove(struct udevice *dev)
  448. {
  449. struct eth_pdata *pdata = dev->platdata;
  450. eth_get_ops(dev)->stop(dev);
  451. /* clear the MAC address */
  452. memset(pdata->enetaddr, 0, ARP_HLEN);
  453. return 0;
  454. }
  455. UCLASS_DRIVER(eth) = {
  456. .name = "eth",
  457. .id = UCLASS_ETH,
  458. .post_bind = eth_post_bind,
  459. .pre_unbind = eth_pre_unbind,
  460. .post_probe = eth_post_probe,
  461. .pre_remove = eth_pre_remove,
  462. .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
  463. .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
  464. .flags = DM_UC_FLAG_SEQ_ALIAS,
  465. };