PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/ethernet/intel/ixgbe/ixgbe_phy.c

http://github.com/mirrors/linux
C | 2667 lines | 1663 code | 380 blank | 624 comment | 411 complexity | 402331e6976d95843b194b1978bfb596 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include <linux/pci.h>
  4. #include <linux/delay.h>
  5. #include <linux/iopoll.h>
  6. #include <linux/sched.h>
  7. #include "ixgbe.h"
  8. #include "ixgbe_phy.h"
  9. static void ixgbe_i2c_start(struct ixgbe_hw *hw);
  10. static void ixgbe_i2c_stop(struct ixgbe_hw *hw);
  11. static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data);
  12. static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data);
  13. static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw);
  14. static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data);
  15. static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data);
  16. static void ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
  17. static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
  18. static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data);
  19. static bool ixgbe_get_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl);
  20. static void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw);
  21. static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id);
  22. static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw);
  23. static s32 ixgbe_identify_qsfp_module_generic(struct ixgbe_hw *hw);
  24. /**
  25. * ixgbe_out_i2c_byte_ack - Send I2C byte with ack
  26. * @hw: pointer to the hardware structure
  27. * @byte: byte to send
  28. *
  29. * Returns an error code on error.
  30. **/
  31. static s32 ixgbe_out_i2c_byte_ack(struct ixgbe_hw *hw, u8 byte)
  32. {
  33. s32 status;
  34. status = ixgbe_clock_out_i2c_byte(hw, byte);
  35. if (status)
  36. return status;
  37. return ixgbe_get_i2c_ack(hw);
  38. }
  39. /**
  40. * ixgbe_in_i2c_byte_ack - Receive an I2C byte and send ack
  41. * @hw: pointer to the hardware structure
  42. * @byte: pointer to a u8 to receive the byte
  43. *
  44. * Returns an error code on error.
  45. **/
  46. static s32 ixgbe_in_i2c_byte_ack(struct ixgbe_hw *hw, u8 *byte)
  47. {
  48. s32 status;
  49. status = ixgbe_clock_in_i2c_byte(hw, byte);
  50. if (status)
  51. return status;
  52. /* ACK */
  53. return ixgbe_clock_out_i2c_bit(hw, false);
  54. }
  55. /**
  56. * ixgbe_ones_comp_byte_add - Perform one's complement addition
  57. * @add1: addend 1
  58. * @add2: addend 2
  59. *
  60. * Returns one's complement 8-bit sum.
  61. **/
  62. static u8 ixgbe_ones_comp_byte_add(u8 add1, u8 add2)
  63. {
  64. u16 sum = add1 + add2;
  65. sum = (sum & 0xFF) + (sum >> 8);
  66. return sum & 0xFF;
  67. }
  68. /**
  69. * ixgbe_read_i2c_combined_generic_int - Perform I2C read combined operation
  70. * @hw: pointer to the hardware structure
  71. * @addr: I2C bus address to read from
  72. * @reg: I2C device register to read from
  73. * @val: pointer to location to receive read value
  74. * @lock: true if to take and release semaphore
  75. *
  76. * Returns an error code on error.
  77. */
  78. s32 ixgbe_read_i2c_combined_generic_int(struct ixgbe_hw *hw, u8 addr,
  79. u16 reg, u16 *val, bool lock)
  80. {
  81. u32 swfw_mask = hw->phy.phy_semaphore_mask;
  82. int max_retry = 3;
  83. int retry = 0;
  84. u8 csum_byte;
  85. u8 high_bits;
  86. u8 low_bits;
  87. u8 reg_high;
  88. u8 csum;
  89. reg_high = ((reg >> 7) & 0xFE) | 1; /* Indicate read combined */
  90. csum = ixgbe_ones_comp_byte_add(reg_high, reg & 0xFF);
  91. csum = ~csum;
  92. do {
  93. if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
  94. return IXGBE_ERR_SWFW_SYNC;
  95. ixgbe_i2c_start(hw);
  96. /* Device Address and write indication */
  97. if (ixgbe_out_i2c_byte_ack(hw, addr))
  98. goto fail;
  99. /* Write bits 14:8 */
  100. if (ixgbe_out_i2c_byte_ack(hw, reg_high))
  101. goto fail;
  102. /* Write bits 7:0 */
  103. if (ixgbe_out_i2c_byte_ack(hw, reg & 0xFF))
  104. goto fail;
  105. /* Write csum */
  106. if (ixgbe_out_i2c_byte_ack(hw, csum))
  107. goto fail;
  108. /* Re-start condition */
  109. ixgbe_i2c_start(hw);
  110. /* Device Address and read indication */
  111. if (ixgbe_out_i2c_byte_ack(hw, addr | 1))
  112. goto fail;
  113. /* Get upper bits */
  114. if (ixgbe_in_i2c_byte_ack(hw, &high_bits))
  115. goto fail;
  116. /* Get low bits */
  117. if (ixgbe_in_i2c_byte_ack(hw, &low_bits))
  118. goto fail;
  119. /* Get csum */
  120. if (ixgbe_clock_in_i2c_byte(hw, &csum_byte))
  121. goto fail;
  122. /* NACK */
  123. if (ixgbe_clock_out_i2c_bit(hw, false))
  124. goto fail;
  125. ixgbe_i2c_stop(hw);
  126. if (lock)
  127. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  128. *val = (high_bits << 8) | low_bits;
  129. return 0;
  130. fail:
  131. ixgbe_i2c_bus_clear(hw);
  132. if (lock)
  133. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  134. retry++;
  135. if (retry < max_retry)
  136. hw_dbg(hw, "I2C byte read combined error - Retry.\n");
  137. else
  138. hw_dbg(hw, "I2C byte read combined error.\n");
  139. } while (retry < max_retry);
  140. return IXGBE_ERR_I2C;
  141. }
  142. /**
  143. * ixgbe_write_i2c_combined_generic_int - Perform I2C write combined operation
  144. * @hw: pointer to the hardware structure
  145. * @addr: I2C bus address to write to
  146. * @reg: I2C device register to write to
  147. * @val: value to write
  148. * @lock: true if to take and release semaphore
  149. *
  150. * Returns an error code on error.
  151. */
  152. s32 ixgbe_write_i2c_combined_generic_int(struct ixgbe_hw *hw, u8 addr,
  153. u16 reg, u16 val, bool lock)
  154. {
  155. u32 swfw_mask = hw->phy.phy_semaphore_mask;
  156. int max_retry = 1;
  157. int retry = 0;
  158. u8 reg_high;
  159. u8 csum;
  160. reg_high = (reg >> 7) & 0xFE; /* Indicate write combined */
  161. csum = ixgbe_ones_comp_byte_add(reg_high, reg & 0xFF);
  162. csum = ixgbe_ones_comp_byte_add(csum, val >> 8);
  163. csum = ixgbe_ones_comp_byte_add(csum, val & 0xFF);
  164. csum = ~csum;
  165. do {
  166. if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
  167. return IXGBE_ERR_SWFW_SYNC;
  168. ixgbe_i2c_start(hw);
  169. /* Device Address and write indication */
  170. if (ixgbe_out_i2c_byte_ack(hw, addr))
  171. goto fail;
  172. /* Write bits 14:8 */
  173. if (ixgbe_out_i2c_byte_ack(hw, reg_high))
  174. goto fail;
  175. /* Write bits 7:0 */
  176. if (ixgbe_out_i2c_byte_ack(hw, reg & 0xFF))
  177. goto fail;
  178. /* Write data 15:8 */
  179. if (ixgbe_out_i2c_byte_ack(hw, val >> 8))
  180. goto fail;
  181. /* Write data 7:0 */
  182. if (ixgbe_out_i2c_byte_ack(hw, val & 0xFF))
  183. goto fail;
  184. /* Write csum */
  185. if (ixgbe_out_i2c_byte_ack(hw, csum))
  186. goto fail;
  187. ixgbe_i2c_stop(hw);
  188. if (lock)
  189. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  190. return 0;
  191. fail:
  192. ixgbe_i2c_bus_clear(hw);
  193. if (lock)
  194. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  195. retry++;
  196. if (retry < max_retry)
  197. hw_dbg(hw, "I2C byte write combined error - Retry.\n");
  198. else
  199. hw_dbg(hw, "I2C byte write combined error.\n");
  200. } while (retry < max_retry);
  201. return IXGBE_ERR_I2C;
  202. }
  203. /**
  204. * ixgbe_probe_phy - Probe a single address for a PHY
  205. * @hw: pointer to hardware structure
  206. * @phy_addr: PHY address to probe
  207. *
  208. * Returns true if PHY found
  209. **/
  210. static bool ixgbe_probe_phy(struct ixgbe_hw *hw, u16 phy_addr)
  211. {
  212. u16 ext_ability = 0;
  213. hw->phy.mdio.prtad = phy_addr;
  214. if (mdio45_probe(&hw->phy.mdio, phy_addr) != 0)
  215. return false;
  216. if (ixgbe_get_phy_id(hw))
  217. return false;
  218. hw->phy.type = ixgbe_get_phy_type_from_id(hw->phy.id);
  219. if (hw->phy.type == ixgbe_phy_unknown) {
  220. hw->phy.ops.read_reg(hw,
  221. MDIO_PMA_EXTABLE,
  222. MDIO_MMD_PMAPMD,
  223. &ext_ability);
  224. if (ext_ability &
  225. (MDIO_PMA_EXTABLE_10GBT |
  226. MDIO_PMA_EXTABLE_1000BT))
  227. hw->phy.type = ixgbe_phy_cu_unknown;
  228. else
  229. hw->phy.type = ixgbe_phy_generic;
  230. }
  231. return true;
  232. }
  233. /**
  234. * ixgbe_identify_phy_generic - Get physical layer module
  235. * @hw: pointer to hardware structure
  236. *
  237. * Determines the physical layer module found on the current adapter.
  238. **/
  239. s32 ixgbe_identify_phy_generic(struct ixgbe_hw *hw)
  240. {
  241. u32 phy_addr;
  242. u32 status = IXGBE_ERR_PHY_ADDR_INVALID;
  243. if (!hw->phy.phy_semaphore_mask) {
  244. if (hw->bus.lan_id)
  245. hw->phy.phy_semaphore_mask = IXGBE_GSSR_PHY1_SM;
  246. else
  247. hw->phy.phy_semaphore_mask = IXGBE_GSSR_PHY0_SM;
  248. }
  249. if (hw->phy.type != ixgbe_phy_unknown)
  250. return 0;
  251. if (hw->phy.nw_mng_if_sel) {
  252. phy_addr = (hw->phy.nw_mng_if_sel &
  253. IXGBE_NW_MNG_IF_SEL_MDIO_PHY_ADD) >>
  254. IXGBE_NW_MNG_IF_SEL_MDIO_PHY_ADD_SHIFT;
  255. if (ixgbe_probe_phy(hw, phy_addr))
  256. return 0;
  257. else
  258. return IXGBE_ERR_PHY_ADDR_INVALID;
  259. }
  260. for (phy_addr = 0; phy_addr < IXGBE_MAX_PHY_ADDR; phy_addr++) {
  261. if (ixgbe_probe_phy(hw, phy_addr)) {
  262. status = 0;
  263. break;
  264. }
  265. }
  266. /* Certain media types do not have a phy so an address will not
  267. * be found and the code will take this path. Caller has to
  268. * decide if it is an error or not.
  269. */
  270. if (status)
  271. hw->phy.mdio.prtad = MDIO_PRTAD_NONE;
  272. return status;
  273. }
  274. /**
  275. * ixgbe_check_reset_blocked - check status of MNG FW veto bit
  276. * @hw: pointer to the hardware structure
  277. *
  278. * This function checks the MMNGC.MNG_VETO bit to see if there are
  279. * any constraints on link from manageability. For MAC's that don't
  280. * have this bit just return false since the link can not be blocked
  281. * via this method.
  282. **/
  283. bool ixgbe_check_reset_blocked(struct ixgbe_hw *hw)
  284. {
  285. u32 mmngc;
  286. /* If we don't have this bit, it can't be blocking */
  287. if (hw->mac.type == ixgbe_mac_82598EB)
  288. return false;
  289. mmngc = IXGBE_READ_REG(hw, IXGBE_MMNGC);
  290. if (mmngc & IXGBE_MMNGC_MNG_VETO) {
  291. hw_dbg(hw, "MNG_VETO bit detected.\n");
  292. return true;
  293. }
  294. return false;
  295. }
  296. /**
  297. * ixgbe_get_phy_id - Get the phy type
  298. * @hw: pointer to hardware structure
  299. *
  300. **/
  301. static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw)
  302. {
  303. s32 status;
  304. u16 phy_id_high = 0;
  305. u16 phy_id_low = 0;
  306. status = hw->phy.ops.read_reg(hw, MDIO_DEVID1, MDIO_MMD_PMAPMD,
  307. &phy_id_high);
  308. if (!status) {
  309. hw->phy.id = (u32)(phy_id_high << 16);
  310. status = hw->phy.ops.read_reg(hw, MDIO_DEVID2, MDIO_MMD_PMAPMD,
  311. &phy_id_low);
  312. hw->phy.id |= (u32)(phy_id_low & IXGBE_PHY_REVISION_MASK);
  313. hw->phy.revision = (u32)(phy_id_low & ~IXGBE_PHY_REVISION_MASK);
  314. }
  315. return status;
  316. }
  317. /**
  318. * ixgbe_get_phy_type_from_id - Get the phy type
  319. * @phy_id: hardware phy id
  320. *
  321. **/
  322. static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id)
  323. {
  324. enum ixgbe_phy_type phy_type;
  325. switch (phy_id) {
  326. case TN1010_PHY_ID:
  327. phy_type = ixgbe_phy_tn;
  328. break;
  329. case X550_PHY_ID2:
  330. case X550_PHY_ID3:
  331. case X540_PHY_ID:
  332. phy_type = ixgbe_phy_aq;
  333. break;
  334. case QT2022_PHY_ID:
  335. phy_type = ixgbe_phy_qt;
  336. break;
  337. case ATH_PHY_ID:
  338. phy_type = ixgbe_phy_nl;
  339. break;
  340. case X557_PHY_ID:
  341. case X557_PHY_ID2:
  342. phy_type = ixgbe_phy_x550em_ext_t;
  343. break;
  344. default:
  345. phy_type = ixgbe_phy_unknown;
  346. break;
  347. }
  348. return phy_type;
  349. }
  350. /**
  351. * ixgbe_reset_phy_generic - Performs a PHY reset
  352. * @hw: pointer to hardware structure
  353. **/
  354. s32 ixgbe_reset_phy_generic(struct ixgbe_hw *hw)
  355. {
  356. u32 i;
  357. u16 ctrl = 0;
  358. s32 status = 0;
  359. if (hw->phy.type == ixgbe_phy_unknown)
  360. status = ixgbe_identify_phy_generic(hw);
  361. if (status != 0 || hw->phy.type == ixgbe_phy_none)
  362. return status;
  363. /* Don't reset PHY if it's shut down due to overtemp. */
  364. if (!hw->phy.reset_if_overtemp &&
  365. (IXGBE_ERR_OVERTEMP == hw->phy.ops.check_overtemp(hw)))
  366. return 0;
  367. /* Blocked by MNG FW so bail */
  368. if (ixgbe_check_reset_blocked(hw))
  369. return 0;
  370. /*
  371. * Perform soft PHY reset to the PHY_XS.
  372. * This will cause a soft reset to the PHY
  373. */
  374. hw->phy.ops.write_reg(hw, MDIO_CTRL1,
  375. MDIO_MMD_PHYXS,
  376. MDIO_CTRL1_RESET);
  377. /*
  378. * Poll for reset bit to self-clear indicating reset is complete.
  379. * Some PHYs could take up to 3 seconds to complete and need about
  380. * 1.7 usec delay after the reset is complete.
  381. */
  382. for (i = 0; i < 30; i++) {
  383. msleep(100);
  384. if (hw->phy.type == ixgbe_phy_x550em_ext_t) {
  385. status = hw->phy.ops.read_reg(hw,
  386. IXGBE_MDIO_TX_VENDOR_ALARMS_3,
  387. MDIO_MMD_PMAPMD, &ctrl);
  388. if (status)
  389. return status;
  390. if (ctrl & IXGBE_MDIO_TX_VENDOR_ALARMS_3_RST_MASK) {
  391. udelay(2);
  392. break;
  393. }
  394. } else {
  395. status = hw->phy.ops.read_reg(hw, MDIO_CTRL1,
  396. MDIO_MMD_PHYXS, &ctrl);
  397. if (status)
  398. return status;
  399. if (!(ctrl & MDIO_CTRL1_RESET)) {
  400. udelay(2);
  401. break;
  402. }
  403. }
  404. }
  405. if (ctrl & MDIO_CTRL1_RESET) {
  406. hw_dbg(hw, "PHY reset polling failed to complete.\n");
  407. return IXGBE_ERR_RESET_FAILED;
  408. }
  409. return 0;
  410. }
  411. /**
  412. * ixgbe_read_phy_mdi - Reads a value from a specified PHY register without
  413. * the SWFW lock
  414. * @hw: pointer to hardware structure
  415. * @reg_addr: 32 bit address of PHY register to read
  416. * @device_type: 5 bit device type
  417. * @phy_data: Pointer to read data from PHY register
  418. **/
  419. s32 ixgbe_read_phy_reg_mdi(struct ixgbe_hw *hw, u32 reg_addr, u32 device_type,
  420. u16 *phy_data)
  421. {
  422. u32 i, data, command;
  423. /* Setup and write the address cycle command */
  424. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  425. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  426. (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  427. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  428. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  429. /* Check every 10 usec to see if the address cycle completed.
  430. * The MDI Command bit will clear when the operation is
  431. * complete
  432. */
  433. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  434. udelay(10);
  435. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  436. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  437. break;
  438. }
  439. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  440. hw_dbg(hw, "PHY address command did not complete.\n");
  441. return IXGBE_ERR_PHY;
  442. }
  443. /* Address cycle complete, setup and write the read
  444. * command
  445. */
  446. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  447. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  448. (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  449. (IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND));
  450. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  451. /* Check every 10 usec to see if the address cycle
  452. * completed. The MDI Command bit will clear when the
  453. * operation is complete
  454. */
  455. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  456. udelay(10);
  457. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  458. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  459. break;
  460. }
  461. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  462. hw_dbg(hw, "PHY read command didn't complete\n");
  463. return IXGBE_ERR_PHY;
  464. }
  465. /* Read operation is complete. Get the data
  466. * from MSRWD
  467. */
  468. data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
  469. data >>= IXGBE_MSRWD_READ_DATA_SHIFT;
  470. *phy_data = (u16)(data);
  471. return 0;
  472. }
  473. /**
  474. * ixgbe_read_phy_reg_generic - Reads a value from a specified PHY register
  475. * using the SWFW lock - this function is needed in most cases
  476. * @hw: pointer to hardware structure
  477. * @reg_addr: 32 bit address of PHY register to read
  478. * @device_type: 5 bit device type
  479. * @phy_data: Pointer to read data from PHY register
  480. **/
  481. s32 ixgbe_read_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
  482. u32 device_type, u16 *phy_data)
  483. {
  484. s32 status;
  485. u32 gssr = hw->phy.phy_semaphore_mask;
  486. if (hw->mac.ops.acquire_swfw_sync(hw, gssr) == 0) {
  487. status = ixgbe_read_phy_reg_mdi(hw, reg_addr, device_type,
  488. phy_data);
  489. hw->mac.ops.release_swfw_sync(hw, gssr);
  490. } else {
  491. return IXGBE_ERR_SWFW_SYNC;
  492. }
  493. return status;
  494. }
  495. /**
  496. * ixgbe_write_phy_reg_mdi - Writes a value to specified PHY register
  497. * without SWFW lock
  498. * @hw: pointer to hardware structure
  499. * @reg_addr: 32 bit PHY register to write
  500. * @device_type: 5 bit device type
  501. * @phy_data: Data to write to the PHY register
  502. **/
  503. s32 ixgbe_write_phy_reg_mdi(struct ixgbe_hw *hw, u32 reg_addr,
  504. u32 device_type, u16 phy_data)
  505. {
  506. u32 i, command;
  507. /* Put the data in the MDI single read and write data register*/
  508. IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)phy_data);
  509. /* Setup and write the address cycle command */
  510. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  511. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  512. (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  513. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  514. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  515. /*
  516. * Check every 10 usec to see if the address cycle completed.
  517. * The MDI Command bit will clear when the operation is
  518. * complete
  519. */
  520. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  521. udelay(10);
  522. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  523. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  524. break;
  525. }
  526. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  527. hw_dbg(hw, "PHY address cmd didn't complete\n");
  528. return IXGBE_ERR_PHY;
  529. }
  530. /*
  531. * Address cycle complete, setup and write the write
  532. * command
  533. */
  534. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  535. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  536. (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  537. (IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND));
  538. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  539. /* Check every 10 usec to see if the address cycle
  540. * completed. The MDI Command bit will clear when the
  541. * operation is complete
  542. */
  543. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  544. udelay(10);
  545. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  546. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  547. break;
  548. }
  549. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  550. hw_dbg(hw, "PHY write cmd didn't complete\n");
  551. return IXGBE_ERR_PHY;
  552. }
  553. return 0;
  554. }
  555. /**
  556. * ixgbe_write_phy_reg_generic - Writes a value to specified PHY register
  557. * using SWFW lock- this function is needed in most cases
  558. * @hw: pointer to hardware structure
  559. * @reg_addr: 32 bit PHY register to write
  560. * @device_type: 5 bit device type
  561. * @phy_data: Data to write to the PHY register
  562. **/
  563. s32 ixgbe_write_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
  564. u32 device_type, u16 phy_data)
  565. {
  566. s32 status;
  567. u32 gssr = hw->phy.phy_semaphore_mask;
  568. if (hw->mac.ops.acquire_swfw_sync(hw, gssr) == 0) {
  569. status = ixgbe_write_phy_reg_mdi(hw, reg_addr, device_type,
  570. phy_data);
  571. hw->mac.ops.release_swfw_sync(hw, gssr);
  572. } else {
  573. return IXGBE_ERR_SWFW_SYNC;
  574. }
  575. return status;
  576. }
  577. #define IXGBE_HW_READ_REG(addr) IXGBE_READ_REG(hw, addr)
  578. /**
  579. * ixgbe_msca_cmd - Write the command register and poll for completion/timeout
  580. * @hw: pointer to hardware structure
  581. * @cmd: command register value to write
  582. **/
  583. static s32 ixgbe_msca_cmd(struct ixgbe_hw *hw, u32 cmd)
  584. {
  585. IXGBE_WRITE_REG(hw, IXGBE_MSCA, cmd);
  586. return readx_poll_timeout(IXGBE_HW_READ_REG, IXGBE_MSCA, cmd,
  587. !(cmd & IXGBE_MSCA_MDI_COMMAND), 10,
  588. 10 * IXGBE_MDIO_COMMAND_TIMEOUT);
  589. }
  590. /**
  591. * ixgbe_mii_bus_read_generic - Read a clause 22/45 register with gssr flags
  592. * @hw: pointer to hardware structure
  593. * @addr: address
  594. * @regnum: register number
  595. * @gssr: semaphore flags to acquire
  596. **/
  597. static s32 ixgbe_mii_bus_read_generic(struct ixgbe_hw *hw, int addr,
  598. int regnum, u32 gssr)
  599. {
  600. u32 hwaddr, cmd;
  601. s32 data;
  602. if (hw->mac.ops.acquire_swfw_sync(hw, gssr))
  603. return -EBUSY;
  604. hwaddr = addr << IXGBE_MSCA_PHY_ADDR_SHIFT;
  605. if (regnum & MII_ADDR_C45) {
  606. hwaddr |= regnum & GENMASK(21, 0);
  607. cmd = hwaddr | IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND;
  608. } else {
  609. hwaddr |= (regnum & GENMASK(5, 0)) << IXGBE_MSCA_DEV_TYPE_SHIFT;
  610. cmd = hwaddr | IXGBE_MSCA_OLD_PROTOCOL |
  611. IXGBE_MSCA_READ_AUTOINC | IXGBE_MSCA_MDI_COMMAND;
  612. }
  613. data = ixgbe_msca_cmd(hw, cmd);
  614. if (data < 0)
  615. goto mii_bus_read_done;
  616. /* For a clause 45 access the address cycle just completed, we still
  617. * need to do the read command, otherwise just get the data
  618. */
  619. if (!(regnum & MII_ADDR_C45))
  620. goto do_mii_bus_read;
  621. cmd = hwaddr | IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND;
  622. data = ixgbe_msca_cmd(hw, cmd);
  623. if (data < 0)
  624. goto mii_bus_read_done;
  625. do_mii_bus_read:
  626. data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
  627. data = (data >> IXGBE_MSRWD_READ_DATA_SHIFT) & GENMASK(16, 0);
  628. mii_bus_read_done:
  629. hw->mac.ops.release_swfw_sync(hw, gssr);
  630. return data;
  631. }
  632. /**
  633. * ixgbe_mii_bus_write_generic - Write a clause 22/45 register with gssr flags
  634. * @hw: pointer to hardware structure
  635. * @addr: address
  636. * @regnum: register number
  637. * @val: value to write
  638. * @gssr: semaphore flags to acquire
  639. **/
  640. static s32 ixgbe_mii_bus_write_generic(struct ixgbe_hw *hw, int addr,
  641. int regnum, u16 val, u32 gssr)
  642. {
  643. u32 hwaddr, cmd;
  644. s32 err;
  645. if (hw->mac.ops.acquire_swfw_sync(hw, gssr))
  646. return -EBUSY;
  647. IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)val);
  648. hwaddr = addr << IXGBE_MSCA_PHY_ADDR_SHIFT;
  649. if (regnum & MII_ADDR_C45) {
  650. hwaddr |= regnum & GENMASK(21, 0);
  651. cmd = hwaddr | IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND;
  652. } else {
  653. hwaddr |= (regnum & GENMASK(5, 0)) << IXGBE_MSCA_DEV_TYPE_SHIFT;
  654. cmd = hwaddr | IXGBE_MSCA_OLD_PROTOCOL | IXGBE_MSCA_WRITE |
  655. IXGBE_MSCA_MDI_COMMAND;
  656. }
  657. /* For clause 45 this is an address cycle, for clause 22 this is the
  658. * entire transaction
  659. */
  660. err = ixgbe_msca_cmd(hw, cmd);
  661. if (err < 0 || !(regnum & MII_ADDR_C45))
  662. goto mii_bus_write_done;
  663. cmd = hwaddr | IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND;
  664. err = ixgbe_msca_cmd(hw, cmd);
  665. mii_bus_write_done:
  666. hw->mac.ops.release_swfw_sync(hw, gssr);
  667. return err;
  668. }
  669. /**
  670. * ixgbe_mii_bus_read - Read a clause 22/45 register
  671. * @hw: pointer to hardware structure
  672. * @addr: address
  673. * @regnum: register number
  674. **/
  675. static s32 ixgbe_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
  676. {
  677. struct ixgbe_adapter *adapter = bus->priv;
  678. struct ixgbe_hw *hw = &adapter->hw;
  679. u32 gssr = hw->phy.phy_semaphore_mask;
  680. return ixgbe_mii_bus_read_generic(hw, addr, regnum, gssr);
  681. }
  682. /**
  683. * ixgbe_mii_bus_write - Write a clause 22/45 register
  684. * @hw: pointer to hardware structure
  685. * @addr: address
  686. * @regnum: register number
  687. * @val: value to write
  688. **/
  689. static s32 ixgbe_mii_bus_write(struct mii_bus *bus, int addr, int regnum,
  690. u16 val)
  691. {
  692. struct ixgbe_adapter *adapter = bus->priv;
  693. struct ixgbe_hw *hw = &adapter->hw;
  694. u32 gssr = hw->phy.phy_semaphore_mask;
  695. return ixgbe_mii_bus_write_generic(hw, addr, regnum, val, gssr);
  696. }
  697. /**
  698. * ixgbe_x550em_a_mii_bus_read - Read a clause 22/45 register on x550em_a
  699. * @hw: pointer to hardware structure
  700. * @addr: address
  701. * @regnum: register number
  702. **/
  703. static s32 ixgbe_x550em_a_mii_bus_read(struct mii_bus *bus, int addr,
  704. int regnum)
  705. {
  706. struct ixgbe_adapter *adapter = bus->priv;
  707. struct ixgbe_hw *hw = &adapter->hw;
  708. u32 gssr = hw->phy.phy_semaphore_mask;
  709. gssr |= IXGBE_GSSR_TOKEN_SM | IXGBE_GSSR_PHY0_SM;
  710. return ixgbe_mii_bus_read_generic(hw, addr, regnum, gssr);
  711. }
  712. /**
  713. * ixgbe_x550em_a_mii_bus_write - Write a clause 22/45 register on x550em_a
  714. * @hw: pointer to hardware structure
  715. * @addr: address
  716. * @regnum: register number
  717. * @val: value to write
  718. **/
  719. static s32 ixgbe_x550em_a_mii_bus_write(struct mii_bus *bus, int addr,
  720. int regnum, u16 val)
  721. {
  722. struct ixgbe_adapter *adapter = bus->priv;
  723. struct ixgbe_hw *hw = &adapter->hw;
  724. u32 gssr = hw->phy.phy_semaphore_mask;
  725. gssr |= IXGBE_GSSR_TOKEN_SM | IXGBE_GSSR_PHY0_SM;
  726. return ixgbe_mii_bus_write_generic(hw, addr, regnum, val, gssr);
  727. }
  728. /**
  729. * ixgbe_get_first_secondary_devfn - get first device downstream of root port
  730. * @devfn: PCI_DEVFN of root port on domain 0, bus 0
  731. *
  732. * Returns pci_dev pointer to PCI_DEVFN(0, 0) on subordinate side of root
  733. * on domain 0, bus 0, devfn = 'devfn'
  734. **/
  735. static struct pci_dev *ixgbe_get_first_secondary_devfn(unsigned int devfn)
  736. {
  737. struct pci_dev *rp_pdev;
  738. int bus;
  739. rp_pdev = pci_get_domain_bus_and_slot(0, 0, devfn);
  740. if (rp_pdev && rp_pdev->subordinate) {
  741. bus = rp_pdev->subordinate->number;
  742. return pci_get_domain_bus_and_slot(0, bus, 0);
  743. }
  744. return NULL;
  745. }
  746. /**
  747. * ixgbe_x550em_a_has_mii - is this the first ixgbe x550em_a PCI function?
  748. * @hw: pointer to hardware structure
  749. *
  750. * Returns true if hw points to lowest numbered PCI B:D.F x550_em_a device in
  751. * the SoC. There are up to 4 MACs sharing a single MDIO bus on the x550em_a,
  752. * but we only want to register one MDIO bus.
  753. **/
  754. static bool ixgbe_x550em_a_has_mii(struct ixgbe_hw *hw)
  755. {
  756. struct ixgbe_adapter *adapter = hw->back;
  757. struct pci_dev *pdev = adapter->pdev;
  758. struct pci_dev *func0_pdev;
  759. /* For the C3000 family of SoCs (x550em_a) the internal ixgbe devices
  760. * are always downstream of root ports @ 0000:00:16.0 & 0000:00:17.0
  761. * It's not valid for function 0 to be disabled and function 1 is up,
  762. * so the lowest numbered ixgbe dev will be device 0 function 0 on one
  763. * of those two root ports
  764. */
  765. func0_pdev = ixgbe_get_first_secondary_devfn(PCI_DEVFN(0x16, 0));
  766. if (func0_pdev) {
  767. if (func0_pdev == pdev)
  768. return true;
  769. else
  770. return false;
  771. }
  772. func0_pdev = ixgbe_get_first_secondary_devfn(PCI_DEVFN(0x17, 0));
  773. if (func0_pdev == pdev)
  774. return true;
  775. return false;
  776. }
  777. /**
  778. * ixgbe_mii_bus_init - mii_bus structure setup
  779. * @hw: pointer to hardware structure
  780. *
  781. * Returns 0 on success, negative on failure
  782. *
  783. * ixgbe_mii_bus_init initializes a mii_bus structure in adapter
  784. **/
  785. s32 ixgbe_mii_bus_init(struct ixgbe_hw *hw)
  786. {
  787. struct ixgbe_adapter *adapter = hw->back;
  788. struct pci_dev *pdev = adapter->pdev;
  789. struct device *dev = &adapter->netdev->dev;
  790. struct mii_bus *bus;
  791. int err = -ENODEV;
  792. bus = devm_mdiobus_alloc(dev);
  793. if (!bus)
  794. return -ENOMEM;
  795. switch (hw->device_id) {
  796. /* C3000 SoCs */
  797. case IXGBE_DEV_ID_X550EM_A_KR:
  798. case IXGBE_DEV_ID_X550EM_A_KR_L:
  799. case IXGBE_DEV_ID_X550EM_A_SFP_N:
  800. case IXGBE_DEV_ID_X550EM_A_SGMII:
  801. case IXGBE_DEV_ID_X550EM_A_SGMII_L:
  802. case IXGBE_DEV_ID_X550EM_A_10G_T:
  803. case IXGBE_DEV_ID_X550EM_A_SFP:
  804. case IXGBE_DEV_ID_X550EM_A_1G_T:
  805. case IXGBE_DEV_ID_X550EM_A_1G_T_L:
  806. if (!ixgbe_x550em_a_has_mii(hw))
  807. goto ixgbe_no_mii_bus;
  808. bus->read = &ixgbe_x550em_a_mii_bus_read;
  809. bus->write = &ixgbe_x550em_a_mii_bus_write;
  810. break;
  811. default:
  812. bus->read = &ixgbe_mii_bus_read;
  813. bus->write = &ixgbe_mii_bus_write;
  814. break;
  815. }
  816. /* Use the position of the device in the PCI hierarchy as the id */
  817. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mdio-%s", ixgbe_driver_name,
  818. pci_name(pdev));
  819. bus->name = "ixgbe-mdio";
  820. bus->priv = adapter;
  821. bus->parent = dev;
  822. bus->phy_mask = GENMASK(31, 0);
  823. /* Support clause 22/45 natively. ixgbe_probe() sets MDIO_EMULATE_C22
  824. * unfortunately that causes some clause 22 frames to be sent with
  825. * clause 45 addressing. We don't want that.
  826. */
  827. hw->phy.mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22;
  828. err = mdiobus_register(bus);
  829. if (!err) {
  830. adapter->mii_bus = bus;
  831. return 0;
  832. }
  833. ixgbe_no_mii_bus:
  834. devm_mdiobus_free(dev, bus);
  835. return err;
  836. }
  837. /**
  838. * ixgbe_setup_phy_link_generic - Set and restart autoneg
  839. * @hw: pointer to hardware structure
  840. *
  841. * Restart autonegotiation and PHY and waits for completion.
  842. **/
  843. s32 ixgbe_setup_phy_link_generic(struct ixgbe_hw *hw)
  844. {
  845. s32 status = 0;
  846. u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
  847. bool autoneg = false;
  848. ixgbe_link_speed speed;
  849. ixgbe_get_copper_link_capabilities_generic(hw, &speed, &autoneg);
  850. /* Set or unset auto-negotiation 10G advertisement */
  851. hw->phy.ops.read_reg(hw, MDIO_AN_10GBT_CTRL, MDIO_MMD_AN, &autoneg_reg);
  852. autoneg_reg &= ~MDIO_AN_10GBT_CTRL_ADV10G;
  853. if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL) &&
  854. (speed & IXGBE_LINK_SPEED_10GB_FULL))
  855. autoneg_reg |= MDIO_AN_10GBT_CTRL_ADV10G;
  856. hw->phy.ops.write_reg(hw, MDIO_AN_10GBT_CTRL, MDIO_MMD_AN, autoneg_reg);
  857. hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
  858. MDIO_MMD_AN, &autoneg_reg);
  859. if (hw->mac.type == ixgbe_mac_X550) {
  860. /* Set or unset auto-negotiation 5G advertisement */
  861. autoneg_reg &= ~IXGBE_MII_5GBASE_T_ADVERTISE;
  862. if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_5GB_FULL) &&
  863. (speed & IXGBE_LINK_SPEED_5GB_FULL))
  864. autoneg_reg |= IXGBE_MII_5GBASE_T_ADVERTISE;
  865. /* Set or unset auto-negotiation 2.5G advertisement */
  866. autoneg_reg &= ~IXGBE_MII_2_5GBASE_T_ADVERTISE;
  867. if ((hw->phy.autoneg_advertised &
  868. IXGBE_LINK_SPEED_2_5GB_FULL) &&
  869. (speed & IXGBE_LINK_SPEED_2_5GB_FULL))
  870. autoneg_reg |= IXGBE_MII_2_5GBASE_T_ADVERTISE;
  871. }
  872. /* Set or unset auto-negotiation 1G advertisement */
  873. autoneg_reg &= ~IXGBE_MII_1GBASE_T_ADVERTISE;
  874. if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL) &&
  875. (speed & IXGBE_LINK_SPEED_1GB_FULL))
  876. autoneg_reg |= IXGBE_MII_1GBASE_T_ADVERTISE;
  877. hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_VENDOR_PROVISION_1_REG,
  878. MDIO_MMD_AN, autoneg_reg);
  879. /* Set or unset auto-negotiation 100M advertisement */
  880. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE, MDIO_MMD_AN, &autoneg_reg);
  881. autoneg_reg &= ~(ADVERTISE_100FULL | ADVERTISE_100HALF);
  882. if ((hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL) &&
  883. (speed & IXGBE_LINK_SPEED_100_FULL))
  884. autoneg_reg |= ADVERTISE_100FULL;
  885. hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE, MDIO_MMD_AN, autoneg_reg);
  886. /* Blocked by MNG FW so don't reset PHY */
  887. if (ixgbe_check_reset_blocked(hw))
  888. return 0;
  889. /* Restart PHY autonegotiation and wait for completion */
  890. hw->phy.ops.read_reg(hw, MDIO_CTRL1,
  891. MDIO_MMD_AN, &autoneg_reg);
  892. autoneg_reg |= MDIO_AN_CTRL1_RESTART;
  893. hw->phy.ops.write_reg(hw, MDIO_CTRL1,
  894. MDIO_MMD_AN, autoneg_reg);
  895. return status;
  896. }
  897. /**
  898. * ixgbe_setup_phy_link_speed_generic - Sets the auto advertised capabilities
  899. * @hw: pointer to hardware structure
  900. * @speed: new link speed
  901. * @autoneg_wait_to_complete: unused
  902. **/
  903. s32 ixgbe_setup_phy_link_speed_generic(struct ixgbe_hw *hw,
  904. ixgbe_link_speed speed,
  905. bool autoneg_wait_to_complete)
  906. {
  907. /* Clear autoneg_advertised and set new values based on input link
  908. * speed.
  909. */
  910. hw->phy.autoneg_advertised = 0;
  911. if (speed & IXGBE_LINK_SPEED_10GB_FULL)
  912. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
  913. if (speed & IXGBE_LINK_SPEED_5GB_FULL)
  914. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_5GB_FULL;
  915. if (speed & IXGBE_LINK_SPEED_2_5GB_FULL)
  916. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_2_5GB_FULL;
  917. if (speed & IXGBE_LINK_SPEED_1GB_FULL)
  918. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
  919. if (speed & IXGBE_LINK_SPEED_100_FULL)
  920. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_100_FULL;
  921. if (speed & IXGBE_LINK_SPEED_10_FULL)
  922. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10_FULL;
  923. /* Setup link based on the new speed settings */
  924. if (hw->phy.ops.setup_link)
  925. hw->phy.ops.setup_link(hw);
  926. return 0;
  927. }
  928. /**
  929. * ixgbe_get_copper_speeds_supported - Get copper link speed from phy
  930. * @hw: pointer to hardware structure
  931. *
  932. * Determines the supported link capabilities by reading the PHY auto
  933. * negotiation register.
  934. */
  935. static s32 ixgbe_get_copper_speeds_supported(struct ixgbe_hw *hw)
  936. {
  937. u16 speed_ability;
  938. s32 status;
  939. status = hw->phy.ops.read_reg(hw, MDIO_SPEED, MDIO_MMD_PMAPMD,
  940. &speed_ability);
  941. if (status)
  942. return status;
  943. if (speed_ability & MDIO_SPEED_10G)
  944. hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10GB_FULL;
  945. if (speed_ability & MDIO_PMA_SPEED_1000)
  946. hw->phy.speeds_supported |= IXGBE_LINK_SPEED_1GB_FULL;
  947. if (speed_ability & MDIO_PMA_SPEED_100)
  948. hw->phy.speeds_supported |= IXGBE_LINK_SPEED_100_FULL;
  949. switch (hw->mac.type) {
  950. case ixgbe_mac_X550:
  951. hw->phy.speeds_supported |= IXGBE_LINK_SPEED_2_5GB_FULL;
  952. hw->phy.speeds_supported |= IXGBE_LINK_SPEED_5GB_FULL;
  953. break;
  954. case ixgbe_mac_X550EM_x:
  955. case ixgbe_mac_x550em_a:
  956. hw->phy.speeds_supported &= ~IXGBE_LINK_SPEED_100_FULL;
  957. break;
  958. default:
  959. break;
  960. }
  961. return 0;
  962. }
  963. /**
  964. * ixgbe_get_copper_link_capabilities_generic - Determines link capabilities
  965. * @hw: pointer to hardware structure
  966. * @speed: pointer to link speed
  967. * @autoneg: boolean auto-negotiation value
  968. */
  969. s32 ixgbe_get_copper_link_capabilities_generic(struct ixgbe_hw *hw,
  970. ixgbe_link_speed *speed,
  971. bool *autoneg)
  972. {
  973. s32 status = 0;
  974. *autoneg = true;
  975. if (!hw->phy.speeds_supported)
  976. status = ixgbe_get_copper_speeds_supported(hw);
  977. *speed = hw->phy.speeds_supported;
  978. return status;
  979. }
  980. /**
  981. * ixgbe_check_phy_link_tnx - Determine link and speed status
  982. * @hw: pointer to hardware structure
  983. * @speed: link speed
  984. * @link_up: status of link
  985. *
  986. * Reads the VS1 register to determine if link is up and the current speed for
  987. * the PHY.
  988. **/
  989. s32 ixgbe_check_phy_link_tnx(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
  990. bool *link_up)
  991. {
  992. s32 status;
  993. u32 time_out;
  994. u32 max_time_out = 10;
  995. u16 phy_link = 0;
  996. u16 phy_speed = 0;
  997. u16 phy_data = 0;
  998. /* Initialize speed and link to default case */
  999. *link_up = false;
  1000. *speed = IXGBE_LINK_SPEED_10GB_FULL;
  1001. /*
  1002. * Check current speed and link status of the PHY register.
  1003. * This is a vendor specific register and may have to
  1004. * be changed for other copper PHYs.
  1005. */
  1006. for (time_out = 0; time_out < max_time_out; time_out++) {
  1007. udelay(10);
  1008. status = hw->phy.ops.read_reg(hw,
  1009. MDIO_STAT1,
  1010. MDIO_MMD_VEND1,
  1011. &phy_data);
  1012. phy_link = phy_data &
  1013. IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS;
  1014. phy_speed = phy_data &
  1015. IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS;
  1016. if (phy_link == IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS) {
  1017. *link_up = true;
  1018. if (phy_speed ==
  1019. IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS)
  1020. *speed = IXGBE_LINK_SPEED_1GB_FULL;
  1021. break;
  1022. }
  1023. }
  1024. return status;
  1025. }
  1026. /**
  1027. * ixgbe_setup_phy_link_tnx - Set and restart autoneg
  1028. * @hw: pointer to hardware structure
  1029. *
  1030. * Restart autonegotiation and PHY and waits for completion.
  1031. * This function always returns success, this is nessary since
  1032. * it is called via a function pointer that could call other
  1033. * functions that could return an error.
  1034. **/
  1035. s32 ixgbe_setup_phy_link_tnx(struct ixgbe_hw *hw)
  1036. {
  1037. u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
  1038. bool autoneg = false;
  1039. ixgbe_link_speed speed;
  1040. ixgbe_get_copper_link_capabilities_generic(hw, &speed, &autoneg);
  1041. if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
  1042. /* Set or unset auto-negotiation 10G advertisement */
  1043. hw->phy.ops.read_reg(hw, MDIO_AN_10GBT_CTRL,
  1044. MDIO_MMD_AN,
  1045. &autoneg_reg);
  1046. autoneg_reg &= ~MDIO_AN_10GBT_CTRL_ADV10G;
  1047. if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL)
  1048. autoneg_reg |= MDIO_AN_10GBT_CTRL_ADV10G;
  1049. hw->phy.ops.write_reg(hw, MDIO_AN_10GBT_CTRL,
  1050. MDIO_MMD_AN,
  1051. autoneg_reg);
  1052. }
  1053. if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
  1054. /* Set or unset auto-negotiation 1G advertisement */
  1055. hw->phy.ops.read_reg(hw, IXGBE_MII_AUTONEG_XNP_TX_REG,
  1056. MDIO_MMD_AN,
  1057. &autoneg_reg);
  1058. autoneg_reg &= ~IXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
  1059. if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL)
  1060. autoneg_reg |= IXGBE_MII_1GBASE_T_ADVERTISE_XNP_TX;
  1061. hw->phy.ops.write_reg(hw, IXGBE_MII_AUTONEG_XNP_TX_REG,
  1062. MDIO_MMD_AN,
  1063. autoneg_reg);
  1064. }
  1065. if (speed & IXGBE_LINK_SPEED_100_FULL) {
  1066. /* Set or unset auto-negotiation 100M advertisement */
  1067. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
  1068. MDIO_MMD_AN,
  1069. &autoneg_reg);
  1070. autoneg_reg &= ~(ADVERTISE_100FULL |
  1071. ADVERTISE_100HALF);
  1072. if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL)
  1073. autoneg_reg |= ADVERTISE_100FULL;
  1074. hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE,
  1075. MDIO_MMD_AN,
  1076. autoneg_reg);
  1077. }
  1078. /* Blocked by MNG FW so don't reset PHY */
  1079. if (ixgbe_check_reset_blocked(hw))
  1080. return 0;
  1081. /* Restart PHY autonegotiation and wait for completion */
  1082. hw->phy.ops.read_reg(hw, MDIO_CTRL1,
  1083. MDIO_MMD_AN, &autoneg_reg);
  1084. autoneg_reg |= MDIO_AN_CTRL1_RESTART;
  1085. hw->phy.ops.write_reg(hw, MDIO_CTRL1,
  1086. MDIO_MMD_AN, autoneg_reg);
  1087. return 0;
  1088. }
  1089. /**
  1090. * ixgbe_reset_phy_nl - Performs a PHY reset
  1091. * @hw: pointer to hardware structure
  1092. **/
  1093. s32 ixgbe_reset_phy_nl(struct ixgbe_hw *hw)
  1094. {
  1095. u16 phy_offset, control, eword, edata, block_crc;
  1096. bool end_data = false;
  1097. u16 list_offset, data_offset;
  1098. u16 phy_data = 0;
  1099. s32 ret_val;
  1100. u32 i;
  1101. /* Blocked by MNG FW so bail */
  1102. if (ixgbe_check_reset_blocked(hw))
  1103. return 0;
  1104. hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS, &phy_data);
  1105. /* reset the PHY and poll for completion */
  1106. hw->phy.ops.write_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS,
  1107. (phy_data | MDIO_CTRL1_RESET));
  1108. for (i = 0; i < 100; i++) {
  1109. hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS,
  1110. &phy_data);
  1111. if ((phy_data & MDIO_CTRL1_RESET) == 0)
  1112. break;
  1113. usleep_range(10000, 20000);
  1114. }
  1115. if ((phy_data & MDIO_CTRL1_RESET) != 0) {
  1116. hw_dbg(hw, "PHY reset did not complete.\n");
  1117. return IXGBE_ERR_PHY;
  1118. }
  1119. /* Get init offsets */
  1120. ret_val = ixgbe_get_sfp_init_sequence_offsets(hw, &list_offset,
  1121. &data_offset);
  1122. if (ret_val)
  1123. return ret_val;
  1124. ret_val = hw->eeprom.ops.read(hw, data_offset, &block_crc);
  1125. data_offset++;
  1126. while (!end_data) {
  1127. /*
  1128. * Read control word from PHY init contents offset
  1129. */
  1130. ret_val = hw->eeprom.ops.read(hw, data_offset, &eword);
  1131. if (ret_val)
  1132. goto err_eeprom;
  1133. control = (eword & IXGBE_CONTROL_MASK_NL) >>
  1134. IXGBE_CONTROL_SHIFT_NL;
  1135. edata = eword & IXGBE_DATA_MASK_NL;
  1136. switch (control) {
  1137. case IXGBE_DELAY_NL:
  1138. data_offset++;
  1139. hw_dbg(hw, "DELAY: %d MS\n", edata);
  1140. usleep_range(edata * 1000, edata * 2000);
  1141. break;
  1142. case IXGBE_DATA_NL:
  1143. hw_dbg(hw, "DATA:\n");
  1144. data_offset++;
  1145. ret_val = hw->eeprom.ops.read(hw, data_offset++,
  1146. &phy_offset);
  1147. if (ret_val)
  1148. goto err_eeprom;
  1149. for (i = 0; i < edata; i++) {
  1150. ret_val = hw->eeprom.ops.read(hw, data_offset,
  1151. &eword);
  1152. if (ret_val)
  1153. goto err_eeprom;
  1154. hw->phy.ops.write_reg(hw, phy_offset,
  1155. MDIO_MMD_PMAPMD, eword);
  1156. hw_dbg(hw, "Wrote %4.4x to %4.4x\n", eword,
  1157. phy_offset);
  1158. data_offset++;
  1159. phy_offset++;
  1160. }
  1161. break;
  1162. case IXGBE_CONTROL_NL:
  1163. data_offset++;
  1164. hw_dbg(hw, "CONTROL:\n");
  1165. if (edata == IXGBE_CONTROL_EOL_NL) {
  1166. hw_dbg(hw, "EOL\n");
  1167. end_data = true;
  1168. } else if (edata == IXGBE_CONTROL_SOL_NL) {
  1169. hw_dbg(hw, "SOL\n");
  1170. } else {
  1171. hw_dbg(hw, "Bad control value\n");
  1172. return IXGBE_ERR_PHY;
  1173. }
  1174. break;
  1175. default:
  1176. hw_dbg(hw, "Bad control type\n");
  1177. return IXGBE_ERR_PHY;
  1178. }
  1179. }
  1180. return ret_val;
  1181. err_eeprom:
  1182. hw_err(hw, "eeprom read at offset %d failed\n", data_offset);
  1183. return IXGBE_ERR_PHY;
  1184. }
  1185. /**
  1186. * ixgbe_identify_module_generic - Identifies module type
  1187. * @hw: pointer to hardware structure
  1188. *
  1189. * Determines HW type and calls appropriate function.
  1190. **/
  1191. s32 ixgbe_identify_module_generic(struct ixgbe_hw *hw)
  1192. {
  1193. switch (hw->mac.ops.get_media_type(hw)) {
  1194. case ixgbe_media_type_fiber:
  1195. return ixgbe_identify_sfp_module_generic(hw);
  1196. case ixgbe_media_type_fiber_qsfp:
  1197. return ixgbe_identify_qsfp_module_generic(hw);
  1198. default:
  1199. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  1200. return IXGBE_ERR_SFP_NOT_PRESENT;
  1201. }
  1202. return IXGBE_ERR_SFP_NOT_PRESENT;
  1203. }
  1204. /**
  1205. * ixgbe_identify_sfp_module_generic - Identifies SFP modules
  1206. * @hw: pointer to hardware structure
  1207. *
  1208. * Searches for and identifies the SFP module and assigns appropriate PHY type.
  1209. **/
  1210. s32 ixgbe_identify_sfp_module_generic(struct ixgbe_hw *hw)
  1211. {
  1212. struct ixgbe_adapter *adapter = hw->back;
  1213. s32 status;
  1214. u32 vendor_oui = 0;
  1215. enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
  1216. u8 identifier = 0;
  1217. u8 comp_codes_1g = 0;
  1218. u8 comp_codes_10g = 0;
  1219. u8 oui_bytes[3] = {0, 0, 0};
  1220. u8 cable_tech = 0;
  1221. u8 cable_spec = 0;
  1222. u16 enforce_sfp = 0;
  1223. if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber) {
  1224. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  1225. return IXGBE_ERR_SFP_NOT_PRESENT;
  1226. }
  1227. /* LAN ID is needed for sfp_type determination */
  1228. hw->mac.ops.set_lan_id(hw);
  1229. status = hw->phy.ops.read_i2c_eeprom(hw,
  1230. IXGBE_SFF_IDENTIFIER,
  1231. &identifier);
  1232. if (status)
  1233. goto err_read_i2c_eeprom;
  1234. if (identifier != IXGBE_SFF_IDENTIFIER_SFP) {
  1235. hw->phy.type = ixgbe_phy_sfp_unsupported;
  1236. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1237. }
  1238. status = hw->phy.ops.read_i2c_eeprom(hw,
  1239. IXGBE_SFF_1GBE_COMP_CODES,
  1240. &comp_codes_1g);
  1241. if (status)
  1242. goto err_read_i2c_eeprom;
  1243. status = hw->phy.ops.read_i2c_eeprom(hw,
  1244. IXGBE_SFF_10GBE_COMP_CODES,
  1245. &comp_codes_10g);
  1246. if (status)
  1247. goto err_read_i2c_eeprom;
  1248. status = hw->phy.ops.read_i2c_eeprom(hw,
  1249. IXGBE_SFF_CABLE_TECHNOLOGY,
  1250. &cable_tech);
  1251. if (status)
  1252. goto err_read_i2c_eeprom;
  1253. /* ID Module
  1254. * =========
  1255. * 0 SFP_DA_CU
  1256. * 1 SFP_SR
  1257. * 2 SFP_LR
  1258. * 3 SFP_DA_CORE0 - 82599-specific
  1259. * 4 SFP_DA_CORE1 - 82599-specific
  1260. * 5 SFP_SR/LR_CORE0 - 82599-specific
  1261. * 6 SFP_SR/LR_CORE1 - 82599-specific
  1262. * 7 SFP_act_lmt_DA_CORE0 - 82599-specific
  1263. * 8 SFP_act_lmt_DA_CORE1 - 82599-specific
  1264. * 9 SFP_1g_cu_CORE0 - 82599-specific
  1265. * 10 SFP_1g_cu_CORE1 - 82599-specific
  1266. * 11 SFP_1g_sx_CORE0 - 82599-specific
  1267. * 12 SFP_1g_sx_CORE1 - 82599-specific
  1268. */
  1269. if (hw->mac.type == ixgbe_mac_82598EB) {
  1270. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
  1271. hw->phy.sfp_type = ixgbe_sfp_type_da_cu;
  1272. else if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
  1273. hw->phy.sfp_type = ixgbe_sfp_type_sr;
  1274. else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
  1275. hw->phy.sfp_type = ixgbe_sfp_type_lr;
  1276. else
  1277. hw->phy.sfp_type = ixgbe_sfp_type_unknown;
  1278. } else {
  1279. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE) {
  1280. if (hw->bus.lan_id == 0)
  1281. hw->phy.sfp_type =
  1282. ixgbe_sfp_type_da_cu_core0;
  1283. else
  1284. hw->phy.sfp_type =
  1285. ixgbe_sfp_type_da_cu_core1;
  1286. } else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE) {
  1287. hw->phy.ops.read_i2c_eeprom(
  1288. hw, IXGBE_SFF_CABLE_SPEC_COMP,
  1289. &cable_spec);
  1290. if (cable_spec &
  1291. IXGBE_SFF_DA_SPEC_ACTIVE_LIMITING) {
  1292. if (hw->bus.lan_id == 0)
  1293. hw->phy.sfp_type =
  1294. ixgbe_sfp_type_da_act_lmt_core0;
  1295. else
  1296. hw->phy.sfp_type =
  1297. ixgbe_sfp_type_da_act_lmt_core1;
  1298. } else {
  1299. hw->phy.sfp_type =
  1300. ixgbe_sfp_type_unknown;
  1301. }
  1302. } else if (comp_codes_10g &
  1303. (IXGBE_SFF_10GBASESR_CAPABLE |
  1304. IXGBE_SFF_10GBASELR_CAPABLE)) {
  1305. if (hw->bus.lan_id == 0)
  1306. hw->phy.sfp_type =
  1307. ixgbe_sfp_type_srlr_core0;
  1308. else
  1309. hw->phy.sfp_type =
  1310. ixgbe_sfp_type_srlr_core1;
  1311. } else if (comp_codes_1g & IXGBE_SFF_1GBASET_CAPABLE) {
  1312. if (hw->bus.lan_id == 0)
  1313. hw->phy.sfp_type =
  1314. ixgbe_sfp_type_1g_cu_core0;
  1315. else
  1316. hw->phy.sfp_type =
  1317. ixgbe_sfp_type_1g_cu_core1;
  1318. } else if (comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) {
  1319. if (hw->bus.lan_id == 0)
  1320. hw->phy.sfp_type =
  1321. ixgbe_sfp_type_1g_sx_core0;
  1322. else
  1323. hw->phy.sfp_type =
  1324. ixgbe_sfp_type_1g_sx_core1;
  1325. } else if (comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) {
  1326. if (hw->bus.lan_id == 0)
  1327. hw->phy.sfp_type =
  1328. ixgbe_sfp_type_1g_lx_core0;
  1329. else
  1330. hw->phy.sfp_type =
  1331. ixgbe_sfp_type_1g_lx_core1;
  1332. } else {
  1333. hw->phy.sfp_type = ixgbe_sfp_type_unknown;
  1334. }
  1335. }
  1336. if (hw->phy.sfp_type != stored_sfp_type)
  1337. hw->phy.sfp_setup_needed = true;
  1338. /* Determine if the SFP+ PHY is dual speed or not. */
  1339. hw->phy.multispeed_fiber = false;
  1340. if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
  1341. (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
  1342. ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
  1343. (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
  1344. hw->phy.multispeed_fiber = true;
  1345. /* Determine PHY vendor */
  1346. if (hw->phy.type != ixgbe_phy_nl) {
  1347. hw->phy.id = identifier;
  1348. status = hw->phy.ops.read_i2c_eeprom(hw,
  1349. IXGBE_SFF_VENDOR_OUI_BYTE0,
  1350. &oui_bytes[0]);
  1351. if (status != 0)
  1352. goto err_read_i2c_eeprom;
  1353. status = hw->phy.ops.read_i2c_eeprom(hw,
  1354. IXGBE_SFF_VENDOR_OUI_BYTE1,
  1355. &oui_bytes[1]);
  1356. if (status != 0)
  1357. goto err_read_i2c_eeprom;
  1358. status = hw->phy.ops.read_i2c_eeprom(hw,
  1359. IXGBE_SFF_VENDOR_OUI_BYTE2,
  1360. &oui_bytes[2]);
  1361. if (status != 0)
  1362. goto err_read_i2c_eeprom;
  1363. vendor_oui =
  1364. ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
  1365. (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
  1366. (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
  1367. switch (vendor_oui) {
  1368. case IXGBE_SFF_VENDOR_OUI_TYCO:
  1369. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
  1370. hw->phy.type =
  1371. ixgbe_phy_sfp_passive_tyco;
  1372. break;
  1373. case IXGBE_SFF_VENDOR_OUI_FTL:
  1374. if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
  1375. hw->phy.type = ixgbe_phy_sfp_ftl_active;
  1376. else
  1377. hw->phy.type = ixgbe_phy_sfp_ftl;
  1378. break;
  1379. case IXGBE_SFF_VENDOR_OUI_AVAGO:
  1380. hw->phy.type = ixgbe_phy_sfp_avago;
  1381. break;
  1382. case IXGBE_SFF_VENDOR_OUI_INTEL:
  1383. hw->phy.type = ixgbe_phy_sfp_intel;
  1384. break;
  1385. default:
  1386. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
  1387. hw->phy.type =
  1388. ixgbe_phy_sfp_passive_unknown;
  1389. else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
  1390. hw->phy.type =
  1391. ixgbe_phy_sfp_active_unknown;
  1392. else
  1393. hw->phy.type = ixgbe_phy_sfp_unknown;
  1394. break;
  1395. }
  1396. }
  1397. /* Allow any DA cable vendor */
  1398. if (cable_tech & (IXGBE_SFF_DA_PASSIVE_CABLE |
  1399. IXGBE_SFF_DA_ACTIVE_CABLE))
  1400. return 0;
  1401. /* Verify supported 1G SFP modules */
  1402. if (comp_codes_10g == 0 &&
  1403. !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
  1404. hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
  1405. hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
  1406. hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
  1407. hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
  1408. hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1)) {
  1409. hw->phy.type = ixgbe_phy_sfp_unsupported;
  1410. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1411. }
  1412. /* Anything else 82598-based is supported */
  1413. if (hw->mac.type == ixgbe_mac_82598EB)
  1414. return 0;
  1415. hw->mac.ops.get_device_caps(hw, &enforce_sfp);
  1416. if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP) &&
  1417. !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
  1418. hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
  1419. hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
  1420. hw->phy.sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
  1421. hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core0 ||
  1422. hw->phy.sfp_type == ixgbe_sfp_type_1g_sx_core1)) {
  1423. /* Make sure we're a supported PHY type */
  1424. if (hw->phy.type == ixgbe_phy_sfp_intel)
  1425. return 0;
  1426. if (hw->allow_unsupported_sfp) {
  1427. e_warn(drv, "WARNING: Intel (R) Network Connections are quality tested using Intel (R) Ethernet Optics. Using untested modules is not supported and may cause unstable operation or damage to the module or the adapter. Intel Corporation is not responsible for any harm caused by using untested modules.\n");
  1428. return 0;
  1429. }
  1430. hw_dbg(hw, "SFP+ module not supported\n");
  1431. hw->phy.type = ixgbe_phy_sfp_unsupported;
  1432. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1433. }
  1434. return 0;
  1435. err_read_i2c_eeprom:
  1436. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  1437. if (hw->phy.type != ixgbe_phy_nl) {
  1438. hw->phy.id = 0;
  1439. hw->phy.type = ixgbe_phy_unknown;
  1440. }
  1441. return IXGBE_ERR_SFP_NOT_PRESENT;
  1442. }
  1443. /**
  1444. * ixgbe_identify_qsfp_module_generic - Identifies QSFP modules
  1445. * @hw: pointer to hardware structure
  1446. *
  1447. * Searches for and identifies the QSFP module and assigns appropriate PHY type
  1448. **/
  1449. static s32 ixgbe_identify_qsfp_module_generic(struct ixgbe_hw *hw)
  1450. {
  1451. struct ixgbe_adapter *adapter = hw->back;
  1452. s32 status;
  1453. u32 vendor_oui = 0;
  1454. enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
  1455. u8 identifier = 0;
  1456. u8 comp_codes_1g = 0;
  1457. u8 comp_codes_10g = 0;
  1458. u8 oui_bytes[3] = {0, 0, 0};
  1459. u16 enforce_sfp = 0;
  1460. u8 connector = 0;
  1461. u8 cable_length = 0;
  1462. u8 device_tech = 0;
  1463. bool active_cable = false;
  1464. if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber_qsfp) {
  1465. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  1466. return IXGBE_ERR_SFP_NOT_PRESENT;
  1467. }
  1468. /* LAN ID is needed for sfp_type determination */
  1469. hw->mac.ops.set_lan_id(hw);
  1470. status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_IDENTIFIER,
  1471. &identifier);
  1472. if (status != 0)
  1473. goto err_read_i2c_eeprom;
  1474. if (identifier != IXGBE_SFF_IDENTIFIER_QSFP_PLUS) {
  1475. hw->phy.type = ixgbe_phy_sfp_unsupported;
  1476. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1477. }
  1478. hw->phy.id = identifier;
  1479. status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_QSFP_10GBE_COMP,
  1480. &comp_codes_10g);
  1481. if (status != 0)
  1482. goto err_read_i2c_eeprom;
  1483. status = hw->phy.ops.read_i2c_eeprom(hw, IXGBE_SFF_QSFP_1GBE_COMP,
  1484. &comp_codes_1g);
  1485. if (status != 0)
  1486. goto err_read_i2c_eeprom;
  1487. if (comp_codes_10g & IXGBE_SFF_QSFP_DA_PASSIVE_CABLE) {
  1488. hw->phy.type = ixgbe_phy_qsfp_passive_unknown;
  1489. if (hw->bus.lan_id == 0)
  1490. hw->phy.sfp_type = ixgbe_sfp_type_da_cu_core0;
  1491. else
  1492. hw->phy.sfp_type = ixgbe_sfp_type_da_cu_core1;
  1493. } else if (comp_codes_10g & (IXGBE_SFF_10GBASESR_CAPABLE |
  1494. IXGBE_SFF_10GBASELR_CAPABLE)) {
  1495. if (hw->bus.lan_id == 0)
  1496. hw->phy.sfp_type = ixgbe_sfp_type_srlr_core0;
  1497. else
  1498. hw->phy.sfp_type = ixgbe_sfp_type_srlr_core1;
  1499. } else {
  1500. if (comp_codes_10g & IXGBE_SFF_QSFP_DA_ACTIVE_CABLE)
  1501. active_cable = true;
  1502. if (!active_cable) {
  1503. /* check for active DA cables that pre-date
  1504. * SFF-8436 v3.6
  1505. */
  1506. hw->phy.ops.read_i2c_eeprom(hw,
  1507. IXGBE_SFF_QSFP_CONNECTOR,
  1508. &connector);
  1509. hw->phy.ops.read_i2c_eeprom(hw,
  1510. IXGBE_SFF_QSFP_CABLE_LENGTH,
  1511. &cable_length);
  1512. hw->phy.ops.read_i2c_eeprom(hw,
  1513. IXGBE_SFF_QSFP_DEVICE_TECH,
  1514. &device_tech);
  1515. if ((connector ==
  1516. IXGBE_SFF_QSFP_CONNECTOR_NOT_SEPARABLE) &&
  1517. (cable_length > 0) &&
  1518. ((device_tech >> 4) ==
  1519. IXGBE_SFF_QSFP_TRANSMITER_850NM_VCSEL))
  1520. active_cable = true;
  1521. }
  1522. if (active_cable) {
  1523. hw->phy.type = ixgbe_phy_qsfp_active_unknown;
  1524. if (hw->bus.lan_id == 0)
  1525. hw->phy.sfp_type =
  1526. ixgbe_sfp_type_da_act_lmt_core0;
  1527. else
  1528. hw->phy.sfp_type =
  1529. ixgbe_sfp_type_da_act_lmt_core1;
  1530. } else {
  1531. /* unsupported module type */
  1532. hw->phy.type = ixgbe_phy_sfp_unsupported;
  1533. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1534. }
  1535. }
  1536. if (hw->phy.sfp_type != stored_sfp_type)
  1537. hw->phy.sfp_setup_needed = true;
  1538. /* Determine if the QSFP+ PHY is dual speed or not. */
  1539. hw->phy.multispeed_fiber = false;
  1540. if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
  1541. (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
  1542. ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
  1543. (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
  1544. hw->phy.multispeed_fiber = true;
  1545. /* Determine PHY vendor for optical modules */
  1546. if (comp_codes_10g & (IXGBE_SFF_10GBASESR_CAPABLE |
  1547. IXGBE_SFF_10GBASELR_CAPABLE)) {
  1548. status = hw->phy.ops.read_i2c_eeprom(hw,
  1549. IXGBE_SFF_QSFP_VENDOR_OUI_BYTE0,
  1550. &oui_bytes[0]);
  1551. if (status != 0)
  1552. goto err_read_i2c_eeprom;
  1553. status = hw->phy.ops.read_i2c_eeprom(hw,
  1554. IXGBE_SFF_QSFP_VENDOR_OUI_BYTE1,
  1555. &oui_bytes[1]);
  1556. if (status != 0)
  1557. goto err_read_i2c_eeprom;
  1558. status = hw->phy.ops.read_i2c_eeprom(hw,
  1559. IXGBE_SFF_QSFP_VENDOR_OUI_BYTE2,
  1560. &oui_bytes[2]);
  1561. if (status != 0)
  1562. goto err_read_i2c_eeprom;
  1563. vendor_oui =
  1564. ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
  1565. (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
  1566. (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
  1567. if (vendor_oui == IXGBE_SFF_VENDOR_OUI_INTEL)
  1568. hw->phy.type = ixgbe_phy_qsfp_intel;
  1569. else
  1570. hw->phy.type = ixgbe_phy_qsfp_unknown;
  1571. hw->mac.ops.get_device_caps(hw, &enforce_sfp);
  1572. if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP)) {
  1573. /* Make sure we're a supported PHY type */
  1574. if (hw->phy.type == ixgbe_phy_qsfp_intel)
  1575. return 0;
  1576. if (hw->allow_unsupported_sfp) {
  1577. e_warn(drv, "WARNING: Intel (R) Network Connections are quality tested using Intel (R) Ethernet Optics. Using untested modules is not supported and may cause unstable operation or damage to the module or the adapter. Intel Corporation is not responsible for any harm caused by using untested modules.\n");
  1578. return 0;
  1579. }
  1580. hw_dbg(hw, "QSFP module not supported\n");
  1581. hw->phy.type = ixgbe_phy_sfp_unsupported;
  1582. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1583. }
  1584. return 0;
  1585. }
  1586. return 0;
  1587. err_read_i2c_eeprom:
  1588. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  1589. hw->phy.id = 0;
  1590. hw->phy.type = ixgbe_phy_unknown;
  1591. return IXGBE_ERR_SFP_NOT_PRESENT;
  1592. }
  1593. /**
  1594. * ixgbe_get_sfp_init_sequence_offsets - Provides offset of PHY init sequence
  1595. * @hw: pointer to hardware structure
  1596. * @list_offset: offset to the SFP ID list
  1597. * @data_offset: offset to the SFP data block
  1598. *
  1599. * Checks the MAC's EEPROM to see if it supports a given SFP+ module type, if
  1600. * so it returns the offsets to the phy init sequence block.
  1601. **/
  1602. s32 ixgbe_get_sfp_init_sequence_offsets(struct ixgbe_hw *hw,
  1603. u16 *list_offset,
  1604. u16 *data_offset)
  1605. {
  1606. u16 sfp_id;
  1607. u16 sfp_type = hw->phy.sfp_type;
  1608. if (hw->phy.sfp_type == ixgbe_sfp_type_unknown)
  1609. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1610. if (hw->phy.sfp_type == ixgbe_sfp_type_not_present)
  1611. return IXGBE_ERR_SFP_NOT_PRESENT;
  1612. if ((hw->device_id == IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM) &&
  1613. (hw->phy.sfp_type == ixgbe_sfp_type_da_cu))
  1614. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1615. /*
  1616. * Limiting active cables and 1G Phys must be initialized as
  1617. * SR modules
  1618. */
  1619. if (sfp_type == ixgbe_sfp_type_da_act_lmt_core0 ||
  1620. sfp_type == ixgbe_sfp_type_1g_lx_core0 ||
  1621. sfp_type == ixgbe_sfp_type_1g_cu_core0 ||
  1622. sfp_type == ixgbe_sfp_type_1g_sx_core0)
  1623. sfp_type = ixgbe_sfp_type_srlr_core0;
  1624. else if (sfp_type == ixgbe_sfp_type_da_act_lmt_core1 ||
  1625. sfp_type == ixgbe_sfp_type_1g_lx_core1 ||
  1626. sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
  1627. sfp_type == ixgbe_sfp_type_1g_sx_core1)
  1628. sfp_type = ixgbe_sfp_type_srlr_core1;
  1629. /* Read offset to PHY init contents */
  1630. if (hw->eeprom.ops.read(hw, IXGBE_PHY_INIT_OFFSET_NL, list_offset)) {
  1631. hw_err(hw, "eeprom read at %d failed\n",
  1632. IXGBE_PHY_INIT_OFFSET_NL);
  1633. return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
  1634. }
  1635. if ((!*list_offset) || (*list_offset == 0xFFFF))
  1636. return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
  1637. /* Shift offset to first ID word */
  1638. (*list_offset)++;
  1639. /*
  1640. * Find the matching SFP ID in the EEPROM
  1641. * and program the init sequence
  1642. */
  1643. if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
  1644. goto err_phy;
  1645. while (sfp_id != IXGBE_PHY_INIT_END_NL) {
  1646. if (sfp_id == sfp_type) {
  1647. (*list_offset)++;
  1648. if (hw->eeprom.ops.read(hw, *list_offset, data_offset))
  1649. goto err_phy;
  1650. if ((!*data_offset) || (*data_offset == 0xFFFF)) {
  1651. hw_dbg(hw, "SFP+ module not supported\n");
  1652. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1653. } else {
  1654. break;
  1655. }
  1656. } else {
  1657. (*list_offset) += 2;
  1658. if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
  1659. goto err_phy;
  1660. }
  1661. }
  1662. if (sfp_id == IXGBE_PHY_INIT_END_NL) {
  1663. hw_dbg(hw, "No matching SFP+ module found\n");
  1664. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  1665. }
  1666. return 0;
  1667. err_phy:
  1668. hw_err(hw, "eeprom read at offset %d failed\n", *list_offset);
  1669. return IXGBE_ERR_PHY;
  1670. }
  1671. /**
  1672. * ixgbe_read_i2c_eeprom_generic - Reads 8 bit EEPROM word over I2C interface
  1673. * @hw: pointer to hardware structure
  1674. * @byte_offset: EEPROM byte offset to read
  1675. * @eeprom_data: value read
  1676. *
  1677. * Performs byte read operation to SFP module's EEPROM over I2C interface.
  1678. **/
  1679. s32 ixgbe_read_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
  1680. u8 *eeprom_data)
  1681. {
  1682. return hw->phy.ops.read_i2c_byte(hw, byte_offset,
  1683. IXGBE_I2C_EEPROM_DEV_ADDR,
  1684. eeprom_data);
  1685. }
  1686. /**
  1687. * ixgbe_read_i2c_sff8472_generic - Reads 8 bit word over I2C interface
  1688. * @hw: pointer to hardware structure
  1689. * @byte_offset: byte offset at address 0xA2
  1690. * @sff8472_data: value read
  1691. *
  1692. * Performs byte read operation to SFP module's SFF-8472 data over I2C
  1693. **/
  1694. s32 ixgbe_read_i2c_sff8472_generic(struct ixgbe_hw *hw, u8 byte_offset,
  1695. u8 *sff8472_data)
  1696. {
  1697. return hw->phy.ops.read_i2c_byte(hw, byte_offset,
  1698. IXGBE_I2C_EEPROM_DEV_ADDR2,
  1699. sff8472_data);
  1700. }
  1701. /**
  1702. * ixgbe_write_i2c_eeprom_generic - Writes 8 bit EEPROM word over I2C interface
  1703. * @hw: pointer to hardware structure
  1704. * @byte_offset: EEPROM byte offset to write
  1705. * @eeprom_data: value to write
  1706. *
  1707. * Performs byte write operation to SFP module's EEPROM over I2C interface.
  1708. **/
  1709. s32 ixgbe_write_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
  1710. u8 eeprom_data)
  1711. {
  1712. return hw->phy.ops.write_i2c_byte(hw, byte_offset,
  1713. IXGBE_I2C_EEPROM_DEV_ADDR,
  1714. eeprom_data);
  1715. }
  1716. /**
  1717. * ixgbe_is_sfp_probe - Returns true if SFP is being detected
  1718. * @hw: pointer to hardware structure
  1719. * @offset: eeprom offset to be read
  1720. * @addr: I2C address to be read
  1721. */
  1722. static bool ixgbe_is_sfp_probe(struct ixgbe_hw *hw, u8 offset, u8 addr)
  1723. {
  1724. if (addr == IXGBE_I2C_EEPROM_DEV_ADDR &&
  1725. offset == IXGBE_SFF_IDENTIFIER &&
  1726. hw->phy.sfp_type == ixgbe_sfp_type_not_present)
  1727. return true;
  1728. return false;
  1729. }
  1730. /**
  1731. * ixgbe_read_i2c_byte_generic_int - Reads 8 bit word over I2C
  1732. * @hw: pointer to hardware structure
  1733. * @byte_offset: byte offset to read
  1734. * @dev_addr: device address
  1735. * @data: value read
  1736. * @lock: true if to take and release semaphore
  1737. *
  1738. * Performs byte read operation to SFP module's EEPROM over I2C interface at
  1739. * a specified device address.
  1740. */
  1741. static s32 ixgbe_read_i2c_byte_generic_int(struct ixgbe_hw *hw, u8 byte_offset,
  1742. u8 dev_addr, u8 *data, bool lock)
  1743. {
  1744. s32 status;
  1745. u32 max_retry = 10;
  1746. u32 retry = 0;
  1747. u32 swfw_mask = hw->phy.phy_semaphore_mask;
  1748. bool nack = true;
  1749. if (hw->mac.type >= ixgbe_mac_X550)
  1750. max_retry = 3;
  1751. if (ixgbe_is_sfp_probe(hw, byte_offset, dev_addr))
  1752. max_retry = IXGBE_SFP_DETECT_RETRIES;
  1753. *data = 0;
  1754. do {
  1755. if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
  1756. return IXGBE_ERR_SWFW_SYNC;
  1757. ixgbe_i2c_start(hw);
  1758. /* Device Address and write indication */
  1759. status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
  1760. if (status != 0)
  1761. goto fail;
  1762. status = ixgbe_get_i2c_ack(hw);
  1763. if (status != 0)
  1764. goto fail;
  1765. status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
  1766. if (status != 0)
  1767. goto fail;
  1768. status = ixgbe_get_i2c_ack(hw);
  1769. if (status != 0)
  1770. goto fail;
  1771. ixgbe_i2c_start(hw);
  1772. /* Device Address and read indication */
  1773. status = ixgbe_clock_out_i2c_byte(hw, (dev_addr | 0x1));
  1774. if (status != 0)
  1775. goto fail;
  1776. status = ixgbe_get_i2c_ack(hw);
  1777. if (status != 0)
  1778. goto fail;
  1779. status = ixgbe_clock_in_i2c_byte(hw, data);
  1780. if (status != 0)
  1781. goto fail;
  1782. status = ixgbe_clock_out_i2c_bit(hw, nack);
  1783. if (status != 0)
  1784. goto fail;
  1785. ixgbe_i2c_stop(hw);
  1786. if (lock)
  1787. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  1788. return 0;
  1789. fail:
  1790. ixgbe_i2c_bus_clear(hw);
  1791. if (lock) {
  1792. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  1793. msleep(100);
  1794. }
  1795. retry++;
  1796. if (retry < max_retry)
  1797. hw_dbg(hw, "I2C byte read error - Retrying.\n");
  1798. else
  1799. hw_dbg(hw, "I2C byte read error.\n");
  1800. } while (retry < max_retry);
  1801. return status;
  1802. }
  1803. /**
  1804. * ixgbe_read_i2c_byte_generic - Reads 8 bit word over I2C
  1805. * @hw: pointer to hardware structure
  1806. * @byte_offset: byte offset to read
  1807. * @dev_addr: device address
  1808. * @data: value read
  1809. *
  1810. * Performs byte read operation to SFP module's EEPROM over I2C interface at
  1811. * a specified device address.
  1812. */
  1813. s32 ixgbe_read_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
  1814. u8 dev_addr, u8 *data)
  1815. {
  1816. return ixgbe_read_i2c_byte_generic_int(hw, byte_offset, dev_addr,
  1817. data, true);
  1818. }
  1819. /**
  1820. * ixgbe_read_i2c_byte_generic_unlocked - Reads 8 bit word over I2C
  1821. * @hw: pointer to hardware structure
  1822. * @byte_offset: byte offset to read
  1823. * @dev_addr: device address
  1824. * @data: value read
  1825. *
  1826. * Performs byte read operation to SFP module's EEPROM over I2C interface at
  1827. * a specified device address.
  1828. */
  1829. s32 ixgbe_read_i2c_byte_generic_unlocked(struct ixgbe_hw *hw, u8 byte_offset,
  1830. u8 dev_addr, u8 *data)
  1831. {
  1832. return ixgbe_read_i2c_byte_generic_int(hw, byte_offset, dev_addr,
  1833. data, false);
  1834. }
  1835. /**
  1836. * ixgbe_write_i2c_byte_generic_int - Writes 8 bit word over I2C
  1837. * @hw: pointer to hardware structure
  1838. * @byte_offset: byte offset to write
  1839. * @dev_addr: device address
  1840. * @data: value to write
  1841. * @lock: true if to take and release semaphore
  1842. *
  1843. * Performs byte write operation to SFP module's EEPROM over I2C interface at
  1844. * a specified device address.
  1845. */
  1846. static s32 ixgbe_write_i2c_byte_generic_int(struct ixgbe_hw *hw, u8 byte_offset,
  1847. u8 dev_addr, u8 data, bool lock)
  1848. {
  1849. s32 status;
  1850. u32 max_retry = 1;
  1851. u32 retry = 0;
  1852. u32 swfw_mask = hw->phy.phy_semaphore_mask;
  1853. if (lock && hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
  1854. return IXGBE_ERR_SWFW_SYNC;
  1855. do {
  1856. ixgbe_i2c_start(hw);
  1857. status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
  1858. if (status != 0)
  1859. goto fail;
  1860. status = ixgbe_get_i2c_ack(hw);
  1861. if (status != 0)
  1862. goto fail;
  1863. status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
  1864. if (status != 0)
  1865. goto fail;
  1866. status = ixgbe_get_i2c_ack(hw);
  1867. if (status != 0)
  1868. goto fail;
  1869. status = ixgbe_clock_out_i2c_byte(hw, data);
  1870. if (status != 0)
  1871. goto fail;
  1872. status = ixgbe_get_i2c_ack(hw);
  1873. if (status != 0)
  1874. goto fail;
  1875. ixgbe_i2c_stop(hw);
  1876. if (lock)
  1877. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  1878. return 0;
  1879. fail:
  1880. ixgbe_i2c_bus_clear(hw);
  1881. retry++;
  1882. if (retry < max_retry)
  1883. hw_dbg(hw, "I2C byte write error - Retrying.\n");
  1884. else
  1885. hw_dbg(hw, "I2C byte write error.\n");
  1886. } while (retry < max_retry);
  1887. if (lock)
  1888. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  1889. return status;
  1890. }
  1891. /**
  1892. * ixgbe_write_i2c_byte_generic - Writes 8 bit word over I2C
  1893. * @hw: pointer to hardware structure
  1894. * @byte_offset: byte offset to write
  1895. * @dev_addr: device address
  1896. * @data: value to write
  1897. *
  1898. * Performs byte write operation to SFP module's EEPROM over I2C interface at
  1899. * a specified device address.
  1900. */
  1901. s32 ixgbe_write_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
  1902. u8 dev_addr, u8 data)
  1903. {
  1904. return ixgbe_write_i2c_byte_generic_int(hw, byte_offset, dev_addr,
  1905. data, true);
  1906. }
  1907. /**
  1908. * ixgbe_write_i2c_byte_generic_unlocked - Writes 8 bit word over I2C
  1909. * @hw: pointer to hardware structure
  1910. * @byte_offset: byte offset to write
  1911. * @dev_addr: device address
  1912. * @data: value to write
  1913. *
  1914. * Performs byte write operation to SFP module's EEPROM over I2C interface at
  1915. * a specified device address.
  1916. */
  1917. s32 ixgbe_write_i2c_byte_generic_unlocked(struct ixgbe_hw *hw, u8 byte_offset,
  1918. u8 dev_addr, u8 data)
  1919. {
  1920. return ixgbe_write_i2c_byte_generic_int(hw, byte_offset, dev_addr,
  1921. data, false);
  1922. }
  1923. /**
  1924. * ixgbe_i2c_start - Sets I2C start condition
  1925. * @hw: pointer to hardware structure
  1926. *
  1927. * Sets I2C start condition (High -> Low on SDA while SCL is High)
  1928. * Set bit-bang mode on X550 hardware.
  1929. **/
  1930. static void ixgbe_i2c_start(struct ixgbe_hw *hw)
  1931. {
  1932. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  1933. i2cctl |= IXGBE_I2C_BB_EN(hw);
  1934. /* Start condition must begin with data and clock high */
  1935. ixgbe_set_i2c_data(hw, &i2cctl, 1);
  1936. ixgbe_raise_i2c_clk(hw, &i2cctl);
  1937. /* Setup time for start condition (4.7us) */
  1938. udelay(IXGBE_I2C_T_SU_STA);
  1939. ixgbe_set_i2c_data(hw, &i2cctl, 0);
  1940. /* Hold time for start condition (4us) */
  1941. udelay(IXGBE_I2C_T_HD_STA);
  1942. ixgbe_lower_i2c_clk(hw, &i2cctl);
  1943. /* Minimum low period of clock is 4.7 us */
  1944. udelay(IXGBE_I2C_T_LOW);
  1945. }
  1946. /**
  1947. * ixgbe_i2c_stop - Sets I2C stop condition
  1948. * @hw: pointer to hardware structure
  1949. *
  1950. * Sets I2C stop condition (Low -> High on SDA while SCL is High)
  1951. * Disables bit-bang mode and negates data output enable on X550
  1952. * hardware.
  1953. **/
  1954. static void ixgbe_i2c_stop(struct ixgbe_hw *hw)
  1955. {
  1956. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  1957. u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
  1958. u32 clk_oe_bit = IXGBE_I2C_CLK_OE_N_EN(hw);
  1959. u32 bb_en_bit = IXGBE_I2C_BB_EN(hw);
  1960. /* Stop condition must begin with data low and clock high */
  1961. ixgbe_set_i2c_data(hw, &i2cctl, 0);
  1962. ixgbe_raise_i2c_clk(hw, &i2cctl);
  1963. /* Setup time for stop condition (4us) */
  1964. udelay(IXGBE_I2C_T_SU_STO);
  1965. ixgbe_set_i2c_data(hw, &i2cctl, 1);
  1966. /* bus free time between stop and start (4.7us)*/
  1967. udelay(IXGBE_I2C_T_BUF);
  1968. if (bb_en_bit || data_oe_bit || clk_oe_bit) {
  1969. i2cctl &= ~bb_en_bit;
  1970. i2cctl |= data_oe_bit | clk_oe_bit;
  1971. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
  1972. IXGBE_WRITE_FLUSH(hw);
  1973. }
  1974. }
  1975. /**
  1976. * ixgbe_clock_in_i2c_byte - Clocks in one byte via I2C
  1977. * @hw: pointer to hardware structure
  1978. * @data: data byte to clock in
  1979. *
  1980. * Clocks in one byte data via I2C data/clock
  1981. **/
  1982. static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data)
  1983. {
  1984. s32 i;
  1985. bool bit = false;
  1986. *data = 0;
  1987. for (i = 7; i >= 0; i--) {
  1988. ixgbe_clock_in_i2c_bit(hw, &bit);
  1989. *data |= bit << i;
  1990. }
  1991. return 0;
  1992. }
  1993. /**
  1994. * ixgbe_clock_out_i2c_byte - Clocks out one byte via I2C
  1995. * @hw: pointer to hardware structure
  1996. * @data: data byte clocked out
  1997. *
  1998. * Clocks out one byte data via I2C data/clock
  1999. **/
  2000. static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data)
  2001. {
  2002. s32 status;
  2003. s32 i;
  2004. u32 i2cctl;
  2005. bool bit = false;
  2006. for (i = 7; i >= 0; i--) {
  2007. bit = (data >> i) & 0x1;
  2008. status = ixgbe_clock_out_i2c_bit(hw, bit);
  2009. if (status != 0)
  2010. break;
  2011. }
  2012. /* Release SDA line (set high) */
  2013. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2014. i2cctl |= IXGBE_I2C_DATA_OUT(hw);
  2015. i2cctl |= IXGBE_I2C_DATA_OE_N_EN(hw);
  2016. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
  2017. IXGBE_WRITE_FLUSH(hw);
  2018. return status;
  2019. }
  2020. /**
  2021. * ixgbe_get_i2c_ack - Polls for I2C ACK
  2022. * @hw: pointer to hardware structure
  2023. *
  2024. * Clocks in/out one bit via I2C data/clock
  2025. **/
  2026. static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw)
  2027. {
  2028. u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
  2029. s32 status = 0;
  2030. u32 i = 0;
  2031. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2032. u32 timeout = 10;
  2033. bool ack = true;
  2034. if (data_oe_bit) {
  2035. i2cctl |= IXGBE_I2C_DATA_OUT(hw);
  2036. i2cctl |= data_oe_bit;
  2037. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
  2038. IXGBE_WRITE_FLUSH(hw);
  2039. }
  2040. ixgbe_raise_i2c_clk(hw, &i2cctl);
  2041. /* Minimum high period of clock is 4us */
  2042. udelay(IXGBE_I2C_T_HIGH);
  2043. /* Poll for ACK. Note that ACK in I2C spec is
  2044. * transition from 1 to 0 */
  2045. for (i = 0; i < timeout; i++) {
  2046. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2047. ack = ixgbe_get_i2c_data(hw, &i2cctl);
  2048. udelay(1);
  2049. if (ack == 0)
  2050. break;
  2051. }
  2052. if (ack == 1) {
  2053. hw_dbg(hw, "I2C ack was not received.\n");
  2054. status = IXGBE_ERR_I2C;
  2055. }
  2056. ixgbe_lower_i2c_clk(hw, &i2cctl);
  2057. /* Minimum low period of clock is 4.7 us */
  2058. udelay(IXGBE_I2C_T_LOW);
  2059. return status;
  2060. }
  2061. /**
  2062. * ixgbe_clock_in_i2c_bit - Clocks in one bit via I2C data/clock
  2063. * @hw: pointer to hardware structure
  2064. * @data: read data value
  2065. *
  2066. * Clocks in one bit via I2C data/clock
  2067. **/
  2068. static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data)
  2069. {
  2070. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2071. u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
  2072. if (data_oe_bit) {
  2073. i2cctl |= IXGBE_I2C_DATA_OUT(hw);
  2074. i2cctl |= data_oe_bit;
  2075. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), i2cctl);
  2076. IXGBE_WRITE_FLUSH(hw);
  2077. }
  2078. ixgbe_raise_i2c_clk(hw, &i2cctl);
  2079. /* Minimum high period of clock is 4us */
  2080. udelay(IXGBE_I2C_T_HIGH);
  2081. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2082. *data = ixgbe_get_i2c_data(hw, &i2cctl);
  2083. ixgbe_lower_i2c_clk(hw, &i2cctl);
  2084. /* Minimum low period of clock is 4.7 us */
  2085. udelay(IXGBE_I2C_T_LOW);
  2086. return 0;
  2087. }
  2088. /**
  2089. * ixgbe_clock_out_i2c_bit - Clocks in/out one bit via I2C data/clock
  2090. * @hw: pointer to hardware structure
  2091. * @data: data value to write
  2092. *
  2093. * Clocks out one bit via I2C data/clock
  2094. **/
  2095. static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data)
  2096. {
  2097. s32 status;
  2098. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2099. status = ixgbe_set_i2c_data(hw, &i2cctl, data);
  2100. if (status == 0) {
  2101. ixgbe_raise_i2c_clk(hw, &i2cctl);
  2102. /* Minimum high period of clock is 4us */
  2103. udelay(IXGBE_I2C_T_HIGH);
  2104. ixgbe_lower_i2c_clk(hw, &i2cctl);
  2105. /* Minimum low period of clock is 4.7 us.
  2106. * This also takes care of the data hold time.
  2107. */
  2108. udelay(IXGBE_I2C_T_LOW);
  2109. } else {
  2110. hw_dbg(hw, "I2C data was not set to %X\n", data);
  2111. return IXGBE_ERR_I2C;
  2112. }
  2113. return 0;
  2114. }
  2115. /**
  2116. * ixgbe_raise_i2c_clk - Raises the I2C SCL clock
  2117. * @hw: pointer to hardware structure
  2118. * @i2cctl: Current value of I2CCTL register
  2119. *
  2120. * Raises the I2C clock line '0'->'1'
  2121. * Negates the I2C clock output enable on X550 hardware.
  2122. **/
  2123. static void ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
  2124. {
  2125. u32 clk_oe_bit = IXGBE_I2C_CLK_OE_N_EN(hw);
  2126. u32 i = 0;
  2127. u32 timeout = IXGBE_I2C_CLOCK_STRETCHING_TIMEOUT;
  2128. u32 i2cctl_r = 0;
  2129. if (clk_oe_bit) {
  2130. *i2cctl |= clk_oe_bit;
  2131. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
  2132. }
  2133. for (i = 0; i < timeout; i++) {
  2134. *i2cctl |= IXGBE_I2C_CLK_OUT(hw);
  2135. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
  2136. IXGBE_WRITE_FLUSH(hw);
  2137. /* SCL rise time (1000ns) */
  2138. udelay(IXGBE_I2C_T_RISE);
  2139. i2cctl_r = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2140. if (i2cctl_r & IXGBE_I2C_CLK_IN(hw))
  2141. break;
  2142. }
  2143. }
  2144. /**
  2145. * ixgbe_lower_i2c_clk - Lowers the I2C SCL clock
  2146. * @hw: pointer to hardware structure
  2147. * @i2cctl: Current value of I2CCTL register
  2148. *
  2149. * Lowers the I2C clock line '1'->'0'
  2150. * Asserts the I2C clock output enable on X550 hardware.
  2151. **/
  2152. static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
  2153. {
  2154. *i2cctl &= ~IXGBE_I2C_CLK_OUT(hw);
  2155. *i2cctl &= ~IXGBE_I2C_CLK_OE_N_EN(hw);
  2156. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
  2157. IXGBE_WRITE_FLUSH(hw);
  2158. /* SCL fall time (300ns) */
  2159. udelay(IXGBE_I2C_T_FALL);
  2160. }
  2161. /**
  2162. * ixgbe_set_i2c_data - Sets the I2C data bit
  2163. * @hw: pointer to hardware structure
  2164. * @i2cctl: Current value of I2CCTL register
  2165. * @data: I2C data value (0 or 1) to set
  2166. *
  2167. * Sets the I2C data bit
  2168. * Asserts the I2C data output enable on X550 hardware.
  2169. **/
  2170. static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data)
  2171. {
  2172. u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
  2173. if (data)
  2174. *i2cctl |= IXGBE_I2C_DATA_OUT(hw);
  2175. else
  2176. *i2cctl &= ~IXGBE_I2C_DATA_OUT(hw);
  2177. *i2cctl &= ~data_oe_bit;
  2178. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
  2179. IXGBE_WRITE_FLUSH(hw);
  2180. /* Data rise/fall (1000ns/300ns) and set-up time (250ns) */
  2181. udelay(IXGBE_I2C_T_RISE + IXGBE_I2C_T_FALL + IXGBE_I2C_T_SU_DATA);
  2182. if (!data) /* Can't verify data in this case */
  2183. return 0;
  2184. if (data_oe_bit) {
  2185. *i2cctl |= data_oe_bit;
  2186. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
  2187. IXGBE_WRITE_FLUSH(hw);
  2188. }
  2189. /* Verify data was set correctly */
  2190. *i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2191. if (data != ixgbe_get_i2c_data(hw, i2cctl)) {
  2192. hw_dbg(hw, "Error - I2C data was not set to %X.\n", data);
  2193. return IXGBE_ERR_I2C;
  2194. }
  2195. return 0;
  2196. }
  2197. /**
  2198. * ixgbe_get_i2c_data - Reads the I2C SDA data bit
  2199. * @hw: pointer to hardware structure
  2200. * @i2cctl: Current value of I2CCTL register
  2201. *
  2202. * Returns the I2C data bit value
  2203. * Negates the I2C data output enable on X550 hardware.
  2204. **/
  2205. static bool ixgbe_get_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl)
  2206. {
  2207. u32 data_oe_bit = IXGBE_I2C_DATA_OE_N_EN(hw);
  2208. if (data_oe_bit) {
  2209. *i2cctl |= data_oe_bit;
  2210. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL(hw), *i2cctl);
  2211. IXGBE_WRITE_FLUSH(hw);
  2212. udelay(IXGBE_I2C_T_FALL);
  2213. }
  2214. if (*i2cctl & IXGBE_I2C_DATA_IN(hw))
  2215. return true;
  2216. return false;
  2217. }
  2218. /**
  2219. * ixgbe_i2c_bus_clear - Clears the I2C bus
  2220. * @hw: pointer to hardware structure
  2221. *
  2222. * Clears the I2C bus by sending nine clock pulses.
  2223. * Used when data line is stuck low.
  2224. **/
  2225. static void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw)
  2226. {
  2227. u32 i2cctl;
  2228. u32 i;
  2229. ixgbe_i2c_start(hw);
  2230. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL(hw));
  2231. ixgbe_set_i2c_data(hw, &i2cctl, 1);
  2232. for (i = 0; i < 9; i++) {
  2233. ixgbe_raise_i2c_clk(hw, &i2cctl);
  2234. /* Min high period of clock is 4us */
  2235. udelay(IXGBE_I2C_T_HIGH);
  2236. ixgbe_lower_i2c_clk(hw, &i2cctl);
  2237. /* Min low period of clock is 4.7us*/
  2238. udelay(IXGBE_I2C_T_LOW);
  2239. }
  2240. ixgbe_i2c_start(hw);
  2241. /* Put the i2c bus back to default state */
  2242. ixgbe_i2c_stop(hw);
  2243. }
  2244. /**
  2245. * ixgbe_tn_check_overtemp - Checks if an overtemp occurred.
  2246. * @hw: pointer to hardware structure
  2247. *
  2248. * Checks if the LASI temp alarm status was triggered due to overtemp
  2249. **/
  2250. s32 ixgbe_tn_check_overtemp(struct ixgbe_hw *hw)
  2251. {
  2252. u16 phy_data = 0;
  2253. if (hw->device_id != IXGBE_DEV_ID_82599_T3_LOM)
  2254. return 0;
  2255. /* Check that the LASI temp alarm status was triggered */
  2256. hw->phy.ops.read_reg(hw, IXGBE_TN_LASI_STATUS_REG,
  2257. MDIO_MMD_PMAPMD, &phy_data);
  2258. if (!(phy_data & IXGBE_TN_LASI_STATUS_TEMP_ALARM))
  2259. return 0;
  2260. return IXGBE_ERR_OVERTEMP;
  2261. }
  2262. /** ixgbe_set_copper_phy_power - Control power for copper phy
  2263. * @hw: pointer to hardware structure
  2264. * @on: true for on, false for off
  2265. **/
  2266. s32 ixgbe_set_copper_phy_power(struct ixgbe_hw *hw, bool on)
  2267. {
  2268. u32 status;
  2269. u16 reg;
  2270. /* Bail if we don't have copper phy */
  2271. if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_copper)
  2272. return 0;
  2273. if (!on && ixgbe_mng_present(hw))
  2274. return 0;
  2275. status = hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_VEND1, &reg);
  2276. if (status)
  2277. return status;
  2278. if (on) {
  2279. reg &= ~IXGBE_MDIO_PHY_SET_LOW_POWER_MODE;
  2280. } else {
  2281. if (ixgbe_check_reset_blocked(hw))
  2282. return 0;
  2283. reg |= IXGBE_MDIO_PHY_SET_LOW_POWER_MODE;
  2284. }
  2285. status = hw->phy.ops.write_reg(hw, MDIO_CTRL1, MDIO_MMD_VEND1, reg);
  2286. return status;
  2287. }