mdio_10g.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. u32 devices;
  112. int mmd = 0, 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_DEVS_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. mdio_clause45_read(efx, efx->mii.phy_id,
  121. probe_mmd, MDIO_MMDREG_DEVS1) << 16);
  122. /* Check all the expected MMDs are present */
  123. if (devices < 0) {
  124. EFX_ERR(efx, "failed to read devices present\n");
  125. return -EIO;
  126. }
  127. if ((devices & mmd_mask) != mmd_mask) {
  128. EFX_ERR(efx, "required MMDs not present: got %x, "
  129. "wanted %x\n", devices, mmd_mask);
  130. return -ENODEV;
  131. }
  132. EFX_TRACE(efx, "Devices present: %x\n", devices);
  133. /* Check all required MMDs are responding and happy. */
  134. while (mmd_mask) {
  135. if (mmd_mask & 1) {
  136. int fault_fatal = fatal_mask & 1;
  137. if (mdio_clause45_check_mmd(efx, mmd, fault_fatal))
  138. return -EIO;
  139. }
  140. mmd_mask = mmd_mask >> 1;
  141. fatal_mask = fatal_mask >> 1;
  142. mmd++;
  143. }
  144. return 0;
  145. }
  146. bool mdio_clause45_links_ok(struct efx_nic *efx, unsigned int mmd_mask)
  147. {
  148. int phy_id = efx->mii.phy_id;
  149. int status;
  150. bool ok = true;
  151. int mmd = 0;
  152. /* If the port is in loopback, then we should only consider a subset
  153. * of mmd's */
  154. if (LOOPBACK_INTERNAL(efx))
  155. return true;
  156. else if (efx->loopback_mode == LOOPBACK_NETWORK)
  157. return false;
  158. else if (efx_phy_mode_disabled(efx->phy_mode))
  159. return false;
  160. else if (efx->loopback_mode == LOOPBACK_PHYXS)
  161. mmd_mask &= ~(MDIO_MMDREG_DEVS_PHYXS |
  162. MDIO_MMDREG_DEVS_PCS |
  163. MDIO_MMDREG_DEVS_PMAPMD);
  164. else if (efx->loopback_mode == LOOPBACK_PCS)
  165. mmd_mask &= ~(MDIO_MMDREG_DEVS_PCS |
  166. MDIO_MMDREG_DEVS_PMAPMD);
  167. else if (efx->loopback_mode == LOOPBACK_PMAPMD)
  168. mmd_mask &= ~MDIO_MMDREG_DEVS_PMAPMD;
  169. while (mmd_mask) {
  170. if (mmd_mask & 1) {
  171. /* Double reads because link state is latched, and a
  172. * read moves the current state into the register */
  173. status = mdio_clause45_read(efx, phy_id,
  174. mmd, MDIO_MMDREG_STAT1);
  175. status = mdio_clause45_read(efx, phy_id,
  176. mmd, MDIO_MMDREG_STAT1);
  177. ok = ok && (status & (1 << MDIO_MMDREG_STAT1_LINK_LBN));
  178. }
  179. mmd_mask = (mmd_mask >> 1);
  180. mmd++;
  181. }
  182. return ok;
  183. }
  184. void mdio_clause45_transmit_disable(struct efx_nic *efx)
  185. {
  186. int phy_id = efx->mii.phy_id;
  187. int ctrl1, ctrl2;
  188. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
  189. MDIO_MMDREG_TXDIS);
  190. if (efx->phy_mode & PHY_MODE_TX_DISABLED)
  191. ctrl2 |= (1 << MDIO_MMDREG_TXDIS_GLOBAL_LBN);
  192. else
  193. ctrl1 &= ~(1 << MDIO_MMDREG_TXDIS_GLOBAL_LBN);
  194. if (ctrl1 != ctrl2)
  195. mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
  196. MDIO_MMDREG_TXDIS, ctrl2);
  197. }
  198. void mdio_clause45_phy_reconfigure(struct efx_nic *efx)
  199. {
  200. int phy_id = efx->mii.phy_id;
  201. int ctrl1, ctrl2;
  202. /* Handle (with debouncing) PMA/PMD loopback */
  203. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
  204. MDIO_MMDREG_CTRL1);
  205. if (efx->loopback_mode == LOOPBACK_PMAPMD)
  206. ctrl2 |= (1 << MDIO_PMAPMD_CTRL1_LBACK_LBN);
  207. else
  208. ctrl2 &= ~(1 << MDIO_PMAPMD_CTRL1_LBACK_LBN);
  209. if (ctrl1 != ctrl2)
  210. mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
  211. MDIO_MMDREG_CTRL1, ctrl2);
  212. /* Handle (with debouncing) PCS loopback */
  213. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PCS,
  214. MDIO_MMDREG_CTRL1);
  215. if (efx->loopback_mode == LOOPBACK_PCS)
  216. ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  217. else
  218. ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  219. if (ctrl1 != ctrl2)
  220. mdio_clause45_write(efx, phy_id, MDIO_MMD_PCS,
  221. MDIO_MMDREG_CTRL1, ctrl2);
  222. /* Handle (with debouncing) PHYXS network loopback */
  223. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
  224. MDIO_MMDREG_CTRL1);
  225. if (efx->loopback_mode == LOOPBACK_NETWORK)
  226. ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  227. else
  228. ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
  229. if (ctrl1 != ctrl2)
  230. mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
  231. MDIO_MMDREG_CTRL1, ctrl2);
  232. }
  233. static void mdio_clause45_set_mmd_lpower(struct efx_nic *efx,
  234. int lpower, int mmd)
  235. {
  236. int phy = efx->mii.phy_id;
  237. int stat = mdio_clause45_read(efx, phy, mmd, MDIO_MMDREG_STAT1);
  238. int ctrl1, ctrl2;
  239. EFX_TRACE(efx, "Setting low power mode for MMD %d to %d\n",
  240. mmd, lpower);
  241. if (stat & (1 << MDIO_MMDREG_STAT1_LPABLE_LBN)) {
  242. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy,
  243. mmd, MDIO_MMDREG_CTRL1);
  244. if (lpower)
  245. ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LPOWER_LBN);
  246. else
  247. ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LPOWER_LBN);
  248. if (ctrl1 != ctrl2)
  249. mdio_clause45_write(efx, phy, mmd,
  250. MDIO_MMDREG_CTRL1, ctrl2);
  251. }
  252. }
  253. void mdio_clause45_set_mmds_lpower(struct efx_nic *efx,
  254. int low_power, unsigned int mmd_mask)
  255. {
  256. int mmd = 0;
  257. while (mmd_mask) {
  258. if (mmd_mask & 1)
  259. mdio_clause45_set_mmd_lpower(efx, low_power, mmd);
  260. mmd_mask = (mmd_mask >> 1);
  261. mmd++;
  262. }
  263. }
  264. /**
  265. * mdio_clause45_get_settings - Read (some of) the PHY settings over MDIO.
  266. * @efx: Efx NIC
  267. * @ecmd: Buffer for settings
  268. *
  269. * On return the 'port', 'speed', 'supported' and 'advertising' fields of
  270. * ecmd have been filled out based on the PMA type.
  271. */
  272. void mdio_clause45_get_settings(struct efx_nic *efx,
  273. struct ethtool_cmd *ecmd)
  274. {
  275. int pma_type;
  276. /* If no PMA is present we are presumably talking something XAUI-ish
  277. * like CX4. Which we report as FIBRE (see below) */
  278. if ((efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_PMAPMD)) == 0) {
  279. ecmd->speed = SPEED_10000;
  280. ecmd->port = PORT_FIBRE;
  281. ecmd->supported = SUPPORTED_FIBRE;
  282. ecmd->advertising = ADVERTISED_FIBRE;
  283. return;
  284. }
  285. pma_type = mdio_clause45_read(efx, efx->mii.phy_id,
  286. MDIO_MMD_PMAPMD, MDIO_MMDREG_CTRL2);
  287. pma_type &= MDIO_PMAPMD_CTRL2_TYPE_MASK;
  288. switch (pma_type) {
  289. /* We represent CX4 as fibre in the absence of anything
  290. better. */
  291. case MDIO_PMAPMD_CTRL2_10G_CX4:
  292. ecmd->speed = SPEED_10000;
  293. ecmd->port = PORT_FIBRE;
  294. ecmd->supported = SUPPORTED_FIBRE;
  295. ecmd->advertising = ADVERTISED_FIBRE;
  296. break;
  297. /* 10G Base-T */
  298. case MDIO_PMAPMD_CTRL2_10G_BT:
  299. ecmd->speed = SPEED_10000;
  300. ecmd->port = PORT_TP;
  301. ecmd->supported = SUPPORTED_TP | SUPPORTED_10000baseT_Full;
  302. ecmd->advertising = (ADVERTISED_FIBRE
  303. | ADVERTISED_10000baseT_Full);
  304. break;
  305. case MDIO_PMAPMD_CTRL2_1G_BT:
  306. ecmd->speed = SPEED_1000;
  307. ecmd->port = PORT_TP;
  308. ecmd->supported = SUPPORTED_TP | SUPPORTED_1000baseT_Full;
  309. ecmd->advertising = (ADVERTISED_FIBRE
  310. | ADVERTISED_1000baseT_Full);
  311. break;
  312. case MDIO_PMAPMD_CTRL2_100_BT:
  313. ecmd->speed = SPEED_100;
  314. ecmd->port = PORT_TP;
  315. ecmd->supported = SUPPORTED_TP | SUPPORTED_100baseT_Full;
  316. ecmd->advertising = (ADVERTISED_FIBRE
  317. | ADVERTISED_100baseT_Full);
  318. break;
  319. case MDIO_PMAPMD_CTRL2_10_BT:
  320. ecmd->speed = SPEED_10;
  321. ecmd->port = PORT_TP;
  322. ecmd->supported = SUPPORTED_TP | SUPPORTED_10baseT_Full;
  323. ecmd->advertising = ADVERTISED_FIBRE | ADVERTISED_10baseT_Full;
  324. break;
  325. /* All the other defined modes are flavours of
  326. * 10G optical */
  327. default:
  328. ecmd->speed = SPEED_10000;
  329. ecmd->port = PORT_FIBRE;
  330. ecmd->supported = SUPPORTED_FIBRE;
  331. ecmd->advertising = ADVERTISED_FIBRE;
  332. break;
  333. }
  334. }
  335. /**
  336. * mdio_clause45_set_settings - Set (some of) the PHY settings over MDIO.
  337. * @efx: Efx NIC
  338. * @ecmd: New settings
  339. *
  340. * Currently this just enforces that we are _not_ changing the
  341. * 'port', 'speed', 'supported' or 'advertising' settings as these
  342. * cannot be changed on any currently supported PHY.
  343. */
  344. int mdio_clause45_set_settings(struct efx_nic *efx,
  345. struct ethtool_cmd *ecmd)
  346. {
  347. struct ethtool_cmd tmpcmd;
  348. mdio_clause45_get_settings(efx, &tmpcmd);
  349. /* None of the current PHYs support more than one mode
  350. * of operation (and only 10GBT ever will), so keep things
  351. * simple for now */
  352. if ((ecmd->speed == tmpcmd.speed) && (ecmd->port == tmpcmd.port) &&
  353. (ecmd->supported == tmpcmd.supported) &&
  354. (ecmd->advertising == tmpcmd.advertising))
  355. return 0;
  356. return -EOPNOTSUPP;
  357. }