tenxpress.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2007-2009 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. #include <linux/delay.h>
  10. #include <linux/rtnetlink.h>
  11. #include <linux/seq_file.h>
  12. #include "efx.h"
  13. #include "mdio_10g.h"
  14. #include "nic.h"
  15. #include "phy.h"
  16. #include "regs.h"
  17. #include "workarounds.h"
  18. #include "selftest.h"
  19. /* We expect these MMDs to be in the package. SFT9001 also has a
  20. * clause 22 extension MMD, but since it doesn't have all the generic
  21. * MMD registers it is pointless to include it here.
  22. */
  23. #define TENXPRESS_REQUIRED_DEVS (MDIO_DEVS_PMAPMD | \
  24. MDIO_DEVS_PCS | \
  25. MDIO_DEVS_PHYXS | \
  26. MDIO_DEVS_AN)
  27. #define SFX7101_LOOPBACKS ((1 << LOOPBACK_PHYXS) | \
  28. (1 << LOOPBACK_PCS) | \
  29. (1 << LOOPBACK_PMAPMD) | \
  30. (1 << LOOPBACK_PHYXS_WS))
  31. #define SFT9001_LOOPBACKS ((1 << LOOPBACK_GPHY) | \
  32. (1 << LOOPBACK_PHYXS) | \
  33. (1 << LOOPBACK_PCS) | \
  34. (1 << LOOPBACK_PMAPMD) | \
  35. (1 << LOOPBACK_PHYXS_WS))
  36. /* We complain if we fail to see the link partner as 10G capable this many
  37. * times in a row (must be > 1 as sampling the autoneg. registers is racy)
  38. */
  39. #define MAX_BAD_LP_TRIES (5)
  40. /* Extended control register */
  41. #define PMA_PMD_XCONTROL_REG 49152
  42. #define PMA_PMD_EXT_GMII_EN_LBN 1
  43. #define PMA_PMD_EXT_GMII_EN_WIDTH 1
  44. #define PMA_PMD_EXT_CLK_OUT_LBN 2
  45. #define PMA_PMD_EXT_CLK_OUT_WIDTH 1
  46. #define PMA_PMD_LNPGA_POWERDOWN_LBN 8 /* SFX7101 only */
  47. #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
  48. #define PMA_PMD_EXT_CLK312_LBN 8 /* SFT9001 only */
  49. #define PMA_PMD_EXT_CLK312_WIDTH 1
  50. #define PMA_PMD_EXT_LPOWER_LBN 12
  51. #define PMA_PMD_EXT_LPOWER_WIDTH 1
  52. #define PMA_PMD_EXT_ROBUST_LBN 14
  53. #define PMA_PMD_EXT_ROBUST_WIDTH 1
  54. #define PMA_PMD_EXT_SSR_LBN 15
  55. #define PMA_PMD_EXT_SSR_WIDTH 1
  56. /* extended status register */
  57. #define PMA_PMD_XSTATUS_REG 49153
  58. #define PMA_PMD_XSTAT_MDIX_LBN 14
  59. #define PMA_PMD_XSTAT_FLP_LBN (12)
  60. /* LED control register */
  61. #define PMA_PMD_LED_CTRL_REG 49159
  62. #define PMA_PMA_LED_ACTIVITY_LBN (3)
  63. /* LED function override register */
  64. #define PMA_PMD_LED_OVERR_REG 49161
  65. /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
  66. #define PMA_PMD_LED_LINK_LBN (0)
  67. #define PMA_PMD_LED_SPEED_LBN (2)
  68. #define PMA_PMD_LED_TX_LBN (4)
  69. #define PMA_PMD_LED_RX_LBN (6)
  70. /* Override settings */
  71. #define PMA_PMD_LED_AUTO (0) /* H/W control */
  72. #define PMA_PMD_LED_ON (1)
  73. #define PMA_PMD_LED_OFF (2)
  74. #define PMA_PMD_LED_FLASH (3)
  75. #define PMA_PMD_LED_MASK 3
  76. /* All LEDs under hardware control */
  77. #define SFT9001_PMA_PMD_LED_DEFAULT 0
  78. /* Green and Amber under hardware control, Red off */
  79. #define SFX7101_PMA_PMD_LED_DEFAULT (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
  80. #define PMA_PMD_SPEED_ENABLE_REG 49192
  81. #define PMA_PMD_100TX_ADV_LBN 1
  82. #define PMA_PMD_100TX_ADV_WIDTH 1
  83. #define PMA_PMD_1000T_ADV_LBN 2
  84. #define PMA_PMD_1000T_ADV_WIDTH 1
  85. #define PMA_PMD_10000T_ADV_LBN 3
  86. #define PMA_PMD_10000T_ADV_WIDTH 1
  87. #define PMA_PMD_SPEED_LBN 4
  88. #define PMA_PMD_SPEED_WIDTH 4
  89. /* Cable diagnostics - SFT9001 only */
  90. #define PMA_PMD_CDIAG_CTRL_REG 49213
  91. #define CDIAG_CTRL_IMMED_LBN 15
  92. #define CDIAG_CTRL_BRK_LINK_LBN 12
  93. #define CDIAG_CTRL_IN_PROG_LBN 11
  94. #define CDIAG_CTRL_LEN_UNIT_LBN 10
  95. #define CDIAG_CTRL_LEN_METRES 1
  96. #define PMA_PMD_CDIAG_RES_REG 49174
  97. #define CDIAG_RES_A_LBN 12
  98. #define CDIAG_RES_B_LBN 8
  99. #define CDIAG_RES_C_LBN 4
  100. #define CDIAG_RES_D_LBN 0
  101. #define CDIAG_RES_WIDTH 4
  102. #define CDIAG_RES_OPEN 2
  103. #define CDIAG_RES_OK 1
  104. #define CDIAG_RES_INVALID 0
  105. /* Set of 4 registers for pairs A-D */
  106. #define PMA_PMD_CDIAG_LEN_REG 49175
  107. /* Serdes control registers - SFT9001 only */
  108. #define PMA_PMD_CSERDES_CTRL_REG 64258
  109. /* Set the 156.25 MHz output to 312.5 MHz to drive Falcon's XMAC */
  110. #define PMA_PMD_CSERDES_DEFAULT 0x000f
  111. /* Misc register defines - SFX7101 only */
  112. #define PCS_CLOCK_CTRL_REG 55297
  113. #define PLL312_RST_N_LBN 2
  114. #define PCS_SOFT_RST2_REG 55302
  115. #define SERDES_RST_N_LBN 13
  116. #define XGXS_RST_N_LBN 12
  117. #define PCS_TEST_SELECT_REG 55303 /* PRM 10.5.8 */
  118. #define CLK312_EN_LBN 3
  119. /* PHYXS registers */
  120. #define PHYXS_XCONTROL_REG 49152
  121. #define PHYXS_RESET_LBN 15
  122. #define PHYXS_RESET_WIDTH 1
  123. #define PHYXS_TEST1 (49162)
  124. #define LOOPBACK_NEAR_LBN (8)
  125. #define LOOPBACK_NEAR_WIDTH (1)
  126. /* Boot status register */
  127. #define PCS_BOOT_STATUS_REG 53248
  128. #define PCS_BOOT_FATAL_ERROR_LBN 0
  129. #define PCS_BOOT_PROGRESS_LBN 1
  130. #define PCS_BOOT_PROGRESS_WIDTH 2
  131. #define PCS_BOOT_PROGRESS_INIT 0
  132. #define PCS_BOOT_PROGRESS_WAIT_MDIO 1
  133. #define PCS_BOOT_PROGRESS_CHECKSUM 2
  134. #define PCS_BOOT_PROGRESS_JUMP 3
  135. #define PCS_BOOT_DOWNLOAD_WAIT_LBN 3
  136. #define PCS_BOOT_CODE_STARTED_LBN 4
  137. /* 100M/1G PHY registers */
  138. #define GPHY_XCONTROL_REG 49152
  139. #define GPHY_ISOLATE_LBN 10
  140. #define GPHY_ISOLATE_WIDTH 1
  141. #define GPHY_DUPLEX_LBN 8
  142. #define GPHY_DUPLEX_WIDTH 1
  143. #define GPHY_LOOPBACK_NEAR_LBN 14
  144. #define GPHY_LOOPBACK_NEAR_WIDTH 1
  145. #define C22EXT_STATUS_REG 49153
  146. #define C22EXT_STATUS_LINK_LBN 2
  147. #define C22EXT_STATUS_LINK_WIDTH 1
  148. #define C22EXT_MSTSLV_CTRL 49161
  149. #define C22EXT_MSTSLV_CTRL_ADV_1000_HD_LBN 8
  150. #define C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN 9
  151. #define C22EXT_MSTSLV_STATUS 49162
  152. #define C22EXT_MSTSLV_STATUS_LP_1000_HD_LBN 10
  153. #define C22EXT_MSTSLV_STATUS_LP_1000_FD_LBN 11
  154. /* Time to wait between powering down the LNPGA and turning off the power
  155. * rails */
  156. #define LNPGA_PDOWN_WAIT (HZ / 5)
  157. struct tenxpress_phy_data {
  158. enum efx_loopback_mode loopback_mode;
  159. enum efx_phy_mode phy_mode;
  160. int bad_lp_tries;
  161. };
  162. static ssize_t show_phy_short_reach(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  166. int reg;
  167. reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, MDIO_PMA_10GBT_TXPWR);
  168. return sprintf(buf, "%d\n", !!(reg & MDIO_PMA_10GBT_TXPWR_SHORT));
  169. }
  170. static ssize_t set_phy_short_reach(struct device *dev,
  171. struct device_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  175. int rc;
  176. rtnl_lock();
  177. if (efx->state != STATE_RUNNING) {
  178. rc = -EBUSY;
  179. } else {
  180. efx_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_PMA_10GBT_TXPWR,
  181. MDIO_PMA_10GBT_TXPWR_SHORT,
  182. count != 0 && *buf != '0');
  183. rc = efx_reconfigure_port(efx);
  184. }
  185. rtnl_unlock();
  186. return rc < 0 ? rc : (ssize_t)count;
  187. }
  188. static DEVICE_ATTR(phy_short_reach, 0644, show_phy_short_reach,
  189. set_phy_short_reach);
  190. int sft9001_wait_boot(struct efx_nic *efx)
  191. {
  192. unsigned long timeout = jiffies + HZ + 1;
  193. int boot_stat;
  194. for (;;) {
  195. boot_stat = efx_mdio_read(efx, MDIO_MMD_PCS,
  196. PCS_BOOT_STATUS_REG);
  197. if (boot_stat >= 0) {
  198. EFX_LOG(efx, "PHY boot status = %#x\n", boot_stat);
  199. switch (boot_stat &
  200. ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
  201. (3 << PCS_BOOT_PROGRESS_LBN) |
  202. (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN) |
  203. (1 << PCS_BOOT_CODE_STARTED_LBN))) {
  204. case ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
  205. (PCS_BOOT_PROGRESS_CHECKSUM <<
  206. PCS_BOOT_PROGRESS_LBN)):
  207. case ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
  208. (PCS_BOOT_PROGRESS_INIT <<
  209. PCS_BOOT_PROGRESS_LBN) |
  210. (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN)):
  211. return -EINVAL;
  212. case ((PCS_BOOT_PROGRESS_WAIT_MDIO <<
  213. PCS_BOOT_PROGRESS_LBN) |
  214. (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN)):
  215. return (efx->phy_mode & PHY_MODE_SPECIAL) ?
  216. 0 : -EIO;
  217. case ((PCS_BOOT_PROGRESS_JUMP <<
  218. PCS_BOOT_PROGRESS_LBN) |
  219. (1 << PCS_BOOT_CODE_STARTED_LBN)):
  220. case ((PCS_BOOT_PROGRESS_JUMP <<
  221. PCS_BOOT_PROGRESS_LBN) |
  222. (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN) |
  223. (1 << PCS_BOOT_CODE_STARTED_LBN)):
  224. return (efx->phy_mode & PHY_MODE_SPECIAL) ?
  225. -EIO : 0;
  226. default:
  227. if (boot_stat & (1 << PCS_BOOT_FATAL_ERROR_LBN))
  228. return -EIO;
  229. break;
  230. }
  231. }
  232. if (time_after_eq(jiffies, timeout))
  233. return -ETIMEDOUT;
  234. msleep(50);
  235. }
  236. }
  237. static int tenxpress_init(struct efx_nic *efx)
  238. {
  239. int reg;
  240. if (efx->phy_type == PHY_TYPE_SFX7101) {
  241. /* Enable 312.5 MHz clock */
  242. efx_mdio_write(efx, MDIO_MMD_PCS, PCS_TEST_SELECT_REG,
  243. 1 << CLK312_EN_LBN);
  244. } else {
  245. /* Enable 312.5 MHz clock and GMII */
  246. reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
  247. reg |= ((1 << PMA_PMD_EXT_GMII_EN_LBN) |
  248. (1 << PMA_PMD_EXT_CLK_OUT_LBN) |
  249. (1 << PMA_PMD_EXT_CLK312_LBN) |
  250. (1 << PMA_PMD_EXT_ROBUST_LBN));
  251. efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG, reg);
  252. efx_mdio_set_flag(efx, MDIO_MMD_C22EXT,
  253. GPHY_XCONTROL_REG, 1 << GPHY_ISOLATE_LBN,
  254. false);
  255. }
  256. /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
  257. if (efx->phy_type == PHY_TYPE_SFX7101) {
  258. efx_mdio_set_flag(efx, MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG,
  259. 1 << PMA_PMA_LED_ACTIVITY_LBN, true);
  260. efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG,
  261. SFX7101_PMA_PMD_LED_DEFAULT);
  262. }
  263. return 0;
  264. }
  265. static int tenxpress_phy_probe(struct efx_nic *efx)
  266. {
  267. struct tenxpress_phy_data *phy_data;
  268. int rc;
  269. /* Allocate phy private storage */
  270. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  271. if (!phy_data)
  272. return -ENOMEM;
  273. efx->phy_data = phy_data;
  274. phy_data->phy_mode = efx->phy_mode;
  275. /* Create any special files */
  276. if (efx->phy_type == PHY_TYPE_SFT9001B) {
  277. rc = device_create_file(&efx->pci_dev->dev,
  278. &dev_attr_phy_short_reach);
  279. if (rc)
  280. goto fail;
  281. }
  282. if (efx->phy_type == PHY_TYPE_SFX7101) {
  283. efx->mdio.mmds = TENXPRESS_REQUIRED_DEVS;
  284. efx->mdio.mode_support = MDIO_SUPPORTS_C45;
  285. efx->loopback_modes = SFX7101_LOOPBACKS | FALCON_XMAC_LOOPBACKS;
  286. efx->link_advertising = (ADVERTISED_TP | ADVERTISED_Autoneg |
  287. ADVERTISED_10000baseT_Full);
  288. } else {
  289. efx->mdio.mmds = TENXPRESS_REQUIRED_DEVS;
  290. efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  291. efx->loopback_modes = (SFT9001_LOOPBACKS |
  292. FALCON_XMAC_LOOPBACKS |
  293. FALCON_GMAC_LOOPBACKS);
  294. efx->link_advertising = (ADVERTISED_TP | ADVERTISED_Autoneg |
  295. ADVERTISED_10000baseT_Full |
  296. ADVERTISED_1000baseT_Full |
  297. ADVERTISED_100baseT_Full);
  298. }
  299. return 0;
  300. fail:
  301. kfree(efx->phy_data);
  302. efx->phy_data = NULL;
  303. return rc;
  304. }
  305. static int tenxpress_phy_init(struct efx_nic *efx)
  306. {
  307. int rc;
  308. falcon_board(efx)->type->init_phy(efx);
  309. if (!(efx->phy_mode & PHY_MODE_SPECIAL)) {
  310. if (efx->phy_type == PHY_TYPE_SFT9001A) {
  311. int reg;
  312. reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
  313. PMA_PMD_XCONTROL_REG);
  314. reg |= (1 << PMA_PMD_EXT_SSR_LBN);
  315. efx_mdio_write(efx, MDIO_MMD_PMAPMD,
  316. PMA_PMD_XCONTROL_REG, reg);
  317. mdelay(200);
  318. }
  319. rc = efx_mdio_wait_reset_mmds(efx, TENXPRESS_REQUIRED_DEVS);
  320. if (rc < 0)
  321. return rc;
  322. rc = efx_mdio_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
  323. if (rc < 0)
  324. return rc;
  325. }
  326. rc = tenxpress_init(efx);
  327. if (rc < 0)
  328. return rc;
  329. /* Reinitialise flow control settings */
  330. efx_link_set_wanted_fc(efx, efx->wanted_fc);
  331. efx_mdio_an_reconfigure(efx);
  332. schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
  333. /* Let XGXS and SerDes out of reset */
  334. falcon_reset_xaui(efx);
  335. return 0;
  336. }
  337. /* Perform a "special software reset" on the PHY. The caller is
  338. * responsible for saving and restoring the PHY hardware registers
  339. * properly, and masking/unmasking LASI */
  340. static int tenxpress_special_reset(struct efx_nic *efx)
  341. {
  342. int rc, reg;
  343. /* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
  344. * a special software reset can glitch the XGMAC sufficiently for stats
  345. * requests to fail. */
  346. falcon_stop_nic_stats(efx);
  347. /* Initiate reset */
  348. reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
  349. reg |= (1 << PMA_PMD_EXT_SSR_LBN);
  350. efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG, reg);
  351. mdelay(200);
  352. /* Wait for the blocks to come out of reset */
  353. rc = efx_mdio_wait_reset_mmds(efx, TENXPRESS_REQUIRED_DEVS);
  354. if (rc < 0)
  355. goto out;
  356. /* Try and reconfigure the device */
  357. rc = tenxpress_init(efx);
  358. if (rc < 0)
  359. goto out;
  360. /* Wait for the XGXS state machine to churn */
  361. mdelay(10);
  362. out:
  363. falcon_start_nic_stats(efx);
  364. return rc;
  365. }
  366. static void sfx7101_check_bad_lp(struct efx_nic *efx, bool link_ok)
  367. {
  368. struct tenxpress_phy_data *pd = efx->phy_data;
  369. bool bad_lp;
  370. int reg;
  371. if (link_ok) {
  372. bad_lp = false;
  373. } else {
  374. /* Check that AN has started but not completed. */
  375. reg = efx_mdio_read(efx, MDIO_MMD_AN, MDIO_STAT1);
  376. if (!(reg & MDIO_AN_STAT1_LPABLE))
  377. return; /* LP status is unknown */
  378. bad_lp = !(reg & MDIO_AN_STAT1_COMPLETE);
  379. if (bad_lp)
  380. pd->bad_lp_tries++;
  381. }
  382. /* Nothing to do if all is well and was previously so. */
  383. if (!pd->bad_lp_tries)
  384. return;
  385. /* Use the RX (red) LED as an error indicator once we've seen AN
  386. * failure several times in a row, and also log a message. */
  387. if (!bad_lp || pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
  388. reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
  389. PMA_PMD_LED_OVERR_REG);
  390. reg &= ~(PMA_PMD_LED_MASK << PMA_PMD_LED_RX_LBN);
  391. if (!bad_lp) {
  392. reg |= PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN;
  393. } else {
  394. reg |= PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN;
  395. EFX_ERR(efx, "appears to be plugged into a port"
  396. " that is not 10GBASE-T capable. The PHY"
  397. " supports 10GBASE-T ONLY, so no link can"
  398. " be established\n");
  399. }
  400. efx_mdio_write(efx, MDIO_MMD_PMAPMD,
  401. PMA_PMD_LED_OVERR_REG, reg);
  402. pd->bad_lp_tries = bad_lp;
  403. }
  404. }
  405. static bool sfx7101_link_ok(struct efx_nic *efx)
  406. {
  407. return efx_mdio_links_ok(efx,
  408. MDIO_DEVS_PMAPMD |
  409. MDIO_DEVS_PCS |
  410. MDIO_DEVS_PHYXS);
  411. }
  412. static bool sft9001_link_ok(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  413. {
  414. u32 reg;
  415. if (efx_phy_mode_disabled(efx->phy_mode))
  416. return false;
  417. else if (efx->loopback_mode == LOOPBACK_GPHY)
  418. return true;
  419. else if (efx->loopback_mode)
  420. return efx_mdio_links_ok(efx,
  421. MDIO_DEVS_PMAPMD |
  422. MDIO_DEVS_PHYXS);
  423. /* We must use the same definition of link state as LASI,
  424. * otherwise we can miss a link state transition
  425. */
  426. if (ecmd->speed == 10000) {
  427. reg = efx_mdio_read(efx, MDIO_MMD_PCS, MDIO_PCS_10GBRT_STAT1);
  428. return reg & MDIO_PCS_10GBRT_STAT1_BLKLK;
  429. } else {
  430. reg = efx_mdio_read(efx, MDIO_MMD_C22EXT, C22EXT_STATUS_REG);
  431. return reg & (1 << C22EXT_STATUS_LINK_LBN);
  432. }
  433. }
  434. static void tenxpress_ext_loopback(struct efx_nic *efx)
  435. {
  436. efx_mdio_set_flag(efx, MDIO_MMD_PHYXS, PHYXS_TEST1,
  437. 1 << LOOPBACK_NEAR_LBN,
  438. efx->loopback_mode == LOOPBACK_PHYXS);
  439. if (efx->phy_type != PHY_TYPE_SFX7101)
  440. efx_mdio_set_flag(efx, MDIO_MMD_C22EXT, GPHY_XCONTROL_REG,
  441. 1 << GPHY_LOOPBACK_NEAR_LBN,
  442. efx->loopback_mode == LOOPBACK_GPHY);
  443. }
  444. static void tenxpress_low_power(struct efx_nic *efx)
  445. {
  446. if (efx->phy_type == PHY_TYPE_SFX7101)
  447. efx_mdio_set_mmds_lpower(
  448. efx, !!(efx->phy_mode & PHY_MODE_LOW_POWER),
  449. TENXPRESS_REQUIRED_DEVS);
  450. else
  451. efx_mdio_set_flag(
  452. efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG,
  453. 1 << PMA_PMD_EXT_LPOWER_LBN,
  454. !!(efx->phy_mode & PHY_MODE_LOW_POWER));
  455. }
  456. static int tenxpress_phy_reconfigure(struct efx_nic *efx)
  457. {
  458. struct tenxpress_phy_data *phy_data = efx->phy_data;
  459. bool phy_mode_change, loop_reset;
  460. if (efx->phy_mode & (PHY_MODE_OFF | PHY_MODE_SPECIAL)) {
  461. phy_data->phy_mode = efx->phy_mode;
  462. return 0;
  463. }
  464. phy_mode_change = (efx->phy_mode == PHY_MODE_NORMAL &&
  465. phy_data->phy_mode != PHY_MODE_NORMAL);
  466. loop_reset = (LOOPBACK_OUT_OF(phy_data, efx, LOOPBACKS_EXTERNAL(efx)) ||
  467. LOOPBACK_CHANGED(phy_data, efx, 1 << LOOPBACK_GPHY));
  468. if (loop_reset || phy_mode_change) {
  469. tenxpress_special_reset(efx);
  470. /* Reset XAUI if we were in 10G, and are staying
  471. * in 10G. If we're moving into and out of 10G
  472. * then xaui will be reset anyway */
  473. if (EFX_IS10G(efx))
  474. falcon_reset_xaui(efx);
  475. }
  476. tenxpress_low_power(efx);
  477. efx_mdio_transmit_disable(efx);
  478. efx_mdio_phy_reconfigure(efx);
  479. tenxpress_ext_loopback(efx);
  480. efx_mdio_an_reconfigure(efx);
  481. phy_data->loopback_mode = efx->loopback_mode;
  482. phy_data->phy_mode = efx->phy_mode;
  483. return 0;
  484. }
  485. static void
  486. tenxpress_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd);
  487. /* Poll for link state changes */
  488. static bool tenxpress_phy_poll(struct efx_nic *efx)
  489. {
  490. struct efx_link_state old_state = efx->link_state;
  491. if (efx->phy_type == PHY_TYPE_SFX7101) {
  492. efx->link_state.up = sfx7101_link_ok(efx);
  493. efx->link_state.speed = 10000;
  494. efx->link_state.fd = true;
  495. efx->link_state.fc = efx_mdio_get_pause(efx);
  496. sfx7101_check_bad_lp(efx, efx->link_state.up);
  497. } else {
  498. struct ethtool_cmd ecmd;
  499. /* Check the LASI alarm first */
  500. if (efx->loopback_mode == LOOPBACK_NONE &&
  501. !(efx_mdio_read(efx, MDIO_MMD_PMAPMD, MDIO_PMA_LASI_STAT) &
  502. MDIO_PMA_LASI_LSALARM))
  503. return false;
  504. tenxpress_get_settings(efx, &ecmd);
  505. efx->link_state.up = sft9001_link_ok(efx, &ecmd);
  506. efx->link_state.speed = ecmd.speed;
  507. efx->link_state.fd = (ecmd.duplex == DUPLEX_FULL);
  508. efx->link_state.fc = efx_mdio_get_pause(efx);
  509. }
  510. return !efx_link_state_equal(&efx->link_state, &old_state);
  511. }
  512. static void sfx7101_phy_fini(struct efx_nic *efx)
  513. {
  514. int reg;
  515. /* Power down the LNPGA */
  516. reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
  517. efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG, reg);
  518. /* Waiting here ensures that the board fini, which can turn
  519. * off the power to the PHY, won't get run until the LNPGA
  520. * powerdown has been given long enough to complete. */
  521. schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
  522. }
  523. static void tenxpress_phy_remove(struct efx_nic *efx)
  524. {
  525. if (efx->phy_type == PHY_TYPE_SFT9001B)
  526. device_remove_file(&efx->pci_dev->dev,
  527. &dev_attr_phy_short_reach);
  528. kfree(efx->phy_data);
  529. efx->phy_data = NULL;
  530. }
  531. /* Override the RX, TX and link LEDs */
  532. void tenxpress_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
  533. {
  534. int reg;
  535. switch (mode) {
  536. case EFX_LED_OFF:
  537. reg = (PMA_PMD_LED_OFF << PMA_PMD_LED_TX_LBN) |
  538. (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN) |
  539. (PMA_PMD_LED_OFF << PMA_PMD_LED_LINK_LBN);
  540. break;
  541. case EFX_LED_ON:
  542. reg = (PMA_PMD_LED_ON << PMA_PMD_LED_TX_LBN) |
  543. (PMA_PMD_LED_ON << PMA_PMD_LED_RX_LBN) |
  544. (PMA_PMD_LED_ON << PMA_PMD_LED_LINK_LBN);
  545. break;
  546. default:
  547. if (efx->phy_type == PHY_TYPE_SFX7101)
  548. reg = SFX7101_PMA_PMD_LED_DEFAULT;
  549. else
  550. reg = SFT9001_PMA_PMD_LED_DEFAULT;
  551. break;
  552. }
  553. efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG, reg);
  554. }
  555. static const char *const sfx7101_test_names[] = {
  556. "bist"
  557. };
  558. static const char *sfx7101_test_name(struct efx_nic *efx, unsigned int index)
  559. {
  560. if (index < ARRAY_SIZE(sfx7101_test_names))
  561. return sfx7101_test_names[index];
  562. return NULL;
  563. }
  564. static int
  565. sfx7101_run_tests(struct efx_nic *efx, int *results, unsigned flags)
  566. {
  567. int rc;
  568. if (!(flags & ETH_TEST_FL_OFFLINE))
  569. return 0;
  570. /* BIST is automatically run after a special software reset */
  571. rc = tenxpress_special_reset(efx);
  572. results[0] = rc ? -1 : 1;
  573. efx_mdio_an_reconfigure(efx);
  574. return rc;
  575. }
  576. static const char *const sft9001_test_names[] = {
  577. "bist",
  578. "cable.pairA.status",
  579. "cable.pairB.status",
  580. "cable.pairC.status",
  581. "cable.pairD.status",
  582. "cable.pairA.length",
  583. "cable.pairB.length",
  584. "cable.pairC.length",
  585. "cable.pairD.length",
  586. };
  587. static const char *sft9001_test_name(struct efx_nic *efx, unsigned int index)
  588. {
  589. if (index < ARRAY_SIZE(sft9001_test_names))
  590. return sft9001_test_names[index];
  591. return NULL;
  592. }
  593. static int sft9001_run_tests(struct efx_nic *efx, int *results, unsigned flags)
  594. {
  595. int rc = 0, rc2, i, ctrl_reg, res_reg;
  596. /* Initialise cable diagnostic results to unknown failure */
  597. for (i = 1; i < 9; ++i)
  598. results[i] = -1;
  599. /* Run cable diagnostics; wait up to 5 seconds for them to complete.
  600. * A cable fault is not a self-test failure, but a timeout is. */
  601. ctrl_reg = ((1 << CDIAG_CTRL_IMMED_LBN) |
  602. (CDIAG_CTRL_LEN_METRES << CDIAG_CTRL_LEN_UNIT_LBN));
  603. if (flags & ETH_TEST_FL_OFFLINE) {
  604. /* Break the link in order to run full diagnostics. We
  605. * must reset the PHY to resume normal service. */
  606. ctrl_reg |= (1 << CDIAG_CTRL_BRK_LINK_LBN);
  607. }
  608. efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_CDIAG_CTRL_REG,
  609. ctrl_reg);
  610. i = 0;
  611. while (efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_CDIAG_CTRL_REG) &
  612. (1 << CDIAG_CTRL_IN_PROG_LBN)) {
  613. if (++i == 50) {
  614. rc = -ETIMEDOUT;
  615. goto out;
  616. }
  617. msleep(100);
  618. }
  619. res_reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_CDIAG_RES_REG);
  620. for (i = 0; i < 4; i++) {
  621. int pair_res =
  622. (res_reg >> (CDIAG_RES_A_LBN - i * CDIAG_RES_WIDTH))
  623. & ((1 << CDIAG_RES_WIDTH) - 1);
  624. int len_reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
  625. PMA_PMD_CDIAG_LEN_REG + i);
  626. if (pair_res == CDIAG_RES_OK)
  627. results[1 + i] = 1;
  628. else if (pair_res == CDIAG_RES_INVALID)
  629. results[1 + i] = -1;
  630. else
  631. results[1 + i] = -pair_res;
  632. if (pair_res != CDIAG_RES_INVALID &&
  633. pair_res != CDIAG_RES_OPEN &&
  634. len_reg != 0xffff)
  635. results[5 + i] = len_reg;
  636. }
  637. out:
  638. if (flags & ETH_TEST_FL_OFFLINE) {
  639. /* Reset, running the BIST and then resuming normal service. */
  640. rc2 = tenxpress_special_reset(efx);
  641. results[0] = rc2 ? -1 : 1;
  642. if (!rc)
  643. rc = rc2;
  644. efx_mdio_an_reconfigure(efx);
  645. }
  646. return rc;
  647. }
  648. static void
  649. tenxpress_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  650. {
  651. u32 adv = 0, lpa = 0;
  652. int reg;
  653. if (efx->phy_type != PHY_TYPE_SFX7101) {
  654. reg = efx_mdio_read(efx, MDIO_MMD_C22EXT, C22EXT_MSTSLV_CTRL);
  655. if (reg & (1 << C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN))
  656. adv |= ADVERTISED_1000baseT_Full;
  657. reg = efx_mdio_read(efx, MDIO_MMD_C22EXT, C22EXT_MSTSLV_STATUS);
  658. if (reg & (1 << C22EXT_MSTSLV_STATUS_LP_1000_HD_LBN))
  659. lpa |= ADVERTISED_1000baseT_Half;
  660. if (reg & (1 << C22EXT_MSTSLV_STATUS_LP_1000_FD_LBN))
  661. lpa |= ADVERTISED_1000baseT_Full;
  662. }
  663. reg = efx_mdio_read(efx, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL);
  664. if (reg & MDIO_AN_10GBT_CTRL_ADV10G)
  665. adv |= ADVERTISED_10000baseT_Full;
  666. reg = efx_mdio_read(efx, MDIO_MMD_AN, MDIO_AN_10GBT_STAT);
  667. if (reg & MDIO_AN_10GBT_STAT_LP10G)
  668. lpa |= ADVERTISED_10000baseT_Full;
  669. mdio45_ethtool_gset_npage(&efx->mdio, ecmd, adv, lpa);
  670. if (efx->phy_type != PHY_TYPE_SFX7101) {
  671. ecmd->supported |= (SUPPORTED_100baseT_Full |
  672. SUPPORTED_1000baseT_Full);
  673. if (ecmd->speed != SPEED_10000) {
  674. ecmd->eth_tp_mdix =
  675. (efx_mdio_read(efx, MDIO_MMD_PMAPMD,
  676. PMA_PMD_XSTATUS_REG) &
  677. (1 << PMA_PMD_XSTAT_MDIX_LBN))
  678. ? ETH_TP_MDI_X : ETH_TP_MDI;
  679. }
  680. }
  681. /* In loopback, the PHY automatically brings up the correct interface,
  682. * but doesn't advertise the correct speed. So override it */
  683. if (efx->loopback_mode == LOOPBACK_GPHY)
  684. ecmd->speed = SPEED_1000;
  685. else if (LOOPBACK_EXTERNAL(efx))
  686. ecmd->speed = SPEED_10000;
  687. }
  688. static int tenxpress_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  689. {
  690. if (!ecmd->autoneg)
  691. return -EINVAL;
  692. return efx_mdio_set_settings(efx, ecmd);
  693. }
  694. static void sfx7101_set_npage_adv(struct efx_nic *efx, u32 advertising)
  695. {
  696. efx_mdio_set_flag(efx, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
  697. MDIO_AN_10GBT_CTRL_ADV10G,
  698. advertising & ADVERTISED_10000baseT_Full);
  699. }
  700. static void sft9001_set_npage_adv(struct efx_nic *efx, u32 advertising)
  701. {
  702. efx_mdio_set_flag(efx, MDIO_MMD_C22EXT, C22EXT_MSTSLV_CTRL,
  703. 1 << C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN,
  704. advertising & ADVERTISED_1000baseT_Full);
  705. efx_mdio_set_flag(efx, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
  706. MDIO_AN_10GBT_CTRL_ADV10G,
  707. advertising & ADVERTISED_10000baseT_Full);
  708. }
  709. struct efx_phy_operations falcon_sfx7101_phy_ops = {
  710. .probe = tenxpress_phy_probe,
  711. .init = tenxpress_phy_init,
  712. .reconfigure = tenxpress_phy_reconfigure,
  713. .poll = tenxpress_phy_poll,
  714. .fini = sfx7101_phy_fini,
  715. .remove = tenxpress_phy_remove,
  716. .get_settings = tenxpress_get_settings,
  717. .set_settings = tenxpress_set_settings,
  718. .set_npage_adv = sfx7101_set_npage_adv,
  719. .test_name = sfx7101_test_name,
  720. .run_tests = sfx7101_run_tests,
  721. };
  722. struct efx_phy_operations falcon_sft9001_phy_ops = {
  723. .probe = tenxpress_phy_probe,
  724. .init = tenxpress_phy_init,
  725. .reconfigure = tenxpress_phy_reconfigure,
  726. .poll = tenxpress_phy_poll,
  727. .fini = efx_port_dummy_op_void,
  728. .remove = tenxpress_phy_remove,
  729. .get_settings = tenxpress_get_settings,
  730. .set_settings = tenxpress_set_settings,
  731. .set_npage_adv = sft9001_set_npage_adv,
  732. .test_name = sft9001_test_name,
  733. .run_tests = sft9001_run_tests,
  734. };