/sys/dev/mii/mii.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 646 lines · 453 code · 86 blank · 107 comment · 103 complexity · 2c55695d890ad316227d884761c1251d MD5 · raw file

  1. /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */
  2. /*-
  3. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  8. * NASA Ames Research Center.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/cdefs.h>
  32. __FBSDID("$FreeBSD$");
  33. /*
  34. * MII bus layer, glues MII-capable network interface drivers to sharable
  35. * PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
  36. * plus some NetBSD extensions.
  37. */
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/socket.h>
  41. #include <sys/malloc.h>
  42. #include <sys/module.h>
  43. #include <sys/bus.h>
  44. #include <net/if.h>
  45. #include <net/if_media.h>
  46. #include <dev/mii/mii.h>
  47. #include <dev/mii/miivar.h>
  48. MODULE_VERSION(miibus, 1);
  49. #include "miibus_if.h"
  50. static device_attach_t miibus_attach;
  51. static bus_child_location_str_t miibus_child_location_str;
  52. static bus_child_pnpinfo_str_t miibus_child_pnpinfo_str;
  53. static device_detach_t miibus_detach;
  54. static bus_hinted_child_t miibus_hinted_child;
  55. static bus_print_child_t miibus_print_child;
  56. static device_probe_t miibus_probe;
  57. static bus_read_ivar_t miibus_read_ivar;
  58. static miibus_readreg_t miibus_readreg;
  59. static miibus_statchg_t miibus_statchg;
  60. static miibus_writereg_t miibus_writereg;
  61. static miibus_linkchg_t miibus_linkchg;
  62. static miibus_mediainit_t miibus_mediainit;
  63. static unsigned char mii_bitreverse(unsigned char x);
  64. static device_method_t miibus_methods[] = {
  65. /* device interface */
  66. DEVMETHOD(device_probe, miibus_probe),
  67. DEVMETHOD(device_attach, miibus_attach),
  68. DEVMETHOD(device_detach, miibus_detach),
  69. DEVMETHOD(device_shutdown, bus_generic_shutdown),
  70. /* bus interface */
  71. DEVMETHOD(bus_print_child, miibus_print_child),
  72. DEVMETHOD(bus_read_ivar, miibus_read_ivar),
  73. DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
  74. DEVMETHOD(bus_child_location_str, miibus_child_location_str),
  75. DEVMETHOD(bus_hinted_child, miibus_hinted_child),
  76. /* MII interface */
  77. DEVMETHOD(miibus_readreg, miibus_readreg),
  78. DEVMETHOD(miibus_writereg, miibus_writereg),
  79. DEVMETHOD(miibus_statchg, miibus_statchg),
  80. DEVMETHOD(miibus_linkchg, miibus_linkchg),
  81. DEVMETHOD(miibus_mediainit, miibus_mediainit),
  82. DEVMETHOD_END
  83. };
  84. devclass_t miibus_devclass;
  85. driver_t miibus_driver = {
  86. "miibus",
  87. miibus_methods,
  88. sizeof(struct mii_data)
  89. };
  90. struct miibus_ivars {
  91. struct ifnet *ifp;
  92. ifm_change_cb_t ifmedia_upd;
  93. ifm_stat_cb_t ifmedia_sts;
  94. u_int mii_flags;
  95. u_int mii_offset;
  96. };
  97. static int
  98. miibus_probe(device_t dev)
  99. {
  100. device_set_desc(dev, "MII bus");
  101. return (BUS_PROBE_SPECIFIC);
  102. }
  103. static int
  104. miibus_attach(device_t dev)
  105. {
  106. struct miibus_ivars *ivars;
  107. struct mii_attach_args *ma;
  108. struct mii_data *mii;
  109. device_t *children;
  110. int i, nchildren;
  111. mii = device_get_softc(dev);
  112. if (device_get_children(dev, &children, &nchildren) == 0) {
  113. for (i = 0; i < nchildren; i++) {
  114. ma = device_get_ivars(children[i]);
  115. ma->mii_data = mii;
  116. }
  117. free(children, M_TEMP);
  118. }
  119. if (nchildren == 0) {
  120. device_printf(dev, "cannot get children\n");
  121. return (ENXIO);
  122. }
  123. ivars = device_get_ivars(dev);
  124. ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
  125. ivars->ifmedia_sts);
  126. mii->mii_ifp = ivars->ifp;
  127. mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
  128. mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
  129. LIST_INIT(&mii->mii_phys);
  130. return (bus_generic_attach(dev));
  131. }
  132. static int
  133. miibus_detach(device_t dev)
  134. {
  135. struct mii_data *mii;
  136. bus_generic_detach(dev);
  137. mii = device_get_softc(dev);
  138. ifmedia_removeall(&mii->mii_media);
  139. mii->mii_ifp = NULL;
  140. return (0);
  141. }
  142. static int
  143. miibus_print_child(device_t dev, device_t child)
  144. {
  145. struct mii_attach_args *ma;
  146. int retval;
  147. ma = device_get_ivars(child);
  148. retval = bus_print_child_header(dev, child);
  149. retval += printf(" PHY %d", ma->mii_phyno);
  150. retval += bus_print_child_footer(dev, child);
  151. return (retval);
  152. }
  153. static int
  154. miibus_read_ivar(device_t dev, device_t child __unused, int which,
  155. uintptr_t *result)
  156. {
  157. struct miibus_ivars *ivars;
  158. /*
  159. * NB: this uses the instance variables of the miibus rather than
  160. * its PHY children.
  161. */
  162. ivars = device_get_ivars(dev);
  163. switch (which) {
  164. case MIIBUS_IVAR_FLAGS:
  165. *result = ivars->mii_flags;
  166. break;
  167. default:
  168. return (ENOENT);
  169. }
  170. return (0);
  171. }
  172. static int
  173. miibus_child_pnpinfo_str(device_t dev __unused, device_t child, char *buf,
  174. size_t buflen)
  175. {
  176. struct mii_attach_args *ma;
  177. ma = device_get_ivars(child);
  178. snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
  179. MII_OUI(ma->mii_id1, ma->mii_id2),
  180. MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
  181. return (0);
  182. }
  183. static int
  184. miibus_child_location_str(device_t dev __unused, device_t child, char *buf,
  185. size_t buflen)
  186. {
  187. struct mii_attach_args *ma;
  188. ma = device_get_ivars(child);
  189. snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
  190. return (0);
  191. }
  192. static void
  193. miibus_hinted_child(device_t dev, const char *name, int unit)
  194. {
  195. struct miibus_ivars *ivars;
  196. struct mii_attach_args *args, *ma;
  197. device_t *children, phy;
  198. int i, nchildren;
  199. u_int val;
  200. if (resource_int_value(name, unit, "phyno", &val) != 0)
  201. return;
  202. if (device_get_children(dev, &children, &nchildren) != 0)
  203. return;
  204. ma = NULL;
  205. for (i = 0; i < nchildren; i++) {
  206. args = device_get_ivars(children[i]);
  207. if (args->mii_phyno == val) {
  208. ma = args;
  209. break;
  210. }
  211. }
  212. free(children, M_TEMP);
  213. /*
  214. * Don't add a PHY that was automatically identified by having media
  215. * in its BMSR twice, only allow to alter its attach arguments.
  216. */
  217. if (ma == NULL) {
  218. ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
  219. M_NOWAIT);
  220. if (ma == NULL)
  221. return;
  222. phy = device_add_child(dev, name, unit);
  223. if (phy == NULL) {
  224. free(ma, M_DEVBUF);
  225. return;
  226. }
  227. ivars = device_get_ivars(dev);
  228. ma->mii_phyno = val;
  229. ma->mii_offset = ivars->mii_offset++;
  230. ma->mii_id1 = 0;
  231. ma->mii_id2 = 0;
  232. ma->mii_capmask = BMSR_DEFCAPMASK;
  233. device_set_ivars(phy, ma);
  234. }
  235. if (resource_int_value(name, unit, "id1", &val) == 0)
  236. ma->mii_id1 = val;
  237. if (resource_int_value(name, unit, "id2", &val) == 0)
  238. ma->mii_id2 = val;
  239. if (resource_int_value(name, unit, "capmask", &val) == 0)
  240. ma->mii_capmask = val;
  241. }
  242. static int
  243. miibus_readreg(device_t dev, int phy, int reg)
  244. {
  245. device_t parent;
  246. parent = device_get_parent(dev);
  247. return (MIIBUS_READREG(parent, phy, reg));
  248. }
  249. static int
  250. miibus_writereg(device_t dev, int phy, int reg, int data)
  251. {
  252. device_t parent;
  253. parent = device_get_parent(dev);
  254. return (MIIBUS_WRITEREG(parent, phy, reg, data));
  255. }
  256. static void
  257. miibus_statchg(device_t dev)
  258. {
  259. device_t parent;
  260. struct mii_data *mii;
  261. parent = device_get_parent(dev);
  262. MIIBUS_STATCHG(parent);
  263. mii = device_get_softc(dev);
  264. mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
  265. }
  266. static void
  267. miibus_linkchg(device_t dev)
  268. {
  269. struct mii_data *mii;
  270. device_t parent;
  271. int link_state;
  272. parent = device_get_parent(dev);
  273. MIIBUS_LINKCHG(parent);
  274. mii = device_get_softc(dev);
  275. if (mii->mii_media_status & IFM_AVALID) {
  276. if (mii->mii_media_status & IFM_ACTIVE)
  277. link_state = LINK_STATE_UP;
  278. else
  279. link_state = LINK_STATE_DOWN;
  280. } else
  281. link_state = LINK_STATE_UNKNOWN;
  282. if_link_state_change(mii->mii_ifp, link_state);
  283. }
  284. static void
  285. miibus_mediainit(device_t dev)
  286. {
  287. struct mii_data *mii;
  288. struct ifmedia_entry *m;
  289. int media = 0;
  290. /* Poke the parent in case it has any media of its own to add. */
  291. MIIBUS_MEDIAINIT(device_get_parent(dev));
  292. mii = device_get_softc(dev);
  293. LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
  294. media = m->ifm_media;
  295. if (media == (IFM_ETHER | IFM_AUTO))
  296. break;
  297. }
  298. ifmedia_set(&mii->mii_media, media);
  299. }
  300. /*
  301. * Helper function used by network interface drivers, attaches the miibus and
  302. * the PHYs to the network interface driver parent.
  303. */
  304. int
  305. mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
  306. ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
  307. int phyloc, int offloc, int flags)
  308. {
  309. struct miibus_ivars *ivars;
  310. struct mii_attach_args *args, ma;
  311. device_t *children, phy;
  312. int bmsr, first, i, nchildren, phymax, phymin, rv;
  313. uint32_t phymask;
  314. if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
  315. printf("%s: phyloc and offloc specified\n", __func__);
  316. return (EINVAL);
  317. }
  318. if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
  319. printf("%s: ivalid offloc %d\n", __func__, offloc);
  320. return (EINVAL);
  321. }
  322. if (phyloc == MII_PHY_ANY) {
  323. phymin = 0;
  324. phymax = MII_NPHY - 1;
  325. } else {
  326. if (phyloc < 0 || phyloc >= MII_NPHY) {
  327. printf("%s: ivalid phyloc %d\n", __func__, phyloc);
  328. return (EINVAL);
  329. }
  330. phymin = phymax = phyloc;
  331. }
  332. first = 0;
  333. if (*miibus == NULL) {
  334. first = 1;
  335. ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
  336. if (ivars == NULL)
  337. return (ENOMEM);
  338. ivars->ifp = ifp;
  339. ivars->ifmedia_upd = ifmedia_upd;
  340. ivars->ifmedia_sts = ifmedia_sts;
  341. ivars->mii_flags = flags;
  342. *miibus = device_add_child(dev, "miibus", -1);
  343. if (*miibus == NULL) {
  344. rv = ENXIO;
  345. goto fail;
  346. }
  347. device_set_ivars(*miibus, ivars);
  348. } else {
  349. ivars = device_get_ivars(*miibus);
  350. if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
  351. ivars->ifmedia_sts != ifmedia_sts ||
  352. ivars->mii_flags != flags) {
  353. printf("%s: non-matching invariant\n", __func__);
  354. return (EINVAL);
  355. }
  356. /*
  357. * Assignment of the attach arguments mii_data for the first
  358. * pass is done in miibus_attach(), i.e. once the miibus softc
  359. * has been allocated.
  360. */
  361. ma.mii_data = device_get_softc(*miibus);
  362. }
  363. ma.mii_capmask = capmask;
  364. if (resource_int_value(device_get_name(*miibus),
  365. device_get_unit(*miibus), "phymask", &phymask) != 0)
  366. phymask = 0xffffffff;
  367. if (device_get_children(*miibus, &children, &nchildren) != 0) {
  368. children = NULL;
  369. nchildren = 0;
  370. }
  371. ivars->mii_offset = 0;
  372. for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
  373. /*
  374. * Make sure we haven't already configured a PHY at this
  375. * address. This allows mii_attach() to be called
  376. * multiple times.
  377. */
  378. for (i = 0; i < nchildren; i++) {
  379. args = device_get_ivars(children[i]);
  380. if (args->mii_phyno == ma.mii_phyno) {
  381. /*
  382. * Yes, there is already something
  383. * configured at this address.
  384. */
  385. goto skip;
  386. }
  387. }
  388. /*
  389. * Check to see if there is a PHY at this address. Note,
  390. * many braindead PHYs report 0/0 in their ID registers,
  391. * so we test for media in the BMSR.
  392. */
  393. bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
  394. if (bmsr == 0 || bmsr == 0xffff ||
  395. (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
  396. /* Assume no PHY at this address. */
  397. continue;
  398. }
  399. /*
  400. * There is a PHY at this address. If we were given an
  401. * `offset' locator, skip this PHY if it doesn't match.
  402. */
  403. if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset)
  404. goto skip;
  405. /*
  406. * Skip this PHY if it's not included in the phymask hint.
  407. */
  408. if ((phymask & (1 << ma.mii_phyno)) == 0)
  409. goto skip;
  410. /*
  411. * Extract the IDs. Braindead PHYs will be handled by
  412. * the `ukphy' driver, as we have no ID information to
  413. * match on.
  414. */
  415. ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
  416. ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
  417. ma.mii_offset = ivars->mii_offset;
  418. args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
  419. M_NOWAIT);
  420. if (args == NULL)
  421. goto skip;
  422. bcopy((char *)&ma, (char *)args, sizeof(ma));
  423. phy = device_add_child(*miibus, NULL, -1);
  424. if (phy == NULL) {
  425. free(args, M_DEVBUF);
  426. goto skip;
  427. }
  428. device_set_ivars(phy, args);
  429. skip:
  430. ivars->mii_offset++;
  431. }
  432. free(children, M_TEMP);
  433. if (first != 0) {
  434. rv = device_set_driver(*miibus, &miibus_driver);
  435. if (rv != 0)
  436. goto fail;
  437. bus_enumerate_hinted_children(*miibus);
  438. rv = device_get_children(*miibus, &children, &nchildren);
  439. if (rv != 0)
  440. goto fail;
  441. free(children, M_TEMP);
  442. if (nchildren == 0) {
  443. rv = ENXIO;
  444. goto fail;
  445. }
  446. rv = bus_generic_attach(dev);
  447. if (rv != 0)
  448. goto fail;
  449. /* Attaching of the PHY drivers is done in miibus_attach(). */
  450. return (0);
  451. }
  452. rv = bus_generic_attach(*miibus);
  453. if (rv != 0)
  454. goto fail;
  455. return (0);
  456. fail:
  457. if (*miibus != NULL)
  458. device_delete_child(dev, *miibus);
  459. free(ivars, M_DEVBUF);
  460. if (first != 0)
  461. *miibus = NULL;
  462. return (rv);
  463. }
  464. /*
  465. * Media changed; notify all PHYs.
  466. */
  467. int
  468. mii_mediachg(struct mii_data *mii)
  469. {
  470. struct mii_softc *child;
  471. struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
  472. int rv;
  473. mii->mii_media_status = 0;
  474. mii->mii_media_active = IFM_NONE;
  475. LIST_FOREACH(child, &mii->mii_phys, mii_list) {
  476. /*
  477. * If the media indicates a different PHY instance,
  478. * isolate this one.
  479. */
  480. if (IFM_INST(ife->ifm_media) != child->mii_inst) {
  481. if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
  482. device_printf(child->mii_dev, "%s: "
  483. "can't handle non-zero PHY instance %d\n",
  484. __func__, child->mii_inst);
  485. continue;
  486. }
  487. PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
  488. BMCR_ISO);
  489. continue;
  490. }
  491. rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
  492. if (rv)
  493. return (rv);
  494. }
  495. return (0);
  496. }
  497. /*
  498. * Call the PHY tick routines, used during autonegotiation.
  499. */
  500. void
  501. mii_tick(struct mii_data *mii)
  502. {
  503. struct mii_softc *child;
  504. struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
  505. LIST_FOREACH(child, &mii->mii_phys, mii_list) {
  506. /*
  507. * If this PHY instance isn't currently selected, just skip
  508. * it.
  509. */
  510. if (IFM_INST(ife->ifm_media) != child->mii_inst)
  511. continue;
  512. (void)PHY_SERVICE(child, mii, MII_TICK);
  513. }
  514. }
  515. /*
  516. * Get media status from PHYs.
  517. */
  518. void
  519. mii_pollstat(struct mii_data *mii)
  520. {
  521. struct mii_softc *child;
  522. struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
  523. mii->mii_media_status = 0;
  524. mii->mii_media_active = IFM_NONE;
  525. LIST_FOREACH(child, &mii->mii_phys, mii_list) {
  526. /*
  527. * If we're not polling this PHY instance, just skip it.
  528. */
  529. if (IFM_INST(ife->ifm_media) != child->mii_inst)
  530. continue;
  531. (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
  532. }
  533. }
  534. /*
  535. * Inform the PHYs that the interface is down.
  536. */
  537. void
  538. mii_down(struct mii_data *mii)
  539. {
  540. struct mii_softc *child;
  541. LIST_FOREACH(child, &mii->mii_phys, mii_list)
  542. mii_phy_down(child);
  543. }
  544. static unsigned char
  545. mii_bitreverse(unsigned char x)
  546. {
  547. static unsigned const char const nibbletab[16] = {
  548. 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
  549. };
  550. return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
  551. }
  552. u_int
  553. mii_oui(u_int id1, u_int id2)
  554. {
  555. u_int h;
  556. h = (id1 << 6) | (id2 >> 10);
  557. return ((mii_bitreverse(h >> 16) << 16) |
  558. (mii_bitreverse((h >> 8) & 0xff) << 8) |
  559. mii_bitreverse(h & 0xff));
  560. }