igc_mac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018 Intel Corporation */
  3. #include <linux/pci.h>
  4. #include <linux/delay.h>
  5. #include "igc_mac.h"
  6. #include "igc_hw.h"
  7. /* forward declaration */
  8. static s32 igc_set_default_fc(struct igc_hw *hw);
  9. static s32 igc_set_fc_watermarks(struct igc_hw *hw);
  10. /**
  11. * igc_disable_pcie_master - Disables PCI-express master access
  12. * @hw: pointer to the HW structure
  13. *
  14. * Returns 0 (0) if successful, else returns -10
  15. * (-IGC_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
  16. * the master requests to be disabled.
  17. *
  18. * Disables PCI-Express master access and verifies there are no pending
  19. * requests.
  20. */
  21. s32 igc_disable_pcie_master(struct igc_hw *hw)
  22. {
  23. s32 timeout = MASTER_DISABLE_TIMEOUT;
  24. s32 ret_val = 0;
  25. u32 ctrl;
  26. ctrl = rd32(IGC_CTRL);
  27. ctrl |= IGC_CTRL_GIO_MASTER_DISABLE;
  28. wr32(IGC_CTRL, ctrl);
  29. while (timeout) {
  30. if (!(rd32(IGC_STATUS) &
  31. IGC_STATUS_GIO_MASTER_ENABLE))
  32. break;
  33. usleep_range(2000, 3000);
  34. timeout--;
  35. }
  36. if (!timeout) {
  37. hw_dbg("Master requests are pending.\n");
  38. ret_val = -IGC_ERR_MASTER_REQUESTS_PENDING;
  39. goto out;
  40. }
  41. out:
  42. return ret_val;
  43. }
  44. /**
  45. * igc_init_rx_addrs - Initialize receive addresses
  46. * @hw: pointer to the HW structure
  47. * @rar_count: receive address registers
  48. *
  49. * Setup the receive address registers by setting the base receive address
  50. * register to the devices MAC address and clearing all the other receive
  51. * address registers to 0.
  52. */
  53. void igc_init_rx_addrs(struct igc_hw *hw, u16 rar_count)
  54. {
  55. u8 mac_addr[ETH_ALEN] = {0};
  56. u32 i;
  57. /* Setup the receive address */
  58. hw_dbg("Programming MAC Address into RAR[0]\n");
  59. hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
  60. /* Zero out the other (rar_entry_count - 1) receive addresses */
  61. hw_dbg("Clearing RAR[1-%u]\n", rar_count - 1);
  62. for (i = 1; i < rar_count; i++)
  63. hw->mac.ops.rar_set(hw, mac_addr, i);
  64. }
  65. /**
  66. * igc_setup_link - Setup flow control and link settings
  67. * @hw: pointer to the HW structure
  68. *
  69. * Determines which flow control settings to use, then configures flow
  70. * control. Calls the appropriate media-specific link configuration
  71. * function. Assuming the adapter has a valid link partner, a valid link
  72. * should be established. Assumes the hardware has previously been reset
  73. * and the transmitter and receiver are not enabled.
  74. */
  75. s32 igc_setup_link(struct igc_hw *hw)
  76. {
  77. s32 ret_val = 0;
  78. /* In the case of the phy reset being blocked, we already have a link.
  79. * We do not need to set it up again.
  80. */
  81. /* If requested flow control is set to default, set flow control
  82. * based on the EEPROM flow control settings.
  83. */
  84. if (hw->fc.requested_mode == igc_fc_default) {
  85. ret_val = igc_set_default_fc(hw);
  86. if (ret_val)
  87. goto out;
  88. }
  89. /* We want to save off the original Flow Control configuration just
  90. * in case we get disconnected and then reconnected into a different
  91. * hub or switch with different Flow Control capabilities.
  92. */
  93. hw->fc.current_mode = hw->fc.requested_mode;
  94. hw_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
  95. /* Call the necessary media_type subroutine to configure the link. */
  96. ret_val = hw->mac.ops.setup_physical_interface(hw);
  97. if (ret_val)
  98. goto out;
  99. /* Initialize the flow control address, type, and PAUSE timer
  100. * registers to their default values. This is done even if flow
  101. * control is disabled, because it does not hurt anything to
  102. * initialize these registers.
  103. */
  104. hw_dbg("Initializing the Flow Control address, type and timer regs\n");
  105. wr32(IGC_FCT, FLOW_CONTROL_TYPE);
  106. wr32(IGC_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
  107. wr32(IGC_FCAL, FLOW_CONTROL_ADDRESS_LOW);
  108. wr32(IGC_FCTTV, hw->fc.pause_time);
  109. ret_val = igc_set_fc_watermarks(hw);
  110. out:
  111. return ret_val;
  112. }
  113. /**
  114. * igc_set_default_fc - Set flow control default values
  115. * @hw: pointer to the HW structure
  116. *
  117. * Read the EEPROM for the default values for flow control and store the
  118. * values.
  119. */
  120. static s32 igc_set_default_fc(struct igc_hw *hw)
  121. {
  122. return 0;
  123. }
  124. /**
  125. * igc_set_fc_watermarks - Set flow control high/low watermarks
  126. * @hw: pointer to the HW structure
  127. *
  128. * Sets the flow control high/low threshold (watermark) registers. If
  129. * flow control XON frame transmission is enabled, then set XON frame
  130. * transmission as well.
  131. */
  132. static s32 igc_set_fc_watermarks(struct igc_hw *hw)
  133. {
  134. u32 fcrtl = 0, fcrth = 0;
  135. /* Set the flow control receive threshold registers. Normally,
  136. * these registers will be set to a default threshold that may be
  137. * adjusted later by the driver's runtime code. However, if the
  138. * ability to transmit pause frames is not enabled, then these
  139. * registers will be set to 0.
  140. */
  141. if (hw->fc.current_mode & igc_fc_tx_pause) {
  142. /* We need to set up the Receive Threshold high and low water
  143. * marks as well as (optionally) enabling the transmission of
  144. * XON frames.
  145. */
  146. fcrtl = hw->fc.low_water;
  147. if (hw->fc.send_xon)
  148. fcrtl |= IGC_FCRTL_XONE;
  149. fcrth = hw->fc.high_water;
  150. }
  151. wr32(IGC_FCRTL, fcrtl);
  152. wr32(IGC_FCRTH, fcrth);
  153. return 0;
  154. }
  155. /**
  156. * igc_clear_hw_cntrs_base - Clear base hardware counters
  157. * @hw: pointer to the HW structure
  158. *
  159. * Clears the base hardware counters by reading the counter registers.
  160. */
  161. void igc_clear_hw_cntrs_base(struct igc_hw *hw)
  162. {
  163. rd32(IGC_CRCERRS);
  164. rd32(IGC_SYMERRS);
  165. rd32(IGC_MPC);
  166. rd32(IGC_SCC);
  167. rd32(IGC_ECOL);
  168. rd32(IGC_MCC);
  169. rd32(IGC_LATECOL);
  170. rd32(IGC_COLC);
  171. rd32(IGC_DC);
  172. rd32(IGC_SEC);
  173. rd32(IGC_RLEC);
  174. rd32(IGC_XONRXC);
  175. rd32(IGC_XONTXC);
  176. rd32(IGC_XOFFRXC);
  177. rd32(IGC_XOFFTXC);
  178. rd32(IGC_FCRUC);
  179. rd32(IGC_GPRC);
  180. rd32(IGC_BPRC);
  181. rd32(IGC_MPRC);
  182. rd32(IGC_GPTC);
  183. rd32(IGC_GORCL);
  184. rd32(IGC_GORCH);
  185. rd32(IGC_GOTCL);
  186. rd32(IGC_GOTCH);
  187. rd32(IGC_RNBC);
  188. rd32(IGC_RUC);
  189. rd32(IGC_RFC);
  190. rd32(IGC_ROC);
  191. rd32(IGC_RJC);
  192. rd32(IGC_TORL);
  193. rd32(IGC_TORH);
  194. rd32(IGC_TOTL);
  195. rd32(IGC_TOTH);
  196. rd32(IGC_TPR);
  197. rd32(IGC_TPT);
  198. rd32(IGC_MPTC);
  199. rd32(IGC_BPTC);
  200. rd32(IGC_PRC64);
  201. rd32(IGC_PRC127);
  202. rd32(IGC_PRC255);
  203. rd32(IGC_PRC511);
  204. rd32(IGC_PRC1023);
  205. rd32(IGC_PRC1522);
  206. rd32(IGC_PTC64);
  207. rd32(IGC_PTC127);
  208. rd32(IGC_PTC255);
  209. rd32(IGC_PTC511);
  210. rd32(IGC_PTC1023);
  211. rd32(IGC_PTC1522);
  212. rd32(IGC_ALGNERRC);
  213. rd32(IGC_RXERRC);
  214. rd32(IGC_TNCRS);
  215. rd32(IGC_CEXTERR);
  216. rd32(IGC_TSCTC);
  217. rd32(IGC_TSCTFC);
  218. rd32(IGC_MGTPRC);
  219. rd32(IGC_MGTPDC);
  220. rd32(IGC_MGTPTC);
  221. rd32(IGC_IAC);
  222. rd32(IGC_ICRXOC);
  223. rd32(IGC_ICRXPTC);
  224. rd32(IGC_ICRXATC);
  225. rd32(IGC_ICTXPTC);
  226. rd32(IGC_ICTXATC);
  227. rd32(IGC_ICTXQEC);
  228. rd32(IGC_ICTXQMTC);
  229. rd32(IGC_ICRXDMTC);
  230. rd32(IGC_CBTMPC);
  231. rd32(IGC_HTDPMC);
  232. rd32(IGC_CBRMPC);
  233. rd32(IGC_RPTHC);
  234. rd32(IGC_HGPTC);
  235. rd32(IGC_HTCBDPC);
  236. rd32(IGC_HGORCL);
  237. rd32(IGC_HGORCH);
  238. rd32(IGC_HGOTCL);
  239. rd32(IGC_HGOTCH);
  240. rd32(IGC_LENERRS);
  241. }
  242. /**
  243. * igc_rar_set - Set receive address register
  244. * @hw: pointer to the HW structure
  245. * @addr: pointer to the receive address
  246. * @index: receive address array register
  247. *
  248. * Sets the receive address array register at index to the address passed
  249. * in by addr.
  250. */
  251. void igc_rar_set(struct igc_hw *hw, u8 *addr, u32 index)
  252. {
  253. u32 rar_low, rar_high;
  254. /* HW expects these in little endian so we reverse the byte order
  255. * from network order (big endian) to little endian
  256. */
  257. rar_low = ((u32)addr[0] |
  258. ((u32)addr[1] << 8) |
  259. ((u32)addr[2] << 16) | ((u32)addr[3] << 24));
  260. rar_high = ((u32)addr[4] | ((u32)addr[5] << 8));
  261. /* If MAC address zero, no need to set the AV bit */
  262. if (rar_low || rar_high)
  263. rar_high |= IGC_RAH_AV;
  264. /* Some bridges will combine consecutive 32-bit writes into
  265. * a single burst write, which will malfunction on some parts.
  266. * The flushes avoid this.
  267. */
  268. wr32(IGC_RAL(index), rar_low);
  269. wrfl();
  270. wr32(IGC_RAH(index), rar_high);
  271. wrfl();
  272. }
  273. /**
  274. * igc_check_for_copper_link - Check for link (Copper)
  275. * @hw: pointer to the HW structure
  276. *
  277. * Checks to see of the link status of the hardware has changed. If a
  278. * change in link status has been detected, then we read the PHY registers
  279. * to get the current speed/duplex if link exists.
  280. */
  281. s32 igc_check_for_copper_link(struct igc_hw *hw)
  282. {
  283. struct igc_mac_info *mac = &hw->mac;
  284. s32 ret_val;
  285. bool link;
  286. /* We only want to go out to the PHY registers to see if Auto-Neg
  287. * has completed and/or if our link status has changed. The
  288. * get_link_status flag is set upon receiving a Link Status
  289. * Change or Rx Sequence Error interrupt.
  290. */
  291. if (!mac->get_link_status) {
  292. ret_val = 0;
  293. goto out;
  294. }
  295. /* First we want to see if the MII Status Register reports
  296. * link. If so, then we want to get the current speed/duplex
  297. * of the PHY.
  298. */
  299. if (ret_val)
  300. goto out;
  301. if (!link)
  302. goto out; /* No link detected */
  303. mac->get_link_status = false;
  304. /* Check if there was DownShift, must be checked
  305. * immediately after link-up
  306. */
  307. /* If we are forcing speed/duplex, then we simply return since
  308. * we have already determined whether we have link or not.
  309. */
  310. if (!mac->autoneg) {
  311. ret_val = -IGC_ERR_CONFIG;
  312. goto out;
  313. }
  314. /* Auto-Neg is enabled. Auto Speed Detection takes care
  315. * of MAC speed/duplex configuration. So we only need to
  316. * configure Collision Distance in the MAC.
  317. */
  318. igc_config_collision_dist(hw);
  319. /* Configure Flow Control now that Auto-Neg has completed.
  320. * First, we need to restore the desired flow control
  321. * settings because we may have had to re-autoneg with a
  322. * different link partner.
  323. */
  324. if (ret_val)
  325. hw_dbg("Error configuring flow control\n");
  326. out:
  327. return ret_val;
  328. }
  329. /**
  330. * igc_config_collision_dist - Configure collision distance
  331. * @hw: pointer to the HW structure
  332. *
  333. * Configures the collision distance to the default value and is used
  334. * during link setup. Currently no func pointer exists and all
  335. * implementations are handled in the generic version of this function.
  336. */
  337. void igc_config_collision_dist(struct igc_hw *hw)
  338. {
  339. u32 tctl;
  340. tctl = rd32(IGC_TCTL);
  341. tctl &= ~IGC_TCTL_COLD;
  342. tctl |= IGC_COLLISION_DISTANCE << IGC_COLD_SHIFT;
  343. wr32(IGC_TCTL, tctl);
  344. wrfl();
  345. }
  346. /**
  347. * igc_get_auto_rd_done - Check for auto read completion
  348. * @hw: pointer to the HW structure
  349. *
  350. * Check EEPROM for Auto Read done bit.
  351. */
  352. s32 igc_get_auto_rd_done(struct igc_hw *hw)
  353. {
  354. s32 ret_val = 0;
  355. s32 i = 0;
  356. while (i < AUTO_READ_DONE_TIMEOUT) {
  357. if (rd32(IGC_EECD) & IGC_EECD_AUTO_RD)
  358. break;
  359. usleep_range(1000, 2000);
  360. i++;
  361. }
  362. if (i == AUTO_READ_DONE_TIMEOUT) {
  363. hw_dbg("Auto read by HW from NVM has not completed.\n");
  364. ret_val = -IGC_ERR_RESET;
  365. goto out;
  366. }
  367. out:
  368. return ret_val;
  369. }
  370. /**
  371. * igc_get_speed_and_duplex_copper - Retrieve current speed/duplex
  372. * @hw: pointer to the HW structure
  373. * @speed: stores the current speed
  374. * @duplex: stores the current duplex
  375. *
  376. * Read the status register for the current speed/duplex and store the current
  377. * speed and duplex for copper connections.
  378. */
  379. s32 igc_get_speed_and_duplex_copper(struct igc_hw *hw, u16 *speed,
  380. u16 *duplex)
  381. {
  382. u32 status;
  383. status = rd32(IGC_STATUS);
  384. if (status & IGC_STATUS_SPEED_1000) {
  385. /* For I225, STATUS will indicate 1G speed in both 1 Gbps
  386. * and 2.5 Gbps link modes. An additional bit is used
  387. * to differentiate between 1 Gbps and 2.5 Gbps.
  388. */
  389. if (hw->mac.type == igc_i225 &&
  390. (status & IGC_STATUS_SPEED_2500)) {
  391. *speed = SPEED_2500;
  392. hw_dbg("2500 Mbs, ");
  393. } else {
  394. *speed = SPEED_1000;
  395. hw_dbg("1000 Mbs, ");
  396. }
  397. } else if (status & IGC_STATUS_SPEED_100) {
  398. *speed = SPEED_100;
  399. hw_dbg("100 Mbs, ");
  400. } else {
  401. *speed = SPEED_10;
  402. hw_dbg("10 Mbs, ");
  403. }
  404. if (status & IGC_STATUS_FD) {
  405. *duplex = FULL_DUPLEX;
  406. hw_dbg("Full Duplex\n");
  407. } else {
  408. *duplex = HALF_DUPLEX;
  409. hw_dbg("Half Duplex\n");
  410. }
  411. return 0;
  412. }
  413. /**
  414. * igc_put_hw_semaphore - Release hardware semaphore
  415. * @hw: pointer to the HW structure
  416. *
  417. * Release hardware semaphore used to access the PHY or NVM
  418. */
  419. void igc_put_hw_semaphore(struct igc_hw *hw)
  420. {
  421. u32 swsm;
  422. swsm = rd32(IGC_SWSM);
  423. swsm &= ~(IGC_SWSM_SMBI | IGC_SWSM_SWESMBI);
  424. wr32(IGC_SWSM, swsm);
  425. }