mdio_10g.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2006-2008 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. /*
  10. * Useful functions for working with MDIO clause 45 PHYs
  11. */
  12. #include <linux/types.h>
  13. #include <linux/ethtool.h>
  14. #include <linux/delay.h>
  15. #include "net_driver.h"
  16. #include "mdio_10g.h"
  17. #include "boards.h"
  18. int mdio_clause45_reset_mmd(struct efx_nic *port, int mmd,
  19. int spins, int spintime)
  20. {
  21. u32 ctrl;
  22. int phy_id = port->mii.phy_id;
  23. /* Catch callers passing values in the wrong units (or just silly) */
  24. EFX_BUG_ON_PARANOID(spins * spintime >= 5000);
  25. mdio_clause45_write(port, phy_id, mmd, MDIO_MMDREG_CTRL1,
  26. (1 << MDIO_MMDREG_CTRL1_RESET_LBN));
  27. /* Wait for the reset bit to clear. */
  28. do {
  29. msleep(spintime);
  30. ctrl = mdio_clause45_read(port, phy_id, mmd, MDIO_MMDREG_CTRL1);
  31. spins--;
  32. } while (spins && (ctrl & (1 << MDIO_MMDREG_CTRL1_RESET_LBN)));
  33. return spins ? spins : -ETIMEDOUT;
  34. }
  35. static int mdio_clause45_check_mmd(struct efx_nic *efx, int mmd,
  36. int fault_fatal)
  37. {
  38. int status;
  39. int phy_id = efx->mii.phy_id;
  40. if (LOOPBACK_INTERNAL(efx))
  41. return 0;
  42. /* Read MMD STATUS2 to check it is responding. */
  43. status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT2);
  44. if (((status >> MDIO_MMDREG_STAT2_PRESENT_LBN) &
  45. ((1 << MDIO_MMDREG_STAT2_PRESENT_WIDTH) - 1)) !=
  46. MDIO_MMDREG_STAT2_PRESENT_VAL) {
  47. EFX_ERR(efx, "PHY MMD %d not responding.\n", mmd);
  48. return -EIO;
  49. }
  50. /* Read MMD STATUS 1 to check for fault. */
  51. status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT1);
  52. if ((status & (1 << MDIO_MMDREG_STAT1_FAULT_LBN)) != 0) {
  53. if (fault_fatal) {
  54. EFX_ERR(efx, "PHY MMD %d reporting fatal"
  55. " fault: status %x\n", mmd, status);
  56. return -EIO;
  57. } else {
  58. EFX_LOG(efx, "PHY MMD %d reporting status"
  59. " %x (expected)\n", mmd, status);
  60. }
  61. }
  62. return 0;
  63. }
  64. /* This ought to be ridiculous overkill. We expect it to fail rarely */
  65. #define MDIO45_RESET_TIME 1000 /* ms */
  66. #define MDIO45_RESET_ITERS 100
  67. int mdio_clause45_wait_reset_mmds(struct efx_nic *efx,
  68. unsigned int mmd_mask)
  69. {
  70. const int spintime = MDIO45_RESET_TIME / MDIO45_RESET_ITERS;
  71. int tries = MDIO45_RESET_ITERS;
  72. int rc = 0;
  73. int in_reset;
  74. while (tries) {
  75. int mask = mmd_mask;
  76. int mmd = 0;
  77. int stat;
  78. in_reset = 0;
  79. while (mask) {
  80. if (mask & 1) {
  81. stat = mdio_clause45_read(efx,
  82. efx->mii.phy_id,
  83. mmd,
  84. MDIO_MMDREG_CTRL1);
  85. if (stat < 0) {
  86. EFX_ERR(efx, "failed to read status of"
  87. " MMD %d\n", mmd);
  88. return -EIO;
  89. }
  90. if (stat & (1 << MDIO_MMDREG_CTRL1_RESET_LBN))
  91. in_reset |= (1 << mmd);
  92. }
  93. mask = mask >> 1;
  94. mmd++;
  95. }
  96. if (!in_reset)
  97. break;
  98. tries--;
  99. msleep(spintime);
  100. }
  101. if (in_reset != 0) {
  102. EFX_ERR(efx, "not all MMDs came out of reset in time."
  103. " MMDs still in reset: %x\n", in_reset);
  104. rc = -ETIMEDOUT;
  105. }
  106. return rc;
  107. }
  108. int mdio_clause45_check_mmds(struct efx_nic *efx,
  109. unsigned int mmd_mask, unsigned int fatal_mask)
  110. {
  111. int devices, mmd = 0;
  112. int probe_mmd;
  113. /* Historically we have probed the PHYXS to find out what devices are
  114. * present,but that doesn't work so well if the PHYXS isn't expected
  115. * to exist, if so just find the first item in the list supplied. */
  116. probe_mmd = (mmd_mask & MDIO_MMDREG_DEVS0_PHYXS) ? MDIO_MMD_PHYXS :
  117. __ffs(mmd_mask);
  118. devices = mdio_clause45_read(efx, efx->mii.phy_id,
  119. probe_mmd, MDIO_MMDREG_DEVS0);
  120. /* Check all the expected MMDs are present */
  121. if (devices < 0) {
  122. EFX_ERR(efx, "failed to read devices present\n");
  123. return -EIO;
  124. }
  125. if ((devices & mmd_mask) != mmd_mask) {
  126. EFX_ERR(efx, "required MMDs not present: got %x, "
  127. "wanted %x\n", devices, mmd_mask);
  128. return -ENODEV;
  129. }
  130. EFX_TRACE(efx, "Devices present: %x\n", devices);
  131. /* Check all required MMDs are responding and happy. */
  132. while (mmd_mask) {
  133. if (mmd_mask & 1) {
  134. int fault_fatal = fatal_mask & 1;
  135. if (mdio_clause45_check_mmd(efx, mmd, fault_fatal))
  136. return -EIO;
  137. }
  138. mmd_mask = mmd_mask >> 1;
  139. fatal_mask = fatal_mask >> 1;
  140. mmd++;
  141. }
  142. return 0;
  143. }
  144. bool mdio_clause45_links_ok(struct efx_nic *efx, unsigned int mmd_mask)
  145. {
  146. int phy_id = efx->mii.phy_id;
  147. int status;
  148. bool ok = true;
  149. int mmd = 0;
  150. /* If the port is in loopback, then we should only consider a subset
  151. * of mmd's */
  152. if (LOOPBACK_INTERNAL(efx))
  153. return true;
  154. else if (efx->loopback_mode == LOOPBACK_NETWORK)
  155. return false;
  156. else if (efx_phy_mode_disabled(efx->phy_mode))
  157. return false;
  158. else if (efx->loopback_mode == LOOPBACK_PHYXS)
  159. mmd_mask &= ~(MDIO_MMDREG_DEVS0_PHYXS |
  160. MDIO_MMDREG_DEVS0_PCS |
  161. MDIO_MMDREG_DEVS0_PMAPMD);
  162. else if (efx->loopback_mode == LOOPBACK_PCS)
  163. mmd_mask &= ~(MDIO_MMDREG_DEVS0_PCS |
  164. MDIO_MMDREG_DEVS0_PMAPMD);
  165. else if (efx->loopback_mode == LOOPBACK_PMAPMD)
  166. mmd_mask &= ~MDIO_MMDREG_DEVS0_PMAPMD;
  167. while (mmd_mask) {
  168. if (mmd_mask & 1) {
  169. /* Double reads because link state is latched, and a
  170. * read moves the current state into the register */
  171. status = mdio_clause45_read(efx, phy_id,
  172. mmd, MDIO_MMDREG_STAT1);
  173. status = mdio_clause45_read(efx, phy_id,
  174. mmd, MDIO_MMDREG_STAT1);
  175. ok = ok && (status & (1 << MDIO_MMDREG_STAT1_LINK_LBN));
  176. }
  177. mmd_mask = (mmd_mask >> 1);
  178. mmd++;
  179. }
  180. return ok;
  181. }
  182. void mdio_clause45_transmit_disable(struct efx_nic *efx)
  183. {
  184. int phy_id = efx->mii.phy_id;
  185. int ctrl1, ctrl2;
  186. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
  187. MDIO_MMDREG_TXDIS);
  188. if (efx->phy_mode & PHY_MODE_TX_DISABLED)
  189. ctrl2 |= (1 << MDIO_MMDREG_TXDIS_GLOBAL_LBN);
  190. else
  191. ctrl1 &= ~(1 << MDIO_MMDREG_TXDIS_GLOBAL_LBN);
  192. if (ctrl1 != ctrl2)
  193. mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
  194. MDIO_MMDREG_TXDIS, ctrl2);
  195. }
  196. void mdio_clause45_phy_reconfigure(struct efx_nic *efx)
  197. {
  198. int phy_id = efx->mii.phy_id;
  199. int ctrl1, ctrl2;
  200. /* Handle (with debouncing) PMA/PMD loopback */
  201. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
  202. MDIO_MMDREG_CTRL1);
  203. if (efx->loopback_mode == LOOPBACK_PMAPMD)
  204. ctrl2 |= (1 << MDIO_PMAPMD_CTRL1_LBACK_LBN);
  205. else
  206. ctrl2 &= ~(1 << MDIO_PMAPMD_CTRL1_LBACK_LBN);
  207. if (ctrl1 != ctrl2)
  208. mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
  209. MDIO_MMDREG_CTRL1, ctrl2);
  210. /* Handle (with debouncing) PCS loopback */
  211. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PCS,
  212. MDIO_MMDREG_CTRL1);
  213. if (efx->loopback_mode == LOOPBACK_PCS)
  214. ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  215. else
  216. ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  217. if (ctrl1 != ctrl2)
  218. mdio_clause45_write(efx, phy_id, MDIO_MMD_PCS,
  219. MDIO_MMDREG_CTRL1, ctrl2);
  220. /* Handle (with debouncing) PHYXS network loopback */
  221. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
  222. MDIO_MMDREG_CTRL1);
  223. if (efx->loopback_mode == LOOPBACK_NETWORK)
  224. ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  225. else
  226. ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  227. if (ctrl1 != ctrl2)
  228. mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
  229. MDIO_MMDREG_CTRL1, ctrl2);
  230. }
  231. static void mdio_clause45_set_mmd_lpower(struct efx_nic *efx,
  232. int lpower, int mmd)
  233. {
  234. int phy = efx->mii.phy_id;
  235. int stat = mdio_clause45_read(efx, phy, mmd, MDIO_MMDREG_STAT1);
  236. int ctrl1, ctrl2;
  237. EFX_TRACE(efx, "Setting low power mode for MMD %d to %d\n",
  238. mmd, lpower);
  239. if (stat & (1 << MDIO_MMDREG_STAT1_LPABLE_LBN)) {
  240. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy,
  241. mmd, MDIO_MMDREG_CTRL1);
  242. if (lpower)
  243. ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LPOWER_LBN);
  244. else
  245. ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LPOWER_LBN);
  246. if (ctrl1 != ctrl2)
  247. mdio_clause45_write(efx, phy, mmd,
  248. MDIO_MMDREG_CTRL1, ctrl2);
  249. }
  250. }
  251. void mdio_clause45_set_mmds_lpower(struct efx_nic *efx,
  252. int low_power, unsigned int mmd_mask)
  253. {
  254. int mmd = 0;
  255. while (mmd_mask) {
  256. if (mmd_mask & 1)
  257. mdio_clause45_set_mmd_lpower(efx, low_power, mmd);
  258. mmd_mask = (mmd_mask >> 1);
  259. mmd++;
  260. }
  261. }
  262. /**
  263. * mdio_clause45_get_settings - Read (some of) the PHY settings over MDIO.
  264. * @efx: Efx NIC
  265. * @ecmd: Buffer for settings
  266. *
  267. * On return the 'port', 'speed', 'supported' and 'advertising' fields of
  268. * ecmd have been filled out based on the PMA type.
  269. */
  270. void mdio_clause45_get_settings(struct efx_nic *efx,
  271. struct ethtool_cmd *ecmd)
  272. {
  273. int pma_type;
  274. /* If no PMA is present we are presumably talking something XAUI-ish
  275. * like CX4. Which we report as FIBRE (see below) */
  276. if ((efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_PMAPMD)) == 0) {
  277. ecmd->speed = SPEED_10000;
  278. ecmd->port = PORT_FIBRE;
  279. ecmd->supported = SUPPORTED_FIBRE;
  280. ecmd->advertising = ADVERTISED_FIBRE;
  281. return;
  282. }
  283. pma_type = mdio_clause45_read(efx, efx->mii.phy_id,
  284. MDIO_MMD_PMAPMD, MDIO_MMDREG_CTRL2);
  285. pma_type &= MDIO_PMAPMD_CTRL2_TYPE_MASK;
  286. switch (pma_type) {
  287. /* We represent CX4 as fibre in the absence of anything
  288. better. */
  289. case MDIO_PMAPMD_CTRL2_10G_CX4:
  290. ecmd->speed = SPEED_10000;
  291. ecmd->port = PORT_FIBRE;
  292. ecmd->supported = SUPPORTED_FIBRE;
  293. ecmd->advertising = ADVERTISED_FIBRE;
  294. break;
  295. /* 10G Base-T */
  296. case MDIO_PMAPMD_CTRL2_10G_BT:
  297. ecmd->speed = SPEED_10000;
  298. ecmd->port = PORT_TP;
  299. ecmd->supported = SUPPORTED_TP | SUPPORTED_10000baseT_Full;
  300. ecmd->advertising = (ADVERTISED_FIBRE
  301. | ADVERTISED_10000baseT_Full);
  302. break;
  303. case MDIO_PMAPMD_CTRL2_1G_BT:
  304. ecmd->speed = SPEED_1000;
  305. ecmd->port = PORT_TP;
  306. ecmd->supported = SUPPORTED_TP | SUPPORTED_1000baseT_Full;
  307. ecmd->advertising = (ADVERTISED_FIBRE
  308. | ADVERTISED_1000baseT_Full);
  309. break;
  310. case MDIO_PMAPMD_CTRL2_100_BT:
  311. ecmd->speed = SPEED_100;
  312. ecmd->port = PORT_TP;
  313. ecmd->supported = SUPPORTED_TP | SUPPORTED_100baseT_Full;
  314. ecmd->advertising = (ADVERTISED_FIBRE
  315. | ADVERTISED_100baseT_Full);
  316. break;
  317. case MDIO_PMAPMD_CTRL2_10_BT:
  318. ecmd->speed = SPEED_10;
  319. ecmd->port = PORT_TP;
  320. ecmd->supported = SUPPORTED_TP | SUPPORTED_10baseT_Full;
  321. ecmd->advertising = ADVERTISED_FIBRE | ADVERTISED_10baseT_Full;
  322. break;
  323. /* All the other defined modes are flavours of
  324. * 10G optical */
  325. default:
  326. ecmd->speed = SPEED_10000;
  327. ecmd->port = PORT_FIBRE;
  328. ecmd->supported = SUPPORTED_FIBRE;
  329. ecmd->advertising = ADVERTISED_FIBRE;
  330. break;
  331. }
  332. }
  333. /**
  334. * mdio_clause45_set_settings - Set (some of) the PHY settings over MDIO.
  335. * @efx: Efx NIC
  336. * @ecmd: New settings
  337. *
  338. * Currently this just enforces that we are _not_ changing the
  339. * 'port', 'speed', 'supported' or 'advertising' settings as these
  340. * cannot be changed on any currently supported PHY.
  341. */
  342. int mdio_clause45_set_settings(struct efx_nic *efx,
  343. struct ethtool_cmd *ecmd)
  344. {
  345. struct ethtool_cmd tmpcmd;
  346. mdio_clause45_get_settings(efx, &tmpcmd);
  347. /* None of the current PHYs support more than one mode
  348. * of operation (and only 10GBT ever will), so keep things
  349. * simple for now */
  350. if ((ecmd->speed == tmpcmd.speed) && (ecmd->port == tmpcmd.port) &&
  351. (ecmd->supported == tmpcmd.supported) &&
  352. (ecmd->advertising == tmpcmd.advertising))
  353. return 0;
  354. return -EOPNOTSUPP;
  355. }